2 * Copyright (c) 2004 Poul-Henning Kamp
3 * Copyright (c) 1994,1997 John S. Dyson
4 * Copyright (c) 2013 The FreeBSD Foundation
7 * Portions of this software were developed by Konstantin Belousov
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * this file contains a new buffer I/O scheme implementing a coherent
34 * VM object and buffer cache scheme. Pains have been taken to make
35 * sure that the performance degradation associated with schemes such
36 * as this is not realized.
38 * Author: John S. Dyson
39 * Significant help during the development and debugging phases
40 * had been provided by David Greenman, also of the FreeBSD core team.
42 * see man buf(9) for more info.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
53 #include <sys/devicestat.h>
54 #include <sys/eventhandler.h>
56 #include <sys/limits.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/mutex.h>
61 #include <sys/kernel.h>
62 #include <sys/kthread.h>
64 #include <sys/resourcevar.h>
65 #include <sys/rwlock.h>
66 #include <sys/sysctl.h>
67 #include <sys/sysproto.h>
69 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
71 #include <sys/watchdog.h>
72 #include <geom/geom.h>
74 #include <vm/vm_param.h>
75 #include <vm/vm_kern.h>
76 #include <vm/vm_pageout.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_object.h>
79 #include <vm/vm_extern.h>
80 #include <vm/vm_map.h>
81 #include <vm/swap_pager.h>
82 #include "opt_compat.h"
85 static MALLOC_DEFINE(M_BIOBUF, "biobuf", "BIO buffer");
87 struct bio_ops bioops; /* I/O operation notification */
89 struct buf_ops buf_ops_bio = {
90 .bop_name = "buf_ops_bio",
91 .bop_write = bufwrite,
92 .bop_strategy = bufstrategy,
94 .bop_bdflush = bufbdflush,
97 static struct buf *buf; /* buffer header pool */
98 extern struct buf *swbuf; /* Swap buffer header pool. */
101 /* Used below and for softdep flushing threads in ufs/ffs/ffs_softdep.c */
102 struct proc *bufdaemonproc;
104 static int inmem(struct vnode *vp, daddr_t blkno);
105 static void vm_hold_free_pages(struct buf *bp, int newbsize);
106 static void vm_hold_load_pages(struct buf *bp, vm_offset_t from,
108 static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m);
109 static void vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off,
111 static void vfs_clean_pages_dirty_buf(struct buf *bp);
112 static void vfs_setdirty_locked_object(struct buf *bp);
113 static void vfs_vmio_invalidate(struct buf *bp);
114 static void vfs_vmio_release(struct buf *bp);
115 static void vfs_vmio_truncate(struct buf *bp, int npages);
116 static void vfs_vmio_extend(struct buf *bp, int npages, int size);
117 static int vfs_bio_clcheck(struct vnode *vp, int size,
118 daddr_t lblkno, daddr_t blkno);
119 static int buf_flush(struct vnode *vp, int);
120 static int flushbufqueues(struct vnode *, int, int);
121 static void buf_daemon(void);
122 static void bremfreel(struct buf *bp);
123 static __inline void bd_wakeup(void);
124 static int sysctl_runningspace(SYSCTL_HANDLER_ARGS);
125 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
126 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
127 static int sysctl_bufspace(SYSCTL_HANDLER_ARGS);
130 int vmiodirenable = TRUE;
131 SYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW, &vmiodirenable, 0,
132 "Use the VM system for directory writes");
133 long runningbufspace;
134 SYSCTL_LONG(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0,
135 "Amount of presently outstanding async buffer io");
136 static long bufspace;
137 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
138 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
139 SYSCTL_PROC(_vfs, OID_AUTO, bufspace, CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RD,
140 &bufspace, 0, sysctl_bufspace, "L", "Virtual memory used for buffers");
142 SYSCTL_LONG(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0,
143 "Physical memory used for buffers");
145 static long bufkvaspace;
146 SYSCTL_LONG(_vfs, OID_AUTO, bufkvaspace, CTLFLAG_RD, &bufkvaspace, 0,
147 "Kernel virtual memory used for buffers");
148 static long maxbufspace;
149 SYSCTL_LONG(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RD, &maxbufspace, 0,
150 "Maximum allowed value of bufspace (including buf_daemon)");
151 static long bufmallocspace;
152 SYSCTL_LONG(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0,
153 "Amount of malloced memory for buffers");
154 static long maxbufmallocspace;
155 SYSCTL_LONG(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace, 0,
156 "Maximum amount of malloced memory for buffers");
157 static long lobufspace;
158 SYSCTL_LONG(_vfs, OID_AUTO, lobufspace, CTLFLAG_RD, &lobufspace, 0,
159 "Minimum amount of buffers we want to have");
161 SYSCTL_LONG(_vfs, OID_AUTO, hibufspace, CTLFLAG_RD, &hibufspace, 0,
162 "Maximum allowed value of bufspace (excluding buf_daemon)");
163 static int bufreusecnt;
164 SYSCTL_INT(_vfs, OID_AUTO, bufreusecnt, CTLFLAG_RW, &bufreusecnt, 0,
165 "Number of times we have reused a buffer");
166 static int buffreekvacnt;
167 SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt, 0,
168 "Number of times we have freed the KVA space from some buffer");
169 static int bufdefragcnt;
170 SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt, 0,
171 "Number of times we have had to repeat buffer allocation to defragment");
172 static long lorunningspace;
173 SYSCTL_PROC(_vfs, OID_AUTO, lorunningspace, CTLTYPE_LONG | CTLFLAG_MPSAFE |
174 CTLFLAG_RW, &lorunningspace, 0, sysctl_runningspace, "L",
175 "Minimum preferred space used for in-progress I/O");
176 static long hirunningspace;
177 SYSCTL_PROC(_vfs, OID_AUTO, hirunningspace, CTLTYPE_LONG | CTLFLAG_MPSAFE |
178 CTLFLAG_RW, &hirunningspace, 0, sysctl_runningspace, "L",
179 "Maximum amount of space to use for in-progress I/O");
180 int dirtybufferflushes;
181 SYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes,
182 0, "Number of bdwrite to bawrite conversions to limit dirty buffers");
184 SYSCTL_INT(_vfs, OID_AUTO, bdwriteskip, CTLFLAG_RW, &bdwriteskip,
185 0, "Number of buffers supplied to bdwrite with snapshot deadlock risk");
186 int altbufferflushes;
187 SYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes,
188 0, "Number of fsync flushes to limit dirty buffers");
189 static int recursiveflushes;
190 SYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes,
191 0, "Number of flushes skipped due to being recursive");
192 static int numdirtybuffers;
193 SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0,
194 "Number of buffers that are dirty (has unwritten changes) at the moment");
195 static int lodirtybuffers;
196 SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0,
197 "How many buffers we want to have free before bufdaemon can sleep");
198 static int hidirtybuffers;
199 SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0,
200 "When the number of dirty buffers is considered severe");
202 SYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh,
203 0, "Number of bdwrite to bawrite conversions to clear dirty buffers");
204 static int numfreebuffers;
205 SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0,
206 "Number of free buffers");
207 static int lofreebuffers;
208 SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0,
210 static int hifreebuffers;
211 SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0,
212 "XXX Complicatedly unused");
213 static int getnewbufcalls;
214 SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW, &getnewbufcalls, 0,
215 "Number of calls to getnewbuf");
216 static int getnewbufrestarts;
217 SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW, &getnewbufrestarts, 0,
218 "Number of times getnewbuf has had to restart a buffer aquisition");
219 static int mappingrestarts;
220 SYSCTL_INT(_vfs, OID_AUTO, mappingrestarts, CTLFLAG_RW, &mappingrestarts, 0,
221 "Number of times getblk has had to restart a buffer mapping for "
223 static int flushbufqtarget = 100;
224 SYSCTL_INT(_vfs, OID_AUTO, flushbufqtarget, CTLFLAG_RW, &flushbufqtarget, 0,
225 "Amount of work to do in flushbufqueues when helping bufdaemon");
226 static long notbufdflushes;
227 SYSCTL_LONG(_vfs, OID_AUTO, notbufdflushes, CTLFLAG_RD, ¬bufdflushes, 0,
228 "Number of dirty buffer flushes done by the bufdaemon helpers");
229 static long barrierwrites;
230 SYSCTL_LONG(_vfs, OID_AUTO, barrierwrites, CTLFLAG_RW, &barrierwrites, 0,
231 "Number of barrier writes");
232 SYSCTL_INT(_vfs, OID_AUTO, unmapped_buf_allowed, CTLFLAG_RD,
233 &unmapped_buf_allowed, 0,
234 "Permit the use of the unmapped i/o");
237 * Lock for the non-dirty bufqueues
239 static struct mtx_padalign bqclean;
242 * Lock for the dirty queue.
244 static struct mtx_padalign bqdirty;
247 * This lock synchronizes access to bd_request.
249 static struct mtx_padalign bdlock;
252 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
253 * waitrunningbufspace().
255 static struct mtx_padalign rbreqlock;
258 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it.
260 static struct rwlock_padalign nblock;
263 * Lock that protects bdirtywait.
265 static struct mtx_padalign bdirtylock;
268 * Wakeup point for bufdaemon, as well as indicator of whether it is already
269 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it
272 static int bd_request;
275 * Request for the buf daemon to write more buffers than is indicated by
276 * lodirtybuf. This may be necessary to push out excess dependencies or
277 * defragment the address space where a simple count of the number of dirty
278 * buffers is insufficient to characterize the demand for flushing them.
280 static int bd_speedupreq;
283 * bogus page -- for I/O to/from partially complete buffers
284 * this is a temporary solution to the problem, but it is not
285 * really that bad. it would be better to split the buffer
286 * for input in the case of buffers partially already in memory,
287 * but the code is intricate enough already.
289 vm_page_t bogus_page;
292 * Synchronization (sleep/wakeup) variable for active buffer space requests.
293 * Set when wait starts, cleared prior to wakeup().
294 * Used in runningbufwakeup() and waitrunningbufspace().
296 static int runningbufreq;
299 * Synchronization (sleep/wakeup) variable for buffer requests.
300 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done
302 * Used in numdirtywakeup(), bufspacewakeup(), bufcountadd(), bwillwrite(),
303 * getnewbuf(), and getblk().
305 static volatile int needsbuffer;
308 * Synchronization for bwillwrite() waiters.
310 static int bdirtywait;
313 * Definitions for the buffer free lists.
315 #define BUFFER_QUEUES 4 /* number of free buffer queues */
317 #define QUEUE_NONE 0 /* on no queue */
318 #define QUEUE_CLEAN 1 /* non-B_DELWRI buffers */
319 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
320 #define QUEUE_EMPTY 3 /* empty buffer headers */
321 #define QUEUE_SENTINEL 1024 /* not an queue index, but mark for sentinel */
323 /* Queues for free buffers with various properties */
324 static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } };
326 static int bq_len[BUFFER_QUEUES];
330 * Single global constant for BUF_WMESG, to avoid getting multiple references.
331 * buf_wmesg is referred from macros.
333 const char *buf_wmesg = BUF_WMESG;
335 #define VFS_BIO_NEED_ANY 0x01 /* any freeable buffer */
336 #define VFS_BIO_NEED_FREE 0x04 /* wait for free bufs, hi hysteresis */
337 #define VFS_BIO_NEED_BUFSPACE 0x08 /* wait for buf space, lo hysteresis */
340 sysctl_runningspace(SYSCTL_HANDLER_ARGS)
345 value = *(long *)arg1;
346 error = sysctl_handle_long(oidp, &value, 0, req);
347 if (error != 0 || req->newptr == NULL)
349 mtx_lock(&rbreqlock);
350 if (arg1 == &hirunningspace) {
351 if (value < lorunningspace)
354 hirunningspace = value;
356 KASSERT(arg1 == &lorunningspace,
357 ("%s: unknown arg1", __func__));
358 if (value > hirunningspace)
361 lorunningspace = value;
363 mtx_unlock(&rbreqlock);
367 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
368 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
370 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
375 if (sizeof(int) == sizeof(long) || req->oldlen >= sizeof(long))
376 return (sysctl_handle_long(oidp, arg1, arg2, req));
377 lvalue = *(long *)arg1;
378 if (lvalue > INT_MAX)
379 /* On overflow, still write out a long to trigger ENOMEM. */
380 return (sysctl_handle_long(oidp, &lvalue, 0, req));
382 return (sysctl_handle_int(oidp, &ivalue, 0, req));
389 * Return the appropriate queue lock based on the index.
391 static inline struct mtx *
395 if (qindex == QUEUE_DIRTY)
396 return (struct mtx *)(&bqdirty);
397 return (struct mtx *)(&bqclean);
403 * Wakeup any bwillwrite() waiters.
408 mtx_lock(&bdirtylock);
413 mtx_unlock(&bdirtylock);
419 * Decrement the numdirtybuffers count by one and wakeup any
420 * threads blocked in bwillwrite().
426 if (atomic_fetchadd_int(&numdirtybuffers, -1) ==
427 (lodirtybuffers + hidirtybuffers) / 2)
434 * Increment the numdirtybuffers count by one and wakeup the buf
442 * Only do the wakeup once as we cross the boundary. The
443 * buf daemon will keep running until the condition clears.
445 if (atomic_fetchadd_int(&numdirtybuffers, 1) ==
446 (lodirtybuffers + hidirtybuffers) / 2)
453 * Called when buffer space is potentially available for recovery.
454 * getnewbuf() will block on this flag when it is unable to free
455 * sufficient buffer space. Buffer space becomes recoverable when
456 * bp's get placed back in the queues.
464 * If someone is waiting for bufspace, wake them up. Even
465 * though we may not have freed the kva space yet, the waiting
466 * process will be able to now.
472 if ((on & VFS_BIO_NEED_BUFSPACE) == 0)
475 if (atomic_cmpset_rel_int(&needsbuffer, on,
476 on & ~VFS_BIO_NEED_BUFSPACE))
480 wakeup(__DEVOLATILE(void *, &needsbuffer));
487 * Adjust the reported bufspace for a KVA managed buffer, possibly
488 * waking any waiters.
491 bufspaceadjust(struct buf *bp, int bufsize)
495 KASSERT((bp->b_flags & B_MALLOC) == 0,
496 ("bufspaceadjust: malloc buf %p", bp));
497 diff = bufsize - bp->b_bufsize;
499 atomic_subtract_long(&bufspace, -diff);
502 atomic_add_long(&bufspace, diff);
503 bp->b_bufsize = bufsize;
509 * Adjust the reported bufspace for a malloc managed buffer, possibly
510 * waking any waiters.
513 bufmallocadjust(struct buf *bp, int bufsize)
517 KASSERT((bp->b_flags & B_MALLOC) != 0,
518 ("bufmallocadjust: non-malloc buf %p", bp));
519 diff = bufsize - bp->b_bufsize;
521 atomic_subtract_long(&bufmallocspace, -diff);
524 atomic_add_long(&bufmallocspace, diff);
525 bp->b_bufsize = bufsize;
531 * Wake up processes that are waiting on asynchronous writes to fall
532 * below lorunningspace.
538 mtx_lock(&rbreqlock);
541 wakeup(&runningbufreq);
543 mtx_unlock(&rbreqlock);
549 * Decrement the outstanding write count according.
552 runningbufwakeup(struct buf *bp)
556 bspace = bp->b_runningbufspace;
559 space = atomic_fetchadd_long(&runningbufspace, -bspace);
560 KASSERT(space >= bspace, ("runningbufspace underflow %ld %ld",
562 bp->b_runningbufspace = 0;
564 * Only acquire the lock and wakeup on the transition from exceeding
565 * the threshold to falling below it.
567 if (space < lorunningspace)
569 if (space - bspace > lorunningspace)
577 * Called when a buffer has been added to one of the free queues to
578 * account for the buffer and to wakeup anyone waiting for free buffers.
579 * This typically occurs when large amounts of metadata are being handled
580 * by the buffer cache ( else buffer space runs out first, usually ).
583 bufcountadd(struct buf *bp)
585 int mask, need_wakeup, old, on;
587 KASSERT((bp->b_flags & B_INFREECNT) == 0,
588 ("buf %p already counted as free", bp));
589 bp->b_flags |= B_INFREECNT;
590 old = atomic_fetchadd_int(&numfreebuffers, 1);
591 KASSERT(old >= 0 && old < nbuf,
592 ("numfreebuffers climbed to %d", old + 1));
593 mask = VFS_BIO_NEED_ANY;
594 if (numfreebuffers >= hifreebuffers)
595 mask |= VFS_BIO_NEED_FREE;
603 if (atomic_cmpset_rel_int(&needsbuffer, on, on & ~mask))
607 wakeup(__DEVOLATILE(void *, &needsbuffer));
614 * Decrement the numfreebuffers count as needed.
617 bufcountsub(struct buf *bp)
622 * Fixup numfreebuffers count. If the buffer is invalid or not
623 * delayed-write, the buffer was free and we must decrement
626 if ((bp->b_flags & B_INVAL) || (bp->b_flags & B_DELWRI) == 0) {
627 KASSERT((bp->b_flags & B_INFREECNT) != 0,
628 ("buf %p not counted in numfreebuffers", bp));
629 bp->b_flags &= ~B_INFREECNT;
630 old = atomic_fetchadd_int(&numfreebuffers, -1);
631 KASSERT(old > 0, ("numfreebuffers dropped to %d", old - 1));
636 * waitrunningbufspace()
638 * runningbufspace is a measure of the amount of I/O currently
639 * running. This routine is used in async-write situations to
640 * prevent creating huge backups of pending writes to a device.
641 * Only asynchronous writes are governed by this function.
643 * This does NOT turn an async write into a sync write. It waits
644 * for earlier writes to complete and generally returns before the
645 * caller's write has reached the device.
648 waitrunningbufspace(void)
651 mtx_lock(&rbreqlock);
652 while (runningbufspace > hirunningspace) {
654 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
656 mtx_unlock(&rbreqlock);
661 * vfs_buf_test_cache:
663 * Called when a buffer is extended. This function clears the B_CACHE
664 * bit if the newly extended portion of the buffer does not contain
668 vfs_buf_test_cache(struct buf *bp, vm_ooffset_t foff, vm_offset_t off,
669 vm_offset_t size, vm_page_t m)
672 VM_OBJECT_ASSERT_LOCKED(m->object);
673 if (bp->b_flags & B_CACHE) {
674 int base = (foff + off) & PAGE_MASK;
675 if (vm_page_is_valid(m, base, size) == 0)
676 bp->b_flags &= ~B_CACHE;
680 /* Wake up the buffer daemon if necessary */
686 if (bd_request == 0) {
694 * bd_speedup - speedup the buffer cache flushing code
703 if (bd_speedupreq == 0 || bd_request == 0)
713 #define NSWBUF_MIN 16
717 #define TRANSIENT_DENOM 5
719 #define TRANSIENT_DENOM 10
723 * Calculating buffer cache scaling values and reserve space for buffer
724 * headers. This is called during low level kernel initialization and
725 * may be called more then once. We CANNOT write to the memory area
726 * being reserved at this time.
729 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
732 long maxbuf, maxbuf_sz, buf_sz, biotmap_sz;
735 * physmem_est is in pages. Convert it to kilobytes (assumes
736 * PAGE_SIZE is >= 1K)
738 physmem_est = physmem_est * (PAGE_SIZE / 1024);
741 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
742 * For the first 64MB of ram nominally allocate sufficient buffers to
743 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
744 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing
745 * the buffer cache we limit the eventual kva reservation to
748 * factor represents the 1/4 x ram conversion.
751 int factor = 4 * BKVASIZE / 1024;
754 if (physmem_est > 4096)
755 nbuf += min((physmem_est - 4096) / factor,
757 if (physmem_est > 65536)
758 nbuf += min((physmem_est - 65536) * 2 / (factor * 5),
759 32 * 1024 * 1024 / (factor * 5));
761 if (maxbcache && nbuf > maxbcache / BKVASIZE)
762 nbuf = maxbcache / BKVASIZE;
767 /* XXX Avoid unsigned long overflows later on with maxbufspace. */
768 maxbuf = (LONG_MAX / 3) / BKVASIZE;
771 printf("Warning: nbufs lowered from %d to %ld\n", nbuf,
777 * Ideal allocation size for the transient bio submap is 10%
778 * of the maximal space buffer map. This roughly corresponds
779 * to the amount of the buffer mapped for typical UFS load.
781 * Clip the buffer map to reserve space for the transient
782 * BIOs, if its extent is bigger than 90% (80% on i386) of the
783 * maximum buffer map extent on the platform.
785 * The fall-back to the maxbuf in case of maxbcache unset,
786 * allows to not trim the buffer KVA for the architectures
787 * with ample KVA space.
789 if (bio_transient_maxcnt == 0 && unmapped_buf_allowed) {
790 maxbuf_sz = maxbcache != 0 ? maxbcache : maxbuf * BKVASIZE;
791 buf_sz = (long)nbuf * BKVASIZE;
792 if (buf_sz < maxbuf_sz / TRANSIENT_DENOM *
793 (TRANSIENT_DENOM - 1)) {
795 * There is more KVA than memory. Do not
796 * adjust buffer map size, and assign the rest
797 * of maxbuf to transient map.
799 biotmap_sz = maxbuf_sz - buf_sz;
802 * Buffer map spans all KVA we could afford on
803 * this platform. Give 10% (20% on i386) of
804 * the buffer map to the transient bio map.
806 biotmap_sz = buf_sz / TRANSIENT_DENOM;
807 buf_sz -= biotmap_sz;
809 if (biotmap_sz / INT_MAX > MAXPHYS)
810 bio_transient_maxcnt = INT_MAX;
812 bio_transient_maxcnt = biotmap_sz / MAXPHYS;
814 * Artifically limit to 1024 simultaneous in-flight I/Os
815 * using the transient mapping.
817 if (bio_transient_maxcnt > 1024)
818 bio_transient_maxcnt = 1024;
820 nbuf = buf_sz / BKVASIZE;
824 * swbufs are used as temporary holders for I/O, such as paging I/O.
825 * We have no less then 16 and no more then 256.
827 nswbuf = min(nbuf / 4, 256);
828 TUNABLE_INT_FETCH("kern.nswbuf", &nswbuf);
829 if (nswbuf < NSWBUF_MIN)
833 * Reserve space for the buffer cache buffers
836 v = (caddr_t)(swbuf + nswbuf);
838 v = (caddr_t)(buf + nbuf);
843 /* Initialize the buffer subsystem. Called before use of any buffers. */
850 CTASSERT(MAXBCACHEBUF >= MAXBSIZE);
851 mtx_init(&bqclean, "bufq clean lock", NULL, MTX_DEF);
852 mtx_init(&bqdirty, "bufq dirty lock", NULL, MTX_DEF);
853 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
854 rw_init(&nblock, "needsbuffer lock");
855 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
856 mtx_init(&bdirtylock, "dirty buf lock", NULL, MTX_DEF);
858 /* next, make a null set of free lists */
859 for (i = 0; i < BUFFER_QUEUES; i++)
860 TAILQ_INIT(&bufqueues[i]);
862 unmapped_buf = (caddr_t)kva_alloc(MAXPHYS);
864 /* finally, initialize each buffer header and stick on empty q */
865 for (i = 0; i < nbuf; i++) {
867 bzero(bp, sizeof *bp);
868 bp->b_flags = B_INVAL | B_INFREECNT;
869 bp->b_rcred = NOCRED;
870 bp->b_wcred = NOCRED;
871 bp->b_qindex = QUEUE_EMPTY;
873 bp->b_data = bp->b_kvabase = unmapped_buf;
874 LIST_INIT(&bp->b_dep);
876 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
878 bq_len[QUEUE_EMPTY]++;
883 * maxbufspace is the absolute maximum amount of buffer space we are
884 * allowed to reserve in KVM and in real terms. The absolute maximum
885 * is nominally used by buf_daemon. hibufspace is the nominal maximum
886 * used by most other processes. The differential is required to
887 * ensure that buf_daemon is able to run when other processes might
888 * be blocked waiting for buffer space.
890 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
891 * this may result in KVM fragmentation which is not handled optimally
894 maxbufspace = (long)nbuf * BKVASIZE;
895 hibufspace = lmax(3 * maxbufspace / 4, maxbufspace - MAXBCACHEBUF * 10);
896 lobufspace = hibufspace - MAXBCACHEBUF;
899 * Note: The 16 MiB upper limit for hirunningspace was chosen
900 * arbitrarily and may need further tuning. It corresponds to
901 * 128 outstanding write IO requests (if IO size is 128 KiB),
902 * which fits with many RAID controllers' tagged queuing limits.
903 * The lower 1 MiB limit is the historical upper limit for
906 hirunningspace = lmax(lmin(roundup(hibufspace / 64, MAXBCACHEBUF),
907 16 * 1024 * 1024), 1024 * 1024);
908 lorunningspace = roundup((hirunningspace * 2) / 3, MAXBCACHEBUF);
911 * Limit the amount of malloc memory since it is wired permanently into
912 * the kernel space. Even though this is accounted for in the buffer
913 * allocation, we don't want the malloced region to grow uncontrolled.
914 * The malloc scheme improves memory utilization significantly on average
915 * (small) directories.
917 maxbufmallocspace = hibufspace / 20;
920 * Reduce the chance of a deadlock occuring by limiting the number
921 * of delayed-write dirty buffers we allow to stack up.
923 hidirtybuffers = nbuf / 4 + 20;
924 dirtybufthresh = hidirtybuffers * 9 / 10;
927 * To support extreme low-memory systems, make sure hidirtybuffers cannot
928 * eat up all available buffer space. This occurs when our minimum cannot
929 * be met. We try to size hidirtybuffers to 3/4 our buffer space assuming
930 * BKVASIZE'd buffers.
932 while ((long)hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
933 hidirtybuffers >>= 1;
935 lodirtybuffers = hidirtybuffers / 2;
938 * Try to keep the number of free buffers in the specified range,
939 * and give special processes (e.g. like buf_daemon) access to an
942 lofreebuffers = nbuf / 18 + 5;
943 hifreebuffers = 2 * lofreebuffers;
944 numfreebuffers = nbuf;
946 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
947 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
952 vfs_buf_check_mapped(struct buf *bp)
955 KASSERT(bp->b_kvabase != unmapped_buf,
956 ("mapped buf: b_kvabase was not updated %p", bp));
957 KASSERT(bp->b_data != unmapped_buf,
958 ("mapped buf: b_data was not updated %p", bp));
959 KASSERT(bp->b_data < unmapped_buf || bp->b_data >= unmapped_buf +
960 MAXPHYS, ("b_data + b_offset unmapped %p", bp));
964 vfs_buf_check_unmapped(struct buf *bp)
967 KASSERT(bp->b_data == unmapped_buf,
968 ("unmapped buf: corrupted b_data %p", bp));
971 #define BUF_CHECK_MAPPED(bp) vfs_buf_check_mapped(bp)
972 #define BUF_CHECK_UNMAPPED(bp) vfs_buf_check_unmapped(bp)
974 #define BUF_CHECK_MAPPED(bp) do {} while (0)
975 #define BUF_CHECK_UNMAPPED(bp) do {} while (0)
979 isbufbusy(struct buf *bp)
981 if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
983 ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
989 * Shutdown the system cleanly to prepare for reboot, halt, or power off.
992 bufshutdown(int show_busybufs)
994 static int first_buf_printf = 1;
996 int iter, nbusy, pbusy;
1002 * Sync filesystems for shutdown
1004 wdog_kern_pat(WD_LASTVAL);
1005 sys_sync(curthread, NULL);
1008 * With soft updates, some buffers that are
1009 * written will be remarked as dirty until other
1010 * buffers are written.
1012 for (iter = pbusy = 0; iter < 20; iter++) {
1014 for (bp = &buf[nbuf]; --bp >= buf; )
1018 if (first_buf_printf)
1019 printf("All buffers synced.");
1022 if (first_buf_printf) {
1023 printf("Syncing disks, buffers remaining... ");
1024 first_buf_printf = 0;
1026 printf("%d ", nbusy);
1031 wdog_kern_pat(WD_LASTVAL);
1032 sys_sync(curthread, NULL);
1036 * Drop Giant and spin for a while to allow
1037 * interrupt threads to run.
1040 DELAY(50000 * iter);
1044 * Drop Giant and context switch several times to
1045 * allow interrupt threads to run.
1048 for (subiter = 0; subiter < 50 * iter; subiter++) {
1049 thread_lock(curthread);
1050 mi_switch(SW_VOL, NULL);
1051 thread_unlock(curthread);
1059 * Count only busy local buffers to prevent forcing
1060 * a fsck if we're just a client of a wedged NFS server
1063 for (bp = &buf[nbuf]; --bp >= buf; ) {
1064 if (isbufbusy(bp)) {
1066 /* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */
1067 if (bp->b_dev == NULL) {
1068 TAILQ_REMOVE(&mountlist,
1069 bp->b_vp->v_mount, mnt_list);
1074 if (show_busybufs > 0) {
1076 "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
1077 nbusy, bp, bp->b_vp, bp->b_flags,
1078 (intmax_t)bp->b_blkno,
1079 (intmax_t)bp->b_lblkno);
1080 BUF_LOCKPRINTINFO(bp);
1081 if (show_busybufs > 1)
1089 * Failed to sync all blocks. Indicate this and don't
1090 * unmount filesystems (thus forcing an fsck on reboot).
1092 printf("Giving up on %d buffers\n", nbusy);
1093 DELAY(5000000); /* 5 seconds */
1095 if (!first_buf_printf)
1096 printf("Final sync complete\n");
1098 * Unmount filesystems
1104 DELAY(100000); /* wait for console output to finish */
1108 bpmap_qenter(struct buf *bp)
1111 BUF_CHECK_MAPPED(bp);
1114 * bp->b_data is relative to bp->b_offset, but
1115 * bp->b_offset may be offset into the first page.
1117 bp->b_data = (caddr_t)trunc_page((vm_offset_t)bp->b_data);
1118 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1119 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
1120 (vm_offset_t)(bp->b_offset & PAGE_MASK));
1126 * Insert the buffer into the appropriate free list.
1129 binsfree(struct buf *bp, int qindex)
1131 struct mtx *olock, *nlock;
1133 BUF_ASSERT_XLOCKED(bp);
1135 nlock = bqlock(qindex);
1136 /* Handle delayed bremfree() processing. */
1137 if (bp->b_flags & B_REMFREE) {
1138 olock = bqlock(bp->b_qindex);
1141 if (olock != nlock) {
1148 if (bp->b_qindex != QUEUE_NONE)
1149 panic("binsfree: free buffer onto another queue???");
1151 bp->b_qindex = qindex;
1152 if (bp->b_flags & B_AGE)
1153 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1155 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1157 bq_len[bp->b_qindex]++;
1162 * Something we can maybe free or reuse.
1164 if (bp->b_bufsize && !(bp->b_flags & B_DELWRI))
1167 if ((bp->b_flags & B_INVAL) || !(bp->b_flags & B_DELWRI))
1174 * Mark the buffer for removal from the appropriate free list.
1178 bremfree(struct buf *bp)
1181 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1182 KASSERT((bp->b_flags & B_REMFREE) == 0,
1183 ("bremfree: buffer %p already marked for delayed removal.", bp));
1184 KASSERT(bp->b_qindex != QUEUE_NONE,
1185 ("bremfree: buffer %p not on a queue.", bp));
1186 BUF_ASSERT_XLOCKED(bp);
1188 bp->b_flags |= B_REMFREE;
1195 * Force an immediate removal from a free list. Used only in nfs when
1196 * it abuses the b_freelist pointer.
1199 bremfreef(struct buf *bp)
1203 qlock = bqlock(bp->b_qindex);
1212 * Removes a buffer from the free list, must be called with the
1213 * correct qlock held.
1216 bremfreel(struct buf *bp)
1219 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X",
1220 bp, bp->b_vp, bp->b_flags);
1221 KASSERT(bp->b_qindex != QUEUE_NONE,
1222 ("bremfreel: buffer %p not on a queue.", bp));
1223 BUF_ASSERT_XLOCKED(bp);
1224 mtx_assert(bqlock(bp->b_qindex), MA_OWNED);
1226 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
1228 KASSERT(bq_len[bp->b_qindex] >= 1, ("queue %d underflow",
1230 bq_len[bp->b_qindex]--;
1232 bp->b_qindex = QUEUE_NONE;
1234 * If this was a delayed bremfree() we only need to remove the buffer
1235 * from the queue and return the stats are already done.
1237 if (bp->b_flags & B_REMFREE) {
1238 bp->b_flags &= ~B_REMFREE;
1247 * Free the kva allocation for a buffer.
1251 bufkvafree(struct buf *bp)
1255 if (bp->b_kvasize == 0) {
1256 KASSERT(bp->b_kvabase == unmapped_buf &&
1257 bp->b_data == unmapped_buf,
1258 ("Leaked KVA space on %p", bp));
1259 } else if (buf_mapped(bp))
1260 BUF_CHECK_MAPPED(bp);
1262 BUF_CHECK_UNMAPPED(bp);
1264 if (bp->b_kvasize == 0)
1267 vmem_free(buffer_arena, (vm_offset_t)bp->b_kvabase, bp->b_kvasize);
1268 atomic_subtract_long(&bufkvaspace, bp->b_kvasize);
1269 atomic_add_int(&buffreekvacnt, 1);
1270 bp->b_data = bp->b_kvabase = unmapped_buf;
1277 * Allocate the buffer KVA and set b_kvasize and b_kvabase.
1280 bufkvaalloc(struct buf *bp, int maxsize, int gbflags)
1285 KASSERT((gbflags & GB_UNMAPPED) == 0 || (gbflags & GB_KVAALLOC) != 0,
1286 ("Invalid gbflags 0x%x in %s", gbflags, __func__));
1291 error = vmem_alloc(buffer_arena, maxsize, M_BESTFIT | M_NOWAIT, &addr);
1294 * Buffer map is too fragmented. Request the caller
1295 * to defragment the map.
1297 atomic_add_int(&bufdefragcnt, 1);
1300 bp->b_kvabase = (caddr_t)addr;
1301 bp->b_kvasize = maxsize;
1302 atomic_add_long(&bufkvaspace, bp->b_kvasize);
1303 if ((gbflags & GB_UNMAPPED) != 0) {
1304 bp->b_data = unmapped_buf;
1305 BUF_CHECK_UNMAPPED(bp);
1307 bp->b_data = bp->b_kvabase;
1308 BUF_CHECK_MAPPED(bp);
1314 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must
1315 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set,
1316 * the buffer is valid and we do not have to do anything.
1319 breada(struct vnode * vp, daddr_t * rablkno, int * rabsize,
1320 int cnt, struct ucred * cred)
1325 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
1326 if (inmem(vp, *rablkno))
1328 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
1330 if ((rabp->b_flags & B_CACHE) == 0) {
1331 if (!TD_IS_IDLETHREAD(curthread))
1332 curthread->td_ru.ru_inblock++;
1333 rabp->b_flags |= B_ASYNC;
1334 rabp->b_flags &= ~B_INVAL;
1335 rabp->b_ioflags &= ~BIO_ERROR;
1336 rabp->b_iocmd = BIO_READ;
1337 if (rabp->b_rcred == NOCRED && cred != NOCRED)
1338 rabp->b_rcred = crhold(cred);
1339 vfs_busy_pages(rabp, 0);
1341 rabp->b_iooffset = dbtob(rabp->b_blkno);
1350 * Entry point for bread() and breadn() via #defines in sys/buf.h.
1352 * Get a buffer with the specified data. Look in the cache first. We
1353 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
1354 * is set, the buffer is valid and we do not have to do anything, see
1355 * getblk(). Also starts asynchronous I/O on read-ahead blocks.
1358 breadn_flags(struct vnode *vp, daddr_t blkno, int size, daddr_t *rablkno,
1359 int *rabsize, int cnt, struct ucred *cred, int flags, struct buf **bpp)
1362 int rv = 0, readwait = 0;
1364 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
1366 * Can only return NULL if GB_LOCK_NOWAIT flag is specified.
1368 *bpp = bp = getblk(vp, blkno, size, 0, 0, flags);
1372 /* if not found in cache, do some I/O */
1373 if ((bp->b_flags & B_CACHE) == 0) {
1374 if (!TD_IS_IDLETHREAD(curthread))
1375 curthread->td_ru.ru_inblock++;
1376 bp->b_iocmd = BIO_READ;
1377 bp->b_flags &= ~B_INVAL;
1378 bp->b_ioflags &= ~BIO_ERROR;
1379 if (bp->b_rcred == NOCRED && cred != NOCRED)
1380 bp->b_rcred = crhold(cred);
1381 vfs_busy_pages(bp, 0);
1382 bp->b_iooffset = dbtob(bp->b_blkno);
1387 breada(vp, rablkno, rabsize, cnt, cred);
1396 * Write, release buffer on completion. (Done by iodone
1397 * if async). Do not bother writing anything if the buffer
1400 * Note that we set B_CACHE here, indicating that buffer is
1401 * fully valid and thus cacheable. This is true even of NFS
1402 * now so we set it generally. This could be set either here
1403 * or in biodone() since the I/O is synchronous. We put it
1407 bufwrite(struct buf *bp)
1414 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1415 if ((bp->b_bufobj->bo_flag & BO_DEAD) != 0) {
1416 bp->b_flags |= B_INVAL | B_RELBUF;
1417 bp->b_flags &= ~B_CACHE;
1421 if (bp->b_flags & B_INVAL) {
1426 if (bp->b_flags & B_BARRIER)
1429 oldflags = bp->b_flags;
1431 BUF_ASSERT_HELD(bp);
1433 if (bp->b_pin_count > 0)
1436 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
1437 ("FFS background buffer should not get here %p", bp));
1441 vp_md = vp->v_vflag & VV_MD;
1446 * Mark the buffer clean. Increment the bufobj write count
1447 * before bundirty() call, to prevent other thread from seeing
1448 * empty dirty list and zero counter for writes in progress,
1449 * falsely indicating that the bufobj is clean.
1451 bufobj_wref(bp->b_bufobj);
1454 bp->b_flags &= ~B_DONE;
1455 bp->b_ioflags &= ~BIO_ERROR;
1456 bp->b_flags |= B_CACHE;
1457 bp->b_iocmd = BIO_WRITE;
1459 vfs_busy_pages(bp, 1);
1462 * Normal bwrites pipeline writes
1464 bp->b_runningbufspace = bp->b_bufsize;
1465 space = atomic_fetchadd_long(&runningbufspace, bp->b_runningbufspace);
1467 if (!TD_IS_IDLETHREAD(curthread))
1468 curthread->td_ru.ru_oublock++;
1469 if (oldflags & B_ASYNC)
1471 bp->b_iooffset = dbtob(bp->b_blkno);
1474 if ((oldflags & B_ASYNC) == 0) {
1475 int rtval = bufwait(bp);
1478 } else if (space > hirunningspace) {
1480 * don't allow the async write to saturate the I/O
1481 * system. We will not deadlock here because
1482 * we are blocking waiting for I/O that is already in-progress
1483 * to complete. We do not block here if it is the update
1484 * or syncer daemon trying to clean up as that can lead
1487 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md)
1488 waitrunningbufspace();
1495 bufbdflush(struct bufobj *bo, struct buf *bp)
1499 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
1500 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread);
1502 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
1505 * Try to find a buffer to flush.
1507 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
1508 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
1510 LK_EXCLUSIVE | LK_NOWAIT, NULL))
1513 panic("bdwrite: found ourselves");
1515 /* Don't countdeps with the bo lock held. */
1516 if (buf_countdeps(nbp, 0)) {
1521 if (nbp->b_flags & B_CLUSTEROK) {
1522 vfs_bio_awrite(nbp);
1527 dirtybufferflushes++;
1536 * Delayed write. (Buffer is marked dirty). Do not bother writing
1537 * anything if the buffer is marked invalid.
1539 * Note that since the buffer must be completely valid, we can safely
1540 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
1541 * biodone() in order to prevent getblk from writing the buffer
1542 * out synchronously.
1545 bdwrite(struct buf *bp)
1547 struct thread *td = curthread;
1551 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1552 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1553 KASSERT((bp->b_flags & B_BARRIER) == 0,
1554 ("Barrier request in delayed write %p", bp));
1555 BUF_ASSERT_HELD(bp);
1557 if (bp->b_flags & B_INVAL) {
1563 * If we have too many dirty buffers, don't create any more.
1564 * If we are wildly over our limit, then force a complete
1565 * cleanup. Otherwise, just keep the situation from getting
1566 * out of control. Note that we have to avoid a recursive
1567 * disaster and not try to clean up after our own cleanup!
1571 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) {
1572 td->td_pflags |= TDP_INBDFLUSH;
1574 td->td_pflags &= ~TDP_INBDFLUSH;
1580 * Set B_CACHE, indicating that the buffer is fully valid. This is
1581 * true even of NFS now.
1583 bp->b_flags |= B_CACHE;
1586 * This bmap keeps the system from needing to do the bmap later,
1587 * perhaps when the system is attempting to do a sync. Since it
1588 * is likely that the indirect block -- or whatever other datastructure
1589 * that the filesystem needs is still in memory now, it is a good
1590 * thing to do this. Note also, that if the pageout daemon is
1591 * requesting a sync -- there might not be enough memory to do
1592 * the bmap then... So, this is important to do.
1594 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
1595 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
1599 * Set the *dirty* buffer range based upon the VM system dirty
1602 * Mark the buffer pages as clean. We need to do this here to
1603 * satisfy the vnode_pager and the pageout daemon, so that it
1604 * thinks that the pages have been "cleaned". Note that since
1605 * the pages are in a delayed write buffer -- the VFS layer
1606 * "will" see that the pages get written out on the next sync,
1607 * or perhaps the cluster will be completed.
1609 vfs_clean_pages_dirty_buf(bp);
1613 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
1614 * due to the softdep code.
1621 * Turn buffer into delayed write request. We must clear BIO_READ and
1622 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
1623 * itself to properly update it in the dirty/clean lists. We mark it
1624 * B_DONE to ensure that any asynchronization of the buffer properly
1625 * clears B_DONE ( else a panic will occur later ).
1627 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
1628 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
1629 * should only be called if the buffer is known-good.
1631 * Since the buffer is not on a queue, we do not update the numfreebuffers
1634 * The buffer must be on QUEUE_NONE.
1637 bdirty(struct buf *bp)
1640 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
1641 bp, bp->b_vp, bp->b_flags);
1642 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1643 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1644 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
1645 BUF_ASSERT_HELD(bp);
1646 bp->b_flags &= ~(B_RELBUF);
1647 bp->b_iocmd = BIO_WRITE;
1649 if ((bp->b_flags & B_DELWRI) == 0) {
1650 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
1659 * Clear B_DELWRI for buffer.
1661 * Since the buffer is not on a queue, we do not update the numfreebuffers
1664 * The buffer must be on QUEUE_NONE.
1668 bundirty(struct buf *bp)
1671 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1672 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1673 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
1674 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
1675 BUF_ASSERT_HELD(bp);
1677 if (bp->b_flags & B_DELWRI) {
1678 bp->b_flags &= ~B_DELWRI;
1683 * Since it is now being written, we can clear its deferred write flag.
1685 bp->b_flags &= ~B_DEFERRED;
1691 * Asynchronous write. Start output on a buffer, but do not wait for
1692 * it to complete. The buffer is released when the output completes.
1694 * bwrite() ( or the VOP routine anyway ) is responsible for handling
1695 * B_INVAL buffers. Not us.
1698 bawrite(struct buf *bp)
1701 bp->b_flags |= B_ASYNC;
1708 * Asynchronous barrier write. Start output on a buffer, but do not
1709 * wait for it to complete. Place a write barrier after this write so
1710 * that this buffer and all buffers written before it are committed to
1711 * the disk before any buffers written after this write are committed
1712 * to the disk. The buffer is released when the output completes.
1715 babarrierwrite(struct buf *bp)
1718 bp->b_flags |= B_ASYNC | B_BARRIER;
1725 * Synchronous barrier write. Start output on a buffer and wait for
1726 * it to complete. Place a write barrier after this write so that
1727 * this buffer and all buffers written before it are committed to
1728 * the disk before any buffers written after this write are committed
1729 * to the disk. The buffer is released when the output completes.
1732 bbarrierwrite(struct buf *bp)
1735 bp->b_flags |= B_BARRIER;
1736 return (bwrite(bp));
1742 * Called prior to the locking of any vnodes when we are expecting to
1743 * write. We do not want to starve the buffer cache with too many
1744 * dirty buffers so we block here. By blocking prior to the locking
1745 * of any vnodes we attempt to avoid the situation where a locked vnode
1746 * prevents the various system daemons from flushing related buffers.
1752 if (numdirtybuffers >= hidirtybuffers) {
1753 mtx_lock(&bdirtylock);
1754 while (numdirtybuffers >= hidirtybuffers) {
1756 msleep(&bdirtywait, &bdirtylock, (PRIBIO + 4),
1759 mtx_unlock(&bdirtylock);
1764 * Return true if we have too many dirty buffers.
1767 buf_dirty_count_severe(void)
1770 return(numdirtybuffers >= hidirtybuffers);
1776 * Release a busy buffer and, if requested, free its resources. The
1777 * buffer will be stashed in the appropriate bufqueue[] allowing it
1778 * to be accessed later as a cache entity or reused for other purposes.
1781 brelse(struct buf *bp)
1785 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
1786 bp, bp->b_vp, bp->b_flags);
1787 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1788 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1790 if (BUF_LOCKRECURSED(bp)) {
1792 * Do not process, in particular, do not handle the
1793 * B_INVAL/B_RELBUF and do not release to free list.
1799 if (bp->b_flags & B_MANAGED) {
1804 if ((bp->b_vflags & (BV_BKGRDINPROG | BV_BKGRDERR)) == BV_BKGRDERR) {
1805 BO_LOCK(bp->b_bufobj);
1806 bp->b_vflags &= ~BV_BKGRDERR;
1807 BO_UNLOCK(bp->b_bufobj);
1810 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) &&
1811 bp->b_error == EIO && !(bp->b_flags & B_INVAL)) {
1813 * Failed write, redirty. Must clear BIO_ERROR to prevent
1814 * pages from being scrapped. If the error is anything
1815 * other than an I/O error (EIO), assume that retrying
1818 bp->b_ioflags &= ~BIO_ERROR;
1820 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
1821 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
1823 * Either a failed I/O or we were asked to free or not
1826 bp->b_flags |= B_INVAL;
1827 if (!LIST_EMPTY(&bp->b_dep))
1829 if (bp->b_flags & B_DELWRI)
1831 bp->b_flags &= ~(B_DELWRI | B_CACHE);
1832 if ((bp->b_flags & B_VMIO) == 0) {
1841 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_release()
1842 * is called with B_DELWRI set, the underlying pages may wind up
1843 * getting freed causing a previous write (bdwrite()) to get 'lost'
1844 * because pages associated with a B_DELWRI bp are marked clean.
1846 * We still allow the B_INVAL case to call vfs_vmio_release(), even
1847 * if B_DELWRI is set.
1849 if (bp->b_flags & B_DELWRI)
1850 bp->b_flags &= ~B_RELBUF;
1853 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
1854 * constituted, not even NFS buffers now. Two flags effect this. If
1855 * B_INVAL, the struct buf is invalidated but the VM object is kept
1856 * around ( i.e. so it is trivial to reconstitute the buffer later ).
1858 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
1859 * invalidated. BIO_ERROR cannot be set for a failed write unless the
1860 * buffer is also B_INVAL because it hits the re-dirtying code above.
1862 * Normally we can do this whether a buffer is B_DELWRI or not. If
1863 * the buffer is an NFS buffer, it is tracking piecemeal writes or
1864 * the commit state and we cannot afford to lose the buffer. If the
1865 * buffer has a background write in progress, we need to keep it
1866 * around to prevent it from being reconstituted and starting a second
1869 if ((bp->b_flags & B_VMIO) && (bp->b_flags & B_NOCACHE ||
1870 (bp->b_ioflags & BIO_ERROR && bp->b_iocmd == BIO_READ)) &&
1871 !(bp->b_vp->v_mount != NULL &&
1872 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
1873 !vn_isdisk(bp->b_vp, NULL) && (bp->b_flags & B_DELWRI)))
1874 vfs_vmio_invalidate(bp);
1876 if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0) {
1877 if (bp->b_flags & B_VMIO)
1878 vfs_vmio_release(bp);
1879 if (bp->b_bufsize != 0)
1881 if (bp->b_vp != NULL)
1886 * If the buffer has junk contents signal it and eventually
1887 * clean up B_DELWRI and diassociate the vnode so that gbincore()
1890 if (bp->b_bufsize == 0 || (bp->b_ioflags & BIO_ERROR) != 0 ||
1891 (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) != 0)
1892 bp->b_flags |= B_INVAL;
1893 if (bp->b_flags & B_INVAL) {
1894 if (bp->b_flags & B_DELWRI)
1900 /* buffers with no memory */
1901 if (bp->b_bufsize == 0) {
1902 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1903 if (bp->b_vflags & BV_BKGRDINPROG)
1904 panic("losing buffer 1");
1906 qindex = QUEUE_EMPTY;
1907 bp->b_flags |= B_AGE;
1908 /* buffers with junk contents */
1909 } else if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
1910 (bp->b_ioflags & BIO_ERROR)) {
1911 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
1912 if (bp->b_vflags & BV_BKGRDINPROG)
1913 panic("losing buffer 2");
1914 qindex = QUEUE_CLEAN;
1915 bp->b_flags |= B_AGE;
1916 /* remaining buffers */
1917 } else if (bp->b_flags & B_DELWRI)
1918 qindex = QUEUE_DIRTY;
1920 qindex = QUEUE_CLEAN;
1922 binsfree(bp, qindex);
1924 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT);
1925 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
1926 panic("brelse: not dirty");
1932 * Release a buffer back to the appropriate queue but do not try to free
1933 * it. The buffer is expected to be used again soon.
1935 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
1936 * biodone() to requeue an async I/O on completion. It is also used when
1937 * known good buffers need to be requeued but we think we may need the data
1940 * XXX we should be able to leave the B_RELBUF hint set on completion.
1943 bqrelse(struct buf *bp)
1947 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1948 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
1949 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
1951 if (BUF_LOCKRECURSED(bp)) {
1952 /* do not release to free list */
1956 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
1958 if (bp->b_flags & B_MANAGED) {
1959 if (bp->b_flags & B_REMFREE)
1964 /* buffers with stale but valid contents */
1965 if ((bp->b_flags & B_DELWRI) != 0 || (bp->b_vflags & (BV_BKGRDINPROG |
1966 BV_BKGRDERR)) == BV_BKGRDERR) {
1967 BO_LOCK(bp->b_bufobj);
1968 bp->b_vflags &= ~BV_BKGRDERR;
1969 BO_UNLOCK(bp->b_bufobj);
1970 qindex = QUEUE_DIRTY;
1972 if ((bp->b_flags & B_DELWRI) == 0 &&
1973 (bp->b_xflags & BX_VNDIRTY))
1974 panic("bqrelse: not dirty");
1975 qindex = QUEUE_CLEAN;
1977 binsfree(bp, qindex);
1985 * Complete I/O to a VMIO backed page. Validate the pages as appropriate,
1986 * restore bogus pages.
1989 vfs_vmio_iodone(struct buf *bp)
1995 int bogus, i, iosize;
1997 obj = bp->b_bufobj->bo_object;
1998 KASSERT(obj->paging_in_progress >= bp->b_npages,
1999 ("vfs_vmio_iodone: paging in progress(%d) < b_npages(%d)",
2000 obj->paging_in_progress, bp->b_npages));
2003 KASSERT(vp->v_holdcnt > 0,
2004 ("vfs_vmio_iodone: vnode %p has zero hold count", vp));
2005 KASSERT(vp->v_object != NULL,
2006 ("vfs_vmio_iodone: vnode %p has no vm_object", vp));
2008 foff = bp->b_offset;
2009 KASSERT(bp->b_offset != NOOFFSET,
2010 ("vfs_vmio_iodone: bp %p has no buffer offset", bp));
2013 iosize = bp->b_bcount - bp->b_resid;
2014 VM_OBJECT_WLOCK(obj);
2015 for (i = 0; i < bp->b_npages; i++) {
2018 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
2023 * cleanup bogus pages, restoring the originals
2026 if (m == bogus_page) {
2028 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
2030 panic("biodone: page disappeared!");
2032 } else if ((bp->b_iocmd == BIO_READ) && resid > 0) {
2034 * In the write case, the valid and clean bits are
2035 * already changed correctly ( see bdwrite() ), so we
2036 * only need to do this here in the read case.
2038 KASSERT((m->dirty & vm_page_bits(foff & PAGE_MASK,
2039 resid)) == 0, ("vfs_vmio_iodone: page %p "
2040 "has unexpected dirty bits", m));
2041 vfs_page_set_valid(bp, foff, m);
2043 KASSERT(OFF_TO_IDX(foff) == m->pindex,
2044 ("vfs_vmio_iodone: foff(%jd)/pindex(%ju) mismatch",
2045 (intmax_t)foff, (uintmax_t)m->pindex));
2048 vm_object_pip_subtract(obj, 1);
2049 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
2052 vm_object_pip_wakeupn(obj, 0);
2053 VM_OBJECT_WUNLOCK(obj);
2054 if (bogus && buf_mapped(bp)) {
2055 BUF_CHECK_MAPPED(bp);
2056 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
2057 bp->b_pages, bp->b_npages);
2062 * Perform page invalidation when a buffer is released. The fully invalid
2063 * pages will be reclaimed later in vfs_vmio_release().
2066 vfs_vmio_invalidate(struct buf *bp)
2070 int i, resid, poffset, presid;
2073 * Get the base offset and length of the buffer. Note that
2074 * in the VMIO case if the buffer block size is not
2075 * page-aligned then b_data pointer may not be page-aligned.
2076 * But our b_pages[] array *IS* page aligned.
2078 * block sizes less then DEV_BSIZE (usually 512) are not
2079 * supported due to the page granularity bits (m->valid,
2080 * m->dirty, etc...).
2082 * See man buf(9) for more information
2084 obj = bp->b_bufobj->bo_object;
2085 resid = bp->b_bufsize;
2086 poffset = bp->b_offset & PAGE_MASK;
2087 VM_OBJECT_WLOCK(obj);
2088 for (i = 0; i < bp->b_npages; i++) {
2090 if (m == bogus_page)
2091 panic("vfs_vmio_invalidate: Unexpected bogus page.");
2093 presid = resid > (PAGE_SIZE - poffset) ?
2094 (PAGE_SIZE - poffset) : resid;
2095 KASSERT(presid >= 0, ("brelse: extra page"));
2096 while (vm_page_xbusied(m)) {
2098 VM_OBJECT_WUNLOCK(obj);
2099 vm_page_busy_sleep(m, "mbncsh");
2100 VM_OBJECT_WLOCK(obj);
2102 if (pmap_page_wired_mappings(m) == 0)
2103 vm_page_set_invalid(m, poffset, presid);
2107 VM_OBJECT_WUNLOCK(obj);
2110 /* Give pages used by the bp back to the VM system (where possible) */
2112 vfs_vmio_release(struct buf *bp)
2119 if (buf_mapped(bp)) {
2120 BUF_CHECK_MAPPED(bp);
2121 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), bp->b_npages);
2123 BUF_CHECK_UNMAPPED(bp);
2124 obj = bp->b_bufobj->bo_object;
2126 VM_OBJECT_WLOCK(obj);
2127 for (i = 0; i < bp->b_npages; i++) {
2129 bp->b_pages[i] = NULL;
2131 if (vm_page_unwire(m, PQ_NONE)) {
2133 * Determine if the page should be freed before adding
2134 * it to the inactive queue.
2136 if ((bp->b_flags & B_ASYNC) == 0 && m->valid == 0) {
2137 freed = !vm_page_busied(m);
2140 } else if ((bp->b_flags & B_DIRECT) != 0)
2141 freed = vm_page_try_to_free(m);
2146 * In order to maintain LRU page ordering, put
2147 * the page at the tail of the inactive queue.
2149 vm_page_deactivate(m);
2155 VM_OBJECT_WUNLOCK(obj);
2158 bufspaceadjust(bp, 0);
2160 bp->b_flags &= ~B_VMIO;
2164 * Page-granular truncation of an existing VMIO buffer.
2167 vfs_vmio_truncate(struct buf *bp, int desiredpages)
2172 if (bp->b_npages == desiredpages)
2175 if (buf_mapped(bp)) {
2176 BUF_CHECK_MAPPED(bp);
2177 pmap_qremove((vm_offset_t)trunc_page((vm_offset_t)bp->b_data) +
2178 (desiredpages << PAGE_SHIFT), bp->b_npages - desiredpages);
2180 BUF_CHECK_UNMAPPED(bp);
2181 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
2182 for (i = desiredpages; i < bp->b_npages; i++) {
2184 * The page is not freed here -- it is the responsibility of
2185 * vnode_pager_setsize.
2188 KASSERT(m != bogus_page, ("allocbuf: bogus page found"));
2189 while (vm_page_sleep_if_busy(m, "biodep"))
2191 bp->b_pages[i] = NULL;
2193 vm_page_unwire(m, PQ_INACTIVE);
2196 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
2197 bp->b_npages = desiredpages;
2201 * Byte granular extension of VMIO buffers.
2204 vfs_vmio_extend(struct buf *bp, int desiredpages, int size)
2207 * We are growing the buffer, possibly in a
2208 * byte-granular fashion.
2216 * Step 1, bring in the VM pages from the object, allocating
2217 * them if necessary. We must clear B_CACHE if these pages
2218 * are not valid for the range covered by the buffer.
2220 obj = bp->b_bufobj->bo_object;
2221 VM_OBJECT_WLOCK(obj);
2222 while (bp->b_npages < desiredpages) {
2224 * We must allocate system pages since blocking
2225 * here could interfere with paging I/O, no
2226 * matter which process we are.
2228 * Only exclusive busy can be tested here.
2229 * Blocking on shared busy might lead to
2230 * deadlocks once allocbuf() is called after
2231 * pages are vfs_busy_pages().
2233 m = vm_page_grab(obj, OFF_TO_IDX(bp->b_offset) + bp->b_npages,
2234 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM |
2235 VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY |
2236 VM_ALLOC_COUNT(desiredpages - bp->b_npages));
2238 bp->b_flags &= ~B_CACHE;
2239 bp->b_pages[bp->b_npages] = m;
2244 * Step 2. We've loaded the pages into the buffer,
2245 * we have to figure out if we can still have B_CACHE
2246 * set. Note that B_CACHE is set according to the
2247 * byte-granular range ( bcount and size ), not the
2248 * aligned range ( newbsize ).
2250 * The VM test is against m->valid, which is DEV_BSIZE
2251 * aligned. Needless to say, the validity of the data
2252 * needs to also be DEV_BSIZE aligned. Note that this
2253 * fails with NFS if the server or some other client
2254 * extends the file's EOF. If our buffer is resized,
2255 * B_CACHE may remain set! XXX
2257 toff = bp->b_bcount;
2258 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
2259 while ((bp->b_flags & B_CACHE) && toff < size) {
2262 if (tinc > (size - toff))
2264 pi = ((bp->b_offset & PAGE_MASK) + toff) >> PAGE_SHIFT;
2265 m = bp->b_pages[pi];
2266 vfs_buf_test_cache(bp, bp->b_offset, toff, tinc, m);
2270 VM_OBJECT_WUNLOCK(obj);
2273 * Step 3, fixup the KVA pmap.
2278 BUF_CHECK_UNMAPPED(bp);
2282 * Check to see if a block at a particular lbn is available for a clustered
2286 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
2293 /* If the buf isn't in core skip it */
2294 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
2297 /* If the buf is busy we don't want to wait for it */
2298 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2301 /* Only cluster with valid clusterable delayed write buffers */
2302 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
2303 (B_DELWRI | B_CLUSTEROK))
2306 if (bpa->b_bufsize != size)
2310 * Check to see if it is in the expected place on disk and that the
2311 * block has been mapped.
2313 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
2323 * Implement clustered async writes for clearing out B_DELWRI buffers.
2324 * This is much better then the old way of writing only one buffer at
2325 * a time. Note that we may not be presented with the buffers in the
2326 * correct order, so we search for the cluster in both directions.
2329 vfs_bio_awrite(struct buf *bp)
2334 daddr_t lblkno = bp->b_lblkno;
2335 struct vnode *vp = bp->b_vp;
2343 gbflags = (bp->b_data == unmapped_buf) ? GB_UNMAPPED : 0;
2345 * right now we support clustered writing only to regular files. If
2346 * we find a clusterable block we could be in the middle of a cluster
2347 * rather then at the beginning.
2349 if ((vp->v_type == VREG) &&
2350 (vp->v_mount != 0) && /* Only on nodes that have the size info */
2351 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
2353 size = vp->v_mount->mnt_stat.f_iosize;
2354 maxcl = MAXPHYS / size;
2357 for (i = 1; i < maxcl; i++)
2358 if (vfs_bio_clcheck(vp, size, lblkno + i,
2359 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
2362 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
2363 if (vfs_bio_clcheck(vp, size, lblkno - j,
2364 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
2370 * this is a possible cluster write
2374 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl,
2380 bp->b_flags |= B_ASYNC;
2382 * default (old) behavior, writing out only one block
2384 * XXX returns b_bufsize instead of b_bcount for nwritten?
2386 nwritten = bp->b_bufsize;
2393 * Ask the bufdaemon for help, or act as bufdaemon itself, when a
2394 * locked vnode is supplied.
2397 getnewbuf_bufd_help(struct vnode *vp, int gbflags, int slpflag, int slptimeo,
2402 int error, fl, flags, norunbuf;
2404 mtx_assert(&bqclean, MA_OWNED);
2407 flags = VFS_BIO_NEED_BUFSPACE;
2409 } else if (bufspace >= hibufspace) {
2411 flags = VFS_BIO_NEED_BUFSPACE;
2414 flags = VFS_BIO_NEED_ANY;
2416 atomic_set_int(&needsbuffer, flags);
2417 mtx_unlock(&bqclean);
2419 bd_speedup(); /* heeeelp */
2420 if ((gbflags & GB_NOWAIT_BD) != 0)
2425 while ((needsbuffer & flags) != 0) {
2426 if (vp != NULL && vp->v_type != VCHR &&
2427 (td->td_pflags & TDP_BUFNEED) == 0) {
2428 rw_wunlock(&nblock);
2430 * getblk() is called with a vnode locked, and
2431 * some majority of the dirty buffers may as
2432 * well belong to the vnode. Flushing the
2433 * buffers there would make a progress that
2434 * cannot be achieved by the buf_daemon, that
2435 * cannot lock the vnode.
2437 norunbuf = ~(TDP_BUFNEED | TDP_NORUNNINGBUF) |
2438 (td->td_pflags & TDP_NORUNNINGBUF);
2441 * Play bufdaemon. The getnewbuf() function
2442 * may be called while the thread owns lock
2443 * for another dirty buffer for the same
2444 * vnode, which makes it impossible to use
2445 * VOP_FSYNC() there, due to the buffer lock
2448 td->td_pflags |= TDP_BUFNEED | TDP_NORUNNINGBUF;
2449 fl = buf_flush(vp, flushbufqtarget);
2450 td->td_pflags &= norunbuf;
2454 if ((needsbuffer & flags) == 0)
2457 error = rw_sleep(__DEVOLATILE(void *, &needsbuffer), &nblock,
2458 (PRIBIO + 4) | slpflag, waitmsg, slptimeo);
2462 rw_wunlock(&nblock);
2466 getnewbuf_reuse_bp(struct buf *bp, int qindex)
2469 CTR6(KTR_BUF, "getnewbuf(%p) vp %p flags %X kvasize %d bufsize %d "
2470 "queue %d (recycling)", bp, bp->b_vp, bp->b_flags,
2471 bp->b_kvasize, bp->b_bufsize, qindex);
2472 mtx_assert(&bqclean, MA_NOTOWNED);
2475 * Note: we no longer distinguish between VMIO and non-VMIO
2478 KASSERT((bp->b_flags & B_DELWRI) == 0,
2479 ("delwri buffer %p found in queue %d", bp, qindex));
2481 if (qindex == QUEUE_CLEAN) {
2482 if (bp->b_flags & B_VMIO) {
2483 bp->b_flags &= ~B_ASYNC;
2484 vfs_vmio_release(bp);
2486 if (bp->b_vp != NULL)
2491 * Get the rest of the buffer freed up. b_kva* is still valid
2492 * after this operation.
2495 if (bp->b_rcred != NOCRED) {
2496 crfree(bp->b_rcred);
2497 bp->b_rcred = NOCRED;
2499 if (bp->b_wcred != NOCRED) {
2500 crfree(bp->b_wcred);
2501 bp->b_wcred = NOCRED;
2503 if (!LIST_EMPTY(&bp->b_dep))
2505 if (bp->b_vflags & BV_BKGRDINPROG)
2506 panic("losing buffer 3");
2507 KASSERT(bp->b_vp == NULL, ("bp: %p still has vnode %p. qindex: %d",
2508 bp, bp->b_vp, qindex));
2509 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
2510 ("bp: %p still on a buffer list. xflags %X", bp, bp->b_xflags));
2518 KASSERT((bp->b_flags & B_INFREECNT) == 0,
2519 ("buf %p still counted as free?", bp));
2522 bp->b_blkno = bp->b_lblkno = 0;
2523 bp->b_offset = NOOFFSET;
2529 bp->b_dirtyoff = bp->b_dirtyend = 0;
2530 bp->b_bufobj = NULL;
2531 bp->b_pin_count = 0;
2532 bp->b_data = bp->b_kvabase;
2533 bp->b_fsprivate1 = NULL;
2534 bp->b_fsprivate2 = NULL;
2535 bp->b_fsprivate3 = NULL;
2537 LIST_INIT(&bp->b_dep);
2541 getnewbuf_scan(int maxsize, int defrag, int unmapped, int metadata)
2543 struct buf *bp, *nbp;
2544 int nqindex, qindex, pass;
2546 KASSERT(!unmapped || !defrag, ("both unmapped and defrag"));
2551 atomic_add_int(&getnewbufrestarts, 1);
2556 * If we're not defragging or low on bufspace attempt to make a new
2557 * buf from a header.
2559 if (defrag == 0 && bufspace + maxsize < hibufspace) {
2560 nqindex = QUEUE_EMPTY;
2561 nbp = TAILQ_FIRST(&bufqueues[nqindex]);
2564 * All available buffers might be clean or we need to start recycling.
2567 nqindex = QUEUE_CLEAN;
2568 nbp = TAILQ_FIRST(&bufqueues[QUEUE_CLEAN]);
2572 * Run scan, possibly freeing data and/or kva mappings on the fly
2575 while ((bp = nbp) != NULL) {
2579 * Calculate next bp (we can only use it if we do not
2580 * release the bqlock)
2582 if ((nbp = TAILQ_NEXT(bp, b_freelist)) == NULL) {
2585 nqindex = QUEUE_CLEAN;
2586 nbp = TAILQ_FIRST(&bufqueues[nqindex]);
2591 if (metadata && pass == 0) {
2593 nqindex = QUEUE_EMPTY;
2594 nbp = TAILQ_FIRST(&bufqueues[nqindex]);
2603 * If we are defragging then we need a buffer with
2604 * b_kvasize != 0. This situation occurs when we
2605 * have many unmapped bufs.
2607 if (defrag && bp->b_kvasize == 0)
2611 * Start freeing the bp. This is somewhat involved. nbp
2612 * remains valid only for QUEUE_EMPTY[KVA] bp's.
2614 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2617 * BKGRDINPROG can only be set with the buf and bufobj
2618 * locks both held. We tolerate a race to clear it here.
2620 if (bp->b_vflags & BV_BKGRDINPROG) {
2626 * Requeue the background write buffer with error.
2628 if ((bp->b_vflags & BV_BKGRDERR) != 0) {
2630 mtx_unlock(&bqclean);
2635 KASSERT(bp->b_qindex == qindex,
2636 ("getnewbuf: inconsistent queue %d bp %p", qindex, bp));
2639 mtx_unlock(&bqclean);
2642 * NOTE: nbp is now entirely invalid. We can only restart
2643 * the scan from this point on.
2645 getnewbuf_reuse_bp(bp, qindex);
2646 mtx_assert(&bqclean, MA_NOTOWNED);
2649 * If we are defragging then free the buffer.
2652 bp->b_flags |= B_INVAL;
2659 * Notify any waiters for the buffer lock about
2660 * identity change by freeing the buffer.
2662 if (qindex == QUEUE_CLEAN && BUF_LOCKWAITERS(bp)) {
2663 bp->b_flags |= B_INVAL;
2672 * If we are overcomitted then recover the buffer and its
2673 * KVM space. This occurs in rare situations when multiple
2674 * processes are blocked in getnewbuf() or allocbuf().
2676 if (bufspace >= hibufspace && bp->b_kvasize != 0) {
2677 bp->b_flags |= B_INVAL;
2689 * Find and initialize a new buffer header, freeing up existing buffers
2690 * in the bufqueues as necessary. The new buffer is returned locked.
2692 * Important: B_INVAL is not set. If the caller wishes to throw the
2693 * buffer away, the caller must set B_INVAL prior to calling brelse().
2696 * We have insufficient buffer headers
2697 * We have insufficient buffer space
2698 * buffer_arena is too fragmented ( space reservation fails )
2699 * If we have to flush dirty buffers ( but we try to avoid this )
2702 getnewbuf(struct vnode *vp, int slpflag, int slptimeo, int size, int maxsize,
2706 int defrag, metadata;
2708 KASSERT((gbflags & (GB_UNMAPPED | GB_KVAALLOC)) != GB_KVAALLOC,
2709 ("GB_KVAALLOC only makes sense with GB_UNMAPPED"));
2710 if (!unmapped_buf_allowed)
2711 gbflags &= ~(GB_UNMAPPED | GB_KVAALLOC);
2714 if (vp == NULL || (vp->v_vflag & (VV_MD | VV_SYSTEM)) != 0 ||
2720 * We can't afford to block since we might be holding a vnode lock,
2721 * which may prevent system daemons from running. We deal with
2722 * low-memory situations by proactively returning memory and running
2723 * async I/O rather then sync I/O.
2725 atomic_add_int(&getnewbufcalls, 1);
2727 bp = getnewbuf_scan(maxsize, defrag, (gbflags & (GB_UNMAPPED |
2728 GB_KVAALLOC)) == GB_UNMAPPED, metadata);
2733 * If we exhausted our list, sleep as appropriate. We may have to
2734 * wakeup various daemons and write out some dirty buffers.
2736 * Generally we are sleeping due to insufficient buffer space.
2739 mtx_assert(&bqclean, MA_OWNED);
2740 getnewbuf_bufd_help(vp, gbflags, slpflag, slptimeo, defrag);
2741 mtx_assert(&bqclean, MA_NOTOWNED);
2742 } else if ((gbflags & (GB_UNMAPPED | GB_KVAALLOC)) == GB_UNMAPPED) {
2743 mtx_assert(&bqclean, MA_NOTOWNED);
2746 atomic_add_int(&bufreusecnt, 1);
2748 mtx_assert(&bqclean, MA_NOTOWNED);
2751 * We finally have a valid bp. We aren't quite out of the
2752 * woods, we still have to reserve kva space. In order to
2753 * keep fragmentation sane we only allocate kva in BKVASIZE
2756 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
2758 if (maxsize != bp->b_kvasize &&
2759 bufkvaalloc(bp, maxsize, gbflags)) {
2761 bp->b_flags |= B_INVAL;
2764 } else if ((gbflags & (GB_UNMAPPED | GB_KVAALLOC)) ==
2765 (GB_UNMAPPED | GB_KVAALLOC)) {
2766 bp->b_data = unmapped_buf;
2767 BUF_CHECK_UNMAPPED(bp);
2769 atomic_add_int(&bufreusecnt, 1);
2777 * buffer flushing daemon. Buffers are normally flushed by the
2778 * update daemon but if it cannot keep up this process starts to
2779 * take the load in an attempt to prevent getnewbuf() from blocking.
2782 static struct kproc_desc buf_kp = {
2787 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp);
2790 buf_flush(struct vnode *vp, int target)
2794 flushed = flushbufqueues(vp, target, 0);
2797 * Could not find any buffers without rollback
2798 * dependencies, so just write the first one
2799 * in the hopes of eventually making progress.
2801 if (vp != NULL && target > 2)
2803 flushbufqueues(vp, target, 1);
2814 * This process needs to be suspended prior to shutdown sync.
2816 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
2820 * This process is allowed to take the buffer cache to the limit
2822 curthread->td_pflags |= TDP_NORUNNINGBUF | TDP_BUFNEED;
2826 mtx_unlock(&bdlock);
2828 kproc_suspend_check(bufdaemonproc);
2829 lodirty = lodirtybuffers;
2830 if (bd_speedupreq) {
2831 lodirty = numdirtybuffers / 2;
2835 * Do the flush. Limit the amount of in-transit I/O we
2836 * allow to build up, otherwise we would completely saturate
2839 while (numdirtybuffers > lodirty) {
2840 if (buf_flush(NULL, numdirtybuffers - lodirty) == 0)
2842 kern_yield(PRI_USER);
2846 * Only clear bd_request if we have reached our low water
2847 * mark. The buf_daemon normally waits 1 second and
2848 * then incrementally flushes any dirty buffers that have
2849 * built up, within reason.
2851 * If we were unable to hit our low water mark and couldn't
2852 * find any flushable buffers, we sleep for a short period
2853 * to avoid endless loops on unlockable buffers.
2856 if (numdirtybuffers <= lodirtybuffers) {
2858 * We reached our low water mark, reset the
2859 * request and sleep until we are needed again.
2860 * The sleep is just so the suspend code works.
2864 * Do an extra wakeup in case dirty threshold
2865 * changed via sysctl and the explicit transition
2866 * out of shortfall was missed.
2869 if (runningbufspace <= lorunningspace)
2871 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
2874 * We couldn't find any flushable dirty buffers but
2875 * still have too many dirty buffers, we
2876 * have to sleep and try again. (rare)
2878 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
2886 * Try to flush a buffer in the dirty queue. We must be careful to
2887 * free up B_INVAL buffers instead of write them, which NFS is
2888 * particularly sensitive to.
2890 static int flushwithdeps = 0;
2891 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
2892 0, "Number of buffers flushed with dependecies that require rollbacks");
2895 flushbufqueues(struct vnode *lvp, int target, int flushdeps)
2897 struct buf *sentinel;
2908 queue = QUEUE_DIRTY;
2910 sentinel = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
2911 sentinel->b_qindex = QUEUE_SENTINEL;
2913 TAILQ_INSERT_HEAD(&bufqueues[queue], sentinel, b_freelist);
2914 mtx_unlock(&bqdirty);
2915 while (flushed != target) {
2918 bp = TAILQ_NEXT(sentinel, b_freelist);
2920 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
2921 TAILQ_INSERT_AFTER(&bufqueues[queue], bp, sentinel,
2924 mtx_unlock(&bqdirty);
2928 * Skip sentinels inserted by other invocations of the
2929 * flushbufqueues(), taking care to not reorder them.
2931 * Only flush the buffers that belong to the
2932 * vnode locked by the curthread.
2934 if (bp->b_qindex == QUEUE_SENTINEL || (lvp != NULL &&
2936 mtx_unlock(&bqdirty);
2939 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL);
2940 mtx_unlock(&bqdirty);
2943 if (bp->b_pin_count > 0) {
2948 * BKGRDINPROG can only be set with the buf and bufobj
2949 * locks both held. We tolerate a race to clear it here.
2951 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
2952 (bp->b_flags & B_DELWRI) == 0) {
2956 if (bp->b_flags & B_INVAL) {
2963 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) {
2964 if (flushdeps == 0) {
2972 * We must hold the lock on a vnode before writing
2973 * one of its buffers. Otherwise we may confuse, or
2974 * in the case of a snapshot vnode, deadlock the
2977 * The lock order here is the reverse of the normal
2978 * of vnode followed by buf lock. This is ok because
2979 * the NOWAIT will prevent deadlock.
2982 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2988 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
2990 ASSERT_VOP_LOCKED(vp, "getbuf");
2992 error = VOP_ISLOCKED(vp) == LK_EXCLUSIVE ? 0 :
2993 vn_lock(vp, LK_TRYUPGRADE);
2996 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
2997 bp, bp->b_vp, bp->b_flags);
2998 if (curproc == bufdaemonproc) {
3005 vn_finished_write(mp);
3008 flushwithdeps += hasdeps;
3012 * Sleeping on runningbufspace while holding
3013 * vnode lock leads to deadlock.
3015 if (curproc == bufdaemonproc &&
3016 runningbufspace > hirunningspace)
3017 waitrunningbufspace();
3020 vn_finished_write(mp);
3024 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
3025 mtx_unlock(&bqdirty);
3026 free(sentinel, M_TEMP);
3031 * Check to see if a block is currently memory resident.
3034 incore(struct bufobj *bo, daddr_t blkno)
3039 bp = gbincore(bo, blkno);
3045 * Returns true if no I/O is needed to access the
3046 * associated VM object. This is like incore except
3047 * it also hunts around in the VM system for the data.
3051 inmem(struct vnode * vp, daddr_t blkno)
3054 vm_offset_t toff, tinc, size;
3058 ASSERT_VOP_LOCKED(vp, "inmem");
3060 if (incore(&vp->v_bufobj, blkno))
3062 if (vp->v_mount == NULL)
3069 if (size > vp->v_mount->mnt_stat.f_iosize)
3070 size = vp->v_mount->mnt_stat.f_iosize;
3071 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
3073 VM_OBJECT_RLOCK(obj);
3074 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
3075 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
3079 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
3080 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
3081 if (vm_page_is_valid(m,
3082 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
3085 VM_OBJECT_RUNLOCK(obj);
3089 VM_OBJECT_RUNLOCK(obj);
3094 * Set the dirty range for a buffer based on the status of the dirty
3095 * bits in the pages comprising the buffer. The range is limited
3096 * to the size of the buffer.
3098 * Tell the VM system that the pages associated with this buffer
3099 * are clean. This is used for delayed writes where the data is
3100 * going to go to disk eventually without additional VM intevention.
3102 * Note that while we only really need to clean through to b_bcount, we
3103 * just go ahead and clean through to b_bufsize.
3106 vfs_clean_pages_dirty_buf(struct buf *bp)
3108 vm_ooffset_t foff, noff, eoff;
3112 if ((bp->b_flags & B_VMIO) == 0 || bp->b_bufsize == 0)
3115 foff = bp->b_offset;
3116 KASSERT(bp->b_offset != NOOFFSET,
3117 ("vfs_clean_pages_dirty_buf: no buffer offset"));
3119 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
3120 vfs_drain_busy_pages(bp);
3121 vfs_setdirty_locked_object(bp);
3122 for (i = 0; i < bp->b_npages; i++) {
3123 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3125 if (eoff > bp->b_offset + bp->b_bufsize)
3126 eoff = bp->b_offset + bp->b_bufsize;
3128 vfs_page_set_validclean(bp, foff, m);
3129 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
3132 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
3136 vfs_setdirty_locked_object(struct buf *bp)
3141 object = bp->b_bufobj->bo_object;
3142 VM_OBJECT_ASSERT_WLOCKED(object);
3145 * We qualify the scan for modified pages on whether the
3146 * object has been flushed yet.
3148 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) {
3149 vm_offset_t boffset;
3150 vm_offset_t eoffset;
3153 * test the pages to see if they have been modified directly
3154 * by users through the VM system.
3156 for (i = 0; i < bp->b_npages; i++)
3157 vm_page_test_dirty(bp->b_pages[i]);
3160 * Calculate the encompassing dirty range, boffset and eoffset,
3161 * (eoffset - boffset) bytes.
3164 for (i = 0; i < bp->b_npages; i++) {
3165 if (bp->b_pages[i]->dirty)
3168 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
3170 for (i = bp->b_npages - 1; i >= 0; --i) {
3171 if (bp->b_pages[i]->dirty) {
3175 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
3178 * Fit it to the buffer.
3181 if (eoffset > bp->b_bcount)
3182 eoffset = bp->b_bcount;
3185 * If we have a good dirty range, merge with the existing
3189 if (boffset < eoffset) {
3190 if (bp->b_dirtyoff > boffset)
3191 bp->b_dirtyoff = boffset;
3192 if (bp->b_dirtyend < eoffset)
3193 bp->b_dirtyend = eoffset;
3199 * Allocate the KVA mapping for an existing buffer.
3200 * If an unmapped buffer is provided but a mapped buffer is requested, take
3201 * also care to properly setup mappings between pages and KVA.
3204 bp_unmapped_get_kva(struct buf *bp, daddr_t blkno, int size, int gbflags)
3206 struct buf *scratch_bp;
3207 int bsize, maxsize, need_mapping, need_kva;
3210 need_mapping = bp->b_data == unmapped_buf &&
3211 (gbflags & GB_UNMAPPED) == 0;
3212 need_kva = bp->b_kvabase == unmapped_buf &&
3213 bp->b_data == unmapped_buf &&
3214 (gbflags & GB_KVAALLOC) != 0;
3215 if (!need_mapping && !need_kva)
3218 BUF_CHECK_UNMAPPED(bp);
3220 if (need_mapping && bp->b_kvabase != unmapped_buf) {
3222 * Buffer is not mapped, but the KVA was already
3223 * reserved at the time of the instantiation. Use the
3230 * Calculate the amount of the address space we would reserve
3231 * if the buffer was mapped.
3233 bsize = vn_isdisk(bp->b_vp, NULL) ? DEV_BSIZE : bp->b_bufobj->bo_bsize;
3234 KASSERT(bsize != 0, ("bsize == 0, check bo->bo_bsize"));
3235 offset = blkno * bsize;
3236 maxsize = size + (offset & PAGE_MASK);
3237 maxsize = imax(maxsize, bsize);
3240 if (bufkvaalloc(bp, maxsize, gbflags)) {
3242 * Request defragmentation. getnewbuf() returns us the
3243 * allocated space by the scratch buffer KVA.
3245 scratch_bp = getnewbuf(bp->b_vp, 0, 0, size, maxsize, gbflags |
3246 (GB_UNMAPPED | GB_KVAALLOC));
3247 if (scratch_bp == NULL) {
3248 if ((gbflags & GB_NOWAIT_BD) != 0) {
3250 * XXXKIB: defragmentation cannot
3251 * succeed, not sure what else to do.
3253 panic("GB_NOWAIT_BD and GB_UNMAPPED %p", bp);
3255 atomic_add_int(&mappingrestarts, 1);
3258 KASSERT(scratch_bp->b_kvabase != unmapped_buf,
3259 ("scratch bp has no KVA %p", scratch_bp));
3260 /* Grab pointers. */
3261 bp->b_kvabase = scratch_bp->b_kvabase;
3262 bp->b_kvasize = scratch_bp->b_kvasize;
3263 bp->b_data = scratch_bp->b_data;
3265 /* Get rid of the scratch buffer. */
3266 scratch_bp->b_kvasize = 0;
3267 scratch_bp->b_flags |= B_INVAL;
3268 scratch_bp->b_data = scratch_bp->b_kvabase = unmapped_buf;
3273 /* b_offset is handled by bpmap_qenter. */
3274 bp->b_data = bp->b_kvabase;
3275 BUF_CHECK_MAPPED(bp);
3283 * Get a block given a specified block and offset into a file/device.
3284 * The buffers B_DONE bit will be cleared on return, making it almost
3285 * ready for an I/O initiation. B_INVAL may or may not be set on
3286 * return. The caller should clear B_INVAL prior to initiating a
3289 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
3290 * an existing buffer.
3292 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
3293 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
3294 * and then cleared based on the backing VM. If the previous buffer is
3295 * non-0-sized but invalid, B_CACHE will be cleared.
3297 * If getblk() must create a new buffer, the new buffer is returned with
3298 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
3299 * case it is returned with B_INVAL clear and B_CACHE set based on the
3302 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
3303 * B_CACHE bit is clear.
3305 * What this means, basically, is that the caller should use B_CACHE to
3306 * determine whether the buffer is fully valid or not and should clear
3307 * B_INVAL prior to issuing a read. If the caller intends to validate
3308 * the buffer by loading its data area with something, the caller needs
3309 * to clear B_INVAL. If the caller does this without issuing an I/O,
3310 * the caller should set B_CACHE ( as an optimization ), else the caller
3311 * should issue the I/O and biodone() will set B_CACHE if the I/O was
3312 * a write attempt or if it was a successfull read. If the caller
3313 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
3314 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
3317 getblk(struct vnode *vp, daddr_t blkno, int size, int slpflag, int slptimeo,
3322 int bsize, error, maxsize, vmio;
3325 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
3326 KASSERT((flags & (GB_UNMAPPED | GB_KVAALLOC)) != GB_KVAALLOC,
3327 ("GB_KVAALLOC only makes sense with GB_UNMAPPED"));
3328 ASSERT_VOP_LOCKED(vp, "getblk");
3329 if (size > MAXBCACHEBUF)
3330 panic("getblk: size(%d) > MAXBCACHEBUF(%d)\n", size,
3332 if (!unmapped_buf_allowed)
3333 flags &= ~(GB_UNMAPPED | GB_KVAALLOC);
3338 bp = gbincore(bo, blkno);
3342 * Buffer is in-core. If the buffer is not busy nor managed,
3343 * it must be on a queue.
3345 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
3347 if (flags & GB_LOCK_NOWAIT)
3348 lockflags |= LK_NOWAIT;
3350 error = BUF_TIMELOCK(bp, lockflags,
3351 BO_LOCKPTR(bo), "getblk", slpflag, slptimeo);
3354 * If we slept and got the lock we have to restart in case
3355 * the buffer changed identities.
3357 if (error == ENOLCK)
3359 /* We timed out or were interrupted. */
3362 /* If recursed, assume caller knows the rules. */
3363 else if (BUF_LOCKRECURSED(bp))
3367 * The buffer is locked. B_CACHE is cleared if the buffer is
3368 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
3369 * and for a VMIO buffer B_CACHE is adjusted according to the
3372 if (bp->b_flags & B_INVAL)
3373 bp->b_flags &= ~B_CACHE;
3374 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
3375 bp->b_flags |= B_CACHE;
3376 if (bp->b_flags & B_MANAGED)
3377 MPASS(bp->b_qindex == QUEUE_NONE);
3382 * check for size inconsistencies for non-VMIO case.
3384 if (bp->b_bcount != size) {
3385 if ((bp->b_flags & B_VMIO) == 0 ||
3386 (size > bp->b_kvasize)) {
3387 if (bp->b_flags & B_DELWRI) {
3389 * If buffer is pinned and caller does
3390 * not want sleep waiting for it to be
3391 * unpinned, bail out
3393 if (bp->b_pin_count > 0) {
3394 if (flags & GB_LOCK_NOWAIT) {
3401 bp->b_flags |= B_NOCACHE;
3404 if (LIST_EMPTY(&bp->b_dep)) {
3405 bp->b_flags |= B_RELBUF;
3408 bp->b_flags |= B_NOCACHE;
3417 * Handle the case of unmapped buffer which should
3418 * become mapped, or the buffer for which KVA
3419 * reservation is requested.
3421 bp_unmapped_get_kva(bp, blkno, size, flags);
3424 * If the size is inconsistant in the VMIO case, we can resize
3425 * the buffer. This might lead to B_CACHE getting set or
3426 * cleared. If the size has not changed, B_CACHE remains
3427 * unchanged from its previous state.
3429 if (bp->b_bcount != size)
3432 KASSERT(bp->b_offset != NOOFFSET,
3433 ("getblk: no buffer offset"));
3436 * A buffer with B_DELWRI set and B_CACHE clear must
3437 * be committed before we can return the buffer in
3438 * order to prevent the caller from issuing a read
3439 * ( due to B_CACHE not being set ) and overwriting
3442 * Most callers, including NFS and FFS, need this to
3443 * operate properly either because they assume they
3444 * can issue a read if B_CACHE is not set, or because
3445 * ( for example ) an uncached B_DELWRI might loop due
3446 * to softupdates re-dirtying the buffer. In the latter
3447 * case, B_CACHE is set after the first write completes,
3448 * preventing further loops.
3449 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
3450 * above while extending the buffer, we cannot allow the
3451 * buffer to remain with B_CACHE set after the write
3452 * completes or it will represent a corrupt state. To
3453 * deal with this we set B_NOCACHE to scrap the buffer
3456 * We might be able to do something fancy, like setting
3457 * B_CACHE in bwrite() except if B_DELWRI is already set,
3458 * so the below call doesn't set B_CACHE, but that gets real
3459 * confusing. This is much easier.
3462 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
3463 bp->b_flags |= B_NOCACHE;
3467 bp->b_flags &= ~B_DONE;
3470 * Buffer is not in-core, create new buffer. The buffer
3471 * returned by getnewbuf() is locked. Note that the returned
3472 * buffer is also considered valid (not marked B_INVAL).
3476 * If the user does not want us to create the buffer, bail out
3479 if (flags & GB_NOCREAT)
3481 if (numfreebuffers == 0 && TD_IS_IDLETHREAD(curthread))
3484 bsize = vn_isdisk(vp, NULL) ? DEV_BSIZE : bo->bo_bsize;
3485 KASSERT(bsize != 0, ("bsize == 0, check bo->bo_bsize"));
3486 offset = blkno * bsize;
3487 vmio = vp->v_object != NULL;
3489 maxsize = size + (offset & PAGE_MASK);
3492 /* Do not allow non-VMIO notmapped buffers. */
3493 flags &= ~(GB_UNMAPPED | GB_KVAALLOC);
3495 maxsize = imax(maxsize, bsize);
3497 bp = getnewbuf(vp, slpflag, slptimeo, size, maxsize, flags);
3499 if (slpflag || slptimeo)
3505 * This code is used to make sure that a buffer is not
3506 * created while the getnewbuf routine is blocked.
3507 * This can be a problem whether the vnode is locked or not.
3508 * If the buffer is created out from under us, we have to
3509 * throw away the one we just created.
3511 * Note: this must occur before we associate the buffer
3512 * with the vp especially considering limitations in
3513 * the splay tree implementation when dealing with duplicate
3517 if (gbincore(bo, blkno)) {
3519 bp->b_flags |= B_INVAL;
3525 * Insert the buffer into the hash, so that it can
3526 * be found by incore.
3528 bp->b_blkno = bp->b_lblkno = blkno;
3529 bp->b_offset = offset;
3534 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
3535 * buffer size starts out as 0, B_CACHE will be set by
3536 * allocbuf() for the VMIO case prior to it testing the
3537 * backing store for validity.
3541 bp->b_flags |= B_VMIO;
3542 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
3543 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
3544 bp, vp->v_object, bp->b_bufobj->bo_object));
3546 bp->b_flags &= ~B_VMIO;
3547 KASSERT(bp->b_bufobj->bo_object == NULL,
3548 ("ARGH! has b_bufobj->bo_object %p %p\n",
3549 bp, bp->b_bufobj->bo_object));
3550 BUF_CHECK_MAPPED(bp);
3554 bp->b_flags &= ~B_DONE;
3556 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
3557 BUF_ASSERT_HELD(bp);
3559 KASSERT(bp->b_bufobj == bo,
3560 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
3565 * Get an empty, disassociated buffer of given size. The buffer is initially
3569 geteblk(int size, int flags)
3574 maxsize = (size + BKVAMASK) & ~BKVAMASK;
3575 while ((bp = getnewbuf(NULL, 0, 0, size, maxsize, flags)) == NULL) {
3576 if ((flags & GB_NOWAIT_BD) &&
3577 (curthread->td_pflags & TDP_BUFNEED) != 0)
3581 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
3582 BUF_ASSERT_HELD(bp);
3587 * Truncate the backing store for a non-vmio buffer.
3590 vfs_nonvmio_truncate(struct buf *bp, int newbsize)
3593 if (bp->b_flags & B_MALLOC) {
3595 * malloced buffers are not shrunk
3597 if (newbsize == 0) {
3598 bufmallocadjust(bp, 0);
3599 free(bp->b_data, M_BIOBUF);
3600 bp->b_data = bp->b_kvabase;
3601 bp->b_flags &= ~B_MALLOC;
3605 vm_hold_free_pages(bp, newbsize);
3606 bufspaceadjust(bp, newbsize);
3610 * Extend the backing for a non-VMIO buffer.
3613 vfs_nonvmio_extend(struct buf *bp, int newbsize)
3619 * We only use malloced memory on the first allocation.
3620 * and revert to page-allocated memory when the buffer
3623 * There is a potential smp race here that could lead
3624 * to bufmallocspace slightly passing the max. It
3625 * is probably extremely rare and not worth worrying
3628 if (bp->b_bufsize == 0 && newbsize <= PAGE_SIZE/2 &&
3629 bufmallocspace < maxbufmallocspace) {
3630 bp->b_data = malloc(newbsize, M_BIOBUF, M_WAITOK);
3631 bp->b_flags |= B_MALLOC;
3632 bufmallocadjust(bp, newbsize);
3637 * If the buffer is growing on its other-than-first
3638 * allocation then we revert to the page-allocation
3643 if (bp->b_flags & B_MALLOC) {
3644 origbuf = bp->b_data;
3645 origbufsize = bp->b_bufsize;
3646 bp->b_data = bp->b_kvabase;
3647 bufmallocadjust(bp, 0);
3648 bp->b_flags &= ~B_MALLOC;
3649 newbsize = round_page(newbsize);
3651 vm_hold_load_pages(bp, (vm_offset_t) bp->b_data + bp->b_bufsize,
3652 (vm_offset_t) bp->b_data + newbsize);
3653 if (origbuf != NULL) {
3654 bcopy(origbuf, bp->b_data, origbufsize);
3655 free(origbuf, M_BIOBUF);
3657 bufspaceadjust(bp, newbsize);
3661 * This code constitutes the buffer memory from either anonymous system
3662 * memory (in the case of non-VMIO operations) or from an associated
3663 * VM object (in the case of VMIO operations). This code is able to
3664 * resize a buffer up or down.
3666 * Note that this code is tricky, and has many complications to resolve
3667 * deadlock or inconsistant data situations. Tread lightly!!!
3668 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
3669 * the caller. Calling this code willy nilly can result in the loss of data.
3671 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
3672 * B_CACHE for the non-VMIO case.
3675 allocbuf(struct buf *bp, int size)
3679 BUF_ASSERT_HELD(bp);
3681 if (bp->b_kvasize != 0 && bp->b_kvasize < size)
3682 panic("allocbuf: buffer too small");
3684 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
3685 if ((bp->b_flags & B_VMIO) == 0) {
3686 if ((bp->b_flags & B_MALLOC) == 0)
3687 newbsize = round_page(newbsize);
3689 * Just get anonymous memory from the kernel. Don't
3690 * mess with B_CACHE.
3692 if (newbsize < bp->b_bufsize)
3693 vfs_nonvmio_truncate(bp, newbsize);
3694 else if (newbsize > bp->b_bufsize)
3695 vfs_nonvmio_extend(bp, newbsize);
3699 desiredpages = (size == 0) ? 0 :
3700 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
3702 if (bp->b_flags & B_MALLOC)
3703 panic("allocbuf: VMIO buffer can't be malloced");
3705 * Set B_CACHE initially if buffer is 0 length or will become
3708 if (size == 0 || bp->b_bufsize == 0)
3709 bp->b_flags |= B_CACHE;
3711 if (newbsize < bp->b_bufsize)
3712 vfs_vmio_truncate(bp, desiredpages);
3713 /* XXX This looks as if it should be newbsize > b_bufsize */
3714 else if (size > bp->b_bcount)
3715 vfs_vmio_extend(bp, desiredpages, size);
3716 bufspaceadjust(bp, newbsize);
3718 bp->b_bcount = size; /* requested buffer size. */
3722 extern int inflight_transient_maps;
3725 biodone(struct bio *bp)
3728 void (*done)(struct bio *);
3729 vm_offset_t start, end;
3731 if ((bp->bio_flags & BIO_TRANSIENT_MAPPING) != 0) {
3732 bp->bio_flags &= ~BIO_TRANSIENT_MAPPING;
3733 bp->bio_flags |= BIO_UNMAPPED;
3734 start = trunc_page((vm_offset_t)bp->bio_data);
3735 end = round_page((vm_offset_t)bp->bio_data + bp->bio_length);
3736 bp->bio_data = unmapped_buf;
3737 pmap_qremove(start, OFF_TO_IDX(end - start));
3738 vmem_free(transient_arena, start, end - start);
3739 atomic_add_int(&inflight_transient_maps, -1);
3741 done = bp->bio_done;
3743 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3745 bp->bio_flags |= BIO_DONE;
3749 bp->bio_flags |= BIO_DONE;
3755 * Wait for a BIO to finish.
3758 biowait(struct bio *bp, const char *wchan)
3762 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3764 while ((bp->bio_flags & BIO_DONE) == 0)
3765 msleep(bp, mtxp, PRIBIO, wchan, 0);
3767 if (bp->bio_error != 0)
3768 return (bp->bio_error);
3769 if (!(bp->bio_flags & BIO_ERROR))
3775 biofinish(struct bio *bp, struct devstat *stat, int error)
3779 bp->bio_error = error;
3780 bp->bio_flags |= BIO_ERROR;
3783 devstat_end_transaction_bio(stat, bp);
3790 * Wait for buffer I/O completion, returning error status. The buffer
3791 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
3792 * error and cleared.
3795 bufwait(struct buf *bp)
3797 if (bp->b_iocmd == BIO_READ)
3798 bwait(bp, PRIBIO, "biord");
3800 bwait(bp, PRIBIO, "biowr");
3801 if (bp->b_flags & B_EINTR) {
3802 bp->b_flags &= ~B_EINTR;
3805 if (bp->b_ioflags & BIO_ERROR) {
3806 return (bp->b_error ? bp->b_error : EIO);
3815 * Finish I/O on a buffer, optionally calling a completion function.
3816 * This is usually called from an interrupt so process blocking is
3819 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
3820 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
3821 * assuming B_INVAL is clear.
3823 * For the VMIO case, we set B_CACHE if the op was a read and no
3824 * read error occured, or if the op was a write. B_CACHE is never
3825 * set if the buffer is invalid or otherwise uncacheable.
3827 * biodone does not mess with B_INVAL, allowing the I/O routine or the
3828 * initiator to leave B_INVAL set to brelse the buffer out of existance
3829 * in the biodone routine.
3832 bufdone(struct buf *bp)
3834 struct bufobj *dropobj;
3835 void (*biodone)(struct buf *);
3837 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
3840 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
3841 BUF_ASSERT_HELD(bp);
3843 runningbufwakeup(bp);
3844 if (bp->b_iocmd == BIO_WRITE)
3845 dropobj = bp->b_bufobj;
3846 /* call optional completion function if requested */
3847 if (bp->b_iodone != NULL) {
3848 biodone = bp->b_iodone;
3849 bp->b_iodone = NULL;
3852 bufobj_wdrop(dropobj);
3859 bufobj_wdrop(dropobj);
3863 bufdone_finish(struct buf *bp)
3865 BUF_ASSERT_HELD(bp);
3867 if (!LIST_EMPTY(&bp->b_dep))
3870 if (bp->b_flags & B_VMIO) {
3872 * Set B_CACHE if the op was a normal read and no error
3873 * occured. B_CACHE is set for writes in the b*write()
3876 if (bp->b_iocmd == BIO_READ &&
3877 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
3878 !(bp->b_ioflags & BIO_ERROR))
3879 bp->b_flags |= B_CACHE;
3880 vfs_vmio_iodone(bp);
3884 * For asynchronous completions, release the buffer now. The brelse
3885 * will do a wakeup there if necessary - so no need to do a wakeup
3886 * here in the async case. The sync case always needs to do a wakeup.
3888 if (bp->b_flags & B_ASYNC) {
3889 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) ||
3890 (bp->b_ioflags & BIO_ERROR))
3899 * This routine is called in lieu of iodone in the case of
3900 * incomplete I/O. This keeps the busy status for pages
3904 vfs_unbusy_pages(struct buf *bp)
3910 runningbufwakeup(bp);
3911 if (!(bp->b_flags & B_VMIO))
3914 obj = bp->b_bufobj->bo_object;
3915 VM_OBJECT_WLOCK(obj);
3916 for (i = 0; i < bp->b_npages; i++) {
3918 if (m == bogus_page) {
3919 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
3921 panic("vfs_unbusy_pages: page missing\n");
3923 if (buf_mapped(bp)) {
3924 BUF_CHECK_MAPPED(bp);
3925 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
3926 bp->b_pages, bp->b_npages);
3928 BUF_CHECK_UNMAPPED(bp);
3930 vm_object_pip_subtract(obj, 1);
3933 vm_object_pip_wakeupn(obj, 0);
3934 VM_OBJECT_WUNLOCK(obj);
3938 * vfs_page_set_valid:
3940 * Set the valid bits in a page based on the supplied offset. The
3941 * range is restricted to the buffer's size.
3943 * This routine is typically called after a read completes.
3946 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3951 * Compute the end offset, eoff, such that [off, eoff) does not span a
3952 * page boundary and eoff is not greater than the end of the buffer.
3953 * The end of the buffer, in this case, is our file EOF, not the
3954 * allocation size of the buffer.
3956 eoff = (off + PAGE_SIZE) & ~(vm_ooffset_t)PAGE_MASK;
3957 if (eoff > bp->b_offset + bp->b_bcount)
3958 eoff = bp->b_offset + bp->b_bcount;
3961 * Set valid range. This is typically the entire buffer and thus the
3965 vm_page_set_valid_range(m, off & PAGE_MASK, eoff - off);
3969 * vfs_page_set_validclean:
3971 * Set the valid bits and clear the dirty bits in a page based on the
3972 * supplied offset. The range is restricted to the buffer's size.
3975 vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off, vm_page_t m)
3977 vm_ooffset_t soff, eoff;
3980 * Start and end offsets in buffer. eoff - soff may not cross a
3981 * page boundry or cross the end of the buffer. The end of the
3982 * buffer, in this case, is our file EOF, not the allocation size
3986 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3987 if (eoff > bp->b_offset + bp->b_bcount)
3988 eoff = bp->b_offset + bp->b_bcount;
3991 * Set valid range. This is typically the entire buffer and thus the
3995 vm_page_set_validclean(
3997 (vm_offset_t) (soff & PAGE_MASK),
3998 (vm_offset_t) (eoff - soff)
4004 * Ensure that all buffer pages are not exclusive busied. If any page is
4005 * exclusive busy, drain it.
4008 vfs_drain_busy_pages(struct buf *bp)
4013 VM_OBJECT_ASSERT_WLOCKED(bp->b_bufobj->bo_object);
4015 for (i = 0; i < bp->b_npages; i++) {
4017 if (vm_page_xbusied(m)) {
4018 for (; last_busied < i; last_busied++)
4019 vm_page_sbusy(bp->b_pages[last_busied]);
4020 while (vm_page_xbusied(m)) {
4022 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
4023 vm_page_busy_sleep(m, "vbpage");
4024 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
4028 for (i = 0; i < last_busied; i++)
4029 vm_page_sunbusy(bp->b_pages[i]);
4033 * This routine is called before a device strategy routine.
4034 * It is used to tell the VM system that paging I/O is in
4035 * progress, and treat the pages associated with the buffer
4036 * almost as being exclusive busy. Also the object paging_in_progress
4037 * flag is handled to make sure that the object doesn't become
4040 * Since I/O has not been initiated yet, certain buffer flags
4041 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
4042 * and should be ignored.
4045 vfs_busy_pages(struct buf *bp, int clear_modify)
4052 if (!(bp->b_flags & B_VMIO))
4055 obj = bp->b_bufobj->bo_object;
4056 foff = bp->b_offset;
4057 KASSERT(bp->b_offset != NOOFFSET,
4058 ("vfs_busy_pages: no buffer offset"));
4059 VM_OBJECT_WLOCK(obj);
4060 vfs_drain_busy_pages(bp);
4061 if (bp->b_bufsize != 0)
4062 vfs_setdirty_locked_object(bp);
4064 for (i = 0; i < bp->b_npages; i++) {
4067 if ((bp->b_flags & B_CLUSTER) == 0) {
4068 vm_object_pip_add(obj, 1);
4072 * When readying a buffer for a read ( i.e
4073 * clear_modify == 0 ), it is important to do
4074 * bogus_page replacement for valid pages in
4075 * partially instantiated buffers. Partially
4076 * instantiated buffers can, in turn, occur when
4077 * reconstituting a buffer from its VM backing store
4078 * base. We only have to do this if B_CACHE is
4079 * clear ( which causes the I/O to occur in the
4080 * first place ). The replacement prevents the read
4081 * I/O from overwriting potentially dirty VM-backed
4082 * pages. XXX bogus page replacement is, uh, bogus.
4083 * It may not work properly with small-block devices.
4084 * We need to find a better way.
4087 pmap_remove_write(m);
4088 vfs_page_set_validclean(bp, foff, m);
4089 } else if (m->valid == VM_PAGE_BITS_ALL &&
4090 (bp->b_flags & B_CACHE) == 0) {
4091 bp->b_pages[i] = bogus_page;
4094 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
4096 VM_OBJECT_WUNLOCK(obj);
4097 if (bogus && buf_mapped(bp)) {
4098 BUF_CHECK_MAPPED(bp);
4099 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
4100 bp->b_pages, bp->b_npages);
4105 * vfs_bio_set_valid:
4107 * Set the range within the buffer to valid. The range is
4108 * relative to the beginning of the buffer, b_offset. Note that
4109 * b_offset itself may be offset from the beginning of the first
4113 vfs_bio_set_valid(struct buf *bp, int base, int size)
4118 if (!(bp->b_flags & B_VMIO))
4122 * Fixup base to be relative to beginning of first page.
4123 * Set initial n to be the maximum number of bytes in the
4124 * first page that can be validated.
4126 base += (bp->b_offset & PAGE_MASK);
4127 n = PAGE_SIZE - (base & PAGE_MASK);
4129 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
4130 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
4134 vm_page_set_valid_range(m, base & PAGE_MASK, n);
4139 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
4145 * If the specified buffer is a non-VMIO buffer, clear the entire
4146 * buffer. If the specified buffer is a VMIO buffer, clear and
4147 * validate only the previously invalid portions of the buffer.
4148 * This routine essentially fakes an I/O, so we need to clear
4149 * BIO_ERROR and B_INVAL.
4151 * Note that while we only theoretically need to clear through b_bcount,
4152 * we go ahead and clear through b_bufsize.
4155 vfs_bio_clrbuf(struct buf *bp)
4157 int i, j, mask, sa, ea, slide;
4159 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
4163 bp->b_flags &= ~B_INVAL;
4164 bp->b_ioflags &= ~BIO_ERROR;
4165 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
4166 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
4167 (bp->b_offset & PAGE_MASK) == 0) {
4168 if (bp->b_pages[0] == bogus_page)
4170 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
4171 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[0]->object);
4172 if ((bp->b_pages[0]->valid & mask) == mask)
4174 if ((bp->b_pages[0]->valid & mask) == 0) {
4175 pmap_zero_page_area(bp->b_pages[0], 0, bp->b_bufsize);
4176 bp->b_pages[0]->valid |= mask;
4180 sa = bp->b_offset & PAGE_MASK;
4182 for (i = 0; i < bp->b_npages; i++, sa = 0) {
4183 slide = imin(slide + PAGE_SIZE, bp->b_offset + bp->b_bufsize);
4184 ea = slide & PAGE_MASK;
4187 if (bp->b_pages[i] == bogus_page)
4190 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
4191 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[i]->object);
4192 if ((bp->b_pages[i]->valid & mask) == mask)
4194 if ((bp->b_pages[i]->valid & mask) == 0)
4195 pmap_zero_page_area(bp->b_pages[i], sa, ea - sa);
4197 for (; sa < ea; sa += DEV_BSIZE, j++) {
4198 if ((bp->b_pages[i]->valid & (1 << j)) == 0) {
4199 pmap_zero_page_area(bp->b_pages[i],
4204 bp->b_pages[i]->valid |= mask;
4207 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
4212 vfs_bio_bzero_buf(struct buf *bp, int base, int size)
4217 if (buf_mapped(bp)) {
4218 BUF_CHECK_MAPPED(bp);
4219 bzero(bp->b_data + base, size);
4221 BUF_CHECK_UNMAPPED(bp);
4222 n = PAGE_SIZE - (base & PAGE_MASK);
4223 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
4227 pmap_zero_page_area(m, base & PAGE_MASK, n);
4236 * vm_hold_load_pages and vm_hold_free_pages get pages into
4237 * a buffers address space. The pages are anonymous and are
4238 * not associated with a file object.
4241 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
4247 BUF_CHECK_MAPPED(bp);
4249 to = round_page(to);
4250 from = round_page(from);
4251 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
4253 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
4256 * note: must allocate system pages since blocking here
4257 * could interfere with paging I/O, no matter which
4260 p = vm_page_alloc(NULL, 0, VM_ALLOC_SYSTEM | VM_ALLOC_NOOBJ |
4261 VM_ALLOC_WIRED | VM_ALLOC_COUNT((to - pg) >> PAGE_SHIFT));
4266 pmap_qenter(pg, &p, 1);
4267 bp->b_pages[index] = p;
4269 bp->b_npages = index;
4272 /* Return pages associated with this buf to the vm system */
4274 vm_hold_free_pages(struct buf *bp, int newbsize)
4278 int index, newnpages;
4280 BUF_CHECK_MAPPED(bp);
4282 from = round_page((vm_offset_t)bp->b_data + newbsize);
4283 newnpages = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
4284 if (bp->b_npages > newnpages)
4285 pmap_qremove(from, bp->b_npages - newnpages);
4286 for (index = newnpages; index < bp->b_npages; index++) {
4287 p = bp->b_pages[index];
4288 bp->b_pages[index] = NULL;
4289 if (vm_page_sbusied(p))
4290 printf("vm_hold_free_pages: blkno: %jd, lblkno: %jd\n",
4291 (intmax_t)bp->b_blkno, (intmax_t)bp->b_lblkno);
4294 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
4296 bp->b_npages = newnpages;
4300 * Map an IO request into kernel virtual address space.
4302 * All requests are (re)mapped into kernel VA space.
4303 * Notice that we use b_bufsize for the size of the buffer
4304 * to be mapped. b_bcount might be modified by the driver.
4306 * Note that even if the caller determines that the address space should
4307 * be valid, a race or a smaller-file mapped into a larger space may
4308 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
4309 * check the return value.
4311 * This function only works with pager buffers.
4314 vmapbuf(struct buf *bp, int mapbuf)
4319 if (bp->b_bufsize < 0)
4321 prot = VM_PROT_READ;
4322 if (bp->b_iocmd == BIO_READ)
4323 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
4324 if ((pidx = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
4325 (vm_offset_t)bp->b_data, bp->b_bufsize, prot, bp->b_pages,
4326 btoc(MAXPHYS))) < 0)
4328 bp->b_npages = pidx;
4329 bp->b_offset = ((vm_offset_t)bp->b_data) & PAGE_MASK;
4330 if (mapbuf || !unmapped_buf_allowed) {
4331 pmap_qenter((vm_offset_t)bp->b_kvabase, bp->b_pages, pidx);
4332 bp->b_data = bp->b_kvabase + bp->b_offset;
4334 bp->b_data = unmapped_buf;
4339 * Free the io map PTEs associated with this IO operation.
4340 * We also invalidate the TLB entries and restore the original b_addr.
4342 * This function only works with pager buffers.
4345 vunmapbuf(struct buf *bp)
4349 npages = bp->b_npages;
4351 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
4352 vm_page_unhold_pages(bp->b_pages, npages);
4354 bp->b_data = unmapped_buf;
4358 bdone(struct buf *bp)
4362 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4364 bp->b_flags |= B_DONE;
4370 bwait(struct buf *bp, u_char pri, const char *wchan)
4374 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4376 while ((bp->b_flags & B_DONE) == 0)
4377 msleep(bp, mtxp, pri, wchan, 0);
4382 bufsync(struct bufobj *bo, int waitfor)
4385 return (VOP_FSYNC(bo->__bo_vnode, waitfor, curthread));
4389 bufstrategy(struct bufobj *bo, struct buf *bp)
4395 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
4396 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
4397 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
4398 i = VOP_STRATEGY(vp, bp);
4399 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
4403 bufobj_wrefl(struct bufobj *bo)
4406 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
4407 ASSERT_BO_WLOCKED(bo);
4412 bufobj_wref(struct bufobj *bo)
4415 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
4422 bufobj_wdrop(struct bufobj *bo)
4425 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
4427 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
4428 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
4429 bo->bo_flag &= ~BO_WWAIT;
4430 wakeup(&bo->bo_numoutput);
4436 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
4440 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
4441 ASSERT_BO_WLOCKED(bo);
4443 while (bo->bo_numoutput) {
4444 bo->bo_flag |= BO_WWAIT;
4445 error = msleep(&bo->bo_numoutput, BO_LOCKPTR(bo),
4446 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
4454 bpin(struct buf *bp)
4458 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4465 bunpin(struct buf *bp)
4469 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4471 if (--bp->b_pin_count == 0)
4477 bunpin_wait(struct buf *bp)
4481 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4483 while (bp->b_pin_count > 0)
4484 msleep(bp, mtxp, PRIBIO, "bwunpin", 0);
4489 * Set bio_data or bio_ma for struct bio from the struct buf.
4492 bdata2bio(struct buf *bp, struct bio *bip)
4495 if (!buf_mapped(bp)) {
4496 KASSERT(unmapped_buf_allowed, ("unmapped"));
4497 bip->bio_ma = bp->b_pages;
4498 bip->bio_ma_n = bp->b_npages;
4499 bip->bio_data = unmapped_buf;
4500 bip->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
4501 bip->bio_flags |= BIO_UNMAPPED;
4502 KASSERT(round_page(bip->bio_ma_offset + bip->bio_length) /
4503 PAGE_SIZE == bp->b_npages,
4504 ("Buffer %p too short: %d %lld %d", bp, bip->bio_ma_offset,
4505 (long long)bip->bio_length, bip->bio_ma_n));
4507 bip->bio_data = bp->b_data;
4512 #include "opt_ddb.h"
4514 #include <ddb/ddb.h>
4516 /* DDB command to show buffer data */
4517 DB_SHOW_COMMAND(buffer, db_show_buffer)
4520 struct buf *bp = (struct buf *)addr;
4523 db_printf("usage: show buffer <addr>\n");
4527 db_printf("buf at %p\n", bp);
4528 db_printf("b_flags = 0x%b, b_xflags=0x%b, b_vflags=0x%b\n",
4529 (u_int)bp->b_flags, PRINT_BUF_FLAGS, (u_int)bp->b_xflags,
4530 PRINT_BUF_XFLAGS, (u_int)bp->b_vflags, PRINT_BUF_VFLAGS);
4532 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
4533 "b_bufobj = (%p), b_data = %p, b_blkno = %jd, b_lblkno = %jd, "
4535 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
4536 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno,
4537 (intmax_t)bp->b_lblkno, bp->b_dep.lh_first);
4538 db_printf("b_kvabase = %p, b_kvasize = %d\n",
4539 bp->b_kvabase, bp->b_kvasize);
4542 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
4543 for (i = 0; i < bp->b_npages; i++) {
4546 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
4547 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
4548 if ((i + 1) < bp->b_npages)
4554 BUF_LOCKPRINTINFO(bp);
4557 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
4562 for (i = 0; i < nbuf; i++) {
4564 if (BUF_ISLOCKED(bp)) {
4565 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4571 DB_SHOW_COMMAND(vnodebufs, db_show_vnodebufs)
4577 db_printf("usage: show vnodebufs <addr>\n");
4580 vp = (struct vnode *)addr;
4581 db_printf("Clean buffers:\n");
4582 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_clean.bv_hd, b_bobufs) {
4583 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4586 db_printf("Dirty buffers:\n");
4587 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
4588 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4593 DB_COMMAND(countfreebufs, db_coundfreebufs)
4596 int i, used = 0, nfree = 0;
4599 db_printf("usage: countfreebufs\n");
4603 for (i = 0; i < nbuf; i++) {
4605 if ((bp->b_flags & B_INFREECNT) != 0)
4611 db_printf("Counted %d free, %d used (%d tot)\n", nfree, used,
4613 db_printf("numfreebuffers is %d\n", numfreebuffers);