2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
40 * External virtual filesystem routines
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 #include "opt_watchdog.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
53 #include <sys/capsicum.h>
54 #include <sys/condvar.h>
56 #include <sys/counter.h>
57 #include <sys/dirent.h>
58 #include <sys/event.h>
59 #include <sys/eventhandler.h>
60 #include <sys/extattr.h>
62 #include <sys/fcntl.h>
65 #include <sys/kernel.h>
66 #include <sys/kthread.h>
68 #include <sys/lockf.h>
69 #include <sys/malloc.h>
70 #include <sys/mount.h>
71 #include <sys/namei.h>
72 #include <sys/pctrie.h>
74 #include <sys/reboot.h>
75 #include <sys/refcount.h>
76 #include <sys/rwlock.h>
77 #include <sys/sched.h>
78 #include <sys/sleepqueue.h>
82 #include <sys/sysctl.h>
83 #include <sys/syslog.h>
84 #include <sys/vmmeter.h>
85 #include <sys/vnode.h>
86 #include <sys/watchdog.h>
88 #include <machine/stdarg.h>
90 #include <security/mac/mac_framework.h>
93 #include <vm/vm_object.h>
94 #include <vm/vm_extern.h>
96 #include <vm/vm_map.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_kern.h>
105 static void delmntque(struct vnode *vp);
106 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
107 int slpflag, int slptimeo);
108 static void syncer_shutdown(void *arg, int howto);
109 static int vtryrecycle(struct vnode *vp);
110 static void v_init_counters(struct vnode *);
111 static void vgonel(struct vnode *);
112 static bool vhold_recycle_free(struct vnode *);
113 static void vfs_knllock(void *arg);
114 static void vfs_knlunlock(void *arg);
115 static void vfs_knl_assert_lock(void *arg, int what);
116 static void destroy_vpollinfo(struct vpollinfo *vi);
117 static int v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
118 daddr_t startlbn, daddr_t endlbn);
119 static void vnlru_recalc(void);
122 * These fences are intended for cases where some synchronization is
123 * needed between access of v_iflags and lockless vnode refcount (v_holdcnt
124 * and v_usecount) updates. Access to v_iflags is generally synchronized
125 * by the interlock, but we have some internal assertions that check vnode
126 * flags without acquiring the lock. Thus, these fences are INVARIANTS-only
130 #define VNODE_REFCOUNT_FENCE_ACQ() atomic_thread_fence_acq()
131 #define VNODE_REFCOUNT_FENCE_REL() atomic_thread_fence_rel()
133 #define VNODE_REFCOUNT_FENCE_ACQ()
134 #define VNODE_REFCOUNT_FENCE_REL()
138 * Number of vnodes in existence. Increased whenever getnewvnode()
139 * allocates a new vnode, decreased in vdropl() for VIRF_DOOMED vnode.
141 static u_long __exclusive_cache_line numvnodes;
143 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
144 "Number of vnodes in existence");
146 static counter_u64_t vnodes_created;
147 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
148 "Number of vnodes created by getnewvnode");
151 * Conversion tables for conversion from vnode types to inode formats
154 enum vtype iftovt_tab[16] = {
155 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
156 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
158 int vttoif_tab[10] = {
159 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
160 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
164 * List of allocates vnodes in the system.
166 static TAILQ_HEAD(freelst, vnode) vnode_list;
167 static struct vnode *vnode_list_free_marker;
168 static struct vnode *vnode_list_reclaim_marker;
171 * "Free" vnode target. Free vnodes are rarely completely free, but are
172 * just ones that are cheap to recycle. Usually they are for files which
173 * have been stat'd but not read; these usually have inode and namecache
174 * data attached to them. This target is the preferred minimum size of a
175 * sub-cache consisting mostly of such files. The system balances the size
176 * of this sub-cache with its complement to try to prevent either from
177 * thrashing while the other is relatively inactive. The targets express
178 * a preference for the best balance.
180 * "Above" this target there are 2 further targets (watermarks) related
181 * to recyling of free vnodes. In the best-operating case, the cache is
182 * exactly full, the free list has size between vlowat and vhiwat above the
183 * free target, and recycling from it and normal use maintains this state.
184 * Sometimes the free list is below vlowat or even empty, but this state
185 * is even better for immediate use provided the cache is not full.
186 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free
187 * ones) to reach one of these states. The watermarks are currently hard-
188 * coded as 4% and 9% of the available space higher. These and the default
189 * of 25% for wantfreevnodes are too large if the memory size is large.
190 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim
191 * whenever vnlru_proc() becomes active.
193 static long wantfreevnodes;
194 static long __exclusive_cache_line freevnodes;
195 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD,
196 &freevnodes, 0, "Number of \"free\" vnodes");
197 static long freevnodes_old;
199 static counter_u64_t recycles_count;
200 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count,
201 "Number of vnodes recycled to meet vnode cache targets");
203 static counter_u64_t recycles_free_count;
204 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles_free, CTLFLAG_RD, &recycles_free_count,
205 "Number of free vnodes recycled to meet vnode cache targets");
207 static counter_u64_t deferred_inact;
208 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, deferred_inact, CTLFLAG_RD, &deferred_inact,
209 "Number of times inactive processing was deferred");
211 /* To keep more than one thread at a time from running vfs_getnewfsid */
212 static struct mtx mntid_mtx;
215 * Lock for any access to the following:
220 static struct mtx __exclusive_cache_line vnode_list_mtx;
222 /* Publicly exported FS */
223 struct nfs_public nfs_pub;
225 static uma_zone_t buf_trie_zone;
226 static smr_t buf_trie_smr;
228 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
229 static uma_zone_t vnode_zone;
230 MALLOC_DEFINE(M_VNODEPOLL, "VN POLL", "vnode poll");
232 __read_frequently smr_t vfs_smr;
235 * The workitem queue.
237 * It is useful to delay writes of file data and filesystem metadata
238 * for tens of seconds so that quickly created and deleted files need
239 * not waste disk bandwidth being created and removed. To realize this,
240 * we append vnodes to a "workitem" queue. When running with a soft
241 * updates implementation, most pending metadata dependencies should
242 * not wait for more than a few seconds. Thus, mounted on block devices
243 * are delayed only about a half the time that file data is delayed.
244 * Similarly, directory updates are more critical, so are only delayed
245 * about a third the time that file data is delayed. Thus, there are
246 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
247 * one each second (driven off the filesystem syncer process). The
248 * syncer_delayno variable indicates the next queue that is to be processed.
249 * Items that need to be processed soon are placed in this queue:
251 * syncer_workitem_pending[syncer_delayno]
253 * A delay of fifteen seconds is done by placing the request fifteen
254 * entries later in the queue:
256 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
259 static int syncer_delayno;
260 static long syncer_mask;
261 LIST_HEAD(synclist, bufobj);
262 static struct synclist *syncer_workitem_pending;
264 * The sync_mtx protects:
269 * syncer_workitem_pending
270 * syncer_worklist_len
273 static struct mtx sync_mtx;
274 static struct cv sync_wakeup;
276 #define SYNCER_MAXDELAY 32
277 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
278 static int syncdelay = 30; /* max time to delay syncing data */
279 static int filedelay = 30; /* time to delay syncing files */
280 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
281 "Time to delay syncing files (in seconds)");
282 static int dirdelay = 29; /* time to delay syncing directories */
283 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
284 "Time to delay syncing directories (in seconds)");
285 static int metadelay = 28; /* time to delay syncing metadata */
286 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
287 "Time to delay syncing metadata (in seconds)");
288 static int rushjob; /* number of slots to run ASAP */
289 static int stat_rush_requests; /* number of times I/O speeded up */
290 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
291 "Number of times I/O speeded up (rush requests)");
293 #define VDBATCH_SIZE 8
298 struct vnode *tab[VDBATCH_SIZE];
300 DPCPU_DEFINE_STATIC(struct vdbatch, vd);
302 static void vdbatch_dequeue(struct vnode *vp);
305 * When shutting down the syncer, run it at four times normal speed.
307 #define SYNCER_SHUTDOWN_SPEEDUP 4
308 static int sync_vnode_count;
309 static int syncer_worklist_len;
310 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
313 /* Target for maximum number of vnodes. */
314 u_long desiredvnodes;
315 static u_long gapvnodes; /* gap between wanted and desired */
316 static u_long vhiwat; /* enough extras after expansion */
317 static u_long vlowat; /* minimal extras before expansion */
318 static u_long vstir; /* nonzero to stir non-free vnodes */
319 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */
321 static u_long vnlru_read_freevnodes(void);
324 * Note that no attempt is made to sanitize these parameters.
327 sysctl_maxvnodes(SYSCTL_HANDLER_ARGS)
333 error = sysctl_handle_long(oidp, &val, 0, req);
334 if (error != 0 || req->newptr == NULL)
337 if (val == desiredvnodes)
339 mtx_lock(&vnode_list_mtx);
341 wantfreevnodes = desiredvnodes / 4;
343 mtx_unlock(&vnode_list_mtx);
345 * XXX There is no protection against multiple threads changing
346 * desiredvnodes at the same time. Locking above only helps vnlru and
349 vfs_hash_changesize(desiredvnodes);
350 cache_changesize(desiredvnodes);
354 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
355 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_maxvnodes,
356 "LU", "Target for maximum number of vnodes");
359 sysctl_wantfreevnodes(SYSCTL_HANDLER_ARGS)
364 val = wantfreevnodes;
365 error = sysctl_handle_long(oidp, &val, 0, req);
366 if (error != 0 || req->newptr == NULL)
369 if (val == wantfreevnodes)
371 mtx_lock(&vnode_list_mtx);
372 wantfreevnodes = val;
374 mtx_unlock(&vnode_list_mtx);
378 SYSCTL_PROC(_vfs, OID_AUTO, wantfreevnodes,
379 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_wantfreevnodes,
380 "LU", "Target for minimum number of \"free\" vnodes");
382 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
383 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)");
384 static int vnlru_nowhere;
385 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
386 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
389 sysctl_try_reclaim_vnode(SYSCTL_HANDLER_ARGS)
394 unsigned long ndflags;
397 if (req->newptr == NULL)
399 if (req->newlen >= PATH_MAX)
402 buf = malloc(PATH_MAX, M_TEMP, M_WAITOK);
403 error = SYSCTL_IN(req, buf, req->newlen);
407 buf[req->newlen] = '\0';
409 ndflags = LOCKLEAF | NOFOLLOW | AUDITVNODE1 | SAVENAME;
410 NDINIT(&nd, LOOKUP, ndflags, UIO_SYSSPACE, buf, curthread);
411 if ((error = namei(&nd)) != 0)
415 if (VN_IS_DOOMED(vp)) {
417 * This vnode is being recycled. Return != 0 to let the caller
418 * know that the sysctl had no effect. Return EAGAIN because a
419 * subsequent call will likely succeed (since namei will create
420 * a new vnode if necessary)
426 counter_u64_add(recycles_count, 1);
436 sysctl_ftry_reclaim_vnode(SYSCTL_HANDLER_ARGS)
438 struct thread *td = curthread;
444 if (req->newptr == NULL)
447 error = sysctl_handle_int(oidp, &fd, 0, req);
450 error = getvnode(curthread, fd, &cap_fcntl_rights, &fp);
455 error = vn_lock(vp, LK_EXCLUSIVE);
459 counter_u64_add(recycles_count, 1);
467 SYSCTL_PROC(_debug, OID_AUTO, try_reclaim_vnode,
468 CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
469 sysctl_try_reclaim_vnode, "A", "Try to reclaim a vnode by its pathname");
470 SYSCTL_PROC(_debug, OID_AUTO, ftry_reclaim_vnode,
471 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
472 sysctl_ftry_reclaim_vnode, "I",
473 "Try to reclaim a vnode by its file descriptor");
475 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
479 * Support for the bufobj clean & dirty pctrie.
482 buf_trie_alloc(struct pctrie *ptree)
484 return (uma_zalloc_smr(buf_trie_zone, M_NOWAIT));
488 buf_trie_free(struct pctrie *ptree, void *node)
490 uma_zfree_smr(buf_trie_zone, node);
492 PCTRIE_DEFINE_SMR(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free,
496 * Initialize the vnode management data structures.
498 * Reevaluate the following cap on the number of vnodes after the physical
499 * memory size exceeds 512GB. In the limit, as the physical memory size
500 * grows, the ratio of the memory size in KB to vnodes approaches 64:1.
502 #ifndef MAXVNODES_MAX
503 #define MAXVNODES_MAX (512UL * 1024 * 1024 / 64) /* 8M */
506 static MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
508 static struct vnode *
509 vn_alloc_marker(struct mount *mp)
513 vp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
514 vp->v_type = VMARKER;
521 vn_free_marker(struct vnode *vp)
524 MPASS(vp->v_type == VMARKER);
525 free(vp, M_VNODE_MARKER);
529 * Initialize a vnode as it first enters the zone.
532 vnode_init(void *mem, int size, int flags)
541 vp->v_vnlock = &vp->v_lock;
542 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
544 * By default, don't allow shared locks unless filesystems opt-in.
546 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT,
547 LK_NOSHARE | LK_IS_VNODE);
551 bufobj_init(&vp->v_bufobj, vp);
553 * Initialize namecache.
555 cache_vnode_init(vp);
557 * Initialize rangelocks.
559 rangelock_init(&vp->v_rl);
561 vp->v_dbatchcpu = NOCPU;
564 * Check vhold_recycle_free for an explanation.
566 vp->v_holdcnt = VHOLD_NO_SMR;
568 mtx_lock(&vnode_list_mtx);
569 TAILQ_INSERT_BEFORE(vnode_list_free_marker, vp, v_vnodelist);
570 mtx_unlock(&vnode_list_mtx);
575 * Free a vnode when it is cleared from the zone.
578 vnode_fini(void *mem, int size)
585 mtx_lock(&vnode_list_mtx);
586 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist);
587 mtx_unlock(&vnode_list_mtx);
588 rangelock_destroy(&vp->v_rl);
589 lockdestroy(vp->v_vnlock);
590 mtx_destroy(&vp->v_interlock);
592 rw_destroy(BO_LOCKPTR(bo));
596 * Provide the size of NFS nclnode and NFS fh for calculation of the
597 * vnode memory consumption. The size is specified directly to
598 * eliminate dependency on NFS-private header.
600 * Other filesystems may use bigger or smaller (like UFS and ZFS)
601 * private inode data, but the NFS-based estimation is ample enough.
602 * Still, we care about differences in the size between 64- and 32-bit
605 * Namecache structure size is heuristically
606 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1.
609 #define NFS_NCLNODE_SZ (528 + 64)
612 #define NFS_NCLNODE_SZ (360 + 32)
617 vntblinit(void *dummy __unused)
620 int cpu, physvnodes, virtvnodes;
624 * Desiredvnodes is a function of the physical memory size and the
625 * kernel's heap size. Generally speaking, it scales with the
626 * physical memory size. The ratio of desiredvnodes to the physical
627 * memory size is 1:16 until desiredvnodes exceeds 98,304.
629 * marginal ratio of desiredvnodes to the physical memory size is
630 * 1:64. However, desiredvnodes is limited by the kernel's heap
631 * size. The memory required by desiredvnodes vnodes and vm objects
632 * must not exceed 1/10th of the kernel's heap size.
634 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 +
635 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64;
636 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) +
637 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ));
638 desiredvnodes = min(physvnodes, virtvnodes);
639 if (desiredvnodes > MAXVNODES_MAX) {
641 printf("Reducing kern.maxvnodes %lu -> %lu\n",
642 desiredvnodes, MAXVNODES_MAX);
643 desiredvnodes = MAXVNODES_MAX;
645 wantfreevnodes = desiredvnodes / 4;
646 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
647 TAILQ_INIT(&vnode_list);
648 mtx_init(&vnode_list_mtx, "vnode_list", NULL, MTX_DEF);
650 * The lock is taken to appease WITNESS.
652 mtx_lock(&vnode_list_mtx);
654 mtx_unlock(&vnode_list_mtx);
655 vnode_list_free_marker = vn_alloc_marker(NULL);
656 TAILQ_INSERT_HEAD(&vnode_list, vnode_list_free_marker, v_vnodelist);
657 vnode_list_reclaim_marker = vn_alloc_marker(NULL);
658 TAILQ_INSERT_HEAD(&vnode_list, vnode_list_reclaim_marker, v_vnodelist);
659 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
660 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0);
661 uma_zone_set_smr(vnode_zone, vfs_smr);
663 * Preallocate enough nodes to support one-per buf so that
664 * we can not fail an insert. reassignbuf() callers can not
665 * tolerate the insertion failure.
667 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
668 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
669 UMA_ZONE_NOFREE | UMA_ZONE_SMR);
670 buf_trie_smr = uma_zone_get_smr(buf_trie_zone);
671 uma_prealloc(buf_trie_zone, nbuf);
673 vnodes_created = counter_u64_alloc(M_WAITOK);
674 recycles_count = counter_u64_alloc(M_WAITOK);
675 recycles_free_count = counter_u64_alloc(M_WAITOK);
676 deferred_inact = counter_u64_alloc(M_WAITOK);
679 * Initialize the filesystem syncer.
681 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
683 syncer_maxdelay = syncer_mask + 1;
684 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
685 cv_init(&sync_wakeup, "syncer");
686 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
691 vd = DPCPU_ID_PTR((cpu), vd);
692 bzero(vd, sizeof(*vd));
693 mtx_init(&vd->lock, "vdbatch", NULL, MTX_DEF);
696 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
699 * Mark a mount point as busy. Used to synchronize access and to delay
700 * unmounting. Eventually, mountlist_mtx is not released on failure.
702 * vfs_busy() is a custom lock, it can block the caller.
703 * vfs_busy() only sleeps if the unmount is active on the mount point.
704 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
705 * vnode belonging to mp.
707 * Lookup uses vfs_busy() to traverse mount points.
709 * / vnode lock A / vnode lock (/var) D
710 * /var vnode lock B /log vnode lock(/var/log) E
711 * vfs_busy lock C vfs_busy lock F
713 * Within each file system, the lock order is C->A->B and F->D->E.
715 * When traversing across mounts, the system follows that lock order:
721 * The lookup() process for namei("/var") illustrates the process:
722 * VOP_LOOKUP() obtains B while A is held
723 * vfs_busy() obtains a shared lock on F while A and B are held
724 * vput() releases lock on B
725 * vput() releases lock on A
726 * VFS_ROOT() obtains lock on D while shared lock on F is held
727 * vfs_unbusy() releases shared lock on F
728 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
729 * Attempt to lock A (instead of vp_crossmp) while D is held would
730 * violate the global order, causing deadlocks.
732 * dounmount() locks B while F is drained.
735 vfs_busy(struct mount *mp, int flags)
737 struct mount_pcpu *mpcpu;
739 MPASS((flags & ~MBF_MASK) == 0);
740 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
742 if (vfs_op_thread_enter(mp, mpcpu)) {
743 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
744 MPASS((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0);
745 MPASS((mp->mnt_kern_flag & MNTK_REFEXPIRE) == 0);
746 vfs_mp_count_add_pcpu(mpcpu, ref, 1);
747 vfs_mp_count_add_pcpu(mpcpu, lockref, 1);
748 vfs_op_thread_exit(mp, mpcpu);
749 if (flags & MBF_MNTLSTLOCK)
750 mtx_unlock(&mountlist_mtx);
755 vfs_assert_mount_counters(mp);
758 * If mount point is currently being unmounted, sleep until the
759 * mount point fate is decided. If thread doing the unmounting fails,
760 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
761 * that this mount point has survived the unmount attempt and vfs_busy
762 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
763 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
764 * about to be really destroyed. vfs_busy needs to release its
765 * reference on the mount point in this case and return with ENOENT,
766 * telling the caller that mount mount it tried to busy is no longer
769 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
770 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
773 CTR1(KTR_VFS, "%s: failed busying before sleeping",
777 if (flags & MBF_MNTLSTLOCK)
778 mtx_unlock(&mountlist_mtx);
779 mp->mnt_kern_flag |= MNTK_MWAIT;
780 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
781 if (flags & MBF_MNTLSTLOCK)
782 mtx_lock(&mountlist_mtx);
785 if (flags & MBF_MNTLSTLOCK)
786 mtx_unlock(&mountlist_mtx);
793 * Free a busy filesystem.
796 vfs_unbusy(struct mount *mp)
798 struct mount_pcpu *mpcpu;
801 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
803 if (vfs_op_thread_enter(mp, mpcpu)) {
804 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
805 vfs_mp_count_sub_pcpu(mpcpu, lockref, 1);
806 vfs_mp_count_sub_pcpu(mpcpu, ref, 1);
807 vfs_op_thread_exit(mp, mpcpu);
812 vfs_assert_mount_counters(mp);
814 c = --mp->mnt_lockref;
815 if (mp->mnt_vfs_ops == 0) {
816 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
821 vfs_dump_mount_counters(mp);
822 if (c == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
823 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
824 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
825 mp->mnt_kern_flag &= ~MNTK_DRAINING;
826 wakeup(&mp->mnt_lockref);
832 * Lookup a mount point by filesystem identifier.
835 vfs_getvfs(fsid_t *fsid)
839 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
840 mtx_lock(&mountlist_mtx);
841 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
842 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0) {
844 mtx_unlock(&mountlist_mtx);
848 mtx_unlock(&mountlist_mtx);
849 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
850 return ((struct mount *) 0);
854 * Lookup a mount point by filesystem identifier, busying it before
857 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
858 * cache for popular filesystem identifiers. The cache is lockess, using
859 * the fact that struct mount's are never freed. In worst case we may
860 * get pointer to unmounted or even different filesystem, so we have to
861 * check what we got, and go slow way if so.
864 vfs_busyfs(fsid_t *fsid)
866 #define FSID_CACHE_SIZE 256
867 typedef struct mount * volatile vmp_t;
868 static vmp_t cache[FSID_CACHE_SIZE];
873 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
874 hash = fsid->val[0] ^ fsid->val[1];
875 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
877 if (mp == NULL || fsidcmp(&mp->mnt_stat.f_fsid, fsid) != 0)
879 if (vfs_busy(mp, 0) != 0) {
883 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0)
889 mtx_lock(&mountlist_mtx);
890 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
891 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0) {
892 error = vfs_busy(mp, MBF_MNTLSTLOCK);
895 mtx_unlock(&mountlist_mtx);
902 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
903 mtx_unlock(&mountlist_mtx);
904 return ((struct mount *) 0);
908 * Check if a user can access privileged mount options.
911 vfs_suser(struct mount *mp, struct thread *td)
915 if (jailed(td->td_ucred)) {
917 * If the jail of the calling thread lacks permission for
918 * this type of file system, deny immediately.
920 if (!prison_allow(td->td_ucred, mp->mnt_vfc->vfc_prison_flag))
924 * If the file system was mounted outside the jail of the
925 * calling thread, deny immediately.
927 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
932 * If file system supports delegated administration, we don't check
933 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
934 * by the file system itself.
935 * If this is not the user that did original mount, we check for
936 * the PRIV_VFS_MOUNT_OWNER privilege.
938 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
939 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
940 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
947 * Get a new unique fsid. Try to make its val[0] unique, since this value
948 * will be used to create fake device numbers for stat(). Also try (but
949 * not so hard) make its val[0] unique mod 2^16, since some emulators only
950 * support 16-bit device numbers. We end up with unique val[0]'s for the
951 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
953 * Keep in mind that several mounts may be running in parallel. Starting
954 * the search one past where the previous search terminated is both a
955 * micro-optimization and a defense against returning the same fsid to
959 vfs_getnewfsid(struct mount *mp)
961 static uint16_t mntid_base;
966 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
967 mtx_lock(&mntid_mtx);
968 mtype = mp->mnt_vfc->vfc_typenum;
969 tfsid.val[1] = mtype;
970 mtype = (mtype & 0xFF) << 24;
972 tfsid.val[0] = makedev(255,
973 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
975 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
979 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
980 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
981 mtx_unlock(&mntid_mtx);
985 * Knob to control the precision of file timestamps:
987 * 0 = seconds only; nanoseconds zeroed.
988 * 1 = seconds and nanoseconds, accurate within 1/HZ.
989 * 2 = seconds and nanoseconds, truncated to microseconds.
990 * >=3 = seconds and nanoseconds, maximum precision.
992 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
994 static int timestamp_precision = TSP_USEC;
995 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
996 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
997 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, "
998 "3+: sec + ns (max. precision))");
1001 * Get a current timestamp.
1004 vfs_timestamp(struct timespec *tsp)
1008 switch (timestamp_precision) {
1010 tsp->tv_sec = time_second;
1018 TIMEVAL_TO_TIMESPEC(&tv, tsp);
1028 * Set vnode attributes to VNOVAL
1031 vattr_null(struct vattr *vap)
1034 vap->va_type = VNON;
1035 vap->va_size = VNOVAL;
1036 vap->va_bytes = VNOVAL;
1037 vap->va_mode = VNOVAL;
1038 vap->va_nlink = VNOVAL;
1039 vap->va_uid = VNOVAL;
1040 vap->va_gid = VNOVAL;
1041 vap->va_fsid = VNOVAL;
1042 vap->va_fileid = VNOVAL;
1043 vap->va_blocksize = VNOVAL;
1044 vap->va_rdev = VNOVAL;
1045 vap->va_atime.tv_sec = VNOVAL;
1046 vap->va_atime.tv_nsec = VNOVAL;
1047 vap->va_mtime.tv_sec = VNOVAL;
1048 vap->va_mtime.tv_nsec = VNOVAL;
1049 vap->va_ctime.tv_sec = VNOVAL;
1050 vap->va_ctime.tv_nsec = VNOVAL;
1051 vap->va_birthtime.tv_sec = VNOVAL;
1052 vap->va_birthtime.tv_nsec = VNOVAL;
1053 vap->va_flags = VNOVAL;
1054 vap->va_gen = VNOVAL;
1055 vap->va_vaflags = 0;
1059 * Try to reduce the total number of vnodes.
1061 * This routine (and its user) are buggy in at least the following ways:
1062 * - all parameters were picked years ago when RAM sizes were significantly
1064 * - it can pick vnodes based on pages used by the vm object, but filesystems
1065 * like ZFS don't use it making the pick broken
1066 * - since ZFS has its own aging policy it gets partially combated by this one
1067 * - a dedicated method should be provided for filesystems to let them decide
1068 * whether the vnode should be recycled
1070 * This routine is called when we have too many vnodes. It attempts
1071 * to free <count> vnodes and will potentially free vnodes that still
1072 * have VM backing store (VM backing store is typically the cause
1073 * of a vnode blowout so we want to do this). Therefore, this operation
1074 * is not considered cheap.
1076 * A number of conditions may prevent a vnode from being reclaimed.
1077 * the buffer cache may have references on the vnode, a directory
1078 * vnode may still have references due to the namei cache representing
1079 * underlying files, or the vnode may be in active use. It is not
1080 * desirable to reuse such vnodes. These conditions may cause the
1081 * number of vnodes to reach some minimum value regardless of what
1082 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
1084 * @param reclaim_nc_src Only reclaim directories with outgoing namecache
1085 * entries if this argument is strue
1086 * @param trigger Only reclaim vnodes with fewer than this many resident
1088 * @param target How many vnodes to reclaim.
1089 * @return The number of vnodes that were reclaimed.
1092 vlrureclaim(bool reclaim_nc_src, int trigger, u_long target)
1094 struct vnode *vp, *mvp;
1096 struct vm_object *object;
1100 mtx_assert(&vnode_list_mtx, MA_OWNED);
1105 mvp = vnode_list_reclaim_marker;
1108 while (done < target) {
1109 vp = TAILQ_NEXT(vp, v_vnodelist);
1110 if (__predict_false(vp == NULL))
1113 if (__predict_false(vp->v_type == VMARKER))
1117 * If it's been deconstructed already, it's still
1118 * referenced, or it exceeds the trigger, skip it.
1119 * Also skip free vnodes. We are trying to make space
1120 * to expand the free list, not reduce it.
1122 if (vp->v_usecount > 0 || vp->v_holdcnt == 0 ||
1123 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)))
1126 if (vp->v_type == VBAD || vp->v_type == VNON)
1129 object = atomic_load_ptr(&vp->v_object);
1130 if (object == NULL || object->resident_page_count > trigger) {
1135 * Handle races against vnode allocation. Filesystems lock the
1136 * vnode some time after it gets returned from getnewvnode,
1137 * despite type and hold count being manipulated earlier.
1138 * Resorting to checking v_mount restores guarantees present
1139 * before the global list was reworked to contain all vnodes.
1141 if (!VI_TRYLOCK(vp))
1143 if (__predict_false(vp->v_type == VBAD || vp->v_type == VNON)) {
1147 if (vp->v_mount == NULL) {
1153 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1154 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist);
1155 mtx_unlock(&vnode_list_mtx);
1157 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1159 goto next_iter_unlocked;
1161 if (VOP_LOCK(vp, LK_EXCLUSIVE|LK_NOWAIT) != 0) {
1163 vn_finished_write(mp);
1164 goto next_iter_unlocked;
1168 if (vp->v_usecount > 0 ||
1169 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
1170 (vp->v_object != NULL &&
1171 vp->v_object->resident_page_count > trigger)) {
1174 vn_finished_write(mp);
1175 goto next_iter_unlocked;
1177 counter_u64_add(recycles_count, 1);
1181 vn_finished_write(mp);
1185 kern_yield(PRI_USER);
1186 mtx_lock(&vnode_list_mtx);
1189 MPASS(vp->v_type != VMARKER);
1190 if (!should_yield())
1192 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1193 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist);
1194 mtx_unlock(&vnode_list_mtx);
1195 kern_yield(PRI_USER);
1196 mtx_lock(&vnode_list_mtx);
1199 if (done == 0 && !retried) {
1200 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1201 TAILQ_INSERT_HEAD(&vnode_list, mvp, v_vnodelist);
1208 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */
1209 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free,
1211 "limit on vnode free requests per call to the vnlru_free routine");
1214 * Attempt to reduce the free list by the requested amount.
1217 vnlru_free_locked(int count, struct vfsops *mnt_op)
1219 struct vnode *vp, *mvp;
1223 mtx_assert(&vnode_list_mtx, MA_OWNED);
1224 if (count > max_vnlru_free)
1225 count = max_vnlru_free;
1227 mvp = vnode_list_free_marker;
1233 vp = TAILQ_NEXT(vp, v_vnodelist);
1234 if (__predict_false(vp == NULL)) {
1235 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1236 TAILQ_INSERT_TAIL(&vnode_list, mvp, v_vnodelist);
1239 if (__predict_false(vp->v_type == VMARKER))
1241 if (vp->v_holdcnt > 0)
1244 * Don't recycle if our vnode is from different type
1245 * of mount point. Note that mp is type-safe, the
1246 * check does not reach unmapped address even if
1247 * vnode is reclaimed.
1249 if (mnt_op != NULL && (mp = vp->v_mount) != NULL &&
1250 mp->mnt_op != mnt_op) {
1253 if (__predict_false(vp->v_type == VBAD || vp->v_type == VNON)) {
1256 if (!vhold_recycle_free(vp))
1258 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1259 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist);
1260 mtx_unlock(&vnode_list_mtx);
1261 if (vtryrecycle(vp) == 0)
1263 mtx_lock(&vnode_list_mtx);
1266 return (ocount - count);
1270 vnlru_free(int count, struct vfsops *mnt_op)
1273 mtx_lock(&vnode_list_mtx);
1274 vnlru_free_locked(count, mnt_op);
1275 mtx_unlock(&vnode_list_mtx);
1282 mtx_assert(&vnode_list_mtx, MA_OWNED);
1283 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100);
1284 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */
1285 vlowat = vhiwat / 2;
1289 * Attempt to recycle vnodes in a context that is always safe to block.
1290 * Calling vlrurecycle() from the bowels of filesystem code has some
1291 * interesting deadlock problems.
1293 static struct proc *vnlruproc;
1294 static int vnlruproc_sig;
1297 * The main freevnodes counter is only updated when threads requeue their vnode
1298 * batches. CPUs are conditionally walked to compute a more accurate total.
1300 * Limit how much of a slop are we willing to tolerate. Note: the actual value
1301 * at any given moment can still exceed slop, but it should not be by significant
1302 * margin in practice.
1304 #define VNLRU_FREEVNODES_SLOP 128
1306 static __inline void
1307 vn_freevnodes_inc(void)
1317 static __inline void
1318 vn_freevnodes_dec(void)
1329 vnlru_read_freevnodes(void)
1335 mtx_assert(&vnode_list_mtx, MA_OWNED);
1336 if (freevnodes > freevnodes_old)
1337 slop = freevnodes - freevnodes_old;
1339 slop = freevnodes_old - freevnodes;
1340 if (slop < VNLRU_FREEVNODES_SLOP)
1341 return (freevnodes >= 0 ? freevnodes : 0);
1342 freevnodes_old = freevnodes;
1344 vd = DPCPU_ID_PTR((cpu), vd);
1345 freevnodes_old += vd->freevnodes;
1347 return (freevnodes_old >= 0 ? freevnodes_old : 0);
1351 vnlru_under(u_long rnumvnodes, u_long limit)
1353 u_long rfreevnodes, space;
1355 if (__predict_false(rnumvnodes > desiredvnodes))
1358 space = desiredvnodes - rnumvnodes;
1359 if (space < limit) {
1360 rfreevnodes = vnlru_read_freevnodes();
1361 if (rfreevnodes > wantfreevnodes)
1362 space += rfreevnodes - wantfreevnodes;
1364 return (space < limit);
1368 vnlru_under_unlocked(u_long rnumvnodes, u_long limit)
1370 long rfreevnodes, space;
1372 if (__predict_false(rnumvnodes > desiredvnodes))
1375 space = desiredvnodes - rnumvnodes;
1376 if (space < limit) {
1377 rfreevnodes = atomic_load_long(&freevnodes);
1378 if (rfreevnodes > wantfreevnodes)
1379 space += rfreevnodes - wantfreevnodes;
1381 return (space < limit);
1388 mtx_assert(&vnode_list_mtx, MA_OWNED);
1389 if (vnlruproc_sig == 0) {
1398 u_long rnumvnodes, rfreevnodes, target;
1399 unsigned long onumvnodes;
1400 int done, force, trigger, usevnodes;
1401 bool reclaim_nc_src, want_reread;
1403 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc,
1404 SHUTDOWN_PRI_FIRST);
1407 want_reread = false;
1409 kproc_suspend_check(vnlruproc);
1410 mtx_lock(&vnode_list_mtx);
1411 rnumvnodes = atomic_load_long(&numvnodes);
1414 force = vnlru_under(numvnodes, vhiwat) ? 1 : 0;
1415 want_reread = false;
1419 * If numvnodes is too large (due to desiredvnodes being
1420 * adjusted using its sysctl, or emergency growth), first
1421 * try to reduce it by discarding from the free list.
1423 if (rnumvnodes > desiredvnodes) {
1424 vnlru_free_locked(rnumvnodes - desiredvnodes, NULL);
1425 rnumvnodes = atomic_load_long(&numvnodes);
1428 * Sleep if the vnode cache is in a good state. This is
1429 * when it is not over-full and has space for about a 4%
1430 * or 9% expansion (by growing its size or inexcessively
1431 * reducing its free list). Otherwise, try to reclaim
1432 * space for a 10% expansion.
1434 if (vstir && force == 0) {
1438 if (force == 0 && !vnlru_under(rnumvnodes, vlowat)) {
1440 wakeup(&vnlruproc_sig);
1441 msleep(vnlruproc, &vnode_list_mtx,
1442 PVFS|PDROP, "vlruwt", hz);
1445 rfreevnodes = vnlru_read_freevnodes();
1447 onumvnodes = rnumvnodes;
1449 * Calculate parameters for recycling. These are the same
1450 * throughout the loop to give some semblance of fairness.
1451 * The trigger point is to avoid recycling vnodes with lots
1452 * of resident pages. We aren't trying to free memory; we
1453 * are trying to recycle or at least free vnodes.
1455 if (rnumvnodes <= desiredvnodes)
1456 usevnodes = rnumvnodes - rfreevnodes;
1458 usevnodes = rnumvnodes;
1462 * The trigger value is is chosen to give a conservatively
1463 * large value to ensure that it alone doesn't prevent
1464 * making progress. The value can easily be so large that
1465 * it is effectively infinite in some congested and
1466 * misconfigured cases, and this is necessary. Normally
1467 * it is about 8 to 100 (pages), which is quite large.
1469 trigger = vm_cnt.v_page_count * 2 / usevnodes;
1471 trigger = vsmalltrigger;
1472 reclaim_nc_src = force >= 3;
1473 target = rnumvnodes * (int64_t)gapvnodes / imax(desiredvnodes, 1);
1474 target = target / 10 + 1;
1475 done = vlrureclaim(reclaim_nc_src, trigger, target);
1476 mtx_unlock(&vnode_list_mtx);
1477 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes)
1478 uma_reclaim(UMA_RECLAIM_DRAIN);
1480 if (force == 0 || force == 1) {
1491 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
1494 kern_yield(PRI_USER);
1499 static struct kproc_desc vnlru_kp = {
1504 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
1508 * Routines having to do with the management of the vnode table.
1512 * Try to recycle a freed vnode. We abort if anyone picks up a reference
1513 * before we actually vgone(). This function must be called with the vnode
1514 * held to prevent the vnode from being returned to the free list midway
1518 vtryrecycle(struct vnode *vp)
1522 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
1523 VNASSERT(vp->v_holdcnt, vp,
1524 ("vtryrecycle: Recycling vp %p without a reference.", vp));
1526 * This vnode may found and locked via some other list, if so we
1527 * can't recycle it yet.
1529 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1531 "%s: impossible to recycle, vp %p lock is already held",
1534 return (EWOULDBLOCK);
1537 * Don't recycle if its filesystem is being suspended.
1539 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1542 "%s: impossible to recycle, cannot start the write for %p",
1548 * If we got this far, we need to acquire the interlock and see if
1549 * anyone picked up this vnode from another list. If not, we will
1550 * mark it with DOOMED via vgonel() so that anyone who does find it
1551 * will skip over it.
1554 if (vp->v_usecount) {
1557 vn_finished_write(vnmp);
1559 "%s: impossible to recycle, %p is already referenced",
1563 if (!VN_IS_DOOMED(vp)) {
1564 counter_u64_add(recycles_free_count, 1);
1569 vn_finished_write(vnmp);
1574 * Allocate a new vnode.
1576 * The operation never returns an error. Returning an error was disabled
1577 * in r145385 (dated 2005) with the following comment:
1579 * XXX Not all VFS_VGET/ffs_vget callers check returns.
1581 * Given the age of this commit (almost 15 years at the time of writing this
1582 * comment) restoring the ability to fail requires a significant audit of
1585 * The routine can try to free a vnode or stall for up to 1 second waiting for
1586 * vnlru to clear things up, but ultimately always performs a M_WAITOK allocation.
1588 static u_long vn_alloc_cyclecount;
1590 static struct vnode * __noinline
1591 vn_alloc_hard(struct mount *mp)
1593 u_long rnumvnodes, rfreevnodes;
1595 mtx_lock(&vnode_list_mtx);
1596 rnumvnodes = atomic_load_long(&numvnodes);
1597 if (rnumvnodes + 1 < desiredvnodes) {
1598 vn_alloc_cyclecount = 0;
1601 rfreevnodes = vnlru_read_freevnodes();
1602 if (vn_alloc_cyclecount++ >= rfreevnodes) {
1603 vn_alloc_cyclecount = 0;
1607 * Grow the vnode cache if it will not be above its target max
1608 * after growing. Otherwise, if the free list is nonempty, try
1609 * to reclaim 1 item from it before growing the cache (possibly
1610 * above its target max if the reclamation failed or is delayed).
1611 * Otherwise, wait for some space. In all cases, schedule
1612 * vnlru_proc() if we are getting short of space. The watermarks
1613 * should be chosen so that we never wait or even reclaim from
1614 * the free list to below its target minimum.
1616 if (vnlru_free_locked(1, NULL) > 0)
1618 if (mp == NULL || (mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
1620 * Wait for space for a new vnode.
1623 msleep(&vnlruproc_sig, &vnode_list_mtx, PVFS, "vlruwk", hz);
1624 if (atomic_load_long(&numvnodes) + 1 > desiredvnodes &&
1625 vnlru_read_freevnodes() > 1)
1626 vnlru_free_locked(1, NULL);
1629 rnumvnodes = atomic_fetchadd_long(&numvnodes, 1) + 1;
1630 if (vnlru_under(rnumvnodes, vlowat))
1632 mtx_unlock(&vnode_list_mtx);
1633 return (uma_zalloc_smr(vnode_zone, M_WAITOK));
1636 static struct vnode *
1637 vn_alloc(struct mount *mp)
1641 if (__predict_false(vn_alloc_cyclecount != 0))
1642 return (vn_alloc_hard(mp));
1643 rnumvnodes = atomic_fetchadd_long(&numvnodes, 1) + 1;
1644 if (__predict_false(vnlru_under_unlocked(rnumvnodes, vlowat))) {
1645 atomic_subtract_long(&numvnodes, 1);
1646 return (vn_alloc_hard(mp));
1649 return (uma_zalloc_smr(vnode_zone, M_WAITOK));
1653 vn_free(struct vnode *vp)
1656 atomic_subtract_long(&numvnodes, 1);
1657 uma_zfree_smr(vnode_zone, vp);
1661 * Return the next vnode from the free list.
1664 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1669 struct lock_object *lo;
1671 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1673 KASSERT(vops->registered,
1674 ("%s: not registered vector op %p\n", __func__, vops));
1677 if (td->td_vp_reserved != NULL) {
1678 vp = td->td_vp_reserved;
1679 td->td_vp_reserved = NULL;
1683 counter_u64_add(vnodes_created, 1);
1685 * Locks are given the generic name "vnode" when created.
1686 * Follow the historic practice of using the filesystem
1687 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc.
1689 * Locks live in a witness group keyed on their name. Thus,
1690 * when a lock is renamed, it must also move from the witness
1691 * group of its old name to the witness group of its new name.
1693 * The change only needs to be made when the vnode moves
1694 * from one filesystem type to another. We ensure that each
1695 * filesystem use a single static name pointer for its tag so
1696 * that we can compare pointers rather than doing a strcmp().
1698 lo = &vp->v_vnlock->lock_object;
1700 if (lo->lo_name != tag) {
1704 WITNESS_DESTROY(lo);
1705 WITNESS_INIT(lo, tag);
1709 * By default, don't allow shared locks unless filesystems opt-in.
1711 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE;
1713 * Finalize various vnode identity bits.
1715 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp));
1716 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp));
1717 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp));
1720 v_init_counters(vp);
1721 vp->v_bufobj.bo_ops = &buf_ops_bio;
1723 if (mp == NULL && vops != &dead_vnodeops)
1724 printf("NULL mp in getnewvnode(9), tag %s\n", tag);
1728 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1729 mac_vnode_associate_singlelabel(mp, vp);
1732 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize;
1733 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1734 vp->v_vflag |= VV_NOKNOTE;
1738 * For the filesystems which do not use vfs_hash_insert(),
1739 * still initialize v_hash to have vfs_hash_index() useful.
1740 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1743 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1750 getnewvnode_reserve(void)
1755 MPASS(td->td_vp_reserved == NULL);
1756 td->td_vp_reserved = vn_alloc(NULL);
1760 getnewvnode_drop_reserve(void)
1765 if (td->td_vp_reserved != NULL) {
1766 vn_free(td->td_vp_reserved);
1767 td->td_vp_reserved = NULL;
1771 static void __noinline
1772 freevnode(struct vnode *vp)
1777 * The vnode has been marked for destruction, so free it.
1779 * The vnode will be returned to the zone where it will
1780 * normally remain until it is needed for another vnode. We
1781 * need to cleanup (or verify that the cleanup has already
1782 * been done) any residual data left from its current use
1783 * so as not to contaminate the freshly allocated vnode.
1785 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
1787 * Paired with vgone.
1789 vn_seqc_write_end_locked(vp);
1790 VNPASS(vp->v_seqc_users == 0, vp);
1793 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
1794 VNPASS(vp->v_holdcnt == VHOLD_NO_SMR, vp);
1795 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
1796 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
1797 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
1798 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
1799 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
1800 ("clean blk trie not empty"));
1801 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
1802 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
1803 ("dirty blk trie not empty"));
1804 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
1805 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
1806 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
1807 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp,
1808 ("Dangling rangelock waiters"));
1809 VNASSERT((vp->v_iflag & (VI_DOINGINACT | VI_OWEINACT)) == 0, vp,
1810 ("Leaked inactivation"));
1813 mac_vnode_destroy(vp);
1815 if (vp->v_pollinfo != NULL) {
1816 destroy_vpollinfo(vp->v_pollinfo);
1817 vp->v_pollinfo = NULL;
1819 vp->v_mountedhere = NULL;
1822 vp->v_fifoinfo = NULL;
1823 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
1832 * Delete from old mount point vnode list, if on one.
1835 delmntque(struct vnode *vp)
1839 VNPASS((vp->v_mflag & VMP_LAZYLIST) == 0, vp);
1848 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1849 ("bad mount point vnode list size"));
1850 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1851 mp->mnt_nvnodelistsize--;
1857 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1861 vp->v_op = &dead_vnodeops;
1867 * Insert into list of vnodes for the new mount point, if available.
1870 insmntque1(struct vnode *vp, struct mount *mp,
1871 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1874 KASSERT(vp->v_mount == NULL,
1875 ("insmntque: vnode already on per mount vnode list"));
1876 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1877 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1880 * We acquire the vnode interlock early to ensure that the
1881 * vnode cannot be recycled by another process releasing a
1882 * holdcnt on it before we get it on both the vnode list
1883 * and the active vnode list. The mount mutex protects only
1884 * manipulation of the vnode list and the vnode freelist
1885 * mutex protects only manipulation of the active vnode list.
1886 * Hence the need to hold the vnode interlock throughout.
1890 if (((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 &&
1891 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1892 mp->mnt_nvnodelistsize == 0)) &&
1893 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1902 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1903 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1904 ("neg mount point vnode list size"));
1905 mp->mnt_nvnodelistsize++;
1912 insmntque(struct vnode *vp, struct mount *mp)
1915 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1919 * Flush out and invalidate all buffers associated with a bufobj
1920 * Called with the underlying object locked.
1923 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1928 if (flags & V_SAVE) {
1929 error = bufobj_wwait(bo, slpflag, slptimeo);
1934 if (bo->bo_dirty.bv_cnt > 0) {
1937 error = BO_SYNC(bo, MNT_WAIT);
1938 } while (error == ERELOOKUP);
1942 * XXX We could save a lock/unlock if this was only
1943 * enabled under INVARIANTS
1946 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1947 panic("vinvalbuf: dirty bufs");
1951 * If you alter this loop please notice that interlock is dropped and
1952 * reacquired in flushbuflist. Special care is needed to ensure that
1953 * no race conditions occur from this.
1956 error = flushbuflist(&bo->bo_clean,
1957 flags, bo, slpflag, slptimeo);
1958 if (error == 0 && !(flags & V_CLEANONLY))
1959 error = flushbuflist(&bo->bo_dirty,
1960 flags, bo, slpflag, slptimeo);
1961 if (error != 0 && error != EAGAIN) {
1965 } while (error != 0);
1968 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1969 * have write I/O in-progress but if there is a VM object then the
1970 * VM object can also have read-I/O in-progress.
1973 bufobj_wwait(bo, 0, 0);
1974 if ((flags & V_VMIO) == 0 && bo->bo_object != NULL) {
1976 vm_object_pip_wait_unlocked(bo->bo_object, "bovlbx");
1979 } while (bo->bo_numoutput > 0);
1983 * Destroy the copy in the VM cache, too.
1985 if (bo->bo_object != NULL &&
1986 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) {
1987 VM_OBJECT_WLOCK(bo->bo_object);
1988 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1989 OBJPR_CLEANONLY : 0);
1990 VM_OBJECT_WUNLOCK(bo->bo_object);
1995 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO |
1996 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 ||
1997 bo->bo_clean.bv_cnt > 0))
1998 panic("vinvalbuf: flush failed");
1999 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 &&
2000 bo->bo_dirty.bv_cnt > 0)
2001 panic("vinvalbuf: flush dirty failed");
2008 * Flush out and invalidate all buffers associated with a vnode.
2009 * Called with the underlying object locked.
2012 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
2015 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2016 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
2017 if (vp->v_object != NULL && vp->v_object->handle != vp)
2019 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
2023 * Flush out buffers on the specified list.
2027 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
2030 struct buf *bp, *nbp;
2035 ASSERT_BO_WLOCKED(bo);
2038 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
2040 * If we are flushing both V_NORMAL and V_ALT buffers then
2041 * do not skip any buffers. If we are flushing only V_NORMAL
2042 * buffers then skip buffers marked as BX_ALTDATA. If we are
2043 * flushing only V_ALT buffers then skip buffers not marked
2046 if (((flags & (V_NORMAL | V_ALT)) != (V_NORMAL | V_ALT)) &&
2047 (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA) != 0) ||
2048 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0))) {
2052 lblkno = nbp->b_lblkno;
2053 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
2056 error = BUF_TIMELOCK(bp,
2057 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
2058 "flushbuf", slpflag, slptimeo);
2061 return (error != ENOLCK ? error : EAGAIN);
2063 KASSERT(bp->b_bufobj == bo,
2064 ("bp %p wrong b_bufobj %p should be %p",
2065 bp, bp->b_bufobj, bo));
2067 * XXX Since there are no node locks for NFS, I
2068 * believe there is a slight chance that a delayed
2069 * write will occur while sleeping just above, so
2072 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
2075 bp->b_flags |= B_ASYNC;
2078 return (EAGAIN); /* XXX: why not loop ? */
2081 bp->b_flags |= (B_INVAL | B_RELBUF);
2082 bp->b_flags &= ~B_ASYNC;
2087 nbp = gbincore(bo, lblkno);
2088 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2090 break; /* nbp invalid */
2096 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn)
2102 ASSERT_BO_LOCKED(bo);
2104 for (lblkno = startn;;) {
2106 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno);
2107 if (bp == NULL || bp->b_lblkno >= endn ||
2108 bp->b_lblkno < startn)
2110 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
2111 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0);
2114 if (error == ENOLCK)
2118 KASSERT(bp->b_bufobj == bo,
2119 ("bp %p wrong b_bufobj %p should be %p",
2120 bp, bp->b_bufobj, bo));
2121 lblkno = bp->b_lblkno + 1;
2122 if ((bp->b_flags & B_MANAGED) == 0)
2124 bp->b_flags |= B_RELBUF;
2126 * In the VMIO case, use the B_NOREUSE flag to hint that the
2127 * pages backing each buffer in the range are unlikely to be
2128 * reused. Dirty buffers will have the hint applied once
2129 * they've been written.
2131 if ((bp->b_flags & B_VMIO) != 0)
2132 bp->b_flags |= B_NOREUSE;
2140 * Truncate a file's buffer and pages to a specified length. This
2141 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
2145 vtruncbuf(struct vnode *vp, off_t length, int blksize)
2147 struct buf *bp, *nbp;
2151 CTR4(KTR_VFS, "%s: vp %p with block %d:%ju", __func__,
2152 vp, blksize, (uintmax_t)length);
2155 * Round up to the *next* lbn.
2157 startlbn = howmany(length, blksize);
2159 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
2165 while (v_inval_buf_range_locked(vp, bo, startlbn, INT64_MAX) == EAGAIN)
2170 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2171 if (bp->b_lblkno > 0)
2174 * Since we hold the vnode lock this should only
2175 * fail if we're racing with the buf daemon.
2178 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2179 BO_LOCKPTR(bo)) == ENOLCK)
2180 goto restart_unlocked;
2182 VNASSERT((bp->b_flags & B_DELWRI), vp,
2183 ("buf(%p) on dirty queue without DELWRI", bp));
2192 bufobj_wwait(bo, 0, 0);
2194 vnode_pager_setsize(vp, length);
2200 * Invalidate the cached pages of a file's buffer within the range of block
2201 * numbers [startlbn, endlbn).
2204 v_inval_buf_range(struct vnode *vp, daddr_t startlbn, daddr_t endlbn,
2210 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range");
2212 start = blksize * startlbn;
2213 end = blksize * endlbn;
2217 MPASS(blksize == bo->bo_bsize);
2219 while (v_inval_buf_range_locked(vp, bo, startlbn, endlbn) == EAGAIN)
2223 vn_pages_remove(vp, OFF_TO_IDX(start), OFF_TO_IDX(end + PAGE_SIZE - 1));
2227 v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
2228 daddr_t startlbn, daddr_t endlbn)
2230 struct buf *bp, *nbp;
2233 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range_locked");
2234 ASSERT_BO_LOCKED(bo);
2238 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
2239 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2242 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2243 BO_LOCKPTR(bo)) == ENOLCK) {
2249 bp->b_flags |= B_INVAL | B_RELBUF;
2250 bp->b_flags &= ~B_ASYNC;
2256 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
2258 (nbp->b_flags & B_DELWRI) != 0))
2262 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2263 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2266 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2267 BO_LOCKPTR(bo)) == ENOLCK) {
2272 bp->b_flags |= B_INVAL | B_RELBUF;
2273 bp->b_flags &= ~B_ASYNC;
2279 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
2280 (nbp->b_vp != vp) ||
2281 (nbp->b_flags & B_DELWRI) == 0))
2289 buf_vlist_remove(struct buf *bp)
2294 flags = bp->b_xflags;
2296 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2297 ASSERT_BO_WLOCKED(bp->b_bufobj);
2298 KASSERT((flags & (BX_VNDIRTY | BX_VNCLEAN)) != 0 &&
2299 (flags & (BX_VNDIRTY | BX_VNCLEAN)) != (BX_VNDIRTY | BX_VNCLEAN),
2300 ("%s: buffer %p has invalid queue state", __func__, bp));
2302 if ((flags & BX_VNDIRTY) != 0)
2303 bv = &bp->b_bufobj->bo_dirty;
2305 bv = &bp->b_bufobj->bo_clean;
2306 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
2307 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
2309 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
2313 * Add the buffer to the sorted clean or dirty block list.
2315 * NOTE: xflags is passed as a constant, optimizing this inline function!
2318 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
2324 ASSERT_BO_WLOCKED(bo);
2325 KASSERT((bo->bo_flag & BO_NOBUFS) == 0,
2326 ("buf_vlist_add: bo %p does not allow bufs", bo));
2327 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
2328 ("dead bo %p", bo));
2329 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
2330 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
2331 bp->b_xflags |= xflags;
2332 if (xflags & BX_VNDIRTY)
2338 * Keep the list ordered. Optimize empty list insertion. Assume
2339 * we tend to grow at the tail so lookup_le should usually be cheaper
2342 if (bv->bv_cnt == 0 ||
2343 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
2344 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
2345 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
2346 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
2348 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
2349 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
2351 panic("buf_vlist_add: Preallocated nodes insufficient.");
2356 * Look up a buffer using the buffer tries.
2359 gbincore(struct bufobj *bo, daddr_t lblkno)
2363 ASSERT_BO_LOCKED(bo);
2364 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
2367 return (BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno));
2371 * Look up a buf using the buffer tries, without the bufobj lock. This relies
2372 * on SMR for safe lookup, and bufs being in a no-free zone to provide type
2373 * stability of the result. Like other lockless lookups, the found buf may
2374 * already be invalid by the time this function returns.
2377 gbincore_unlocked(struct bufobj *bo, daddr_t lblkno)
2381 ASSERT_BO_UNLOCKED(bo);
2382 bp = BUF_PCTRIE_LOOKUP_UNLOCKED(&bo->bo_clean.bv_root, lblkno);
2385 return (BUF_PCTRIE_LOOKUP_UNLOCKED(&bo->bo_dirty.bv_root, lblkno));
2389 * Associate a buffer with a vnode.
2392 bgetvp(struct vnode *vp, struct buf *bp)
2397 ASSERT_BO_WLOCKED(bo);
2398 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
2400 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
2401 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
2402 ("bgetvp: bp already attached! %p", bp));
2408 * Insert onto list for new vnode.
2410 buf_vlist_add(bp, bo, BX_VNCLEAN);
2414 * Disassociate a buffer from a vnode.
2417 brelvp(struct buf *bp)
2422 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2423 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
2426 * Delete from old vnode list, if on one.
2428 vp = bp->b_vp; /* XXX */
2431 buf_vlist_remove(bp);
2432 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2433 bo->bo_flag &= ~BO_ONWORKLST;
2434 mtx_lock(&sync_mtx);
2435 LIST_REMOVE(bo, bo_synclist);
2436 syncer_worklist_len--;
2437 mtx_unlock(&sync_mtx);
2440 bp->b_bufobj = NULL;
2446 * Add an item to the syncer work queue.
2449 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
2453 ASSERT_BO_WLOCKED(bo);
2455 mtx_lock(&sync_mtx);
2456 if (bo->bo_flag & BO_ONWORKLST)
2457 LIST_REMOVE(bo, bo_synclist);
2459 bo->bo_flag |= BO_ONWORKLST;
2460 syncer_worklist_len++;
2463 if (delay > syncer_maxdelay - 2)
2464 delay = syncer_maxdelay - 2;
2465 slot = (syncer_delayno + delay) & syncer_mask;
2467 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
2468 mtx_unlock(&sync_mtx);
2472 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
2476 mtx_lock(&sync_mtx);
2477 len = syncer_worklist_len - sync_vnode_count;
2478 mtx_unlock(&sync_mtx);
2479 error = SYSCTL_OUT(req, &len, sizeof(len));
2483 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len,
2484 CTLTYPE_INT | CTLFLAG_MPSAFE| CTLFLAG_RD, NULL, 0,
2485 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
2487 static struct proc *updateproc;
2488 static void sched_sync(void);
2489 static struct kproc_desc up_kp = {
2494 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
2497 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
2502 *bo = LIST_FIRST(slp);
2506 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
2509 * We use vhold in case the vnode does not
2510 * successfully sync. vhold prevents the vnode from
2511 * going away when we unlock the sync_mtx so that
2512 * we can acquire the vnode interlock.
2515 mtx_unlock(&sync_mtx);
2517 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2519 mtx_lock(&sync_mtx);
2520 return (*bo == LIST_FIRST(slp));
2522 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2523 (void) VOP_FSYNC(vp, MNT_LAZY, td);
2525 vn_finished_write(mp);
2527 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
2529 * Put us back on the worklist. The worklist
2530 * routine will remove us from our current
2531 * position and then add us back in at a later
2534 vn_syncer_add_to_worklist(*bo, syncdelay);
2538 mtx_lock(&sync_mtx);
2542 static int first_printf = 1;
2545 * System filesystem synchronizer daemon.
2550 struct synclist *next, *slp;
2553 struct thread *td = curthread;
2555 int net_worklist_len;
2556 int syncer_final_iter;
2560 syncer_final_iter = 0;
2561 syncer_state = SYNCER_RUNNING;
2562 starttime = time_uptime;
2563 td->td_pflags |= TDP_NORUNNINGBUF;
2565 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
2568 mtx_lock(&sync_mtx);
2570 if (syncer_state == SYNCER_FINAL_DELAY &&
2571 syncer_final_iter == 0) {
2572 mtx_unlock(&sync_mtx);
2573 kproc_suspend_check(td->td_proc);
2574 mtx_lock(&sync_mtx);
2576 net_worklist_len = syncer_worklist_len - sync_vnode_count;
2577 if (syncer_state != SYNCER_RUNNING &&
2578 starttime != time_uptime) {
2580 printf("\nSyncing disks, vnodes remaining... ");
2583 printf("%d ", net_worklist_len);
2585 starttime = time_uptime;
2588 * Push files whose dirty time has expired. Be careful
2589 * of interrupt race on slp queue.
2591 * Skip over empty worklist slots when shutting down.
2594 slp = &syncer_workitem_pending[syncer_delayno];
2595 syncer_delayno += 1;
2596 if (syncer_delayno == syncer_maxdelay)
2598 next = &syncer_workitem_pending[syncer_delayno];
2600 * If the worklist has wrapped since the
2601 * it was emptied of all but syncer vnodes,
2602 * switch to the FINAL_DELAY state and run
2603 * for one more second.
2605 if (syncer_state == SYNCER_SHUTTING_DOWN &&
2606 net_worklist_len == 0 &&
2607 last_work_seen == syncer_delayno) {
2608 syncer_state = SYNCER_FINAL_DELAY;
2609 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
2611 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
2612 syncer_worklist_len > 0);
2615 * Keep track of the last time there was anything
2616 * on the worklist other than syncer vnodes.
2617 * Return to the SHUTTING_DOWN state if any
2620 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
2621 last_work_seen = syncer_delayno;
2622 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
2623 syncer_state = SYNCER_SHUTTING_DOWN;
2624 while (!LIST_EMPTY(slp)) {
2625 error = sync_vnode(slp, &bo, td);
2627 LIST_REMOVE(bo, bo_synclist);
2628 LIST_INSERT_HEAD(next, bo, bo_synclist);
2632 if (first_printf == 0) {
2634 * Drop the sync mutex, because some watchdog
2635 * drivers need to sleep while patting
2637 mtx_unlock(&sync_mtx);
2638 wdog_kern_pat(WD_LASTVAL);
2639 mtx_lock(&sync_mtx);
2642 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
2643 syncer_final_iter--;
2645 * The variable rushjob allows the kernel to speed up the
2646 * processing of the filesystem syncer process. A rushjob
2647 * value of N tells the filesystem syncer to process the next
2648 * N seconds worth of work on its queue ASAP. Currently rushjob
2649 * is used by the soft update code to speed up the filesystem
2650 * syncer process when the incore state is getting so far
2651 * ahead of the disk that the kernel memory pool is being
2652 * threatened with exhaustion.
2659 * Just sleep for a short period of time between
2660 * iterations when shutting down to allow some I/O
2663 * If it has taken us less than a second to process the
2664 * current work, then wait. Otherwise start right over
2665 * again. We can still lose time if any single round
2666 * takes more than two seconds, but it does not really
2667 * matter as we are just trying to generally pace the
2668 * filesystem activity.
2670 if (syncer_state != SYNCER_RUNNING ||
2671 time_uptime == starttime) {
2673 sched_prio(td, PPAUSE);
2676 if (syncer_state != SYNCER_RUNNING)
2677 cv_timedwait(&sync_wakeup, &sync_mtx,
2678 hz / SYNCER_SHUTDOWN_SPEEDUP);
2679 else if (time_uptime == starttime)
2680 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2685 * Request the syncer daemon to speed up its work.
2686 * We never push it to speed up more than half of its
2687 * normal turn time, otherwise it could take over the cpu.
2690 speedup_syncer(void)
2694 mtx_lock(&sync_mtx);
2695 if (rushjob < syncdelay / 2) {
2697 stat_rush_requests += 1;
2700 mtx_unlock(&sync_mtx);
2701 cv_broadcast(&sync_wakeup);
2706 * Tell the syncer to speed up its work and run though its work
2707 * list several times, then tell it to shut down.
2710 syncer_shutdown(void *arg, int howto)
2713 if (howto & RB_NOSYNC)
2715 mtx_lock(&sync_mtx);
2716 syncer_state = SYNCER_SHUTTING_DOWN;
2718 mtx_unlock(&sync_mtx);
2719 cv_broadcast(&sync_wakeup);
2720 kproc_shutdown(arg, howto);
2724 syncer_suspend(void)
2727 syncer_shutdown(updateproc, 0);
2734 mtx_lock(&sync_mtx);
2736 syncer_state = SYNCER_RUNNING;
2737 mtx_unlock(&sync_mtx);
2738 cv_broadcast(&sync_wakeup);
2739 kproc_resume(updateproc);
2743 * Move the buffer between the clean and dirty lists of its vnode.
2746 reassignbuf(struct buf *bp)
2758 KASSERT((bp->b_flags & B_PAGING) == 0,
2759 ("%s: cannot reassign paging buffer %p", __func__, bp));
2761 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2762 bp, bp->b_vp, bp->b_flags);
2765 buf_vlist_remove(bp);
2768 * If dirty, put on list of dirty buffers; otherwise insert onto list
2771 if (bp->b_flags & B_DELWRI) {
2772 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2773 switch (vp->v_type) {
2783 vn_syncer_add_to_worklist(bo, delay);
2785 buf_vlist_add(bp, bo, BX_VNDIRTY);
2787 buf_vlist_add(bp, bo, BX_VNCLEAN);
2789 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2790 mtx_lock(&sync_mtx);
2791 LIST_REMOVE(bo, bo_synclist);
2792 syncer_worklist_len--;
2793 mtx_unlock(&sync_mtx);
2794 bo->bo_flag &= ~BO_ONWORKLST;
2799 bp = TAILQ_FIRST(&bv->bv_hd);
2800 KASSERT(bp == NULL || bp->b_bufobj == bo,
2801 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2802 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2803 KASSERT(bp == NULL || bp->b_bufobj == bo,
2804 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2806 bp = TAILQ_FIRST(&bv->bv_hd);
2807 KASSERT(bp == NULL || bp->b_bufobj == bo,
2808 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2809 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2810 KASSERT(bp == NULL || bp->b_bufobj == bo,
2811 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2817 v_init_counters(struct vnode *vp)
2820 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2821 vp, ("%s called for an initialized vnode", __FUNCTION__));
2822 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2824 refcount_init(&vp->v_holdcnt, 1);
2825 refcount_init(&vp->v_usecount, 1);
2829 * Grab a particular vnode from the free list, increment its
2830 * reference count and lock it. VIRF_DOOMED is set if the vnode
2831 * is being destroyed. Only callers who specify LK_RETRY will
2832 * see doomed vnodes. If inactive processing was delayed in
2833 * vput try to do it here.
2835 * usecount is manipulated using atomics without holding any locks.
2837 * holdcnt can be manipulated using atomics without holding any locks,
2838 * except when transitioning 1<->0, in which case the interlock is held.
2840 * Consumers which don't guarantee liveness of the vnode can use SMR to
2841 * try to get a reference. Note this operation can fail since the vnode
2842 * may be awaiting getting freed by the time they get to it.
2845 vget_prep_smr(struct vnode *vp)
2849 VFS_SMR_ASSERT_ENTERED();
2851 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2863 vget_prep(struct vnode *vp)
2867 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2877 vget_abort(struct vnode *vp, enum vgetstate vs)
2888 __assert_unreachable();
2893 vget(struct vnode *vp, int flags)
2898 return (vget_finish(vp, flags, vs));
2902 vget_finish(struct vnode *vp, int flags, enum vgetstate vs)
2906 if ((flags & LK_INTERLOCK) != 0)
2907 ASSERT_VI_LOCKED(vp, __func__);
2909 ASSERT_VI_UNLOCKED(vp, __func__);
2910 VNPASS(vs == VGET_HOLDCNT || vs == VGET_USECOUNT, vp);
2911 VNPASS(vp->v_holdcnt > 0, vp);
2912 VNPASS(vs == VGET_HOLDCNT || vp->v_usecount > 0, vp);
2914 error = vn_lock(vp, flags);
2915 if (__predict_false(error != 0)) {
2917 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2922 vget_finish_ref(vp, vs);
2927 vget_finish_ref(struct vnode *vp, enum vgetstate vs)
2931 VNPASS(vs == VGET_HOLDCNT || vs == VGET_USECOUNT, vp);
2932 VNPASS(vp->v_holdcnt > 0, vp);
2933 VNPASS(vs == VGET_HOLDCNT || vp->v_usecount > 0, vp);
2935 if (vs == VGET_USECOUNT)
2939 * We hold the vnode. If the usecount is 0 it will be utilized to keep
2940 * the vnode around. Otherwise someone else lended their hold count and
2941 * we have to drop ours.
2943 old = atomic_fetchadd_int(&vp->v_usecount, 1);
2944 VNASSERT(old >= 0, vp, ("%s: wrong use count %d", __func__, old));
2947 old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
2948 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old));
2950 refcount_release(&vp->v_holdcnt);
2956 vref(struct vnode *vp)
2960 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2962 vget_finish_ref(vp, vs);
2966 vrefact(struct vnode *vp)
2969 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2971 int old = atomic_fetchadd_int(&vp->v_usecount, 1);
2972 VNASSERT(old > 0, vp, ("%s: wrong use count %d", __func__, old));
2974 refcount_acquire(&vp->v_usecount);
2979 vlazy(struct vnode *vp)
2983 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__));
2985 if ((vp->v_mflag & VMP_LAZYLIST) != 0)
2988 * We may get here for inactive routines after the vnode got doomed.
2990 if (VN_IS_DOOMED(vp))
2993 mtx_lock(&mp->mnt_listmtx);
2994 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
2995 vp->v_mflag |= VMP_LAZYLIST;
2996 TAILQ_INSERT_TAIL(&mp->mnt_lazyvnodelist, vp, v_lazylist);
2997 mp->mnt_lazyvnodelistsize++;
2999 mtx_unlock(&mp->mnt_listmtx);
3003 * This routine is only meant to be called from vgonel prior to dooming
3007 vunlazy_gone(struct vnode *vp)
3011 ASSERT_VOP_ELOCKED(vp, __func__);
3012 ASSERT_VI_LOCKED(vp, __func__);
3013 VNPASS(!VN_IS_DOOMED(vp), vp);
3015 if (vp->v_mflag & VMP_LAZYLIST) {
3017 mtx_lock(&mp->mnt_listmtx);
3018 VNPASS(vp->v_mflag & VMP_LAZYLIST, vp);
3019 vp->v_mflag &= ~VMP_LAZYLIST;
3020 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3021 mp->mnt_lazyvnodelistsize--;
3022 mtx_unlock(&mp->mnt_listmtx);
3027 vdefer_inactive(struct vnode *vp)
3030 ASSERT_VI_LOCKED(vp, __func__);
3031 VNASSERT(vp->v_holdcnt > 0, vp,
3032 ("%s: vnode without hold count", __func__));
3033 if (VN_IS_DOOMED(vp)) {
3037 if (vp->v_iflag & VI_DEFINACT) {
3038 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count"));
3042 if (vp->v_usecount > 0) {
3043 vp->v_iflag &= ~VI_OWEINACT;
3048 vp->v_iflag |= VI_DEFINACT;
3050 counter_u64_add(deferred_inact, 1);
3054 vdefer_inactive_unlocked(struct vnode *vp)
3058 if ((vp->v_iflag & VI_OWEINACT) == 0) {
3062 vdefer_inactive(vp);
3065 enum vput_op { VRELE, VPUT, VUNREF };
3068 * Handle ->v_usecount transitioning to 0.
3070 * By releasing the last usecount we take ownership of the hold count which
3071 * provides liveness of the vnode, meaning we have to vdrop.
3073 * For all vnodes we may need to perform inactive processing. It requires an
3074 * exclusive lock on the vnode, while it is legal to call here with only a
3075 * shared lock (or no locks). If locking the vnode in an expected manner fails,
3076 * inactive processing gets deferred to the syncer.
3078 * XXX Some filesystems pass in an exclusively locked vnode and strongly depend
3079 * on the lock being held all the way until VOP_INACTIVE. This in particular
3080 * happens with UFS which adds half-constructed vnodes to the hash, where they
3081 * can be found by other code.
3084 vput_final(struct vnode *vp, enum vput_op func)
3089 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3090 VNPASS(vp->v_holdcnt > 0, vp);
3095 * By the time we got here someone else might have transitioned
3096 * the count back to > 0.
3098 if (vp->v_usecount > 0)
3102 * If the vnode is doomed vgone already performed inactive processing
3105 if (VN_IS_DOOMED(vp))
3108 if (__predict_true(VOP_NEED_INACTIVE(vp) == 0))
3111 if (vp->v_iflag & VI_DOINGINACT)
3115 * Locking operations here will drop the interlock and possibly the
3116 * vnode lock, opening a window where the vnode can get doomed all the
3117 * while ->v_usecount is 0. Set VI_OWEINACT to let vgone know to
3120 vp->v_iflag |= VI_OWEINACT;
3121 want_unlock = false;
3125 switch (VOP_ISLOCKED(vp)) {
3131 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
3136 * The lock has at least one sharer, but we have no way
3137 * to conclude whether this is us. Play it safe and
3146 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
3147 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
3153 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
3154 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
3165 vdefer_inactive(vp);
3175 * Decrement ->v_usecount for a vnode.
3177 * Releasing the last use count requires additional processing, see vput_final
3178 * above for details.
3180 * Comment above each variant denotes lock state on entry and exit.
3185 * out: same as passed in
3188 vrele(struct vnode *vp)
3191 ASSERT_VI_UNLOCKED(vp, __func__);
3192 if (!refcount_release(&vp->v_usecount))
3194 vput_final(vp, VRELE);
3202 vput(struct vnode *vp)
3205 ASSERT_VOP_LOCKED(vp, __func__);
3206 ASSERT_VI_UNLOCKED(vp, __func__);
3207 if (!refcount_release(&vp->v_usecount)) {
3211 vput_final(vp, VPUT);
3219 vunref(struct vnode *vp)
3222 ASSERT_VOP_LOCKED(vp, __func__);
3223 ASSERT_VI_UNLOCKED(vp, __func__);
3224 if (!refcount_release(&vp->v_usecount))
3226 vput_final(vp, VUNREF);
3230 vhold(struct vnode *vp)
3234 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3235 old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3236 VNASSERT(old >= 0 && (old & VHOLD_ALL_FLAGS) == 0, vp,
3237 ("%s: wrong hold count %d", __func__, old));
3239 vn_freevnodes_dec();
3243 vholdnz(struct vnode *vp)
3246 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3248 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3249 VNASSERT(old > 0 && (old & VHOLD_ALL_FLAGS) == 0, vp,
3250 ("%s: wrong hold count %d", __func__, old));
3252 atomic_add_int(&vp->v_holdcnt, 1);
3257 * Grab a hold count unless the vnode is freed.
3259 * Only use this routine if vfs smr is the only protection you have against
3260 * freeing the vnode.
3262 * The code loops trying to add a hold count as long as the VHOLD_NO_SMR flag
3263 * is not set. After the flag is set the vnode becomes immutable to anyone but
3264 * the thread which managed to set the flag.
3266 * It may be tempting to replace the loop with:
3267 * count = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3268 * if (count & VHOLD_NO_SMR) {
3269 * backpedal and error out;
3272 * However, while this is more performant, it hinders debugging by eliminating
3273 * the previously mentioned invariant.
3276 vhold_smr(struct vnode *vp)
3280 VFS_SMR_ASSERT_ENTERED();
3282 count = atomic_load_int(&vp->v_holdcnt);
3284 if (count & VHOLD_NO_SMR) {
3285 VNASSERT((count & ~VHOLD_NO_SMR) == 0, vp,
3286 ("non-zero hold count with flags %d\n", count));
3289 VNASSERT(count >= 0, vp, ("invalid hold count %d\n", count));
3290 if (atomic_fcmpset_int(&vp->v_holdcnt, &count, count + 1)) {
3292 vn_freevnodes_dec();
3299 * Hold a free vnode for recycling.
3301 * Note: vnode_init references this comment.
3303 * Attempts to recycle only need the global vnode list lock and have no use for
3306 * However, vnodes get inserted into the global list before they get fully
3307 * initialized and stay there until UMA decides to free the memory. This in
3308 * particular means the target can be found before it becomes usable and after
3309 * it becomes recycled. Picking up such vnodes is guarded with v_holdcnt set to
3312 * Note: the vnode may gain more references after we transition the count 0->1.
3315 vhold_recycle_free(struct vnode *vp)
3319 mtx_assert(&vnode_list_mtx, MA_OWNED);
3321 count = atomic_load_int(&vp->v_holdcnt);
3323 if (count & VHOLD_NO_SMR) {
3324 VNASSERT((count & ~VHOLD_NO_SMR) == 0, vp,
3325 ("non-zero hold count with flags %d\n", count));
3328 VNASSERT(count >= 0, vp, ("invalid hold count %d\n", count));
3332 if (atomic_fcmpset_int(&vp->v_holdcnt, &count, count + 1)) {
3333 vn_freevnodes_dec();
3339 static void __noinline
3340 vdbatch_process(struct vdbatch *vd)
3345 mtx_assert(&vd->lock, MA_OWNED);
3346 MPASS(curthread->td_pinned > 0);
3347 MPASS(vd->index == VDBATCH_SIZE);
3349 mtx_lock(&vnode_list_mtx);
3351 freevnodes += vd->freevnodes;
3352 for (i = 0; i < VDBATCH_SIZE; i++) {
3354 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist);
3355 TAILQ_INSERT_TAIL(&vnode_list, vp, v_vnodelist);
3356 MPASS(vp->v_dbatchcpu != NOCPU);
3357 vp->v_dbatchcpu = NOCPU;
3359 mtx_unlock(&vnode_list_mtx);
3361 bzero(vd->tab, sizeof(vd->tab));
3367 vdbatch_enqueue(struct vnode *vp)
3371 ASSERT_VI_LOCKED(vp, __func__);
3372 VNASSERT(!VN_IS_DOOMED(vp), vp,
3373 ("%s: deferring requeue of a doomed vnode", __func__));
3375 if (vp->v_dbatchcpu != NOCPU) {
3382 mtx_lock(&vd->lock);
3383 MPASS(vd->index < VDBATCH_SIZE);
3384 MPASS(vd->tab[vd->index] == NULL);
3386 * A hack: we depend on being pinned so that we know what to put in
3389 vp->v_dbatchcpu = curcpu;
3390 vd->tab[vd->index] = vp;
3393 if (vd->index == VDBATCH_SIZE)
3394 vdbatch_process(vd);
3395 mtx_unlock(&vd->lock);
3400 * This routine must only be called for vnodes which are about to be
3401 * deallocated. Supporting dequeue for arbitrary vndoes would require
3402 * validating that the locked batch matches.
3405 vdbatch_dequeue(struct vnode *vp)
3411 VNASSERT(vp->v_type == VBAD || vp->v_type == VNON, vp,
3412 ("%s: called for a used vnode\n", __func__));
3414 cpu = vp->v_dbatchcpu;
3418 vd = DPCPU_ID_PTR(cpu, vd);
3419 mtx_lock(&vd->lock);
3420 for (i = 0; i < vd->index; i++) {
3421 if (vd->tab[i] != vp)
3423 vp->v_dbatchcpu = NOCPU;
3425 vd->tab[i] = vd->tab[vd->index];
3426 vd->tab[vd->index] = NULL;
3429 mtx_unlock(&vd->lock);
3431 * Either we dequeued the vnode above or the target CPU beat us to it.
3433 MPASS(vp->v_dbatchcpu == NOCPU);
3437 * Drop the hold count of the vnode. If this is the last reference to
3438 * the vnode we place it on the free list unless it has been vgone'd
3439 * (marked VIRF_DOOMED) in which case we will free it.
3441 * Because the vnode vm object keeps a hold reference on the vnode if
3442 * there is at least one resident non-cached page, the vnode cannot
3443 * leave the active list without the page cleanup done.
3446 vdrop_deactivate(struct vnode *vp)
3450 ASSERT_VI_LOCKED(vp, __func__);
3452 * Mark a vnode as free: remove it from its active list
3453 * and put it up for recycling on the freelist.
3455 VNASSERT(!VN_IS_DOOMED(vp), vp,
3456 ("vdrop: returning doomed vnode"));
3457 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
3458 ("vnode with VI_OWEINACT set"));
3459 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp,
3460 ("vnode with VI_DEFINACT set"));
3461 if (vp->v_mflag & VMP_LAZYLIST) {
3463 mtx_lock(&mp->mnt_listmtx);
3464 VNASSERT(vp->v_mflag & VMP_LAZYLIST, vp, ("lost VMP_LAZYLIST"));
3466 * Don't remove the vnode from the lazy list if another thread
3467 * has increased the hold count. It may have re-enqueued the
3468 * vnode to the lazy list and is now responsible for its
3471 if (vp->v_holdcnt == 0) {
3472 vp->v_mflag &= ~VMP_LAZYLIST;
3473 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3474 mp->mnt_lazyvnodelistsize--;
3476 mtx_unlock(&mp->mnt_listmtx);
3478 vdbatch_enqueue(vp);
3481 static void __noinline
3482 vdropl_final(struct vnode *vp)
3485 ASSERT_VI_LOCKED(vp, __func__);
3486 VNPASS(VN_IS_DOOMED(vp), vp);
3488 * Set the VHOLD_NO_SMR flag.
3490 * We may be racing against vhold_smr. If they win we can just pretend
3491 * we never got this far, they will vdrop later.
3493 if (__predict_false(!atomic_cmpset_int(&vp->v_holdcnt, 0, VHOLD_NO_SMR))) {
3494 vn_freevnodes_inc();
3497 * We lost the aforementioned race. Any subsequent access is
3498 * invalid as they might have managed to vdropl on their own.
3503 * Don't bump freevnodes as this one is going away.
3509 vdrop(struct vnode *vp)
3512 ASSERT_VI_UNLOCKED(vp, __func__);
3513 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3514 if (refcount_release_if_not_last(&vp->v_holdcnt))
3521 vdropl(struct vnode *vp)
3524 ASSERT_VI_LOCKED(vp, __func__);
3525 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3526 if (!refcount_release(&vp->v_holdcnt)) {
3530 if (!VN_IS_DOOMED(vp)) {
3531 vn_freevnodes_inc();
3532 vdrop_deactivate(vp);
3534 * Also unlocks the interlock. We can't assert on it as we
3535 * released our hold and by now the vnode might have been
3544 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
3545 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
3548 vinactivef(struct vnode *vp)
3550 struct vm_object *obj;
3552 ASSERT_VOP_ELOCKED(vp, "vinactive");
3553 ASSERT_VI_LOCKED(vp, "vinactive");
3554 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
3555 ("vinactive: recursed on VI_DOINGINACT"));
3556 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3557 vp->v_iflag |= VI_DOINGINACT;
3558 vp->v_iflag &= ~VI_OWEINACT;
3561 * Before moving off the active list, we must be sure that any
3562 * modified pages are converted into the vnode's dirty
3563 * buffers, since these will no longer be checked once the
3564 * vnode is on the inactive list.
3566 * The write-out of the dirty pages is asynchronous. At the
3567 * point that VOP_INACTIVE() is called, there could still be
3568 * pending I/O and dirty pages in the object.
3570 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
3571 vm_object_mightbedirty(obj)) {
3572 VM_OBJECT_WLOCK(obj);
3573 vm_object_page_clean(obj, 0, 0, 0);
3574 VM_OBJECT_WUNLOCK(obj);
3578 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
3579 ("vinactive: lost VI_DOINGINACT"));
3580 vp->v_iflag &= ~VI_DOINGINACT;
3584 vinactive(struct vnode *vp)
3587 ASSERT_VOP_ELOCKED(vp, "vinactive");
3588 ASSERT_VI_LOCKED(vp, "vinactive");
3589 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3591 if ((vp->v_iflag & VI_OWEINACT) == 0)
3593 if (vp->v_iflag & VI_DOINGINACT)
3595 if (vp->v_usecount > 0) {
3596 vp->v_iflag &= ~VI_OWEINACT;
3603 * Remove any vnodes in the vnode table belonging to mount point mp.
3605 * If FORCECLOSE is not specified, there should not be any active ones,
3606 * return error if any are found (nb: this is a user error, not a
3607 * system error). If FORCECLOSE is specified, detach any active vnodes
3610 * If WRITECLOSE is set, only flush out regular file vnodes open for
3613 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
3615 * `rootrefs' specifies the base reference count for the root vnode
3616 * of this filesystem. The root vnode is considered busy if its
3617 * v_usecount exceeds this value. On a successful return, vflush(, td)
3618 * will call vrele() on the root vnode exactly rootrefs times.
3619 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
3623 static int busyprt = 0; /* print out busy vnodes */
3624 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
3628 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
3630 struct vnode *vp, *mvp, *rootvp = NULL;
3632 int busy = 0, error;
3634 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
3637 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
3638 ("vflush: bad args"));
3640 * Get the filesystem root vnode. We can vput() it
3641 * immediately, since with rootrefs > 0, it won't go away.
3643 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
3644 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
3651 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
3653 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
3656 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3660 * Skip over a vnodes marked VV_SYSTEM.
3662 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
3668 * If WRITECLOSE is set, flush out unlinked but still open
3669 * files (even if open only for reading) and regular file
3670 * vnodes open for writing.
3672 if (flags & WRITECLOSE) {
3673 if (vp->v_object != NULL) {
3674 VM_OBJECT_WLOCK(vp->v_object);
3675 vm_object_page_clean(vp->v_object, 0, 0, 0);
3676 VM_OBJECT_WUNLOCK(vp->v_object);
3679 error = VOP_FSYNC(vp, MNT_WAIT, td);
3680 } while (error == ERELOOKUP);
3684 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3687 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3690 if ((vp->v_type == VNON ||
3691 (error == 0 && vattr.va_nlink > 0)) &&
3692 (vp->v_writecount <= 0 || vp->v_type != VREG)) {
3700 * With v_usecount == 0, all we need to do is clear out the
3701 * vnode data structures and we are done.
3703 * If FORCECLOSE is set, forcibly close the vnode.
3705 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
3711 vn_printf(vp, "vflush: busy vnode ");
3717 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
3719 * If just the root vnode is busy, and if its refcount
3720 * is equal to `rootrefs', then go ahead and kill it.
3723 KASSERT(busy > 0, ("vflush: not busy"));
3724 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
3725 ("vflush: usecount %d < rootrefs %d",
3726 rootvp->v_usecount, rootrefs));
3727 if (busy == 1 && rootvp->v_usecount == rootrefs) {
3728 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
3736 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
3740 for (; rootrefs > 0; rootrefs--)
3746 * Recycle an unused vnode to the front of the free list.
3749 vrecycle(struct vnode *vp)
3754 recycled = vrecyclel(vp);
3760 * vrecycle, with the vp interlock held.
3763 vrecyclel(struct vnode *vp)
3767 ASSERT_VOP_ELOCKED(vp, __func__);
3768 ASSERT_VI_LOCKED(vp, __func__);
3769 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3771 if (vp->v_usecount == 0) {
3779 * Eliminate all activity associated with a vnode
3780 * in preparation for reuse.
3783 vgone(struct vnode *vp)
3791 notify_lowervp_vfs_dummy(struct mount *mp __unused,
3792 struct vnode *lowervp __unused)
3797 * Notify upper mounts about reclaimed or unlinked vnode.
3800 vfs_notify_upper(struct vnode *vp, int event)
3802 static struct vfsops vgonel_vfsops = {
3803 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
3804 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
3806 struct mount *mp, *ump, *mmp;
3811 if (TAILQ_EMPTY(&mp->mnt_uppers))
3814 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
3815 mmp->mnt_op = &vgonel_vfsops;
3816 mmp->mnt_kern_flag |= MNTK_MARKER;
3818 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
3819 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
3820 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
3821 ump = TAILQ_NEXT(ump, mnt_upper_link);
3824 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
3827 case VFS_NOTIFY_UPPER_RECLAIM:
3828 VFS_RECLAIM_LOWERVP(ump, vp);
3830 case VFS_NOTIFY_UPPER_UNLINK:
3831 VFS_UNLINK_LOWERVP(ump, vp);
3834 KASSERT(0, ("invalid event %d", event));
3838 ump = TAILQ_NEXT(mmp, mnt_upper_link);
3839 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
3842 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
3843 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
3844 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
3845 wakeup(&mp->mnt_uppers);
3851 * vgone, with the vp interlock held.
3854 vgonel(struct vnode *vp)
3859 bool active, doinginact, oweinact;
3861 ASSERT_VOP_ELOCKED(vp, "vgonel");
3862 ASSERT_VI_LOCKED(vp, "vgonel");
3863 VNASSERT(vp->v_holdcnt, vp,
3864 ("vgonel: vp %p has no reference.", vp));
3865 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3869 * Don't vgonel if we're already doomed.
3871 if (vp->v_irflag & VIRF_DOOMED)
3874 * Paired with freevnode.
3876 vn_seqc_write_begin_locked(vp);
3878 vp->v_irflag |= VIRF_DOOMED;
3881 * Check to see if the vnode is in use. If so, we have to
3882 * call VOP_CLOSE() and VOP_INACTIVE().
3884 * It could be that VOP_INACTIVE() requested reclamation, in
3885 * which case we should avoid recursion, so check
3886 * VI_DOINGINACT. This is not precise but good enough.
3888 active = vp->v_usecount > 0;
3889 oweinact = (vp->v_iflag & VI_OWEINACT) != 0;
3890 doinginact = (vp->v_iflag & VI_DOINGINACT) != 0;
3893 * If we need to do inactive VI_OWEINACT will be set.
3895 if (vp->v_iflag & VI_DEFINACT) {
3896 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count"));
3897 vp->v_iflag &= ~VI_DEFINACT;
3900 VNASSERT(vp->v_holdcnt > 0, vp, ("vnode without hold count"));
3903 cache_purge_vgone(vp);
3904 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
3907 * If purging an active vnode, it must be closed and
3908 * deactivated before being reclaimed.
3911 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
3912 if ((oweinact || active) && !doinginact) {
3917 if (vp->v_type == VSOCK)
3918 vfs_unp_reclaim(vp);
3921 * Clean out any buffers associated with the vnode.
3922 * If the flush fails, just toss the buffers.
3925 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
3926 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
3927 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
3928 while (vinvalbuf(vp, 0, 0, 0) != 0)
3932 BO_LOCK(&vp->v_bufobj);
3933 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
3934 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3935 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
3936 vp->v_bufobj.bo_clean.bv_cnt == 0,
3937 ("vp %p bufobj not invalidated", vp));
3940 * For VMIO bufobj, BO_DEAD is set later, or in
3941 * vm_object_terminate() after the object's page queue is
3944 object = vp->v_bufobj.bo_object;
3946 vp->v_bufobj.bo_flag |= BO_DEAD;
3947 BO_UNLOCK(&vp->v_bufobj);
3950 * Handle the VM part. Tmpfs handles v_object on its own (the
3951 * OBJT_VNODE check). Nullfs or other bypassing filesystems
3952 * should not touch the object borrowed from the lower vnode
3953 * (the handle check).
3955 if (object != NULL && object->type == OBJT_VNODE &&
3956 object->handle == vp)
3957 vnode_destroy_vobject(vp);
3960 * Reclaim the vnode.
3962 if (VOP_RECLAIM(vp))
3963 panic("vgone: cannot reclaim");
3965 vn_finished_secondary_write(mp);
3966 VNASSERT(vp->v_object == NULL, vp,
3967 ("vop_reclaim left v_object vp=%p", vp));
3969 * Clear the advisory locks and wake up waiting threads.
3971 (void)VOP_ADVLOCKPURGE(vp);
3974 * Delete from old mount point vnode list.
3978 * Done with purge, reset to the standard lock and invalidate
3982 vp->v_vnlock = &vp->v_lock;
3983 vp->v_op = &dead_vnodeops;
3988 * Print out a description of a vnode.
3990 static const char * const typename[] =
3991 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
3994 _Static_assert((VHOLD_ALL_FLAGS & ~VHOLD_NO_SMR) == 0,
3995 "new hold count flag not added to vn_printf");
3998 vn_printf(struct vnode *vp, const char *fmt, ...)
4001 char buf[256], buf2[16];
4008 printf("%p: ", (void *)vp);
4009 printf("type %s\n", typename[vp->v_type]);
4010 holdcnt = atomic_load_int(&vp->v_holdcnt);
4011 printf(" usecount %d, writecount %d, refcount %d seqc users %d",
4012 vp->v_usecount, vp->v_writecount, holdcnt & ~VHOLD_ALL_FLAGS,
4014 switch (vp->v_type) {
4016 printf(" mountedhere %p\n", vp->v_mountedhere);
4019 printf(" rdev %p\n", vp->v_rdev);
4022 printf(" socket %p\n", vp->v_unpcb);
4025 printf(" fifoinfo %p\n", vp->v_fifoinfo);
4033 if (holdcnt & VHOLD_NO_SMR)
4034 strlcat(buf, "|VHOLD_NO_SMR", sizeof(buf));
4035 printf(" hold count flags (%s)\n", buf + 1);
4039 if (vp->v_irflag & VIRF_DOOMED)
4040 strlcat(buf, "|VIRF_DOOMED", sizeof(buf));
4041 if (vp->v_irflag & VIRF_PGREAD)
4042 strlcat(buf, "|VIRF_PGREAD", sizeof(buf));
4043 flags = vp->v_irflag & ~(VIRF_DOOMED | VIRF_PGREAD);
4045 snprintf(buf2, sizeof(buf2), "|VIRF(0x%lx)", flags);
4046 strlcat(buf, buf2, sizeof(buf));
4048 if (vp->v_vflag & VV_ROOT)
4049 strlcat(buf, "|VV_ROOT", sizeof(buf));
4050 if (vp->v_vflag & VV_ISTTY)
4051 strlcat(buf, "|VV_ISTTY", sizeof(buf));
4052 if (vp->v_vflag & VV_NOSYNC)
4053 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
4054 if (vp->v_vflag & VV_ETERNALDEV)
4055 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
4056 if (vp->v_vflag & VV_CACHEDLABEL)
4057 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
4058 if (vp->v_vflag & VV_VMSIZEVNLOCK)
4059 strlcat(buf, "|VV_VMSIZEVNLOCK", sizeof(buf));
4060 if (vp->v_vflag & VV_COPYONWRITE)
4061 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
4062 if (vp->v_vflag & VV_SYSTEM)
4063 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
4064 if (vp->v_vflag & VV_PROCDEP)
4065 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
4066 if (vp->v_vflag & VV_NOKNOTE)
4067 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
4068 if (vp->v_vflag & VV_DELETED)
4069 strlcat(buf, "|VV_DELETED", sizeof(buf));
4070 if (vp->v_vflag & VV_MD)
4071 strlcat(buf, "|VV_MD", sizeof(buf));
4072 if (vp->v_vflag & VV_FORCEINSMQ)
4073 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
4074 if (vp->v_vflag & VV_READLINK)
4075 strlcat(buf, "|VV_READLINK", sizeof(buf));
4076 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
4077 VV_CACHEDLABEL | VV_VMSIZEVNLOCK | VV_COPYONWRITE | VV_SYSTEM |
4078 VV_PROCDEP | VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ |
4081 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
4082 strlcat(buf, buf2, sizeof(buf));
4084 if (vp->v_iflag & VI_TEXT_REF)
4085 strlcat(buf, "|VI_TEXT_REF", sizeof(buf));
4086 if (vp->v_iflag & VI_MOUNT)
4087 strlcat(buf, "|VI_MOUNT", sizeof(buf));
4088 if (vp->v_iflag & VI_DOINGINACT)
4089 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
4090 if (vp->v_iflag & VI_OWEINACT)
4091 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
4092 if (vp->v_iflag & VI_DEFINACT)
4093 strlcat(buf, "|VI_DEFINACT", sizeof(buf));
4094 flags = vp->v_iflag & ~(VI_TEXT_REF | VI_MOUNT | VI_DOINGINACT |
4095 VI_OWEINACT | VI_DEFINACT);
4097 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
4098 strlcat(buf, buf2, sizeof(buf));
4100 if (vp->v_mflag & VMP_LAZYLIST)
4101 strlcat(buf, "|VMP_LAZYLIST", sizeof(buf));
4102 flags = vp->v_mflag & ~(VMP_LAZYLIST);
4104 snprintf(buf2, sizeof(buf2), "|VMP(0x%lx)", flags);
4105 strlcat(buf, buf2, sizeof(buf));
4107 printf(" flags (%s)", buf + 1);
4108 if (mtx_owned(VI_MTX(vp)))
4109 printf(" VI_LOCKed");
4111 if (vp->v_object != NULL)
4112 printf(" v_object %p ref %d pages %d "
4113 "cleanbuf %d dirtybuf %d\n",
4114 vp->v_object, vp->v_object->ref_count,
4115 vp->v_object->resident_page_count,
4116 vp->v_bufobj.bo_clean.bv_cnt,
4117 vp->v_bufobj.bo_dirty.bv_cnt);
4119 lockmgr_printinfo(vp->v_vnlock);
4120 if (vp->v_data != NULL)
4126 * List all of the locked vnodes in the system.
4127 * Called when debugging the kernel.
4129 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
4135 * Note: because this is DDB, we can't obey the locking semantics
4136 * for these structures, which means we could catch an inconsistent
4137 * state and dereference a nasty pointer. Not much to be done
4140 db_printf("Locked vnodes\n");
4141 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4142 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4143 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
4144 vn_printf(vp, "vnode ");
4150 * Show details about the given vnode.
4152 DB_SHOW_COMMAND(vnode, db_show_vnode)
4158 vp = (struct vnode *)addr;
4159 vn_printf(vp, "vnode ");
4163 * Show details about the given mount point.
4165 DB_SHOW_COMMAND(mount, db_show_mount)
4176 /* No address given, print short info about all mount points. */
4177 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4178 db_printf("%p %s on %s (%s)\n", mp,
4179 mp->mnt_stat.f_mntfromname,
4180 mp->mnt_stat.f_mntonname,
4181 mp->mnt_stat.f_fstypename);
4185 db_printf("\nMore info: show mount <addr>\n");
4189 mp = (struct mount *)addr;
4190 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
4191 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
4194 mflags = mp->mnt_flag;
4195 #define MNT_FLAG(flag) do { \
4196 if (mflags & (flag)) { \
4197 if (buf[0] != '\0') \
4198 strlcat(buf, ", ", sizeof(buf)); \
4199 strlcat(buf, (#flag) + 4, sizeof(buf)); \
4200 mflags &= ~(flag); \
4203 MNT_FLAG(MNT_RDONLY);
4204 MNT_FLAG(MNT_SYNCHRONOUS);
4205 MNT_FLAG(MNT_NOEXEC);
4206 MNT_FLAG(MNT_NOSUID);
4207 MNT_FLAG(MNT_NFS4ACLS);
4208 MNT_FLAG(MNT_UNION);
4209 MNT_FLAG(MNT_ASYNC);
4210 MNT_FLAG(MNT_SUIDDIR);
4211 MNT_FLAG(MNT_SOFTDEP);
4212 MNT_FLAG(MNT_NOSYMFOLLOW);
4213 MNT_FLAG(MNT_GJOURNAL);
4214 MNT_FLAG(MNT_MULTILABEL);
4216 MNT_FLAG(MNT_NOATIME);
4217 MNT_FLAG(MNT_NOCLUSTERR);
4218 MNT_FLAG(MNT_NOCLUSTERW);
4220 MNT_FLAG(MNT_EXRDONLY);
4221 MNT_FLAG(MNT_EXPORTED);
4222 MNT_FLAG(MNT_DEFEXPORTED);
4223 MNT_FLAG(MNT_EXPORTANON);
4224 MNT_FLAG(MNT_EXKERB);
4225 MNT_FLAG(MNT_EXPUBLIC);
4226 MNT_FLAG(MNT_LOCAL);
4227 MNT_FLAG(MNT_QUOTA);
4228 MNT_FLAG(MNT_ROOTFS);
4230 MNT_FLAG(MNT_IGNORE);
4231 MNT_FLAG(MNT_UPDATE);
4232 MNT_FLAG(MNT_DELEXPORT);
4233 MNT_FLAG(MNT_RELOAD);
4234 MNT_FLAG(MNT_FORCE);
4235 MNT_FLAG(MNT_SNAPSHOT);
4236 MNT_FLAG(MNT_BYFSID);
4240 strlcat(buf, ", ", sizeof(buf));
4241 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
4242 "0x%016jx", mflags);
4244 db_printf(" mnt_flag = %s\n", buf);
4247 flags = mp->mnt_kern_flag;
4248 #define MNT_KERN_FLAG(flag) do { \
4249 if (flags & (flag)) { \
4250 if (buf[0] != '\0') \
4251 strlcat(buf, ", ", sizeof(buf)); \
4252 strlcat(buf, (#flag) + 5, sizeof(buf)); \
4256 MNT_KERN_FLAG(MNTK_UNMOUNTF);
4257 MNT_KERN_FLAG(MNTK_ASYNC);
4258 MNT_KERN_FLAG(MNTK_SOFTDEP);
4259 MNT_KERN_FLAG(MNTK_DRAINING);
4260 MNT_KERN_FLAG(MNTK_REFEXPIRE);
4261 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
4262 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
4263 MNT_KERN_FLAG(MNTK_NO_IOPF);
4264 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
4265 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
4266 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
4267 MNT_KERN_FLAG(MNTK_MARKER);
4268 MNT_KERN_FLAG(MNTK_USES_BCACHE);
4269 MNT_KERN_FLAG(MNTK_FPLOOKUP);
4270 MNT_KERN_FLAG(MNTK_NOASYNC);
4271 MNT_KERN_FLAG(MNTK_UNMOUNT);
4272 MNT_KERN_FLAG(MNTK_MWAIT);
4273 MNT_KERN_FLAG(MNTK_SUSPEND);
4274 MNT_KERN_FLAG(MNTK_SUSPEND2);
4275 MNT_KERN_FLAG(MNTK_SUSPENDED);
4276 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
4277 MNT_KERN_FLAG(MNTK_NOKNOTE);
4278 #undef MNT_KERN_FLAG
4281 strlcat(buf, ", ", sizeof(buf));
4282 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
4285 db_printf(" mnt_kern_flag = %s\n", buf);
4287 db_printf(" mnt_opt = ");
4288 opt = TAILQ_FIRST(mp->mnt_opt);
4290 db_printf("%s", opt->name);
4291 opt = TAILQ_NEXT(opt, link);
4292 while (opt != NULL) {
4293 db_printf(", %s", opt->name);
4294 opt = TAILQ_NEXT(opt, link);
4300 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
4301 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
4302 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
4303 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
4304 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
4305 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
4306 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
4307 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
4308 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
4309 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
4310 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
4311 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
4313 db_printf(" mnt_cred = { uid=%u ruid=%u",
4314 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
4315 if (jailed(mp->mnt_cred))
4316 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
4318 db_printf(" mnt_ref = %d (with %d in the struct)\n",
4319 vfs_mount_fetch_counter(mp, MNT_COUNT_REF), mp->mnt_ref);
4320 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
4321 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
4322 db_printf(" mnt_lazyvnodelistsize = %d\n",
4323 mp->mnt_lazyvnodelistsize);
4324 db_printf(" mnt_writeopcount = %d (with %d in the struct)\n",
4325 vfs_mount_fetch_counter(mp, MNT_COUNT_WRITEOPCOUNT), mp->mnt_writeopcount);
4326 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
4327 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
4328 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
4329 db_printf(" mnt_lockref = %d (with %d in the struct)\n",
4330 vfs_mount_fetch_counter(mp, MNT_COUNT_LOCKREF), mp->mnt_lockref);
4331 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
4332 db_printf(" mnt_secondary_accwrites = %d\n",
4333 mp->mnt_secondary_accwrites);
4334 db_printf(" mnt_gjprovider = %s\n",
4335 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
4336 db_printf(" mnt_vfs_ops = %d\n", mp->mnt_vfs_ops);
4338 db_printf("\n\nList of active vnodes\n");
4339 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4340 if (vp->v_type != VMARKER && vp->v_holdcnt > 0) {
4341 vn_printf(vp, "vnode ");
4346 db_printf("\n\nList of inactive vnodes\n");
4347 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4348 if (vp->v_type != VMARKER && vp->v_holdcnt == 0) {
4349 vn_printf(vp, "vnode ");
4358 * Fill in a struct xvfsconf based on a struct vfsconf.
4361 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
4363 struct xvfsconf xvfsp;
4365 bzero(&xvfsp, sizeof(xvfsp));
4366 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4367 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4368 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4369 xvfsp.vfc_flags = vfsp->vfc_flags;
4371 * These are unused in userland, we keep them
4372 * to not break binary compatibility.
4374 xvfsp.vfc_vfsops = NULL;
4375 xvfsp.vfc_next = NULL;
4376 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4379 #ifdef COMPAT_FREEBSD32
4381 uint32_t vfc_vfsops;
4382 char vfc_name[MFSNAMELEN];
4383 int32_t vfc_typenum;
4384 int32_t vfc_refcount;
4390 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
4392 struct xvfsconf32 xvfsp;
4394 bzero(&xvfsp, sizeof(xvfsp));
4395 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4396 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4397 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4398 xvfsp.vfc_flags = vfsp->vfc_flags;
4399 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4404 * Top level filesystem related information gathering.
4407 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
4409 struct vfsconf *vfsp;
4414 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4415 #ifdef COMPAT_FREEBSD32
4416 if (req->flags & SCTL_MASK32)
4417 error = vfsconf2x32(req, vfsp);
4420 error = vfsconf2x(req, vfsp);
4428 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
4429 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
4430 "S,xvfsconf", "List of all configured filesystems");
4432 #ifndef BURN_BRIDGES
4433 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
4436 vfs_sysctl(SYSCTL_HANDLER_ARGS)
4438 int *name = (int *)arg1 - 1; /* XXX */
4439 u_int namelen = arg2 + 1; /* XXX */
4440 struct vfsconf *vfsp;
4442 log(LOG_WARNING, "userland calling deprecated sysctl, "
4443 "please rebuild world\n");
4445 #if 1 || defined(COMPAT_PRELITE2)
4446 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
4448 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
4452 case VFS_MAXTYPENUM:
4455 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
4458 return (ENOTDIR); /* overloaded */
4460 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4461 if (vfsp->vfc_typenum == name[2])
4466 return (EOPNOTSUPP);
4467 #ifdef COMPAT_FREEBSD32
4468 if (req->flags & SCTL_MASK32)
4469 return (vfsconf2x32(req, vfsp));
4472 return (vfsconf2x(req, vfsp));
4474 return (EOPNOTSUPP);
4477 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
4478 CTLFLAG_MPSAFE, vfs_sysctl,
4479 "Generic filesystem");
4481 #if 1 || defined(COMPAT_PRELITE2)
4484 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
4487 struct vfsconf *vfsp;
4488 struct ovfsconf ovfs;
4491 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4492 bzero(&ovfs, sizeof(ovfs));
4493 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
4494 strcpy(ovfs.vfc_name, vfsp->vfc_name);
4495 ovfs.vfc_index = vfsp->vfc_typenum;
4496 ovfs.vfc_refcount = vfsp->vfc_refcount;
4497 ovfs.vfc_flags = vfsp->vfc_flags;
4498 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
4508 #endif /* 1 || COMPAT_PRELITE2 */
4509 #endif /* !BURN_BRIDGES */
4511 #define KINFO_VNODESLOP 10
4514 * Dump vnode list (via sysctl).
4518 sysctl_vnode(SYSCTL_HANDLER_ARGS)
4526 * Stale numvnodes access is not fatal here.
4529 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
4531 /* Make an estimate */
4532 return (SYSCTL_OUT(req, 0, len));
4534 error = sysctl_wire_old_buffer(req, 0);
4537 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
4539 mtx_lock(&mountlist_mtx);
4540 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4541 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
4544 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4548 xvn[n].xv_size = sizeof *xvn;
4549 xvn[n].xv_vnode = vp;
4550 xvn[n].xv_id = 0; /* XXX compat */
4551 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
4553 XV_COPY(writecount);
4559 xvn[n].xv_flag = vp->v_vflag;
4561 switch (vp->v_type) {
4568 if (vp->v_rdev == NULL) {
4572 xvn[n].xv_dev = dev2udev(vp->v_rdev);
4575 xvn[n].xv_socket = vp->v_socket;
4578 xvn[n].xv_fifo = vp->v_fifoinfo;
4583 /* shouldn't happen? */
4591 mtx_lock(&mountlist_mtx);
4596 mtx_unlock(&mountlist_mtx);
4598 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
4603 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
4604 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
4609 unmount_or_warn(struct mount *mp)
4613 error = dounmount(mp, MNT_FORCE, curthread);
4615 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
4619 printf("%d)\n", error);
4624 * Unmount all filesystems. The list is traversed in reverse order
4625 * of mounting to avoid dependencies.
4628 vfs_unmountall(void)
4630 struct mount *mp, *tmp;
4632 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
4635 * Since this only runs when rebooting, it is not interlocked.
4637 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
4641 * Forcibly unmounting "/dev" before "/" would prevent clean
4642 * unmount of the latter.
4644 if (mp == rootdevmp)
4647 unmount_or_warn(mp);
4650 if (rootdevmp != NULL)
4651 unmount_or_warn(rootdevmp);
4655 vfs_deferred_inactive(struct vnode *vp, int lkflags)
4658 ASSERT_VI_LOCKED(vp, __func__);
4659 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, ("VI_DEFINACT still set"));
4660 if ((vp->v_iflag & VI_OWEINACT) == 0) {
4664 if (vn_lock(vp, lkflags) == 0) {
4671 vdefer_inactive_unlocked(vp);
4675 vfs_periodic_inactive_filter(struct vnode *vp, void *arg)
4678 return (vp->v_iflag & VI_DEFINACT);
4681 static void __noinline
4682 vfs_periodic_inactive(struct mount *mp, int flags)
4684 struct vnode *vp, *mvp;
4687 lkflags = LK_EXCLUSIVE | LK_INTERLOCK;
4688 if (flags != MNT_WAIT)
4689 lkflags |= LK_NOWAIT;
4691 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_inactive_filter, NULL) {
4692 if ((vp->v_iflag & VI_DEFINACT) == 0) {
4696 vp->v_iflag &= ~VI_DEFINACT;
4697 vfs_deferred_inactive(vp, lkflags);
4702 vfs_want_msync(struct vnode *vp)
4704 struct vm_object *obj;
4707 * This test may be performed without any locks held.
4708 * We rely on vm_object's type stability.
4710 if (vp->v_vflag & VV_NOSYNC)
4713 return (obj != NULL && vm_object_mightbedirty(obj));
4717 vfs_periodic_msync_inactive_filter(struct vnode *vp, void *arg __unused)
4720 if (vp->v_vflag & VV_NOSYNC)
4722 if (vp->v_iflag & VI_DEFINACT)
4724 return (vfs_want_msync(vp));
4727 static void __noinline
4728 vfs_periodic_msync_inactive(struct mount *mp, int flags)
4730 struct vnode *vp, *mvp;
4731 struct vm_object *obj;
4732 int lkflags, objflags;
4735 lkflags = LK_EXCLUSIVE | LK_INTERLOCK;
4736 if (flags != MNT_WAIT) {
4737 lkflags |= LK_NOWAIT;
4738 objflags = OBJPC_NOSYNC;
4740 objflags = OBJPC_SYNC;
4743 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_msync_inactive_filter, NULL) {
4745 if (vp->v_iflag & VI_DEFINACT) {
4746 vp->v_iflag &= ~VI_DEFINACT;
4749 if (!vfs_want_msync(vp)) {
4751 vfs_deferred_inactive(vp, lkflags);
4756 if (vget(vp, lkflags) == 0) {
4758 if (obj != NULL && (vp->v_vflag & VV_NOSYNC) == 0) {
4759 VM_OBJECT_WLOCK(obj);
4760 vm_object_page_clean(obj, 0, 0, objflags);
4761 VM_OBJECT_WUNLOCK(obj);
4768 vdefer_inactive_unlocked(vp);
4774 vfs_periodic(struct mount *mp, int flags)
4777 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
4779 if ((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0)
4780 vfs_periodic_inactive(mp, flags);
4782 vfs_periodic_msync_inactive(mp, flags);
4786 destroy_vpollinfo_free(struct vpollinfo *vi)
4789 knlist_destroy(&vi->vpi_selinfo.si_note);
4790 mtx_destroy(&vi->vpi_lock);
4791 free(vi, M_VNODEPOLL);
4795 destroy_vpollinfo(struct vpollinfo *vi)
4798 knlist_clear(&vi->vpi_selinfo.si_note, 1);
4799 seldrain(&vi->vpi_selinfo);
4800 destroy_vpollinfo_free(vi);
4804 * Initialize per-vnode helper structure to hold poll-related state.
4807 v_addpollinfo(struct vnode *vp)
4809 struct vpollinfo *vi;
4811 if (vp->v_pollinfo != NULL)
4813 vi = malloc(sizeof(*vi), M_VNODEPOLL, M_WAITOK | M_ZERO);
4814 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
4815 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
4816 vfs_knlunlock, vfs_knl_assert_lock);
4818 if (vp->v_pollinfo != NULL) {
4820 destroy_vpollinfo_free(vi);
4823 vp->v_pollinfo = vi;
4828 * Record a process's interest in events which might happen to
4829 * a vnode. Because poll uses the historic select-style interface
4830 * internally, this routine serves as both the ``check for any
4831 * pending events'' and the ``record my interest in future events''
4832 * functions. (These are done together, while the lock is held,
4833 * to avoid race conditions.)
4836 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
4840 mtx_lock(&vp->v_pollinfo->vpi_lock);
4841 if (vp->v_pollinfo->vpi_revents & events) {
4843 * This leaves events we are not interested
4844 * in available for the other process which
4845 * which presumably had requested them
4846 * (otherwise they would never have been
4849 events &= vp->v_pollinfo->vpi_revents;
4850 vp->v_pollinfo->vpi_revents &= ~events;
4852 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4855 vp->v_pollinfo->vpi_events |= events;
4856 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
4857 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4862 * Routine to create and manage a filesystem syncer vnode.
4864 #define sync_close ((int (*)(struct vop_close_args *))nullop)
4865 static int sync_fsync(struct vop_fsync_args *);
4866 static int sync_inactive(struct vop_inactive_args *);
4867 static int sync_reclaim(struct vop_reclaim_args *);
4869 static struct vop_vector sync_vnodeops = {
4870 .vop_bypass = VOP_EOPNOTSUPP,
4871 .vop_close = sync_close, /* close */
4872 .vop_fsync = sync_fsync, /* fsync */
4873 .vop_inactive = sync_inactive, /* inactive */
4874 .vop_need_inactive = vop_stdneed_inactive, /* need_inactive */
4875 .vop_reclaim = sync_reclaim, /* reclaim */
4876 .vop_lock1 = vop_stdlock, /* lock */
4877 .vop_unlock = vop_stdunlock, /* unlock */
4878 .vop_islocked = vop_stdislocked, /* islocked */
4880 VFS_VOP_VECTOR_REGISTER(sync_vnodeops);
4883 * Create a new filesystem syncer vnode for the specified mount point.
4886 vfs_allocate_syncvnode(struct mount *mp)
4890 static long start, incr, next;
4893 /* Allocate a new vnode */
4894 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
4896 panic("vfs_allocate_syncvnode: getnewvnode() failed");
4898 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4899 vp->v_vflag |= VV_FORCEINSMQ;
4900 error = insmntque(vp, mp);
4902 panic("vfs_allocate_syncvnode: insmntque() failed");
4903 vp->v_vflag &= ~VV_FORCEINSMQ;
4906 * Place the vnode onto the syncer worklist. We attempt to
4907 * scatter them about on the list so that they will go off
4908 * at evenly distributed times even if all the filesystems
4909 * are mounted at once.
4912 if (next == 0 || next > syncer_maxdelay) {
4916 start = syncer_maxdelay / 2;
4917 incr = syncer_maxdelay;
4923 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
4924 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
4925 mtx_lock(&sync_mtx);
4927 if (mp->mnt_syncer == NULL) {
4928 mp->mnt_syncer = vp;
4931 mtx_unlock(&sync_mtx);
4934 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4941 vfs_deallocate_syncvnode(struct mount *mp)
4945 mtx_lock(&sync_mtx);
4946 vp = mp->mnt_syncer;
4948 mp->mnt_syncer = NULL;
4949 mtx_unlock(&sync_mtx);
4955 * Do a lazy sync of the filesystem.
4958 sync_fsync(struct vop_fsync_args *ap)
4960 struct vnode *syncvp = ap->a_vp;
4961 struct mount *mp = syncvp->v_mount;
4966 * We only need to do something if this is a lazy evaluation.
4968 if (ap->a_waitfor != MNT_LAZY)
4972 * Move ourselves to the back of the sync list.
4974 bo = &syncvp->v_bufobj;
4976 vn_syncer_add_to_worklist(bo, syncdelay);
4980 * Walk the list of vnodes pushing all that are dirty and
4981 * not already on the sync list.
4983 if (vfs_busy(mp, MBF_NOWAIT) != 0)
4985 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
4989 save = curthread_pflags_set(TDP_SYNCIO);
4991 * The filesystem at hand may be idle with free vnodes stored in the
4992 * batch. Return them instead of letting them stay there indefinitely.
4994 vfs_periodic(mp, MNT_NOWAIT);
4995 error = VFS_SYNC(mp, MNT_LAZY);
4996 curthread_pflags_restore(save);
4997 vn_finished_write(mp);
5003 * The syncer vnode is no referenced.
5006 sync_inactive(struct vop_inactive_args *ap)
5014 * The syncer vnode is no longer needed and is being decommissioned.
5016 * Modifications to the worklist must be protected by sync_mtx.
5019 sync_reclaim(struct vop_reclaim_args *ap)
5021 struct vnode *vp = ap->a_vp;
5026 mtx_lock(&sync_mtx);
5027 if (vp->v_mount->mnt_syncer == vp)
5028 vp->v_mount->mnt_syncer = NULL;
5029 if (bo->bo_flag & BO_ONWORKLST) {
5030 LIST_REMOVE(bo, bo_synclist);
5031 syncer_worklist_len--;
5033 bo->bo_flag &= ~BO_ONWORKLST;
5035 mtx_unlock(&sync_mtx);
5042 vn_need_pageq_flush(struct vnode *vp)
5044 struct vm_object *obj;
5047 MPASS(mtx_owned(VI_MTX(vp)));
5049 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
5050 vm_object_mightbedirty(obj))
5056 * Check if vnode represents a disk device
5059 vn_isdisk_error(struct vnode *vp, int *errp)
5063 if (vp->v_type != VCHR) {
5069 if (vp->v_rdev == NULL)
5071 else if (vp->v_rdev->si_devsw == NULL)
5073 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
5078 return (error == 0);
5082 vn_isdisk(struct vnode *vp)
5086 return (vn_isdisk_error(vp, &error));
5090 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
5091 * the comment above cache_fplookup for details.
5094 vaccess_vexec_smr(mode_t file_mode, uid_t file_uid, gid_t file_gid, struct ucred *cred)
5098 VFS_SMR_ASSERT_ENTERED();
5100 /* Check the owner. */
5101 if (cred->cr_uid == file_uid) {
5102 if (file_mode & S_IXUSR)
5107 /* Otherwise, check the groups (first match) */
5108 if (groupmember(file_gid, cred)) {
5109 if (file_mode & S_IXGRP)
5114 /* Otherwise, check everyone else. */
5115 if (file_mode & S_IXOTH)
5119 * Permission check failed, but it is possible denial will get overwritten
5120 * (e.g., when root is traversing through a 700 directory owned by someone
5123 * vaccess() calls priv_check_cred which in turn can descent into MAC
5124 * modules overriding this result. It's quite unclear what semantics
5125 * are allowed for them to operate, thus for safety we don't call them
5126 * from within the SMR section. This also means if any such modules
5127 * are present, we have to let the regular lookup decide.
5129 error = priv_check_cred_vfs_lookup_nomac(cred);
5135 * MAC modules present.
5146 * Common filesystem object access control check routine. Accepts a
5147 * vnode's type, "mode", uid and gid, requested access mode, and credentials.
5148 * Returns 0 on success, or an errno on failure.
5151 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
5152 accmode_t accmode, struct ucred *cred)
5154 accmode_t dac_granted;
5155 accmode_t priv_granted;
5157 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
5158 ("invalid bit in accmode"));
5159 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
5160 ("VAPPEND without VWRITE"));
5163 * Look for a normal, non-privileged way to access the file/directory
5164 * as requested. If it exists, go with that.
5169 /* Check the owner. */
5170 if (cred->cr_uid == file_uid) {
5171 dac_granted |= VADMIN;
5172 if (file_mode & S_IXUSR)
5173 dac_granted |= VEXEC;
5174 if (file_mode & S_IRUSR)
5175 dac_granted |= VREAD;
5176 if (file_mode & S_IWUSR)
5177 dac_granted |= (VWRITE | VAPPEND);
5179 if ((accmode & dac_granted) == accmode)
5185 /* Otherwise, check the groups (first match) */
5186 if (groupmember(file_gid, cred)) {
5187 if (file_mode & S_IXGRP)
5188 dac_granted |= VEXEC;
5189 if (file_mode & S_IRGRP)
5190 dac_granted |= VREAD;
5191 if (file_mode & S_IWGRP)
5192 dac_granted |= (VWRITE | VAPPEND);
5194 if ((accmode & dac_granted) == accmode)
5200 /* Otherwise, check everyone else. */
5201 if (file_mode & S_IXOTH)
5202 dac_granted |= VEXEC;
5203 if (file_mode & S_IROTH)
5204 dac_granted |= VREAD;
5205 if (file_mode & S_IWOTH)
5206 dac_granted |= (VWRITE | VAPPEND);
5207 if ((accmode & dac_granted) == accmode)
5212 * Build a privilege mask to determine if the set of privileges
5213 * satisfies the requirements when combined with the granted mask
5214 * from above. For each privilege, if the privilege is required,
5215 * bitwise or the request type onto the priv_granted mask.
5221 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
5222 * requests, instead of PRIV_VFS_EXEC.
5224 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
5225 !priv_check_cred(cred, PRIV_VFS_LOOKUP))
5226 priv_granted |= VEXEC;
5229 * Ensure that at least one execute bit is on. Otherwise,
5230 * a privileged user will always succeed, and we don't want
5231 * this to happen unless the file really is executable.
5233 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
5234 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
5235 !priv_check_cred(cred, PRIV_VFS_EXEC))
5236 priv_granted |= VEXEC;
5239 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
5240 !priv_check_cred(cred, PRIV_VFS_READ))
5241 priv_granted |= VREAD;
5243 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
5244 !priv_check_cred(cred, PRIV_VFS_WRITE))
5245 priv_granted |= (VWRITE | VAPPEND);
5247 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
5248 !priv_check_cred(cred, PRIV_VFS_ADMIN))
5249 priv_granted |= VADMIN;
5251 if ((accmode & (priv_granted | dac_granted)) == accmode) {
5255 return ((accmode & VADMIN) ? EPERM : EACCES);
5259 * Credential check based on process requesting service, and per-attribute
5263 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
5264 struct thread *td, accmode_t accmode)
5268 * Kernel-invoked always succeeds.
5274 * Do not allow privileged processes in jail to directly manipulate
5275 * system attributes.
5277 switch (attrnamespace) {
5278 case EXTATTR_NAMESPACE_SYSTEM:
5279 /* Potentially should be: return (EPERM); */
5280 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
5281 case EXTATTR_NAMESPACE_USER:
5282 return (VOP_ACCESS(vp, accmode, cred, td));
5288 #ifdef DEBUG_VFS_LOCKS
5290 * This only exists to suppress warnings from unlocked specfs accesses. It is
5291 * no longer ok to have an unlocked VFS.
5293 #define IGNORE_LOCK(vp) (KERNEL_PANICKED() || (vp) == NULL || \
5294 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
5296 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
5297 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
5298 "Drop into debugger on lock violation");
5300 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
5301 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
5302 0, "Check for interlock across VOPs");
5304 int vfs_badlock_print = 1; /* Print lock violations. */
5305 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
5306 0, "Print lock violations");
5308 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */
5309 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode,
5310 0, "Print vnode details on lock violations");
5313 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
5314 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
5315 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
5319 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
5323 if (vfs_badlock_backtrace)
5326 if (vfs_badlock_vnode)
5327 vn_printf(vp, "vnode ");
5328 if (vfs_badlock_print)
5329 printf("%s: %p %s\n", str, (void *)vp, msg);
5330 if (vfs_badlock_ddb)
5331 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
5335 assert_vi_locked(struct vnode *vp, const char *str)
5338 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
5339 vfs_badlock("interlock is not locked but should be", str, vp);
5343 assert_vi_unlocked(struct vnode *vp, const char *str)
5346 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
5347 vfs_badlock("interlock is locked but should not be", str, vp);
5351 assert_vop_locked(struct vnode *vp, const char *str)
5355 if (!IGNORE_LOCK(vp)) {
5356 locked = VOP_ISLOCKED(vp);
5357 if (locked == 0 || locked == LK_EXCLOTHER)
5358 vfs_badlock("is not locked but should be", str, vp);
5363 assert_vop_unlocked(struct vnode *vp, const char *str)
5366 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
5367 vfs_badlock("is locked but should not be", str, vp);
5371 assert_vop_elocked(struct vnode *vp, const char *str)
5374 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
5375 vfs_badlock("is not exclusive locked but should be", str, vp);
5377 #endif /* DEBUG_VFS_LOCKS */
5380 vop_rename_fail(struct vop_rename_args *ap)
5383 if (ap->a_tvp != NULL)
5385 if (ap->a_tdvp == ap->a_tvp)
5394 vop_rename_pre(void *ap)
5396 struct vop_rename_args *a = ap;
5398 #ifdef DEBUG_VFS_LOCKS
5400 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
5401 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
5402 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
5403 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
5405 /* Check the source (from). */
5406 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
5407 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
5408 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
5409 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
5410 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
5412 /* Check the target. */
5414 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
5415 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
5418 * It may be tempting to add vn_seqc_write_begin/end calls here and
5419 * in vop_rename_post but that's not going to work out since some
5420 * filesystems relookup vnodes mid-rename. This is probably a bug.
5422 * For now filesystems are expected to do the relevant calls after they
5423 * decide what vnodes to operate on.
5425 if (a->a_tdvp != a->a_fdvp)
5427 if (a->a_tvp != a->a_fvp)
5434 #ifdef DEBUG_VFS_LOCKS
5436 vop_fplookup_vexec_debugpre(void *ap __unused)
5439 VFS_SMR_ASSERT_ENTERED();
5443 vop_fplookup_vexec_debugpost(void *ap __unused, int rc __unused)
5446 VFS_SMR_ASSERT_ENTERED();
5450 vop_strategy_debugpre(void *ap)
5452 struct vop_strategy_args *a;
5459 * Cluster ops lock their component buffers but not the IO container.
5461 if ((bp->b_flags & B_CLUSTER) != 0)
5464 if (!KERNEL_PANICKED() && !BUF_ISLOCKED(bp)) {
5465 if (vfs_badlock_print)
5467 "VOP_STRATEGY: bp is not locked but should be\n");
5468 if (vfs_badlock_ddb)
5469 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
5474 vop_lock_debugpre(void *ap)
5476 struct vop_lock1_args *a = ap;
5478 if ((a->a_flags & LK_INTERLOCK) == 0)
5479 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
5481 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
5485 vop_lock_debugpost(void *ap, int rc)
5487 struct vop_lock1_args *a = ap;
5489 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
5490 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
5491 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
5495 vop_unlock_debugpre(void *ap)
5497 struct vop_unlock_args *a = ap;
5499 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
5503 vop_need_inactive_debugpre(void *ap)
5505 struct vop_need_inactive_args *a = ap;
5507 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5511 vop_need_inactive_debugpost(void *ap, int rc)
5513 struct vop_need_inactive_args *a = ap;
5515 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5520 vop_create_pre(void *ap)
5522 struct vop_create_args *a;
5527 vn_seqc_write_begin(dvp);
5531 vop_create_post(void *ap, int rc)
5533 struct vop_create_args *a;
5538 vn_seqc_write_end(dvp);
5540 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5544 vop_whiteout_pre(void *ap)
5546 struct vop_whiteout_args *a;
5551 vn_seqc_write_begin(dvp);
5555 vop_whiteout_post(void *ap, int rc)
5557 struct vop_whiteout_args *a;
5562 vn_seqc_write_end(dvp);
5566 vop_deleteextattr_pre(void *ap)
5568 struct vop_deleteextattr_args *a;
5573 vn_seqc_write_begin(vp);
5577 vop_deleteextattr_post(void *ap, int rc)
5579 struct vop_deleteextattr_args *a;
5584 vn_seqc_write_end(vp);
5586 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5590 vop_link_pre(void *ap)
5592 struct vop_link_args *a;
5593 struct vnode *vp, *tdvp;
5598 vn_seqc_write_begin(vp);
5599 vn_seqc_write_begin(tdvp);
5603 vop_link_post(void *ap, int rc)
5605 struct vop_link_args *a;
5606 struct vnode *vp, *tdvp;
5611 vn_seqc_write_end(vp);
5612 vn_seqc_write_end(tdvp);
5614 VFS_KNOTE_LOCKED(vp, NOTE_LINK);
5615 VFS_KNOTE_LOCKED(tdvp, NOTE_WRITE);
5620 vop_mkdir_pre(void *ap)
5622 struct vop_mkdir_args *a;
5627 vn_seqc_write_begin(dvp);
5631 vop_mkdir_post(void *ap, int rc)
5633 struct vop_mkdir_args *a;
5638 vn_seqc_write_end(dvp);
5640 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE | NOTE_LINK);
5643 #ifdef DEBUG_VFS_LOCKS
5645 vop_mkdir_debugpost(void *ap, int rc)
5647 struct vop_mkdir_args *a;
5651 cache_validate(a->a_dvp, *a->a_vpp, a->a_cnp);
5656 vop_mknod_pre(void *ap)
5658 struct vop_mknod_args *a;
5663 vn_seqc_write_begin(dvp);
5667 vop_mknod_post(void *ap, int rc)
5669 struct vop_mknod_args *a;
5674 vn_seqc_write_end(dvp);
5676 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5680 vop_reclaim_post(void *ap, int rc)
5682 struct vop_reclaim_args *a;
5687 ASSERT_VOP_IN_SEQC(vp);
5689 VFS_KNOTE_LOCKED(vp, NOTE_REVOKE);
5693 vop_remove_pre(void *ap)
5695 struct vop_remove_args *a;
5696 struct vnode *dvp, *vp;
5701 vn_seqc_write_begin(dvp);
5702 vn_seqc_write_begin(vp);
5706 vop_remove_post(void *ap, int rc)
5708 struct vop_remove_args *a;
5709 struct vnode *dvp, *vp;
5714 vn_seqc_write_end(dvp);
5715 vn_seqc_write_end(vp);
5717 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5718 VFS_KNOTE_LOCKED(vp, NOTE_DELETE);
5723 vop_rename_post(void *ap, int rc)
5725 struct vop_rename_args *a = ap;
5730 if (a->a_fdvp == a->a_tdvp) {
5731 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR)
5733 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5734 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5736 hint |= NOTE_EXTEND;
5737 if (a->a_fvp->v_type == VDIR)
5739 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5741 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL &&
5742 a->a_tvp->v_type == VDIR)
5744 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5747 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
5749 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
5751 if (a->a_tdvp != a->a_fdvp)
5753 if (a->a_tvp != a->a_fvp)
5761 vop_rmdir_pre(void *ap)
5763 struct vop_rmdir_args *a;
5764 struct vnode *dvp, *vp;
5769 vn_seqc_write_begin(dvp);
5770 vn_seqc_write_begin(vp);
5774 vop_rmdir_post(void *ap, int rc)
5776 struct vop_rmdir_args *a;
5777 struct vnode *dvp, *vp;
5782 vn_seqc_write_end(dvp);
5783 vn_seqc_write_end(vp);
5785 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE | NOTE_LINK);
5786 VFS_KNOTE_LOCKED(vp, NOTE_DELETE);
5791 vop_setattr_pre(void *ap)
5793 struct vop_setattr_args *a;
5798 vn_seqc_write_begin(vp);
5802 vop_setattr_post(void *ap, int rc)
5804 struct vop_setattr_args *a;
5809 vn_seqc_write_end(vp);
5811 VFS_KNOTE_LOCKED(vp, NOTE_ATTRIB);
5815 vop_setacl_pre(void *ap)
5817 struct vop_setacl_args *a;
5822 vn_seqc_write_begin(vp);
5826 vop_setacl_post(void *ap, int rc __unused)
5828 struct vop_setacl_args *a;
5833 vn_seqc_write_end(vp);
5837 vop_setextattr_pre(void *ap)
5839 struct vop_setextattr_args *a;
5844 vn_seqc_write_begin(vp);
5848 vop_setextattr_post(void *ap, int rc)
5850 struct vop_setextattr_args *a;
5855 vn_seqc_write_end(vp);
5857 VFS_KNOTE_LOCKED(vp, NOTE_ATTRIB);
5861 vop_symlink_pre(void *ap)
5863 struct vop_symlink_args *a;
5868 vn_seqc_write_begin(dvp);
5872 vop_symlink_post(void *ap, int rc)
5874 struct vop_symlink_args *a;
5879 vn_seqc_write_end(dvp);
5881 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5885 vop_open_post(void *ap, int rc)
5887 struct vop_open_args *a = ap;
5890 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN);
5894 vop_close_post(void *ap, int rc)
5896 struct vop_close_args *a = ap;
5898 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */
5899 !VN_IS_DOOMED(a->a_vp))) {
5900 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ?
5901 NOTE_CLOSE_WRITE : NOTE_CLOSE);
5906 vop_read_post(void *ap, int rc)
5908 struct vop_read_args *a = ap;
5911 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5915 vop_read_pgcache_post(void *ap, int rc)
5917 struct vop_read_pgcache_args *a = ap;
5920 VFS_KNOTE_UNLOCKED(a->a_vp, NOTE_READ);
5924 vop_readdir_post(void *ap, int rc)
5926 struct vop_readdir_args *a = ap;
5929 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5932 static struct knlist fs_knlist;
5935 vfs_event_init(void *arg)
5937 knlist_init_mtx(&fs_knlist, NULL);
5939 /* XXX - correct order? */
5940 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
5943 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
5946 KNOTE_UNLOCKED(&fs_knlist, event);
5949 static int filt_fsattach(struct knote *kn);
5950 static void filt_fsdetach(struct knote *kn);
5951 static int filt_fsevent(struct knote *kn, long hint);
5953 struct filterops fs_filtops = {
5955 .f_attach = filt_fsattach,
5956 .f_detach = filt_fsdetach,
5957 .f_event = filt_fsevent
5961 filt_fsattach(struct knote *kn)
5964 kn->kn_flags |= EV_CLEAR;
5965 knlist_add(&fs_knlist, kn, 0);
5970 filt_fsdetach(struct knote *kn)
5973 knlist_remove(&fs_knlist, kn, 0);
5977 filt_fsevent(struct knote *kn, long hint)
5980 kn->kn_fflags |= hint;
5981 return (kn->kn_fflags != 0);
5985 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
5991 error = SYSCTL_IN(req, &vc, sizeof(vc));
5994 if (vc.vc_vers != VFS_CTL_VERS1)
5996 mp = vfs_getvfs(&vc.vc_fsid);
5999 /* ensure that a specific sysctl goes to the right filesystem. */
6000 if (strcmp(vc.vc_fstypename, "*") != 0 &&
6001 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
6005 VCTLTOREQ(&vc, req);
6006 error = VFS_SYSCTL(mp, vc.vc_op, req);
6011 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | CTLFLAG_WR,
6012 NULL, 0, sysctl_vfs_ctl, "",
6016 * Function to initialize a va_filerev field sensibly.
6017 * XXX: Wouldn't a random number make a lot more sense ??
6020 init_va_filerev(void)
6025 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
6028 static int filt_vfsread(struct knote *kn, long hint);
6029 static int filt_vfswrite(struct knote *kn, long hint);
6030 static int filt_vfsvnode(struct knote *kn, long hint);
6031 static void filt_vfsdetach(struct knote *kn);
6032 static struct filterops vfsread_filtops = {
6034 .f_detach = filt_vfsdetach,
6035 .f_event = filt_vfsread
6037 static struct filterops vfswrite_filtops = {
6039 .f_detach = filt_vfsdetach,
6040 .f_event = filt_vfswrite
6042 static struct filterops vfsvnode_filtops = {
6044 .f_detach = filt_vfsdetach,
6045 .f_event = filt_vfsvnode
6049 vfs_knllock(void *arg)
6051 struct vnode *vp = arg;
6053 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
6057 vfs_knlunlock(void *arg)
6059 struct vnode *vp = arg;
6065 vfs_knl_assert_lock(void *arg, int what)
6067 #ifdef DEBUG_VFS_LOCKS
6068 struct vnode *vp = arg;
6070 if (what == LA_LOCKED)
6071 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
6073 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
6078 vfs_kqfilter(struct vop_kqfilter_args *ap)
6080 struct vnode *vp = ap->a_vp;
6081 struct knote *kn = ap->a_kn;
6084 switch (kn->kn_filter) {
6086 kn->kn_fop = &vfsread_filtops;
6089 kn->kn_fop = &vfswrite_filtops;
6092 kn->kn_fop = &vfsvnode_filtops;
6098 kn->kn_hook = (caddr_t)vp;
6101 if (vp->v_pollinfo == NULL)
6103 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
6105 knlist_add(knl, kn, 0);
6111 * Detach knote from vnode
6114 filt_vfsdetach(struct knote *kn)
6116 struct vnode *vp = (struct vnode *)kn->kn_hook;
6118 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
6119 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
6125 filt_vfsread(struct knote *kn, long hint)
6127 struct vnode *vp = (struct vnode *)kn->kn_hook;
6132 * filesystem is gone, so set the EOF flag and schedule
6133 * the knote for deletion.
6135 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
6137 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
6142 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
6146 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
6147 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
6154 filt_vfswrite(struct knote *kn, long hint)
6156 struct vnode *vp = (struct vnode *)kn->kn_hook;
6161 * filesystem is gone, so set the EOF flag and schedule
6162 * the knote for deletion.
6164 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
6165 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
6173 filt_vfsvnode(struct knote *kn, long hint)
6175 struct vnode *vp = (struct vnode *)kn->kn_hook;
6179 if (kn->kn_sfflags & hint)
6180 kn->kn_fflags |= hint;
6181 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
6182 kn->kn_flags |= EV_EOF;
6186 res = (kn->kn_fflags != 0);
6192 * Returns whether the directory is empty or not.
6193 * If it is empty, the return value is 0; otherwise
6194 * the return value is an error value (which may
6198 vfs_emptydir(struct vnode *vp)
6202 struct dirent *dirent, *dp, *endp;
6208 ASSERT_VOP_LOCKED(vp, "vfs_emptydir");
6210 dirent = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK);
6211 iov.iov_base = dirent;
6212 iov.iov_len = sizeof(struct dirent);
6217 uio.uio_resid = sizeof(struct dirent);
6218 uio.uio_segflg = UIO_SYSSPACE;
6219 uio.uio_rw = UIO_READ;
6220 uio.uio_td = curthread;
6222 while (eof == 0 && error == 0) {
6223 error = VOP_READDIR(vp, &uio, curthread->td_ucred, &eof,
6227 endp = (void *)((uint8_t *)dirent +
6228 sizeof(struct dirent) - uio.uio_resid);
6229 for (dp = dirent; dp < endp;
6230 dp = (void *)((uint8_t *)dp + GENERIC_DIRSIZ(dp))) {
6231 if (dp->d_type == DT_WHT)
6233 if (dp->d_namlen == 0)
6235 if (dp->d_type != DT_DIR &&
6236 dp->d_type != DT_UNKNOWN) {
6240 if (dp->d_namlen > 2) {
6244 if (dp->d_namlen == 1 &&
6245 dp->d_name[0] != '.') {
6249 if (dp->d_namlen == 2 &&
6250 dp->d_name[1] != '.') {
6254 uio.uio_resid = sizeof(struct dirent);
6257 free(dirent, M_TEMP);
6262 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
6266 if (dp->d_reclen > ap->a_uio->uio_resid)
6267 return (ENAMETOOLONG);
6268 error = uiomove(dp, dp->d_reclen, ap->a_uio);
6270 if (ap->a_ncookies != NULL) {
6271 if (ap->a_cookies != NULL)
6272 free(ap->a_cookies, M_TEMP);
6273 ap->a_cookies = NULL;
6274 *ap->a_ncookies = 0;
6278 if (ap->a_ncookies == NULL)
6281 KASSERT(ap->a_cookies,
6282 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
6284 *ap->a_cookies = realloc(*ap->a_cookies,
6285 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
6286 (*ap->a_cookies)[*ap->a_ncookies] = off;
6287 *ap->a_ncookies += 1;
6292 * The purpose of this routine is to remove granularity from accmode_t,
6293 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
6294 * VADMIN and VAPPEND.
6296 * If it returns 0, the caller is supposed to continue with the usual
6297 * access checks using 'accmode' as modified by this routine. If it
6298 * returns nonzero value, the caller is supposed to return that value
6301 * Note that after this routine runs, accmode may be zero.
6304 vfs_unixify_accmode(accmode_t *accmode)
6307 * There is no way to specify explicit "deny" rule using
6308 * file mode or POSIX.1e ACLs.
6310 if (*accmode & VEXPLICIT_DENY) {
6316 * None of these can be translated into usual access bits.
6317 * Also, the common case for NFSv4 ACLs is to not contain
6318 * either of these bits. Caller should check for VWRITE
6319 * on the containing directory instead.
6321 if (*accmode & (VDELETE_CHILD | VDELETE))
6324 if (*accmode & VADMIN_PERMS) {
6325 *accmode &= ~VADMIN_PERMS;
6330 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
6331 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
6333 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
6339 * Clear out a doomed vnode (if any) and replace it with a new one as long
6340 * as the fs is not being unmounted. Return the root vnode to the caller.
6342 static int __noinline
6343 vfs_cache_root_fallback(struct mount *mp, int flags, struct vnode **vpp)
6349 if (mp->mnt_rootvnode != NULL) {
6351 vp = mp->mnt_rootvnode;
6353 if (!VN_IS_DOOMED(vp)) {
6356 error = vn_lock(vp, flags);
6365 * Clear the old one.
6367 mp->mnt_rootvnode = NULL;
6371 vfs_op_barrier_wait(mp);
6375 error = VFS_CACHEDROOT(mp, flags, vpp);
6378 if (mp->mnt_vfs_ops == 0) {
6380 if (mp->mnt_vfs_ops != 0) {
6384 if (mp->mnt_rootvnode == NULL) {
6386 mp->mnt_rootvnode = *vpp;
6388 if (mp->mnt_rootvnode != *vpp) {
6389 if (!VN_IS_DOOMED(mp->mnt_rootvnode)) {
6390 panic("%s: mismatch between vnode returned "
6391 " by VFS_CACHEDROOT and the one cached "
6393 __func__, *vpp, mp->mnt_rootvnode);
6403 vfs_cache_root(struct mount *mp, int flags, struct vnode **vpp)
6405 struct mount_pcpu *mpcpu;
6409 if (!vfs_op_thread_enter(mp, mpcpu))
6410 return (vfs_cache_root_fallback(mp, flags, vpp));
6411 vp = atomic_load_ptr(&mp->mnt_rootvnode);
6412 if (vp == NULL || VN_IS_DOOMED(vp)) {
6413 vfs_op_thread_exit(mp, mpcpu);
6414 return (vfs_cache_root_fallback(mp, flags, vpp));
6417 vfs_op_thread_exit(mp, mpcpu);
6418 error = vn_lock(vp, flags);
6421 return (vfs_cache_root_fallback(mp, flags, vpp));
6428 vfs_cache_root_clear(struct mount *mp)
6433 * ops > 0 guarantees there is nobody who can see this vnode
6435 MPASS(mp->mnt_vfs_ops > 0);
6436 vp = mp->mnt_rootvnode;
6438 vn_seqc_write_begin(vp);
6439 mp->mnt_rootvnode = NULL;
6444 vfs_cache_root_set(struct mount *mp, struct vnode *vp)
6447 MPASS(mp->mnt_vfs_ops > 0);
6449 mp->mnt_rootvnode = vp;
6453 * These are helper functions for filesystems to traverse all
6454 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
6456 * This interface replaces MNT_VNODE_FOREACH.
6460 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
6465 kern_yield(PRI_USER);
6467 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6468 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL;
6469 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
6470 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */
6471 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp))
6474 if (VN_IS_DOOMED(vp)) {
6481 __mnt_vnode_markerfree_all(mvp, mp);
6482 /* MNT_IUNLOCK(mp); -- done in above function */
6483 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
6486 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
6487 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
6493 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
6497 *mvp = vn_alloc_marker(mp);
6501 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
6502 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */
6503 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp))
6506 if (VN_IS_DOOMED(vp)) {
6515 vn_free_marker(*mvp);
6519 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
6525 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
6533 mtx_assert(MNT_MTX(mp), MA_OWNED);
6535 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6536 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
6539 vn_free_marker(*mvp);
6544 * These are helper functions for filesystems to traverse their
6545 * lazy vnodes. See MNT_VNODE_FOREACH_LAZY() in sys/mount.h
6548 mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp)
6551 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6556 vn_free_marker(*mvp);
6561 * Relock the mp mount vnode list lock with the vp vnode interlock in the
6562 * conventional lock order during mnt_vnode_next_lazy iteration.
6564 * On entry, the mount vnode list lock is held and the vnode interlock is not.
6565 * The list lock is dropped and reacquired. On success, both locks are held.
6566 * On failure, the mount vnode list lock is held but the vnode interlock is
6567 * not, and the procedure may have yielded.
6570 mnt_vnode_next_lazy_relock(struct vnode *mvp, struct mount *mp,
6574 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER &&
6575 TAILQ_NEXT(mvp, v_lazylist) != NULL, mvp,
6576 ("%s: bad marker", __func__));
6577 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp,
6578 ("%s: inappropriate vnode", __func__));
6579 ASSERT_VI_UNLOCKED(vp, __func__);
6580 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6582 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, mvp, v_lazylist);
6583 TAILQ_INSERT_BEFORE(vp, mvp, v_lazylist);
6586 * Note we may be racing against vdrop which transitioned the hold
6587 * count to 0 and now waits for the ->mnt_listmtx lock. This is fine,
6588 * if we are the only user after we get the interlock we will just
6592 mtx_unlock(&mp->mnt_listmtx);
6594 if (VN_IS_DOOMED(vp)) {
6595 VNPASS((vp->v_mflag & VMP_LAZYLIST) == 0, vp);
6598 VNPASS(vp->v_mflag & VMP_LAZYLIST, vp);
6600 * There is nothing to do if we are the last user.
6602 if (!refcount_release_if_not_last(&vp->v_holdcnt))
6604 mtx_lock(&mp->mnt_listmtx);
6609 mtx_lock(&mp->mnt_listmtx);
6613 static struct vnode *
6614 mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6619 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6620 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6622 vp = TAILQ_NEXT(*mvp, v_lazylist);
6623 while (vp != NULL) {
6624 if (vp->v_type == VMARKER) {
6625 vp = TAILQ_NEXT(vp, v_lazylist);
6629 * See if we want to process the vnode. Note we may encounter a
6630 * long string of vnodes we don't care about and hog the list
6631 * as a result. Check for it and requeue the marker.
6633 VNPASS(!VN_IS_DOOMED(vp), vp);
6634 if (!cb(vp, cbarg)) {
6635 if (!should_yield()) {
6636 vp = TAILQ_NEXT(vp, v_lazylist);
6639 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp,
6641 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp,
6643 mtx_unlock(&mp->mnt_listmtx);
6644 kern_yield(PRI_USER);
6645 mtx_lock(&mp->mnt_listmtx);
6649 * Try-lock because this is the wrong lock order.
6651 if (!VI_TRYLOCK(vp) &&
6652 !mnt_vnode_next_lazy_relock(*mvp, mp, vp))
6654 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
6655 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
6656 ("alien vnode on the lazy list %p %p", vp, mp));
6657 VNPASS(vp->v_mount == mp, vp);
6658 VNPASS(!VN_IS_DOOMED(vp), vp);
6661 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist);
6663 /* Check if we are done */
6665 mtx_unlock(&mp->mnt_listmtx);
6666 mnt_vnode_markerfree_lazy(mvp, mp);
6669 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, v_lazylist);
6670 mtx_unlock(&mp->mnt_listmtx);
6671 ASSERT_VI_LOCKED(vp, "lazy iter");
6676 __mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6681 kern_yield(PRI_USER);
6682 mtx_lock(&mp->mnt_listmtx);
6683 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg));
6687 __mnt_vnode_first_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6692 if (TAILQ_EMPTY(&mp->mnt_lazyvnodelist))
6695 *mvp = vn_alloc_marker(mp);
6700 mtx_lock(&mp->mnt_listmtx);
6701 vp = TAILQ_FIRST(&mp->mnt_lazyvnodelist);
6703 mtx_unlock(&mp->mnt_listmtx);
6704 mnt_vnode_markerfree_lazy(mvp, mp);
6707 TAILQ_INSERT_BEFORE(vp, *mvp, v_lazylist);
6708 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg));
6712 __mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp)
6718 mtx_lock(&mp->mnt_listmtx);
6719 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist);
6720 mtx_unlock(&mp->mnt_listmtx);
6721 mnt_vnode_markerfree_lazy(mvp, mp);
6725 vn_dir_check_exec(struct vnode *vp, struct componentname *cnp)
6728 if ((cnp->cn_flags & NOEXECCHECK) != 0) {
6729 cnp->cn_flags &= ~NOEXECCHECK;
6733 return (VOP_ACCESS(vp, VEXEC, cnp->cn_cred, cnp->cn_thread));
6737 * Do not use this variant unless you have means other than the hold count
6738 * to prevent the vnode from getting freed.
6741 vn_seqc_write_begin_unheld_locked(struct vnode *vp)
6744 ASSERT_VI_LOCKED(vp, __func__);
6745 VNPASS(vp->v_seqc_users >= 0, vp);
6747 if (vp->v_seqc_users == 1)
6748 seqc_sleepable_write_begin(&vp->v_seqc);
6752 vn_seqc_write_begin_locked(struct vnode *vp)
6755 ASSERT_VI_LOCKED(vp, __func__);
6756 VNPASS(vp->v_holdcnt > 0, vp);
6757 vn_seqc_write_begin_unheld_locked(vp);
6761 vn_seqc_write_begin(struct vnode *vp)
6765 vn_seqc_write_begin_locked(vp);
6770 vn_seqc_write_begin_unheld(struct vnode *vp)
6774 vn_seqc_write_begin_unheld_locked(vp);
6779 vn_seqc_write_end_locked(struct vnode *vp)
6782 ASSERT_VI_LOCKED(vp, __func__);
6783 VNPASS(vp->v_seqc_users > 0, vp);
6785 if (vp->v_seqc_users == 0)
6786 seqc_sleepable_write_end(&vp->v_seqc);
6790 vn_seqc_write_end(struct vnode *vp)
6794 vn_seqc_write_end_locked(vp);