2 * Copyright (c) 2004 Poul-Henning Kamp
3 * Copyright (c) 1994,1997 John S. Dyson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * this file contains a new buffer I/O scheme implementing a coherent
30 * VM object and buffer cache scheme. Pains have been taken to make
31 * sure that the performance degradation associated with schemes such
32 * as this is not realized.
34 * Author: John S. Dyson
35 * Significant help during the development and debugging phases
36 * had been provided by David Greenman, also of the FreeBSD core team.
38 * see man buf(9) for more info.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
49 #include <sys/devicestat.h>
50 #include <sys/eventhandler.h>
52 #include <sys/limits.h>
54 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
60 #include <sys/resourcevar.h>
61 #include <sys/sysctl.h>
62 #include <sys/vmmeter.h>
63 #include <sys/vnode.h>
64 #include <geom/geom.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_pageout.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_map.h>
73 #include "opt_compat.h"
74 #include "opt_directio.h"
77 static MALLOC_DEFINE(M_BIOBUF, "biobuf", "BIO buffer");
79 struct bio_ops bioops; /* I/O operation notification */
81 struct buf_ops buf_ops_bio = {
82 .bop_name = "buf_ops_bio",
83 .bop_write = bufwrite,
84 .bop_strategy = bufstrategy,
86 .bop_bdflush = bufbdflush,
90 * XXX buf is global because kern_shutdown.c and ffs_checkoverlap has
91 * carnal knowledge of buffers. This knowledge should be moved to vfs_bio.c.
93 struct buf *buf; /* buffer header pool */
95 static struct proc *bufdaemonproc;
97 static int inmem(struct vnode *vp, daddr_t blkno);
98 static void vm_hold_free_pages(struct buf *bp, vm_offset_t from,
100 static void vm_hold_load_pages(struct buf *bp, vm_offset_t from,
102 static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m);
103 static void vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off,
105 static void vfs_drain_busy_pages(struct buf *bp);
106 static void vfs_clean_pages_dirty_buf(struct buf *bp);
107 static void vfs_setdirty_locked_object(struct buf *bp);
108 static void vfs_vmio_release(struct buf *bp);
109 static int vfs_bio_clcheck(struct vnode *vp, int size,
110 daddr_t lblkno, daddr_t blkno);
111 static int buf_do_flush(struct vnode *vp);
112 static int flushbufqueues(struct vnode *, int, int);
113 static void buf_daemon(void);
114 static void bremfreel(struct buf *bp);
115 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
116 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
117 static int sysctl_bufspace(SYSCTL_HANDLER_ARGS);
120 int vmiodirenable = TRUE;
121 SYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW, &vmiodirenable, 0,
122 "Use the VM system for directory writes");
123 long runningbufspace;
124 SYSCTL_LONG(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0,
125 "Amount of presently outstanding async buffer io");
126 static long bufspace;
127 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
128 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
129 SYSCTL_PROC(_vfs, OID_AUTO, bufspace, CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RD,
130 &bufspace, 0, sysctl_bufspace, "L", "Virtual memory used for buffers");
132 SYSCTL_LONG(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0,
133 "Virtual memory used for buffers");
135 static long maxbufspace;
136 SYSCTL_LONG(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0,
137 "Maximum allowed value of bufspace (including buf_daemon)");
138 static long bufmallocspace;
139 SYSCTL_LONG(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0,
140 "Amount of malloced memory for buffers");
141 static long maxbufmallocspace;
142 SYSCTL_LONG(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace, 0,
143 "Maximum amount of malloced memory for buffers");
144 static long lobufspace;
145 SYSCTL_LONG(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0,
146 "Minimum amount of buffers we want to have");
148 SYSCTL_LONG(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0,
149 "Maximum allowed value of bufspace (excluding buf_daemon)");
150 static int bufreusecnt;
151 SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RW, &bufreusecnt, 0,
152 "Number of times we have reused a buffer");
153 static int buffreekvacnt;
154 SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt, 0,
155 "Number of times we have freed the KVA space from some buffer");
156 static int bufdefragcnt;
157 SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt, 0,
158 "Number of times we have had to repeat buffer allocation to defragment");
159 static long lorunningspace;
160 SYSCTL_LONG(_vfs, OID_AUTO, lorunningspace, CTLFLAG_RW, &lorunningspace, 0,
161 "Minimum preferred space used for in-progress I/O");
162 static long hirunningspace;
163 SYSCTL_LONG(_vfs, OID_AUTO, hirunningspace, CTLFLAG_RW, &hirunningspace, 0,
164 "Maximum amount of space to use for in-progress I/O");
165 int dirtybufferflushes;
166 SYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes,
167 0, "Number of bdwrite to bawrite conversions to limit dirty buffers");
169 SYSCTL_INT(_vfs, OID_AUTO, bdwriteskip, CTLFLAG_RW, &bdwriteskip,
170 0, "Number of buffers supplied to bdwrite with snapshot deadlock risk");
171 int altbufferflushes;
172 SYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes,
173 0, "Number of fsync flushes to limit dirty buffers");
174 static int recursiveflushes;
175 SYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes,
176 0, "Number of flushes skipped due to being recursive");
177 static int numdirtybuffers;
178 SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0,
179 "Number of buffers that are dirty (has unwritten changes) at the moment");
180 static int lodirtybuffers;
181 SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0,
182 "How many buffers we want to have free before bufdaemon can sleep");
183 static int hidirtybuffers;
184 SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0,
185 "When the number of dirty buffers is considered severe");
187 SYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh,
188 0, "Number of bdwrite to bawrite conversions to clear dirty buffers");
189 static int numfreebuffers;
190 SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0,
191 "Number of free buffers");
192 static int lofreebuffers;
193 SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0,
195 static int hifreebuffers;
196 SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0,
197 "XXX Complicatedly unused");
198 static int getnewbufcalls;
199 SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW, &getnewbufcalls, 0,
200 "Number of calls to getnewbuf");
201 static int getnewbufrestarts;
202 SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW, &getnewbufrestarts, 0,
203 "Number of times getnewbuf has had to restart a buffer aquisition");
204 static int flushbufqtarget = 100;
205 SYSCTL_INT(_vfs, OID_AUTO, flushbufqtarget, CTLFLAG_RW, &flushbufqtarget, 0,
206 "Amount of work to do in flushbufqueues when helping bufdaemon");
207 static long notbufdflashes;
208 SYSCTL_LONG(_vfs, OID_AUTO, notbufdflashes, CTLFLAG_RD, ¬bufdflashes, 0,
209 "Number of dirty buffer flushes done by the bufdaemon helpers");
212 * Wakeup point for bufdaemon, as well as indicator of whether it is already
213 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it
216 static int bd_request;
219 * Request for the buf daemon to write more buffers than is indicated by
220 * lodirtybuf. This may be necessary to push out excess dependencies or
221 * defragment the address space where a simple count of the number of dirty
222 * buffers is insufficient to characterize the demand for flushing them.
224 static int bd_speedupreq;
227 * This lock synchronizes access to bd_request.
229 static struct mtx bdlock;
232 * bogus page -- for I/O to/from partially complete buffers
233 * this is a temporary solution to the problem, but it is not
234 * really that bad. it would be better to split the buffer
235 * for input in the case of buffers partially already in memory,
236 * but the code is intricate enough already.
238 vm_page_t bogus_page;
241 * Synchronization (sleep/wakeup) variable for active buffer space requests.
242 * Set when wait starts, cleared prior to wakeup().
243 * Used in runningbufwakeup() and waitrunningbufspace().
245 static int runningbufreq;
248 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
249 * waitrunningbufspace().
251 static struct mtx rbreqlock;
254 * Synchronization (sleep/wakeup) variable for buffer requests.
255 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done
257 * Used in numdirtywakeup(), bufspacewakeup(), bufcountwakeup(), bwillwrite(),
258 * getnewbuf(), and getblk().
260 static int needsbuffer;
263 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it.
265 static struct mtx nblock;
268 * Definitions for the buffer free lists.
270 #define BUFFER_QUEUES 6 /* number of free buffer queues */
272 #define QUEUE_NONE 0 /* on no queue */
273 #define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */
274 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
275 #define QUEUE_DIRTY_GIANT 3 /* B_DELWRI buffers that need giant */
276 #define QUEUE_EMPTYKVA 4 /* empty buffer headers w/KVA assignment */
277 #define QUEUE_EMPTY 5 /* empty buffer headers */
278 #define QUEUE_SENTINEL 1024 /* not an queue index, but mark for sentinel */
280 /* Queues for free buffers with various properties */
281 static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } };
283 /* Lock for the bufqueues */
284 static struct mtx bqlock;
287 * Single global constant for BUF_WMESG, to avoid getting multiple references.
288 * buf_wmesg is referred from macros.
290 const char *buf_wmesg = BUF_WMESG;
292 #define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */
293 #define VFS_BIO_NEED_DIRTYFLUSH 0x02 /* waiting for dirty buffer flush */
294 #define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */
295 #define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */
297 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
298 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
300 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
305 if (sizeof(int) == sizeof(long) || req->oldlen >= sizeof(long))
306 return (sysctl_handle_long(oidp, arg1, arg2, req));
307 lvalue = *(long *)arg1;
308 if (lvalue > INT_MAX)
309 /* On overflow, still write out a long to trigger ENOMEM. */
310 return (sysctl_handle_long(oidp, &lvalue, 0, req));
312 return (sysctl_handle_int(oidp, &ivalue, 0, req));
317 extern void ffs_rawread_setup(void);
318 #endif /* DIRECTIO */
322 * If someone is blocked due to there being too many dirty buffers,
323 * and numdirtybuffers is now reasonable, wake them up.
327 numdirtywakeup(int level)
330 if (numdirtybuffers <= level) {
332 if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) {
333 needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH;
334 wakeup(&needsbuffer);
343 * Called when buffer space is potentially available for recovery.
344 * getnewbuf() will block on this flag when it is unable to free
345 * sufficient buffer space. Buffer space becomes recoverable when
346 * bp's get placed back in the queues.
354 * If someone is waiting for BUF space, wake them up. Even
355 * though we haven't freed the kva space yet, the waiting
356 * process will be able to now.
359 if (needsbuffer & VFS_BIO_NEED_BUFSPACE) {
360 needsbuffer &= ~VFS_BIO_NEED_BUFSPACE;
361 wakeup(&needsbuffer);
367 * runningbufwakeup() - in-progress I/O accounting.
371 runningbufwakeup(struct buf *bp)
374 if (bp->b_runningbufspace) {
375 atomic_subtract_long(&runningbufspace, bp->b_runningbufspace);
376 bp->b_runningbufspace = 0;
377 mtx_lock(&rbreqlock);
378 if (runningbufreq && runningbufspace <= lorunningspace) {
380 wakeup(&runningbufreq);
382 mtx_unlock(&rbreqlock);
389 * Called when a buffer has been added to one of the free queues to
390 * account for the buffer and to wakeup anyone waiting for free buffers.
391 * This typically occurs when large amounts of metadata are being handled
392 * by the buffer cache ( else buffer space runs out first, usually ).
396 bufcountwakeup(struct buf *bp)
400 KASSERT((bp->b_vflags & BV_INFREECNT) == 0,
401 ("buf %p already counted as free", bp));
402 bp->b_vflags |= BV_INFREECNT;
403 old = atomic_fetchadd_int(&numfreebuffers, 1);
404 KASSERT(old >= 0 && old < nbuf,
405 ("numfreebuffers climbed to %d", old + 1));
408 needsbuffer &= ~VFS_BIO_NEED_ANY;
409 if (numfreebuffers >= hifreebuffers)
410 needsbuffer &= ~VFS_BIO_NEED_FREE;
411 wakeup(&needsbuffer);
417 * waitrunningbufspace()
419 * runningbufspace is a measure of the amount of I/O currently
420 * running. This routine is used in async-write situations to
421 * prevent creating huge backups of pending writes to a device.
422 * Only asynchronous writes are governed by this function.
424 * Reads will adjust runningbufspace, but will not block based on it.
425 * The read load has a side effect of reducing the allowed write load.
427 * This does NOT turn an async write into a sync write. It waits
428 * for earlier writes to complete and generally returns before the
429 * caller's write has reached the device.
432 waitrunningbufspace(void)
435 mtx_lock(&rbreqlock);
436 while (runningbufspace > hirunningspace) {
438 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
440 mtx_unlock(&rbreqlock);
445 * vfs_buf_test_cache:
447 * Called when a buffer is extended. This function clears the B_CACHE
448 * bit if the newly extended portion of the buffer does not contain
453 vfs_buf_test_cache(struct buf *bp,
454 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
458 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
459 if (bp->b_flags & B_CACHE) {
460 int base = (foff + off) & PAGE_MASK;
461 if (vm_page_is_valid(m, base, size) == 0)
462 bp->b_flags &= ~B_CACHE;
466 /* Wake up the buffer daemon if necessary */
469 bd_wakeup(int dirtybuflevel)
473 if (bd_request == 0 && numdirtybuffers >= dirtybuflevel) {
481 * bd_speedup - speedup the buffer cache flushing code
491 if (bd_speedupreq == 0 || bd_request == 0)
501 * Calculating buffer cache scaling values and reserve space for buffer
502 * headers. This is called during low level kernel initialization and
503 * may be called more then once. We CANNOT write to the memory area
504 * being reserved at this time.
507 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
513 * physmem_est is in pages. Convert it to kilobytes (assumes
514 * PAGE_SIZE is >= 1K)
516 physmem_est = physmem_est * (PAGE_SIZE / 1024);
519 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
520 * For the first 64MB of ram nominally allocate sufficient buffers to
521 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
522 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing
523 * the buffer cache we limit the eventual kva reservation to
526 * factor represents the 1/4 x ram conversion.
529 int factor = 4 * BKVASIZE / 1024;
532 if (physmem_est > 4096)
533 nbuf += min((physmem_est - 4096) / factor,
535 if (physmem_est > 65536)
536 nbuf += (physmem_est - 65536) * 2 / (factor * 5);
538 if (maxbcache && nbuf > maxbcache / BKVASIZE)
539 nbuf = maxbcache / BKVASIZE;
544 /* XXX Avoid unsigned long overflows later on with maxbufspace. */
545 maxbuf = (LONG_MAX / 3) / BKVASIZE;
548 printf("Warning: nbufs lowered from %d to %ld\n", nbuf,
554 * swbufs are used as temporary holders for I/O, such as paging I/O.
555 * We have no less then 16 and no more then 256.
557 nswbuf = max(min(nbuf/4, 256), 16);
559 if (nswbuf < NSWBUF_MIN)
567 * Reserve space for the buffer cache buffers
570 v = (caddr_t)(swbuf + nswbuf);
572 v = (caddr_t)(buf + nbuf);
577 /* Initialize the buffer subsystem. Called before use of any buffers. */
584 mtx_init(&bqlock, "buf queue lock", NULL, MTX_DEF);
585 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
586 mtx_init(&nblock, "needsbuffer lock", NULL, MTX_DEF);
587 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
589 /* next, make a null set of free lists */
590 for (i = 0; i < BUFFER_QUEUES; i++)
591 TAILQ_INIT(&bufqueues[i]);
593 /* finally, initialize each buffer header and stick on empty q */
594 for (i = 0; i < nbuf; i++) {
596 bzero(bp, sizeof *bp);
597 bp->b_flags = B_INVAL; /* we're just an empty header */
598 bp->b_rcred = NOCRED;
599 bp->b_wcred = NOCRED;
600 bp->b_qindex = QUEUE_EMPTY;
601 bp->b_vflags = BV_INFREECNT; /* buf is counted as free */
603 LIST_INIT(&bp->b_dep);
605 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
609 * maxbufspace is the absolute maximum amount of buffer space we are
610 * allowed to reserve in KVM and in real terms. The absolute maximum
611 * is nominally used by buf_daemon. hibufspace is the nominal maximum
612 * used by most other processes. The differential is required to
613 * ensure that buf_daemon is able to run when other processes might
614 * be blocked waiting for buffer space.
616 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
617 * this may result in KVM fragmentation which is not handled optimally
620 maxbufspace = (long)nbuf * BKVASIZE;
621 hibufspace = lmax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10);
622 lobufspace = hibufspace - MAXBSIZE;
624 lorunningspace = 512 * 1024;
625 hirunningspace = 1024 * 1024;
628 * Limit the amount of malloc memory since it is wired permanently into
629 * the kernel space. Even though this is accounted for in the buffer
630 * allocation, we don't want the malloced region to grow uncontrolled.
631 * The malloc scheme improves memory utilization significantly on average
632 * (small) directories.
634 maxbufmallocspace = hibufspace / 20;
637 * Reduce the chance of a deadlock occuring by limiting the number
638 * of delayed-write dirty buffers we allow to stack up.
640 hidirtybuffers = nbuf / 4 + 20;
641 dirtybufthresh = hidirtybuffers * 9 / 10;
644 * To support extreme low-memory systems, make sure hidirtybuffers cannot
645 * eat up all available buffer space. This occurs when our minimum cannot
646 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming
647 * BKVASIZE'd (8K) buffers.
649 while ((long)hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
650 hidirtybuffers >>= 1;
652 lodirtybuffers = hidirtybuffers / 2;
655 * Try to keep the number of free buffers in the specified range,
656 * and give special processes (e.g. like buf_daemon) access to an
659 lofreebuffers = nbuf / 18 + 5;
660 hifreebuffers = 2 * lofreebuffers;
661 numfreebuffers = nbuf;
663 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
664 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
668 * bfreekva() - free the kva allocation for a buffer.
670 * Since this call frees up buffer space, we call bufspacewakeup().
673 bfreekva(struct buf *bp)
677 atomic_add_int(&buffreekvacnt, 1);
678 atomic_subtract_long(&bufspace, bp->b_kvasize);
679 vm_map_remove(buffer_map, (vm_offset_t) bp->b_kvabase,
680 (vm_offset_t) bp->b_kvabase + bp->b_kvasize);
689 * Mark the buffer for removal from the appropriate free list in brelse.
693 bremfree(struct buf *bp)
697 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
698 KASSERT((bp->b_flags & B_REMFREE) == 0,
699 ("bremfree: buffer %p already marked for delayed removal.", bp));
700 KASSERT(bp->b_qindex != QUEUE_NONE,
701 ("bremfree: buffer %p not on a queue.", bp));
704 bp->b_flags |= B_REMFREE;
705 /* Fixup numfreebuffers count. */
706 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
707 KASSERT((bp->b_vflags & BV_INFREECNT) != 0,
708 ("buf %p not counted in numfreebuffers", bp));
709 bp->b_vflags &= ~BV_INFREECNT;
710 old = atomic_fetchadd_int(&numfreebuffers, -1);
711 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
718 * Force an immediate removal from a free list. Used only in nfs when
719 * it abuses the b_freelist pointer.
722 bremfreef(struct buf *bp)
732 * Removes a buffer from the free list, must be called with the
736 bremfreel(struct buf *bp)
740 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X",
741 bp, bp->b_vp, bp->b_flags);
742 KASSERT(bp->b_qindex != QUEUE_NONE,
743 ("bremfreel: buffer %p not on a queue.", bp));
745 mtx_assert(&bqlock, MA_OWNED);
747 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
748 bp->b_qindex = QUEUE_NONE;
750 * If this was a delayed bremfree() we only need to remove the buffer
751 * from the queue and return the stats are already done.
753 if (bp->b_flags & B_REMFREE) {
754 bp->b_flags &= ~B_REMFREE;
758 * Fixup numfreebuffers count. If the buffer is invalid or not
759 * delayed-write, the buffer was free and we must decrement
762 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
763 KASSERT((bp->b_vflags & BV_INFREECNT) != 0,
764 ("buf %p not counted in numfreebuffers", bp));
765 bp->b_vflags &= ~BV_INFREECNT;
766 old = atomic_fetchadd_int(&numfreebuffers, -1);
767 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
773 * Get a buffer with the specified data. Look in the cache first. We
774 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
775 * is set, the buffer is valid and we do not have to do anything ( see
776 * getblk() ). This is really just a special case of breadn().
779 bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred,
783 return (breadn(vp, blkno, size, 0, 0, 0, cred, bpp));
787 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must
788 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set,
789 * the buffer is valid and we do not have to do anything.
792 breada(struct vnode * vp, daddr_t * rablkno, int * rabsize,
793 int cnt, struct ucred * cred)
798 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
799 if (inmem(vp, *rablkno))
801 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
803 if ((rabp->b_flags & B_CACHE) == 0) {
804 if (!TD_IS_IDLETHREAD(curthread))
805 curthread->td_ru.ru_inblock++;
806 rabp->b_flags |= B_ASYNC;
807 rabp->b_flags &= ~B_INVAL;
808 rabp->b_ioflags &= ~BIO_ERROR;
809 rabp->b_iocmd = BIO_READ;
810 if (rabp->b_rcred == NOCRED && cred != NOCRED)
811 rabp->b_rcred = crhold(cred);
812 vfs_busy_pages(rabp, 0);
814 rabp->b_iooffset = dbtob(rabp->b_blkno);
823 * Operates like bread, but also starts asynchronous I/O on
827 breadn(struct vnode * vp, daddr_t blkno, int size,
828 daddr_t * rablkno, int *rabsize,
829 int cnt, struct ucred * cred, struct buf **bpp)
832 int rv = 0, readwait = 0;
834 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
835 *bpp = bp = getblk(vp, blkno, size, 0, 0, 0);
837 /* if not found in cache, do some I/O */
838 if ((bp->b_flags & B_CACHE) == 0) {
839 if (!TD_IS_IDLETHREAD(curthread))
840 curthread->td_ru.ru_inblock++;
841 bp->b_iocmd = BIO_READ;
842 bp->b_flags &= ~B_INVAL;
843 bp->b_ioflags &= ~BIO_ERROR;
844 if (bp->b_rcred == NOCRED && cred != NOCRED)
845 bp->b_rcred = crhold(cred);
846 vfs_busy_pages(bp, 0);
847 bp->b_iooffset = dbtob(bp->b_blkno);
852 breada(vp, rablkno, rabsize, cnt, cred);
861 * Write, release buffer on completion. (Done by iodone
862 * if async). Do not bother writing anything if the buffer
865 * Note that we set B_CACHE here, indicating that buffer is
866 * fully valid and thus cacheable. This is true even of NFS
867 * now so we set it generally. This could be set either here
868 * or in biodone() since the I/O is synchronous. We put it
872 bufwrite(struct buf *bp)
878 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
879 if (bp->b_flags & B_INVAL) {
884 oldflags = bp->b_flags;
888 if (bp->b_pin_count > 0)
891 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
892 ("FFS background buffer should not get here %p", bp));
896 vp_md = vp->v_vflag & VV_MD;
900 /* Mark the buffer clean */
903 bp->b_flags &= ~B_DONE;
904 bp->b_ioflags &= ~BIO_ERROR;
905 bp->b_flags |= B_CACHE;
906 bp->b_iocmd = BIO_WRITE;
908 bufobj_wref(bp->b_bufobj);
909 vfs_busy_pages(bp, 1);
912 * Normal bwrites pipeline writes
914 bp->b_runningbufspace = bp->b_bufsize;
915 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
917 if (!TD_IS_IDLETHREAD(curthread))
918 curthread->td_ru.ru_oublock++;
919 if (oldflags & B_ASYNC)
921 bp->b_iooffset = dbtob(bp->b_blkno);
924 if ((oldflags & B_ASYNC) == 0) {
925 int rtval = bufwait(bp);
930 * don't allow the async write to saturate the I/O
931 * system. We will not deadlock here because
932 * we are blocking waiting for I/O that is already in-progress
933 * to complete. We do not block here if it is the update
934 * or syncer daemon trying to clean up as that can lead
937 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md)
938 waitrunningbufspace();
945 bufbdflush(struct bufobj *bo, struct buf *bp)
949 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
950 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread);
952 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
955 * Try to find a buffer to flush.
957 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
958 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
960 LK_EXCLUSIVE | LK_NOWAIT, NULL))
963 panic("bdwrite: found ourselves");
965 /* Don't countdeps with the bo lock held. */
966 if (buf_countdeps(nbp, 0)) {
971 if (nbp->b_flags & B_CLUSTEROK) {
977 dirtybufferflushes++;
986 * Delayed write. (Buffer is marked dirty). Do not bother writing
987 * anything if the buffer is marked invalid.
989 * Note that since the buffer must be completely valid, we can safely
990 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
991 * biodone() in order to prevent getblk from writing the buffer
995 bdwrite(struct buf *bp)
997 struct thread *td = curthread;
1001 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1002 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1003 BUF_ASSERT_HELD(bp);
1005 if (bp->b_flags & B_INVAL) {
1011 * If we have too many dirty buffers, don't create any more.
1012 * If we are wildly over our limit, then force a complete
1013 * cleanup. Otherwise, just keep the situation from getting
1014 * out of control. Note that we have to avoid a recursive
1015 * disaster and not try to clean up after our own cleanup!
1019 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) {
1020 td->td_pflags |= TDP_INBDFLUSH;
1022 td->td_pflags &= ~TDP_INBDFLUSH;
1028 * Set B_CACHE, indicating that the buffer is fully valid. This is
1029 * true even of NFS now.
1031 bp->b_flags |= B_CACHE;
1034 * This bmap keeps the system from needing to do the bmap later,
1035 * perhaps when the system is attempting to do a sync. Since it
1036 * is likely that the indirect block -- or whatever other datastructure
1037 * that the filesystem needs is still in memory now, it is a good
1038 * thing to do this. Note also, that if the pageout daemon is
1039 * requesting a sync -- there might not be enough memory to do
1040 * the bmap then... So, this is important to do.
1042 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
1043 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1047 * Set the *dirty* buffer range based upon the VM system dirty
1050 * Mark the buffer pages as clean. We need to do this here to
1051 * satisfy the vnode_pager and the pageout daemon, so that it
1052 * thinks that the pages have been "cleaned". Note that since
1053 * the pages are in a delayed write buffer -- the VFS layer
1054 * "will" see that the pages get written out on the next sync,
1055 * or perhaps the cluster will be completed.
1057 vfs_clean_pages_dirty_buf(bp);
1061 * Wakeup the buffer flushing daemon if we have a lot of dirty
1062 * buffers (midpoint between our recovery point and our stall
1065 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1068 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
1069 * due to the softdep code.
1076 * Turn buffer into delayed write request. We must clear BIO_READ and
1077 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
1078 * itself to properly update it in the dirty/clean lists. We mark it
1079 * B_DONE to ensure that any asynchronization of the buffer properly
1080 * clears B_DONE ( else a panic will occur later ).
1082 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
1083 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
1084 * should only be called if the buffer is known-good.
1086 * Since the buffer is not on a queue, we do not update the numfreebuffers
1089 * The buffer must be on QUEUE_NONE.
1092 bdirty(struct buf *bp)
1095 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
1096 bp, bp->b_vp, bp->b_flags);
1097 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1098 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1099 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
1100 BUF_ASSERT_HELD(bp);
1101 bp->b_flags &= ~(B_RELBUF);
1102 bp->b_iocmd = BIO_WRITE;
1104 if ((bp->b_flags & B_DELWRI) == 0) {
1105 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
1107 atomic_add_int(&numdirtybuffers, 1);
1108 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1115 * Clear B_DELWRI for buffer.
1117 * Since the buffer is not on a queue, we do not update the numfreebuffers
1120 * The buffer must be on QUEUE_NONE.
1124 bundirty(struct buf *bp)
1127 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1128 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1129 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1130 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
1131 BUF_ASSERT_HELD(bp);
1133 if (bp->b_flags & B_DELWRI) {
1134 bp->b_flags &= ~B_DELWRI;
1136 atomic_subtract_int(&numdirtybuffers, 1);
1137 numdirtywakeup(lodirtybuffers);
1140 * Since it is now being written, we can clear its deferred write flag.
1142 bp->b_flags &= ~B_DEFERRED;
1148 * Asynchronous write. Start output on a buffer, but do not wait for
1149 * it to complete. The buffer is released when the output completes.
1151 * bwrite() ( or the VOP routine anyway ) is responsible for handling
1152 * B_INVAL buffers. Not us.
1155 bawrite(struct buf *bp)
1158 bp->b_flags |= B_ASYNC;
1165 * Called prior to the locking of any vnodes when we are expecting to
1166 * write. We do not want to starve the buffer cache with too many
1167 * dirty buffers so we block here. By blocking prior to the locking
1168 * of any vnodes we attempt to avoid the situation where a locked vnode
1169 * prevents the various system daemons from flushing related buffers.
1176 if (numdirtybuffers >= hidirtybuffers) {
1178 while (numdirtybuffers >= hidirtybuffers) {
1180 needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH;
1181 msleep(&needsbuffer, &nblock,
1182 (PRIBIO + 4), "flswai", 0);
1184 mtx_unlock(&nblock);
1189 * Return true if we have too many dirty buffers.
1192 buf_dirty_count_severe(void)
1195 return(numdirtybuffers >= hidirtybuffers);
1198 static __noinline int
1199 buf_vm_page_count_severe(void)
1202 KFAIL_POINT_CODE(DEBUG_FP, buf_pressure, return 1);
1204 return vm_page_count_severe();
1210 * Release a busy buffer and, if requested, free its resources. The
1211 * buffer will be stashed in the appropriate bufqueue[] allowing it
1212 * to be accessed later as a cache entity or reused for other purposes.
1215 brelse(struct buf *bp)
1217 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
1218 bp, bp->b_vp, bp->b_flags);
1219 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1220 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1222 if (bp->b_flags & B_MANAGED) {
1227 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) &&
1228 bp->b_error == EIO && !(bp->b_flags & B_INVAL)) {
1230 * Failed write, redirty. Must clear BIO_ERROR to prevent
1231 * pages from being scrapped. If the error is anything
1232 * other than an I/O error (EIO), assume that retrying
1235 bp->b_ioflags &= ~BIO_ERROR;
1237 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
1238 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
1240 * Either a failed I/O or we were asked to free or not
1243 bp->b_flags |= B_INVAL;
1244 if (!LIST_EMPTY(&bp->b_dep))
1246 if (bp->b_flags & B_DELWRI) {
1247 atomic_subtract_int(&numdirtybuffers, 1);
1248 numdirtywakeup(lodirtybuffers);
1250 bp->b_flags &= ~(B_DELWRI | B_CACHE);
1251 if ((bp->b_flags & B_VMIO) == 0) {
1260 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release()
1261 * is called with B_DELWRI set, the underlying pages may wind up
1262 * getting freed causing a previous write (bdwrite()) to get 'lost'
1263 * because pages associated with a B_DELWRI bp are marked clean.
1265 * We still allow the B_INVAL case to call vfs_vmio_release(), even
1266 * if B_DELWRI is set.
1268 * If B_DELWRI is not set we may have to set B_RELBUF if we are low
1269 * on pages to return pages to the VM page queues.
1271 if (bp->b_flags & B_DELWRI)
1272 bp->b_flags &= ~B_RELBUF;
1273 else if (buf_vm_page_count_severe()) {
1275 * The locking of the BO_LOCK is not necessary since
1276 * BKGRDINPROG cannot be set while we hold the buf
1277 * lock, it can only be cleared if it is already
1281 if (!(bp->b_vflags & BV_BKGRDINPROG))
1282 bp->b_flags |= B_RELBUF;
1284 bp->b_flags |= B_RELBUF;
1288 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
1289 * constituted, not even NFS buffers now. Two flags effect this. If
1290 * B_INVAL, the struct buf is invalidated but the VM object is kept
1291 * around ( i.e. so it is trivial to reconstitute the buffer later ).
1293 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
1294 * invalidated. BIO_ERROR cannot be set for a failed write unless the
1295 * buffer is also B_INVAL because it hits the re-dirtying code above.
1297 * Normally we can do this whether a buffer is B_DELWRI or not. If
1298 * the buffer is an NFS buffer, it is tracking piecemeal writes or
1299 * the commit state and we cannot afford to lose the buffer. If the
1300 * buffer has a background write in progress, we need to keep it
1301 * around to prevent it from being reconstituted and starting a second
1304 if ((bp->b_flags & B_VMIO)
1305 && !(bp->b_vp->v_mount != NULL &&
1306 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
1307 !vn_isdisk(bp->b_vp, NULL) &&
1308 (bp->b_flags & B_DELWRI))
1317 obj = bp->b_bufobj->bo_object;
1320 * Get the base offset and length of the buffer. Note that
1321 * in the VMIO case if the buffer block size is not
1322 * page-aligned then b_data pointer may not be page-aligned.
1323 * But our b_pages[] array *IS* page aligned.
1325 * block sizes less then DEV_BSIZE (usually 512) are not
1326 * supported due to the page granularity bits (m->valid,
1327 * m->dirty, etc...).
1329 * See man buf(9) for more information
1331 resid = bp->b_bufsize;
1332 foff = bp->b_offset;
1333 VM_OBJECT_LOCK(obj);
1334 for (i = 0; i < bp->b_npages; i++) {
1340 * If we hit a bogus page, fixup *all* the bogus pages
1343 if (m == bogus_page) {
1344 poff = OFF_TO_IDX(bp->b_offset);
1347 for (j = i; j < bp->b_npages; j++) {
1349 mtmp = bp->b_pages[j];
1350 if (mtmp == bogus_page) {
1351 mtmp = vm_page_lookup(obj, poff + j);
1353 panic("brelse: page missing\n");
1355 bp->b_pages[j] = mtmp;
1359 if ((bp->b_flags & B_INVAL) == 0) {
1361 trunc_page((vm_offset_t)bp->b_data),
1362 bp->b_pages, bp->b_npages);
1366 if ((bp->b_flags & B_NOCACHE) ||
1367 (bp->b_ioflags & BIO_ERROR &&
1368 bp->b_iocmd == BIO_READ)) {
1369 int poffset = foff & PAGE_MASK;
1370 int presid = resid > (PAGE_SIZE - poffset) ?
1371 (PAGE_SIZE - poffset) : resid;
1373 KASSERT(presid >= 0, ("brelse: extra page"));
1374 vm_page_set_invalid(m, poffset, presid);
1376 printf("avoided corruption bug in bogus_page/brelse code\n");
1378 resid -= PAGE_SIZE - (foff & PAGE_MASK);
1379 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
1381 VM_OBJECT_UNLOCK(obj);
1382 if (bp->b_flags & (B_INVAL | B_RELBUF))
1383 vfs_vmio_release(bp);
1385 } else if (bp->b_flags & B_VMIO) {
1387 if (bp->b_flags & (B_INVAL | B_RELBUF)) {
1388 vfs_vmio_release(bp);
1391 } else if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0) {
1392 if (bp->b_bufsize != 0)
1394 if (bp->b_vp != NULL)
1398 if (BUF_LOCKRECURSED(bp)) {
1399 /* do not release to free list */
1406 /* Handle delayed bremfree() processing. */
1407 if (bp->b_flags & B_REMFREE)
1409 if (bp->b_qindex != QUEUE_NONE)
1410 panic("brelse: free buffer onto another queue???");
1413 * If the buffer has junk contents signal it and eventually
1414 * clean up B_DELWRI and diassociate the vnode so that gbincore()
1417 if (bp->b_bufsize == 0 || (bp->b_ioflags & BIO_ERROR) != 0 ||
1418 (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) != 0)
1419 bp->b_flags |= B_INVAL;
1420 if (bp->b_flags & B_INVAL) {
1421 if (bp->b_flags & B_DELWRI)
1427 /* buffers with no memory */
1428 if (bp->b_bufsize == 0) {
1429 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1430 if (bp->b_vflags & BV_BKGRDINPROG)
1431 panic("losing buffer 1");
1432 if (bp->b_kvasize) {
1433 bp->b_qindex = QUEUE_EMPTYKVA;
1435 bp->b_qindex = QUEUE_EMPTY;
1437 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1438 /* buffers with junk contents */
1439 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
1440 (bp->b_ioflags & BIO_ERROR)) {
1441 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1442 if (bp->b_vflags & BV_BKGRDINPROG)
1443 panic("losing buffer 2");
1444 bp->b_qindex = QUEUE_CLEAN;
1445 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1446 /* remaining buffers */
1448 if ((bp->b_flags & (B_DELWRI|B_NEEDSGIANT)) ==
1449 (B_DELWRI|B_NEEDSGIANT))
1450 bp->b_qindex = QUEUE_DIRTY_GIANT;
1451 else if (bp->b_flags & B_DELWRI)
1452 bp->b_qindex = QUEUE_DIRTY;
1454 bp->b_qindex = QUEUE_CLEAN;
1455 if (bp->b_flags & B_AGE)
1456 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1458 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1460 mtx_unlock(&bqlock);
1463 * Fixup numfreebuffers count. The bp is on an appropriate queue
1464 * unless locked. We then bump numfreebuffers if it is not B_DELWRI.
1465 * We've already handled the B_INVAL case ( B_DELWRI will be clear
1466 * if B_INVAL is set ).
1469 if (!(bp->b_flags & B_DELWRI))
1473 * Something we can maybe free or reuse
1475 if (bp->b_bufsize || bp->b_kvasize)
1478 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT);
1479 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1480 panic("brelse: not dirty");
1486 * Release a buffer back to the appropriate queue but do not try to free
1487 * it. The buffer is expected to be used again soon.
1489 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1490 * biodone() to requeue an async I/O on completion. It is also used when
1491 * known good buffers need to be requeued but we think we may need the data
1494 * XXX we should be able to leave the B_RELBUF hint set on completion.
1497 bqrelse(struct buf *bp)
1499 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1500 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1501 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1503 if (BUF_LOCKRECURSED(bp)) {
1504 /* do not release to free list */
1509 if (bp->b_flags & B_MANAGED) {
1510 if (bp->b_flags & B_REMFREE) {
1513 mtx_unlock(&bqlock);
1515 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1521 /* Handle delayed bremfree() processing. */
1522 if (bp->b_flags & B_REMFREE)
1524 if (bp->b_qindex != QUEUE_NONE)
1525 panic("bqrelse: free buffer onto another queue???");
1526 /* buffers with stale but valid contents */
1527 if (bp->b_flags & B_DELWRI) {
1528 if (bp->b_flags & B_NEEDSGIANT)
1529 bp->b_qindex = QUEUE_DIRTY_GIANT;
1531 bp->b_qindex = QUEUE_DIRTY;
1532 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1535 * The locking of the BO_LOCK for checking of the
1536 * BV_BKGRDINPROG is not necessary since the
1537 * BV_BKGRDINPROG cannot be set while we hold the buf
1538 * lock, it can only be cleared if it is already
1541 if (!buf_vm_page_count_severe() || (bp->b_vflags & BV_BKGRDINPROG)) {
1542 bp->b_qindex = QUEUE_CLEAN;
1543 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp,
1547 * We are too low on memory, we have to try to free
1548 * the buffer (most importantly: the wired pages
1549 * making up its backing store) *now*.
1551 mtx_unlock(&bqlock);
1556 mtx_unlock(&bqlock);
1558 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI))
1562 * Something we can maybe free or reuse.
1564 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1567 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1568 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1569 panic("bqrelse: not dirty");
1574 /* Give pages used by the bp back to the VM system (where possible) */
1576 vfs_vmio_release(struct buf *bp)
1581 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
1582 for (i = 0; i < bp->b_npages; i++) {
1584 bp->b_pages[i] = NULL;
1586 * In order to keep page LRU ordering consistent, put
1587 * everything on the inactive queue.
1590 vm_page_unwire(m, 0);
1592 * We don't mess with busy pages, it is
1593 * the responsibility of the process that
1594 * busied the pages to deal with them.
1596 if ((m->oflags & VPO_BUSY) == 0 && m->busy == 0 &&
1597 m->wire_count == 0) {
1599 * Might as well free the page if we can and it has
1600 * no valid data. We also free the page if the
1601 * buffer was used for direct I/O
1603 if ((bp->b_flags & B_ASYNC) == 0 && !m->valid &&
1604 m->hold_count == 0) {
1606 } else if (bp->b_flags & B_DIRECT) {
1607 vm_page_try_to_free(m);
1608 } else if (buf_vm_page_count_severe()) {
1609 vm_page_try_to_cache(m);
1614 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
1615 pmap_qremove(trunc_page((vm_offset_t) bp->b_data), bp->b_npages);
1617 if (bp->b_bufsize) {
1622 bp->b_flags &= ~B_VMIO;
1628 * Check to see if a block at a particular lbn is available for a clustered
1632 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
1639 /* If the buf isn't in core skip it */
1640 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
1643 /* If the buf is busy we don't want to wait for it */
1644 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1647 /* Only cluster with valid clusterable delayed write buffers */
1648 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
1649 (B_DELWRI | B_CLUSTEROK))
1652 if (bpa->b_bufsize != size)
1656 * Check to see if it is in the expected place on disk and that the
1657 * block has been mapped.
1659 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
1669 * Implement clustered async writes for clearing out B_DELWRI buffers.
1670 * This is much better then the old way of writing only one buffer at
1671 * a time. Note that we may not be presented with the buffers in the
1672 * correct order, so we search for the cluster in both directions.
1675 vfs_bio_awrite(struct buf *bp)
1680 daddr_t lblkno = bp->b_lblkno;
1681 struct vnode *vp = bp->b_vp;
1689 * right now we support clustered writing only to regular files. If
1690 * we find a clusterable block we could be in the middle of a cluster
1691 * rather then at the beginning.
1693 if ((vp->v_type == VREG) &&
1694 (vp->v_mount != 0) && /* Only on nodes that have the size info */
1695 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
1697 size = vp->v_mount->mnt_stat.f_iosize;
1698 maxcl = MAXPHYS / size;
1701 for (i = 1; i < maxcl; i++)
1702 if (vfs_bio_clcheck(vp, size, lblkno + i,
1703 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
1706 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
1707 if (vfs_bio_clcheck(vp, size, lblkno - j,
1708 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
1714 * this is a possible cluster write
1718 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl);
1723 bp->b_flags |= B_ASYNC;
1725 * default (old) behavior, writing out only one block
1727 * XXX returns b_bufsize instead of b_bcount for nwritten?
1729 nwritten = bp->b_bufsize;
1738 * Find and initialize a new buffer header, freeing up existing buffers
1739 * in the bufqueues as necessary. The new buffer is returned locked.
1741 * Important: B_INVAL is not set. If the caller wishes to throw the
1742 * buffer away, the caller must set B_INVAL prior to calling brelse().
1745 * We have insufficient buffer headers
1746 * We have insufficient buffer space
1747 * buffer_map is too fragmented ( space reservation fails )
1748 * If we have to flush dirty buffers ( but we try to avoid this )
1750 * To avoid VFS layer recursion we do not flush dirty buffers ourselves.
1751 * Instead we ask the buf daemon to do it for us. We attempt to
1752 * avoid piecemeal wakeups of the pageout daemon.
1756 getnewbuf(struct vnode *vp, int slpflag, int slptimeo, int size, int maxsize,
1764 static int flushingbufs;
1768 * We can't afford to block since we might be holding a vnode lock,
1769 * which may prevent system daemons from running. We deal with
1770 * low-memory situations by proactively returning memory and running
1771 * async I/O rather then sync I/O.
1773 atomic_add_int(&getnewbufcalls, 1);
1774 atomic_subtract_int(&getnewbufrestarts, 1);
1776 atomic_add_int(&getnewbufrestarts, 1);
1779 * Setup for scan. If we do not have enough free buffers,
1780 * we setup a degenerate case that immediately fails. Note
1781 * that if we are specially marked process, we are allowed to
1782 * dip into our reserves.
1784 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN
1786 * We start with EMPTYKVA. If the list is empty we backup to EMPTY.
1787 * However, there are a number of cases (defragging, reusing, ...)
1788 * where we cannot backup.
1791 nqindex = QUEUE_EMPTYKVA;
1792 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]);
1796 * If no EMPTYKVA buffers and we are either
1797 * defragging or reusing, locate a CLEAN buffer
1798 * to free or reuse. If bufspace useage is low
1799 * skip this step so we can allocate a new buffer.
1801 if (defrag || bufspace >= lobufspace) {
1802 nqindex = QUEUE_CLEAN;
1803 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
1807 * If we could not find or were not allowed to reuse a
1808 * CLEAN buffer, check to see if it is ok to use an EMPTY
1809 * buffer. We can only use an EMPTY buffer if allocating
1810 * its KVA would not otherwise run us out of buffer space.
1812 if (nbp == NULL && defrag == 0 &&
1813 bufspace + maxsize < hibufspace) {
1814 nqindex = QUEUE_EMPTY;
1815 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1820 * Run scan, possibly freeing data and/or kva mappings on the fly
1824 while ((bp = nbp) != NULL) {
1825 int qindex = nqindex;
1828 * Calculate next bp ( we can only use it if we do not block
1829 * or do other fancy things ).
1831 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
1834 nqindex = QUEUE_EMPTYKVA;
1835 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA])))
1838 case QUEUE_EMPTYKVA:
1839 nqindex = QUEUE_CLEAN;
1840 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN])))
1851 * If we are defragging then we need a buffer with
1852 * b_kvasize != 0. XXX this situation should no longer
1853 * occur, if defrag is non-zero the buffer's b_kvasize
1854 * should also be non-zero at this point. XXX
1856 if (defrag && bp->b_kvasize == 0) {
1857 printf("Warning: defrag empty buffer %p\n", bp);
1862 * Start freeing the bp. This is somewhat involved. nbp
1863 * remains valid only for QUEUE_EMPTY[KVA] bp's.
1865 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1868 BO_LOCK(bp->b_bufobj);
1869 if (bp->b_vflags & BV_BKGRDINPROG) {
1870 BO_UNLOCK(bp->b_bufobj);
1874 BO_UNLOCK(bp->b_bufobj);
1877 "getnewbuf(%p) vp %p flags %X kvasize %d bufsize %d "
1878 "queue %d (recycling)", bp, bp->b_vp, bp->b_flags,
1879 bp->b_kvasize, bp->b_bufsize, qindex);
1884 KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp));
1887 * Note: we no longer distinguish between VMIO and non-VMIO
1891 KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex));
1894 mtx_unlock(&bqlock);
1896 if (qindex == QUEUE_CLEAN) {
1897 if (bp->b_flags & B_VMIO) {
1898 bp->b_flags &= ~B_ASYNC;
1899 vfs_vmio_release(bp);
1906 * NOTE: nbp is now entirely invalid. We can only restart
1907 * the scan from this point on.
1909 * Get the rest of the buffer freed up. b_kva* is still
1910 * valid after this operation.
1913 if (bp->b_rcred != NOCRED) {
1914 crfree(bp->b_rcred);
1915 bp->b_rcred = NOCRED;
1917 if (bp->b_wcred != NOCRED) {
1918 crfree(bp->b_wcred);
1919 bp->b_wcred = NOCRED;
1921 if (!LIST_EMPTY(&bp->b_dep))
1923 if (bp->b_vflags & BV_BKGRDINPROG)
1924 panic("losing buffer 3");
1925 KASSERT(bp->b_vp == NULL,
1926 ("bp: %p still has vnode %p. qindex: %d",
1927 bp, bp->b_vp, qindex));
1928 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
1929 ("bp: %p still on a buffer list. xflags %X",
1938 KASSERT((bp->b_vflags & BV_INFREECNT) == 0,
1939 ("buf %p still counted as free?", bp));
1942 bp->b_blkno = bp->b_lblkno = 0;
1943 bp->b_offset = NOOFFSET;
1949 bp->b_dirtyoff = bp->b_dirtyend = 0;
1950 bp->b_bufobj = NULL;
1951 bp->b_pin_count = 0;
1952 bp->b_fsprivate1 = NULL;
1953 bp->b_fsprivate2 = NULL;
1954 bp->b_fsprivate3 = NULL;
1956 LIST_INIT(&bp->b_dep);
1959 * If we are defragging then free the buffer.
1962 bp->b_flags |= B_INVAL;
1970 * Notify any waiters for the buffer lock about
1971 * identity change by freeing the buffer.
1973 if (qindex == QUEUE_CLEAN && BUF_LOCKWAITERS(bp)) {
1974 bp->b_flags |= B_INVAL;
1981 * If we are overcomitted then recover the buffer and its
1982 * KVM space. This occurs in rare situations when multiple
1983 * processes are blocked in getnewbuf() or allocbuf().
1985 if (bufspace >= hibufspace)
1987 if (flushingbufs && bp->b_kvasize != 0) {
1988 bp->b_flags |= B_INVAL;
1993 if (bufspace < lobufspace)
1999 * If we exhausted our list, sleep as appropriate. We may have to
2000 * wakeup various daemons and write out some dirty buffers.
2002 * Generally we are sleeping due to insufficient buffer space.
2006 int flags, norunbuf;
2011 flags = VFS_BIO_NEED_BUFSPACE;
2013 } else if (bufspace >= hibufspace) {
2015 flags = VFS_BIO_NEED_BUFSPACE;
2018 flags = VFS_BIO_NEED_ANY;
2021 needsbuffer |= flags;
2022 mtx_unlock(&nblock);
2023 mtx_unlock(&bqlock);
2025 bd_speedup(); /* heeeelp */
2026 if (gbflags & GB_NOWAIT_BD)
2030 while (needsbuffer & flags) {
2031 if (vp != NULL && (td->td_pflags & TDP_BUFNEED) == 0) {
2032 mtx_unlock(&nblock);
2034 * getblk() is called with a vnode
2035 * locked, and some majority of the
2036 * dirty buffers may as well belong to
2037 * the vnode. Flushing the buffers
2038 * there would make a progress that
2039 * cannot be achieved by the
2040 * buf_daemon, that cannot lock the
2043 norunbuf = ~(TDP_BUFNEED | TDP_NORUNNINGBUF) |
2044 (td->td_pflags & TDP_NORUNNINGBUF);
2045 /* play bufdaemon */
2046 td->td_pflags |= TDP_BUFNEED | TDP_NORUNNINGBUF;
2047 fl = buf_do_flush(vp);
2048 td->td_pflags &= norunbuf;
2052 if ((needsbuffer & flags) == 0)
2055 if (msleep(&needsbuffer, &nblock,
2056 (PRIBIO + 4) | slpflag, waitmsg, slptimeo)) {
2057 mtx_unlock(&nblock);
2061 mtx_unlock(&nblock);
2064 * We finally have a valid bp. We aren't quite out of the
2065 * woods, we still have to reserve kva space. In order
2066 * to keep fragmentation sane we only allocate kva in
2069 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
2071 if (maxsize != bp->b_kvasize) {
2072 vm_offset_t addr = 0;
2076 vm_map_lock(buffer_map);
2077 if (vm_map_findspace(buffer_map,
2078 vm_map_min(buffer_map), maxsize, &addr)) {
2080 * Uh oh. Buffer map is to fragmented. We
2081 * must defragment the map.
2083 atomic_add_int(&bufdefragcnt, 1);
2084 vm_map_unlock(buffer_map);
2086 bp->b_flags |= B_INVAL;
2091 vm_map_insert(buffer_map, NULL, 0,
2092 addr, addr + maxsize,
2093 VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
2095 bp->b_kvabase = (caddr_t) addr;
2096 bp->b_kvasize = maxsize;
2097 atomic_add_long(&bufspace, bp->b_kvasize);
2098 atomic_add_int(&bufreusecnt, 1);
2100 vm_map_unlock(buffer_map);
2102 bp->b_saveaddr = bp->b_kvabase;
2103 bp->b_data = bp->b_saveaddr;
2111 * buffer flushing daemon. Buffers are normally flushed by the
2112 * update daemon but if it cannot keep up this process starts to
2113 * take the load in an attempt to prevent getnewbuf() from blocking.
2116 static struct kproc_desc buf_kp = {
2121 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp);
2124 buf_do_flush(struct vnode *vp)
2128 flushed = flushbufqueues(vp, QUEUE_DIRTY, 0);
2129 /* The list empty check here is slightly racy */
2130 if (!TAILQ_EMPTY(&bufqueues[QUEUE_DIRTY_GIANT])) {
2132 flushed += flushbufqueues(vp, QUEUE_DIRTY_GIANT, 0);
2137 * Could not find any buffers without rollback
2138 * dependencies, so just write the first one
2139 * in the hopes of eventually making progress.
2141 flushbufqueues(vp, QUEUE_DIRTY, 1);
2143 &bufqueues[QUEUE_DIRTY_GIANT])) {
2145 flushbufqueues(vp, QUEUE_DIRTY_GIANT, 1);
2158 * This process needs to be suspended prior to shutdown sync.
2160 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
2164 * This process is allowed to take the buffer cache to the limit
2166 curthread->td_pflags |= TDP_NORUNNINGBUF | TDP_BUFNEED;
2170 mtx_unlock(&bdlock);
2172 kproc_suspend_check(bufdaemonproc);
2173 lodirtysave = lodirtybuffers;
2174 if (bd_speedupreq) {
2175 lodirtybuffers = numdirtybuffers / 2;
2179 * Do the flush. Limit the amount of in-transit I/O we
2180 * allow to build up, otherwise we would completely saturate
2181 * the I/O system. Wakeup any waiting processes before we
2182 * normally would so they can run in parallel with our drain.
2184 while (numdirtybuffers > lodirtybuffers) {
2185 if (buf_do_flush(NULL) == 0)
2189 lodirtybuffers = lodirtysave;
2192 * Only clear bd_request if we have reached our low water
2193 * mark. The buf_daemon normally waits 1 second and
2194 * then incrementally flushes any dirty buffers that have
2195 * built up, within reason.
2197 * If we were unable to hit our low water mark and couldn't
2198 * find any flushable buffers, we sleep half a second.
2199 * Otherwise we loop immediately.
2202 if (numdirtybuffers <= lodirtybuffers) {
2204 * We reached our low water mark, reset the
2205 * request and sleep until we are needed again.
2206 * The sleep is just so the suspend code works.
2209 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
2212 * We couldn't find any flushable dirty buffers but
2213 * still have too many dirty buffers, we
2214 * have to sleep and try again. (rare)
2216 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
2224 * Try to flush a buffer in the dirty queue. We must be careful to
2225 * free up B_INVAL buffers instead of write them, which NFS is
2226 * particularly sensitive to.
2228 static int flushwithdeps = 0;
2229 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
2230 0, "Number of buffers flushed with dependecies that require rollbacks");
2233 flushbufqueues(struct vnode *lvp, int queue, int flushdeps)
2235 struct buf *sentinel;
2244 target = numdirtybuffers - lodirtybuffers;
2245 if (flushdeps && target > 2)
2248 target = flushbufqtarget;
2251 sentinel = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
2252 sentinel->b_qindex = QUEUE_SENTINEL;
2254 TAILQ_INSERT_HEAD(&bufqueues[queue], sentinel, b_freelist);
2255 while (flushed != target) {
2256 bp = TAILQ_NEXT(sentinel, b_freelist);
2258 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2259 TAILQ_INSERT_AFTER(&bufqueues[queue], bp, sentinel,
2264 * Skip sentinels inserted by other invocations of the
2265 * flushbufqueues(), taking care to not reorder them.
2267 if (bp->b_qindex == QUEUE_SENTINEL)
2270 * Only flush the buffers that belong to the
2271 * vnode locked by the curthread.
2273 if (lvp != NULL && bp->b_vp != lvp)
2275 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2277 if (bp->b_pin_count > 0) {
2281 BO_LOCK(bp->b_bufobj);
2282 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
2283 (bp->b_flags & B_DELWRI) == 0) {
2284 BO_UNLOCK(bp->b_bufobj);
2288 BO_UNLOCK(bp->b_bufobj);
2289 if (bp->b_flags & B_INVAL) {
2291 mtx_unlock(&bqlock);
2294 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2299 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) {
2300 if (flushdeps == 0) {
2308 * We must hold the lock on a vnode before writing
2309 * one of its buffers. Otherwise we may confuse, or
2310 * in the case of a snapshot vnode, deadlock the
2313 * The lock order here is the reverse of the normal
2314 * of vnode followed by buf lock. This is ok because
2315 * the NOWAIT will prevent deadlock.
2318 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2322 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_CANRECURSE) == 0) {
2323 mtx_unlock(&bqlock);
2324 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
2325 bp, bp->b_vp, bp->b_flags);
2326 if (curproc == bufdaemonproc)
2333 vn_finished_write(mp);
2335 flushwithdeps += hasdeps;
2339 * Sleeping on runningbufspace while holding
2340 * vnode lock leads to deadlock.
2342 if (curproc == bufdaemonproc)
2343 waitrunningbufspace();
2344 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2348 vn_finished_write(mp);
2351 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2352 mtx_unlock(&bqlock);
2353 free(sentinel, M_TEMP);
2358 * Check to see if a block is currently memory resident.
2361 incore(struct bufobj *bo, daddr_t blkno)
2366 bp = gbincore(bo, blkno);
2372 * Returns true if no I/O is needed to access the
2373 * associated VM object. This is like incore except
2374 * it also hunts around in the VM system for the data.
2378 inmem(struct vnode * vp, daddr_t blkno)
2381 vm_offset_t toff, tinc, size;
2385 ASSERT_VOP_LOCKED(vp, "inmem");
2387 if (incore(&vp->v_bufobj, blkno))
2389 if (vp->v_mount == NULL)
2396 if (size > vp->v_mount->mnt_stat.f_iosize)
2397 size = vp->v_mount->mnt_stat.f_iosize;
2398 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
2400 VM_OBJECT_LOCK(obj);
2401 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
2402 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
2406 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
2407 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
2408 if (vm_page_is_valid(m,
2409 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
2412 VM_OBJECT_UNLOCK(obj);
2416 VM_OBJECT_UNLOCK(obj);
2421 * Set the dirty range for a buffer based on the status of the dirty
2422 * bits in the pages comprising the buffer. The range is limited
2423 * to the size of the buffer.
2425 * Tell the VM system that the pages associated with this buffer
2426 * are clean. This is used for delayed writes where the data is
2427 * going to go to disk eventually without additional VM intevention.
2429 * Note that while we only really need to clean through to b_bcount, we
2430 * just go ahead and clean through to b_bufsize.
2433 vfs_clean_pages_dirty_buf(struct buf *bp)
2435 vm_ooffset_t foff, noff, eoff;
2439 if ((bp->b_flags & B_VMIO) == 0 || bp->b_bufsize == 0)
2442 foff = bp->b_offset;
2443 KASSERT(bp->b_offset != NOOFFSET,
2444 ("vfs_clean_pages_dirty_buf: no buffer offset"));
2446 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
2447 vfs_drain_busy_pages(bp);
2448 vfs_setdirty_locked_object(bp);
2449 for (i = 0; i < bp->b_npages; i++) {
2450 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
2452 if (eoff > bp->b_offset + bp->b_bufsize)
2453 eoff = bp->b_offset + bp->b_bufsize;
2455 vfs_page_set_validclean(bp, foff, m);
2456 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
2459 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
2463 vfs_setdirty_locked_object(struct buf *bp)
2468 object = bp->b_bufobj->bo_object;
2469 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2472 * We qualify the scan for modified pages on whether the
2473 * object has been flushed yet.
2475 if (object->flags & (OBJ_MIGHTBEDIRTY|OBJ_CLEANING)) {
2476 vm_offset_t boffset;
2477 vm_offset_t eoffset;
2480 * test the pages to see if they have been modified directly
2481 * by users through the VM system.
2483 for (i = 0; i < bp->b_npages; i++)
2484 vm_page_test_dirty(bp->b_pages[i]);
2487 * Calculate the encompassing dirty range, boffset and eoffset,
2488 * (eoffset - boffset) bytes.
2491 for (i = 0; i < bp->b_npages; i++) {
2492 if (bp->b_pages[i]->dirty)
2495 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2497 for (i = bp->b_npages - 1; i >= 0; --i) {
2498 if (bp->b_pages[i]->dirty) {
2502 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2505 * Fit it to the buffer.
2508 if (eoffset > bp->b_bcount)
2509 eoffset = bp->b_bcount;
2512 * If we have a good dirty range, merge with the existing
2516 if (boffset < eoffset) {
2517 if (bp->b_dirtyoff > boffset)
2518 bp->b_dirtyoff = boffset;
2519 if (bp->b_dirtyend < eoffset)
2520 bp->b_dirtyend = eoffset;
2528 * Get a block given a specified block and offset into a file/device.
2529 * The buffers B_DONE bit will be cleared on return, making it almost
2530 * ready for an I/O initiation. B_INVAL may or may not be set on
2531 * return. The caller should clear B_INVAL prior to initiating a
2534 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
2535 * an existing buffer.
2537 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
2538 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
2539 * and then cleared based on the backing VM. If the previous buffer is
2540 * non-0-sized but invalid, B_CACHE will be cleared.
2542 * If getblk() must create a new buffer, the new buffer is returned with
2543 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
2544 * case it is returned with B_INVAL clear and B_CACHE set based on the
2547 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
2548 * B_CACHE bit is clear.
2550 * What this means, basically, is that the caller should use B_CACHE to
2551 * determine whether the buffer is fully valid or not and should clear
2552 * B_INVAL prior to issuing a read. If the caller intends to validate
2553 * the buffer by loading its data area with something, the caller needs
2554 * to clear B_INVAL. If the caller does this without issuing an I/O,
2555 * the caller should set B_CACHE ( as an optimization ), else the caller
2556 * should issue the I/O and biodone() will set B_CACHE if the I/O was
2557 * a write attempt or if it was a successfull read. If the caller
2558 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
2559 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
2562 getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo,
2569 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
2570 ASSERT_VOP_LOCKED(vp, "getblk");
2571 if (size > MAXBSIZE)
2572 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE);
2577 * Block if we are low on buffers. Certain processes are allowed
2578 * to completely exhaust the buffer cache.
2580 * If this check ever becomes a bottleneck it may be better to
2581 * move it into the else, when gbincore() fails. At the moment
2582 * it isn't a problem.
2584 * XXX remove if 0 sections (clean this up after its proven)
2586 if (numfreebuffers == 0) {
2587 if (TD_IS_IDLETHREAD(curthread))
2590 needsbuffer |= VFS_BIO_NEED_ANY;
2591 mtx_unlock(&nblock);
2595 bp = gbincore(bo, blkno);
2599 * Buffer is in-core. If the buffer is not busy, it must
2602 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
2604 if (flags & GB_LOCK_NOWAIT)
2605 lockflags |= LK_NOWAIT;
2607 error = BUF_TIMELOCK(bp, lockflags,
2608 BO_MTX(bo), "getblk", slpflag, slptimeo);
2611 * If we slept and got the lock we have to restart in case
2612 * the buffer changed identities.
2614 if (error == ENOLCK)
2616 /* We timed out or were interrupted. */
2621 * The buffer is locked. B_CACHE is cleared if the buffer is
2622 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
2623 * and for a VMIO buffer B_CACHE is adjusted according to the
2626 if (bp->b_flags & B_INVAL)
2627 bp->b_flags &= ~B_CACHE;
2628 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
2629 bp->b_flags |= B_CACHE;
2633 * check for size inconsistancies for non-VMIO case.
2636 if (bp->b_bcount != size) {
2637 if ((bp->b_flags & B_VMIO) == 0 ||
2638 (size > bp->b_kvasize)) {
2639 if (bp->b_flags & B_DELWRI) {
2641 * If buffer is pinned and caller does
2642 * not want sleep waiting for it to be
2643 * unpinned, bail out
2645 if (bp->b_pin_count > 0) {
2646 if (flags & GB_LOCK_NOWAIT) {
2653 bp->b_flags |= B_NOCACHE;
2656 if (LIST_EMPTY(&bp->b_dep)) {
2657 bp->b_flags |= B_RELBUF;
2660 bp->b_flags |= B_NOCACHE;
2669 * If the size is inconsistant in the VMIO case, we can resize
2670 * the buffer. This might lead to B_CACHE getting set or
2671 * cleared. If the size has not changed, B_CACHE remains
2672 * unchanged from its previous state.
2675 if (bp->b_bcount != size)
2678 KASSERT(bp->b_offset != NOOFFSET,
2679 ("getblk: no buffer offset"));
2682 * A buffer with B_DELWRI set and B_CACHE clear must
2683 * be committed before we can return the buffer in
2684 * order to prevent the caller from issuing a read
2685 * ( due to B_CACHE not being set ) and overwriting
2688 * Most callers, including NFS and FFS, need this to
2689 * operate properly either because they assume they
2690 * can issue a read if B_CACHE is not set, or because
2691 * ( for example ) an uncached B_DELWRI might loop due
2692 * to softupdates re-dirtying the buffer. In the latter
2693 * case, B_CACHE is set after the first write completes,
2694 * preventing further loops.
2695 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
2696 * above while extending the buffer, we cannot allow the
2697 * buffer to remain with B_CACHE set after the write
2698 * completes or it will represent a corrupt state. To
2699 * deal with this we set B_NOCACHE to scrap the buffer
2702 * We might be able to do something fancy, like setting
2703 * B_CACHE in bwrite() except if B_DELWRI is already set,
2704 * so the below call doesn't set B_CACHE, but that gets real
2705 * confusing. This is much easier.
2708 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
2709 bp->b_flags |= B_NOCACHE;
2713 bp->b_flags &= ~B_DONE;
2715 int bsize, maxsize, vmio;
2719 * Buffer is not in-core, create new buffer. The buffer
2720 * returned by getnewbuf() is locked. Note that the returned
2721 * buffer is also considered valid (not marked B_INVAL).
2725 * If the user does not want us to create the buffer, bail out
2728 if (flags & GB_NOCREAT)
2730 bsize = vn_isdisk(vp, NULL) ? DEV_BSIZE : bo->bo_bsize;
2731 offset = blkno * bsize;
2732 vmio = vp->v_object != NULL;
2733 maxsize = vmio ? size + (offset & PAGE_MASK) : size;
2734 maxsize = imax(maxsize, bsize);
2736 bp = getnewbuf(vp, slpflag, slptimeo, size, maxsize, flags);
2738 if (slpflag || slptimeo)
2744 * This code is used to make sure that a buffer is not
2745 * created while the getnewbuf routine is blocked.
2746 * This can be a problem whether the vnode is locked or not.
2747 * If the buffer is created out from under us, we have to
2748 * throw away the one we just created.
2750 * Note: this must occur before we associate the buffer
2751 * with the vp especially considering limitations in
2752 * the splay tree implementation when dealing with duplicate
2756 if (gbincore(bo, blkno)) {
2758 bp->b_flags |= B_INVAL;
2764 * Insert the buffer into the hash, so that it can
2765 * be found by incore.
2767 bp->b_blkno = bp->b_lblkno = blkno;
2768 bp->b_offset = offset;
2773 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
2774 * buffer size starts out as 0, B_CACHE will be set by
2775 * allocbuf() for the VMIO case prior to it testing the
2776 * backing store for validity.
2780 bp->b_flags |= B_VMIO;
2781 #if defined(VFS_BIO_DEBUG)
2782 if (vn_canvmio(vp) != TRUE)
2783 printf("getblk: VMIO on vnode type %d\n",
2786 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
2787 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
2788 bp, vp->v_object, bp->b_bufobj->bo_object));
2790 bp->b_flags &= ~B_VMIO;
2791 KASSERT(bp->b_bufobj->bo_object == NULL,
2792 ("ARGH! has b_bufobj->bo_object %p %p\n",
2793 bp, bp->b_bufobj->bo_object));
2797 bp->b_flags &= ~B_DONE;
2799 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
2800 BUF_ASSERT_HELD(bp);
2801 KASSERT(bp->b_bufobj == bo,
2802 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2807 * Get an empty, disassociated buffer of given size. The buffer is initially
2811 geteblk(int size, int flags)
2816 maxsize = (size + BKVAMASK) & ~BKVAMASK;
2817 while ((bp = getnewbuf(NULL, 0, 0, size, maxsize, flags)) == NULL) {
2818 if ((flags & GB_NOWAIT_BD) &&
2819 (curthread->td_pflags & TDP_BUFNEED) != 0)
2823 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
2824 BUF_ASSERT_HELD(bp);
2830 * This code constitutes the buffer memory from either anonymous system
2831 * memory (in the case of non-VMIO operations) or from an associated
2832 * VM object (in the case of VMIO operations). This code is able to
2833 * resize a buffer up or down.
2835 * Note that this code is tricky, and has many complications to resolve
2836 * deadlock or inconsistant data situations. Tread lightly!!!
2837 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
2838 * the caller. Calling this code willy nilly can result in the loss of data.
2840 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
2841 * B_CACHE for the non-VMIO case.
2845 allocbuf(struct buf *bp, int size)
2847 int newbsize, mbsize;
2850 BUF_ASSERT_HELD(bp);
2852 if (bp->b_kvasize < size)
2853 panic("allocbuf: buffer too small");
2855 if ((bp->b_flags & B_VMIO) == 0) {
2859 * Just get anonymous memory from the kernel. Don't
2860 * mess with B_CACHE.
2862 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2863 if (bp->b_flags & B_MALLOC)
2866 newbsize = round_page(size);
2868 if (newbsize < bp->b_bufsize) {
2870 * malloced buffers are not shrunk
2872 if (bp->b_flags & B_MALLOC) {
2874 bp->b_bcount = size;
2876 free(bp->b_data, M_BIOBUF);
2877 if (bp->b_bufsize) {
2878 atomic_subtract_long(
2884 bp->b_saveaddr = bp->b_kvabase;
2885 bp->b_data = bp->b_saveaddr;
2887 bp->b_flags &= ~B_MALLOC;
2893 (vm_offset_t) bp->b_data + newbsize,
2894 (vm_offset_t) bp->b_data + bp->b_bufsize);
2895 } else if (newbsize > bp->b_bufsize) {
2897 * We only use malloced memory on the first allocation.
2898 * and revert to page-allocated memory when the buffer
2902 * There is a potential smp race here that could lead
2903 * to bufmallocspace slightly passing the max. It
2904 * is probably extremely rare and not worth worrying
2907 if ( (bufmallocspace < maxbufmallocspace) &&
2908 (bp->b_bufsize == 0) &&
2909 (mbsize <= PAGE_SIZE/2)) {
2911 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK);
2912 bp->b_bufsize = mbsize;
2913 bp->b_bcount = size;
2914 bp->b_flags |= B_MALLOC;
2915 atomic_add_long(&bufmallocspace, mbsize);
2921 * If the buffer is growing on its other-than-first allocation,
2922 * then we revert to the page-allocation scheme.
2924 if (bp->b_flags & B_MALLOC) {
2925 origbuf = bp->b_data;
2926 origbufsize = bp->b_bufsize;
2927 bp->b_data = bp->b_kvabase;
2928 if (bp->b_bufsize) {
2929 atomic_subtract_long(&bufmallocspace,
2934 bp->b_flags &= ~B_MALLOC;
2935 newbsize = round_page(newbsize);
2939 (vm_offset_t) bp->b_data + bp->b_bufsize,
2940 (vm_offset_t) bp->b_data + newbsize);
2942 bcopy(origbuf, bp->b_data, origbufsize);
2943 free(origbuf, M_BIOBUF);
2949 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2950 desiredpages = (size == 0) ? 0 :
2951 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
2953 if (bp->b_flags & B_MALLOC)
2954 panic("allocbuf: VMIO buffer can't be malloced");
2956 * Set B_CACHE initially if buffer is 0 length or will become
2959 if (size == 0 || bp->b_bufsize == 0)
2960 bp->b_flags |= B_CACHE;
2962 if (newbsize < bp->b_bufsize) {
2964 * DEV_BSIZE aligned new buffer size is less then the
2965 * DEV_BSIZE aligned existing buffer size. Figure out
2966 * if we have to remove any pages.
2968 if (desiredpages < bp->b_npages) {
2971 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
2972 for (i = desiredpages; i < bp->b_npages; i++) {
2974 * the page is not freed here -- it
2975 * is the responsibility of
2976 * vnode_pager_setsize
2979 KASSERT(m != bogus_page,
2980 ("allocbuf: bogus page found"));
2981 while (vm_page_sleep_if_busy(m, TRUE,
2985 bp->b_pages[i] = NULL;
2987 vm_page_unwire(m, 0);
2990 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
2991 pmap_qremove((vm_offset_t) trunc_page((vm_offset_t)bp->b_data) +
2992 (desiredpages << PAGE_SHIFT), (bp->b_npages - desiredpages));
2993 bp->b_npages = desiredpages;
2995 } else if (size > bp->b_bcount) {
2997 * We are growing the buffer, possibly in a
2998 * byte-granular fashion.
3005 * Step 1, bring in the VM pages from the object,
3006 * allocating them if necessary. We must clear
3007 * B_CACHE if these pages are not valid for the
3008 * range covered by the buffer.
3011 obj = bp->b_bufobj->bo_object;
3013 VM_OBJECT_LOCK(obj);
3014 while (bp->b_npages < desiredpages) {
3018 * We must allocate system pages since blocking
3019 * here could intefere with paging I/O, no
3020 * matter which process we are.
3022 * We can only test VPO_BUSY here. Blocking on
3023 * m->busy might lead to a deadlock:
3024 * vm_fault->getpages->cluster_read->allocbuf
3025 * Thus, we specify VM_ALLOC_IGN_SBUSY.
3027 m = vm_page_grab(obj, OFF_TO_IDX(bp->b_offset) +
3028 bp->b_npages, VM_ALLOC_NOBUSY |
3029 VM_ALLOC_SYSTEM | VM_ALLOC_WIRED |
3030 VM_ALLOC_RETRY | VM_ALLOC_IGN_SBUSY |
3031 VM_ALLOC_COUNT(desiredpages - bp->b_npages));
3033 bp->b_flags &= ~B_CACHE;
3034 bp->b_pages[bp->b_npages] = m;
3039 * Step 2. We've loaded the pages into the buffer,
3040 * we have to figure out if we can still have B_CACHE
3041 * set. Note that B_CACHE is set according to the
3042 * byte-granular range ( bcount and size ), new the
3043 * aligned range ( newbsize ).
3045 * The VM test is against m->valid, which is DEV_BSIZE
3046 * aligned. Needless to say, the validity of the data
3047 * needs to also be DEV_BSIZE aligned. Note that this
3048 * fails with NFS if the server or some other client
3049 * extends the file's EOF. If our buffer is resized,
3050 * B_CACHE may remain set! XXX
3053 toff = bp->b_bcount;
3054 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
3056 while ((bp->b_flags & B_CACHE) && toff < size) {
3059 if (tinc > (size - toff))
3062 pi = ((bp->b_offset & PAGE_MASK) + toff) >>
3075 VM_OBJECT_UNLOCK(obj);
3078 * Step 3, fixup the KVM pmap. Remember that
3079 * bp->b_data is relative to bp->b_offset, but
3080 * bp->b_offset may be offset into the first page.
3083 bp->b_data = (caddr_t)
3084 trunc_page((vm_offset_t)bp->b_data);
3086 (vm_offset_t)bp->b_data,
3091 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
3092 (vm_offset_t)(bp->b_offset & PAGE_MASK));
3095 if (newbsize < bp->b_bufsize)
3097 bp->b_bufsize = newbsize; /* actual buffer allocation */
3098 bp->b_bcount = size; /* requested buffer size */
3103 biodone(struct bio *bp)
3106 void (*done)(struct bio *);
3108 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3110 bp->bio_flags |= BIO_DONE;
3111 done = bp->bio_done;
3120 * Wait for a BIO to finish.
3122 * XXX: resort to a timeout for now. The optimal locking (if any) for this
3123 * case is not yet clear.
3126 biowait(struct bio *bp, const char *wchan)
3130 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3132 while ((bp->bio_flags & BIO_DONE) == 0)
3133 msleep(bp, mtxp, PRIBIO, wchan, hz / 10);
3135 if (bp->bio_error != 0)
3136 return (bp->bio_error);
3137 if (!(bp->bio_flags & BIO_ERROR))
3143 biofinish(struct bio *bp, struct devstat *stat, int error)
3147 bp->bio_error = error;
3148 bp->bio_flags |= BIO_ERROR;
3151 devstat_end_transaction_bio(stat, bp);
3158 * Wait for buffer I/O completion, returning error status. The buffer
3159 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
3160 * error and cleared.
3163 bufwait(struct buf *bp)
3165 if (bp->b_iocmd == BIO_READ)
3166 bwait(bp, PRIBIO, "biord");
3168 bwait(bp, PRIBIO, "biowr");
3169 if (bp->b_flags & B_EINTR) {
3170 bp->b_flags &= ~B_EINTR;
3173 if (bp->b_ioflags & BIO_ERROR) {
3174 return (bp->b_error ? bp->b_error : EIO);
3181 * Call back function from struct bio back up to struct buf.
3184 bufdonebio(struct bio *bip)
3188 bp = bip->bio_caller2;
3189 bp->b_resid = bp->b_bcount - bip->bio_completed;
3190 bp->b_resid = bip->bio_resid; /* XXX: remove */
3191 bp->b_ioflags = bip->bio_flags;
3192 bp->b_error = bip->bio_error;
3194 bp->b_ioflags |= BIO_ERROR;
3200 dev_strategy(struct cdev *dev, struct buf *bp)
3205 if ((!bp->b_iocmd) || (bp->b_iocmd & (bp->b_iocmd - 1)))
3206 panic("b_iocmd botch");
3211 /* Try again later */
3212 tsleep(&bp, PRIBIO, "dev_strat", hz/10);
3214 bip->bio_cmd = bp->b_iocmd;
3215 bip->bio_offset = bp->b_iooffset;
3216 bip->bio_length = bp->b_bcount;
3217 bip->bio_bcount = bp->b_bcount; /* XXX: remove */
3218 bip->bio_data = bp->b_data;
3219 bip->bio_done = bufdonebio;
3220 bip->bio_caller2 = bp;
3222 KASSERT(dev->si_refcount > 0,
3223 ("dev_strategy on un-referenced struct cdev *(%s)",
3225 csw = dev_refthread(dev);
3228 bp->b_error = ENXIO;
3229 bp->b_ioflags = BIO_ERROR;
3233 (*csw->d_strategy)(bip);
3240 * Finish I/O on a buffer, optionally calling a completion function.
3241 * This is usually called from an interrupt so process blocking is
3244 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
3245 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
3246 * assuming B_INVAL is clear.
3248 * For the VMIO case, we set B_CACHE if the op was a read and no
3249 * read error occured, or if the op was a write. B_CACHE is never
3250 * set if the buffer is invalid or otherwise uncacheable.
3252 * biodone does not mess with B_INVAL, allowing the I/O routine or the
3253 * initiator to leave B_INVAL set to brelse the buffer out of existance
3254 * in the biodone routine.
3257 bufdone(struct buf *bp)
3259 struct bufobj *dropobj;
3260 void (*biodone)(struct buf *);
3262 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
3265 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
3266 BUF_ASSERT_HELD(bp);
3268 runningbufwakeup(bp);
3269 if (bp->b_iocmd == BIO_WRITE)
3270 dropobj = bp->b_bufobj;
3271 /* call optional completion function if requested */
3272 if (bp->b_iodone != NULL) {
3273 biodone = bp->b_iodone;
3274 bp->b_iodone = NULL;
3277 bufobj_wdrop(dropobj);
3284 bufobj_wdrop(dropobj);
3288 bufdone_finish(struct buf *bp)
3290 BUF_ASSERT_HELD(bp);
3292 if (!LIST_EMPTY(&bp->b_dep))
3295 if (bp->b_flags & B_VMIO) {
3301 struct vnode *vp = bp->b_vp;
3303 obj = bp->b_bufobj->bo_object;
3305 #if defined(VFS_BIO_DEBUG)
3306 mp_fixme("usecount and vflag accessed without locks.");
3307 if (vp->v_usecount == 0) {
3308 panic("biodone: zero vnode ref count");
3311 KASSERT(vp->v_object != NULL,
3312 ("biodone: vnode %p has no vm_object", vp));
3315 foff = bp->b_offset;
3316 KASSERT(bp->b_offset != NOOFFSET,
3317 ("biodone: no buffer offset"));
3319 VM_OBJECT_LOCK(obj);
3320 #if defined(VFS_BIO_DEBUG)
3321 if (obj->paging_in_progress < bp->b_npages) {
3322 printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n",
3323 obj->paging_in_progress, bp->b_npages);
3328 * Set B_CACHE if the op was a normal read and no error
3329 * occured. B_CACHE is set for writes in the b*write()
3332 iosize = bp->b_bcount - bp->b_resid;
3333 if (bp->b_iocmd == BIO_READ &&
3334 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
3335 !(bp->b_ioflags & BIO_ERROR)) {
3336 bp->b_flags |= B_CACHE;
3339 for (i = 0; i < bp->b_npages; i++) {
3343 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
3348 * cleanup bogus pages, restoring the originals
3351 if (m == bogus_page) {
3352 bogus = bogusflag = 1;
3353 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
3355 panic("biodone: page disappeared!");
3358 #if defined(VFS_BIO_DEBUG)
3359 if (OFF_TO_IDX(foff) != m->pindex) {
3361 "biodone: foff(%jd)/m->pindex(%ju) mismatch\n",
3362 (intmax_t)foff, (uintmax_t)m->pindex);
3367 * In the write case, the valid and clean bits are
3368 * already changed correctly ( see bdwrite() ), so we
3369 * only need to do this here in the read case.
3371 if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) {
3372 KASSERT((m->dirty & vm_page_bits(foff &
3373 PAGE_MASK, resid)) == 0, ("bufdone_finish:"
3374 " page %p has unexpected dirty bits", m));
3375 vfs_page_set_valid(bp, foff, m);
3379 * when debugging new filesystems or buffer I/O methods, this
3380 * is the most common error that pops up. if you see this, you
3381 * have not set the page busy flag correctly!!!
3384 printf("biodone: page busy < 0, "
3385 "pindex: %d, foff: 0x(%x,%x), "
3386 "resid: %d, index: %d\n",
3387 (int) m->pindex, (int)(foff >> 32),
3388 (int) foff & 0xffffffff, resid, i);
3389 if (!vn_isdisk(vp, NULL))
3390 printf(" iosize: %jd, lblkno: %jd, flags: 0x%x, npages: %d\n",
3391 (intmax_t)bp->b_vp->v_mount->mnt_stat.f_iosize,
3392 (intmax_t) bp->b_lblkno,
3393 bp->b_flags, bp->b_npages);
3395 printf(" VDEV, lblkno: %jd, flags: 0x%x, npages: %d\n",
3396 (intmax_t) bp->b_lblkno,
3397 bp->b_flags, bp->b_npages);
3398 printf(" valid: 0x%lx, dirty: 0x%lx, wired: %d\n",
3399 (u_long)m->valid, (u_long)m->dirty,
3401 panic("biodone: page busy < 0\n");
3403 vm_page_io_finish(m);
3404 vm_object_pip_subtract(obj, 1);
3405 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3408 vm_object_pip_wakeupn(obj, 0);
3409 VM_OBJECT_UNLOCK(obj);
3411 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3412 bp->b_pages, bp->b_npages);
3416 * For asynchronous completions, release the buffer now. The brelse
3417 * will do a wakeup there if necessary - so no need to do a wakeup
3418 * here in the async case. The sync case always needs to do a wakeup.
3421 if (bp->b_flags & B_ASYNC) {
3422 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR))
3431 * This routine is called in lieu of iodone in the case of
3432 * incomplete I/O. This keeps the busy status for pages
3436 vfs_unbusy_pages(struct buf *bp)
3442 runningbufwakeup(bp);
3443 if (!(bp->b_flags & B_VMIO))
3446 obj = bp->b_bufobj->bo_object;
3447 VM_OBJECT_LOCK(obj);
3448 for (i = 0; i < bp->b_npages; i++) {
3450 if (m == bogus_page) {
3451 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
3453 panic("vfs_unbusy_pages: page missing\n");
3455 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3456 bp->b_pages, bp->b_npages);
3458 vm_object_pip_subtract(obj, 1);
3459 vm_page_io_finish(m);
3461 vm_object_pip_wakeupn(obj, 0);
3462 VM_OBJECT_UNLOCK(obj);
3466 * vfs_page_set_valid:
3468 * Set the valid bits in a page based on the supplied offset. The
3469 * range is restricted to the buffer's size.
3471 * This routine is typically called after a read completes.
3474 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3479 * Compute the end offset, eoff, such that [off, eoff) does not span a
3480 * page boundary and eoff is not greater than the end of the buffer.
3481 * The end of the buffer, in this case, is our file EOF, not the
3482 * allocation size of the buffer.
3484 eoff = (off + PAGE_SIZE) & ~(vm_ooffset_t)PAGE_MASK;
3485 if (eoff > bp->b_offset + bp->b_bcount)
3486 eoff = bp->b_offset + bp->b_bcount;
3489 * Set valid range. This is typically the entire buffer and thus the
3493 vm_page_set_valid(m, off & PAGE_MASK, eoff - off);
3497 * vfs_page_set_validclean:
3499 * Set the valid bits and clear the dirty bits in a page based on the
3500 * supplied offset. The range is restricted to the buffer's size.
3503 vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3505 vm_ooffset_t soff, eoff;
3508 * Start and end offsets in buffer. eoff - soff may not cross a
3509 * page boundry or cross the end of the buffer. The end of the
3510 * buffer, in this case, is our file EOF, not the allocation size
3514 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3515 if (eoff > bp->b_offset + bp->b_bcount)
3516 eoff = bp->b_offset + bp->b_bcount;
3519 * Set valid range. This is typically the entire buffer and thus the
3523 vm_page_set_validclean(
3525 (vm_offset_t) (soff & PAGE_MASK),
3526 (vm_offset_t) (eoff - soff)
3532 * Ensure that all buffer pages are not busied by VPO_BUSY flag. If
3533 * any page is busy, drain the flag.
3536 vfs_drain_busy_pages(struct buf *bp)
3541 VM_OBJECT_LOCK_ASSERT(bp->b_bufobj->bo_object, MA_OWNED);
3543 for (i = 0; i < bp->b_npages; i++) {
3545 if ((m->oflags & VPO_BUSY) != 0) {
3546 for (; last_busied < i; last_busied++)
3547 vm_page_busy(bp->b_pages[last_busied]);
3548 while ((m->oflags & VPO_BUSY) != 0)
3549 vm_page_sleep(m, "vbpage");
3552 for (i = 0; i < last_busied; i++)
3553 vm_page_wakeup(bp->b_pages[i]);
3557 * This routine is called before a device strategy routine.
3558 * It is used to tell the VM system that paging I/O is in
3559 * progress, and treat the pages associated with the buffer
3560 * almost as being VPO_BUSY. Also the object paging_in_progress
3561 * flag is handled to make sure that the object doesn't become
3564 * Since I/O has not been initiated yet, certain buffer flags
3565 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
3566 * and should be ignored.
3569 vfs_busy_pages(struct buf *bp, int clear_modify)
3576 if (!(bp->b_flags & B_VMIO))
3579 obj = bp->b_bufobj->bo_object;
3580 foff = bp->b_offset;
3581 KASSERT(bp->b_offset != NOOFFSET,
3582 ("vfs_busy_pages: no buffer offset"));
3583 VM_OBJECT_LOCK(obj);
3584 vfs_drain_busy_pages(bp);
3585 if (bp->b_bufsize != 0)
3586 vfs_setdirty_locked_object(bp);
3588 for (i = 0; i < bp->b_npages; i++) {
3591 if ((bp->b_flags & B_CLUSTER) == 0) {
3592 vm_object_pip_add(obj, 1);
3593 vm_page_io_start(m);
3596 * When readying a buffer for a read ( i.e
3597 * clear_modify == 0 ), it is important to do
3598 * bogus_page replacement for valid pages in
3599 * partially instantiated buffers. Partially
3600 * instantiated buffers can, in turn, occur when
3601 * reconstituting a buffer from its VM backing store
3602 * base. We only have to do this if B_CACHE is
3603 * clear ( which causes the I/O to occur in the
3604 * first place ). The replacement prevents the read
3605 * I/O from overwriting potentially dirty VM-backed
3606 * pages. XXX bogus page replacement is, uh, bogus.
3607 * It may not work properly with small-block devices.
3608 * We need to find a better way.
3611 pmap_remove_write(m);
3612 vfs_page_set_validclean(bp, foff, m);
3613 } else if (m->valid == VM_PAGE_BITS_ALL &&
3614 (bp->b_flags & B_CACHE) == 0) {
3615 bp->b_pages[i] = bogus_page;
3618 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3620 VM_OBJECT_UNLOCK(obj);
3622 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3623 bp->b_pages, bp->b_npages);
3627 * vfs_bio_set_valid:
3629 * Set the range within the buffer to valid. The range is
3630 * relative to the beginning of the buffer, b_offset. Note that
3631 * b_offset itself may be offset from the beginning of the first
3635 vfs_bio_set_valid(struct buf *bp, int base, int size)
3640 if (!(bp->b_flags & B_VMIO))
3644 * Fixup base to be relative to beginning of first page.
3645 * Set initial n to be the maximum number of bytes in the
3646 * first page that can be validated.
3648 base += (bp->b_offset & PAGE_MASK);
3649 n = PAGE_SIZE - (base & PAGE_MASK);
3651 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3652 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
3656 vm_page_set_valid(m, base & PAGE_MASK, n);
3661 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3667 * If the specified buffer is a non-VMIO buffer, clear the entire
3668 * buffer. If the specified buffer is a VMIO buffer, clear and
3669 * validate only the previously invalid portions of the buffer.
3670 * This routine essentially fakes an I/O, so we need to clear
3671 * BIO_ERROR and B_INVAL.
3673 * Note that while we only theoretically need to clear through b_bcount,
3674 * we go ahead and clear through b_bufsize.
3677 vfs_bio_clrbuf(struct buf *bp)
3682 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
3686 bp->b_flags &= ~B_INVAL;
3687 bp->b_ioflags &= ~BIO_ERROR;
3688 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3689 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
3690 (bp->b_offset & PAGE_MASK) == 0) {
3691 if (bp->b_pages[0] == bogus_page)
3693 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
3694 VM_OBJECT_LOCK_ASSERT(bp->b_pages[0]->object, MA_OWNED);
3695 if ((bp->b_pages[0]->valid & mask) == mask)
3697 if ((bp->b_pages[0]->valid & mask) == 0) {
3698 bzero(bp->b_data, bp->b_bufsize);
3699 bp->b_pages[0]->valid |= mask;
3703 ea = sa = bp->b_data;
3704 for(i = 0; i < bp->b_npages; i++, sa = ea) {
3705 ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE);
3706 ea = (caddr_t)(vm_offset_t)ulmin(
3707 (u_long)(vm_offset_t)ea,
3708 (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize);
3709 if (bp->b_pages[i] == bogus_page)
3711 j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE;
3712 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
3713 VM_OBJECT_LOCK_ASSERT(bp->b_pages[i]->object, MA_OWNED);
3714 if ((bp->b_pages[i]->valid & mask) == mask)
3716 if ((bp->b_pages[i]->valid & mask) == 0)
3719 for (; sa < ea; sa += DEV_BSIZE, j++) {
3720 if ((bp->b_pages[i]->valid & (1 << j)) == 0)
3721 bzero(sa, DEV_BSIZE);
3724 bp->b_pages[i]->valid |= mask;
3727 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3732 * vm_hold_load_pages and vm_hold_free_pages get pages into
3733 * a buffers address space. The pages are anonymous and are
3734 * not associated with a file object.
3737 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3743 to = round_page(to);
3744 from = round_page(from);
3745 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3747 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3750 * note: must allocate system pages since blocking here
3751 * could interfere with paging I/O, no matter which
3754 p = vm_page_alloc(NULL, pg >> PAGE_SHIFT, VM_ALLOC_NOOBJ |
3755 VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
3757 atomic_add_int(&vm_pageout_deficit,
3758 (to - pg) >> PAGE_SHIFT);
3762 pmap_qenter(pg, &p, 1);
3763 bp->b_pages[index] = p;
3765 bp->b_npages = index;
3768 /* Return pages associated with this buf to the vm system */
3770 vm_hold_free_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3774 int index, newnpages;
3776 from = round_page(from);
3777 to = round_page(to);
3778 newnpages = index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3780 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3781 p = bp->b_pages[index];
3782 if (p && (index < bp->b_npages)) {
3785 "vm_hold_free_pages: blkno: %jd, lblkno: %jd\n",
3786 (intmax_t)bp->b_blkno,
3787 (intmax_t)bp->b_lblkno);
3789 bp->b_pages[index] = NULL;
3790 pmap_qremove(pg, 1);
3793 atomic_subtract_int(&cnt.v_wire_count, 1);
3796 bp->b_npages = newnpages;
3800 * Map an IO request into kernel virtual address space.
3802 * All requests are (re)mapped into kernel VA space.
3803 * Notice that we use b_bufsize for the size of the buffer
3804 * to be mapped. b_bcount might be modified by the driver.
3806 * Note that even if the caller determines that the address space should
3807 * be valid, a race or a smaller-file mapped into a larger space may
3808 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
3809 * check the return value.
3812 vmapbuf(struct buf *bp)
3818 struct pmap *pmap = &curproc->p_vmspace->vm_pmap;
3820 if (bp->b_bufsize < 0)
3822 prot = VM_PROT_READ;
3823 if (bp->b_iocmd == BIO_READ)
3824 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
3825 for (addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data), pidx = 0;
3826 addr < bp->b_data + bp->b_bufsize;
3827 addr += PAGE_SIZE, pidx++) {
3829 * Do the vm_fault if needed; do the copy-on-write thing
3830 * when reading stuff off device into memory.
3832 * NOTE! Must use pmap_extract() because addr may be in
3833 * the userland address space, and kextract is only guarenteed
3834 * to work for the kernland address space (see: sparc64 port).
3837 if (vm_fault_quick(addr >= bp->b_data ? addr : bp->b_data,
3839 for (i = 0; i < pidx; ++i) {
3840 vm_page_lock(bp->b_pages[i]);
3841 vm_page_unhold(bp->b_pages[i]);
3842 vm_page_unlock(bp->b_pages[i]);
3843 bp->b_pages[i] = NULL;
3847 m = pmap_extract_and_hold(pmap, (vm_offset_t)addr, prot);
3850 bp->b_pages[pidx] = m;
3852 if (pidx > btoc(MAXPHYS))
3853 panic("vmapbuf: mapped more than MAXPHYS");
3854 pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx);
3856 kva = bp->b_saveaddr;
3857 bp->b_npages = pidx;
3858 bp->b_saveaddr = bp->b_data;
3859 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
3864 * Free the io map PTEs associated with this IO operation.
3865 * We also invalidate the TLB entries and restore the original b_addr.
3868 vunmapbuf(struct buf *bp)
3873 npages = bp->b_npages;
3874 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
3875 for (pidx = 0; pidx < npages; pidx++) {
3876 vm_page_lock(bp->b_pages[pidx]);
3877 vm_page_unhold(bp->b_pages[pidx]);
3878 vm_page_unlock(bp->b_pages[pidx]);
3881 bp->b_data = bp->b_saveaddr;
3885 bdone(struct buf *bp)
3889 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3891 bp->b_flags |= B_DONE;
3897 bwait(struct buf *bp, u_char pri, const char *wchan)
3901 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3903 while ((bp->b_flags & B_DONE) == 0)
3904 msleep(bp, mtxp, pri, wchan, 0);
3909 bufsync(struct bufobj *bo, int waitfor)
3912 return (VOP_FSYNC(bo->__bo_vnode, waitfor, curthread));
3916 bufstrategy(struct bufobj *bo, struct buf *bp)
3922 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
3923 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
3924 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
3925 i = VOP_STRATEGY(vp, bp);
3926 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
3930 bufobj_wrefl(struct bufobj *bo)
3933 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3934 ASSERT_BO_LOCKED(bo);
3939 bufobj_wref(struct bufobj *bo)
3942 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3949 bufobj_wdrop(struct bufobj *bo)
3952 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
3954 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
3955 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
3956 bo->bo_flag &= ~BO_WWAIT;
3957 wakeup(&bo->bo_numoutput);
3963 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
3967 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
3968 ASSERT_BO_LOCKED(bo);
3970 while (bo->bo_numoutput) {
3971 bo->bo_flag |= BO_WWAIT;
3972 error = msleep(&bo->bo_numoutput, BO_MTX(bo),
3973 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
3981 bpin(struct buf *bp)
3985 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3992 bunpin(struct buf *bp)
3996 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3998 if (--bp->b_pin_count == 0)
4004 bunpin_wait(struct buf *bp)
4008 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4010 while (bp->b_pin_count > 0)
4011 msleep(bp, mtxp, PRIBIO, "bwunpin", 0);
4015 #include "opt_ddb.h"
4017 #include <ddb/ddb.h>
4019 /* DDB command to show buffer data */
4020 DB_SHOW_COMMAND(buffer, db_show_buffer)
4023 struct buf *bp = (struct buf *)addr;
4026 db_printf("usage: show buffer <addr>\n");
4030 db_printf("buf at %p\n", bp);
4031 db_printf("b_flags = 0x%b\n", (u_int)bp->b_flags, PRINT_BUF_FLAGS);
4033 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
4034 "b_bufobj = (%p), b_data = %p, b_blkno = %jd, b_dep = %p\n",
4035 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
4036 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno,
4037 bp->b_dep.lh_first);
4040 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
4041 for (i = 0; i < bp->b_npages; i++) {
4044 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
4045 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
4046 if ((i + 1) < bp->b_npages)
4052 lockmgr_printinfo(&bp->b_lock);
4055 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
4060 for (i = 0; i < nbuf; i++) {
4062 if (BUF_ISLOCKED(bp)) {
4063 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4069 DB_SHOW_COMMAND(vnodebufs, db_show_vnodebufs)
4075 db_printf("usage: show vnodebufs <addr>\n");
4078 vp = (struct vnode *)addr;
4079 db_printf("Clean buffers:\n");
4080 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_clean.bv_hd, b_bobufs) {
4081 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4084 db_printf("Dirty buffers:\n");
4085 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
4086 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4091 DB_COMMAND(countfreebufs, db_coundfreebufs)
4094 int i, used = 0, nfree = 0;
4097 db_printf("usage: countfreebufs\n");
4101 for (i = 0; i < nbuf; i++) {
4103 if ((bp->b_vflags & BV_INFREECNT) != 0)
4109 db_printf("Counted %d free, %d used (%d tot)\n", nfree, used,
4111 db_printf("numfreebuffers is %d\n", numfreebuffers);