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/condvar.h>
55 #include <sys/counter.h>
56 #include <sys/dirent.h>
57 #include <sys/event.h>
58 #include <sys/eventhandler.h>
59 #include <sys/extattr.h>
61 #include <sys/fcntl.h>
64 #include <sys/kernel.h>
65 #include <sys/kthread.h>
66 #include <sys/lockf.h>
67 #include <sys/malloc.h>
68 #include <sys/mount.h>
69 #include <sys/namei.h>
70 #include <sys/pctrie.h>
72 #include <sys/reboot.h>
73 #include <sys/refcount.h>
74 #include <sys/rwlock.h>
75 #include <sys/sched.h>
76 #include <sys/sleepqueue.h>
79 #include <sys/sysctl.h>
80 #include <sys/syslog.h>
81 #include <sys/vmmeter.h>
82 #include <sys/vnode.h>
83 #include <sys/watchdog.h>
85 #include <machine/stdarg.h>
87 #include <security/mac/mac_framework.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_extern.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_kern.h>
102 static void delmntque(struct vnode *vp);
103 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
104 int slpflag, int slptimeo);
105 static void syncer_shutdown(void *arg, int howto);
106 static int vtryrecycle(struct vnode *vp);
107 static void v_init_counters(struct vnode *);
108 static void v_incr_usecount(struct vnode *);
109 static void v_incr_usecount_locked(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);
121 * Number of vnodes in existence. Increased whenever getnewvnode()
122 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
124 static unsigned long numvnodes;
126 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
127 "Number of vnodes in existence");
129 static counter_u64_t vnodes_created;
130 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
131 "Number of vnodes created by getnewvnode");
133 static u_long mnt_free_list_batch = 128;
134 SYSCTL_ULONG(_vfs, OID_AUTO, mnt_free_list_batch, CTLFLAG_RW,
135 &mnt_free_list_batch, 0, "Limit of vnodes held on mnt's free list");
138 * Conversion tables for conversion from vnode types to inode formats
141 enum vtype iftovt_tab[16] = {
142 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
143 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
145 int vttoif_tab[10] = {
146 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
147 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
151 * List of vnodes that are ready for recycling.
153 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
156 * "Free" vnode target. Free vnodes are rarely completely free, but are
157 * just ones that are cheap to recycle. Usually they are for files which
158 * have been stat'd but not read; these usually have inode and namecache
159 * data attached to them. This target is the preferred minimum size of a
160 * sub-cache consisting mostly of such files. The system balances the size
161 * of this sub-cache with its complement to try to prevent either from
162 * thrashing while the other is relatively inactive. The targets express
163 * a preference for the best balance.
165 * "Above" this target there are 2 further targets (watermarks) related
166 * to recyling of free vnodes. In the best-operating case, the cache is
167 * exactly full, the free list has size between vlowat and vhiwat above the
168 * free target, and recycling from it and normal use maintains this state.
169 * Sometimes the free list is below vlowat or even empty, but this state
170 * is even better for immediate use provided the cache is not full.
171 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free
172 * ones) to reach one of these states. The watermarks are currently hard-
173 * coded as 4% and 9% of the available space higher. These and the default
174 * of 25% for wantfreevnodes are too large if the memory size is large.
175 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim
176 * whenever vnlru_proc() becomes active.
178 static u_long wantfreevnodes;
179 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW,
180 &wantfreevnodes, 0, "Target for minimum number of \"free\" vnodes");
181 static u_long freevnodes;
182 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD,
183 &freevnodes, 0, "Number of \"free\" vnodes");
185 static counter_u64_t recycles_count;
186 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count,
187 "Number of vnodes recycled to meet vnode cache targets");
190 * Various variables used for debugging the new implementation of
192 * XXX these are probably of (very) limited utility now.
194 static int reassignbufcalls;
195 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
196 "Number of calls to reassignbuf");
198 static counter_u64_t free_owe_inact;
199 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact,
200 "Number of times free vnodes kept on active list due to VFS "
201 "owing inactivation");
203 /* To keep more than one thread at a time from running vfs_getnewfsid */
204 static struct mtx mntid_mtx;
207 * Lock for any access to the following:
212 static struct mtx vnode_free_list_mtx;
214 /* Publicly exported FS */
215 struct nfs_public nfs_pub;
217 static uma_zone_t buf_trie_zone;
219 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
220 static uma_zone_t vnode_zone;
221 static uma_zone_t vnodepoll_zone;
224 * The workitem queue.
226 * It is useful to delay writes of file data and filesystem metadata
227 * for tens of seconds so that quickly created and deleted files need
228 * not waste disk bandwidth being created and removed. To realize this,
229 * we append vnodes to a "workitem" queue. When running with a soft
230 * updates implementation, most pending metadata dependencies should
231 * not wait for more than a few seconds. Thus, mounted on block devices
232 * are delayed only about a half the time that file data is delayed.
233 * Similarly, directory updates are more critical, so are only delayed
234 * about a third the time that file data is delayed. Thus, there are
235 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
236 * one each second (driven off the filesystem syncer process). The
237 * syncer_delayno variable indicates the next queue that is to be processed.
238 * Items that need to be processed soon are placed in this queue:
240 * syncer_workitem_pending[syncer_delayno]
242 * A delay of fifteen seconds is done by placing the request fifteen
243 * entries later in the queue:
245 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
248 static int syncer_delayno;
249 static long syncer_mask;
250 LIST_HEAD(synclist, bufobj);
251 static struct synclist *syncer_workitem_pending;
253 * The sync_mtx protects:
258 * syncer_workitem_pending
259 * syncer_worklist_len
262 static struct mtx sync_mtx;
263 static struct cv sync_wakeup;
265 #define SYNCER_MAXDELAY 32
266 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
267 static int syncdelay = 30; /* max time to delay syncing data */
268 static int filedelay = 30; /* time to delay syncing files */
269 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
270 "Time to delay syncing files (in seconds)");
271 static int dirdelay = 29; /* time to delay syncing directories */
272 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
273 "Time to delay syncing directories (in seconds)");
274 static int metadelay = 28; /* time to delay syncing metadata */
275 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
276 "Time to delay syncing metadata (in seconds)");
277 static int rushjob; /* number of slots to run ASAP */
278 static int stat_rush_requests; /* number of times I/O speeded up */
279 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
280 "Number of times I/O speeded up (rush requests)");
283 * When shutting down the syncer, run it at four times normal speed.
285 #define SYNCER_SHUTDOWN_SPEEDUP 4
286 static int sync_vnode_count;
287 static int syncer_worklist_len;
288 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
291 /* Target for maximum number of vnodes. */
293 static int gapvnodes; /* gap between wanted and desired */
294 static int vhiwat; /* enough extras after expansion */
295 static int vlowat; /* minimal extras before expansion */
296 static int vstir; /* nonzero to stir non-free vnodes */
297 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */
300 sysctl_update_desiredvnodes(SYSCTL_HANDLER_ARGS)
302 int error, old_desiredvnodes;
304 old_desiredvnodes = desiredvnodes;
305 if ((error = sysctl_handle_int(oidp, arg1, arg2, req)) != 0)
307 if (old_desiredvnodes != desiredvnodes) {
308 wantfreevnodes = desiredvnodes / 4;
309 /* XXX locking seems to be incomplete. */
310 vfs_hash_changesize(desiredvnodes);
311 cache_changesize(desiredvnodes);
316 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
317 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &desiredvnodes, 0,
318 sysctl_update_desiredvnodes, "I", "Target for maximum number of vnodes");
319 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
320 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)");
321 static int vnlru_nowhere;
322 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
323 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
325 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
329 * Support for the bufobj clean & dirty pctrie.
332 buf_trie_alloc(struct pctrie *ptree)
335 return uma_zalloc(buf_trie_zone, M_NOWAIT);
339 buf_trie_free(struct pctrie *ptree, void *node)
342 uma_zfree(buf_trie_zone, node);
344 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
347 * Initialize the vnode management data structures.
349 * Reevaluate the following cap on the number of vnodes after the physical
350 * memory size exceeds 512GB. In the limit, as the physical memory size
351 * grows, the ratio of the memory size in KB to vnodes approaches 64:1.
353 #ifndef MAXVNODES_MAX
354 #define MAXVNODES_MAX (512 * 1024 * 1024 / 64) /* 8M */
358 * Initialize a vnode as it first enters the zone.
361 vnode_init(void *mem, int size, int flags)
370 vp->v_vnlock = &vp->v_lock;
371 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
373 * By default, don't allow shared locks unless filesystems opt-in.
375 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT,
376 LK_NOSHARE | LK_IS_VNODE);
380 bufobj_init(&vp->v_bufobj, vp);
382 * Initialize namecache.
384 LIST_INIT(&vp->v_cache_src);
385 TAILQ_INIT(&vp->v_cache_dst);
387 * Initialize rangelocks.
389 rangelock_init(&vp->v_rl);
394 * Free a vnode when it is cleared from the zone.
397 vnode_fini(void *mem, int size)
403 rangelock_destroy(&vp->v_rl);
404 lockdestroy(vp->v_vnlock);
405 mtx_destroy(&vp->v_interlock);
407 rw_destroy(BO_LOCKPTR(bo));
411 * Provide the size of NFS nclnode and NFS fh for calculation of the
412 * vnode memory consumption. The size is specified directly to
413 * eliminate dependency on NFS-private header.
415 * Other filesystems may use bigger or smaller (like UFS and ZFS)
416 * private inode data, but the NFS-based estimation is ample enough.
417 * Still, we care about differences in the size between 64- and 32-bit
420 * Namecache structure size is heuristically
421 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1.
424 #define NFS_NCLNODE_SZ (528 + 64)
427 #define NFS_NCLNODE_SZ (360 + 32)
432 vntblinit(void *dummy __unused)
435 int physvnodes, virtvnodes;
438 * Desiredvnodes is a function of the physical memory size and the
439 * kernel's heap size. Generally speaking, it scales with the
440 * physical memory size. The ratio of desiredvnodes to the physical
441 * memory size is 1:16 until desiredvnodes exceeds 98,304.
443 * marginal ratio of desiredvnodes to the physical memory size is
444 * 1:64. However, desiredvnodes is limited by the kernel's heap
445 * size. The memory required by desiredvnodes vnodes and vm objects
446 * must not exceed 1/10th of the kernel's heap size.
448 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 +
449 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64;
450 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) +
451 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ));
452 desiredvnodes = min(physvnodes, virtvnodes);
453 if (desiredvnodes > MAXVNODES_MAX) {
455 printf("Reducing kern.maxvnodes %d -> %d\n",
456 desiredvnodes, MAXVNODES_MAX);
457 desiredvnodes = MAXVNODES_MAX;
459 wantfreevnodes = desiredvnodes / 4;
460 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
461 TAILQ_INIT(&vnode_free_list);
462 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
463 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
464 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0);
465 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
466 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
468 * Preallocate enough nodes to support one-per buf so that
469 * we can not fail an insert. reassignbuf() callers can not
470 * tolerate the insertion failure.
472 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
473 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
474 UMA_ZONE_NOFREE | UMA_ZONE_VM);
475 uma_prealloc(buf_trie_zone, nbuf);
477 vnodes_created = counter_u64_alloc(M_WAITOK);
478 recycles_count = counter_u64_alloc(M_WAITOK);
479 free_owe_inact = counter_u64_alloc(M_WAITOK);
482 * Initialize the filesystem syncer.
484 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
486 syncer_maxdelay = syncer_mask + 1;
487 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
488 cv_init(&sync_wakeup, "syncer");
489 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
493 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
497 * Mark a mount point as busy. Used to synchronize access and to delay
498 * unmounting. Eventually, mountlist_mtx is not released on failure.
500 * vfs_busy() is a custom lock, it can block the caller.
501 * vfs_busy() only sleeps if the unmount is active on the mount point.
502 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
503 * vnode belonging to mp.
505 * Lookup uses vfs_busy() to traverse mount points.
507 * / vnode lock A / vnode lock (/var) D
508 * /var vnode lock B /log vnode lock(/var/log) E
509 * vfs_busy lock C vfs_busy lock F
511 * Within each file system, the lock order is C->A->B and F->D->E.
513 * When traversing across mounts, the system follows that lock order:
519 * The lookup() process for namei("/var") illustrates the process:
520 * VOP_LOOKUP() obtains B while A is held
521 * vfs_busy() obtains a shared lock on F while A and B are held
522 * vput() releases lock on B
523 * vput() releases lock on A
524 * VFS_ROOT() obtains lock on D while shared lock on F is held
525 * vfs_unbusy() releases shared lock on F
526 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
527 * Attempt to lock A (instead of vp_crossmp) while D is held would
528 * violate the global order, causing deadlocks.
530 * dounmount() locks B while F is drained.
533 vfs_busy(struct mount *mp, int flags)
536 MPASS((flags & ~MBF_MASK) == 0);
537 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
542 * If mount point is currently being unmounted, sleep until the
543 * mount point fate is decided. If thread doing the unmounting fails,
544 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
545 * that this mount point has survived the unmount attempt and vfs_busy
546 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
547 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
548 * about to be really destroyed. vfs_busy needs to release its
549 * reference on the mount point in this case and return with ENOENT,
550 * telling the caller that mount mount it tried to busy is no longer
553 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
554 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
557 CTR1(KTR_VFS, "%s: failed busying before sleeping",
561 if (flags & MBF_MNTLSTLOCK)
562 mtx_unlock(&mountlist_mtx);
563 mp->mnt_kern_flag |= MNTK_MWAIT;
564 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
565 if (flags & MBF_MNTLSTLOCK)
566 mtx_lock(&mountlist_mtx);
569 if (flags & MBF_MNTLSTLOCK)
570 mtx_unlock(&mountlist_mtx);
577 * Free a busy filesystem.
580 vfs_unbusy(struct mount *mp)
583 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
586 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
588 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
589 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
590 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
591 mp->mnt_kern_flag &= ~MNTK_DRAINING;
592 wakeup(&mp->mnt_lockref);
598 * Lookup a mount point by filesystem identifier.
601 vfs_getvfs(fsid_t *fsid)
605 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
606 mtx_lock(&mountlist_mtx);
607 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
608 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
609 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
611 mtx_unlock(&mountlist_mtx);
615 mtx_unlock(&mountlist_mtx);
616 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
617 return ((struct mount *) 0);
621 * Lookup a mount point by filesystem identifier, busying it before
624 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
625 * cache for popular filesystem identifiers. The cache is lockess, using
626 * the fact that struct mount's are never freed. In worst case we may
627 * get pointer to unmounted or even different filesystem, so we have to
628 * check what we got, and go slow way if so.
631 vfs_busyfs(fsid_t *fsid)
633 #define FSID_CACHE_SIZE 256
634 typedef struct mount * volatile vmp_t;
635 static vmp_t cache[FSID_CACHE_SIZE];
640 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
641 hash = fsid->val[0] ^ fsid->val[1];
642 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
645 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
646 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
648 if (vfs_busy(mp, 0) != 0) {
652 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
653 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
659 mtx_lock(&mountlist_mtx);
660 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
661 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
662 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
663 error = vfs_busy(mp, MBF_MNTLSTLOCK);
666 mtx_unlock(&mountlist_mtx);
673 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
674 mtx_unlock(&mountlist_mtx);
675 return ((struct mount *) 0);
679 * Check if a user can access privileged mount options.
682 vfs_suser(struct mount *mp, struct thread *td)
687 * If the thread is jailed, but this is not a jail-friendly file
688 * system, deny immediately.
690 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
694 * If the file system was mounted outside the jail of the calling
695 * thread, deny immediately.
697 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
701 * If file system supports delegated administration, we don't check
702 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
703 * by the file system itself.
704 * If this is not the user that did original mount, we check for
705 * the PRIV_VFS_MOUNT_OWNER privilege.
707 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
708 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
709 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
716 * Get a new unique fsid. Try to make its val[0] unique, since this value
717 * will be used to create fake device numbers for stat(). Also try (but
718 * not so hard) make its val[0] unique mod 2^16, since some emulators only
719 * support 16-bit device numbers. We end up with unique val[0]'s for the
720 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
722 * Keep in mind that several mounts may be running in parallel. Starting
723 * the search one past where the previous search terminated is both a
724 * micro-optimization and a defense against returning the same fsid to
728 vfs_getnewfsid(struct mount *mp)
730 static uint16_t mntid_base;
735 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
736 mtx_lock(&mntid_mtx);
737 mtype = mp->mnt_vfc->vfc_typenum;
738 tfsid.val[1] = mtype;
739 mtype = (mtype & 0xFF) << 24;
741 tfsid.val[0] = makedev(255,
742 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
744 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
748 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
749 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
750 mtx_unlock(&mntid_mtx);
754 * Knob to control the precision of file timestamps:
756 * 0 = seconds only; nanoseconds zeroed.
757 * 1 = seconds and nanoseconds, accurate within 1/HZ.
758 * 2 = seconds and nanoseconds, truncated to microseconds.
759 * >=3 = seconds and nanoseconds, maximum precision.
761 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
763 static int timestamp_precision = TSP_USEC;
764 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
765 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
766 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, "
767 "3+: sec + ns (max. precision))");
770 * Get a current timestamp.
773 vfs_timestamp(struct timespec *tsp)
777 switch (timestamp_precision) {
779 tsp->tv_sec = time_second;
787 TIMEVAL_TO_TIMESPEC(&tv, tsp);
797 * Set vnode attributes to VNOVAL
800 vattr_null(struct vattr *vap)
804 vap->va_size = VNOVAL;
805 vap->va_bytes = VNOVAL;
806 vap->va_mode = VNOVAL;
807 vap->va_nlink = VNOVAL;
808 vap->va_uid = VNOVAL;
809 vap->va_gid = VNOVAL;
810 vap->va_fsid = VNOVAL;
811 vap->va_fileid = VNOVAL;
812 vap->va_blocksize = VNOVAL;
813 vap->va_rdev = VNOVAL;
814 vap->va_atime.tv_sec = VNOVAL;
815 vap->va_atime.tv_nsec = VNOVAL;
816 vap->va_mtime.tv_sec = VNOVAL;
817 vap->va_mtime.tv_nsec = VNOVAL;
818 vap->va_ctime.tv_sec = VNOVAL;
819 vap->va_ctime.tv_nsec = VNOVAL;
820 vap->va_birthtime.tv_sec = VNOVAL;
821 vap->va_birthtime.tv_nsec = VNOVAL;
822 vap->va_flags = VNOVAL;
823 vap->va_gen = VNOVAL;
828 * This routine is called when we have too many vnodes. It attempts
829 * to free <count> vnodes and will potentially free vnodes that still
830 * have VM backing store (VM backing store is typically the cause
831 * of a vnode blowout so we want to do this). Therefore, this operation
832 * is not considered cheap.
834 * A number of conditions may prevent a vnode from being reclaimed.
835 * the buffer cache may have references on the vnode, a directory
836 * vnode may still have references due to the namei cache representing
837 * underlying files, or the vnode may be in active use. It is not
838 * desirable to reuse such vnodes. These conditions may cause the
839 * number of vnodes to reach some minimum value regardless of what
840 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
843 vlrureclaim(struct mount *mp, int reclaim_nc_src, int trigger)
846 int count, done, target;
849 vn_start_write(NULL, &mp, V_WAIT);
851 count = mp->mnt_nvnodelistsize;
852 target = count * (int64_t)gapvnodes / imax(desiredvnodes, 1);
853 target = target / 10 + 1;
854 while (count != 0 && done < target) {
855 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
856 while (vp != NULL && vp->v_type == VMARKER)
857 vp = TAILQ_NEXT(vp, v_nmntvnodes);
861 * XXX LRU is completely broken for non-free vnodes. First
862 * by calling here in mountpoint order, then by moving
863 * unselected vnodes to the end here, and most grossly by
864 * removing the vlruvp() function that was supposed to
865 * maintain the order. (This function was born broken
866 * since syncer problems prevented it doing anything.) The
867 * order is closer to LRC (C = Created).
869 * LRU reclaiming of vnodes seems to have last worked in
870 * FreeBSD-3 where LRU wasn't mentioned under any spelling.
871 * Then there was no hold count, and inactive vnodes were
872 * simply put on the free list in LRU order. The separate
873 * lists also break LRU. We prefer to reclaim from the
874 * free list for technical reasons. This tends to thrash
875 * the free list to keep very unrecently used held vnodes.
876 * The problem is mitigated by keeping the free list large.
878 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
879 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
884 * If it's been deconstructed already, it's still
885 * referenced, or it exceeds the trigger, skip it.
886 * Also skip free vnodes. We are trying to make space
887 * to expand the free list, not reduce it.
889 if (vp->v_usecount ||
890 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
891 ((vp->v_iflag & VI_FREE) != 0) ||
892 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
893 vp->v_object->resident_page_count > trigger)) {
899 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
901 goto next_iter_mntunlocked;
905 * v_usecount may have been bumped after VOP_LOCK() dropped
906 * the vnode interlock and before it was locked again.
908 * It is not necessary to recheck VI_DOOMED because it can
909 * only be set by another thread that holds both the vnode
910 * lock and vnode interlock. If another thread has the
911 * vnode lock before we get to VOP_LOCK() and obtains the
912 * vnode interlock after VOP_LOCK() drops the vnode
913 * interlock, the other thread will be unable to drop the
914 * vnode lock before our VOP_LOCK() call fails.
916 if (vp->v_usecount ||
917 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
918 (vp->v_iflag & VI_FREE) != 0 ||
919 (vp->v_object != NULL &&
920 vp->v_object->resident_page_count > trigger)) {
921 VOP_UNLOCK(vp, LK_INTERLOCK);
923 goto next_iter_mntunlocked;
925 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
926 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
927 counter_u64_add(recycles_count, 1);
932 next_iter_mntunlocked:
941 kern_yield(PRI_USER);
946 vn_finished_write(mp);
950 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */
951 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free,
953 "limit on vnode free requests per call to the vnlru_free routine");
956 * Attempt to reduce the free list by the requested amount.
959 vnlru_free_locked(int count, struct vfsops *mnt_op)
965 tried_batches = false;
966 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
967 if (count > max_vnlru_free)
968 count = max_vnlru_free;
969 for (; count > 0; count--) {
970 vp = TAILQ_FIRST(&vnode_free_list);
972 * The list can be modified while the free_list_mtx
973 * has been dropped and vp could be NULL here.
978 mtx_unlock(&vnode_free_list_mtx);
979 vnlru_return_batches(mnt_op);
980 tried_batches = true;
981 mtx_lock(&vnode_free_list_mtx);
985 VNASSERT(vp->v_op != NULL, vp,
986 ("vnlru_free: vnode already reclaimed."));
987 KASSERT((vp->v_iflag & VI_FREE) != 0,
988 ("Removing vnode not on freelist"));
989 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
990 ("Mangling active vnode"));
991 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
994 * Don't recycle if our vnode is from different type
995 * of mount point. Note that mp is type-safe, the
996 * check does not reach unmapped address even if
997 * vnode is reclaimed.
998 * Don't recycle if we can't get the interlock without
1001 if ((mnt_op != NULL && (mp = vp->v_mount) != NULL &&
1002 mp->mnt_op != mnt_op) || !VI_TRYLOCK(vp)) {
1003 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
1006 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
1007 vp, ("vp inconsistent on freelist"));
1010 * The clear of VI_FREE prevents activation of the
1011 * vnode. There is no sense in putting the vnode on
1012 * the mount point active list, only to remove it
1013 * later during recycling. Inline the relevant part
1014 * of vholdl(), to avoid triggering assertions or
1018 vp->v_iflag &= ~VI_FREE;
1019 refcount_acquire(&vp->v_holdcnt);
1021 mtx_unlock(&vnode_free_list_mtx);
1025 * If the recycled succeeded this vdrop will actually free
1026 * the vnode. If not it will simply place it back on
1030 mtx_lock(&vnode_free_list_mtx);
1035 vnlru_free(int count, struct vfsops *mnt_op)
1038 mtx_lock(&vnode_free_list_mtx);
1039 vnlru_free_locked(count, mnt_op);
1040 mtx_unlock(&vnode_free_list_mtx);
1044 /* XXX some names and initialization are bad for limits and watermarks. */
1050 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100);
1051 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */
1052 vlowat = vhiwat / 2;
1053 if (numvnodes > desiredvnodes)
1055 space = desiredvnodes - numvnodes;
1056 if (freevnodes > wantfreevnodes)
1057 space += freevnodes - wantfreevnodes;
1062 vnlru_return_batch_locked(struct mount *mp)
1066 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
1068 if (mp->mnt_tmpfreevnodelistsize == 0)
1071 TAILQ_FOREACH(vp, &mp->mnt_tmpfreevnodelist, v_actfreelist) {
1072 VNASSERT((vp->v_mflag & VMP_TMPMNTFREELIST) != 0, vp,
1073 ("vnode without VMP_TMPMNTFREELIST on mnt_tmpfreevnodelist"));
1074 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
1076 mtx_lock(&vnode_free_list_mtx);
1077 TAILQ_CONCAT(&vnode_free_list, &mp->mnt_tmpfreevnodelist, v_actfreelist);
1078 freevnodes += mp->mnt_tmpfreevnodelistsize;
1079 mtx_unlock(&vnode_free_list_mtx);
1080 mp->mnt_tmpfreevnodelistsize = 0;
1084 vnlru_return_batch(struct mount *mp)
1087 mtx_lock(&mp->mnt_listmtx);
1088 vnlru_return_batch_locked(mp);
1089 mtx_unlock(&mp->mnt_listmtx);
1093 vnlru_return_batches(struct vfsops *mnt_op)
1095 struct mount *mp, *nmp;
1098 mtx_lock(&mountlist_mtx);
1099 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1100 need_unbusy = false;
1101 if (mnt_op != NULL && mp->mnt_op != mnt_op)
1103 if (mp->mnt_tmpfreevnodelistsize == 0)
1105 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) == 0) {
1106 vnlru_return_batch(mp);
1108 mtx_lock(&mountlist_mtx);
1111 nmp = TAILQ_NEXT(mp, mnt_list);
1115 mtx_unlock(&mountlist_mtx);
1119 * Attempt to recycle vnodes in a context that is always safe to block.
1120 * Calling vlrurecycle() from the bowels of filesystem code has some
1121 * interesting deadlock problems.
1123 static struct proc *vnlruproc;
1124 static int vnlruproc_sig;
1129 struct mount *mp, *nmp;
1130 unsigned long onumvnodes;
1131 int done, force, reclaim_nc_src, trigger, usevnodes;
1133 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc,
1134 SHUTDOWN_PRI_FIRST);
1138 kproc_suspend_check(vnlruproc);
1139 mtx_lock(&vnode_free_list_mtx);
1141 * If numvnodes is too large (due to desiredvnodes being
1142 * adjusted using its sysctl, or emergency growth), first
1143 * try to reduce it by discarding from the free list.
1145 if (numvnodes > desiredvnodes)
1146 vnlru_free_locked(numvnodes - desiredvnodes, NULL);
1148 * Sleep if the vnode cache is in a good state. This is
1149 * when it is not over-full and has space for about a 4%
1150 * or 9% expansion (by growing its size or inexcessively
1151 * reducing its free list). Otherwise, try to reclaim
1152 * space for a 10% expansion.
1154 if (vstir && force == 0) {
1158 if (vspace() >= vlowat && force == 0) {
1160 wakeup(&vnlruproc_sig);
1161 msleep(vnlruproc, &vnode_free_list_mtx,
1162 PVFS|PDROP, "vlruwt", hz);
1165 mtx_unlock(&vnode_free_list_mtx);
1167 onumvnodes = numvnodes;
1169 * Calculate parameters for recycling. These are the same
1170 * throughout the loop to give some semblance of fairness.
1171 * The trigger point is to avoid recycling vnodes with lots
1172 * of resident pages. We aren't trying to free memory; we
1173 * are trying to recycle or at least free vnodes.
1175 if (numvnodes <= desiredvnodes)
1176 usevnodes = numvnodes - freevnodes;
1178 usevnodes = numvnodes;
1182 * The trigger value is is chosen to give a conservatively
1183 * large value to ensure that it alone doesn't prevent
1184 * making progress. The value can easily be so large that
1185 * it is effectively infinite in some congested and
1186 * misconfigured cases, and this is necessary. Normally
1187 * it is about 8 to 100 (pages), which is quite large.
1189 trigger = vm_cnt.v_page_count * 2 / usevnodes;
1191 trigger = vsmalltrigger;
1192 reclaim_nc_src = force >= 3;
1193 mtx_lock(&mountlist_mtx);
1194 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1195 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
1196 nmp = TAILQ_NEXT(mp, mnt_list);
1199 done += vlrureclaim(mp, reclaim_nc_src, trigger);
1200 mtx_lock(&mountlist_mtx);
1201 nmp = TAILQ_NEXT(mp, mnt_list);
1204 mtx_unlock(&mountlist_mtx);
1205 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes)
1208 if (force == 0 || force == 1) {
1218 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
1220 kern_yield(PRI_USER);
1222 * After becoming active to expand above low water, keep
1223 * active until above high water.
1225 force = vspace() < vhiwat;
1229 static struct kproc_desc vnlru_kp = {
1234 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
1238 * Routines having to do with the management of the vnode table.
1242 * Try to recycle a freed vnode. We abort if anyone picks up a reference
1243 * before we actually vgone(). This function must be called with the vnode
1244 * held to prevent the vnode from being returned to the free list midway
1248 vtryrecycle(struct vnode *vp)
1252 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
1253 VNASSERT(vp->v_holdcnt, vp,
1254 ("vtryrecycle: Recycling vp %p without a reference.", vp));
1256 * This vnode may found and locked via some other list, if so we
1257 * can't recycle it yet.
1259 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1261 "%s: impossible to recycle, vp %p lock is already held",
1263 return (EWOULDBLOCK);
1266 * Don't recycle if its filesystem is being suspended.
1268 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1271 "%s: impossible to recycle, cannot start the write for %p",
1276 * If we got this far, we need to acquire the interlock and see if
1277 * anyone picked up this vnode from another list. If not, we will
1278 * mark it with DOOMED via vgonel() so that anyone who does find it
1279 * will skip over it.
1282 if (vp->v_usecount) {
1283 VOP_UNLOCK(vp, LK_INTERLOCK);
1284 vn_finished_write(vnmp);
1286 "%s: impossible to recycle, %p is already referenced",
1290 if ((vp->v_iflag & VI_DOOMED) == 0) {
1291 counter_u64_add(recycles_count, 1);
1294 VOP_UNLOCK(vp, LK_INTERLOCK);
1295 vn_finished_write(vnmp);
1303 if (vspace() < vlowat && vnlruproc_sig == 0) {
1310 * Wait if necessary for space for a new vnode.
1313 getnewvnode_wait(int suspended)
1316 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1317 if (numvnodes >= desiredvnodes) {
1320 * The file system is being suspended. We cannot
1321 * risk a deadlock here, so allow allocation of
1322 * another vnode even if this would give too many.
1326 if (vnlruproc_sig == 0) {
1327 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1330 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1333 /* Post-adjust like the pre-adjust in getnewvnode(). */
1334 if (numvnodes + 1 > desiredvnodes && freevnodes > 1)
1335 vnlru_free_locked(1, NULL);
1336 return (numvnodes >= desiredvnodes ? ENFILE : 0);
1340 * This hack is fragile, and probably not needed any more now that the
1341 * watermark handling works.
1344 getnewvnode_reserve(u_int count)
1348 /* Pre-adjust like the pre-adjust in getnewvnode(), with any count. */
1349 /* XXX no longer so quick, but this part is not racy. */
1350 mtx_lock(&vnode_free_list_mtx);
1351 if (numvnodes + count > desiredvnodes && freevnodes > wantfreevnodes)
1352 vnlru_free_locked(ulmin(numvnodes + count - desiredvnodes,
1353 freevnodes - wantfreevnodes), NULL);
1354 mtx_unlock(&vnode_free_list_mtx);
1357 /* First try to be quick and racy. */
1358 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1359 td->td_vp_reserv += count;
1360 vcheckspace(); /* XXX no longer so quick, but more racy */
1363 atomic_subtract_long(&numvnodes, count);
1365 mtx_lock(&vnode_free_list_mtx);
1367 if (getnewvnode_wait(0) == 0) {
1370 atomic_add_long(&numvnodes, 1);
1374 mtx_unlock(&vnode_free_list_mtx);
1378 * This hack is fragile, especially if desiredvnodes or wantvnodes are
1379 * misconfgured or changed significantly. Reducing desiredvnodes below
1380 * the reserved amount should cause bizarre behaviour like reducing it
1381 * below the number of active vnodes -- the system will try to reduce
1382 * numvnodes to match, but should fail, so the subtraction below should
1386 getnewvnode_drop_reserve(void)
1391 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1392 td->td_vp_reserv = 0;
1396 * Return the next vnode from the free list.
1399 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1404 struct lock_object *lo;
1405 static int cyclecount;
1408 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1411 if (td->td_vp_reserv > 0) {
1412 td->td_vp_reserv -= 1;
1415 mtx_lock(&vnode_free_list_mtx);
1416 if (numvnodes < desiredvnodes)
1418 else if (cyclecount++ >= freevnodes) {
1423 * Grow the vnode cache if it will not be above its target max
1424 * after growing. Otherwise, if the free list is nonempty, try
1425 * to reclaim 1 item from it before growing the cache (possibly
1426 * above its target max if the reclamation failed or is delayed).
1427 * Otherwise, wait for some space. In all cases, schedule
1428 * vnlru_proc() if we are getting short of space. The watermarks
1429 * should be chosen so that we never wait or even reclaim from
1430 * the free list to below its target minimum.
1432 if (numvnodes + 1 <= desiredvnodes)
1434 else if (freevnodes > 0)
1435 vnlru_free_locked(1, NULL);
1437 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1439 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1441 mtx_unlock(&vnode_free_list_mtx);
1447 atomic_add_long(&numvnodes, 1);
1448 mtx_unlock(&vnode_free_list_mtx);
1450 counter_u64_add(vnodes_created, 1);
1451 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK);
1453 * Locks are given the generic name "vnode" when created.
1454 * Follow the historic practice of using the filesystem
1455 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc.
1457 * Locks live in a witness group keyed on their name. Thus,
1458 * when a lock is renamed, it must also move from the witness
1459 * group of its old name to the witness group of its new name.
1461 * The change only needs to be made when the vnode moves
1462 * from one filesystem type to another. We ensure that each
1463 * filesystem use a single static name pointer for its tag so
1464 * that we can compare pointers rather than doing a strcmp().
1466 lo = &vp->v_vnlock->lock_object;
1467 if (lo->lo_name != tag) {
1469 WITNESS_DESTROY(lo);
1470 WITNESS_INIT(lo, tag);
1473 * By default, don't allow shared locks unless filesystems opt-in.
1475 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE;
1477 * Finalize various vnode identity bits.
1479 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp));
1480 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp));
1481 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp));
1485 v_init_counters(vp);
1486 vp->v_bufobj.bo_ops = &buf_ops_bio;
1488 if (mp == NULL && vops != &dead_vnodeops)
1489 printf("NULL mp in getnewvnode(9), tag %s\n", tag);
1493 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1494 mac_vnode_associate_singlelabel(mp, vp);
1497 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize;
1498 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1499 vp->v_vflag |= VV_NOKNOTE;
1503 * For the filesystems which do not use vfs_hash_insert(),
1504 * still initialize v_hash to have vfs_hash_index() useful.
1505 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1508 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1515 * Delete from old mount point vnode list, if on one.
1518 delmntque(struct vnode *vp)
1528 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1529 ("Active vnode list size %d > Vnode list size %d",
1530 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1531 active = vp->v_iflag & VI_ACTIVE;
1532 vp->v_iflag &= ~VI_ACTIVE;
1534 mtx_lock(&mp->mnt_listmtx);
1535 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1536 mp->mnt_activevnodelistsize--;
1537 mtx_unlock(&mp->mnt_listmtx);
1541 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1542 ("bad mount point vnode list size"));
1543 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1544 mp->mnt_nvnodelistsize--;
1550 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1554 vp->v_op = &dead_vnodeops;
1560 * Insert into list of vnodes for the new mount point, if available.
1563 insmntque1(struct vnode *vp, struct mount *mp,
1564 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1567 KASSERT(vp->v_mount == NULL,
1568 ("insmntque: vnode already on per mount vnode list"));
1569 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1570 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1573 * We acquire the vnode interlock early to ensure that the
1574 * vnode cannot be recycled by another process releasing a
1575 * holdcnt on it before we get it on both the vnode list
1576 * and the active vnode list. The mount mutex protects only
1577 * manipulation of the vnode list and the vnode freelist
1578 * mutex protects only manipulation of the active vnode list.
1579 * Hence the need to hold the vnode interlock throughout.
1583 if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1584 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1585 mp->mnt_nvnodelistsize == 0)) &&
1586 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1595 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1596 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1597 ("neg mount point vnode list size"));
1598 mp->mnt_nvnodelistsize++;
1599 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1600 ("Activating already active vnode"));
1601 vp->v_iflag |= VI_ACTIVE;
1602 mtx_lock(&mp->mnt_listmtx);
1603 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1604 mp->mnt_activevnodelistsize++;
1605 mtx_unlock(&mp->mnt_listmtx);
1612 insmntque(struct vnode *vp, struct mount *mp)
1615 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1619 * Flush out and invalidate all buffers associated with a bufobj
1620 * Called with the underlying object locked.
1623 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1628 if (flags & V_SAVE) {
1629 error = bufobj_wwait(bo, slpflag, slptimeo);
1634 if (bo->bo_dirty.bv_cnt > 0) {
1636 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1639 * XXX We could save a lock/unlock if this was only
1640 * enabled under INVARIANTS
1643 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1644 panic("vinvalbuf: dirty bufs");
1648 * If you alter this loop please notice that interlock is dropped and
1649 * reacquired in flushbuflist. Special care is needed to ensure that
1650 * no race conditions occur from this.
1653 error = flushbuflist(&bo->bo_clean,
1654 flags, bo, slpflag, slptimeo);
1655 if (error == 0 && !(flags & V_CLEANONLY))
1656 error = flushbuflist(&bo->bo_dirty,
1657 flags, bo, slpflag, slptimeo);
1658 if (error != 0 && error != EAGAIN) {
1662 } while (error != 0);
1665 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1666 * have write I/O in-progress but if there is a VM object then the
1667 * VM object can also have read-I/O in-progress.
1670 bufobj_wwait(bo, 0, 0);
1671 if ((flags & V_VMIO) == 0) {
1673 if (bo->bo_object != NULL) {
1674 VM_OBJECT_WLOCK(bo->bo_object);
1675 vm_object_pip_wait(bo->bo_object, "bovlbx");
1676 VM_OBJECT_WUNLOCK(bo->bo_object);
1680 } while (bo->bo_numoutput > 0);
1684 * Destroy the copy in the VM cache, too.
1686 if (bo->bo_object != NULL &&
1687 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) {
1688 VM_OBJECT_WLOCK(bo->bo_object);
1689 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1690 OBJPR_CLEANONLY : 0);
1691 VM_OBJECT_WUNLOCK(bo->bo_object);
1696 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO |
1697 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 ||
1698 bo->bo_clean.bv_cnt > 0))
1699 panic("vinvalbuf: flush failed");
1700 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 &&
1701 bo->bo_dirty.bv_cnt > 0)
1702 panic("vinvalbuf: flush dirty failed");
1709 * Flush out and invalidate all buffers associated with a vnode.
1710 * Called with the underlying object locked.
1713 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1716 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1717 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1718 if (vp->v_object != NULL && vp->v_object->handle != vp)
1720 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1724 * Flush out buffers on the specified list.
1728 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1731 struct buf *bp, *nbp;
1736 ASSERT_BO_WLOCKED(bo);
1739 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1740 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1741 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1745 lblkno = nbp->b_lblkno;
1746 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1749 error = BUF_TIMELOCK(bp,
1750 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1751 "flushbuf", slpflag, slptimeo);
1754 return (error != ENOLCK ? error : EAGAIN);
1756 KASSERT(bp->b_bufobj == bo,
1757 ("bp %p wrong b_bufobj %p should be %p",
1758 bp, bp->b_bufobj, bo));
1760 * XXX Since there are no node locks for NFS, I
1761 * believe there is a slight chance that a delayed
1762 * write will occur while sleeping just above, so
1765 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1768 bp->b_flags |= B_ASYNC;
1771 return (EAGAIN); /* XXX: why not loop ? */
1774 bp->b_flags |= (B_INVAL | B_RELBUF);
1775 bp->b_flags &= ~B_ASYNC;
1780 nbp = gbincore(bo, lblkno);
1781 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1783 break; /* nbp invalid */
1789 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn)
1795 ASSERT_BO_LOCKED(bo);
1797 for (lblkno = startn;;) {
1799 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno);
1800 if (bp == NULL || bp->b_lblkno >= endn ||
1801 bp->b_lblkno < startn)
1803 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1804 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0);
1807 if (error == ENOLCK)
1811 KASSERT(bp->b_bufobj == bo,
1812 ("bp %p wrong b_bufobj %p should be %p",
1813 bp, bp->b_bufobj, bo));
1814 lblkno = bp->b_lblkno + 1;
1815 if ((bp->b_flags & B_MANAGED) == 0)
1817 bp->b_flags |= B_RELBUF;
1819 * In the VMIO case, use the B_NOREUSE flag to hint that the
1820 * pages backing each buffer in the range are unlikely to be
1821 * reused. Dirty buffers will have the hint applied once
1822 * they've been written.
1824 if (bp->b_vp->v_object != NULL)
1825 bp->b_flags |= B_NOREUSE;
1833 * Truncate a file's buffer and pages to a specified length. This
1834 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1838 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
1840 struct buf *bp, *nbp;
1845 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1846 vp, cred, blksize, (uintmax_t)length);
1849 * Round up to the *next* lbn.
1851 trunclbn = howmany(length, blksize);
1853 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1860 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1861 if (bp->b_lblkno < trunclbn)
1864 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1865 BO_LOCKPTR(bo)) == ENOLCK)
1869 bp->b_flags |= (B_INVAL | B_RELBUF);
1870 bp->b_flags &= ~B_ASYNC;
1876 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1877 (nbp->b_vp != vp) ||
1878 (nbp->b_flags & B_DELWRI))) {
1884 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1885 if (bp->b_lblkno < trunclbn)
1888 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1889 BO_LOCKPTR(bo)) == ENOLCK)
1892 bp->b_flags |= (B_INVAL | B_RELBUF);
1893 bp->b_flags &= ~B_ASYNC;
1899 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1900 (nbp->b_vp != vp) ||
1901 (nbp->b_flags & B_DELWRI) == 0)) {
1910 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1911 if (bp->b_lblkno > 0)
1914 * Since we hold the vnode lock this should only
1915 * fail if we're racing with the buf daemon.
1918 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1919 BO_LOCKPTR(bo)) == ENOLCK) {
1922 VNASSERT((bp->b_flags & B_DELWRI), vp,
1923 ("buf(%p) on dirty queue without DELWRI", bp));
1932 bufobj_wwait(bo, 0, 0);
1934 vnode_pager_setsize(vp, length);
1940 buf_vlist_remove(struct buf *bp)
1944 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1945 ASSERT_BO_WLOCKED(bp->b_bufobj);
1946 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1947 (BX_VNDIRTY|BX_VNCLEAN),
1948 ("buf_vlist_remove: Buf %p is on two lists", bp));
1949 if (bp->b_xflags & BX_VNDIRTY)
1950 bv = &bp->b_bufobj->bo_dirty;
1952 bv = &bp->b_bufobj->bo_clean;
1953 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
1954 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1956 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1960 * Add the buffer to the sorted clean or dirty block list.
1962 * NOTE: xflags is passed as a constant, optimizing this inline function!
1965 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1971 ASSERT_BO_WLOCKED(bo);
1972 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
1973 ("dead bo %p", bo));
1974 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1975 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1976 bp->b_xflags |= xflags;
1977 if (xflags & BX_VNDIRTY)
1983 * Keep the list ordered. Optimize empty list insertion. Assume
1984 * we tend to grow at the tail so lookup_le should usually be cheaper
1987 if (bv->bv_cnt == 0 ||
1988 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
1989 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1990 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
1991 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
1993 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
1994 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
1996 panic("buf_vlist_add: Preallocated nodes insufficient.");
2001 * Look up a buffer using the buffer tries.
2004 gbincore(struct bufobj *bo, daddr_t lblkno)
2008 ASSERT_BO_LOCKED(bo);
2009 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
2012 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
2016 * Associate a buffer with a vnode.
2019 bgetvp(struct vnode *vp, struct buf *bp)
2024 ASSERT_BO_WLOCKED(bo);
2025 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
2027 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
2028 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
2029 ("bgetvp: bp already attached! %p", bp));
2035 * Insert onto list for new vnode.
2037 buf_vlist_add(bp, bo, BX_VNCLEAN);
2041 * Disassociate a buffer from a vnode.
2044 brelvp(struct buf *bp)
2049 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2050 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
2053 * Delete from old vnode list, if on one.
2055 vp = bp->b_vp; /* XXX */
2058 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2059 buf_vlist_remove(bp);
2061 panic("brelvp: Buffer %p not on queue.", bp);
2062 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2063 bo->bo_flag &= ~BO_ONWORKLST;
2064 mtx_lock(&sync_mtx);
2065 LIST_REMOVE(bo, bo_synclist);
2066 syncer_worklist_len--;
2067 mtx_unlock(&sync_mtx);
2070 bp->b_bufobj = NULL;
2076 * Add an item to the syncer work queue.
2079 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
2083 ASSERT_BO_WLOCKED(bo);
2085 mtx_lock(&sync_mtx);
2086 if (bo->bo_flag & BO_ONWORKLST)
2087 LIST_REMOVE(bo, bo_synclist);
2089 bo->bo_flag |= BO_ONWORKLST;
2090 syncer_worklist_len++;
2093 if (delay > syncer_maxdelay - 2)
2094 delay = syncer_maxdelay - 2;
2095 slot = (syncer_delayno + delay) & syncer_mask;
2097 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
2098 mtx_unlock(&sync_mtx);
2102 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
2106 mtx_lock(&sync_mtx);
2107 len = syncer_worklist_len - sync_vnode_count;
2108 mtx_unlock(&sync_mtx);
2109 error = SYSCTL_OUT(req, &len, sizeof(len));
2113 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
2114 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
2116 static struct proc *updateproc;
2117 static void sched_sync(void);
2118 static struct kproc_desc up_kp = {
2123 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
2126 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
2131 *bo = LIST_FIRST(slp);
2135 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
2138 * We use vhold in case the vnode does not
2139 * successfully sync. vhold prevents the vnode from
2140 * going away when we unlock the sync_mtx so that
2141 * we can acquire the vnode interlock.
2144 mtx_unlock(&sync_mtx);
2146 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2148 mtx_lock(&sync_mtx);
2149 return (*bo == LIST_FIRST(slp));
2151 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2152 (void) VOP_FSYNC(vp, MNT_LAZY, td);
2154 vn_finished_write(mp);
2156 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
2158 * Put us back on the worklist. The worklist
2159 * routine will remove us from our current
2160 * position and then add us back in at a later
2163 vn_syncer_add_to_worklist(*bo, syncdelay);
2167 mtx_lock(&sync_mtx);
2171 static int first_printf = 1;
2174 * System filesystem synchronizer daemon.
2179 struct synclist *next, *slp;
2182 struct thread *td = curthread;
2184 int net_worklist_len;
2185 int syncer_final_iter;
2189 syncer_final_iter = 0;
2190 syncer_state = SYNCER_RUNNING;
2191 starttime = time_uptime;
2192 td->td_pflags |= TDP_NORUNNINGBUF;
2194 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
2197 mtx_lock(&sync_mtx);
2199 if (syncer_state == SYNCER_FINAL_DELAY &&
2200 syncer_final_iter == 0) {
2201 mtx_unlock(&sync_mtx);
2202 kproc_suspend_check(td->td_proc);
2203 mtx_lock(&sync_mtx);
2205 net_worklist_len = syncer_worklist_len - sync_vnode_count;
2206 if (syncer_state != SYNCER_RUNNING &&
2207 starttime != time_uptime) {
2209 printf("\nSyncing disks, vnodes remaining... ");
2212 printf("%d ", net_worklist_len);
2214 starttime = time_uptime;
2217 * Push files whose dirty time has expired. Be careful
2218 * of interrupt race on slp queue.
2220 * Skip over empty worklist slots when shutting down.
2223 slp = &syncer_workitem_pending[syncer_delayno];
2224 syncer_delayno += 1;
2225 if (syncer_delayno == syncer_maxdelay)
2227 next = &syncer_workitem_pending[syncer_delayno];
2229 * If the worklist has wrapped since the
2230 * it was emptied of all but syncer vnodes,
2231 * switch to the FINAL_DELAY state and run
2232 * for one more second.
2234 if (syncer_state == SYNCER_SHUTTING_DOWN &&
2235 net_worklist_len == 0 &&
2236 last_work_seen == syncer_delayno) {
2237 syncer_state = SYNCER_FINAL_DELAY;
2238 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
2240 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
2241 syncer_worklist_len > 0);
2244 * Keep track of the last time there was anything
2245 * on the worklist other than syncer vnodes.
2246 * Return to the SHUTTING_DOWN state if any
2249 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
2250 last_work_seen = syncer_delayno;
2251 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
2252 syncer_state = SYNCER_SHUTTING_DOWN;
2253 while (!LIST_EMPTY(slp)) {
2254 error = sync_vnode(slp, &bo, td);
2256 LIST_REMOVE(bo, bo_synclist);
2257 LIST_INSERT_HEAD(next, bo, bo_synclist);
2261 if (first_printf == 0) {
2263 * Drop the sync mutex, because some watchdog
2264 * drivers need to sleep while patting
2266 mtx_unlock(&sync_mtx);
2267 wdog_kern_pat(WD_LASTVAL);
2268 mtx_lock(&sync_mtx);
2272 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
2273 syncer_final_iter--;
2275 * The variable rushjob allows the kernel to speed up the
2276 * processing of the filesystem syncer process. A rushjob
2277 * value of N tells the filesystem syncer to process the next
2278 * N seconds worth of work on its queue ASAP. Currently rushjob
2279 * is used by the soft update code to speed up the filesystem
2280 * syncer process when the incore state is getting so far
2281 * ahead of the disk that the kernel memory pool is being
2282 * threatened with exhaustion.
2289 * Just sleep for a short period of time between
2290 * iterations when shutting down to allow some I/O
2293 * If it has taken us less than a second to process the
2294 * current work, then wait. Otherwise start right over
2295 * again. We can still lose time if any single round
2296 * takes more than two seconds, but it does not really
2297 * matter as we are just trying to generally pace the
2298 * filesystem activity.
2300 if (syncer_state != SYNCER_RUNNING ||
2301 time_uptime == starttime) {
2303 sched_prio(td, PPAUSE);
2306 if (syncer_state != SYNCER_RUNNING)
2307 cv_timedwait(&sync_wakeup, &sync_mtx,
2308 hz / SYNCER_SHUTDOWN_SPEEDUP);
2309 else if (time_uptime == starttime)
2310 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2315 * Request the syncer daemon to speed up its work.
2316 * We never push it to speed up more than half of its
2317 * normal turn time, otherwise it could take over the cpu.
2320 speedup_syncer(void)
2324 mtx_lock(&sync_mtx);
2325 if (rushjob < syncdelay / 2) {
2327 stat_rush_requests += 1;
2330 mtx_unlock(&sync_mtx);
2331 cv_broadcast(&sync_wakeup);
2336 * Tell the syncer to speed up its work and run though its work
2337 * list several times, then tell it to shut down.
2340 syncer_shutdown(void *arg, int howto)
2343 if (howto & RB_NOSYNC)
2345 mtx_lock(&sync_mtx);
2346 syncer_state = SYNCER_SHUTTING_DOWN;
2348 mtx_unlock(&sync_mtx);
2349 cv_broadcast(&sync_wakeup);
2350 kproc_shutdown(arg, howto);
2354 syncer_suspend(void)
2357 syncer_shutdown(updateproc, 0);
2364 mtx_lock(&sync_mtx);
2366 syncer_state = SYNCER_RUNNING;
2367 mtx_unlock(&sync_mtx);
2368 cv_broadcast(&sync_wakeup);
2369 kproc_resume(updateproc);
2373 * Reassign a buffer from one vnode to another.
2374 * Used to assign file specific control information
2375 * (indirect blocks) to the vnode to which they belong.
2378 reassignbuf(struct buf *bp)
2391 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2392 bp, bp->b_vp, bp->b_flags);
2394 * B_PAGING flagged buffers cannot be reassigned because their vp
2395 * is not fully linked in.
2397 if (bp->b_flags & B_PAGING)
2398 panic("cannot reassign paging buffer");
2401 * Delete from old vnode list, if on one.
2404 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2405 buf_vlist_remove(bp);
2407 panic("reassignbuf: Buffer %p not on queue.", bp);
2409 * If dirty, put on list of dirty buffers; otherwise insert onto list
2412 if (bp->b_flags & B_DELWRI) {
2413 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2414 switch (vp->v_type) {
2424 vn_syncer_add_to_worklist(bo, delay);
2426 buf_vlist_add(bp, bo, BX_VNDIRTY);
2428 buf_vlist_add(bp, bo, BX_VNCLEAN);
2430 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2431 mtx_lock(&sync_mtx);
2432 LIST_REMOVE(bo, bo_synclist);
2433 syncer_worklist_len--;
2434 mtx_unlock(&sync_mtx);
2435 bo->bo_flag &= ~BO_ONWORKLST;
2440 bp = TAILQ_FIRST(&bv->bv_hd);
2441 KASSERT(bp == NULL || bp->b_bufobj == bo,
2442 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2443 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2444 KASSERT(bp == NULL || bp->b_bufobj == bo,
2445 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2447 bp = TAILQ_FIRST(&bv->bv_hd);
2448 KASSERT(bp == NULL || bp->b_bufobj == bo,
2449 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2450 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2451 KASSERT(bp == NULL || bp->b_bufobj == bo,
2452 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2458 v_init_counters(struct vnode *vp)
2461 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2462 vp, ("%s called for an initialized vnode", __FUNCTION__));
2463 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2465 refcount_init(&vp->v_holdcnt, 1);
2466 refcount_init(&vp->v_usecount, 1);
2470 v_incr_usecount_locked(struct vnode *vp)
2473 ASSERT_VI_LOCKED(vp, __func__);
2474 if ((vp->v_iflag & VI_OWEINACT) != 0) {
2475 VNASSERT(vp->v_usecount == 0, vp,
2476 ("vnode with usecount and VI_OWEINACT set"));
2477 vp->v_iflag &= ~VI_OWEINACT;
2479 refcount_acquire(&vp->v_usecount);
2480 v_incr_devcount(vp);
2484 * Increment the use count on the vnode, taking care to reference
2485 * the driver's usecount if this is a chardev.
2488 v_incr_usecount(struct vnode *vp)
2491 ASSERT_VI_UNLOCKED(vp, __func__);
2492 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2494 if (vp->v_type != VCHR &&
2495 refcount_acquire_if_not_zero(&vp->v_usecount)) {
2496 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2497 ("vnode with usecount and VI_OWEINACT set"));
2500 v_incr_usecount_locked(vp);
2506 * Increment si_usecount of the associated device, if any.
2509 v_incr_devcount(struct vnode *vp)
2512 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2513 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2515 vp->v_rdev->si_usecount++;
2521 * Decrement si_usecount of the associated device, if any.
2524 v_decr_devcount(struct vnode *vp)
2527 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2528 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2530 vp->v_rdev->si_usecount--;
2536 * Grab a particular vnode from the free list, increment its
2537 * reference count and lock it. VI_DOOMED is set if the vnode
2538 * is being destroyed. Only callers who specify LK_RETRY will
2539 * see doomed vnodes. If inactive processing was delayed in
2540 * vput try to do it here.
2542 * Notes on lockless counter manipulation:
2543 * _vhold, vputx and other routines make various decisions based
2544 * on either holdcnt or usecount being 0. As long as either counter
2545 * is not transitioning 0->1 nor 1->0, the manipulation can be done
2546 * with atomic operations. Otherwise the interlock is taken covering
2547 * both the atomic and additional actions.
2550 vget(struct vnode *vp, int flags, struct thread *td)
2552 int error, oweinact;
2554 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2555 ("vget: invalid lock operation"));
2557 if ((flags & LK_INTERLOCK) != 0)
2558 ASSERT_VI_LOCKED(vp, __func__);
2560 ASSERT_VI_UNLOCKED(vp, __func__);
2561 if ((flags & LK_VNHELD) != 0)
2562 VNASSERT((vp->v_holdcnt > 0), vp,
2563 ("vget: LK_VNHELD passed but vnode not held"));
2565 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2567 if ((flags & LK_VNHELD) == 0)
2568 _vhold(vp, (flags & LK_INTERLOCK) != 0);
2570 if ((error = vn_lock(vp, flags)) != 0) {
2572 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2576 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2577 panic("vget: vn_lock failed to return ENOENT\n");
2579 * We don't guarantee that any particular close will
2580 * trigger inactive processing so just make a best effort
2581 * here at preventing a reference to a removed file. If
2582 * we don't succeed no harm is done.
2584 * Upgrade our holdcnt to a usecount.
2586 if (vp->v_type == VCHR ||
2587 !refcount_acquire_if_not_zero(&vp->v_usecount)) {
2589 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2593 vp->v_iflag &= ~VI_OWEINACT;
2595 refcount_acquire(&vp->v_usecount);
2596 v_incr_devcount(vp);
2597 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2598 (flags & LK_NOWAIT) == 0)
2606 * Increase the reference (use) and hold count of a vnode.
2607 * This will also remove the vnode from the free list if it is presently free.
2610 vref(struct vnode *vp)
2613 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2615 v_incr_usecount(vp);
2619 vrefl(struct vnode *vp)
2622 ASSERT_VI_LOCKED(vp, __func__);
2623 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2625 v_incr_usecount_locked(vp);
2629 vrefact(struct vnode *vp)
2632 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2633 if (__predict_false(vp->v_type == VCHR)) {
2634 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp,
2635 ("%s: wrong ref counts", __func__));
2640 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
2641 VNASSERT(old > 0, vp, ("%s: wrong hold count", __func__));
2642 old = atomic_fetchadd_int(&vp->v_usecount, 1);
2643 VNASSERT(old > 0, vp, ("%s: wrong use count", __func__));
2645 refcount_acquire(&vp->v_holdcnt);
2646 refcount_acquire(&vp->v_usecount);
2651 * Return reference count of a vnode.
2653 * The results of this call are only guaranteed when some mechanism is used to
2654 * stop other processes from gaining references to the vnode. This may be the
2655 * case if the caller holds the only reference. This is also useful when stale
2656 * data is acceptable as race conditions may be accounted for by some other
2660 vrefcnt(struct vnode *vp)
2663 return (vp->v_usecount);
2666 #define VPUTX_VRELE 1
2667 #define VPUTX_VPUT 2
2668 #define VPUTX_VUNREF 3
2671 * Decrement the use and hold counts for a vnode.
2673 * See an explanation near vget() as to why atomic operation is safe.
2676 vputx(struct vnode *vp, int func)
2680 KASSERT(vp != NULL, ("vputx: null vp"));
2681 if (func == VPUTX_VUNREF)
2682 ASSERT_VOP_LOCKED(vp, "vunref");
2683 else if (func == VPUTX_VPUT)
2684 ASSERT_VOP_LOCKED(vp, "vput");
2686 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2687 ASSERT_VI_UNLOCKED(vp, __func__);
2688 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2690 if (vp->v_type != VCHR &&
2691 refcount_release_if_not_last(&vp->v_usecount)) {
2692 if (func == VPUTX_VPUT)
2701 * We want to hold the vnode until the inactive finishes to
2702 * prevent vgone() races. We drop the use count here and the
2703 * hold count below when we're done.
2705 if (!refcount_release(&vp->v_usecount) ||
2706 (vp->v_iflag & VI_DOINGINACT)) {
2707 if (func == VPUTX_VPUT)
2709 v_decr_devcount(vp);
2714 v_decr_devcount(vp);
2718 if (vp->v_usecount != 0) {
2719 vn_printf(vp, "vputx: usecount not zero for vnode ");
2720 panic("vputx: usecount not zero");
2723 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2726 * We must call VOP_INACTIVE with the node locked. Mark
2727 * as VI_DOINGINACT to avoid recursion.
2729 vp->v_iflag |= VI_OWEINACT;
2732 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2736 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2737 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2743 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2744 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2749 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp,
2750 ("vnode with usecount and VI_OWEINACT set"));
2752 if (vp->v_iflag & VI_OWEINACT)
2753 vinactive(vp, curthread);
2754 if (func != VPUTX_VUNREF)
2761 * Vnode put/release.
2762 * If count drops to zero, call inactive routine and return to freelist.
2765 vrele(struct vnode *vp)
2768 vputx(vp, VPUTX_VRELE);
2772 * Release an already locked vnode. This give the same effects as
2773 * unlock+vrele(), but takes less time and avoids releasing and
2774 * re-aquiring the lock (as vrele() acquires the lock internally.)
2777 vput(struct vnode *vp)
2780 vputx(vp, VPUTX_VPUT);
2784 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2787 vunref(struct vnode *vp)
2790 vputx(vp, VPUTX_VUNREF);
2794 * Increase the hold count and activate if this is the first reference.
2797 _vhold(struct vnode *vp, bool locked)
2802 ASSERT_VI_LOCKED(vp, __func__);
2804 ASSERT_VI_UNLOCKED(vp, __func__);
2805 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2807 if (refcount_acquire_if_not_zero(&vp->v_holdcnt)) {
2808 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2809 ("_vhold: vnode with holdcnt is free"));
2814 if ((vp->v_iflag & VI_FREE) == 0) {
2815 refcount_acquire(&vp->v_holdcnt);
2820 VNASSERT(vp->v_holdcnt == 0, vp,
2821 ("%s: wrong hold count", __func__));
2822 VNASSERT(vp->v_op != NULL, vp,
2823 ("%s: vnode already reclaimed.", __func__));
2825 * Remove a vnode from the free list, mark it as in use,
2826 * and put it on the active list.
2828 VNASSERT(vp->v_mount != NULL, vp,
2829 ("_vhold: vnode not on per mount vnode list"));
2831 mtx_lock(&mp->mnt_listmtx);
2832 if ((vp->v_mflag & VMP_TMPMNTFREELIST) != 0) {
2833 TAILQ_REMOVE(&mp->mnt_tmpfreevnodelist, vp, v_actfreelist);
2834 mp->mnt_tmpfreevnodelistsize--;
2835 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
2837 mtx_lock(&vnode_free_list_mtx);
2838 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2840 mtx_unlock(&vnode_free_list_mtx);
2842 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2843 ("Activating already active vnode"));
2844 vp->v_iflag &= ~VI_FREE;
2845 vp->v_iflag |= VI_ACTIVE;
2846 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2847 mp->mnt_activevnodelistsize++;
2848 mtx_unlock(&mp->mnt_listmtx);
2849 refcount_acquire(&vp->v_holdcnt);
2855 * Drop the hold count of the vnode. If this is the last reference to
2856 * the vnode we place it on the free list unless it has been vgone'd
2857 * (marked VI_DOOMED) in which case we will free it.
2859 * Because the vnode vm object keeps a hold reference on the vnode if
2860 * there is at least one resident non-cached page, the vnode cannot
2861 * leave the active list without the page cleanup done.
2864 _vdrop(struct vnode *vp, bool locked)
2871 ASSERT_VI_LOCKED(vp, __func__);
2873 ASSERT_VI_UNLOCKED(vp, __func__);
2874 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2875 if ((int)vp->v_holdcnt <= 0)
2876 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2878 if (refcount_release_if_not_last(&vp->v_holdcnt))
2882 if (refcount_release(&vp->v_holdcnt) == 0) {
2886 if ((vp->v_iflag & VI_DOOMED) == 0) {
2888 * Mark a vnode as free: remove it from its active list
2889 * and put it up for recycling on the freelist.
2891 VNASSERT(vp->v_op != NULL, vp,
2892 ("vdropl: vnode already reclaimed."));
2893 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2894 ("vnode already free"));
2895 VNASSERT(vp->v_holdcnt == 0, vp,
2896 ("vdropl: freeing when we shouldn't"));
2897 active = vp->v_iflag & VI_ACTIVE;
2898 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2899 vp->v_iflag &= ~VI_ACTIVE;
2902 mtx_lock(&mp->mnt_listmtx);
2904 TAILQ_REMOVE(&mp->mnt_activevnodelist,
2906 mp->mnt_activevnodelistsize--;
2908 TAILQ_INSERT_TAIL(&mp->mnt_tmpfreevnodelist,
2910 mp->mnt_tmpfreevnodelistsize++;
2911 vp->v_iflag |= VI_FREE;
2912 vp->v_mflag |= VMP_TMPMNTFREELIST;
2914 if (mp->mnt_tmpfreevnodelistsize >=
2915 mnt_free_list_batch)
2916 vnlru_return_batch_locked(mp);
2917 mtx_unlock(&mp->mnt_listmtx);
2919 VNASSERT(active == 0, vp,
2920 ("vdropl: active vnode not on per mount "
2922 mtx_lock(&vnode_free_list_mtx);
2923 TAILQ_INSERT_TAIL(&vnode_free_list, vp,
2926 vp->v_iflag |= VI_FREE;
2928 mtx_unlock(&vnode_free_list_mtx);
2932 counter_u64_add(free_owe_inact, 1);
2937 * The vnode has been marked for destruction, so free it.
2939 * The vnode will be returned to the zone where it will
2940 * normally remain until it is needed for another vnode. We
2941 * need to cleanup (or verify that the cleanup has already
2942 * been done) any residual data left from its current use
2943 * so as not to contaminate the freshly allocated vnode.
2945 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2946 atomic_subtract_long(&numvnodes, 1);
2948 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2949 ("cleaned vnode still on the free list."));
2950 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2951 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2952 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2953 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2954 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2955 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2956 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
2957 ("clean blk trie not empty"));
2958 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2959 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
2960 ("dirty blk trie not empty"));
2961 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2962 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2963 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2964 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp,
2965 ("Dangling rangelock waiters"));
2968 mac_vnode_destroy(vp);
2970 if (vp->v_pollinfo != NULL) {
2971 destroy_vpollinfo(vp->v_pollinfo);
2972 vp->v_pollinfo = NULL;
2975 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2978 vp->v_mountedhere = NULL;
2981 vp->v_fifoinfo = NULL;
2982 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
2986 uma_zfree(vnode_zone, vp);
2990 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2991 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2992 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2993 * failed lock upgrade.
2996 vinactive(struct vnode *vp, struct thread *td)
2998 struct vm_object *obj;
3000 ASSERT_VOP_ELOCKED(vp, "vinactive");
3001 ASSERT_VI_LOCKED(vp, "vinactive");
3002 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
3003 ("vinactive: recursed on VI_DOINGINACT"));
3004 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3005 vp->v_iflag |= VI_DOINGINACT;
3006 vp->v_iflag &= ~VI_OWEINACT;
3009 * Before moving off the active list, we must be sure that any
3010 * modified pages are converted into the vnode's dirty
3011 * buffers, since these will no longer be checked once the
3012 * vnode is on the inactive list.
3014 * The write-out of the dirty pages is asynchronous. At the
3015 * point that VOP_INACTIVE() is called, there could still be
3016 * pending I/O and dirty pages in the object.
3018 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
3019 (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
3020 VM_OBJECT_WLOCK(obj);
3021 vm_object_page_clean(obj, 0, 0, 0);
3022 VM_OBJECT_WUNLOCK(obj);
3024 VOP_INACTIVE(vp, td);
3026 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
3027 ("vinactive: lost VI_DOINGINACT"));
3028 vp->v_iflag &= ~VI_DOINGINACT;
3032 * Remove any vnodes in the vnode table belonging to mount point mp.
3034 * If FORCECLOSE is not specified, there should not be any active ones,
3035 * return error if any are found (nb: this is a user error, not a
3036 * system error). If FORCECLOSE is specified, detach any active vnodes
3039 * If WRITECLOSE is set, only flush out regular file vnodes open for
3042 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
3044 * `rootrefs' specifies the base reference count for the root vnode
3045 * of this filesystem. The root vnode is considered busy if its
3046 * v_usecount exceeds this value. On a successful return, vflush(, td)
3047 * will call vrele() on the root vnode exactly rootrefs times.
3048 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
3052 static int busyprt = 0; /* print out busy vnodes */
3053 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
3057 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
3059 struct vnode *vp, *mvp, *rootvp = NULL;
3061 int busy = 0, error;
3063 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
3066 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
3067 ("vflush: bad args"));
3069 * Get the filesystem root vnode. We can vput() it
3070 * immediately, since with rootrefs > 0, it won't go away.
3072 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
3073 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
3080 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
3082 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
3085 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3089 * Skip over a vnodes marked VV_SYSTEM.
3091 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
3097 * If WRITECLOSE is set, flush out unlinked but still open
3098 * files (even if open only for reading) and regular file
3099 * vnodes open for writing.
3101 if (flags & WRITECLOSE) {
3102 if (vp->v_object != NULL) {
3103 VM_OBJECT_WLOCK(vp->v_object);
3104 vm_object_page_clean(vp->v_object, 0, 0, 0);
3105 VM_OBJECT_WUNLOCK(vp->v_object);
3107 error = VOP_FSYNC(vp, MNT_WAIT, td);
3111 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3114 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3117 if ((vp->v_type == VNON ||
3118 (error == 0 && vattr.va_nlink > 0)) &&
3119 (vp->v_writecount == 0 || vp->v_type != VREG)) {
3127 * With v_usecount == 0, all we need to do is clear out the
3128 * vnode data structures and we are done.
3130 * If FORCECLOSE is set, forcibly close the vnode.
3132 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
3138 vn_printf(vp, "vflush: busy vnode ");
3144 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
3146 * If just the root vnode is busy, and if its refcount
3147 * is equal to `rootrefs', then go ahead and kill it.
3150 KASSERT(busy > 0, ("vflush: not busy"));
3151 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
3152 ("vflush: usecount %d < rootrefs %d",
3153 rootvp->v_usecount, rootrefs));
3154 if (busy == 1 && rootvp->v_usecount == rootrefs) {
3155 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
3157 VOP_UNLOCK(rootvp, 0);
3163 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
3167 for (; rootrefs > 0; rootrefs--)
3173 * Recycle an unused vnode to the front of the free list.
3176 vrecycle(struct vnode *vp)
3181 recycled = vrecyclel(vp);
3187 * vrecycle, with the vp interlock held.
3190 vrecyclel(struct vnode *vp)
3194 ASSERT_VOP_ELOCKED(vp, __func__);
3195 ASSERT_VI_LOCKED(vp, __func__);
3196 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3198 if (vp->v_usecount == 0) {
3206 * Eliminate all activity associated with a vnode
3207 * in preparation for reuse.
3210 vgone(struct vnode *vp)
3218 notify_lowervp_vfs_dummy(struct mount *mp __unused,
3219 struct vnode *lowervp __unused)
3224 * Notify upper mounts about reclaimed or unlinked vnode.
3227 vfs_notify_upper(struct vnode *vp, int event)
3229 static struct vfsops vgonel_vfsops = {
3230 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
3231 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
3233 struct mount *mp, *ump, *mmp;
3240 if (TAILQ_EMPTY(&mp->mnt_uppers))
3243 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
3244 mmp->mnt_op = &vgonel_vfsops;
3245 mmp->mnt_kern_flag |= MNTK_MARKER;
3247 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
3248 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
3249 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
3250 ump = TAILQ_NEXT(ump, mnt_upper_link);
3253 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
3256 case VFS_NOTIFY_UPPER_RECLAIM:
3257 VFS_RECLAIM_LOWERVP(ump, vp);
3259 case VFS_NOTIFY_UPPER_UNLINK:
3260 VFS_UNLINK_LOWERVP(ump, vp);
3263 KASSERT(0, ("invalid event %d", event));
3267 ump = TAILQ_NEXT(mmp, mnt_upper_link);
3268 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
3271 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
3272 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
3273 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
3274 wakeup(&mp->mnt_uppers);
3281 * vgone, with the vp interlock held.
3284 vgonel(struct vnode *vp)
3291 ASSERT_VOP_ELOCKED(vp, "vgonel");
3292 ASSERT_VI_LOCKED(vp, "vgonel");
3293 VNASSERT(vp->v_holdcnt, vp,
3294 ("vgonel: vp %p has no reference.", vp));
3295 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3299 * Don't vgonel if we're already doomed.
3301 if (vp->v_iflag & VI_DOOMED)
3303 vp->v_iflag |= VI_DOOMED;
3306 * Check to see if the vnode is in use. If so, we have to call
3307 * VOP_CLOSE() and VOP_INACTIVE().
3309 active = vp->v_usecount;
3310 oweinact = (vp->v_iflag & VI_OWEINACT);
3312 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
3315 * If purging an active vnode, it must be closed and
3316 * deactivated before being reclaimed.
3319 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
3320 if (oweinact || active) {
3322 if ((vp->v_iflag & VI_DOINGINACT) == 0)
3326 if (vp->v_type == VSOCK)
3327 vfs_unp_reclaim(vp);
3330 * Clean out any buffers associated with the vnode.
3331 * If the flush fails, just toss the buffers.
3334 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
3335 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
3336 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
3337 while (vinvalbuf(vp, 0, 0, 0) != 0)
3341 BO_LOCK(&vp->v_bufobj);
3342 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
3343 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3344 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
3345 vp->v_bufobj.bo_clean.bv_cnt == 0,
3346 ("vp %p bufobj not invalidated", vp));
3349 * For VMIO bufobj, BO_DEAD is set in vm_object_terminate()
3350 * after the object's page queue is flushed.
3352 if (vp->v_bufobj.bo_object == NULL)
3353 vp->v_bufobj.bo_flag |= BO_DEAD;
3354 BO_UNLOCK(&vp->v_bufobj);
3357 * Reclaim the vnode.
3359 if (VOP_RECLAIM(vp, td))
3360 panic("vgone: cannot reclaim");
3362 vn_finished_secondary_write(mp);
3363 VNASSERT(vp->v_object == NULL, vp,
3364 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
3366 * Clear the advisory locks and wake up waiting threads.
3368 (void)VOP_ADVLOCKPURGE(vp);
3371 * Delete from old mount point vnode list.
3376 * Done with purge, reset to the standard lock and invalidate
3380 vp->v_vnlock = &vp->v_lock;
3381 vp->v_op = &dead_vnodeops;
3387 * Calculate the total number of references to a special device.
3390 vcount(struct vnode *vp)
3395 count = vp->v_rdev->si_usecount;
3401 * Same as above, but using the struct cdev *as argument
3404 count_dev(struct cdev *dev)
3409 count = dev->si_usecount;
3415 * Print out a description of a vnode.
3417 static char *typename[] =
3418 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
3422 vn_printf(struct vnode *vp, const char *fmt, ...)
3425 char buf[256], buf2[16];
3431 printf("%p: ", (void *)vp);
3432 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
3433 printf(" usecount %d, writecount %d, refcount %d",
3434 vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
3435 switch (vp->v_type) {
3437 printf(" mountedhere %p\n", vp->v_mountedhere);
3440 printf(" rdev %p\n", vp->v_rdev);
3443 printf(" socket %p\n", vp->v_unpcb);
3446 printf(" fifoinfo %p\n", vp->v_fifoinfo);
3454 if (vp->v_vflag & VV_ROOT)
3455 strlcat(buf, "|VV_ROOT", sizeof(buf));
3456 if (vp->v_vflag & VV_ISTTY)
3457 strlcat(buf, "|VV_ISTTY", sizeof(buf));
3458 if (vp->v_vflag & VV_NOSYNC)
3459 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
3460 if (vp->v_vflag & VV_ETERNALDEV)
3461 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
3462 if (vp->v_vflag & VV_CACHEDLABEL)
3463 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
3464 if (vp->v_vflag & VV_TEXT)
3465 strlcat(buf, "|VV_TEXT", sizeof(buf));
3466 if (vp->v_vflag & VV_COPYONWRITE)
3467 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
3468 if (vp->v_vflag & VV_SYSTEM)
3469 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
3470 if (vp->v_vflag & VV_PROCDEP)
3471 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
3472 if (vp->v_vflag & VV_NOKNOTE)
3473 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
3474 if (vp->v_vflag & VV_DELETED)
3475 strlcat(buf, "|VV_DELETED", sizeof(buf));
3476 if (vp->v_vflag & VV_MD)
3477 strlcat(buf, "|VV_MD", sizeof(buf));
3478 if (vp->v_vflag & VV_FORCEINSMQ)
3479 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
3480 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
3481 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
3482 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
3484 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
3485 strlcat(buf, buf2, sizeof(buf));
3487 if (vp->v_iflag & VI_MOUNT)
3488 strlcat(buf, "|VI_MOUNT", sizeof(buf));
3489 if (vp->v_iflag & VI_DOOMED)
3490 strlcat(buf, "|VI_DOOMED", sizeof(buf));
3491 if (vp->v_iflag & VI_FREE)
3492 strlcat(buf, "|VI_FREE", sizeof(buf));
3493 if (vp->v_iflag & VI_ACTIVE)
3494 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3495 if (vp->v_iflag & VI_DOINGINACT)
3496 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3497 if (vp->v_iflag & VI_OWEINACT)
3498 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3499 flags = vp->v_iflag & ~(VI_MOUNT | VI_DOOMED | VI_FREE |
3500 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
3502 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3503 strlcat(buf, buf2, sizeof(buf));
3505 printf(" flags (%s)\n", buf + 1);
3506 if (mtx_owned(VI_MTX(vp)))
3507 printf(" VI_LOCKed");
3508 if (vp->v_object != NULL)
3509 printf(" v_object %p ref %d pages %d "
3510 "cleanbuf %d dirtybuf %d\n",
3511 vp->v_object, vp->v_object->ref_count,
3512 vp->v_object->resident_page_count,
3513 vp->v_bufobj.bo_clean.bv_cnt,
3514 vp->v_bufobj.bo_dirty.bv_cnt);
3516 lockmgr_printinfo(vp->v_vnlock);
3517 if (vp->v_data != NULL)
3523 * List all of the locked vnodes in the system.
3524 * Called when debugging the kernel.
3526 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3532 * Note: because this is DDB, we can't obey the locking semantics
3533 * for these structures, which means we could catch an inconsistent
3534 * state and dereference a nasty pointer. Not much to be done
3537 db_printf("Locked vnodes\n");
3538 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3539 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3540 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3541 vn_printf(vp, "vnode ");
3547 * Show details about the given vnode.
3549 DB_SHOW_COMMAND(vnode, db_show_vnode)
3555 vp = (struct vnode *)addr;
3556 vn_printf(vp, "vnode ");
3560 * Show details about the given mount point.
3562 DB_SHOW_COMMAND(mount, db_show_mount)
3573 /* No address given, print short info about all mount points. */
3574 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3575 db_printf("%p %s on %s (%s)\n", mp,
3576 mp->mnt_stat.f_mntfromname,
3577 mp->mnt_stat.f_mntonname,
3578 mp->mnt_stat.f_fstypename);
3582 db_printf("\nMore info: show mount <addr>\n");
3586 mp = (struct mount *)addr;
3587 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3588 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3591 mflags = mp->mnt_flag;
3592 #define MNT_FLAG(flag) do { \
3593 if (mflags & (flag)) { \
3594 if (buf[0] != '\0') \
3595 strlcat(buf, ", ", sizeof(buf)); \
3596 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3597 mflags &= ~(flag); \
3600 MNT_FLAG(MNT_RDONLY);
3601 MNT_FLAG(MNT_SYNCHRONOUS);
3602 MNT_FLAG(MNT_NOEXEC);
3603 MNT_FLAG(MNT_NOSUID);
3604 MNT_FLAG(MNT_NFS4ACLS);
3605 MNT_FLAG(MNT_UNION);
3606 MNT_FLAG(MNT_ASYNC);
3607 MNT_FLAG(MNT_SUIDDIR);
3608 MNT_FLAG(MNT_SOFTDEP);
3609 MNT_FLAG(MNT_NOSYMFOLLOW);
3610 MNT_FLAG(MNT_GJOURNAL);
3611 MNT_FLAG(MNT_MULTILABEL);
3613 MNT_FLAG(MNT_NOATIME);
3614 MNT_FLAG(MNT_NOCLUSTERR);
3615 MNT_FLAG(MNT_NOCLUSTERW);
3617 MNT_FLAG(MNT_EXRDONLY);
3618 MNT_FLAG(MNT_EXPORTED);
3619 MNT_FLAG(MNT_DEFEXPORTED);
3620 MNT_FLAG(MNT_EXPORTANON);
3621 MNT_FLAG(MNT_EXKERB);
3622 MNT_FLAG(MNT_EXPUBLIC);
3623 MNT_FLAG(MNT_LOCAL);
3624 MNT_FLAG(MNT_QUOTA);
3625 MNT_FLAG(MNT_ROOTFS);
3627 MNT_FLAG(MNT_IGNORE);
3628 MNT_FLAG(MNT_UPDATE);
3629 MNT_FLAG(MNT_DELEXPORT);
3630 MNT_FLAG(MNT_RELOAD);
3631 MNT_FLAG(MNT_FORCE);
3632 MNT_FLAG(MNT_SNAPSHOT);
3633 MNT_FLAG(MNT_BYFSID);
3637 strlcat(buf, ", ", sizeof(buf));
3638 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3639 "0x%016jx", mflags);
3641 db_printf(" mnt_flag = %s\n", buf);
3644 flags = mp->mnt_kern_flag;
3645 #define MNT_KERN_FLAG(flag) do { \
3646 if (flags & (flag)) { \
3647 if (buf[0] != '\0') \
3648 strlcat(buf, ", ", sizeof(buf)); \
3649 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3653 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3654 MNT_KERN_FLAG(MNTK_ASYNC);
3655 MNT_KERN_FLAG(MNTK_SOFTDEP);
3656 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3657 MNT_KERN_FLAG(MNTK_DRAINING);
3658 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3659 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3660 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3661 MNT_KERN_FLAG(MNTK_NO_IOPF);
3662 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3663 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3664 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3665 MNT_KERN_FLAG(MNTK_MARKER);
3666 MNT_KERN_FLAG(MNTK_USES_BCACHE);
3667 MNT_KERN_FLAG(MNTK_NOASYNC);
3668 MNT_KERN_FLAG(MNTK_UNMOUNT);
3669 MNT_KERN_FLAG(MNTK_MWAIT);
3670 MNT_KERN_FLAG(MNTK_SUSPEND);
3671 MNT_KERN_FLAG(MNTK_SUSPEND2);
3672 MNT_KERN_FLAG(MNTK_SUSPENDED);
3673 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3674 MNT_KERN_FLAG(MNTK_NOKNOTE);
3675 #undef MNT_KERN_FLAG
3678 strlcat(buf, ", ", sizeof(buf));
3679 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3682 db_printf(" mnt_kern_flag = %s\n", buf);
3684 db_printf(" mnt_opt = ");
3685 opt = TAILQ_FIRST(mp->mnt_opt);
3687 db_printf("%s", opt->name);
3688 opt = TAILQ_NEXT(opt, link);
3689 while (opt != NULL) {
3690 db_printf(", %s", opt->name);
3691 opt = TAILQ_NEXT(opt, link);
3697 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3698 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3699 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3700 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3701 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3702 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3703 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3704 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3705 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3706 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3707 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3708 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3710 db_printf(" mnt_cred = { uid=%u ruid=%u",
3711 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3712 if (jailed(mp->mnt_cred))
3713 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3715 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3716 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3717 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3718 db_printf(" mnt_activevnodelistsize = %d\n",
3719 mp->mnt_activevnodelistsize);
3720 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3721 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3722 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3723 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3724 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3725 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3726 db_printf(" mnt_secondary_accwrites = %d\n",
3727 mp->mnt_secondary_accwrites);
3728 db_printf(" mnt_gjprovider = %s\n",
3729 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3731 db_printf("\n\nList of active vnodes\n");
3732 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3733 if (vp->v_type != VMARKER) {
3734 vn_printf(vp, "vnode ");
3739 db_printf("\n\nList of inactive vnodes\n");
3740 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3741 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3742 vn_printf(vp, "vnode ");
3751 * Fill in a struct xvfsconf based on a struct vfsconf.
3754 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3756 struct xvfsconf xvfsp;
3758 bzero(&xvfsp, sizeof(xvfsp));
3759 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3760 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3761 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3762 xvfsp.vfc_flags = vfsp->vfc_flags;
3764 * These are unused in userland, we keep them
3765 * to not break binary compatibility.
3767 xvfsp.vfc_vfsops = NULL;
3768 xvfsp.vfc_next = NULL;
3769 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3772 #ifdef COMPAT_FREEBSD32
3774 uint32_t vfc_vfsops;
3775 char vfc_name[MFSNAMELEN];
3776 int32_t vfc_typenum;
3777 int32_t vfc_refcount;
3783 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3785 struct xvfsconf32 xvfsp;
3787 bzero(&xvfsp, sizeof(xvfsp));
3788 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3789 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3790 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3791 xvfsp.vfc_flags = vfsp->vfc_flags;
3792 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3797 * Top level filesystem related information gathering.
3800 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3802 struct vfsconf *vfsp;
3807 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3808 #ifdef COMPAT_FREEBSD32
3809 if (req->flags & SCTL_MASK32)
3810 error = vfsconf2x32(req, vfsp);
3813 error = vfsconf2x(req, vfsp);
3821 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3822 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3823 "S,xvfsconf", "List of all configured filesystems");
3825 #ifndef BURN_BRIDGES
3826 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3829 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3831 int *name = (int *)arg1 - 1; /* XXX */
3832 u_int namelen = arg2 + 1; /* XXX */
3833 struct vfsconf *vfsp;
3835 log(LOG_WARNING, "userland calling deprecated sysctl, "
3836 "please rebuild world\n");
3838 #if 1 || defined(COMPAT_PRELITE2)
3839 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3841 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3845 case VFS_MAXTYPENUM:
3848 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3851 return (ENOTDIR); /* overloaded */
3853 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3854 if (vfsp->vfc_typenum == name[2])
3859 return (EOPNOTSUPP);
3860 #ifdef COMPAT_FREEBSD32
3861 if (req->flags & SCTL_MASK32)
3862 return (vfsconf2x32(req, vfsp));
3865 return (vfsconf2x(req, vfsp));
3867 return (EOPNOTSUPP);
3870 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
3871 CTLFLAG_MPSAFE, vfs_sysctl,
3872 "Generic filesystem");
3874 #if 1 || defined(COMPAT_PRELITE2)
3877 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3880 struct vfsconf *vfsp;
3881 struct ovfsconf ovfs;
3884 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3885 bzero(&ovfs, sizeof(ovfs));
3886 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3887 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3888 ovfs.vfc_index = vfsp->vfc_typenum;
3889 ovfs.vfc_refcount = vfsp->vfc_refcount;
3890 ovfs.vfc_flags = vfsp->vfc_flags;
3891 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3901 #endif /* 1 || COMPAT_PRELITE2 */
3902 #endif /* !BURN_BRIDGES */
3904 #define KINFO_VNODESLOP 10
3907 * Dump vnode list (via sysctl).
3911 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3919 * Stale numvnodes access is not fatal here.
3922 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3924 /* Make an estimate */
3925 return (SYSCTL_OUT(req, 0, len));
3927 error = sysctl_wire_old_buffer(req, 0);
3930 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3932 mtx_lock(&mountlist_mtx);
3933 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3934 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3937 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3941 xvn[n].xv_size = sizeof *xvn;
3942 xvn[n].xv_vnode = vp;
3943 xvn[n].xv_id = 0; /* XXX compat */
3944 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3946 XV_COPY(writecount);
3952 xvn[n].xv_flag = vp->v_vflag;
3954 switch (vp->v_type) {
3961 if (vp->v_rdev == NULL) {
3965 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3968 xvn[n].xv_socket = vp->v_socket;
3971 xvn[n].xv_fifo = vp->v_fifoinfo;
3976 /* shouldn't happen? */
3984 mtx_lock(&mountlist_mtx);
3989 mtx_unlock(&mountlist_mtx);
3991 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3996 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
3997 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
4002 unmount_or_warn(struct mount *mp)
4006 error = dounmount(mp, MNT_FORCE, curthread);
4008 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
4012 printf("%d)\n", error);
4017 * Unmount all filesystems. The list is traversed in reverse order
4018 * of mounting to avoid dependencies.
4021 vfs_unmountall(void)
4023 struct mount *mp, *tmp;
4025 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
4028 * Since this only runs when rebooting, it is not interlocked.
4030 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
4034 * Forcibly unmounting "/dev" before "/" would prevent clean
4035 * unmount of the latter.
4037 if (mp == rootdevmp)
4040 unmount_or_warn(mp);
4043 if (rootdevmp != NULL)
4044 unmount_or_warn(rootdevmp);
4048 * perform msync on all vnodes under a mount point
4049 * the mount point must be locked.
4052 vfs_msync(struct mount *mp, int flags)
4054 struct vnode *vp, *mvp;
4055 struct vm_object *obj;
4057 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
4059 vnlru_return_batch(mp);
4061 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
4063 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
4064 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
4066 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
4068 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
4075 VM_OBJECT_WLOCK(obj);
4076 vm_object_page_clean(obj, 0, 0,
4078 OBJPC_SYNC : OBJPC_NOSYNC);
4079 VM_OBJECT_WUNLOCK(obj);
4089 destroy_vpollinfo_free(struct vpollinfo *vi)
4092 knlist_destroy(&vi->vpi_selinfo.si_note);
4093 mtx_destroy(&vi->vpi_lock);
4094 uma_zfree(vnodepoll_zone, vi);
4098 destroy_vpollinfo(struct vpollinfo *vi)
4101 knlist_clear(&vi->vpi_selinfo.si_note, 1);
4102 seldrain(&vi->vpi_selinfo);
4103 destroy_vpollinfo_free(vi);
4107 * Initialize per-vnode helper structure to hold poll-related state.
4110 v_addpollinfo(struct vnode *vp)
4112 struct vpollinfo *vi;
4114 if (vp->v_pollinfo != NULL)
4116 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
4117 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
4118 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
4119 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
4121 if (vp->v_pollinfo != NULL) {
4123 destroy_vpollinfo_free(vi);
4126 vp->v_pollinfo = vi;
4131 * Record a process's interest in events which might happen to
4132 * a vnode. Because poll uses the historic select-style interface
4133 * internally, this routine serves as both the ``check for any
4134 * pending events'' and the ``record my interest in future events''
4135 * functions. (These are done together, while the lock is held,
4136 * to avoid race conditions.)
4139 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
4143 mtx_lock(&vp->v_pollinfo->vpi_lock);
4144 if (vp->v_pollinfo->vpi_revents & events) {
4146 * This leaves events we are not interested
4147 * in available for the other process which
4148 * which presumably had requested them
4149 * (otherwise they would never have been
4152 events &= vp->v_pollinfo->vpi_revents;
4153 vp->v_pollinfo->vpi_revents &= ~events;
4155 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4158 vp->v_pollinfo->vpi_events |= events;
4159 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
4160 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4165 * Routine to create and manage a filesystem syncer vnode.
4167 #define sync_close ((int (*)(struct vop_close_args *))nullop)
4168 static int sync_fsync(struct vop_fsync_args *);
4169 static int sync_inactive(struct vop_inactive_args *);
4170 static int sync_reclaim(struct vop_reclaim_args *);
4172 static struct vop_vector sync_vnodeops = {
4173 .vop_bypass = VOP_EOPNOTSUPP,
4174 .vop_close = sync_close, /* close */
4175 .vop_fsync = sync_fsync, /* fsync */
4176 .vop_inactive = sync_inactive, /* inactive */
4177 .vop_reclaim = sync_reclaim, /* reclaim */
4178 .vop_lock1 = vop_stdlock, /* lock */
4179 .vop_unlock = vop_stdunlock, /* unlock */
4180 .vop_islocked = vop_stdislocked, /* islocked */
4184 * Create a new filesystem syncer vnode for the specified mount point.
4187 vfs_allocate_syncvnode(struct mount *mp)
4191 static long start, incr, next;
4194 /* Allocate a new vnode */
4195 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
4197 panic("vfs_allocate_syncvnode: getnewvnode() failed");
4199 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4200 vp->v_vflag |= VV_FORCEINSMQ;
4201 error = insmntque(vp, mp);
4203 panic("vfs_allocate_syncvnode: insmntque() failed");
4204 vp->v_vflag &= ~VV_FORCEINSMQ;
4207 * Place the vnode onto the syncer worklist. We attempt to
4208 * scatter them about on the list so that they will go off
4209 * at evenly distributed times even if all the filesystems
4210 * are mounted at once.
4213 if (next == 0 || next > syncer_maxdelay) {
4217 start = syncer_maxdelay / 2;
4218 incr = syncer_maxdelay;
4224 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
4225 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
4226 mtx_lock(&sync_mtx);
4228 if (mp->mnt_syncer == NULL) {
4229 mp->mnt_syncer = vp;
4232 mtx_unlock(&sync_mtx);
4235 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4242 vfs_deallocate_syncvnode(struct mount *mp)
4246 mtx_lock(&sync_mtx);
4247 vp = mp->mnt_syncer;
4249 mp->mnt_syncer = NULL;
4250 mtx_unlock(&sync_mtx);
4256 * Do a lazy sync of the filesystem.
4259 sync_fsync(struct vop_fsync_args *ap)
4261 struct vnode *syncvp = ap->a_vp;
4262 struct mount *mp = syncvp->v_mount;
4267 * We only need to do something if this is a lazy evaluation.
4269 if (ap->a_waitfor != MNT_LAZY)
4273 * Move ourselves to the back of the sync list.
4275 bo = &syncvp->v_bufobj;
4277 vn_syncer_add_to_worklist(bo, syncdelay);
4281 * Walk the list of vnodes pushing all that are dirty and
4282 * not already on the sync list.
4284 if (vfs_busy(mp, MBF_NOWAIT) != 0)
4286 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
4290 save = curthread_pflags_set(TDP_SYNCIO);
4291 vfs_msync(mp, MNT_NOWAIT);
4292 error = VFS_SYNC(mp, MNT_LAZY);
4293 curthread_pflags_restore(save);
4294 vn_finished_write(mp);
4300 * The syncer vnode is no referenced.
4303 sync_inactive(struct vop_inactive_args *ap)
4311 * The syncer vnode is no longer needed and is being decommissioned.
4313 * Modifications to the worklist must be protected by sync_mtx.
4316 sync_reclaim(struct vop_reclaim_args *ap)
4318 struct vnode *vp = ap->a_vp;
4323 mtx_lock(&sync_mtx);
4324 if (vp->v_mount->mnt_syncer == vp)
4325 vp->v_mount->mnt_syncer = NULL;
4326 if (bo->bo_flag & BO_ONWORKLST) {
4327 LIST_REMOVE(bo, bo_synclist);
4328 syncer_worklist_len--;
4330 bo->bo_flag &= ~BO_ONWORKLST;
4332 mtx_unlock(&sync_mtx);
4339 * Check if vnode represents a disk device
4342 vn_isdisk(struct vnode *vp, int *errp)
4346 if (vp->v_type != VCHR) {
4352 if (vp->v_rdev == NULL)
4354 else if (vp->v_rdev->si_devsw == NULL)
4356 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
4362 return (error == 0);
4366 * Common filesystem object access control check routine. Accepts a
4367 * vnode's type, "mode", uid and gid, requested access mode, credentials,
4368 * and optional call-by-reference privused argument allowing vaccess()
4369 * to indicate to the caller whether privilege was used to satisfy the
4370 * request (obsoleted). Returns 0 on success, or an errno on failure.
4373 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
4374 accmode_t accmode, struct ucred *cred, int *privused)
4376 accmode_t dac_granted;
4377 accmode_t priv_granted;
4379 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
4380 ("invalid bit in accmode"));
4381 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
4382 ("VAPPEND without VWRITE"));
4385 * Look for a normal, non-privileged way to access the file/directory
4386 * as requested. If it exists, go with that.
4389 if (privused != NULL)
4394 /* Check the owner. */
4395 if (cred->cr_uid == file_uid) {
4396 dac_granted |= VADMIN;
4397 if (file_mode & S_IXUSR)
4398 dac_granted |= VEXEC;
4399 if (file_mode & S_IRUSR)
4400 dac_granted |= VREAD;
4401 if (file_mode & S_IWUSR)
4402 dac_granted |= (VWRITE | VAPPEND);
4404 if ((accmode & dac_granted) == accmode)
4410 /* Otherwise, check the groups (first match) */
4411 if (groupmember(file_gid, cred)) {
4412 if (file_mode & S_IXGRP)
4413 dac_granted |= VEXEC;
4414 if (file_mode & S_IRGRP)
4415 dac_granted |= VREAD;
4416 if (file_mode & S_IWGRP)
4417 dac_granted |= (VWRITE | VAPPEND);
4419 if ((accmode & dac_granted) == accmode)
4425 /* Otherwise, check everyone else. */
4426 if (file_mode & S_IXOTH)
4427 dac_granted |= VEXEC;
4428 if (file_mode & S_IROTH)
4429 dac_granted |= VREAD;
4430 if (file_mode & S_IWOTH)
4431 dac_granted |= (VWRITE | VAPPEND);
4432 if ((accmode & dac_granted) == accmode)
4437 * Build a privilege mask to determine if the set of privileges
4438 * satisfies the requirements when combined with the granted mask
4439 * from above. For each privilege, if the privilege is required,
4440 * bitwise or the request type onto the priv_granted mask.
4446 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
4447 * requests, instead of PRIV_VFS_EXEC.
4449 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4450 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
4451 priv_granted |= VEXEC;
4454 * Ensure that at least one execute bit is on. Otherwise,
4455 * a privileged user will always succeed, and we don't want
4456 * this to happen unless the file really is executable.
4458 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4459 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
4460 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
4461 priv_granted |= VEXEC;
4464 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
4465 !priv_check_cred(cred, PRIV_VFS_READ, 0))
4466 priv_granted |= VREAD;
4468 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
4469 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
4470 priv_granted |= (VWRITE | VAPPEND);
4472 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
4473 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
4474 priv_granted |= VADMIN;
4476 if ((accmode & (priv_granted | dac_granted)) == accmode) {
4477 /* XXX audit: privilege used */
4478 if (privused != NULL)
4483 return ((accmode & VADMIN) ? EPERM : EACCES);
4487 * Credential check based on process requesting service, and per-attribute
4491 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
4492 struct thread *td, accmode_t accmode)
4496 * Kernel-invoked always succeeds.
4502 * Do not allow privileged processes in jail to directly manipulate
4503 * system attributes.
4505 switch (attrnamespace) {
4506 case EXTATTR_NAMESPACE_SYSTEM:
4507 /* Potentially should be: return (EPERM); */
4508 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
4509 case EXTATTR_NAMESPACE_USER:
4510 return (VOP_ACCESS(vp, accmode, cred, td));
4516 #ifdef DEBUG_VFS_LOCKS
4518 * This only exists to suppress warnings from unlocked specfs accesses. It is
4519 * no longer ok to have an unlocked VFS.
4521 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4522 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4524 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4525 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4526 "Drop into debugger on lock violation");
4528 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4529 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4530 0, "Check for interlock across VOPs");
4532 int vfs_badlock_print = 1; /* Print lock violations. */
4533 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4534 0, "Print lock violations");
4536 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */
4537 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode,
4538 0, "Print vnode details on lock violations");
4541 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4542 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4543 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4547 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4551 if (vfs_badlock_backtrace)
4554 if (vfs_badlock_vnode)
4555 vn_printf(vp, "vnode ");
4556 if (vfs_badlock_print)
4557 printf("%s: %p %s\n", str, (void *)vp, msg);
4558 if (vfs_badlock_ddb)
4559 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4563 assert_vi_locked(struct vnode *vp, const char *str)
4566 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4567 vfs_badlock("interlock is not locked but should be", str, vp);
4571 assert_vi_unlocked(struct vnode *vp, const char *str)
4574 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4575 vfs_badlock("interlock is locked but should not be", str, vp);
4579 assert_vop_locked(struct vnode *vp, const char *str)
4583 if (!IGNORE_LOCK(vp)) {
4584 locked = VOP_ISLOCKED(vp);
4585 if (locked == 0 || locked == LK_EXCLOTHER)
4586 vfs_badlock("is not locked but should be", str, vp);
4591 assert_vop_unlocked(struct vnode *vp, const char *str)
4594 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4595 vfs_badlock("is locked but should not be", str, vp);
4599 assert_vop_elocked(struct vnode *vp, const char *str)
4602 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4603 vfs_badlock("is not exclusive locked but should be", str, vp);
4605 #endif /* DEBUG_VFS_LOCKS */
4608 vop_rename_fail(struct vop_rename_args *ap)
4611 if (ap->a_tvp != NULL)
4613 if (ap->a_tdvp == ap->a_tvp)
4622 vop_rename_pre(void *ap)
4624 struct vop_rename_args *a = ap;
4626 #ifdef DEBUG_VFS_LOCKS
4628 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4629 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4630 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4631 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4633 /* Check the source (from). */
4634 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4635 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4636 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4637 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4638 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4640 /* Check the target. */
4642 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4643 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4645 if (a->a_tdvp != a->a_fdvp)
4647 if (a->a_tvp != a->a_fvp)
4654 #ifdef DEBUG_VFS_LOCKS
4656 vop_strategy_pre(void *ap)
4658 struct vop_strategy_args *a;
4665 * Cluster ops lock their component buffers but not the IO container.
4667 if ((bp->b_flags & B_CLUSTER) != 0)
4670 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4671 if (vfs_badlock_print)
4673 "VOP_STRATEGY: bp is not locked but should be\n");
4674 if (vfs_badlock_ddb)
4675 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4680 vop_lock_pre(void *ap)
4682 struct vop_lock1_args *a = ap;
4684 if ((a->a_flags & LK_INTERLOCK) == 0)
4685 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4687 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4691 vop_lock_post(void *ap, int rc)
4693 struct vop_lock1_args *a = ap;
4695 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4696 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4697 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4701 vop_unlock_pre(void *ap)
4703 struct vop_unlock_args *a = ap;
4705 if (a->a_flags & LK_INTERLOCK)
4706 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4707 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4711 vop_unlock_post(void *ap, int rc)
4713 struct vop_unlock_args *a = ap;
4715 if (a->a_flags & LK_INTERLOCK)
4716 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4721 vop_create_post(void *ap, int rc)
4723 struct vop_create_args *a = ap;
4726 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4730 vop_deleteextattr_post(void *ap, int rc)
4732 struct vop_deleteextattr_args *a = ap;
4735 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4739 vop_link_post(void *ap, int rc)
4741 struct vop_link_args *a = ap;
4744 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4745 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4750 vop_mkdir_post(void *ap, int rc)
4752 struct vop_mkdir_args *a = ap;
4755 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4759 vop_mknod_post(void *ap, int rc)
4761 struct vop_mknod_args *a = ap;
4764 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4768 vop_reclaim_post(void *ap, int rc)
4770 struct vop_reclaim_args *a = ap;
4773 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE);
4777 vop_remove_post(void *ap, int rc)
4779 struct vop_remove_args *a = ap;
4782 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4783 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4788 vop_rename_post(void *ap, int rc)
4790 struct vop_rename_args *a = ap;
4795 if (a->a_fdvp == a->a_tdvp) {
4796 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR)
4798 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
4799 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
4801 hint |= NOTE_EXTEND;
4802 if (a->a_fvp->v_type == VDIR)
4804 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
4806 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL &&
4807 a->a_tvp->v_type == VDIR)
4809 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
4812 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4814 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4816 if (a->a_tdvp != a->a_fdvp)
4818 if (a->a_tvp != a->a_fvp)
4826 vop_rmdir_post(void *ap, int rc)
4828 struct vop_rmdir_args *a = ap;
4831 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4832 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4837 vop_setattr_post(void *ap, int rc)
4839 struct vop_setattr_args *a = ap;
4842 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4846 vop_setextattr_post(void *ap, int rc)
4848 struct vop_setextattr_args *a = ap;
4851 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4855 vop_symlink_post(void *ap, int rc)
4857 struct vop_symlink_args *a = ap;
4860 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4864 vop_open_post(void *ap, int rc)
4866 struct vop_open_args *a = ap;
4869 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN);
4873 vop_close_post(void *ap, int rc)
4875 struct vop_close_args *a = ap;
4877 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */
4878 (a->a_vp->v_iflag & VI_DOOMED) == 0)) {
4879 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ?
4880 NOTE_CLOSE_WRITE : NOTE_CLOSE);
4885 vop_read_post(void *ap, int rc)
4887 struct vop_read_args *a = ap;
4890 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
4894 vop_readdir_post(void *ap, int rc)
4896 struct vop_readdir_args *a = ap;
4899 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
4902 static struct knlist fs_knlist;
4905 vfs_event_init(void *arg)
4907 knlist_init_mtx(&fs_knlist, NULL);
4909 /* XXX - correct order? */
4910 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4913 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4916 KNOTE_UNLOCKED(&fs_knlist, event);
4919 static int filt_fsattach(struct knote *kn);
4920 static void filt_fsdetach(struct knote *kn);
4921 static int filt_fsevent(struct knote *kn, long hint);
4923 struct filterops fs_filtops = {
4925 .f_attach = filt_fsattach,
4926 .f_detach = filt_fsdetach,
4927 .f_event = filt_fsevent
4931 filt_fsattach(struct knote *kn)
4934 kn->kn_flags |= EV_CLEAR;
4935 knlist_add(&fs_knlist, kn, 0);
4940 filt_fsdetach(struct knote *kn)
4943 knlist_remove(&fs_knlist, kn, 0);
4947 filt_fsevent(struct knote *kn, long hint)
4950 kn->kn_fflags |= hint;
4951 return (kn->kn_fflags != 0);
4955 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4961 error = SYSCTL_IN(req, &vc, sizeof(vc));
4964 if (vc.vc_vers != VFS_CTL_VERS1)
4966 mp = vfs_getvfs(&vc.vc_fsid);
4969 /* ensure that a specific sysctl goes to the right filesystem. */
4970 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4971 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4975 VCTLTOREQ(&vc, req);
4976 error = VFS_SYSCTL(mp, vc.vc_op, req);
4981 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4982 NULL, 0, sysctl_vfs_ctl, "",
4986 * Function to initialize a va_filerev field sensibly.
4987 * XXX: Wouldn't a random number make a lot more sense ??
4990 init_va_filerev(void)
4995 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4998 static int filt_vfsread(struct knote *kn, long hint);
4999 static int filt_vfswrite(struct knote *kn, long hint);
5000 static int filt_vfsvnode(struct knote *kn, long hint);
5001 static void filt_vfsdetach(struct knote *kn);
5002 static struct filterops vfsread_filtops = {
5004 .f_detach = filt_vfsdetach,
5005 .f_event = filt_vfsread
5007 static struct filterops vfswrite_filtops = {
5009 .f_detach = filt_vfsdetach,
5010 .f_event = filt_vfswrite
5012 static struct filterops vfsvnode_filtops = {
5014 .f_detach = filt_vfsdetach,
5015 .f_event = filt_vfsvnode
5019 vfs_knllock(void *arg)
5021 struct vnode *vp = arg;
5023 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5027 vfs_knlunlock(void *arg)
5029 struct vnode *vp = arg;
5035 vfs_knl_assert_locked(void *arg)
5037 #ifdef DEBUG_VFS_LOCKS
5038 struct vnode *vp = arg;
5040 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
5045 vfs_knl_assert_unlocked(void *arg)
5047 #ifdef DEBUG_VFS_LOCKS
5048 struct vnode *vp = arg;
5050 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
5055 vfs_kqfilter(struct vop_kqfilter_args *ap)
5057 struct vnode *vp = ap->a_vp;
5058 struct knote *kn = ap->a_kn;
5061 switch (kn->kn_filter) {
5063 kn->kn_fop = &vfsread_filtops;
5066 kn->kn_fop = &vfswrite_filtops;
5069 kn->kn_fop = &vfsvnode_filtops;
5075 kn->kn_hook = (caddr_t)vp;
5078 if (vp->v_pollinfo == NULL)
5080 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
5082 knlist_add(knl, kn, 0);
5088 * Detach knote from vnode
5091 filt_vfsdetach(struct knote *kn)
5093 struct vnode *vp = (struct vnode *)kn->kn_hook;
5095 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
5096 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
5102 filt_vfsread(struct knote *kn, long hint)
5104 struct vnode *vp = (struct vnode *)kn->kn_hook;
5109 * filesystem is gone, so set the EOF flag and schedule
5110 * the knote for deletion.
5112 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5114 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5119 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
5123 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
5124 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
5131 filt_vfswrite(struct knote *kn, long hint)
5133 struct vnode *vp = (struct vnode *)kn->kn_hook;
5138 * filesystem is gone, so set the EOF flag and schedule
5139 * the knote for deletion.
5141 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
5142 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5150 filt_vfsvnode(struct knote *kn, long hint)
5152 struct vnode *vp = (struct vnode *)kn->kn_hook;
5156 if (kn->kn_sfflags & hint)
5157 kn->kn_fflags |= hint;
5158 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5159 kn->kn_flags |= EV_EOF;
5163 res = (kn->kn_fflags != 0);
5169 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
5173 if (dp->d_reclen > ap->a_uio->uio_resid)
5174 return (ENAMETOOLONG);
5175 error = uiomove(dp, dp->d_reclen, ap->a_uio);
5177 if (ap->a_ncookies != NULL) {
5178 if (ap->a_cookies != NULL)
5179 free(ap->a_cookies, M_TEMP);
5180 ap->a_cookies = NULL;
5181 *ap->a_ncookies = 0;
5185 if (ap->a_ncookies == NULL)
5188 KASSERT(ap->a_cookies,
5189 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
5191 *ap->a_cookies = realloc(*ap->a_cookies,
5192 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
5193 (*ap->a_cookies)[*ap->a_ncookies] = off;
5194 *ap->a_ncookies += 1;
5199 * Mark for update the access time of the file if the filesystem
5200 * supports VOP_MARKATIME. This functionality is used by execve and
5201 * mmap, so we want to avoid the I/O implied by directly setting
5202 * va_atime for the sake of efficiency.
5205 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
5210 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
5211 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
5212 (void)VOP_MARKATIME(vp);
5216 * The purpose of this routine is to remove granularity from accmode_t,
5217 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
5218 * VADMIN and VAPPEND.
5220 * If it returns 0, the caller is supposed to continue with the usual
5221 * access checks using 'accmode' as modified by this routine. If it
5222 * returns nonzero value, the caller is supposed to return that value
5225 * Note that after this routine runs, accmode may be zero.
5228 vfs_unixify_accmode(accmode_t *accmode)
5231 * There is no way to specify explicit "deny" rule using
5232 * file mode or POSIX.1e ACLs.
5234 if (*accmode & VEXPLICIT_DENY) {
5240 * None of these can be translated into usual access bits.
5241 * Also, the common case for NFSv4 ACLs is to not contain
5242 * either of these bits. Caller should check for VWRITE
5243 * on the containing directory instead.
5245 if (*accmode & (VDELETE_CHILD | VDELETE))
5248 if (*accmode & VADMIN_PERMS) {
5249 *accmode &= ~VADMIN_PERMS;
5254 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
5255 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
5257 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
5263 * These are helper functions for filesystems to traverse all
5264 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
5266 * This interface replaces MNT_VNODE_FOREACH.
5269 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
5272 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
5277 kern_yield(PRI_USER);
5279 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5280 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL;
5281 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
5282 /* Allow a racy peek at VI_DOOMED to save a lock acquisition. */
5283 if (vp->v_type == VMARKER || (vp->v_iflag & VI_DOOMED) != 0)
5286 if ((vp->v_iflag & VI_DOOMED) != 0) {
5293 __mnt_vnode_markerfree_all(mvp, mp);
5294 /* MNT_IUNLOCK(mp); -- done in above function */
5295 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
5298 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
5299 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
5305 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
5309 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
5312 (*mvp)->v_mount = mp;
5313 (*mvp)->v_type = VMARKER;
5315 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
5316 /* Allow a racy peek at VI_DOOMED to save a lock acquisition. */
5317 if (vp->v_type == VMARKER || (vp->v_iflag & VI_DOOMED) != 0)
5320 if ((vp->v_iflag & VI_DOOMED) != 0) {
5329 free(*mvp, M_VNODE_MARKER);
5333 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
5339 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
5347 mtx_assert(MNT_MTX(mp), MA_OWNED);
5349 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5350 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
5353 free(*mvp, M_VNODE_MARKER);
5358 * These are helper functions for filesystems to traverse their
5359 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
5362 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
5365 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5370 free(*mvp, M_VNODE_MARKER);
5375 * Relock the mp mount vnode list lock with the vp vnode interlock in the
5376 * conventional lock order during mnt_vnode_next_active iteration.
5378 * On entry, the mount vnode list lock is held and the vnode interlock is not.
5379 * The list lock is dropped and reacquired. On success, both locks are held.
5380 * On failure, the mount vnode list lock is held but the vnode interlock is
5381 * not, and the procedure may have yielded.
5384 mnt_vnode_next_active_relock(struct vnode *mvp, struct mount *mp,
5387 const struct vnode *tmp;
5390 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER &&
5391 TAILQ_NEXT(mvp, v_actfreelist) != NULL, mvp,
5392 ("%s: bad marker", __func__));
5393 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp,
5394 ("%s: inappropriate vnode", __func__));
5395 ASSERT_VI_UNLOCKED(vp, __func__);
5396 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5400 TAILQ_REMOVE(&mp->mnt_activevnodelist, mvp, v_actfreelist);
5401 TAILQ_INSERT_BEFORE(vp, mvp, v_actfreelist);
5404 * Use a hold to prevent vp from disappearing while the mount vnode
5405 * list lock is dropped and reacquired. Normally a hold would be
5406 * acquired with vhold(), but that might try to acquire the vnode
5407 * interlock, which would be a LOR with the mount vnode list lock.
5409 held = refcount_acquire_if_not_zero(&vp->v_holdcnt);
5410 mtx_unlock(&mp->mnt_listmtx);
5414 if (!refcount_release_if_not_last(&vp->v_holdcnt)) {
5418 mtx_lock(&mp->mnt_listmtx);
5421 * Determine whether the vnode is still the next one after the marker,
5422 * excepting any other markers. If the vnode has not been doomed by
5423 * vgone() then the hold should have ensured that it remained on the
5424 * active list. If it has been doomed but is still on the active list,
5425 * don't abort, but rather skip over it (avoid spinning on doomed
5430 tmp = TAILQ_NEXT(tmp, v_actfreelist);
5431 } while (tmp != NULL && tmp->v_type == VMARKER);
5433 mtx_unlock(&mp->mnt_listmtx);
5442 mtx_lock(&mp->mnt_listmtx);
5445 ASSERT_VI_LOCKED(vp, __func__);
5447 ASSERT_VI_UNLOCKED(vp, __func__);
5448 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5452 static struct vnode *
5453 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
5455 struct vnode *vp, *nvp;
5457 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5458 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5460 vp = TAILQ_NEXT(*mvp, v_actfreelist);
5461 while (vp != NULL) {
5462 if (vp->v_type == VMARKER) {
5463 vp = TAILQ_NEXT(vp, v_actfreelist);
5467 * Try-lock because this is the wrong lock order. If that does
5468 * not succeed, drop the mount vnode list lock and try to
5469 * reacquire it and the vnode interlock in the right order.
5471 if (!VI_TRYLOCK(vp) &&
5472 !mnt_vnode_next_active_relock(*mvp, mp, vp))
5474 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
5475 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
5476 ("alien vnode on the active list %p %p", vp, mp));
5477 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
5479 nvp = TAILQ_NEXT(vp, v_actfreelist);
5483 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
5485 /* Check if we are done */
5487 mtx_unlock(&mp->mnt_listmtx);
5488 mnt_vnode_markerfree_active(mvp, mp);
5491 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
5492 mtx_unlock(&mp->mnt_listmtx);
5493 ASSERT_VI_LOCKED(vp, "active iter");
5494 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
5499 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
5503 kern_yield(PRI_USER);
5504 mtx_lock(&mp->mnt_listmtx);
5505 return (mnt_vnode_next_active(mvp, mp));
5509 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
5513 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
5517 (*mvp)->v_type = VMARKER;
5518 (*mvp)->v_mount = mp;
5520 mtx_lock(&mp->mnt_listmtx);
5521 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
5523 mtx_unlock(&mp->mnt_listmtx);
5524 mnt_vnode_markerfree_active(mvp, mp);
5527 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
5528 return (mnt_vnode_next_active(mvp, mp));
5532 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
5538 mtx_lock(&mp->mnt_listmtx);
5539 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
5540 mtx_unlock(&mp->mnt_listmtx);
5541 mnt_vnode_markerfree_active(mvp, mp);