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/rwlock.h>
62 #include <sys/sysctl.h>
63 #include <sys/vmmeter.h>
64 #include <sys/vnode.h>
65 #include <geom/geom.h>
67 #include <vm/vm_param.h>
68 #include <vm/vm_kern.h>
69 #include <vm/vm_pageout.h>
70 #include <vm/vm_page.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_map.h>
74 #include "opt_compat.h"
75 #include "opt_directio.h"
78 static MALLOC_DEFINE(M_BIOBUF, "biobuf", "BIO buffer");
80 struct bio_ops bioops; /* I/O operation notification */
82 struct buf_ops buf_ops_bio = {
83 .bop_name = "buf_ops_bio",
84 .bop_write = bufwrite,
85 .bop_strategy = bufstrategy,
87 .bop_bdflush = bufbdflush,
91 * XXX buf is global because kern_shutdown.c and ffs_checkoverlap has
92 * carnal knowledge of buffers. This knowledge should be moved to vfs_bio.c.
94 struct buf *buf; /* buffer header pool */
96 static struct proc *bufdaemonproc;
98 static int inmem(struct vnode *vp, daddr_t blkno);
99 static void vm_hold_free_pages(struct buf *bp, int newbsize);
100 static void vm_hold_load_pages(struct buf *bp, vm_offset_t from,
102 static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m);
103 static void vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off,
105 static void vfs_drain_busy_pages(struct buf *bp);
106 static void vfs_clean_pages_dirty_buf(struct buf *bp);
107 static void vfs_setdirty_locked_object(struct buf *bp);
108 static void vfs_vmio_release(struct buf *bp);
109 static int vfs_bio_clcheck(struct vnode *vp, int size,
110 daddr_t lblkno, daddr_t blkno);
111 static int buf_do_flush(struct vnode *vp);
112 static int flushbufqueues(struct vnode *, int, int);
113 static void buf_daemon(void);
114 static void bremfreel(struct buf *bp);
115 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
116 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
117 static int sysctl_bufspace(SYSCTL_HANDLER_ARGS);
120 int vmiodirenable = TRUE;
121 SYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW, &vmiodirenable, 0,
122 "Use the VM system for directory writes");
123 long runningbufspace;
124 SYSCTL_LONG(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0,
125 "Amount of presently outstanding async buffer io");
126 static long bufspace;
127 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
128 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
129 SYSCTL_PROC(_vfs, OID_AUTO, bufspace, CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RD,
130 &bufspace, 0, sysctl_bufspace, "L", "Virtual memory used for buffers");
132 SYSCTL_LONG(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0,
133 "Virtual memory used for buffers");
135 static long maxbufspace;
136 SYSCTL_LONG(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0,
137 "Maximum allowed value of bufspace (including buf_daemon)");
138 static long bufmallocspace;
139 SYSCTL_LONG(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0,
140 "Amount of malloced memory for buffers");
141 static long maxbufmallocspace;
142 SYSCTL_LONG(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace, 0,
143 "Maximum amount of malloced memory for buffers");
144 static long lobufspace;
145 SYSCTL_LONG(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0,
146 "Minimum amount of buffers we want to have");
148 SYSCTL_LONG(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0,
149 "Maximum allowed value of bufspace (excluding buf_daemon)");
150 static int bufreusecnt;
151 SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RW, &bufreusecnt, 0,
152 "Number of times we have reused a buffer");
153 static int buffreekvacnt;
154 SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt, 0,
155 "Number of times we have freed the KVA space from some buffer");
156 static int bufdefragcnt;
157 SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt, 0,
158 "Number of times we have had to repeat buffer allocation to defragment");
159 static long lorunningspace;
160 SYSCTL_LONG(_vfs, OID_AUTO, lorunningspace, CTLFLAG_RW, &lorunningspace, 0,
161 "Minimum preferred space used for in-progress I/O");
162 static long hirunningspace;
163 SYSCTL_LONG(_vfs, OID_AUTO, hirunningspace, CTLFLAG_RW, &hirunningspace, 0,
164 "Maximum amount of space to use for in-progress I/O");
165 int dirtybufferflushes;
166 SYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes,
167 0, "Number of bdwrite to bawrite conversions to limit dirty buffers");
169 SYSCTL_INT(_vfs, OID_AUTO, bdwriteskip, CTLFLAG_RW, &bdwriteskip,
170 0, "Number of buffers supplied to bdwrite with snapshot deadlock risk");
171 int altbufferflushes;
172 SYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes,
173 0, "Number of fsync flushes to limit dirty buffers");
174 static int recursiveflushes;
175 SYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes,
176 0, "Number of flushes skipped due to being recursive");
177 static int numdirtybuffers;
178 SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0,
179 "Number of buffers that are dirty (has unwritten changes) at the moment");
180 static int lodirtybuffers;
181 SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0,
182 "How many buffers we want to have free before bufdaemon can sleep");
183 static int hidirtybuffers;
184 SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0,
185 "When the number of dirty buffers is considered severe");
187 SYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh,
188 0, "Number of bdwrite to bawrite conversions to clear dirty buffers");
189 static int numfreebuffers;
190 SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0,
191 "Number of free buffers");
192 static int lofreebuffers;
193 SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0,
195 static int hifreebuffers;
196 SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0,
197 "XXX Complicatedly unused");
198 static int getnewbufcalls;
199 SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW, &getnewbufcalls, 0,
200 "Number of calls to getnewbuf");
201 static int getnewbufrestarts;
202 SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW, &getnewbufrestarts, 0,
203 "Number of times getnewbuf has had to restart a buffer aquisition");
204 static int flushbufqtarget = 100;
205 SYSCTL_INT(_vfs, OID_AUTO, flushbufqtarget, CTLFLAG_RW, &flushbufqtarget, 0,
206 "Amount of work to do in flushbufqueues when helping bufdaemon");
207 static long notbufdflashes;
208 SYSCTL_LONG(_vfs, OID_AUTO, notbufdflashes, CTLFLAG_RD, ¬bufdflashes, 0,
209 "Number of dirty buffer flushes done by the bufdaemon helpers");
210 static long barrierwrites;
211 SYSCTL_LONG(_vfs, OID_AUTO, barrierwrites, CTLFLAG_RW, &barrierwrites, 0,
212 "Number of barrier writes");
215 * Wakeup point for bufdaemon, as well as indicator of whether it is already
216 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it
219 static int bd_request;
222 * Request for the buf daemon to write more buffers than is indicated by
223 * lodirtybuf. This may be necessary to push out excess dependencies or
224 * defragment the address space where a simple count of the number of dirty
225 * buffers is insufficient to characterize the demand for flushing them.
227 static int bd_speedupreq;
230 * This lock synchronizes access to bd_request.
232 static struct mtx bdlock;
235 * bogus page -- for I/O to/from partially complete buffers
236 * this is a temporary solution to the problem, but it is not
237 * really that bad. it would be better to split the buffer
238 * for input in the case of buffers partially already in memory,
239 * but the code is intricate enough already.
241 vm_page_t bogus_page;
244 * Synchronization (sleep/wakeup) variable for active buffer space requests.
245 * Set when wait starts, cleared prior to wakeup().
246 * Used in runningbufwakeup() and waitrunningbufspace().
248 static int runningbufreq;
251 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
252 * waitrunningbufspace().
254 static struct mtx rbreqlock;
257 * Synchronization (sleep/wakeup) variable for buffer requests.
258 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done
260 * Used in numdirtywakeup(), bufspacewakeup(), bufcountwakeup(), bwillwrite(),
261 * getnewbuf(), and getblk().
263 static int needsbuffer;
266 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it.
268 static struct mtx nblock;
271 * Definitions for the buffer free lists.
273 #define BUFFER_QUEUES 5 /* number of free buffer queues */
275 #define QUEUE_NONE 0 /* on no queue */
276 #define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */
277 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
278 #define QUEUE_EMPTYKVA 3 /* empty buffer headers w/KVA assignment */
279 #define QUEUE_EMPTY 4 /* empty buffer headers */
280 #define QUEUE_SENTINEL 1024 /* not an queue index, but mark for sentinel */
282 /* Queues for free buffers with various properties */
283 static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } };
285 /* Lock for the bufqueues */
286 static struct mtx bqlock;
289 * Single global constant for BUF_WMESG, to avoid getting multiple references.
290 * buf_wmesg is referred from macros.
292 const char *buf_wmesg = BUF_WMESG;
294 #define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */
295 #define VFS_BIO_NEED_DIRTYFLUSH 0x02 /* waiting for dirty buffer flush */
296 #define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */
297 #define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */
299 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
300 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
302 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
307 if (sizeof(int) == sizeof(long) || req->oldlen >= sizeof(long))
308 return (sysctl_handle_long(oidp, arg1, arg2, req));
309 lvalue = *(long *)arg1;
310 if (lvalue > INT_MAX)
311 /* On overflow, still write out a long to trigger ENOMEM. */
312 return (sysctl_handle_long(oidp, &lvalue, 0, req));
314 return (sysctl_handle_int(oidp, &ivalue, 0, req));
319 extern void ffs_rawread_setup(void);
320 #endif /* DIRECTIO */
324 * If someone is blocked due to there being too many dirty buffers,
325 * and numdirtybuffers is now reasonable, wake them up.
329 numdirtywakeup(int level)
332 if (numdirtybuffers <= level) {
334 if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) {
335 needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH;
336 wakeup(&needsbuffer);
345 * Called when buffer space is potentially available for recovery.
346 * getnewbuf() will block on this flag when it is unable to free
347 * sufficient buffer space. Buffer space becomes recoverable when
348 * bp's get placed back in the queues.
356 * If someone is waiting for BUF space, wake them up. Even
357 * though we haven't freed the kva space yet, the waiting
358 * process will be able to now.
361 if (needsbuffer & VFS_BIO_NEED_BUFSPACE) {
362 needsbuffer &= ~VFS_BIO_NEED_BUFSPACE;
363 wakeup(&needsbuffer);
369 * runningbufwakeup() - in-progress I/O accounting.
373 runningbufwakeup(struct buf *bp)
376 if (bp->b_runningbufspace) {
377 atomic_subtract_long(&runningbufspace, bp->b_runningbufspace);
378 bp->b_runningbufspace = 0;
379 mtx_lock(&rbreqlock);
380 if (runningbufreq && runningbufspace <= lorunningspace) {
382 wakeup(&runningbufreq);
384 mtx_unlock(&rbreqlock);
391 * Called when a buffer has been added to one of the free queues to
392 * account for the buffer and to wakeup anyone waiting for free buffers.
393 * This typically occurs when large amounts of metadata are being handled
394 * by the buffer cache ( else buffer space runs out first, usually ).
398 bufcountwakeup(struct buf *bp)
402 KASSERT((bp->b_vflags & BV_INFREECNT) == 0,
403 ("buf %p already counted as free", bp));
404 if (bp->b_bufobj != NULL)
405 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
406 bp->b_vflags |= BV_INFREECNT;
407 old = atomic_fetchadd_int(&numfreebuffers, 1);
408 KASSERT(old >= 0 && old < nbuf,
409 ("numfreebuffers climbed to %d", old + 1));
412 needsbuffer &= ~VFS_BIO_NEED_ANY;
413 if (numfreebuffers >= hifreebuffers)
414 needsbuffer &= ~VFS_BIO_NEED_FREE;
415 wakeup(&needsbuffer);
421 * waitrunningbufspace()
423 * runningbufspace is a measure of the amount of I/O currently
424 * running. This routine is used in async-write situations to
425 * prevent creating huge backups of pending writes to a device.
426 * Only asynchronous writes are governed by this function.
428 * Reads will adjust runningbufspace, but will not block based on it.
429 * The read load has a side effect of reducing the allowed write load.
431 * This does NOT turn an async write into a sync write. It waits
432 * for earlier writes to complete and generally returns before the
433 * caller's write has reached the device.
436 waitrunningbufspace(void)
439 mtx_lock(&rbreqlock);
440 while (runningbufspace > hirunningspace) {
442 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
444 mtx_unlock(&rbreqlock);
449 * vfs_buf_test_cache:
451 * Called when a buffer is extended. This function clears the B_CACHE
452 * bit if the newly extended portion of the buffer does not contain
457 vfs_buf_test_cache(struct buf *bp,
458 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
462 VM_OBJECT_ASSERT_WLOCKED(m->object);
463 if (bp->b_flags & B_CACHE) {
464 int base = (foff + off) & PAGE_MASK;
465 if (vm_page_is_valid(m, base, size) == 0)
466 bp->b_flags &= ~B_CACHE;
470 /* Wake up the buffer daemon if necessary */
473 bd_wakeup(int dirtybuflevel)
477 if (bd_request == 0 && numdirtybuffers >= dirtybuflevel) {
485 * bd_speedup - speedup the buffer cache flushing code
495 if (bd_speedupreq == 0 || bd_request == 0)
505 * Calculating buffer cache scaling values and reserve space for buffer
506 * headers. This is called during low level kernel initialization and
507 * may be called more then once. We CANNOT write to the memory area
508 * being reserved at this time.
511 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
517 * physmem_est is in pages. Convert it to kilobytes (assumes
518 * PAGE_SIZE is >= 1K)
520 physmem_est = physmem_est * (PAGE_SIZE / 1024);
523 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
524 * For the first 64MB of ram nominally allocate sufficient buffers to
525 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
526 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing
527 * the buffer cache we limit the eventual kva reservation to
530 * factor represents the 1/4 x ram conversion.
533 int factor = 4 * BKVASIZE / 1024;
536 if (physmem_est > 4096)
537 nbuf += min((physmem_est - 4096) / factor,
539 if (physmem_est > 65536)
540 nbuf += (physmem_est - 65536) * 2 / (factor * 5);
542 if (maxbcache && nbuf > maxbcache / BKVASIZE)
543 nbuf = maxbcache / BKVASIZE;
548 /* XXX Avoid unsigned long overflows later on with maxbufspace. */
549 maxbuf = (LONG_MAX / 3) / BKVASIZE;
552 printf("Warning: nbufs lowered from %d to %ld\n", nbuf,
558 * swbufs are used as temporary holders for I/O, such as paging I/O.
559 * We have no less then 16 and no more then 256.
561 nswbuf = max(min(nbuf/4, 256), 16);
563 if (nswbuf < NSWBUF_MIN)
571 * Reserve space for the buffer cache buffers
574 v = (caddr_t)(swbuf + nswbuf);
576 v = (caddr_t)(buf + nbuf);
581 /* Initialize the buffer subsystem. Called before use of any buffers. */
588 mtx_init(&bqlock, "buf queue lock", NULL, MTX_DEF);
589 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
590 mtx_init(&nblock, "needsbuffer lock", NULL, MTX_DEF);
591 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
593 /* next, make a null set of free lists */
594 for (i = 0; i < BUFFER_QUEUES; i++)
595 TAILQ_INIT(&bufqueues[i]);
597 /* finally, initialize each buffer header and stick on empty q */
598 for (i = 0; i < nbuf; i++) {
600 bzero(bp, sizeof *bp);
601 bp->b_flags = B_INVAL; /* we're just an empty header */
602 bp->b_rcred = NOCRED;
603 bp->b_wcred = NOCRED;
604 bp->b_qindex = QUEUE_EMPTY;
605 bp->b_vflags = BV_INFREECNT; /* buf is counted as free */
607 LIST_INIT(&bp->b_dep);
609 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
613 * maxbufspace is the absolute maximum amount of buffer space we are
614 * allowed to reserve in KVM and in real terms. The absolute maximum
615 * is nominally used by buf_daemon. hibufspace is the nominal maximum
616 * used by most other processes. The differential is required to
617 * ensure that buf_daemon is able to run when other processes might
618 * be blocked waiting for buffer space.
620 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
621 * this may result in KVM fragmentation which is not handled optimally
624 maxbufspace = (long)nbuf * BKVASIZE;
625 hibufspace = lmax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10);
626 lobufspace = hibufspace - MAXBSIZE;
629 * Note: The 16 MiB upper limit for hirunningspace was chosen
630 * arbitrarily and may need further tuning. It corresponds to
631 * 128 outstanding write IO requests (if IO size is 128 KiB),
632 * which fits with many RAID controllers' tagged queuing limits.
633 * The lower 1 MiB limit is the historical upper limit for
636 hirunningspace = lmax(lmin(roundup(hibufspace / 64, MAXBSIZE),
637 16 * 1024 * 1024), 1024 * 1024);
638 lorunningspace = roundup((hirunningspace * 2) / 3, MAXBSIZE);
641 * Limit the amount of malloc memory since it is wired permanently into
642 * the kernel space. Even though this is accounted for in the buffer
643 * allocation, we don't want the malloced region to grow uncontrolled.
644 * The malloc scheme improves memory utilization significantly on average
645 * (small) directories.
647 maxbufmallocspace = hibufspace / 20;
650 * Reduce the chance of a deadlock occuring by limiting the number
651 * of delayed-write dirty buffers we allow to stack up.
653 hidirtybuffers = nbuf / 4 + 20;
654 dirtybufthresh = hidirtybuffers * 9 / 10;
657 * To support extreme low-memory systems, make sure hidirtybuffers cannot
658 * eat up all available buffer space. This occurs when our minimum cannot
659 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming
660 * BKVASIZE'd buffers.
662 while ((long)hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
663 hidirtybuffers >>= 1;
665 lodirtybuffers = hidirtybuffers / 2;
668 * Try to keep the number of free buffers in the specified range,
669 * and give special processes (e.g. like buf_daemon) access to an
672 lofreebuffers = nbuf / 18 + 5;
673 hifreebuffers = 2 * lofreebuffers;
674 numfreebuffers = nbuf;
676 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
677 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
681 * bfreekva() - free the kva allocation for a buffer.
683 * Since this call frees up buffer space, we call bufspacewakeup().
686 bfreekva(struct buf *bp)
690 atomic_add_int(&buffreekvacnt, 1);
691 atomic_subtract_long(&bufspace, bp->b_kvasize);
692 vm_map_remove(buffer_map, (vm_offset_t) bp->b_kvabase,
693 (vm_offset_t) bp->b_kvabase + bp->b_kvasize);
702 * Mark the buffer for removal from the appropriate free list in brelse.
706 bremfree(struct buf *bp)
710 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
711 KASSERT((bp->b_flags & B_REMFREE) == 0,
712 ("bremfree: buffer %p already marked for delayed removal.", bp));
713 KASSERT(bp->b_qindex != QUEUE_NONE,
714 ("bremfree: buffer %p not on a queue.", bp));
717 bp->b_flags |= B_REMFREE;
718 /* Fixup numfreebuffers count. */
719 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
720 KASSERT((bp->b_vflags & BV_INFREECNT) != 0,
721 ("buf %p not counted in numfreebuffers", bp));
722 if (bp->b_bufobj != NULL)
723 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
724 bp->b_vflags &= ~BV_INFREECNT;
725 old = atomic_fetchadd_int(&numfreebuffers, -1);
726 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
733 * Force an immediate removal from a free list. Used only in nfs when
734 * it abuses the b_freelist pointer.
737 bremfreef(struct buf *bp)
747 * Removes a buffer from the free list, must be called with the
751 bremfreel(struct buf *bp)
755 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X",
756 bp, bp->b_vp, bp->b_flags);
757 KASSERT(bp->b_qindex != QUEUE_NONE,
758 ("bremfreel: buffer %p not on a queue.", bp));
760 mtx_assert(&bqlock, MA_OWNED);
762 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
763 bp->b_qindex = QUEUE_NONE;
765 * If this was a delayed bremfree() we only need to remove the buffer
766 * from the queue and return the stats are already done.
768 if (bp->b_flags & B_REMFREE) {
769 bp->b_flags &= ~B_REMFREE;
773 * Fixup numfreebuffers count. If the buffer is invalid or not
774 * delayed-write, the buffer was free and we must decrement
777 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
778 KASSERT((bp->b_vflags & BV_INFREECNT) != 0,
779 ("buf %p not counted in numfreebuffers", bp));
780 if (bp->b_bufobj != NULL)
781 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
782 bp->b_vflags &= ~BV_INFREECNT;
783 old = atomic_fetchadd_int(&numfreebuffers, -1);
784 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
789 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must
790 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set,
791 * the buffer is valid and we do not have to do anything.
794 breada(struct vnode * vp, daddr_t * rablkno, int * rabsize,
795 int cnt, struct ucred * cred)
800 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
801 if (inmem(vp, *rablkno))
803 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
805 if ((rabp->b_flags & B_CACHE) == 0) {
806 if (!TD_IS_IDLETHREAD(curthread))
807 curthread->td_ru.ru_inblock++;
808 rabp->b_flags |= B_ASYNC;
809 rabp->b_flags &= ~B_INVAL;
810 rabp->b_ioflags &= ~BIO_ERROR;
811 rabp->b_iocmd = BIO_READ;
812 if (rabp->b_rcred == NOCRED && cred != NOCRED)
813 rabp->b_rcred = crhold(cred);
814 vfs_busy_pages(rabp, 0);
816 rabp->b_iooffset = dbtob(rabp->b_blkno);
825 * Entry point for bread() and breadn() via #defines in sys/buf.h.
827 * Get a buffer with the specified data. Look in the cache first. We
828 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
829 * is set, the buffer is valid and we do not have to do anything, see
830 * getblk(). Also starts asynchronous I/O on read-ahead blocks.
833 breadn_flags(struct vnode * vp, daddr_t blkno, int size,
834 daddr_t * rablkno, int *rabsize, int cnt,
835 struct ucred * cred, int flags, struct buf **bpp)
838 int rv = 0, readwait = 0;
840 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
842 * Can only return NULL if GB_LOCK_NOWAIT flag is specified.
844 *bpp = bp = getblk(vp, blkno, size, 0, 0, flags);
848 /* if not found in cache, do some I/O */
849 if ((bp->b_flags & B_CACHE) == 0) {
850 if (!TD_IS_IDLETHREAD(curthread))
851 curthread->td_ru.ru_inblock++;
852 bp->b_iocmd = BIO_READ;
853 bp->b_flags &= ~B_INVAL;
854 bp->b_ioflags &= ~BIO_ERROR;
855 if (bp->b_rcred == NOCRED && cred != NOCRED)
856 bp->b_rcred = crhold(cred);
857 vfs_busy_pages(bp, 0);
858 bp->b_iooffset = dbtob(bp->b_blkno);
863 breada(vp, rablkno, rabsize, cnt, cred);
872 * Write, release buffer on completion. (Done by iodone
873 * if async). Do not bother writing anything if the buffer
876 * Note that we set B_CACHE here, indicating that buffer is
877 * fully valid and thus cacheable. This is true even of NFS
878 * now so we set it generally. This could be set either here
879 * or in biodone() since the I/O is synchronous. We put it
883 bufwrite(struct buf *bp)
889 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
890 if (bp->b_flags & B_INVAL) {
895 if (bp->b_flags & B_BARRIER)
898 oldflags = bp->b_flags;
902 if (bp->b_pin_count > 0)
905 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
906 ("FFS background buffer should not get here %p", bp));
910 vp_md = vp->v_vflag & VV_MD;
914 /* Mark the buffer clean */
917 bp->b_flags &= ~B_DONE;
918 bp->b_ioflags &= ~BIO_ERROR;
919 bp->b_flags |= B_CACHE;
920 bp->b_iocmd = BIO_WRITE;
922 bufobj_wref(bp->b_bufobj);
923 vfs_busy_pages(bp, 1);
926 * Normal bwrites pipeline writes
928 bp->b_runningbufspace = bp->b_bufsize;
929 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
931 if (!TD_IS_IDLETHREAD(curthread))
932 curthread->td_ru.ru_oublock++;
933 if (oldflags & B_ASYNC)
935 bp->b_iooffset = dbtob(bp->b_blkno);
938 if ((oldflags & B_ASYNC) == 0) {
939 int rtval = bufwait(bp);
944 * don't allow the async write to saturate the I/O
945 * system. We will not deadlock here because
946 * we are blocking waiting for I/O that is already in-progress
947 * to complete. We do not block here if it is the update
948 * or syncer daemon trying to clean up as that can lead
951 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md)
952 waitrunningbufspace();
959 bufbdflush(struct bufobj *bo, struct buf *bp)
963 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
964 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread);
966 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
969 * Try to find a buffer to flush.
971 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
972 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
974 LK_EXCLUSIVE | LK_NOWAIT, NULL))
977 panic("bdwrite: found ourselves");
979 /* Don't countdeps with the bo lock held. */
980 if (buf_countdeps(nbp, 0)) {
985 if (nbp->b_flags & B_CLUSTEROK) {
991 dirtybufferflushes++;
1000 * Delayed write. (Buffer is marked dirty). Do not bother writing
1001 * anything if the buffer is marked invalid.
1003 * Note that since the buffer must be completely valid, we can safely
1004 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
1005 * biodone() in order to prevent getblk from writing the buffer
1006 * out synchronously.
1009 bdwrite(struct buf *bp)
1011 struct thread *td = curthread;
1015 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1016 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1017 KASSERT((bp->b_flags & B_BARRIER) == 0,
1018 ("Barrier request in delayed write %p", bp));
1019 BUF_ASSERT_HELD(bp);
1021 if (bp->b_flags & B_INVAL) {
1027 * If we have too many dirty buffers, don't create any more.
1028 * If we are wildly over our limit, then force a complete
1029 * cleanup. Otherwise, just keep the situation from getting
1030 * out of control. Note that we have to avoid a recursive
1031 * disaster and not try to clean up after our own cleanup!
1035 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) {
1036 td->td_pflags |= TDP_INBDFLUSH;
1038 td->td_pflags &= ~TDP_INBDFLUSH;
1044 * Set B_CACHE, indicating that the buffer is fully valid. This is
1045 * true even of NFS now.
1047 bp->b_flags |= B_CACHE;
1050 * This bmap keeps the system from needing to do the bmap later,
1051 * perhaps when the system is attempting to do a sync. Since it
1052 * is likely that the indirect block -- or whatever other datastructure
1053 * that the filesystem needs is still in memory now, it is a good
1054 * thing to do this. Note also, that if the pageout daemon is
1055 * requesting a sync -- there might not be enough memory to do
1056 * the bmap then... So, this is important to do.
1058 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
1059 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1063 * Set the *dirty* buffer range based upon the VM system dirty
1066 * Mark the buffer pages as clean. We need to do this here to
1067 * satisfy the vnode_pager and the pageout daemon, so that it
1068 * thinks that the pages have been "cleaned". Note that since
1069 * the pages are in a delayed write buffer -- the VFS layer
1070 * "will" see that the pages get written out on the next sync,
1071 * or perhaps the cluster will be completed.
1073 vfs_clean_pages_dirty_buf(bp);
1077 * Wakeup the buffer flushing daemon if we have a lot of dirty
1078 * buffers (midpoint between our recovery point and our stall
1081 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1084 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
1085 * due to the softdep code.
1092 * Turn buffer into delayed write request. We must clear BIO_READ and
1093 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
1094 * itself to properly update it in the dirty/clean lists. We mark it
1095 * B_DONE to ensure that any asynchronization of the buffer properly
1096 * clears B_DONE ( else a panic will occur later ).
1098 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
1099 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
1100 * should only be called if the buffer is known-good.
1102 * Since the buffer is not on a queue, we do not update the numfreebuffers
1105 * The buffer must be on QUEUE_NONE.
1108 bdirty(struct buf *bp)
1111 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
1112 bp, bp->b_vp, bp->b_flags);
1113 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1114 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1115 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
1116 BUF_ASSERT_HELD(bp);
1117 bp->b_flags &= ~(B_RELBUF);
1118 bp->b_iocmd = BIO_WRITE;
1120 if ((bp->b_flags & B_DELWRI) == 0) {
1121 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
1123 atomic_add_int(&numdirtybuffers, 1);
1124 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1131 * Clear B_DELWRI for buffer.
1133 * Since the buffer is not on a queue, we do not update the numfreebuffers
1136 * The buffer must be on QUEUE_NONE.
1140 bundirty(struct buf *bp)
1143 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1144 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1145 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1146 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
1147 BUF_ASSERT_HELD(bp);
1149 if (bp->b_flags & B_DELWRI) {
1150 bp->b_flags &= ~B_DELWRI;
1152 atomic_subtract_int(&numdirtybuffers, 1);
1153 numdirtywakeup(lodirtybuffers);
1156 * Since it is now being written, we can clear its deferred write flag.
1158 bp->b_flags &= ~B_DEFERRED;
1164 * Asynchronous write. Start output on a buffer, but do not wait for
1165 * it to complete. The buffer is released when the output completes.
1167 * bwrite() ( or the VOP routine anyway ) is responsible for handling
1168 * B_INVAL buffers. Not us.
1171 bawrite(struct buf *bp)
1174 bp->b_flags |= B_ASYNC;
1181 * Asynchronous barrier write. Start output on a buffer, but do not
1182 * wait for it to complete. Place a write barrier after this write so
1183 * that this buffer and all buffers written before it are committed to
1184 * the disk before any buffers written after this write are committed
1185 * to the disk. The buffer is released when the output completes.
1188 babarrierwrite(struct buf *bp)
1191 bp->b_flags |= B_ASYNC | B_BARRIER;
1198 * Synchronous barrier write. Start output on a buffer and wait for
1199 * it to complete. Place a write barrier after this write so that
1200 * this buffer and all buffers written before it are committed to
1201 * the disk before any buffers written after this write are committed
1202 * to the disk. The buffer is released when the output completes.
1205 bbarrierwrite(struct buf *bp)
1208 bp->b_flags |= B_BARRIER;
1209 return (bwrite(bp));
1215 * Called prior to the locking of any vnodes when we are expecting to
1216 * write. We do not want to starve the buffer cache with too many
1217 * dirty buffers so we block here. By blocking prior to the locking
1218 * of any vnodes we attempt to avoid the situation where a locked vnode
1219 * prevents the various system daemons from flushing related buffers.
1226 if (numdirtybuffers >= hidirtybuffers) {
1228 while (numdirtybuffers >= hidirtybuffers) {
1230 needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH;
1231 msleep(&needsbuffer, &nblock,
1232 (PRIBIO + 4), "flswai", 0);
1234 mtx_unlock(&nblock);
1239 * Return true if we have too many dirty buffers.
1242 buf_dirty_count_severe(void)
1245 return(numdirtybuffers >= hidirtybuffers);
1248 static __noinline int
1249 buf_vm_page_count_severe(void)
1252 KFAIL_POINT_CODE(DEBUG_FP, buf_pressure, return 1);
1254 return vm_page_count_severe();
1260 * Release a busy buffer and, if requested, free its resources. The
1261 * buffer will be stashed in the appropriate bufqueue[] allowing it
1262 * to be accessed later as a cache entity or reused for other purposes.
1265 brelse(struct buf *bp)
1267 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
1268 bp, bp->b_vp, bp->b_flags);
1269 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1270 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1272 if (BUF_LOCKRECURSED(bp)) {
1274 * Do not process, in particular, do not handle the
1275 * B_INVAL/B_RELBUF and do not release to free list.
1281 if (bp->b_flags & B_MANAGED) {
1286 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) &&
1287 bp->b_error == EIO && !(bp->b_flags & B_INVAL)) {
1289 * Failed write, redirty. Must clear BIO_ERROR to prevent
1290 * pages from being scrapped. If the error is anything
1291 * other than an I/O error (EIO), assume that retrying
1294 bp->b_ioflags &= ~BIO_ERROR;
1296 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
1297 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
1299 * Either a failed I/O or we were asked to free or not
1302 bp->b_flags |= B_INVAL;
1303 if (!LIST_EMPTY(&bp->b_dep))
1305 if (bp->b_flags & B_DELWRI) {
1306 atomic_subtract_int(&numdirtybuffers, 1);
1307 numdirtywakeup(lodirtybuffers);
1309 bp->b_flags &= ~(B_DELWRI | B_CACHE);
1310 if ((bp->b_flags & B_VMIO) == 0) {
1319 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release()
1320 * is called with B_DELWRI set, the underlying pages may wind up
1321 * getting freed causing a previous write (bdwrite()) to get 'lost'
1322 * because pages associated with a B_DELWRI bp are marked clean.
1324 * We still allow the B_INVAL case to call vfs_vmio_release(), even
1325 * if B_DELWRI is set.
1327 * If B_DELWRI is not set we may have to set B_RELBUF if we are low
1328 * on pages to return pages to the VM page queues.
1330 if (bp->b_flags & B_DELWRI)
1331 bp->b_flags &= ~B_RELBUF;
1332 else if (buf_vm_page_count_severe()) {
1334 * The locking of the BO_LOCK is not necessary since
1335 * BKGRDINPROG cannot be set while we hold the buf
1336 * lock, it can only be cleared if it is already
1340 if (!(bp->b_vflags & BV_BKGRDINPROG))
1341 bp->b_flags |= B_RELBUF;
1343 bp->b_flags |= B_RELBUF;
1347 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
1348 * constituted, not even NFS buffers now. Two flags effect this. If
1349 * B_INVAL, the struct buf is invalidated but the VM object is kept
1350 * around ( i.e. so it is trivial to reconstitute the buffer later ).
1352 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
1353 * invalidated. BIO_ERROR cannot be set for a failed write unless the
1354 * buffer is also B_INVAL because it hits the re-dirtying code above.
1356 * Normally we can do this whether a buffer is B_DELWRI or not. If
1357 * the buffer is an NFS buffer, it is tracking piecemeal writes or
1358 * the commit state and we cannot afford to lose the buffer. If the
1359 * buffer has a background write in progress, we need to keep it
1360 * around to prevent it from being reconstituted and starting a second
1363 if ((bp->b_flags & B_VMIO)
1364 && !(bp->b_vp->v_mount != NULL &&
1365 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
1366 !vn_isdisk(bp->b_vp, NULL) &&
1367 (bp->b_flags & B_DELWRI))
1376 obj = bp->b_bufobj->bo_object;
1379 * Get the base offset and length of the buffer. Note that
1380 * in the VMIO case if the buffer block size is not
1381 * page-aligned then b_data pointer may not be page-aligned.
1382 * But our b_pages[] array *IS* page aligned.
1384 * block sizes less then DEV_BSIZE (usually 512) are not
1385 * supported due to the page granularity bits (m->valid,
1386 * m->dirty, etc...).
1388 * See man buf(9) for more information
1390 resid = bp->b_bufsize;
1391 foff = bp->b_offset;
1392 VM_OBJECT_WLOCK(obj);
1393 for (i = 0; i < bp->b_npages; i++) {
1399 * If we hit a bogus page, fixup *all* the bogus pages
1402 if (m == bogus_page) {
1403 poff = OFF_TO_IDX(bp->b_offset);
1406 for (j = i; j < bp->b_npages; j++) {
1408 mtmp = bp->b_pages[j];
1409 if (mtmp == bogus_page) {
1410 mtmp = vm_page_lookup(obj, poff + j);
1412 panic("brelse: page missing\n");
1414 bp->b_pages[j] = mtmp;
1418 if ((bp->b_flags & B_INVAL) == 0) {
1420 trunc_page((vm_offset_t)bp->b_data),
1421 bp->b_pages, bp->b_npages);
1425 if ((bp->b_flags & B_NOCACHE) ||
1426 (bp->b_ioflags & BIO_ERROR &&
1427 bp->b_iocmd == BIO_READ)) {
1428 int poffset = foff & PAGE_MASK;
1429 int presid = resid > (PAGE_SIZE - poffset) ?
1430 (PAGE_SIZE - poffset) : resid;
1432 KASSERT(presid >= 0, ("brelse: extra page"));
1433 vm_page_set_invalid(m, poffset, presid);
1435 printf("avoided corruption bug in bogus_page/brelse code\n");
1437 resid -= PAGE_SIZE - (foff & PAGE_MASK);
1438 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
1440 VM_OBJECT_WUNLOCK(obj);
1441 if (bp->b_flags & (B_INVAL | B_RELBUF))
1442 vfs_vmio_release(bp);
1444 } else if (bp->b_flags & B_VMIO) {
1446 if (bp->b_flags & (B_INVAL | B_RELBUF)) {
1447 vfs_vmio_release(bp);
1450 } else if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0) {
1451 if (bp->b_bufsize != 0)
1453 if (bp->b_vp != NULL)
1459 /* Handle delayed bremfree() processing. */
1460 if (bp->b_flags & B_REMFREE) {
1470 if (bp->b_qindex != QUEUE_NONE)
1471 panic("brelse: free buffer onto another queue???");
1474 * If the buffer has junk contents signal it and eventually
1475 * clean up B_DELWRI and diassociate the vnode so that gbincore()
1478 if (bp->b_bufsize == 0 || (bp->b_ioflags & BIO_ERROR) != 0 ||
1479 (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) != 0)
1480 bp->b_flags |= B_INVAL;
1481 if (bp->b_flags & B_INVAL) {
1482 if (bp->b_flags & B_DELWRI)
1488 /* buffers with no memory */
1489 if (bp->b_bufsize == 0) {
1490 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1491 if (bp->b_vflags & BV_BKGRDINPROG)
1492 panic("losing buffer 1");
1493 if (bp->b_kvasize) {
1494 bp->b_qindex = QUEUE_EMPTYKVA;
1496 bp->b_qindex = QUEUE_EMPTY;
1498 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1499 /* buffers with junk contents */
1500 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
1501 (bp->b_ioflags & BIO_ERROR)) {
1502 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1503 if (bp->b_vflags & BV_BKGRDINPROG)
1504 panic("losing buffer 2");
1505 bp->b_qindex = QUEUE_CLEAN;
1506 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1507 /* remaining buffers */
1509 if (bp->b_flags & B_DELWRI)
1510 bp->b_qindex = QUEUE_DIRTY;
1512 bp->b_qindex = QUEUE_CLEAN;
1513 if (bp->b_flags & B_AGE)
1514 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1516 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1518 mtx_unlock(&bqlock);
1521 * Fixup numfreebuffers count. The bp is on an appropriate queue
1522 * unless locked. We then bump numfreebuffers if it is not B_DELWRI.
1523 * We've already handled the B_INVAL case ( B_DELWRI will be clear
1524 * if B_INVAL is set ).
1527 if (!(bp->b_flags & B_DELWRI)) {
1539 * Something we can maybe free or reuse
1541 if (bp->b_bufsize || bp->b_kvasize)
1544 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT);
1545 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1546 panic("brelse: not dirty");
1552 * Release a buffer back to the appropriate queue but do not try to free
1553 * it. The buffer is expected to be used again soon.
1555 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1556 * biodone() to requeue an async I/O on completion. It is also used when
1557 * known good buffers need to be requeued but we think we may need the data
1560 * XXX we should be able to leave the B_RELBUF hint set on completion.
1563 bqrelse(struct buf *bp)
1567 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1568 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1569 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1571 if (BUF_LOCKRECURSED(bp)) {
1572 /* do not release to free list */
1578 if (bp->b_flags & B_MANAGED) {
1579 if (bp->b_flags & B_REMFREE) {
1586 mtx_unlock(&bqlock);
1588 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1594 /* Handle delayed bremfree() processing. */
1595 if (bp->b_flags & B_REMFREE) {
1602 if (bp->b_qindex != QUEUE_NONE)
1603 panic("bqrelse: free buffer onto another queue???");
1604 /* buffers with stale but valid contents */
1605 if (bp->b_flags & B_DELWRI) {
1606 bp->b_qindex = QUEUE_DIRTY;
1607 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1610 * The locking of the BO_LOCK for checking of the
1611 * BV_BKGRDINPROG is not necessary since the
1612 * BV_BKGRDINPROG cannot be set while we hold the buf
1613 * lock, it can only be cleared if it is already
1616 if (!buf_vm_page_count_severe() || (bp->b_vflags & BV_BKGRDINPROG)) {
1617 bp->b_qindex = QUEUE_CLEAN;
1618 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp,
1622 * We are too low on memory, we have to try to free
1623 * the buffer (most importantly: the wired pages
1624 * making up its backing store) *now*.
1626 mtx_unlock(&bqlock);
1631 mtx_unlock(&bqlock);
1633 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI)) {
1642 * Something we can maybe free or reuse.
1644 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1647 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1648 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1649 panic("bqrelse: not dirty");
1654 /* Give pages used by the bp back to the VM system (where possible) */
1656 vfs_vmio_release(struct buf *bp)
1661 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), bp->b_npages);
1662 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
1663 for (i = 0; i < bp->b_npages; i++) {
1665 bp->b_pages[i] = NULL;
1667 * In order to keep page LRU ordering consistent, put
1668 * everything on the inactive queue.
1671 vm_page_unwire(m, 0);
1673 * We don't mess with busy pages, it is
1674 * the responsibility of the process that
1675 * busied the pages to deal with them.
1677 if ((m->oflags & VPO_BUSY) == 0 && m->busy == 0 &&
1678 m->wire_count == 0) {
1680 * Might as well free the page if we can and it has
1681 * no valid data. We also free the page if the
1682 * buffer was used for direct I/O
1684 if ((bp->b_flags & B_ASYNC) == 0 && !m->valid) {
1686 } else if (bp->b_flags & B_DIRECT) {
1687 vm_page_try_to_free(m);
1688 } else if (buf_vm_page_count_severe()) {
1689 vm_page_try_to_cache(m);
1694 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
1696 if (bp->b_bufsize) {
1701 bp->b_flags &= ~B_VMIO;
1707 * Check to see if a block at a particular lbn is available for a clustered
1711 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
1718 /* If the buf isn't in core skip it */
1719 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
1722 /* If the buf is busy we don't want to wait for it */
1723 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1726 /* Only cluster with valid clusterable delayed write buffers */
1727 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
1728 (B_DELWRI | B_CLUSTEROK))
1731 if (bpa->b_bufsize != size)
1735 * Check to see if it is in the expected place on disk and that the
1736 * block has been mapped.
1738 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
1748 * Implement clustered async writes for clearing out B_DELWRI buffers.
1749 * This is much better then the old way of writing only one buffer at
1750 * a time. Note that we may not be presented with the buffers in the
1751 * correct order, so we search for the cluster in both directions.
1754 vfs_bio_awrite(struct buf *bp)
1759 daddr_t lblkno = bp->b_lblkno;
1760 struct vnode *vp = bp->b_vp;
1768 * right now we support clustered writing only to regular files. If
1769 * we find a clusterable block we could be in the middle of a cluster
1770 * rather then at the beginning.
1772 if ((vp->v_type == VREG) &&
1773 (vp->v_mount != 0) && /* Only on nodes that have the size info */
1774 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
1776 size = vp->v_mount->mnt_stat.f_iosize;
1777 maxcl = MAXPHYS / size;
1780 for (i = 1; i < maxcl; i++)
1781 if (vfs_bio_clcheck(vp, size, lblkno + i,
1782 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
1785 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
1786 if (vfs_bio_clcheck(vp, size, lblkno - j,
1787 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
1793 * this is a possible cluster write
1797 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl);
1802 bp->b_flags |= B_ASYNC;
1804 * default (old) behavior, writing out only one block
1806 * XXX returns b_bufsize instead of b_bcount for nwritten?
1808 nwritten = bp->b_bufsize;
1817 * Find and initialize a new buffer header, freeing up existing buffers
1818 * in the bufqueues as necessary. The new buffer is returned locked.
1820 * Important: B_INVAL is not set. If the caller wishes to throw the
1821 * buffer away, the caller must set B_INVAL prior to calling brelse().
1824 * We have insufficient buffer headers
1825 * We have insufficient buffer space
1826 * buffer_map is too fragmented ( space reservation fails )
1827 * If we have to flush dirty buffers ( but we try to avoid this )
1829 * To avoid VFS layer recursion we do not flush dirty buffers ourselves.
1830 * Instead we ask the buf daemon to do it for us. We attempt to
1831 * avoid piecemeal wakeups of the pageout daemon.
1835 getnewbuf(struct vnode *vp, int slpflag, int slptimeo, int size, int maxsize,
1843 static int flushingbufs;
1847 * We can't afford to block since we might be holding a vnode lock,
1848 * which may prevent system daemons from running. We deal with
1849 * low-memory situations by proactively returning memory and running
1850 * async I/O rather then sync I/O.
1852 atomic_add_int(&getnewbufcalls, 1);
1853 atomic_subtract_int(&getnewbufrestarts, 1);
1855 atomic_add_int(&getnewbufrestarts, 1);
1858 * Setup for scan. If we do not have enough free buffers,
1859 * we setup a degenerate case that immediately fails. Note
1860 * that if we are specially marked process, we are allowed to
1861 * dip into our reserves.
1863 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN
1865 * We start with EMPTYKVA. If the list is empty we backup to EMPTY.
1866 * However, there are a number of cases (defragging, reusing, ...)
1867 * where we cannot backup.
1870 nqindex = QUEUE_EMPTYKVA;
1871 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]);
1875 * If no EMPTYKVA buffers and we are either
1876 * defragging or reusing, locate a CLEAN buffer
1877 * to free or reuse. If bufspace useage is low
1878 * skip this step so we can allocate a new buffer.
1880 if (defrag || bufspace >= lobufspace) {
1881 nqindex = QUEUE_CLEAN;
1882 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
1886 * If we could not find or were not allowed to reuse a
1887 * CLEAN buffer, check to see if it is ok to use an EMPTY
1888 * buffer. We can only use an EMPTY buffer if allocating
1889 * its KVA would not otherwise run us out of buffer space.
1891 if (nbp == NULL && defrag == 0 &&
1892 bufspace + maxsize < hibufspace) {
1893 nqindex = QUEUE_EMPTY;
1894 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1899 * Run scan, possibly freeing data and/or kva mappings on the fly
1903 while ((bp = nbp) != NULL) {
1904 int qindex = nqindex;
1907 * Calculate next bp ( we can only use it if we do not block
1908 * or do other fancy things ).
1910 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
1913 nqindex = QUEUE_EMPTYKVA;
1914 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA])))
1917 case QUEUE_EMPTYKVA:
1918 nqindex = QUEUE_CLEAN;
1919 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN])))
1930 * If we are defragging then we need a buffer with
1931 * b_kvasize != 0. XXX this situation should no longer
1932 * occur, if defrag is non-zero the buffer's b_kvasize
1933 * should also be non-zero at this point. XXX
1935 if (defrag && bp->b_kvasize == 0) {
1936 printf("Warning: defrag empty buffer %p\n", bp);
1941 * Start freeing the bp. This is somewhat involved. nbp
1942 * remains valid only for QUEUE_EMPTY[KVA] bp's.
1944 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1947 BO_LOCK(bp->b_bufobj);
1948 if (bp->b_vflags & BV_BKGRDINPROG) {
1949 BO_UNLOCK(bp->b_bufobj);
1953 BO_UNLOCK(bp->b_bufobj);
1956 "getnewbuf(%p) vp %p flags %X kvasize %d bufsize %d "
1957 "queue %d (recycling)", bp, bp->b_vp, bp->b_flags,
1958 bp->b_kvasize, bp->b_bufsize, qindex);
1963 KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp));
1966 * Note: we no longer distinguish between VMIO and non-VMIO
1970 KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex));
1972 if (bp->b_bufobj != NULL)
1973 BO_LOCK(bp->b_bufobj);
1975 if (bp->b_bufobj != NULL)
1976 BO_UNLOCK(bp->b_bufobj);
1977 mtx_unlock(&bqlock);
1979 if (qindex == QUEUE_CLEAN) {
1980 if (bp->b_flags & B_VMIO) {
1981 bp->b_flags &= ~B_ASYNC;
1982 vfs_vmio_release(bp);
1989 * NOTE: nbp is now entirely invalid. We can only restart
1990 * the scan from this point on.
1992 * Get the rest of the buffer freed up. b_kva* is still
1993 * valid after this operation.
1996 if (bp->b_rcred != NOCRED) {
1997 crfree(bp->b_rcred);
1998 bp->b_rcred = NOCRED;
2000 if (bp->b_wcred != NOCRED) {
2001 crfree(bp->b_wcred);
2002 bp->b_wcred = NOCRED;
2004 if (!LIST_EMPTY(&bp->b_dep))
2006 if (bp->b_vflags & BV_BKGRDINPROG)
2007 panic("losing buffer 3");
2008 KASSERT(bp->b_vp == NULL,
2009 ("bp: %p still has vnode %p. qindex: %d",
2010 bp, bp->b_vp, qindex));
2011 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
2012 ("bp: %p still on a buffer list. xflags %X",
2021 KASSERT((bp->b_vflags & BV_INFREECNT) == 0,
2022 ("buf %p still counted as free?", bp));
2025 bp->b_blkno = bp->b_lblkno = 0;
2026 bp->b_offset = NOOFFSET;
2032 bp->b_dirtyoff = bp->b_dirtyend = 0;
2033 bp->b_bufobj = NULL;
2034 bp->b_pin_count = 0;
2035 bp->b_fsprivate1 = NULL;
2036 bp->b_fsprivate2 = NULL;
2037 bp->b_fsprivate3 = NULL;
2039 LIST_INIT(&bp->b_dep);
2042 * If we are defragging then free the buffer.
2045 bp->b_flags |= B_INVAL;
2053 * Notify any waiters for the buffer lock about
2054 * identity change by freeing the buffer.
2056 if (qindex == QUEUE_CLEAN && BUF_LOCKWAITERS(bp)) {
2057 bp->b_flags |= B_INVAL;
2064 * If we are overcomitted then recover the buffer and its
2065 * KVM space. This occurs in rare situations when multiple
2066 * processes are blocked in getnewbuf() or allocbuf().
2068 if (bufspace >= hibufspace)
2070 if (flushingbufs && bp->b_kvasize != 0) {
2071 bp->b_flags |= B_INVAL;
2076 if (bufspace < lobufspace)
2082 * If we exhausted our list, sleep as appropriate. We may have to
2083 * wakeup various daemons and write out some dirty buffers.
2085 * Generally we are sleeping due to insufficient buffer space.
2089 int flags, norunbuf;
2094 flags = VFS_BIO_NEED_BUFSPACE;
2096 } else if (bufspace >= hibufspace) {
2098 flags = VFS_BIO_NEED_BUFSPACE;
2101 flags = VFS_BIO_NEED_ANY;
2104 needsbuffer |= flags;
2105 mtx_unlock(&nblock);
2106 mtx_unlock(&bqlock);
2108 bd_speedup(); /* heeeelp */
2109 if (gbflags & GB_NOWAIT_BD)
2113 while (needsbuffer & flags) {
2114 if (vp != NULL && (td->td_pflags & TDP_BUFNEED) == 0) {
2115 mtx_unlock(&nblock);
2117 * getblk() is called with a vnode
2118 * locked, and some majority of the
2119 * dirty buffers may as well belong to
2120 * the vnode. Flushing the buffers
2121 * there would make a progress that
2122 * cannot be achieved by the
2123 * buf_daemon, that cannot lock the
2126 norunbuf = ~(TDP_BUFNEED | TDP_NORUNNINGBUF) |
2127 (td->td_pflags & TDP_NORUNNINGBUF);
2128 /* play bufdaemon */
2129 td->td_pflags |= TDP_BUFNEED | TDP_NORUNNINGBUF;
2130 fl = buf_do_flush(vp);
2131 td->td_pflags &= norunbuf;
2135 if ((needsbuffer & flags) == 0)
2138 if (msleep(&needsbuffer, &nblock,
2139 (PRIBIO + 4) | slpflag, waitmsg, slptimeo)) {
2140 mtx_unlock(&nblock);
2144 mtx_unlock(&nblock);
2147 * We finally have a valid bp. We aren't quite out of the
2148 * woods, we still have to reserve kva space. In order
2149 * to keep fragmentation sane we only allocate kva in
2152 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
2154 if (maxsize != bp->b_kvasize) {
2155 vm_offset_t addr = 0;
2160 vm_map_lock(buffer_map);
2161 if (vm_map_findspace(buffer_map,
2162 vm_map_min(buffer_map), maxsize, &addr)) {
2164 * Buffer map is too fragmented.
2165 * We must defragment the map.
2167 atomic_add_int(&bufdefragcnt, 1);
2168 vm_map_unlock(buffer_map);
2170 bp->b_flags |= B_INVAL;
2174 rv = vm_map_insert(buffer_map, NULL, 0, addr,
2175 addr + maxsize, VM_PROT_ALL, VM_PROT_ALL,
2177 KASSERT(rv == KERN_SUCCESS,
2178 ("vm_map_insert(buffer_map) rv %d", rv));
2179 vm_map_unlock(buffer_map);
2180 bp->b_kvabase = (caddr_t)addr;
2181 bp->b_kvasize = maxsize;
2182 atomic_add_long(&bufspace, bp->b_kvasize);
2183 atomic_add_int(&bufreusecnt, 1);
2185 bp->b_saveaddr = bp->b_kvabase;
2186 bp->b_data = bp->b_saveaddr;
2194 * buffer flushing daemon. Buffers are normally flushed by the
2195 * update daemon but if it cannot keep up this process starts to
2196 * take the load in an attempt to prevent getnewbuf() from blocking.
2199 static struct kproc_desc buf_kp = {
2204 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp);
2207 buf_do_flush(struct vnode *vp)
2211 flushed = flushbufqueues(vp, QUEUE_DIRTY, 0);
2214 * Could not find any buffers without rollback
2215 * dependencies, so just write the first one
2216 * in the hopes of eventually making progress.
2218 flushbufqueues(vp, QUEUE_DIRTY, 1);
2229 * This process needs to be suspended prior to shutdown sync.
2231 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
2235 * This process is allowed to take the buffer cache to the limit
2237 curthread->td_pflags |= TDP_NORUNNINGBUF | TDP_BUFNEED;
2241 mtx_unlock(&bdlock);
2243 kproc_suspend_check(bufdaemonproc);
2244 lodirtysave = lodirtybuffers;
2245 if (bd_speedupreq) {
2246 lodirtybuffers = numdirtybuffers / 2;
2250 * Do the flush. Limit the amount of in-transit I/O we
2251 * allow to build up, otherwise we would completely saturate
2252 * the I/O system. Wakeup any waiting processes before we
2253 * normally would so they can run in parallel with our drain.
2255 while (numdirtybuffers > lodirtybuffers) {
2256 if (buf_do_flush(NULL) == 0)
2258 kern_yield(PRI_USER);
2260 lodirtybuffers = lodirtysave;
2263 * Only clear bd_request if we have reached our low water
2264 * mark. The buf_daemon normally waits 1 second and
2265 * then incrementally flushes any dirty buffers that have
2266 * built up, within reason.
2268 * If we were unable to hit our low water mark and couldn't
2269 * find any flushable buffers, we sleep half a second.
2270 * Otherwise we loop immediately.
2273 if (numdirtybuffers <= lodirtybuffers) {
2275 * We reached our low water mark, reset the
2276 * request and sleep until we are needed again.
2277 * The sleep is just so the suspend code works.
2280 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
2283 * We couldn't find any flushable dirty buffers but
2284 * still have too many dirty buffers, we
2285 * have to sleep and try again. (rare)
2287 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
2295 * Try to flush a buffer in the dirty queue. We must be careful to
2296 * free up B_INVAL buffers instead of write them, which NFS is
2297 * particularly sensitive to.
2299 static int flushwithdeps = 0;
2300 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
2301 0, "Number of buffers flushed with dependecies that require rollbacks");
2304 flushbufqueues(struct vnode *lvp, int queue, int flushdeps)
2306 struct buf *sentinel;
2315 target = numdirtybuffers - lodirtybuffers;
2316 if (flushdeps && target > 2)
2319 target = flushbufqtarget;
2322 sentinel = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
2323 sentinel->b_qindex = QUEUE_SENTINEL;
2325 TAILQ_INSERT_HEAD(&bufqueues[queue], sentinel, b_freelist);
2326 while (flushed != target) {
2327 bp = TAILQ_NEXT(sentinel, b_freelist);
2329 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2330 TAILQ_INSERT_AFTER(&bufqueues[queue], bp, sentinel,
2335 * Skip sentinels inserted by other invocations of the
2336 * flushbufqueues(), taking care to not reorder them.
2338 if (bp->b_qindex == QUEUE_SENTINEL)
2341 * Only flush the buffers that belong to the
2342 * vnode locked by the curthread.
2344 if (lvp != NULL && bp->b_vp != lvp)
2346 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2348 if (bp->b_pin_count > 0) {
2352 BO_LOCK(bp->b_bufobj);
2353 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
2354 (bp->b_flags & B_DELWRI) == 0) {
2355 BO_UNLOCK(bp->b_bufobj);
2359 BO_UNLOCK(bp->b_bufobj);
2360 if (bp->b_flags & B_INVAL) {
2362 mtx_unlock(&bqlock);
2365 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2370 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) {
2371 if (flushdeps == 0) {
2379 * We must hold the lock on a vnode before writing
2380 * one of its buffers. Otherwise we may confuse, or
2381 * in the case of a snapshot vnode, deadlock the
2384 * The lock order here is the reverse of the normal
2385 * of vnode followed by buf lock. This is ok because
2386 * the NOWAIT will prevent deadlock.
2389 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2393 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_CANRECURSE) == 0) {
2394 mtx_unlock(&bqlock);
2395 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
2396 bp, bp->b_vp, bp->b_flags);
2397 if (curproc == bufdaemonproc)
2404 vn_finished_write(mp);
2406 flushwithdeps += hasdeps;
2410 * Sleeping on runningbufspace while holding
2411 * vnode lock leads to deadlock.
2413 if (curproc == bufdaemonproc)
2414 waitrunningbufspace();
2415 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2419 vn_finished_write(mp);
2422 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2423 mtx_unlock(&bqlock);
2424 free(sentinel, M_TEMP);
2429 * Check to see if a block is currently memory resident.
2432 incore(struct bufobj *bo, daddr_t blkno)
2437 bp = gbincore(bo, blkno);
2443 * Returns true if no I/O is needed to access the
2444 * associated VM object. This is like incore except
2445 * it also hunts around in the VM system for the data.
2449 inmem(struct vnode * vp, daddr_t blkno)
2452 vm_offset_t toff, tinc, size;
2456 ASSERT_VOP_LOCKED(vp, "inmem");
2458 if (incore(&vp->v_bufobj, blkno))
2460 if (vp->v_mount == NULL)
2467 if (size > vp->v_mount->mnt_stat.f_iosize)
2468 size = vp->v_mount->mnt_stat.f_iosize;
2469 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
2471 VM_OBJECT_WLOCK(obj);
2472 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
2473 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
2477 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
2478 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
2479 if (vm_page_is_valid(m,
2480 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
2483 VM_OBJECT_WUNLOCK(obj);
2487 VM_OBJECT_WUNLOCK(obj);
2492 * Set the dirty range for a buffer based on the status of the dirty
2493 * bits in the pages comprising the buffer. The range is limited
2494 * to the size of the buffer.
2496 * Tell the VM system that the pages associated with this buffer
2497 * are clean. This is used for delayed writes where the data is
2498 * going to go to disk eventually without additional VM intevention.
2500 * Note that while we only really need to clean through to b_bcount, we
2501 * just go ahead and clean through to b_bufsize.
2504 vfs_clean_pages_dirty_buf(struct buf *bp)
2506 vm_ooffset_t foff, noff, eoff;
2510 if ((bp->b_flags & B_VMIO) == 0 || bp->b_bufsize == 0)
2513 foff = bp->b_offset;
2514 KASSERT(bp->b_offset != NOOFFSET,
2515 ("vfs_clean_pages_dirty_buf: no buffer offset"));
2517 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
2518 vfs_drain_busy_pages(bp);
2519 vfs_setdirty_locked_object(bp);
2520 for (i = 0; i < bp->b_npages; i++) {
2521 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
2523 if (eoff > bp->b_offset + bp->b_bufsize)
2524 eoff = bp->b_offset + bp->b_bufsize;
2526 vfs_page_set_validclean(bp, foff, m);
2527 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
2530 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
2534 vfs_setdirty_locked_object(struct buf *bp)
2539 object = bp->b_bufobj->bo_object;
2540 VM_OBJECT_ASSERT_WLOCKED(object);
2543 * We qualify the scan for modified pages on whether the
2544 * object has been flushed yet.
2546 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) {
2547 vm_offset_t boffset;
2548 vm_offset_t eoffset;
2551 * test the pages to see if they have been modified directly
2552 * by users through the VM system.
2554 for (i = 0; i < bp->b_npages; i++)
2555 vm_page_test_dirty(bp->b_pages[i]);
2558 * Calculate the encompassing dirty range, boffset and eoffset,
2559 * (eoffset - boffset) bytes.
2562 for (i = 0; i < bp->b_npages; i++) {
2563 if (bp->b_pages[i]->dirty)
2566 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2568 for (i = bp->b_npages - 1; i >= 0; --i) {
2569 if (bp->b_pages[i]->dirty) {
2573 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2576 * Fit it to the buffer.
2579 if (eoffset > bp->b_bcount)
2580 eoffset = bp->b_bcount;
2583 * If we have a good dirty range, merge with the existing
2587 if (boffset < eoffset) {
2588 if (bp->b_dirtyoff > boffset)
2589 bp->b_dirtyoff = boffset;
2590 if (bp->b_dirtyend < eoffset)
2591 bp->b_dirtyend = eoffset;
2599 * Get a block given a specified block and offset into a file/device.
2600 * The buffers B_DONE bit will be cleared on return, making it almost
2601 * ready for an I/O initiation. B_INVAL may or may not be set on
2602 * return. The caller should clear B_INVAL prior to initiating a
2605 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
2606 * an existing buffer.
2608 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
2609 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
2610 * and then cleared based on the backing VM. If the previous buffer is
2611 * non-0-sized but invalid, B_CACHE will be cleared.
2613 * If getblk() must create a new buffer, the new buffer is returned with
2614 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
2615 * case it is returned with B_INVAL clear and B_CACHE set based on the
2618 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
2619 * B_CACHE bit is clear.
2621 * What this means, basically, is that the caller should use B_CACHE to
2622 * determine whether the buffer is fully valid or not and should clear
2623 * B_INVAL prior to issuing a read. If the caller intends to validate
2624 * the buffer by loading its data area with something, the caller needs
2625 * to clear B_INVAL. If the caller does this without issuing an I/O,
2626 * the caller should set B_CACHE ( as an optimization ), else the caller
2627 * should issue the I/O and biodone() will set B_CACHE if the I/O was
2628 * a write attempt or if it was a successfull read. If the caller
2629 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
2630 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
2633 getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo,
2640 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
2641 ASSERT_VOP_LOCKED(vp, "getblk");
2642 if (size > MAXBSIZE)
2643 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE);
2648 * Block if we are low on buffers. Certain processes are allowed
2649 * to completely exhaust the buffer cache.
2651 * If this check ever becomes a bottleneck it may be better to
2652 * move it into the else, when gbincore() fails. At the moment
2653 * it isn't a problem.
2655 if (numfreebuffers == 0) {
2656 if (TD_IS_IDLETHREAD(curthread))
2659 needsbuffer |= VFS_BIO_NEED_ANY;
2660 mtx_unlock(&nblock);
2664 bp = gbincore(bo, blkno);
2668 * Buffer is in-core. If the buffer is not busy nor managed,
2669 * it must be on a queue.
2671 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
2673 if (flags & GB_LOCK_NOWAIT)
2674 lockflags |= LK_NOWAIT;
2676 error = BUF_TIMELOCK(bp, lockflags,
2677 BO_MTX(bo), "getblk", slpflag, slptimeo);
2680 * If we slept and got the lock we have to restart in case
2681 * the buffer changed identities.
2683 if (error == ENOLCK)
2685 /* We timed out or were interrupted. */
2688 /* If recursed, assume caller knows the rules. */
2689 else if (BUF_LOCKRECURSED(bp))
2693 * The buffer is locked. B_CACHE is cleared if the buffer is
2694 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
2695 * and for a VMIO buffer B_CACHE is adjusted according to the
2698 if (bp->b_flags & B_INVAL)
2699 bp->b_flags &= ~B_CACHE;
2700 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
2701 bp->b_flags |= B_CACHE;
2702 if (bp->b_flags & B_MANAGED)
2703 MPASS(bp->b_qindex == QUEUE_NONE);
2711 * check for size inconsistancies for non-VMIO case.
2714 if (bp->b_bcount != size) {
2715 if ((bp->b_flags & B_VMIO) == 0 ||
2716 (size > bp->b_kvasize)) {
2717 if (bp->b_flags & B_DELWRI) {
2719 * If buffer is pinned and caller does
2720 * not want sleep waiting for it to be
2721 * unpinned, bail out
2723 if (bp->b_pin_count > 0) {
2724 if (flags & GB_LOCK_NOWAIT) {
2731 bp->b_flags |= B_NOCACHE;
2734 if (LIST_EMPTY(&bp->b_dep)) {
2735 bp->b_flags |= B_RELBUF;
2738 bp->b_flags |= B_NOCACHE;
2747 * If the size is inconsistant in the VMIO case, we can resize
2748 * the buffer. This might lead to B_CACHE getting set or
2749 * cleared. If the size has not changed, B_CACHE remains
2750 * unchanged from its previous state.
2753 if (bp->b_bcount != size)
2756 KASSERT(bp->b_offset != NOOFFSET,
2757 ("getblk: no buffer offset"));
2760 * A buffer with B_DELWRI set and B_CACHE clear must
2761 * be committed before we can return the buffer in
2762 * order to prevent the caller from issuing a read
2763 * ( due to B_CACHE not being set ) and overwriting
2766 * Most callers, including NFS and FFS, need this to
2767 * operate properly either because they assume they
2768 * can issue a read if B_CACHE is not set, or because
2769 * ( for example ) an uncached B_DELWRI might loop due
2770 * to softupdates re-dirtying the buffer. In the latter
2771 * case, B_CACHE is set after the first write completes,
2772 * preventing further loops.
2773 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
2774 * above while extending the buffer, we cannot allow the
2775 * buffer to remain with B_CACHE set after the write
2776 * completes or it will represent a corrupt state. To
2777 * deal with this we set B_NOCACHE to scrap the buffer
2780 * We might be able to do something fancy, like setting
2781 * B_CACHE in bwrite() except if B_DELWRI is already set,
2782 * so the below call doesn't set B_CACHE, but that gets real
2783 * confusing. This is much easier.
2786 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
2787 bp->b_flags |= B_NOCACHE;
2791 bp->b_flags &= ~B_DONE;
2793 int bsize, maxsize, vmio;
2797 * Buffer is not in-core, create new buffer. The buffer
2798 * returned by getnewbuf() is locked. Note that the returned
2799 * buffer is also considered valid (not marked B_INVAL).
2803 * If the user does not want us to create the buffer, bail out
2806 if (flags & GB_NOCREAT)
2808 bsize = vn_isdisk(vp, NULL) ? DEV_BSIZE : bo->bo_bsize;
2809 offset = blkno * bsize;
2810 vmio = vp->v_object != NULL;
2811 maxsize = vmio ? size + (offset & PAGE_MASK) : size;
2812 maxsize = imax(maxsize, bsize);
2814 bp = getnewbuf(vp, slpflag, slptimeo, size, maxsize, flags);
2816 if (slpflag || slptimeo)
2822 * This code is used to make sure that a buffer is not
2823 * created while the getnewbuf routine is blocked.
2824 * This can be a problem whether the vnode is locked or not.
2825 * If the buffer is created out from under us, we have to
2826 * throw away the one we just created.
2828 * Note: this must occur before we associate the buffer
2829 * with the vp especially considering limitations in
2830 * the splay tree implementation when dealing with duplicate
2834 if (gbincore(bo, blkno)) {
2836 bp->b_flags |= B_INVAL;
2842 * Insert the buffer into the hash, so that it can
2843 * be found by incore.
2845 bp->b_blkno = bp->b_lblkno = blkno;
2846 bp->b_offset = offset;
2851 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
2852 * buffer size starts out as 0, B_CACHE will be set by
2853 * allocbuf() for the VMIO case prior to it testing the
2854 * backing store for validity.
2858 bp->b_flags |= B_VMIO;
2859 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
2860 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
2861 bp, vp->v_object, bp->b_bufobj->bo_object));
2863 bp->b_flags &= ~B_VMIO;
2864 KASSERT(bp->b_bufobj->bo_object == NULL,
2865 ("ARGH! has b_bufobj->bo_object %p %p\n",
2866 bp, bp->b_bufobj->bo_object));
2870 bp->b_flags &= ~B_DONE;
2872 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
2873 BUF_ASSERT_HELD(bp);
2875 KASSERT(bp->b_bufobj == bo,
2876 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2881 * Get an empty, disassociated buffer of given size. The buffer is initially
2885 geteblk(int size, int flags)
2890 maxsize = (size + BKVAMASK) & ~BKVAMASK;
2891 while ((bp = getnewbuf(NULL, 0, 0, size, maxsize, flags)) == NULL) {
2892 if ((flags & GB_NOWAIT_BD) &&
2893 (curthread->td_pflags & TDP_BUFNEED) != 0)
2897 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
2898 BUF_ASSERT_HELD(bp);
2904 * This code constitutes the buffer memory from either anonymous system
2905 * memory (in the case of non-VMIO operations) or from an associated
2906 * VM object (in the case of VMIO operations). This code is able to
2907 * resize a buffer up or down.
2909 * Note that this code is tricky, and has many complications to resolve
2910 * deadlock or inconsistant data situations. Tread lightly!!!
2911 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
2912 * the caller. Calling this code willy nilly can result in the loss of data.
2914 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
2915 * B_CACHE for the non-VMIO case.
2919 allocbuf(struct buf *bp, int size)
2921 int newbsize, mbsize;
2924 BUF_ASSERT_HELD(bp);
2926 if (bp->b_kvasize < size)
2927 panic("allocbuf: buffer too small");
2929 if ((bp->b_flags & B_VMIO) == 0) {
2933 * Just get anonymous memory from the kernel. Don't
2934 * mess with B_CACHE.
2936 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2937 if (bp->b_flags & B_MALLOC)
2940 newbsize = round_page(size);
2942 if (newbsize < bp->b_bufsize) {
2944 * malloced buffers are not shrunk
2946 if (bp->b_flags & B_MALLOC) {
2948 bp->b_bcount = size;
2950 free(bp->b_data, M_BIOBUF);
2951 if (bp->b_bufsize) {
2952 atomic_subtract_long(
2958 bp->b_saveaddr = bp->b_kvabase;
2959 bp->b_data = bp->b_saveaddr;
2961 bp->b_flags &= ~B_MALLOC;
2965 vm_hold_free_pages(bp, newbsize);
2966 } else if (newbsize > bp->b_bufsize) {
2968 * We only use malloced memory on the first allocation.
2969 * and revert to page-allocated memory when the buffer
2973 * There is a potential smp race here that could lead
2974 * to bufmallocspace slightly passing the max. It
2975 * is probably extremely rare and not worth worrying
2978 if ( (bufmallocspace < maxbufmallocspace) &&
2979 (bp->b_bufsize == 0) &&
2980 (mbsize <= PAGE_SIZE/2)) {
2982 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK);
2983 bp->b_bufsize = mbsize;
2984 bp->b_bcount = size;
2985 bp->b_flags |= B_MALLOC;
2986 atomic_add_long(&bufmallocspace, mbsize);
2992 * If the buffer is growing on its other-than-first allocation,
2993 * then we revert to the page-allocation scheme.
2995 if (bp->b_flags & B_MALLOC) {
2996 origbuf = bp->b_data;
2997 origbufsize = bp->b_bufsize;
2998 bp->b_data = bp->b_kvabase;
2999 if (bp->b_bufsize) {
3000 atomic_subtract_long(&bufmallocspace,
3005 bp->b_flags &= ~B_MALLOC;
3006 newbsize = round_page(newbsize);
3010 (vm_offset_t) bp->b_data + bp->b_bufsize,
3011 (vm_offset_t) bp->b_data + newbsize);
3013 bcopy(origbuf, bp->b_data, origbufsize);
3014 free(origbuf, M_BIOBUF);
3020 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
3021 desiredpages = (size == 0) ? 0 :
3022 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
3024 if (bp->b_flags & B_MALLOC)
3025 panic("allocbuf: VMIO buffer can't be malloced");
3027 * Set B_CACHE initially if buffer is 0 length or will become
3030 if (size == 0 || bp->b_bufsize == 0)
3031 bp->b_flags |= B_CACHE;
3033 if (newbsize < bp->b_bufsize) {
3035 * DEV_BSIZE aligned new buffer size is less then the
3036 * DEV_BSIZE aligned existing buffer size. Figure out
3037 * if we have to remove any pages.
3039 if (desiredpages < bp->b_npages) {
3042 pmap_qremove((vm_offset_t)trunc_page(
3043 (vm_offset_t)bp->b_data) +
3044 (desiredpages << PAGE_SHIFT),
3045 (bp->b_npages - desiredpages));
3046 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
3047 for (i = desiredpages; i < bp->b_npages; i++) {
3049 * the page is not freed here -- it
3050 * is the responsibility of
3051 * vnode_pager_setsize
3054 KASSERT(m != bogus_page,
3055 ("allocbuf: bogus page found"));
3056 while (vm_page_sleep_if_busy(m, TRUE,
3060 bp->b_pages[i] = NULL;
3062 vm_page_unwire(m, 0);
3065 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
3066 bp->b_npages = desiredpages;
3068 } else if (size > bp->b_bcount) {
3070 * We are growing the buffer, possibly in a
3071 * byte-granular fashion.
3078 * Step 1, bring in the VM pages from the object,
3079 * allocating them if necessary. We must clear
3080 * B_CACHE if these pages are not valid for the
3081 * range covered by the buffer.
3084 obj = bp->b_bufobj->bo_object;
3086 VM_OBJECT_WLOCK(obj);
3087 while (bp->b_npages < desiredpages) {
3091 * We must allocate system pages since blocking
3092 * here could interfere with paging I/O, no
3093 * matter which process we are.
3095 * We can only test VPO_BUSY here. Blocking on
3096 * m->busy might lead to a deadlock:
3097 * vm_fault->getpages->cluster_read->allocbuf
3098 * Thus, we specify VM_ALLOC_IGN_SBUSY.
3100 m = vm_page_grab(obj, OFF_TO_IDX(bp->b_offset) +
3101 bp->b_npages, VM_ALLOC_NOBUSY |
3102 VM_ALLOC_SYSTEM | VM_ALLOC_WIRED |
3103 VM_ALLOC_RETRY | VM_ALLOC_IGN_SBUSY |
3104 VM_ALLOC_COUNT(desiredpages - bp->b_npages));
3106 bp->b_flags &= ~B_CACHE;
3107 bp->b_pages[bp->b_npages] = m;
3112 * Step 2. We've loaded the pages into the buffer,
3113 * we have to figure out if we can still have B_CACHE
3114 * set. Note that B_CACHE is set according to the
3115 * byte-granular range ( bcount and size ), new the
3116 * aligned range ( newbsize ).
3118 * The VM test is against m->valid, which is DEV_BSIZE
3119 * aligned. Needless to say, the validity of the data
3120 * needs to also be DEV_BSIZE aligned. Note that this
3121 * fails with NFS if the server or some other client
3122 * extends the file's EOF. If our buffer is resized,
3123 * B_CACHE may remain set! XXX
3126 toff = bp->b_bcount;
3127 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
3129 while ((bp->b_flags & B_CACHE) && toff < size) {
3132 if (tinc > (size - toff))
3135 pi = ((bp->b_offset & PAGE_MASK) + toff) >>
3148 VM_OBJECT_WUNLOCK(obj);
3151 * Step 3, fixup the KVM pmap. Remember that
3152 * bp->b_data is relative to bp->b_offset, but
3153 * bp->b_offset may be offset into the first page.
3156 bp->b_data = (caddr_t)
3157 trunc_page((vm_offset_t)bp->b_data);
3159 (vm_offset_t)bp->b_data,
3164 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
3165 (vm_offset_t)(bp->b_offset & PAGE_MASK));
3168 if (newbsize < bp->b_bufsize)
3170 bp->b_bufsize = newbsize; /* actual buffer allocation */
3171 bp->b_bcount = size; /* requested buffer size */
3176 biodone(struct bio *bp)
3179 void (*done)(struct bio *);
3181 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3183 bp->bio_flags |= BIO_DONE;
3184 done = bp->bio_done;
3193 * Wait for a BIO to finish.
3195 * XXX: resort to a timeout for now. The optimal locking (if any) for this
3196 * case is not yet clear.
3199 biowait(struct bio *bp, const char *wchan)
3203 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3205 while ((bp->bio_flags & BIO_DONE) == 0)
3206 msleep(bp, mtxp, PRIBIO, wchan, hz / 10);
3208 if (bp->bio_error != 0)
3209 return (bp->bio_error);
3210 if (!(bp->bio_flags & BIO_ERROR))
3216 biofinish(struct bio *bp, struct devstat *stat, int error)
3220 bp->bio_error = error;
3221 bp->bio_flags |= BIO_ERROR;
3224 devstat_end_transaction_bio(stat, bp);
3231 * Wait for buffer I/O completion, returning error status. The buffer
3232 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
3233 * error and cleared.
3236 bufwait(struct buf *bp)
3238 if (bp->b_iocmd == BIO_READ)
3239 bwait(bp, PRIBIO, "biord");
3241 bwait(bp, PRIBIO, "biowr");
3242 if (bp->b_flags & B_EINTR) {
3243 bp->b_flags &= ~B_EINTR;
3246 if (bp->b_ioflags & BIO_ERROR) {
3247 return (bp->b_error ? bp->b_error : EIO);
3254 * Call back function from struct bio back up to struct buf.
3257 bufdonebio(struct bio *bip)
3261 bp = bip->bio_caller2;
3262 bp->b_resid = bp->b_bcount - bip->bio_completed;
3263 bp->b_resid = bip->bio_resid; /* XXX: remove */
3264 bp->b_ioflags = bip->bio_flags;
3265 bp->b_error = bip->bio_error;
3267 bp->b_ioflags |= BIO_ERROR;
3273 dev_strategy(struct cdev *dev, struct buf *bp)
3279 if ((!bp->b_iocmd) || (bp->b_iocmd & (bp->b_iocmd - 1)))
3280 panic("b_iocmd botch");
3285 /* Try again later */
3286 tsleep(&bp, PRIBIO, "dev_strat", hz/10);
3288 bip->bio_cmd = bp->b_iocmd;
3289 bip->bio_offset = bp->b_iooffset;
3290 bip->bio_length = bp->b_bcount;
3291 bip->bio_bcount = bp->b_bcount; /* XXX: remove */
3292 bip->bio_data = bp->b_data;
3293 bip->bio_done = bufdonebio;
3294 bip->bio_caller2 = bp;
3296 KASSERT(dev->si_refcount > 0,
3297 ("dev_strategy on un-referenced struct cdev *(%s)",
3299 csw = dev_refthread(dev, &ref);
3302 bp->b_error = ENXIO;
3303 bp->b_ioflags = BIO_ERROR;
3307 (*csw->d_strategy)(bip);
3308 dev_relthread(dev, ref);
3314 * Finish I/O on a buffer, optionally calling a completion function.
3315 * This is usually called from an interrupt so process blocking is
3318 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
3319 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
3320 * assuming B_INVAL is clear.
3322 * For the VMIO case, we set B_CACHE if the op was a read and no
3323 * read error occured, or if the op was a write. B_CACHE is never
3324 * set if the buffer is invalid or otherwise uncacheable.
3326 * biodone does not mess with B_INVAL, allowing the I/O routine or the
3327 * initiator to leave B_INVAL set to brelse the buffer out of existance
3328 * in the biodone routine.
3331 bufdone(struct buf *bp)
3333 struct bufobj *dropobj;
3334 void (*biodone)(struct buf *);
3336 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
3339 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
3340 BUF_ASSERT_HELD(bp);
3342 runningbufwakeup(bp);
3343 if (bp->b_iocmd == BIO_WRITE)
3344 dropobj = bp->b_bufobj;
3345 /* call optional completion function if requested */
3346 if (bp->b_iodone != NULL) {
3347 biodone = bp->b_iodone;
3348 bp->b_iodone = NULL;
3351 bufobj_wdrop(dropobj);
3358 bufobj_wdrop(dropobj);
3362 bufdone_finish(struct buf *bp)
3364 BUF_ASSERT_HELD(bp);
3366 if (!LIST_EMPTY(&bp->b_dep))
3369 if (bp->b_flags & B_VMIO) {
3374 int bogus, i, iosize;
3376 obj = bp->b_bufobj->bo_object;
3377 KASSERT(obj->paging_in_progress >= bp->b_npages,
3378 ("biodone_finish: paging in progress(%d) < b_npages(%d)",
3379 obj->paging_in_progress, bp->b_npages));
3382 KASSERT(vp->v_holdcnt > 0,
3383 ("biodone_finish: vnode %p has zero hold count", vp));
3384 KASSERT(vp->v_object != NULL,
3385 ("biodone_finish: vnode %p has no vm_object", vp));
3387 foff = bp->b_offset;
3388 KASSERT(bp->b_offset != NOOFFSET,
3389 ("biodone_finish: bp %p has no buffer offset", bp));
3392 * Set B_CACHE if the op was a normal read and no error
3393 * occured. B_CACHE is set for writes in the b*write()
3396 iosize = bp->b_bcount - bp->b_resid;
3397 if (bp->b_iocmd == BIO_READ &&
3398 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
3399 !(bp->b_ioflags & BIO_ERROR)) {
3400 bp->b_flags |= B_CACHE;
3403 VM_OBJECT_WLOCK(obj);
3404 for (i = 0; i < bp->b_npages; i++) {
3408 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
3413 * cleanup bogus pages, restoring the originals
3416 if (m == bogus_page) {
3417 bogus = bogusflag = 1;
3418 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
3420 panic("biodone: page disappeared!");
3423 KASSERT(OFF_TO_IDX(foff) == m->pindex,
3424 ("biodone_finish: foff(%jd)/pindex(%ju) mismatch",
3425 (intmax_t)foff, (uintmax_t)m->pindex));
3428 * In the write case, the valid and clean bits are
3429 * already changed correctly ( see bdwrite() ), so we
3430 * only need to do this here in the read case.
3432 if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) {
3433 KASSERT((m->dirty & vm_page_bits(foff &
3434 PAGE_MASK, resid)) == 0, ("bufdone_finish:"
3435 " page %p has unexpected dirty bits", m));
3436 vfs_page_set_valid(bp, foff, m);
3439 vm_page_io_finish(m);
3440 vm_object_pip_subtract(obj, 1);
3441 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3444 vm_object_pip_wakeupn(obj, 0);
3445 VM_OBJECT_WUNLOCK(obj);
3447 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3448 bp->b_pages, bp->b_npages);
3452 * For asynchronous completions, release the buffer now. The brelse
3453 * will do a wakeup there if necessary - so no need to do a wakeup
3454 * here in the async case. The sync case always needs to do a wakeup.
3457 if (bp->b_flags & B_ASYNC) {
3458 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR))
3467 * This routine is called in lieu of iodone in the case of
3468 * incomplete I/O. This keeps the busy status for pages
3472 vfs_unbusy_pages(struct buf *bp)
3478 runningbufwakeup(bp);
3479 if (!(bp->b_flags & B_VMIO))
3482 obj = bp->b_bufobj->bo_object;
3483 VM_OBJECT_WLOCK(obj);
3484 for (i = 0; i < bp->b_npages; i++) {
3486 if (m == bogus_page) {
3487 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
3489 panic("vfs_unbusy_pages: page missing\n");
3491 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3492 bp->b_pages, bp->b_npages);
3494 vm_object_pip_subtract(obj, 1);
3495 vm_page_io_finish(m);
3497 vm_object_pip_wakeupn(obj, 0);
3498 VM_OBJECT_WUNLOCK(obj);
3502 * vfs_page_set_valid:
3504 * Set the valid bits in a page based on the supplied offset. The
3505 * range is restricted to the buffer's size.
3507 * This routine is typically called after a read completes.
3510 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3515 * Compute the end offset, eoff, such that [off, eoff) does not span a
3516 * page boundary and eoff is not greater than the end of the buffer.
3517 * The end of the buffer, in this case, is our file EOF, not the
3518 * allocation size of the buffer.
3520 eoff = (off + PAGE_SIZE) & ~(vm_ooffset_t)PAGE_MASK;
3521 if (eoff > bp->b_offset + bp->b_bcount)
3522 eoff = bp->b_offset + bp->b_bcount;
3525 * Set valid range. This is typically the entire buffer and thus the
3529 vm_page_set_valid_range(m, off & PAGE_MASK, eoff - off);
3533 * vfs_page_set_validclean:
3535 * Set the valid bits and clear the dirty bits in a page based on the
3536 * supplied offset. The range is restricted to the buffer's size.
3539 vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3541 vm_ooffset_t soff, eoff;
3544 * Start and end offsets in buffer. eoff - soff may not cross a
3545 * page boundry or cross the end of the buffer. The end of the
3546 * buffer, in this case, is our file EOF, not the allocation size
3550 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3551 if (eoff > bp->b_offset + bp->b_bcount)
3552 eoff = bp->b_offset + bp->b_bcount;
3555 * Set valid range. This is typically the entire buffer and thus the
3559 vm_page_set_validclean(
3561 (vm_offset_t) (soff & PAGE_MASK),
3562 (vm_offset_t) (eoff - soff)
3568 * Ensure that all buffer pages are not busied by VPO_BUSY flag. If
3569 * any page is busy, drain the flag.
3572 vfs_drain_busy_pages(struct buf *bp)
3577 VM_OBJECT_ASSERT_WLOCKED(bp->b_bufobj->bo_object);
3579 for (i = 0; i < bp->b_npages; i++) {
3581 if ((m->oflags & VPO_BUSY) != 0) {
3582 for (; last_busied < i; last_busied++)
3583 vm_page_busy(bp->b_pages[last_busied]);
3584 while ((m->oflags & VPO_BUSY) != 0)
3585 vm_page_sleep(m, "vbpage");
3588 for (i = 0; i < last_busied; i++)
3589 vm_page_wakeup(bp->b_pages[i]);
3593 * This routine is called before a device strategy routine.
3594 * It is used to tell the VM system that paging I/O is in
3595 * progress, and treat the pages associated with the buffer
3596 * almost as being VPO_BUSY. Also the object paging_in_progress
3597 * flag is handled to make sure that the object doesn't become
3600 * Since I/O has not been initiated yet, certain buffer flags
3601 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
3602 * and should be ignored.
3605 vfs_busy_pages(struct buf *bp, int clear_modify)
3612 if (!(bp->b_flags & B_VMIO))
3615 obj = bp->b_bufobj->bo_object;
3616 foff = bp->b_offset;
3617 KASSERT(bp->b_offset != NOOFFSET,
3618 ("vfs_busy_pages: no buffer offset"));
3619 VM_OBJECT_WLOCK(obj);
3620 vfs_drain_busy_pages(bp);
3621 if (bp->b_bufsize != 0)
3622 vfs_setdirty_locked_object(bp);
3624 for (i = 0; i < bp->b_npages; i++) {
3627 if ((bp->b_flags & B_CLUSTER) == 0) {
3628 vm_object_pip_add(obj, 1);
3629 vm_page_io_start(m);
3632 * When readying a buffer for a read ( i.e
3633 * clear_modify == 0 ), it is important to do
3634 * bogus_page replacement for valid pages in
3635 * partially instantiated buffers. Partially
3636 * instantiated buffers can, in turn, occur when
3637 * reconstituting a buffer from its VM backing store
3638 * base. We only have to do this if B_CACHE is
3639 * clear ( which causes the I/O to occur in the
3640 * first place ). The replacement prevents the read
3641 * I/O from overwriting potentially dirty VM-backed
3642 * pages. XXX bogus page replacement is, uh, bogus.
3643 * It may not work properly with small-block devices.
3644 * We need to find a better way.
3647 pmap_remove_write(m);
3648 vfs_page_set_validclean(bp, foff, m);
3649 } else if (m->valid == VM_PAGE_BITS_ALL &&
3650 (bp->b_flags & B_CACHE) == 0) {
3651 bp->b_pages[i] = bogus_page;
3654 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3656 VM_OBJECT_WUNLOCK(obj);
3658 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3659 bp->b_pages, bp->b_npages);
3663 * vfs_bio_set_valid:
3665 * Set the range within the buffer to valid. The range is
3666 * relative to the beginning of the buffer, b_offset. Note that
3667 * b_offset itself may be offset from the beginning of the first
3671 vfs_bio_set_valid(struct buf *bp, int base, int size)
3676 if (!(bp->b_flags & B_VMIO))
3680 * Fixup base to be relative to beginning of first page.
3681 * Set initial n to be the maximum number of bytes in the
3682 * first page that can be validated.
3684 base += (bp->b_offset & PAGE_MASK);
3685 n = PAGE_SIZE - (base & PAGE_MASK);
3687 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
3688 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
3692 vm_page_set_valid_range(m, base & PAGE_MASK, n);
3697 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
3703 * If the specified buffer is a non-VMIO buffer, clear the entire
3704 * buffer. If the specified buffer is a VMIO buffer, clear and
3705 * validate only the previously invalid portions of the buffer.
3706 * This routine essentially fakes an I/O, so we need to clear
3707 * BIO_ERROR and B_INVAL.
3709 * Note that while we only theoretically need to clear through b_bcount,
3710 * we go ahead and clear through b_bufsize.
3713 vfs_bio_clrbuf(struct buf *bp)
3718 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
3722 bp->b_flags &= ~B_INVAL;
3723 bp->b_ioflags &= ~BIO_ERROR;
3724 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
3725 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
3726 (bp->b_offset & PAGE_MASK) == 0) {
3727 if (bp->b_pages[0] == bogus_page)
3729 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
3730 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[0]->object);
3731 if ((bp->b_pages[0]->valid & mask) == mask)
3733 if ((bp->b_pages[0]->valid & mask) == 0) {
3734 bzero(bp->b_data, bp->b_bufsize);
3735 bp->b_pages[0]->valid |= mask;
3739 ea = sa = bp->b_data;
3740 for(i = 0; i < bp->b_npages; i++, sa = ea) {
3741 ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE);
3742 ea = (caddr_t)(vm_offset_t)ulmin(
3743 (u_long)(vm_offset_t)ea,
3744 (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize);
3745 if (bp->b_pages[i] == bogus_page)
3747 j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE;
3748 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
3749 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[i]->object);
3750 if ((bp->b_pages[i]->valid & mask) == mask)
3752 if ((bp->b_pages[i]->valid & mask) == 0)
3755 for (; sa < ea; sa += DEV_BSIZE, j++) {
3756 if ((bp->b_pages[i]->valid & (1 << j)) == 0)
3757 bzero(sa, DEV_BSIZE);
3760 bp->b_pages[i]->valid |= mask;
3763 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
3768 * vm_hold_load_pages and vm_hold_free_pages get pages into
3769 * a buffers address space. The pages are anonymous and are
3770 * not associated with a file object.
3773 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3779 to = round_page(to);
3780 from = round_page(from);
3781 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3783 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3786 * note: must allocate system pages since blocking here
3787 * could interfere with paging I/O, no matter which
3790 p = vm_page_alloc(NULL, 0, VM_ALLOC_SYSTEM | VM_ALLOC_NOOBJ |
3791 VM_ALLOC_WIRED | VM_ALLOC_COUNT((to - pg) >> PAGE_SHIFT));
3796 pmap_qenter(pg, &p, 1);
3797 bp->b_pages[index] = p;
3799 bp->b_npages = index;
3802 /* Return pages associated with this buf to the vm system */
3804 vm_hold_free_pages(struct buf *bp, int newbsize)
3808 int index, newnpages;
3810 from = round_page((vm_offset_t)bp->b_data + newbsize);
3811 newnpages = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3812 if (bp->b_npages > newnpages)
3813 pmap_qremove(from, bp->b_npages - newnpages);
3814 for (index = newnpages; index < bp->b_npages; index++) {
3815 p = bp->b_pages[index];
3816 bp->b_pages[index] = NULL;
3818 printf("vm_hold_free_pages: blkno: %jd, lblkno: %jd\n",
3819 (intmax_t)bp->b_blkno, (intmax_t)bp->b_lblkno);
3822 atomic_subtract_int(&cnt.v_wire_count, 1);
3824 bp->b_npages = newnpages;
3828 * Map an IO request into kernel virtual address space.
3830 * All requests are (re)mapped into kernel VA space.
3831 * Notice that we use b_bufsize for the size of the buffer
3832 * to be mapped. b_bcount might be modified by the driver.
3834 * Note that even if the caller determines that the address space should
3835 * be valid, a race or a smaller-file mapped into a larger space may
3836 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
3837 * check the return value.
3840 vmapbuf(struct buf *bp)
3846 if (bp->b_bufsize < 0)
3848 prot = VM_PROT_READ;
3849 if (bp->b_iocmd == BIO_READ)
3850 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
3851 if ((pidx = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
3852 (vm_offset_t)bp->b_data, bp->b_bufsize, prot, bp->b_pages,
3853 btoc(MAXPHYS))) < 0)
3855 pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx);
3857 kva = bp->b_saveaddr;
3858 bp->b_npages = pidx;
3859 bp->b_saveaddr = bp->b_data;
3860 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
3865 * Free the io map PTEs associated with this IO operation.
3866 * We also invalidate the TLB entries and restore the original b_addr.
3869 vunmapbuf(struct buf *bp)
3873 npages = bp->b_npages;
3874 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
3875 vm_page_unhold_pages(bp->b_pages, npages);
3877 bp->b_data = bp->b_saveaddr;
3881 bdone(struct buf *bp)
3885 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3887 bp->b_flags |= B_DONE;
3893 bwait(struct buf *bp, u_char pri, const char *wchan)
3897 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3899 while ((bp->b_flags & B_DONE) == 0)
3900 msleep(bp, mtxp, pri, wchan, 0);
3905 bufsync(struct bufobj *bo, int waitfor)
3908 return (VOP_FSYNC(bo->__bo_vnode, waitfor, curthread));
3912 bufstrategy(struct bufobj *bo, struct buf *bp)
3918 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
3919 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
3920 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
3921 i = VOP_STRATEGY(vp, bp);
3922 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
3926 bufobj_wrefl(struct bufobj *bo)
3929 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3930 ASSERT_BO_LOCKED(bo);
3935 bufobj_wref(struct bufobj *bo)
3938 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3945 bufobj_wdrop(struct bufobj *bo)
3948 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
3950 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
3951 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
3952 bo->bo_flag &= ~BO_WWAIT;
3953 wakeup(&bo->bo_numoutput);
3959 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
3963 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
3964 ASSERT_BO_LOCKED(bo);
3966 while (bo->bo_numoutput) {
3967 bo->bo_flag |= BO_WWAIT;
3968 error = msleep(&bo->bo_numoutput, BO_MTX(bo),
3969 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
3977 bpin(struct buf *bp)
3981 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3988 bunpin(struct buf *bp)
3992 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3994 if (--bp->b_pin_count == 0)
4000 bunpin_wait(struct buf *bp)
4004 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4006 while (bp->b_pin_count > 0)
4007 msleep(bp, mtxp, PRIBIO, "bwunpin", 0);
4011 #include "opt_ddb.h"
4013 #include <ddb/ddb.h>
4015 /* DDB command to show buffer data */
4016 DB_SHOW_COMMAND(buffer, db_show_buffer)
4019 struct buf *bp = (struct buf *)addr;
4022 db_printf("usage: show buffer <addr>\n");
4026 db_printf("buf at %p\n", bp);
4027 db_printf("b_flags = 0x%b, b_xflags=0x%b, b_vflags=0x%b\n",
4028 (u_int)bp->b_flags, PRINT_BUF_FLAGS, (u_int)bp->b_xflags,
4029 PRINT_BUF_XFLAGS, (u_int)bp->b_vflags, PRINT_BUF_VFLAGS);
4031 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
4032 "b_bufobj = (%p), b_data = %p, b_blkno = %jd, b_lblkno = %jd, "
4034 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
4035 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno,
4036 (intmax_t)bp->b_lblkno, bp->b_dep.lh_first);
4039 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
4040 for (i = 0; i < bp->b_npages; i++) {
4043 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
4044 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
4045 if ((i + 1) < bp->b_npages)
4051 BUF_LOCKPRINTINFO(bp);
4054 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
4059 for (i = 0; i < nbuf; i++) {
4061 if (BUF_ISLOCKED(bp)) {
4062 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4068 DB_SHOW_COMMAND(vnodebufs, db_show_vnodebufs)
4074 db_printf("usage: show vnodebufs <addr>\n");
4077 vp = (struct vnode *)addr;
4078 db_printf("Clean buffers:\n");
4079 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_clean.bv_hd, b_bobufs) {
4080 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4083 db_printf("Dirty buffers:\n");
4084 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
4085 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4090 DB_COMMAND(countfreebufs, db_coundfreebufs)
4093 int i, used = 0, nfree = 0;
4096 db_printf("usage: countfreebufs\n");
4100 for (i = 0; i < nbuf; i++) {
4102 if ((bp->b_vflags & BV_INFREECNT) != 0)
4108 db_printf("Counted %d free, %d used (%d tot)\n", nfree, used,
4110 db_printf("numfreebuffers is %d\n", numfreebuffers);