2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
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.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
40 * External virtual filesystem routines
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 #include "opt_watchdog.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
53 #include <sys/capsicum.h>
54 #include <sys/condvar.h>
56 #include <sys/counter.h>
57 #include <sys/dirent.h>
58 #include <sys/event.h>
59 #include <sys/eventhandler.h>
60 #include <sys/extattr.h>
62 #include <sys/fcntl.h>
65 #include <sys/kernel.h>
66 #include <sys/kthread.h>
68 #include <sys/lockf.h>
69 #include <sys/malloc.h>
70 #include <sys/mount.h>
71 #include <sys/namei.h>
72 #include <sys/pctrie.h>
74 #include <sys/reboot.h>
75 #include <sys/refcount.h>
76 #include <sys/rwlock.h>
77 #include <sys/sched.h>
78 #include <sys/sleepqueue.h>
81 #include <sys/sysctl.h>
82 #include <sys/syslog.h>
83 #include <sys/vmmeter.h>
84 #include <sys/vnode.h>
85 #include <sys/watchdog.h>
87 #include <machine/stdarg.h>
89 #include <security/mac/mac_framework.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_extern.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_page.h>
97 #include <vm/vm_kern.h>
104 static void delmntque(struct vnode *vp);
105 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
106 int slpflag, int slptimeo);
107 static void syncer_shutdown(void *arg, int howto);
108 static int vtryrecycle(struct vnode *vp);
109 static void v_init_counters(struct vnode *);
110 static void v_incr_devcount(struct vnode *);
111 static void v_decr_devcount(struct vnode *);
112 static void vgonel(struct vnode *);
113 static void vfs_knllock(void *arg);
114 static void vfs_knlunlock(void *arg);
115 static void vfs_knl_assert_locked(void *arg);
116 static void vfs_knl_assert_unlocked(void *arg);
117 static void vnlru_return_batches(struct vfsops *mnt_op);
118 static void destroy_vpollinfo(struct vpollinfo *vi);
119 static int v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
120 daddr_t startlbn, daddr_t endlbn);
121 static void vnlru_recalc(void);
124 * These fences are intended for cases where some synchronization is
125 * needed between access of v_iflags and lockless vnode refcount (v_holdcnt
126 * and v_usecount) updates. Access to v_iflags is generally synchronized
127 * by the interlock, but we have some internal assertions that check vnode
128 * flags without acquiring the lock. Thus, these fences are INVARIANTS-only
132 #define VNODE_REFCOUNT_FENCE_ACQ() atomic_thread_fence_acq()
133 #define VNODE_REFCOUNT_FENCE_REL() atomic_thread_fence_rel()
135 #define VNODE_REFCOUNT_FENCE_ACQ()
136 #define VNODE_REFCOUNT_FENCE_REL()
140 * Number of vnodes in existence. Increased whenever getnewvnode()
141 * allocates a new vnode, decreased in vdropl() for VIRF_DOOMED vnode.
143 static u_long __exclusive_cache_line numvnodes;
145 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
146 "Number of vnodes in existence");
148 static counter_u64_t vnodes_created;
149 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
150 "Number of vnodes created by getnewvnode");
152 static u_long mnt_free_list_batch = 128;
153 SYSCTL_ULONG(_vfs, OID_AUTO, mnt_free_list_batch, CTLFLAG_RW,
154 &mnt_free_list_batch, 0, "Limit of vnodes held on mnt's free list");
157 * Conversion tables for conversion from vnode types to inode formats
160 enum vtype iftovt_tab[16] = {
161 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
162 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
164 int vttoif_tab[10] = {
165 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
166 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
170 * List of vnodes that are ready for recycling.
172 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
175 * "Free" vnode target. Free vnodes are rarely completely free, but are
176 * just ones that are cheap to recycle. Usually they are for files which
177 * have been stat'd but not read; these usually have inode and namecache
178 * data attached to them. This target is the preferred minimum size of a
179 * sub-cache consisting mostly of such files. The system balances the size
180 * of this sub-cache with its complement to try to prevent either from
181 * thrashing while the other is relatively inactive. The targets express
182 * a preference for the best balance.
184 * "Above" this target there are 2 further targets (watermarks) related
185 * to recyling of free vnodes. In the best-operating case, the cache is
186 * exactly full, the free list has size between vlowat and vhiwat above the
187 * free target, and recycling from it and normal use maintains this state.
188 * Sometimes the free list is below vlowat or even empty, but this state
189 * is even better for immediate use provided the cache is not full.
190 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free
191 * ones) to reach one of these states. The watermarks are currently hard-
192 * coded as 4% and 9% of the available space higher. These and the default
193 * of 25% for wantfreevnodes are too large if the memory size is large.
194 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim
195 * whenever vnlru_proc() becomes active.
197 static u_long wantfreevnodes;
198 static u_long freevnodes;
199 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD,
200 &freevnodes, 0, "Number of \"free\" vnodes");
202 static counter_u64_t recycles_count;
203 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count,
204 "Number of vnodes recycled to meet vnode cache targets");
206 static counter_u64_t recycles_free_count;
207 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles_free, CTLFLAG_RD, &recycles_free_count,
208 "Number of free vnodes recycled to meet vnode cache targets");
211 * Various variables used for debugging the new implementation of
213 * XXX these are probably of (very) limited utility now.
215 static int reassignbufcalls;
216 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW | CTLFLAG_STATS,
217 &reassignbufcalls, 0, "Number of calls to reassignbuf");
219 static counter_u64_t deferred_inact;
220 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, deferred_inact, CTLFLAG_RD, &deferred_inact,
221 "Number of times inactive processing was deferred");
223 /* To keep more than one thread at a time from running vfs_getnewfsid */
224 static struct mtx mntid_mtx;
227 * Lock for any access to the following:
232 static struct mtx __exclusive_cache_line vnode_free_list_mtx;
234 /* Publicly exported FS */
235 struct nfs_public nfs_pub;
237 static uma_zone_t buf_trie_zone;
239 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
240 static uma_zone_t vnode_zone;
241 static uma_zone_t vnodepoll_zone;
244 * The workitem queue.
246 * It is useful to delay writes of file data and filesystem metadata
247 * for tens of seconds so that quickly created and deleted files need
248 * not waste disk bandwidth being created and removed. To realize this,
249 * we append vnodes to a "workitem" queue. When running with a soft
250 * updates implementation, most pending metadata dependencies should
251 * not wait for more than a few seconds. Thus, mounted on block devices
252 * are delayed only about a half the time that file data is delayed.
253 * Similarly, directory updates are more critical, so are only delayed
254 * about a third the time that file data is delayed. Thus, there are
255 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
256 * one each second (driven off the filesystem syncer process). The
257 * syncer_delayno variable indicates the next queue that is to be processed.
258 * Items that need to be processed soon are placed in this queue:
260 * syncer_workitem_pending[syncer_delayno]
262 * A delay of fifteen seconds is done by placing the request fifteen
263 * entries later in the queue:
265 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
268 static int syncer_delayno;
269 static long syncer_mask;
270 LIST_HEAD(synclist, bufobj);
271 static struct synclist *syncer_workitem_pending;
273 * The sync_mtx protects:
278 * syncer_workitem_pending
279 * syncer_worklist_len
282 static struct mtx sync_mtx;
283 static struct cv sync_wakeup;
285 #define SYNCER_MAXDELAY 32
286 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
287 static int syncdelay = 30; /* max time to delay syncing data */
288 static int filedelay = 30; /* time to delay syncing files */
289 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
290 "Time to delay syncing files (in seconds)");
291 static int dirdelay = 29; /* time to delay syncing directories */
292 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
293 "Time to delay syncing directories (in seconds)");
294 static int metadelay = 28; /* time to delay syncing metadata */
295 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
296 "Time to delay syncing metadata (in seconds)");
297 static int rushjob; /* number of slots to run ASAP */
298 static int stat_rush_requests; /* number of times I/O speeded up */
299 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
300 "Number of times I/O speeded up (rush requests)");
303 * When shutting down the syncer, run it at four times normal speed.
305 #define SYNCER_SHUTDOWN_SPEEDUP 4
306 static int sync_vnode_count;
307 static int syncer_worklist_len;
308 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
311 /* Target for maximum number of vnodes. */
312 u_long desiredvnodes;
313 static u_long gapvnodes; /* gap between wanted and desired */
314 static u_long vhiwat; /* enough extras after expansion */
315 static u_long vlowat; /* minimal extras before expansion */
316 static u_long vstir; /* nonzero to stir non-free vnodes */
317 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */
320 * Note that no attempt is made to sanitize these parameters.
323 sysctl_maxvnodes(SYSCTL_HANDLER_ARGS)
329 error = sysctl_handle_long(oidp, &val, 0, req);
330 if (error != 0 || req->newptr == NULL)
333 if (val == desiredvnodes)
335 mtx_lock(&vnode_free_list_mtx);
337 wantfreevnodes = desiredvnodes / 4;
339 mtx_unlock(&vnode_free_list_mtx);
341 * XXX There is no protection against multiple threads changing
342 * desiredvnodes at the same time. Locking above only helps vnlru and
345 vfs_hash_changesize(desiredvnodes);
346 cache_changesize(desiredvnodes);
350 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
351 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_maxvnodes,
352 "UL", "Target for maximum number of vnodes");
355 sysctl_wantfreevnodes(SYSCTL_HANDLER_ARGS)
360 val = wantfreevnodes;
361 error = sysctl_handle_long(oidp, &val, 0, req);
362 if (error != 0 || req->newptr == NULL)
365 if (val == wantfreevnodes)
367 mtx_lock(&vnode_free_list_mtx);
368 wantfreevnodes = val;
370 mtx_unlock(&vnode_free_list_mtx);
374 SYSCTL_PROC(_vfs, OID_AUTO, wantfreevnodes,
375 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_wantfreevnodes,
376 "UL", "Target for minimum number of \"free\" vnodes");
378 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
379 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)");
380 static int vnlru_nowhere;
381 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
382 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
385 sysctl_try_reclaim_vnode(SYSCTL_HANDLER_ARGS)
390 unsigned long ndflags;
393 if (req->newptr == NULL)
395 if (req->newlen >= PATH_MAX)
398 buf = malloc(PATH_MAX, M_TEMP, M_WAITOK);
399 error = SYSCTL_IN(req, buf, req->newlen);
403 buf[req->newlen] = '\0';
405 ndflags = LOCKLEAF | NOFOLLOW | AUDITVNODE1 | NOCACHE | SAVENAME;
406 NDINIT(&nd, LOOKUP, ndflags, UIO_SYSSPACE, buf, curthread);
407 if ((error = namei(&nd)) != 0)
411 if (VN_IS_DOOMED(vp)) {
413 * This vnode is being recycled. Return != 0 to let the caller
414 * know that the sysctl had no effect. Return EAGAIN because a
415 * subsequent call will likely succeed (since namei will create
416 * a new vnode if necessary)
422 counter_u64_add(recycles_count, 1);
432 sysctl_ftry_reclaim_vnode(SYSCTL_HANDLER_ARGS)
434 struct thread *td = curthread;
440 if (req->newptr == NULL)
443 error = sysctl_handle_int(oidp, &fd, 0, req);
446 error = getvnode(curthread, fd, &cap_fcntl_rights, &fp);
451 error = vn_lock(vp, LK_EXCLUSIVE);
455 counter_u64_add(recycles_count, 1);
463 SYSCTL_PROC(_debug, OID_AUTO, try_reclaim_vnode,
464 CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
465 sysctl_try_reclaim_vnode, "A", "Try to reclaim a vnode by its pathname");
466 SYSCTL_PROC(_debug, OID_AUTO, ftry_reclaim_vnode,
467 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
468 sysctl_ftry_reclaim_vnode, "I",
469 "Try to reclaim a vnode by its file descriptor");
471 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
475 * Support for the bufobj clean & dirty pctrie.
478 buf_trie_alloc(struct pctrie *ptree)
481 return uma_zalloc(buf_trie_zone, M_NOWAIT);
485 buf_trie_free(struct pctrie *ptree, void *node)
488 uma_zfree(buf_trie_zone, node);
490 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
493 * Initialize the vnode management data structures.
495 * Reevaluate the following cap on the number of vnodes after the physical
496 * memory size exceeds 512GB. In the limit, as the physical memory size
497 * grows, the ratio of the memory size in KB to vnodes approaches 64:1.
499 #ifndef MAXVNODES_MAX
500 #define MAXVNODES_MAX (512UL * 1024 * 1024 / 64) /* 8M */
503 static MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
505 static struct vnode *
506 vn_alloc_marker(struct mount *mp)
510 vp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
511 vp->v_type = VMARKER;
518 vn_free_marker(struct vnode *vp)
521 MPASS(vp->v_type == VMARKER);
522 free(vp, M_VNODE_MARKER);
526 * Initialize a vnode as it first enters the zone.
529 vnode_init(void *mem, int size, int flags)
538 vp->v_vnlock = &vp->v_lock;
539 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
541 * By default, don't allow shared locks unless filesystems opt-in.
543 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT,
544 LK_NOSHARE | LK_IS_VNODE);
548 bufobj_init(&vp->v_bufobj, vp);
550 * Initialize namecache.
552 LIST_INIT(&vp->v_cache_src);
553 TAILQ_INIT(&vp->v_cache_dst);
555 * Initialize rangelocks.
557 rangelock_init(&vp->v_rl);
562 * Free a vnode when it is cleared from the zone.
565 vnode_fini(void *mem, int size)
571 rangelock_destroy(&vp->v_rl);
572 lockdestroy(vp->v_vnlock);
573 mtx_destroy(&vp->v_interlock);
575 rw_destroy(BO_LOCKPTR(bo));
579 * Provide the size of NFS nclnode and NFS fh for calculation of the
580 * vnode memory consumption. The size is specified directly to
581 * eliminate dependency on NFS-private header.
583 * Other filesystems may use bigger or smaller (like UFS and ZFS)
584 * private inode data, but the NFS-based estimation is ample enough.
585 * Still, we care about differences in the size between 64- and 32-bit
588 * Namecache structure size is heuristically
589 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1.
592 #define NFS_NCLNODE_SZ (528 + 64)
595 #define NFS_NCLNODE_SZ (360 + 32)
600 vntblinit(void *dummy __unused)
603 int physvnodes, virtvnodes;
606 * Desiredvnodes is a function of the physical memory size and the
607 * kernel's heap size. Generally speaking, it scales with the
608 * physical memory size. The ratio of desiredvnodes to the physical
609 * memory size is 1:16 until desiredvnodes exceeds 98,304.
611 * marginal ratio of desiredvnodes to the physical memory size is
612 * 1:64. However, desiredvnodes is limited by the kernel's heap
613 * size. The memory required by desiredvnodes vnodes and vm objects
614 * must not exceed 1/10th of the kernel's heap size.
616 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 +
617 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64;
618 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) +
619 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ));
620 desiredvnodes = min(physvnodes, virtvnodes);
621 if (desiredvnodes > MAXVNODES_MAX) {
623 printf("Reducing kern.maxvnodes %lu -> %lu\n",
624 desiredvnodes, MAXVNODES_MAX);
625 desiredvnodes = MAXVNODES_MAX;
627 wantfreevnodes = desiredvnodes / 4;
628 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
629 TAILQ_INIT(&vnode_free_list);
630 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
632 * The lock is taken to appease WITNESS.
634 mtx_lock(&vnode_free_list_mtx);
636 mtx_unlock(&vnode_free_list_mtx);
637 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
638 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0);
639 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
640 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
642 * Preallocate enough nodes to support one-per buf so that
643 * we can not fail an insert. reassignbuf() callers can not
644 * tolerate the insertion failure.
646 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
647 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
648 UMA_ZONE_NOFREE | UMA_ZONE_VM);
649 uma_prealloc(buf_trie_zone, nbuf);
651 vnodes_created = counter_u64_alloc(M_WAITOK);
652 recycles_count = counter_u64_alloc(M_WAITOK);
653 recycles_free_count = counter_u64_alloc(M_WAITOK);
654 deferred_inact = counter_u64_alloc(M_WAITOK);
657 * Initialize the filesystem syncer.
659 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
661 syncer_maxdelay = syncer_mask + 1;
662 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
663 cv_init(&sync_wakeup, "syncer");
664 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
668 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
672 * Mark a mount point as busy. Used to synchronize access and to delay
673 * unmounting. Eventually, mountlist_mtx is not released on failure.
675 * vfs_busy() is a custom lock, it can block the caller.
676 * vfs_busy() only sleeps if the unmount is active on the mount point.
677 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
678 * vnode belonging to mp.
680 * Lookup uses vfs_busy() to traverse mount points.
682 * / vnode lock A / vnode lock (/var) D
683 * /var vnode lock B /log vnode lock(/var/log) E
684 * vfs_busy lock C vfs_busy lock F
686 * Within each file system, the lock order is C->A->B and F->D->E.
688 * When traversing across mounts, the system follows that lock order:
694 * The lookup() process for namei("/var") illustrates the process:
695 * VOP_LOOKUP() obtains B while A is held
696 * vfs_busy() obtains a shared lock on F while A and B are held
697 * vput() releases lock on B
698 * vput() releases lock on A
699 * VFS_ROOT() obtains lock on D while shared lock on F is held
700 * vfs_unbusy() releases shared lock on F
701 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
702 * Attempt to lock A (instead of vp_crossmp) while D is held would
703 * violate the global order, causing deadlocks.
705 * dounmount() locks B while F is drained.
708 vfs_busy(struct mount *mp, int flags)
711 MPASS((flags & ~MBF_MASK) == 0);
712 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
714 if (vfs_op_thread_enter(mp)) {
715 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
716 MPASS((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0);
717 MPASS((mp->mnt_kern_flag & MNTK_REFEXPIRE) == 0);
718 vfs_mp_count_add_pcpu(mp, ref, 1);
719 vfs_mp_count_add_pcpu(mp, lockref, 1);
720 vfs_op_thread_exit(mp);
721 if (flags & MBF_MNTLSTLOCK)
722 mtx_unlock(&mountlist_mtx);
727 vfs_assert_mount_counters(mp);
730 * If mount point is currently being unmounted, sleep until the
731 * mount point fate is decided. If thread doing the unmounting fails,
732 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
733 * that this mount point has survived the unmount attempt and vfs_busy
734 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
735 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
736 * about to be really destroyed. vfs_busy needs to release its
737 * reference on the mount point in this case and return with ENOENT,
738 * telling the caller that mount mount it tried to busy is no longer
741 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
742 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
745 CTR1(KTR_VFS, "%s: failed busying before sleeping",
749 if (flags & MBF_MNTLSTLOCK)
750 mtx_unlock(&mountlist_mtx);
751 mp->mnt_kern_flag |= MNTK_MWAIT;
752 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
753 if (flags & MBF_MNTLSTLOCK)
754 mtx_lock(&mountlist_mtx);
757 if (flags & MBF_MNTLSTLOCK)
758 mtx_unlock(&mountlist_mtx);
765 * Free a busy filesystem.
768 vfs_unbusy(struct mount *mp)
772 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
774 if (vfs_op_thread_enter(mp)) {
775 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
776 vfs_mp_count_sub_pcpu(mp, lockref, 1);
777 vfs_mp_count_sub_pcpu(mp, ref, 1);
778 vfs_op_thread_exit(mp);
783 vfs_assert_mount_counters(mp);
785 c = --mp->mnt_lockref;
786 if (mp->mnt_vfs_ops == 0) {
787 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
792 vfs_dump_mount_counters(mp);
793 if (c == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
794 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
795 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
796 mp->mnt_kern_flag &= ~MNTK_DRAINING;
797 wakeup(&mp->mnt_lockref);
803 * Lookup a mount point by filesystem identifier.
806 vfs_getvfs(fsid_t *fsid)
810 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
811 mtx_lock(&mountlist_mtx);
812 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
813 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
814 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
816 mtx_unlock(&mountlist_mtx);
820 mtx_unlock(&mountlist_mtx);
821 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
822 return ((struct mount *) 0);
826 * Lookup a mount point by filesystem identifier, busying it before
829 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
830 * cache for popular filesystem identifiers. The cache is lockess, using
831 * the fact that struct mount's are never freed. In worst case we may
832 * get pointer to unmounted or even different filesystem, so we have to
833 * check what we got, and go slow way if so.
836 vfs_busyfs(fsid_t *fsid)
838 #define FSID_CACHE_SIZE 256
839 typedef struct mount * volatile vmp_t;
840 static vmp_t cache[FSID_CACHE_SIZE];
845 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
846 hash = fsid->val[0] ^ fsid->val[1];
847 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
850 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
851 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
853 if (vfs_busy(mp, 0) != 0) {
857 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
858 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
864 mtx_lock(&mountlist_mtx);
865 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
866 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
867 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
868 error = vfs_busy(mp, MBF_MNTLSTLOCK);
871 mtx_unlock(&mountlist_mtx);
878 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
879 mtx_unlock(&mountlist_mtx);
880 return ((struct mount *) 0);
884 * Check if a user can access privileged mount options.
887 vfs_suser(struct mount *mp, struct thread *td)
891 if (jailed(td->td_ucred)) {
893 * If the jail of the calling thread lacks permission for
894 * this type of file system, deny immediately.
896 if (!prison_allow(td->td_ucred, mp->mnt_vfc->vfc_prison_flag))
900 * If the file system was mounted outside the jail of the
901 * calling thread, deny immediately.
903 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
908 * If file system supports delegated administration, we don't check
909 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
910 * by the file system itself.
911 * If this is not the user that did original mount, we check for
912 * the PRIV_VFS_MOUNT_OWNER privilege.
914 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
915 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
916 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
923 * Get a new unique fsid. Try to make its val[0] unique, since this value
924 * will be used to create fake device numbers for stat(). Also try (but
925 * not so hard) make its val[0] unique mod 2^16, since some emulators only
926 * support 16-bit device numbers. We end up with unique val[0]'s for the
927 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
929 * Keep in mind that several mounts may be running in parallel. Starting
930 * the search one past where the previous search terminated is both a
931 * micro-optimization and a defense against returning the same fsid to
935 vfs_getnewfsid(struct mount *mp)
937 static uint16_t mntid_base;
942 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
943 mtx_lock(&mntid_mtx);
944 mtype = mp->mnt_vfc->vfc_typenum;
945 tfsid.val[1] = mtype;
946 mtype = (mtype & 0xFF) << 24;
948 tfsid.val[0] = makedev(255,
949 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
951 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
955 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
956 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
957 mtx_unlock(&mntid_mtx);
961 * Knob to control the precision of file timestamps:
963 * 0 = seconds only; nanoseconds zeroed.
964 * 1 = seconds and nanoseconds, accurate within 1/HZ.
965 * 2 = seconds and nanoseconds, truncated to microseconds.
966 * >=3 = seconds and nanoseconds, maximum precision.
968 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
970 static int timestamp_precision = TSP_USEC;
971 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
972 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
973 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, "
974 "3+: sec + ns (max. precision))");
977 * Get a current timestamp.
980 vfs_timestamp(struct timespec *tsp)
984 switch (timestamp_precision) {
986 tsp->tv_sec = time_second;
994 TIMEVAL_TO_TIMESPEC(&tv, tsp);
1004 * Set vnode attributes to VNOVAL
1007 vattr_null(struct vattr *vap)
1010 vap->va_type = VNON;
1011 vap->va_size = VNOVAL;
1012 vap->va_bytes = VNOVAL;
1013 vap->va_mode = VNOVAL;
1014 vap->va_nlink = VNOVAL;
1015 vap->va_uid = VNOVAL;
1016 vap->va_gid = VNOVAL;
1017 vap->va_fsid = VNOVAL;
1018 vap->va_fileid = VNOVAL;
1019 vap->va_blocksize = VNOVAL;
1020 vap->va_rdev = VNOVAL;
1021 vap->va_atime.tv_sec = VNOVAL;
1022 vap->va_atime.tv_nsec = VNOVAL;
1023 vap->va_mtime.tv_sec = VNOVAL;
1024 vap->va_mtime.tv_nsec = VNOVAL;
1025 vap->va_ctime.tv_sec = VNOVAL;
1026 vap->va_ctime.tv_nsec = VNOVAL;
1027 vap->va_birthtime.tv_sec = VNOVAL;
1028 vap->va_birthtime.tv_nsec = VNOVAL;
1029 vap->va_flags = VNOVAL;
1030 vap->va_gen = VNOVAL;
1031 vap->va_vaflags = 0;
1035 * This routine is called when we have too many vnodes. It attempts
1036 * to free <count> vnodes and will potentially free vnodes that still
1037 * have VM backing store (VM backing store is typically the cause
1038 * of a vnode blowout so we want to do this). Therefore, this operation
1039 * is not considered cheap.
1041 * A number of conditions may prevent a vnode from being reclaimed.
1042 * the buffer cache may have references on the vnode, a directory
1043 * vnode may still have references due to the namei cache representing
1044 * underlying files, or the vnode may be in active use. It is not
1045 * desirable to reuse such vnodes. These conditions may cause the
1046 * number of vnodes to reach some minimum value regardless of what
1047 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
1049 * @param mp Try to reclaim vnodes from this mountpoint
1050 * @param reclaim_nc_src Only reclaim directories with outgoing namecache
1051 * entries if this argument is strue
1052 * @param trigger Only reclaim vnodes with fewer than this many resident
1054 * @return The number of vnodes that were reclaimed.
1057 vlrureclaim(struct mount *mp, bool reclaim_nc_src, int trigger)
1060 int count, done, target;
1063 vn_start_write(NULL, &mp, V_WAIT);
1065 count = mp->mnt_nvnodelistsize;
1066 target = count * (int64_t)gapvnodes / imax(desiredvnodes, 1);
1067 target = target / 10 + 1;
1068 while (count != 0 && done < target) {
1069 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
1070 while (vp != NULL && vp->v_type == VMARKER)
1071 vp = TAILQ_NEXT(vp, v_nmntvnodes);
1075 * XXX LRU is completely broken for non-free vnodes. First
1076 * by calling here in mountpoint order, then by moving
1077 * unselected vnodes to the end here, and most grossly by
1078 * removing the vlruvp() function that was supposed to
1079 * maintain the order. (This function was born broken
1080 * since syncer problems prevented it doing anything.) The
1081 * order is closer to LRC (C = Created).
1083 * LRU reclaiming of vnodes seems to have last worked in
1084 * FreeBSD-3 where LRU wasn't mentioned under any spelling.
1085 * Then there was no hold count, and inactive vnodes were
1086 * simply put on the free list in LRU order. The separate
1087 * lists also break LRU. We prefer to reclaim from the
1088 * free list for technical reasons. This tends to thrash
1089 * the free list to keep very unrecently used held vnodes.
1090 * The problem is mitigated by keeping the free list large.
1092 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1093 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1095 if (!VI_TRYLOCK(vp))
1098 * If it's been deconstructed already, it's still
1099 * referenced, or it exceeds the trigger, skip it.
1100 * Also skip free vnodes. We are trying to make space
1101 * to expand the free list, not reduce it.
1103 if (vp->v_usecount ||
1104 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
1105 ((vp->v_iflag & VI_FREE) != 0) ||
1106 VN_IS_DOOMED(vp) || (vp->v_object != NULL &&
1107 vp->v_object->resident_page_count > trigger)) {
1113 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
1115 goto next_iter_mntunlocked;
1119 * v_usecount may have been bumped after VOP_LOCK() dropped
1120 * the vnode interlock and before it was locked again.
1122 * It is not necessary to recheck VIRF_DOOMED because it can
1123 * only be set by another thread that holds both the vnode
1124 * lock and vnode interlock. If another thread has the
1125 * vnode lock before we get to VOP_LOCK() and obtains the
1126 * vnode interlock after VOP_LOCK() drops the vnode
1127 * interlock, the other thread will be unable to drop the
1128 * vnode lock before our VOP_LOCK() call fails.
1130 if (vp->v_usecount ||
1131 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
1132 (vp->v_object != NULL &&
1133 vp->v_object->resident_page_count > trigger)) {
1136 goto next_iter_mntunlocked;
1138 KASSERT(!VN_IS_DOOMED(vp),
1139 ("VIRF_DOOMED unexpectedly detected in vlrureclaim()"));
1140 counter_u64_add(recycles_count, 1);
1145 next_iter_mntunlocked:
1146 if (!should_yield())
1150 if (!should_yield())
1154 kern_yield(PRI_USER);
1159 vn_finished_write(mp);
1163 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */
1164 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free,
1166 "limit on vnode free requests per call to the vnlru_free routine");
1169 * Attempt to reduce the free list by the requested amount.
1172 vnlru_free_locked(int count, struct vfsops *mnt_op)
1178 tried_batches = false;
1179 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1180 if (count > max_vnlru_free)
1181 count = max_vnlru_free;
1182 for (; count > 0; count--) {
1183 vp = TAILQ_FIRST(&vnode_free_list);
1185 * The list can be modified while the free_list_mtx
1186 * has been dropped and vp could be NULL here.
1191 mtx_unlock(&vnode_free_list_mtx);
1192 vnlru_return_batches(mnt_op);
1193 tried_batches = true;
1194 mtx_lock(&vnode_free_list_mtx);
1198 VNASSERT(vp->v_op != NULL, vp,
1199 ("vnlru_free: vnode already reclaimed."));
1200 KASSERT((vp->v_iflag & VI_FREE) != 0,
1201 ("Removing vnode not on freelist"));
1202 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1203 ("Mangling active vnode"));
1204 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
1207 * Don't recycle if our vnode is from different type
1208 * of mount point. Note that mp is type-safe, the
1209 * check does not reach unmapped address even if
1210 * vnode is reclaimed.
1211 * Don't recycle if we can't get the interlock without
1214 if ((mnt_op != NULL && (mp = vp->v_mount) != NULL &&
1215 mp->mnt_op != mnt_op) || !VI_TRYLOCK(vp)) {
1216 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
1219 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
1220 vp, ("vp inconsistent on freelist"));
1223 * The clear of VI_FREE prevents activation of the
1224 * vnode. There is no sense in putting the vnode on
1225 * the mount point active list, only to remove it
1226 * later during recycling. Inline the relevant part
1227 * of vholdl(), to avoid triggering assertions or
1231 vp->v_iflag &= ~VI_FREE;
1232 VNODE_REFCOUNT_FENCE_REL();
1233 refcount_acquire(&vp->v_holdcnt);
1235 mtx_unlock(&vnode_free_list_mtx);
1239 * If the recycled succeeded this vdrop will actually free
1240 * the vnode. If not it will simply place it back on
1244 mtx_lock(&vnode_free_list_mtx);
1249 vnlru_free(int count, struct vfsops *mnt_op)
1252 mtx_lock(&vnode_free_list_mtx);
1253 vnlru_free_locked(count, mnt_op);
1254 mtx_unlock(&vnode_free_list_mtx);
1261 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1262 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100);
1263 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */
1264 vlowat = vhiwat / 2;
1267 /* XXX some names and initialization are bad for limits and watermarks. */
1271 u_long rnumvnodes, rfreevnodes;
1274 rnumvnodes = atomic_load_long(&numvnodes);
1275 rfreevnodes = atomic_load_long(&freevnodes);
1276 if (rnumvnodes > desiredvnodes)
1278 space = desiredvnodes - rnumvnodes;
1279 if (freevnodes > wantfreevnodes)
1280 space += rfreevnodes - wantfreevnodes;
1285 vnlru_return_batch_locked(struct mount *mp)
1289 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
1291 if (mp->mnt_tmpfreevnodelistsize == 0)
1294 TAILQ_FOREACH(vp, &mp->mnt_tmpfreevnodelist, v_actfreelist) {
1295 VNASSERT((vp->v_mflag & VMP_TMPMNTFREELIST) != 0, vp,
1296 ("vnode without VMP_TMPMNTFREELIST on mnt_tmpfreevnodelist"));
1297 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
1299 mtx_lock(&vnode_free_list_mtx);
1300 TAILQ_CONCAT(&vnode_free_list, &mp->mnt_tmpfreevnodelist, v_actfreelist);
1301 freevnodes += mp->mnt_tmpfreevnodelistsize;
1302 mtx_unlock(&vnode_free_list_mtx);
1303 mp->mnt_tmpfreevnodelistsize = 0;
1307 vnlru_return_batch(struct mount *mp)
1310 mtx_lock(&mp->mnt_listmtx);
1311 vnlru_return_batch_locked(mp);
1312 mtx_unlock(&mp->mnt_listmtx);
1316 vnlru_return_batches(struct vfsops *mnt_op)
1318 struct mount *mp, *nmp;
1321 mtx_lock(&mountlist_mtx);
1322 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1323 need_unbusy = false;
1324 if (mnt_op != NULL && mp->mnt_op != mnt_op)
1326 if (mp->mnt_tmpfreevnodelistsize == 0)
1328 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) == 0) {
1329 vnlru_return_batch(mp);
1331 mtx_lock(&mountlist_mtx);
1334 nmp = TAILQ_NEXT(mp, mnt_list);
1338 mtx_unlock(&mountlist_mtx);
1342 * Attempt to recycle vnodes in a context that is always safe to block.
1343 * Calling vlrurecycle() from the bowels of filesystem code has some
1344 * interesting deadlock problems.
1346 static struct proc *vnlruproc;
1347 static int vnlruproc_sig;
1352 u_long rnumvnodes, rfreevnodes;
1353 struct mount *mp, *nmp;
1354 unsigned long onumvnodes;
1355 int done, force, trigger, usevnodes, vsp;
1356 bool reclaim_nc_src;
1358 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc,
1359 SHUTDOWN_PRI_FIRST);
1363 kproc_suspend_check(vnlruproc);
1364 mtx_lock(&vnode_free_list_mtx);
1365 rnumvnodes = atomic_load_long(&numvnodes);
1367 * If numvnodes is too large (due to desiredvnodes being
1368 * adjusted using its sysctl, or emergency growth), first
1369 * try to reduce it by discarding from the free list.
1371 if (rnumvnodes > desiredvnodes)
1372 vnlru_free_locked(rnumvnodes - desiredvnodes, NULL);
1374 * Sleep if the vnode cache is in a good state. This is
1375 * when it is not over-full and has space for about a 4%
1376 * or 9% expansion (by growing its size or inexcessively
1377 * reducing its free list). Otherwise, try to reclaim
1378 * space for a 10% expansion.
1380 if (vstir && force == 0) {
1385 if (vsp >= vlowat && force == 0) {
1387 wakeup(&vnlruproc_sig);
1388 msleep(vnlruproc, &vnode_free_list_mtx,
1389 PVFS|PDROP, "vlruwt", hz);
1392 mtx_unlock(&vnode_free_list_mtx);
1394 rnumvnodes = atomic_load_long(&numvnodes);
1395 rfreevnodes = atomic_load_long(&freevnodes);
1397 onumvnodes = rnumvnodes;
1399 * Calculate parameters for recycling. These are the same
1400 * throughout the loop to give some semblance of fairness.
1401 * The trigger point is to avoid recycling vnodes with lots
1402 * of resident pages. We aren't trying to free memory; we
1403 * are trying to recycle or at least free vnodes.
1405 if (rnumvnodes <= desiredvnodes)
1406 usevnodes = rnumvnodes - rfreevnodes;
1408 usevnodes = rnumvnodes;
1412 * The trigger value is is chosen to give a conservatively
1413 * large value to ensure that it alone doesn't prevent
1414 * making progress. The value can easily be so large that
1415 * it is effectively infinite in some congested and
1416 * misconfigured cases, and this is necessary. Normally
1417 * it is about 8 to 100 (pages), which is quite large.
1419 trigger = vm_cnt.v_page_count * 2 / usevnodes;
1421 trigger = vsmalltrigger;
1422 reclaim_nc_src = force >= 3;
1423 mtx_lock(&mountlist_mtx);
1424 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1425 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
1426 nmp = TAILQ_NEXT(mp, mnt_list);
1429 done += vlrureclaim(mp, reclaim_nc_src, trigger);
1430 mtx_lock(&mountlist_mtx);
1431 nmp = TAILQ_NEXT(mp, mnt_list);
1434 mtx_unlock(&mountlist_mtx);
1435 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes)
1436 uma_reclaim(UMA_RECLAIM_DRAIN);
1438 if (force == 0 || force == 1) {
1448 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
1450 kern_yield(PRI_USER);
1452 * After becoming active to expand above low water, keep
1453 * active until above high water.
1456 force = vsp < vhiwat;
1460 static struct kproc_desc vnlru_kp = {
1465 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
1469 * Routines having to do with the management of the vnode table.
1473 * Try to recycle a freed vnode. We abort if anyone picks up a reference
1474 * before we actually vgone(). This function must be called with the vnode
1475 * held to prevent the vnode from being returned to the free list midway
1479 vtryrecycle(struct vnode *vp)
1483 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
1484 VNASSERT(vp->v_holdcnt, vp,
1485 ("vtryrecycle: Recycling vp %p without a reference.", vp));
1487 * This vnode may found and locked via some other list, if so we
1488 * can't recycle it yet.
1490 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1492 "%s: impossible to recycle, vp %p lock is already held",
1494 return (EWOULDBLOCK);
1497 * Don't recycle if its filesystem is being suspended.
1499 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1502 "%s: impossible to recycle, cannot start the write for %p",
1507 * If we got this far, we need to acquire the interlock and see if
1508 * anyone picked up this vnode from another list. If not, we will
1509 * mark it with DOOMED via vgonel() so that anyone who does find it
1510 * will skip over it.
1513 if (vp->v_usecount) {
1516 vn_finished_write(vnmp);
1518 "%s: impossible to recycle, %p is already referenced",
1522 if (!VN_IS_DOOMED(vp)) {
1523 counter_u64_add(recycles_free_count, 1);
1528 vn_finished_write(vnmp);
1538 if (vsp < vlowat && vnlruproc_sig == 0) {
1545 * Wait if necessary for space for a new vnode.
1548 vn_alloc_wait(int suspended)
1551 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1552 if (numvnodes >= desiredvnodes) {
1555 * The file system is being suspended. We cannot
1556 * risk a deadlock here, so allow allocation of
1557 * another vnode even if this would give too many.
1561 if (vnlruproc_sig == 0) {
1562 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1565 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1568 /* Post-adjust like the pre-adjust in getnewvnode(). */
1569 if (numvnodes + 1 > desiredvnodes && freevnodes > 1)
1570 vnlru_free_locked(1, NULL);
1571 return (numvnodes >= desiredvnodes ? ENFILE : 0);
1574 static struct vnode *
1575 vn_alloc(struct mount *mp)
1577 static int cyclecount;
1580 mtx_lock(&vnode_free_list_mtx);
1581 if (numvnodes < desiredvnodes)
1583 else if (cyclecount++ >= freevnodes) {
1588 * Grow the vnode cache if it will not be above its target max
1589 * after growing. Otherwise, if the free list is nonempty, try
1590 * to reclaim 1 item from it before growing the cache (possibly
1591 * above its target max if the reclamation failed or is delayed).
1592 * Otherwise, wait for some space. In all cases, schedule
1593 * vnlru_proc() if we are getting short of space. The watermarks
1594 * should be chosen so that we never wait or even reclaim from
1595 * the free list to below its target minimum.
1597 if (numvnodes + 1 <= desiredvnodes)
1599 else if (freevnodes > 0)
1600 vnlru_free_locked(1, NULL);
1602 error = vn_alloc_wait(mp != NULL && (mp->mnt_kern_flag &
1604 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1606 mtx_unlock(&vnode_free_list_mtx);
1612 atomic_add_long(&numvnodes, 1);
1613 mtx_unlock(&vnode_free_list_mtx);
1614 return (uma_zalloc(vnode_zone, M_WAITOK));
1618 vn_free(struct vnode *vp)
1621 atomic_subtract_long(&numvnodes, 1);
1622 uma_zfree(vnode_zone, vp);
1626 * Return the next vnode from the free list.
1629 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1634 struct lock_object *lo;
1636 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1638 KASSERT(vops->registered,
1639 ("%s: not registered vector op %p\n", __func__, vops));
1642 if (td->td_vp_reserved != NULL) {
1643 vp = td->td_vp_reserved;
1644 td->td_vp_reserved = NULL;
1648 counter_u64_add(vnodes_created, 1);
1650 * Locks are given the generic name "vnode" when created.
1651 * Follow the historic practice of using the filesystem
1652 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc.
1654 * Locks live in a witness group keyed on their name. Thus,
1655 * when a lock is renamed, it must also move from the witness
1656 * group of its old name to the witness group of its new name.
1658 * The change only needs to be made when the vnode moves
1659 * from one filesystem type to another. We ensure that each
1660 * filesystem use a single static name pointer for its tag so
1661 * that we can compare pointers rather than doing a strcmp().
1663 lo = &vp->v_vnlock->lock_object;
1664 if (lo->lo_name != tag) {
1666 WITNESS_DESTROY(lo);
1667 WITNESS_INIT(lo, tag);
1670 * By default, don't allow shared locks unless filesystems opt-in.
1672 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE;
1674 * Finalize various vnode identity bits.
1676 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp));
1677 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp));
1678 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp));
1681 v_init_counters(vp);
1682 vp->v_bufobj.bo_ops = &buf_ops_bio;
1684 if (mp == NULL && vops != &dead_vnodeops)
1685 printf("NULL mp in getnewvnode(9), tag %s\n", tag);
1689 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1690 mac_vnode_associate_singlelabel(mp, vp);
1693 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize;
1694 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1695 vp->v_vflag |= VV_NOKNOTE;
1699 * For the filesystems which do not use vfs_hash_insert(),
1700 * still initialize v_hash to have vfs_hash_index() useful.
1701 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1704 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1711 getnewvnode_reserve(void)
1716 MPASS(td->td_vp_reserved == NULL);
1717 td->td_vp_reserved = vn_alloc(NULL);
1721 getnewvnode_drop_reserve(void)
1726 if (td->td_vp_reserved != NULL) {
1727 vn_free(td->td_vp_reserved);
1728 td->td_vp_reserved = NULL;
1733 freevnode(struct vnode *vp)
1738 * The vnode has been marked for destruction, so free it.
1740 * The vnode will be returned to the zone where it will
1741 * normally remain until it is needed for another vnode. We
1742 * need to cleanup (or verify that the cleanup has already
1743 * been done) any residual data left from its current use
1744 * so as not to contaminate the freshly allocated vnode.
1746 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
1748 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
1749 ("cleaned vnode still on the free list."));
1750 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
1751 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
1752 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
1753 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
1754 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
1755 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
1756 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
1757 ("clean blk trie not empty"));
1758 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
1759 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
1760 ("dirty blk trie not empty"));
1761 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
1762 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
1763 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
1764 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp,
1765 ("Dangling rangelock waiters"));
1768 mac_vnode_destroy(vp);
1770 if (vp->v_pollinfo != NULL) {
1771 destroy_vpollinfo(vp->v_pollinfo);
1772 vp->v_pollinfo = NULL;
1775 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
1778 vp->v_mountedhere = NULL;
1781 vp->v_fifoinfo = NULL;
1782 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
1791 * Delete from old mount point vnode list, if on one.
1794 delmntque(struct vnode *vp)
1803 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1804 ("Active vnode list size %d > Vnode list size %d",
1805 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1806 if (vp->v_iflag & VI_ACTIVE) {
1807 vp->v_iflag &= ~VI_ACTIVE;
1808 mtx_lock(&mp->mnt_listmtx);
1809 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1810 mp->mnt_activevnodelistsize--;
1811 mtx_unlock(&mp->mnt_listmtx);
1813 if (vp->v_mflag & VMP_LAZYLIST) {
1814 mtx_lock(&mp->mnt_listmtx);
1815 if (vp->v_mflag & VMP_LAZYLIST) {
1816 vp->v_mflag &= ~VMP_LAZYLIST;
1817 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist);
1818 mp->mnt_lazyvnodelistsize--;
1820 mtx_unlock(&mp->mnt_listmtx);
1824 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1825 ("bad mount point vnode list size"));
1826 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1827 mp->mnt_nvnodelistsize--;
1833 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1837 vp->v_op = &dead_vnodeops;
1843 * Insert into list of vnodes for the new mount point, if available.
1846 insmntque1(struct vnode *vp, struct mount *mp,
1847 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1850 KASSERT(vp->v_mount == NULL,
1851 ("insmntque: vnode already on per mount vnode list"));
1852 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1853 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1856 * We acquire the vnode interlock early to ensure that the
1857 * vnode cannot be recycled by another process releasing a
1858 * holdcnt on it before we get it on both the vnode list
1859 * and the active vnode list. The mount mutex protects only
1860 * manipulation of the vnode list and the vnode freelist
1861 * mutex protects only manipulation of the active vnode list.
1862 * Hence the need to hold the vnode interlock throughout.
1866 if (((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 &&
1867 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1868 mp->mnt_nvnodelistsize == 0)) &&
1869 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1878 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1879 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1880 ("neg mount point vnode list size"));
1881 mp->mnt_nvnodelistsize++;
1882 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1883 ("Activating already active vnode"));
1884 vp->v_iflag |= VI_ACTIVE;
1885 mtx_lock(&mp->mnt_listmtx);
1886 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1887 mp->mnt_activevnodelistsize++;
1888 mtx_unlock(&mp->mnt_listmtx);
1895 insmntque(struct vnode *vp, struct mount *mp)
1898 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1902 * Flush out and invalidate all buffers associated with a bufobj
1903 * Called with the underlying object locked.
1906 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1911 if (flags & V_SAVE) {
1912 error = bufobj_wwait(bo, slpflag, slptimeo);
1917 if (bo->bo_dirty.bv_cnt > 0) {
1919 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1922 * XXX We could save a lock/unlock if this was only
1923 * enabled under INVARIANTS
1926 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1927 panic("vinvalbuf: dirty bufs");
1931 * If you alter this loop please notice that interlock is dropped and
1932 * reacquired in flushbuflist. Special care is needed to ensure that
1933 * no race conditions occur from this.
1936 error = flushbuflist(&bo->bo_clean,
1937 flags, bo, slpflag, slptimeo);
1938 if (error == 0 && !(flags & V_CLEANONLY))
1939 error = flushbuflist(&bo->bo_dirty,
1940 flags, bo, slpflag, slptimeo);
1941 if (error != 0 && error != EAGAIN) {
1945 } while (error != 0);
1948 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1949 * have write I/O in-progress but if there is a VM object then the
1950 * VM object can also have read-I/O in-progress.
1953 bufobj_wwait(bo, 0, 0);
1954 if ((flags & V_VMIO) == 0 && bo->bo_object != NULL) {
1956 vm_object_pip_wait_unlocked(bo->bo_object, "bovlbx");
1959 } while (bo->bo_numoutput > 0);
1963 * Destroy the copy in the VM cache, too.
1965 if (bo->bo_object != NULL &&
1966 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) {
1967 VM_OBJECT_WLOCK(bo->bo_object);
1968 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1969 OBJPR_CLEANONLY : 0);
1970 VM_OBJECT_WUNLOCK(bo->bo_object);
1975 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO |
1976 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 ||
1977 bo->bo_clean.bv_cnt > 0))
1978 panic("vinvalbuf: flush failed");
1979 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 &&
1980 bo->bo_dirty.bv_cnt > 0)
1981 panic("vinvalbuf: flush dirty failed");
1988 * Flush out and invalidate all buffers associated with a vnode.
1989 * Called with the underlying object locked.
1992 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1995 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1996 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1997 if (vp->v_object != NULL && vp->v_object->handle != vp)
1999 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
2003 * Flush out buffers on the specified list.
2007 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
2010 struct buf *bp, *nbp;
2015 ASSERT_BO_WLOCKED(bo);
2018 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
2020 * If we are flushing both V_NORMAL and V_ALT buffers then
2021 * do not skip any buffers. If we are flushing only V_NORMAL
2022 * buffers then skip buffers marked as BX_ALTDATA. If we are
2023 * flushing only V_ALT buffers then skip buffers not marked
2026 if (((flags & (V_NORMAL | V_ALT)) != (V_NORMAL | V_ALT)) &&
2027 (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA) != 0) ||
2028 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0))) {
2032 lblkno = nbp->b_lblkno;
2033 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
2036 error = BUF_TIMELOCK(bp,
2037 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
2038 "flushbuf", slpflag, slptimeo);
2041 return (error != ENOLCK ? error : EAGAIN);
2043 KASSERT(bp->b_bufobj == bo,
2044 ("bp %p wrong b_bufobj %p should be %p",
2045 bp, bp->b_bufobj, bo));
2047 * XXX Since there are no node locks for NFS, I
2048 * believe there is a slight chance that a delayed
2049 * write will occur while sleeping just above, so
2052 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
2055 bp->b_flags |= B_ASYNC;
2058 return (EAGAIN); /* XXX: why not loop ? */
2061 bp->b_flags |= (B_INVAL | B_RELBUF);
2062 bp->b_flags &= ~B_ASYNC;
2067 nbp = gbincore(bo, lblkno);
2068 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2070 break; /* nbp invalid */
2076 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn)
2082 ASSERT_BO_LOCKED(bo);
2084 for (lblkno = startn;;) {
2086 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno);
2087 if (bp == NULL || bp->b_lblkno >= endn ||
2088 bp->b_lblkno < startn)
2090 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
2091 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0);
2094 if (error == ENOLCK)
2098 KASSERT(bp->b_bufobj == bo,
2099 ("bp %p wrong b_bufobj %p should be %p",
2100 bp, bp->b_bufobj, bo));
2101 lblkno = bp->b_lblkno + 1;
2102 if ((bp->b_flags & B_MANAGED) == 0)
2104 bp->b_flags |= B_RELBUF;
2106 * In the VMIO case, use the B_NOREUSE flag to hint that the
2107 * pages backing each buffer in the range are unlikely to be
2108 * reused. Dirty buffers will have the hint applied once
2109 * they've been written.
2111 if ((bp->b_flags & B_VMIO) != 0)
2112 bp->b_flags |= B_NOREUSE;
2120 * Truncate a file's buffer and pages to a specified length. This
2121 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
2125 vtruncbuf(struct vnode *vp, off_t length, int blksize)
2127 struct buf *bp, *nbp;
2131 CTR4(KTR_VFS, "%s: vp %p with block %d:%ju", __func__,
2132 vp, blksize, (uintmax_t)length);
2135 * Round up to the *next* lbn.
2137 startlbn = howmany(length, blksize);
2139 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
2145 while (v_inval_buf_range_locked(vp, bo, startlbn, INT64_MAX) == EAGAIN)
2150 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2151 if (bp->b_lblkno > 0)
2154 * Since we hold the vnode lock this should only
2155 * fail if we're racing with the buf daemon.
2158 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2159 BO_LOCKPTR(bo)) == ENOLCK)
2160 goto restart_unlocked;
2162 VNASSERT((bp->b_flags & B_DELWRI), vp,
2163 ("buf(%p) on dirty queue without DELWRI", bp));
2172 bufobj_wwait(bo, 0, 0);
2174 vnode_pager_setsize(vp, length);
2180 * Invalidate the cached pages of a file's buffer within the range of block
2181 * numbers [startlbn, endlbn).
2184 v_inval_buf_range(struct vnode *vp, daddr_t startlbn, daddr_t endlbn,
2190 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range");
2192 start = blksize * startlbn;
2193 end = blksize * endlbn;
2197 MPASS(blksize == bo->bo_bsize);
2199 while (v_inval_buf_range_locked(vp, bo, startlbn, endlbn) == EAGAIN)
2203 vn_pages_remove(vp, OFF_TO_IDX(start), OFF_TO_IDX(end + PAGE_SIZE - 1));
2207 v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
2208 daddr_t startlbn, daddr_t endlbn)
2210 struct buf *bp, *nbp;
2213 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range_locked");
2214 ASSERT_BO_LOCKED(bo);
2218 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
2219 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2222 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2223 BO_LOCKPTR(bo)) == ENOLCK) {
2229 bp->b_flags |= B_INVAL | B_RELBUF;
2230 bp->b_flags &= ~B_ASYNC;
2236 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
2238 (nbp->b_flags & B_DELWRI) != 0))
2242 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2243 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2246 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2247 BO_LOCKPTR(bo)) == ENOLCK) {
2252 bp->b_flags |= B_INVAL | B_RELBUF;
2253 bp->b_flags &= ~B_ASYNC;
2259 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
2260 (nbp->b_vp != vp) ||
2261 (nbp->b_flags & B_DELWRI) == 0))
2269 buf_vlist_remove(struct buf *bp)
2273 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2274 ASSERT_BO_WLOCKED(bp->b_bufobj);
2275 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
2276 (BX_VNDIRTY|BX_VNCLEAN),
2277 ("buf_vlist_remove: Buf %p is on two lists", bp));
2278 if (bp->b_xflags & BX_VNDIRTY)
2279 bv = &bp->b_bufobj->bo_dirty;
2281 bv = &bp->b_bufobj->bo_clean;
2282 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
2283 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
2285 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
2289 * Add the buffer to the sorted clean or dirty block list.
2291 * NOTE: xflags is passed as a constant, optimizing this inline function!
2294 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
2300 ASSERT_BO_WLOCKED(bo);
2301 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
2302 ("dead bo %p", bo));
2303 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
2304 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
2305 bp->b_xflags |= xflags;
2306 if (xflags & BX_VNDIRTY)
2312 * Keep the list ordered. Optimize empty list insertion. Assume
2313 * we tend to grow at the tail so lookup_le should usually be cheaper
2316 if (bv->bv_cnt == 0 ||
2317 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
2318 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
2319 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
2320 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
2322 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
2323 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
2325 panic("buf_vlist_add: Preallocated nodes insufficient.");
2330 * Look up a buffer using the buffer tries.
2333 gbincore(struct bufobj *bo, daddr_t lblkno)
2337 ASSERT_BO_LOCKED(bo);
2338 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
2341 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
2345 * Associate a buffer with a vnode.
2348 bgetvp(struct vnode *vp, struct buf *bp)
2353 ASSERT_BO_WLOCKED(bo);
2354 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
2356 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
2357 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
2358 ("bgetvp: bp already attached! %p", bp));
2364 * Insert onto list for new vnode.
2366 buf_vlist_add(bp, bo, BX_VNCLEAN);
2370 * Disassociate a buffer from a vnode.
2373 brelvp(struct buf *bp)
2378 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2379 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
2382 * Delete from old vnode list, if on one.
2384 vp = bp->b_vp; /* XXX */
2387 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2388 buf_vlist_remove(bp);
2390 panic("brelvp: Buffer %p not on queue.", bp);
2391 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2392 bo->bo_flag &= ~BO_ONWORKLST;
2393 mtx_lock(&sync_mtx);
2394 LIST_REMOVE(bo, bo_synclist);
2395 syncer_worklist_len--;
2396 mtx_unlock(&sync_mtx);
2399 bp->b_bufobj = NULL;
2405 * Add an item to the syncer work queue.
2408 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
2412 ASSERT_BO_WLOCKED(bo);
2414 mtx_lock(&sync_mtx);
2415 if (bo->bo_flag & BO_ONWORKLST)
2416 LIST_REMOVE(bo, bo_synclist);
2418 bo->bo_flag |= BO_ONWORKLST;
2419 syncer_worklist_len++;
2422 if (delay > syncer_maxdelay - 2)
2423 delay = syncer_maxdelay - 2;
2424 slot = (syncer_delayno + delay) & syncer_mask;
2426 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
2427 mtx_unlock(&sync_mtx);
2431 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
2435 mtx_lock(&sync_mtx);
2436 len = syncer_worklist_len - sync_vnode_count;
2437 mtx_unlock(&sync_mtx);
2438 error = SYSCTL_OUT(req, &len, sizeof(len));
2442 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len,
2443 CTLTYPE_INT | CTLFLAG_MPSAFE| CTLFLAG_RD, NULL, 0,
2444 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
2446 static struct proc *updateproc;
2447 static void sched_sync(void);
2448 static struct kproc_desc up_kp = {
2453 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
2456 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
2461 *bo = LIST_FIRST(slp);
2465 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
2468 * We use vhold in case the vnode does not
2469 * successfully sync. vhold prevents the vnode from
2470 * going away when we unlock the sync_mtx so that
2471 * we can acquire the vnode interlock.
2474 mtx_unlock(&sync_mtx);
2476 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2478 mtx_lock(&sync_mtx);
2479 return (*bo == LIST_FIRST(slp));
2481 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2482 (void) VOP_FSYNC(vp, MNT_LAZY, td);
2484 vn_finished_write(mp);
2486 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
2488 * Put us back on the worklist. The worklist
2489 * routine will remove us from our current
2490 * position and then add us back in at a later
2493 vn_syncer_add_to_worklist(*bo, syncdelay);
2497 mtx_lock(&sync_mtx);
2501 static int first_printf = 1;
2504 * System filesystem synchronizer daemon.
2509 struct synclist *next, *slp;
2512 struct thread *td = curthread;
2514 int net_worklist_len;
2515 int syncer_final_iter;
2519 syncer_final_iter = 0;
2520 syncer_state = SYNCER_RUNNING;
2521 starttime = time_uptime;
2522 td->td_pflags |= TDP_NORUNNINGBUF;
2524 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
2527 mtx_lock(&sync_mtx);
2529 if (syncer_state == SYNCER_FINAL_DELAY &&
2530 syncer_final_iter == 0) {
2531 mtx_unlock(&sync_mtx);
2532 kproc_suspend_check(td->td_proc);
2533 mtx_lock(&sync_mtx);
2535 net_worklist_len = syncer_worklist_len - sync_vnode_count;
2536 if (syncer_state != SYNCER_RUNNING &&
2537 starttime != time_uptime) {
2539 printf("\nSyncing disks, vnodes remaining... ");
2542 printf("%d ", net_worklist_len);
2544 starttime = time_uptime;
2547 * Push files whose dirty time has expired. Be careful
2548 * of interrupt race on slp queue.
2550 * Skip over empty worklist slots when shutting down.
2553 slp = &syncer_workitem_pending[syncer_delayno];
2554 syncer_delayno += 1;
2555 if (syncer_delayno == syncer_maxdelay)
2557 next = &syncer_workitem_pending[syncer_delayno];
2559 * If the worklist has wrapped since the
2560 * it was emptied of all but syncer vnodes,
2561 * switch to the FINAL_DELAY state and run
2562 * for one more second.
2564 if (syncer_state == SYNCER_SHUTTING_DOWN &&
2565 net_worklist_len == 0 &&
2566 last_work_seen == syncer_delayno) {
2567 syncer_state = SYNCER_FINAL_DELAY;
2568 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
2570 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
2571 syncer_worklist_len > 0);
2574 * Keep track of the last time there was anything
2575 * on the worklist other than syncer vnodes.
2576 * Return to the SHUTTING_DOWN state if any
2579 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
2580 last_work_seen = syncer_delayno;
2581 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
2582 syncer_state = SYNCER_SHUTTING_DOWN;
2583 while (!LIST_EMPTY(slp)) {
2584 error = sync_vnode(slp, &bo, td);
2586 LIST_REMOVE(bo, bo_synclist);
2587 LIST_INSERT_HEAD(next, bo, bo_synclist);
2591 if (first_printf == 0) {
2593 * Drop the sync mutex, because some watchdog
2594 * drivers need to sleep while patting
2596 mtx_unlock(&sync_mtx);
2597 wdog_kern_pat(WD_LASTVAL);
2598 mtx_lock(&sync_mtx);
2602 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
2603 syncer_final_iter--;
2605 * The variable rushjob allows the kernel to speed up the
2606 * processing of the filesystem syncer process. A rushjob
2607 * value of N tells the filesystem syncer to process the next
2608 * N seconds worth of work on its queue ASAP. Currently rushjob
2609 * is used by the soft update code to speed up the filesystem
2610 * syncer process when the incore state is getting so far
2611 * ahead of the disk that the kernel memory pool is being
2612 * threatened with exhaustion.
2619 * Just sleep for a short period of time between
2620 * iterations when shutting down to allow some I/O
2623 * If it has taken us less than a second to process the
2624 * current work, then wait. Otherwise start right over
2625 * again. We can still lose time if any single round
2626 * takes more than two seconds, but it does not really
2627 * matter as we are just trying to generally pace the
2628 * filesystem activity.
2630 if (syncer_state != SYNCER_RUNNING ||
2631 time_uptime == starttime) {
2633 sched_prio(td, PPAUSE);
2636 if (syncer_state != SYNCER_RUNNING)
2637 cv_timedwait(&sync_wakeup, &sync_mtx,
2638 hz / SYNCER_SHUTDOWN_SPEEDUP);
2639 else if (time_uptime == starttime)
2640 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2645 * Request the syncer daemon to speed up its work.
2646 * We never push it to speed up more than half of its
2647 * normal turn time, otherwise it could take over the cpu.
2650 speedup_syncer(void)
2654 mtx_lock(&sync_mtx);
2655 if (rushjob < syncdelay / 2) {
2657 stat_rush_requests += 1;
2660 mtx_unlock(&sync_mtx);
2661 cv_broadcast(&sync_wakeup);
2666 * Tell the syncer to speed up its work and run though its work
2667 * list several times, then tell it to shut down.
2670 syncer_shutdown(void *arg, int howto)
2673 if (howto & RB_NOSYNC)
2675 mtx_lock(&sync_mtx);
2676 syncer_state = SYNCER_SHUTTING_DOWN;
2678 mtx_unlock(&sync_mtx);
2679 cv_broadcast(&sync_wakeup);
2680 kproc_shutdown(arg, howto);
2684 syncer_suspend(void)
2687 syncer_shutdown(updateproc, 0);
2694 mtx_lock(&sync_mtx);
2696 syncer_state = SYNCER_RUNNING;
2697 mtx_unlock(&sync_mtx);
2698 cv_broadcast(&sync_wakeup);
2699 kproc_resume(updateproc);
2703 * Reassign a buffer from one vnode to another.
2704 * Used to assign file specific control information
2705 * (indirect blocks) to the vnode to which they belong.
2708 reassignbuf(struct buf *bp)
2721 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2722 bp, bp->b_vp, bp->b_flags);
2724 * B_PAGING flagged buffers cannot be reassigned because their vp
2725 * is not fully linked in.
2727 if (bp->b_flags & B_PAGING)
2728 panic("cannot reassign paging buffer");
2731 * Delete from old vnode list, if on one.
2734 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2735 buf_vlist_remove(bp);
2737 panic("reassignbuf: Buffer %p not on queue.", bp);
2739 * If dirty, put on list of dirty buffers; otherwise insert onto list
2742 if (bp->b_flags & B_DELWRI) {
2743 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2744 switch (vp->v_type) {
2754 vn_syncer_add_to_worklist(bo, delay);
2756 buf_vlist_add(bp, bo, BX_VNDIRTY);
2758 buf_vlist_add(bp, bo, BX_VNCLEAN);
2760 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2761 mtx_lock(&sync_mtx);
2762 LIST_REMOVE(bo, bo_synclist);
2763 syncer_worklist_len--;
2764 mtx_unlock(&sync_mtx);
2765 bo->bo_flag &= ~BO_ONWORKLST;
2770 bp = TAILQ_FIRST(&bv->bv_hd);
2771 KASSERT(bp == NULL || bp->b_bufobj == bo,
2772 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2773 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2774 KASSERT(bp == NULL || bp->b_bufobj == bo,
2775 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2777 bp = TAILQ_FIRST(&bv->bv_hd);
2778 KASSERT(bp == NULL || bp->b_bufobj == bo,
2779 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2780 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2781 KASSERT(bp == NULL || bp->b_bufobj == bo,
2782 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2788 v_init_counters(struct vnode *vp)
2791 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2792 vp, ("%s called for an initialized vnode", __FUNCTION__));
2793 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2795 refcount_init(&vp->v_holdcnt, 1);
2796 refcount_init(&vp->v_usecount, 1);
2800 * Increment si_usecount of the associated device, if any.
2803 v_incr_devcount(struct vnode *vp)
2806 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2807 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2809 vp->v_rdev->si_usecount++;
2815 * Decrement si_usecount of the associated device, if any.
2818 v_decr_devcount(struct vnode *vp)
2821 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2822 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2824 vp->v_rdev->si_usecount--;
2830 * Grab a particular vnode from the free list, increment its
2831 * reference count and lock it. VIRF_DOOMED is set if the vnode
2832 * is being destroyed. Only callers who specify LK_RETRY will
2833 * see doomed vnodes. If inactive processing was delayed in
2834 * vput try to do it here.
2836 * Both holdcnt and usecount can be manipulated using atomics without holding
2837 * any locks except in these cases which require the vnode interlock:
2838 * holdcnt: 1->0 and 0->1
2841 * usecount is permitted to transition 1->0 without the interlock because
2842 * vnode is kept live by holdcnt.
2844 static enum vgetstate __always_inline
2845 _vget_prep(struct vnode *vp, bool interlock)
2849 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2862 vget_prep(struct vnode *vp)
2865 return (_vget_prep(vp, false));
2869 vget(struct vnode *vp, int flags, struct thread *td)
2873 MPASS(td == curthread);
2875 vs = _vget_prep(vp, (flags & LK_INTERLOCK) != 0);
2876 return (vget_finish(vp, flags, vs));
2880 vget_finish(struct vnode *vp, int flags, enum vgetstate vs)
2882 int error, oweinact;
2884 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2885 ("%s: invalid lock operation", __func__));
2887 if ((flags & LK_INTERLOCK) != 0)
2888 ASSERT_VI_LOCKED(vp, __func__);
2890 ASSERT_VI_UNLOCKED(vp, __func__);
2891 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__));
2892 if (vs == VGET_USECOUNT) {
2893 VNASSERT(vp->v_usecount > 0, vp,
2894 ("%s: vnode without usecount when VGET_USECOUNT was passed",
2898 if ((error = vn_lock(vp, flags)) != 0) {
2899 if (vs == VGET_USECOUNT)
2903 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2908 if (vs == VGET_USECOUNT) {
2909 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2910 ("%s: vnode with usecount and VI_OWEINACT set", __func__));
2915 * We hold the vnode. If the usecount is 0 it will be utilized to keep
2916 * the vnode around. Otherwise someone else lended their hold count and
2917 * we have to drop ours.
2919 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2921 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
2922 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old));
2924 refcount_release(&vp->v_holdcnt);
2926 VNODE_REFCOUNT_FENCE_ACQ();
2927 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2928 ("%s: vnode with usecount and VI_OWEINACT set", __func__));
2933 * We don't guarantee that any particular close will
2934 * trigger inactive processing so just make a best effort
2935 * here at preventing a reference to a removed file. If
2936 * we don't succeed no harm is done.
2938 * Upgrade our holdcnt to a usecount.
2942 * See the previous section. By the time we get here we may find
2943 * ourselves in the same spot.
2945 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2947 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
2948 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old));
2950 refcount_release(&vp->v_holdcnt);
2952 VNODE_REFCOUNT_FENCE_ACQ();
2953 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2954 ("%s: vnode with usecount and VI_OWEINACT set",
2959 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2963 vp->v_iflag &= ~VI_OWEINACT;
2964 VNODE_REFCOUNT_FENCE_REL();
2966 v_incr_devcount(vp);
2967 refcount_acquire(&vp->v_usecount);
2968 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2969 (flags & LK_NOWAIT) == 0)
2976 * Increase the reference (use) and hold count of a vnode.
2977 * This will also remove the vnode from the free list if it is presently free.
2980 vref(struct vnode *vp)
2983 ASSERT_VI_UNLOCKED(vp, __func__);
2984 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2985 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2986 VNODE_REFCOUNT_FENCE_ACQ();
2987 VNASSERT(vp->v_holdcnt > 0, vp,
2988 ("%s: active vnode not held", __func__));
2989 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2990 ("%s: vnode with usecount and VI_OWEINACT set", __func__));
2999 vrefl(struct vnode *vp)
3002 ASSERT_VI_LOCKED(vp, __func__);
3003 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3004 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
3005 VNODE_REFCOUNT_FENCE_ACQ();
3006 VNASSERT(vp->v_holdcnt > 0, vp,
3007 ("%s: active vnode not held", __func__));
3008 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
3009 ("%s: vnode with usecount and VI_OWEINACT set", __func__));
3013 if ((vp->v_iflag & VI_OWEINACT) != 0) {
3014 vp->v_iflag &= ~VI_OWEINACT;
3015 VNODE_REFCOUNT_FENCE_REL();
3017 v_incr_devcount(vp);
3018 refcount_acquire(&vp->v_usecount);
3022 vrefact(struct vnode *vp)
3025 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3027 int old = atomic_fetchadd_int(&vp->v_usecount, 1);
3028 VNASSERT(old > 0, vp, ("%s: wrong use count %d", __func__, old));
3030 refcount_acquire(&vp->v_usecount);
3035 * Return reference count of a vnode.
3037 * The results of this call are only guaranteed when some mechanism is used to
3038 * stop other processes from gaining references to the vnode. This may be the
3039 * case if the caller holds the only reference. This is also useful when stale
3040 * data is acceptable as race conditions may be accounted for by some other
3044 vrefcnt(struct vnode *vp)
3047 return (vp->v_usecount);
3051 vlazy(struct vnode *vp)
3055 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__));
3057 if ((vp->v_mflag & VMP_LAZYLIST) != 0)
3060 mtx_lock(&mp->mnt_listmtx);
3061 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
3062 vp->v_mflag |= VMP_LAZYLIST;
3063 TAILQ_INSERT_TAIL(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3064 mp->mnt_lazyvnodelistsize++;
3066 mtx_unlock(&mp->mnt_listmtx);
3070 vdefer_inactive(struct vnode *vp)
3073 ASSERT_VI_LOCKED(vp, __func__);
3074 VNASSERT(vp->v_iflag & VI_OWEINACT, vp,
3075 ("%s: vnode without VI_OWEINACT", __func__));
3076 if (VN_IS_DOOMED(vp)) {
3080 if (vp->v_iflag & VI_DEFINACT) {
3081 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count"));
3086 vp->v_iflag |= VI_DEFINACT;
3088 counter_u64_add(deferred_inact, 1);
3092 vdefer_inactive_cond(struct vnode *vp)
3096 VNASSERT(vp->v_holdcnt > 0, vp, ("vnode without hold count"));
3097 if ((vp->v_iflag & VI_OWEINACT) == 0) {
3101 vdefer_inactive(vp);
3104 enum vputx_op { VPUTX_VRELE, VPUTX_VPUT, VPUTX_VUNREF };
3107 * Decrement the use and hold counts for a vnode.
3109 * See an explanation near vget() as to why atomic operation is safe.
3112 vputx(struct vnode *vp, enum vputx_op func)
3116 KASSERT(vp != NULL, ("vputx: null vp"));
3117 if (func == VPUTX_VUNREF)
3118 ASSERT_VOP_LOCKED(vp, "vunref");
3119 ASSERT_VI_UNLOCKED(vp, __func__);
3120 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp,
3121 ("%s: wrong ref counts", __func__));
3123 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3126 * We want to hold the vnode until the inactive finishes to
3127 * prevent vgone() races. We drop the use count here and the
3128 * hold count below when we're done.
3130 * If we release the last usecount we take ownership of the hold
3131 * count which provides liveness of the vnode, in which case we
3134 if (!refcount_release(&vp->v_usecount))
3137 v_decr_devcount(vp);
3139 * By the time we got here someone else might have transitioned
3140 * the count back to > 0.
3142 if (vp->v_usecount > 0) {
3146 if (vp->v_iflag & VI_DOINGINACT) {
3152 * Check if the fs wants to perform inactive processing. Note we
3153 * may be only holding the interlock, in which case it is possible
3154 * someone else called vgone on the vnode and ->v_data is now NULL.
3155 * Since vgone performs inactive on its own there is nothing to do
3156 * here but to drop our hold count.
3158 if (__predict_false(VN_IS_DOOMED(vp)) ||
3159 VOP_NEED_INACTIVE(vp) == 0) {
3165 * We must call VOP_INACTIVE with the node locked. Mark
3166 * as VI_DOINGINACT to avoid recursion.
3168 vp->v_iflag |= VI_OWEINACT;
3171 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
3175 error = VOP_LOCK(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT);
3180 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
3181 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
3186 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp,
3187 ("vnode with usecount and VI_OWEINACT set"));
3189 if (vp->v_iflag & VI_OWEINACT)
3191 if (func != VPUTX_VUNREF)
3194 } else if (vp->v_iflag & VI_OWEINACT) {
3195 vdefer_inactive(vp);
3202 * Vnode put/release.
3203 * If count drops to zero, call inactive routine and return to freelist.
3206 vrele(struct vnode *vp)
3209 vputx(vp, VPUTX_VRELE);
3213 * Release an already locked vnode. This give the same effects as
3214 * unlock+vrele(), but takes less time and avoids releasing and
3215 * re-aquiring the lock (as vrele() acquires the lock internally.)
3217 * It is an invariant that all VOP_* calls operate on a held vnode.
3218 * We may be only having an implicit hold stemming from our usecount,
3219 * which we are about to release. If we unlock the vnode afterwards we
3220 * open a time window where someone else dropped the last usecount and
3221 * proceeded to free the vnode before our unlock finished. For this
3222 * reason we unlock the vnode early. This is a little bit wasteful as
3223 * it may be the vnode is exclusively locked and inactive processing is
3224 * needed, in which case we are adding work.
3227 vput(struct vnode *vp)
3231 vputx(vp, VPUTX_VPUT);
3235 * Release an exclusively locked vnode. Do not unlock the vnode lock.
3238 vunref(struct vnode *vp)
3241 vputx(vp, VPUTX_VUNREF);
3245 * Increase the hold count and activate if this is the first reference.
3248 vhold_activate(struct vnode *vp)
3252 ASSERT_VI_LOCKED(vp, __func__);
3253 VNASSERT(vp->v_holdcnt == 0, vp,
3254 ("%s: wrong hold count", __func__));
3255 VNASSERT(vp->v_op != NULL, vp,
3256 ("%s: vnode already reclaimed.", __func__));
3258 * Remove a vnode from the free list, mark it as in use,
3259 * and put it on the active list.
3261 VNASSERT(vp->v_mount != NULL, vp,
3262 ("_vhold: vnode not on per mount vnode list"));
3264 mtx_lock(&mp->mnt_listmtx);
3265 if ((vp->v_mflag & VMP_TMPMNTFREELIST) != 0) {
3266 TAILQ_REMOVE(&mp->mnt_tmpfreevnodelist, vp, v_actfreelist);
3267 mp->mnt_tmpfreevnodelistsize--;
3268 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
3270 mtx_lock(&vnode_free_list_mtx);
3271 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
3273 mtx_unlock(&vnode_free_list_mtx);
3275 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
3276 ("Activating already active vnode"));
3277 vp->v_iflag &= ~VI_FREE;
3278 vp->v_iflag |= VI_ACTIVE;
3279 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
3280 mp->mnt_activevnodelistsize++;
3281 mtx_unlock(&mp->mnt_listmtx);
3282 refcount_acquire(&vp->v_holdcnt);
3286 vhold(struct vnode *vp)
3289 ASSERT_VI_UNLOCKED(vp, __func__);
3290 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3291 if (refcount_acquire_if_not_zero(&vp->v_holdcnt)) {
3292 VNODE_REFCOUNT_FENCE_ACQ();
3293 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
3294 ("vhold: vnode with holdcnt is free"));
3303 vholdl(struct vnode *vp)
3306 ASSERT_VI_LOCKED(vp, __func__);
3307 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3308 if ((vp->v_iflag & VI_FREE) == 0) {
3309 refcount_acquire(&vp->v_holdcnt);
3316 vholdnz(struct vnode *vp)
3319 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3321 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3322 VNASSERT(old > 0, vp, ("%s: wrong hold count %d", __func__, old));
3324 atomic_add_int(&vp->v_holdcnt, 1);
3329 * Drop the hold count of the vnode. If this is the last reference to
3330 * the vnode we place it on the free list unless it has been vgone'd
3331 * (marked VIRF_DOOMED) in which case we will free it.
3333 * Because the vnode vm object keeps a hold reference on the vnode if
3334 * there is at least one resident non-cached page, the vnode cannot
3335 * leave the active list without the page cleanup done.
3338 vdrop_deactivate(struct vnode *vp)
3342 ASSERT_VI_LOCKED(vp, __func__);
3344 * Mark a vnode as free: remove it from its active list
3345 * and put it up for recycling on the freelist.
3347 VNASSERT(!VN_IS_DOOMED(vp), vp,
3348 ("vdrop: returning doomed vnode"));
3349 VNASSERT(vp->v_op != NULL, vp,
3350 ("vdrop: vnode already reclaimed."));
3351 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
3352 ("vnode already free"));
3353 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
3354 ("vnode with VI_OWEINACT set"));
3355 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp,
3356 ("vnode with VI_DEFINACT set"));
3357 VNASSERT(vp->v_holdcnt == 0, vp,
3358 ("vdrop: freeing when we shouldn't"));
3360 mtx_lock(&mp->mnt_listmtx);
3361 if (vp->v_mflag & VMP_LAZYLIST) {
3362 vp->v_mflag &= ~VMP_LAZYLIST;
3363 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3364 mp->mnt_lazyvnodelistsize--;
3366 if (vp->v_iflag & VI_ACTIVE) {
3367 vp->v_iflag &= ~VI_ACTIVE;
3368 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
3369 mp->mnt_activevnodelistsize--;
3371 TAILQ_INSERT_TAIL(&mp->mnt_tmpfreevnodelist, vp, v_actfreelist);
3372 mp->mnt_tmpfreevnodelistsize++;
3373 vp->v_iflag |= VI_FREE;
3374 vp->v_mflag |= VMP_TMPMNTFREELIST;
3376 if (mp->mnt_tmpfreevnodelistsize >= mnt_free_list_batch)
3377 vnlru_return_batch_locked(mp);
3378 mtx_unlock(&mp->mnt_listmtx);
3382 vdrop(struct vnode *vp)
3385 ASSERT_VI_UNLOCKED(vp, __func__);
3386 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3387 if (refcount_release_if_not_last(&vp->v_holdcnt))
3394 vdropl(struct vnode *vp)
3397 ASSERT_VI_LOCKED(vp, __func__);
3398 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3399 if (!refcount_release(&vp->v_holdcnt)) {
3403 if (VN_IS_DOOMED(vp)) {
3407 vdrop_deactivate(vp);
3411 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
3412 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
3413 * OWEINACT tracks whether a vnode missed a call to inactive due to a
3414 * failed lock upgrade.
3417 vinactive(struct vnode *vp)
3419 struct vm_object *obj;
3421 ASSERT_VOP_ELOCKED(vp, "vinactive");
3422 ASSERT_VI_LOCKED(vp, "vinactive");
3423 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
3424 ("vinactive: recursed on VI_DOINGINACT"));
3425 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3426 vp->v_iflag |= VI_DOINGINACT;
3427 vp->v_iflag &= ~VI_OWEINACT;
3430 * Before moving off the active list, we must be sure that any
3431 * modified pages are converted into the vnode's dirty
3432 * buffers, since these will no longer be checked once the
3433 * vnode is on the inactive list.
3435 * The write-out of the dirty pages is asynchronous. At the
3436 * point that VOP_INACTIVE() is called, there could still be
3437 * pending I/O and dirty pages in the object.
3439 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
3440 vm_object_mightbedirty(obj)) {
3441 VM_OBJECT_WLOCK(obj);
3442 vm_object_page_clean(obj, 0, 0, 0);
3443 VM_OBJECT_WUNLOCK(obj);
3445 VOP_INACTIVE(vp, curthread);
3447 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
3448 ("vinactive: lost VI_DOINGINACT"));
3449 vp->v_iflag &= ~VI_DOINGINACT;
3453 * Remove any vnodes in the vnode table belonging to mount point mp.
3455 * If FORCECLOSE is not specified, there should not be any active ones,
3456 * return error if any are found (nb: this is a user error, not a
3457 * system error). If FORCECLOSE is specified, detach any active vnodes
3460 * If WRITECLOSE is set, only flush out regular file vnodes open for
3463 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
3465 * `rootrefs' specifies the base reference count for the root vnode
3466 * of this filesystem. The root vnode is considered busy if its
3467 * v_usecount exceeds this value. On a successful return, vflush(, td)
3468 * will call vrele() on the root vnode exactly rootrefs times.
3469 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
3473 static int busyprt = 0; /* print out busy vnodes */
3474 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
3478 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
3480 struct vnode *vp, *mvp, *rootvp = NULL;
3482 int busy = 0, error;
3484 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
3487 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
3488 ("vflush: bad args"));
3490 * Get the filesystem root vnode. We can vput() it
3491 * immediately, since with rootrefs > 0, it won't go away.
3493 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
3494 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
3501 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
3503 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
3506 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3510 * Skip over a vnodes marked VV_SYSTEM.
3512 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
3518 * If WRITECLOSE is set, flush out unlinked but still open
3519 * files (even if open only for reading) and regular file
3520 * vnodes open for writing.
3522 if (flags & WRITECLOSE) {
3523 if (vp->v_object != NULL) {
3524 VM_OBJECT_WLOCK(vp->v_object);
3525 vm_object_page_clean(vp->v_object, 0, 0, 0);
3526 VM_OBJECT_WUNLOCK(vp->v_object);
3528 error = VOP_FSYNC(vp, MNT_WAIT, td);
3532 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3535 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3538 if ((vp->v_type == VNON ||
3539 (error == 0 && vattr.va_nlink > 0)) &&
3540 (vp->v_writecount <= 0 || vp->v_type != VREG)) {
3548 * With v_usecount == 0, all we need to do is clear out the
3549 * vnode data structures and we are done.
3551 * If FORCECLOSE is set, forcibly close the vnode.
3553 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
3559 vn_printf(vp, "vflush: busy vnode ");
3565 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
3567 * If just the root vnode is busy, and if its refcount
3568 * is equal to `rootrefs', then go ahead and kill it.
3571 KASSERT(busy > 0, ("vflush: not busy"));
3572 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
3573 ("vflush: usecount %d < rootrefs %d",
3574 rootvp->v_usecount, rootrefs));
3575 if (busy == 1 && rootvp->v_usecount == rootrefs) {
3576 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
3584 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
3588 for (; rootrefs > 0; rootrefs--)
3594 * Recycle an unused vnode to the front of the free list.
3597 vrecycle(struct vnode *vp)
3602 recycled = vrecyclel(vp);
3608 * vrecycle, with the vp interlock held.
3611 vrecyclel(struct vnode *vp)
3615 ASSERT_VOP_ELOCKED(vp, __func__);
3616 ASSERT_VI_LOCKED(vp, __func__);
3617 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3619 if (vp->v_usecount == 0) {
3627 * Eliminate all activity associated with a vnode
3628 * in preparation for reuse.
3631 vgone(struct vnode *vp)
3639 notify_lowervp_vfs_dummy(struct mount *mp __unused,
3640 struct vnode *lowervp __unused)
3645 * Notify upper mounts about reclaimed or unlinked vnode.
3648 vfs_notify_upper(struct vnode *vp, int event)
3650 static struct vfsops vgonel_vfsops = {
3651 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
3652 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
3654 struct mount *mp, *ump, *mmp;
3659 if (TAILQ_EMPTY(&mp->mnt_uppers))
3662 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
3663 mmp->mnt_op = &vgonel_vfsops;
3664 mmp->mnt_kern_flag |= MNTK_MARKER;
3666 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
3667 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
3668 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
3669 ump = TAILQ_NEXT(ump, mnt_upper_link);
3672 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
3675 case VFS_NOTIFY_UPPER_RECLAIM:
3676 VFS_RECLAIM_LOWERVP(ump, vp);
3678 case VFS_NOTIFY_UPPER_UNLINK:
3679 VFS_UNLINK_LOWERVP(ump, vp);
3682 KASSERT(0, ("invalid event %d", event));
3686 ump = TAILQ_NEXT(mmp, mnt_upper_link);
3687 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
3690 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
3691 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
3692 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
3693 wakeup(&mp->mnt_uppers);
3699 * vgone, with the vp interlock held.
3702 vgonel(struct vnode *vp)
3707 bool active, oweinact;
3709 ASSERT_VOP_ELOCKED(vp, "vgonel");
3710 ASSERT_VI_LOCKED(vp, "vgonel");
3711 VNASSERT(vp->v_holdcnt, vp,
3712 ("vgonel: vp %p has no reference.", vp));
3713 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3717 * Don't vgonel if we're already doomed.
3719 if (vp->v_irflag & VIRF_DOOMED)
3721 vp->v_irflag |= VIRF_DOOMED;
3724 * Check to see if the vnode is in use. If so, we have to call
3725 * VOP_CLOSE() and VOP_INACTIVE().
3727 active = vp->v_usecount > 0;
3728 oweinact = (vp->v_iflag & VI_OWEINACT) != 0;
3730 * If we need to do inactive VI_OWEINACT will be set.
3732 if (vp->v_iflag & VI_DEFINACT) {
3733 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count"));
3734 vp->v_iflag &= ~VI_DEFINACT;
3737 VNASSERT(vp->v_holdcnt > 0, vp, ("vnode without hold count"));
3740 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
3743 * If purging an active vnode, it must be closed and
3744 * deactivated before being reclaimed.
3747 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
3748 if (oweinact || active) {
3750 if ((vp->v_iflag & VI_DOINGINACT) == 0)
3754 if (vp->v_type == VSOCK)
3755 vfs_unp_reclaim(vp);
3758 * Clean out any buffers associated with the vnode.
3759 * If the flush fails, just toss the buffers.
3762 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
3763 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
3764 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
3765 while (vinvalbuf(vp, 0, 0, 0) != 0)
3769 BO_LOCK(&vp->v_bufobj);
3770 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
3771 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3772 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
3773 vp->v_bufobj.bo_clean.bv_cnt == 0,
3774 ("vp %p bufobj not invalidated", vp));
3777 * For VMIO bufobj, BO_DEAD is set later, or in
3778 * vm_object_terminate() after the object's page queue is
3781 object = vp->v_bufobj.bo_object;
3783 vp->v_bufobj.bo_flag |= BO_DEAD;
3784 BO_UNLOCK(&vp->v_bufobj);
3787 * Handle the VM part. Tmpfs handles v_object on its own (the
3788 * OBJT_VNODE check). Nullfs or other bypassing filesystems
3789 * should not touch the object borrowed from the lower vnode
3790 * (the handle check).
3792 if (object != NULL && object->type == OBJT_VNODE &&
3793 object->handle == vp)
3794 vnode_destroy_vobject(vp);
3797 * Reclaim the vnode.
3799 if (VOP_RECLAIM(vp, td))
3800 panic("vgone: cannot reclaim");
3802 vn_finished_secondary_write(mp);
3803 VNASSERT(vp->v_object == NULL, vp,
3804 ("vop_reclaim left v_object vp=%p", vp));
3806 * Clear the advisory locks and wake up waiting threads.
3808 (void)VOP_ADVLOCKPURGE(vp);
3811 * Delete from old mount point vnode list.
3816 * Done with purge, reset to the standard lock and invalidate
3820 vp->v_vnlock = &vp->v_lock;
3821 vp->v_op = &dead_vnodeops;
3826 * Calculate the total number of references to a special device.
3829 vcount(struct vnode *vp)
3834 count = vp->v_rdev->si_usecount;
3840 * Print out a description of a vnode.
3842 static char *typename[] =
3843 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
3847 vn_printf(struct vnode *vp, const char *fmt, ...)
3850 char buf[256], buf2[16];
3856 printf("%p: ", (void *)vp);
3857 printf("type %s\n", typename[vp->v_type]);
3858 printf(" usecount %d, writecount %d, refcount %d",
3859 vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
3860 switch (vp->v_type) {
3862 printf(" mountedhere %p\n", vp->v_mountedhere);
3865 printf(" rdev %p\n", vp->v_rdev);
3868 printf(" socket %p\n", vp->v_unpcb);
3871 printf(" fifoinfo %p\n", vp->v_fifoinfo);
3879 if (vp->v_irflag & VIRF_DOOMED)
3880 strlcat(buf, "|VIRF_DOOMED", sizeof(buf));
3881 flags = vp->v_irflag & ~(VIRF_DOOMED);
3883 snprintf(buf2, sizeof(buf2), "|VIRF(0x%lx)", flags);
3884 strlcat(buf, buf2, sizeof(buf));
3886 if (vp->v_vflag & VV_ROOT)
3887 strlcat(buf, "|VV_ROOT", sizeof(buf));
3888 if (vp->v_vflag & VV_ISTTY)
3889 strlcat(buf, "|VV_ISTTY", sizeof(buf));
3890 if (vp->v_vflag & VV_NOSYNC)
3891 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
3892 if (vp->v_vflag & VV_ETERNALDEV)
3893 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
3894 if (vp->v_vflag & VV_CACHEDLABEL)
3895 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
3896 if (vp->v_vflag & VV_VMSIZEVNLOCK)
3897 strlcat(buf, "|VV_VMSIZEVNLOCK", sizeof(buf));
3898 if (vp->v_vflag & VV_COPYONWRITE)
3899 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
3900 if (vp->v_vflag & VV_SYSTEM)
3901 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
3902 if (vp->v_vflag & VV_PROCDEP)
3903 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
3904 if (vp->v_vflag & VV_NOKNOTE)
3905 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
3906 if (vp->v_vflag & VV_DELETED)
3907 strlcat(buf, "|VV_DELETED", sizeof(buf));
3908 if (vp->v_vflag & VV_MD)
3909 strlcat(buf, "|VV_MD", sizeof(buf));
3910 if (vp->v_vflag & VV_FORCEINSMQ)
3911 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
3912 if (vp->v_vflag & VV_READLINK)
3913 strlcat(buf, "|VV_READLINK", sizeof(buf));
3914 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
3915 VV_CACHEDLABEL | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
3916 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
3918 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
3919 strlcat(buf, buf2, sizeof(buf));
3921 if (vp->v_iflag & VI_TEXT_REF)
3922 strlcat(buf, "|VI_TEXT_REF", sizeof(buf));
3923 if (vp->v_iflag & VI_MOUNT)
3924 strlcat(buf, "|VI_MOUNT", sizeof(buf));
3925 if (vp->v_iflag & VI_FREE)
3926 strlcat(buf, "|VI_FREE", sizeof(buf));
3927 if (vp->v_iflag & VI_ACTIVE)
3928 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3929 if (vp->v_iflag & VI_DOINGINACT)
3930 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3931 if (vp->v_iflag & VI_OWEINACT)
3932 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3933 if (vp->v_iflag & VI_DEFINACT)
3934 strlcat(buf, "|VI_DEFINACT", sizeof(buf));
3935 flags = vp->v_iflag & ~(VI_TEXT_REF | VI_MOUNT | VI_FREE | VI_ACTIVE |
3936 VI_DOINGINACT | VI_OWEINACT | VI_DEFINACT);
3938 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3939 strlcat(buf, buf2, sizeof(buf));
3941 if (vp->v_mflag & VMP_TMPMNTFREELIST)
3942 strlcat(buf, "|VMP_TMPMNTFREELIST", sizeof(buf));
3943 if (vp->v_mflag & VMP_LAZYLIST)
3944 strlcat(buf, "|VMP_LAZYLIST", sizeof(buf));
3945 flags = vp->v_mflag & ~(VMP_TMPMNTFREELIST | VMP_LAZYLIST);
3947 snprintf(buf2, sizeof(buf2), "|VMP(0x%lx)", flags);
3948 strlcat(buf, buf2, sizeof(buf));
3950 printf(" flags (%s)\n", buf + 1);
3951 if (mtx_owned(VI_MTX(vp)))
3952 printf(" VI_LOCKed");
3953 if (vp->v_object != NULL)
3954 printf(" v_object %p ref %d pages %d "
3955 "cleanbuf %d dirtybuf %d\n",
3956 vp->v_object, vp->v_object->ref_count,
3957 vp->v_object->resident_page_count,
3958 vp->v_bufobj.bo_clean.bv_cnt,
3959 vp->v_bufobj.bo_dirty.bv_cnt);
3961 lockmgr_printinfo(vp->v_vnlock);
3962 if (vp->v_data != NULL)
3968 * List all of the locked vnodes in the system.
3969 * Called when debugging the kernel.
3971 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3977 * Note: because this is DDB, we can't obey the locking semantics
3978 * for these structures, which means we could catch an inconsistent
3979 * state and dereference a nasty pointer. Not much to be done
3982 db_printf("Locked vnodes\n");
3983 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3984 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3985 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3986 vn_printf(vp, "vnode ");
3992 * Show details about the given vnode.
3994 DB_SHOW_COMMAND(vnode, db_show_vnode)
4000 vp = (struct vnode *)addr;
4001 vn_printf(vp, "vnode ");
4005 * Show details about the given mount point.
4007 DB_SHOW_COMMAND(mount, db_show_mount)
4018 /* No address given, print short info about all mount points. */
4019 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4020 db_printf("%p %s on %s (%s)\n", mp,
4021 mp->mnt_stat.f_mntfromname,
4022 mp->mnt_stat.f_mntonname,
4023 mp->mnt_stat.f_fstypename);
4027 db_printf("\nMore info: show mount <addr>\n");
4031 mp = (struct mount *)addr;
4032 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
4033 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
4036 mflags = mp->mnt_flag;
4037 #define MNT_FLAG(flag) do { \
4038 if (mflags & (flag)) { \
4039 if (buf[0] != '\0') \
4040 strlcat(buf, ", ", sizeof(buf)); \
4041 strlcat(buf, (#flag) + 4, sizeof(buf)); \
4042 mflags &= ~(flag); \
4045 MNT_FLAG(MNT_RDONLY);
4046 MNT_FLAG(MNT_SYNCHRONOUS);
4047 MNT_FLAG(MNT_NOEXEC);
4048 MNT_FLAG(MNT_NOSUID);
4049 MNT_FLAG(MNT_NFS4ACLS);
4050 MNT_FLAG(MNT_UNION);
4051 MNT_FLAG(MNT_ASYNC);
4052 MNT_FLAG(MNT_SUIDDIR);
4053 MNT_FLAG(MNT_SOFTDEP);
4054 MNT_FLAG(MNT_NOSYMFOLLOW);
4055 MNT_FLAG(MNT_GJOURNAL);
4056 MNT_FLAG(MNT_MULTILABEL);
4058 MNT_FLAG(MNT_NOATIME);
4059 MNT_FLAG(MNT_NOCLUSTERR);
4060 MNT_FLAG(MNT_NOCLUSTERW);
4062 MNT_FLAG(MNT_EXRDONLY);
4063 MNT_FLAG(MNT_EXPORTED);
4064 MNT_FLAG(MNT_DEFEXPORTED);
4065 MNT_FLAG(MNT_EXPORTANON);
4066 MNT_FLAG(MNT_EXKERB);
4067 MNT_FLAG(MNT_EXPUBLIC);
4068 MNT_FLAG(MNT_LOCAL);
4069 MNT_FLAG(MNT_QUOTA);
4070 MNT_FLAG(MNT_ROOTFS);
4072 MNT_FLAG(MNT_IGNORE);
4073 MNT_FLAG(MNT_UPDATE);
4074 MNT_FLAG(MNT_DELEXPORT);
4075 MNT_FLAG(MNT_RELOAD);
4076 MNT_FLAG(MNT_FORCE);
4077 MNT_FLAG(MNT_SNAPSHOT);
4078 MNT_FLAG(MNT_BYFSID);
4082 strlcat(buf, ", ", sizeof(buf));
4083 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
4084 "0x%016jx", mflags);
4086 db_printf(" mnt_flag = %s\n", buf);
4089 flags = mp->mnt_kern_flag;
4090 #define MNT_KERN_FLAG(flag) do { \
4091 if (flags & (flag)) { \
4092 if (buf[0] != '\0') \
4093 strlcat(buf, ", ", sizeof(buf)); \
4094 strlcat(buf, (#flag) + 5, sizeof(buf)); \
4098 MNT_KERN_FLAG(MNTK_UNMOUNTF);
4099 MNT_KERN_FLAG(MNTK_ASYNC);
4100 MNT_KERN_FLAG(MNTK_SOFTDEP);
4101 MNT_KERN_FLAG(MNTK_DRAINING);
4102 MNT_KERN_FLAG(MNTK_REFEXPIRE);
4103 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
4104 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
4105 MNT_KERN_FLAG(MNTK_NO_IOPF);
4106 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
4107 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
4108 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
4109 MNT_KERN_FLAG(MNTK_MARKER);
4110 MNT_KERN_FLAG(MNTK_USES_BCACHE);
4111 MNT_KERN_FLAG(MNTK_NOASYNC);
4112 MNT_KERN_FLAG(MNTK_UNMOUNT);
4113 MNT_KERN_FLAG(MNTK_MWAIT);
4114 MNT_KERN_FLAG(MNTK_SUSPEND);
4115 MNT_KERN_FLAG(MNTK_SUSPEND2);
4116 MNT_KERN_FLAG(MNTK_SUSPENDED);
4117 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
4118 MNT_KERN_FLAG(MNTK_NOKNOTE);
4119 #undef MNT_KERN_FLAG
4122 strlcat(buf, ", ", sizeof(buf));
4123 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
4126 db_printf(" mnt_kern_flag = %s\n", buf);
4128 db_printf(" mnt_opt = ");
4129 opt = TAILQ_FIRST(mp->mnt_opt);
4131 db_printf("%s", opt->name);
4132 opt = TAILQ_NEXT(opt, link);
4133 while (opt != NULL) {
4134 db_printf(", %s", opt->name);
4135 opt = TAILQ_NEXT(opt, link);
4141 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
4142 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
4143 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
4144 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
4145 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
4146 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
4147 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
4148 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
4149 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
4150 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
4151 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
4152 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
4154 db_printf(" mnt_cred = { uid=%u ruid=%u",
4155 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
4156 if (jailed(mp->mnt_cred))
4157 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
4159 db_printf(" mnt_ref = %d (with %d in the struct)\n",
4160 vfs_mount_fetch_counter(mp, MNT_COUNT_REF), mp->mnt_ref);
4161 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
4162 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
4163 db_printf(" mnt_activevnodelistsize = %d\n",
4164 mp->mnt_activevnodelistsize);
4165 db_printf(" mnt_lazyvnodelistsize = %d\n",
4166 mp->mnt_lazyvnodelistsize);
4167 db_printf(" mnt_writeopcount = %d (with %d in the struct)\n",
4168 vfs_mount_fetch_counter(mp, MNT_COUNT_WRITEOPCOUNT), mp->mnt_writeopcount);
4169 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
4170 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
4171 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
4172 db_printf(" mnt_lockref = %d (with %d in the struct)\n",
4173 vfs_mount_fetch_counter(mp, MNT_COUNT_LOCKREF), mp->mnt_lockref);
4174 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
4175 db_printf(" mnt_secondary_accwrites = %d\n",
4176 mp->mnt_secondary_accwrites);
4177 db_printf(" mnt_gjprovider = %s\n",
4178 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
4179 db_printf(" mnt_vfs_ops = %d\n", mp->mnt_vfs_ops);
4181 db_printf("\n\nList of active vnodes\n");
4182 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
4183 if (vp->v_type != VMARKER) {
4184 vn_printf(vp, "vnode ");
4189 db_printf("\n\nList of inactive vnodes\n");
4190 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4191 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
4192 vn_printf(vp, "vnode ");
4201 * Fill in a struct xvfsconf based on a struct vfsconf.
4204 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
4206 struct xvfsconf xvfsp;
4208 bzero(&xvfsp, sizeof(xvfsp));
4209 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4210 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4211 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4212 xvfsp.vfc_flags = vfsp->vfc_flags;
4214 * These are unused in userland, we keep them
4215 * to not break binary compatibility.
4217 xvfsp.vfc_vfsops = NULL;
4218 xvfsp.vfc_next = NULL;
4219 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4222 #ifdef COMPAT_FREEBSD32
4224 uint32_t vfc_vfsops;
4225 char vfc_name[MFSNAMELEN];
4226 int32_t vfc_typenum;
4227 int32_t vfc_refcount;
4233 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
4235 struct xvfsconf32 xvfsp;
4237 bzero(&xvfsp, sizeof(xvfsp));
4238 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4239 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4240 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4241 xvfsp.vfc_flags = vfsp->vfc_flags;
4242 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4247 * Top level filesystem related information gathering.
4250 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
4252 struct vfsconf *vfsp;
4257 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4258 #ifdef COMPAT_FREEBSD32
4259 if (req->flags & SCTL_MASK32)
4260 error = vfsconf2x32(req, vfsp);
4263 error = vfsconf2x(req, vfsp);
4271 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
4272 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
4273 "S,xvfsconf", "List of all configured filesystems");
4275 #ifndef BURN_BRIDGES
4276 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
4279 vfs_sysctl(SYSCTL_HANDLER_ARGS)
4281 int *name = (int *)arg1 - 1; /* XXX */
4282 u_int namelen = arg2 + 1; /* XXX */
4283 struct vfsconf *vfsp;
4285 log(LOG_WARNING, "userland calling deprecated sysctl, "
4286 "please rebuild world\n");
4288 #if 1 || defined(COMPAT_PRELITE2)
4289 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
4291 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
4295 case VFS_MAXTYPENUM:
4298 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
4301 return (ENOTDIR); /* overloaded */
4303 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4304 if (vfsp->vfc_typenum == name[2])
4309 return (EOPNOTSUPP);
4310 #ifdef COMPAT_FREEBSD32
4311 if (req->flags & SCTL_MASK32)
4312 return (vfsconf2x32(req, vfsp));
4315 return (vfsconf2x(req, vfsp));
4317 return (EOPNOTSUPP);
4320 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
4321 CTLFLAG_MPSAFE, vfs_sysctl,
4322 "Generic filesystem");
4324 #if 1 || defined(COMPAT_PRELITE2)
4327 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
4330 struct vfsconf *vfsp;
4331 struct ovfsconf ovfs;
4334 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4335 bzero(&ovfs, sizeof(ovfs));
4336 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
4337 strcpy(ovfs.vfc_name, vfsp->vfc_name);
4338 ovfs.vfc_index = vfsp->vfc_typenum;
4339 ovfs.vfc_refcount = vfsp->vfc_refcount;
4340 ovfs.vfc_flags = vfsp->vfc_flags;
4341 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
4351 #endif /* 1 || COMPAT_PRELITE2 */
4352 #endif /* !BURN_BRIDGES */
4354 #define KINFO_VNODESLOP 10
4357 * Dump vnode list (via sysctl).
4361 sysctl_vnode(SYSCTL_HANDLER_ARGS)
4369 * Stale numvnodes access is not fatal here.
4372 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
4374 /* Make an estimate */
4375 return (SYSCTL_OUT(req, 0, len));
4377 error = sysctl_wire_old_buffer(req, 0);
4380 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
4382 mtx_lock(&mountlist_mtx);
4383 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4384 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
4387 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4391 xvn[n].xv_size = sizeof *xvn;
4392 xvn[n].xv_vnode = vp;
4393 xvn[n].xv_id = 0; /* XXX compat */
4394 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
4396 XV_COPY(writecount);
4402 xvn[n].xv_flag = vp->v_vflag;
4404 switch (vp->v_type) {
4411 if (vp->v_rdev == NULL) {
4415 xvn[n].xv_dev = dev2udev(vp->v_rdev);
4418 xvn[n].xv_socket = vp->v_socket;
4421 xvn[n].xv_fifo = vp->v_fifoinfo;
4426 /* shouldn't happen? */
4434 mtx_lock(&mountlist_mtx);
4439 mtx_unlock(&mountlist_mtx);
4441 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
4446 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
4447 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
4452 unmount_or_warn(struct mount *mp)
4456 error = dounmount(mp, MNT_FORCE, curthread);
4458 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
4462 printf("%d)\n", error);
4467 * Unmount all filesystems. The list is traversed in reverse order
4468 * of mounting to avoid dependencies.
4471 vfs_unmountall(void)
4473 struct mount *mp, *tmp;
4475 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
4478 * Since this only runs when rebooting, it is not interlocked.
4480 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
4484 * Forcibly unmounting "/dev" before "/" would prevent clean
4485 * unmount of the latter.
4487 if (mp == rootdevmp)
4490 unmount_or_warn(mp);
4493 if (rootdevmp != NULL)
4494 unmount_or_warn(rootdevmp);
4498 vfs_deferred_inactive(struct vnode *vp, int lkflags)
4501 ASSERT_VI_LOCKED(vp, __func__);
4502 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, ("VI_DEFINACT still set"));
4503 if ((vp->v_iflag & VI_OWEINACT) == 0) {
4507 if (vn_lock(vp, lkflags) == 0) {
4509 if ((vp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == VI_OWEINACT)
4515 vdefer_inactive_cond(vp);
4519 vfs_periodic_inactive_filter(struct vnode *vp, void *arg)
4522 return (vp->v_iflag & VI_DEFINACT);
4525 static void __noinline
4526 vfs_periodic_inactive(struct mount *mp, int flags)
4528 struct vnode *vp, *mvp;
4531 lkflags = LK_EXCLUSIVE | LK_INTERLOCK;
4532 if (flags != MNT_WAIT)
4533 lkflags |= LK_NOWAIT;
4535 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_inactive_filter, NULL) {
4536 if ((vp->v_iflag & VI_DEFINACT) == 0) {
4540 vp->v_iflag &= ~VI_DEFINACT;
4541 vfs_deferred_inactive(vp, lkflags);
4546 vfs_want_msync(struct vnode *vp)
4548 struct vm_object *obj;
4551 * This test may be performed without any locks held.
4552 * We rely on vm_object's type stability.
4554 if (vp->v_vflag & VV_NOSYNC)
4557 return (obj != NULL && vm_object_mightbedirty(obj));
4561 vfs_periodic_msync_inactive_filter(struct vnode *vp, void *arg __unused)
4564 if (vp->v_vflag & VV_NOSYNC)
4566 if (vp->v_iflag & VI_DEFINACT)
4568 return (vfs_want_msync(vp));
4571 static void __noinline
4572 vfs_periodic_msync_inactive(struct mount *mp, int flags)
4574 struct vnode *vp, *mvp;
4575 struct vm_object *obj;
4577 int lkflags, objflags;
4582 lkflags = LK_EXCLUSIVE | LK_INTERLOCK;
4583 if (flags != MNT_WAIT) {
4584 lkflags |= LK_NOWAIT;
4585 objflags = OBJPC_NOSYNC;
4587 objflags = OBJPC_SYNC;
4590 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_msync_inactive_filter, NULL) {
4592 if (vp->v_iflag & VI_DEFINACT) {
4593 vp->v_iflag &= ~VI_DEFINACT;
4596 if (!vfs_want_msync(vp)) {
4598 vfs_deferred_inactive(vp, lkflags);
4603 if (vget(vp, lkflags, td) == 0) {
4605 if (obj != NULL && (vp->v_vflag & VV_NOSYNC) == 0) {
4606 VM_OBJECT_WLOCK(obj);
4607 vm_object_page_clean(obj, 0, 0, objflags);
4608 VM_OBJECT_WUNLOCK(obj);
4615 vdefer_inactive_cond(vp);
4621 vfs_periodic(struct mount *mp, int flags)
4624 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
4626 if ((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0)
4627 vfs_periodic_inactive(mp, flags);
4629 vfs_periodic_msync_inactive(mp, flags);
4633 destroy_vpollinfo_free(struct vpollinfo *vi)
4636 knlist_destroy(&vi->vpi_selinfo.si_note);
4637 mtx_destroy(&vi->vpi_lock);
4638 uma_zfree(vnodepoll_zone, vi);
4642 destroy_vpollinfo(struct vpollinfo *vi)
4645 knlist_clear(&vi->vpi_selinfo.si_note, 1);
4646 seldrain(&vi->vpi_selinfo);
4647 destroy_vpollinfo_free(vi);
4651 * Initialize per-vnode helper structure to hold poll-related state.
4654 v_addpollinfo(struct vnode *vp)
4656 struct vpollinfo *vi;
4658 if (vp->v_pollinfo != NULL)
4660 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
4661 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
4662 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
4663 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
4665 if (vp->v_pollinfo != NULL) {
4667 destroy_vpollinfo_free(vi);
4670 vp->v_pollinfo = vi;
4675 * Record a process's interest in events which might happen to
4676 * a vnode. Because poll uses the historic select-style interface
4677 * internally, this routine serves as both the ``check for any
4678 * pending events'' and the ``record my interest in future events''
4679 * functions. (These are done together, while the lock is held,
4680 * to avoid race conditions.)
4683 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
4687 mtx_lock(&vp->v_pollinfo->vpi_lock);
4688 if (vp->v_pollinfo->vpi_revents & events) {
4690 * This leaves events we are not interested
4691 * in available for the other process which
4692 * which presumably had requested them
4693 * (otherwise they would never have been
4696 events &= vp->v_pollinfo->vpi_revents;
4697 vp->v_pollinfo->vpi_revents &= ~events;
4699 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4702 vp->v_pollinfo->vpi_events |= events;
4703 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
4704 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4709 * Routine to create and manage a filesystem syncer vnode.
4711 #define sync_close ((int (*)(struct vop_close_args *))nullop)
4712 static int sync_fsync(struct vop_fsync_args *);
4713 static int sync_inactive(struct vop_inactive_args *);
4714 static int sync_reclaim(struct vop_reclaim_args *);
4716 static struct vop_vector sync_vnodeops = {
4717 .vop_bypass = VOP_EOPNOTSUPP,
4718 .vop_close = sync_close, /* close */
4719 .vop_fsync = sync_fsync, /* fsync */
4720 .vop_inactive = sync_inactive, /* inactive */
4721 .vop_need_inactive = vop_stdneed_inactive, /* need_inactive */
4722 .vop_reclaim = sync_reclaim, /* reclaim */
4723 .vop_lock1 = vop_stdlock, /* lock */
4724 .vop_unlock = vop_stdunlock, /* unlock */
4725 .vop_islocked = vop_stdislocked, /* islocked */
4727 VFS_VOP_VECTOR_REGISTER(sync_vnodeops);
4730 * Create a new filesystem syncer vnode for the specified mount point.
4733 vfs_allocate_syncvnode(struct mount *mp)
4737 static long start, incr, next;
4740 /* Allocate a new vnode */
4741 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
4743 panic("vfs_allocate_syncvnode: getnewvnode() failed");
4745 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4746 vp->v_vflag |= VV_FORCEINSMQ;
4747 error = insmntque(vp, mp);
4749 panic("vfs_allocate_syncvnode: insmntque() failed");
4750 vp->v_vflag &= ~VV_FORCEINSMQ;
4753 * Place the vnode onto the syncer worklist. We attempt to
4754 * scatter them about on the list so that they will go off
4755 * at evenly distributed times even if all the filesystems
4756 * are mounted at once.
4759 if (next == 0 || next > syncer_maxdelay) {
4763 start = syncer_maxdelay / 2;
4764 incr = syncer_maxdelay;
4770 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
4771 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
4772 mtx_lock(&sync_mtx);
4774 if (mp->mnt_syncer == NULL) {
4775 mp->mnt_syncer = vp;
4778 mtx_unlock(&sync_mtx);
4781 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4788 vfs_deallocate_syncvnode(struct mount *mp)
4792 mtx_lock(&sync_mtx);
4793 vp = mp->mnt_syncer;
4795 mp->mnt_syncer = NULL;
4796 mtx_unlock(&sync_mtx);
4802 * Do a lazy sync of the filesystem.
4805 sync_fsync(struct vop_fsync_args *ap)
4807 struct vnode *syncvp = ap->a_vp;
4808 struct mount *mp = syncvp->v_mount;
4813 * We only need to do something if this is a lazy evaluation.
4815 if (ap->a_waitfor != MNT_LAZY)
4819 * Move ourselves to the back of the sync list.
4821 bo = &syncvp->v_bufobj;
4823 vn_syncer_add_to_worklist(bo, syncdelay);
4827 * Walk the list of vnodes pushing all that are dirty and
4828 * not already on the sync list.
4830 if (vfs_busy(mp, MBF_NOWAIT) != 0)
4832 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
4836 save = curthread_pflags_set(TDP_SYNCIO);
4838 * The filesystem at hand may be idle with free vnodes stored in the
4839 * batch. Return them instead of letting them stay there indefinitely.
4841 vnlru_return_batch(mp);
4842 vfs_periodic(mp, MNT_NOWAIT);
4843 error = VFS_SYNC(mp, MNT_LAZY);
4844 curthread_pflags_restore(save);
4845 vn_finished_write(mp);
4851 * The syncer vnode is no referenced.
4854 sync_inactive(struct vop_inactive_args *ap)
4862 * The syncer vnode is no longer needed and is being decommissioned.
4864 * Modifications to the worklist must be protected by sync_mtx.
4867 sync_reclaim(struct vop_reclaim_args *ap)
4869 struct vnode *vp = ap->a_vp;
4874 mtx_lock(&sync_mtx);
4875 if (vp->v_mount->mnt_syncer == vp)
4876 vp->v_mount->mnt_syncer = NULL;
4877 if (bo->bo_flag & BO_ONWORKLST) {
4878 LIST_REMOVE(bo, bo_synclist);
4879 syncer_worklist_len--;
4881 bo->bo_flag &= ~BO_ONWORKLST;
4883 mtx_unlock(&sync_mtx);
4890 vn_need_pageq_flush(struct vnode *vp)
4892 struct vm_object *obj;
4895 MPASS(mtx_owned(VI_MTX(vp)));
4897 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
4898 vm_object_mightbedirty(obj))
4904 * Check if vnode represents a disk device
4907 vn_isdisk(struct vnode *vp, int *errp)
4911 if (vp->v_type != VCHR) {
4917 if (vp->v_rdev == NULL)
4919 else if (vp->v_rdev->si_devsw == NULL)
4921 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
4927 return (error == 0);
4931 * Common filesystem object access control check routine. Accepts a
4932 * vnode's type, "mode", uid and gid, requested access mode, credentials,
4933 * and optional call-by-reference privused argument allowing vaccess()
4934 * to indicate to the caller whether privilege was used to satisfy the
4935 * request (obsoleted). Returns 0 on success, or an errno on failure.
4938 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
4939 accmode_t accmode, struct ucred *cred, int *privused)
4941 accmode_t dac_granted;
4942 accmode_t priv_granted;
4944 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
4945 ("invalid bit in accmode"));
4946 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
4947 ("VAPPEND without VWRITE"));
4950 * Look for a normal, non-privileged way to access the file/directory
4951 * as requested. If it exists, go with that.
4954 if (privused != NULL)
4959 /* Check the owner. */
4960 if (cred->cr_uid == file_uid) {
4961 dac_granted |= VADMIN;
4962 if (file_mode & S_IXUSR)
4963 dac_granted |= VEXEC;
4964 if (file_mode & S_IRUSR)
4965 dac_granted |= VREAD;
4966 if (file_mode & S_IWUSR)
4967 dac_granted |= (VWRITE | VAPPEND);
4969 if ((accmode & dac_granted) == accmode)
4975 /* Otherwise, check the groups (first match) */
4976 if (groupmember(file_gid, cred)) {
4977 if (file_mode & S_IXGRP)
4978 dac_granted |= VEXEC;
4979 if (file_mode & S_IRGRP)
4980 dac_granted |= VREAD;
4981 if (file_mode & S_IWGRP)
4982 dac_granted |= (VWRITE | VAPPEND);
4984 if ((accmode & dac_granted) == accmode)
4990 /* Otherwise, check everyone else. */
4991 if (file_mode & S_IXOTH)
4992 dac_granted |= VEXEC;
4993 if (file_mode & S_IROTH)
4994 dac_granted |= VREAD;
4995 if (file_mode & S_IWOTH)
4996 dac_granted |= (VWRITE | VAPPEND);
4997 if ((accmode & dac_granted) == accmode)
5002 * Build a privilege mask to determine if the set of privileges
5003 * satisfies the requirements when combined with the granted mask
5004 * from above. For each privilege, if the privilege is required,
5005 * bitwise or the request type onto the priv_granted mask.
5011 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
5012 * requests, instead of PRIV_VFS_EXEC.
5014 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
5015 !priv_check_cred(cred, PRIV_VFS_LOOKUP))
5016 priv_granted |= VEXEC;
5019 * Ensure that at least one execute bit is on. Otherwise,
5020 * a privileged user will always succeed, and we don't want
5021 * this to happen unless the file really is executable.
5023 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
5024 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
5025 !priv_check_cred(cred, PRIV_VFS_EXEC))
5026 priv_granted |= VEXEC;
5029 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
5030 !priv_check_cred(cred, PRIV_VFS_READ))
5031 priv_granted |= VREAD;
5033 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
5034 !priv_check_cred(cred, PRIV_VFS_WRITE))
5035 priv_granted |= (VWRITE | VAPPEND);
5037 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
5038 !priv_check_cred(cred, PRIV_VFS_ADMIN))
5039 priv_granted |= VADMIN;
5041 if ((accmode & (priv_granted | dac_granted)) == accmode) {
5042 /* XXX audit: privilege used */
5043 if (privused != NULL)
5048 return ((accmode & VADMIN) ? EPERM : EACCES);
5052 * Credential check based on process requesting service, and per-attribute
5056 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
5057 struct thread *td, accmode_t accmode)
5061 * Kernel-invoked always succeeds.
5067 * Do not allow privileged processes in jail to directly manipulate
5068 * system attributes.
5070 switch (attrnamespace) {
5071 case EXTATTR_NAMESPACE_SYSTEM:
5072 /* Potentially should be: return (EPERM); */
5073 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
5074 case EXTATTR_NAMESPACE_USER:
5075 return (VOP_ACCESS(vp, accmode, cred, td));
5081 #ifdef DEBUG_VFS_LOCKS
5083 * This only exists to suppress warnings from unlocked specfs accesses. It is
5084 * no longer ok to have an unlocked VFS.
5086 #define IGNORE_LOCK(vp) (KERNEL_PANICKED() || (vp) == NULL || \
5087 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
5089 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
5090 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
5091 "Drop into debugger on lock violation");
5093 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
5094 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
5095 0, "Check for interlock across VOPs");
5097 int vfs_badlock_print = 1; /* Print lock violations. */
5098 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
5099 0, "Print lock violations");
5101 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */
5102 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode,
5103 0, "Print vnode details on lock violations");
5106 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
5107 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
5108 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
5112 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
5116 if (vfs_badlock_backtrace)
5119 if (vfs_badlock_vnode)
5120 vn_printf(vp, "vnode ");
5121 if (vfs_badlock_print)
5122 printf("%s: %p %s\n", str, (void *)vp, msg);
5123 if (vfs_badlock_ddb)
5124 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
5128 assert_vi_locked(struct vnode *vp, const char *str)
5131 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
5132 vfs_badlock("interlock is not locked but should be", str, vp);
5136 assert_vi_unlocked(struct vnode *vp, const char *str)
5139 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
5140 vfs_badlock("interlock is locked but should not be", str, vp);
5144 assert_vop_locked(struct vnode *vp, const char *str)
5148 if (!IGNORE_LOCK(vp)) {
5149 locked = VOP_ISLOCKED(vp);
5150 if (locked == 0 || locked == LK_EXCLOTHER)
5151 vfs_badlock("is not locked but should be", str, vp);
5156 assert_vop_unlocked(struct vnode *vp, const char *str)
5159 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
5160 vfs_badlock("is locked but should not be", str, vp);
5164 assert_vop_elocked(struct vnode *vp, const char *str)
5167 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
5168 vfs_badlock("is not exclusive locked but should be", str, vp);
5170 #endif /* DEBUG_VFS_LOCKS */
5173 vop_rename_fail(struct vop_rename_args *ap)
5176 if (ap->a_tvp != NULL)
5178 if (ap->a_tdvp == ap->a_tvp)
5187 vop_rename_pre(void *ap)
5189 struct vop_rename_args *a = ap;
5191 #ifdef DEBUG_VFS_LOCKS
5193 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
5194 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
5195 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
5196 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
5198 /* Check the source (from). */
5199 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
5200 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
5201 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
5202 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
5203 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
5205 /* Check the target. */
5207 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
5208 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
5210 if (a->a_tdvp != a->a_fdvp)
5212 if (a->a_tvp != a->a_fvp)
5219 #ifdef DEBUG_VFS_LOCKS
5221 vop_strategy_pre(void *ap)
5223 struct vop_strategy_args *a;
5230 * Cluster ops lock their component buffers but not the IO container.
5232 if ((bp->b_flags & B_CLUSTER) != 0)
5235 if (!KERNEL_PANICKED() && !BUF_ISLOCKED(bp)) {
5236 if (vfs_badlock_print)
5238 "VOP_STRATEGY: bp is not locked but should be\n");
5239 if (vfs_badlock_ddb)
5240 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
5245 vop_lock_pre(void *ap)
5247 struct vop_lock1_args *a = ap;
5249 if ((a->a_flags & LK_INTERLOCK) == 0)
5250 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
5252 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
5256 vop_lock_post(void *ap, int rc)
5258 struct vop_lock1_args *a = ap;
5260 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
5261 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
5262 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
5266 vop_unlock_pre(void *ap)
5268 struct vop_unlock_args *a = ap;
5270 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
5274 vop_unlock_post(void *ap, int rc)
5280 vop_need_inactive_pre(void *ap)
5282 struct vop_need_inactive_args *a = ap;
5284 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5288 vop_need_inactive_post(void *ap, int rc)
5290 struct vop_need_inactive_args *a = ap;
5292 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5297 vop_create_post(void *ap, int rc)
5299 struct vop_create_args *a = ap;
5302 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
5306 vop_deleteextattr_post(void *ap, int rc)
5308 struct vop_deleteextattr_args *a = ap;
5311 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5315 vop_link_post(void *ap, int rc)
5317 struct vop_link_args *a = ap;
5320 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
5321 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
5326 vop_mkdir_post(void *ap, int rc)
5328 struct vop_mkdir_args *a = ap;
5331 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
5335 vop_mknod_post(void *ap, int rc)
5337 struct vop_mknod_args *a = ap;
5340 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
5344 vop_reclaim_post(void *ap, int rc)
5346 struct vop_reclaim_args *a = ap;
5349 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE);
5353 vop_remove_post(void *ap, int rc)
5355 struct vop_remove_args *a = ap;
5358 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
5359 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
5364 vop_rename_post(void *ap, int rc)
5366 struct vop_rename_args *a = ap;
5371 if (a->a_fdvp == a->a_tdvp) {
5372 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR)
5374 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5375 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5377 hint |= NOTE_EXTEND;
5378 if (a->a_fvp->v_type == VDIR)
5380 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5382 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL &&
5383 a->a_tvp->v_type == VDIR)
5385 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5388 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
5390 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
5392 if (a->a_tdvp != a->a_fdvp)
5394 if (a->a_tvp != a->a_fvp)
5402 vop_rmdir_post(void *ap, int rc)
5404 struct vop_rmdir_args *a = ap;
5407 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
5408 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
5413 vop_setattr_post(void *ap, int rc)
5415 struct vop_setattr_args *a = ap;
5418 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5422 vop_setextattr_post(void *ap, int rc)
5424 struct vop_setextattr_args *a = ap;
5427 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5431 vop_symlink_post(void *ap, int rc)
5433 struct vop_symlink_args *a = ap;
5436 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
5440 vop_open_post(void *ap, int rc)
5442 struct vop_open_args *a = ap;
5445 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN);
5449 vop_close_post(void *ap, int rc)
5451 struct vop_close_args *a = ap;
5453 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */
5454 !VN_IS_DOOMED(a->a_vp))) {
5455 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ?
5456 NOTE_CLOSE_WRITE : NOTE_CLOSE);
5461 vop_read_post(void *ap, int rc)
5463 struct vop_read_args *a = ap;
5466 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5470 vop_readdir_post(void *ap, int rc)
5472 struct vop_readdir_args *a = ap;
5475 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5478 static struct knlist fs_knlist;
5481 vfs_event_init(void *arg)
5483 knlist_init_mtx(&fs_knlist, NULL);
5485 /* XXX - correct order? */
5486 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
5489 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
5492 KNOTE_UNLOCKED(&fs_knlist, event);
5495 static int filt_fsattach(struct knote *kn);
5496 static void filt_fsdetach(struct knote *kn);
5497 static int filt_fsevent(struct knote *kn, long hint);
5499 struct filterops fs_filtops = {
5501 .f_attach = filt_fsattach,
5502 .f_detach = filt_fsdetach,
5503 .f_event = filt_fsevent
5507 filt_fsattach(struct knote *kn)
5510 kn->kn_flags |= EV_CLEAR;
5511 knlist_add(&fs_knlist, kn, 0);
5516 filt_fsdetach(struct knote *kn)
5519 knlist_remove(&fs_knlist, kn, 0);
5523 filt_fsevent(struct knote *kn, long hint)
5526 kn->kn_fflags |= hint;
5527 return (kn->kn_fflags != 0);
5531 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
5537 error = SYSCTL_IN(req, &vc, sizeof(vc));
5540 if (vc.vc_vers != VFS_CTL_VERS1)
5542 mp = vfs_getvfs(&vc.vc_fsid);
5545 /* ensure that a specific sysctl goes to the right filesystem. */
5546 if (strcmp(vc.vc_fstypename, "*") != 0 &&
5547 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
5551 VCTLTOREQ(&vc, req);
5552 error = VFS_SYSCTL(mp, vc.vc_op, req);
5557 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | CTLFLAG_WR,
5558 NULL, 0, sysctl_vfs_ctl, "",
5562 * Function to initialize a va_filerev field sensibly.
5563 * XXX: Wouldn't a random number make a lot more sense ??
5566 init_va_filerev(void)
5571 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
5574 static int filt_vfsread(struct knote *kn, long hint);
5575 static int filt_vfswrite(struct knote *kn, long hint);
5576 static int filt_vfsvnode(struct knote *kn, long hint);
5577 static void filt_vfsdetach(struct knote *kn);
5578 static struct filterops vfsread_filtops = {
5580 .f_detach = filt_vfsdetach,
5581 .f_event = filt_vfsread
5583 static struct filterops vfswrite_filtops = {
5585 .f_detach = filt_vfsdetach,
5586 .f_event = filt_vfswrite
5588 static struct filterops vfsvnode_filtops = {
5590 .f_detach = filt_vfsdetach,
5591 .f_event = filt_vfsvnode
5595 vfs_knllock(void *arg)
5597 struct vnode *vp = arg;
5599 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5603 vfs_knlunlock(void *arg)
5605 struct vnode *vp = arg;
5611 vfs_knl_assert_locked(void *arg)
5613 #ifdef DEBUG_VFS_LOCKS
5614 struct vnode *vp = arg;
5616 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
5621 vfs_knl_assert_unlocked(void *arg)
5623 #ifdef DEBUG_VFS_LOCKS
5624 struct vnode *vp = arg;
5626 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
5631 vfs_kqfilter(struct vop_kqfilter_args *ap)
5633 struct vnode *vp = ap->a_vp;
5634 struct knote *kn = ap->a_kn;
5637 switch (kn->kn_filter) {
5639 kn->kn_fop = &vfsread_filtops;
5642 kn->kn_fop = &vfswrite_filtops;
5645 kn->kn_fop = &vfsvnode_filtops;
5651 kn->kn_hook = (caddr_t)vp;
5654 if (vp->v_pollinfo == NULL)
5656 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
5658 knlist_add(knl, kn, 0);
5664 * Detach knote from vnode
5667 filt_vfsdetach(struct knote *kn)
5669 struct vnode *vp = (struct vnode *)kn->kn_hook;
5671 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
5672 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
5678 filt_vfsread(struct knote *kn, long hint)
5680 struct vnode *vp = (struct vnode *)kn->kn_hook;
5685 * filesystem is gone, so set the EOF flag and schedule
5686 * the knote for deletion.
5688 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5690 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5695 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
5699 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
5700 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
5707 filt_vfswrite(struct knote *kn, long hint)
5709 struct vnode *vp = (struct vnode *)kn->kn_hook;
5714 * filesystem is gone, so set the EOF flag and schedule
5715 * the knote for deletion.
5717 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
5718 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5726 filt_vfsvnode(struct knote *kn, long hint)
5728 struct vnode *vp = (struct vnode *)kn->kn_hook;
5732 if (kn->kn_sfflags & hint)
5733 kn->kn_fflags |= hint;
5734 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5735 kn->kn_flags |= EV_EOF;
5739 res = (kn->kn_fflags != 0);
5745 * Returns whether the directory is empty or not.
5746 * If it is empty, the return value is 0; otherwise
5747 * the return value is an error value (which may
5751 vfs_emptydir(struct vnode *vp)
5755 struct dirent *dirent, *dp, *endp;
5761 ASSERT_VOP_LOCKED(vp, "vfs_emptydir");
5763 dirent = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK);
5764 iov.iov_base = dirent;
5765 iov.iov_len = sizeof(struct dirent);
5770 uio.uio_resid = sizeof(struct dirent);
5771 uio.uio_segflg = UIO_SYSSPACE;
5772 uio.uio_rw = UIO_READ;
5773 uio.uio_td = curthread;
5775 while (eof == 0 && error == 0) {
5776 error = VOP_READDIR(vp, &uio, curthread->td_ucred, &eof,
5780 endp = (void *)((uint8_t *)dirent +
5781 sizeof(struct dirent) - uio.uio_resid);
5782 for (dp = dirent; dp < endp;
5783 dp = (void *)((uint8_t *)dp + GENERIC_DIRSIZ(dp))) {
5784 if (dp->d_type == DT_WHT)
5786 if (dp->d_namlen == 0)
5788 if (dp->d_type != DT_DIR &&
5789 dp->d_type != DT_UNKNOWN) {
5793 if (dp->d_namlen > 2) {
5797 if (dp->d_namlen == 1 &&
5798 dp->d_name[0] != '.') {
5802 if (dp->d_namlen == 2 &&
5803 dp->d_name[1] != '.') {
5807 uio.uio_resid = sizeof(struct dirent);
5810 free(dirent, M_TEMP);
5815 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
5819 if (dp->d_reclen > ap->a_uio->uio_resid)
5820 return (ENAMETOOLONG);
5821 error = uiomove(dp, dp->d_reclen, ap->a_uio);
5823 if (ap->a_ncookies != NULL) {
5824 if (ap->a_cookies != NULL)
5825 free(ap->a_cookies, M_TEMP);
5826 ap->a_cookies = NULL;
5827 *ap->a_ncookies = 0;
5831 if (ap->a_ncookies == NULL)
5834 KASSERT(ap->a_cookies,
5835 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
5837 *ap->a_cookies = realloc(*ap->a_cookies,
5838 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
5839 (*ap->a_cookies)[*ap->a_ncookies] = off;
5840 *ap->a_ncookies += 1;
5845 * Mark for update the access time of the file if the filesystem
5846 * supports VOP_MARKATIME. This functionality is used by execve and
5847 * mmap, so we want to avoid the I/O implied by directly setting
5848 * va_atime for the sake of efficiency.
5851 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
5856 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
5857 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
5858 (void)VOP_MARKATIME(vp);
5862 * The purpose of this routine is to remove granularity from accmode_t,
5863 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
5864 * VADMIN and VAPPEND.
5866 * If it returns 0, the caller is supposed to continue with the usual
5867 * access checks using 'accmode' as modified by this routine. If it
5868 * returns nonzero value, the caller is supposed to return that value
5871 * Note that after this routine runs, accmode may be zero.
5874 vfs_unixify_accmode(accmode_t *accmode)
5877 * There is no way to specify explicit "deny" rule using
5878 * file mode or POSIX.1e ACLs.
5880 if (*accmode & VEXPLICIT_DENY) {
5886 * None of these can be translated into usual access bits.
5887 * Also, the common case for NFSv4 ACLs is to not contain
5888 * either of these bits. Caller should check for VWRITE
5889 * on the containing directory instead.
5891 if (*accmode & (VDELETE_CHILD | VDELETE))
5894 if (*accmode & VADMIN_PERMS) {
5895 *accmode &= ~VADMIN_PERMS;
5900 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
5901 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
5903 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
5909 * Clear out a doomed vnode (if any) and replace it with a new one as long
5910 * as the fs is not being unmounted. Return the root vnode to the caller.
5912 static int __noinline
5913 vfs_cache_root_fallback(struct mount *mp, int flags, struct vnode **vpp)
5919 if (mp->mnt_rootvnode != NULL) {
5921 vp = mp->mnt_rootvnode;
5923 if (!VN_IS_DOOMED(vp)) {
5926 error = vn_lock(vp, flags);
5935 * Clear the old one.
5937 mp->mnt_rootvnode = NULL;
5942 * Paired with a fence in vfs_op_thread_exit().
5944 atomic_thread_fence_acq();
5945 vfs_op_barrier_wait(mp);
5949 error = VFS_CACHEDROOT(mp, flags, vpp);
5952 if (mp->mnt_vfs_ops == 0) {
5954 if (mp->mnt_vfs_ops != 0) {
5958 if (mp->mnt_rootvnode == NULL) {
5960 mp->mnt_rootvnode = *vpp;
5962 if (mp->mnt_rootvnode != *vpp) {
5963 if (!VN_IS_DOOMED(mp->mnt_rootvnode)) {
5964 panic("%s: mismatch between vnode returned "
5965 " by VFS_CACHEDROOT and the one cached "
5967 __func__, *vpp, mp->mnt_rootvnode);
5977 vfs_cache_root(struct mount *mp, int flags, struct vnode **vpp)
5982 if (!vfs_op_thread_enter(mp))
5983 return (vfs_cache_root_fallback(mp, flags, vpp));
5984 vp = (struct vnode *)atomic_load_ptr(&mp->mnt_rootvnode);
5985 if (vp == NULL || VN_IS_DOOMED(vp)) {
5986 vfs_op_thread_exit(mp);
5987 return (vfs_cache_root_fallback(mp, flags, vpp));
5990 vfs_op_thread_exit(mp);
5991 error = vn_lock(vp, flags);
5994 return (vfs_cache_root_fallback(mp, flags, vpp));
6001 vfs_cache_root_clear(struct mount *mp)
6006 * ops > 0 guarantees there is nobody who can see this vnode
6008 MPASS(mp->mnt_vfs_ops > 0);
6009 vp = mp->mnt_rootvnode;
6010 mp->mnt_rootvnode = NULL;
6015 vfs_cache_root_set(struct mount *mp, struct vnode *vp)
6018 MPASS(mp->mnt_vfs_ops > 0);
6020 mp->mnt_rootvnode = vp;
6024 * These are helper functions for filesystems to traverse all
6025 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
6027 * This interface replaces MNT_VNODE_FOREACH.
6032 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
6037 kern_yield(PRI_USER);
6039 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6040 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL;
6041 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
6042 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */
6043 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp))
6046 if (VN_IS_DOOMED(vp)) {
6053 __mnt_vnode_markerfree_all(mvp, mp);
6054 /* MNT_IUNLOCK(mp); -- done in above function */
6055 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
6058 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
6059 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
6065 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
6069 *mvp = vn_alloc_marker(mp);
6073 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
6074 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */
6075 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp))
6078 if (VN_IS_DOOMED(vp)) {
6087 vn_free_marker(*mvp);
6091 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
6097 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
6105 mtx_assert(MNT_MTX(mp), MA_OWNED);
6107 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6108 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
6111 vn_free_marker(*mvp);
6116 * These are helper functions for filesystems to traverse their
6117 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
6120 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
6123 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6128 vn_free_marker(*mvp);
6133 * Relock the mp mount vnode list lock with the vp vnode interlock in the
6134 * conventional lock order during mnt_vnode_next_active iteration.
6136 * On entry, the mount vnode list lock is held and the vnode interlock is not.
6137 * The list lock is dropped and reacquired. On success, both locks are held.
6138 * On failure, the mount vnode list lock is held but the vnode interlock is
6139 * not, and the procedure may have yielded.
6142 mnt_vnode_next_active_relock(struct vnode *mvp, struct mount *mp,
6145 const struct vnode *tmp;
6148 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER &&
6149 TAILQ_NEXT(mvp, v_actfreelist) != NULL, mvp,
6150 ("%s: bad marker", __func__));
6151 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp,
6152 ("%s: inappropriate vnode", __func__));
6153 ASSERT_VI_UNLOCKED(vp, __func__);
6154 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6158 TAILQ_REMOVE(&mp->mnt_activevnodelist, mvp, v_actfreelist);
6159 TAILQ_INSERT_BEFORE(vp, mvp, v_actfreelist);
6162 * Use a hold to prevent vp from disappearing while the mount vnode
6163 * list lock is dropped and reacquired. Normally a hold would be
6164 * acquired with vhold(), but that might try to acquire the vnode
6165 * interlock, which would be a LOR with the mount vnode list lock.
6167 held = refcount_acquire_if_not_zero(&vp->v_holdcnt);
6168 mtx_unlock(&mp->mnt_listmtx);
6172 if (!refcount_release_if_not_last(&vp->v_holdcnt)) {
6176 mtx_lock(&mp->mnt_listmtx);
6179 * Determine whether the vnode is still the next one after the marker,
6180 * excepting any other markers. If the vnode has not been doomed by
6181 * vgone() then the hold should have ensured that it remained on the
6182 * active list. If it has been doomed but is still on the active list,
6183 * don't abort, but rather skip over it (avoid spinning on doomed
6188 tmp = TAILQ_NEXT(tmp, v_actfreelist);
6189 } while (tmp != NULL && tmp->v_type == VMARKER);
6191 mtx_unlock(&mp->mnt_listmtx);
6200 mtx_lock(&mp->mnt_listmtx);
6203 ASSERT_VI_LOCKED(vp, __func__);
6205 ASSERT_VI_UNLOCKED(vp, __func__);
6206 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6210 static struct vnode *
6211 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
6213 struct vnode *vp, *nvp;
6215 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6216 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6218 vp = TAILQ_NEXT(*mvp, v_actfreelist);
6219 while (vp != NULL) {
6220 if (vp->v_type == VMARKER) {
6221 vp = TAILQ_NEXT(vp, v_actfreelist);
6225 * Try-lock because this is the wrong lock order. If that does
6226 * not succeed, drop the mount vnode list lock and try to
6227 * reacquire it and the vnode interlock in the right order.
6229 if (!VI_TRYLOCK(vp) &&
6230 !mnt_vnode_next_active_relock(*mvp, mp, vp))
6232 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
6233 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
6234 ("alien vnode on the active list %p %p", vp, mp));
6235 if (vp->v_mount == mp && !VN_IS_DOOMED(vp))
6237 nvp = TAILQ_NEXT(vp, v_actfreelist);
6241 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
6243 /* Check if we are done */
6245 mtx_unlock(&mp->mnt_listmtx);
6246 mnt_vnode_markerfree_active(mvp, mp);
6249 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
6250 mtx_unlock(&mp->mnt_listmtx);
6251 ASSERT_VI_LOCKED(vp, "active iter");
6252 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
6257 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
6261 kern_yield(PRI_USER);
6262 mtx_lock(&mp->mnt_listmtx);
6263 return (mnt_vnode_next_active(mvp, mp));
6267 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
6271 *mvp = vn_alloc_marker(mp);
6276 mtx_lock(&mp->mnt_listmtx);
6277 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
6279 mtx_unlock(&mp->mnt_listmtx);
6280 mnt_vnode_markerfree_active(mvp, mp);
6283 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
6284 return (mnt_vnode_next_active(mvp, mp));
6288 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
6294 mtx_lock(&mp->mnt_listmtx);
6295 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
6296 mtx_unlock(&mp->mnt_listmtx);
6297 mnt_vnode_markerfree_active(mvp, mp);
6301 * These are helper functions for filesystems to traverse their
6302 * lazy vnodes. See MNT_VNODE_FOREACH_LAZY() in sys/mount.h
6305 mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp)
6308 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6313 free(*mvp, M_VNODE_MARKER);
6318 * Relock the mp mount vnode list lock with the vp vnode interlock in the
6319 * conventional lock order during mnt_vnode_next_lazy iteration.
6321 * On entry, the mount vnode list lock is held and the vnode interlock is not.
6322 * The list lock is dropped and reacquired. On success, both locks are held.
6323 * On failure, the mount vnode list lock is held but the vnode interlock is
6324 * not, and the procedure may have yielded.
6327 mnt_vnode_next_lazy_relock(struct vnode *mvp, struct mount *mp,
6330 const struct vnode *tmp;
6333 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER &&
6334 TAILQ_NEXT(mvp, v_lazylist) != NULL, mvp,
6335 ("%s: bad marker", __func__));
6336 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp,
6337 ("%s: inappropriate vnode", __func__));
6338 ASSERT_VI_UNLOCKED(vp, __func__);
6339 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6343 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, mvp, v_lazylist);
6344 TAILQ_INSERT_BEFORE(vp, mvp, v_lazylist);
6347 * Use a hold to prevent vp from disappearing while the mount vnode
6348 * list lock is dropped and reacquired. Normally a hold would be
6349 * acquired with vhold(), but that might try to acquire the vnode
6350 * interlock, which would be a LOR with the mount vnode list lock.
6352 held = refcount_acquire_if_not_zero(&vp->v_holdcnt);
6353 mtx_unlock(&mp->mnt_listmtx);
6357 if (!refcount_release_if_not_last(&vp->v_holdcnt)) {
6361 mtx_lock(&mp->mnt_listmtx);
6364 * Determine whether the vnode is still the next one after the marker,
6365 * excepting any other markers. If the vnode has not been doomed by
6366 * vgone() then the hold should have ensured that it remained on the
6367 * lazy list. If it has been doomed but is still on the lazy list,
6368 * don't abort, but rather skip over it (avoid spinning on doomed
6373 tmp = TAILQ_NEXT(tmp, v_lazylist);
6374 } while (tmp != NULL && tmp->v_type == VMARKER);
6376 mtx_unlock(&mp->mnt_listmtx);
6385 mtx_lock(&mp->mnt_listmtx);
6388 ASSERT_VI_LOCKED(vp, __func__);
6390 ASSERT_VI_UNLOCKED(vp, __func__);
6391 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6395 static struct vnode *
6396 mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6399 struct vnode *vp, *nvp;
6401 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6402 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6404 vp = TAILQ_NEXT(*mvp, v_lazylist);
6405 while (vp != NULL) {
6406 if (vp->v_type == VMARKER) {
6407 vp = TAILQ_NEXT(vp, v_lazylist);
6411 * See if we want to process the vnode. Note we may encounter a
6412 * long string of vnodes we don't care about and hog the list
6413 * as a result. Check for it and requeue the marker.
6415 if (VN_IS_DOOMED(vp) || !cb(vp, cbarg)) {
6416 if (!should_yield()) {
6417 vp = TAILQ_NEXT(vp, v_lazylist);
6420 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp,
6422 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp,
6424 mtx_unlock(&mp->mnt_listmtx);
6425 kern_yield(PRI_USER);
6426 mtx_lock(&mp->mnt_listmtx);
6430 * Try-lock because this is the wrong lock order. If that does
6431 * not succeed, drop the mount vnode list lock and try to
6432 * reacquire it and the vnode interlock in the right order.
6434 if (!VI_TRYLOCK(vp) &&
6435 !mnt_vnode_next_lazy_relock(*mvp, mp, vp))
6437 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
6438 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
6439 ("alien vnode on the lazy list %p %p", vp, mp));
6440 if (vp->v_mount == mp && !VN_IS_DOOMED(vp))
6442 nvp = TAILQ_NEXT(vp, v_lazylist);
6446 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist);
6448 /* Check if we are done */
6450 mtx_unlock(&mp->mnt_listmtx);
6451 mnt_vnode_markerfree_lazy(mvp, mp);
6454 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, v_lazylist);
6455 mtx_unlock(&mp->mnt_listmtx);
6456 ASSERT_VI_LOCKED(vp, "lazy iter");
6457 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
6462 __mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6467 kern_yield(PRI_USER);
6468 mtx_lock(&mp->mnt_listmtx);
6469 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg));
6473 __mnt_vnode_first_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6478 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
6482 (*mvp)->v_type = VMARKER;
6483 (*mvp)->v_mount = mp;
6485 mtx_lock(&mp->mnt_listmtx);
6486 vp = TAILQ_FIRST(&mp->mnt_lazyvnodelist);
6488 mtx_unlock(&mp->mnt_listmtx);
6489 mnt_vnode_markerfree_lazy(mvp, mp);
6492 TAILQ_INSERT_BEFORE(vp, *mvp, v_lazylist);
6493 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg));
6497 __mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp)
6503 mtx_lock(&mp->mnt_listmtx);
6504 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist);
6505 mtx_unlock(&mp->mnt_listmtx);
6506 mnt_vnode_markerfree_lazy(mvp, mp);