2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 Poul-Henning Kamp
5 * Copyright (c) 1994,1997 John S. Dyson
6 * Copyright (c) 2013 The FreeBSD Foundation
9 * Portions of this software were developed by Konstantin Belousov
10 * under sponsorship from the FreeBSD Foundation.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * this file contains a new buffer I/O scheme implementing a coherent
36 * VM object and buffer cache scheme. Pains have been taken to make
37 * sure that the performance degradation associated with schemes such
38 * as this is not realized.
40 * Author: John S. Dyson
41 * Significant help during the development and debugging phases
42 * had been provided by David Greenman, also of the FreeBSD core team.
44 * see man buf(9) for more info.
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
51 #include <sys/systm.h>
54 #include <sys/counter.h>
56 #include <sys/devicestat.h>
57 #include <sys/eventhandler.h>
59 #include <sys/limits.h>
61 #include <sys/malloc.h>
62 #include <sys/mount.h>
63 #include <sys/mutex.h>
64 #include <sys/kernel.h>
65 #include <sys/kthread.h>
67 #include <sys/racct.h>
68 #include <sys/resourcevar.h>
69 #include <sys/rwlock.h>
71 #include <sys/sysctl.h>
72 #include <sys/sysproto.h>
74 #include <sys/vmmeter.h>
75 #include <sys/vnode.h>
76 #include <sys/watchdog.h>
77 #include <geom/geom.h>
79 #include <vm/vm_param.h>
80 #include <vm/vm_kern.h>
81 #include <vm/vm_object.h>
82 #include <vm/vm_page.h>
83 #include <vm/vm_pageout.h>
84 #include <vm/vm_pager.h>
85 #include <vm/vm_extern.h>
86 #include <vm/vm_map.h>
87 #include <vm/swap_pager.h>
88 #include "opt_compat.h"
91 static MALLOC_DEFINE(M_BIOBUF, "biobuf", "BIO buffer");
93 struct bio_ops bioops; /* I/O operation notification */
95 struct buf_ops buf_ops_bio = {
96 .bop_name = "buf_ops_bio",
97 .bop_write = bufwrite,
98 .bop_strategy = bufstrategy,
100 .bop_bdflush = bufbdflush,
103 static struct buf *buf; /* buffer header pool */
104 extern struct buf *swbuf; /* Swap buffer header pool. */
105 caddr_t unmapped_buf;
107 /* Used below and for softdep flushing threads in ufs/ffs/ffs_softdep.c */
108 struct proc *bufdaemonproc;
110 static int inmem(struct vnode *vp, daddr_t blkno);
111 static void vm_hold_free_pages(struct buf *bp, int newbsize);
112 static void vm_hold_load_pages(struct buf *bp, vm_offset_t from,
114 static void vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m);
115 static void vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off,
117 static void vfs_clean_pages_dirty_buf(struct buf *bp);
118 static void vfs_setdirty_locked_object(struct buf *bp);
119 static void vfs_vmio_invalidate(struct buf *bp);
120 static void vfs_vmio_truncate(struct buf *bp, int npages);
121 static void vfs_vmio_extend(struct buf *bp, int npages, int size);
122 static int vfs_bio_clcheck(struct vnode *vp, int size,
123 daddr_t lblkno, daddr_t blkno);
124 static void breada(struct vnode *, daddr_t *, int *, int, struct ucred *, int,
125 void (*)(struct buf *));
126 static int buf_flush(struct vnode *vp, int);
127 static int flushbufqueues(struct vnode *, int, int);
128 static void buf_daemon(void);
129 static __inline void bd_wakeup(void);
130 static int sysctl_runningspace(SYSCTL_HANDLER_ARGS);
131 static void bufkva_reclaim(vmem_t *, int);
132 static void bufkva_free(struct buf *);
133 static int buf_import(void *, void **, int, int, int);
134 static void buf_release(void *, void **, int);
135 static void maxbcachebuf_adjust(void);
137 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 SYSCTL_PROC(_vfs, OID_AUTO, bufspace, CTLTYPE_LONG|CTLFLAG_MPSAFE|CTLFLAG_RD,
145 NULL, 0, sysctl_bufspace, "L", "Physical memory used for buffers");
146 static counter_u64_t bufkvaspace;
147 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, bufkvaspace, CTLFLAG_RD, &bufkvaspace,
148 "Kernel virtual memory used for buffers");
149 static long maxbufspace;
150 SYSCTL_LONG(_vfs, OID_AUTO, maxbufspace, CTLFLAG_RW, &maxbufspace, 0,
151 "Maximum allowed value of bufspace (including metadata)");
152 static long bufmallocspace;
153 SYSCTL_LONG(_vfs, OID_AUTO, bufmallocspace, CTLFLAG_RD, &bufmallocspace, 0,
154 "Amount of malloced memory for buffers");
155 static long maxbufmallocspace;
156 SYSCTL_LONG(_vfs, OID_AUTO, maxmallocbufspace, CTLFLAG_RW, &maxbufmallocspace,
157 0, "Maximum amount of malloced memory for buffers");
158 static long lobufspace;
159 SYSCTL_LONG(_vfs, OID_AUTO, lobufspace, CTLFLAG_RW, &lobufspace, 0,
160 "Minimum amount of buffers we want to have");
162 SYSCTL_LONG(_vfs, OID_AUTO, hibufspace, CTLFLAG_RW, &hibufspace, 0,
163 "Maximum allowed value of bufspace (excluding metadata)");
165 SYSCTL_LONG(_vfs, OID_AUTO, bufspacethresh, CTLFLAG_RW, &bufspacethresh,
166 0, "Bufspace consumed before waking the daemon to free some");
167 static counter_u64_t buffreekvacnt;
168 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, buffreekvacnt, CTLFLAG_RW, &buffreekvacnt,
169 "Number of times we have freed the KVA space from some buffer");
170 static counter_u64_t bufdefragcnt;
171 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, bufdefragcnt, CTLFLAG_RW, &bufdefragcnt,
172 "Number of times we have had to repeat buffer allocation to defragment");
173 static long lorunningspace;
174 SYSCTL_PROC(_vfs, OID_AUTO, lorunningspace, CTLTYPE_LONG | CTLFLAG_MPSAFE |
175 CTLFLAG_RW, &lorunningspace, 0, sysctl_runningspace, "L",
176 "Minimum preferred space used for in-progress I/O");
177 static long hirunningspace;
178 SYSCTL_PROC(_vfs, OID_AUTO, hirunningspace, CTLTYPE_LONG | CTLFLAG_MPSAFE |
179 CTLFLAG_RW, &hirunningspace, 0, sysctl_runningspace, "L",
180 "Maximum amount of space to use for in-progress I/O");
181 int dirtybufferflushes;
182 SYSCTL_INT(_vfs, OID_AUTO, dirtybufferflushes, CTLFLAG_RW, &dirtybufferflushes,
183 0, "Number of bdwrite to bawrite conversions to limit dirty buffers");
185 SYSCTL_INT(_vfs, OID_AUTO, bdwriteskip, CTLFLAG_RW, &bdwriteskip,
186 0, "Number of buffers supplied to bdwrite with snapshot deadlock risk");
187 int altbufferflushes;
188 SYSCTL_INT(_vfs, OID_AUTO, altbufferflushes, CTLFLAG_RW, &altbufferflushes,
189 0, "Number of fsync flushes to limit dirty buffers");
190 static int recursiveflushes;
191 SYSCTL_INT(_vfs, OID_AUTO, recursiveflushes, CTLFLAG_RW, &recursiveflushes,
192 0, "Number of flushes skipped due to being recursive");
193 static int numdirtybuffers;
194 SYSCTL_INT(_vfs, OID_AUTO, numdirtybuffers, CTLFLAG_RD, &numdirtybuffers, 0,
195 "Number of buffers that are dirty (has unwritten changes) at the moment");
196 static int lodirtybuffers;
197 SYSCTL_INT(_vfs, OID_AUTO, lodirtybuffers, CTLFLAG_RW, &lodirtybuffers, 0,
198 "How many buffers we want to have free before bufdaemon can sleep");
199 static int hidirtybuffers;
200 SYSCTL_INT(_vfs, OID_AUTO, hidirtybuffers, CTLFLAG_RW, &hidirtybuffers, 0,
201 "When the number of dirty buffers is considered severe");
203 SYSCTL_INT(_vfs, OID_AUTO, dirtybufthresh, CTLFLAG_RW, &dirtybufthresh,
204 0, "Number of bdwrite to bawrite conversions to clear dirty buffers");
205 static int numfreebuffers;
206 SYSCTL_INT(_vfs, OID_AUTO, numfreebuffers, CTLFLAG_RD, &numfreebuffers, 0,
207 "Number of free buffers");
208 static int lofreebuffers;
209 SYSCTL_INT(_vfs, OID_AUTO, lofreebuffers, CTLFLAG_RW, &lofreebuffers, 0,
210 "Target number of free buffers");
211 static int hifreebuffers;
212 SYSCTL_INT(_vfs, OID_AUTO, hifreebuffers, CTLFLAG_RW, &hifreebuffers, 0,
213 "Threshold for clean buffer recycling");
214 static counter_u64_t getnewbufcalls;
215 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, getnewbufcalls, CTLFLAG_RD,
216 &getnewbufcalls, "Number of calls to getnewbuf");
217 static counter_u64_t getnewbufrestarts;
218 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, getnewbufrestarts, CTLFLAG_RD,
220 "Number of times getnewbuf has had to restart a buffer acquisition");
221 static counter_u64_t mappingrestarts;
222 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, mappingrestarts, CTLFLAG_RD,
224 "Number of times getblk has had to restart a buffer mapping for "
226 static counter_u64_t numbufallocfails;
227 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, numbufallocfails, CTLFLAG_RW,
228 &numbufallocfails, "Number of times buffer allocations failed");
229 static int flushbufqtarget = 100;
230 SYSCTL_INT(_vfs, OID_AUTO, flushbufqtarget, CTLFLAG_RW, &flushbufqtarget, 0,
231 "Amount of work to do in flushbufqueues when helping bufdaemon");
232 static counter_u64_t notbufdflushes;
233 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, notbufdflushes, CTLFLAG_RD, ¬bufdflushes,
234 "Number of dirty buffer flushes done by the bufdaemon helpers");
235 static long barrierwrites;
236 SYSCTL_LONG(_vfs, OID_AUTO, barrierwrites, CTLFLAG_RW, &barrierwrites, 0,
237 "Number of barrier writes");
238 SYSCTL_INT(_vfs, OID_AUTO, unmapped_buf_allowed, CTLFLAG_RD,
239 &unmapped_buf_allowed, 0,
240 "Permit the use of the unmapped i/o");
241 int maxbcachebuf = MAXBCACHEBUF;
242 SYSCTL_INT(_vfs, OID_AUTO, maxbcachebuf, CTLFLAG_RDTUN, &maxbcachebuf, 0,
243 "Maximum size of a buffer cache block");
246 * This lock synchronizes access to bd_request.
248 static struct mtx_padalign __exclusive_cache_line bdlock;
251 * This lock protects the runningbufreq and synchronizes runningbufwakeup and
252 * waitrunningbufspace().
254 static struct mtx_padalign __exclusive_cache_line rbreqlock;
257 * Lock that protects bdirtywait.
259 static struct mtx_padalign __exclusive_cache_line bdirtylock;
262 * Wakeup point for bufdaemon, as well as indicator of whether it is already
263 * active. Set to 1 when the bufdaemon is already "on" the queue, 0 when it
266 static int bd_request;
269 * Request for the buf daemon to write more buffers than is indicated by
270 * lodirtybuf. This may be necessary to push out excess dependencies or
271 * defragment the address space where a simple count of the number of dirty
272 * buffers is insufficient to characterize the demand for flushing them.
274 static int bd_speedupreq;
277 * Synchronization (sleep/wakeup) variable for active buffer space requests.
278 * Set when wait starts, cleared prior to wakeup().
279 * Used in runningbufwakeup() and waitrunningbufspace().
281 static int runningbufreq;
284 * Synchronization for bwillwrite() waiters.
286 static int bdirtywait;
289 * Definitions for the buffer free lists.
291 #define QUEUE_NONE 0 /* on no queue */
292 #define QUEUE_EMPTY 1 /* empty buffer headers */
293 #define QUEUE_DIRTY 2 /* B_DELWRI buffers */
294 #define QUEUE_CLEAN 3 /* non-B_DELWRI buffers */
295 #define QUEUE_SENTINEL 4 /* not an queue index, but mark for sentinel */
298 struct mtx_padalign bq_lock;
299 TAILQ_HEAD(, buf) bq_queue;
301 uint16_t bq_subqueue;
303 } __aligned(CACHE_LINE_SIZE);
305 #define BQ_LOCKPTR(bq) (&(bq)->bq_lock)
306 #define BQ_LOCK(bq) mtx_lock(BQ_LOCKPTR((bq)))
307 #define BQ_UNLOCK(bq) mtx_unlock(BQ_LOCKPTR((bq)))
308 #define BQ_ASSERT_LOCKED(bq) mtx_assert(BQ_LOCKPTR((bq)), MA_OWNED)
310 struct bufqueue __exclusive_cache_line bqempty;
311 struct bufqueue __exclusive_cache_line bqdirty;
314 struct bufqueue bd_subq[MAXCPU + 1]; /* Per-cpu sub queues + global */
315 struct bufqueue *bd_cleanq;
316 struct mtx_padalign bd_run_lock;
321 long bd_bufspacethresh;
322 int bd_hifreebuffers;
323 int bd_lofreebuffers;
327 int __aligned(CACHE_LINE_SIZE) bd_running;
328 long __aligned(CACHE_LINE_SIZE) bd_bufspace;
329 int __aligned(CACHE_LINE_SIZE) bd_freebuffers;
330 } __aligned(CACHE_LINE_SIZE);
332 #define BD_LOCKPTR(bd) (&(bd)->bd_cleanq->bq_lock)
333 #define BD_LOCK(bd) mtx_lock(BD_LOCKPTR((bd)))
334 #define BD_UNLOCK(bd) mtx_unlock(BD_LOCKPTR((bd)))
335 #define BD_ASSERT_LOCKED(bd) mtx_assert(BD_LOCKPTR((bd)), MA_OWNED)
336 #define BD_RUN_LOCKPTR(bd) (&(bd)->bd_run_lock)
337 #define BD_RUN_LOCK(bd) mtx_lock(BD_RUN_LOCKPTR((bd)))
338 #define BD_RUN_UNLOCK(bd) mtx_unlock(BD_RUN_LOCKPTR((bd)))
339 #define BD_DOMAIN(bd) (bd - bdclean)
341 /* Maximum number of clean buffer domains. */
342 #define CLEAN_DOMAINS 8
344 /* Configured number of clean queues. */
345 static int __read_mostly clean_domains;
347 struct bufdomain __exclusive_cache_line bdclean[CLEAN_DOMAINS];
349 static void bq_remove(struct bufqueue *bq, struct buf *bp);
350 static void bq_insert(struct bufqueue *bq, struct buf *bp, bool unlock);
351 static int buf_recycle(struct bufdomain *, bool kva);
352 static void bq_init(struct bufqueue *bq, int qindex, int cpu,
353 const char *lockname);
354 static void bd_init(struct bufdomain *bd);
355 static int bd_flushall(struct bufdomain *bd);
358 * per-cpu empty buffer cache.
363 * Single global constant for BUF_WMESG, to avoid getting multiple references.
364 * buf_wmesg is referred from macros.
366 const char *buf_wmesg = BUF_WMESG;
369 sysctl_runningspace(SYSCTL_HANDLER_ARGS)
374 value = *(long *)arg1;
375 error = sysctl_handle_long(oidp, &value, 0, req);
376 if (error != 0 || req->newptr == NULL)
378 mtx_lock(&rbreqlock);
379 if (arg1 == &hirunningspace) {
380 if (value < lorunningspace)
383 hirunningspace = value;
385 KASSERT(arg1 == &lorunningspace,
386 ("%s: unknown arg1", __func__));
387 if (value > hirunningspace)
390 lorunningspace = value;
392 mtx_unlock(&rbreqlock);
396 #if defined(COMPAT_FREEBSD4) || defined(COMPAT_FREEBSD5) || \
397 defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD7)
399 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
406 for (i = 0; i < clean_domains; i++)
407 lvalue += bdclean[i].bd_bufspace;
408 if (sizeof(int) == sizeof(long) || req->oldlen >= sizeof(long))
409 return (sysctl_handle_long(oidp, &lvalue, 0, req));
410 if (lvalue > INT_MAX)
411 /* On overflow, still write out a long to trigger ENOMEM. */
412 return (sysctl_handle_long(oidp, &lvalue, 0, req));
414 return (sysctl_handle_int(oidp, &ivalue, 0, req));
418 sysctl_bufspace(SYSCTL_HANDLER_ARGS)
424 for (i = 0; i < clean_domains; i++)
425 lvalue += bdclean[i].bd_bufspace;
426 return (sysctl_handle_int(oidp, &lvalue, 0, req));
433 * Wakeup any bwillwrite() waiters.
438 mtx_lock(&bdirtylock);
443 mtx_unlock(&bdirtylock);
449 * Decrement the numdirtybuffers count by one and wakeup any
450 * threads blocked in bwillwrite().
456 if (atomic_fetchadd_int(&numdirtybuffers, -1) ==
457 (lodirtybuffers + hidirtybuffers) / 2)
464 * Increment the numdirtybuffers count by one and wakeup the buf
472 * Only do the wakeup once as we cross the boundary. The
473 * buf daemon will keep running until the condition clears.
475 if (atomic_fetchadd_int(&numdirtybuffers, 1) ==
476 (lodirtybuffers + hidirtybuffers) / 2)
481 * bufspace_daemon_wakeup:
483 * Wakeup the daemons responsible for freeing clean bufs.
486 bufspace_daemon_wakeup(struct bufdomain *bd)
490 * avoid the lock if the daemon is running.
492 if (atomic_fetchadd_int(&bd->bd_running, 1) == 0) {
494 atomic_store_int(&bd->bd_running, 1);
495 wakeup(&bd->bd_running);
501 * bufspace_daemon_wait:
503 * Sleep until the domain falls below a limit or one second passes.
506 bufspace_daemon_wait(struct bufdomain *bd)
509 * Re-check our limits and sleep. bd_running must be
510 * cleared prior to checking the limits to avoid missed
511 * wakeups. The waker will adjust one of bufspace or
512 * freebuffers prior to checking bd_running.
515 atomic_store_int(&bd->bd_running, 0);
516 if (bd->bd_bufspace < bd->bd_bufspacethresh &&
517 bd->bd_freebuffers > bd->bd_lofreebuffers) {
518 msleep(&bd->bd_running, BD_RUN_LOCKPTR(bd), PRIBIO|PDROP,
521 /* Avoid spurious wakeups while running. */
522 atomic_store_int(&bd->bd_running, 1);
530 * Adjust the reported bufspace for a KVA managed buffer, possibly
531 * waking any waiters.
534 bufspace_adjust(struct buf *bp, int bufsize)
536 struct bufdomain *bd;
540 KASSERT((bp->b_flags & B_MALLOC) == 0,
541 ("bufspace_adjust: malloc buf %p", bp));
542 bd = &bdclean[bp->b_domain];
543 diff = bufsize - bp->b_bufsize;
545 atomic_subtract_long(&bd->bd_bufspace, -diff);
547 space = atomic_fetchadd_long(&bd->bd_bufspace, diff);
548 /* Wake up the daemon on the transition. */
549 if (space < bd->bd_bufspacethresh &&
550 space + diff >= bd->bd_bufspacethresh)
551 bufspace_daemon_wakeup(bd);
553 bp->b_bufsize = bufsize;
559 * Reserve bufspace before calling allocbuf(). metadata has a
560 * different space limit than data.
563 bufspace_reserve(struct bufdomain *bd, int size, bool metadata)
569 limit = bd->bd_maxbufspace;
571 limit = bd->bd_hibufspace;
572 space = atomic_fetchadd_long(&bd->bd_bufspace, size);
575 atomic_subtract_long(&bd->bd_bufspace, size);
579 /* Wake up the daemon on the transition. */
580 if (space < bd->bd_bufspacethresh && new >= bd->bd_bufspacethresh)
581 bufspace_daemon_wakeup(bd);
589 * Release reserved bufspace after bufspace_adjust() has consumed it.
592 bufspace_release(struct bufdomain *bd, int size)
595 atomic_subtract_long(&bd->bd_bufspace, size);
601 * Wait for bufspace, acting as the buf daemon if a locked vnode is
602 * supplied. bd_wanted must be set prior to polling for space. The
603 * operation must be re-tried on return.
606 bufspace_wait(struct bufdomain *bd, struct vnode *vp, int gbflags,
607 int slpflag, int slptimeo)
610 int error, fl, norunbuf;
612 if ((gbflags & GB_NOWAIT_BD) != 0)
617 while (bd->bd_wanted) {
618 if (vp != NULL && vp->v_type != VCHR &&
619 (td->td_pflags & TDP_BUFNEED) == 0) {
622 * getblk() is called with a vnode locked, and
623 * some majority of the dirty buffers may as
624 * well belong to the vnode. Flushing the
625 * buffers there would make a progress that
626 * cannot be achieved by the buf_daemon, that
627 * cannot lock the vnode.
629 norunbuf = ~(TDP_BUFNEED | TDP_NORUNNINGBUF) |
630 (td->td_pflags & TDP_NORUNNINGBUF);
633 * Play bufdaemon. The getnewbuf() function
634 * may be called while the thread owns lock
635 * for another dirty buffer for the same
636 * vnode, which makes it impossible to use
637 * VOP_FSYNC() there, due to the buffer lock
640 td->td_pflags |= TDP_BUFNEED | TDP_NORUNNINGBUF;
641 fl = buf_flush(vp, flushbufqtarget);
642 td->td_pflags &= norunbuf;
646 if (bd->bd_wanted == 0)
649 error = msleep(&bd->bd_wanted, BD_LOCKPTR(bd),
650 (PRIBIO + 4) | slpflag, "newbuf", slptimeo);
661 * buffer space management daemon. Tries to maintain some marginal
662 * amount of free buffer space so that requesting processes neither
663 * block nor work to reclaim buffers.
666 bufspace_daemon(void *arg)
668 struct bufdomain *bd;
672 kproc_suspend_check(curproc);
675 * Free buffers from the clean queue until we meet our
678 * Theory of operation: The buffer cache is most efficient
679 * when some free buffer headers and space are always
680 * available to getnewbuf(). This daemon attempts to prevent
681 * the excessive blocking and synchronization associated
682 * with shortfall. It goes through three phases according
685 * 1) The daemon wakes up voluntarily once per-second
686 * during idle periods when the counters are below
687 * the wakeup thresholds (bufspacethresh, lofreebuffers).
689 * 2) The daemon wakes up as we cross the thresholds
690 * ahead of any potential blocking. This may bounce
691 * slightly according to the rate of consumption and
694 * 3) The daemon and consumers are starved for working
695 * clean buffers. This is the 'bufspace' sleep below
696 * which will inefficiently trade bufs with bqrelse
697 * until we return to condition 2.
700 if (buf_recycle(bd, false) != 0) {
705 msleep(&bd->bd_wanted, BD_LOCKPTR(bd),
706 PRIBIO|PDROP, "bufspace", hz/10);
711 } while (bd->bd_bufspace > bd->bd_lobufspace ||
712 bd->bd_freebuffers < bd->bd_hifreebuffers);
714 bufspace_daemon_wait(bd);
721 * Adjust the reported bufspace for a malloc managed buffer, possibly
722 * waking any waiters.
725 bufmallocadjust(struct buf *bp, int bufsize)
729 KASSERT((bp->b_flags & B_MALLOC) != 0,
730 ("bufmallocadjust: non-malloc buf %p", bp));
731 diff = bufsize - bp->b_bufsize;
733 atomic_subtract_long(&bufmallocspace, -diff);
735 atomic_add_long(&bufmallocspace, diff);
736 bp->b_bufsize = bufsize;
742 * Wake up processes that are waiting on asynchronous writes to fall
743 * below lorunningspace.
749 mtx_lock(&rbreqlock);
752 wakeup(&runningbufreq);
754 mtx_unlock(&rbreqlock);
760 * Decrement the outstanding write count according.
763 runningbufwakeup(struct buf *bp)
767 bspace = bp->b_runningbufspace;
770 space = atomic_fetchadd_long(&runningbufspace, -bspace);
771 KASSERT(space >= bspace, ("runningbufspace underflow %ld %ld",
773 bp->b_runningbufspace = 0;
775 * Only acquire the lock and wakeup on the transition from exceeding
776 * the threshold to falling below it.
778 if (space < lorunningspace)
780 if (space - bspace > lorunningspace)
786 * waitrunningbufspace()
788 * runningbufspace is a measure of the amount of I/O currently
789 * running. This routine is used in async-write situations to
790 * prevent creating huge backups of pending writes to a device.
791 * Only asynchronous writes are governed by this function.
793 * This does NOT turn an async write into a sync write. It waits
794 * for earlier writes to complete and generally returns before the
795 * caller's write has reached the device.
798 waitrunningbufspace(void)
801 mtx_lock(&rbreqlock);
802 while (runningbufspace > hirunningspace) {
804 msleep(&runningbufreq, &rbreqlock, PVM, "wdrain", 0);
806 mtx_unlock(&rbreqlock);
811 * vfs_buf_test_cache:
813 * Called when a buffer is extended. This function clears the B_CACHE
814 * bit if the newly extended portion of the buffer does not contain
818 vfs_buf_test_cache(struct buf *bp, vm_ooffset_t foff, vm_offset_t off,
819 vm_offset_t size, vm_page_t m)
822 VM_OBJECT_ASSERT_LOCKED(m->object);
823 if (bp->b_flags & B_CACHE) {
824 int base = (foff + off) & PAGE_MASK;
825 if (vm_page_is_valid(m, base, size) == 0)
826 bp->b_flags &= ~B_CACHE;
830 /* Wake up the buffer daemon if necessary */
836 if (bd_request == 0) {
844 * Adjust the maxbcachbuf tunable.
847 maxbcachebuf_adjust(void)
852 * maxbcachebuf must be a power of 2 >= MAXBSIZE.
855 while (i * 2 <= maxbcachebuf)
858 if (maxbcachebuf < MAXBSIZE)
859 maxbcachebuf = MAXBSIZE;
860 if (maxbcachebuf > MAXPHYS)
861 maxbcachebuf = MAXPHYS;
862 if (bootverbose != 0 && maxbcachebuf != MAXBCACHEBUF)
863 printf("maxbcachebuf=%d\n", maxbcachebuf);
867 * bd_speedup - speedup the buffer cache flushing code
876 if (bd_speedupreq == 0 || bd_request == 0)
886 #define NSWBUF_MIN 16
890 #define TRANSIENT_DENOM 5
892 #define TRANSIENT_DENOM 10
896 * Calculating buffer cache scaling values and reserve space for buffer
897 * headers. This is called during low level kernel initialization and
898 * may be called more then once. We CANNOT write to the memory area
899 * being reserved at this time.
902 kern_vfs_bio_buffer_alloc(caddr_t v, long physmem_est)
905 long maxbuf, maxbuf_sz, buf_sz, biotmap_sz;
908 * physmem_est is in pages. Convert it to kilobytes (assumes
909 * PAGE_SIZE is >= 1K)
911 physmem_est = physmem_est * (PAGE_SIZE / 1024);
913 maxbcachebuf_adjust();
915 * The nominal buffer size (and minimum KVA allocation) is BKVASIZE.
916 * For the first 64MB of ram nominally allocate sufficient buffers to
917 * cover 1/4 of our ram. Beyond the first 64MB allocate additional
918 * buffers to cover 1/10 of our ram over 64MB. When auto-sizing
919 * the buffer cache we limit the eventual kva reservation to
922 * factor represents the 1/4 x ram conversion.
925 int factor = 4 * BKVASIZE / 1024;
928 if (physmem_est > 4096)
929 nbuf += min((physmem_est - 4096) / factor,
931 if (physmem_est > 65536)
932 nbuf += min((physmem_est - 65536) * 2 / (factor * 5),
933 32 * 1024 * 1024 / (factor * 5));
935 if (maxbcache && nbuf > maxbcache / BKVASIZE)
936 nbuf = maxbcache / BKVASIZE;
941 /* XXX Avoid unsigned long overflows later on with maxbufspace. */
942 maxbuf = (LONG_MAX / 3) / BKVASIZE;
945 printf("Warning: nbufs lowered from %d to %ld\n", nbuf,
951 * Ideal allocation size for the transient bio submap is 10%
952 * of the maximal space buffer map. This roughly corresponds
953 * to the amount of the buffer mapped for typical UFS load.
955 * Clip the buffer map to reserve space for the transient
956 * BIOs, if its extent is bigger than 90% (80% on i386) of the
957 * maximum buffer map extent on the platform.
959 * The fall-back to the maxbuf in case of maxbcache unset,
960 * allows to not trim the buffer KVA for the architectures
961 * with ample KVA space.
963 if (bio_transient_maxcnt == 0 && unmapped_buf_allowed) {
964 maxbuf_sz = maxbcache != 0 ? maxbcache : maxbuf * BKVASIZE;
965 buf_sz = (long)nbuf * BKVASIZE;
966 if (buf_sz < maxbuf_sz / TRANSIENT_DENOM *
967 (TRANSIENT_DENOM - 1)) {
969 * There is more KVA than memory. Do not
970 * adjust buffer map size, and assign the rest
971 * of maxbuf to transient map.
973 biotmap_sz = maxbuf_sz - buf_sz;
976 * Buffer map spans all KVA we could afford on
977 * this platform. Give 10% (20% on i386) of
978 * the buffer map to the transient bio map.
980 biotmap_sz = buf_sz / TRANSIENT_DENOM;
981 buf_sz -= biotmap_sz;
983 if (biotmap_sz / INT_MAX > MAXPHYS)
984 bio_transient_maxcnt = INT_MAX;
986 bio_transient_maxcnt = biotmap_sz / MAXPHYS;
988 * Artificially limit to 1024 simultaneous in-flight I/Os
989 * using the transient mapping.
991 if (bio_transient_maxcnt > 1024)
992 bio_transient_maxcnt = 1024;
994 nbuf = buf_sz / BKVASIZE;
998 * swbufs are used as temporary holders for I/O, such as paging I/O.
999 * We have no less then 16 and no more then 256.
1001 nswbuf = min(nbuf / 4, 256);
1002 TUNABLE_INT_FETCH("kern.nswbuf", &nswbuf);
1003 if (nswbuf < NSWBUF_MIN)
1004 nswbuf = NSWBUF_MIN;
1007 * Reserve space for the buffer cache buffers
1010 v = (caddr_t)(swbuf + nswbuf);
1012 v = (caddr_t)(buf + nbuf);
1017 /* Initialize the buffer subsystem. Called before use of any buffers. */
1024 KASSERT(maxbcachebuf >= MAXBSIZE,
1025 ("maxbcachebuf (%d) must be >= MAXBSIZE (%d)\n", maxbcachebuf,
1027 bq_init(&bqempty, QUEUE_EMPTY, -1, "bufq empty lock");
1028 bq_init(&bqdirty, QUEUE_DIRTY, -1, "bufq dirty lock");
1029 mtx_init(&rbreqlock, "runningbufspace lock", NULL, MTX_DEF);
1030 mtx_init(&bdlock, "buffer daemon lock", NULL, MTX_DEF);
1031 mtx_init(&bdirtylock, "dirty buf lock", NULL, MTX_DEF);
1033 unmapped_buf = (caddr_t)kva_alloc(MAXPHYS);
1035 /* finally, initialize each buffer header and stick on empty q */
1036 for (i = 0; i < nbuf; i++) {
1038 bzero(bp, sizeof *bp);
1039 bp->b_flags = B_INVAL;
1040 bp->b_rcred = NOCRED;
1041 bp->b_wcred = NOCRED;
1042 bp->b_qindex = QUEUE_NONE;
1044 bp->b_subqueue = mp_ncpus;
1046 bp->b_data = bp->b_kvabase = unmapped_buf;
1047 LIST_INIT(&bp->b_dep);
1049 bq_insert(&bqempty, bp, false);
1053 * maxbufspace is the absolute maximum amount of buffer space we are
1054 * allowed to reserve in KVM and in real terms. The absolute maximum
1055 * is nominally used by metadata. hibufspace is the nominal maximum
1056 * used by most other requests. The differential is required to
1057 * ensure that metadata deadlocks don't occur.
1059 * maxbufspace is based on BKVASIZE. Allocating buffers larger then
1060 * this may result in KVM fragmentation which is not handled optimally
1061 * by the system. XXX This is less true with vmem. We could use
1064 maxbufspace = (long)nbuf * BKVASIZE;
1065 hibufspace = lmax(3 * maxbufspace / 4, maxbufspace - maxbcachebuf * 10);
1066 lobufspace = (hibufspace / 20) * 19; /* 95% */
1067 bufspacethresh = lobufspace + (hibufspace - lobufspace) / 2;
1070 * Note: The 16 MiB upper limit for hirunningspace was chosen
1071 * arbitrarily and may need further tuning. It corresponds to
1072 * 128 outstanding write IO requests (if IO size is 128 KiB),
1073 * which fits with many RAID controllers' tagged queuing limits.
1074 * The lower 1 MiB limit is the historical upper limit for
1077 hirunningspace = lmax(lmin(roundup(hibufspace / 64, maxbcachebuf),
1078 16 * 1024 * 1024), 1024 * 1024);
1079 lorunningspace = roundup((hirunningspace * 2) / 3, maxbcachebuf);
1082 * Limit the amount of malloc memory since it is wired permanently into
1083 * the kernel space. Even though this is accounted for in the buffer
1084 * allocation, we don't want the malloced region to grow uncontrolled.
1085 * The malloc scheme improves memory utilization significantly on
1086 * average (small) directories.
1088 maxbufmallocspace = hibufspace / 20;
1091 * Reduce the chance of a deadlock occurring by limiting the number
1092 * of delayed-write dirty buffers we allow to stack up.
1094 hidirtybuffers = nbuf / 4 + 20;
1095 dirtybufthresh = hidirtybuffers * 9 / 10;
1096 numdirtybuffers = 0;
1098 * To support extreme low-memory systems, make sure hidirtybuffers
1099 * cannot eat up all available buffer space. This occurs when our
1100 * minimum cannot be met. We try to size hidirtybuffers to 3/4 our
1101 * buffer space assuming BKVASIZE'd buffers.
1103 while ((long)hidirtybuffers * BKVASIZE > 3 * hibufspace / 4) {
1104 hidirtybuffers >>= 1;
1106 lodirtybuffers = hidirtybuffers / 2;
1109 * lofreebuffers should be sufficient to avoid stalling waiting on
1110 * buf headers under heavy utilization. The bufs in per-cpu caches
1111 * are counted as free but will be unavailable to threads executing
1114 * hifreebuffers is the free target for the bufspace daemon. This
1115 * should be set appropriately to limit work per-iteration.
1117 lofreebuffers = MIN((nbuf / 25) + (20 * mp_ncpus), 128 * mp_ncpus);
1118 hifreebuffers = (3 * lofreebuffers) / 2;
1119 numfreebuffers = nbuf;
1121 /* Setup the kva and free list allocators. */
1122 vmem_set_reclaim(buffer_arena, bufkva_reclaim);
1123 buf_zone = uma_zcache_create("buf free cache", sizeof(struct buf),
1124 NULL, NULL, NULL, NULL, buf_import, buf_release, NULL, 0);
1127 * Size the clean queue according to the amount of buffer space.
1128 * One queue per-256mb up to the max. More queues gives better
1129 * concurrency but less accurate LRU.
1131 clean_domains = MIN(howmany(maxbufspace, 256*1024*1024), CLEAN_DOMAINS);
1132 for (i = 0 ; i < clean_domains; i++) {
1133 struct bufdomain *bd;
1137 bd->bd_freebuffers = nbuf / clean_domains;
1138 bd->bd_hifreebuffers = hifreebuffers / clean_domains;
1139 bd->bd_lofreebuffers = lofreebuffers / clean_domains;
1140 bd->bd_bufspace = 0;
1141 bd->bd_maxbufspace = maxbufspace / clean_domains;
1142 bd->bd_hibufspace = hibufspace / clean_domains;
1143 bd->bd_lobufspace = lobufspace / clean_domains;
1144 bd->bd_bufspacethresh = bufspacethresh / clean_domains;
1145 /* Don't allow more than 2% of bufs in the per-cpu caches. */
1146 bd->bd_lim = nbuf / clean_domains / 50 / mp_ncpus;
1148 getnewbufcalls = counter_u64_alloc(M_WAITOK);
1149 getnewbufrestarts = counter_u64_alloc(M_WAITOK);
1150 mappingrestarts = counter_u64_alloc(M_WAITOK);
1151 numbufallocfails = counter_u64_alloc(M_WAITOK);
1152 notbufdflushes = counter_u64_alloc(M_WAITOK);
1153 buffreekvacnt = counter_u64_alloc(M_WAITOK);
1154 bufdefragcnt = counter_u64_alloc(M_WAITOK);
1155 bufkvaspace = counter_u64_alloc(M_WAITOK);
1160 vfs_buf_check_mapped(struct buf *bp)
1163 KASSERT(bp->b_kvabase != unmapped_buf,
1164 ("mapped buf: b_kvabase was not updated %p", bp));
1165 KASSERT(bp->b_data != unmapped_buf,
1166 ("mapped buf: b_data was not updated %p", bp));
1167 KASSERT(bp->b_data < unmapped_buf || bp->b_data >= unmapped_buf +
1168 MAXPHYS, ("b_data + b_offset unmapped %p", bp));
1172 vfs_buf_check_unmapped(struct buf *bp)
1175 KASSERT(bp->b_data == unmapped_buf,
1176 ("unmapped buf: corrupted b_data %p", bp));
1179 #define BUF_CHECK_MAPPED(bp) vfs_buf_check_mapped(bp)
1180 #define BUF_CHECK_UNMAPPED(bp) vfs_buf_check_unmapped(bp)
1182 #define BUF_CHECK_MAPPED(bp) do {} while (0)
1183 #define BUF_CHECK_UNMAPPED(bp) do {} while (0)
1187 isbufbusy(struct buf *bp)
1189 if (((bp->b_flags & B_INVAL) == 0 && BUF_ISLOCKED(bp)) ||
1190 ((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI))
1196 * Shutdown the system cleanly to prepare for reboot, halt, or power off.
1199 bufshutdown(int show_busybufs)
1201 static int first_buf_printf = 1;
1203 int iter, nbusy, pbusy;
1209 * Sync filesystems for shutdown
1211 wdog_kern_pat(WD_LASTVAL);
1212 sys_sync(curthread, NULL);
1215 * With soft updates, some buffers that are
1216 * written will be remarked as dirty until other
1217 * buffers are written.
1219 for (iter = pbusy = 0; iter < 20; iter++) {
1221 for (bp = &buf[nbuf]; --bp >= buf; )
1225 if (first_buf_printf)
1226 printf("All buffers synced.");
1229 if (first_buf_printf) {
1230 printf("Syncing disks, buffers remaining... ");
1231 first_buf_printf = 0;
1233 printf("%d ", nbusy);
1238 wdog_kern_pat(WD_LASTVAL);
1239 sys_sync(curthread, NULL);
1243 * Drop Giant and spin for a while to allow
1244 * interrupt threads to run.
1247 DELAY(50000 * iter);
1251 * Drop Giant and context switch several times to
1252 * allow interrupt threads to run.
1255 for (subiter = 0; subiter < 50 * iter; subiter++) {
1256 thread_lock(curthread);
1257 mi_switch(SW_VOL, NULL);
1258 thread_unlock(curthread);
1266 * Count only busy local buffers to prevent forcing
1267 * a fsck if we're just a client of a wedged NFS server
1270 for (bp = &buf[nbuf]; --bp >= buf; ) {
1271 if (isbufbusy(bp)) {
1273 /* XXX: This is bogus. We should probably have a BO_REMOTE flag instead */
1274 if (bp->b_dev == NULL) {
1275 TAILQ_REMOVE(&mountlist,
1276 bp->b_vp->v_mount, mnt_list);
1281 if (show_busybufs > 0) {
1283 "%d: buf:%p, vnode:%p, flags:%0x, blkno:%jd, lblkno:%jd, buflock:",
1284 nbusy, bp, bp->b_vp, bp->b_flags,
1285 (intmax_t)bp->b_blkno,
1286 (intmax_t)bp->b_lblkno);
1287 BUF_LOCKPRINTINFO(bp);
1288 if (show_busybufs > 1)
1296 * Failed to sync all blocks. Indicate this and don't
1297 * unmount filesystems (thus forcing an fsck on reboot).
1299 printf("Giving up on %d buffers\n", nbusy);
1300 DELAY(5000000); /* 5 seconds */
1302 if (!first_buf_printf)
1303 printf("Final sync complete\n");
1305 * Unmount filesystems
1307 if (panicstr == NULL)
1311 DELAY(100000); /* wait for console output to finish */
1315 bpmap_qenter(struct buf *bp)
1318 BUF_CHECK_MAPPED(bp);
1321 * bp->b_data is relative to bp->b_offset, but
1322 * bp->b_offset may be offset into the first page.
1324 bp->b_data = (caddr_t)trunc_page((vm_offset_t)bp->b_data);
1325 pmap_qenter((vm_offset_t)bp->b_data, bp->b_pages, bp->b_npages);
1326 bp->b_data = (caddr_t)((vm_offset_t)bp->b_data |
1327 (vm_offset_t)(bp->b_offset & PAGE_MASK));
1330 static struct bufqueue *
1331 bufqueue(struct buf *bp)
1334 switch (bp->b_qindex) {
1337 case QUEUE_SENTINEL:
1344 return (&bdclean[bp->b_domain].bd_subq[bp->b_subqueue]);
1348 panic("bufqueue(%p): Unhandled type %d\n", bp, bp->b_qindex);
1352 * Return the locked bufqueue that bp is a member of.
1354 static struct bufqueue *
1355 bufqueue_acquire(struct buf *bp)
1357 struct bufqueue *bq, *nbq;
1360 * bp can be pushed from a per-cpu queue to the
1361 * cleanq while we're waiting on the lock. Retry
1362 * if the queues don't match.
1380 * Insert the buffer into the appropriate free list. Requires a
1381 * locked buffer on entry and buffer is unlocked before return.
1384 binsfree(struct buf *bp, int qindex)
1386 struct bufdomain *bd;
1387 struct bufqueue *bq;
1389 KASSERT(qindex == QUEUE_CLEAN || qindex == QUEUE_DIRTY,
1390 ("binsfree: Invalid qindex %d", qindex));
1391 BUF_ASSERT_XLOCKED(bp);
1394 * Handle delayed bremfree() processing.
1396 if (bp->b_flags & B_REMFREE) {
1397 if (bp->b_qindex == qindex) {
1398 bp->b_flags |= B_REUSE;
1399 bp->b_flags &= ~B_REMFREE;
1403 bq = bufqueue_acquire(bp);
1407 if (qindex == QUEUE_CLEAN) {
1408 bd = &bdclean[bp->b_domain];
1409 if (bd->bd_lim != 0)
1410 bq = &bd->bd_subq[PCPU_GET(cpuid)];
1415 bq_insert(bq, bp, true);
1421 * Free a buffer to the buf zone once it no longer has valid contents.
1424 buf_free(struct buf *bp)
1427 if (bp->b_flags & B_REMFREE)
1429 if (bp->b_vflags & BV_BKGRDINPROG)
1430 panic("losing buffer 1");
1431 if (bp->b_rcred != NOCRED) {
1432 crfree(bp->b_rcred);
1433 bp->b_rcred = NOCRED;
1435 if (bp->b_wcred != NOCRED) {
1436 crfree(bp->b_wcred);
1437 bp->b_wcred = NOCRED;
1439 if (!LIST_EMPTY(&bp->b_dep))
1442 atomic_add_int(&bdclean[bp->b_domain].bd_freebuffers, 1);
1444 uma_zfree(buf_zone, bp);
1450 * Import bufs into the uma cache from the buf list. The system still
1451 * expects a static array of bufs and much of the synchronization
1452 * around bufs assumes type stable storage. As a result, UMA is used
1453 * only as a per-cpu cache of bufs still maintained on a global list.
1456 buf_import(void *arg, void **store, int cnt, int domain, int flags)
1462 for (i = 0; i < cnt; i++) {
1463 bp = TAILQ_FIRST(&bqempty.bq_queue);
1466 bq_remove(&bqempty, bp);
1469 BQ_UNLOCK(&bqempty);
1477 * Release bufs from the uma cache back to the buffer queues.
1480 buf_release(void *arg, void **store, int cnt)
1482 struct bufqueue *bq;
1488 for (i = 0; i < cnt; i++) {
1490 /* Inline bq_insert() to batch locking. */
1491 TAILQ_INSERT_TAIL(&bq->bq_queue, bp, b_freelist);
1492 bp->b_flags &= ~(B_AGE | B_REUSE);
1494 bp->b_qindex = bq->bq_index;
1502 * Allocate an empty buffer header.
1505 buf_alloc(struct bufdomain *bd)
1511 * We can only run out of bufs in the buf zone if the average buf
1512 * is less than BKVASIZE. In this case the actual wait/block will
1513 * come from buf_reycle() failing to flush one of these small bufs.
1516 freebufs = atomic_fetchadd_int(&bd->bd_freebuffers, -1);
1518 bp = uma_zalloc(buf_zone, M_NOWAIT);
1520 atomic_fetchadd_int(&bd->bd_freebuffers, 1);
1521 bufspace_daemon_wakeup(bd);
1522 counter_u64_add(numbufallocfails, 1);
1526 * Wake-up the bufspace daemon on transition below threshold.
1528 if (freebufs == bd->bd_lofreebuffers)
1529 bufspace_daemon_wakeup(bd);
1531 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1532 panic("getnewbuf_empty: Locked buf %p on free queue.", bp);
1534 KASSERT(bp->b_vp == NULL,
1535 ("bp: %p still has vnode %p.", bp, bp->b_vp));
1536 KASSERT((bp->b_flags & (B_DELWRI | B_NOREUSE)) == 0,
1537 ("invalid buffer %p flags %#x", bp, bp->b_flags));
1538 KASSERT((bp->b_xflags & (BX_VNCLEAN|BX_VNDIRTY)) == 0,
1539 ("bp: %p still on a buffer list. xflags %X", bp, bp->b_xflags));
1540 KASSERT(bp->b_npages == 0,
1541 ("bp: %p still has %d vm pages\n", bp, bp->b_npages));
1542 KASSERT(bp->b_kvasize == 0, ("bp: %p still has kva\n", bp));
1543 KASSERT(bp->b_bufsize == 0, ("bp: %p still has bufspace\n", bp));
1545 bp->b_domain = BD_DOMAIN(bd);
1551 bp->b_blkno = bp->b_lblkno = 0;
1552 bp->b_offset = NOOFFSET;
1558 bp->b_dirtyoff = bp->b_dirtyend = 0;
1559 bp->b_bufobj = NULL;
1560 bp->b_data = bp->b_kvabase = unmapped_buf;
1561 bp->b_fsprivate1 = NULL;
1562 bp->b_fsprivate2 = NULL;
1563 bp->b_fsprivate3 = NULL;
1564 LIST_INIT(&bp->b_dep);
1572 * Free a buffer from the given bufqueue. kva controls whether the
1573 * freed buf must own some kva resources. This is used for
1577 buf_recycle(struct bufdomain *bd, bool kva)
1579 struct bufqueue *bq;
1580 struct buf *bp, *nbp;
1583 counter_u64_add(bufdefragcnt, 1);
1587 KASSERT(BQ_LOCKPTR(bq) == BD_LOCKPTR(bd),
1588 ("buf_recycle: Locks don't match"));
1589 nbp = TAILQ_FIRST(&bq->bq_queue);
1592 * Run scan, possibly freeing data and/or kva mappings on the fly
1595 while ((bp = nbp) != NULL) {
1597 * Calculate next bp (we can only use it if we do not
1598 * release the bqlock).
1600 nbp = TAILQ_NEXT(bp, b_freelist);
1603 * If we are defragging then we need a buffer with
1604 * some kva to reclaim.
1606 if (kva && bp->b_kvasize == 0)
1609 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
1613 * Implement a second chance algorithm for frequently
1616 if ((bp->b_flags & B_REUSE) != 0) {
1617 TAILQ_REMOVE(&bq->bq_queue, bp, b_freelist);
1618 TAILQ_INSERT_TAIL(&bq->bq_queue, bp, b_freelist);
1619 bp->b_flags &= ~B_REUSE;
1625 * Skip buffers with background writes in progress.
1627 if ((bp->b_vflags & BV_BKGRDINPROG) != 0) {
1632 KASSERT(bp->b_qindex == QUEUE_CLEAN,
1633 ("buf_recycle: inconsistent queue %d bp %p",
1635 KASSERT(bp->b_domain == BD_DOMAIN(bd),
1636 ("getnewbuf: queue domain %d doesn't match request %d",
1637 bp->b_domain, (int)BD_DOMAIN(bd)));
1639 * NOTE: nbp is now entirely invalid. We can only restart
1640 * the scan from this point on.
1646 * Requeue the background write buffer with error and
1649 if ((bp->b_vflags & BV_BKGRDERR) != 0) {
1652 nbp = TAILQ_FIRST(&bq->bq_queue);
1655 bp->b_flags |= B_INVAL;
1668 * Mark the buffer for removal from the appropriate free list.
1672 bremfree(struct buf *bp)
1675 CTR3(KTR_BUF, "bremfree(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1676 KASSERT((bp->b_flags & B_REMFREE) == 0,
1677 ("bremfree: buffer %p already marked for delayed removal.", bp));
1678 KASSERT(bp->b_qindex != QUEUE_NONE,
1679 ("bremfree: buffer %p not on a queue.", bp));
1680 BUF_ASSERT_XLOCKED(bp);
1682 bp->b_flags |= B_REMFREE;
1688 * Force an immediate removal from a free list. Used only in nfs when
1689 * it abuses the b_freelist pointer.
1692 bremfreef(struct buf *bp)
1694 struct bufqueue *bq;
1696 bq = bufqueue_acquire(bp);
1702 bq_init(struct bufqueue *bq, int qindex, int subqueue, const char *lockname)
1705 mtx_init(&bq->bq_lock, lockname, NULL, MTX_DEF);
1706 TAILQ_INIT(&bq->bq_queue);
1708 bq->bq_index = qindex;
1709 bq->bq_subqueue = subqueue;
1713 bd_init(struct bufdomain *bd)
1718 domain = bd - bdclean;
1719 bd->bd_cleanq = &bd->bd_subq[mp_ncpus];
1720 bq_init(bd->bd_cleanq, QUEUE_CLEAN, mp_ncpus, "bufq clean lock");
1721 for (i = 0; i <= mp_maxid; i++)
1722 bq_init(&bd->bd_subq[i], QUEUE_CLEAN, i,
1723 "bufq clean subqueue lock");
1724 mtx_init(&bd->bd_run_lock, "bufspace daemon run lock", NULL, MTX_DEF);
1730 * Removes a buffer from the free list, must be called with the
1731 * correct qlock held.
1734 bq_remove(struct bufqueue *bq, struct buf *bp)
1737 CTR3(KTR_BUF, "bq_remove(%p) vp %p flags %X",
1738 bp, bp->b_vp, bp->b_flags);
1739 KASSERT(bp->b_qindex != QUEUE_NONE,
1740 ("bq_remove: buffer %p not on a queue.", bp));
1741 KASSERT(bufqueue(bp) == bq,
1742 ("bq_remove: Remove buffer %p from wrong queue.", bp));
1744 BQ_ASSERT_LOCKED(bq);
1745 if (bp->b_qindex != QUEUE_EMPTY) {
1746 BUF_ASSERT_XLOCKED(bp);
1748 KASSERT(bq->bq_len >= 1,
1749 ("queue %d underflow", bp->b_qindex));
1750 TAILQ_REMOVE(&bq->bq_queue, bp, b_freelist);
1752 bp->b_qindex = QUEUE_NONE;
1753 bp->b_flags &= ~(B_REMFREE | B_REUSE);
1757 bd_flush(struct bufdomain *bd, struct bufqueue *bq)
1761 BQ_ASSERT_LOCKED(bq);
1762 if (bq != bd->bd_cleanq) {
1764 while ((bp = TAILQ_FIRST(&bq->bq_queue)) != NULL) {
1765 TAILQ_REMOVE(&bq->bq_queue, bp, b_freelist);
1766 TAILQ_INSERT_TAIL(&bd->bd_cleanq->bq_queue, bp,
1768 bp->b_subqueue = mp_ncpus;
1770 bd->bd_cleanq->bq_len += bq->bq_len;
1773 if (bd->bd_wanted) {
1775 wakeup(&bd->bd_wanted);
1777 if (bq != bd->bd_cleanq)
1782 bd_flushall(struct bufdomain *bd)
1784 struct bufqueue *bq;
1788 if (bd->bd_lim == 0)
1791 for (i = 0; i < mp_maxid; i++) {
1792 bq = &bd->bd_subq[i];
1793 if (bq->bq_len == 0)
1805 bq_insert(struct bufqueue *bq, struct buf *bp, bool unlock)
1807 struct bufdomain *bd;
1809 if (bp->b_qindex != QUEUE_NONE)
1810 panic("bq_insert: free buffer %p onto another queue?", bp);
1812 bd = &bdclean[bp->b_domain];
1813 if (bp->b_flags & B_AGE) {
1814 /* Place this buf directly on the real queue. */
1815 if (bq->bq_index == QUEUE_CLEAN)
1818 TAILQ_INSERT_HEAD(&bq->bq_queue, bp, b_freelist);
1821 TAILQ_INSERT_TAIL(&bq->bq_queue, bp, b_freelist);
1823 bp->b_flags &= ~(B_AGE | B_REUSE);
1825 bp->b_qindex = bq->bq_index;
1826 bp->b_subqueue = bq->bq_subqueue;
1829 * Unlock before we notify so that we don't wakeup a waiter that
1830 * fails a trylock on the buf and sleeps again.
1835 if (bp->b_qindex == QUEUE_CLEAN) {
1837 * Flush the per-cpu queue and notify any waiters.
1839 if (bd->bd_wanted || (bq != bd->bd_cleanq &&
1840 bq->bq_len >= bd->bd_lim))
1849 * Free the kva allocation for a buffer.
1853 bufkva_free(struct buf *bp)
1857 if (bp->b_kvasize == 0) {
1858 KASSERT(bp->b_kvabase == unmapped_buf &&
1859 bp->b_data == unmapped_buf,
1860 ("Leaked KVA space on %p", bp));
1861 } else if (buf_mapped(bp))
1862 BUF_CHECK_MAPPED(bp);
1864 BUF_CHECK_UNMAPPED(bp);
1866 if (bp->b_kvasize == 0)
1869 vmem_free(buffer_arena, (vm_offset_t)bp->b_kvabase, bp->b_kvasize);
1870 counter_u64_add(bufkvaspace, -bp->b_kvasize);
1871 counter_u64_add(buffreekvacnt, 1);
1872 bp->b_data = bp->b_kvabase = unmapped_buf;
1879 * Allocate the buffer KVA and set b_kvasize and b_kvabase.
1882 bufkva_alloc(struct buf *bp, int maxsize, int gbflags)
1887 KASSERT((gbflags & GB_UNMAPPED) == 0 || (gbflags & GB_KVAALLOC) != 0,
1888 ("Invalid gbflags 0x%x in %s", gbflags, __func__));
1893 error = vmem_alloc(buffer_arena, maxsize, M_BESTFIT | M_NOWAIT, &addr);
1896 * Buffer map is too fragmented. Request the caller
1897 * to defragment the map.
1901 bp->b_kvabase = (caddr_t)addr;
1902 bp->b_kvasize = maxsize;
1903 counter_u64_add(bufkvaspace, bp->b_kvasize);
1904 if ((gbflags & GB_UNMAPPED) != 0) {
1905 bp->b_data = unmapped_buf;
1906 BUF_CHECK_UNMAPPED(bp);
1908 bp->b_data = bp->b_kvabase;
1909 BUF_CHECK_MAPPED(bp);
1917 * Reclaim buffer kva by freeing buffers holding kva. This is a vmem
1918 * callback that fires to avoid returning failure.
1921 bufkva_reclaim(vmem_t *vmem, int flags)
1928 for (i = 0; i < 5; i++) {
1929 for (q = 0; q < clean_domains; q++)
1930 if (buf_recycle(&bdclean[q], true) != 0)
1939 * Attempt to initiate asynchronous I/O on read-ahead blocks. We must
1940 * clear BIO_ERROR and B_INVAL prior to initiating I/O . If B_CACHE is set,
1941 * the buffer is valid and we do not have to do anything.
1944 breada(struct vnode * vp, daddr_t * rablkno, int * rabsize, int cnt,
1945 struct ucred * cred, int flags, void (*ckhashfunc)(struct buf *))
1950 for (i = 0; i < cnt; i++, rablkno++, rabsize++) {
1951 if (inmem(vp, *rablkno))
1953 rabp = getblk(vp, *rablkno, *rabsize, 0, 0, 0);
1954 if ((rabp->b_flags & B_CACHE) != 0) {
1958 if (!TD_IS_IDLETHREAD(curthread)) {
1962 racct_add_buf(curproc, rabp, 0);
1963 PROC_UNLOCK(curproc);
1966 curthread->td_ru.ru_inblock++;
1968 rabp->b_flags |= B_ASYNC;
1969 rabp->b_flags &= ~B_INVAL;
1970 if ((flags & GB_CKHASH) != 0) {
1971 rabp->b_flags |= B_CKHASH;
1972 rabp->b_ckhashcalc = ckhashfunc;
1974 rabp->b_ioflags &= ~BIO_ERROR;
1975 rabp->b_iocmd = BIO_READ;
1976 if (rabp->b_rcred == NOCRED && cred != NOCRED)
1977 rabp->b_rcred = crhold(cred);
1978 vfs_busy_pages(rabp, 0);
1980 rabp->b_iooffset = dbtob(rabp->b_blkno);
1986 * Entry point for bread() and breadn() via #defines in sys/buf.h.
1988 * Get a buffer with the specified data. Look in the cache first. We
1989 * must clear BIO_ERROR and B_INVAL prior to initiating I/O. If B_CACHE
1990 * is set, the buffer is valid and we do not have to do anything, see
1991 * getblk(). Also starts asynchronous I/O on read-ahead blocks.
1993 * Always return a NULL buffer pointer (in bpp) when returning an error.
1996 breadn_flags(struct vnode *vp, daddr_t blkno, int size, daddr_t *rablkno,
1997 int *rabsize, int cnt, struct ucred *cred, int flags,
1998 void (*ckhashfunc)(struct buf *), struct buf **bpp)
2003 CTR3(KTR_BUF, "breadn(%p, %jd, %d)", vp, blkno, size);
2005 * Can only return NULL if GB_LOCK_NOWAIT flag is specified.
2007 *bpp = bp = getblk(vp, blkno, size, 0, 0, flags);
2012 * If not found in cache, do some I/O
2015 if ((bp->b_flags & B_CACHE) == 0) {
2016 if (!TD_IS_IDLETHREAD(curthread)) {
2020 racct_add_buf(curproc, bp, 0);
2021 PROC_UNLOCK(curproc);
2024 curthread->td_ru.ru_inblock++;
2026 bp->b_iocmd = BIO_READ;
2027 bp->b_flags &= ~B_INVAL;
2028 if ((flags & GB_CKHASH) != 0) {
2029 bp->b_flags |= B_CKHASH;
2030 bp->b_ckhashcalc = ckhashfunc;
2032 bp->b_ioflags &= ~BIO_ERROR;
2033 if (bp->b_rcred == NOCRED && cred != NOCRED)
2034 bp->b_rcred = crhold(cred);
2035 vfs_busy_pages(bp, 0);
2036 bp->b_iooffset = dbtob(bp->b_blkno);
2042 * Attempt to initiate asynchronous I/O on read-ahead blocks.
2044 breada(vp, rablkno, rabsize, cnt, cred, flags, ckhashfunc);
2058 * Write, release buffer on completion. (Done by iodone
2059 * if async). Do not bother writing anything if the buffer
2062 * Note that we set B_CACHE here, indicating that buffer is
2063 * fully valid and thus cacheable. This is true even of NFS
2064 * now so we set it generally. This could be set either here
2065 * or in biodone() since the I/O is synchronous. We put it
2069 bufwrite(struct buf *bp)
2076 CTR3(KTR_BUF, "bufwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2077 if ((bp->b_bufobj->bo_flag & BO_DEAD) != 0) {
2078 bp->b_flags |= B_INVAL | B_RELBUF;
2079 bp->b_flags &= ~B_CACHE;
2083 if (bp->b_flags & B_INVAL) {
2088 if (bp->b_flags & B_BARRIER)
2091 oldflags = bp->b_flags;
2093 BUF_ASSERT_HELD(bp);
2095 KASSERT(!(bp->b_vflags & BV_BKGRDINPROG),
2096 ("FFS background buffer should not get here %p", bp));
2100 vp_md = vp->v_vflag & VV_MD;
2105 * Mark the buffer clean. Increment the bufobj write count
2106 * before bundirty() call, to prevent other thread from seeing
2107 * empty dirty list and zero counter for writes in progress,
2108 * falsely indicating that the bufobj is clean.
2110 bufobj_wref(bp->b_bufobj);
2113 bp->b_flags &= ~B_DONE;
2114 bp->b_ioflags &= ~BIO_ERROR;
2115 bp->b_flags |= B_CACHE;
2116 bp->b_iocmd = BIO_WRITE;
2118 vfs_busy_pages(bp, 1);
2121 * Normal bwrites pipeline writes
2123 bp->b_runningbufspace = bp->b_bufsize;
2124 space = atomic_fetchadd_long(&runningbufspace, bp->b_runningbufspace);
2126 if (!TD_IS_IDLETHREAD(curthread)) {
2130 racct_add_buf(curproc, bp, 1);
2131 PROC_UNLOCK(curproc);
2134 curthread->td_ru.ru_oublock++;
2136 if (oldflags & B_ASYNC)
2138 bp->b_iooffset = dbtob(bp->b_blkno);
2139 buf_track(bp, __func__);
2142 if ((oldflags & B_ASYNC) == 0) {
2143 int rtval = bufwait(bp);
2146 } else if (space > hirunningspace) {
2148 * don't allow the async write to saturate the I/O
2149 * system. We will not deadlock here because
2150 * we are blocking waiting for I/O that is already in-progress
2151 * to complete. We do not block here if it is the update
2152 * or syncer daemon trying to clean up as that can lead
2155 if ((curthread->td_pflags & TDP_NORUNNINGBUF) == 0 && !vp_md)
2156 waitrunningbufspace();
2163 bufbdflush(struct bufobj *bo, struct buf *bp)
2167 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10) {
2168 (void) VOP_FSYNC(bp->b_vp, MNT_NOWAIT, curthread);
2170 } else if (bo->bo_dirty.bv_cnt > dirtybufthresh) {
2173 * Try to find a buffer to flush.
2175 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2176 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2178 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2181 panic("bdwrite: found ourselves");
2183 /* Don't countdeps with the bo lock held. */
2184 if (buf_countdeps(nbp, 0)) {
2189 if (nbp->b_flags & B_CLUSTEROK) {
2190 vfs_bio_awrite(nbp);
2195 dirtybufferflushes++;
2204 * Delayed write. (Buffer is marked dirty). Do not bother writing
2205 * anything if the buffer is marked invalid.
2207 * Note that since the buffer must be completely valid, we can safely
2208 * set B_CACHE. In fact, we have to set B_CACHE here rather then in
2209 * biodone() in order to prevent getblk from writing the buffer
2210 * out synchronously.
2213 bdwrite(struct buf *bp)
2215 struct thread *td = curthread;
2219 CTR3(KTR_BUF, "bdwrite(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2220 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2221 KASSERT((bp->b_flags & B_BARRIER) == 0,
2222 ("Barrier request in delayed write %p", bp));
2223 BUF_ASSERT_HELD(bp);
2225 if (bp->b_flags & B_INVAL) {
2231 * If we have too many dirty buffers, don't create any more.
2232 * If we are wildly over our limit, then force a complete
2233 * cleanup. Otherwise, just keep the situation from getting
2234 * out of control. Note that we have to avoid a recursive
2235 * disaster and not try to clean up after our own cleanup!
2239 if ((td->td_pflags & (TDP_COWINPROGRESS|TDP_INBDFLUSH)) == 0) {
2240 td->td_pflags |= TDP_INBDFLUSH;
2242 td->td_pflags &= ~TDP_INBDFLUSH;
2248 * Set B_CACHE, indicating that the buffer is fully valid. This is
2249 * true even of NFS now.
2251 bp->b_flags |= B_CACHE;
2254 * This bmap keeps the system from needing to do the bmap later,
2255 * perhaps when the system is attempting to do a sync. Since it
2256 * is likely that the indirect block -- or whatever other datastructure
2257 * that the filesystem needs is still in memory now, it is a good
2258 * thing to do this. Note also, that if the pageout daemon is
2259 * requesting a sync -- there might not be enough memory to do
2260 * the bmap then... So, this is important to do.
2262 if (vp->v_type != VCHR && bp->b_lblkno == bp->b_blkno) {
2263 VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL, NULL);
2266 buf_track(bp, __func__);
2269 * Set the *dirty* buffer range based upon the VM system dirty
2272 * Mark the buffer pages as clean. We need to do this here to
2273 * satisfy the vnode_pager and the pageout daemon, so that it
2274 * thinks that the pages have been "cleaned". Note that since
2275 * the pages are in a delayed write buffer -- the VFS layer
2276 * "will" see that the pages get written out on the next sync,
2277 * or perhaps the cluster will be completed.
2279 vfs_clean_pages_dirty_buf(bp);
2283 * note: we cannot initiate I/O from a bdwrite even if we wanted to,
2284 * due to the softdep code.
2291 * Turn buffer into delayed write request. We must clear BIO_READ and
2292 * B_RELBUF, and we must set B_DELWRI. We reassign the buffer to
2293 * itself to properly update it in the dirty/clean lists. We mark it
2294 * B_DONE to ensure that any asynchronization of the buffer properly
2295 * clears B_DONE ( else a panic will occur later ).
2297 * bdirty() is kinda like bdwrite() - we have to clear B_INVAL which
2298 * might have been set pre-getblk(). Unlike bwrite/bdwrite, bdirty()
2299 * should only be called if the buffer is known-good.
2301 * Since the buffer is not on a queue, we do not update the numfreebuffers
2304 * The buffer must be on QUEUE_NONE.
2307 bdirty(struct buf *bp)
2310 CTR3(KTR_BUF, "bdirty(%p) vp %p flags %X",
2311 bp, bp->b_vp, bp->b_flags);
2312 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2313 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
2314 ("bdirty: buffer %p still on queue %d", bp, bp->b_qindex));
2315 BUF_ASSERT_HELD(bp);
2316 bp->b_flags &= ~(B_RELBUF);
2317 bp->b_iocmd = BIO_WRITE;
2319 if ((bp->b_flags & B_DELWRI) == 0) {
2320 bp->b_flags |= /* XXX B_DONE | */ B_DELWRI;
2329 * Clear B_DELWRI for buffer.
2331 * Since the buffer is not on a queue, we do not update the numfreebuffers
2334 * The buffer must be on QUEUE_NONE.
2338 bundirty(struct buf *bp)
2341 CTR3(KTR_BUF, "bundirty(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2342 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2343 KASSERT(bp->b_flags & B_REMFREE || bp->b_qindex == QUEUE_NONE,
2344 ("bundirty: buffer %p still on queue %d", bp, bp->b_qindex));
2345 BUF_ASSERT_HELD(bp);
2347 if (bp->b_flags & B_DELWRI) {
2348 bp->b_flags &= ~B_DELWRI;
2353 * Since it is now being written, we can clear its deferred write flag.
2355 bp->b_flags &= ~B_DEFERRED;
2361 * Asynchronous write. Start output on a buffer, but do not wait for
2362 * it to complete. The buffer is released when the output completes.
2364 * bwrite() ( or the VOP routine anyway ) is responsible for handling
2365 * B_INVAL buffers. Not us.
2368 bawrite(struct buf *bp)
2371 bp->b_flags |= B_ASYNC;
2378 * Asynchronous barrier write. Start output on a buffer, but do not
2379 * wait for it to complete. Place a write barrier after this write so
2380 * that this buffer and all buffers written before it are committed to
2381 * the disk before any buffers written after this write are committed
2382 * to the disk. The buffer is released when the output completes.
2385 babarrierwrite(struct buf *bp)
2388 bp->b_flags |= B_ASYNC | B_BARRIER;
2395 * Synchronous barrier write. Start output on a buffer and wait for
2396 * it to complete. Place a write barrier after this write so that
2397 * this buffer and all buffers written before it are committed to
2398 * the disk before any buffers written after this write are committed
2399 * to the disk. The buffer is released when the output completes.
2402 bbarrierwrite(struct buf *bp)
2405 bp->b_flags |= B_BARRIER;
2406 return (bwrite(bp));
2412 * Called prior to the locking of any vnodes when we are expecting to
2413 * write. We do not want to starve the buffer cache with too many
2414 * dirty buffers so we block here. By blocking prior to the locking
2415 * of any vnodes we attempt to avoid the situation where a locked vnode
2416 * prevents the various system daemons from flushing related buffers.
2422 if (numdirtybuffers >= hidirtybuffers) {
2423 mtx_lock(&bdirtylock);
2424 while (numdirtybuffers >= hidirtybuffers) {
2426 msleep(&bdirtywait, &bdirtylock, (PRIBIO + 4),
2429 mtx_unlock(&bdirtylock);
2434 * Return true if we have too many dirty buffers.
2437 buf_dirty_count_severe(void)
2440 return(numdirtybuffers >= hidirtybuffers);
2446 * Release a busy buffer and, if requested, free its resources. The
2447 * buffer will be stashed in the appropriate bufqueue[] allowing it
2448 * to be accessed later as a cache entity or reused for other purposes.
2451 brelse(struct buf *bp)
2456 * Many functions erroneously call brelse with a NULL bp under rare
2457 * error conditions. Simply return when called with a NULL bp.
2461 CTR3(KTR_BUF, "brelse(%p) vp %p flags %X",
2462 bp, bp->b_vp, bp->b_flags);
2463 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
2464 ("brelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
2465 KASSERT((bp->b_flags & B_VMIO) != 0 || (bp->b_flags & B_NOREUSE) == 0,
2466 ("brelse: non-VMIO buffer marked NOREUSE"));
2468 if (BUF_LOCKRECURSED(bp)) {
2470 * Do not process, in particular, do not handle the
2471 * B_INVAL/B_RELBUF and do not release to free list.
2477 if (bp->b_flags & B_MANAGED) {
2482 if ((bp->b_vflags & (BV_BKGRDINPROG | BV_BKGRDERR)) == BV_BKGRDERR) {
2483 BO_LOCK(bp->b_bufobj);
2484 bp->b_vflags &= ~BV_BKGRDERR;
2485 BO_UNLOCK(bp->b_bufobj);
2488 if (bp->b_iocmd == BIO_WRITE && (bp->b_ioflags & BIO_ERROR) &&
2489 (bp->b_error != ENXIO || !LIST_EMPTY(&bp->b_dep)) &&
2490 !(bp->b_flags & B_INVAL)) {
2492 * Failed write, redirty. All errors except ENXIO (which
2493 * means the device is gone) are treated as being
2496 * XXX Treating EIO as transient is not correct; the
2497 * contract with the local storage device drivers is that
2498 * they will only return EIO once the I/O is no longer
2499 * retriable. Network I/O also respects this through the
2500 * guarantees of TCP and/or the internal retries of NFS.
2501 * ENOMEM might be transient, but we also have no way of
2502 * knowing when its ok to retry/reschedule. In general,
2503 * this entire case should be made obsolete through better
2504 * error handling/recovery and resource scheduling.
2506 * Do this also for buffers that failed with ENXIO, but have
2507 * non-empty dependencies - the soft updates code might need
2508 * to access the buffer to untangle them.
2510 * Must clear BIO_ERROR to prevent pages from being scrapped.
2512 bp->b_ioflags &= ~BIO_ERROR;
2514 } else if ((bp->b_flags & (B_NOCACHE | B_INVAL)) ||
2515 (bp->b_ioflags & BIO_ERROR) || (bp->b_bufsize <= 0)) {
2517 * Either a failed read I/O, or we were asked to free or not
2518 * cache the buffer, or we failed to write to a device that's
2519 * no longer present.
2521 bp->b_flags |= B_INVAL;
2522 if (!LIST_EMPTY(&bp->b_dep))
2524 if (bp->b_flags & B_DELWRI)
2526 bp->b_flags &= ~(B_DELWRI | B_CACHE);
2527 if ((bp->b_flags & B_VMIO) == 0) {
2535 * We must clear B_RELBUF if B_DELWRI is set. If vfs_vmio_truncate()
2536 * is called with B_DELWRI set, the underlying pages may wind up
2537 * getting freed causing a previous write (bdwrite()) to get 'lost'
2538 * because pages associated with a B_DELWRI bp are marked clean.
2540 * We still allow the B_INVAL case to call vfs_vmio_truncate(), even
2541 * if B_DELWRI is set.
2543 if (bp->b_flags & B_DELWRI)
2544 bp->b_flags &= ~B_RELBUF;
2547 * VMIO buffer rundown. It is not very necessary to keep a VMIO buffer
2548 * constituted, not even NFS buffers now. Two flags effect this. If
2549 * B_INVAL, the struct buf is invalidated but the VM object is kept
2550 * around ( i.e. so it is trivial to reconstitute the buffer later ).
2552 * If BIO_ERROR or B_NOCACHE is set, pages in the VM object will be
2553 * invalidated. BIO_ERROR cannot be set for a failed write unless the
2554 * buffer is also B_INVAL because it hits the re-dirtying code above.
2556 * Normally we can do this whether a buffer is B_DELWRI or not. If
2557 * the buffer is an NFS buffer, it is tracking piecemeal writes or
2558 * the commit state and we cannot afford to lose the buffer. If the
2559 * buffer has a background write in progress, we need to keep it
2560 * around to prevent it from being reconstituted and starting a second
2563 if ((bp->b_flags & B_VMIO) && (bp->b_flags & B_NOCACHE ||
2564 (bp->b_ioflags & BIO_ERROR && bp->b_iocmd == BIO_READ)) &&
2565 !(bp->b_vp->v_mount != NULL &&
2566 (bp->b_vp->v_mount->mnt_vfc->vfc_flags & VFCF_NETWORK) != 0 &&
2567 !vn_isdisk(bp->b_vp, NULL) && (bp->b_flags & B_DELWRI))) {
2568 vfs_vmio_invalidate(bp);
2572 if ((bp->b_flags & (B_INVAL | B_RELBUF)) != 0 ||
2573 (bp->b_flags & (B_DELWRI | B_NOREUSE)) == B_NOREUSE) {
2575 bp->b_flags &= ~B_NOREUSE;
2576 if (bp->b_vp != NULL)
2581 * If the buffer has junk contents signal it and eventually
2582 * clean up B_DELWRI and diassociate the vnode so that gbincore()
2585 if (bp->b_bufsize == 0 || (bp->b_ioflags & BIO_ERROR) != 0 ||
2586 (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF)) != 0)
2587 bp->b_flags |= B_INVAL;
2588 if (bp->b_flags & B_INVAL) {
2589 if (bp->b_flags & B_DELWRI)
2595 buf_track(bp, __func__);
2597 /* buffers with no memory */
2598 if (bp->b_bufsize == 0) {
2602 /* buffers with junk contents */
2603 if (bp->b_flags & (B_INVAL | B_NOCACHE | B_RELBUF) ||
2604 (bp->b_ioflags & BIO_ERROR)) {
2605 bp->b_xflags &= ~(BX_BKGRDWRITE | BX_ALTDATA);
2606 if (bp->b_vflags & BV_BKGRDINPROG)
2607 panic("losing buffer 2");
2608 qindex = QUEUE_CLEAN;
2609 bp->b_flags |= B_AGE;
2610 /* remaining buffers */
2611 } else if (bp->b_flags & B_DELWRI)
2612 qindex = QUEUE_DIRTY;
2614 qindex = QUEUE_CLEAN;
2616 if ((bp->b_flags & B_DELWRI) == 0 && (bp->b_xflags & BX_VNDIRTY))
2617 panic("brelse: not dirty");
2619 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_RELBUF | B_DIRECT);
2620 /* binsfree unlocks bp. */
2621 binsfree(bp, qindex);
2625 * Release a buffer back to the appropriate queue but do not try to free
2626 * it. The buffer is expected to be used again soon.
2628 * bqrelse() is used by bdwrite() to requeue a delayed write, and used by
2629 * biodone() to requeue an async I/O on completion. It is also used when
2630 * known good buffers need to be requeued but we think we may need the data
2633 * XXX we should be able to leave the B_RELBUF hint set on completion.
2636 bqrelse(struct buf *bp)
2640 CTR3(KTR_BUF, "bqrelse(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2641 KASSERT(!(bp->b_flags & (B_CLUSTER|B_PAGING)),
2642 ("bqrelse: inappropriate B_PAGING or B_CLUSTER bp %p", bp));
2644 qindex = QUEUE_NONE;
2645 if (BUF_LOCKRECURSED(bp)) {
2646 /* do not release to free list */
2650 bp->b_flags &= ~(B_ASYNC | B_NOCACHE | B_AGE | B_RELBUF);
2652 if (bp->b_flags & B_MANAGED) {
2653 if (bp->b_flags & B_REMFREE)
2658 /* buffers with stale but valid contents */
2659 if ((bp->b_flags & B_DELWRI) != 0 || (bp->b_vflags & (BV_BKGRDINPROG |
2660 BV_BKGRDERR)) == BV_BKGRDERR) {
2661 BO_LOCK(bp->b_bufobj);
2662 bp->b_vflags &= ~BV_BKGRDERR;
2663 BO_UNLOCK(bp->b_bufobj);
2664 qindex = QUEUE_DIRTY;
2666 if ((bp->b_flags & B_DELWRI) == 0 &&
2667 (bp->b_xflags & BX_VNDIRTY))
2668 panic("bqrelse: not dirty");
2669 if ((bp->b_flags & B_NOREUSE) != 0) {
2673 qindex = QUEUE_CLEAN;
2675 buf_track(bp, __func__);
2676 /* binsfree unlocks bp. */
2677 binsfree(bp, qindex);
2681 buf_track(bp, __func__);
2687 * Complete I/O to a VMIO backed page. Validate the pages as appropriate,
2688 * restore bogus pages.
2691 vfs_vmio_iodone(struct buf *bp)
2697 int i, iosize, resid;
2700 obj = bp->b_bufobj->bo_object;
2701 KASSERT(obj->paging_in_progress >= bp->b_npages,
2702 ("vfs_vmio_iodone: paging in progress(%d) < b_npages(%d)",
2703 obj->paging_in_progress, bp->b_npages));
2706 KASSERT(vp->v_holdcnt > 0,
2707 ("vfs_vmio_iodone: vnode %p has zero hold count", vp));
2708 KASSERT(vp->v_object != NULL,
2709 ("vfs_vmio_iodone: vnode %p has no vm_object", vp));
2711 foff = bp->b_offset;
2712 KASSERT(bp->b_offset != NOOFFSET,
2713 ("vfs_vmio_iodone: bp %p has no buffer offset", bp));
2716 iosize = bp->b_bcount - bp->b_resid;
2717 VM_OBJECT_WLOCK(obj);
2718 for (i = 0; i < bp->b_npages; i++) {
2719 resid = ((foff + PAGE_SIZE) & ~(off_t)PAGE_MASK) - foff;
2724 * cleanup bogus pages, restoring the originals
2727 if (m == bogus_page) {
2729 m = vm_page_lookup(obj, OFF_TO_IDX(foff));
2731 panic("biodone: page disappeared!");
2733 } else if ((bp->b_iocmd == BIO_READ) && resid > 0) {
2735 * In the write case, the valid and clean bits are
2736 * already changed correctly ( see bdwrite() ), so we
2737 * only need to do this here in the read case.
2739 KASSERT((m->dirty & vm_page_bits(foff & PAGE_MASK,
2740 resid)) == 0, ("vfs_vmio_iodone: page %p "
2741 "has unexpected dirty bits", m));
2742 vfs_page_set_valid(bp, foff, m);
2744 KASSERT(OFF_TO_IDX(foff) == m->pindex,
2745 ("vfs_vmio_iodone: foff(%jd)/pindex(%ju) mismatch",
2746 (intmax_t)foff, (uintmax_t)m->pindex));
2749 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
2752 vm_object_pip_wakeupn(obj, bp->b_npages);
2753 VM_OBJECT_WUNLOCK(obj);
2754 if (bogus && buf_mapped(bp)) {
2755 BUF_CHECK_MAPPED(bp);
2756 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
2757 bp->b_pages, bp->b_npages);
2762 * Unwire a page held by a buf and place it on the appropriate vm queue.
2765 vfs_vmio_unwire(struct buf *bp, vm_page_t m)
2770 if (vm_page_unwire_noq(m)) {
2772 * Determine if the page should be freed before adding
2773 * it to the inactive queue.
2775 if (m->valid == 0) {
2776 freed = !vm_page_busied(m);
2779 } else if ((bp->b_flags & B_DIRECT) != 0)
2780 freed = vm_page_try_to_free(m);
2785 * If the page is unlikely to be reused, let the
2786 * VM know. Otherwise, maintain LRU.
2788 if ((bp->b_flags & B_NOREUSE) != 0)
2789 vm_page_deactivate_noreuse(m);
2790 else if (m->queue == PQ_ACTIVE)
2791 vm_page_reference(m);
2792 else if (m->queue != PQ_INACTIVE)
2793 vm_page_deactivate(m);
2802 * Perform page invalidation when a buffer is released. The fully invalid
2803 * pages will be reclaimed later in vfs_vmio_truncate().
2806 vfs_vmio_invalidate(struct buf *bp)
2810 int i, resid, poffset, presid;
2812 if (buf_mapped(bp)) {
2813 BUF_CHECK_MAPPED(bp);
2814 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), bp->b_npages);
2816 BUF_CHECK_UNMAPPED(bp);
2818 * Get the base offset and length of the buffer. Note that
2819 * in the VMIO case if the buffer block size is not
2820 * page-aligned then b_data pointer may not be page-aligned.
2821 * But our b_pages[] array *IS* page aligned.
2823 * block sizes less then DEV_BSIZE (usually 512) are not
2824 * supported due to the page granularity bits (m->valid,
2825 * m->dirty, etc...).
2827 * See man buf(9) for more information
2829 obj = bp->b_bufobj->bo_object;
2830 resid = bp->b_bufsize;
2831 poffset = bp->b_offset & PAGE_MASK;
2832 VM_OBJECT_WLOCK(obj);
2833 for (i = 0; i < bp->b_npages; i++) {
2835 if (m == bogus_page)
2836 panic("vfs_vmio_invalidate: Unexpected bogus page.");
2837 bp->b_pages[i] = NULL;
2839 presid = resid > (PAGE_SIZE - poffset) ?
2840 (PAGE_SIZE - poffset) : resid;
2841 KASSERT(presid >= 0, ("brelse: extra page"));
2842 while (vm_page_xbusied(m)) {
2844 VM_OBJECT_WUNLOCK(obj);
2845 vm_page_busy_sleep(m, "mbncsh", true);
2846 VM_OBJECT_WLOCK(obj);
2848 if (pmap_page_wired_mappings(m) == 0)
2849 vm_page_set_invalid(m, poffset, presid);
2850 vfs_vmio_unwire(bp, m);
2854 VM_OBJECT_WUNLOCK(obj);
2859 * Page-granular truncation of an existing VMIO buffer.
2862 vfs_vmio_truncate(struct buf *bp, int desiredpages)
2868 if (bp->b_npages == desiredpages)
2871 if (buf_mapped(bp)) {
2872 BUF_CHECK_MAPPED(bp);
2873 pmap_qremove((vm_offset_t)trunc_page((vm_offset_t)bp->b_data) +
2874 (desiredpages << PAGE_SHIFT), bp->b_npages - desiredpages);
2876 BUF_CHECK_UNMAPPED(bp);
2877 obj = bp->b_bufobj->bo_object;
2879 VM_OBJECT_WLOCK(obj);
2880 for (i = desiredpages; i < bp->b_npages; i++) {
2882 KASSERT(m != bogus_page, ("allocbuf: bogus page found"));
2883 bp->b_pages[i] = NULL;
2884 vfs_vmio_unwire(bp, m);
2887 VM_OBJECT_WUNLOCK(obj);
2888 bp->b_npages = desiredpages;
2892 * Byte granular extension of VMIO buffers.
2895 vfs_vmio_extend(struct buf *bp, int desiredpages, int size)
2898 * We are growing the buffer, possibly in a
2899 * byte-granular fashion.
2907 * Step 1, bring in the VM pages from the object, allocating
2908 * them if necessary. We must clear B_CACHE if these pages
2909 * are not valid for the range covered by the buffer.
2911 obj = bp->b_bufobj->bo_object;
2912 VM_OBJECT_WLOCK(obj);
2913 if (bp->b_npages < desiredpages) {
2915 * We must allocate system pages since blocking
2916 * here could interfere with paging I/O, no
2917 * matter which process we are.
2919 * Only exclusive busy can be tested here.
2920 * Blocking on shared busy might lead to
2921 * deadlocks once allocbuf() is called after
2922 * pages are vfs_busy_pages().
2924 (void)vm_page_grab_pages(obj,
2925 OFF_TO_IDX(bp->b_offset) + bp->b_npages,
2926 VM_ALLOC_SYSTEM | VM_ALLOC_IGN_SBUSY |
2927 VM_ALLOC_NOBUSY | VM_ALLOC_WIRED,
2928 &bp->b_pages[bp->b_npages], desiredpages - bp->b_npages);
2929 bp->b_npages = desiredpages;
2933 * Step 2. We've loaded the pages into the buffer,
2934 * we have to figure out if we can still have B_CACHE
2935 * set. Note that B_CACHE is set according to the
2936 * byte-granular range ( bcount and size ), not the
2937 * aligned range ( newbsize ).
2939 * The VM test is against m->valid, which is DEV_BSIZE
2940 * aligned. Needless to say, the validity of the data
2941 * needs to also be DEV_BSIZE aligned. Note that this
2942 * fails with NFS if the server or some other client
2943 * extends the file's EOF. If our buffer is resized,
2944 * B_CACHE may remain set! XXX
2946 toff = bp->b_bcount;
2947 tinc = PAGE_SIZE - ((bp->b_offset + toff) & PAGE_MASK);
2948 while ((bp->b_flags & B_CACHE) && toff < size) {
2951 if (tinc > (size - toff))
2953 pi = ((bp->b_offset & PAGE_MASK) + toff) >> PAGE_SHIFT;
2954 m = bp->b_pages[pi];
2955 vfs_buf_test_cache(bp, bp->b_offset, toff, tinc, m);
2959 VM_OBJECT_WUNLOCK(obj);
2962 * Step 3, fixup the KVA pmap.
2967 BUF_CHECK_UNMAPPED(bp);
2971 * Check to see if a block at a particular lbn is available for a clustered
2975 vfs_bio_clcheck(struct vnode *vp, int size, daddr_t lblkno, daddr_t blkno)
2982 /* If the buf isn't in core skip it */
2983 if ((bpa = gbincore(&vp->v_bufobj, lblkno)) == NULL)
2986 /* If the buf is busy we don't want to wait for it */
2987 if (BUF_LOCK(bpa, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0)
2990 /* Only cluster with valid clusterable delayed write buffers */
2991 if ((bpa->b_flags & (B_DELWRI | B_CLUSTEROK | B_INVAL)) !=
2992 (B_DELWRI | B_CLUSTEROK))
2995 if (bpa->b_bufsize != size)
2999 * Check to see if it is in the expected place on disk and that the
3000 * block has been mapped.
3002 if ((bpa->b_blkno != bpa->b_lblkno) && (bpa->b_blkno == blkno))
3012 * Implement clustered async writes for clearing out B_DELWRI buffers.
3013 * This is much better then the old way of writing only one buffer at
3014 * a time. Note that we may not be presented with the buffers in the
3015 * correct order, so we search for the cluster in both directions.
3018 vfs_bio_awrite(struct buf *bp)
3023 daddr_t lblkno = bp->b_lblkno;
3024 struct vnode *vp = bp->b_vp;
3032 gbflags = (bp->b_data == unmapped_buf) ? GB_UNMAPPED : 0;
3034 * right now we support clustered writing only to regular files. If
3035 * we find a clusterable block we could be in the middle of a cluster
3036 * rather then at the beginning.
3038 if ((vp->v_type == VREG) &&
3039 (vp->v_mount != 0) && /* Only on nodes that have the size info */
3040 (bp->b_flags & (B_CLUSTEROK | B_INVAL)) == B_CLUSTEROK) {
3042 size = vp->v_mount->mnt_stat.f_iosize;
3043 maxcl = MAXPHYS / size;
3046 for (i = 1; i < maxcl; i++)
3047 if (vfs_bio_clcheck(vp, size, lblkno + i,
3048 bp->b_blkno + ((i * size) >> DEV_BSHIFT)) == 0)
3051 for (j = 1; i + j <= maxcl && j <= lblkno; j++)
3052 if (vfs_bio_clcheck(vp, size, lblkno - j,
3053 bp->b_blkno - ((j * size) >> DEV_BSHIFT)) == 0)
3059 * this is a possible cluster write
3063 nwritten = cluster_wbuild(vp, size, lblkno - j, ncl,
3069 bp->b_flags |= B_ASYNC;
3071 * default (old) behavior, writing out only one block
3073 * XXX returns b_bufsize instead of b_bcount for nwritten?
3075 nwritten = bp->b_bufsize;
3084 * Allocate KVA for an empty buf header according to gbflags.
3087 getnewbuf_kva(struct buf *bp, int gbflags, int maxsize)
3090 if ((gbflags & (GB_UNMAPPED | GB_KVAALLOC)) != GB_UNMAPPED) {
3092 * In order to keep fragmentation sane we only allocate kva
3093 * in BKVASIZE chunks. XXX with vmem we can do page size.
3095 maxsize = (maxsize + BKVAMASK) & ~BKVAMASK;
3097 if (maxsize != bp->b_kvasize &&
3098 bufkva_alloc(bp, maxsize, gbflags))
3107 * Find and initialize a new buffer header, freeing up existing buffers
3108 * in the bufqueues as necessary. The new buffer is returned locked.
3111 * We have insufficient buffer headers
3112 * We have insufficient buffer space
3113 * buffer_arena is too fragmented ( space reservation fails )
3114 * If we have to flush dirty buffers ( but we try to avoid this )
3116 * The caller is responsible for releasing the reserved bufspace after
3117 * allocbuf() is called.
3120 getnewbuf(struct vnode *vp, int slpflag, int slptimeo, int maxsize, int gbflags)
3122 struct bufdomain *bd;
3124 bool metadata, reserved;
3127 KASSERT((gbflags & (GB_UNMAPPED | GB_KVAALLOC)) != GB_KVAALLOC,
3128 ("GB_KVAALLOC only makes sense with GB_UNMAPPED"));
3129 if (!unmapped_buf_allowed)
3130 gbflags &= ~(GB_UNMAPPED | GB_KVAALLOC);
3132 if (vp == NULL || (vp->v_vflag & (VV_MD | VV_SYSTEM)) != 0 ||
3140 bd = &bdclean[vp->v_bufobj.bo_domain];
3142 counter_u64_add(getnewbufcalls, 1);
3145 if (reserved == false &&
3146 bufspace_reserve(bd, maxsize, metadata) != 0) {
3147 counter_u64_add(getnewbufrestarts, 1);
3151 if ((bp = buf_alloc(bd)) == NULL) {
3152 counter_u64_add(getnewbufrestarts, 1);
3155 if (getnewbuf_kva(bp, gbflags, maxsize) == 0)
3158 } while (buf_recycle(bd, false) == 0);
3161 bufspace_release(bd, maxsize);
3163 bp->b_flags |= B_INVAL;
3166 bufspace_wait(bd, vp, gbflags, slpflag, slptimeo);
3174 * buffer flushing daemon. Buffers are normally flushed by the
3175 * update daemon but if it cannot keep up this process starts to
3176 * take the load in an attempt to prevent getnewbuf() from blocking.
3178 static struct kproc_desc buf_kp = {
3183 SYSINIT(bufdaemon, SI_SUB_KTHREAD_BUF, SI_ORDER_FIRST, kproc_start, &buf_kp);
3186 buf_flush(struct vnode *vp, int target)
3190 flushed = flushbufqueues(vp, target, 0);
3193 * Could not find any buffers without rollback
3194 * dependencies, so just write the first one
3195 * in the hopes of eventually making progress.
3197 if (vp != NULL && target > 2)
3199 flushbufqueues(vp, target, 1);
3211 * This process needs to be suspended prior to shutdown sync.
3213 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, bufdaemonproc,
3217 * Start the buf clean daemons as children threads.
3219 for (i = 0 ; i < clean_domains; i++) {
3222 error = kthread_add((void (*)(void *))bufspace_daemon,
3223 &bdclean[i], curproc, NULL, 0, 0, "bufspacedaemon-%d", i);
3225 panic("error %d spawning bufspace daemon", error);
3229 * This process is allowed to take the buffer cache to the limit
3231 curthread->td_pflags |= TDP_NORUNNINGBUF | TDP_BUFNEED;
3235 mtx_unlock(&bdlock);
3237 kproc_suspend_check(bufdaemonproc);
3238 lodirty = lodirtybuffers;
3239 if (bd_speedupreq) {
3240 lodirty = numdirtybuffers / 2;
3244 * Do the flush. Limit the amount of in-transit I/O we
3245 * allow to build up, otherwise we would completely saturate
3248 while (numdirtybuffers > lodirty) {
3249 if (buf_flush(NULL, numdirtybuffers - lodirty) == 0)
3251 kern_yield(PRI_USER);
3255 * Only clear bd_request if we have reached our low water
3256 * mark. The buf_daemon normally waits 1 second and
3257 * then incrementally flushes any dirty buffers that have
3258 * built up, within reason.
3260 * If we were unable to hit our low water mark and couldn't
3261 * find any flushable buffers, we sleep for a short period
3262 * to avoid endless loops on unlockable buffers.
3265 if (numdirtybuffers <= lodirtybuffers) {
3267 * We reached our low water mark, reset the
3268 * request and sleep until we are needed again.
3269 * The sleep is just so the suspend code works.
3273 * Do an extra wakeup in case dirty threshold
3274 * changed via sysctl and the explicit transition
3275 * out of shortfall was missed.
3278 if (runningbufspace <= lorunningspace)
3280 msleep(&bd_request, &bdlock, PVM, "psleep", hz);
3283 * We couldn't find any flushable dirty buffers but
3284 * still have too many dirty buffers, we
3285 * have to sleep and try again. (rare)
3287 msleep(&bd_request, &bdlock, PVM, "qsleep", hz / 10);
3295 * Try to flush a buffer in the dirty queue. We must be careful to
3296 * free up B_INVAL buffers instead of write them, which NFS is
3297 * particularly sensitive to.
3299 static int flushwithdeps = 0;
3300 SYSCTL_INT(_vfs, OID_AUTO, flushwithdeps, CTLFLAG_RW, &flushwithdeps,
3301 0, "Number of buffers flushed with dependecies that require rollbacks");
3304 flushbufqueues(struct vnode *lvp, int target, int flushdeps)
3306 struct bufqueue *bq;
3307 struct buf *sentinel;
3319 sentinel = malloc(sizeof(struct buf), M_TEMP, M_WAITOK | M_ZERO);
3320 sentinel->b_qindex = QUEUE_SENTINEL;
3322 TAILQ_INSERT_HEAD(&bq->bq_queue, sentinel, b_freelist);
3324 while (flushed != target) {
3327 bp = TAILQ_NEXT(sentinel, b_freelist);
3329 TAILQ_REMOVE(&bq->bq_queue, sentinel, b_freelist);
3330 TAILQ_INSERT_AFTER(&bq->bq_queue, bp, sentinel,
3337 * Skip sentinels inserted by other invocations of the
3338 * flushbufqueues(), taking care to not reorder them.
3340 * Only flush the buffers that belong to the
3341 * vnode locked by the curthread.
3343 if (bp->b_qindex == QUEUE_SENTINEL || (lvp != NULL &&
3348 error = BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT, NULL);
3354 * BKGRDINPROG can only be set with the buf and bufobj
3355 * locks both held. We tolerate a race to clear it here.
3357 if ((bp->b_vflags & BV_BKGRDINPROG) != 0 ||
3358 (bp->b_flags & B_DELWRI) == 0) {
3362 if (bp->b_flags & B_INVAL) {
3369 if (!LIST_EMPTY(&bp->b_dep) && buf_countdeps(bp, 0)) {
3370 if (flushdeps == 0) {
3378 * We must hold the lock on a vnode before writing
3379 * one of its buffers. Otherwise we may confuse, or
3380 * in the case of a snapshot vnode, deadlock the
3383 * The lock order here is the reverse of the normal
3384 * of vnode followed by buf lock. This is ok because
3385 * the NOWAIT will prevent deadlock.
3388 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
3394 error = vn_lock(vp, LK_EXCLUSIVE | LK_NOWAIT);
3396 ASSERT_VOP_LOCKED(vp, "getbuf");
3398 error = VOP_ISLOCKED(vp) == LK_EXCLUSIVE ? 0 :
3399 vn_lock(vp, LK_TRYUPGRADE);
3402 CTR3(KTR_BUF, "flushbufqueue(%p) vp %p flags %X",
3403 bp, bp->b_vp, bp->b_flags);
3404 if (curproc == bufdaemonproc) {
3409 counter_u64_add(notbufdflushes, 1);
3411 vn_finished_write(mp);
3414 flushwithdeps += hasdeps;
3418 * Sleeping on runningbufspace while holding
3419 * vnode lock leads to deadlock.
3421 if (curproc == bufdaemonproc &&
3422 runningbufspace > hirunningspace)
3423 waitrunningbufspace();
3426 vn_finished_write(mp);
3430 TAILQ_REMOVE(&bq->bq_queue, sentinel, b_freelist);
3432 free(sentinel, M_TEMP);
3437 * Check to see if a block is currently memory resident.
3440 incore(struct bufobj *bo, daddr_t blkno)
3445 bp = gbincore(bo, blkno);
3451 * Returns true if no I/O is needed to access the
3452 * associated VM object. This is like incore except
3453 * it also hunts around in the VM system for the data.
3457 inmem(struct vnode * vp, daddr_t blkno)
3460 vm_offset_t toff, tinc, size;
3464 ASSERT_VOP_LOCKED(vp, "inmem");
3466 if (incore(&vp->v_bufobj, blkno))
3468 if (vp->v_mount == NULL)
3475 if (size > vp->v_mount->mnt_stat.f_iosize)
3476 size = vp->v_mount->mnt_stat.f_iosize;
3477 off = (vm_ooffset_t)blkno * (vm_ooffset_t)vp->v_mount->mnt_stat.f_iosize;
3479 VM_OBJECT_RLOCK(obj);
3480 for (toff = 0; toff < vp->v_mount->mnt_stat.f_iosize; toff += tinc) {
3481 m = vm_page_lookup(obj, OFF_TO_IDX(off + toff));
3485 if (tinc > PAGE_SIZE - ((toff + off) & PAGE_MASK))
3486 tinc = PAGE_SIZE - ((toff + off) & PAGE_MASK);
3487 if (vm_page_is_valid(m,
3488 (vm_offset_t) ((toff + off) & PAGE_MASK), tinc) == 0)
3491 VM_OBJECT_RUNLOCK(obj);
3495 VM_OBJECT_RUNLOCK(obj);
3500 * Set the dirty range for a buffer based on the status of the dirty
3501 * bits in the pages comprising the buffer. The range is limited
3502 * to the size of the buffer.
3504 * Tell the VM system that the pages associated with this buffer
3505 * are clean. This is used for delayed writes where the data is
3506 * going to go to disk eventually without additional VM intevention.
3508 * Note that while we only really need to clean through to b_bcount, we
3509 * just go ahead and clean through to b_bufsize.
3512 vfs_clean_pages_dirty_buf(struct buf *bp)
3514 vm_ooffset_t foff, noff, eoff;
3518 if ((bp->b_flags & B_VMIO) == 0 || bp->b_bufsize == 0)
3521 foff = bp->b_offset;
3522 KASSERT(bp->b_offset != NOOFFSET,
3523 ("vfs_clean_pages_dirty_buf: no buffer offset"));
3525 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
3526 vfs_drain_busy_pages(bp);
3527 vfs_setdirty_locked_object(bp);
3528 for (i = 0; i < bp->b_npages; i++) {
3529 noff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
3531 if (eoff > bp->b_offset + bp->b_bufsize)
3532 eoff = bp->b_offset + bp->b_bufsize;
3534 vfs_page_set_validclean(bp, foff, m);
3535 /* vm_page_clear_dirty(m, foff & PAGE_MASK, eoff - foff); */
3538 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
3542 vfs_setdirty_locked_object(struct buf *bp)
3547 object = bp->b_bufobj->bo_object;
3548 VM_OBJECT_ASSERT_WLOCKED(object);
3551 * We qualify the scan for modified pages on whether the
3552 * object has been flushed yet.
3554 if ((object->flags & OBJ_MIGHTBEDIRTY) != 0) {
3555 vm_offset_t boffset;
3556 vm_offset_t eoffset;
3559 * test the pages to see if they have been modified directly
3560 * by users through the VM system.
3562 for (i = 0; i < bp->b_npages; i++)
3563 vm_page_test_dirty(bp->b_pages[i]);
3566 * Calculate the encompassing dirty range, boffset and eoffset,
3567 * (eoffset - boffset) bytes.
3570 for (i = 0; i < bp->b_npages; i++) {
3571 if (bp->b_pages[i]->dirty)
3574 boffset = (i << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
3576 for (i = bp->b_npages - 1; i >= 0; --i) {
3577 if (bp->b_pages[i]->dirty) {
3581 eoffset = ((i + 1) << PAGE_SHIFT) - (bp->b_offset & PAGE_MASK);
3584 * Fit it to the buffer.
3587 if (eoffset > bp->b_bcount)
3588 eoffset = bp->b_bcount;
3591 * If we have a good dirty range, merge with the existing
3595 if (boffset < eoffset) {
3596 if (bp->b_dirtyoff > boffset)
3597 bp->b_dirtyoff = boffset;
3598 if (bp->b_dirtyend < eoffset)
3599 bp->b_dirtyend = eoffset;
3605 * Allocate the KVA mapping for an existing buffer.
3606 * If an unmapped buffer is provided but a mapped buffer is requested, take
3607 * also care to properly setup mappings between pages and KVA.
3610 bp_unmapped_get_kva(struct buf *bp, daddr_t blkno, int size, int gbflags)
3612 int bsize, maxsize, need_mapping, need_kva;
3615 need_mapping = bp->b_data == unmapped_buf &&
3616 (gbflags & GB_UNMAPPED) == 0;
3617 need_kva = bp->b_kvabase == unmapped_buf &&
3618 bp->b_data == unmapped_buf &&
3619 (gbflags & GB_KVAALLOC) != 0;
3620 if (!need_mapping && !need_kva)
3623 BUF_CHECK_UNMAPPED(bp);
3625 if (need_mapping && bp->b_kvabase != unmapped_buf) {
3627 * Buffer is not mapped, but the KVA was already
3628 * reserved at the time of the instantiation. Use the
3635 * Calculate the amount of the address space we would reserve
3636 * if the buffer was mapped.
3638 bsize = vn_isdisk(bp->b_vp, NULL) ? DEV_BSIZE : bp->b_bufobj->bo_bsize;
3639 KASSERT(bsize != 0, ("bsize == 0, check bo->bo_bsize"));
3640 offset = blkno * bsize;
3641 maxsize = size + (offset & PAGE_MASK);
3642 maxsize = imax(maxsize, bsize);
3644 while (bufkva_alloc(bp, maxsize, gbflags) != 0) {
3645 if ((gbflags & GB_NOWAIT_BD) != 0) {
3647 * XXXKIB: defragmentation cannot
3648 * succeed, not sure what else to do.
3650 panic("GB_NOWAIT_BD and GB_UNMAPPED %p", bp);
3652 counter_u64_add(mappingrestarts, 1);
3653 bufspace_wait(&bdclean[bp->b_domain], bp->b_vp, gbflags, 0, 0);
3657 /* b_offset is handled by bpmap_qenter. */
3658 bp->b_data = bp->b_kvabase;
3659 BUF_CHECK_MAPPED(bp);
3667 * Get a block given a specified block and offset into a file/device.
3668 * The buffers B_DONE bit will be cleared on return, making it almost
3669 * ready for an I/O initiation. B_INVAL may or may not be set on
3670 * return. The caller should clear B_INVAL prior to initiating a
3673 * For a non-VMIO buffer, B_CACHE is set to the opposite of B_INVAL for
3674 * an existing buffer.
3676 * For a VMIO buffer, B_CACHE is modified according to the backing VM.
3677 * If getblk()ing a previously 0-sized invalid buffer, B_CACHE is set
3678 * and then cleared based on the backing VM. If the previous buffer is
3679 * non-0-sized but invalid, B_CACHE will be cleared.
3681 * If getblk() must create a new buffer, the new buffer is returned with
3682 * both B_INVAL and B_CACHE clear unless it is a VMIO buffer, in which
3683 * case it is returned with B_INVAL clear and B_CACHE set based on the
3686 * getblk() also forces a bwrite() for any B_DELWRI buffer whos
3687 * B_CACHE bit is clear.
3689 * What this means, basically, is that the caller should use B_CACHE to
3690 * determine whether the buffer is fully valid or not and should clear
3691 * B_INVAL prior to issuing a read. If the caller intends to validate
3692 * the buffer by loading its data area with something, the caller needs
3693 * to clear B_INVAL. If the caller does this without issuing an I/O,
3694 * the caller should set B_CACHE ( as an optimization ), else the caller
3695 * should issue the I/O and biodone() will set B_CACHE if the I/O was
3696 * a write attempt or if it was a successful read. If the caller
3697 * intends to issue a READ, the caller must clear B_INVAL and BIO_ERROR
3698 * prior to issuing the READ. biodone() will *not* clear B_INVAL.
3701 getblk(struct vnode *vp, daddr_t blkno, int size, int slpflag, int slptimeo,
3706 int bsize, error, maxsize, vmio;
3709 CTR3(KTR_BUF, "getblk(%p, %ld, %d)", vp, (long)blkno, size);
3710 KASSERT((flags & (GB_UNMAPPED | GB_KVAALLOC)) != GB_KVAALLOC,
3711 ("GB_KVAALLOC only makes sense with GB_UNMAPPED"));
3712 ASSERT_VOP_LOCKED(vp, "getblk");
3713 if (size > maxbcachebuf)
3714 panic("getblk: size(%d) > maxbcachebuf(%d)\n", size,
3716 if (!unmapped_buf_allowed)
3717 flags &= ~(GB_UNMAPPED | GB_KVAALLOC);
3722 bp = gbincore(bo, blkno);
3726 * Buffer is in-core. If the buffer is not busy nor managed,
3727 * it must be on a queue.
3729 lockflags = LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK;
3731 if (flags & GB_LOCK_NOWAIT)
3732 lockflags |= LK_NOWAIT;
3734 error = BUF_TIMELOCK(bp, lockflags,
3735 BO_LOCKPTR(bo), "getblk", slpflag, slptimeo);
3738 * If we slept and got the lock we have to restart in case
3739 * the buffer changed identities.
3741 if (error == ENOLCK)
3743 /* We timed out or were interrupted. */
3746 /* If recursed, assume caller knows the rules. */
3747 else if (BUF_LOCKRECURSED(bp))
3751 * The buffer is locked. B_CACHE is cleared if the buffer is
3752 * invalid. Otherwise, for a non-VMIO buffer, B_CACHE is set
3753 * and for a VMIO buffer B_CACHE is adjusted according to the
3756 if (bp->b_flags & B_INVAL)
3757 bp->b_flags &= ~B_CACHE;
3758 else if ((bp->b_flags & (B_VMIO | B_INVAL)) == 0)
3759 bp->b_flags |= B_CACHE;
3760 if (bp->b_flags & B_MANAGED)
3761 MPASS(bp->b_qindex == QUEUE_NONE);
3766 * check for size inconsistencies for non-VMIO case.
3768 if (bp->b_bcount != size) {
3769 if ((bp->b_flags & B_VMIO) == 0 ||
3770 (size > bp->b_kvasize)) {
3771 if (bp->b_flags & B_DELWRI) {
3772 bp->b_flags |= B_NOCACHE;
3775 if (LIST_EMPTY(&bp->b_dep)) {
3776 bp->b_flags |= B_RELBUF;
3779 bp->b_flags |= B_NOCACHE;
3788 * Handle the case of unmapped buffer which should
3789 * become mapped, or the buffer for which KVA
3790 * reservation is requested.
3792 bp_unmapped_get_kva(bp, blkno, size, flags);
3795 * If the size is inconsistent in the VMIO case, we can resize
3796 * the buffer. This might lead to B_CACHE getting set or
3797 * cleared. If the size has not changed, B_CACHE remains
3798 * unchanged from its previous state.
3802 KASSERT(bp->b_offset != NOOFFSET,
3803 ("getblk: no buffer offset"));
3806 * A buffer with B_DELWRI set and B_CACHE clear must
3807 * be committed before we can return the buffer in
3808 * order to prevent the caller from issuing a read
3809 * ( due to B_CACHE not being set ) and overwriting
3812 * Most callers, including NFS and FFS, need this to
3813 * operate properly either because they assume they
3814 * can issue a read if B_CACHE is not set, or because
3815 * ( for example ) an uncached B_DELWRI might loop due
3816 * to softupdates re-dirtying the buffer. In the latter
3817 * case, B_CACHE is set after the first write completes,
3818 * preventing further loops.
3819 * NOTE! b*write() sets B_CACHE. If we cleared B_CACHE
3820 * above while extending the buffer, we cannot allow the
3821 * buffer to remain with B_CACHE set after the write
3822 * completes or it will represent a corrupt state. To
3823 * deal with this we set B_NOCACHE to scrap the buffer
3826 * We might be able to do something fancy, like setting
3827 * B_CACHE in bwrite() except if B_DELWRI is already set,
3828 * so the below call doesn't set B_CACHE, but that gets real
3829 * confusing. This is much easier.
3832 if ((bp->b_flags & (B_CACHE|B_DELWRI)) == B_DELWRI) {
3833 bp->b_flags |= B_NOCACHE;
3837 bp->b_flags &= ~B_DONE;
3840 * Buffer is not in-core, create new buffer. The buffer
3841 * returned by getnewbuf() is locked. Note that the returned
3842 * buffer is also considered valid (not marked B_INVAL).
3846 * If the user does not want us to create the buffer, bail out
3849 if (flags & GB_NOCREAT)
3851 if (bdclean[bo->bo_domain].bd_freebuffers == 0 &&
3852 TD_IS_IDLETHREAD(curthread))
3855 bsize = vn_isdisk(vp, NULL) ? DEV_BSIZE : bo->bo_bsize;
3856 KASSERT(bsize != 0, ("bsize == 0, check bo->bo_bsize"));
3857 offset = blkno * bsize;
3858 vmio = vp->v_object != NULL;
3860 maxsize = size + (offset & PAGE_MASK);
3863 /* Do not allow non-VMIO notmapped buffers. */
3864 flags &= ~(GB_UNMAPPED | GB_KVAALLOC);
3866 maxsize = imax(maxsize, bsize);
3868 bp = getnewbuf(vp, slpflag, slptimeo, maxsize, flags);
3870 if (slpflag || slptimeo)
3873 * XXX This is here until the sleep path is diagnosed
3874 * enough to work under very low memory conditions.
3876 * There's an issue on low memory, 4BSD+non-preempt
3877 * systems (eg MIPS routers with 32MB RAM) where buffer
3878 * exhaustion occurs without sleeping for buffer
3879 * reclaimation. This just sticks in a loop and
3880 * constantly attempts to allocate a buffer, which
3881 * hits exhaustion and tries to wakeup bufdaemon.
3882 * This never happens because we never yield.
3884 * The real solution is to identify and fix these cases
3885 * so we aren't effectively busy-waiting in a loop
3886 * until the reclaimation path has cycles to run.
3888 kern_yield(PRI_USER);
3893 * This code is used to make sure that a buffer is not
3894 * created while the getnewbuf routine is blocked.
3895 * This can be a problem whether the vnode is locked or not.
3896 * If the buffer is created out from under us, we have to
3897 * throw away the one we just created.
3899 * Note: this must occur before we associate the buffer
3900 * with the vp especially considering limitations in
3901 * the splay tree implementation when dealing with duplicate
3905 if (gbincore(bo, blkno)) {
3907 bp->b_flags |= B_INVAL;
3908 bufspace_release(&bdclean[bp->b_domain], maxsize);
3914 * Insert the buffer into the hash, so that it can
3915 * be found by incore.
3917 bp->b_blkno = bp->b_lblkno = blkno;
3918 bp->b_offset = offset;
3923 * set B_VMIO bit. allocbuf() the buffer bigger. Since the
3924 * buffer size starts out as 0, B_CACHE will be set by
3925 * allocbuf() for the VMIO case prior to it testing the
3926 * backing store for validity.
3930 bp->b_flags |= B_VMIO;
3931 KASSERT(vp->v_object == bp->b_bufobj->bo_object,
3932 ("ARGH! different b_bufobj->bo_object %p %p %p\n",
3933 bp, vp->v_object, bp->b_bufobj->bo_object));
3935 bp->b_flags &= ~B_VMIO;
3936 KASSERT(bp->b_bufobj->bo_object == NULL,
3937 ("ARGH! has b_bufobj->bo_object %p %p\n",
3938 bp, bp->b_bufobj->bo_object));
3939 BUF_CHECK_MAPPED(bp);
3943 bufspace_release(&bdclean[bp->b_domain], maxsize);
3944 bp->b_flags &= ~B_DONE;
3946 CTR4(KTR_BUF, "getblk(%p, %ld, %d) = %p", vp, (long)blkno, size, bp);
3947 BUF_ASSERT_HELD(bp);
3949 buf_track(bp, __func__);
3950 KASSERT(bp->b_bufobj == bo,
3951 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
3956 * Get an empty, disassociated buffer of given size. The buffer is initially
3960 geteblk(int size, int flags)
3965 maxsize = (size + BKVAMASK) & ~BKVAMASK;
3966 while ((bp = getnewbuf(NULL, 0, 0, maxsize, flags)) == NULL) {
3967 if ((flags & GB_NOWAIT_BD) &&
3968 (curthread->td_pflags & TDP_BUFNEED) != 0)
3972 bufspace_release(&bdclean[bp->b_domain], maxsize);
3973 bp->b_flags |= B_INVAL; /* b_dep cleared by getnewbuf() */
3974 BUF_ASSERT_HELD(bp);
3979 * Truncate the backing store for a non-vmio buffer.
3982 vfs_nonvmio_truncate(struct buf *bp, int newbsize)
3985 if (bp->b_flags & B_MALLOC) {
3987 * malloced buffers are not shrunk
3989 if (newbsize == 0) {
3990 bufmallocadjust(bp, 0);
3991 free(bp->b_data, M_BIOBUF);
3992 bp->b_data = bp->b_kvabase;
3993 bp->b_flags &= ~B_MALLOC;
3997 vm_hold_free_pages(bp, newbsize);
3998 bufspace_adjust(bp, newbsize);
4002 * Extend the backing for a non-VMIO buffer.
4005 vfs_nonvmio_extend(struct buf *bp, int newbsize)
4011 * We only use malloced memory on the first allocation.
4012 * and revert to page-allocated memory when the buffer
4015 * There is a potential smp race here that could lead
4016 * to bufmallocspace slightly passing the max. It
4017 * is probably extremely rare and not worth worrying
4020 if (bp->b_bufsize == 0 && newbsize <= PAGE_SIZE/2 &&
4021 bufmallocspace < maxbufmallocspace) {
4022 bp->b_data = malloc(newbsize, M_BIOBUF, M_WAITOK);
4023 bp->b_flags |= B_MALLOC;
4024 bufmallocadjust(bp, newbsize);
4029 * If the buffer is growing on its other-than-first
4030 * allocation then we revert to the page-allocation
4035 if (bp->b_flags & B_MALLOC) {
4036 origbuf = bp->b_data;
4037 origbufsize = bp->b_bufsize;
4038 bp->b_data = bp->b_kvabase;
4039 bufmallocadjust(bp, 0);
4040 bp->b_flags &= ~B_MALLOC;
4041 newbsize = round_page(newbsize);
4043 vm_hold_load_pages(bp, (vm_offset_t) bp->b_data + bp->b_bufsize,
4044 (vm_offset_t) bp->b_data + newbsize);
4045 if (origbuf != NULL) {
4046 bcopy(origbuf, bp->b_data, origbufsize);
4047 free(origbuf, M_BIOBUF);
4049 bufspace_adjust(bp, newbsize);
4053 * This code constitutes the buffer memory from either anonymous system
4054 * memory (in the case of non-VMIO operations) or from an associated
4055 * VM object (in the case of VMIO operations). This code is able to
4056 * resize a buffer up or down.
4058 * Note that this code is tricky, and has many complications to resolve
4059 * deadlock or inconsistent data situations. Tread lightly!!!
4060 * There are B_CACHE and B_DELWRI interactions that must be dealt with by
4061 * the caller. Calling this code willy nilly can result in the loss of data.
4063 * allocbuf() only adjusts B_CACHE for VMIO buffers. getblk() deals with
4064 * B_CACHE for the non-VMIO case.
4067 allocbuf(struct buf *bp, int size)
4071 BUF_ASSERT_HELD(bp);
4073 if (bp->b_bcount == size)
4076 if (bp->b_kvasize != 0 && bp->b_kvasize < size)
4077 panic("allocbuf: buffer too small");
4079 newbsize = roundup2(size, DEV_BSIZE);
4080 if ((bp->b_flags & B_VMIO) == 0) {
4081 if ((bp->b_flags & B_MALLOC) == 0)
4082 newbsize = round_page(newbsize);
4084 * Just get anonymous memory from the kernel. Don't
4085 * mess with B_CACHE.
4087 if (newbsize < bp->b_bufsize)
4088 vfs_nonvmio_truncate(bp, newbsize);
4089 else if (newbsize > bp->b_bufsize)
4090 vfs_nonvmio_extend(bp, newbsize);
4094 desiredpages = (size == 0) ? 0 :
4095 num_pages((bp->b_offset & PAGE_MASK) + newbsize);
4097 if (bp->b_flags & B_MALLOC)
4098 panic("allocbuf: VMIO buffer can't be malloced");
4100 * Set B_CACHE initially if buffer is 0 length or will become
4103 if (size == 0 || bp->b_bufsize == 0)
4104 bp->b_flags |= B_CACHE;
4106 if (newbsize < bp->b_bufsize)
4107 vfs_vmio_truncate(bp, desiredpages);
4108 /* XXX This looks as if it should be newbsize > b_bufsize */
4109 else if (size > bp->b_bcount)
4110 vfs_vmio_extend(bp, desiredpages, size);
4111 bufspace_adjust(bp, newbsize);
4113 bp->b_bcount = size; /* requested buffer size. */
4117 extern int inflight_transient_maps;
4120 biodone(struct bio *bp)
4123 void (*done)(struct bio *);
4124 vm_offset_t start, end;
4126 biotrack(bp, __func__);
4127 if ((bp->bio_flags & BIO_TRANSIENT_MAPPING) != 0) {
4128 bp->bio_flags &= ~BIO_TRANSIENT_MAPPING;
4129 bp->bio_flags |= BIO_UNMAPPED;
4130 start = trunc_page((vm_offset_t)bp->bio_data);
4131 end = round_page((vm_offset_t)bp->bio_data + bp->bio_length);
4132 bp->bio_data = unmapped_buf;
4133 pmap_qremove(start, atop(end - start));
4134 vmem_free(transient_arena, start, end - start);
4135 atomic_add_int(&inflight_transient_maps, -1);
4137 done = bp->bio_done;
4139 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4141 bp->bio_flags |= BIO_DONE;
4149 * Wait for a BIO to finish.
4152 biowait(struct bio *bp, const char *wchan)
4156 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4158 while ((bp->bio_flags & BIO_DONE) == 0)
4159 msleep(bp, mtxp, PRIBIO, wchan, 0);
4161 if (bp->bio_error != 0)
4162 return (bp->bio_error);
4163 if (!(bp->bio_flags & BIO_ERROR))
4169 biofinish(struct bio *bp, struct devstat *stat, int error)
4173 bp->bio_error = error;
4174 bp->bio_flags |= BIO_ERROR;
4177 devstat_end_transaction_bio(stat, bp);
4181 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4183 biotrack_buf(struct bio *bp, const char *location)
4186 buf_track(bp->bio_track_bp, location);
4193 * Wait for buffer I/O completion, returning error status. The buffer
4194 * is left locked and B_DONE on return. B_EINTR is converted into an EINTR
4195 * error and cleared.
4198 bufwait(struct buf *bp)
4200 if (bp->b_iocmd == BIO_READ)
4201 bwait(bp, PRIBIO, "biord");
4203 bwait(bp, PRIBIO, "biowr");
4204 if (bp->b_flags & B_EINTR) {
4205 bp->b_flags &= ~B_EINTR;
4208 if (bp->b_ioflags & BIO_ERROR) {
4209 return (bp->b_error ? bp->b_error : EIO);
4218 * Finish I/O on a buffer, optionally calling a completion function.
4219 * This is usually called from an interrupt so process blocking is
4222 * biodone is also responsible for setting B_CACHE in a B_VMIO bp.
4223 * In a non-VMIO bp, B_CACHE will be set on the next getblk()
4224 * assuming B_INVAL is clear.
4226 * For the VMIO case, we set B_CACHE if the op was a read and no
4227 * read error occurred, or if the op was a write. B_CACHE is never
4228 * set if the buffer is invalid or otherwise uncacheable.
4230 * biodone does not mess with B_INVAL, allowing the I/O routine or the
4231 * initiator to leave B_INVAL set to brelse the buffer out of existence
4232 * in the biodone routine.
4235 bufdone(struct buf *bp)
4237 struct bufobj *dropobj;
4238 void (*biodone)(struct buf *);
4240 buf_track(bp, __func__);
4241 CTR3(KTR_BUF, "bufdone(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
4244 KASSERT(!(bp->b_flags & B_DONE), ("biodone: bp %p already done", bp));
4245 BUF_ASSERT_HELD(bp);
4247 runningbufwakeup(bp);
4248 if (bp->b_iocmd == BIO_WRITE)
4249 dropobj = bp->b_bufobj;
4250 /* call optional completion function if requested */
4251 if (bp->b_iodone != NULL) {
4252 biodone = bp->b_iodone;
4253 bp->b_iodone = NULL;
4256 bufobj_wdrop(dropobj);
4259 if (bp->b_flags & B_VMIO) {
4261 * Set B_CACHE if the op was a normal read and no error
4262 * occurred. B_CACHE is set for writes in the b*write()
4265 if (bp->b_iocmd == BIO_READ &&
4266 !(bp->b_flags & (B_INVAL|B_NOCACHE)) &&
4267 !(bp->b_ioflags & BIO_ERROR))
4268 bp->b_flags |= B_CACHE;
4269 vfs_vmio_iodone(bp);
4271 if (!LIST_EMPTY(&bp->b_dep))
4273 if ((bp->b_flags & B_CKHASH) != 0) {
4274 KASSERT(bp->b_iocmd == BIO_READ,
4275 ("bufdone: b_iocmd %d not BIO_READ", bp->b_iocmd));
4276 KASSERT(buf_mapped(bp), ("bufdone: bp %p not mapped", bp));
4277 (*bp->b_ckhashcalc)(bp);
4280 * For asynchronous completions, release the buffer now. The brelse
4281 * will do a wakeup there if necessary - so no need to do a wakeup
4282 * here in the async case. The sync case always needs to do a wakeup.
4284 if (bp->b_flags & B_ASYNC) {
4285 if ((bp->b_flags & (B_NOCACHE | B_INVAL | B_RELBUF)) ||
4286 (bp->b_ioflags & BIO_ERROR))
4293 bufobj_wdrop(dropobj);
4297 * This routine is called in lieu of iodone in the case of
4298 * incomplete I/O. This keeps the busy status for pages
4302 vfs_unbusy_pages(struct buf *bp)
4308 runningbufwakeup(bp);
4309 if (!(bp->b_flags & B_VMIO))
4312 obj = bp->b_bufobj->bo_object;
4313 VM_OBJECT_WLOCK(obj);
4314 for (i = 0; i < bp->b_npages; i++) {
4316 if (m == bogus_page) {
4317 m = vm_page_lookup(obj, OFF_TO_IDX(bp->b_offset) + i);
4319 panic("vfs_unbusy_pages: page missing\n");
4321 if (buf_mapped(bp)) {
4322 BUF_CHECK_MAPPED(bp);
4323 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
4324 bp->b_pages, bp->b_npages);
4326 BUF_CHECK_UNMAPPED(bp);
4330 vm_object_pip_wakeupn(obj, bp->b_npages);
4331 VM_OBJECT_WUNLOCK(obj);
4335 * vfs_page_set_valid:
4337 * Set the valid bits in a page based on the supplied offset. The
4338 * range is restricted to the buffer's size.
4340 * This routine is typically called after a read completes.
4343 vfs_page_set_valid(struct buf *bp, vm_ooffset_t off, vm_page_t m)
4348 * Compute the end offset, eoff, such that [off, eoff) does not span a
4349 * page boundary and eoff is not greater than the end of the buffer.
4350 * The end of the buffer, in this case, is our file EOF, not the
4351 * allocation size of the buffer.
4353 eoff = (off + PAGE_SIZE) & ~(vm_ooffset_t)PAGE_MASK;
4354 if (eoff > bp->b_offset + bp->b_bcount)
4355 eoff = bp->b_offset + bp->b_bcount;
4358 * Set valid range. This is typically the entire buffer and thus the
4362 vm_page_set_valid_range(m, off & PAGE_MASK, eoff - off);
4366 * vfs_page_set_validclean:
4368 * Set the valid bits and clear the dirty bits in a page based on the
4369 * supplied offset. The range is restricted to the buffer's size.
4372 vfs_page_set_validclean(struct buf *bp, vm_ooffset_t off, vm_page_t m)
4374 vm_ooffset_t soff, eoff;
4377 * Start and end offsets in buffer. eoff - soff may not cross a
4378 * page boundary or cross the end of the buffer. The end of the
4379 * buffer, in this case, is our file EOF, not the allocation size
4383 eoff = (off + PAGE_SIZE) & ~(off_t)PAGE_MASK;
4384 if (eoff > bp->b_offset + bp->b_bcount)
4385 eoff = bp->b_offset + bp->b_bcount;
4388 * Set valid range. This is typically the entire buffer and thus the
4392 vm_page_set_validclean(
4394 (vm_offset_t) (soff & PAGE_MASK),
4395 (vm_offset_t) (eoff - soff)
4401 * Ensure that all buffer pages are not exclusive busied. If any page is
4402 * exclusive busy, drain it.
4405 vfs_drain_busy_pages(struct buf *bp)
4410 VM_OBJECT_ASSERT_WLOCKED(bp->b_bufobj->bo_object);
4412 for (i = 0; i < bp->b_npages; i++) {
4414 if (vm_page_xbusied(m)) {
4415 for (; last_busied < i; last_busied++)
4416 vm_page_sbusy(bp->b_pages[last_busied]);
4417 while (vm_page_xbusied(m)) {
4419 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
4420 vm_page_busy_sleep(m, "vbpage", true);
4421 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
4425 for (i = 0; i < last_busied; i++)
4426 vm_page_sunbusy(bp->b_pages[i]);
4430 * This routine is called before a device strategy routine.
4431 * It is used to tell the VM system that paging I/O is in
4432 * progress, and treat the pages associated with the buffer
4433 * almost as being exclusive busy. Also the object paging_in_progress
4434 * flag is handled to make sure that the object doesn't become
4437 * Since I/O has not been initiated yet, certain buffer flags
4438 * such as BIO_ERROR or B_INVAL may be in an inconsistent state
4439 * and should be ignored.
4442 vfs_busy_pages(struct buf *bp, int clear_modify)
4450 if (!(bp->b_flags & B_VMIO))
4453 obj = bp->b_bufobj->bo_object;
4454 foff = bp->b_offset;
4455 KASSERT(bp->b_offset != NOOFFSET,
4456 ("vfs_busy_pages: no buffer offset"));
4457 VM_OBJECT_WLOCK(obj);
4458 vfs_drain_busy_pages(bp);
4459 if (bp->b_bufsize != 0)
4460 vfs_setdirty_locked_object(bp);
4462 for (i = 0; i < bp->b_npages; i++) {
4465 if ((bp->b_flags & B_CLUSTER) == 0) {
4466 vm_object_pip_add(obj, 1);
4470 * When readying a buffer for a read ( i.e
4471 * clear_modify == 0 ), it is important to do
4472 * bogus_page replacement for valid pages in
4473 * partially instantiated buffers. Partially
4474 * instantiated buffers can, in turn, occur when
4475 * reconstituting a buffer from its VM backing store
4476 * base. We only have to do this if B_CACHE is
4477 * clear ( which causes the I/O to occur in the
4478 * first place ). The replacement prevents the read
4479 * I/O from overwriting potentially dirty VM-backed
4480 * pages. XXX bogus page replacement is, uh, bogus.
4481 * It may not work properly with small-block devices.
4482 * We need to find a better way.
4485 pmap_remove_write(m);
4486 vfs_page_set_validclean(bp, foff, m);
4487 } else if (m->valid == VM_PAGE_BITS_ALL &&
4488 (bp->b_flags & B_CACHE) == 0) {
4489 bp->b_pages[i] = bogus_page;
4492 foff = (foff + PAGE_SIZE) & ~(off_t)PAGE_MASK;
4494 VM_OBJECT_WUNLOCK(obj);
4495 if (bogus && buf_mapped(bp)) {
4496 BUF_CHECK_MAPPED(bp);
4497 pmap_qenter(trunc_page((vm_offset_t)bp->b_data),
4498 bp->b_pages, bp->b_npages);
4503 * vfs_bio_set_valid:
4505 * Set the range within the buffer to valid. The range is
4506 * relative to the beginning of the buffer, b_offset. Note that
4507 * b_offset itself may be offset from the beginning of the first
4511 vfs_bio_set_valid(struct buf *bp, int base, int size)
4516 if (!(bp->b_flags & B_VMIO))
4520 * Fixup base to be relative to beginning of first page.
4521 * Set initial n to be the maximum number of bytes in the
4522 * first page that can be validated.
4524 base += (bp->b_offset & PAGE_MASK);
4525 n = PAGE_SIZE - (base & PAGE_MASK);
4527 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
4528 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
4532 vm_page_set_valid_range(m, base & PAGE_MASK, n);
4537 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
4543 * If the specified buffer is a non-VMIO buffer, clear the entire
4544 * buffer. If the specified buffer is a VMIO buffer, clear and
4545 * validate only the previously invalid portions of the buffer.
4546 * This routine essentially fakes an I/O, so we need to clear
4547 * BIO_ERROR and B_INVAL.
4549 * Note that while we only theoretically need to clear through b_bcount,
4550 * we go ahead and clear through b_bufsize.
4553 vfs_bio_clrbuf(struct buf *bp)
4555 int i, j, mask, sa, ea, slide;
4557 if ((bp->b_flags & (B_VMIO | B_MALLOC)) != B_VMIO) {
4561 bp->b_flags &= ~B_INVAL;
4562 bp->b_ioflags &= ~BIO_ERROR;
4563 VM_OBJECT_WLOCK(bp->b_bufobj->bo_object);
4564 if ((bp->b_npages == 1) && (bp->b_bufsize < PAGE_SIZE) &&
4565 (bp->b_offset & PAGE_MASK) == 0) {
4566 if (bp->b_pages[0] == bogus_page)
4568 mask = (1 << (bp->b_bufsize / DEV_BSIZE)) - 1;
4569 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[0]->object);
4570 if ((bp->b_pages[0]->valid & mask) == mask)
4572 if ((bp->b_pages[0]->valid & mask) == 0) {
4573 pmap_zero_page_area(bp->b_pages[0], 0, bp->b_bufsize);
4574 bp->b_pages[0]->valid |= mask;
4578 sa = bp->b_offset & PAGE_MASK;
4580 for (i = 0; i < bp->b_npages; i++, sa = 0) {
4581 slide = imin(slide + PAGE_SIZE, bp->b_offset + bp->b_bufsize);
4582 ea = slide & PAGE_MASK;
4585 if (bp->b_pages[i] == bogus_page)
4588 mask = ((1 << ((ea - sa) / DEV_BSIZE)) - 1) << j;
4589 VM_OBJECT_ASSERT_WLOCKED(bp->b_pages[i]->object);
4590 if ((bp->b_pages[i]->valid & mask) == mask)
4592 if ((bp->b_pages[i]->valid & mask) == 0)
4593 pmap_zero_page_area(bp->b_pages[i], sa, ea - sa);
4595 for (; sa < ea; sa += DEV_BSIZE, j++) {
4596 if ((bp->b_pages[i]->valid & (1 << j)) == 0) {
4597 pmap_zero_page_area(bp->b_pages[i],
4602 bp->b_pages[i]->valid |= mask;
4605 VM_OBJECT_WUNLOCK(bp->b_bufobj->bo_object);
4610 vfs_bio_bzero_buf(struct buf *bp, int base, int size)
4615 if (buf_mapped(bp)) {
4616 BUF_CHECK_MAPPED(bp);
4617 bzero(bp->b_data + base, size);
4619 BUF_CHECK_UNMAPPED(bp);
4620 n = PAGE_SIZE - (base & PAGE_MASK);
4621 for (i = base / PAGE_SIZE; size > 0 && i < bp->b_npages; ++i) {
4625 pmap_zero_page_area(m, base & PAGE_MASK, n);
4634 * Update buffer flags based on I/O request parameters, optionally releasing the
4635 * buffer. If it's VMIO or direct I/O, the buffer pages are released to the VM,
4636 * where they may be placed on a page queue (VMIO) or freed immediately (direct
4637 * I/O). Otherwise the buffer is released to the cache.
4640 b_io_dismiss(struct buf *bp, int ioflag, bool release)
4643 KASSERT((ioflag & IO_NOREUSE) == 0 || (ioflag & IO_VMIO) != 0,
4644 ("buf %p non-VMIO noreuse", bp));
4646 if ((ioflag & IO_DIRECT) != 0)
4647 bp->b_flags |= B_DIRECT;
4648 if ((ioflag & (IO_VMIO | IO_DIRECT)) != 0 && LIST_EMPTY(&bp->b_dep)) {
4649 bp->b_flags |= B_RELBUF;
4650 if ((ioflag & IO_NOREUSE) != 0)
4651 bp->b_flags |= B_NOREUSE;
4659 vfs_bio_brelse(struct buf *bp, int ioflag)
4662 b_io_dismiss(bp, ioflag, true);
4666 vfs_bio_set_flags(struct buf *bp, int ioflag)
4669 b_io_dismiss(bp, ioflag, false);
4673 * vm_hold_load_pages and vm_hold_free_pages get pages into
4674 * a buffers address space. The pages are anonymous and are
4675 * not associated with a file object.
4678 vm_hold_load_pages(struct buf *bp, vm_offset_t from, vm_offset_t to)
4684 BUF_CHECK_MAPPED(bp);
4686 to = round_page(to);
4687 from = round_page(from);
4688 index = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
4690 for (pg = from; pg < to; pg += PAGE_SIZE, index++) {
4692 * note: must allocate system pages since blocking here
4693 * could interfere with paging I/O, no matter which
4696 p = vm_page_alloc(NULL, 0, VM_ALLOC_SYSTEM | VM_ALLOC_NOOBJ |
4697 VM_ALLOC_WIRED | VM_ALLOC_COUNT((to - pg) >> PAGE_SHIFT) |
4699 pmap_qenter(pg, &p, 1);
4700 bp->b_pages[index] = p;
4702 bp->b_npages = index;
4705 /* Return pages associated with this buf to the vm system */
4707 vm_hold_free_pages(struct buf *bp, int newbsize)
4711 int index, newnpages;
4713 BUF_CHECK_MAPPED(bp);
4715 from = round_page((vm_offset_t)bp->b_data + newbsize);
4716 newnpages = (from - trunc_page((vm_offset_t)bp->b_data)) >> PAGE_SHIFT;
4717 if (bp->b_npages > newnpages)
4718 pmap_qremove(from, bp->b_npages - newnpages);
4719 for (index = newnpages; index < bp->b_npages; index++) {
4720 p = bp->b_pages[index];
4721 bp->b_pages[index] = NULL;
4725 vm_wire_sub(bp->b_npages - newnpages);
4726 bp->b_npages = newnpages;
4730 * Map an IO request into kernel virtual address space.
4732 * All requests are (re)mapped into kernel VA space.
4733 * Notice that we use b_bufsize for the size of the buffer
4734 * to be mapped. b_bcount might be modified by the driver.
4736 * Note that even if the caller determines that the address space should
4737 * be valid, a race or a smaller-file mapped into a larger space may
4738 * actually cause vmapbuf() to fail, so all callers of vmapbuf() MUST
4739 * check the return value.
4741 * This function only works with pager buffers.
4744 vmapbuf(struct buf *bp, int mapbuf)
4749 if (bp->b_bufsize < 0)
4751 prot = VM_PROT_READ;
4752 if (bp->b_iocmd == BIO_READ)
4753 prot |= VM_PROT_WRITE; /* Less backwards than it looks */
4754 if ((pidx = vm_fault_quick_hold_pages(&curproc->p_vmspace->vm_map,
4755 (vm_offset_t)bp->b_data, bp->b_bufsize, prot, bp->b_pages,
4756 btoc(MAXPHYS))) < 0)
4758 bp->b_npages = pidx;
4759 bp->b_offset = ((vm_offset_t)bp->b_data) & PAGE_MASK;
4760 if (mapbuf || !unmapped_buf_allowed) {
4761 pmap_qenter((vm_offset_t)bp->b_kvabase, bp->b_pages, pidx);
4762 bp->b_data = bp->b_kvabase + bp->b_offset;
4764 bp->b_data = unmapped_buf;
4769 * Free the io map PTEs associated with this IO operation.
4770 * We also invalidate the TLB entries and restore the original b_addr.
4772 * This function only works with pager buffers.
4775 vunmapbuf(struct buf *bp)
4779 npages = bp->b_npages;
4781 pmap_qremove(trunc_page((vm_offset_t)bp->b_data), npages);
4782 vm_page_unhold_pages(bp->b_pages, npages);
4784 bp->b_data = unmapped_buf;
4788 bdone(struct buf *bp)
4792 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4794 bp->b_flags |= B_DONE;
4800 bwait(struct buf *bp, u_char pri, const char *wchan)
4804 mtxp = mtx_pool_find(mtxpool_sleep, bp);
4806 while ((bp->b_flags & B_DONE) == 0)
4807 msleep(bp, mtxp, pri, wchan, 0);
4812 bufsync(struct bufobj *bo, int waitfor)
4815 return (VOP_FSYNC(bo2vnode(bo), waitfor, curthread));
4819 bufstrategy(struct bufobj *bo, struct buf *bp)
4825 KASSERT(vp == bo->bo_private, ("Inconsistent vnode bufstrategy"));
4826 KASSERT(vp->v_type != VCHR && vp->v_type != VBLK,
4827 ("Wrong vnode in bufstrategy(bp=%p, vp=%p)", bp, vp));
4828 i = VOP_STRATEGY(vp, bp);
4829 KASSERT(i == 0, ("VOP_STRATEGY failed bp=%p vp=%p", bp, bp->b_vp));
4833 * Initialize a struct bufobj before use. Memory is assumed zero filled.
4836 bufobj_init(struct bufobj *bo, void *private)
4838 static volatile int bufobj_cleanq;
4841 atomic_fetchadd_int(&bufobj_cleanq, 1) % clean_domains;
4842 rw_init(BO_LOCKPTR(bo), "bufobj interlock");
4843 bo->bo_private = private;
4844 TAILQ_INIT(&bo->bo_clean.bv_hd);
4845 TAILQ_INIT(&bo->bo_dirty.bv_hd);
4849 bufobj_wrefl(struct bufobj *bo)
4852 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
4853 ASSERT_BO_WLOCKED(bo);
4858 bufobj_wref(struct bufobj *bo)
4861 KASSERT(bo != NULL, ("NULL bo in bufobj_wref"));
4868 bufobj_wdrop(struct bufobj *bo)
4871 KASSERT(bo != NULL, ("NULL bo in bufobj_wdrop"));
4873 KASSERT(bo->bo_numoutput > 0, ("bufobj_wdrop non-positive count"));
4874 if ((--bo->bo_numoutput == 0) && (bo->bo_flag & BO_WWAIT)) {
4875 bo->bo_flag &= ~BO_WWAIT;
4876 wakeup(&bo->bo_numoutput);
4882 bufobj_wwait(struct bufobj *bo, int slpflag, int timeo)
4886 KASSERT(bo != NULL, ("NULL bo in bufobj_wwait"));
4887 ASSERT_BO_WLOCKED(bo);
4889 while (bo->bo_numoutput) {
4890 bo->bo_flag |= BO_WWAIT;
4891 error = msleep(&bo->bo_numoutput, BO_LOCKPTR(bo),
4892 slpflag | (PRIBIO + 1), "bo_wwait", timeo);
4900 * Set bio_data or bio_ma for struct bio from the struct buf.
4903 bdata2bio(struct buf *bp, struct bio *bip)
4906 if (!buf_mapped(bp)) {
4907 KASSERT(unmapped_buf_allowed, ("unmapped"));
4908 bip->bio_ma = bp->b_pages;
4909 bip->bio_ma_n = bp->b_npages;
4910 bip->bio_data = unmapped_buf;
4911 bip->bio_ma_offset = (vm_offset_t)bp->b_offset & PAGE_MASK;
4912 bip->bio_flags |= BIO_UNMAPPED;
4913 KASSERT(round_page(bip->bio_ma_offset + bip->bio_length) /
4914 PAGE_SIZE == bp->b_npages,
4915 ("Buffer %p too short: %d %lld %d", bp, bip->bio_ma_offset,
4916 (long long)bip->bio_length, bip->bio_ma_n));
4918 bip->bio_data = bp->b_data;
4924 * The MIPS pmap code currently doesn't handle aliased pages.
4925 * The VIPT caches may not handle page aliasing themselves, leading
4926 * to data corruption.
4928 * As such, this code makes a system extremely unhappy if said
4929 * system doesn't support unaliasing the above situation in hardware.
4930 * Some "recent" systems (eg some mips24k/mips74k cores) don't enable
4931 * this feature at build time, so it has to be handled in software.
4933 * Once the MIPS pmap/cache code grows to support this function on
4934 * earlier chips, it should be flipped back off.
4937 static int buf_pager_relbuf = 1;
4939 static int buf_pager_relbuf = 0;
4941 SYSCTL_INT(_vfs, OID_AUTO, buf_pager_relbuf, CTLFLAG_RWTUN,
4942 &buf_pager_relbuf, 0,
4943 "Make buffer pager release buffers after reading");
4946 * The buffer pager. It uses buffer reads to validate pages.
4948 * In contrast to the generic local pager from vm/vnode_pager.c, this
4949 * pager correctly and easily handles volumes where the underlying
4950 * device block size is greater than the machine page size. The
4951 * buffer cache transparently extends the requested page run to be
4952 * aligned at the block boundary, and does the necessary bogus page
4953 * replacements in the addends to avoid obliterating already valid
4956 * The only non-trivial issue is that the exclusive busy state for
4957 * pages, which is assumed by the vm_pager_getpages() interface, is
4958 * incompatible with the VMIO buffer cache's desire to share-busy the
4959 * pages. This function performs a trivial downgrade of the pages'
4960 * state before reading buffers, and a less trivial upgrade from the
4961 * shared-busy to excl-busy state after the read.
4964 vfs_bio_getpages(struct vnode *vp, vm_page_t *ma, int count,
4965 int *rbehind, int *rahead, vbg_get_lblkno_t get_lblkno,
4966 vbg_get_blksize_t get_blksize)
4973 vm_ooffset_t la, lb, poff, poffe;
4975 int bo_bs, br_flags, error, i, pgsin, pgsin_a, pgsin_b;
4978 object = vp->v_object;
4980 la = IDX_TO_OFF(ma[count - 1]->pindex);
4981 if (la >= object->un_pager.vnp.vnp_size)
4982 return (VM_PAGER_BAD);
4985 * Change the meaning of la from where the last requested page starts
4986 * to where it ends, because that's the end of the requested region
4987 * and the start of the potential read-ahead region.
4990 lpart = la > object->un_pager.vnp.vnp_size;
4991 bo_bs = get_blksize(vp, get_lblkno(vp, IDX_TO_OFF(ma[0]->pindex)));
4994 * Calculate read-ahead, behind and total pages.
4997 lb = IDX_TO_OFF(ma[0]->pindex);
4998 pgsin_b = OFF_TO_IDX(lb - rounddown2(lb, bo_bs));
5000 if (rbehind != NULL)
5002 pgsin_a = OFF_TO_IDX(roundup2(la, bo_bs) - la);
5003 if (la + IDX_TO_OFF(pgsin_a) >= object->un_pager.vnp.vnp_size)
5004 pgsin_a = OFF_TO_IDX(roundup2(object->un_pager.vnp.vnp_size,
5009 VM_CNT_INC(v_vnodein);
5010 VM_CNT_ADD(v_vnodepgsin, pgsin);
5012 br_flags = (mp != NULL && (mp->mnt_kern_flag & MNTK_UNMAPPED_BUFS)
5013 != 0) ? GB_UNMAPPED : 0;
5014 VM_OBJECT_WLOCK(object);
5016 for (i = 0; i < count; i++)
5017 vm_page_busy_downgrade(ma[i]);
5018 VM_OBJECT_WUNLOCK(object);
5021 for (i = 0; i < count; i++) {
5025 * Pages are shared busy and the object lock is not
5026 * owned, which together allow for the pages'
5027 * invalidation. The racy test for validity avoids
5028 * useless creation of the buffer for the most typical
5029 * case when invalidation is not used in redo or for
5030 * parallel read. The shared->excl upgrade loop at
5031 * the end of the function catches the race in a
5032 * reliable way (protected by the object lock).
5034 if (m->valid == VM_PAGE_BITS_ALL)
5037 poff = IDX_TO_OFF(m->pindex);
5038 poffe = MIN(poff + PAGE_SIZE, object->un_pager.vnp.vnp_size);
5039 for (; poff < poffe; poff += bsize) {
5040 lbn = get_lblkno(vp, poff);
5045 bsize = get_blksize(vp, lbn);
5046 error = bread_gb(vp, lbn, bsize, curthread->td_ucred,
5050 if (LIST_EMPTY(&bp->b_dep)) {
5052 * Invalidation clears m->valid, but
5053 * may leave B_CACHE flag if the
5054 * buffer existed at the invalidation
5055 * time. In this case, recycle the
5056 * buffer to do real read on next
5057 * bread() after redo.
5059 * Otherwise B_RELBUF is not strictly
5060 * necessary, enable to reduce buf
5063 if (buf_pager_relbuf ||
5064 m->valid != VM_PAGE_BITS_ALL)
5065 bp->b_flags |= B_RELBUF;
5067 bp->b_flags &= ~B_NOCACHE;
5073 KASSERT(1 /* racy, enable for debugging */ ||
5074 m->valid == VM_PAGE_BITS_ALL || i == count - 1,
5075 ("buf %d %p invalid", i, m));
5076 if (i == count - 1 && lpart) {
5077 VM_OBJECT_WLOCK(object);
5078 if (m->valid != 0 &&
5079 m->valid != VM_PAGE_BITS_ALL)
5080 vm_page_zero_invalid(m, TRUE);
5081 VM_OBJECT_WUNLOCK(object);
5087 VM_OBJECT_WLOCK(object);
5089 for (i = 0; i < count; i++) {
5090 vm_page_sunbusy(ma[i]);
5091 ma[i] = vm_page_grab(object, ma[i]->pindex, VM_ALLOC_NORMAL);
5094 * Since the pages were only sbusy while neither the
5095 * buffer nor the object lock was held by us, or
5096 * reallocated while vm_page_grab() slept for busy
5097 * relinguish, they could have been invalidated.
5098 * Recheck the valid bits and re-read as needed.
5100 * Note that the last page is made fully valid in the
5101 * read loop, and partial validity for the page at
5102 * index count - 1 could mean that the page was
5103 * invalidated or removed, so we must restart for
5106 if (ma[i]->valid != VM_PAGE_BITS_ALL)
5109 if (redo && error == 0)
5111 VM_OBJECT_WUNLOCK(object);
5112 return (error != 0 ? VM_PAGER_ERROR : VM_PAGER_OK);
5115 #include "opt_ddb.h"
5117 #include <ddb/ddb.h>
5119 /* DDB command to show buffer data */
5120 DB_SHOW_COMMAND(buffer, db_show_buffer)
5123 struct buf *bp = (struct buf *)addr;
5124 #ifdef FULL_BUF_TRACKING
5129 db_printf("usage: show buffer <addr>\n");
5133 db_printf("buf at %p\n", bp);
5134 db_printf("b_flags = 0x%b, b_xflags=0x%b, b_vflags=0x%b\n",
5135 (u_int)bp->b_flags, PRINT_BUF_FLAGS, (u_int)bp->b_xflags,
5136 PRINT_BUF_XFLAGS, (u_int)bp->b_vflags, PRINT_BUF_VFLAGS);
5138 "b_error = %d, b_bufsize = %ld, b_bcount = %ld, b_resid = %ld\n"
5139 "b_bufobj = (%p), b_data = %p, b_blkno = %jd, b_lblkno = %jd, "
5141 bp->b_error, bp->b_bufsize, bp->b_bcount, bp->b_resid,
5142 bp->b_bufobj, bp->b_data, (intmax_t)bp->b_blkno,
5143 (intmax_t)bp->b_lblkno, bp->b_dep.lh_first);
5144 db_printf("b_kvabase = %p, b_kvasize = %d\n",
5145 bp->b_kvabase, bp->b_kvasize);
5148 db_printf("b_npages = %d, pages(OBJ, IDX, PA): ", bp->b_npages);
5149 for (i = 0; i < bp->b_npages; i++) {
5153 db_printf("(%p, 0x%lx, 0x%lx)", m->object,
5155 (u_long)VM_PAGE_TO_PHYS(m));
5157 db_printf("( ??? )");
5158 if ((i + 1) < bp->b_npages)
5163 #if defined(FULL_BUF_TRACKING)
5164 db_printf("b_io_tracking: b_io_tcnt = %u\n", bp->b_io_tcnt);
5166 i = bp->b_io_tcnt % BUF_TRACKING_SIZE;
5167 for (j = 1; j <= BUF_TRACKING_SIZE; j++) {
5168 if (bp->b_io_tracking[BUF_TRACKING_ENTRY(i - j)] == NULL)
5170 db_printf(" %2u: %s\n", j,
5171 bp->b_io_tracking[BUF_TRACKING_ENTRY(i - j)]);
5173 #elif defined(BUF_TRACKING)
5174 db_printf("b_io_tracking: %s\n", bp->b_io_tracking);
5177 BUF_LOCKPRINTINFO(bp);
5180 DB_SHOW_COMMAND(bufqueues, bufqueues)
5182 struct bufdomain *bd;
5185 db_printf("bqempty: %d\n", bqempty.bq_len);
5186 db_printf("bqdirty: %d\n", bqdirty.bq_len);
5188 for (i = 0; i < clean_domains; i++) {
5190 db_printf("Buf domain %d\n", i);
5191 db_printf("\tfreebufs\t%d\n", bd->bd_freebuffers);
5192 db_printf("\tlofreebufs\t%d\n", bd->bd_lofreebuffers);
5193 db_printf("\thifreebufs\t%d\n", bd->bd_hifreebuffers);
5195 db_printf("\tbufspace\t%ld\n", bd->bd_bufspace);
5196 db_printf("\tmaxbufspace\t%ld\n", bd->bd_maxbufspace);
5197 db_printf("\thibufspace\t%ld\n", bd->bd_hibufspace);
5198 db_printf("\tlobufspace\t%ld\n", bd->bd_lobufspace);
5199 db_printf("\tbufspacethresh\t%ld\n", bd->bd_bufspacethresh);
5201 db_printf("\tcleanq count\t%d\n", bd->bd_cleanq->bq_len);
5202 db_printf("\twakeup\t\t%d\n", bd->bd_wanted);
5203 db_printf("\tlim\t\t%d\n", bd->bd_lim);
5204 db_printf("\tCPU ");
5205 for (j = 0; j < mp_maxid + 1; j++)
5206 db_printf("%d, ", bd->bd_subq[j].bq_len);
5211 DB_SHOW_COMMAND(lockedbufs, lockedbufs)
5216 for (i = 0; i < nbuf; i++) {
5218 if (BUF_ISLOCKED(bp)) {
5219 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
5227 DB_SHOW_COMMAND(vnodebufs, db_show_vnodebufs)
5233 db_printf("usage: show vnodebufs <addr>\n");
5236 vp = (struct vnode *)addr;
5237 db_printf("Clean buffers:\n");
5238 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_clean.bv_hd, b_bobufs) {
5239 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
5242 db_printf("Dirty buffers:\n");
5243 TAILQ_FOREACH(bp, &vp->v_bufobj.bo_dirty.bv_hd, b_bobufs) {
5244 db_show_buffer((uintptr_t)bp, 1, 0, NULL);
5249 DB_COMMAND(countfreebufs, db_coundfreebufs)
5252 int i, used = 0, nfree = 0;
5255 db_printf("usage: countfreebufs\n");
5259 for (i = 0; i < nbuf; i++) {
5261 if (bp->b_qindex == QUEUE_EMPTY)
5267 db_printf("Counted %d free, %d used (%d tot)\n", nfree, used,
5269 db_printf("numfreebuffers is %d\n", numfreebuffers);