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>
67 #include <sys/sysctl.h>
68 #include <sys/sysproto.h>
70 #include <sys/vmmeter.h>
71 #include <sys/vnode.h>
72 #include <sys/watchdog.h>
73 #include <geom/geom.h>
75 #include <vm/vm_param.h>
76 #include <vm/vm_kern.h>
77 #include <vm/vm_pageout.h>
78 #include <vm/vm_page.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_extern.h>
81 #include <vm/vm_map.h>
82 #include <vm/swap_pager.h>
83 #include "opt_compat.h"
86 static MALLOC_DEFINE(M_BIOBUF, "biobuf", "BIO buffer");
88 struct bio_ops bioops; /* I/O operation notification */
90 struct buf_ops buf_ops_bio = {
91 .bop_name = "buf_ops_bio",
92 .bop_write = bufwrite,
93 .bop_strategy = bufstrategy,
95 .bop_bdflush = bufbdflush,
98 static struct buf *buf; /* buffer header pool */
99 extern struct buf *swbuf; /* Swap buffer header pool. */
100 caddr_t unmapped_buf;
102 /* Used below and for softdep flushing threads in ufs/ffs/ffs_softdep.c */
103 struct proc *bufdaemonproc;
104 struct proc *bufspacedaemonproc;
106 static int inmem(struct vnode *vp, daddr_t blkno);
107 static void vm_hold_free_pages(struct buf *bp, int newbsize);
108 static void vm_hold_load_pages(struct buf *bp, vm_offset_t from,
110 static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m);
111 static void vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off,
113 static void vfs_clean_pages_dirty_buf(struct buf *bp);
114 static void vfs_setdirty_locked_object(struct buf *bp);
115 static void vfs_vmio_invalidate(struct buf *bp);
116 static void vfs_vmio_truncate(struct buf *bp, int npages);
117 static void vfs_vmio_extend(struct buf *bp, int npages, int size);
118 static int vfs_bio_clcheck(struct vnode *vp, int size,
119 daddr_t lblkno, daddr_t blkno);
120 static int buf_flush(struct vnode *vp, int);
121 static int buf_recycle(bool);
122 static int buf_scan(bool);
123 static int flushbufqueues(struct vnode *, int, int);
124 static void buf_daemon(void);
125 static void bremfreel(struct buf *bp);
126 static __inline void bd_wakeup(void);
127 static int sysctl_runningspace(SYSCTL_HANDLER_ARGS);
128 static void bufkva_reclaim(vmem_t *, int);
129 static void bufkva_free(struct buf *);
130 static int buf_import(void *, void **, int, int);
131 static void buf_release(void *, void **, int);
133 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
134 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
135 static int sysctl_bufspace(SYSCTL_HANDLER_ARGS);
138 int vmiodirenable = TRUE;
139 SYSCTL_INT(_vfs, OID_AUTO, vmiodirenable, CTLFLAG_RW, &vmiodirenable, 0,
140 "Use the VM system for directory writes");
141 long runningbufspace;
142 SYSCTL_LONG(_vfs, OID_AUTO, runningbufspace, CTLFLAG_RD, &runningbufspace, 0,
143 "Amount of presently outstanding async buffer io");
144 static long bufspace;
145 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
146 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
147 SYSCTL_PROC(_vfs, OID_AUTO, bufspace, CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RD,
148 &bufspace, 0, sysctl_bufspace, "L", "Virtual memory used for buffers");
150 SYSCTL_LONG(_vfs, OID_AUTO, bufspace, CTLFLAG_RD, &bufspace, 0,
151 "Physical memory used for buffers");
153 static long bufkvaspace;
154 SYSCTL_LONG(_vfs, OID_AUTO, bufkvaspace, CTLFLAG_RD, &bufkvaspace, 0,
155 "Kernel virtual memory used for buffers");
156 static long maxbufspace;
157 SYSCTL_LONG(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RW, &maxbufspace, 0,
158 "Maximum allowed value of bufspace (including metadata)");
159 static long bufmallocspace;
160 SYSCTL_LONG(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0,
161 "Amount of malloced memory for buffers");
162 static long maxbufmallocspace;
163 SYSCTL_LONG(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace,
164 0, "Maximum amount of malloced memory for buffers");
165 static long lobufspace;
166 SYSCTL_LONG(_vfs, OID_AUTO, lobufspace, CTLFLAG_RW, &lobufspace, 0,
167 "Minimum amount of buffers we want to have");
169 SYSCTL_LONG(_vfs, OID_AUTO, hibufspace, CTLFLAG_RW, &hibufspace, 0,
170 "Maximum allowed value of bufspace (excluding metadata)");
172 SYSCTL_LONG(_vfs, OID_AUTO, bufspacethresh, CTLFLAG_RW, &bufspacethresh,
173 0, "Bufspace consumed before waking the daemon to free some");
174 static int buffreekvacnt;
175 SYSCTL_INT(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt, 0,
176 "Number of times we have freed the KVA space from some buffer");
177 static int bufdefragcnt;
178 SYSCTL_INT(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt, 0,
179 "Number of times we have had to repeat buffer allocation to defragment");
180 static long lorunningspace;
181 SYSCTL_PROC(_vfs, OID_AUTO, lorunningspace, CTLTYPE_LONG | CTLFLAG_MPSAFE |
182 CTLFLAG_RW, &lorunningspace, 0, sysctl_runningspace, "L",
183 "Minimum preferred space used for in-progress I/O");
184 static long hirunningspace;
185 SYSCTL_PROC(_vfs, OID_AUTO, hirunningspace, CTLTYPE_LONG | CTLFLAG_MPSAFE |
186 CTLFLAG_RW, &hirunningspace, 0, sysctl_runningspace, "L",
187 "Maximum amount of space to use for in-progress I/O");
188 int dirtybufferflushes;
189 SYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes,
190 0, "Number of bdwrite to bawrite conversions to limit dirty buffers");
192 SYSCTL_INT(_vfs, OID_AUTO, bdwriteskip, CTLFLAG_RW, &bdwriteskip,
193 0, "Number of buffers supplied to bdwrite with snapshot deadlock risk");
194 int altbufferflushes;
195 SYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes,
196 0, "Number of fsync flushes to limit dirty buffers");
197 static int recursiveflushes;
198 SYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes,
199 0, "Number of flushes skipped due to being recursive");
200 static int numdirtybuffers;
201 SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0,
202 "Number of buffers that are dirty (has unwritten changes) at the moment");
203 static int lodirtybuffers;
204 SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0,
205 "How many buffers we want to have free before bufdaemon can sleep");
206 static int hidirtybuffers;
207 SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0,
208 "When the number of dirty buffers is considered severe");
210 SYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh,
211 0, "Number of bdwrite to bawrite conversions to clear dirty buffers");
212 static int numfreebuffers;
213 SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0,
214 "Number of free buffers");
215 static int lofreebuffers;
216 SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0,
217 "Target number of free buffers");
218 static int hifreebuffers;
219 SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0,
220 "Threshold for clean buffer recycling");
221 static int getnewbufcalls;
222 SYSCTL_INT(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RW, &getnewbufcalls, 0,
223 "Number of calls to getnewbuf");
224 static int getnewbufrestarts;
225 SYSCTL_INT(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RW, &getnewbufrestarts, 0,
226 "Number of times getnewbuf has had to restart a buffer aquisition");
227 static int mappingrestarts;
228 SYSCTL_INT(_vfs, OID_AUTO, mappingrestarts, CTLFLAG_RW, &mappingrestarts, 0,
229 "Number of times getblk has had to restart a buffer mapping for "
231 static int numbufallocfails;
232 SYSCTL_INT(_vfs, OID_AUTO, numbufallocfails, CTLFLAG_RW, &numbufallocfails, 0,
233 "Number of times buffer allocations failed");
234 static int flushbufqtarget = 100;
235 SYSCTL_INT(_vfs, OID_AUTO, flushbufqtarget, CTLFLAG_RW, &flushbufqtarget, 0,
236 "Amount of work to do in flushbufqueues when helping bufdaemon");
237 static long notbufdflushes;
238 SYSCTL_LONG(_vfs, OID_AUTO, notbufdflushes, CTLFLAG_RD, ¬bufdflushes, 0,
239 "Number of dirty buffer flushes done by the bufdaemon helpers");
240 static long barrierwrites;
241 SYSCTL_LONG(_vfs, OID_AUTO, barrierwrites, CTLFLAG_RW, &barrierwrites, 0,
242 "Number of barrier writes");
243 SYSCTL_INT(_vfs, OID_AUTO, unmapped_buf_allowed, CTLFLAG_RD,
244 &unmapped_buf_allowed, 0,
245 "Permit the use of the unmapped i/o");
248 * This lock synchronizes access to bd_request.
250 static struct mtx_padalign bdlock;
253 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
254 * waitrunningbufspace().
256 static struct mtx_padalign rbreqlock;
259 * Lock that protects needsbuffer and the sleeps/wakeups surrounding it.
261 static struct rwlock_padalign nblock;
264 * Lock that protects bdirtywait.
266 static struct mtx_padalign bdirtylock;
269 * Wakeup point for bufdaemon, as well as indicator of whether it is already
270 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it
273 static int bd_request;
276 * Request/wakeup point for the bufspace daemon.
278 static int bufspace_request;
281 * Request for the buf daemon to write more buffers than is indicated by
282 * lodirtybuf. This may be necessary to push out excess dependencies or
283 * defragment the address space where a simple count of the number of dirty
284 * buffers is insufficient to characterize the demand for flushing them.
286 static int bd_speedupreq;
289 * bogus page -- for I/O to/from partially complete buffers
290 * this is a temporary solution to the problem, but it is not
291 * really that bad. it would be better to split the buffer
292 * for input in the case of buffers partially already in memory,
293 * but the code is intricate enough already.
295 vm_page_t bogus_page;
298 * Synchronization (sleep/wakeup) variable for active buffer space requests.
299 * Set when wait starts, cleared prior to wakeup().
300 * Used in runningbufwakeup() and waitrunningbufspace().
302 static int runningbufreq;
305 * Synchronization (sleep/wakeup) variable for buffer requests.
306 * Can contain the VFS_BIO_NEED flags defined below; setting/clearing is done
308 * Used in numdirtywakeup(), bufspace_wakeup(), bwillwrite(),
309 * getnewbuf(), and getblk().
311 static volatile int needsbuffer;
314 * Synchronization for bwillwrite() waiters.
316 static int bdirtywait;
319 * Definitions for the buffer free lists.
321 #define QUEUE_NONE 0 /* on no queue */
322 #define QUEUE_EMPTY 1 /* empty buffer headers */
323 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
324 #define QUEUE_CLEAN 3 /* non-B_DELWRI buffers */
325 #define QUEUE_SENTINEL 1024 /* not an queue index, but mark for sentinel */
327 /* Maximum number of clean buffer queues. */
328 #define CLEAN_QUEUES 16
330 /* Configured number of clean queues. */
331 static int clean_queues;
333 /* Maximum number of buffer queues. */
334 #define BUFFER_QUEUES (QUEUE_CLEAN + CLEAN_QUEUES)
336 /* Queues for free buffers with various properties */
337 static TAILQ_HEAD(bqueues, buf) bufqueues[BUFFER_QUEUES] = { { 0 } };
339 static int bq_len[BUFFER_QUEUES];
343 * Lock for each bufqueue
345 static struct mtx_padalign bqlocks[BUFFER_QUEUES];
348 * per-cpu empty buffer cache.
353 * Single global constant for BUF_WMESG, to avoid getting multiple references.
354 * buf_wmesg is referred from macros.
356 const char *buf_wmesg = BUF_WMESG;
359 sysctl_runningspace(SYSCTL_HANDLER_ARGS)
364 value = *(long *)arg1;
365 error = sysctl_handle_long(oidp, &value, 0, req);
366 if (error != 0 || req->newptr == NULL)
368 mtx_lock(&rbreqlock);
369 if (arg1 == &hirunningspace) {
370 if (value < lorunningspace)
373 hirunningspace = value;
375 KASSERT(arg1 == &lorunningspace,
376 ("%s: unknown arg1", __func__));
377 if (value > hirunningspace)
380 lorunningspace = value;
382 mtx_unlock(&rbreqlock);
386 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
387 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
389 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
394 if (sizeof(int) == sizeof(long) || req->oldlen >= sizeof(long))
395 return (sysctl_handle_long(oidp, arg1, arg2, req));
396 lvalue = *(long *)arg1;
397 if (lvalue > INT_MAX)
398 /* On overflow, still write out a long to trigger ENOMEM. */
399 return (sysctl_handle_long(oidp, &lvalue, 0, req));
401 return (sysctl_handle_int(oidp, &ivalue, 0, req));
410 return ((atomic_fetchadd_int(&nextq, 1) % clean_queues) + QUEUE_CLEAN);
414 bqisclean(int qindex)
417 return (qindex >= QUEUE_CLEAN && qindex < QUEUE_CLEAN + CLEAN_QUEUES);
423 * Return the appropriate queue lock based on the index.
425 static inline struct mtx *
429 return (struct mtx *)&bqlocks[qindex];
435 * Wakeup any bwillwrite() waiters.
440 mtx_lock(&bdirtylock);
445 mtx_unlock(&bdirtylock);
451 * Decrement the numdirtybuffers count by one and wakeup any
452 * threads blocked in bwillwrite().
458 if (atomic_fetchadd_int(&numdirtybuffers, -1) ==
459 (lodirtybuffers + hidirtybuffers) / 2)
466 * Increment the numdirtybuffers count by one and wakeup the buf
474 * Only do the wakeup once as we cross the boundary. The
475 * buf daemon will keep running until the condition clears.
477 if (atomic_fetchadd_int(&numdirtybuffers, 1) ==
478 (lodirtybuffers + hidirtybuffers) / 2)
485 * Called when buffer space is potentially available for recovery.
486 * getnewbuf() will block on this flag when it is unable to free
487 * sufficient buffer space. Buffer space becomes recoverable when
488 * bp's get placed back in the queues.
491 bufspace_wakeup(void)
495 * If someone is waiting for bufspace, wake them up.
497 * Since needsbuffer is set prior to doing an additional queue
498 * scan it is safe to check for the flag prior to acquiring the
499 * lock. The thread that is preparing to scan again before
500 * blocking would discover the buf we released.
504 if (atomic_cmpset_int(&needsbuffer, 1, 0) == 1)
505 wakeup(__DEVOLATILE(void *, &needsbuffer));
511 * bufspace_daemonwakeup:
513 * Wakeup the daemon responsible for freeing clean bufs.
516 bufspace_daemonwakeup(void)
519 if (bufspace_request == 0) {
520 bufspace_request = 1;
521 wakeup(&bufspace_request);
529 * Adjust the reported bufspace for a KVA managed buffer, possibly
530 * waking any waiters.
533 bufspace_adjust(struct buf *bp, int bufsize)
538 KASSERT((bp->b_flags & B_MALLOC) == 0,
539 ("bufspace_adjust: malloc buf %p", bp));
540 diff = bufsize - bp->b_bufsize;
542 atomic_subtract_long(&bufspace, -diff);
545 space = atomic_fetchadd_long(&bufspace, diff);
546 /* Wake up the daemon on the transition. */
547 if (space < bufspacethresh && space + diff >= bufspacethresh)
548 bufspace_daemonwakeup();
550 bp->b_bufsize = bufsize;
556 * Reserve bufspace before calling allocbuf(). metadata has a
557 * different space limit than data.
560 bufspace_reserve(int size, bool metadata)
571 if (space + size > limit)
573 } while (atomic_cmpset_long(&bufspace, space, space + size) == 0);
575 /* Wake up the daemon on the transition. */
576 if (space < bufspacethresh && space + size >= bufspacethresh)
577 bufspace_daemonwakeup();
585 * Release reserved bufspace after bufspace_adjust() has consumed it.
588 bufspace_release(int size)
590 atomic_subtract_long(&bufspace, size);
597 * Wait for bufspace, acting as the buf daemon if a locked vnode is
598 * supplied. needsbuffer must be set in a safe fashion prior to
599 * polling for space. The operation must be re-tried on return.
602 bufspace_wait(struct vnode *vp, int gbflags, int slpflag, int slptimeo)
605 int error, fl, norunbuf;
607 if ((gbflags & GB_NOWAIT_BD) != 0)
612 while (needsbuffer != 0) {
613 if (vp != NULL && vp->v_type != VCHR &&
614 (td->td_pflags & TDP_BUFNEED) == 0) {
617 * getblk() is called with a vnode locked, and
618 * some majority of the dirty buffers may as
619 * well belong to the vnode. Flushing the
620 * buffers there would make a progress that
621 * cannot be achieved by the buf_daemon, that
622 * cannot lock the vnode.
624 norunbuf = ~(TDP_BUFNEED | TDP_NORUNNINGBUF) |
625 (td->td_pflags & TDP_NORUNNINGBUF);
628 * Play bufdaemon. The getnewbuf() function
629 * may be called while the thread owns lock
630 * for another dirty buffer for the same
631 * vnode, which makes it impossible to use
632 * VOP_FSYNC() there, due to the buffer lock
635 td->td_pflags |= TDP_BUFNEED | TDP_NORUNNINGBUF;
636 fl = buf_flush(vp, flushbufqtarget);
637 td->td_pflags &= norunbuf;
641 if (needsbuffer == 0)
644 error = rw_sleep(__DEVOLATILE(void *, &needsbuffer), &nblock,
645 (PRIBIO + 4) | slpflag, "newbuf", slptimeo);
656 * buffer space management daemon. Tries to maintain some marginal
657 * amount of free buffer space so that requesting processes neither
658 * block nor work to reclaim buffers.
661 bufspace_daemon(void)
664 kproc_suspend_check(bufspacedaemonproc);
667 * Free buffers from the clean queue until we meet our
670 * Theory of operation: The buffer cache is most efficient
671 * when some free buffer headers and space are always
672 * available to getnewbuf(). This daemon attempts to prevent
673 * the excessive blocking and synchronization associated
674 * with shortfall. It goes through three phases according
677 * 1) The daemon wakes up voluntarily once per-second
678 * during idle periods when the counters are below
679 * the wakeup thresholds (bufspacethresh, lofreebuffers).
681 * 2) The daemon wakes up as we cross the thresholds
682 * ahead of any potential blocking. This may bounce
683 * slightly according to the rate of consumption and
686 * 3) The daemon and consumers are starved for working
687 * clean buffers. This is the 'bufspace' sleep below
688 * which will inefficiently trade bufs with bqrelse
689 * until we return to condition 2.
691 while (bufspace > lobufspace ||
692 numfreebuffers < hifreebuffers) {
693 if (buf_recycle(false) != 0) {
694 atomic_set_int(&needsbuffer, 1);
695 if (buf_recycle(false) != 0) {
698 rw_sleep(__DEVOLATILE(void *,
699 &needsbuffer), &nblock,
700 PRIBIO|PDROP, "bufspace",
710 * Re-check our limits under the exclusive nblock.
713 if (bufspace < bufspacethresh &&
714 numfreebuffers > lofreebuffers) {
715 bufspace_request = 0;
716 rw_sleep(&bufspace_request, &nblock, PRIBIO|PDROP,
723 static struct kproc_desc bufspace_kp = {
728 SYSINIT(bufspacedaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start,
734 * Adjust the reported bufspace for a malloc managed buffer, possibly
735 * waking any waiters.
738 bufmallocadjust(struct buf *bp, int bufsize)
742 KASSERT((bp->b_flags & B_MALLOC) != 0,
743 ("bufmallocadjust: non-malloc buf %p", bp));
744 diff = bufsize - bp->b_bufsize;
746 atomic_subtract_long(&bufmallocspace, -diff);
748 atomic_add_long(&bufmallocspace, diff);
749 bp->b_bufsize = bufsize;
755 * Wake up processes that are waiting on asynchronous writes to fall
756 * below lorunningspace.
762 mtx_lock(&rbreqlock);
765 wakeup(&runningbufreq);
767 mtx_unlock(&rbreqlock);
773 * Decrement the outstanding write count according.
776 runningbufwakeup(struct buf *bp)
780 bspace = bp->b_runningbufspace;
783 space = atomic_fetchadd_long(&runningbufspace, -bspace);
784 KASSERT(space >= bspace, ("runningbufspace underflow %ld %ld",
786 bp->b_runningbufspace = 0;
788 * Only acquire the lock and wakeup on the transition from exceeding
789 * the threshold to falling below it.
791 if (space < lorunningspace)
793 if (space - bspace > lorunningspace)
799 * waitrunningbufspace()
801 * runningbufspace is a measure of the amount of I/O currently
802 * running. This routine is used in async-write situations to
803 * prevent creating huge backups of pending writes to a device.
804 * Only asynchronous writes are governed by this function.
806 * This does NOT turn an async write into a sync write. It waits
807 * for earlier writes to complete and generally returns before the
808 * caller's write has reached the device.
811 waitrunningbufspace(void)
814 mtx_lock(&rbreqlock);
815 while (runningbufspace > hirunningspace) {
817 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
819 mtx_unlock(&rbreqlock);
824 * vfs_buf_test_cache:
826 * Called when a buffer is extended. This function clears the B_CACHE
827 * bit if the newly extended portion of the buffer does not contain
831 vfs_buf_test_cache(struct buf *bp, vm_ooffset_t foff, vm_offset_t off,
832 vm_offset_t size, vm_page_t m)
835 VM_OBJECT_ASSERT_LOCKED(m->object);
836 if (bp->b_flags & B_CACHE) {
837 int base = (foff + off) & PAGE_MASK;
838 if (vm_page_is_valid(m, base, size) == 0)
839 bp->b_flags &= ~B_CACHE;
843 /* Wake up the buffer daemon if necessary */
849 if (bd_request == 0) {
857 * bd_speedup - speedup the buffer cache flushing code
866 if (bd_speedupreq == 0 || bd_request == 0)
876 #define NSWBUF_MIN 16
880 #define TRANSIENT_DENOM 5
882 #define TRANSIENT_DENOM 10
886 * Calculating buffer cache scaling values and reserve space for buffer
887 * headers. This is called during low level kernel initialization and
888 * may be called more then once. We CANNOT write to the memory area
889 * being reserved at this time.
892 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
895 long maxbuf, maxbuf_sz, buf_sz, biotmap_sz;
898 * physmem_est is in pages. Convert it to kilobytes (assumes
899 * PAGE_SIZE is >= 1K)
901 physmem_est = physmem_est * (PAGE_SIZE / 1024);
904 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
905 * For the first 64MB of ram nominally allocate sufficient buffers to
906 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
907 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing
908 * the buffer cache we limit the eventual kva reservation to
911 * factor represents the 1/4 x ram conversion.
914 int factor = 4 * BKVASIZE / 1024;
917 if (physmem_est > 4096)
918 nbuf += min((physmem_est - 4096) / factor,
920 if (physmem_est > 65536)
921 nbuf += min((physmem_est - 65536) * 2 / (factor * 5),
922 32 * 1024 * 1024 / (factor * 5));
924 if (maxbcache && nbuf > maxbcache / BKVASIZE)
925 nbuf = maxbcache / BKVASIZE;
930 /* XXX Avoid unsigned long overflows later on with maxbufspace. */
931 maxbuf = (LONG_MAX / 3) / BKVASIZE;
934 printf("Warning: nbufs lowered from %d to %ld\n", nbuf,
940 * Ideal allocation size for the transient bio submap is 10%
941 * of the maximal space buffer map. This roughly corresponds
942 * to the amount of the buffer mapped for typical UFS load.
944 * Clip the buffer map to reserve space for the transient
945 * BIOs, if its extent is bigger than 90% (80% on i386) of the
946 * maximum buffer map extent on the platform.
948 * The fall-back to the maxbuf in case of maxbcache unset,
949 * allows to not trim the buffer KVA for the architectures
950 * with ample KVA space.
952 if (bio_transient_maxcnt == 0 && unmapped_buf_allowed) {
953 maxbuf_sz = maxbcache != 0 ? maxbcache : maxbuf * BKVASIZE;
954 buf_sz = (long)nbuf * BKVASIZE;
955 if (buf_sz < maxbuf_sz / TRANSIENT_DENOM *
956 (TRANSIENT_DENOM - 1)) {
958 * There is more KVA than memory. Do not
959 * adjust buffer map size, and assign the rest
960 * of maxbuf to transient map.
962 biotmap_sz = maxbuf_sz - buf_sz;
965 * Buffer map spans all KVA we could afford on
966 * this platform. Give 10% (20% on i386) of
967 * the buffer map to the transient bio map.
969 biotmap_sz = buf_sz / TRANSIENT_DENOM;
970 buf_sz -= biotmap_sz;
972 if (biotmap_sz / INT_MAX > MAXPHYS)
973 bio_transient_maxcnt = INT_MAX;
975 bio_transient_maxcnt = biotmap_sz / MAXPHYS;
977 * Artifically limit to 1024 simultaneous in-flight I/Os
978 * using the transient mapping.
980 if (bio_transient_maxcnt > 1024)
981 bio_transient_maxcnt = 1024;
983 nbuf = buf_sz / BKVASIZE;
987 * swbufs are used as temporary holders for I/O, such as paging I/O.
988 * We have no less then 16 and no more then 256.
990 nswbuf = min(nbuf / 4, 256);
991 TUNABLE_INT_FETCH("kern.nswbuf", &nswbuf);
992 if (nswbuf < NSWBUF_MIN)
996 * Reserve space for the buffer cache buffers
999 v = (caddr_t)(swbuf + nswbuf);
1001 v = (caddr_t)(buf + nbuf);
1006 /* Initialize the buffer subsystem. Called before use of any buffers. */
1013 CTASSERT(MAXBCACHEBUF >= MAXBSIZE);
1014 mtx_init(&bqlocks[QUEUE_DIRTY], "bufq dirty lock", NULL, MTX_DEF);
1015 mtx_init(&bqlocks[QUEUE_EMPTY], "bufq empty lock", NULL, MTX_DEF);
1016 for (i = QUEUE_CLEAN; i < QUEUE_CLEAN + CLEAN_QUEUES; i++)
1017 mtx_init(&bqlocks[i], "bufq clean lock", NULL, MTX_DEF);
1018 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
1019 rw_init(&nblock, "needsbuffer lock");
1020 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
1021 mtx_init(&bdirtylock, "dirty buf lock", NULL, MTX_DEF);
1023 /* next, make a null set of free lists */
1024 for (i = 0; i < BUFFER_QUEUES; i++)
1025 TAILQ_INIT(&bufqueues[i]);
1027 unmapped_buf = (caddr_t)kva_alloc(MAXPHYS);
1029 /* finally, initialize each buffer header and stick on empty q */
1030 for (i = 0; i < nbuf; i++) {
1032 bzero(bp, sizeof *bp);
1033 bp->b_flags = B_INVAL;
1034 bp->b_rcred = NOCRED;
1035 bp->b_wcred = NOCRED;
1036 bp->b_qindex = QUEUE_EMPTY;
1038 bp->b_data = bp->b_kvabase = unmapped_buf;
1039 LIST_INIT(&bp->b_dep);
1041 TAILQ_INSERT_TAIL(&bufqueues[QUEUE_EMPTY], bp, b_freelist);
1043 bq_len[QUEUE_EMPTY]++;
1048 * maxbufspace is the absolute maximum amount of buffer space we are
1049 * allowed to reserve in KVM and in real terms. The absolute maximum
1050 * is nominally used by metadata. hibufspace is the nominal maximum
1051 * used by most other requests. The differential is required to
1052 * ensure that metadata deadlocks don't occur.
1054 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
1055 * this may result in KVM fragmentation which is not handled optimally
1056 * by the system. XXX This is less true with vmem. We could use
1059 maxbufspace = (long)nbuf * BKVASIZE;
1060 hibufspace = lmax(3 * maxbufspace / 4, maxbufspace - MAXBCACHEBUF * 10);
1061 lobufspace = (hibufspace / 20) * 19; /* 95% */
1062 bufspacethresh = lobufspace + (hibufspace - lobufspace) / 2;
1065 * Note: The 16 MiB upper limit for hirunningspace was chosen
1066 * arbitrarily and may need further tuning. It corresponds to
1067 * 128 outstanding write IO requests (if IO size is 128 KiB),
1068 * which fits with many RAID controllers' tagged queuing limits.
1069 * The lower 1 MiB limit is the historical upper limit for
1072 hirunningspace = lmax(lmin(roundup(hibufspace / 64, MAXBCACHEBUF),
1073 16 * 1024 * 1024), 1024 * 1024);
1074 lorunningspace = roundup((hirunningspace * 2) / 3, MAXBCACHEBUF);
1077 * Limit the amount of malloc memory since it is wired permanently into
1078 * the kernel space. Even though this is accounted for in the buffer
1079 * allocation, we don't want the malloced region to grow uncontrolled.
1080 * The malloc scheme improves memory utilization significantly on
1081 * average (small) directories.
1083 maxbufmallocspace = hibufspace / 20;
1086 * Reduce the chance of a deadlock occuring by limiting the number
1087 * of delayed-write dirty buffers we allow to stack up.
1089 hidirtybuffers = nbuf / 4 + 20;
1090 dirtybufthresh = hidirtybuffers * 9 / 10;
1091 numdirtybuffers = 0;
1093 * To support extreme low-memory systems, make sure hidirtybuffers
1094 * cannot eat up all available buffer space. This occurs when our
1095 * minimum cannot be met. We try to size hidirtybuffers to 3/4 our
1096 * buffer space assuming BKVASIZE'd buffers.
1098 while ((long)hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
1099 hidirtybuffers >>= 1;
1101 lodirtybuffers = hidirtybuffers / 2;
1104 * lofreebuffers should be sufficient to avoid stalling waiting on
1105 * buf headers under heavy utilization. The bufs in per-cpu caches
1106 * are counted as free but will be unavailable to threads executing
1109 * hifreebuffers is the free target for the bufspace daemon. This
1110 * should be set appropriately to limit work per-iteration.
1112 lofreebuffers = MIN((nbuf / 25) + (20 * mp_ncpus), 128 * mp_ncpus);
1113 hifreebuffers = (3 * lofreebuffers) / 2;
1114 numfreebuffers = nbuf;
1116 bogus_page = vm_page_alloc(NULL, 0, VM_ALLOC_NOOBJ |
1117 VM_ALLOC_NORMAL | VM_ALLOC_WIRED);
1119 /* Setup the kva and free list allocators. */
1120 vmem_set_reclaim(buffer_arena, bufkva_reclaim);
1121 buf_zone = uma_zcache_create("buf free cache", sizeof(struct buf),
1122 NULL, NULL, NULL, NULL, buf_import, buf_release, NULL, 0);
1125 * Size the clean queue according to the amount of buffer space.
1126 * One queue per-256mb up to the max. More queues gives better
1127 * concurrency but less accurate LRU.
1129 clean_queues = MIN(howmany(maxbufspace, 256*1024*1024), CLEAN_QUEUES);
1135 vfs_buf_check_mapped(struct buf *bp)
1138 KASSERT(bp->b_kvabase != unmapped_buf,
1139 ("mapped buf: b_kvabase was not updated %p", bp));
1140 KASSERT(bp->b_data != unmapped_buf,
1141 ("mapped buf: b_data was not updated %p", bp));
1142 KASSERT(bp->b_data < unmapped_buf || bp->b_data >= unmapped_buf +
1143 MAXPHYS, ("b_data + b_offset unmapped %p", bp));
1147 vfs_buf_check_unmapped(struct buf *bp)
1150 KASSERT(bp->b_data == unmapped_buf,
1151 ("unmapped buf: corrupted b_data %p", bp));
1154 #define BUF_CHECK_MAPPED(bp) vfs_buf_check_mapped(bp)
1155 #define BUF_CHECK_UNMAPPED(bp) vfs_buf_check_unmapped(bp)
1157 #define BUF_CHECK_MAPPED(bp) do {} while (0)
1158 #define BUF_CHECK_UNMAPPED(bp) do {} while (0)
1162 isbufbusy(struct buf *bp)
1164 if (((bp->b_flags & (B_INVAL | B_PERSISTENT)) == 0 &&
1165 BUF_ISLOCKED(bp)) ||
1166 ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
1172 * Shutdown the system cleanly to prepare for reboot, halt, or power off.
1175 bufshutdown(int show_busybufs)
1177 static int first_buf_printf = 1;
1179 int iter, nbusy, pbusy;
1185 * Sync filesystems for shutdown
1187 wdog_kern_pat(WD_LASTVAL);
1188 sys_sync(curthread, NULL);
1191 * With soft updates, some buffers that are
1192 * written will be remarked as dirty until other
1193 * buffers are written.
1195 for (iter = pbusy = 0; iter < 20; iter++) {
1197 for (bp = &buf[nbuf]; --bp >= buf; )
1201 if (first_buf_printf)
1202 printf("All buffers synced.");
1205 if (first_buf_printf) {
1206 printf("Syncing disks, buffers remaining... ");
1207 first_buf_printf = 0;
1209 printf("%d ", nbusy);
1214 wdog_kern_pat(WD_LASTVAL);
1215 sys_sync(curthread, NULL);
1219 * Drop Giant and spin for a while to allow
1220 * interrupt threads to run.
1223 DELAY(50000 * iter);
1227 * Drop Giant and context switch several times to
1228 * allow interrupt threads to run.
1231 for (subiter = 0; subiter < 50 * iter; subiter++) {
1232 thread_lock(curthread);
1233 mi_switch(SW_VOL, NULL);
1234 thread_unlock(curthread);
1242 * Count only busy local buffers to prevent forcing
1243 * a fsck if we're just a client of a wedged NFS server
1246 for (bp = &buf[nbuf]; --bp >= buf; ) {
1247 if (isbufbusy(bp)) {
1249 /* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */
1250 if (bp->b_dev == NULL) {
1251 TAILQ_REMOVE(&mountlist,
1252 bp->b_vp->v_mount, mnt_list);
1257 if (show_busybufs > 0) {
1259 "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
1260 nbusy, bp, bp->b_vp, bp->b_flags,
1261 (intmax_t)bp->b_blkno,
1262 (intmax_t)bp->b_lblkno);
1263 BUF_LOCKPRINTINFO(bp);
1264 if (show_busybufs > 1)
1272 * Failed to sync all blocks. Indicate this and don't
1273 * unmount filesystems (thus forcing an fsck on reboot).
1275 printf("Giving up on %d buffers\n", nbusy);
1276 DELAY(5000000); /* 5 seconds */
1278 if (!first_buf_printf)
1279 printf("Final sync complete\n");
1281 * Unmount filesystems
1287 DELAY(100000); /* wait for console output to finish */
1291 bpmap_qenter(struct buf *bp)
1294 BUF_CHECK_MAPPED(bp);
1297 * bp->b_data is relative to bp->b_offset, but
1298 * bp->b_offset may be offset into the first page.
1300 bp->b_data = (caddr_t)trunc_page((vm_offset_t)bp->b_data);
1301 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1302 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
1303 (vm_offset_t)(bp->b_offset & PAGE_MASK));
1309 * Insert the buffer into the appropriate free list.
1312 binsfree(struct buf *bp, int qindex)
1314 struct mtx *olock, *nlock;
1316 if (qindex != QUEUE_EMPTY) {
1317 BUF_ASSERT_XLOCKED(bp);
1321 * Stick to the same clean queue for the lifetime of the buf to
1322 * limit locking below. Otherwise pick ont sequentially.
1324 if (qindex == QUEUE_CLEAN) {
1325 if (bqisclean(bp->b_qindex))
1326 qindex = bp->b_qindex;
1328 qindex = bqcleanq();
1332 * Handle delayed bremfree() processing.
1334 nlock = bqlock(qindex);
1335 if (bp->b_flags & B_REMFREE) {
1336 olock = bqlock(bp->b_qindex);
1339 if (olock != nlock) {
1346 if (bp->b_qindex != QUEUE_NONE)
1347 panic("binsfree: free buffer onto another queue???");
1349 bp->b_qindex = qindex;
1350 if (bp->b_flags & B_AGE)
1351 TAILQ_INSERT_HEAD(&bufqueues[bp->b_qindex], bp, b_freelist);
1353 TAILQ_INSERT_TAIL(&bufqueues[bp->b_qindex], bp, b_freelist);
1355 bq_len[bp->b_qindex]++;
1363 * Free a buffer to the buf zone once it no longer has valid contents.
1366 buf_free(struct buf *bp)
1369 if (bp->b_flags & B_REMFREE)
1371 if (bp->b_vflags & BV_BKGRDINPROG)
1372 panic("losing buffer 1");
1373 if (bp->b_rcred != NOCRED) {
1374 crfree(bp->b_rcred);
1375 bp->b_rcred = NOCRED;
1377 if (bp->b_wcred != NOCRED) {
1378 crfree(bp->b_wcred);
1379 bp->b_wcred = NOCRED;
1381 if (!LIST_EMPTY(&bp->b_dep))
1385 uma_zfree(buf_zone, bp);
1386 atomic_add_int(&numfreebuffers, 1);
1393 * Import bufs into the uma cache from the buf list. The system still
1394 * expects a static array of bufs and much of the synchronization
1395 * around bufs assumes type stable storage. As a result, UMA is used
1396 * only as a per-cpu cache of bufs still maintained on a global list.
1399 buf_import(void *arg, void **store, int cnt, int flags)
1404 mtx_lock(&bqlocks[QUEUE_EMPTY]);
1405 for (i = 0; i < cnt; i++) {
1406 bp = TAILQ_FIRST(&bufqueues[QUEUE_EMPTY]);
1412 mtx_unlock(&bqlocks[QUEUE_EMPTY]);
1420 * Release bufs from the uma cache back to the buffer queues.
1423 buf_release(void *arg, void **store, int cnt)
1427 for (i = 0; i < cnt; i++)
1428 binsfree(store[i], QUEUE_EMPTY);
1434 * Allocate an empty buffer header.
1441 bp = uma_zalloc(buf_zone, M_NOWAIT);
1443 bufspace_daemonwakeup();
1444 atomic_add_int(&numbufallocfails, 1);
1449 * Wake-up the bufspace daemon on transition.
1451 if (atomic_fetchadd_int(&numfreebuffers, -1) == lofreebuffers)
1452 bufspace_daemonwakeup();
1454 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1455 panic("getnewbuf_empty: Locked buf %p on free queue.", bp);
1457 KASSERT(bp->b_vp == NULL,
1458 ("bp: %p still has vnode %p.", bp, bp->b_vp));
1459 KASSERT((bp->b_flags & (B_DELWRI | B_NOREUSE)) == 0,
1460 ("invalid buffer %p flags %#x", bp, bp->b_flags));
1461 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
1462 ("bp: %p still on a buffer list. xflags %X", bp, bp->b_xflags));
1463 KASSERT(bp->b_npages == 0,
1464 ("bp: %p still has %d vm pages\n", bp, bp->b_npages));
1465 KASSERT(bp->b_kvasize == 0, ("bp: %p still has kva\n", bp));
1466 KASSERT(bp->b_bufsize == 0, ("bp: %p still has bufspace\n", bp));
1473 bp->b_blkno = bp->b_lblkno = 0;
1474 bp->b_offset = NOOFFSET;
1480 bp->b_dirtyoff = bp->b_dirtyend = 0;
1481 bp->b_bufobj = NULL;
1482 bp->b_pin_count = 0;
1483 bp->b_data = bp->b_kvabase = unmapped_buf;
1484 bp->b_fsprivate1 = NULL;
1485 bp->b_fsprivate2 = NULL;
1486 bp->b_fsprivate3 = NULL;
1487 LIST_INIT(&bp->b_dep);
1495 * Free a buffer from the given bufqueue. kva controls whether the
1496 * freed buf must own some kva resources. This is used for
1500 buf_qrecycle(int qindex, bool kva)
1502 struct buf *bp, *nbp;
1505 atomic_add_int(&bufdefragcnt, 1);
1507 mtx_lock(&bqlocks[qindex]);
1508 nbp = TAILQ_FIRST(&bufqueues[qindex]);
1511 * Run scan, possibly freeing data and/or kva mappings on the fly
1514 while ((bp = nbp) != NULL) {
1516 * Calculate next bp (we can only use it if we do not
1517 * release the bqlock).
1519 nbp = TAILQ_NEXT(bp, b_freelist);
1522 * If we are defragging then we need a buffer with
1523 * some kva to reclaim.
1525 if (kva && bp->b_kvasize == 0)
1528 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1532 * Skip buffers with background writes in progress.
1534 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
1539 KASSERT(bp->b_qindex == qindex,
1540 ("getnewbuf: inconsistent queue %d bp %p", qindex, bp));
1542 * NOTE: nbp is now entirely invalid. We can only restart
1543 * the scan from this point on.
1546 mtx_unlock(&bqlocks[qindex]);
1549 * Requeue the background write buffer with error and
1552 if ((bp->b_vflags & BV_BKGRDERR) != 0) {
1554 mtx_lock(&bqlocks[qindex]);
1555 nbp = TAILQ_FIRST(&bufqueues[qindex]);
1558 bp->b_flags |= B_INVAL;
1562 mtx_unlock(&bqlocks[qindex]);
1570 * Iterate through all clean queues until we find a buf to recycle or
1571 * exhaust the search.
1574 buf_recycle(bool kva)
1576 int qindex, first_qindex;
1578 qindex = first_qindex = bqcleanq();
1580 if (buf_qrecycle(qindex, kva) == 0)
1582 if (++qindex == QUEUE_CLEAN + clean_queues)
1583 qindex = QUEUE_CLEAN;
1584 } while (qindex != first_qindex);
1592 * Scan the clean queues looking for a buffer to recycle. needsbuffer
1593 * is set on failure so that the caller may optionally bufspace_wait()
1594 * in a race-free fashion.
1597 buf_scan(bool defrag)
1602 * To avoid heavy synchronization and wakeup races we set
1603 * needsbuffer and re-poll before failing. This ensures that
1604 * no frees can be missed between an unsuccessful poll and
1605 * going to sleep in a synchronized fashion.
1607 if ((error = buf_recycle(defrag)) != 0) {
1608 atomic_set_int(&needsbuffer, 1);
1609 bufspace_daemonwakeup();
1610 error = buf_recycle(defrag);
1613 atomic_add_int(&getnewbufrestarts, 1);
1620 * Mark the buffer for removal from the appropriate free list.
1624 bremfree(struct buf *bp)
1627 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1628 KASSERT((bp->b_flags & B_REMFREE) == 0,
1629 ("bremfree: buffer %p already marked for delayed removal.", bp));
1630 KASSERT(bp->b_qindex != QUEUE_NONE,
1631 ("bremfree: buffer %p not on a queue.", bp));
1632 BUF_ASSERT_XLOCKED(bp);
1634 bp->b_flags |= B_REMFREE;
1640 * Force an immediate removal from a free list. Used only in nfs when
1641 * it abuses the b_freelist pointer.
1644 bremfreef(struct buf *bp)
1648 qlock = bqlock(bp->b_qindex);
1657 * Removes a buffer from the free list, must be called with the
1658 * correct qlock held.
1661 bremfreel(struct buf *bp)
1664 CTR3(KTR_BUF, "bremfreel(%p) vp %p flags %X",
1665 bp, bp->b_vp, bp->b_flags);
1666 KASSERT(bp->b_qindex != QUEUE_NONE,
1667 ("bremfreel: buffer %p not on a queue.", bp));
1668 if (bp->b_qindex != QUEUE_EMPTY) {
1669 BUF_ASSERT_XLOCKED(bp);
1671 mtx_assert(bqlock(bp->b_qindex), MA_OWNED);
1673 TAILQ_REMOVE(&bufqueues[bp->b_qindex], bp, b_freelist);
1675 KASSERT(bq_len[bp->b_qindex] >= 1, ("queue %d underflow",
1677 bq_len[bp->b_qindex]--;
1679 bp->b_qindex = QUEUE_NONE;
1680 bp->b_flags &= ~B_REMFREE;
1686 * Free the kva allocation for a buffer.
1690 bufkva_free(struct buf *bp)
1694 if (bp->b_kvasize == 0) {
1695 KASSERT(bp->b_kvabase == unmapped_buf &&
1696 bp->b_data == unmapped_buf,
1697 ("Leaked KVA space on %p", bp));
1698 } else if (buf_mapped(bp))
1699 BUF_CHECK_MAPPED(bp);
1701 BUF_CHECK_UNMAPPED(bp);
1703 if (bp->b_kvasize == 0)
1706 vmem_free(buffer_arena, (vm_offset_t)bp->b_kvabase, bp->b_kvasize);
1707 atomic_subtract_long(&bufkvaspace, bp->b_kvasize);
1708 atomic_add_int(&buffreekvacnt, 1);
1709 bp->b_data = bp->b_kvabase = unmapped_buf;
1716 * Allocate the buffer KVA and set b_kvasize and b_kvabase.
1719 bufkva_alloc(struct buf *bp, int maxsize, int gbflags)
1724 KASSERT((gbflags & GB_UNMAPPED) == 0 || (gbflags & GB_KVAALLOC) != 0,
1725 ("Invalid gbflags 0x%x in %s", gbflags, __func__));
1730 error = vmem_alloc(buffer_arena, maxsize, M_BESTFIT | M_NOWAIT, &addr);
1733 * Buffer map is too fragmented. Request the caller
1734 * to defragment the map.
1738 bp->b_kvabase = (caddr_t)addr;
1739 bp->b_kvasize = maxsize;
1740 atomic_add_long(&bufkvaspace, bp->b_kvasize);
1741 if ((gbflags & GB_UNMAPPED) != 0) {
1742 bp->b_data = unmapped_buf;
1743 BUF_CHECK_UNMAPPED(bp);
1745 bp->b_data = bp->b_kvabase;
1746 BUF_CHECK_MAPPED(bp);
1754 * Reclaim buffer kva by freeing buffers holding kva. This is a vmem
1755 * callback that fires to avoid returning failure.
1758 bufkva_reclaim(vmem_t *vmem, int flags)
1762 for (i = 0; i < 5; i++)
1763 if (buf_scan(true) != 0)
1770 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must
1771 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set,
1772 * the buffer is valid and we do not have to do anything.
1775 breada(struct vnode * vp, daddr_t * rablkno, int * rabsize,
1776 int cnt, struct ucred * cred)
1781 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
1782 if (inmem(vp, *rablkno))
1784 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
1786 if ((rabp->b_flags & B_CACHE) == 0) {
1787 if (!TD_IS_IDLETHREAD(curthread))
1788 curthread->td_ru.ru_inblock++;
1789 rabp->b_flags |= B_ASYNC;
1790 rabp->b_flags &= ~B_INVAL;
1791 rabp->b_ioflags &= ~BIO_ERROR;
1792 rabp->b_iocmd = BIO_READ;
1793 if (rabp->b_rcred == NOCRED && cred != NOCRED)
1794 rabp->b_rcred = crhold(cred);
1795 vfs_busy_pages(rabp, 0);
1797 rabp->b_iooffset = dbtob(rabp->b_blkno);
1806 * Entry point for bread() and breadn() via #defines in sys/buf.h.
1808 * Get a buffer with the specified data. Look in the cache first. We
1809 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
1810 * is set, the buffer is valid and we do not have to do anything, see
1811 * getblk(). Also starts asynchronous I/O on read-ahead blocks.
1814 breadn_flags(struct vnode *vp, daddr_t blkno, int size, daddr_t *rablkno,
1815 int *rabsize, int cnt, struct ucred *cred, int flags, struct buf **bpp)
1818 int rv = 0, readwait = 0;
1820 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
1822 * Can only return NULL if GB_LOCK_NOWAIT flag is specified.
1824 *bpp = bp = getblk(vp, blkno, size, 0, 0, flags);
1828 /* if not found in cache, do some I/O */
1829 if ((bp->b_flags & B_CACHE) == 0) {
1830 if (!TD_IS_IDLETHREAD(curthread))
1831 curthread->td_ru.ru_inblock++;
1832 bp->b_iocmd = BIO_READ;
1833 bp->b_flags &= ~B_INVAL;
1834 bp->b_ioflags &= ~BIO_ERROR;
1835 if (bp->b_rcred == NOCRED && cred != NOCRED)
1836 bp->b_rcred = crhold(cred);
1837 vfs_busy_pages(bp, 0);
1838 bp->b_iooffset = dbtob(bp->b_blkno);
1843 breada(vp, rablkno, rabsize, cnt, cred);
1852 * Write, release buffer on completion. (Done by iodone
1853 * if async). Do not bother writing anything if the buffer
1856 * Note that we set B_CACHE here, indicating that buffer is
1857 * fully valid and thus cacheable. This is true even of NFS
1858 * now so we set it generally. This could be set either here
1859 * or in biodone() since the I/O is synchronous. We put it
1863 bufwrite(struct buf *bp)
1870 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1871 if ((bp->b_bufobj->bo_flag & BO_DEAD) != 0) {
1872 bp->b_flags |= B_INVAL | B_RELBUF;
1873 bp->b_flags &= ~B_CACHE;
1877 if (bp->b_flags & B_INVAL) {
1882 if (bp->b_flags & B_BARRIER)
1885 oldflags = bp->b_flags;
1887 BUF_ASSERT_HELD(bp);
1889 if (bp->b_pin_count > 0)
1892 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
1893 ("FFS background buffer should not get here %p", bp));
1897 vp_md = vp->v_vflag & VV_MD;
1902 * Mark the buffer clean. Increment the bufobj write count
1903 * before bundirty() call, to prevent other thread from seeing
1904 * empty dirty list and zero counter for writes in progress,
1905 * falsely indicating that the bufobj is clean.
1907 bufobj_wref(bp->b_bufobj);
1910 bp->b_flags &= ~B_DONE;
1911 bp->b_ioflags &= ~BIO_ERROR;
1912 bp->b_flags |= B_CACHE;
1913 bp->b_iocmd = BIO_WRITE;
1915 vfs_busy_pages(bp, 1);
1918 * Normal bwrites pipeline writes
1920 bp->b_runningbufspace = bp->b_bufsize;
1921 space = atomic_fetchadd_long(&runningbufspace, bp->b_runningbufspace);
1923 if (!TD_IS_IDLETHREAD(curthread))
1924 curthread->td_ru.ru_oublock++;
1925 if (oldflags & B_ASYNC)
1927 bp->b_iooffset = dbtob(bp->b_blkno);
1930 if ((oldflags & B_ASYNC) == 0) {
1931 int rtval = bufwait(bp);
1934 } else if (space > hirunningspace) {
1936 * don't allow the async write to saturate the I/O
1937 * system. We will not deadlock here because
1938 * we are blocking waiting for I/O that is already in-progress
1939 * to complete. We do not block here if it is the update
1940 * or syncer daemon trying to clean up as that can lead
1943 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md)
1944 waitrunningbufspace();
1951 bufbdflush(struct bufobj *bo, struct buf *bp)
1955 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
1956 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread);
1958 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
1961 * Try to find a buffer to flush.
1963 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
1964 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
1966 LK_EXCLUSIVE | LK_NOWAIT, NULL))
1969 panic("bdwrite: found ourselves");
1971 /* Don't countdeps with the bo lock held. */
1972 if (buf_countdeps(nbp, 0)) {
1977 if (nbp->b_flags & B_CLUSTEROK) {
1978 vfs_bio_awrite(nbp);
1983 dirtybufferflushes++;
1992 * Delayed write. (Buffer is marked dirty). Do not bother writing
1993 * anything if the buffer is marked invalid.
1995 * Note that since the buffer must be completely valid, we can safely
1996 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
1997 * biodone() in order to prevent getblk from writing the buffer
1998 * out synchronously.
2001 bdwrite(struct buf *bp)
2003 struct thread *td = curthread;
2007 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2008 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2009 KASSERT((bp->b_flags & B_BARRIER) == 0,
2010 ("Barrier request in delayed write %p", bp));
2011 BUF_ASSERT_HELD(bp);
2013 if (bp->b_flags & B_INVAL) {
2019 * If we have too many dirty buffers, don't create any more.
2020 * If we are wildly over our limit, then force a complete
2021 * cleanup. Otherwise, just keep the situation from getting
2022 * out of control. Note that we have to avoid a recursive
2023 * disaster and not try to clean up after our own cleanup!
2027 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) {
2028 td->td_pflags |= TDP_INBDFLUSH;
2030 td->td_pflags &= ~TDP_INBDFLUSH;
2036 * Set B_CACHE, indicating that the buffer is fully valid. This is
2037 * true even of NFS now.
2039 bp->b_flags |= B_CACHE;
2042 * This bmap keeps the system from needing to do the bmap later,
2043 * perhaps when the system is attempting to do a sync. Since it
2044 * is likely that the indirect block -- or whatever other datastructure
2045 * that the filesystem needs is still in memory now, it is a good
2046 * thing to do this. Note also, that if the pageout daemon is
2047 * requesting a sync -- there might not be enough memory to do
2048 * the bmap then... So, this is important to do.
2050 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
2051 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
2055 * Set the *dirty* buffer range based upon the VM system dirty
2058 * Mark the buffer pages as clean. We need to do this here to
2059 * satisfy the vnode_pager and the pageout daemon, so that it
2060 * thinks that the pages have been "cleaned". Note that since
2061 * the pages are in a delayed write buffer -- the VFS layer
2062 * "will" see that the pages get written out on the next sync,
2063 * or perhaps the cluster will be completed.
2065 vfs_clean_pages_dirty_buf(bp);
2069 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
2070 * due to the softdep code.
2077 * Turn buffer into delayed write request. We must clear BIO_READ and
2078 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
2079 * itself to properly update it in the dirty/clean lists. We mark it
2080 * B_DONE to ensure that any asynchronization of the buffer properly
2081 * clears B_DONE ( else a panic will occur later ).
2083 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
2084 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
2085 * should only be called if the buffer is known-good.
2087 * Since the buffer is not on a queue, we do not update the numfreebuffers
2090 * The buffer must be on QUEUE_NONE.
2093 bdirty(struct buf *bp)
2096 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
2097 bp, bp->b_vp, bp->b_flags);
2098 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2099 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
2100 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
2101 BUF_ASSERT_HELD(bp);
2102 bp->b_flags &= ~(B_RELBUF);
2103 bp->b_iocmd = BIO_WRITE;
2105 if ((bp->b_flags & B_DELWRI) == 0) {
2106 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
2115 * Clear B_DELWRI for buffer.
2117 * Since the buffer is not on a queue, we do not update the numfreebuffers
2120 * The buffer must be on QUEUE_NONE.
2124 bundirty(struct buf *bp)
2127 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2128 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2129 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
2130 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
2131 BUF_ASSERT_HELD(bp);
2133 if (bp->b_flags & B_DELWRI) {
2134 bp->b_flags &= ~B_DELWRI;
2139 * Since it is now being written, we can clear its deferred write flag.
2141 bp->b_flags &= ~B_DEFERRED;
2147 * Asynchronous write. Start output on a buffer, but do not wait for
2148 * it to complete. The buffer is released when the output completes.
2150 * bwrite() ( or the VOP routine anyway ) is responsible for handling
2151 * B_INVAL buffers. Not us.
2154 bawrite(struct buf *bp)
2157 bp->b_flags |= B_ASYNC;
2164 * Asynchronous barrier write. Start output on a buffer, but do not
2165 * wait for it to complete. Place a write barrier after this write so
2166 * that this buffer and all buffers written before it are committed to
2167 * the disk before any buffers written after this write are committed
2168 * to the disk. The buffer is released when the output completes.
2171 babarrierwrite(struct buf *bp)
2174 bp->b_flags |= B_ASYNC | B_BARRIER;
2181 * Synchronous barrier write. Start output on a buffer and wait for
2182 * it to complete. Place a write barrier after this write so that
2183 * this buffer and all buffers written before it are committed to
2184 * the disk before any buffers written after this write are committed
2185 * to the disk. The buffer is released when the output completes.
2188 bbarrierwrite(struct buf *bp)
2191 bp->b_flags |= B_BARRIER;
2192 return (bwrite(bp));
2198 * Called prior to the locking of any vnodes when we are expecting to
2199 * write. We do not want to starve the buffer cache with too many
2200 * dirty buffers so we block here. By blocking prior to the locking
2201 * of any vnodes we attempt to avoid the situation where a locked vnode
2202 * prevents the various system daemons from flushing related buffers.
2208 if (numdirtybuffers >= hidirtybuffers) {
2209 mtx_lock(&bdirtylock);
2210 while (numdirtybuffers >= hidirtybuffers) {
2212 msleep(&bdirtywait, &bdirtylock, (PRIBIO + 4),
2215 mtx_unlock(&bdirtylock);
2220 * Return true if we have too many dirty buffers.
2223 buf_dirty_count_severe(void)
2226 return(numdirtybuffers >= hidirtybuffers);
2232 * Release a busy buffer and, if requested, free its resources. The
2233 * buffer will be stashed in the appropriate bufqueue[] allowing it
2234 * to be accessed later as a cache entity or reused for other purposes.
2237 brelse(struct buf *bp)
2241 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
2242 bp, bp->b_vp, bp->b_flags);
2243 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
2244 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
2245 KASSERT((bp->b_flags & B_VMIO) != 0 || (bp->b_flags & B_NOREUSE) == 0,
2246 ("brelse: non-VMIO buffer marked NOREUSE"));
2248 if (BUF_LOCKRECURSED(bp)) {
2250 * Do not process, in particular, do not handle the
2251 * B_INVAL/B_RELBUF and do not release to free list.
2257 if (bp->b_flags & B_MANAGED) {
2262 if ((bp->b_vflags & (BV_BKGRDINPROG | BV_BKGRDERR)) == BV_BKGRDERR) {
2263 BO_LOCK(bp->b_bufobj);
2264 bp->b_vflags &= ~BV_BKGRDERR;
2265 BO_UNLOCK(bp->b_bufobj);
2268 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) &&
2269 !(bp->b_flags & B_INVAL)) {
2271 * Failed write, redirty. Must clear BIO_ERROR to prevent
2272 * pages from being scrapped.
2274 bp->b_ioflags &= ~BIO_ERROR;
2276 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
2277 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
2279 * Either a failed read I/O or we were asked to free or not
2282 bp->b_flags |= B_INVAL;
2283 if (!LIST_EMPTY(&bp->b_dep))
2285 if (bp->b_flags & B_DELWRI)
2287 bp->b_flags &= ~(B_DELWRI | B_CACHE);
2288 if ((bp->b_flags & B_VMIO) == 0) {
2296 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_truncate()
2297 * is called with B_DELWRI set, the underlying pages may wind up
2298 * getting freed causing a previous write (bdwrite()) to get 'lost'
2299 * because pages associated with a B_DELWRI bp are marked clean.
2301 * We still allow the B_INVAL case to call vfs_vmio_truncate(), even
2302 * if B_DELWRI is set.
2304 if (bp->b_flags & B_DELWRI)
2305 bp->b_flags &= ~B_RELBUF;
2308 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
2309 * constituted, not even NFS buffers now. Two flags effect this. If
2310 * B_INVAL, the struct buf is invalidated but the VM object is kept
2311 * around ( i.e. so it is trivial to reconstitute the buffer later ).
2313 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
2314 * invalidated. BIO_ERROR cannot be set for a failed write unless the
2315 * buffer is also B_INVAL because it hits the re-dirtying code above.
2317 * Normally we can do this whether a buffer is B_DELWRI or not. If
2318 * the buffer is an NFS buffer, it is tracking piecemeal writes or
2319 * the commit state and we cannot afford to lose the buffer. If the
2320 * buffer has a background write in progress, we need to keep it
2321 * around to prevent it from being reconstituted and starting a second
2324 if ((bp->b_flags & B_VMIO) && (bp->b_flags & B_NOCACHE ||
2325 (bp->b_ioflags & BIO_ERROR && bp->b_iocmd == BIO_READ)) &&
2326 !(bp->b_vp->v_mount != NULL &&
2327 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
2328 !vn_isdisk(bp->b_vp, NULL) && (bp->b_flags & B_DELWRI))) {
2329 vfs_vmio_invalidate(bp);
2333 if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0 ||
2334 (bp->b_flags & (B_DELWRI | B_NOREUSE)) == B_NOREUSE) {
2336 bp->b_flags &= ~B_NOREUSE;
2337 if (bp->b_vp != NULL)
2342 * If the buffer has junk contents signal it and eventually
2343 * clean up B_DELWRI and diassociate the vnode so that gbincore()
2346 if (bp->b_bufsize == 0 || (bp->b_ioflags & BIO_ERROR) != 0 ||
2347 (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) != 0)
2348 bp->b_flags |= B_INVAL;
2349 if (bp->b_flags & B_INVAL) {
2350 if (bp->b_flags & B_DELWRI)
2356 /* buffers with no memory */
2357 if (bp->b_bufsize == 0) {
2361 /* buffers with junk contents */
2362 if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
2363 (bp->b_ioflags & BIO_ERROR)) {
2364 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
2365 if (bp->b_vflags & BV_BKGRDINPROG)
2366 panic("losing buffer 2");
2367 qindex = QUEUE_CLEAN;
2368 bp->b_flags |= B_AGE;
2369 /* remaining buffers */
2370 } else if (bp->b_flags & B_DELWRI)
2371 qindex = QUEUE_DIRTY;
2373 qindex = QUEUE_CLEAN;
2375 binsfree(bp, qindex);
2377 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF | B_DIRECT);
2378 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
2379 panic("brelse: not dirty");
2382 if (qindex == QUEUE_CLEAN)
2387 * Release a buffer back to the appropriate queue but do not try to free
2388 * it. The buffer is expected to be used again soon.
2390 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
2391 * biodone() to requeue an async I/O on completion. It is also used when
2392 * known good buffers need to be requeued but we think we may need the data
2395 * XXX we should be able to leave the B_RELBUF hint set on completion.
2398 bqrelse(struct buf *bp)
2402 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2403 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
2404 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
2406 qindex = QUEUE_NONE;
2407 if (BUF_LOCKRECURSED(bp)) {
2408 /* do not release to free list */
2412 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
2414 if (bp->b_flags & B_MANAGED) {
2415 if (bp->b_flags & B_REMFREE)
2420 /* buffers with stale but valid contents */
2421 if ((bp->b_flags & B_DELWRI) != 0 || (bp->b_vflags & (BV_BKGRDINPROG |
2422 BV_BKGRDERR)) == BV_BKGRDERR) {
2423 BO_LOCK(bp->b_bufobj);
2424 bp->b_vflags &= ~BV_BKGRDERR;
2425 BO_UNLOCK(bp->b_bufobj);
2426 qindex = QUEUE_DIRTY;
2428 if ((bp->b_flags & B_DELWRI) == 0 &&
2429 (bp->b_xflags & BX_VNDIRTY))
2430 panic("bqrelse: not dirty");
2431 if ((bp->b_flags & B_NOREUSE) != 0) {
2435 qindex = QUEUE_CLEAN;
2437 binsfree(bp, qindex);
2442 if (qindex == QUEUE_CLEAN)
2447 * Complete I/O to a VMIO backed page. Validate the pages as appropriate,
2448 * restore bogus pages.
2451 vfs_vmio_iodone(struct buf *bp)
2457 int bogus, i, iosize;
2459 obj = bp->b_bufobj->bo_object;
2460 KASSERT(obj->paging_in_progress >= bp->b_npages,
2461 ("vfs_vmio_iodone: paging in progress(%d) < b_npages(%d)",
2462 obj->paging_in_progress, bp->b_npages));
2465 KASSERT(vp->v_holdcnt > 0,
2466 ("vfs_vmio_iodone: vnode %p has zero hold count", vp));
2467 KASSERT(vp->v_object != NULL,
2468 ("vfs_vmio_iodone: vnode %p has no vm_object", vp));
2470 foff = bp->b_offset;
2471 KASSERT(bp->b_offset != NOOFFSET,
2472 ("vfs_vmio_iodone: bp %p has no buffer offset", bp));
2475 iosize = bp->b_bcount - bp->b_resid;
2476 VM_OBJECT_WLOCK(obj);
2477 for (i = 0; i < bp->b_npages; i++) {
2480 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
2485 * cleanup bogus pages, restoring the originals
2488 if (m == bogus_page) {
2490 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
2492 panic("biodone: page disappeared!");
2494 } else if ((bp->b_iocmd == BIO_READ) && resid > 0) {
2496 * In the write case, the valid and clean bits are
2497 * already changed correctly ( see bdwrite() ), so we
2498 * only need to do this here in the read case.
2500 KASSERT((m->dirty & vm_page_bits(foff & PAGE_MASK,
2501 resid)) == 0, ("vfs_vmio_iodone: page %p "
2502 "has unexpected dirty bits", m));
2503 vfs_page_set_valid(bp, foff, m);
2505 KASSERT(OFF_TO_IDX(foff) == m->pindex,
2506 ("vfs_vmio_iodone: foff(%jd)/pindex(%ju) mismatch",
2507 (intmax_t)foff, (uintmax_t)m->pindex));
2510 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
2513 vm_object_pip_wakeupn(obj, bp->b_npages);
2514 VM_OBJECT_WUNLOCK(obj);
2515 if (bogus && buf_mapped(bp)) {
2516 BUF_CHECK_MAPPED(bp);
2517 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
2518 bp->b_pages, bp->b_npages);
2523 * Unwire a page held by a buf and place it on the appropriate vm queue.
2526 vfs_vmio_unwire(struct buf *bp, vm_page_t m)
2531 if (vm_page_unwire(m, PQ_NONE)) {
2533 * Determine if the page should be freed before adding
2534 * it to the inactive queue.
2536 if (m->valid == 0) {
2537 freed = !vm_page_busied(m);
2540 } else if ((bp->b_flags & B_DIRECT) != 0)
2541 freed = vm_page_try_to_free(m);
2546 * If the page is unlikely to be reused, let the
2547 * VM know. Otherwise, maintain LRU page
2548 * ordering and put the page at the tail of the
2551 if ((bp->b_flags & B_NOREUSE) != 0)
2552 vm_page_deactivate_noreuse(m);
2554 vm_page_deactivate(m);
2561 * Perform page invalidation when a buffer is released. The fully invalid
2562 * pages will be reclaimed later in vfs_vmio_truncate().
2565 vfs_vmio_invalidate(struct buf *bp)
2569 int i, resid, poffset, presid;
2571 if (buf_mapped(bp)) {
2572 BUF_CHECK_MAPPED(bp);
2573 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), bp->b_npages);
2575 BUF_CHECK_UNMAPPED(bp);
2577 * Get the base offset and length of the buffer. Note that
2578 * in the VMIO case if the buffer block size is not
2579 * page-aligned then b_data pointer may not be page-aligned.
2580 * But our b_pages[] array *IS* page aligned.
2582 * block sizes less then DEV_BSIZE (usually 512) are not
2583 * supported due to the page granularity bits (m->valid,
2584 * m->dirty, etc...).
2586 * See man buf(9) for more information
2588 obj = bp->b_bufobj->bo_object;
2589 resid = bp->b_bufsize;
2590 poffset = bp->b_offset & PAGE_MASK;
2591 VM_OBJECT_WLOCK(obj);
2592 for (i = 0; i < bp->b_npages; i++) {
2594 if (m == bogus_page)
2595 panic("vfs_vmio_invalidate: Unexpected bogus page.");
2596 bp->b_pages[i] = NULL;
2598 presid = resid > (PAGE_SIZE - poffset) ?
2599 (PAGE_SIZE - poffset) : resid;
2600 KASSERT(presid >= 0, ("brelse: extra page"));
2601 while (vm_page_xbusied(m)) {
2603 VM_OBJECT_WUNLOCK(obj);
2604 vm_page_busy_sleep(m, "mbncsh");
2605 VM_OBJECT_WLOCK(obj);
2607 if (pmap_page_wired_mappings(m) == 0)
2608 vm_page_set_invalid(m, poffset, presid);
2609 vfs_vmio_unwire(bp, m);
2613 VM_OBJECT_WUNLOCK(obj);
2618 * Page-granular truncation of an existing VMIO buffer.
2621 vfs_vmio_truncate(struct buf *bp, int desiredpages)
2627 if (bp->b_npages == desiredpages)
2630 if (buf_mapped(bp)) {
2631 BUF_CHECK_MAPPED(bp);
2632 pmap_qremove((vm_offset_t)trunc_page((vm_offset_t)bp->b_data) +
2633 (desiredpages << PAGE_SHIFT), bp->b_npages - desiredpages);
2635 BUF_CHECK_UNMAPPED(bp);
2636 obj = bp->b_bufobj->bo_object;
2638 VM_OBJECT_WLOCK(obj);
2639 for (i = desiredpages; i < bp->b_npages; i++) {
2641 KASSERT(m != bogus_page, ("allocbuf: bogus page found"));
2642 bp->b_pages[i] = NULL;
2643 vfs_vmio_unwire(bp, m);
2646 VM_OBJECT_WUNLOCK(obj);
2647 bp->b_npages = desiredpages;
2651 * Byte granular extension of VMIO buffers.
2654 vfs_vmio_extend(struct buf *bp, int desiredpages, int size)
2657 * We are growing the buffer, possibly in a
2658 * byte-granular fashion.
2666 * Step 1, bring in the VM pages from the object, allocating
2667 * them if necessary. We must clear B_CACHE if these pages
2668 * are not valid for the range covered by the buffer.
2670 obj = bp->b_bufobj->bo_object;
2671 VM_OBJECT_WLOCK(obj);
2672 while (bp->b_npages < desiredpages) {
2674 * We must allocate system pages since blocking
2675 * here could interfere with paging I/O, no
2676 * matter which process we are.
2678 * Only exclusive busy can be tested here.
2679 * Blocking on shared busy might lead to
2680 * deadlocks once allocbuf() is called after
2681 * pages are vfs_busy_pages().
2683 m = vm_page_grab(obj, OFF_TO_IDX(bp->b_offset) + bp->b_npages,
2684 VM_ALLOC_NOBUSY | VM_ALLOC_SYSTEM |
2685 VM_ALLOC_WIRED | VM_ALLOC_IGN_SBUSY |
2686 VM_ALLOC_COUNT(desiredpages - bp->b_npages));
2688 bp->b_flags &= ~B_CACHE;
2689 bp->b_pages[bp->b_npages] = m;
2694 * Step 2. We've loaded the pages into the buffer,
2695 * we have to figure out if we can still have B_CACHE
2696 * set. Note that B_CACHE is set according to the
2697 * byte-granular range ( bcount and size ), not the
2698 * aligned range ( newbsize ).
2700 * The VM test is against m->valid, which is DEV_BSIZE
2701 * aligned. Needless to say, the validity of the data
2702 * needs to also be DEV_BSIZE aligned. Note that this
2703 * fails with NFS if the server or some other client
2704 * extends the file's EOF. If our buffer is resized,
2705 * B_CACHE may remain set! XXX
2707 toff = bp->b_bcount;
2708 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
2709 while ((bp->b_flags & B_CACHE) && toff < size) {
2712 if (tinc > (size - toff))
2714 pi = ((bp->b_offset & PAGE_MASK) + toff) >> PAGE_SHIFT;
2715 m = bp->b_pages[pi];
2716 vfs_buf_test_cache(bp, bp->b_offset, toff, tinc, m);
2720 VM_OBJECT_WUNLOCK(obj);
2723 * Step 3, fixup the KVA pmap.
2728 BUF_CHECK_UNMAPPED(bp);
2732 * Check to see if a block at a particular lbn is available for a clustered
2736 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
2743 /* If the buf isn't in core skip it */
2744 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
2747 /* If the buf is busy we don't want to wait for it */
2748 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2751 /* Only cluster with valid clusterable delayed write buffers */
2752 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
2753 (B_DELWRI | B_CLUSTEROK))
2756 if (bpa->b_bufsize != size)
2760 * Check to see if it is in the expected place on disk and that the
2761 * block has been mapped.
2763 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
2773 * Implement clustered async writes for clearing out B_DELWRI buffers.
2774 * This is much better then the old way of writing only one buffer at
2775 * a time. Note that we may not be presented with the buffers in the
2776 * correct order, so we search for the cluster in both directions.
2779 vfs_bio_awrite(struct buf *bp)
2784 daddr_t lblkno = bp->b_lblkno;
2785 struct vnode *vp = bp->b_vp;
2793 gbflags = (bp->b_data == unmapped_buf) ? GB_UNMAPPED : 0;
2795 * right now we support clustered writing only to regular files. If
2796 * we find a clusterable block we could be in the middle of a cluster
2797 * rather then at the beginning.
2799 if ((vp->v_type == VREG) &&
2800 (vp->v_mount != 0) && /* Only on nodes that have the size info */
2801 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
2803 size = vp->v_mount->mnt_stat.f_iosize;
2804 maxcl = MAXPHYS / size;
2807 for (i = 1; i < maxcl; i++)
2808 if (vfs_bio_clcheck(vp, size, lblkno + i,
2809 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
2812 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
2813 if (vfs_bio_clcheck(vp, size, lblkno - j,
2814 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
2820 * this is a possible cluster write
2824 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl,
2830 bp->b_flags |= B_ASYNC;
2832 * default (old) behavior, writing out only one block
2834 * XXX returns b_bufsize instead of b_bcount for nwritten?
2836 nwritten = bp->b_bufsize;
2845 * Allocate KVA for an empty buf header according to gbflags.
2848 getnewbuf_kva(struct buf *bp, int gbflags, int maxsize)
2851 if ((gbflags & (GB_UNMAPPED | GB_KVAALLOC)) != GB_UNMAPPED) {
2853 * In order to keep fragmentation sane we only allocate kva
2854 * in BKVASIZE chunks. XXX with vmem we can do page size.
2856 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
2858 if (maxsize != bp->b_kvasize &&
2859 bufkva_alloc(bp, maxsize, gbflags))
2868 * Find and initialize a new buffer header, freeing up existing buffers
2869 * in the bufqueues as necessary. The new buffer is returned locked.
2872 * We have insufficient buffer headers
2873 * We have insufficient buffer space
2874 * buffer_arena is too fragmented ( space reservation fails )
2875 * If we have to flush dirty buffers ( but we try to avoid this )
2877 * The caller is responsible for releasing the reserved bufspace after
2878 * allocbuf() is called.
2881 getnewbuf(struct vnode *vp, int slpflag, int slptimeo, int maxsize, int gbflags)
2884 bool metadata, reserved;
2887 KASSERT((gbflags & (GB_UNMAPPED | GB_KVAALLOC)) != GB_KVAALLOC,
2888 ("GB_KVAALLOC only makes sense with GB_UNMAPPED"));
2889 if (!unmapped_buf_allowed)
2890 gbflags &= ~(GB_UNMAPPED | GB_KVAALLOC);
2892 if (vp == NULL || (vp->v_vflag & (VV_MD | VV_SYSTEM)) != 0 ||
2897 atomic_add_int(&getnewbufcalls, 1);
2900 if (reserved == false &&
2901 bufspace_reserve(maxsize, metadata) != 0)
2904 if ((bp = buf_alloc()) == NULL)
2906 if (getnewbuf_kva(bp, gbflags, maxsize) == 0)
2909 } while(buf_scan(false) == 0);
2912 bufspace_release(maxsize);
2914 bp->b_flags |= B_INVAL;
2917 bufspace_wait(vp, gbflags, slpflag, slptimeo);
2925 * buffer flushing daemon. Buffers are normally flushed by the
2926 * update daemon but if it cannot keep up this process starts to
2927 * take the load in an attempt to prevent getnewbuf() from blocking.
2929 static struct kproc_desc buf_kp = {
2934 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp);
2937 buf_flush(struct vnode *vp, int target)
2941 flushed = flushbufqueues(vp, target, 0);
2944 * Could not find any buffers without rollback
2945 * dependencies, so just write the first one
2946 * in the hopes of eventually making progress.
2948 if (vp != NULL && target > 2)
2950 flushbufqueues(vp, target, 1);
2961 * This process needs to be suspended prior to shutdown sync.
2963 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
2967 * This process is allowed to take the buffer cache to the limit
2969 curthread->td_pflags |= TDP_NORUNNINGBUF | TDP_BUFNEED;
2973 mtx_unlock(&bdlock);
2975 kproc_suspend_check(bufdaemonproc);
2976 lodirty = lodirtybuffers;
2977 if (bd_speedupreq) {
2978 lodirty = numdirtybuffers / 2;
2982 * Do the flush. Limit the amount of in-transit I/O we
2983 * allow to build up, otherwise we would completely saturate
2986 while (numdirtybuffers > lodirty) {
2987 if (buf_flush(NULL, numdirtybuffers - lodirty) == 0)
2989 kern_yield(PRI_USER);
2993 * Only clear bd_request if we have reached our low water
2994 * mark. The buf_daemon normally waits 1 second and
2995 * then incrementally flushes any dirty buffers that have
2996 * built up, within reason.
2998 * If we were unable to hit our low water mark and couldn't
2999 * find any flushable buffers, we sleep for a short period
3000 * to avoid endless loops on unlockable buffers.
3003 if (numdirtybuffers <= lodirtybuffers) {
3005 * We reached our low water mark, reset the
3006 * request and sleep until we are needed again.
3007 * The sleep is just so the suspend code works.
3011 * Do an extra wakeup in case dirty threshold
3012 * changed via sysctl and the explicit transition
3013 * out of shortfall was missed.
3016 if (runningbufspace <= lorunningspace)
3018 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
3021 * We couldn't find any flushable dirty buffers but
3022 * still have too many dirty buffers, we
3023 * have to sleep and try again. (rare)
3025 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
3033 * Try to flush a buffer in the dirty queue. We must be careful to
3034 * free up B_INVAL buffers instead of write them, which NFS is
3035 * particularly sensitive to.
3037 static int flushwithdeps = 0;
3038 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
3039 0, "Number of buffers flushed with dependecies that require rollbacks");
3042 flushbufqueues(struct vnode *lvp, int target, int flushdeps)
3044 struct buf *sentinel;
3055 queue = QUEUE_DIRTY;
3057 sentinel = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
3058 sentinel->b_qindex = QUEUE_SENTINEL;
3059 mtx_lock(&bqlocks[queue]);
3060 TAILQ_INSERT_HEAD(&bufqueues[queue], sentinel, b_freelist);
3061 mtx_unlock(&bqlocks[queue]);
3062 while (flushed != target) {
3064 mtx_lock(&bqlocks[queue]);
3065 bp = TAILQ_NEXT(sentinel, b_freelist);
3067 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
3068 TAILQ_INSERT_AFTER(&bufqueues[queue], bp, sentinel,
3071 mtx_unlock(&bqlocks[queue]);
3075 * Skip sentinels inserted by other invocations of the
3076 * flushbufqueues(), taking care to not reorder them.
3078 * Only flush the buffers that belong to the
3079 * vnode locked by the curthread.
3081 if (bp->b_qindex == QUEUE_SENTINEL || (lvp != NULL &&
3083 mtx_unlock(&bqlocks[queue]);
3086 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL);
3087 mtx_unlock(&bqlocks[queue]);
3090 if (bp->b_pin_count > 0) {
3095 * BKGRDINPROG can only be set with the buf and bufobj
3096 * locks both held. We tolerate a race to clear it here.
3098 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
3099 (bp->b_flags & B_DELWRI) == 0) {
3103 if (bp->b_flags & B_INVAL) {
3110 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) {
3111 if (flushdeps == 0) {
3119 * We must hold the lock on a vnode before writing
3120 * one of its buffers. Otherwise we may confuse, or
3121 * in the case of a snapshot vnode, deadlock the
3124 * The lock order here is the reverse of the normal
3125 * of vnode followed by buf lock. This is ok because
3126 * the NOWAIT will prevent deadlock.
3129 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3135 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
3137 ASSERT_VOP_LOCKED(vp, "getbuf");
3139 error = VOP_ISLOCKED(vp) == LK_EXCLUSIVE ? 0 :
3140 vn_lock(vp, LK_TRYUPGRADE);
3143 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
3144 bp, bp->b_vp, bp->b_flags);
3145 if (curproc == bufdaemonproc) {
3152 vn_finished_write(mp);
3155 flushwithdeps += hasdeps;
3159 * Sleeping on runningbufspace while holding
3160 * vnode lock leads to deadlock.
3162 if (curproc == bufdaemonproc &&
3163 runningbufspace > hirunningspace)
3164 waitrunningbufspace();
3167 vn_finished_write(mp);
3170 mtx_lock(&bqlocks[queue]);
3171 TAILQ_REMOVE(&bufqueues[queue], sentinel, b_freelist);
3172 mtx_unlock(&bqlocks[queue]);
3173 free(sentinel, M_TEMP);
3178 * Check to see if a block is currently memory resident.
3181 incore(struct bufobj *bo, daddr_t blkno)
3186 bp = gbincore(bo, blkno);
3192 * Returns true if no I/O is needed to access the
3193 * associated VM object. This is like incore except
3194 * it also hunts around in the VM system for the data.
3198 inmem(struct vnode * vp, daddr_t blkno)
3201 vm_offset_t toff, tinc, size;
3205 ASSERT_VOP_LOCKED(vp, "inmem");
3207 if (incore(&vp->v_bufobj, blkno))
3209 if (vp->v_mount == NULL)
3216 if (size > vp->v_mount->mnt_stat.f_iosize)
3217 size = vp->v_mount->mnt_stat.f_iosize;
3218 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
3220 VM_OBJECT_RLOCK(obj);
3221 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
3222 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
3226 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
3227 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
3228 if (vm_page_is_valid(m,
3229 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
3232 VM_OBJECT_RUNLOCK(obj);
3236 VM_OBJECT_RUNLOCK(obj);
3241 * Set the dirty range for a buffer based on the status of the dirty
3242 * bits in the pages comprising the buffer. The range is limited
3243 * to the size of the buffer.
3245 * Tell the VM system that the pages associated with this buffer
3246 * are clean. This is used for delayed writes where the data is
3247 * going to go to disk eventually without additional VM intevention.
3249 * Note that while we only really need to clean through to b_bcount, we
3250 * just go ahead and clean through to b_bufsize.
3253 vfs_clean_pages_dirty_buf(struct buf *bp)
3255 vm_ooffset_t foff, noff, eoff;
3259 if ((bp->b_flags & B_VMIO) == 0 || bp->b_bufsize == 0)
3262 foff = bp->b_offset;
3263 KASSERT(bp->b_offset != NOOFFSET,
3264 ("vfs_clean_pages_dirty_buf: no buffer offset"));
3266 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
3267 vfs_drain_busy_pages(bp);
3268 vfs_setdirty_locked_object(bp);
3269 for (i = 0; i < bp->b_npages; i++) {
3270 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3272 if (eoff > bp->b_offset + bp->b_bufsize)
3273 eoff = bp->b_offset + bp->b_bufsize;
3275 vfs_page_set_validclean(bp, foff, m);
3276 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
3279 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
3283 vfs_setdirty_locked_object(struct buf *bp)
3288 object = bp->b_bufobj->bo_object;
3289 VM_OBJECT_ASSERT_WLOCKED(object);
3292 * We qualify the scan for modified pages on whether the
3293 * object has been flushed yet.
3295 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) {
3296 vm_offset_t boffset;
3297 vm_offset_t eoffset;
3300 * test the pages to see if they have been modified directly
3301 * by users through the VM system.
3303 for (i = 0; i < bp->b_npages; i++)
3304 vm_page_test_dirty(bp->b_pages[i]);
3307 * Calculate the encompassing dirty range, boffset and eoffset,
3308 * (eoffset - boffset) bytes.
3311 for (i = 0; i < bp->b_npages; i++) {
3312 if (bp->b_pages[i]->dirty)
3315 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
3317 for (i = bp->b_npages - 1; i >= 0; --i) {
3318 if (bp->b_pages[i]->dirty) {
3322 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
3325 * Fit it to the buffer.
3328 if (eoffset > bp->b_bcount)
3329 eoffset = bp->b_bcount;
3332 * If we have a good dirty range, merge with the existing
3336 if (boffset < eoffset) {
3337 if (bp->b_dirtyoff > boffset)
3338 bp->b_dirtyoff = boffset;
3339 if (bp->b_dirtyend < eoffset)
3340 bp->b_dirtyend = eoffset;
3346 * Allocate the KVA mapping for an existing buffer.
3347 * If an unmapped buffer is provided but a mapped buffer is requested, take
3348 * also care to properly setup mappings between pages and KVA.
3351 bp_unmapped_get_kva(struct buf *bp, daddr_t blkno, int size, int gbflags)
3353 int bsize, maxsize, need_mapping, need_kva;
3356 need_mapping = bp->b_data == unmapped_buf &&
3357 (gbflags & GB_UNMAPPED) == 0;
3358 need_kva = bp->b_kvabase == unmapped_buf &&
3359 bp->b_data == unmapped_buf &&
3360 (gbflags & GB_KVAALLOC) != 0;
3361 if (!need_mapping && !need_kva)
3364 BUF_CHECK_UNMAPPED(bp);
3366 if (need_mapping && bp->b_kvabase != unmapped_buf) {
3368 * Buffer is not mapped, but the KVA was already
3369 * reserved at the time of the instantiation. Use the
3376 * Calculate the amount of the address space we would reserve
3377 * if the buffer was mapped.
3379 bsize = vn_isdisk(bp->b_vp, NULL) ? DEV_BSIZE : bp->b_bufobj->bo_bsize;
3380 KASSERT(bsize != 0, ("bsize == 0, check bo->bo_bsize"));
3381 offset = blkno * bsize;
3382 maxsize = size + (offset & PAGE_MASK);
3383 maxsize = imax(maxsize, bsize);
3385 while (bufkva_alloc(bp, maxsize, gbflags) != 0) {
3386 if ((gbflags & GB_NOWAIT_BD) != 0) {
3388 * XXXKIB: defragmentation cannot
3389 * succeed, not sure what else to do.
3391 panic("GB_NOWAIT_BD and GB_UNMAPPED %p", bp);
3393 atomic_add_int(&mappingrestarts, 1);
3394 bufspace_wait(bp->b_vp, gbflags, 0, 0);
3398 /* b_offset is handled by bpmap_qenter. */
3399 bp->b_data = bp->b_kvabase;
3400 BUF_CHECK_MAPPED(bp);
3408 * Get a block given a specified block and offset into a file/device.
3409 * The buffers B_DONE bit will be cleared on return, making it almost
3410 * ready for an I/O initiation. B_INVAL may or may not be set on
3411 * return. The caller should clear B_INVAL prior to initiating a
3414 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
3415 * an existing buffer.
3417 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
3418 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
3419 * and then cleared based on the backing VM. If the previous buffer is
3420 * non-0-sized but invalid, B_CACHE will be cleared.
3422 * If getblk() must create a new buffer, the new buffer is returned with
3423 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
3424 * case it is returned with B_INVAL clear and B_CACHE set based on the
3427 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
3428 * B_CACHE bit is clear.
3430 * What this means, basically, is that the caller should use B_CACHE to
3431 * determine whether the buffer is fully valid or not and should clear
3432 * B_INVAL prior to issuing a read. If the caller intends to validate
3433 * the buffer by loading its data area with something, the caller needs
3434 * to clear B_INVAL. If the caller does this without issuing an I/O,
3435 * the caller should set B_CACHE ( as an optimization ), else the caller
3436 * should issue the I/O and biodone() will set B_CACHE if the I/O was
3437 * a write attempt or if it was a successfull read. If the caller
3438 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
3439 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
3442 getblk(struct vnode *vp, daddr_t blkno, int size, int slpflag, int slptimeo,
3447 int bsize, error, maxsize, vmio;
3450 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
3451 KASSERT((flags & (GB_UNMAPPED | GB_KVAALLOC)) != GB_KVAALLOC,
3452 ("GB_KVAALLOC only makes sense with GB_UNMAPPED"));
3453 ASSERT_VOP_LOCKED(vp, "getblk");
3454 if (size > MAXBCACHEBUF)
3455 panic("getblk: size(%d) > MAXBCACHEBUF(%d)\n", size,
3457 if (!unmapped_buf_allowed)
3458 flags &= ~(GB_UNMAPPED | GB_KVAALLOC);
3463 bp = gbincore(bo, blkno);
3467 * Buffer is in-core. If the buffer is not busy nor managed,
3468 * it must be on a queue.
3470 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
3472 if (flags & GB_LOCK_NOWAIT)
3473 lockflags |= LK_NOWAIT;
3475 error = BUF_TIMELOCK(bp, lockflags,
3476 BO_LOCKPTR(bo), "getblk", slpflag, slptimeo);
3479 * If we slept and got the lock we have to restart in case
3480 * the buffer changed identities.
3482 if (error == ENOLCK)
3484 /* We timed out or were interrupted. */
3487 /* If recursed, assume caller knows the rules. */
3488 else if (BUF_LOCKRECURSED(bp))
3492 * The buffer is locked. B_CACHE is cleared if the buffer is
3493 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
3494 * and for a VMIO buffer B_CACHE is adjusted according to the
3497 if (bp->b_flags & B_INVAL)
3498 bp->b_flags &= ~B_CACHE;
3499 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
3500 bp->b_flags |= B_CACHE;
3501 if (bp->b_flags & B_MANAGED)
3502 MPASS(bp->b_qindex == QUEUE_NONE);
3507 * check for size inconsistencies for non-VMIO case.
3509 if (bp->b_bcount != size) {
3510 if ((bp->b_flags & B_VMIO) == 0 ||
3511 (size > bp->b_kvasize)) {
3512 if (bp->b_flags & B_DELWRI) {
3514 * If buffer is pinned and caller does
3515 * not want sleep waiting for it to be
3516 * unpinned, bail out
3518 if (bp->b_pin_count > 0) {
3519 if (flags & GB_LOCK_NOWAIT) {
3526 bp->b_flags |= B_NOCACHE;
3529 if (LIST_EMPTY(&bp->b_dep)) {
3530 bp->b_flags |= B_RELBUF;
3533 bp->b_flags |= B_NOCACHE;
3542 * Handle the case of unmapped buffer which should
3543 * become mapped, or the buffer for which KVA
3544 * reservation is requested.
3546 bp_unmapped_get_kva(bp, blkno, size, flags);
3549 * If the size is inconsistant in the VMIO case, we can resize
3550 * the buffer. This might lead to B_CACHE getting set or
3551 * cleared. If the size has not changed, B_CACHE remains
3552 * unchanged from its previous state.
3556 KASSERT(bp->b_offset != NOOFFSET,
3557 ("getblk: no buffer offset"));
3560 * A buffer with B_DELWRI set and B_CACHE clear must
3561 * be committed before we can return the buffer in
3562 * order to prevent the caller from issuing a read
3563 * ( due to B_CACHE not being set ) and overwriting
3566 * Most callers, including NFS and FFS, need this to
3567 * operate properly either because they assume they
3568 * can issue a read if B_CACHE is not set, or because
3569 * ( for example ) an uncached B_DELWRI might loop due
3570 * to softupdates re-dirtying the buffer. In the latter
3571 * case, B_CACHE is set after the first write completes,
3572 * preventing further loops.
3573 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
3574 * above while extending the buffer, we cannot allow the
3575 * buffer to remain with B_CACHE set after the write
3576 * completes or it will represent a corrupt state. To
3577 * deal with this we set B_NOCACHE to scrap the buffer
3580 * We might be able to do something fancy, like setting
3581 * B_CACHE in bwrite() except if B_DELWRI is already set,
3582 * so the below call doesn't set B_CACHE, but that gets real
3583 * confusing. This is much easier.
3586 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
3587 bp->b_flags |= B_NOCACHE;
3591 bp->b_flags &= ~B_DONE;
3594 * Buffer is not in-core, create new buffer. The buffer
3595 * returned by getnewbuf() is locked. Note that the returned
3596 * buffer is also considered valid (not marked B_INVAL).
3600 * If the user does not want us to create the buffer, bail out
3603 if (flags & GB_NOCREAT)
3605 if (numfreebuffers == 0 && TD_IS_IDLETHREAD(curthread))
3608 bsize = vn_isdisk(vp, NULL) ? DEV_BSIZE : bo->bo_bsize;
3609 KASSERT(bsize != 0, ("bsize == 0, check bo->bo_bsize"));
3610 offset = blkno * bsize;
3611 vmio = vp->v_object != NULL;
3613 maxsize = size + (offset & PAGE_MASK);
3616 /* Do not allow non-VMIO notmapped buffers. */
3617 flags &= ~(GB_UNMAPPED | GB_KVAALLOC);
3619 maxsize = imax(maxsize, bsize);
3621 bp = getnewbuf(vp, slpflag, slptimeo, maxsize, flags);
3623 if (slpflag || slptimeo)
3626 * XXX This is here until the sleep path is diagnosed
3627 * enough to work under very low memory conditions.
3629 * There's an issue on low memory, 4BSD+non-preempt
3630 * systems (eg MIPS routers with 32MB RAM) where buffer
3631 * exhaustion occurs without sleeping for buffer
3632 * reclaimation. This just sticks in a loop and
3633 * constantly attempts to allocate a buffer, which
3634 * hits exhaustion and tries to wakeup bufdaemon.
3635 * This never happens because we never yield.
3637 * The real solution is to identify and fix these cases
3638 * so we aren't effectively busy-waiting in a loop
3639 * until the reclaimation path has cycles to run.
3641 kern_yield(PRI_USER);
3646 * This code is used to make sure that a buffer is not
3647 * created while the getnewbuf routine is blocked.
3648 * This can be a problem whether the vnode is locked or not.
3649 * If the buffer is created out from under us, we have to
3650 * throw away the one we just created.
3652 * Note: this must occur before we associate the buffer
3653 * with the vp especially considering limitations in
3654 * the splay tree implementation when dealing with duplicate
3658 if (gbincore(bo, blkno)) {
3660 bp->b_flags |= B_INVAL;
3662 bufspace_release(maxsize);
3667 * Insert the buffer into the hash, so that it can
3668 * be found by incore.
3670 bp->b_blkno = bp->b_lblkno = blkno;
3671 bp->b_offset = offset;
3676 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
3677 * buffer size starts out as 0, B_CACHE will be set by
3678 * allocbuf() for the VMIO case prior to it testing the
3679 * backing store for validity.
3683 bp->b_flags |= B_VMIO;
3684 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
3685 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
3686 bp, vp->v_object, bp->b_bufobj->bo_object));
3688 bp->b_flags &= ~B_VMIO;
3689 KASSERT(bp->b_bufobj->bo_object == NULL,
3690 ("ARGH! has b_bufobj->bo_object %p %p\n",
3691 bp, bp->b_bufobj->bo_object));
3692 BUF_CHECK_MAPPED(bp);
3696 bufspace_release(maxsize);
3697 bp->b_flags &= ~B_DONE;
3699 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
3700 BUF_ASSERT_HELD(bp);
3702 KASSERT(bp->b_bufobj == bo,
3703 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
3708 * Get an empty, disassociated buffer of given size. The buffer is initially
3712 geteblk(int size, int flags)
3717 maxsize = (size + BKVAMASK) & ~BKVAMASK;
3718 while ((bp = getnewbuf(NULL, 0, 0, maxsize, flags)) == NULL) {
3719 if ((flags & GB_NOWAIT_BD) &&
3720 (curthread->td_pflags & TDP_BUFNEED) != 0)
3724 bufspace_release(maxsize);
3725 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
3726 BUF_ASSERT_HELD(bp);
3731 * Truncate the backing store for a non-vmio buffer.
3734 vfs_nonvmio_truncate(struct buf *bp, int newbsize)
3737 if (bp->b_flags & B_MALLOC) {
3739 * malloced buffers are not shrunk
3741 if (newbsize == 0) {
3742 bufmallocadjust(bp, 0);
3743 free(bp->b_data, M_BIOBUF);
3744 bp->b_data = bp->b_kvabase;
3745 bp->b_flags &= ~B_MALLOC;
3749 vm_hold_free_pages(bp, newbsize);
3750 bufspace_adjust(bp, newbsize);
3754 * Extend the backing for a non-VMIO buffer.
3757 vfs_nonvmio_extend(struct buf *bp, int newbsize)
3763 * We only use malloced memory on the first allocation.
3764 * and revert to page-allocated memory when the buffer
3767 * There is a potential smp race here that could lead
3768 * to bufmallocspace slightly passing the max. It
3769 * is probably extremely rare and not worth worrying
3772 if (bp->b_bufsize == 0 && newbsize <= PAGE_SIZE/2 &&
3773 bufmallocspace < maxbufmallocspace) {
3774 bp->b_data = malloc(newbsize, M_BIOBUF, M_WAITOK);
3775 bp->b_flags |= B_MALLOC;
3776 bufmallocadjust(bp, newbsize);
3781 * If the buffer is growing on its other-than-first
3782 * allocation then we revert to the page-allocation
3787 if (bp->b_flags & B_MALLOC) {
3788 origbuf = bp->b_data;
3789 origbufsize = bp->b_bufsize;
3790 bp->b_data = bp->b_kvabase;
3791 bufmallocadjust(bp, 0);
3792 bp->b_flags &= ~B_MALLOC;
3793 newbsize = round_page(newbsize);
3795 vm_hold_load_pages(bp, (vm_offset_t) bp->b_data + bp->b_bufsize,
3796 (vm_offset_t) bp->b_data + newbsize);
3797 if (origbuf != NULL) {
3798 bcopy(origbuf, bp->b_data, origbufsize);
3799 free(origbuf, M_BIOBUF);
3801 bufspace_adjust(bp, newbsize);
3805 * This code constitutes the buffer memory from either anonymous system
3806 * memory (in the case of non-VMIO operations) or from an associated
3807 * VM object (in the case of VMIO operations). This code is able to
3808 * resize a buffer up or down.
3810 * Note that this code is tricky, and has many complications to resolve
3811 * deadlock or inconsistant data situations. Tread lightly!!!
3812 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
3813 * the caller. Calling this code willy nilly can result in the loss of data.
3815 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
3816 * B_CACHE for the non-VMIO case.
3819 allocbuf(struct buf *bp, int size)
3823 BUF_ASSERT_HELD(bp);
3825 if (bp->b_bcount == size)
3828 if (bp->b_kvasize != 0 && bp->b_kvasize < size)
3829 panic("allocbuf: buffer too small");
3831 newbsize = (size + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
3832 if ((bp->b_flags & B_VMIO) == 0) {
3833 if ((bp->b_flags & B_MALLOC) == 0)
3834 newbsize = round_page(newbsize);
3836 * Just get anonymous memory from the kernel. Don't
3837 * mess with B_CACHE.
3839 if (newbsize < bp->b_bufsize)
3840 vfs_nonvmio_truncate(bp, newbsize);
3841 else if (newbsize > bp->b_bufsize)
3842 vfs_nonvmio_extend(bp, newbsize);
3846 desiredpages = (size == 0) ? 0 :
3847 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
3849 if (bp->b_flags & B_MALLOC)
3850 panic("allocbuf: VMIO buffer can't be malloced");
3852 * Set B_CACHE initially if buffer is 0 length or will become
3855 if (size == 0 || bp->b_bufsize == 0)
3856 bp->b_flags |= B_CACHE;
3858 if (newbsize < bp->b_bufsize)
3859 vfs_vmio_truncate(bp, desiredpages);
3860 /* XXX This looks as if it should be newbsize > b_bufsize */
3861 else if (size > bp->b_bcount)
3862 vfs_vmio_extend(bp, desiredpages, size);
3863 bufspace_adjust(bp, newbsize);
3865 bp->b_bcount = size; /* requested buffer size. */
3869 extern int inflight_transient_maps;
3872 biodone(struct bio *bp)
3875 void (*done)(struct bio *);
3876 vm_offset_t start, end;
3878 if ((bp->bio_flags & BIO_TRANSIENT_MAPPING) != 0) {
3879 bp->bio_flags &= ~BIO_TRANSIENT_MAPPING;
3880 bp->bio_flags |= BIO_UNMAPPED;
3881 start = trunc_page((vm_offset_t)bp->bio_data);
3882 end = round_page((vm_offset_t)bp->bio_data + bp->bio_length);
3883 bp->bio_data = unmapped_buf;
3884 pmap_qremove(start, OFF_TO_IDX(end - start));
3885 vmem_free(transient_arena, start, end - start);
3886 atomic_add_int(&inflight_transient_maps, -1);
3888 done = bp->bio_done;
3890 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3892 bp->bio_flags |= BIO_DONE;
3896 bp->bio_flags |= BIO_DONE;
3902 * Wait for a BIO to finish.
3905 biowait(struct bio *bp, const char *wchan)
3909 mtxp = mtx_pool_find(mtxpool_sleep, bp);
3911 while ((bp->bio_flags & BIO_DONE) == 0)
3912 msleep(bp, mtxp, PRIBIO, wchan, 0);
3914 if (bp->bio_error != 0)
3915 return (bp->bio_error);
3916 if (!(bp->bio_flags & BIO_ERROR))
3922 biofinish(struct bio *bp, struct devstat *stat, int error)
3926 bp->bio_error = error;
3927 bp->bio_flags |= BIO_ERROR;
3930 devstat_end_transaction_bio(stat, bp);
3937 * Wait for buffer I/O completion, returning error status. The buffer
3938 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
3939 * error and cleared.
3942 bufwait(struct buf *bp)
3944 if (bp->b_iocmd == BIO_READ)
3945 bwait(bp, PRIBIO, "biord");
3947 bwait(bp, PRIBIO, "biowr");
3948 if (bp->b_flags & B_EINTR) {
3949 bp->b_flags &= ~B_EINTR;
3952 if (bp->b_ioflags & BIO_ERROR) {
3953 return (bp->b_error ? bp->b_error : EIO);
3962 * Finish I/O on a buffer, optionally calling a completion function.
3963 * This is usually called from an interrupt so process blocking is
3966 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
3967 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
3968 * assuming B_INVAL is clear.
3970 * For the VMIO case, we set B_CACHE if the op was a read and no
3971 * read error occured, or if the op was a write. B_CACHE is never
3972 * set if the buffer is invalid or otherwise uncacheable.
3974 * biodone does not mess with B_INVAL, allowing the I/O routine or the
3975 * initiator to leave B_INVAL set to brelse the buffer out of existance
3976 * in the biodone routine.
3979 bufdone(struct buf *bp)
3981 struct bufobj *dropobj;
3982 void (*biodone)(struct buf *);
3984 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
3987 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
3988 BUF_ASSERT_HELD(bp);
3990 runningbufwakeup(bp);
3991 if (bp->b_iocmd == BIO_WRITE)
3992 dropobj = bp->b_bufobj;
3993 /* call optional completion function if requested */
3994 if (bp->b_iodone != NULL) {
3995 biodone = bp->b_iodone;
3996 bp->b_iodone = NULL;
3999 bufobj_wdrop(dropobj);
4006 bufobj_wdrop(dropobj);
4010 bufdone_finish(struct buf *bp)
4012 BUF_ASSERT_HELD(bp);
4014 if (!LIST_EMPTY(&bp->b_dep))
4017 if (bp->b_flags & B_VMIO) {
4019 * Set B_CACHE if the op was a normal read and no error
4020 * occured. B_CACHE is set for writes in the b*write()
4023 if (bp->b_iocmd == BIO_READ &&
4024 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
4025 !(bp->b_ioflags & BIO_ERROR))
4026 bp->b_flags |= B_CACHE;
4027 vfs_vmio_iodone(bp);
4031 * For asynchronous completions, release the buffer now. The brelse
4032 * will do a wakeup there if necessary - so no need to do a wakeup
4033 * here in the async case. The sync case always needs to do a wakeup.
4035 if (bp->b_flags & B_ASYNC) {
4036 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) ||
4037 (bp->b_ioflags & BIO_ERROR))
4046 * This routine is called in lieu of iodone in the case of
4047 * incomplete I/O. This keeps the busy status for pages
4051 vfs_unbusy_pages(struct buf *bp)
4057 runningbufwakeup(bp);
4058 if (!(bp->b_flags & B_VMIO))
4061 obj = bp->b_bufobj->bo_object;
4062 VM_OBJECT_WLOCK(obj);
4063 for (i = 0; i < bp->b_npages; i++) {
4065 if (m == bogus_page) {
4066 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
4068 panic("vfs_unbusy_pages: page missing\n");
4070 if (buf_mapped(bp)) {
4071 BUF_CHECK_MAPPED(bp);
4072 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
4073 bp->b_pages, bp->b_npages);
4075 BUF_CHECK_UNMAPPED(bp);
4079 vm_object_pip_wakeupn(obj, bp->b_npages);
4080 VM_OBJECT_WUNLOCK(obj);
4084 * vfs_page_set_valid:
4086 * Set the valid bits in a page based on the supplied offset. The
4087 * range is restricted to the buffer's size.
4089 * This routine is typically called after a read completes.
4092 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m)
4097 * Compute the end offset, eoff, such that [off, eoff) does not span a
4098 * page boundary and eoff is not greater than the end of the buffer.
4099 * The end of the buffer, in this case, is our file EOF, not the
4100 * allocation size of the buffer.
4102 eoff = (off + PAGE_SIZE) & ~(vm_ooffset_t)PAGE_MASK;
4103 if (eoff > bp->b_offset + bp->b_bcount)
4104 eoff = bp->b_offset + bp->b_bcount;
4107 * Set valid range. This is typically the entire buffer and thus the
4111 vm_page_set_valid_range(m, off & PAGE_MASK, eoff - off);
4115 * vfs_page_set_validclean:
4117 * Set the valid bits and clear the dirty bits in a page based on the
4118 * supplied offset. The range is restricted to the buffer's size.
4121 vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off, vm_page_t m)
4123 vm_ooffset_t soff, eoff;
4126 * Start and end offsets in buffer. eoff - soff may not cross a
4127 * page boundry or cross the end of the buffer. The end of the
4128 * buffer, in this case, is our file EOF, not the allocation size
4132 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
4133 if (eoff > bp->b_offset + bp->b_bcount)
4134 eoff = bp->b_offset + bp->b_bcount;
4137 * Set valid range. This is typically the entire buffer and thus the
4141 vm_page_set_validclean(
4143 (vm_offset_t) (soff & PAGE_MASK),
4144 (vm_offset_t) (eoff - soff)
4150 * Ensure that all buffer pages are not exclusive busied. If any page is
4151 * exclusive busy, drain it.
4154 vfs_drain_busy_pages(struct buf *bp)
4159 VM_OBJECT_ASSERT_WLOCKED(bp->b_bufobj->bo_object);
4161 for (i = 0; i < bp->b_npages; i++) {
4163 if (vm_page_xbusied(m)) {
4164 for (; last_busied < i; last_busied++)
4165 vm_page_sbusy(bp->b_pages[last_busied]);
4166 while (vm_page_xbusied(m)) {
4168 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
4169 vm_page_busy_sleep(m, "vbpage");
4170 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
4174 for (i = 0; i < last_busied; i++)
4175 vm_page_sunbusy(bp->b_pages[i]);
4179 * This routine is called before a device strategy routine.
4180 * It is used to tell the VM system that paging I/O is in
4181 * progress, and treat the pages associated with the buffer
4182 * almost as being exclusive busy. Also the object paging_in_progress
4183 * flag is handled to make sure that the object doesn't become
4186 * Since I/O has not been initiated yet, certain buffer flags
4187 * such as BIO_ERROR or B_INVAL may be in an inconsistant state
4188 * and should be ignored.
4191 vfs_busy_pages(struct buf *bp, int clear_modify)
4198 if (!(bp->b_flags & B_VMIO))
4201 obj = bp->b_bufobj->bo_object;
4202 foff = bp->b_offset;
4203 KASSERT(bp->b_offset != NOOFFSET,
4204 ("vfs_busy_pages: no buffer offset"));
4205 VM_OBJECT_WLOCK(obj);
4206 vfs_drain_busy_pages(bp);
4207 if (bp->b_bufsize != 0)
4208 vfs_setdirty_locked_object(bp);
4210 for (i = 0; i < bp->b_npages; i++) {
4213 if ((bp->b_flags & B_CLUSTER) == 0) {
4214 vm_object_pip_add(obj, 1);
4218 * When readying a buffer for a read ( i.e
4219 * clear_modify == 0 ), it is important to do
4220 * bogus_page replacement for valid pages in
4221 * partially instantiated buffers. Partially
4222 * instantiated buffers can, in turn, occur when
4223 * reconstituting a buffer from its VM backing store
4224 * base. We only have to do this if B_CACHE is
4225 * clear ( which causes the I/O to occur in the
4226 * first place ). The replacement prevents the read
4227 * I/O from overwriting potentially dirty VM-backed
4228 * pages. XXX bogus page replacement is, uh, bogus.
4229 * It may not work properly with small-block devices.
4230 * We need to find a better way.
4233 pmap_remove_write(m);
4234 vfs_page_set_validclean(bp, foff, m);
4235 } else if (m->valid == VM_PAGE_BITS_ALL &&
4236 (bp->b_flags & B_CACHE) == 0) {
4237 bp->b_pages[i] = bogus_page;
4240 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
4242 VM_OBJECT_WUNLOCK(obj);
4243 if (bogus && buf_mapped(bp)) {
4244 BUF_CHECK_MAPPED(bp);
4245 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
4246 bp->b_pages, bp->b_npages);
4251 * vfs_bio_set_valid:
4253 * Set the range within the buffer to valid. The range is
4254 * relative to the beginning of the buffer, b_offset. Note that
4255 * b_offset itself may be offset from the beginning of the first
4259 vfs_bio_set_valid(struct buf *bp, int base, int size)
4264 if (!(bp->b_flags & B_VMIO))
4268 * Fixup base to be relative to beginning of first page.
4269 * Set initial n to be the maximum number of bytes in the
4270 * first page that can be validated.
4272 base += (bp->b_offset & PAGE_MASK);
4273 n = PAGE_SIZE - (base & PAGE_MASK);
4275 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
4276 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
4280 vm_page_set_valid_range(m, base & PAGE_MASK, n);
4285 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
4291 * If the specified buffer is a non-VMIO buffer, clear the entire
4292 * buffer. If the specified buffer is a VMIO buffer, clear and
4293 * validate only the previously invalid portions of the buffer.
4294 * This routine essentially fakes an I/O, so we need to clear
4295 * BIO_ERROR and B_INVAL.
4297 * Note that while we only theoretically need to clear through b_bcount,
4298 * we go ahead and clear through b_bufsize.
4301 vfs_bio_clrbuf(struct buf *bp)
4303 int i, j, mask, sa, ea, slide;
4305 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
4309 bp->b_flags &= ~B_INVAL;
4310 bp->b_ioflags &= ~BIO_ERROR;
4311 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
4312 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
4313 (bp->b_offset & PAGE_MASK) == 0) {
4314 if (bp->b_pages[0] == bogus_page)
4316 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
4317 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[0]->object);
4318 if ((bp->b_pages[0]->valid & mask) == mask)
4320 if ((bp->b_pages[0]->valid & mask) == 0) {
4321 pmap_zero_page_area(bp->b_pages[0], 0, bp->b_bufsize);
4322 bp->b_pages[0]->valid |= mask;
4326 sa = bp->b_offset & PAGE_MASK;
4328 for (i = 0; i < bp->b_npages; i++, sa = 0) {
4329 slide = imin(slide + PAGE_SIZE, bp->b_offset + bp->b_bufsize);
4330 ea = slide & PAGE_MASK;
4333 if (bp->b_pages[i] == bogus_page)
4336 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
4337 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[i]->object);
4338 if ((bp->b_pages[i]->valid & mask) == mask)
4340 if ((bp->b_pages[i]->valid & mask) == 0)
4341 pmap_zero_page_area(bp->b_pages[i], sa, ea - sa);
4343 for (; sa < ea; sa += DEV_BSIZE, j++) {
4344 if ((bp->b_pages[i]->valid & (1 << j)) == 0) {
4345 pmap_zero_page_area(bp->b_pages[i],
4350 bp->b_pages[i]->valid |= mask;
4353 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
4358 vfs_bio_bzero_buf(struct buf *bp, int base, int size)
4363 if (buf_mapped(bp)) {
4364 BUF_CHECK_MAPPED(bp);
4365 bzero(bp->b_data + base, size);
4367 BUF_CHECK_UNMAPPED(bp);
4368 n = PAGE_SIZE - (base & PAGE_MASK);
4369 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
4373 pmap_zero_page_area(m, base & PAGE_MASK, n);
4382 * vm_hold_load_pages and vm_hold_free_pages get pages into
4383 * a buffers address space. The pages are anonymous and are
4384 * not associated with a file object.
4387 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
4393 BUF_CHECK_MAPPED(bp);
4395 to = round_page(to);
4396 from = round_page(from);
4397 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
4399 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
4402 * note: must allocate system pages since blocking here
4403 * could interfere with paging I/O, no matter which
4406 p = vm_page_alloc(NULL, 0, VM_ALLOC_SYSTEM | VM_ALLOC_NOOBJ |
4407 VM_ALLOC_WIRED | VM_ALLOC_COUNT((to - pg) >> PAGE_SHIFT));
4412 pmap_qenter(pg, &p, 1);
4413 bp->b_pages[index] = p;
4415 bp->b_npages = index;
4418 /* Return pages associated with this buf to the vm system */
4420 vm_hold_free_pages(struct buf *bp, int newbsize)
4424 int index, newnpages;
4426 BUF_CHECK_MAPPED(bp);
4428 from = round_page((vm_offset_t)bp->b_data + newbsize);
4429 newnpages = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
4430 if (bp->b_npages > newnpages)
4431 pmap_qremove(from, bp->b_npages - newnpages);
4432 for (index = newnpages; index < bp->b_npages; index++) {
4433 p = bp->b_pages[index];
4434 bp->b_pages[index] = NULL;
4435 if (vm_page_sbusied(p))
4436 printf("vm_hold_free_pages: blkno: %jd, lblkno: %jd\n",
4437 (intmax_t)bp->b_blkno, (intmax_t)bp->b_lblkno);
4440 atomic_subtract_int(&vm_cnt.v_wire_count, 1);
4442 bp->b_npages = newnpages;
4446 * Map an IO request into kernel virtual address space.
4448 * All requests are (re)mapped into kernel VA space.
4449 * Notice that we use b_bufsize for the size of the buffer
4450 * to be mapped. b_bcount might be modified by the driver.
4452 * Note that even if the caller determines that the address space should
4453 * be valid, a race or a smaller-file mapped into a larger space may
4454 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
4455 * check the return value.
4457 * This function only works with pager buffers.
4460 vmapbuf(struct buf *bp, int mapbuf)
4465 if (bp->b_bufsize < 0)
4467 prot = VM_PROT_READ;
4468 if (bp->b_iocmd == BIO_READ)
4469 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
4470 if ((pidx = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
4471 (vm_offset_t)bp->b_data, bp->b_bufsize, prot, bp->b_pages,
4472 btoc(MAXPHYS))) < 0)
4474 bp->b_npages = pidx;
4475 bp->b_offset = ((vm_offset_t)bp->b_data) & PAGE_MASK;
4476 if (mapbuf || !unmapped_buf_allowed) {
4477 pmap_qenter((vm_offset_t)bp->b_kvabase, bp->b_pages, pidx);
4478 bp->b_data = bp->b_kvabase + bp->b_offset;
4480 bp->b_data = unmapped_buf;
4485 * Free the io map PTEs associated with this IO operation.
4486 * We also invalidate the TLB entries and restore the original b_addr.
4488 * This function only works with pager buffers.
4491 vunmapbuf(struct buf *bp)
4495 npages = bp->b_npages;
4497 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
4498 vm_page_unhold_pages(bp->b_pages, npages);
4500 bp->b_data = unmapped_buf;
4504 bdone(struct buf *bp)
4508 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4510 bp->b_flags |= B_DONE;
4516 bwait(struct buf *bp, u_char pri, const char *wchan)
4520 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4522 while ((bp->b_flags & B_DONE) == 0)
4523 msleep(bp, mtxp, pri, wchan, 0);
4528 bufsync(struct bufobj *bo, int waitfor)
4531 return (VOP_FSYNC(bo->__bo_vnode, waitfor, curthread));
4535 bufstrategy(struct bufobj *bo, struct buf *bp)
4541 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
4542 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
4543 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
4544 i = VOP_STRATEGY(vp, bp);
4545 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
4549 bufobj_wrefl(struct bufobj *bo)
4552 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
4553 ASSERT_BO_WLOCKED(bo);
4558 bufobj_wref(struct bufobj *bo)
4561 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
4568 bufobj_wdrop(struct bufobj *bo)
4571 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
4573 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
4574 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
4575 bo->bo_flag &= ~BO_WWAIT;
4576 wakeup(&bo->bo_numoutput);
4582 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
4586 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
4587 ASSERT_BO_WLOCKED(bo);
4589 while (bo->bo_numoutput) {
4590 bo->bo_flag |= BO_WWAIT;
4591 error = msleep(&bo->bo_numoutput, BO_LOCKPTR(bo),
4592 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
4600 bpin(struct buf *bp)
4604 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4611 bunpin(struct buf *bp)
4615 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4617 if (--bp->b_pin_count == 0)
4623 bunpin_wait(struct buf *bp)
4627 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4629 while (bp->b_pin_count > 0)
4630 msleep(bp, mtxp, PRIBIO, "bwunpin", 0);
4635 * Set bio_data or bio_ma for struct bio from the struct buf.
4638 bdata2bio(struct buf *bp, struct bio *bip)
4641 if (!buf_mapped(bp)) {
4642 KASSERT(unmapped_buf_allowed, ("unmapped"));
4643 bip->bio_ma = bp->b_pages;
4644 bip->bio_ma_n = bp->b_npages;
4645 bip->bio_data = unmapped_buf;
4646 bip->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
4647 bip->bio_flags |= BIO_UNMAPPED;
4648 KASSERT(round_page(bip->bio_ma_offset + bip->bio_length) /
4649 PAGE_SIZE == bp->b_npages,
4650 ("Buffer %p too short: %d %lld %d", bp, bip->bio_ma_offset,
4651 (long long)bip->bio_length, bip->bio_ma_n));
4653 bip->bio_data = bp->b_data;
4658 #include "opt_ddb.h"
4660 #include <ddb/ddb.h>
4662 /* DDB command to show buffer data */
4663 DB_SHOW_COMMAND(buffer, db_show_buffer)
4666 struct buf *bp = (struct buf *)addr;
4669 db_printf("usage: show buffer <addr>\n");
4673 db_printf("buf at %p\n", bp);
4674 db_printf("b_flags = 0x%b, b_xflags=0x%b, b_vflags=0x%b\n",
4675 (u_int)bp->b_flags, PRINT_BUF_FLAGS, (u_int)bp->b_xflags,
4676 PRINT_BUF_XFLAGS, (u_int)bp->b_vflags, PRINT_BUF_VFLAGS);
4678 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
4679 "b_bufobj = (%p), b_data = %p, b_blkno = %jd, b_lblkno = %jd, "
4681 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
4682 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno,
4683 (intmax_t)bp->b_lblkno, bp->b_dep.lh_first);
4684 db_printf("b_kvabase = %p, b_kvasize = %d\n",
4685 bp->b_kvabase, bp->b_kvasize);
4688 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
4689 for (i = 0; i < bp->b_npages; i++) {
4692 db_printf("(%p, 0x%lx, 0x%lx)", (void *)m->object,
4693 (u_long)m->pindex, (u_long)VM_PAGE_TO_PHYS(m));
4694 if ((i + 1) < bp->b_npages)
4700 BUF_LOCKPRINTINFO(bp);
4703 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
4708 for (i = 0; i < nbuf; i++) {
4710 if (BUF_ISLOCKED(bp)) {
4711 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4717 DB_SHOW_COMMAND(vnodebufs, db_show_vnodebufs)
4723 db_printf("usage: show vnodebufs <addr>\n");
4726 vp = (struct vnode *)addr;
4727 db_printf("Clean buffers:\n");
4728 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_clean.bv_hd, b_bobufs) {
4729 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4732 db_printf("Dirty buffers:\n");
4733 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
4734 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
4739 DB_COMMAND(countfreebufs, db_coundfreebufs)
4742 int i, used = 0, nfree = 0;
4745 db_printf("usage: countfreebufs\n");
4749 for (i = 0; i < nbuf; i++) {
4751 if (bp->b_qindex == QUEUE_EMPTY)
4757 db_printf("Counted %d free, %d used (%d tot)\n", nfree, used,
4759 db_printf("numfreebuffers is %d\n", numfreebuffers);