2 * Copyright (c) 2004 Poul-Henning Kamp
3 * Copyright (c) 1994,1997 John S. Dyson
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * this file contains a new buffer I/O scheme implementing a coherent
30 * VM object and buffer cache scheme. Pains have been taken to make
31 * sure that the performance degradation associated with schemes such
32 * as this is not realized.
34 * Author: John S. Dyson
35 * Significant help during the development and debugging phases
36 * had been provided by David Greenman, also of the FreeBSD core team.
38 * see man buf(9) for more info.
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
45 #include <sys/systm.h>
49 #include <sys/devicestat.h>
50 #include <sys/eventhandler.h>
52 #include <sys/limits.h>
54 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/mutex.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
60 #include <sys/resourcevar.h>
61 #include <sys/sysctl.h>
62 #include <sys/vmmeter.h>
63 #include <sys/vnode.h>
64 #include <geom/geom.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_kern.h>
68 #include <vm/vm_pageout.h>
69 #include <vm/vm_page.h>
70 #include <vm/vm_object.h>
71 #include <vm/vm_extern.h>
72 #include <vm/vm_map.h>
73 #include "opt_compat.h"
74 #include "opt_directio.h"
77 static MALLOC_DEFINE(M_BIOBUF, "biobuf", "BIO buffer");
79 struct bio_ops bioops; /* I/O operation notification */
81 struct buf_ops buf_ops_bio = {
82 .bop_name = "buf_ops_bio",
83 .bop_write = bufwrite,
84 .bop_strategy = bufstrategy,
86 .bop_bdflush = bufbdflush,
90 * XXX buf is global because kern_shutdown.c and ffs_checkoverlap has
91 * carnal knowledge of buffers. This knowledge should be moved to vfs_bio.c.
93 struct buf *buf; /* buffer header pool */
95 static struct proc *bufdaemonproc;
97 static int inmem(struct vnode *vp, daddr_t blkno);
98 static void vm_hold_free_pages(struct buf *bp, int newbsize);
99 static void vm_hold_load_pages(struct buf *bp, vm_offset_t from,
101 static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m);
102 static void vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off,
104 static void vfs_drain_busy_pages(struct buf *bp);
105 static void vfs_clean_pages_dirty_buf(struct buf *bp);
106 static void vfs_setdirty_locked_object(struct buf *bp);
107 static void vfs_vmio_release(struct buf *bp);
108 static int vfs_bio_clcheck(struct vnode *vp, int size,
109 daddr_t lblkno, daddr_t blkno);
110 static int buf_do_flush(struct vnode *vp);
111 static int flushbufqueues(struct vnode *, int, int);
112 static void buf_daemon(void);
113 static void bremfreel(struct buf *bp);
114 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
115 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
116 static int sysctl_bufspace(SYSCTL_HANDLER_ARGS);
119 int vmiodirenable = TRUE;
120 SYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW, &vmiodirenable, 0,
121 "Use the VM system for directory writes");
122 long runningbufspace;
123 SYSCTL_LONG(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0,
124 "Amount of presently outstanding async buffer io");
125 static long bufspace;
126 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
127 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
128 SYSCTL_PROC(_vfs, OID_AUTO, bufspace, CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RD,
129 &bufspace, 0, sysctl_bufspace, "L", "Virtual memory used for buffers");
131 SYSCTL_LONG(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0,
132 "Virtual memory used for buffers");
134 static long maxbufspace;
135 SYSCTL_LONG(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0,
136 "Maximum allowed value of bufspace (including buf_daemon)");
137 static long bufmallocspace;
138 SYSCTL_LONG(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0,
139 "Amount of malloced memory for buffers");
140 static long maxbufmallocspace;
141 SYSCTL_LONG(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace, 0,
142 "Maximum amount of malloced memory for buffers");
143 static long lobufspace;
144 SYSCTL_LONG(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0,
145 "Minimum amount of buffers we want to have");
147 SYSCTL_LONG(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0,
148 "Maximum allowed value of bufspace (excluding buf_daemon)");
149 static int bufreusecnt;
150 SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RW, &bufreusecnt, 0,
151 "Number of times we have reused a buffer");
152 static int buffreekvacnt;
153 SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt, 0,
154 "Number of times we have freed the KVA space from some buffer");
155 static int bufdefragcnt;
156 SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt, 0,
157 "Number of times we have had to repeat buffer allocation to defragment");
158 static long lorunningspace;
159 SYSCTL_LONG(_vfs, OID_AUTO, lorunningspace, CTLFLAG_RW, &lorunningspace, 0,
160 "Minimum preferred space used for in-progress I/O");
161 static long hirunningspace;
162 SYSCTL_LONG(_vfs, OID_AUTO, hirunningspace, CTLFLAG_RW, &hirunningspace, 0,
163 "Maximum amount of space to use for in-progress I/O");
164 int dirtybufferflushes;
165 SYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes,
166 0, "Number of bdwrite to bawrite conversions to limit dirty buffers");
168 SYSCTL_INT(_vfs, OID_AUTO, bdwriteskip, CTLFLAG_RW, &bdwriteskip,
169 0, "Number of buffers supplied to bdwrite with snapshot deadlock risk");
170 int altbufferflushes;
171 SYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes,
172 0, "Number of fsync flushes to limit dirty buffers");
173 static int recursiveflushes;
174 SYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes,
175 0, "Number of flushes skipped due to being recursive");
176 static int numdirtybuffers;
177 SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0,
178 "Number of buffers that are dirty (has unwritten changes) at the moment");
179 static int lodirtybuffers;
180 SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0,
181 "How many buffers we want to have free before bufdaemon can sleep");
182 static int hidirtybuffers;
183 SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0,
184 "When the number of dirty buffers is considered severe");
186 SYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh,
187 0, "Number of bdwrite to bawrite conversions to clear dirty buffers");
188 static int numfreebuffers;
189 SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0,
190 "Number of free buffers");
191 static int lofreebuffers;
192 SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0,
194 static int hifreebuffers;
195 SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0,
196 "XXX Complicatedly unused");
197 static int getnewbufcalls;
198 SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW, &getnewbufcalls, 0,
199 "Number of calls to getnewbuf");
200 static int getnewbufrestarts;
201 SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW, &getnewbufrestarts, 0,
202 "Number of times getnewbuf has had to restart a buffer aquisition");
203 static int flushbufqtarget = 100;
204 SYSCTL_INT(_vfs, OID_AUTO, flushbufqtarget, CTLFLAG_RW, &flushbufqtarget, 0,
205 "Amount of work to do in flushbufqueues when helping bufdaemon");
206 static long notbufdflashes;
207 SYSCTL_LONG(_vfs, OID_AUTO, notbufdflashes, CTLFLAG_RD, ¬bufdflashes, 0,
208 "Number of dirty buffer flushes done by the bufdaemon helpers");
211 * Wakeup point for bufdaemon, as well as indicator of whether it is already
212 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it
215 static int bd_request;
218 * This lock synchronizes access to bd_request.
220 static struct mtx bdlock;
223 * bogus page -- for I/O to/from partially complete buffers
224 * this is a temporary solution to the problem, but it is not
225 * really that bad. it would be better to split the buffer
226 * for input in the case of buffers partially already in memory,
227 * but the code is intricate enough already.
229 vm_page_t bogus_page;
232 * Synchronization (sleep/wakeup) variable for active buffer space requests.
233 * Set when wait starts, cleared prior to wakeup().
234 * Used in runningbufwakeup() and waitrunningbufspace().
236 static int runningbufreq;
239 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
240 * waitrunningbufspace().
242 static struct mtx rbreqlock;
245 * Synchronization (sleep/wakeup) variable for buffer requests.
246 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done
248 * Used in numdirtywakeup(), bufspacewakeup(), bufcountwakeup(), bwillwrite(),
249 * getnewbuf(), and getblk().
251 static int needsbuffer;
254 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it.
256 static struct mtx nblock;
259 * Definitions for the buffer free lists.
261 #define BUFFER_QUEUES 6 /* number of free buffer queues */
263 #define QUEUE_NONE 0 /* on no queue */
264 #define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */
265 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
266 #define QUEUE_DIRTY_GIANT 3 /* B_DELWRI buffers that need giant */
267 #define QUEUE_EMPTYKVA 4 /* empty buffer headers w/KVA assignment */
268 #define QUEUE_EMPTY 5 /* empty buffer headers */
269 #define QUEUE_SENTINEL 1024 /* not an queue index, but mark for sentinel */
271 /* Queues for free buffers with various properties */
272 static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } };
274 /* Lock for the bufqueues */
275 static struct mtx bqlock;
278 * Single global constant for BUF_WMESG, to avoid getting multiple references.
279 * buf_wmesg is referred from macros.
281 const char *buf_wmesg = BUF_WMESG;
283 #define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */
284 #define VFS_BIO_NEED_DIRTYFLUSH 0x02 /* waiting for dirty buffer flush */
285 #define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */
286 #define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */
288 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
289 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
291 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
296 if (sizeof(int) == sizeof(long) || req->oldlen >= sizeof(long))
297 return (sysctl_handle_long(oidp, arg1, arg2, req));
298 lvalue = *(long *)arg1;
299 if (lvalue > INT_MAX)
300 /* On overflow, still write out a long to trigger ENOMEM. */
301 return (sysctl_handle_long(oidp, &lvalue, 0, req));
303 return (sysctl_handle_int(oidp, &ivalue, 0, req));
308 extern void ffs_rawread_setup(void);
309 #endif /* DIRECTIO */
313 * If someone is blocked due to there being too many dirty buffers,
314 * and numdirtybuffers is now reasonable, wake them up.
318 numdirtywakeup(int level)
321 if (numdirtybuffers <= level) {
323 if (needsbuffer & VFS_BIO_NEED_DIRTYFLUSH) {
324 needsbuffer &= ~VFS_BIO_NEED_DIRTYFLUSH;
325 wakeup(&needsbuffer);
334 * Called when buffer space is potentially available for recovery.
335 * getnewbuf() will block on this flag when it is unable to free
336 * sufficient buffer space. Buffer space becomes recoverable when
337 * bp's get placed back in the queues.
345 * If someone is waiting for BUF space, wake them up. Even
346 * though we haven't freed the kva space yet, the waiting
347 * process will be able to now.
350 if (needsbuffer & VFS_BIO_NEED_BUFSPACE) {
351 needsbuffer &= ~VFS_BIO_NEED_BUFSPACE;
352 wakeup(&needsbuffer);
358 * runningbufwakeup() - in-progress I/O accounting.
362 runningbufwakeup(struct buf *bp)
365 if (bp->b_runningbufspace) {
366 atomic_subtract_long(&runningbufspace, bp->b_runningbufspace);
367 bp->b_runningbufspace = 0;
368 mtx_lock(&rbreqlock);
369 if (runningbufreq && runningbufspace <= lorunningspace) {
371 wakeup(&runningbufreq);
373 mtx_unlock(&rbreqlock);
380 * Called when a buffer has been added to one of the free queues to
381 * account for the buffer and to wakeup anyone waiting for free buffers.
382 * This typically occurs when large amounts of metadata are being handled
383 * by the buffer cache ( else buffer space runs out first, usually ).
387 bufcountwakeup(struct buf *bp)
391 KASSERT((bp->b_vflags & BV_INFREECNT) == 0,
392 ("buf %p already counted as free", bp));
393 if (bp->b_bufobj != NULL)
394 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
395 bp->b_vflags |= BV_INFREECNT;
396 old = atomic_fetchadd_int(&numfreebuffers, 1);
397 KASSERT(old >= 0 && old < nbuf,
398 ("numfreebuffers climbed to %d", old + 1));
401 needsbuffer &= ~VFS_BIO_NEED_ANY;
402 if (numfreebuffers >= hifreebuffers)
403 needsbuffer &= ~VFS_BIO_NEED_FREE;
404 wakeup(&needsbuffer);
410 * waitrunningbufspace()
412 * runningbufspace is a measure of the amount of I/O currently
413 * running. This routine is used in async-write situations to
414 * prevent creating huge backups of pending writes to a device.
415 * Only asynchronous writes are governed by this function.
417 * Reads will adjust runningbufspace, but will not block based on it.
418 * The read load has a side effect of reducing the allowed write load.
420 * This does NOT turn an async write into a sync write. It waits
421 * for earlier writes to complete and generally returns before the
422 * caller's write has reached the device.
425 waitrunningbufspace(void)
428 mtx_lock(&rbreqlock);
429 while (runningbufspace > hirunningspace) {
431 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
433 mtx_unlock(&rbreqlock);
438 * vfs_buf_test_cache:
440 * Called when a buffer is extended. This function clears the B_CACHE
441 * bit if the newly extended portion of the buffer does not contain
446 vfs_buf_test_cache(struct buf *bp,
447 vm_ooffset_t foff, vm_offset_t off, vm_offset_t size,
451 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
452 if (bp->b_flags & B_CACHE) {
453 int base = (foff + off) & PAGE_MASK;
454 if (vm_page_is_valid(m, base, size) == 0)
455 bp->b_flags &= ~B_CACHE;
459 /* Wake up the buffer daemon if necessary */
462 bd_wakeup(int dirtybuflevel)
466 if (bd_request == 0 && numdirtybuffers >= dirtybuflevel) {
474 * bd_speedup - speedup the buffer cache flushing code
486 * Calculating buffer cache scaling values and reserve space for buffer
487 * headers. This is called during low level kernel initialization and
488 * may be called more then once. We CANNOT write to the memory area
489 * being reserved at this time.
492 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
498 * physmem_est is in pages. Convert it to kilobytes (assumes
499 * PAGE_SIZE is >= 1K)
501 physmem_est = physmem_est * (PAGE_SIZE / 1024);
504 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
505 * For the first 64MB of ram nominally allocate sufficient buffers to
506 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
507 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing
508 * the buffer cache we limit the eventual kva reservation to
511 * factor represents the 1/4 x ram conversion.
514 int factor = 4 * BKVASIZE / 1024;
517 if (physmem_est > 4096)
518 nbuf += min((physmem_est - 4096) / factor,
520 if (physmem_est > 65536)
521 nbuf += (physmem_est - 65536) * 2 / (factor * 5);
523 if (maxbcache && nbuf > maxbcache / BKVASIZE)
524 nbuf = maxbcache / BKVASIZE;
529 /* XXX Avoid unsigned long overflows later on with maxbufspace. */
530 maxbuf = (LONG_MAX / 3) / BKVASIZE;
533 printf("Warning: nbufs lowered from %d to %ld\n", nbuf,
539 * swbufs are used as temporary holders for I/O, such as paging I/O.
540 * We have no less then 16 and no more then 256.
542 nswbuf = max(min(nbuf/4, 256), 16);
544 if (nswbuf < NSWBUF_MIN)
552 * Reserve space for the buffer cache buffers
555 v = (caddr_t)(swbuf + nswbuf);
557 v = (caddr_t)(buf + nbuf);
562 /* Initialize the buffer subsystem. Called before use of any buffers. */
569 mtx_init(&bqlock, "buf queue lock", NULL, MTX_DEF);
570 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
571 mtx_init(&nblock, "needsbuffer lock", NULL, MTX_DEF);
572 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
574 /* next, make a null set of free lists */
575 for (i = 0; i < BUFFER_QUEUES; i++)
576 TAILQ_INIT(&bufqueues[i]);
578 /* finally, initialize each buffer header and stick on empty q */
579 for (i = 0; i < nbuf; i++) {
581 bzero(bp, sizeof *bp);
582 bp->b_flags = B_INVAL; /* we're just an empty header */
583 bp->b_rcred = NOCRED;
584 bp->b_wcred = NOCRED;
585 bp->b_qindex = QUEUE_EMPTY;
586 bp->b_vflags = BV_INFREECNT; /* buf is counted as free */
588 LIST_INIT(&bp->b_dep);
590 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
594 * maxbufspace is the absolute maximum amount of buffer space we are
595 * allowed to reserve in KVM and in real terms. The absolute maximum
596 * is nominally used by buf_daemon. hibufspace is the nominal maximum
597 * used by most other processes. The differential is required to
598 * ensure that buf_daemon is able to run when other processes might
599 * be blocked waiting for buffer space.
601 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
602 * this may result in KVM fragmentation which is not handled optimally
605 maxbufspace = (long)nbuf * BKVASIZE;
606 hibufspace = lmax(3 * maxbufspace / 4, maxbufspace - MAXBSIZE * 10);
607 lobufspace = hibufspace - MAXBSIZE;
609 lorunningspace = 512 * 1024;
610 hirunningspace = 1024 * 1024;
613 * Limit the amount of malloc memory since it is wired permanently into
614 * the kernel space. Even though this is accounted for in the buffer
615 * allocation, we don't want the malloced region to grow uncontrolled.
616 * The malloc scheme improves memory utilization significantly on average
617 * (small) directories.
619 maxbufmallocspace = hibufspace / 20;
622 * Reduce the chance of a deadlock occuring by limiting the number
623 * of delayed-write dirty buffers we allow to stack up.
625 hidirtybuffers = nbuf / 4 + 20;
626 dirtybufthresh = hidirtybuffers * 9 / 10;
629 * To support extreme low-memory systems, make sure hidirtybuffers cannot
630 * eat up all available buffer space. This occurs when our minimum cannot
631 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming
632 * BKVASIZE'd (8K) buffers.
634 while ((long)hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
635 hidirtybuffers >>= 1;
637 lodirtybuffers = hidirtybuffers / 2;
640 * Try to keep the number of free buffers in the specified range,
641 * and give special processes (e.g. like buf_daemon) access to an
644 lofreebuffers = nbuf / 18 + 5;
645 hifreebuffers = 2 * lofreebuffers;
646 numfreebuffers = nbuf;
648 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
649 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
653 * bfreekva() - free the kva allocation for a buffer.
655 * Since this call frees up buffer space, we call bufspacewakeup().
658 bfreekva(struct buf *bp)
662 atomic_add_int(&buffreekvacnt, 1);
663 atomic_subtract_long(&bufspace, bp->b_kvasize);
664 vm_map_remove(buffer_map, (vm_offset_t) bp->b_kvabase,
665 (vm_offset_t) bp->b_kvabase + bp->b_kvasize);
674 * Mark the buffer for removal from the appropriate free list in brelse.
678 bremfree(struct buf *bp)
682 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
683 KASSERT((bp->b_flags & B_REMFREE) == 0,
684 ("bremfree: buffer %p already marked for delayed removal.", bp));
685 KASSERT(bp->b_qindex != QUEUE_NONE,
686 ("bremfree: buffer %p not on a queue.", bp));
689 bp->b_flags |= B_REMFREE;
690 /* Fixup numfreebuffers count. */
691 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
692 KASSERT((bp->b_vflags & BV_INFREECNT) != 0,
693 ("buf %p not counted in numfreebuffers", bp));
694 if (bp->b_bufobj != NULL)
695 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
696 bp->b_vflags &= ~BV_INFREECNT;
697 old = atomic_fetchadd_int(&numfreebuffers, -1);
698 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
705 * Force an immediate removal from a free list. Used only in nfs when
706 * it abuses the b_freelist pointer.
709 bremfreef(struct buf *bp)
719 * Removes a buffer from the free list, must be called with the
723 bremfreel(struct buf *bp)
727 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X",
728 bp, bp->b_vp, bp->b_flags);
729 KASSERT(bp->b_qindex != QUEUE_NONE,
730 ("bremfreel: buffer %p not on a queue.", bp));
732 mtx_assert(&bqlock, MA_OWNED);
734 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
735 bp->b_qindex = QUEUE_NONE;
737 * If this was a delayed bremfree() we only need to remove the buffer
738 * from the queue and return the stats are already done.
740 if (bp->b_flags & B_REMFREE) {
741 bp->b_flags &= ~B_REMFREE;
745 * Fixup numfreebuffers count. If the buffer is invalid or not
746 * delayed-write, the buffer was free and we must decrement
749 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
750 KASSERT((bp->b_vflags & BV_INFREECNT) != 0,
751 ("buf %p not counted in numfreebuffers", bp));
752 if (bp->b_bufobj != NULL)
753 mtx_assert(BO_MTX(bp->b_bufobj), MA_OWNED);
754 bp->b_vflags &= ~BV_INFREECNT;
755 old = atomic_fetchadd_int(&numfreebuffers, -1);
756 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
762 * Get a buffer with the specified data. Look in the cache first. We
763 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
764 * is set, the buffer is valid and we do not have to do anything ( see
765 * getblk() ). This is really just a special case of breadn().
768 bread(struct vnode * vp, daddr_t blkno, int size, struct ucred * cred,
772 return (breadn(vp, blkno, size, 0, 0, 0, cred, bpp));
776 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must
777 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set,
778 * the buffer is valid and we do not have to do anything.
781 breada(struct vnode * vp, daddr_t * rablkno, int * rabsize,
782 int cnt, struct ucred * cred)
787 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
788 if (inmem(vp, *rablkno))
790 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
792 if ((rabp->b_flags & B_CACHE) == 0) {
793 if (!TD_IS_IDLETHREAD(curthread))
794 curthread->td_ru.ru_inblock++;
795 rabp->b_flags |= B_ASYNC;
796 rabp->b_flags &= ~B_INVAL;
797 rabp->b_ioflags &= ~BIO_ERROR;
798 rabp->b_iocmd = BIO_READ;
799 if (rabp->b_rcred == NOCRED && cred != NOCRED)
800 rabp->b_rcred = crhold(cred);
801 vfs_busy_pages(rabp, 0);
803 rabp->b_iooffset = dbtob(rabp->b_blkno);
812 * Operates like bread, but also starts asynchronous I/O on
816 breadn(struct vnode * vp, daddr_t blkno, int size,
817 daddr_t * rablkno, int *rabsize,
818 int cnt, struct ucred * cred, struct buf **bpp)
821 int rv = 0, readwait = 0;
823 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
824 *bpp = bp = getblk(vp, blkno, size, 0, 0, 0);
826 /* if not found in cache, do some I/O */
827 if ((bp->b_flags & B_CACHE) == 0) {
828 if (!TD_IS_IDLETHREAD(curthread))
829 curthread->td_ru.ru_inblock++;
830 bp->b_iocmd = BIO_READ;
831 bp->b_flags &= ~B_INVAL;
832 bp->b_ioflags &= ~BIO_ERROR;
833 if (bp->b_rcred == NOCRED && cred != NOCRED)
834 bp->b_rcred = crhold(cred);
835 vfs_busy_pages(bp, 0);
836 bp->b_iooffset = dbtob(bp->b_blkno);
841 breada(vp, rablkno, rabsize, cnt, cred);
850 * Write, release buffer on completion. (Done by iodone
851 * if async). Do not bother writing anything if the buffer
854 * Note that we set B_CACHE here, indicating that buffer is
855 * fully valid and thus cacheable. This is true even of NFS
856 * now so we set it generally. This could be set either here
857 * or in biodone() since the I/O is synchronous. We put it
861 bufwrite(struct buf *bp)
867 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
868 if (bp->b_flags & B_INVAL) {
873 oldflags = bp->b_flags;
877 if (bp->b_pin_count > 0)
880 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
881 ("FFS background buffer should not get here %p", bp));
885 vp_md = vp->v_vflag & VV_MD;
889 /* Mark the buffer clean */
892 bp->b_flags &= ~B_DONE;
893 bp->b_ioflags &= ~BIO_ERROR;
894 bp->b_flags |= B_CACHE;
895 bp->b_iocmd = BIO_WRITE;
897 bufobj_wref(bp->b_bufobj);
898 vfs_busy_pages(bp, 1);
901 * Normal bwrites pipeline writes
903 bp->b_runningbufspace = bp->b_bufsize;
904 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
906 if (!TD_IS_IDLETHREAD(curthread))
907 curthread->td_ru.ru_oublock++;
908 if (oldflags & B_ASYNC)
910 bp->b_iooffset = dbtob(bp->b_blkno);
913 if ((oldflags & B_ASYNC) == 0) {
914 int rtval = bufwait(bp);
919 * don't allow the async write to saturate the I/O
920 * system. We will not deadlock here because
921 * we are blocking waiting for I/O that is already in-progress
922 * to complete. We do not block here if it is the update
923 * or syncer daemon trying to clean up as that can lead
926 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md)
927 waitrunningbufspace();
934 bufbdflush(struct bufobj *bo, struct buf *bp)
938 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
939 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread);
941 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
944 * Try to find a buffer to flush.
946 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
947 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
949 LK_EXCLUSIVE | LK_NOWAIT, NULL))
952 panic("bdwrite: found ourselves");
954 /* Don't countdeps with the bo lock held. */
955 if (buf_countdeps(nbp, 0)) {
960 if (nbp->b_flags & B_CLUSTEROK) {
966 dirtybufferflushes++;
975 * Delayed write. (Buffer is marked dirty). Do not bother writing
976 * anything if the buffer is marked invalid.
978 * Note that since the buffer must be completely valid, we can safely
979 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
980 * biodone() in order to prevent getblk from writing the buffer
984 bdwrite(struct buf *bp)
986 struct thread *td = curthread;
990 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
991 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
994 if (bp->b_flags & B_INVAL) {
1000 * If we have too many dirty buffers, don't create any more.
1001 * If we are wildly over our limit, then force a complete
1002 * cleanup. Otherwise, just keep the situation from getting
1003 * out of control. Note that we have to avoid a recursive
1004 * disaster and not try to clean up after our own cleanup!
1008 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) {
1009 td->td_pflags |= TDP_INBDFLUSH;
1011 td->td_pflags &= ~TDP_INBDFLUSH;
1017 * Set B_CACHE, indicating that the buffer is fully valid. This is
1018 * true even of NFS now.
1020 bp->b_flags |= B_CACHE;
1023 * This bmap keeps the system from needing to do the bmap later,
1024 * perhaps when the system is attempting to do a sync. Since it
1025 * is likely that the indirect block -- or whatever other datastructure
1026 * that the filesystem needs is still in memory now, it is a good
1027 * thing to do this. Note also, that if the pageout daemon is
1028 * requesting a sync -- there might not be enough memory to do
1029 * the bmap then... So, this is important to do.
1031 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
1032 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1036 * Set the *dirty* buffer range based upon the VM system dirty
1039 * Mark the buffer pages as clean. We need to do this here to
1040 * satisfy the vnode_pager and the pageout daemon, so that it
1041 * thinks that the pages have been "cleaned". Note that since
1042 * the pages are in a delayed write buffer -- the VFS layer
1043 * "will" see that the pages get written out on the next sync,
1044 * or perhaps the cluster will be completed.
1046 vfs_clean_pages_dirty_buf(bp);
1050 * Wakeup the buffer flushing daemon if we have a lot of dirty
1051 * buffers (midpoint between our recovery point and our stall
1054 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1057 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
1058 * due to the softdep code.
1065 * Turn buffer into delayed write request. We must clear BIO_READ and
1066 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
1067 * itself to properly update it in the dirty/clean lists. We mark it
1068 * B_DONE to ensure that any asynchronization of the buffer properly
1069 * clears B_DONE ( else a panic will occur later ).
1071 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
1072 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
1073 * should only be called if the buffer is known-good.
1075 * Since the buffer is not on a queue, we do not update the numfreebuffers
1078 * The buffer must be on QUEUE_NONE.
1081 bdirty(struct buf *bp)
1084 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
1085 bp, bp->b_vp, bp->b_flags);
1086 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1087 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1088 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
1089 BUF_ASSERT_HELD(bp);
1090 bp->b_flags &= ~(B_RELBUF);
1091 bp->b_iocmd = BIO_WRITE;
1093 if ((bp->b_flags & B_DELWRI) == 0) {
1094 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
1096 atomic_add_int(&numdirtybuffers, 1);
1097 bd_wakeup((lodirtybuffers + hidirtybuffers) / 2);
1104 * Clear B_DELWRI for buffer.
1106 * Since the buffer is not on a queue, we do not update the numfreebuffers
1109 * The buffer must be on QUEUE_NONE.
1113 bundirty(struct buf *bp)
1116 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1117 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1118 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1119 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
1120 BUF_ASSERT_HELD(bp);
1122 if (bp->b_flags & B_DELWRI) {
1123 bp->b_flags &= ~B_DELWRI;
1125 atomic_subtract_int(&numdirtybuffers, 1);
1126 numdirtywakeup(lodirtybuffers);
1129 * Since it is now being written, we can clear its deferred write flag.
1131 bp->b_flags &= ~B_DEFERRED;
1137 * Asynchronous write. Start output on a buffer, but do not wait for
1138 * it to complete. The buffer is released when the output completes.
1140 * bwrite() ( or the VOP routine anyway ) is responsible for handling
1141 * B_INVAL buffers. Not us.
1144 bawrite(struct buf *bp)
1147 bp->b_flags |= B_ASYNC;
1154 * Called prior to the locking of any vnodes when we are expecting to
1155 * write. We do not want to starve the buffer cache with too many
1156 * dirty buffers so we block here. By blocking prior to the locking
1157 * of any vnodes we attempt to avoid the situation where a locked vnode
1158 * prevents the various system daemons from flushing related buffers.
1165 if (numdirtybuffers >= hidirtybuffers) {
1167 while (numdirtybuffers >= hidirtybuffers) {
1169 needsbuffer |= VFS_BIO_NEED_DIRTYFLUSH;
1170 msleep(&needsbuffer, &nblock,
1171 (PRIBIO + 4), "flswai", 0);
1173 mtx_unlock(&nblock);
1178 * Return true if we have too many dirty buffers.
1181 buf_dirty_count_severe(void)
1184 return(numdirtybuffers >= hidirtybuffers);
1187 static __noinline int
1188 buf_vm_page_count_severe(void)
1191 KFAIL_POINT_CODE(DEBUG_FP, buf_pressure, return 1);
1193 return vm_page_count_severe();
1199 * Release a busy buffer and, if requested, free its resources. The
1200 * buffer will be stashed in the appropriate bufqueue[] allowing it
1201 * to be accessed later as a cache entity or reused for other purposes.
1204 brelse(struct buf *bp)
1206 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
1207 bp, bp->b_vp, bp->b_flags);
1208 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1209 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1211 if (bp->b_flags & B_MANAGED) {
1216 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) &&
1217 bp->b_error == EIO && !(bp->b_flags & B_INVAL)) {
1219 * Failed write, redirty. Must clear BIO_ERROR to prevent
1220 * pages from being scrapped. If the error is anything
1221 * other than an I/O error (EIO), assume that retrying
1224 bp->b_ioflags &= ~BIO_ERROR;
1226 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
1227 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
1229 * Either a failed I/O or we were asked to free or not
1232 bp->b_flags |= B_INVAL;
1233 if (!LIST_EMPTY(&bp->b_dep))
1235 if (bp->b_flags & B_DELWRI) {
1236 atomic_subtract_int(&numdirtybuffers, 1);
1237 numdirtywakeup(lodirtybuffers);
1239 bp->b_flags &= ~(B_DELWRI | B_CACHE);
1240 if ((bp->b_flags & B_VMIO) == 0) {
1249 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release()
1250 * is called with B_DELWRI set, the underlying pages may wind up
1251 * getting freed causing a previous write (bdwrite()) to get 'lost'
1252 * because pages associated with a B_DELWRI bp are marked clean.
1254 * We still allow the B_INVAL case to call vfs_vmio_release(), even
1255 * if B_DELWRI is set.
1257 * If B_DELWRI is not set we may have to set B_RELBUF if we are low
1258 * on pages to return pages to the VM page queues.
1260 if (bp->b_flags & B_DELWRI)
1261 bp->b_flags &= ~B_RELBUF;
1262 else if (buf_vm_page_count_severe()) {
1264 * The locking of the BO_LOCK is not necessary since
1265 * BKGRDINPROG cannot be set while we hold the buf
1266 * lock, it can only be cleared if it is already
1270 if (!(bp->b_vflags & BV_BKGRDINPROG))
1271 bp->b_flags |= B_RELBUF;
1273 bp->b_flags |= B_RELBUF;
1277 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
1278 * constituted, not even NFS buffers now. Two flags effect this. If
1279 * B_INVAL, the struct buf is invalidated but the VM object is kept
1280 * around ( i.e. so it is trivial to reconstitute the buffer later ).
1282 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
1283 * invalidated. BIO_ERROR cannot be set for a failed write unless the
1284 * buffer is also B_INVAL because it hits the re-dirtying code above.
1286 * Normally we can do this whether a buffer is B_DELWRI or not. If
1287 * the buffer is an NFS buffer, it is tracking piecemeal writes or
1288 * the commit state and we cannot afford to lose the buffer. If the
1289 * buffer has a background write in progress, we need to keep it
1290 * around to prevent it from being reconstituted and starting a second
1293 if ((bp->b_flags & B_VMIO)
1294 && !(bp->b_vp->v_mount != NULL &&
1295 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
1296 !vn_isdisk(bp->b_vp, NULL) &&
1297 (bp->b_flags & B_DELWRI))
1306 obj = bp->b_bufobj->bo_object;
1309 * Get the base offset and length of the buffer. Note that
1310 * in the VMIO case if the buffer block size is not
1311 * page-aligned then b_data pointer may not be page-aligned.
1312 * But our b_pages[] array *IS* page aligned.
1314 * block sizes less then DEV_BSIZE (usually 512) are not
1315 * supported due to the page granularity bits (m->valid,
1316 * m->dirty, etc...).
1318 * See man buf(9) for more information
1320 resid = bp->b_bufsize;
1321 foff = bp->b_offset;
1322 VM_OBJECT_LOCK(obj);
1323 for (i = 0; i < bp->b_npages; i++) {
1329 * If we hit a bogus page, fixup *all* the bogus pages
1332 if (m == bogus_page) {
1333 poff = OFF_TO_IDX(bp->b_offset);
1336 for (j = i; j < bp->b_npages; j++) {
1338 mtmp = bp->b_pages[j];
1339 if (mtmp == bogus_page) {
1340 mtmp = vm_page_lookup(obj, poff + j);
1342 panic("brelse: page missing\n");
1344 bp->b_pages[j] = mtmp;
1348 if ((bp->b_flags & B_INVAL) == 0) {
1350 trunc_page((vm_offset_t)bp->b_data),
1351 bp->b_pages, bp->b_npages);
1355 if ((bp->b_flags & B_NOCACHE) ||
1356 (bp->b_ioflags & BIO_ERROR &&
1357 bp->b_iocmd == BIO_READ)) {
1358 int poffset = foff & PAGE_MASK;
1359 int presid = resid > (PAGE_SIZE - poffset) ?
1360 (PAGE_SIZE - poffset) : resid;
1362 KASSERT(presid >= 0, ("brelse: extra page"));
1363 vm_page_lock_queues();
1364 vm_page_set_invalid(m, poffset, presid);
1365 vm_page_unlock_queues();
1367 printf("avoided corruption bug in bogus_page/brelse code\n");
1369 resid -= PAGE_SIZE - (foff & PAGE_MASK);
1370 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
1372 VM_OBJECT_UNLOCK(obj);
1373 if (bp->b_flags & (B_INVAL | B_RELBUF))
1374 vfs_vmio_release(bp);
1376 } else if (bp->b_flags & B_VMIO) {
1378 if (bp->b_flags & (B_INVAL | B_RELBUF)) {
1379 vfs_vmio_release(bp);
1382 } else if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0) {
1383 if (bp->b_bufsize != 0)
1385 if (bp->b_vp != NULL)
1389 if (BUF_LOCKRECURSED(bp)) {
1390 /* do not release to free list */
1397 /* Handle delayed bremfree() processing. */
1398 if (bp->b_flags & B_REMFREE) {
1408 if (bp->b_qindex != QUEUE_NONE)
1409 panic("brelse: free buffer onto another queue???");
1412 * If the buffer has junk contents signal it and eventually
1413 * clean up B_DELWRI and diassociate the vnode so that gbincore()
1416 if (bp->b_bufsize == 0 || (bp->b_ioflags & BIO_ERROR) != 0 ||
1417 (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) != 0)
1418 bp->b_flags |= B_INVAL;
1419 if (bp->b_flags & B_INVAL) {
1420 if (bp->b_flags & B_DELWRI)
1426 /* buffers with no memory */
1427 if (bp->b_bufsize == 0) {
1428 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1429 if (bp->b_vflags & BV_BKGRDINPROG)
1430 panic("losing buffer 1");
1431 if (bp->b_kvasize) {
1432 bp->b_qindex = QUEUE_EMPTYKVA;
1434 bp->b_qindex = QUEUE_EMPTY;
1436 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1437 /* buffers with junk contents */
1438 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
1439 (bp->b_ioflags & BIO_ERROR)) {
1440 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1441 if (bp->b_vflags & BV_BKGRDINPROG)
1442 panic("losing buffer 2");
1443 bp->b_qindex = QUEUE_CLEAN;
1444 TAILQ_INSERT_HEAD(&bufqueues[QUEUE_CLEAN], bp, b_freelist);
1445 /* remaining buffers */
1447 if ((bp->b_flags & (B_DELWRI|B_NEEDSGIANT)) ==
1448 (B_DELWRI|B_NEEDSGIANT))
1449 bp->b_qindex = QUEUE_DIRTY_GIANT;
1450 else if (bp->b_flags & B_DELWRI)
1451 bp->b_qindex = QUEUE_DIRTY;
1453 bp->b_qindex = QUEUE_CLEAN;
1454 if (bp->b_flags & B_AGE)
1455 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1457 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1459 mtx_unlock(&bqlock);
1462 * Fixup numfreebuffers count. The bp is on an appropriate queue
1463 * unless locked. We then bump numfreebuffers if it is not B_DELWRI.
1464 * We've already handled the B_INVAL case ( B_DELWRI will be clear
1465 * if B_INVAL is set ).
1468 if (!(bp->b_flags & B_DELWRI)) {
1480 * Something we can maybe free or reuse
1482 if (bp->b_bufsize || bp->b_kvasize)
1485 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT);
1486 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1487 panic("brelse: not dirty");
1493 * Release a buffer back to the appropriate queue but do not try to free
1494 * it. The buffer is expected to be used again soon.
1496 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1497 * biodone() to requeue an async I/O on completion. It is also used when
1498 * known good buffers need to be requeued but we think we may need the data
1501 * XXX we should be able to leave the B_RELBUF hint set on completion.
1504 bqrelse(struct buf *bp)
1508 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1509 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1510 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1512 if (BUF_LOCKRECURSED(bp)) {
1513 /* do not release to free list */
1519 if (bp->b_flags & B_MANAGED) {
1520 if (bp->b_flags & B_REMFREE) {
1527 mtx_unlock(&bqlock);
1529 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1535 /* Handle delayed bremfree() processing. */
1536 if (bp->b_flags & B_REMFREE) {
1543 if (bp->b_qindex != QUEUE_NONE)
1544 panic("bqrelse: free buffer onto another queue???");
1545 /* buffers with stale but valid contents */
1546 if (bp->b_flags & B_DELWRI) {
1547 if (bp->b_flags & B_NEEDSGIANT)
1548 bp->b_qindex = QUEUE_DIRTY_GIANT;
1550 bp->b_qindex = QUEUE_DIRTY;
1551 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1554 * The locking of the BO_LOCK for checking of the
1555 * BV_BKGRDINPROG is not necessary since the
1556 * BV_BKGRDINPROG cannot be set while we hold the buf
1557 * lock, it can only be cleared if it is already
1560 if (!buf_vm_page_count_severe() || (bp->b_vflags & BV_BKGRDINPROG)) {
1561 bp->b_qindex = QUEUE_CLEAN;
1562 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_CLEAN], bp,
1566 * We are too low on memory, we have to try to free
1567 * the buffer (most importantly: the wired pages
1568 * making up its backing store) *now*.
1570 mtx_unlock(&bqlock);
1575 mtx_unlock(&bqlock);
1577 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI)) {
1586 * Something we can maybe free or reuse.
1588 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1591 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1592 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1593 panic("bqrelse: not dirty");
1598 /* Give pages used by the bp back to the VM system (where possible) */
1600 vfs_vmio_release(struct buf *bp)
1605 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), bp->b_npages);
1606 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
1607 vm_page_lock_queues();
1608 for (i = 0; i < bp->b_npages; i++) {
1610 bp->b_pages[i] = NULL;
1612 * In order to keep page LRU ordering consistent, put
1613 * everything on the inactive queue.
1615 vm_page_unwire(m, 0);
1617 * We don't mess with busy pages, it is
1618 * the responsibility of the process that
1619 * busied the pages to deal with them.
1621 if ((m->oflags & VPO_BUSY) || (m->busy != 0))
1624 if (m->wire_count == 0) {
1626 * Might as well free the page if we can and it has
1627 * no valid data. We also free the page if the
1628 * buffer was used for direct I/O
1630 if ((bp->b_flags & B_ASYNC) == 0 && !m->valid &&
1631 m->hold_count == 0) {
1633 } else if (bp->b_flags & B_DIRECT) {
1634 vm_page_try_to_free(m);
1635 } else if (buf_vm_page_count_severe()) {
1636 vm_page_try_to_cache(m);
1640 vm_page_unlock_queues();
1641 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
1643 if (bp->b_bufsize) {
1648 bp->b_flags &= ~B_VMIO;
1654 * Check to see if a block at a particular lbn is available for a clustered
1658 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
1665 /* If the buf isn't in core skip it */
1666 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
1669 /* If the buf is busy we don't want to wait for it */
1670 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1673 /* Only cluster with valid clusterable delayed write buffers */
1674 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
1675 (B_DELWRI | B_CLUSTEROK))
1678 if (bpa->b_bufsize != size)
1682 * Check to see if it is in the expected place on disk and that the
1683 * block has been mapped.
1685 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
1695 * Implement clustered async writes for clearing out B_DELWRI buffers.
1696 * This is much better then the old way of writing only one buffer at
1697 * a time. Note that we may not be presented with the buffers in the
1698 * correct order, so we search for the cluster in both directions.
1701 vfs_bio_awrite(struct buf *bp)
1706 daddr_t lblkno = bp->b_lblkno;
1707 struct vnode *vp = bp->b_vp;
1715 * right now we support clustered writing only to regular files. If
1716 * we find a clusterable block we could be in the middle of a cluster
1717 * rather then at the beginning.
1719 if ((vp->v_type == VREG) &&
1720 (vp->v_mount != 0) && /* Only on nodes that have the size info */
1721 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
1723 size = vp->v_mount->mnt_stat.f_iosize;
1724 maxcl = MAXPHYS / size;
1727 for (i = 1; i < maxcl; i++)
1728 if (vfs_bio_clcheck(vp, size, lblkno + i,
1729 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
1732 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
1733 if (vfs_bio_clcheck(vp, size, lblkno - j,
1734 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
1740 * this is a possible cluster write
1744 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl);
1749 bp->b_flags |= B_ASYNC;
1751 * default (old) behavior, writing out only one block
1753 * XXX returns b_bufsize instead of b_bcount for nwritten?
1755 nwritten = bp->b_bufsize;
1764 * Find and initialize a new buffer header, freeing up existing buffers
1765 * in the bufqueues as necessary. The new buffer is returned locked.
1767 * Important: B_INVAL is not set. If the caller wishes to throw the
1768 * buffer away, the caller must set B_INVAL prior to calling brelse().
1771 * We have insufficient buffer headers
1772 * We have insufficient buffer space
1773 * buffer_map is too fragmented ( space reservation fails )
1774 * If we have to flush dirty buffers ( but we try to avoid this )
1776 * To avoid VFS layer recursion we do not flush dirty buffers ourselves.
1777 * Instead we ask the buf daemon to do it for us. We attempt to
1778 * avoid piecemeal wakeups of the pageout daemon.
1782 getnewbuf(struct vnode *vp, int slpflag, int slptimeo, int size, int maxsize,
1790 static int flushingbufs;
1794 * We can't afford to block since we might be holding a vnode lock,
1795 * which may prevent system daemons from running. We deal with
1796 * low-memory situations by proactively returning memory and running
1797 * async I/O rather then sync I/O.
1799 atomic_add_int(&getnewbufcalls, 1);
1800 atomic_subtract_int(&getnewbufrestarts, 1);
1802 atomic_add_int(&getnewbufrestarts, 1);
1805 * Setup for scan. If we do not have enough free buffers,
1806 * we setup a degenerate case that immediately fails. Note
1807 * that if we are specially marked process, we are allowed to
1808 * dip into our reserves.
1810 * The scanning sequence is nominally: EMPTY->EMPTYKVA->CLEAN
1812 * We start with EMPTYKVA. If the list is empty we backup to EMPTY.
1813 * However, there are a number of cases (defragging, reusing, ...)
1814 * where we cannot backup.
1817 nqindex = QUEUE_EMPTYKVA;
1818 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA]);
1822 * If no EMPTYKVA buffers and we are either
1823 * defragging or reusing, locate a CLEAN buffer
1824 * to free or reuse. If bufspace useage is low
1825 * skip this step so we can allocate a new buffer.
1827 if (defrag || bufspace >= lobufspace) {
1828 nqindex = QUEUE_CLEAN;
1829 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
1833 * If we could not find or were not allowed to reuse a
1834 * CLEAN buffer, check to see if it is ok to use an EMPTY
1835 * buffer. We can only use an EMPTY buffer if allocating
1836 * its KVA would not otherwise run us out of buffer space.
1838 if (nbp == NULL && defrag == 0 &&
1839 bufspace + maxsize < hibufspace) {
1840 nqindex = QUEUE_EMPTY;
1841 nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1846 * Run scan, possibly freeing data and/or kva mappings on the fly
1850 while ((bp = nbp) != NULL) {
1851 int qindex = nqindex;
1854 * Calculate next bp ( we can only use it if we do not block
1855 * or do other fancy things ).
1857 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
1860 nqindex = QUEUE_EMPTYKVA;
1861 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTYKVA])))
1864 case QUEUE_EMPTYKVA:
1865 nqindex = QUEUE_CLEAN;
1866 if ((nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN])))
1877 * If we are defragging then we need a buffer with
1878 * b_kvasize != 0. XXX this situation should no longer
1879 * occur, if defrag is non-zero the buffer's b_kvasize
1880 * should also be non-zero at this point. XXX
1882 if (defrag && bp->b_kvasize == 0) {
1883 printf("Warning: defrag empty buffer %p\n", bp);
1888 * Start freeing the bp. This is somewhat involved. nbp
1889 * remains valid only for QUEUE_EMPTY[KVA] bp's.
1891 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1894 BO_LOCK(bp->b_bufobj);
1895 if (bp->b_vflags & BV_BKGRDINPROG) {
1896 BO_UNLOCK(bp->b_bufobj);
1900 BO_UNLOCK(bp->b_bufobj);
1903 "getnewbuf(%p) vp %p flags %X kvasize %d bufsize %d "
1904 "queue %d (recycling)", bp, bp->b_vp, bp->b_flags,
1905 bp->b_kvasize, bp->b_bufsize, qindex);
1910 KASSERT(bp->b_qindex == qindex, ("getnewbuf: inconsistant queue %d bp %p", qindex, bp));
1913 * Note: we no longer distinguish between VMIO and non-VMIO
1917 KASSERT((bp->b_flags & B_DELWRI) == 0, ("delwri buffer %p found in queue %d", bp, qindex));
1919 if (bp->b_bufobj != NULL)
1920 BO_LOCK(bp->b_bufobj);
1922 if (bp->b_bufobj != NULL)
1923 BO_UNLOCK(bp->b_bufobj);
1924 mtx_unlock(&bqlock);
1926 if (qindex == QUEUE_CLEAN) {
1927 if (bp->b_flags & B_VMIO) {
1928 bp->b_flags &= ~B_ASYNC;
1929 vfs_vmio_release(bp);
1936 * NOTE: nbp is now entirely invalid. We can only restart
1937 * the scan from this point on.
1939 * Get the rest of the buffer freed up. b_kva* is still
1940 * valid after this operation.
1943 if (bp->b_rcred != NOCRED) {
1944 crfree(bp->b_rcred);
1945 bp->b_rcred = NOCRED;
1947 if (bp->b_wcred != NOCRED) {
1948 crfree(bp->b_wcred);
1949 bp->b_wcred = NOCRED;
1951 if (!LIST_EMPTY(&bp->b_dep))
1953 if (bp->b_vflags & BV_BKGRDINPROG)
1954 panic("losing buffer 3");
1955 KASSERT(bp->b_vp == NULL,
1956 ("bp: %p still has vnode %p. qindex: %d",
1957 bp, bp->b_vp, qindex));
1958 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
1959 ("bp: %p still on a buffer list. xflags %X",
1968 KASSERT((bp->b_vflags & BV_INFREECNT) == 0,
1969 ("buf %p still counted as free?", bp));
1972 bp->b_blkno = bp->b_lblkno = 0;
1973 bp->b_offset = NOOFFSET;
1979 bp->b_dirtyoff = bp->b_dirtyend = 0;
1980 bp->b_bufobj = NULL;
1981 bp->b_pin_count = 0;
1982 bp->b_fsprivate1 = NULL;
1983 bp->b_fsprivate2 = NULL;
1984 bp->b_fsprivate3 = NULL;
1986 LIST_INIT(&bp->b_dep);
1989 * If we are defragging then free the buffer.
1992 bp->b_flags |= B_INVAL;
2000 * Notify any waiters for the buffer lock about
2001 * identity change by freeing the buffer.
2003 if (qindex == QUEUE_CLEAN && BUF_LOCKWAITERS(bp)) {
2004 bp->b_flags |= B_INVAL;
2011 * If we are overcomitted then recover the buffer and its
2012 * KVM space. This occurs in rare situations when multiple
2013 * processes are blocked in getnewbuf() or allocbuf().
2015 if (bufspace >= hibufspace)
2017 if (flushingbufs && bp->b_kvasize != 0) {
2018 bp->b_flags |= B_INVAL;
2023 if (bufspace < lobufspace)
2029 * If we exhausted our list, sleep as appropriate. We may have to
2030 * wakeup various daemons and write out some dirty buffers.
2032 * Generally we are sleeping due to insufficient buffer space.
2036 int flags, norunbuf;
2041 flags = VFS_BIO_NEED_BUFSPACE;
2043 } else if (bufspace >= hibufspace) {
2045 flags = VFS_BIO_NEED_BUFSPACE;
2048 flags = VFS_BIO_NEED_ANY;
2051 needsbuffer |= flags;
2052 mtx_unlock(&nblock);
2053 mtx_unlock(&bqlock);
2055 bd_speedup(); /* heeeelp */
2056 if (gbflags & GB_NOWAIT_BD)
2060 while (needsbuffer & flags) {
2061 if (vp != NULL && (td->td_pflags & TDP_BUFNEED) == 0) {
2062 mtx_unlock(&nblock);
2064 * getblk() is called with a vnode
2065 * locked, and some majority of the
2066 * dirty buffers may as well belong to
2067 * the vnode. Flushing the buffers
2068 * there would make a progress that
2069 * cannot be achieved by the
2070 * buf_daemon, that cannot lock the
2073 norunbuf = ~(TDP_BUFNEED | TDP_NORUNNINGBUF) |
2074 (td->td_pflags & TDP_NORUNNINGBUF);
2075 /* play bufdaemon */
2076 td->td_pflags |= TDP_BUFNEED | TDP_NORUNNINGBUF;
2077 fl = buf_do_flush(vp);
2078 td->td_pflags &= norunbuf;
2082 if ((needsbuffer & flags) == 0)
2085 if (msleep(&needsbuffer, &nblock,
2086 (PRIBIO + 4) | slpflag, waitmsg, slptimeo)) {
2087 mtx_unlock(&nblock);
2091 mtx_unlock(&nblock);
2094 * We finally have a valid bp. We aren't quite out of the
2095 * woods, we still have to reserve kva space. In order
2096 * to keep fragmentation sane we only allocate kva in
2099 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
2101 if (maxsize != bp->b_kvasize) {
2102 vm_offset_t addr = 0;
2106 vm_map_lock(buffer_map);
2107 if (vm_map_findspace(buffer_map,
2108 vm_map_min(buffer_map), maxsize, &addr)) {
2110 * Uh oh. Buffer map is to fragmented. We
2111 * must defragment the map.
2113 atomic_add_int(&bufdefragcnt, 1);
2114 vm_map_unlock(buffer_map);
2116 bp->b_flags |= B_INVAL;
2121 vm_map_insert(buffer_map, NULL, 0,
2122 addr, addr + maxsize,
2123 VM_PROT_ALL, VM_PROT_ALL, MAP_NOFAULT);
2125 bp->b_kvabase = (caddr_t) addr;
2126 bp->b_kvasize = maxsize;
2127 atomic_add_long(&bufspace, bp->b_kvasize);
2128 atomic_add_int(&bufreusecnt, 1);
2130 vm_map_unlock(buffer_map);
2132 bp->b_saveaddr = bp->b_kvabase;
2133 bp->b_data = bp->b_saveaddr;
2141 * buffer flushing daemon. Buffers are normally flushed by the
2142 * update daemon but if it cannot keep up this process starts to
2143 * take the load in an attempt to prevent getnewbuf() from blocking.
2146 static struct kproc_desc buf_kp = {
2151 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp);
2154 buf_do_flush(struct vnode *vp)
2158 flushed = flushbufqueues(vp, QUEUE_DIRTY, 0);
2159 /* The list empty check here is slightly racy */
2160 if (!TAILQ_EMPTY(&bufqueues[QUEUE_DIRTY_GIANT])) {
2162 flushed += flushbufqueues(vp, QUEUE_DIRTY_GIANT, 0);
2167 * Could not find any buffers without rollback
2168 * dependencies, so just write the first one
2169 * in the hopes of eventually making progress.
2171 flushbufqueues(vp, QUEUE_DIRTY, 1);
2173 &bufqueues[QUEUE_DIRTY_GIANT])) {
2175 flushbufqueues(vp, QUEUE_DIRTY_GIANT, 1);
2187 * This process needs to be suspended prior to shutdown sync.
2189 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
2193 * This process is allowed to take the buffer cache to the limit
2195 curthread->td_pflags |= TDP_NORUNNINGBUF | TDP_BUFNEED;
2199 mtx_unlock(&bdlock);
2201 kproc_suspend_check(bufdaemonproc);
2204 * Do the flush. Limit the amount of in-transit I/O we
2205 * allow to build up, otherwise we would completely saturate
2206 * the I/O system. Wakeup any waiting processes before we
2207 * normally would so they can run in parallel with our drain.
2209 while (numdirtybuffers > lodirtybuffers) {
2210 if (buf_do_flush(NULL) == 0)
2216 * Only clear bd_request if we have reached our low water
2217 * mark. The buf_daemon normally waits 1 second and
2218 * then incrementally flushes any dirty buffers that have
2219 * built up, within reason.
2221 * If we were unable to hit our low water mark and couldn't
2222 * find any flushable buffers, we sleep half a second.
2223 * Otherwise we loop immediately.
2226 if (numdirtybuffers <= lodirtybuffers) {
2228 * We reached our low water mark, reset the
2229 * request and sleep until we are needed again.
2230 * The sleep is just so the suspend code works.
2233 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
2236 * We couldn't find any flushable dirty buffers but
2237 * still have too many dirty buffers, we
2238 * have to sleep and try again. (rare)
2240 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
2248 * Try to flush a buffer in the dirty queue. We must be careful to
2249 * free up B_INVAL buffers instead of write them, which NFS is
2250 * particularly sensitive to.
2252 static int flushwithdeps = 0;
2253 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
2254 0, "Number of buffers flushed with dependecies that require rollbacks");
2257 flushbufqueues(struct vnode *lvp, int queue, int flushdeps)
2259 struct buf *sentinel;
2268 target = numdirtybuffers - lodirtybuffers;
2269 if (flushdeps && target > 2)
2272 target = flushbufqtarget;
2275 sentinel = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
2276 sentinel->b_qindex = QUEUE_SENTINEL;
2278 TAILQ_INSERT_HEAD(&bufqueues[queue], sentinel, b_freelist);
2279 while (flushed != target) {
2280 bp = TAILQ_NEXT(sentinel, b_freelist);
2282 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2283 TAILQ_INSERT_AFTER(&bufqueues[queue], bp, sentinel,
2288 * Skip sentinels inserted by other invocations of the
2289 * flushbufqueues(), taking care to not reorder them.
2291 if (bp->b_qindex == QUEUE_SENTINEL)
2294 * Only flush the buffers that belong to the
2295 * vnode locked by the curthread.
2297 if (lvp != NULL && bp->b_vp != lvp)
2299 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2301 if (bp->b_pin_count > 0) {
2305 BO_LOCK(bp->b_bufobj);
2306 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
2307 (bp->b_flags & B_DELWRI) == 0) {
2308 BO_UNLOCK(bp->b_bufobj);
2312 BO_UNLOCK(bp->b_bufobj);
2313 if (bp->b_flags & B_INVAL) {
2315 mtx_unlock(&bqlock);
2318 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2323 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) {
2324 if (flushdeps == 0) {
2332 * We must hold the lock on a vnode before writing
2333 * one of its buffers. Otherwise we may confuse, or
2334 * in the case of a snapshot vnode, deadlock the
2337 * The lock order here is the reverse of the normal
2338 * of vnode followed by buf lock. This is ok because
2339 * the NOWAIT will prevent deadlock.
2342 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2346 if (vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_CANRECURSE) == 0) {
2347 mtx_unlock(&bqlock);
2348 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
2349 bp, bp->b_vp, bp->b_flags);
2350 if (curproc == bufdaemonproc)
2357 vn_finished_write(mp);
2359 flushwithdeps += hasdeps;
2363 * Sleeping on runningbufspace while holding
2364 * vnode lock leads to deadlock.
2366 if (curproc == bufdaemonproc)
2367 waitrunningbufspace();
2368 numdirtywakeup((lodirtybuffers + hidirtybuffers) / 2);
2372 vn_finished_write(mp);
2375 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2376 mtx_unlock(&bqlock);
2377 free(sentinel, M_TEMP);
2382 * Check to see if a block is currently memory resident.
2385 incore(struct bufobj *bo, daddr_t blkno)
2390 bp = gbincore(bo, blkno);
2396 * Returns true if no I/O is needed to access the
2397 * associated VM object. This is like incore except
2398 * it also hunts around in the VM system for the data.
2402 inmem(struct vnode * vp, daddr_t blkno)
2405 vm_offset_t toff, tinc, size;
2409 ASSERT_VOP_LOCKED(vp, "inmem");
2411 if (incore(&vp->v_bufobj, blkno))
2413 if (vp->v_mount == NULL)
2420 if (size > vp->v_mount->mnt_stat.f_iosize)
2421 size = vp->v_mount->mnt_stat.f_iosize;
2422 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
2424 VM_OBJECT_LOCK(obj);
2425 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
2426 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
2430 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
2431 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
2432 if (vm_page_is_valid(m,
2433 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
2436 VM_OBJECT_UNLOCK(obj);
2440 VM_OBJECT_UNLOCK(obj);
2445 * Set the dirty range for a buffer based on the status of the dirty
2446 * bits in the pages comprising the buffer. The range is limited
2447 * to the size of the buffer.
2449 * Tell the VM system that the pages associated with this buffer
2450 * are clean. This is used for delayed writes where the data is
2451 * going to go to disk eventually without additional VM intevention.
2453 * Note that while we only really need to clean through to b_bcount, we
2454 * just go ahead and clean through to b_bufsize.
2457 vfs_clean_pages_dirty_buf(struct buf *bp)
2459 vm_ooffset_t foff, noff, eoff;
2463 if ((bp->b_flags & B_VMIO) == 0 || bp->b_bufsize == 0)
2466 foff = bp->b_offset;
2467 KASSERT(bp->b_offset != NOOFFSET,
2468 ("vfs_clean_pages_dirty_buf: no buffer offset"));
2470 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
2471 vfs_drain_busy_pages(bp);
2472 vfs_setdirty_locked_object(bp);
2473 vm_page_lock_queues();
2474 for (i = 0; i < bp->b_npages; i++) {
2475 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
2477 if (eoff > bp->b_offset + bp->b_bufsize)
2478 eoff = bp->b_offset + bp->b_bufsize;
2480 vfs_page_set_validclean(bp, foff, m);
2481 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
2484 vm_page_unlock_queues();
2485 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
2489 vfs_setdirty_locked_object(struct buf *bp)
2494 object = bp->b_bufobj->bo_object;
2495 VM_OBJECT_LOCK_ASSERT(object, MA_OWNED);
2498 * We qualify the scan for modified pages on whether the
2499 * object has been flushed yet.
2501 if (object->flags & (OBJ_MIGHTBEDIRTY|OBJ_CLEANING)) {
2502 vm_offset_t boffset;
2503 vm_offset_t eoffset;
2505 vm_page_lock_queues();
2507 * test the pages to see if they have been modified directly
2508 * by users through the VM system.
2510 for (i = 0; i < bp->b_npages; i++)
2511 vm_page_test_dirty(bp->b_pages[i]);
2514 * Calculate the encompassing dirty range, boffset and eoffset,
2515 * (eoffset - boffset) bytes.
2518 for (i = 0; i < bp->b_npages; i++) {
2519 if (bp->b_pages[i]->dirty)
2522 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2524 for (i = bp->b_npages - 1; i >= 0; --i) {
2525 if (bp->b_pages[i]->dirty) {
2529 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
2531 vm_page_unlock_queues();
2533 * Fit it to the buffer.
2536 if (eoffset > bp->b_bcount)
2537 eoffset = bp->b_bcount;
2540 * If we have a good dirty range, merge with the existing
2544 if (boffset < eoffset) {
2545 if (bp->b_dirtyoff > boffset)
2546 bp->b_dirtyoff = boffset;
2547 if (bp->b_dirtyend < eoffset)
2548 bp->b_dirtyend = eoffset;
2556 * Get a block given a specified block and offset into a file/device.
2557 * The buffers B_DONE bit will be cleared on return, making it almost
2558 * ready for an I/O initiation. B_INVAL may or may not be set on
2559 * return. The caller should clear B_INVAL prior to initiating a
2562 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
2563 * an existing buffer.
2565 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
2566 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
2567 * and then cleared based on the backing VM. If the previous buffer is
2568 * non-0-sized but invalid, B_CACHE will be cleared.
2570 * If getblk() must create a new buffer, the new buffer is returned with
2571 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
2572 * case it is returned with B_INVAL clear and B_CACHE set based on the
2575 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
2576 * B_CACHE bit is clear.
2578 * What this means, basically, is that the caller should use B_CACHE to
2579 * determine whether the buffer is fully valid or not and should clear
2580 * B_INVAL prior to issuing a read. If the caller intends to validate
2581 * the buffer by loading its data area with something, the caller needs
2582 * to clear B_INVAL. If the caller does this without issuing an I/O,
2583 * the caller should set B_CACHE ( as an optimization ), else the caller
2584 * should issue the I/O and biodone() will set B_CACHE if the I/O was
2585 * a write attempt or if it was a successfull read. If the caller
2586 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
2587 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
2590 getblk(struct vnode * vp, daddr_t blkno, int size, int slpflag, int slptimeo,
2597 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
2598 ASSERT_VOP_LOCKED(vp, "getblk");
2599 if (size > MAXBSIZE)
2600 panic("getblk: size(%d) > MAXBSIZE(%d)\n", size, MAXBSIZE);
2605 * Block if we are low on buffers. Certain processes are allowed
2606 * to completely exhaust the buffer cache.
2608 * If this check ever becomes a bottleneck it may be better to
2609 * move it into the else, when gbincore() fails. At the moment
2610 * it isn't a problem.
2612 * XXX remove if 0 sections (clean this up after its proven)
2614 if (numfreebuffers == 0) {
2615 if (TD_IS_IDLETHREAD(curthread))
2618 needsbuffer |= VFS_BIO_NEED_ANY;
2619 mtx_unlock(&nblock);
2623 bp = gbincore(bo, blkno);
2627 * Buffer is in-core. If the buffer is not busy, it must
2630 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
2632 if (flags & GB_LOCK_NOWAIT)
2633 lockflags |= LK_NOWAIT;
2635 error = BUF_TIMELOCK(bp, lockflags,
2636 BO_MTX(bo), "getblk", slpflag, slptimeo);
2639 * If we slept and got the lock we have to restart in case
2640 * the buffer changed identities.
2642 if (error == ENOLCK)
2644 /* We timed out or were interrupted. */
2649 * The buffer is locked. B_CACHE is cleared if the buffer is
2650 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
2651 * and for a VMIO buffer B_CACHE is adjusted according to the
2654 if (bp->b_flags & B_INVAL)
2655 bp->b_flags &= ~B_CACHE;
2656 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
2657 bp->b_flags |= B_CACHE;
2663 * check for size inconsistancies for non-VMIO case.
2666 if (bp->b_bcount != size) {
2667 if ((bp->b_flags & B_VMIO) == 0 ||
2668 (size > bp->b_kvasize)) {
2669 if (bp->b_flags & B_DELWRI) {
2671 * If buffer is pinned and caller does
2672 * not want sleep waiting for it to be
2673 * unpinned, bail out
2675 if (bp->b_pin_count > 0) {
2676 if (flags & GB_LOCK_NOWAIT) {
2683 bp->b_flags |= B_NOCACHE;
2686 if (LIST_EMPTY(&bp->b_dep)) {
2687 bp->b_flags |= B_RELBUF;
2690 bp->b_flags |= B_NOCACHE;
2699 * If the size is inconsistant in the VMIO case, we can resize
2700 * the buffer. This might lead to B_CACHE getting set or
2701 * cleared. If the size has not changed, B_CACHE remains
2702 * unchanged from its previous state.
2705 if (bp->b_bcount != size)
2708 KASSERT(bp->b_offset != NOOFFSET,
2709 ("getblk: no buffer offset"));
2712 * A buffer with B_DELWRI set and B_CACHE clear must
2713 * be committed before we can return the buffer in
2714 * order to prevent the caller from issuing a read
2715 * ( due to B_CACHE not being set ) and overwriting
2718 * Most callers, including NFS and FFS, need this to
2719 * operate properly either because they assume they
2720 * can issue a read if B_CACHE is not set, or because
2721 * ( for example ) an uncached B_DELWRI might loop due
2722 * to softupdates re-dirtying the buffer. In the latter
2723 * case, B_CACHE is set after the first write completes,
2724 * preventing further loops.
2725 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
2726 * above while extending the buffer, we cannot allow the
2727 * buffer to remain with B_CACHE set after the write
2728 * completes or it will represent a corrupt state. To
2729 * deal with this we set B_NOCACHE to scrap the buffer
2732 * We might be able to do something fancy, like setting
2733 * B_CACHE in bwrite() except if B_DELWRI is already set,
2734 * so the below call doesn't set B_CACHE, but that gets real
2735 * confusing. This is much easier.
2738 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
2739 bp->b_flags |= B_NOCACHE;
2743 bp->b_flags &= ~B_DONE;
2745 int bsize, maxsize, vmio;
2749 * Buffer is not in-core, create new buffer. The buffer
2750 * returned by getnewbuf() is locked. Note that the returned
2751 * buffer is also considered valid (not marked B_INVAL).
2755 * If the user does not want us to create the buffer, bail out
2758 if (flags & GB_NOCREAT)
2760 bsize = vn_isdisk(vp, NULL) ? DEV_BSIZE : bo->bo_bsize;
2761 offset = blkno * bsize;
2762 vmio = vp->v_object != NULL;
2763 maxsize = vmio ? size + (offset & PAGE_MASK) : size;
2764 maxsize = imax(maxsize, bsize);
2766 bp = getnewbuf(vp, slpflag, slptimeo, size, maxsize, flags);
2768 if (slpflag || slptimeo)
2774 * This code is used to make sure that a buffer is not
2775 * created while the getnewbuf routine is blocked.
2776 * This can be a problem whether the vnode is locked or not.
2777 * If the buffer is created out from under us, we have to
2778 * throw away the one we just created.
2780 * Note: this must occur before we associate the buffer
2781 * with the vp especially considering limitations in
2782 * the splay tree implementation when dealing with duplicate
2786 if (gbincore(bo, blkno)) {
2788 bp->b_flags |= B_INVAL;
2794 * Insert the buffer into the hash, so that it can
2795 * be found by incore.
2797 bp->b_blkno = bp->b_lblkno = blkno;
2798 bp->b_offset = offset;
2803 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
2804 * buffer size starts out as 0, B_CACHE will be set by
2805 * allocbuf() for the VMIO case prior to it testing the
2806 * backing store for validity.
2810 bp->b_flags |= B_VMIO;
2811 #if defined(VFS_BIO_DEBUG)
2812 if (vn_canvmio(vp) != TRUE)
2813 printf("getblk: VMIO on vnode type %d\n",
2816 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
2817 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
2818 bp, vp->v_object, bp->b_bufobj->bo_object));
2820 bp->b_flags &= ~B_VMIO;
2821 KASSERT(bp->b_bufobj->bo_object == NULL,
2822 ("ARGH! has b_bufobj->bo_object %p %p\n",
2823 bp, bp->b_bufobj->bo_object));
2827 bp->b_flags &= ~B_DONE;
2829 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
2830 BUF_ASSERT_HELD(bp);
2831 KASSERT(bp->b_bufobj == bo,
2832 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2837 * Get an empty, disassociated buffer of given size. The buffer is initially
2841 geteblk(int size, int flags)
2846 maxsize = (size + BKVAMASK) & ~BKVAMASK;
2847 while ((bp = getnewbuf(NULL, 0, 0, size, maxsize, flags)) == NULL) {
2848 if ((flags & GB_NOWAIT_BD) &&
2849 (curthread->td_pflags & TDP_BUFNEED) != 0)
2853 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
2854 BUF_ASSERT_HELD(bp);
2860 * This code constitutes the buffer memory from either anonymous system
2861 * memory (in the case of non-VMIO operations) or from an associated
2862 * VM object (in the case of VMIO operations). This code is able to
2863 * resize a buffer up or down.
2865 * Note that this code is tricky, and has many complications to resolve
2866 * deadlock or inconsistant data situations. Tread lightly!!!
2867 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
2868 * the caller. Calling this code willy nilly can result in the loss of data.
2870 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
2871 * B_CACHE for the non-VMIO case.
2875 allocbuf(struct buf *bp, int size)
2877 int newbsize, mbsize;
2880 BUF_ASSERT_HELD(bp);
2882 if (bp->b_kvasize < size)
2883 panic("allocbuf: buffer too small");
2885 if ((bp->b_flags & B_VMIO) == 0) {
2889 * Just get anonymous memory from the kernel. Don't
2890 * mess with B_CACHE.
2892 mbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2893 if (bp->b_flags & B_MALLOC)
2896 newbsize = round_page(size);
2898 if (newbsize < bp->b_bufsize) {
2900 * malloced buffers are not shrunk
2902 if (bp->b_flags & B_MALLOC) {
2904 bp->b_bcount = size;
2906 free(bp->b_data, M_BIOBUF);
2907 if (bp->b_bufsize) {
2908 atomic_subtract_long(
2914 bp->b_saveaddr = bp->b_kvabase;
2915 bp->b_data = bp->b_saveaddr;
2917 bp->b_flags &= ~B_MALLOC;
2921 vm_hold_free_pages(bp, newbsize);
2922 } else if (newbsize > bp->b_bufsize) {
2924 * We only use malloced memory on the first allocation.
2925 * and revert to page-allocated memory when the buffer
2929 * There is a potential smp race here that could lead
2930 * to bufmallocspace slightly passing the max. It
2931 * is probably extremely rare and not worth worrying
2934 if ( (bufmallocspace < maxbufmallocspace) &&
2935 (bp->b_bufsize == 0) &&
2936 (mbsize <= PAGE_SIZE/2)) {
2938 bp->b_data = malloc(mbsize, M_BIOBUF, M_WAITOK);
2939 bp->b_bufsize = mbsize;
2940 bp->b_bcount = size;
2941 bp->b_flags |= B_MALLOC;
2942 atomic_add_long(&bufmallocspace, mbsize);
2948 * If the buffer is growing on its other-than-first allocation,
2949 * then we revert to the page-allocation scheme.
2951 if (bp->b_flags & B_MALLOC) {
2952 origbuf = bp->b_data;
2953 origbufsize = bp->b_bufsize;
2954 bp->b_data = bp->b_kvabase;
2955 if (bp->b_bufsize) {
2956 atomic_subtract_long(&bufmallocspace,
2961 bp->b_flags &= ~B_MALLOC;
2962 newbsize = round_page(newbsize);
2966 (vm_offset_t) bp->b_data + bp->b_bufsize,
2967 (vm_offset_t) bp->b_data + newbsize);
2969 bcopy(origbuf, bp->b_data, origbufsize);
2970 free(origbuf, M_BIOBUF);
2976 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
2977 desiredpages = (size == 0) ? 0 :
2978 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
2980 if (bp->b_flags & B_MALLOC)
2981 panic("allocbuf: VMIO buffer can't be malloced");
2983 * Set B_CACHE initially if buffer is 0 length or will become
2986 if (size == 0 || bp->b_bufsize == 0)
2987 bp->b_flags |= B_CACHE;
2989 if (newbsize < bp->b_bufsize) {
2991 * DEV_BSIZE aligned new buffer size is less then the
2992 * DEV_BSIZE aligned existing buffer size. Figure out
2993 * if we have to remove any pages.
2995 if (desiredpages < bp->b_npages) {
2998 pmap_qremove((vm_offset_t)trunc_page(
2999 (vm_offset_t)bp->b_data) +
3000 (desiredpages << PAGE_SHIFT),
3001 (bp->b_npages - desiredpages));
3002 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3003 vm_page_lock_queues();
3004 for (i = desiredpages; i < bp->b_npages; i++) {
3006 * the page is not freed here -- it
3007 * is the responsibility of
3008 * vnode_pager_setsize
3011 KASSERT(m != bogus_page,
3012 ("allocbuf: bogus page found"));
3013 while (vm_page_sleep_if_busy(m, TRUE, "biodep"))
3014 vm_page_lock_queues();
3016 bp->b_pages[i] = NULL;
3017 vm_page_unwire(m, 0);
3019 vm_page_unlock_queues();
3020 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3021 bp->b_npages = desiredpages;
3023 } else if (size > bp->b_bcount) {
3025 * We are growing the buffer, possibly in a
3026 * byte-granular fashion.
3033 * Step 1, bring in the VM pages from the object,
3034 * allocating them if necessary. We must clear
3035 * B_CACHE if these pages are not valid for the
3036 * range covered by the buffer.
3039 obj = bp->b_bufobj->bo_object;
3041 VM_OBJECT_LOCK(obj);
3042 while (bp->b_npages < desiredpages) {
3046 pi = OFF_TO_IDX(bp->b_offset) + bp->b_npages;
3047 if ((m = vm_page_lookup(obj, pi)) == NULL) {
3049 * note: must allocate system pages
3050 * since blocking here could intefere
3051 * with paging I/O, no matter which
3054 m = vm_page_alloc(obj, pi,
3055 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM |
3058 atomic_add_int(&vm_pageout_deficit,
3059 desiredpages - bp->b_npages);
3060 VM_OBJECT_UNLOCK(obj);
3062 VM_OBJECT_LOCK(obj);
3065 bp->b_flags &= ~B_CACHE;
3066 bp->b_pages[bp->b_npages] = m;
3073 * We found a page. If we have to sleep on it,
3074 * retry because it might have gotten freed out
3077 * We can only test VPO_BUSY here. Blocking on
3078 * m->busy might lead to a deadlock:
3080 * vm_fault->getpages->cluster_read->allocbuf
3083 if (vm_page_sleep_if_busy(m, FALSE, "pgtblk"))
3087 * We have a good page.
3089 vm_page_lock_queues();
3091 vm_page_unlock_queues();
3092 bp->b_pages[bp->b_npages] = m;
3097 * Step 2. We've loaded the pages into the buffer,
3098 * we have to figure out if we can still have B_CACHE
3099 * set. Note that B_CACHE is set according to the
3100 * byte-granular range ( bcount and size ), new the
3101 * aligned range ( newbsize ).
3103 * The VM test is against m->valid, which is DEV_BSIZE
3104 * aligned. Needless to say, the validity of the data
3105 * needs to also be DEV_BSIZE aligned. Note that this
3106 * fails with NFS if the server or some other client
3107 * extends the file's EOF. If our buffer is resized,
3108 * B_CACHE may remain set! XXX
3111 toff = bp->b_bcount;
3112 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
3114 while ((bp->b_flags & B_CACHE) && toff < size) {
3117 if (tinc > (size - toff))
3120 pi = ((bp->b_offset & PAGE_MASK) + toff) >>
3133 VM_OBJECT_UNLOCK(obj);
3136 * Step 3, fixup the KVM pmap. Remember that
3137 * bp->b_data is relative to bp->b_offset, but
3138 * bp->b_offset may be offset into the first page.
3141 bp->b_data = (caddr_t)
3142 trunc_page((vm_offset_t)bp->b_data);
3144 (vm_offset_t)bp->b_data,
3149 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
3150 (vm_offset_t)(bp->b_offset & PAGE_MASK));
3153 if (newbsize < bp->b_bufsize)
3155 bp->b_bufsize = newbsize; /* actual buffer allocation */
3156 bp->b_bcount = size; /* requested buffer size */
3161 biodone(struct bio *bp)
3164 void (*done)(struct bio *);
3166 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3168 bp->bio_flags |= BIO_DONE;
3169 done = bp->bio_done;
3178 * Wait for a BIO to finish.
3180 * XXX: resort to a timeout for now. The optimal locking (if any) for this
3181 * case is not yet clear.
3184 biowait(struct bio *bp, const char *wchan)
3188 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3190 while ((bp->bio_flags & BIO_DONE) == 0)
3191 msleep(bp, mtxp, PRIBIO, wchan, hz / 10);
3193 if (bp->bio_error != 0)
3194 return (bp->bio_error);
3195 if (!(bp->bio_flags & BIO_ERROR))
3201 biofinish(struct bio *bp, struct devstat *stat, int error)
3205 bp->bio_error = error;
3206 bp->bio_flags |= BIO_ERROR;
3209 devstat_end_transaction_bio(stat, bp);
3216 * Wait for buffer I/O completion, returning error status. The buffer
3217 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
3218 * error and cleared.
3221 bufwait(struct buf *bp)
3223 if (bp->b_iocmd == BIO_READ)
3224 bwait(bp, PRIBIO, "biord");
3226 bwait(bp, PRIBIO, "biowr");
3227 if (bp->b_flags & B_EINTR) {
3228 bp->b_flags &= ~B_EINTR;
3231 if (bp->b_ioflags & BIO_ERROR) {
3232 return (bp->b_error ? bp->b_error : EIO);
3239 * Call back function from struct bio back up to struct buf.
3242 bufdonebio(struct bio *bip)
3246 bp = bip->bio_caller2;
3247 bp->b_resid = bp->b_bcount - bip->bio_completed;
3248 bp->b_resid = bip->bio_resid; /* XXX: remove */
3249 bp->b_ioflags = bip->bio_flags;
3250 bp->b_error = bip->bio_error;
3252 bp->b_ioflags |= BIO_ERROR;
3258 dev_strategy(struct cdev *dev, struct buf *bp)
3264 if ((!bp->b_iocmd) || (bp->b_iocmd & (bp->b_iocmd - 1)))
3265 panic("b_iocmd botch");
3270 /* Try again later */
3271 tsleep(&bp, PRIBIO, "dev_strat", hz/10);
3273 bip->bio_cmd = bp->b_iocmd;
3274 bip->bio_offset = bp->b_iooffset;
3275 bip->bio_length = bp->b_bcount;
3276 bip->bio_bcount = bp->b_bcount; /* XXX: remove */
3277 bip->bio_data = bp->b_data;
3278 bip->bio_done = bufdonebio;
3279 bip->bio_caller2 = bp;
3281 KASSERT(dev->si_refcount > 0,
3282 ("dev_strategy on un-referenced struct cdev *(%s)",
3284 csw = dev_refthread(dev, &ref);
3287 bp->b_error = ENXIO;
3288 bp->b_ioflags = BIO_ERROR;
3292 (*csw->d_strategy)(bip);
3293 dev_relthread(dev, ref);
3299 * Finish I/O on a buffer, optionally calling a completion function.
3300 * This is usually called from an interrupt so process blocking is
3303 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
3304 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
3305 * assuming B_INVAL is clear.
3307 * For the VMIO case, we set B_CACHE if the op was a read and no
3308 * read error occured, or if the op was a write. B_CACHE is never
3309 * set if the buffer is invalid or otherwise uncacheable.
3311 * biodone does not mess with B_INVAL, allowing the I/O routine or the
3312 * initiator to leave B_INVAL set to brelse the buffer out of existance
3313 * in the biodone routine.
3316 bufdone(struct buf *bp)
3318 struct bufobj *dropobj;
3319 void (*biodone)(struct buf *);
3321 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
3324 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
3325 BUF_ASSERT_HELD(bp);
3327 runningbufwakeup(bp);
3328 if (bp->b_iocmd == BIO_WRITE)
3329 dropobj = bp->b_bufobj;
3330 /* call optional completion function if requested */
3331 if (bp->b_iodone != NULL) {
3332 biodone = bp->b_iodone;
3333 bp->b_iodone = NULL;
3336 bufobj_wdrop(dropobj);
3343 bufobj_wdrop(dropobj);
3347 bufdone_finish(struct buf *bp)
3349 BUF_ASSERT_HELD(bp);
3351 if (!LIST_EMPTY(&bp->b_dep))
3354 if (bp->b_flags & B_VMIO) {
3360 struct vnode *vp = bp->b_vp;
3362 obj = bp->b_bufobj->bo_object;
3364 #if defined(VFS_BIO_DEBUG)
3365 mp_fixme("usecount and vflag accessed without locks.");
3366 if (vp->v_usecount == 0) {
3367 panic("biodone: zero vnode ref count");
3370 KASSERT(vp->v_object != NULL,
3371 ("biodone: vnode %p has no vm_object", vp));
3374 foff = bp->b_offset;
3375 KASSERT(bp->b_offset != NOOFFSET,
3376 ("biodone: no buffer offset"));
3378 VM_OBJECT_LOCK(obj);
3379 #if defined(VFS_BIO_DEBUG)
3380 if (obj->paging_in_progress < bp->b_npages) {
3381 printf("biodone: paging in progress(%d) < bp->b_npages(%d)\n",
3382 obj->paging_in_progress, bp->b_npages);
3387 * Set B_CACHE if the op was a normal read and no error
3388 * occured. B_CACHE is set for writes in the b*write()
3391 iosize = bp->b_bcount - bp->b_resid;
3392 if (bp->b_iocmd == BIO_READ &&
3393 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
3394 !(bp->b_ioflags & BIO_ERROR)) {
3395 bp->b_flags |= B_CACHE;
3398 for (i = 0; i < bp->b_npages; i++) {
3402 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
3407 * cleanup bogus pages, restoring the originals
3410 if (m == bogus_page) {
3411 bogus = bogusflag = 1;
3412 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
3414 panic("biodone: page disappeared!");
3417 #if defined(VFS_BIO_DEBUG)
3418 if (OFF_TO_IDX(foff) != m->pindex) {
3420 "biodone: foff(%jd)/m->pindex(%ju) mismatch\n",
3421 (intmax_t)foff, (uintmax_t)m->pindex);
3426 * In the write case, the valid and clean bits are
3427 * already changed correctly ( see bdwrite() ), so we
3428 * only need to do this here in the read case.
3430 if ((bp->b_iocmd == BIO_READ) && !bogusflag && resid > 0) {
3431 KASSERT((m->dirty & vm_page_bits(foff &
3432 PAGE_MASK, resid)) == 0, ("bufdone_finish:"
3433 " page %p has unexpected dirty bits", m));
3434 vfs_page_set_valid(bp, foff, m);
3438 * when debugging new filesystems or buffer I/O methods, this
3439 * is the most common error that pops up. if you see this, you
3440 * have not set the page busy flag correctly!!!
3443 printf("biodone: page busy < 0, "
3444 "pindex: %d, foff: 0x(%x,%x), "
3445 "resid: %d, index: %d\n",
3446 (int) m->pindex, (int)(foff >> 32),
3447 (int) foff & 0xffffffff, resid, i);
3448 if (!vn_isdisk(vp, NULL))
3449 printf(" iosize: %jd, lblkno: %jd, flags: 0x%x, npages: %d\n",
3450 (intmax_t)bp->b_vp->v_mount->mnt_stat.f_iosize,
3451 (intmax_t) bp->b_lblkno,
3452 bp->b_flags, bp->b_npages);
3454 printf(" VDEV, lblkno: %jd, flags: 0x%x, npages: %d\n",
3455 (intmax_t) bp->b_lblkno,
3456 bp->b_flags, bp->b_npages);
3457 printf(" valid: 0x%lx, dirty: 0x%lx, wired: %d\n",
3458 (u_long)m->valid, (u_long)m->dirty,
3460 panic("biodone: page busy < 0\n");
3462 vm_page_io_finish(m);
3463 vm_object_pip_subtract(obj, 1);
3464 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3467 vm_object_pip_wakeupn(obj, 0);
3468 VM_OBJECT_UNLOCK(obj);
3470 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3471 bp->b_pages, bp->b_npages);
3475 * For asynchronous completions, release the buffer now. The brelse
3476 * will do a wakeup there if necessary - so no need to do a wakeup
3477 * here in the async case. The sync case always needs to do a wakeup.
3480 if (bp->b_flags & B_ASYNC) {
3481 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) || (bp->b_ioflags & BIO_ERROR))
3490 * This routine is called in lieu of iodone in the case of
3491 * incomplete I/O. This keeps the busy status for pages
3495 vfs_unbusy_pages(struct buf *bp)
3501 runningbufwakeup(bp);
3502 if (!(bp->b_flags & B_VMIO))
3505 obj = bp->b_bufobj->bo_object;
3506 VM_OBJECT_LOCK(obj);
3507 for (i = 0; i < bp->b_npages; i++) {
3509 if (m == bogus_page) {
3510 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
3512 panic("vfs_unbusy_pages: page missing\n");
3514 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3515 bp->b_pages, bp->b_npages);
3517 vm_object_pip_subtract(obj, 1);
3518 vm_page_io_finish(m);
3520 vm_object_pip_wakeupn(obj, 0);
3521 VM_OBJECT_UNLOCK(obj);
3525 * vfs_page_set_valid:
3527 * Set the valid bits in a page based on the supplied offset. The
3528 * range is restricted to the buffer's size.
3530 * This routine is typically called after a read completes.
3533 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3538 * Compute the end offset, eoff, such that [off, eoff) does not span a
3539 * page boundary and eoff is not greater than the end of the buffer.
3540 * The end of the buffer, in this case, is our file EOF, not the
3541 * allocation size of the buffer.
3543 eoff = (off + PAGE_SIZE) & ~(vm_ooffset_t)PAGE_MASK;
3544 if (eoff > bp->b_offset + bp->b_bcount)
3545 eoff = bp->b_offset + bp->b_bcount;
3548 * Set valid range. This is typically the entire buffer and thus the
3552 vm_page_set_valid(m, off & PAGE_MASK, eoff - off);
3556 * vfs_page_set_validclean:
3558 * Set the valid bits and clear the dirty bits in a page based on the
3559 * supplied offset. The range is restricted to the buffer's size.
3562 vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3564 vm_ooffset_t soff, eoff;
3566 mtx_assert(&vm_page_queue_mtx, MA_OWNED);
3568 * Start and end offsets in buffer. eoff - soff may not cross a
3569 * page boundry or cross the end of the buffer. The end of the
3570 * buffer, in this case, is our file EOF, not the allocation size
3574 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3575 if (eoff > bp->b_offset + bp->b_bcount)
3576 eoff = bp->b_offset + bp->b_bcount;
3579 * Set valid range. This is typically the entire buffer and thus the
3583 vm_page_set_validclean(
3585 (vm_offset_t) (soff & PAGE_MASK),
3586 (vm_offset_t) (eoff - soff)
3592 * Ensure that all buffer pages are not busied by VPO_BUSY flag. If
3593 * any page is busy, drain the flag.
3596 vfs_drain_busy_pages(struct buf *bp)
3601 VM_OBJECT_LOCK_ASSERT(bp->b_bufobj->bo_object, MA_OWNED);
3603 for (i = 0; i < bp->b_npages; i++) {
3605 if ((m->oflags & VPO_BUSY) != 0) {
3606 for (; last_busied < i; last_busied++)
3607 vm_page_busy(bp->b_pages[last_busied]);
3608 while ((m->oflags & VPO_BUSY) != 0)
3609 vm_page_sleep(m, "vbpage");
3612 for (i = 0; i < last_busied; i++)
3613 vm_page_wakeup(bp->b_pages[i]);
3617 * This routine is called before a device strategy routine.
3618 * It is used to tell the VM system that paging I/O is in
3619 * progress, and treat the pages associated with the buffer
3620 * almost as being VPO_BUSY. Also the object paging_in_progress
3621 * flag is handled to make sure that the object doesn't become
3624 * Since I/O has not been initiated yet, certain buffer flags
3625 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
3626 * and should be ignored.
3629 vfs_busy_pages(struct buf *bp, int clear_modify)
3636 if (!(bp->b_flags & B_VMIO))
3639 obj = bp->b_bufobj->bo_object;
3640 foff = bp->b_offset;
3641 KASSERT(bp->b_offset != NOOFFSET,
3642 ("vfs_busy_pages: no buffer offset"));
3643 VM_OBJECT_LOCK(obj);
3644 vfs_drain_busy_pages(bp);
3645 if (bp->b_bufsize != 0)
3646 vfs_setdirty_locked_object(bp);
3649 vm_page_lock_queues();
3650 for (i = 0; i < bp->b_npages; i++) {
3653 if ((bp->b_flags & B_CLUSTER) == 0) {
3654 vm_object_pip_add(obj, 1);
3655 vm_page_io_start(m);
3658 * When readying a buffer for a read ( i.e
3659 * clear_modify == 0 ), it is important to do
3660 * bogus_page replacement for valid pages in
3661 * partially instantiated buffers. Partially
3662 * instantiated buffers can, in turn, occur when
3663 * reconstituting a buffer from its VM backing store
3664 * base. We only have to do this if B_CACHE is
3665 * clear ( which causes the I/O to occur in the
3666 * first place ). The replacement prevents the read
3667 * I/O from overwriting potentially dirty VM-backed
3668 * pages. XXX bogus page replacement is, uh, bogus.
3669 * It may not work properly with small-block devices.
3670 * We need to find a better way.
3673 pmap_remove_write(m);
3674 vfs_page_set_validclean(bp, foff, m);
3675 } else if (m->valid == VM_PAGE_BITS_ALL &&
3676 (bp->b_flags & B_CACHE) == 0) {
3677 bp->b_pages[i] = bogus_page;
3680 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3683 vm_page_unlock_queues();
3684 VM_OBJECT_UNLOCK(obj);
3686 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3687 bp->b_pages, bp->b_npages);
3691 * vfs_bio_set_valid:
3693 * Set the range within the buffer to valid. The range is
3694 * relative to the beginning of the buffer, b_offset. Note that
3695 * b_offset itself may be offset from the beginning of the first
3699 vfs_bio_set_valid(struct buf *bp, int base, int size)
3704 if (!(bp->b_flags & B_VMIO))
3708 * Fixup base to be relative to beginning of first page.
3709 * Set initial n to be the maximum number of bytes in the
3710 * first page that can be validated.
3712 base += (bp->b_offset & PAGE_MASK);
3713 n = PAGE_SIZE - (base & PAGE_MASK);
3715 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3716 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
3720 vm_page_set_valid(m, base & PAGE_MASK, n);
3725 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3731 * If the specified buffer is a non-VMIO buffer, clear the entire
3732 * buffer. If the specified buffer is a VMIO buffer, clear and
3733 * validate only the previously invalid portions of the buffer.
3734 * This routine essentially fakes an I/O, so we need to clear
3735 * BIO_ERROR and B_INVAL.
3737 * Note that while we only theoretically need to clear through b_bcount,
3738 * we go ahead and clear through b_bufsize.
3741 vfs_bio_clrbuf(struct buf *bp)
3746 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
3750 bp->b_flags &= ~B_INVAL;
3751 bp->b_ioflags &= ~BIO_ERROR;
3752 VM_OBJECT_LOCK(bp->b_bufobj->bo_object);
3753 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
3754 (bp->b_offset & PAGE_MASK) == 0) {
3755 if (bp->b_pages[0] == bogus_page)
3757 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
3758 VM_OBJECT_LOCK_ASSERT(bp->b_pages[0]->object, MA_OWNED);
3759 if ((bp->b_pages[0]->valid & mask) == mask)
3761 if ((bp->b_pages[0]->valid & mask) == 0) {
3762 bzero(bp->b_data, bp->b_bufsize);
3763 bp->b_pages[0]->valid |= mask;
3767 ea = sa = bp->b_data;
3768 for(i = 0; i < bp->b_npages; i++, sa = ea) {
3769 ea = (caddr_t)trunc_page((vm_offset_t)sa + PAGE_SIZE);
3770 ea = (caddr_t)(vm_offset_t)ulmin(
3771 (u_long)(vm_offset_t)ea,
3772 (u_long)(vm_offset_t)bp->b_data + bp->b_bufsize);
3773 if (bp->b_pages[i] == bogus_page)
3775 j = ((vm_offset_t)sa & PAGE_MASK) / DEV_BSIZE;
3776 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
3777 VM_OBJECT_LOCK_ASSERT(bp->b_pages[i]->object, MA_OWNED);
3778 if ((bp->b_pages[i]->valid & mask) == mask)
3780 if ((bp->b_pages[i]->valid & mask) == 0)
3783 for (; sa < ea; sa += DEV_BSIZE, j++) {
3784 if ((bp->b_pages[i]->valid & (1 << j)) == 0)
3785 bzero(sa, DEV_BSIZE);
3788 bp->b_pages[i]->valid |= mask;
3791 VM_OBJECT_UNLOCK(bp->b_bufobj->bo_object);
3796 * vm_hold_load_pages and vm_hold_free_pages get pages into
3797 * a buffers address space. The pages are anonymous and are
3798 * not associated with a file object.
3801 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
3807 to = round_page(to);
3808 from = round_page(from);
3809 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3811 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
3814 * note: must allocate system pages since blocking here
3815 * could interfere with paging I/O, no matter which
3818 p = vm_page_alloc(NULL, pg >> PAGE_SHIFT, VM_ALLOC_NOOBJ |
3819 VM_ALLOC_SYSTEM | VM_ALLOC_WIRED);
3821 atomic_add_int(&vm_pageout_deficit,
3822 (to - pg) >> PAGE_SHIFT);
3826 pmap_qenter(pg, &p, 1);
3827 bp->b_pages[index] = p;
3829 bp->b_npages = index;
3832 /* Return pages associated with this buf to the vm system */
3834 vm_hold_free_pages(struct buf *bp, int newbsize)
3838 int index, newnpages;
3840 from = round_page((vm_offset_t)bp->b_data + newbsize);
3841 newnpages = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
3842 if (bp->b_npages > newnpages)
3843 pmap_qremove(from, bp->b_npages - newnpages);
3844 for (index = newnpages; index < bp->b_npages; index++) {
3845 p = bp->b_pages[index];
3846 bp->b_pages[index] = NULL;
3848 printf("vm_hold_free_pages: blkno: %jd, lblkno: %jd\n",
3849 (intmax_t)bp->b_blkno, (intmax_t)bp->b_lblkno);
3852 atomic_subtract_int(&cnt.v_wire_count, 1);
3854 bp->b_npages = newnpages;
3858 * Map an IO request into kernel virtual address space.
3860 * All requests are (re)mapped into kernel VA space.
3861 * Notice that we use b_bufsize for the size of the buffer
3862 * to be mapped. b_bcount might be modified by the driver.
3864 * Note that even if the caller determines that the address space should
3865 * be valid, a race or a smaller-file mapped into a larger space may
3866 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
3867 * check the return value.
3870 vmapbuf(struct buf *bp)
3876 struct pmap *pmap = &curproc->p_vmspace->vm_pmap;
3878 if (bp->b_bufsize < 0)
3880 prot = VM_PROT_READ;
3881 if (bp->b_iocmd == BIO_READ)
3882 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
3883 for (addr = (caddr_t)trunc_page((vm_offset_t)bp->b_data), pidx = 0;
3884 addr < bp->b_data + bp->b_bufsize;
3885 addr += PAGE_SIZE, pidx++) {
3887 * Do the vm_fault if needed; do the copy-on-write thing
3888 * when reading stuff off device into memory.
3890 * NOTE! Must use pmap_extract() because addr may be in
3891 * the userland address space, and kextract is only guarenteed
3892 * to work for the kernland address space (see: sparc64 port).
3895 if (vm_fault_quick(addr >= bp->b_data ? addr : bp->b_data,
3897 vm_page_lock_queues();
3898 for (i = 0; i < pidx; ++i) {
3899 vm_page_unhold(bp->b_pages[i]);
3900 bp->b_pages[i] = NULL;
3902 vm_page_unlock_queues();
3905 m = pmap_extract_and_hold(pmap, (vm_offset_t)addr, prot);
3908 bp->b_pages[pidx] = m;
3910 if (pidx > btoc(MAXPHYS))
3911 panic("vmapbuf: mapped more than MAXPHYS");
3912 pmap_qenter((vm_offset_t)bp->b_saveaddr, bp->b_pages, pidx);
3914 kva = bp->b_saveaddr;
3915 bp->b_npages = pidx;
3916 bp->b_saveaddr = bp->b_data;
3917 bp->b_data = kva + (((vm_offset_t) bp->b_data) & PAGE_MASK);
3922 * Free the io map PTEs associated with this IO operation.
3923 * We also invalidate the TLB entries and restore the original b_addr.
3926 vunmapbuf(struct buf *bp)
3931 npages = bp->b_npages;
3932 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
3933 vm_page_lock_queues();
3934 for (pidx = 0; pidx < npages; pidx++)
3935 vm_page_unhold(bp->b_pages[pidx]);
3936 vm_page_unlock_queues();
3938 bp->b_data = bp->b_saveaddr;
3942 bdone(struct buf *bp)
3946 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3948 bp->b_flags |= B_DONE;
3954 bwait(struct buf *bp, u_char pri, const char *wchan)
3958 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3960 while ((bp->b_flags & B_DONE) == 0)
3961 msleep(bp, mtxp, pri, wchan, 0);
3966 bufsync(struct bufobj *bo, int waitfor)
3969 return (VOP_FSYNC(bo->__bo_vnode, waitfor, curthread));
3973 bufstrategy(struct bufobj *bo, struct buf *bp)
3979 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
3980 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
3981 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
3982 i = VOP_STRATEGY(vp, bp);
3983 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
3987 bufobj_wrefl(struct bufobj *bo)
3990 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
3991 ASSERT_BO_LOCKED(bo);
3996 bufobj_wref(struct bufobj *bo)
3999 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
4006 bufobj_wdrop(struct bufobj *bo)
4009 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
4011 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
4012 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
4013 bo->bo_flag &= ~BO_WWAIT;
4014 wakeup(&bo->bo_numoutput);
4020 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
4024 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
4025 ASSERT_BO_LOCKED(bo);
4027 while (bo->bo_numoutput) {
4028 bo->bo_flag |= BO_WWAIT;
4029 error = msleep(&bo->bo_numoutput, BO_MTX(bo),
4030 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
4038 bpin(struct buf *bp)
4042 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4049 bunpin(struct buf *bp)
4053 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4055 if (--bp->b_pin_count == 0)
4061 bunpin_wait(struct buf *bp)
4065 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4067 while (bp->b_pin_count > 0)
4068 msleep(bp, mtxp, PRIBIO, "bwunpin", 0);
4072 #include "opt_ddb.h"
4074 #include <ddb/ddb.h>
4076 /* DDB command to show buffer data */
4077 DB_SHOW_COMMAND(buffer, db_show_buffer)
4080 struct buf *bp = (struct buf *)addr;
4083 db_printf("usage: show buffer <addr>\n");
4087 db_printf("buf at %p\n", bp);
4088 db_printf("b_flags = 0x%b\n", (u_int)bp->b_flags, PRINT_BUF_FLAGS);
4090 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
4091 "b_bufobj = (%p), b_data = %p, b_blkno = %jd, b_dep = %p\n",
4092 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
4093 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno,
4094 bp->b_dep.lh_first);
4097 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
4098 for (i = 0; i < bp->b_npages; i++) {
4101 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
4102 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
4103 if ((i + 1) < bp->b_npages)
4109 BUF_LOCKPRINTINFO(bp);
4112 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
4117 for (i = 0; i < nbuf; i++) {
4119 if (BUF_ISLOCKED(bp)) {
4120 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4126 DB_SHOW_COMMAND(vnodebufs, db_show_vnodebufs)
4132 db_printf("usage: show vnodebufs <addr>\n");
4135 vp = (struct vnode *)addr;
4136 db_printf("Clean buffers:\n");
4137 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_clean.bv_hd, b_bobufs) {
4138 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4141 db_printf("Dirty buffers:\n");
4142 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
4143 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4148 DB_COMMAND(countfreebufs, db_coundfreebufs)
4151 int i, used = 0, nfree = 0;
4154 db_printf("usage: countfreebufs\n");
4158 for (i = 0; i < nbuf; i++) {
4160 if ((bp->b_vflags & BV_INFREECNT) != 0)
4166 db_printf("Counted %d free, %d used (%d tot)\n", nfree, used,
4168 db_printf("numfreebuffers is %d\n", numfreebuffers);