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 vn_seqc_init(struct vnode *);
112 static void vn_seqc_write_end_free(struct vnode *vp);
113 static void vgonel(struct vnode *);
114 static bool vhold_recycle_free(struct vnode *);
115 static void vfs_knllock(void *arg);
116 static void vfs_knlunlock(void *arg);
117 static void vfs_knl_assert_lock(void *arg, int what);
118 static void destroy_vpollinfo(struct vpollinfo *vi);
119 static int v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
120 daddr_t startlbn, daddr_t endlbn);
121 static void vnlru_recalc(void);
124 * These fences are intended for cases where some synchronization is
125 * needed between access of v_iflags and lockless vnode refcount (v_holdcnt
126 * and v_usecount) updates. Access to v_iflags is generally synchronized
127 * by the interlock, but we have some internal assertions that check vnode
128 * flags without acquiring the lock. Thus, these fences are INVARIANTS-only
132 #define VNODE_REFCOUNT_FENCE_ACQ() atomic_thread_fence_acq()
133 #define VNODE_REFCOUNT_FENCE_REL() atomic_thread_fence_rel()
135 #define VNODE_REFCOUNT_FENCE_ACQ()
136 #define VNODE_REFCOUNT_FENCE_REL()
140 * Number of vnodes in existence. Increased whenever getnewvnode()
141 * allocates a new vnode, decreased in vdropl() for VIRF_DOOMED vnode.
143 static u_long __exclusive_cache_line numvnodes;
145 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
146 "Number of vnodes in existence");
148 static counter_u64_t vnodes_created;
149 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
150 "Number of vnodes created by getnewvnode");
153 * Conversion tables for conversion from vnode types to inode formats
156 enum vtype iftovt_tab[16] = {
157 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
158 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
160 int vttoif_tab[10] = {
161 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
162 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
166 * List of allocates vnodes in the system.
168 static TAILQ_HEAD(freelst, vnode) vnode_list;
169 static struct vnode *vnode_list_free_marker;
170 static struct vnode *vnode_list_reclaim_marker;
173 * "Free" vnode target. Free vnodes are rarely completely free, but are
174 * just ones that are cheap to recycle. Usually they are for files which
175 * have been stat'd but not read; these usually have inode and namecache
176 * data attached to them. This target is the preferred minimum size of a
177 * sub-cache consisting mostly of such files. The system balances the size
178 * of this sub-cache with its complement to try to prevent either from
179 * thrashing while the other is relatively inactive. The targets express
180 * a preference for the best balance.
182 * "Above" this target there are 2 further targets (watermarks) related
183 * to recyling of free vnodes. In the best-operating case, the cache is
184 * exactly full, the free list has size between vlowat and vhiwat above the
185 * free target, and recycling from it and normal use maintains this state.
186 * Sometimes the free list is below vlowat or even empty, but this state
187 * is even better for immediate use provided the cache is not full.
188 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free
189 * ones) to reach one of these states. The watermarks are currently hard-
190 * coded as 4% and 9% of the available space higher. These and the default
191 * of 25% for wantfreevnodes are too large if the memory size is large.
192 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim
193 * whenever vnlru_proc() becomes active.
195 static long wantfreevnodes;
196 static long __exclusive_cache_line freevnodes;
197 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD,
198 &freevnodes, 0, "Number of \"free\" vnodes");
199 static long freevnodes_old;
201 static counter_u64_t recycles_count;
202 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count,
203 "Number of vnodes recycled to meet vnode cache targets");
205 static counter_u64_t recycles_free_count;
206 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles_free, CTLFLAG_RD, &recycles_free_count,
207 "Number of free vnodes recycled to meet vnode cache targets");
209 static counter_u64_t deferred_inact;
210 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, deferred_inact, CTLFLAG_RD, &deferred_inact,
211 "Number of times inactive processing was deferred");
213 /* To keep more than one thread at a time from running vfs_getnewfsid */
214 static struct mtx mntid_mtx;
217 * Lock for any access to the following:
222 static struct mtx __exclusive_cache_line vnode_list_mtx;
224 /* Publicly exported FS */
225 struct nfs_public nfs_pub;
227 static uma_zone_t buf_trie_zone;
228 static smr_t buf_trie_smr;
230 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
231 static uma_zone_t vnode_zone;
232 MALLOC_DEFINE(M_VNODEPOLL, "VN POLL", "vnode poll");
234 __read_frequently smr_t vfs_smr;
237 * The workitem queue.
239 * It is useful to delay writes of file data and filesystem metadata
240 * for tens of seconds so that quickly created and deleted files need
241 * not waste disk bandwidth being created and removed. To realize this,
242 * we append vnodes to a "workitem" queue. When running with a soft
243 * updates implementation, most pending metadata dependencies should
244 * not wait for more than a few seconds. Thus, mounted on block devices
245 * are delayed only about a half the time that file data is delayed.
246 * Similarly, directory updates are more critical, so are only delayed
247 * about a third the time that file data is delayed. Thus, there are
248 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
249 * one each second (driven off the filesystem syncer process). The
250 * syncer_delayno variable indicates the next queue that is to be processed.
251 * Items that need to be processed soon are placed in this queue:
253 * syncer_workitem_pending[syncer_delayno]
255 * A delay of fifteen seconds is done by placing the request fifteen
256 * entries later in the queue:
258 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
261 static int syncer_delayno;
262 static long syncer_mask;
263 LIST_HEAD(synclist, bufobj);
264 static struct synclist *syncer_workitem_pending;
266 * The sync_mtx protects:
271 * syncer_workitem_pending
272 * syncer_worklist_len
275 static struct mtx sync_mtx;
276 static struct cv sync_wakeup;
278 #define SYNCER_MAXDELAY 32
279 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
280 static int syncdelay = 30; /* max time to delay syncing data */
281 static int filedelay = 30; /* time to delay syncing files */
282 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
283 "Time to delay syncing files (in seconds)");
284 static int dirdelay = 29; /* time to delay syncing directories */
285 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
286 "Time to delay syncing directories (in seconds)");
287 static int metadelay = 28; /* time to delay syncing metadata */
288 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
289 "Time to delay syncing metadata (in seconds)");
290 static int rushjob; /* number of slots to run ASAP */
291 static int stat_rush_requests; /* number of times I/O speeded up */
292 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
293 "Number of times I/O speeded up (rush requests)");
295 #define VDBATCH_SIZE 8
300 struct vnode *tab[VDBATCH_SIZE];
302 DPCPU_DEFINE_STATIC(struct vdbatch, vd);
304 static void vdbatch_dequeue(struct vnode *vp);
307 * When shutting down the syncer, run it at four times normal speed.
309 #define SYNCER_SHUTDOWN_SPEEDUP 4
310 static int sync_vnode_count;
311 static int syncer_worklist_len;
312 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
315 /* Target for maximum number of vnodes. */
316 u_long desiredvnodes;
317 static u_long gapvnodes; /* gap between wanted and desired */
318 static u_long vhiwat; /* enough extras after expansion */
319 static u_long vlowat; /* minimal extras before expansion */
320 static u_long vstir; /* nonzero to stir non-free vnodes */
321 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */
323 static u_long vnlru_read_freevnodes(void);
326 * Note that no attempt is made to sanitize these parameters.
329 sysctl_maxvnodes(SYSCTL_HANDLER_ARGS)
335 error = sysctl_handle_long(oidp, &val, 0, req);
336 if (error != 0 || req->newptr == NULL)
339 if (val == desiredvnodes)
341 mtx_lock(&vnode_list_mtx);
343 wantfreevnodes = desiredvnodes / 4;
345 mtx_unlock(&vnode_list_mtx);
347 * XXX There is no protection against multiple threads changing
348 * desiredvnodes at the same time. Locking above only helps vnlru and
351 vfs_hash_changesize(desiredvnodes);
352 cache_changesize(desiredvnodes);
356 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
357 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_maxvnodes,
358 "LU", "Target for maximum number of vnodes");
361 sysctl_wantfreevnodes(SYSCTL_HANDLER_ARGS)
366 val = wantfreevnodes;
367 error = sysctl_handle_long(oidp, &val, 0, req);
368 if (error != 0 || req->newptr == NULL)
371 if (val == wantfreevnodes)
373 mtx_lock(&vnode_list_mtx);
374 wantfreevnodes = val;
376 mtx_unlock(&vnode_list_mtx);
380 SYSCTL_PROC(_vfs, OID_AUTO, wantfreevnodes,
381 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_wantfreevnodes,
382 "LU", "Target for minimum number of \"free\" vnodes");
384 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
385 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)");
386 static int vnlru_nowhere;
387 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
388 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
391 sysctl_try_reclaim_vnode(SYSCTL_HANDLER_ARGS)
396 unsigned long ndflags;
399 if (req->newptr == NULL)
401 if (req->newlen >= PATH_MAX)
404 buf = malloc(PATH_MAX, M_TEMP, M_WAITOK);
405 error = SYSCTL_IN(req, buf, req->newlen);
409 buf[req->newlen] = '\0';
411 ndflags = LOCKLEAF | NOFOLLOW | AUDITVNODE1 | SAVENAME;
412 NDINIT(&nd, LOOKUP, ndflags, UIO_SYSSPACE, buf, curthread);
413 if ((error = namei(&nd)) != 0)
417 if (VN_IS_DOOMED(vp)) {
419 * This vnode is being recycled. Return != 0 to let the caller
420 * know that the sysctl had no effect. Return EAGAIN because a
421 * subsequent call will likely succeed (since namei will create
422 * a new vnode if necessary)
428 counter_u64_add(recycles_count, 1);
438 sysctl_ftry_reclaim_vnode(SYSCTL_HANDLER_ARGS)
440 struct thread *td = curthread;
446 if (req->newptr == NULL)
449 error = sysctl_handle_int(oidp, &fd, 0, req);
452 error = getvnode(curthread, fd, &cap_fcntl_rights, &fp);
457 error = vn_lock(vp, LK_EXCLUSIVE);
461 counter_u64_add(recycles_count, 1);
469 SYSCTL_PROC(_debug, OID_AUTO, try_reclaim_vnode,
470 CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
471 sysctl_try_reclaim_vnode, "A", "Try to reclaim a vnode by its pathname");
472 SYSCTL_PROC(_debug, OID_AUTO, ftry_reclaim_vnode,
473 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
474 sysctl_ftry_reclaim_vnode, "I",
475 "Try to reclaim a vnode by its file descriptor");
477 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
481 * Support for the bufobj clean & dirty pctrie.
484 buf_trie_alloc(struct pctrie *ptree)
486 return (uma_zalloc_smr(buf_trie_zone, M_NOWAIT));
490 buf_trie_free(struct pctrie *ptree, void *node)
492 uma_zfree_smr(buf_trie_zone, node);
494 PCTRIE_DEFINE_SMR(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free,
498 * Initialize the vnode management data structures.
500 * Reevaluate the following cap on the number of vnodes after the physical
501 * memory size exceeds 512GB. In the limit, as the physical memory size
502 * grows, the ratio of the memory size in KB to vnodes approaches 64:1.
504 #ifndef MAXVNODES_MAX
505 #define MAXVNODES_MAX (512UL * 1024 * 1024 / 64) /* 8M */
508 static MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
510 static struct vnode *
511 vn_alloc_marker(struct mount *mp)
515 vp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
516 vp->v_type = VMARKER;
523 vn_free_marker(struct vnode *vp)
526 MPASS(vp->v_type == VMARKER);
527 free(vp, M_VNODE_MARKER);
531 * Initialize a vnode as it first enters the zone.
534 vnode_init(void *mem, int size, int flags)
543 vp->v_vnlock = &vp->v_lock;
544 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
546 * By default, don't allow shared locks unless filesystems opt-in.
548 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT,
549 LK_NOSHARE | LK_IS_VNODE);
553 bufobj_init(&vp->v_bufobj, vp);
555 * Initialize namecache.
557 cache_vnode_init(vp);
559 * Initialize rangelocks.
561 rangelock_init(&vp->v_rl);
563 vp->v_dbatchcpu = NOCPU;
566 * Check vhold_recycle_free for an explanation.
568 vp->v_holdcnt = VHOLD_NO_SMR;
570 mtx_lock(&vnode_list_mtx);
571 TAILQ_INSERT_BEFORE(vnode_list_free_marker, vp, v_vnodelist);
572 mtx_unlock(&vnode_list_mtx);
577 * Free a vnode when it is cleared from the zone.
580 vnode_fini(void *mem, int size)
587 mtx_lock(&vnode_list_mtx);
588 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist);
589 mtx_unlock(&vnode_list_mtx);
590 rangelock_destroy(&vp->v_rl);
591 lockdestroy(vp->v_vnlock);
592 mtx_destroy(&vp->v_interlock);
594 rw_destroy(BO_LOCKPTR(bo));
598 * Provide the size of NFS nclnode and NFS fh for calculation of the
599 * vnode memory consumption. The size is specified directly to
600 * eliminate dependency on NFS-private header.
602 * Other filesystems may use bigger or smaller (like UFS and ZFS)
603 * private inode data, but the NFS-based estimation is ample enough.
604 * Still, we care about differences in the size between 64- and 32-bit
607 * Namecache structure size is heuristically
608 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1.
611 #define NFS_NCLNODE_SZ (528 + 64)
614 #define NFS_NCLNODE_SZ (360 + 32)
619 vntblinit(void *dummy __unused)
622 int cpu, physvnodes, virtvnodes;
626 * Desiredvnodes is a function of the physical memory size and the
627 * kernel's heap size. Generally speaking, it scales with the
628 * physical memory size. The ratio of desiredvnodes to the physical
629 * memory size is 1:16 until desiredvnodes exceeds 98,304.
631 * marginal ratio of desiredvnodes to the physical memory size is
632 * 1:64. However, desiredvnodes is limited by the kernel's heap
633 * size. The memory required by desiredvnodes vnodes and vm objects
634 * must not exceed 1/10th of the kernel's heap size.
636 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 +
637 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64;
638 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) +
639 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ));
640 desiredvnodes = min(physvnodes, virtvnodes);
641 if (desiredvnodes > MAXVNODES_MAX) {
643 printf("Reducing kern.maxvnodes %lu -> %lu\n",
644 desiredvnodes, MAXVNODES_MAX);
645 desiredvnodes = MAXVNODES_MAX;
647 wantfreevnodes = desiredvnodes / 4;
648 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
649 TAILQ_INIT(&vnode_list);
650 mtx_init(&vnode_list_mtx, "vnode_list", NULL, MTX_DEF);
652 * The lock is taken to appease WITNESS.
654 mtx_lock(&vnode_list_mtx);
656 mtx_unlock(&vnode_list_mtx);
657 vnode_list_free_marker = vn_alloc_marker(NULL);
658 TAILQ_INSERT_HEAD(&vnode_list, vnode_list_free_marker, v_vnodelist);
659 vnode_list_reclaim_marker = vn_alloc_marker(NULL);
660 TAILQ_INSERT_HEAD(&vnode_list, vnode_list_reclaim_marker, v_vnodelist);
661 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
662 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0);
663 uma_zone_set_smr(vnode_zone, vfs_smr);
665 * Preallocate enough nodes to support one-per buf so that
666 * we can not fail an insert. reassignbuf() callers can not
667 * tolerate the insertion failure.
669 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
670 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
671 UMA_ZONE_NOFREE | UMA_ZONE_SMR);
672 buf_trie_smr = uma_zone_get_smr(buf_trie_zone);
673 uma_prealloc(buf_trie_zone, nbuf);
675 vnodes_created = counter_u64_alloc(M_WAITOK);
676 recycles_count = counter_u64_alloc(M_WAITOK);
677 recycles_free_count = counter_u64_alloc(M_WAITOK);
678 deferred_inact = counter_u64_alloc(M_WAITOK);
681 * Initialize the filesystem syncer.
683 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
685 syncer_maxdelay = syncer_mask + 1;
686 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
687 cv_init(&sync_wakeup, "syncer");
688 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
693 vd = DPCPU_ID_PTR((cpu), vd);
694 bzero(vd, sizeof(*vd));
695 mtx_init(&vd->lock, "vdbatch", NULL, MTX_DEF);
698 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
701 * Mark a mount point as busy. Used to synchronize access and to delay
702 * unmounting. Eventually, mountlist_mtx is not released on failure.
704 * vfs_busy() is a custom lock, it can block the caller.
705 * vfs_busy() only sleeps if the unmount is active on the mount point.
706 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
707 * vnode belonging to mp.
709 * Lookup uses vfs_busy() to traverse mount points.
711 * / vnode lock A / vnode lock (/var) D
712 * /var vnode lock B /log vnode lock(/var/log) E
713 * vfs_busy lock C vfs_busy lock F
715 * Within each file system, the lock order is C->A->B and F->D->E.
717 * When traversing across mounts, the system follows that lock order:
723 * The lookup() process for namei("/var") illustrates the process:
724 * VOP_LOOKUP() obtains B while A is held
725 * vfs_busy() obtains a shared lock on F while A and B are held
726 * vput() releases lock on B
727 * vput() releases lock on A
728 * VFS_ROOT() obtains lock on D while shared lock on F is held
729 * vfs_unbusy() releases shared lock on F
730 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
731 * Attempt to lock A (instead of vp_crossmp) while D is held would
732 * violate the global order, causing deadlocks.
734 * dounmount() locks B while F is drained.
737 vfs_busy(struct mount *mp, int flags)
739 struct mount_pcpu *mpcpu;
741 MPASS((flags & ~MBF_MASK) == 0);
742 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
744 if (vfs_op_thread_enter(mp, mpcpu)) {
745 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
746 MPASS((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0);
747 MPASS((mp->mnt_kern_flag & MNTK_REFEXPIRE) == 0);
748 vfs_mp_count_add_pcpu(mpcpu, ref, 1);
749 vfs_mp_count_add_pcpu(mpcpu, lockref, 1);
750 vfs_op_thread_exit(mp, mpcpu);
751 if (flags & MBF_MNTLSTLOCK)
752 mtx_unlock(&mountlist_mtx);
757 vfs_assert_mount_counters(mp);
760 * If mount point is currently being unmounted, sleep until the
761 * mount point fate is decided. If thread doing the unmounting fails,
762 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
763 * that this mount point has survived the unmount attempt and vfs_busy
764 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
765 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
766 * about to be really destroyed. vfs_busy needs to release its
767 * reference on the mount point in this case and return with ENOENT,
768 * telling the caller that mount mount it tried to busy is no longer
771 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
772 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
775 CTR1(KTR_VFS, "%s: failed busying before sleeping",
779 if (flags & MBF_MNTLSTLOCK)
780 mtx_unlock(&mountlist_mtx);
781 mp->mnt_kern_flag |= MNTK_MWAIT;
782 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
783 if (flags & MBF_MNTLSTLOCK)
784 mtx_lock(&mountlist_mtx);
787 if (flags & MBF_MNTLSTLOCK)
788 mtx_unlock(&mountlist_mtx);
795 * Free a busy filesystem.
798 vfs_unbusy(struct mount *mp)
800 struct mount_pcpu *mpcpu;
803 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
805 if (vfs_op_thread_enter(mp, mpcpu)) {
806 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
807 vfs_mp_count_sub_pcpu(mpcpu, lockref, 1);
808 vfs_mp_count_sub_pcpu(mpcpu, ref, 1);
809 vfs_op_thread_exit(mp, mpcpu);
814 vfs_assert_mount_counters(mp);
816 c = --mp->mnt_lockref;
817 if (mp->mnt_vfs_ops == 0) {
818 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
823 vfs_dump_mount_counters(mp);
824 if (c == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
825 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
826 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
827 mp->mnt_kern_flag &= ~MNTK_DRAINING;
828 wakeup(&mp->mnt_lockref);
834 * Lookup a mount point by filesystem identifier.
837 vfs_getvfs(fsid_t *fsid)
841 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
842 mtx_lock(&mountlist_mtx);
843 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
844 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0) {
846 mtx_unlock(&mountlist_mtx);
850 mtx_unlock(&mountlist_mtx);
851 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
852 return ((struct mount *) 0);
856 * Lookup a mount point by filesystem identifier, busying it before
859 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
860 * cache for popular filesystem identifiers. The cache is lockess, using
861 * the fact that struct mount's are never freed. In worst case we may
862 * get pointer to unmounted or even different filesystem, so we have to
863 * check what we got, and go slow way if so.
866 vfs_busyfs(fsid_t *fsid)
868 #define FSID_CACHE_SIZE 256
869 typedef struct mount * volatile vmp_t;
870 static vmp_t cache[FSID_CACHE_SIZE];
875 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
876 hash = fsid->val[0] ^ fsid->val[1];
877 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
879 if (mp == NULL || fsidcmp(&mp->mnt_stat.f_fsid, fsid) != 0)
881 if (vfs_busy(mp, 0) != 0) {
885 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0)
891 mtx_lock(&mountlist_mtx);
892 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
893 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0) {
894 error = vfs_busy(mp, MBF_MNTLSTLOCK);
897 mtx_unlock(&mountlist_mtx);
904 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
905 mtx_unlock(&mountlist_mtx);
906 return ((struct mount *) 0);
910 * Check if a user can access privileged mount options.
913 vfs_suser(struct mount *mp, struct thread *td)
917 if (jailed(td->td_ucred)) {
919 * If the jail of the calling thread lacks permission for
920 * this type of file system, deny immediately.
922 if (!prison_allow(td->td_ucred, mp->mnt_vfc->vfc_prison_flag))
926 * If the file system was mounted outside the jail of the
927 * calling thread, deny immediately.
929 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
934 * If file system supports delegated administration, we don't check
935 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
936 * by the file system itself.
937 * If this is not the user that did original mount, we check for
938 * the PRIV_VFS_MOUNT_OWNER privilege.
940 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
941 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
942 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
949 * Get a new unique fsid. Try to make its val[0] unique, since this value
950 * will be used to create fake device numbers for stat(). Also try (but
951 * not so hard) make its val[0] unique mod 2^16, since some emulators only
952 * support 16-bit device numbers. We end up with unique val[0]'s for the
953 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
955 * Keep in mind that several mounts may be running in parallel. Starting
956 * the search one past where the previous search terminated is both a
957 * micro-optimization and a defense against returning the same fsid to
961 vfs_getnewfsid(struct mount *mp)
963 static uint16_t mntid_base;
968 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
969 mtx_lock(&mntid_mtx);
970 mtype = mp->mnt_vfc->vfc_typenum;
971 tfsid.val[1] = mtype;
972 mtype = (mtype & 0xFF) << 24;
974 tfsid.val[0] = makedev(255,
975 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
977 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
981 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
982 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
983 mtx_unlock(&mntid_mtx);
987 * Knob to control the precision of file timestamps:
989 * 0 = seconds only; nanoseconds zeroed.
990 * 1 = seconds and nanoseconds, accurate within 1/HZ.
991 * 2 = seconds and nanoseconds, truncated to microseconds.
992 * >=3 = seconds and nanoseconds, maximum precision.
994 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
996 static int timestamp_precision = TSP_USEC;
997 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
998 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
999 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, "
1000 "3+: sec + ns (max. precision))");
1003 * Get a current timestamp.
1006 vfs_timestamp(struct timespec *tsp)
1010 switch (timestamp_precision) {
1012 tsp->tv_sec = time_second;
1020 TIMEVAL_TO_TIMESPEC(&tv, tsp);
1030 * Set vnode attributes to VNOVAL
1033 vattr_null(struct vattr *vap)
1036 vap->va_type = VNON;
1037 vap->va_size = VNOVAL;
1038 vap->va_bytes = VNOVAL;
1039 vap->va_mode = VNOVAL;
1040 vap->va_nlink = VNOVAL;
1041 vap->va_uid = VNOVAL;
1042 vap->va_gid = VNOVAL;
1043 vap->va_fsid = VNOVAL;
1044 vap->va_fileid = VNOVAL;
1045 vap->va_blocksize = VNOVAL;
1046 vap->va_rdev = VNOVAL;
1047 vap->va_atime.tv_sec = VNOVAL;
1048 vap->va_atime.tv_nsec = VNOVAL;
1049 vap->va_mtime.tv_sec = VNOVAL;
1050 vap->va_mtime.tv_nsec = VNOVAL;
1051 vap->va_ctime.tv_sec = VNOVAL;
1052 vap->va_ctime.tv_nsec = VNOVAL;
1053 vap->va_birthtime.tv_sec = VNOVAL;
1054 vap->va_birthtime.tv_nsec = VNOVAL;
1055 vap->va_flags = VNOVAL;
1056 vap->va_gen = VNOVAL;
1057 vap->va_vaflags = 0;
1061 * Try to reduce the total number of vnodes.
1063 * This routine (and its user) are buggy in at least the following ways:
1064 * - all parameters were picked years ago when RAM sizes were significantly
1066 * - it can pick vnodes based on pages used by the vm object, but filesystems
1067 * like ZFS don't use it making the pick broken
1068 * - since ZFS has its own aging policy it gets partially combated by this one
1069 * - a dedicated method should be provided for filesystems to let them decide
1070 * whether the vnode should be recycled
1072 * This routine is called when we have too many vnodes. It attempts
1073 * to free <count> vnodes and will potentially free vnodes that still
1074 * have VM backing store (VM backing store is typically the cause
1075 * of a vnode blowout so we want to do this). Therefore, this operation
1076 * is not considered cheap.
1078 * A number of conditions may prevent a vnode from being reclaimed.
1079 * the buffer cache may have references on the vnode, a directory
1080 * vnode may still have references due to the namei cache representing
1081 * underlying files, or the vnode may be in active use. It is not
1082 * desirable to reuse such vnodes. These conditions may cause the
1083 * number of vnodes to reach some minimum value regardless of what
1084 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
1086 * @param reclaim_nc_src Only reclaim directories with outgoing namecache
1087 * entries if this argument is strue
1088 * @param trigger Only reclaim vnodes with fewer than this many resident
1090 * @param target How many vnodes to reclaim.
1091 * @return The number of vnodes that were reclaimed.
1094 vlrureclaim(bool reclaim_nc_src, int trigger, u_long target)
1096 struct vnode *vp, *mvp;
1098 struct vm_object *object;
1102 mtx_assert(&vnode_list_mtx, MA_OWNED);
1107 mvp = vnode_list_reclaim_marker;
1110 while (done < target) {
1111 vp = TAILQ_NEXT(vp, v_vnodelist);
1112 if (__predict_false(vp == NULL))
1115 if (__predict_false(vp->v_type == VMARKER))
1119 * If it's been deconstructed already, it's still
1120 * referenced, or it exceeds the trigger, skip it.
1121 * Also skip free vnodes. We are trying to make space
1122 * to expand the free list, not reduce it.
1124 if (vp->v_usecount > 0 || vp->v_holdcnt == 0 ||
1125 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)))
1128 if (vp->v_type == VBAD || vp->v_type == VNON)
1131 object = atomic_load_ptr(&vp->v_object);
1132 if (object == NULL || object->resident_page_count > trigger) {
1137 * Handle races against vnode allocation. Filesystems lock the
1138 * vnode some time after it gets returned from getnewvnode,
1139 * despite type and hold count being manipulated earlier.
1140 * Resorting to checking v_mount restores guarantees present
1141 * before the global list was reworked to contain all vnodes.
1143 if (!VI_TRYLOCK(vp))
1145 if (__predict_false(vp->v_type == VBAD || vp->v_type == VNON)) {
1149 if (vp->v_mount == NULL) {
1155 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1156 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist);
1157 mtx_unlock(&vnode_list_mtx);
1159 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1161 goto next_iter_unlocked;
1163 if (VOP_LOCK(vp, LK_EXCLUSIVE|LK_NOWAIT) != 0) {
1165 vn_finished_write(mp);
1166 goto next_iter_unlocked;
1170 if (vp->v_usecount > 0 ||
1171 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
1172 (vp->v_object != NULL &&
1173 vp->v_object->resident_page_count > trigger)) {
1176 vn_finished_write(mp);
1177 goto next_iter_unlocked;
1179 counter_u64_add(recycles_count, 1);
1183 vn_finished_write(mp);
1187 kern_yield(PRI_USER);
1188 mtx_lock(&vnode_list_mtx);
1191 MPASS(vp->v_type != VMARKER);
1192 if (!should_yield())
1194 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1195 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist);
1196 mtx_unlock(&vnode_list_mtx);
1197 kern_yield(PRI_USER);
1198 mtx_lock(&vnode_list_mtx);
1201 if (done == 0 && !retried) {
1202 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1203 TAILQ_INSERT_HEAD(&vnode_list, mvp, v_vnodelist);
1210 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */
1211 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free,
1213 "limit on vnode free requests per call to the vnlru_free routine");
1216 * Attempt to reduce the free list by the requested amount.
1219 vnlru_free_locked(int count, struct vfsops *mnt_op)
1221 struct vnode *vp, *mvp;
1225 mtx_assert(&vnode_list_mtx, MA_OWNED);
1226 if (count > max_vnlru_free)
1227 count = max_vnlru_free;
1229 mvp = vnode_list_free_marker;
1235 vp = TAILQ_NEXT(vp, v_vnodelist);
1236 if (__predict_false(vp == NULL)) {
1237 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1238 TAILQ_INSERT_TAIL(&vnode_list, mvp, v_vnodelist);
1241 if (__predict_false(vp->v_type == VMARKER))
1243 if (vp->v_holdcnt > 0)
1246 * Don't recycle if our vnode is from different type
1247 * of mount point. Note that mp is type-safe, the
1248 * check does not reach unmapped address even if
1249 * vnode is reclaimed.
1251 if (mnt_op != NULL && (mp = vp->v_mount) != NULL &&
1252 mp->mnt_op != mnt_op) {
1255 if (__predict_false(vp->v_type == VBAD || vp->v_type == VNON)) {
1258 if (!vhold_recycle_free(vp))
1260 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1261 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist);
1262 mtx_unlock(&vnode_list_mtx);
1263 if (vtryrecycle(vp) == 0)
1265 mtx_lock(&vnode_list_mtx);
1268 return (ocount - count);
1272 vnlru_free(int count, struct vfsops *mnt_op)
1275 mtx_lock(&vnode_list_mtx);
1276 vnlru_free_locked(count, mnt_op);
1277 mtx_unlock(&vnode_list_mtx);
1284 mtx_assert(&vnode_list_mtx, MA_OWNED);
1285 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100);
1286 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */
1287 vlowat = vhiwat / 2;
1291 * Attempt to recycle vnodes in a context that is always safe to block.
1292 * Calling vlrurecycle() from the bowels of filesystem code has some
1293 * interesting deadlock problems.
1295 static struct proc *vnlruproc;
1296 static int vnlruproc_sig;
1299 * The main freevnodes counter is only updated when threads requeue their vnode
1300 * batches. CPUs are conditionally walked to compute a more accurate total.
1302 * Limit how much of a slop are we willing to tolerate. Note: the actual value
1303 * at any given moment can still exceed slop, but it should not be by significant
1304 * margin in practice.
1306 #define VNLRU_FREEVNODES_SLOP 128
1308 static __inline void
1309 vn_freevnodes_inc(void)
1319 static __inline void
1320 vn_freevnodes_dec(void)
1331 vnlru_read_freevnodes(void)
1337 mtx_assert(&vnode_list_mtx, MA_OWNED);
1338 if (freevnodes > freevnodes_old)
1339 slop = freevnodes - freevnodes_old;
1341 slop = freevnodes_old - freevnodes;
1342 if (slop < VNLRU_FREEVNODES_SLOP)
1343 return (freevnodes >= 0 ? freevnodes : 0);
1344 freevnodes_old = freevnodes;
1346 vd = DPCPU_ID_PTR((cpu), vd);
1347 freevnodes_old += vd->freevnodes;
1349 return (freevnodes_old >= 0 ? freevnodes_old : 0);
1353 vnlru_under(u_long rnumvnodes, u_long limit)
1355 u_long rfreevnodes, space;
1357 if (__predict_false(rnumvnodes > desiredvnodes))
1360 space = desiredvnodes - rnumvnodes;
1361 if (space < limit) {
1362 rfreevnodes = vnlru_read_freevnodes();
1363 if (rfreevnodes > wantfreevnodes)
1364 space += rfreevnodes - wantfreevnodes;
1366 return (space < limit);
1370 vnlru_under_unlocked(u_long rnumvnodes, u_long limit)
1372 long rfreevnodes, space;
1374 if (__predict_false(rnumvnodes > desiredvnodes))
1377 space = desiredvnodes - rnumvnodes;
1378 if (space < limit) {
1379 rfreevnodes = atomic_load_long(&freevnodes);
1380 if (rfreevnodes > wantfreevnodes)
1381 space += rfreevnodes - wantfreevnodes;
1383 return (space < limit);
1390 mtx_assert(&vnode_list_mtx, MA_OWNED);
1391 if (vnlruproc_sig == 0) {
1400 u_long rnumvnodes, rfreevnodes, target;
1401 unsigned long onumvnodes;
1402 int done, force, trigger, usevnodes;
1403 bool reclaim_nc_src, want_reread;
1405 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc,
1406 SHUTDOWN_PRI_FIRST);
1409 want_reread = false;
1411 kproc_suspend_check(vnlruproc);
1412 mtx_lock(&vnode_list_mtx);
1413 rnumvnodes = atomic_load_long(&numvnodes);
1416 force = vnlru_under(numvnodes, vhiwat) ? 1 : 0;
1417 want_reread = false;
1421 * If numvnodes is too large (due to desiredvnodes being
1422 * adjusted using its sysctl, or emergency growth), first
1423 * try to reduce it by discarding from the free list.
1425 if (rnumvnodes > desiredvnodes) {
1426 vnlru_free_locked(rnumvnodes - desiredvnodes, NULL);
1427 rnumvnodes = atomic_load_long(&numvnodes);
1430 * Sleep if the vnode cache is in a good state. This is
1431 * when it is not over-full and has space for about a 4%
1432 * or 9% expansion (by growing its size or inexcessively
1433 * reducing its free list). Otherwise, try to reclaim
1434 * space for a 10% expansion.
1436 if (vstir && force == 0) {
1440 if (force == 0 && !vnlru_under(rnumvnodes, vlowat)) {
1442 wakeup(&vnlruproc_sig);
1443 msleep(vnlruproc, &vnode_list_mtx,
1444 PVFS|PDROP, "vlruwt", hz);
1447 rfreevnodes = vnlru_read_freevnodes();
1449 onumvnodes = rnumvnodes;
1451 * Calculate parameters for recycling. These are the same
1452 * throughout the loop to give some semblance of fairness.
1453 * The trigger point is to avoid recycling vnodes with lots
1454 * of resident pages. We aren't trying to free memory; we
1455 * are trying to recycle or at least free vnodes.
1457 if (rnumvnodes <= desiredvnodes)
1458 usevnodes = rnumvnodes - rfreevnodes;
1460 usevnodes = rnumvnodes;
1464 * The trigger value is is chosen to give a conservatively
1465 * large value to ensure that it alone doesn't prevent
1466 * making progress. The value can easily be so large that
1467 * it is effectively infinite in some congested and
1468 * misconfigured cases, and this is necessary. Normally
1469 * it is about 8 to 100 (pages), which is quite large.
1471 trigger = vm_cnt.v_page_count * 2 / usevnodes;
1473 trigger = vsmalltrigger;
1474 reclaim_nc_src = force >= 3;
1475 target = rnumvnodes * (int64_t)gapvnodes / imax(desiredvnodes, 1);
1476 target = target / 10 + 1;
1477 done = vlrureclaim(reclaim_nc_src, trigger, target);
1478 mtx_unlock(&vnode_list_mtx);
1479 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes)
1480 uma_reclaim(UMA_RECLAIM_DRAIN);
1482 if (force == 0 || force == 1) {
1493 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
1496 kern_yield(PRI_USER);
1501 static struct kproc_desc vnlru_kp = {
1506 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
1510 * Routines having to do with the management of the vnode table.
1514 * Try to recycle a freed vnode. We abort if anyone picks up a reference
1515 * before we actually vgone(). This function must be called with the vnode
1516 * held to prevent the vnode from being returned to the free list midway
1520 vtryrecycle(struct vnode *vp)
1524 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
1525 VNASSERT(vp->v_holdcnt, vp,
1526 ("vtryrecycle: Recycling vp %p without a reference.", vp));
1528 * This vnode may found and locked via some other list, if so we
1529 * can't recycle it yet.
1531 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1533 "%s: impossible to recycle, vp %p lock is already held",
1536 return (EWOULDBLOCK);
1539 * Don't recycle if its filesystem is being suspended.
1541 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1544 "%s: impossible to recycle, cannot start the write for %p",
1550 * If we got this far, we need to acquire the interlock and see if
1551 * anyone picked up this vnode from another list. If not, we will
1552 * mark it with DOOMED via vgonel() so that anyone who does find it
1553 * will skip over it.
1556 if (vp->v_usecount) {
1559 vn_finished_write(vnmp);
1561 "%s: impossible to recycle, %p is already referenced",
1565 if (!VN_IS_DOOMED(vp)) {
1566 counter_u64_add(recycles_free_count, 1);
1571 vn_finished_write(vnmp);
1576 * Allocate a new vnode.
1578 * The operation never returns an error. Returning an error was disabled
1579 * in r145385 (dated 2005) with the following comment:
1581 * XXX Not all VFS_VGET/ffs_vget callers check returns.
1583 * Given the age of this commit (almost 15 years at the time of writing this
1584 * comment) restoring the ability to fail requires a significant audit of
1587 * The routine can try to free a vnode or stall for up to 1 second waiting for
1588 * vnlru to clear things up, but ultimately always performs a M_WAITOK allocation.
1590 static u_long vn_alloc_cyclecount;
1592 static struct vnode * __noinline
1593 vn_alloc_hard(struct mount *mp)
1595 u_long rnumvnodes, rfreevnodes;
1597 mtx_lock(&vnode_list_mtx);
1598 rnumvnodes = atomic_load_long(&numvnodes);
1599 if (rnumvnodes + 1 < desiredvnodes) {
1600 vn_alloc_cyclecount = 0;
1603 rfreevnodes = vnlru_read_freevnodes();
1604 if (vn_alloc_cyclecount++ >= rfreevnodes) {
1605 vn_alloc_cyclecount = 0;
1609 * Grow the vnode cache if it will not be above its target max
1610 * after growing. Otherwise, if the free list is nonempty, try
1611 * to reclaim 1 item from it before growing the cache (possibly
1612 * above its target max if the reclamation failed or is delayed).
1613 * Otherwise, wait for some space. In all cases, schedule
1614 * vnlru_proc() if we are getting short of space. The watermarks
1615 * should be chosen so that we never wait or even reclaim from
1616 * the free list to below its target minimum.
1618 if (vnlru_free_locked(1, NULL) > 0)
1620 if (mp == NULL || (mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
1622 * Wait for space for a new vnode.
1625 msleep(&vnlruproc_sig, &vnode_list_mtx, PVFS, "vlruwk", hz);
1626 if (atomic_load_long(&numvnodes) + 1 > desiredvnodes &&
1627 vnlru_read_freevnodes() > 1)
1628 vnlru_free_locked(1, NULL);
1631 rnumvnodes = atomic_fetchadd_long(&numvnodes, 1) + 1;
1632 if (vnlru_under(rnumvnodes, vlowat))
1634 mtx_unlock(&vnode_list_mtx);
1635 return (uma_zalloc_smr(vnode_zone, M_WAITOK));
1638 static struct vnode *
1639 vn_alloc(struct mount *mp)
1643 if (__predict_false(vn_alloc_cyclecount != 0))
1644 return (vn_alloc_hard(mp));
1645 rnumvnodes = atomic_fetchadd_long(&numvnodes, 1) + 1;
1646 if (__predict_false(vnlru_under_unlocked(rnumvnodes, vlowat))) {
1647 atomic_subtract_long(&numvnodes, 1);
1648 return (vn_alloc_hard(mp));
1651 return (uma_zalloc_smr(vnode_zone, M_WAITOK));
1655 vn_free(struct vnode *vp)
1658 atomic_subtract_long(&numvnodes, 1);
1659 uma_zfree_smr(vnode_zone, vp);
1663 * Return the next vnode from the free list.
1666 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1671 struct lock_object *lo;
1673 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1675 KASSERT(vops->registered,
1676 ("%s: not registered vector op %p\n", __func__, vops));
1679 if (td->td_vp_reserved != NULL) {
1680 vp = td->td_vp_reserved;
1681 td->td_vp_reserved = NULL;
1685 counter_u64_add(vnodes_created, 1);
1687 * Locks are given the generic name "vnode" when created.
1688 * Follow the historic practice of using the filesystem
1689 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc.
1691 * Locks live in a witness group keyed on their name. Thus,
1692 * when a lock is renamed, it must also move from the witness
1693 * group of its old name to the witness group of its new name.
1695 * The change only needs to be made when the vnode moves
1696 * from one filesystem type to another. We ensure that each
1697 * filesystem use a single static name pointer for its tag so
1698 * that we can compare pointers rather than doing a strcmp().
1700 lo = &vp->v_vnlock->lock_object;
1702 if (lo->lo_name != tag) {
1706 WITNESS_DESTROY(lo);
1707 WITNESS_INIT(lo, tag);
1711 * By default, don't allow shared locks unless filesystems opt-in.
1713 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE;
1715 * Finalize various vnode identity bits.
1717 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp));
1718 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp));
1719 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp));
1723 v_init_counters(vp);
1725 vp->v_bufobj.bo_ops = &buf_ops_bio;
1727 if (mp == NULL && vops != &dead_vnodeops)
1728 printf("NULL mp in getnewvnode(9), tag %s\n", tag);
1732 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1733 mac_vnode_associate_singlelabel(mp, vp);
1736 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize;
1737 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1738 vp->v_vflag |= VV_NOKNOTE;
1742 * For the filesystems which do not use vfs_hash_insert(),
1743 * still initialize v_hash to have vfs_hash_index() useful.
1744 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1747 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1754 getnewvnode_reserve(void)
1759 MPASS(td->td_vp_reserved == NULL);
1760 td->td_vp_reserved = vn_alloc(NULL);
1764 getnewvnode_drop_reserve(void)
1769 if (td->td_vp_reserved != NULL) {
1770 vn_free(td->td_vp_reserved);
1771 td->td_vp_reserved = NULL;
1775 static void __noinline
1776 freevnode(struct vnode *vp)
1781 * The vnode has been marked for destruction, so free it.
1783 * The vnode will be returned to the zone where it will
1784 * normally remain until it is needed for another vnode. We
1785 * need to cleanup (or verify that the cleanup has already
1786 * been done) any residual data left from its current use
1787 * so as not to contaminate the freshly allocated vnode.
1789 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
1791 * Paired with vgone.
1793 vn_seqc_write_end_free(vp);
1796 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
1797 VNPASS(vp->v_holdcnt == VHOLD_NO_SMR, vp);
1798 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
1799 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
1800 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
1801 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
1802 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
1803 ("clean blk trie not empty"));
1804 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
1805 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
1806 ("dirty blk trie not empty"));
1807 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
1808 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
1809 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
1810 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp,
1811 ("Dangling rangelock waiters"));
1812 VNASSERT((vp->v_iflag & (VI_DOINGINACT | VI_OWEINACT)) == 0, vp,
1813 ("Leaked inactivation"));
1816 mac_vnode_destroy(vp);
1818 if (vp->v_pollinfo != NULL) {
1819 destroy_vpollinfo(vp->v_pollinfo);
1820 vp->v_pollinfo = NULL;
1822 vp->v_mountedhere = NULL;
1825 vp->v_fifoinfo = NULL;
1826 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
1834 * Delete from old mount point vnode list, if on one.
1837 delmntque(struct vnode *vp)
1841 VNPASS((vp->v_mflag & VMP_LAZYLIST) == 0, vp);
1850 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1851 ("bad mount point vnode list size"));
1852 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1853 mp->mnt_nvnodelistsize--;
1859 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1863 vp->v_op = &dead_vnodeops;
1869 * Insert into list of vnodes for the new mount point, if available.
1872 insmntque1(struct vnode *vp, struct mount *mp,
1873 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1876 KASSERT(vp->v_mount == NULL,
1877 ("insmntque: vnode already on per mount vnode list"));
1878 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1879 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1882 * We acquire the vnode interlock early to ensure that the
1883 * vnode cannot be recycled by another process releasing a
1884 * holdcnt on it before we get it on both the vnode list
1885 * and the active vnode list. The mount mutex protects only
1886 * manipulation of the vnode list and the vnode freelist
1887 * mutex protects only manipulation of the active vnode list.
1888 * Hence the need to hold the vnode interlock throughout.
1892 if (((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 &&
1893 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1894 mp->mnt_nvnodelistsize == 0)) &&
1895 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1904 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1905 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1906 ("neg mount point vnode list size"));
1907 mp->mnt_nvnodelistsize++;
1914 insmntque(struct vnode *vp, struct mount *mp)
1917 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1921 * Flush out and invalidate all buffers associated with a bufobj
1922 * Called with the underlying object locked.
1925 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1930 if (flags & V_SAVE) {
1931 error = bufobj_wwait(bo, slpflag, slptimeo);
1936 if (bo->bo_dirty.bv_cnt > 0) {
1939 error = BO_SYNC(bo, MNT_WAIT);
1940 } while (error == ERELOOKUP);
1944 * XXX We could save a lock/unlock if this was only
1945 * enabled under INVARIANTS
1948 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1949 panic("vinvalbuf: dirty bufs");
1953 * If you alter this loop please notice that interlock is dropped and
1954 * reacquired in flushbuflist. Special care is needed to ensure that
1955 * no race conditions occur from this.
1958 error = flushbuflist(&bo->bo_clean,
1959 flags, bo, slpflag, slptimeo);
1960 if (error == 0 && !(flags & V_CLEANONLY))
1961 error = flushbuflist(&bo->bo_dirty,
1962 flags, bo, slpflag, slptimeo);
1963 if (error != 0 && error != EAGAIN) {
1967 } while (error != 0);
1970 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1971 * have write I/O in-progress but if there is a VM object then the
1972 * VM object can also have read-I/O in-progress.
1975 bufobj_wwait(bo, 0, 0);
1976 if ((flags & V_VMIO) == 0 && bo->bo_object != NULL) {
1978 vm_object_pip_wait_unlocked(bo->bo_object, "bovlbx");
1981 } while (bo->bo_numoutput > 0);
1985 * Destroy the copy in the VM cache, too.
1987 if (bo->bo_object != NULL &&
1988 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) {
1989 VM_OBJECT_WLOCK(bo->bo_object);
1990 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1991 OBJPR_CLEANONLY : 0);
1992 VM_OBJECT_WUNLOCK(bo->bo_object);
1997 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO |
1998 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 ||
1999 bo->bo_clean.bv_cnt > 0))
2000 panic("vinvalbuf: flush failed");
2001 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 &&
2002 bo->bo_dirty.bv_cnt > 0)
2003 panic("vinvalbuf: flush dirty failed");
2010 * Flush out and invalidate all buffers associated with a vnode.
2011 * Called with the underlying object locked.
2014 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
2017 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2018 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
2019 if (vp->v_object != NULL && vp->v_object->handle != vp)
2021 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
2025 * Flush out buffers on the specified list.
2029 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
2032 struct buf *bp, *nbp;
2037 ASSERT_BO_WLOCKED(bo);
2040 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
2042 * If we are flushing both V_NORMAL and V_ALT buffers then
2043 * do not skip any buffers. If we are flushing only V_NORMAL
2044 * buffers then skip buffers marked as BX_ALTDATA. If we are
2045 * flushing only V_ALT buffers then skip buffers not marked
2048 if (((flags & (V_NORMAL | V_ALT)) != (V_NORMAL | V_ALT)) &&
2049 (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA) != 0) ||
2050 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0))) {
2054 lblkno = nbp->b_lblkno;
2055 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
2058 error = BUF_TIMELOCK(bp,
2059 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
2060 "flushbuf", slpflag, slptimeo);
2063 return (error != ENOLCK ? error : EAGAIN);
2065 KASSERT(bp->b_bufobj == bo,
2066 ("bp %p wrong b_bufobj %p should be %p",
2067 bp, bp->b_bufobj, bo));
2069 * XXX Since there are no node locks for NFS, I
2070 * believe there is a slight chance that a delayed
2071 * write will occur while sleeping just above, so
2074 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
2077 bp->b_flags |= B_ASYNC;
2080 return (EAGAIN); /* XXX: why not loop ? */
2083 bp->b_flags |= (B_INVAL | B_RELBUF);
2084 bp->b_flags &= ~B_ASYNC;
2089 nbp = gbincore(bo, lblkno);
2090 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2092 break; /* nbp invalid */
2098 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn)
2104 ASSERT_BO_LOCKED(bo);
2106 for (lblkno = startn;;) {
2108 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno);
2109 if (bp == NULL || bp->b_lblkno >= endn ||
2110 bp->b_lblkno < startn)
2112 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
2113 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0);
2116 if (error == ENOLCK)
2120 KASSERT(bp->b_bufobj == bo,
2121 ("bp %p wrong b_bufobj %p should be %p",
2122 bp, bp->b_bufobj, bo));
2123 lblkno = bp->b_lblkno + 1;
2124 if ((bp->b_flags & B_MANAGED) == 0)
2126 bp->b_flags |= B_RELBUF;
2128 * In the VMIO case, use the B_NOREUSE flag to hint that the
2129 * pages backing each buffer in the range are unlikely to be
2130 * reused. Dirty buffers will have the hint applied once
2131 * they've been written.
2133 if ((bp->b_flags & B_VMIO) != 0)
2134 bp->b_flags |= B_NOREUSE;
2142 * Truncate a file's buffer and pages to a specified length. This
2143 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
2147 vtruncbuf(struct vnode *vp, off_t length, int blksize)
2149 struct buf *bp, *nbp;
2153 CTR4(KTR_VFS, "%s: vp %p with block %d:%ju", __func__,
2154 vp, blksize, (uintmax_t)length);
2157 * Round up to the *next* lbn.
2159 startlbn = howmany(length, blksize);
2161 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
2167 while (v_inval_buf_range_locked(vp, bo, startlbn, INT64_MAX) == EAGAIN)
2172 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2173 if (bp->b_lblkno > 0)
2176 * Since we hold the vnode lock this should only
2177 * fail if we're racing with the buf daemon.
2180 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2181 BO_LOCKPTR(bo)) == ENOLCK)
2182 goto restart_unlocked;
2184 VNASSERT((bp->b_flags & B_DELWRI), vp,
2185 ("buf(%p) on dirty queue without DELWRI", bp));
2194 bufobj_wwait(bo, 0, 0);
2196 vnode_pager_setsize(vp, length);
2202 * Invalidate the cached pages of a file's buffer within the range of block
2203 * numbers [startlbn, endlbn).
2206 v_inval_buf_range(struct vnode *vp, daddr_t startlbn, daddr_t endlbn,
2212 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range");
2214 start = blksize * startlbn;
2215 end = blksize * endlbn;
2219 MPASS(blksize == bo->bo_bsize);
2221 while (v_inval_buf_range_locked(vp, bo, startlbn, endlbn) == EAGAIN)
2225 vn_pages_remove(vp, OFF_TO_IDX(start), OFF_TO_IDX(end + PAGE_SIZE - 1));
2229 v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
2230 daddr_t startlbn, daddr_t endlbn)
2232 struct buf *bp, *nbp;
2235 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range_locked");
2236 ASSERT_BO_LOCKED(bo);
2240 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
2241 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2244 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2245 BO_LOCKPTR(bo)) == ENOLCK) {
2251 bp->b_flags |= B_INVAL | B_RELBUF;
2252 bp->b_flags &= ~B_ASYNC;
2258 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
2260 (nbp->b_flags & B_DELWRI) != 0))
2264 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2265 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2268 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2269 BO_LOCKPTR(bo)) == ENOLCK) {
2274 bp->b_flags |= B_INVAL | B_RELBUF;
2275 bp->b_flags &= ~B_ASYNC;
2281 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
2282 (nbp->b_vp != vp) ||
2283 (nbp->b_flags & B_DELWRI) == 0))
2291 buf_vlist_remove(struct buf *bp)
2296 flags = bp->b_xflags;
2298 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2299 ASSERT_BO_WLOCKED(bp->b_bufobj);
2300 KASSERT((flags & (BX_VNDIRTY | BX_VNCLEAN)) != 0 &&
2301 (flags & (BX_VNDIRTY | BX_VNCLEAN)) != (BX_VNDIRTY | BX_VNCLEAN),
2302 ("%s: buffer %p has invalid queue state", __func__, bp));
2304 if ((flags & BX_VNDIRTY) != 0)
2305 bv = &bp->b_bufobj->bo_dirty;
2307 bv = &bp->b_bufobj->bo_clean;
2308 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
2309 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
2311 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
2315 * Add the buffer to the sorted clean or dirty block list.
2317 * NOTE: xflags is passed as a constant, optimizing this inline function!
2320 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
2326 ASSERT_BO_WLOCKED(bo);
2327 KASSERT((bo->bo_flag & BO_NOBUFS) == 0,
2328 ("buf_vlist_add: bo %p does not allow bufs", bo));
2329 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
2330 ("dead bo %p", bo));
2331 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
2332 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
2333 bp->b_xflags |= xflags;
2334 if (xflags & BX_VNDIRTY)
2340 * Keep the list ordered. Optimize empty list insertion. Assume
2341 * we tend to grow at the tail so lookup_le should usually be cheaper
2344 if (bv->bv_cnt == 0 ||
2345 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
2346 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
2347 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
2348 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
2350 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
2351 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
2353 panic("buf_vlist_add: Preallocated nodes insufficient.");
2358 * Look up a buffer using the buffer tries.
2361 gbincore(struct bufobj *bo, daddr_t lblkno)
2365 ASSERT_BO_LOCKED(bo);
2366 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
2369 return (BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno));
2373 * Look up a buf using the buffer tries, without the bufobj lock. This relies
2374 * on SMR for safe lookup, and bufs being in a no-free zone to provide type
2375 * stability of the result. Like other lockless lookups, the found buf may
2376 * already be invalid by the time this function returns.
2379 gbincore_unlocked(struct bufobj *bo, daddr_t lblkno)
2383 ASSERT_BO_UNLOCKED(bo);
2384 bp = BUF_PCTRIE_LOOKUP_UNLOCKED(&bo->bo_clean.bv_root, lblkno);
2387 return (BUF_PCTRIE_LOOKUP_UNLOCKED(&bo->bo_dirty.bv_root, lblkno));
2391 * Associate a buffer with a vnode.
2394 bgetvp(struct vnode *vp, struct buf *bp)
2399 ASSERT_BO_WLOCKED(bo);
2400 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
2402 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
2403 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
2404 ("bgetvp: bp already attached! %p", bp));
2410 * Insert onto list for new vnode.
2412 buf_vlist_add(bp, bo, BX_VNCLEAN);
2416 * Disassociate a buffer from a vnode.
2419 brelvp(struct buf *bp)
2424 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2425 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
2428 * Delete from old vnode list, if on one.
2430 vp = bp->b_vp; /* XXX */
2433 buf_vlist_remove(bp);
2434 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2435 bo->bo_flag &= ~BO_ONWORKLST;
2436 mtx_lock(&sync_mtx);
2437 LIST_REMOVE(bo, bo_synclist);
2438 syncer_worklist_len--;
2439 mtx_unlock(&sync_mtx);
2442 bp->b_bufobj = NULL;
2448 * Add an item to the syncer work queue.
2451 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
2455 ASSERT_BO_WLOCKED(bo);
2457 mtx_lock(&sync_mtx);
2458 if (bo->bo_flag & BO_ONWORKLST)
2459 LIST_REMOVE(bo, bo_synclist);
2461 bo->bo_flag |= BO_ONWORKLST;
2462 syncer_worklist_len++;
2465 if (delay > syncer_maxdelay - 2)
2466 delay = syncer_maxdelay - 2;
2467 slot = (syncer_delayno + delay) & syncer_mask;
2469 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
2470 mtx_unlock(&sync_mtx);
2474 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
2478 mtx_lock(&sync_mtx);
2479 len = syncer_worklist_len - sync_vnode_count;
2480 mtx_unlock(&sync_mtx);
2481 error = SYSCTL_OUT(req, &len, sizeof(len));
2485 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len,
2486 CTLTYPE_INT | CTLFLAG_MPSAFE| CTLFLAG_RD, NULL, 0,
2487 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
2489 static struct proc *updateproc;
2490 static void sched_sync(void);
2491 static struct kproc_desc up_kp = {
2496 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
2499 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
2504 *bo = LIST_FIRST(slp);
2508 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
2511 * We use vhold in case the vnode does not
2512 * successfully sync. vhold prevents the vnode from
2513 * going away when we unlock the sync_mtx so that
2514 * we can acquire the vnode interlock.
2517 mtx_unlock(&sync_mtx);
2519 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2521 mtx_lock(&sync_mtx);
2522 return (*bo == LIST_FIRST(slp));
2524 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2525 (void) VOP_FSYNC(vp, MNT_LAZY, td);
2527 vn_finished_write(mp);
2529 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
2531 * Put us back on the worklist. The worklist
2532 * routine will remove us from our current
2533 * position and then add us back in at a later
2536 vn_syncer_add_to_worklist(*bo, syncdelay);
2540 mtx_lock(&sync_mtx);
2544 static int first_printf = 1;
2547 * System filesystem synchronizer daemon.
2552 struct synclist *next, *slp;
2555 struct thread *td = curthread;
2557 int net_worklist_len;
2558 int syncer_final_iter;
2562 syncer_final_iter = 0;
2563 syncer_state = SYNCER_RUNNING;
2564 starttime = time_uptime;
2565 td->td_pflags |= TDP_NORUNNINGBUF;
2567 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
2570 mtx_lock(&sync_mtx);
2572 if (syncer_state == SYNCER_FINAL_DELAY &&
2573 syncer_final_iter == 0) {
2574 mtx_unlock(&sync_mtx);
2575 kproc_suspend_check(td->td_proc);
2576 mtx_lock(&sync_mtx);
2578 net_worklist_len = syncer_worklist_len - sync_vnode_count;
2579 if (syncer_state != SYNCER_RUNNING &&
2580 starttime != time_uptime) {
2582 printf("\nSyncing disks, vnodes remaining... ");
2585 printf("%d ", net_worklist_len);
2587 starttime = time_uptime;
2590 * Push files whose dirty time has expired. Be careful
2591 * of interrupt race on slp queue.
2593 * Skip over empty worklist slots when shutting down.
2596 slp = &syncer_workitem_pending[syncer_delayno];
2597 syncer_delayno += 1;
2598 if (syncer_delayno == syncer_maxdelay)
2600 next = &syncer_workitem_pending[syncer_delayno];
2602 * If the worklist has wrapped since the
2603 * it was emptied of all but syncer vnodes,
2604 * switch to the FINAL_DELAY state and run
2605 * for one more second.
2607 if (syncer_state == SYNCER_SHUTTING_DOWN &&
2608 net_worklist_len == 0 &&
2609 last_work_seen == syncer_delayno) {
2610 syncer_state = SYNCER_FINAL_DELAY;
2611 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
2613 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
2614 syncer_worklist_len > 0);
2617 * Keep track of the last time there was anything
2618 * on the worklist other than syncer vnodes.
2619 * Return to the SHUTTING_DOWN state if any
2622 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
2623 last_work_seen = syncer_delayno;
2624 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
2625 syncer_state = SYNCER_SHUTTING_DOWN;
2626 while (!LIST_EMPTY(slp)) {
2627 error = sync_vnode(slp, &bo, td);
2629 LIST_REMOVE(bo, bo_synclist);
2630 LIST_INSERT_HEAD(next, bo, bo_synclist);
2634 if (first_printf == 0) {
2636 * Drop the sync mutex, because some watchdog
2637 * drivers need to sleep while patting
2639 mtx_unlock(&sync_mtx);
2640 wdog_kern_pat(WD_LASTVAL);
2641 mtx_lock(&sync_mtx);
2644 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
2645 syncer_final_iter--;
2647 * The variable rushjob allows the kernel to speed up the
2648 * processing of the filesystem syncer process. A rushjob
2649 * value of N tells the filesystem syncer to process the next
2650 * N seconds worth of work on its queue ASAP. Currently rushjob
2651 * is used by the soft update code to speed up the filesystem
2652 * syncer process when the incore state is getting so far
2653 * ahead of the disk that the kernel memory pool is being
2654 * threatened with exhaustion.
2661 * Just sleep for a short period of time between
2662 * iterations when shutting down to allow some I/O
2665 * If it has taken us less than a second to process the
2666 * current work, then wait. Otherwise start right over
2667 * again. We can still lose time if any single round
2668 * takes more than two seconds, but it does not really
2669 * matter as we are just trying to generally pace the
2670 * filesystem activity.
2672 if (syncer_state != SYNCER_RUNNING ||
2673 time_uptime == starttime) {
2675 sched_prio(td, PPAUSE);
2678 if (syncer_state != SYNCER_RUNNING)
2679 cv_timedwait(&sync_wakeup, &sync_mtx,
2680 hz / SYNCER_SHUTDOWN_SPEEDUP);
2681 else if (time_uptime == starttime)
2682 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2687 * Request the syncer daemon to speed up its work.
2688 * We never push it to speed up more than half of its
2689 * normal turn time, otherwise it could take over the cpu.
2692 speedup_syncer(void)
2696 mtx_lock(&sync_mtx);
2697 if (rushjob < syncdelay / 2) {
2699 stat_rush_requests += 1;
2702 mtx_unlock(&sync_mtx);
2703 cv_broadcast(&sync_wakeup);
2708 * Tell the syncer to speed up its work and run though its work
2709 * list several times, then tell it to shut down.
2712 syncer_shutdown(void *arg, int howto)
2715 if (howto & RB_NOSYNC)
2717 mtx_lock(&sync_mtx);
2718 syncer_state = SYNCER_SHUTTING_DOWN;
2720 mtx_unlock(&sync_mtx);
2721 cv_broadcast(&sync_wakeup);
2722 kproc_shutdown(arg, howto);
2726 syncer_suspend(void)
2729 syncer_shutdown(updateproc, 0);
2736 mtx_lock(&sync_mtx);
2738 syncer_state = SYNCER_RUNNING;
2739 mtx_unlock(&sync_mtx);
2740 cv_broadcast(&sync_wakeup);
2741 kproc_resume(updateproc);
2745 * Move the buffer between the clean and dirty lists of its vnode.
2748 reassignbuf(struct buf *bp)
2760 KASSERT((bp->b_flags & B_PAGING) == 0,
2761 ("%s: cannot reassign paging buffer %p", __func__, bp));
2763 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2764 bp, bp->b_vp, bp->b_flags);
2767 buf_vlist_remove(bp);
2770 * If dirty, put on list of dirty buffers; otherwise insert onto list
2773 if (bp->b_flags & B_DELWRI) {
2774 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2775 switch (vp->v_type) {
2785 vn_syncer_add_to_worklist(bo, delay);
2787 buf_vlist_add(bp, bo, BX_VNDIRTY);
2789 buf_vlist_add(bp, bo, BX_VNCLEAN);
2791 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2792 mtx_lock(&sync_mtx);
2793 LIST_REMOVE(bo, bo_synclist);
2794 syncer_worklist_len--;
2795 mtx_unlock(&sync_mtx);
2796 bo->bo_flag &= ~BO_ONWORKLST;
2801 bp = TAILQ_FIRST(&bv->bv_hd);
2802 KASSERT(bp == NULL || bp->b_bufobj == bo,
2803 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2804 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2805 KASSERT(bp == NULL || bp->b_bufobj == bo,
2806 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2808 bp = TAILQ_FIRST(&bv->bv_hd);
2809 KASSERT(bp == NULL || bp->b_bufobj == bo,
2810 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2811 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2812 KASSERT(bp == NULL || bp->b_bufobj == bo,
2813 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2819 v_init_counters(struct vnode *vp)
2822 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2823 vp, ("%s called for an initialized vnode", __FUNCTION__));
2824 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2826 refcount_init(&vp->v_holdcnt, 1);
2827 refcount_init(&vp->v_usecount, 1);
2831 * Grab a particular vnode from the free list, increment its
2832 * reference count and lock it. VIRF_DOOMED is set if the vnode
2833 * is being destroyed. Only callers who specify LK_RETRY will
2834 * see doomed vnodes. If inactive processing was delayed in
2835 * vput try to do it here.
2837 * usecount is manipulated using atomics without holding any locks.
2839 * holdcnt can be manipulated using atomics without holding any locks,
2840 * except when transitioning 1<->0, in which case the interlock is held.
2842 * Consumers which don't guarantee liveness of the vnode can use SMR to
2843 * try to get a reference. Note this operation can fail since the vnode
2844 * may be awaiting getting freed by the time they get to it.
2847 vget_prep_smr(struct vnode *vp)
2851 VFS_SMR_ASSERT_ENTERED();
2853 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2865 vget_prep(struct vnode *vp)
2869 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2879 vget_abort(struct vnode *vp, enum vgetstate vs)
2890 __assert_unreachable();
2895 vget(struct vnode *vp, int flags)
2900 return (vget_finish(vp, flags, vs));
2904 vget_finish(struct vnode *vp, int flags, enum vgetstate vs)
2908 if ((flags & LK_INTERLOCK) != 0)
2909 ASSERT_VI_LOCKED(vp, __func__);
2911 ASSERT_VI_UNLOCKED(vp, __func__);
2912 VNPASS(vs == VGET_HOLDCNT || vs == VGET_USECOUNT, vp);
2913 VNPASS(vp->v_holdcnt > 0, vp);
2914 VNPASS(vs == VGET_HOLDCNT || vp->v_usecount > 0, vp);
2916 error = vn_lock(vp, flags);
2917 if (__predict_false(error != 0)) {
2919 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2924 vget_finish_ref(vp, vs);
2929 vget_finish_ref(struct vnode *vp, enum vgetstate vs)
2933 VNPASS(vs == VGET_HOLDCNT || vs == VGET_USECOUNT, vp);
2934 VNPASS(vp->v_holdcnt > 0, vp);
2935 VNPASS(vs == VGET_HOLDCNT || vp->v_usecount > 0, vp);
2937 if (vs == VGET_USECOUNT)
2941 * We hold the vnode. If the usecount is 0 it will be utilized to keep
2942 * the vnode around. Otherwise someone else lended their hold count and
2943 * we have to drop ours.
2945 old = atomic_fetchadd_int(&vp->v_usecount, 1);
2946 VNASSERT(old >= 0, vp, ("%s: wrong use count %d", __func__, old));
2949 old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
2950 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old));
2952 refcount_release(&vp->v_holdcnt);
2958 vref(struct vnode *vp)
2962 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2964 vget_finish_ref(vp, vs);
2968 vrefact(struct vnode *vp)
2971 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2973 int old = atomic_fetchadd_int(&vp->v_usecount, 1);
2974 VNASSERT(old > 0, vp, ("%s: wrong use count %d", __func__, old));
2976 refcount_acquire(&vp->v_usecount);
2981 vlazy(struct vnode *vp)
2985 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__));
2987 if ((vp->v_mflag & VMP_LAZYLIST) != 0)
2990 * We may get here for inactive routines after the vnode got doomed.
2992 if (VN_IS_DOOMED(vp))
2995 mtx_lock(&mp->mnt_listmtx);
2996 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
2997 vp->v_mflag |= VMP_LAZYLIST;
2998 TAILQ_INSERT_TAIL(&mp->mnt_lazyvnodelist, vp, v_lazylist);
2999 mp->mnt_lazyvnodelistsize++;
3001 mtx_unlock(&mp->mnt_listmtx);
3005 * This routine is only meant to be called from vgonel prior to dooming
3009 vunlazy_gone(struct vnode *vp)
3013 ASSERT_VOP_ELOCKED(vp, __func__);
3014 ASSERT_VI_LOCKED(vp, __func__);
3015 VNPASS(!VN_IS_DOOMED(vp), vp);
3017 if (vp->v_mflag & VMP_LAZYLIST) {
3019 mtx_lock(&mp->mnt_listmtx);
3020 VNPASS(vp->v_mflag & VMP_LAZYLIST, vp);
3021 vp->v_mflag &= ~VMP_LAZYLIST;
3022 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3023 mp->mnt_lazyvnodelistsize--;
3024 mtx_unlock(&mp->mnt_listmtx);
3029 vdefer_inactive(struct vnode *vp)
3032 ASSERT_VI_LOCKED(vp, __func__);
3033 VNASSERT(vp->v_holdcnt > 0, vp,
3034 ("%s: vnode without hold count", __func__));
3035 if (VN_IS_DOOMED(vp)) {
3039 if (vp->v_iflag & VI_DEFINACT) {
3040 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count"));
3044 if (vp->v_usecount > 0) {
3045 vp->v_iflag &= ~VI_OWEINACT;
3050 vp->v_iflag |= VI_DEFINACT;
3052 counter_u64_add(deferred_inact, 1);
3056 vdefer_inactive_unlocked(struct vnode *vp)
3060 if ((vp->v_iflag & VI_OWEINACT) == 0) {
3064 vdefer_inactive(vp);
3067 enum vput_op { VRELE, VPUT, VUNREF };
3070 * Handle ->v_usecount transitioning to 0.
3072 * By releasing the last usecount we take ownership of the hold count which
3073 * provides liveness of the vnode, meaning we have to vdrop.
3075 * For all vnodes we may need to perform inactive processing. It requires an
3076 * exclusive lock on the vnode, while it is legal to call here with only a
3077 * shared lock (or no locks). If locking the vnode in an expected manner fails,
3078 * inactive processing gets deferred to the syncer.
3080 * XXX Some filesystems pass in an exclusively locked vnode and strongly depend
3081 * on the lock being held all the way until VOP_INACTIVE. This in particular
3082 * happens with UFS which adds half-constructed vnodes to the hash, where they
3083 * can be found by other code.
3086 vput_final(struct vnode *vp, enum vput_op func)
3091 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3092 VNPASS(vp->v_holdcnt > 0, vp);
3097 * By the time we got here someone else might have transitioned
3098 * the count back to > 0.
3100 if (vp->v_usecount > 0)
3104 * If the vnode is doomed vgone already performed inactive processing
3107 if (VN_IS_DOOMED(vp))
3110 if (__predict_true(VOP_NEED_INACTIVE(vp) == 0))
3113 if (vp->v_iflag & VI_DOINGINACT)
3117 * Locking operations here will drop the interlock and possibly the
3118 * vnode lock, opening a window where the vnode can get doomed all the
3119 * while ->v_usecount is 0. Set VI_OWEINACT to let vgone know to
3122 vp->v_iflag |= VI_OWEINACT;
3123 want_unlock = false;
3127 switch (VOP_ISLOCKED(vp)) {
3133 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
3138 * The lock has at least one sharer, but we have no way
3139 * to conclude whether this is us. Play it safe and
3148 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
3149 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
3155 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
3156 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
3167 vdefer_inactive(vp);
3177 * Decrement ->v_usecount for a vnode.
3179 * Releasing the last use count requires additional processing, see vput_final
3180 * above for details.
3182 * Comment above each variant denotes lock state on entry and exit.
3187 * out: same as passed in
3190 vrele(struct vnode *vp)
3193 ASSERT_VI_UNLOCKED(vp, __func__);
3194 if (!refcount_release(&vp->v_usecount))
3196 vput_final(vp, VRELE);
3204 vput(struct vnode *vp)
3207 ASSERT_VOP_LOCKED(vp, __func__);
3208 ASSERT_VI_UNLOCKED(vp, __func__);
3209 if (!refcount_release(&vp->v_usecount)) {
3213 vput_final(vp, VPUT);
3221 vunref(struct vnode *vp)
3224 ASSERT_VOP_LOCKED(vp, __func__);
3225 ASSERT_VI_UNLOCKED(vp, __func__);
3226 if (!refcount_release(&vp->v_usecount))
3228 vput_final(vp, VUNREF);
3232 vhold(struct vnode *vp)
3236 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3237 old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3238 VNASSERT(old >= 0 && (old & VHOLD_ALL_FLAGS) == 0, vp,
3239 ("%s: wrong hold count %d", __func__, old));
3241 vn_freevnodes_dec();
3245 vholdnz(struct vnode *vp)
3248 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3250 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3251 VNASSERT(old > 0 && (old & VHOLD_ALL_FLAGS) == 0, vp,
3252 ("%s: wrong hold count %d", __func__, old));
3254 atomic_add_int(&vp->v_holdcnt, 1);
3259 * Grab a hold count unless the vnode is freed.
3261 * Only use this routine if vfs smr is the only protection you have against
3262 * freeing the vnode.
3264 * The code loops trying to add a hold count as long as the VHOLD_NO_SMR flag
3265 * is not set. After the flag is set the vnode becomes immutable to anyone but
3266 * the thread which managed to set the flag.
3268 * It may be tempting to replace the loop with:
3269 * count = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3270 * if (count & VHOLD_NO_SMR) {
3271 * backpedal and error out;
3274 * However, while this is more performant, it hinders debugging by eliminating
3275 * the previously mentioned invariant.
3278 vhold_smr(struct vnode *vp)
3282 VFS_SMR_ASSERT_ENTERED();
3284 count = atomic_load_int(&vp->v_holdcnt);
3286 if (count & VHOLD_NO_SMR) {
3287 VNASSERT((count & ~VHOLD_NO_SMR) == 0, vp,
3288 ("non-zero hold count with flags %d\n", count));
3291 VNASSERT(count >= 0, vp, ("invalid hold count %d\n", count));
3292 if (atomic_fcmpset_int(&vp->v_holdcnt, &count, count + 1)) {
3294 vn_freevnodes_dec();
3301 * Hold a free vnode for recycling.
3303 * Note: vnode_init references this comment.
3305 * Attempts to recycle only need the global vnode list lock and have no use for
3308 * However, vnodes get inserted into the global list before they get fully
3309 * initialized and stay there until UMA decides to free the memory. This in
3310 * particular means the target can be found before it becomes usable and after
3311 * it becomes recycled. Picking up such vnodes is guarded with v_holdcnt set to
3314 * Note: the vnode may gain more references after we transition the count 0->1.
3317 vhold_recycle_free(struct vnode *vp)
3321 mtx_assert(&vnode_list_mtx, MA_OWNED);
3323 count = atomic_load_int(&vp->v_holdcnt);
3325 if (count & VHOLD_NO_SMR) {
3326 VNASSERT((count & ~VHOLD_NO_SMR) == 0, vp,
3327 ("non-zero hold count with flags %d\n", count));
3330 VNASSERT(count >= 0, vp, ("invalid hold count %d\n", count));
3334 if (atomic_fcmpset_int(&vp->v_holdcnt, &count, count + 1)) {
3335 vn_freevnodes_dec();
3341 static void __noinline
3342 vdbatch_process(struct vdbatch *vd)
3347 mtx_assert(&vd->lock, MA_OWNED);
3348 MPASS(curthread->td_pinned > 0);
3349 MPASS(vd->index == VDBATCH_SIZE);
3351 mtx_lock(&vnode_list_mtx);
3353 freevnodes += vd->freevnodes;
3354 for (i = 0; i < VDBATCH_SIZE; i++) {
3356 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist);
3357 TAILQ_INSERT_TAIL(&vnode_list, vp, v_vnodelist);
3358 MPASS(vp->v_dbatchcpu != NOCPU);
3359 vp->v_dbatchcpu = NOCPU;
3361 mtx_unlock(&vnode_list_mtx);
3363 bzero(vd->tab, sizeof(vd->tab));
3369 vdbatch_enqueue(struct vnode *vp)
3373 ASSERT_VI_LOCKED(vp, __func__);
3374 VNASSERT(!VN_IS_DOOMED(vp), vp,
3375 ("%s: deferring requeue of a doomed vnode", __func__));
3377 if (vp->v_dbatchcpu != NOCPU) {
3384 mtx_lock(&vd->lock);
3385 MPASS(vd->index < VDBATCH_SIZE);
3386 MPASS(vd->tab[vd->index] == NULL);
3388 * A hack: we depend on being pinned so that we know what to put in
3391 vp->v_dbatchcpu = curcpu;
3392 vd->tab[vd->index] = vp;
3395 if (vd->index == VDBATCH_SIZE)
3396 vdbatch_process(vd);
3397 mtx_unlock(&vd->lock);
3402 * This routine must only be called for vnodes which are about to be
3403 * deallocated. Supporting dequeue for arbitrary vndoes would require
3404 * validating that the locked batch matches.
3407 vdbatch_dequeue(struct vnode *vp)
3413 VNASSERT(vp->v_type == VBAD || vp->v_type == VNON, vp,
3414 ("%s: called for a used vnode\n", __func__));
3416 cpu = vp->v_dbatchcpu;
3420 vd = DPCPU_ID_PTR(cpu, vd);
3421 mtx_lock(&vd->lock);
3422 for (i = 0; i < vd->index; i++) {
3423 if (vd->tab[i] != vp)
3425 vp->v_dbatchcpu = NOCPU;
3427 vd->tab[i] = vd->tab[vd->index];
3428 vd->tab[vd->index] = NULL;
3431 mtx_unlock(&vd->lock);
3433 * Either we dequeued the vnode above or the target CPU beat us to it.
3435 MPASS(vp->v_dbatchcpu == NOCPU);
3439 * Drop the hold count of the vnode. If this is the last reference to
3440 * the vnode we place it on the free list unless it has been vgone'd
3441 * (marked VIRF_DOOMED) in which case we will free it.
3443 * Because the vnode vm object keeps a hold reference on the vnode if
3444 * there is at least one resident non-cached page, the vnode cannot
3445 * leave the active list without the page cleanup done.
3448 vdrop_deactivate(struct vnode *vp)
3452 ASSERT_VI_LOCKED(vp, __func__);
3454 * Mark a vnode as free: remove it from its active list
3455 * and put it up for recycling on the freelist.
3457 VNASSERT(!VN_IS_DOOMED(vp), vp,
3458 ("vdrop: returning doomed vnode"));
3459 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
3460 ("vnode with VI_OWEINACT set"));
3461 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp,
3462 ("vnode with VI_DEFINACT set"));
3463 if (vp->v_mflag & VMP_LAZYLIST) {
3465 mtx_lock(&mp->mnt_listmtx);
3466 VNASSERT(vp->v_mflag & VMP_LAZYLIST, vp, ("lost VMP_LAZYLIST"));
3468 * Don't remove the vnode from the lazy list if another thread
3469 * has increased the hold count. It may have re-enqueued the
3470 * vnode to the lazy list and is now responsible for its
3473 if (vp->v_holdcnt == 0) {
3474 vp->v_mflag &= ~VMP_LAZYLIST;
3475 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3476 mp->mnt_lazyvnodelistsize--;
3478 mtx_unlock(&mp->mnt_listmtx);
3480 vdbatch_enqueue(vp);
3483 static void __noinline
3484 vdropl_final(struct vnode *vp)
3487 ASSERT_VI_LOCKED(vp, __func__);
3488 VNPASS(VN_IS_DOOMED(vp), vp);
3490 * Set the VHOLD_NO_SMR flag.
3492 * We may be racing against vhold_smr. If they win we can just pretend
3493 * we never got this far, they will vdrop later.
3495 if (__predict_false(!atomic_cmpset_int(&vp->v_holdcnt, 0, VHOLD_NO_SMR))) {
3496 vn_freevnodes_inc();
3499 * We lost the aforementioned race. Any subsequent access is
3500 * invalid as they might have managed to vdropl on their own.
3505 * Don't bump freevnodes as this one is going away.
3511 vdrop(struct vnode *vp)
3514 ASSERT_VI_UNLOCKED(vp, __func__);
3515 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3516 if (refcount_release_if_not_last(&vp->v_holdcnt))
3523 vdropl(struct vnode *vp)
3526 ASSERT_VI_LOCKED(vp, __func__);
3527 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3528 if (!refcount_release(&vp->v_holdcnt)) {
3532 if (!VN_IS_DOOMED(vp)) {
3533 vn_freevnodes_inc();
3534 vdrop_deactivate(vp);
3536 * Also unlocks the interlock. We can't assert on it as we
3537 * released our hold and by now the vnode might have been
3546 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
3547 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
3550 vinactivef(struct vnode *vp)
3552 struct vm_object *obj;
3554 ASSERT_VOP_ELOCKED(vp, "vinactive");
3555 ASSERT_VI_LOCKED(vp, "vinactive");
3556 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
3557 ("vinactive: recursed on VI_DOINGINACT"));
3558 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3559 vp->v_iflag |= VI_DOINGINACT;
3560 vp->v_iflag &= ~VI_OWEINACT;
3563 * Before moving off the active list, we must be sure that any
3564 * modified pages are converted into the vnode's dirty
3565 * buffers, since these will no longer be checked once the
3566 * vnode is on the inactive list.
3568 * The write-out of the dirty pages is asynchronous. At the
3569 * point that VOP_INACTIVE() is called, there could still be
3570 * pending I/O and dirty pages in the object.
3572 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
3573 vm_object_mightbedirty(obj)) {
3574 VM_OBJECT_WLOCK(obj);
3575 vm_object_page_clean(obj, 0, 0, 0);
3576 VM_OBJECT_WUNLOCK(obj);
3580 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
3581 ("vinactive: lost VI_DOINGINACT"));
3582 vp->v_iflag &= ~VI_DOINGINACT;
3586 vinactive(struct vnode *vp)
3589 ASSERT_VOP_ELOCKED(vp, "vinactive");
3590 ASSERT_VI_LOCKED(vp, "vinactive");
3591 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3593 if ((vp->v_iflag & VI_OWEINACT) == 0)
3595 if (vp->v_iflag & VI_DOINGINACT)
3597 if (vp->v_usecount > 0) {
3598 vp->v_iflag &= ~VI_OWEINACT;
3605 * Remove any vnodes in the vnode table belonging to mount point mp.
3607 * If FORCECLOSE is not specified, there should not be any active ones,
3608 * return error if any are found (nb: this is a user error, not a
3609 * system error). If FORCECLOSE is specified, detach any active vnodes
3612 * If WRITECLOSE is set, only flush out regular file vnodes open for
3615 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
3617 * `rootrefs' specifies the base reference count for the root vnode
3618 * of this filesystem. The root vnode is considered busy if its
3619 * v_usecount exceeds this value. On a successful return, vflush(, td)
3620 * will call vrele() on the root vnode exactly rootrefs times.
3621 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
3625 static int busyprt = 0; /* print out busy vnodes */
3626 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
3630 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
3632 struct vnode *vp, *mvp, *rootvp = NULL;
3634 int busy = 0, error;
3636 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
3639 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
3640 ("vflush: bad args"));
3642 * Get the filesystem root vnode. We can vput() it
3643 * immediately, since with rootrefs > 0, it won't go away.
3645 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
3646 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
3653 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
3655 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
3658 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3662 * Skip over a vnodes marked VV_SYSTEM.
3664 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
3670 * If WRITECLOSE is set, flush out unlinked but still open
3671 * files (even if open only for reading) and regular file
3672 * vnodes open for writing.
3674 if (flags & WRITECLOSE) {
3675 if (vp->v_object != NULL) {
3676 VM_OBJECT_WLOCK(vp->v_object);
3677 vm_object_page_clean(vp->v_object, 0, 0, 0);
3678 VM_OBJECT_WUNLOCK(vp->v_object);
3681 error = VOP_FSYNC(vp, MNT_WAIT, td);
3682 } while (error == ERELOOKUP);
3686 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3689 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3692 if ((vp->v_type == VNON ||
3693 (error == 0 && vattr.va_nlink > 0)) &&
3694 (vp->v_writecount <= 0 || vp->v_type != VREG)) {
3702 * With v_usecount == 0, all we need to do is clear out the
3703 * vnode data structures and we are done.
3705 * If FORCECLOSE is set, forcibly close the vnode.
3707 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
3713 vn_printf(vp, "vflush: busy vnode ");
3719 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
3721 * If just the root vnode is busy, and if its refcount
3722 * is equal to `rootrefs', then go ahead and kill it.
3725 KASSERT(busy > 0, ("vflush: not busy"));
3726 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
3727 ("vflush: usecount %d < rootrefs %d",
3728 rootvp->v_usecount, rootrefs));
3729 if (busy == 1 && rootvp->v_usecount == rootrefs) {
3730 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
3738 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
3742 for (; rootrefs > 0; rootrefs--)
3748 * Recycle an unused vnode to the front of the free list.
3751 vrecycle(struct vnode *vp)
3756 recycled = vrecyclel(vp);
3762 * vrecycle, with the vp interlock held.
3765 vrecyclel(struct vnode *vp)
3769 ASSERT_VOP_ELOCKED(vp, __func__);
3770 ASSERT_VI_LOCKED(vp, __func__);
3771 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3773 if (vp->v_usecount == 0) {
3781 * Eliminate all activity associated with a vnode
3782 * in preparation for reuse.
3785 vgone(struct vnode *vp)
3793 notify_lowervp_vfs_dummy(struct mount *mp __unused,
3794 struct vnode *lowervp __unused)
3799 * Notify upper mounts about reclaimed or unlinked vnode.
3802 vfs_notify_upper(struct vnode *vp, int event)
3804 static struct vfsops vgonel_vfsops = {
3805 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
3806 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
3808 struct mount *mp, *ump, *mmp;
3813 if (TAILQ_EMPTY(&mp->mnt_uppers))
3816 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
3817 mmp->mnt_op = &vgonel_vfsops;
3818 mmp->mnt_kern_flag |= MNTK_MARKER;
3820 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
3821 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
3822 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
3823 ump = TAILQ_NEXT(ump, mnt_upper_link);
3826 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
3829 case VFS_NOTIFY_UPPER_RECLAIM:
3830 VFS_RECLAIM_LOWERVP(ump, vp);
3832 case VFS_NOTIFY_UPPER_UNLINK:
3833 VFS_UNLINK_LOWERVP(ump, vp);
3836 KASSERT(0, ("invalid event %d", event));
3840 ump = TAILQ_NEXT(mmp, mnt_upper_link);
3841 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
3844 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
3845 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
3846 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
3847 wakeup(&mp->mnt_uppers);
3853 * vgone, with the vp interlock held.
3856 vgonel(struct vnode *vp)
3861 bool active, doinginact, oweinact;
3863 ASSERT_VOP_ELOCKED(vp, "vgonel");
3864 ASSERT_VI_LOCKED(vp, "vgonel");
3865 VNASSERT(vp->v_holdcnt, vp,
3866 ("vgonel: vp %p has no reference.", vp));
3867 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3871 * Don't vgonel if we're already doomed.
3873 if (VN_IS_DOOMED(vp))
3876 * Paired with freevnode.
3878 vn_seqc_write_begin_locked(vp);
3880 vn_irflag_set_locked(vp, VIRF_DOOMED);
3883 * Check to see if the vnode is in use. If so, we have to
3884 * call VOP_CLOSE() and VOP_INACTIVE().
3886 * It could be that VOP_INACTIVE() requested reclamation, in
3887 * which case we should avoid recursion, so check
3888 * VI_DOINGINACT. This is not precise but good enough.
3890 active = vp->v_usecount > 0;
3891 oweinact = (vp->v_iflag & VI_OWEINACT) != 0;
3892 doinginact = (vp->v_iflag & VI_DOINGINACT) != 0;
3895 * If we need to do inactive VI_OWEINACT will be set.
3897 if (vp->v_iflag & VI_DEFINACT) {
3898 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count"));
3899 vp->v_iflag &= ~VI_DEFINACT;
3902 VNASSERT(vp->v_holdcnt > 0, vp, ("vnode without hold count"));
3905 cache_purge_vgone(vp);
3906 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
3909 * If purging an active vnode, it must be closed and
3910 * deactivated before being reclaimed.
3913 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
3914 if ((oweinact || active) && !doinginact) {
3919 if (vp->v_type == VSOCK)
3920 vfs_unp_reclaim(vp);
3923 * Clean out any buffers associated with the vnode.
3924 * If the flush fails, just toss the buffers.
3927 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
3928 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
3929 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
3930 while (vinvalbuf(vp, 0, 0, 0) != 0)
3934 BO_LOCK(&vp->v_bufobj);
3935 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
3936 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3937 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
3938 vp->v_bufobj.bo_clean.bv_cnt == 0,
3939 ("vp %p bufobj not invalidated", vp));
3942 * For VMIO bufobj, BO_DEAD is set later, or in
3943 * vm_object_terminate() after the object's page queue is
3946 object = vp->v_bufobj.bo_object;
3948 vp->v_bufobj.bo_flag |= BO_DEAD;
3949 BO_UNLOCK(&vp->v_bufobj);
3952 * Handle the VM part. Tmpfs handles v_object on its own (the
3953 * OBJT_VNODE check). Nullfs or other bypassing filesystems
3954 * should not touch the object borrowed from the lower vnode
3955 * (the handle check).
3957 if (object != NULL && object->type == OBJT_VNODE &&
3958 object->handle == vp)
3959 vnode_destroy_vobject(vp);
3962 * Reclaim the vnode.
3964 if (VOP_RECLAIM(vp))
3965 panic("vgone: cannot reclaim");
3967 vn_finished_secondary_write(mp);
3968 VNASSERT(vp->v_object == NULL, vp,
3969 ("vop_reclaim left v_object vp=%p", vp));
3971 * Clear the advisory locks and wake up waiting threads.
3973 (void)VOP_ADVLOCKPURGE(vp);
3976 * Delete from old mount point vnode list.
3980 * Done with purge, reset to the standard lock and invalidate
3984 vp->v_vnlock = &vp->v_lock;
3985 vp->v_op = &dead_vnodeops;
3990 * Print out a description of a vnode.
3992 static const char * const typename[] =
3993 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
3996 _Static_assert((VHOLD_ALL_FLAGS & ~VHOLD_NO_SMR) == 0,
3997 "new hold count flag not added to vn_printf");
4000 vn_printf(struct vnode *vp, const char *fmt, ...)
4003 char buf[256], buf2[16];
4011 printf("%p: ", (void *)vp);
4012 printf("type %s\n", typename[vp->v_type]);
4013 holdcnt = atomic_load_int(&vp->v_holdcnt);
4014 printf(" usecount %d, writecount %d, refcount %d seqc users %d",
4015 vp->v_usecount, vp->v_writecount, holdcnt & ~VHOLD_ALL_FLAGS,
4017 switch (vp->v_type) {
4019 printf(" mountedhere %p\n", vp->v_mountedhere);
4022 printf(" rdev %p\n", vp->v_rdev);
4025 printf(" socket %p\n", vp->v_unpcb);
4028 printf(" fifoinfo %p\n", vp->v_fifoinfo);
4036 if (holdcnt & VHOLD_NO_SMR)
4037 strlcat(buf, "|VHOLD_NO_SMR", sizeof(buf));
4038 printf(" hold count flags (%s)\n", buf + 1);
4042 irflag = vn_irflag_read(vp);
4043 if (irflag & VIRF_DOOMED)
4044 strlcat(buf, "|VIRF_DOOMED", sizeof(buf));
4045 if (irflag & VIRF_PGREAD)
4046 strlcat(buf, "|VIRF_PGREAD", sizeof(buf));
4047 if (irflag & VIRF_MOUNTPOINT)
4048 strlcat(buf, "|VIRF_MOUNTPOINT", sizeof(buf));
4049 flags = irflag & ~(VIRF_DOOMED | VIRF_PGREAD | VIRF_MOUNTPOINT);
4051 snprintf(buf2, sizeof(buf2), "|VIRF(0x%lx)", flags);
4052 strlcat(buf, buf2, sizeof(buf));
4054 if (vp->v_vflag & VV_ROOT)
4055 strlcat(buf, "|VV_ROOT", sizeof(buf));
4056 if (vp->v_vflag & VV_ISTTY)
4057 strlcat(buf, "|VV_ISTTY", sizeof(buf));
4058 if (vp->v_vflag & VV_NOSYNC)
4059 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
4060 if (vp->v_vflag & VV_ETERNALDEV)
4061 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
4062 if (vp->v_vflag & VV_CACHEDLABEL)
4063 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
4064 if (vp->v_vflag & VV_VMSIZEVNLOCK)
4065 strlcat(buf, "|VV_VMSIZEVNLOCK", sizeof(buf));
4066 if (vp->v_vflag & VV_COPYONWRITE)
4067 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
4068 if (vp->v_vflag & VV_SYSTEM)
4069 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
4070 if (vp->v_vflag & VV_PROCDEP)
4071 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
4072 if (vp->v_vflag & VV_NOKNOTE)
4073 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
4074 if (vp->v_vflag & VV_DELETED)
4075 strlcat(buf, "|VV_DELETED", sizeof(buf));
4076 if (vp->v_vflag & VV_MD)
4077 strlcat(buf, "|VV_MD", sizeof(buf));
4078 if (vp->v_vflag & VV_FORCEINSMQ)
4079 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
4080 if (vp->v_vflag & VV_READLINK)
4081 strlcat(buf, "|VV_READLINK", sizeof(buf));
4082 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
4083 VV_CACHEDLABEL | VV_VMSIZEVNLOCK | VV_COPYONWRITE | VV_SYSTEM |
4084 VV_PROCDEP | VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ |
4087 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
4088 strlcat(buf, buf2, sizeof(buf));
4090 if (vp->v_iflag & VI_TEXT_REF)
4091 strlcat(buf, "|VI_TEXT_REF", sizeof(buf));
4092 if (vp->v_iflag & VI_MOUNT)
4093 strlcat(buf, "|VI_MOUNT", sizeof(buf));
4094 if (vp->v_iflag & VI_DOINGINACT)
4095 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
4096 if (vp->v_iflag & VI_OWEINACT)
4097 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
4098 if (vp->v_iflag & VI_DEFINACT)
4099 strlcat(buf, "|VI_DEFINACT", sizeof(buf));
4100 flags = vp->v_iflag & ~(VI_TEXT_REF | VI_MOUNT | VI_DOINGINACT |
4101 VI_OWEINACT | VI_DEFINACT);
4103 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
4104 strlcat(buf, buf2, sizeof(buf));
4106 if (vp->v_mflag & VMP_LAZYLIST)
4107 strlcat(buf, "|VMP_LAZYLIST", sizeof(buf));
4108 flags = vp->v_mflag & ~(VMP_LAZYLIST);
4110 snprintf(buf2, sizeof(buf2), "|VMP(0x%lx)", flags);
4111 strlcat(buf, buf2, sizeof(buf));
4113 printf(" flags (%s)", buf + 1);
4114 if (mtx_owned(VI_MTX(vp)))
4115 printf(" VI_LOCKed");
4117 if (vp->v_object != NULL)
4118 printf(" v_object %p ref %d pages %d "
4119 "cleanbuf %d dirtybuf %d\n",
4120 vp->v_object, vp->v_object->ref_count,
4121 vp->v_object->resident_page_count,
4122 vp->v_bufobj.bo_clean.bv_cnt,
4123 vp->v_bufobj.bo_dirty.bv_cnt);
4125 lockmgr_printinfo(vp->v_vnlock);
4126 if (vp->v_data != NULL)
4132 * List all of the locked vnodes in the system.
4133 * Called when debugging the kernel.
4135 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
4141 * Note: because this is DDB, we can't obey the locking semantics
4142 * for these structures, which means we could catch an inconsistent
4143 * state and dereference a nasty pointer. Not much to be done
4146 db_printf("Locked vnodes\n");
4147 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4148 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4149 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
4150 vn_printf(vp, "vnode ");
4156 * Show details about the given vnode.
4158 DB_SHOW_COMMAND(vnode, db_show_vnode)
4164 vp = (struct vnode *)addr;
4165 vn_printf(vp, "vnode ");
4169 * Show details about the given mount point.
4171 DB_SHOW_COMMAND(mount, db_show_mount)
4182 /* No address given, print short info about all mount points. */
4183 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4184 db_printf("%p %s on %s (%s)\n", mp,
4185 mp->mnt_stat.f_mntfromname,
4186 mp->mnt_stat.f_mntonname,
4187 mp->mnt_stat.f_fstypename);
4191 db_printf("\nMore info: show mount <addr>\n");
4195 mp = (struct mount *)addr;
4196 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
4197 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
4200 mflags = mp->mnt_flag;
4201 #define MNT_FLAG(flag) do { \
4202 if (mflags & (flag)) { \
4203 if (buf[0] != '\0') \
4204 strlcat(buf, ", ", sizeof(buf)); \
4205 strlcat(buf, (#flag) + 4, sizeof(buf)); \
4206 mflags &= ~(flag); \
4209 MNT_FLAG(MNT_RDONLY);
4210 MNT_FLAG(MNT_SYNCHRONOUS);
4211 MNT_FLAG(MNT_NOEXEC);
4212 MNT_FLAG(MNT_NOSUID);
4213 MNT_FLAG(MNT_NFS4ACLS);
4214 MNT_FLAG(MNT_UNION);
4215 MNT_FLAG(MNT_ASYNC);
4216 MNT_FLAG(MNT_SUIDDIR);
4217 MNT_FLAG(MNT_SOFTDEP);
4218 MNT_FLAG(MNT_NOSYMFOLLOW);
4219 MNT_FLAG(MNT_GJOURNAL);
4220 MNT_FLAG(MNT_MULTILABEL);
4222 MNT_FLAG(MNT_NOATIME);
4223 MNT_FLAG(MNT_NOCLUSTERR);
4224 MNT_FLAG(MNT_NOCLUSTERW);
4226 MNT_FLAG(MNT_EXRDONLY);
4227 MNT_FLAG(MNT_EXPORTED);
4228 MNT_FLAG(MNT_DEFEXPORTED);
4229 MNT_FLAG(MNT_EXPORTANON);
4230 MNT_FLAG(MNT_EXKERB);
4231 MNT_FLAG(MNT_EXPUBLIC);
4232 MNT_FLAG(MNT_LOCAL);
4233 MNT_FLAG(MNT_QUOTA);
4234 MNT_FLAG(MNT_ROOTFS);
4236 MNT_FLAG(MNT_IGNORE);
4237 MNT_FLAG(MNT_UPDATE);
4238 MNT_FLAG(MNT_DELEXPORT);
4239 MNT_FLAG(MNT_RELOAD);
4240 MNT_FLAG(MNT_FORCE);
4241 MNT_FLAG(MNT_SNAPSHOT);
4242 MNT_FLAG(MNT_BYFSID);
4246 strlcat(buf, ", ", sizeof(buf));
4247 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
4248 "0x%016jx", mflags);
4250 db_printf(" mnt_flag = %s\n", buf);
4253 flags = mp->mnt_kern_flag;
4254 #define MNT_KERN_FLAG(flag) do { \
4255 if (flags & (flag)) { \
4256 if (buf[0] != '\0') \
4257 strlcat(buf, ", ", sizeof(buf)); \
4258 strlcat(buf, (#flag) + 5, sizeof(buf)); \
4262 MNT_KERN_FLAG(MNTK_UNMOUNTF);
4263 MNT_KERN_FLAG(MNTK_ASYNC);
4264 MNT_KERN_FLAG(MNTK_SOFTDEP);
4265 MNT_KERN_FLAG(MNTK_DRAINING);
4266 MNT_KERN_FLAG(MNTK_REFEXPIRE);
4267 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
4268 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
4269 MNT_KERN_FLAG(MNTK_NO_IOPF);
4270 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
4271 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
4272 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
4273 MNT_KERN_FLAG(MNTK_MARKER);
4274 MNT_KERN_FLAG(MNTK_USES_BCACHE);
4275 MNT_KERN_FLAG(MNTK_FPLOOKUP);
4276 MNT_KERN_FLAG(MNTK_NOASYNC);
4277 MNT_KERN_FLAG(MNTK_UNMOUNT);
4278 MNT_KERN_FLAG(MNTK_MWAIT);
4279 MNT_KERN_FLAG(MNTK_SUSPEND);
4280 MNT_KERN_FLAG(MNTK_SUSPEND2);
4281 MNT_KERN_FLAG(MNTK_SUSPENDED);
4282 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
4283 MNT_KERN_FLAG(MNTK_NOKNOTE);
4284 #undef MNT_KERN_FLAG
4287 strlcat(buf, ", ", sizeof(buf));
4288 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
4291 db_printf(" mnt_kern_flag = %s\n", buf);
4293 db_printf(" mnt_opt = ");
4294 opt = TAILQ_FIRST(mp->mnt_opt);
4296 db_printf("%s", opt->name);
4297 opt = TAILQ_NEXT(opt, link);
4298 while (opt != NULL) {
4299 db_printf(", %s", opt->name);
4300 opt = TAILQ_NEXT(opt, link);
4306 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
4307 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
4308 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
4309 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
4310 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
4311 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
4312 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
4313 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
4314 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
4315 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
4316 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
4317 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
4319 db_printf(" mnt_cred = { uid=%u ruid=%u",
4320 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
4321 if (jailed(mp->mnt_cred))
4322 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
4324 db_printf(" mnt_ref = %d (with %d in the struct)\n",
4325 vfs_mount_fetch_counter(mp, MNT_COUNT_REF), mp->mnt_ref);
4326 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
4327 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
4328 db_printf(" mnt_lazyvnodelistsize = %d\n",
4329 mp->mnt_lazyvnodelistsize);
4330 db_printf(" mnt_writeopcount = %d (with %d in the struct)\n",
4331 vfs_mount_fetch_counter(mp, MNT_COUNT_WRITEOPCOUNT), mp->mnt_writeopcount);
4332 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
4333 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
4334 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
4335 db_printf(" mnt_lockref = %d (with %d in the struct)\n",
4336 vfs_mount_fetch_counter(mp, MNT_COUNT_LOCKREF), mp->mnt_lockref);
4337 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
4338 db_printf(" mnt_secondary_accwrites = %d\n",
4339 mp->mnt_secondary_accwrites);
4340 db_printf(" mnt_gjprovider = %s\n",
4341 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
4342 db_printf(" mnt_vfs_ops = %d\n", mp->mnt_vfs_ops);
4344 db_printf("\n\nList of active vnodes\n");
4345 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4346 if (vp->v_type != VMARKER && vp->v_holdcnt > 0) {
4347 vn_printf(vp, "vnode ");
4352 db_printf("\n\nList of inactive vnodes\n");
4353 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4354 if (vp->v_type != VMARKER && vp->v_holdcnt == 0) {
4355 vn_printf(vp, "vnode ");
4364 * Fill in a struct xvfsconf based on a struct vfsconf.
4367 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
4369 struct xvfsconf xvfsp;
4371 bzero(&xvfsp, sizeof(xvfsp));
4372 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4373 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4374 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4375 xvfsp.vfc_flags = vfsp->vfc_flags;
4377 * These are unused in userland, we keep them
4378 * to not break binary compatibility.
4380 xvfsp.vfc_vfsops = NULL;
4381 xvfsp.vfc_next = NULL;
4382 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4385 #ifdef COMPAT_FREEBSD32
4387 uint32_t vfc_vfsops;
4388 char vfc_name[MFSNAMELEN];
4389 int32_t vfc_typenum;
4390 int32_t vfc_refcount;
4396 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
4398 struct xvfsconf32 xvfsp;
4400 bzero(&xvfsp, sizeof(xvfsp));
4401 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4402 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4403 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4404 xvfsp.vfc_flags = vfsp->vfc_flags;
4405 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4410 * Top level filesystem related information gathering.
4413 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
4415 struct vfsconf *vfsp;
4420 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4421 #ifdef COMPAT_FREEBSD32
4422 if (req->flags & SCTL_MASK32)
4423 error = vfsconf2x32(req, vfsp);
4426 error = vfsconf2x(req, vfsp);
4434 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
4435 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
4436 "S,xvfsconf", "List of all configured filesystems");
4438 #ifndef BURN_BRIDGES
4439 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
4442 vfs_sysctl(SYSCTL_HANDLER_ARGS)
4444 int *name = (int *)arg1 - 1; /* XXX */
4445 u_int namelen = arg2 + 1; /* XXX */
4446 struct vfsconf *vfsp;
4448 log(LOG_WARNING, "userland calling deprecated sysctl, "
4449 "please rebuild world\n");
4451 #if 1 || defined(COMPAT_PRELITE2)
4452 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
4454 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
4458 case VFS_MAXTYPENUM:
4461 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
4464 return (ENOTDIR); /* overloaded */
4466 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4467 if (vfsp->vfc_typenum == name[2])
4472 return (EOPNOTSUPP);
4473 #ifdef COMPAT_FREEBSD32
4474 if (req->flags & SCTL_MASK32)
4475 return (vfsconf2x32(req, vfsp));
4478 return (vfsconf2x(req, vfsp));
4480 return (EOPNOTSUPP);
4483 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
4484 CTLFLAG_MPSAFE, vfs_sysctl,
4485 "Generic filesystem");
4487 #if 1 || defined(COMPAT_PRELITE2)
4490 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
4493 struct vfsconf *vfsp;
4494 struct ovfsconf ovfs;
4497 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4498 bzero(&ovfs, sizeof(ovfs));
4499 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
4500 strcpy(ovfs.vfc_name, vfsp->vfc_name);
4501 ovfs.vfc_index = vfsp->vfc_typenum;
4502 ovfs.vfc_refcount = vfsp->vfc_refcount;
4503 ovfs.vfc_flags = vfsp->vfc_flags;
4504 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
4514 #endif /* 1 || COMPAT_PRELITE2 */
4515 #endif /* !BURN_BRIDGES */
4517 #define KINFO_VNODESLOP 10
4520 * Dump vnode list (via sysctl).
4524 sysctl_vnode(SYSCTL_HANDLER_ARGS)
4532 * Stale numvnodes access is not fatal here.
4535 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
4537 /* Make an estimate */
4538 return (SYSCTL_OUT(req, 0, len));
4540 error = sysctl_wire_old_buffer(req, 0);
4543 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
4545 mtx_lock(&mountlist_mtx);
4546 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4547 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
4550 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4554 xvn[n].xv_size = sizeof *xvn;
4555 xvn[n].xv_vnode = vp;
4556 xvn[n].xv_id = 0; /* XXX compat */
4557 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
4559 XV_COPY(writecount);
4565 xvn[n].xv_flag = vp->v_vflag;
4567 switch (vp->v_type) {
4574 if (vp->v_rdev == NULL) {
4578 xvn[n].xv_dev = dev2udev(vp->v_rdev);
4581 xvn[n].xv_socket = vp->v_socket;
4584 xvn[n].xv_fifo = vp->v_fifoinfo;
4589 /* shouldn't happen? */
4597 mtx_lock(&mountlist_mtx);
4602 mtx_unlock(&mountlist_mtx);
4604 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
4609 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
4610 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
4615 unmount_or_warn(struct mount *mp)
4619 error = dounmount(mp, MNT_FORCE, curthread);
4621 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
4625 printf("%d)\n", error);
4630 * Unmount all filesystems. The list is traversed in reverse order
4631 * of mounting to avoid dependencies.
4634 vfs_unmountall(void)
4636 struct mount *mp, *tmp;
4638 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
4641 * Since this only runs when rebooting, it is not interlocked.
4643 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
4647 * Forcibly unmounting "/dev" before "/" would prevent clean
4648 * unmount of the latter.
4650 if (mp == rootdevmp)
4653 unmount_or_warn(mp);
4656 if (rootdevmp != NULL)
4657 unmount_or_warn(rootdevmp);
4661 vfs_deferred_inactive(struct vnode *vp, int lkflags)
4664 ASSERT_VI_LOCKED(vp, __func__);
4665 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, ("VI_DEFINACT still set"));
4666 if ((vp->v_iflag & VI_OWEINACT) == 0) {
4670 if (vn_lock(vp, lkflags) == 0) {
4677 vdefer_inactive_unlocked(vp);
4681 vfs_periodic_inactive_filter(struct vnode *vp, void *arg)
4684 return (vp->v_iflag & VI_DEFINACT);
4687 static void __noinline
4688 vfs_periodic_inactive(struct mount *mp, int flags)
4690 struct vnode *vp, *mvp;
4693 lkflags = LK_EXCLUSIVE | LK_INTERLOCK;
4694 if (flags != MNT_WAIT)
4695 lkflags |= LK_NOWAIT;
4697 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_inactive_filter, NULL) {
4698 if ((vp->v_iflag & VI_DEFINACT) == 0) {
4702 vp->v_iflag &= ~VI_DEFINACT;
4703 vfs_deferred_inactive(vp, lkflags);
4708 vfs_want_msync(struct vnode *vp)
4710 struct vm_object *obj;
4713 * This test may be performed without any locks held.
4714 * We rely on vm_object's type stability.
4716 if (vp->v_vflag & VV_NOSYNC)
4719 return (obj != NULL && vm_object_mightbedirty(obj));
4723 vfs_periodic_msync_inactive_filter(struct vnode *vp, void *arg __unused)
4726 if (vp->v_vflag & VV_NOSYNC)
4728 if (vp->v_iflag & VI_DEFINACT)
4730 return (vfs_want_msync(vp));
4733 static void __noinline
4734 vfs_periodic_msync_inactive(struct mount *mp, int flags)
4736 struct vnode *vp, *mvp;
4737 struct vm_object *obj;
4738 int lkflags, objflags;
4741 lkflags = LK_EXCLUSIVE | LK_INTERLOCK;
4742 if (flags != MNT_WAIT) {
4743 lkflags |= LK_NOWAIT;
4744 objflags = OBJPC_NOSYNC;
4746 objflags = OBJPC_SYNC;
4749 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_msync_inactive_filter, NULL) {
4751 if (vp->v_iflag & VI_DEFINACT) {
4752 vp->v_iflag &= ~VI_DEFINACT;
4755 if (!vfs_want_msync(vp)) {
4757 vfs_deferred_inactive(vp, lkflags);
4762 if (vget(vp, lkflags) == 0) {
4764 if (obj != NULL && (vp->v_vflag & VV_NOSYNC) == 0) {
4765 VM_OBJECT_WLOCK(obj);
4766 vm_object_page_clean(obj, 0, 0, objflags);
4767 VM_OBJECT_WUNLOCK(obj);
4774 vdefer_inactive_unlocked(vp);
4780 vfs_periodic(struct mount *mp, int flags)
4783 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
4785 if ((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0)
4786 vfs_periodic_inactive(mp, flags);
4788 vfs_periodic_msync_inactive(mp, flags);
4792 destroy_vpollinfo_free(struct vpollinfo *vi)
4795 knlist_destroy(&vi->vpi_selinfo.si_note);
4796 mtx_destroy(&vi->vpi_lock);
4797 free(vi, M_VNODEPOLL);
4801 destroy_vpollinfo(struct vpollinfo *vi)
4804 knlist_clear(&vi->vpi_selinfo.si_note, 1);
4805 seldrain(&vi->vpi_selinfo);
4806 destroy_vpollinfo_free(vi);
4810 * Initialize per-vnode helper structure to hold poll-related state.
4813 v_addpollinfo(struct vnode *vp)
4815 struct vpollinfo *vi;
4817 if (vp->v_pollinfo != NULL)
4819 vi = malloc(sizeof(*vi), M_VNODEPOLL, M_WAITOK | M_ZERO);
4820 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
4821 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
4822 vfs_knlunlock, vfs_knl_assert_lock);
4824 if (vp->v_pollinfo != NULL) {
4826 destroy_vpollinfo_free(vi);
4829 vp->v_pollinfo = vi;
4834 * Record a process's interest in events which might happen to
4835 * a vnode. Because poll uses the historic select-style interface
4836 * internally, this routine serves as both the ``check for any
4837 * pending events'' and the ``record my interest in future events''
4838 * functions. (These are done together, while the lock is held,
4839 * to avoid race conditions.)
4842 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
4846 mtx_lock(&vp->v_pollinfo->vpi_lock);
4847 if (vp->v_pollinfo->vpi_revents & events) {
4849 * This leaves events we are not interested
4850 * in available for the other process which
4851 * which presumably had requested them
4852 * (otherwise they would never have been
4855 events &= vp->v_pollinfo->vpi_revents;
4856 vp->v_pollinfo->vpi_revents &= ~events;
4858 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4861 vp->v_pollinfo->vpi_events |= events;
4862 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
4863 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4868 * Routine to create and manage a filesystem syncer vnode.
4870 #define sync_close ((int (*)(struct vop_close_args *))nullop)
4871 static int sync_fsync(struct vop_fsync_args *);
4872 static int sync_inactive(struct vop_inactive_args *);
4873 static int sync_reclaim(struct vop_reclaim_args *);
4875 static struct vop_vector sync_vnodeops = {
4876 .vop_bypass = VOP_EOPNOTSUPP,
4877 .vop_close = sync_close, /* close */
4878 .vop_fsync = sync_fsync, /* fsync */
4879 .vop_inactive = sync_inactive, /* inactive */
4880 .vop_need_inactive = vop_stdneed_inactive, /* need_inactive */
4881 .vop_reclaim = sync_reclaim, /* reclaim */
4882 .vop_lock1 = vop_stdlock, /* lock */
4883 .vop_unlock = vop_stdunlock, /* unlock */
4884 .vop_islocked = vop_stdislocked, /* islocked */
4886 VFS_VOP_VECTOR_REGISTER(sync_vnodeops);
4889 * Create a new filesystem syncer vnode for the specified mount point.
4892 vfs_allocate_syncvnode(struct mount *mp)
4896 static long start, incr, next;
4899 /* Allocate a new vnode */
4900 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
4902 panic("vfs_allocate_syncvnode: getnewvnode() failed");
4904 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4905 vp->v_vflag |= VV_FORCEINSMQ;
4906 error = insmntque(vp, mp);
4908 panic("vfs_allocate_syncvnode: insmntque() failed");
4909 vp->v_vflag &= ~VV_FORCEINSMQ;
4912 * Place the vnode onto the syncer worklist. We attempt to
4913 * scatter them about on the list so that they will go off
4914 * at evenly distributed times even if all the filesystems
4915 * are mounted at once.
4918 if (next == 0 || next > syncer_maxdelay) {
4922 start = syncer_maxdelay / 2;
4923 incr = syncer_maxdelay;
4929 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
4930 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
4931 mtx_lock(&sync_mtx);
4933 if (mp->mnt_syncer == NULL) {
4934 mp->mnt_syncer = vp;
4937 mtx_unlock(&sync_mtx);
4940 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4947 vfs_deallocate_syncvnode(struct mount *mp)
4951 mtx_lock(&sync_mtx);
4952 vp = mp->mnt_syncer;
4954 mp->mnt_syncer = NULL;
4955 mtx_unlock(&sync_mtx);
4961 * Do a lazy sync of the filesystem.
4964 sync_fsync(struct vop_fsync_args *ap)
4966 struct vnode *syncvp = ap->a_vp;
4967 struct mount *mp = syncvp->v_mount;
4972 * We only need to do something if this is a lazy evaluation.
4974 if (ap->a_waitfor != MNT_LAZY)
4978 * Move ourselves to the back of the sync list.
4980 bo = &syncvp->v_bufobj;
4982 vn_syncer_add_to_worklist(bo, syncdelay);
4986 * Walk the list of vnodes pushing all that are dirty and
4987 * not already on the sync list.
4989 if (vfs_busy(mp, MBF_NOWAIT) != 0)
4991 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
4995 save = curthread_pflags_set(TDP_SYNCIO);
4997 * The filesystem at hand may be idle with free vnodes stored in the
4998 * batch. Return them instead of letting them stay there indefinitely.
5000 vfs_periodic(mp, MNT_NOWAIT);
5001 error = VFS_SYNC(mp, MNT_LAZY);
5002 curthread_pflags_restore(save);
5003 vn_finished_write(mp);
5009 * The syncer vnode is no referenced.
5012 sync_inactive(struct vop_inactive_args *ap)
5020 * The syncer vnode is no longer needed and is being decommissioned.
5022 * Modifications to the worklist must be protected by sync_mtx.
5025 sync_reclaim(struct vop_reclaim_args *ap)
5027 struct vnode *vp = ap->a_vp;
5032 mtx_lock(&sync_mtx);
5033 if (vp->v_mount->mnt_syncer == vp)
5034 vp->v_mount->mnt_syncer = NULL;
5035 if (bo->bo_flag & BO_ONWORKLST) {
5036 LIST_REMOVE(bo, bo_synclist);
5037 syncer_worklist_len--;
5039 bo->bo_flag &= ~BO_ONWORKLST;
5041 mtx_unlock(&sync_mtx);
5048 vn_need_pageq_flush(struct vnode *vp)
5050 struct vm_object *obj;
5053 MPASS(mtx_owned(VI_MTX(vp)));
5055 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
5056 vm_object_mightbedirty(obj))
5062 * Check if vnode represents a disk device
5065 vn_isdisk_error(struct vnode *vp, int *errp)
5069 if (vp->v_type != VCHR) {
5075 if (vp->v_rdev == NULL)
5077 else if (vp->v_rdev->si_devsw == NULL)
5079 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
5084 return (error == 0);
5088 vn_isdisk(struct vnode *vp)
5092 return (vn_isdisk_error(vp, &error));
5096 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
5097 * the comment above cache_fplookup for details.
5100 vaccess_vexec_smr(mode_t file_mode, uid_t file_uid, gid_t file_gid, struct ucred *cred)
5104 VFS_SMR_ASSERT_ENTERED();
5106 /* Check the owner. */
5107 if (cred->cr_uid == file_uid) {
5108 if (file_mode & S_IXUSR)
5113 /* Otherwise, check the groups (first match) */
5114 if (groupmember(file_gid, cred)) {
5115 if (file_mode & S_IXGRP)
5120 /* Otherwise, check everyone else. */
5121 if (file_mode & S_IXOTH)
5125 * Permission check failed, but it is possible denial will get overwritten
5126 * (e.g., when root is traversing through a 700 directory owned by someone
5129 * vaccess() calls priv_check_cred which in turn can descent into MAC
5130 * modules overriding this result. It's quite unclear what semantics
5131 * are allowed for them to operate, thus for safety we don't call them
5132 * from within the SMR section. This also means if any such modules
5133 * are present, we have to let the regular lookup decide.
5135 error = priv_check_cred_vfs_lookup_nomac(cred);
5141 * MAC modules present.
5152 * Common filesystem object access control check routine. Accepts a
5153 * vnode's type, "mode", uid and gid, requested access mode, and credentials.
5154 * Returns 0 on success, or an errno on failure.
5157 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
5158 accmode_t accmode, struct ucred *cred)
5160 accmode_t dac_granted;
5161 accmode_t priv_granted;
5163 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
5164 ("invalid bit in accmode"));
5165 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
5166 ("VAPPEND without VWRITE"));
5169 * Look for a normal, non-privileged way to access the file/directory
5170 * as requested. If it exists, go with that.
5175 /* Check the owner. */
5176 if (cred->cr_uid == file_uid) {
5177 dac_granted |= VADMIN;
5178 if (file_mode & S_IXUSR)
5179 dac_granted |= VEXEC;
5180 if (file_mode & S_IRUSR)
5181 dac_granted |= VREAD;
5182 if (file_mode & S_IWUSR)
5183 dac_granted |= (VWRITE | VAPPEND);
5185 if ((accmode & dac_granted) == accmode)
5191 /* Otherwise, check the groups (first match) */
5192 if (groupmember(file_gid, cred)) {
5193 if (file_mode & S_IXGRP)
5194 dac_granted |= VEXEC;
5195 if (file_mode & S_IRGRP)
5196 dac_granted |= VREAD;
5197 if (file_mode & S_IWGRP)
5198 dac_granted |= (VWRITE | VAPPEND);
5200 if ((accmode & dac_granted) == accmode)
5206 /* Otherwise, check everyone else. */
5207 if (file_mode & S_IXOTH)
5208 dac_granted |= VEXEC;
5209 if (file_mode & S_IROTH)
5210 dac_granted |= VREAD;
5211 if (file_mode & S_IWOTH)
5212 dac_granted |= (VWRITE | VAPPEND);
5213 if ((accmode & dac_granted) == accmode)
5218 * Build a privilege mask to determine if the set of privileges
5219 * satisfies the requirements when combined with the granted mask
5220 * from above. For each privilege, if the privilege is required,
5221 * bitwise or the request type onto the priv_granted mask.
5227 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
5228 * requests, instead of PRIV_VFS_EXEC.
5230 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
5231 !priv_check_cred(cred, PRIV_VFS_LOOKUP))
5232 priv_granted |= VEXEC;
5235 * Ensure that at least one execute bit is on. Otherwise,
5236 * a privileged user will always succeed, and we don't want
5237 * this to happen unless the file really is executable.
5239 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
5240 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
5241 !priv_check_cred(cred, PRIV_VFS_EXEC))
5242 priv_granted |= VEXEC;
5245 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
5246 !priv_check_cred(cred, PRIV_VFS_READ))
5247 priv_granted |= VREAD;
5249 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
5250 !priv_check_cred(cred, PRIV_VFS_WRITE))
5251 priv_granted |= (VWRITE | VAPPEND);
5253 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
5254 !priv_check_cred(cred, PRIV_VFS_ADMIN))
5255 priv_granted |= VADMIN;
5257 if ((accmode & (priv_granted | dac_granted)) == accmode) {
5261 return ((accmode & VADMIN) ? EPERM : EACCES);
5265 * Credential check based on process requesting service, and per-attribute
5269 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
5270 struct thread *td, accmode_t accmode)
5274 * Kernel-invoked always succeeds.
5280 * Do not allow privileged processes in jail to directly manipulate
5281 * system attributes.
5283 switch (attrnamespace) {
5284 case EXTATTR_NAMESPACE_SYSTEM:
5285 /* Potentially should be: return (EPERM); */
5286 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
5287 case EXTATTR_NAMESPACE_USER:
5288 return (VOP_ACCESS(vp, accmode, cred, td));
5294 #ifdef DEBUG_VFS_LOCKS
5296 * This only exists to suppress warnings from unlocked specfs accesses. It is
5297 * no longer ok to have an unlocked VFS.
5299 #define IGNORE_LOCK(vp) (KERNEL_PANICKED() || (vp) == NULL || \
5300 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
5302 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
5303 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
5304 "Drop into debugger on lock violation");
5306 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
5307 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
5308 0, "Check for interlock across VOPs");
5310 int vfs_badlock_print = 1; /* Print lock violations. */
5311 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
5312 0, "Print lock violations");
5314 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */
5315 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode,
5316 0, "Print vnode details on lock violations");
5319 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
5320 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
5321 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
5325 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
5329 if (vfs_badlock_backtrace)
5332 if (vfs_badlock_vnode)
5333 vn_printf(vp, "vnode ");
5334 if (vfs_badlock_print)
5335 printf("%s: %p %s\n", str, (void *)vp, msg);
5336 if (vfs_badlock_ddb)
5337 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
5341 assert_vi_locked(struct vnode *vp, const char *str)
5344 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
5345 vfs_badlock("interlock is not locked but should be", str, vp);
5349 assert_vi_unlocked(struct vnode *vp, const char *str)
5352 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
5353 vfs_badlock("interlock is locked but should not be", str, vp);
5357 assert_vop_locked(struct vnode *vp, const char *str)
5361 if (!IGNORE_LOCK(vp)) {
5362 locked = VOP_ISLOCKED(vp);
5363 if (locked == 0 || locked == LK_EXCLOTHER)
5364 vfs_badlock("is not locked but should be", str, vp);
5369 assert_vop_unlocked(struct vnode *vp, const char *str)
5372 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
5373 vfs_badlock("is locked but should not be", str, vp);
5377 assert_vop_elocked(struct vnode *vp, const char *str)
5380 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
5381 vfs_badlock("is not exclusive locked but should be", str, vp);
5383 #endif /* DEBUG_VFS_LOCKS */
5386 vop_rename_fail(struct vop_rename_args *ap)
5389 if (ap->a_tvp != NULL)
5391 if (ap->a_tdvp == ap->a_tvp)
5400 vop_rename_pre(void *ap)
5402 struct vop_rename_args *a = ap;
5404 #ifdef DEBUG_VFS_LOCKS
5406 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
5407 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
5408 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
5409 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
5411 /* Check the source (from). */
5412 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
5413 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
5414 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
5415 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
5416 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
5418 /* Check the target. */
5420 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
5421 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
5424 * It may be tempting to add vn_seqc_write_begin/end calls here and
5425 * in vop_rename_post but that's not going to work out since some
5426 * filesystems relookup vnodes mid-rename. This is probably a bug.
5428 * For now filesystems are expected to do the relevant calls after they
5429 * decide what vnodes to operate on.
5431 if (a->a_tdvp != a->a_fdvp)
5433 if (a->a_tvp != a->a_fvp)
5440 #ifdef DEBUG_VFS_LOCKS
5442 vop_fplookup_vexec_debugpre(void *ap __unused)
5445 VFS_SMR_ASSERT_ENTERED();
5449 vop_fplookup_vexec_debugpost(void *ap __unused, int rc __unused)
5452 VFS_SMR_ASSERT_ENTERED();
5456 vop_strategy_debugpre(void *ap)
5458 struct vop_strategy_args *a;
5465 * Cluster ops lock their component buffers but not the IO container.
5467 if ((bp->b_flags & B_CLUSTER) != 0)
5470 if (!KERNEL_PANICKED() && !BUF_ISLOCKED(bp)) {
5471 if (vfs_badlock_print)
5473 "VOP_STRATEGY: bp is not locked but should be\n");
5474 if (vfs_badlock_ddb)
5475 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
5480 vop_lock_debugpre(void *ap)
5482 struct vop_lock1_args *a = ap;
5484 if ((a->a_flags & LK_INTERLOCK) == 0)
5485 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
5487 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
5491 vop_lock_debugpost(void *ap, int rc)
5493 struct vop_lock1_args *a = ap;
5495 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
5496 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
5497 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
5501 vop_unlock_debugpre(void *ap)
5503 struct vop_unlock_args *a = ap;
5505 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
5509 vop_need_inactive_debugpre(void *ap)
5511 struct vop_need_inactive_args *a = ap;
5513 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5517 vop_need_inactive_debugpost(void *ap, int rc)
5519 struct vop_need_inactive_args *a = ap;
5521 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5526 vop_create_pre(void *ap)
5528 struct vop_create_args *a;
5533 vn_seqc_write_begin(dvp);
5537 vop_create_post(void *ap, int rc)
5539 struct vop_create_args *a;
5544 vn_seqc_write_end(dvp);
5546 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5550 vop_whiteout_pre(void *ap)
5552 struct vop_whiteout_args *a;
5557 vn_seqc_write_begin(dvp);
5561 vop_whiteout_post(void *ap, int rc)
5563 struct vop_whiteout_args *a;
5568 vn_seqc_write_end(dvp);
5572 vop_deleteextattr_pre(void *ap)
5574 struct vop_deleteextattr_args *a;
5579 vn_seqc_write_begin(vp);
5583 vop_deleteextattr_post(void *ap, int rc)
5585 struct vop_deleteextattr_args *a;
5590 vn_seqc_write_end(vp);
5592 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5596 vop_link_pre(void *ap)
5598 struct vop_link_args *a;
5599 struct vnode *vp, *tdvp;
5604 vn_seqc_write_begin(vp);
5605 vn_seqc_write_begin(tdvp);
5609 vop_link_post(void *ap, int rc)
5611 struct vop_link_args *a;
5612 struct vnode *vp, *tdvp;
5617 vn_seqc_write_end(vp);
5618 vn_seqc_write_end(tdvp);
5620 VFS_KNOTE_LOCKED(vp, NOTE_LINK);
5621 VFS_KNOTE_LOCKED(tdvp, NOTE_WRITE);
5626 vop_mkdir_pre(void *ap)
5628 struct vop_mkdir_args *a;
5633 vn_seqc_write_begin(dvp);
5637 vop_mkdir_post(void *ap, int rc)
5639 struct vop_mkdir_args *a;
5644 vn_seqc_write_end(dvp);
5646 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE | NOTE_LINK);
5649 #ifdef DEBUG_VFS_LOCKS
5651 vop_mkdir_debugpost(void *ap, int rc)
5653 struct vop_mkdir_args *a;
5657 cache_validate(a->a_dvp, *a->a_vpp, a->a_cnp);
5662 vop_mknod_pre(void *ap)
5664 struct vop_mknod_args *a;
5669 vn_seqc_write_begin(dvp);
5673 vop_mknod_post(void *ap, int rc)
5675 struct vop_mknod_args *a;
5680 vn_seqc_write_end(dvp);
5682 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5686 vop_reclaim_post(void *ap, int rc)
5688 struct vop_reclaim_args *a;
5693 ASSERT_VOP_IN_SEQC(vp);
5695 VFS_KNOTE_LOCKED(vp, NOTE_REVOKE);
5699 vop_remove_pre(void *ap)
5701 struct vop_remove_args *a;
5702 struct vnode *dvp, *vp;
5707 vn_seqc_write_begin(dvp);
5708 vn_seqc_write_begin(vp);
5712 vop_remove_post(void *ap, int rc)
5714 struct vop_remove_args *a;
5715 struct vnode *dvp, *vp;
5720 vn_seqc_write_end(dvp);
5721 vn_seqc_write_end(vp);
5723 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5724 VFS_KNOTE_LOCKED(vp, NOTE_DELETE);
5729 vop_rename_post(void *ap, int rc)
5731 struct vop_rename_args *a = ap;
5736 if (a->a_fdvp == a->a_tdvp) {
5737 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR)
5739 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5740 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5742 hint |= NOTE_EXTEND;
5743 if (a->a_fvp->v_type == VDIR)
5745 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5747 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL &&
5748 a->a_tvp->v_type == VDIR)
5750 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5753 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
5755 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
5757 if (a->a_tdvp != a->a_fdvp)
5759 if (a->a_tvp != a->a_fvp)
5767 vop_rmdir_pre(void *ap)
5769 struct vop_rmdir_args *a;
5770 struct vnode *dvp, *vp;
5775 vn_seqc_write_begin(dvp);
5776 vn_seqc_write_begin(vp);
5780 vop_rmdir_post(void *ap, int rc)
5782 struct vop_rmdir_args *a;
5783 struct vnode *dvp, *vp;
5788 vn_seqc_write_end(dvp);
5789 vn_seqc_write_end(vp);
5791 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE | NOTE_LINK);
5792 VFS_KNOTE_LOCKED(vp, NOTE_DELETE);
5797 vop_setattr_pre(void *ap)
5799 struct vop_setattr_args *a;
5804 vn_seqc_write_begin(vp);
5808 vop_setattr_post(void *ap, int rc)
5810 struct vop_setattr_args *a;
5815 vn_seqc_write_end(vp);
5817 VFS_KNOTE_LOCKED(vp, NOTE_ATTRIB);
5821 vop_setacl_pre(void *ap)
5823 struct vop_setacl_args *a;
5828 vn_seqc_write_begin(vp);
5832 vop_setacl_post(void *ap, int rc __unused)
5834 struct vop_setacl_args *a;
5839 vn_seqc_write_end(vp);
5843 vop_setextattr_pre(void *ap)
5845 struct vop_setextattr_args *a;
5850 vn_seqc_write_begin(vp);
5854 vop_setextattr_post(void *ap, int rc)
5856 struct vop_setextattr_args *a;
5861 vn_seqc_write_end(vp);
5863 VFS_KNOTE_LOCKED(vp, NOTE_ATTRIB);
5867 vop_symlink_pre(void *ap)
5869 struct vop_symlink_args *a;
5874 vn_seqc_write_begin(dvp);
5878 vop_symlink_post(void *ap, int rc)
5880 struct vop_symlink_args *a;
5885 vn_seqc_write_end(dvp);
5887 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5891 vop_open_post(void *ap, int rc)
5893 struct vop_open_args *a = ap;
5896 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN);
5900 vop_close_post(void *ap, int rc)
5902 struct vop_close_args *a = ap;
5904 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */
5905 !VN_IS_DOOMED(a->a_vp))) {
5906 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ?
5907 NOTE_CLOSE_WRITE : NOTE_CLOSE);
5912 vop_read_post(void *ap, int rc)
5914 struct vop_read_args *a = ap;
5917 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5921 vop_read_pgcache_post(void *ap, int rc)
5923 struct vop_read_pgcache_args *a = ap;
5926 VFS_KNOTE_UNLOCKED(a->a_vp, NOTE_READ);
5930 vop_readdir_post(void *ap, int rc)
5932 struct vop_readdir_args *a = ap;
5935 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5938 static struct knlist fs_knlist;
5941 vfs_event_init(void *arg)
5943 knlist_init_mtx(&fs_knlist, NULL);
5945 /* XXX - correct order? */
5946 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
5949 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
5952 KNOTE_UNLOCKED(&fs_knlist, event);
5955 static int filt_fsattach(struct knote *kn);
5956 static void filt_fsdetach(struct knote *kn);
5957 static int filt_fsevent(struct knote *kn, long hint);
5959 struct filterops fs_filtops = {
5961 .f_attach = filt_fsattach,
5962 .f_detach = filt_fsdetach,
5963 .f_event = filt_fsevent
5967 filt_fsattach(struct knote *kn)
5970 kn->kn_flags |= EV_CLEAR;
5971 knlist_add(&fs_knlist, kn, 0);
5976 filt_fsdetach(struct knote *kn)
5979 knlist_remove(&fs_knlist, kn, 0);
5983 filt_fsevent(struct knote *kn, long hint)
5986 kn->kn_fflags |= hint;
5987 return (kn->kn_fflags != 0);
5991 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
5997 error = SYSCTL_IN(req, &vc, sizeof(vc));
6000 if (vc.vc_vers != VFS_CTL_VERS1)
6002 mp = vfs_getvfs(&vc.vc_fsid);
6005 /* ensure that a specific sysctl goes to the right filesystem. */
6006 if (strcmp(vc.vc_fstypename, "*") != 0 &&
6007 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
6011 VCTLTOREQ(&vc, req);
6012 error = VFS_SYSCTL(mp, vc.vc_op, req);
6017 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | CTLFLAG_WR,
6018 NULL, 0, sysctl_vfs_ctl, "",
6022 * Function to initialize a va_filerev field sensibly.
6023 * XXX: Wouldn't a random number make a lot more sense ??
6026 init_va_filerev(void)
6031 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
6034 static int filt_vfsread(struct knote *kn, long hint);
6035 static int filt_vfswrite(struct knote *kn, long hint);
6036 static int filt_vfsvnode(struct knote *kn, long hint);
6037 static void filt_vfsdetach(struct knote *kn);
6038 static struct filterops vfsread_filtops = {
6040 .f_detach = filt_vfsdetach,
6041 .f_event = filt_vfsread
6043 static struct filterops vfswrite_filtops = {
6045 .f_detach = filt_vfsdetach,
6046 .f_event = filt_vfswrite
6048 static struct filterops vfsvnode_filtops = {
6050 .f_detach = filt_vfsdetach,
6051 .f_event = filt_vfsvnode
6055 vfs_knllock(void *arg)
6057 struct vnode *vp = arg;
6059 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
6063 vfs_knlunlock(void *arg)
6065 struct vnode *vp = arg;
6071 vfs_knl_assert_lock(void *arg, int what)
6073 #ifdef DEBUG_VFS_LOCKS
6074 struct vnode *vp = arg;
6076 if (what == LA_LOCKED)
6077 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
6079 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
6084 vfs_kqfilter(struct vop_kqfilter_args *ap)
6086 struct vnode *vp = ap->a_vp;
6087 struct knote *kn = ap->a_kn;
6090 switch (kn->kn_filter) {
6092 kn->kn_fop = &vfsread_filtops;
6095 kn->kn_fop = &vfswrite_filtops;
6098 kn->kn_fop = &vfsvnode_filtops;
6104 kn->kn_hook = (caddr_t)vp;
6107 if (vp->v_pollinfo == NULL)
6109 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
6111 knlist_add(knl, kn, 0);
6117 * Detach knote from vnode
6120 filt_vfsdetach(struct knote *kn)
6122 struct vnode *vp = (struct vnode *)kn->kn_hook;
6124 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
6125 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
6131 filt_vfsread(struct knote *kn, long hint)
6133 struct vnode *vp = (struct vnode *)kn->kn_hook;
6138 * filesystem is gone, so set the EOF flag and schedule
6139 * the knote for deletion.
6141 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
6143 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
6148 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
6152 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
6153 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
6160 filt_vfswrite(struct knote *kn, long hint)
6162 struct vnode *vp = (struct vnode *)kn->kn_hook;
6167 * filesystem is gone, so set the EOF flag and schedule
6168 * the knote for deletion.
6170 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
6171 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
6179 filt_vfsvnode(struct knote *kn, long hint)
6181 struct vnode *vp = (struct vnode *)kn->kn_hook;
6185 if (kn->kn_sfflags & hint)
6186 kn->kn_fflags |= hint;
6187 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
6188 kn->kn_flags |= EV_EOF;
6192 res = (kn->kn_fflags != 0);
6198 * Returns whether the directory is empty or not.
6199 * If it is empty, the return value is 0; otherwise
6200 * the return value is an error value (which may
6204 vfs_emptydir(struct vnode *vp)
6208 struct dirent *dirent, *dp, *endp;
6214 ASSERT_VOP_LOCKED(vp, "vfs_emptydir");
6216 dirent = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK);
6217 iov.iov_base = dirent;
6218 iov.iov_len = sizeof(struct dirent);
6223 uio.uio_resid = sizeof(struct dirent);
6224 uio.uio_segflg = UIO_SYSSPACE;
6225 uio.uio_rw = UIO_READ;
6226 uio.uio_td = curthread;
6228 while (eof == 0 && error == 0) {
6229 error = VOP_READDIR(vp, &uio, curthread->td_ucred, &eof,
6233 endp = (void *)((uint8_t *)dirent +
6234 sizeof(struct dirent) - uio.uio_resid);
6235 for (dp = dirent; dp < endp;
6236 dp = (void *)((uint8_t *)dp + GENERIC_DIRSIZ(dp))) {
6237 if (dp->d_type == DT_WHT)
6239 if (dp->d_namlen == 0)
6241 if (dp->d_type != DT_DIR &&
6242 dp->d_type != DT_UNKNOWN) {
6246 if (dp->d_namlen > 2) {
6250 if (dp->d_namlen == 1 &&
6251 dp->d_name[0] != '.') {
6255 if (dp->d_namlen == 2 &&
6256 dp->d_name[1] != '.') {
6260 uio.uio_resid = sizeof(struct dirent);
6263 free(dirent, M_TEMP);
6268 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
6272 if (dp->d_reclen > ap->a_uio->uio_resid)
6273 return (ENAMETOOLONG);
6274 error = uiomove(dp, dp->d_reclen, ap->a_uio);
6276 if (ap->a_ncookies != NULL) {
6277 if (ap->a_cookies != NULL)
6278 free(ap->a_cookies, M_TEMP);
6279 ap->a_cookies = NULL;
6280 *ap->a_ncookies = 0;
6284 if (ap->a_ncookies == NULL)
6287 KASSERT(ap->a_cookies,
6288 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
6290 *ap->a_cookies = realloc(*ap->a_cookies,
6291 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
6292 (*ap->a_cookies)[*ap->a_ncookies] = off;
6293 *ap->a_ncookies += 1;
6298 * The purpose of this routine is to remove granularity from accmode_t,
6299 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
6300 * VADMIN and VAPPEND.
6302 * If it returns 0, the caller is supposed to continue with the usual
6303 * access checks using 'accmode' as modified by this routine. If it
6304 * returns nonzero value, the caller is supposed to return that value
6307 * Note that after this routine runs, accmode may be zero.
6310 vfs_unixify_accmode(accmode_t *accmode)
6313 * There is no way to specify explicit "deny" rule using
6314 * file mode or POSIX.1e ACLs.
6316 if (*accmode & VEXPLICIT_DENY) {
6322 * None of these can be translated into usual access bits.
6323 * Also, the common case for NFSv4 ACLs is to not contain
6324 * either of these bits. Caller should check for VWRITE
6325 * on the containing directory instead.
6327 if (*accmode & (VDELETE_CHILD | VDELETE))
6330 if (*accmode & VADMIN_PERMS) {
6331 *accmode &= ~VADMIN_PERMS;
6336 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
6337 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
6339 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
6345 * Clear out a doomed vnode (if any) and replace it with a new one as long
6346 * as the fs is not being unmounted. Return the root vnode to the caller.
6348 static int __noinline
6349 vfs_cache_root_fallback(struct mount *mp, int flags, struct vnode **vpp)
6355 if (mp->mnt_rootvnode != NULL) {
6357 vp = mp->mnt_rootvnode;
6359 if (!VN_IS_DOOMED(vp)) {
6362 error = vn_lock(vp, flags);
6371 * Clear the old one.
6373 mp->mnt_rootvnode = NULL;
6377 vfs_op_barrier_wait(mp);
6381 error = VFS_CACHEDROOT(mp, flags, vpp);
6384 if (mp->mnt_vfs_ops == 0) {
6386 if (mp->mnt_vfs_ops != 0) {
6390 if (mp->mnt_rootvnode == NULL) {
6392 mp->mnt_rootvnode = *vpp;
6394 if (mp->mnt_rootvnode != *vpp) {
6395 if (!VN_IS_DOOMED(mp->mnt_rootvnode)) {
6396 panic("%s: mismatch between vnode returned "
6397 " by VFS_CACHEDROOT and the one cached "
6399 __func__, *vpp, mp->mnt_rootvnode);
6409 vfs_cache_root(struct mount *mp, int flags, struct vnode **vpp)
6411 struct mount_pcpu *mpcpu;
6415 if (!vfs_op_thread_enter(mp, mpcpu))
6416 return (vfs_cache_root_fallback(mp, flags, vpp));
6417 vp = atomic_load_ptr(&mp->mnt_rootvnode);
6418 if (vp == NULL || VN_IS_DOOMED(vp)) {
6419 vfs_op_thread_exit(mp, mpcpu);
6420 return (vfs_cache_root_fallback(mp, flags, vpp));
6423 vfs_op_thread_exit(mp, mpcpu);
6424 error = vn_lock(vp, flags);
6427 return (vfs_cache_root_fallback(mp, flags, vpp));
6434 vfs_cache_root_clear(struct mount *mp)
6439 * ops > 0 guarantees there is nobody who can see this vnode
6441 MPASS(mp->mnt_vfs_ops > 0);
6442 vp = mp->mnt_rootvnode;
6444 vn_seqc_write_begin(vp);
6445 mp->mnt_rootvnode = NULL;
6450 vfs_cache_root_set(struct mount *mp, struct vnode *vp)
6453 MPASS(mp->mnt_vfs_ops > 0);
6455 mp->mnt_rootvnode = vp;
6459 * These are helper functions for filesystems to traverse all
6460 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
6462 * This interface replaces MNT_VNODE_FOREACH.
6466 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
6471 kern_yield(PRI_USER);
6473 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6474 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL;
6475 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
6476 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */
6477 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp))
6480 if (VN_IS_DOOMED(vp)) {
6487 __mnt_vnode_markerfree_all(mvp, mp);
6488 /* MNT_IUNLOCK(mp); -- done in above function */
6489 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
6492 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
6493 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
6499 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
6503 *mvp = vn_alloc_marker(mp);
6507 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
6508 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */
6509 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp))
6512 if (VN_IS_DOOMED(vp)) {
6521 vn_free_marker(*mvp);
6525 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
6531 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
6539 mtx_assert(MNT_MTX(mp), MA_OWNED);
6541 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6542 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
6545 vn_free_marker(*mvp);
6550 * These are helper functions for filesystems to traverse their
6551 * lazy vnodes. See MNT_VNODE_FOREACH_LAZY() in sys/mount.h
6554 mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp)
6557 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6562 vn_free_marker(*mvp);
6567 * Relock the mp mount vnode list lock with the vp vnode interlock in the
6568 * conventional lock order during mnt_vnode_next_lazy iteration.
6570 * On entry, the mount vnode list lock is held and the vnode interlock is not.
6571 * The list lock is dropped and reacquired. On success, both locks are held.
6572 * On failure, the mount vnode list lock is held but the vnode interlock is
6573 * not, and the procedure may have yielded.
6576 mnt_vnode_next_lazy_relock(struct vnode *mvp, struct mount *mp,
6580 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER &&
6581 TAILQ_NEXT(mvp, v_lazylist) != NULL, mvp,
6582 ("%s: bad marker", __func__));
6583 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp,
6584 ("%s: inappropriate vnode", __func__));
6585 ASSERT_VI_UNLOCKED(vp, __func__);
6586 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6588 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, mvp, v_lazylist);
6589 TAILQ_INSERT_BEFORE(vp, mvp, v_lazylist);
6592 * Note we may be racing against vdrop which transitioned the hold
6593 * count to 0 and now waits for the ->mnt_listmtx lock. This is fine,
6594 * if we are the only user after we get the interlock we will just
6598 mtx_unlock(&mp->mnt_listmtx);
6600 if (VN_IS_DOOMED(vp)) {
6601 VNPASS((vp->v_mflag & VMP_LAZYLIST) == 0, vp);
6604 VNPASS(vp->v_mflag & VMP_LAZYLIST, vp);
6606 * There is nothing to do if we are the last user.
6608 if (!refcount_release_if_not_last(&vp->v_holdcnt))
6610 mtx_lock(&mp->mnt_listmtx);
6615 mtx_lock(&mp->mnt_listmtx);
6619 static struct vnode *
6620 mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6625 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6626 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6628 vp = TAILQ_NEXT(*mvp, v_lazylist);
6629 while (vp != NULL) {
6630 if (vp->v_type == VMARKER) {
6631 vp = TAILQ_NEXT(vp, v_lazylist);
6635 * See if we want to process the vnode. Note we may encounter a
6636 * long string of vnodes we don't care about and hog the list
6637 * as a result. Check for it and requeue the marker.
6639 VNPASS(!VN_IS_DOOMED(vp), vp);
6640 if (!cb(vp, cbarg)) {
6641 if (!should_yield()) {
6642 vp = TAILQ_NEXT(vp, v_lazylist);
6645 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp,
6647 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp,
6649 mtx_unlock(&mp->mnt_listmtx);
6650 kern_yield(PRI_USER);
6651 mtx_lock(&mp->mnt_listmtx);
6655 * Try-lock because this is the wrong lock order.
6657 if (!VI_TRYLOCK(vp) &&
6658 !mnt_vnode_next_lazy_relock(*mvp, mp, vp))
6660 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
6661 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
6662 ("alien vnode on the lazy list %p %p", vp, mp));
6663 VNPASS(vp->v_mount == mp, vp);
6664 VNPASS(!VN_IS_DOOMED(vp), vp);
6667 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist);
6669 /* Check if we are done */
6671 mtx_unlock(&mp->mnt_listmtx);
6672 mnt_vnode_markerfree_lazy(mvp, mp);
6675 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, v_lazylist);
6676 mtx_unlock(&mp->mnt_listmtx);
6677 ASSERT_VI_LOCKED(vp, "lazy iter");
6682 __mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6687 kern_yield(PRI_USER);
6688 mtx_lock(&mp->mnt_listmtx);
6689 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg));
6693 __mnt_vnode_first_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6698 if (TAILQ_EMPTY(&mp->mnt_lazyvnodelist))
6701 *mvp = vn_alloc_marker(mp);
6706 mtx_lock(&mp->mnt_listmtx);
6707 vp = TAILQ_FIRST(&mp->mnt_lazyvnodelist);
6709 mtx_unlock(&mp->mnt_listmtx);
6710 mnt_vnode_markerfree_lazy(mvp, mp);
6713 TAILQ_INSERT_BEFORE(vp, *mvp, v_lazylist);
6714 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg));
6718 __mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp)
6724 mtx_lock(&mp->mnt_listmtx);
6725 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist);
6726 mtx_unlock(&mp->mnt_listmtx);
6727 mnt_vnode_markerfree_lazy(mvp, mp);
6731 vn_dir_check_exec(struct vnode *vp, struct componentname *cnp)
6734 if ((cnp->cn_flags & NOEXECCHECK) != 0) {
6735 cnp->cn_flags &= ~NOEXECCHECK;
6739 return (VOP_ACCESS(vp, VEXEC, cnp->cn_cred, cnp->cn_thread));
6743 * Do not use this variant unless you have means other than the hold count
6744 * to prevent the vnode from getting freed.
6747 vn_seqc_write_begin_unheld_locked(struct vnode *vp)
6750 ASSERT_VI_LOCKED(vp, __func__);
6751 VNPASS(vp->v_seqc_users >= 0, vp);
6753 if (vp->v_seqc_users == 1)
6754 seqc_sleepable_write_begin(&vp->v_seqc);
6758 vn_seqc_write_begin_locked(struct vnode *vp)
6761 ASSERT_VI_LOCKED(vp, __func__);
6762 VNPASS(vp->v_holdcnt > 0, vp);
6763 vn_seqc_write_begin_unheld_locked(vp);
6767 vn_seqc_write_begin(struct vnode *vp)
6771 vn_seqc_write_begin_locked(vp);
6776 vn_seqc_write_begin_unheld(struct vnode *vp)
6780 vn_seqc_write_begin_unheld_locked(vp);
6785 vn_seqc_write_end_locked(struct vnode *vp)
6788 ASSERT_VI_LOCKED(vp, __func__);
6789 VNPASS(vp->v_seqc_users > 0, vp);
6791 if (vp->v_seqc_users == 0)
6792 seqc_sleepable_write_end(&vp->v_seqc);
6796 vn_seqc_write_end(struct vnode *vp)
6800 vn_seqc_write_end_locked(vp);
6805 * Special case handling for allocating and freeing vnodes.
6807 * The counter remains unchanged on free so that a doomed vnode will
6808 * keep testing as in modify as long as it is accessible with SMR.
6811 vn_seqc_init(struct vnode *vp)
6815 vp->v_seqc_users = 0;
6819 vn_seqc_write_end_free(struct vnode *vp)
6822 VNPASS(seqc_in_modify(vp->v_seqc), vp);
6823 VNPASS(vp->v_seqc_users == 1, vp);
6827 vn_irflag_set_locked(struct vnode *vp, short toset)
6831 ASSERT_VI_LOCKED(vp, __func__);
6832 flags = vn_irflag_read(vp);
6833 VNASSERT((flags & toset) == 0, vp,
6834 ("%s: some of the passed flags already set (have %d, passed %d)\n",
6835 __func__, flags, toset));
6836 atomic_store_short(&vp->v_irflag, flags | toset);
6840 vn_irflag_set(struct vnode *vp, short toset)
6844 vn_irflag_set_locked(vp, toset);
6849 vn_irflag_set_cond_locked(struct vnode *vp, short toset)
6853 ASSERT_VI_LOCKED(vp, __func__);
6854 flags = vn_irflag_read(vp);
6855 atomic_store_short(&vp->v_irflag, flags | toset);
6859 vn_irflag_set_cond(struct vnode *vp, short toset)
6863 vn_irflag_set_cond_locked(vp, toset);
6868 vn_irflag_unset_locked(struct vnode *vp, short tounset)
6872 ASSERT_VI_LOCKED(vp, __func__);
6873 flags = vn_irflag_read(vp);
6874 VNASSERT((flags & tounset) == tounset, vp,
6875 ("%s: some of the passed flags not set (have %d, passed %d)\n",
6876 __func__, flags, tounset));
6877 atomic_store_short(&vp->v_irflag, flags & ~tounset);
6881 vn_irflag_unset(struct vnode *vp, short tounset)
6885 vn_irflag_unset_locked(vp, tounset);