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 v_incr_devcount(struct vnode *);
112 static void v_decr_devcount(struct vnode *);
113 static void vgonel(struct vnode *);
114 static void vfs_knllock(void *arg);
115 static void vfs_knlunlock(void *arg);
116 static void vfs_knl_assert_locked(void *arg);
117 static void vfs_knl_assert_unlocked(void *arg);
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");
210 * Various variables used for debugging the new implementation of
212 * XXX these are probably of (very) limited utility now.
214 static int reassignbufcalls;
215 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW | CTLFLAG_STATS,
216 &reassignbufcalls, 0, "Number of calls to reassignbuf");
218 static counter_u64_t deferred_inact;
219 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, deferred_inact, CTLFLAG_RD, &deferred_inact,
220 "Number of times inactive processing was deferred");
222 /* To keep more than one thread at a time from running vfs_getnewfsid */
223 static struct mtx mntid_mtx;
226 * Lock for any access to the following:
231 static struct mtx __exclusive_cache_line vnode_list_mtx;
233 /* Publicly exported FS */
234 struct nfs_public nfs_pub;
236 static uma_zone_t buf_trie_zone;
237 static smr_t buf_trie_smr;
239 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
240 static uma_zone_t vnode_zone;
241 static uma_zone_t vnodepoll_zone;
243 __read_frequently smr_t vfs_smr;
246 * The workitem queue.
248 * It is useful to delay writes of file data and filesystem metadata
249 * for tens of seconds so that quickly created and deleted files need
250 * not waste disk bandwidth being created and removed. To realize this,
251 * we append vnodes to a "workitem" queue. When running with a soft
252 * updates implementation, most pending metadata dependencies should
253 * not wait for more than a few seconds. Thus, mounted on block devices
254 * are delayed only about a half the time that file data is delayed.
255 * Similarly, directory updates are more critical, so are only delayed
256 * about a third the time that file data is delayed. Thus, there are
257 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
258 * one each second (driven off the filesystem syncer process). The
259 * syncer_delayno variable indicates the next queue that is to be processed.
260 * Items that need to be processed soon are placed in this queue:
262 * syncer_workitem_pending[syncer_delayno]
264 * A delay of fifteen seconds is done by placing the request fifteen
265 * entries later in the queue:
267 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
270 static int syncer_delayno;
271 static long syncer_mask;
272 LIST_HEAD(synclist, bufobj);
273 static struct synclist *syncer_workitem_pending;
275 * The sync_mtx protects:
280 * syncer_workitem_pending
281 * syncer_worklist_len
284 static struct mtx sync_mtx;
285 static struct cv sync_wakeup;
287 #define SYNCER_MAXDELAY 32
288 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
289 static int syncdelay = 30; /* max time to delay syncing data */
290 static int filedelay = 30; /* time to delay syncing files */
291 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
292 "Time to delay syncing files (in seconds)");
293 static int dirdelay = 29; /* time to delay syncing directories */
294 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
295 "Time to delay syncing directories (in seconds)");
296 static int metadelay = 28; /* time to delay syncing metadata */
297 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
298 "Time to delay syncing metadata (in seconds)");
299 static int rushjob; /* number of slots to run ASAP */
300 static int stat_rush_requests; /* number of times I/O speeded up */
301 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
302 "Number of times I/O speeded up (rush requests)");
304 #define VDBATCH_SIZE 8
309 struct vnode *tab[VDBATCH_SIZE];
311 DPCPU_DEFINE_STATIC(struct vdbatch, vd);
313 static void vdbatch_dequeue(struct vnode *vp);
316 * When shutting down the syncer, run it at four times normal speed.
318 #define SYNCER_SHUTDOWN_SPEEDUP 4
319 static int sync_vnode_count;
320 static int syncer_worklist_len;
321 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
324 /* Target for maximum number of vnodes. */
325 u_long desiredvnodes;
326 static u_long gapvnodes; /* gap between wanted and desired */
327 static u_long vhiwat; /* enough extras after expansion */
328 static u_long vlowat; /* minimal extras before expansion */
329 static u_long vstir; /* nonzero to stir non-free vnodes */
330 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */
332 static u_long vnlru_read_freevnodes(void);
335 * Note that no attempt is made to sanitize these parameters.
338 sysctl_maxvnodes(SYSCTL_HANDLER_ARGS)
344 error = sysctl_handle_long(oidp, &val, 0, req);
345 if (error != 0 || req->newptr == NULL)
348 if (val == desiredvnodes)
350 mtx_lock(&vnode_list_mtx);
352 wantfreevnodes = desiredvnodes / 4;
354 mtx_unlock(&vnode_list_mtx);
356 * XXX There is no protection against multiple threads changing
357 * desiredvnodes at the same time. Locking above only helps vnlru and
360 vfs_hash_changesize(desiredvnodes);
361 cache_changesize(desiredvnodes);
365 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
366 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_maxvnodes,
367 "LU", "Target for maximum number of vnodes");
370 sysctl_wantfreevnodes(SYSCTL_HANDLER_ARGS)
375 val = wantfreevnodes;
376 error = sysctl_handle_long(oidp, &val, 0, req);
377 if (error != 0 || req->newptr == NULL)
380 if (val == wantfreevnodes)
382 mtx_lock(&vnode_list_mtx);
383 wantfreevnodes = val;
385 mtx_unlock(&vnode_list_mtx);
389 SYSCTL_PROC(_vfs, OID_AUTO, wantfreevnodes,
390 CTLTYPE_ULONG | CTLFLAG_MPSAFE | CTLFLAG_RW, NULL, 0, sysctl_wantfreevnodes,
391 "LU", "Target for minimum number of \"free\" vnodes");
393 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
394 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)");
395 static int vnlru_nowhere;
396 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
397 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
400 sysctl_try_reclaim_vnode(SYSCTL_HANDLER_ARGS)
405 unsigned long ndflags;
408 if (req->newptr == NULL)
410 if (req->newlen >= PATH_MAX)
413 buf = malloc(PATH_MAX, M_TEMP, M_WAITOK);
414 error = SYSCTL_IN(req, buf, req->newlen);
418 buf[req->newlen] = '\0';
420 ndflags = LOCKLEAF | NOFOLLOW | AUDITVNODE1 | NOCACHE | SAVENAME;
421 NDINIT(&nd, LOOKUP, ndflags, UIO_SYSSPACE, buf, curthread);
422 if ((error = namei(&nd)) != 0)
426 if (VN_IS_DOOMED(vp)) {
428 * This vnode is being recycled. Return != 0 to let the caller
429 * know that the sysctl had no effect. Return EAGAIN because a
430 * subsequent call will likely succeed (since namei will create
431 * a new vnode if necessary)
437 counter_u64_add(recycles_count, 1);
447 sysctl_ftry_reclaim_vnode(SYSCTL_HANDLER_ARGS)
449 struct thread *td = curthread;
455 if (req->newptr == NULL)
458 error = sysctl_handle_int(oidp, &fd, 0, req);
461 error = getvnode(curthread, fd, &cap_fcntl_rights, &fp);
466 error = vn_lock(vp, LK_EXCLUSIVE);
470 counter_u64_add(recycles_count, 1);
478 SYSCTL_PROC(_debug, OID_AUTO, try_reclaim_vnode,
479 CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
480 sysctl_try_reclaim_vnode, "A", "Try to reclaim a vnode by its pathname");
481 SYSCTL_PROC(_debug, OID_AUTO, ftry_reclaim_vnode,
482 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
483 sysctl_ftry_reclaim_vnode, "I",
484 "Try to reclaim a vnode by its file descriptor");
486 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
490 * Support for the bufobj clean & dirty pctrie.
493 buf_trie_alloc(struct pctrie *ptree)
495 return (uma_zalloc_smr(buf_trie_zone, M_NOWAIT));
499 buf_trie_free(struct pctrie *ptree, void *node)
501 uma_zfree_smr(buf_trie_zone, node);
503 PCTRIE_DEFINE_SMR(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free,
507 * Initialize the vnode management data structures.
509 * Reevaluate the following cap on the number of vnodes after the physical
510 * memory size exceeds 512GB. In the limit, as the physical memory size
511 * grows, the ratio of the memory size in KB to vnodes approaches 64:1.
513 #ifndef MAXVNODES_MAX
514 #define MAXVNODES_MAX (512UL * 1024 * 1024 / 64) /* 8M */
517 static MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
519 static struct vnode *
520 vn_alloc_marker(struct mount *mp)
524 vp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
525 vp->v_type = VMARKER;
532 vn_free_marker(struct vnode *vp)
535 MPASS(vp->v_type == VMARKER);
536 free(vp, M_VNODE_MARKER);
540 * Initialize a vnode as it first enters the zone.
543 vnode_init(void *mem, int size, int flags)
552 vp->v_vnlock = &vp->v_lock;
553 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
555 * By default, don't allow shared locks unless filesystems opt-in.
557 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT,
558 LK_NOSHARE | LK_IS_VNODE);
562 bufobj_init(&vp->v_bufobj, vp);
564 * Initialize namecache.
566 LIST_INIT(&vp->v_cache_src);
567 TAILQ_INIT(&vp->v_cache_dst);
569 * Initialize rangelocks.
571 rangelock_init(&vp->v_rl);
573 vp->v_dbatchcpu = NOCPU;
575 mtx_lock(&vnode_list_mtx);
576 TAILQ_INSERT_BEFORE(vnode_list_free_marker, vp, v_vnodelist);
577 mtx_unlock(&vnode_list_mtx);
582 * Free a vnode when it is cleared from the zone.
585 vnode_fini(void *mem, int size)
592 mtx_lock(&vnode_list_mtx);
593 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist);
594 mtx_unlock(&vnode_list_mtx);
595 rangelock_destroy(&vp->v_rl);
596 lockdestroy(vp->v_vnlock);
597 mtx_destroy(&vp->v_interlock);
599 rw_destroy(BO_LOCKPTR(bo));
603 * Provide the size of NFS nclnode and NFS fh for calculation of the
604 * vnode memory consumption. The size is specified directly to
605 * eliminate dependency on NFS-private header.
607 * Other filesystems may use bigger or smaller (like UFS and ZFS)
608 * private inode data, but the NFS-based estimation is ample enough.
609 * Still, we care about differences in the size between 64- and 32-bit
612 * Namecache structure size is heuristically
613 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1.
616 #define NFS_NCLNODE_SZ (528 + 64)
619 #define NFS_NCLNODE_SZ (360 + 32)
624 vntblinit(void *dummy __unused)
627 int cpu, physvnodes, virtvnodes;
631 * Desiredvnodes is a function of the physical memory size and the
632 * kernel's heap size. Generally speaking, it scales with the
633 * physical memory size. The ratio of desiredvnodes to the physical
634 * memory size is 1:16 until desiredvnodes exceeds 98,304.
636 * marginal ratio of desiredvnodes to the physical memory size is
637 * 1:64. However, desiredvnodes is limited by the kernel's heap
638 * size. The memory required by desiredvnodes vnodes and vm objects
639 * must not exceed 1/10th of the kernel's heap size.
641 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 +
642 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64;
643 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) +
644 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ));
645 desiredvnodes = min(physvnodes, virtvnodes);
646 if (desiredvnodes > MAXVNODES_MAX) {
648 printf("Reducing kern.maxvnodes %lu -> %lu\n",
649 desiredvnodes, MAXVNODES_MAX);
650 desiredvnodes = MAXVNODES_MAX;
652 wantfreevnodes = desiredvnodes / 4;
653 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
654 TAILQ_INIT(&vnode_list);
655 mtx_init(&vnode_list_mtx, "vnode_list", NULL, MTX_DEF);
657 * The lock is taken to appease WITNESS.
659 mtx_lock(&vnode_list_mtx);
661 mtx_unlock(&vnode_list_mtx);
662 vnode_list_free_marker = vn_alloc_marker(NULL);
663 TAILQ_INSERT_HEAD(&vnode_list, vnode_list_free_marker, v_vnodelist);
664 vnode_list_reclaim_marker = vn_alloc_marker(NULL);
665 TAILQ_INSERT_HEAD(&vnode_list, vnode_list_reclaim_marker, v_vnodelist);
666 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
667 vnode_init, vnode_fini, UMA_ALIGN_PTR, UMA_ZONE_SMR);
668 vfs_smr = uma_zone_get_smr(vnode_zone);
669 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
670 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
672 * Preallocate enough nodes to support one-per buf so that
673 * we can not fail an insert. reassignbuf() callers can not
674 * tolerate the insertion failure.
676 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
677 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
678 UMA_ZONE_NOFREE | UMA_ZONE_SMR);
679 buf_trie_smr = uma_zone_get_smr(buf_trie_zone);
680 uma_prealloc(buf_trie_zone, nbuf);
682 vnodes_created = counter_u64_alloc(M_WAITOK);
683 recycles_count = counter_u64_alloc(M_WAITOK);
684 recycles_free_count = counter_u64_alloc(M_WAITOK);
685 deferred_inact = counter_u64_alloc(M_WAITOK);
688 * Initialize the filesystem syncer.
690 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
692 syncer_maxdelay = syncer_mask + 1;
693 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
694 cv_init(&sync_wakeup, "syncer");
695 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
700 vd = DPCPU_ID_PTR((cpu), vd);
701 bzero(vd, sizeof(*vd));
702 mtx_init(&vd->lock, "vdbatch", NULL, MTX_DEF);
705 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
708 * Mark a mount point as busy. Used to synchronize access and to delay
709 * unmounting. Eventually, mountlist_mtx is not released on failure.
711 * vfs_busy() is a custom lock, it can block the caller.
712 * vfs_busy() only sleeps if the unmount is active on the mount point.
713 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
714 * vnode belonging to mp.
716 * Lookup uses vfs_busy() to traverse mount points.
718 * / vnode lock A / vnode lock (/var) D
719 * /var vnode lock B /log vnode lock(/var/log) E
720 * vfs_busy lock C vfs_busy lock F
722 * Within each file system, the lock order is C->A->B and F->D->E.
724 * When traversing across mounts, the system follows that lock order:
730 * The lookup() process for namei("/var") illustrates the process:
731 * VOP_LOOKUP() obtains B while A is held
732 * vfs_busy() obtains a shared lock on F while A and B are held
733 * vput() releases lock on B
734 * vput() releases lock on A
735 * VFS_ROOT() obtains lock on D while shared lock on F is held
736 * vfs_unbusy() releases shared lock on F
737 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
738 * Attempt to lock A (instead of vp_crossmp) while D is held would
739 * violate the global order, causing deadlocks.
741 * dounmount() locks B while F is drained.
744 vfs_busy(struct mount *mp, int flags)
747 MPASS((flags & ~MBF_MASK) == 0);
748 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
750 if (vfs_op_thread_enter(mp)) {
751 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
752 MPASS((mp->mnt_kern_flag & MNTK_UNMOUNT) == 0);
753 MPASS((mp->mnt_kern_flag & MNTK_REFEXPIRE) == 0);
754 vfs_mp_count_add_pcpu(mp, ref, 1);
755 vfs_mp_count_add_pcpu(mp, lockref, 1);
756 vfs_op_thread_exit(mp);
757 if (flags & MBF_MNTLSTLOCK)
758 mtx_unlock(&mountlist_mtx);
763 vfs_assert_mount_counters(mp);
766 * If mount point is currently being unmounted, sleep until the
767 * mount point fate is decided. If thread doing the unmounting fails,
768 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
769 * that this mount point has survived the unmount attempt and vfs_busy
770 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
771 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
772 * about to be really destroyed. vfs_busy needs to release its
773 * reference on the mount point in this case and return with ENOENT,
774 * telling the caller that mount mount it tried to busy is no longer
777 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
778 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
781 CTR1(KTR_VFS, "%s: failed busying before sleeping",
785 if (flags & MBF_MNTLSTLOCK)
786 mtx_unlock(&mountlist_mtx);
787 mp->mnt_kern_flag |= MNTK_MWAIT;
788 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
789 if (flags & MBF_MNTLSTLOCK)
790 mtx_lock(&mountlist_mtx);
793 if (flags & MBF_MNTLSTLOCK)
794 mtx_unlock(&mountlist_mtx);
801 * Free a busy filesystem.
804 vfs_unbusy(struct mount *mp)
808 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
810 if (vfs_op_thread_enter(mp)) {
811 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
812 vfs_mp_count_sub_pcpu(mp, lockref, 1);
813 vfs_mp_count_sub_pcpu(mp, ref, 1);
814 vfs_op_thread_exit(mp);
819 vfs_assert_mount_counters(mp);
821 c = --mp->mnt_lockref;
822 if (mp->mnt_vfs_ops == 0) {
823 MPASS((mp->mnt_kern_flag & MNTK_DRAINING) == 0);
828 vfs_dump_mount_counters(mp);
829 if (c == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
830 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
831 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
832 mp->mnt_kern_flag &= ~MNTK_DRAINING;
833 wakeup(&mp->mnt_lockref);
839 * Lookup a mount point by filesystem identifier.
842 vfs_getvfs(fsid_t *fsid)
846 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
847 mtx_lock(&mountlist_mtx);
848 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
849 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0) {
851 mtx_unlock(&mountlist_mtx);
855 mtx_unlock(&mountlist_mtx);
856 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
857 return ((struct mount *) 0);
861 * Lookup a mount point by filesystem identifier, busying it before
864 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
865 * cache for popular filesystem identifiers. The cache is lockess, using
866 * the fact that struct mount's are never freed. In worst case we may
867 * get pointer to unmounted or even different filesystem, so we have to
868 * check what we got, and go slow way if so.
871 vfs_busyfs(fsid_t *fsid)
873 #define FSID_CACHE_SIZE 256
874 typedef struct mount * volatile vmp_t;
875 static vmp_t cache[FSID_CACHE_SIZE];
880 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
881 hash = fsid->val[0] ^ fsid->val[1];
882 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
884 if (mp == NULL || fsidcmp(&mp->mnt_stat.f_fsid, fsid) != 0)
886 if (vfs_busy(mp, 0) != 0) {
890 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0)
896 mtx_lock(&mountlist_mtx);
897 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
898 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0) {
899 error = vfs_busy(mp, MBF_MNTLSTLOCK);
902 mtx_unlock(&mountlist_mtx);
909 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
910 mtx_unlock(&mountlist_mtx);
911 return ((struct mount *) 0);
915 * Check if a user can access privileged mount options.
918 vfs_suser(struct mount *mp, struct thread *td)
922 if (jailed(td->td_ucred)) {
924 * If the jail of the calling thread lacks permission for
925 * this type of file system, deny immediately.
927 if (!prison_allow(td->td_ucred, mp->mnt_vfc->vfc_prison_flag))
931 * If the file system was mounted outside the jail of the
932 * calling thread, deny immediately.
934 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
939 * If file system supports delegated administration, we don't check
940 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
941 * by the file system itself.
942 * If this is not the user that did original mount, we check for
943 * the PRIV_VFS_MOUNT_OWNER privilege.
945 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
946 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
947 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
954 * Get a new unique fsid. Try to make its val[0] unique, since this value
955 * will be used to create fake device numbers for stat(). Also try (but
956 * not so hard) make its val[0] unique mod 2^16, since some emulators only
957 * support 16-bit device numbers. We end up with unique val[0]'s for the
958 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
960 * Keep in mind that several mounts may be running in parallel. Starting
961 * the search one past where the previous search terminated is both a
962 * micro-optimization and a defense against returning the same fsid to
966 vfs_getnewfsid(struct mount *mp)
968 static uint16_t mntid_base;
973 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
974 mtx_lock(&mntid_mtx);
975 mtype = mp->mnt_vfc->vfc_typenum;
976 tfsid.val[1] = mtype;
977 mtype = (mtype & 0xFF) << 24;
979 tfsid.val[0] = makedev(255,
980 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
982 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
986 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
987 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
988 mtx_unlock(&mntid_mtx);
992 * Knob to control the precision of file timestamps:
994 * 0 = seconds only; nanoseconds zeroed.
995 * 1 = seconds and nanoseconds, accurate within 1/HZ.
996 * 2 = seconds and nanoseconds, truncated to microseconds.
997 * >=3 = seconds and nanoseconds, maximum precision.
999 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
1001 static int timestamp_precision = TSP_USEC;
1002 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
1003 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
1004 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, "
1005 "3+: sec + ns (max. precision))");
1008 * Get a current timestamp.
1011 vfs_timestamp(struct timespec *tsp)
1015 switch (timestamp_precision) {
1017 tsp->tv_sec = time_second;
1025 TIMEVAL_TO_TIMESPEC(&tv, tsp);
1035 * Set vnode attributes to VNOVAL
1038 vattr_null(struct vattr *vap)
1041 vap->va_type = VNON;
1042 vap->va_size = VNOVAL;
1043 vap->va_bytes = VNOVAL;
1044 vap->va_mode = VNOVAL;
1045 vap->va_nlink = VNOVAL;
1046 vap->va_uid = VNOVAL;
1047 vap->va_gid = VNOVAL;
1048 vap->va_fsid = VNOVAL;
1049 vap->va_fileid = VNOVAL;
1050 vap->va_blocksize = VNOVAL;
1051 vap->va_rdev = VNOVAL;
1052 vap->va_atime.tv_sec = VNOVAL;
1053 vap->va_atime.tv_nsec = VNOVAL;
1054 vap->va_mtime.tv_sec = VNOVAL;
1055 vap->va_mtime.tv_nsec = VNOVAL;
1056 vap->va_ctime.tv_sec = VNOVAL;
1057 vap->va_ctime.tv_nsec = VNOVAL;
1058 vap->va_birthtime.tv_sec = VNOVAL;
1059 vap->va_birthtime.tv_nsec = VNOVAL;
1060 vap->va_flags = VNOVAL;
1061 vap->va_gen = VNOVAL;
1062 vap->va_vaflags = 0;
1066 * Try to reduce the total number of vnodes.
1068 * This routine (and its user) are buggy in at least the following ways:
1069 * - all parameters were picked years ago when RAM sizes were significantly
1071 * - it can pick vnodes based on pages used by the vm object, but filesystems
1072 * like ZFS don't use it making the pick broken
1073 * - since ZFS has its own aging policy it gets partially combated by this one
1074 * - a dedicated method should be provided for filesystems to let them decide
1075 * whether the vnode should be recycled
1077 * This routine is called when we have too many vnodes. It attempts
1078 * to free <count> vnodes and will potentially free vnodes that still
1079 * have VM backing store (VM backing store is typically the cause
1080 * of a vnode blowout so we want to do this). Therefore, this operation
1081 * is not considered cheap.
1083 * A number of conditions may prevent a vnode from being reclaimed.
1084 * the buffer cache may have references on the vnode, a directory
1085 * vnode may still have references due to the namei cache representing
1086 * underlying files, or the vnode may be in active use. It is not
1087 * desirable to reuse such vnodes. These conditions may cause the
1088 * number of vnodes to reach some minimum value regardless of what
1089 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
1091 * @param reclaim_nc_src Only reclaim directories with outgoing namecache
1092 * entries if this argument is strue
1093 * @param trigger Only reclaim vnodes with fewer than this many resident
1095 * @param target How many vnodes to reclaim.
1096 * @return The number of vnodes that were reclaimed.
1099 vlrureclaim(bool reclaim_nc_src, int trigger, u_long target)
1101 struct vnode *vp, *mvp;
1103 struct vm_object *object;
1107 mtx_assert(&vnode_list_mtx, MA_OWNED);
1112 mvp = vnode_list_reclaim_marker;
1115 while (done < target) {
1116 vp = TAILQ_NEXT(vp, v_vnodelist);
1117 if (__predict_false(vp == NULL))
1120 if (__predict_false(vp->v_type == VMARKER))
1124 * If it's been deconstructed already, it's still
1125 * referenced, or it exceeds the trigger, skip it.
1126 * Also skip free vnodes. We are trying to make space
1127 * to expand the free list, not reduce it.
1129 if (vp->v_usecount > 0 || vp->v_holdcnt == 0 ||
1130 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)))
1133 if (vp->v_type == VBAD || vp->v_type == VNON)
1136 if (!VI_TRYLOCK(vp))
1139 if (vp->v_usecount > 0 || vp->v_holdcnt == 0 ||
1140 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
1141 VN_IS_DOOMED(vp) || vp->v_type == VNON) {
1146 object = atomic_load_ptr(&vp->v_object);
1147 if (object == NULL || object->resident_page_count > trigger) {
1154 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1155 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist);
1156 mtx_unlock(&vnode_list_mtx);
1158 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1160 goto next_iter_unlocked;
1162 if (VOP_LOCK(vp, LK_EXCLUSIVE|LK_NOWAIT) != 0) {
1164 vn_finished_write(mp);
1165 goto next_iter_unlocked;
1169 if (vp->v_usecount > 0 ||
1170 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
1171 (vp->v_object != NULL &&
1172 vp->v_object->resident_page_count > trigger)) {
1175 vn_finished_write(mp);
1176 goto next_iter_unlocked;
1178 counter_u64_add(recycles_count, 1);
1182 vn_finished_write(mp);
1186 kern_yield(PRI_USER);
1187 mtx_lock(&vnode_list_mtx);
1190 MPASS(vp->v_type != VMARKER);
1191 if (!should_yield())
1193 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1194 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist);
1195 mtx_unlock(&vnode_list_mtx);
1196 kern_yield(PRI_USER);
1197 mtx_lock(&vnode_list_mtx);
1200 if (done == 0 && !retried) {
1201 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1202 TAILQ_INSERT_HEAD(&vnode_list, mvp, v_vnodelist);
1209 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */
1210 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free,
1212 "limit on vnode free requests per call to the vnlru_free routine");
1215 * Attempt to reduce the free list by the requested amount.
1218 vnlru_free_locked(int count, struct vfsops *mnt_op)
1220 struct vnode *vp, *mvp;
1224 mtx_assert(&vnode_list_mtx, MA_OWNED);
1225 if (count > max_vnlru_free)
1226 count = max_vnlru_free;
1228 mvp = vnode_list_free_marker;
1232 vp = TAILQ_NEXT(vp, v_vnodelist);
1233 if (__predict_false(vp == NULL)) {
1234 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1235 TAILQ_INSERT_TAIL(&vnode_list, mvp, v_vnodelist);
1238 if (__predict_false(vp->v_type == VMARKER))
1242 * Don't recycle if our vnode is from different type
1243 * of mount point. Note that mp is type-safe, the
1244 * check does not reach unmapped address even if
1245 * vnode is reclaimed.
1246 * Don't recycle if we can't get the interlock without
1249 if (vp->v_holdcnt > 0 || (mnt_op != NULL && (mp = vp->v_mount) != NULL &&
1250 mp->mnt_op != mnt_op) || !VI_TRYLOCK(vp)) {
1253 TAILQ_REMOVE(&vnode_list, mvp, v_vnodelist);
1254 TAILQ_INSERT_AFTER(&vnode_list, vp, mvp, v_vnodelist);
1255 if (__predict_false(vp->v_type == VBAD || vp->v_type == VNON)) {
1261 mtx_unlock(&vnode_list_mtx);
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
1309 vnlru_read_freevnodes(void)
1315 mtx_assert(&vnode_list_mtx, MA_OWNED);
1316 if (freevnodes > freevnodes_old)
1317 slop = freevnodes - freevnodes_old;
1319 slop = freevnodes_old - freevnodes;
1320 if (slop < VNLRU_FREEVNODES_SLOP)
1321 return (freevnodes >= 0 ? freevnodes : 0);
1322 freevnodes_old = freevnodes;
1324 vd = DPCPU_ID_PTR((cpu), vd);
1325 freevnodes_old += vd->freevnodes;
1327 return (freevnodes_old >= 0 ? freevnodes_old : 0);
1331 vnlru_under(u_long rnumvnodes, u_long limit)
1333 u_long rfreevnodes, space;
1335 if (__predict_false(rnumvnodes > desiredvnodes))
1338 space = desiredvnodes - rnumvnodes;
1339 if (space < limit) {
1340 rfreevnodes = vnlru_read_freevnodes();
1341 if (rfreevnodes > wantfreevnodes)
1342 space += rfreevnodes - wantfreevnodes;
1344 return (space < limit);
1348 vnlru_under_unlocked(u_long rnumvnodes, u_long limit)
1350 long rfreevnodes, space;
1352 if (__predict_false(rnumvnodes > desiredvnodes))
1355 space = desiredvnodes - rnumvnodes;
1356 if (space < limit) {
1357 rfreevnodes = atomic_load_long(&freevnodes);
1358 if (rfreevnodes > wantfreevnodes)
1359 space += rfreevnodes - wantfreevnodes;
1361 return (space < limit);
1368 mtx_assert(&vnode_list_mtx, MA_OWNED);
1369 if (vnlruproc_sig == 0) {
1378 u_long rnumvnodes, rfreevnodes, target;
1379 unsigned long onumvnodes;
1380 int done, force, trigger, usevnodes;
1381 bool reclaim_nc_src, want_reread;
1383 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc,
1384 SHUTDOWN_PRI_FIRST);
1387 want_reread = false;
1389 kproc_suspend_check(vnlruproc);
1390 mtx_lock(&vnode_list_mtx);
1391 rnumvnodes = atomic_load_long(&numvnodes);
1394 force = vnlru_under(numvnodes, vhiwat) ? 1 : 0;
1395 want_reread = false;
1399 * If numvnodes is too large (due to desiredvnodes being
1400 * adjusted using its sysctl, or emergency growth), first
1401 * try to reduce it by discarding from the free list.
1403 if (rnumvnodes > desiredvnodes) {
1404 vnlru_free_locked(rnumvnodes - desiredvnodes, NULL);
1405 rnumvnodes = atomic_load_long(&numvnodes);
1408 * Sleep if the vnode cache is in a good state. This is
1409 * when it is not over-full and has space for about a 4%
1410 * or 9% expansion (by growing its size or inexcessively
1411 * reducing its free list). Otherwise, try to reclaim
1412 * space for a 10% expansion.
1414 if (vstir && force == 0) {
1418 if (force == 0 && !vnlru_under(rnumvnodes, vlowat)) {
1420 wakeup(&vnlruproc_sig);
1421 msleep(vnlruproc, &vnode_list_mtx,
1422 PVFS|PDROP, "vlruwt", hz);
1425 rfreevnodes = vnlru_read_freevnodes();
1427 onumvnodes = rnumvnodes;
1429 * Calculate parameters for recycling. These are the same
1430 * throughout the loop to give some semblance of fairness.
1431 * The trigger point is to avoid recycling vnodes with lots
1432 * of resident pages. We aren't trying to free memory; we
1433 * are trying to recycle or at least free vnodes.
1435 if (rnumvnodes <= desiredvnodes)
1436 usevnodes = rnumvnodes - rfreevnodes;
1438 usevnodes = rnumvnodes;
1442 * The trigger value is is chosen to give a conservatively
1443 * large value to ensure that it alone doesn't prevent
1444 * making progress. The value can easily be so large that
1445 * it is effectively infinite in some congested and
1446 * misconfigured cases, and this is necessary. Normally
1447 * it is about 8 to 100 (pages), which is quite large.
1449 trigger = vm_cnt.v_page_count * 2 / usevnodes;
1451 trigger = vsmalltrigger;
1452 reclaim_nc_src = force >= 3;
1453 target = rnumvnodes * (int64_t)gapvnodes / imax(desiredvnodes, 1);
1454 target = target / 10 + 1;
1455 done = vlrureclaim(reclaim_nc_src, trigger, target);
1456 mtx_unlock(&vnode_list_mtx);
1457 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes)
1458 uma_reclaim(UMA_RECLAIM_DRAIN);
1460 if (force == 0 || force == 1) {
1471 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
1474 kern_yield(PRI_USER);
1479 static struct kproc_desc vnlru_kp = {
1484 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
1488 * Routines having to do with the management of the vnode table.
1492 * Try to recycle a freed vnode. We abort if anyone picks up a reference
1493 * before we actually vgone(). This function must be called with the vnode
1494 * held to prevent the vnode from being returned to the free list midway
1498 vtryrecycle(struct vnode *vp)
1502 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
1503 VNASSERT(vp->v_holdcnt, vp,
1504 ("vtryrecycle: Recycling vp %p without a reference.", vp));
1506 * This vnode may found and locked via some other list, if so we
1507 * can't recycle it yet.
1509 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1511 "%s: impossible to recycle, vp %p lock is already held",
1513 return (EWOULDBLOCK);
1516 * Don't recycle if its filesystem is being suspended.
1518 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1521 "%s: impossible to recycle, cannot start the write for %p",
1526 * If we got this far, we need to acquire the interlock and see if
1527 * anyone picked up this vnode from another list. If not, we will
1528 * mark it with DOOMED via vgonel() so that anyone who does find it
1529 * will skip over it.
1532 if (vp->v_usecount) {
1535 vn_finished_write(vnmp);
1537 "%s: impossible to recycle, %p is already referenced",
1541 if (!VN_IS_DOOMED(vp)) {
1542 counter_u64_add(recycles_free_count, 1);
1547 vn_finished_write(vnmp);
1552 * Allocate a new vnode.
1554 * The operation never returns an error. Returning an error was disabled
1555 * in r145385 (dated 2005) with the following comment:
1557 * XXX Not all VFS_VGET/ffs_vget callers check returns.
1559 * Given the age of this commit (almost 15 years at the time of writing this
1560 * comment) restoring the ability to fail requires a significant audit of
1563 * The routine can try to free a vnode or stall for up to 1 second waiting for
1564 * vnlru to clear things up, but ultimately always performs a M_WAITOK allocation.
1566 static u_long vn_alloc_cyclecount;
1568 static struct vnode * __noinline
1569 vn_alloc_hard(struct mount *mp)
1571 u_long rnumvnodes, rfreevnodes;
1573 mtx_lock(&vnode_list_mtx);
1574 rnumvnodes = atomic_load_long(&numvnodes);
1575 if (rnumvnodes + 1 < desiredvnodes) {
1576 vn_alloc_cyclecount = 0;
1579 rfreevnodes = vnlru_read_freevnodes();
1580 if (vn_alloc_cyclecount++ >= rfreevnodes) {
1581 vn_alloc_cyclecount = 0;
1585 * Grow the vnode cache if it will not be above its target max
1586 * after growing. Otherwise, if the free list is nonempty, try
1587 * to reclaim 1 item from it before growing the cache (possibly
1588 * above its target max if the reclamation failed or is delayed).
1589 * Otherwise, wait for some space. In all cases, schedule
1590 * vnlru_proc() if we are getting short of space. The watermarks
1591 * should be chosen so that we never wait or even reclaim from
1592 * the free list to below its target minimum.
1594 if (vnlru_free_locked(1, NULL) > 0)
1596 if (mp == NULL || (mp->mnt_kern_flag & MNTK_SUSPEND) == 0) {
1598 * Wait for space for a new vnode.
1601 msleep(&vnlruproc_sig, &vnode_list_mtx, PVFS, "vlruwk", hz);
1602 if (atomic_load_long(&numvnodes) + 1 > desiredvnodes &&
1603 vnlru_read_freevnodes() > 1)
1604 vnlru_free_locked(1, NULL);
1607 rnumvnodes = atomic_fetchadd_long(&numvnodes, 1) + 1;
1608 if (vnlru_under(rnumvnodes, vlowat))
1610 mtx_unlock(&vnode_list_mtx);
1611 return (uma_zalloc_smr(vnode_zone, M_WAITOK));
1614 static struct vnode *
1615 vn_alloc(struct mount *mp)
1619 if (__predict_false(vn_alloc_cyclecount != 0))
1620 return (vn_alloc_hard(mp));
1621 rnumvnodes = atomic_fetchadd_long(&numvnodes, 1) + 1;
1622 if (__predict_false(vnlru_under_unlocked(rnumvnodes, vlowat))) {
1623 atomic_subtract_long(&numvnodes, 1);
1624 return (vn_alloc_hard(mp));
1627 return (uma_zalloc_smr(vnode_zone, M_WAITOK));
1631 vn_free(struct vnode *vp)
1634 atomic_subtract_long(&numvnodes, 1);
1635 uma_zfree_smr(vnode_zone, vp);
1639 * Return the next vnode from the free list.
1642 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1647 struct lock_object *lo;
1649 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1651 KASSERT(vops->registered,
1652 ("%s: not registered vector op %p\n", __func__, vops));
1655 if (td->td_vp_reserved != NULL) {
1656 vp = td->td_vp_reserved;
1657 td->td_vp_reserved = NULL;
1661 counter_u64_add(vnodes_created, 1);
1663 * Locks are given the generic name "vnode" when created.
1664 * Follow the historic practice of using the filesystem
1665 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc.
1667 * Locks live in a witness group keyed on their name. Thus,
1668 * when a lock is renamed, it must also move from the witness
1669 * group of its old name to the witness group of its new name.
1671 * The change only needs to be made when the vnode moves
1672 * from one filesystem type to another. We ensure that each
1673 * filesystem use a single static name pointer for its tag so
1674 * that we can compare pointers rather than doing a strcmp().
1676 lo = &vp->v_vnlock->lock_object;
1678 if (lo->lo_name != tag) {
1682 WITNESS_DESTROY(lo);
1683 WITNESS_INIT(lo, tag);
1687 * By default, don't allow shared locks unless filesystems opt-in.
1689 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE;
1691 * Finalize various vnode identity bits.
1693 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp));
1694 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp));
1695 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp));
1698 v_init_counters(vp);
1699 vp->v_bufobj.bo_ops = &buf_ops_bio;
1701 if (mp == NULL && vops != &dead_vnodeops)
1702 printf("NULL mp in getnewvnode(9), tag %s\n", tag);
1706 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1707 mac_vnode_associate_singlelabel(mp, vp);
1710 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize;
1711 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1712 vp->v_vflag |= VV_NOKNOTE;
1716 * For the filesystems which do not use vfs_hash_insert(),
1717 * still initialize v_hash to have vfs_hash_index() useful.
1718 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1721 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1728 getnewvnode_reserve(void)
1733 MPASS(td->td_vp_reserved == NULL);
1734 td->td_vp_reserved = vn_alloc(NULL);
1738 getnewvnode_drop_reserve(void)
1743 if (td->td_vp_reserved != NULL) {
1744 vn_free(td->td_vp_reserved);
1745 td->td_vp_reserved = NULL;
1750 freevnode(struct vnode *vp)
1755 * The vnode has been marked for destruction, so free it.
1757 * The vnode will be returned to the zone where it will
1758 * normally remain until it is needed for another vnode. We
1759 * need to cleanup (or verify that the cleanup has already
1760 * been done) any residual data left from its current use
1761 * so as not to contaminate the freshly allocated vnode.
1763 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
1765 * Paired with vgone.
1767 vn_seqc_write_end_locked(vp);
1768 VNPASS(vp->v_seqc_users == 0, vp);
1771 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
1772 VNPASS(vp->v_holdcnt == VHOLD_NO_SMR, vp);
1773 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
1774 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
1775 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
1776 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
1777 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
1778 ("clean blk trie not empty"));
1779 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
1780 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
1781 ("dirty blk trie not empty"));
1782 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
1783 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
1784 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
1785 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp,
1786 ("Dangling rangelock waiters"));
1789 mac_vnode_destroy(vp);
1791 if (vp->v_pollinfo != NULL) {
1792 destroy_vpollinfo(vp->v_pollinfo);
1793 vp->v_pollinfo = NULL;
1796 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
1799 vp->v_mountedhere = NULL;
1802 vp->v_fifoinfo = NULL;
1803 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
1812 * Delete from old mount point vnode list, if on one.
1815 delmntque(struct vnode *vp)
1819 VNPASS((vp->v_mflag & VMP_LAZYLIST) == 0, vp);
1828 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1829 ("bad mount point vnode list size"));
1830 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1831 mp->mnt_nvnodelistsize--;
1837 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1841 vp->v_op = &dead_vnodeops;
1847 * Insert into list of vnodes for the new mount point, if available.
1850 insmntque1(struct vnode *vp, struct mount *mp,
1851 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1854 KASSERT(vp->v_mount == NULL,
1855 ("insmntque: vnode already on per mount vnode list"));
1856 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1857 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1860 * We acquire the vnode interlock early to ensure that the
1861 * vnode cannot be recycled by another process releasing a
1862 * holdcnt on it before we get it on both the vnode list
1863 * and the active vnode list. The mount mutex protects only
1864 * manipulation of the vnode list and the vnode freelist
1865 * mutex protects only manipulation of the active vnode list.
1866 * Hence the need to hold the vnode interlock throughout.
1870 if (((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 &&
1871 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1872 mp->mnt_nvnodelistsize == 0)) &&
1873 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1882 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1883 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1884 ("neg mount point vnode list size"));
1885 mp->mnt_nvnodelistsize++;
1892 insmntque(struct vnode *vp, struct mount *mp)
1895 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1899 * Flush out and invalidate all buffers associated with a bufobj
1900 * Called with the underlying object locked.
1903 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1908 if (flags & V_SAVE) {
1909 error = bufobj_wwait(bo, slpflag, slptimeo);
1914 if (bo->bo_dirty.bv_cnt > 0) {
1916 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1919 * XXX We could save a lock/unlock if this was only
1920 * enabled under INVARIANTS
1923 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1924 panic("vinvalbuf: dirty bufs");
1928 * If you alter this loop please notice that interlock is dropped and
1929 * reacquired in flushbuflist. Special care is needed to ensure that
1930 * no race conditions occur from this.
1933 error = flushbuflist(&bo->bo_clean,
1934 flags, bo, slpflag, slptimeo);
1935 if (error == 0 && !(flags & V_CLEANONLY))
1936 error = flushbuflist(&bo->bo_dirty,
1937 flags, bo, slpflag, slptimeo);
1938 if (error != 0 && error != EAGAIN) {
1942 } while (error != 0);
1945 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1946 * have write I/O in-progress but if there is a VM object then the
1947 * VM object can also have read-I/O in-progress.
1950 bufobj_wwait(bo, 0, 0);
1951 if ((flags & V_VMIO) == 0 && bo->bo_object != NULL) {
1953 vm_object_pip_wait_unlocked(bo->bo_object, "bovlbx");
1956 } while (bo->bo_numoutput > 0);
1960 * Destroy the copy in the VM cache, too.
1962 if (bo->bo_object != NULL &&
1963 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) {
1964 VM_OBJECT_WLOCK(bo->bo_object);
1965 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1966 OBJPR_CLEANONLY : 0);
1967 VM_OBJECT_WUNLOCK(bo->bo_object);
1972 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO |
1973 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 ||
1974 bo->bo_clean.bv_cnt > 0))
1975 panic("vinvalbuf: flush failed");
1976 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 &&
1977 bo->bo_dirty.bv_cnt > 0)
1978 panic("vinvalbuf: flush dirty failed");
1985 * Flush out and invalidate all buffers associated with a vnode.
1986 * Called with the underlying object locked.
1989 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1992 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1993 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1994 if (vp->v_object != NULL && vp->v_object->handle != vp)
1996 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
2000 * Flush out buffers on the specified list.
2004 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
2007 struct buf *bp, *nbp;
2012 ASSERT_BO_WLOCKED(bo);
2015 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
2017 * If we are flushing both V_NORMAL and V_ALT buffers then
2018 * do not skip any buffers. If we are flushing only V_NORMAL
2019 * buffers then skip buffers marked as BX_ALTDATA. If we are
2020 * flushing only V_ALT buffers then skip buffers not marked
2023 if (((flags & (V_NORMAL | V_ALT)) != (V_NORMAL | V_ALT)) &&
2024 (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA) != 0) ||
2025 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0))) {
2029 lblkno = nbp->b_lblkno;
2030 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
2033 error = BUF_TIMELOCK(bp,
2034 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
2035 "flushbuf", slpflag, slptimeo);
2038 return (error != ENOLCK ? error : EAGAIN);
2040 KASSERT(bp->b_bufobj == bo,
2041 ("bp %p wrong b_bufobj %p should be %p",
2042 bp, bp->b_bufobj, bo));
2044 * XXX Since there are no node locks for NFS, I
2045 * believe there is a slight chance that a delayed
2046 * write will occur while sleeping just above, so
2049 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
2052 bp->b_flags |= B_ASYNC;
2055 return (EAGAIN); /* XXX: why not loop ? */
2058 bp->b_flags |= (B_INVAL | B_RELBUF);
2059 bp->b_flags &= ~B_ASYNC;
2064 nbp = gbincore(bo, lblkno);
2065 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2067 break; /* nbp invalid */
2073 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn)
2079 ASSERT_BO_LOCKED(bo);
2081 for (lblkno = startn;;) {
2083 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno);
2084 if (bp == NULL || bp->b_lblkno >= endn ||
2085 bp->b_lblkno < startn)
2087 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
2088 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0);
2091 if (error == ENOLCK)
2095 KASSERT(bp->b_bufobj == bo,
2096 ("bp %p wrong b_bufobj %p should be %p",
2097 bp, bp->b_bufobj, bo));
2098 lblkno = bp->b_lblkno + 1;
2099 if ((bp->b_flags & B_MANAGED) == 0)
2101 bp->b_flags |= B_RELBUF;
2103 * In the VMIO case, use the B_NOREUSE flag to hint that the
2104 * pages backing each buffer in the range are unlikely to be
2105 * reused. Dirty buffers will have the hint applied once
2106 * they've been written.
2108 if ((bp->b_flags & B_VMIO) != 0)
2109 bp->b_flags |= B_NOREUSE;
2117 * Truncate a file's buffer and pages to a specified length. This
2118 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
2122 vtruncbuf(struct vnode *vp, off_t length, int blksize)
2124 struct buf *bp, *nbp;
2128 CTR4(KTR_VFS, "%s: vp %p with block %d:%ju", __func__,
2129 vp, blksize, (uintmax_t)length);
2132 * Round up to the *next* lbn.
2134 startlbn = howmany(length, blksize);
2136 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
2142 while (v_inval_buf_range_locked(vp, bo, startlbn, INT64_MAX) == EAGAIN)
2147 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2148 if (bp->b_lblkno > 0)
2151 * Since we hold the vnode lock this should only
2152 * fail if we're racing with the buf daemon.
2155 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2156 BO_LOCKPTR(bo)) == ENOLCK)
2157 goto restart_unlocked;
2159 VNASSERT((bp->b_flags & B_DELWRI), vp,
2160 ("buf(%p) on dirty queue without DELWRI", bp));
2169 bufobj_wwait(bo, 0, 0);
2171 vnode_pager_setsize(vp, length);
2177 * Invalidate the cached pages of a file's buffer within the range of block
2178 * numbers [startlbn, endlbn).
2181 v_inval_buf_range(struct vnode *vp, daddr_t startlbn, daddr_t endlbn,
2187 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range");
2189 start = blksize * startlbn;
2190 end = blksize * endlbn;
2194 MPASS(blksize == bo->bo_bsize);
2196 while (v_inval_buf_range_locked(vp, bo, startlbn, endlbn) == EAGAIN)
2200 vn_pages_remove(vp, OFF_TO_IDX(start), OFF_TO_IDX(end + PAGE_SIZE - 1));
2204 v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
2205 daddr_t startlbn, daddr_t endlbn)
2207 struct buf *bp, *nbp;
2210 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range_locked");
2211 ASSERT_BO_LOCKED(bo);
2215 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
2216 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2219 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2220 BO_LOCKPTR(bo)) == ENOLCK) {
2226 bp->b_flags |= B_INVAL | B_RELBUF;
2227 bp->b_flags &= ~B_ASYNC;
2233 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
2235 (nbp->b_flags & B_DELWRI) != 0))
2239 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2240 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2243 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2244 BO_LOCKPTR(bo)) == ENOLCK) {
2249 bp->b_flags |= B_INVAL | B_RELBUF;
2250 bp->b_flags &= ~B_ASYNC;
2256 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
2257 (nbp->b_vp != vp) ||
2258 (nbp->b_flags & B_DELWRI) == 0))
2266 buf_vlist_remove(struct buf *bp)
2270 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2271 ASSERT_BO_WLOCKED(bp->b_bufobj);
2272 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
2273 (BX_VNDIRTY|BX_VNCLEAN),
2274 ("buf_vlist_remove: Buf %p is on two lists", bp));
2275 if (bp->b_xflags & BX_VNDIRTY)
2276 bv = &bp->b_bufobj->bo_dirty;
2278 bv = &bp->b_bufobj->bo_clean;
2279 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
2280 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
2282 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
2286 * Add the buffer to the sorted clean or dirty block list.
2288 * NOTE: xflags is passed as a constant, optimizing this inline function!
2291 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
2297 ASSERT_BO_WLOCKED(bo);
2298 KASSERT((bo->bo_flag & BO_NOBUFS) == 0,
2299 ("buf_vlist_add: bo %p does not allow bufs", bo));
2300 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
2301 ("dead bo %p", bo));
2302 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
2303 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
2304 bp->b_xflags |= xflags;
2305 if (xflags & BX_VNDIRTY)
2311 * Keep the list ordered. Optimize empty list insertion. Assume
2312 * we tend to grow at the tail so lookup_le should usually be cheaper
2315 if (bv->bv_cnt == 0 ||
2316 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
2317 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
2318 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
2319 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
2321 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
2322 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
2324 panic("buf_vlist_add: Preallocated nodes insufficient.");
2329 * Look up a buffer using the buffer tries.
2332 gbincore(struct bufobj *bo, daddr_t lblkno)
2336 ASSERT_BO_LOCKED(bo);
2337 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
2340 return (BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno));
2344 * Look up a buf using the buffer tries, without the bufobj lock. This relies
2345 * on SMR for safe lookup, and bufs being in a no-free zone to provide type
2346 * stability of the result. Like other lockless lookups, the found buf may
2347 * already be invalid by the time this function returns.
2350 gbincore_unlocked(struct bufobj *bo, daddr_t lblkno)
2354 ASSERT_BO_UNLOCKED(bo);
2355 bp = BUF_PCTRIE_LOOKUP_UNLOCKED(&bo->bo_clean.bv_root, lblkno);
2358 return (BUF_PCTRIE_LOOKUP_UNLOCKED(&bo->bo_dirty.bv_root, lblkno));
2362 * Associate a buffer with a vnode.
2365 bgetvp(struct vnode *vp, struct buf *bp)
2370 ASSERT_BO_WLOCKED(bo);
2371 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
2373 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
2374 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
2375 ("bgetvp: bp already attached! %p", bp));
2381 * Insert onto list for new vnode.
2383 buf_vlist_add(bp, bo, BX_VNCLEAN);
2387 * Disassociate a buffer from a vnode.
2390 brelvp(struct buf *bp)
2395 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2396 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
2399 * Delete from old vnode list, if on one.
2401 vp = bp->b_vp; /* XXX */
2404 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2405 buf_vlist_remove(bp);
2407 panic("brelvp: Buffer %p not on queue.", bp);
2408 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2409 bo->bo_flag &= ~BO_ONWORKLST;
2410 mtx_lock(&sync_mtx);
2411 LIST_REMOVE(bo, bo_synclist);
2412 syncer_worklist_len--;
2413 mtx_unlock(&sync_mtx);
2416 bp->b_bufobj = NULL;
2422 * Add an item to the syncer work queue.
2425 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
2429 ASSERT_BO_WLOCKED(bo);
2431 mtx_lock(&sync_mtx);
2432 if (bo->bo_flag & BO_ONWORKLST)
2433 LIST_REMOVE(bo, bo_synclist);
2435 bo->bo_flag |= BO_ONWORKLST;
2436 syncer_worklist_len++;
2439 if (delay > syncer_maxdelay - 2)
2440 delay = syncer_maxdelay - 2;
2441 slot = (syncer_delayno + delay) & syncer_mask;
2443 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
2444 mtx_unlock(&sync_mtx);
2448 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
2452 mtx_lock(&sync_mtx);
2453 len = syncer_worklist_len - sync_vnode_count;
2454 mtx_unlock(&sync_mtx);
2455 error = SYSCTL_OUT(req, &len, sizeof(len));
2459 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len,
2460 CTLTYPE_INT | CTLFLAG_MPSAFE| CTLFLAG_RD, NULL, 0,
2461 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
2463 static struct proc *updateproc;
2464 static void sched_sync(void);
2465 static struct kproc_desc up_kp = {
2470 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
2473 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
2478 *bo = LIST_FIRST(slp);
2482 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
2485 * We use vhold in case the vnode does not
2486 * successfully sync. vhold prevents the vnode from
2487 * going away when we unlock the sync_mtx so that
2488 * we can acquire the vnode interlock.
2491 mtx_unlock(&sync_mtx);
2493 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2495 mtx_lock(&sync_mtx);
2496 return (*bo == LIST_FIRST(slp));
2498 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2499 (void) VOP_FSYNC(vp, MNT_LAZY, td);
2501 vn_finished_write(mp);
2503 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
2505 * Put us back on the worklist. The worklist
2506 * routine will remove us from our current
2507 * position and then add us back in at a later
2510 vn_syncer_add_to_worklist(*bo, syncdelay);
2514 mtx_lock(&sync_mtx);
2518 static int first_printf = 1;
2521 * System filesystem synchronizer daemon.
2526 struct synclist *next, *slp;
2529 struct thread *td = curthread;
2531 int net_worklist_len;
2532 int syncer_final_iter;
2536 syncer_final_iter = 0;
2537 syncer_state = SYNCER_RUNNING;
2538 starttime = time_uptime;
2539 td->td_pflags |= TDP_NORUNNINGBUF;
2541 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
2544 mtx_lock(&sync_mtx);
2546 if (syncer_state == SYNCER_FINAL_DELAY &&
2547 syncer_final_iter == 0) {
2548 mtx_unlock(&sync_mtx);
2549 kproc_suspend_check(td->td_proc);
2550 mtx_lock(&sync_mtx);
2552 net_worklist_len = syncer_worklist_len - sync_vnode_count;
2553 if (syncer_state != SYNCER_RUNNING &&
2554 starttime != time_uptime) {
2556 printf("\nSyncing disks, vnodes remaining... ");
2559 printf("%d ", net_worklist_len);
2561 starttime = time_uptime;
2564 * Push files whose dirty time has expired. Be careful
2565 * of interrupt race on slp queue.
2567 * Skip over empty worklist slots when shutting down.
2570 slp = &syncer_workitem_pending[syncer_delayno];
2571 syncer_delayno += 1;
2572 if (syncer_delayno == syncer_maxdelay)
2574 next = &syncer_workitem_pending[syncer_delayno];
2576 * If the worklist has wrapped since the
2577 * it was emptied of all but syncer vnodes,
2578 * switch to the FINAL_DELAY state and run
2579 * for one more second.
2581 if (syncer_state == SYNCER_SHUTTING_DOWN &&
2582 net_worklist_len == 0 &&
2583 last_work_seen == syncer_delayno) {
2584 syncer_state = SYNCER_FINAL_DELAY;
2585 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
2587 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
2588 syncer_worklist_len > 0);
2591 * Keep track of the last time there was anything
2592 * on the worklist other than syncer vnodes.
2593 * Return to the SHUTTING_DOWN state if any
2596 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
2597 last_work_seen = syncer_delayno;
2598 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
2599 syncer_state = SYNCER_SHUTTING_DOWN;
2600 while (!LIST_EMPTY(slp)) {
2601 error = sync_vnode(slp, &bo, td);
2603 LIST_REMOVE(bo, bo_synclist);
2604 LIST_INSERT_HEAD(next, bo, bo_synclist);
2608 if (first_printf == 0) {
2610 * Drop the sync mutex, because some watchdog
2611 * drivers need to sleep while patting
2613 mtx_unlock(&sync_mtx);
2614 wdog_kern_pat(WD_LASTVAL);
2615 mtx_lock(&sync_mtx);
2619 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
2620 syncer_final_iter--;
2622 * The variable rushjob allows the kernel to speed up the
2623 * processing of the filesystem syncer process. A rushjob
2624 * value of N tells the filesystem syncer to process the next
2625 * N seconds worth of work on its queue ASAP. Currently rushjob
2626 * is used by the soft update code to speed up the filesystem
2627 * syncer process when the incore state is getting so far
2628 * ahead of the disk that the kernel memory pool is being
2629 * threatened with exhaustion.
2636 * Just sleep for a short period of time between
2637 * iterations when shutting down to allow some I/O
2640 * If it has taken us less than a second to process the
2641 * current work, then wait. Otherwise start right over
2642 * again. We can still lose time if any single round
2643 * takes more than two seconds, but it does not really
2644 * matter as we are just trying to generally pace the
2645 * filesystem activity.
2647 if (syncer_state != SYNCER_RUNNING ||
2648 time_uptime == starttime) {
2650 sched_prio(td, PPAUSE);
2653 if (syncer_state != SYNCER_RUNNING)
2654 cv_timedwait(&sync_wakeup, &sync_mtx,
2655 hz / SYNCER_SHUTDOWN_SPEEDUP);
2656 else if (time_uptime == starttime)
2657 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2662 * Request the syncer daemon to speed up its work.
2663 * We never push it to speed up more than half of its
2664 * normal turn time, otherwise it could take over the cpu.
2667 speedup_syncer(void)
2671 mtx_lock(&sync_mtx);
2672 if (rushjob < syncdelay / 2) {
2674 stat_rush_requests += 1;
2677 mtx_unlock(&sync_mtx);
2678 cv_broadcast(&sync_wakeup);
2683 * Tell the syncer to speed up its work and run though its work
2684 * list several times, then tell it to shut down.
2687 syncer_shutdown(void *arg, int howto)
2690 if (howto & RB_NOSYNC)
2692 mtx_lock(&sync_mtx);
2693 syncer_state = SYNCER_SHUTTING_DOWN;
2695 mtx_unlock(&sync_mtx);
2696 cv_broadcast(&sync_wakeup);
2697 kproc_shutdown(arg, howto);
2701 syncer_suspend(void)
2704 syncer_shutdown(updateproc, 0);
2711 mtx_lock(&sync_mtx);
2713 syncer_state = SYNCER_RUNNING;
2714 mtx_unlock(&sync_mtx);
2715 cv_broadcast(&sync_wakeup);
2716 kproc_resume(updateproc);
2720 * Reassign a buffer from one vnode to another.
2721 * Used to assign file specific control information
2722 * (indirect blocks) to the vnode to which they belong.
2725 reassignbuf(struct buf *bp)
2738 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2739 bp, bp->b_vp, bp->b_flags);
2741 * B_PAGING flagged buffers cannot be reassigned because their vp
2742 * is not fully linked in.
2744 if (bp->b_flags & B_PAGING)
2745 panic("cannot reassign paging buffer");
2748 * Delete from old vnode list, if on one.
2751 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2752 buf_vlist_remove(bp);
2754 panic("reassignbuf: Buffer %p not on queue.", bp);
2756 * If dirty, put on list of dirty buffers; otherwise insert onto list
2759 if (bp->b_flags & B_DELWRI) {
2760 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2761 switch (vp->v_type) {
2771 vn_syncer_add_to_worklist(bo, delay);
2773 buf_vlist_add(bp, bo, BX_VNDIRTY);
2775 buf_vlist_add(bp, bo, BX_VNCLEAN);
2777 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2778 mtx_lock(&sync_mtx);
2779 LIST_REMOVE(bo, bo_synclist);
2780 syncer_worklist_len--;
2781 mtx_unlock(&sync_mtx);
2782 bo->bo_flag &= ~BO_ONWORKLST;
2787 bp = TAILQ_FIRST(&bv->bv_hd);
2788 KASSERT(bp == NULL || bp->b_bufobj == bo,
2789 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2790 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2791 KASSERT(bp == NULL || bp->b_bufobj == bo,
2792 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2794 bp = TAILQ_FIRST(&bv->bv_hd);
2795 KASSERT(bp == NULL || bp->b_bufobj == bo,
2796 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2797 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2798 KASSERT(bp == NULL || bp->b_bufobj == bo,
2799 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2805 v_init_counters(struct vnode *vp)
2808 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2809 vp, ("%s called for an initialized vnode", __FUNCTION__));
2810 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2812 refcount_init(&vp->v_holdcnt, 1);
2813 refcount_init(&vp->v_usecount, 1);
2817 * Increment si_usecount of the associated device, if any.
2820 v_incr_devcount(struct vnode *vp)
2823 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2824 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2826 vp->v_rdev->si_usecount++;
2832 * Decrement si_usecount of the associated device, if any.
2834 * The caller is required to hold the interlock when transitioning a VCHR use
2835 * count to zero. This prevents a race with devfs_reclaim_vchr() that would
2836 * leak a si_usecount reference. The vnode lock will also prevent this race
2837 * if it is held while dropping the last ref.
2842 * devfs_reclaim_vchr
2843 * make v_usecount == 0
2845 * sees v_usecount == 0, no updates
2846 * vp->v_rdev = NULL;
2851 * sees v_rdev == NULL, no updates
2853 * In this scenario si_devcount decrement is not performed.
2856 v_decr_devcount(struct vnode *vp)
2859 ASSERT_VOP_LOCKED(vp, __func__);
2860 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2861 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2863 VNPASS(vp->v_rdev->si_usecount > 0, vp);
2864 vp->v_rdev->si_usecount--;
2870 * Grab a particular vnode from the free list, increment its
2871 * reference count and lock it. VIRF_DOOMED is set if the vnode
2872 * is being destroyed. Only callers who specify LK_RETRY will
2873 * see doomed vnodes. If inactive processing was delayed in
2874 * vput try to do it here.
2876 * usecount is manipulated using atomics without holding any locks.
2878 * holdcnt can be manipulated using atomics without holding any locks,
2879 * except when transitioning 1<->0, in which case the interlock is held.
2881 * Consumers which don't guarantee liveness of the vnode can use SMR to
2882 * try to get a reference. Note this operation can fail since the vnode
2883 * may be awaiting getting freed by the time they get to it.
2886 vget_prep_smr(struct vnode *vp)
2890 VFS_SMR_ASSERT_ENTERED();
2892 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2904 vget_prep(struct vnode *vp)
2908 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2918 vget(struct vnode *vp, int flags, struct thread *td)
2922 MPASS(td == curthread);
2925 return (vget_finish(vp, flags, vs));
2928 static int __noinline
2929 vget_finish_vchr(struct vnode *vp)
2932 VNASSERT(vp->v_type == VCHR, vp, ("type != VCHR)"));
2935 * See the comment in vget_finish before usecount bump.
2937 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2939 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
2940 VNASSERT(old > 0, vp, ("%s: wrong hold count %d", __func__, old));
2942 refcount_release(&vp->v_holdcnt);
2948 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2950 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
2951 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old));
2953 refcount_release(&vp->v_holdcnt);
2958 v_incr_devcount(vp);
2959 refcount_acquire(&vp->v_usecount);
2965 vget_finish(struct vnode *vp, int flags, enum vgetstate vs)
2969 if ((flags & LK_INTERLOCK) != 0)
2970 ASSERT_VI_LOCKED(vp, __func__);
2972 ASSERT_VI_UNLOCKED(vp, __func__);
2973 VNPASS(vs == VGET_HOLDCNT || vs == VGET_USECOUNT, vp);
2974 VNPASS(vp->v_holdcnt > 0, vp);
2975 VNPASS(vs == VGET_HOLDCNT || vp->v_usecount > 0, vp);
2977 error = vn_lock(vp, flags);
2978 if (__predict_false(error != 0)) {
2979 if (vs == VGET_USECOUNT)
2983 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2988 if (vs == VGET_USECOUNT)
2991 if (__predict_false(vp->v_type == VCHR))
2992 return (vget_finish_vchr(vp));
2995 * We hold the vnode. If the usecount is 0 it will be utilized to keep
2996 * the vnode around. Otherwise someone else lended their hold count and
2997 * we have to drop ours.
2999 old = atomic_fetchadd_int(&vp->v_usecount, 1);
3000 VNASSERT(old >= 0, vp, ("%s: wrong use count %d", __func__, old));
3003 old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
3004 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old));
3006 refcount_release(&vp->v_holdcnt);
3013 * Increase the reference (use) and hold count of a vnode.
3014 * This will also remove the vnode from the free list if it is presently free.
3016 static void __noinline
3017 vref_vchr(struct vnode *vp, bool interlock)
3021 * See the comment in vget_finish before usecount bump.
3024 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
3025 VNODE_REFCOUNT_FENCE_ACQ();
3026 VNASSERT(vp->v_holdcnt > 0, vp,
3027 ("%s: active vnode not held", __func__));
3032 * By the time we get here the vnode might have been doomed, at
3033 * which point the 0->1 use count transition is no longer
3034 * protected by the interlock. Since it can't bounce back to
3035 * VCHR and requires vref semantics, punt it back
3037 if (__predict_false(vp->v_type == VBAD)) {
3043 VNASSERT(vp->v_type == VCHR, vp, ("type != VCHR)"));
3044 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
3045 VNODE_REFCOUNT_FENCE_ACQ();
3046 VNASSERT(vp->v_holdcnt > 0, vp,
3047 ("%s: active vnode not held", __func__));
3053 v_incr_devcount(vp);
3054 refcount_acquire(&vp->v_usecount);
3061 vref(struct vnode *vp)
3065 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3066 if (__predict_false(vp->v_type == VCHR)) {
3067 vref_vchr(vp, false);
3071 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
3072 VNODE_REFCOUNT_FENCE_ACQ();
3073 VNASSERT(vp->v_holdcnt > 0, vp,
3074 ("%s: active vnode not held", __func__));
3079 * See the comment in vget_finish.
3081 old = atomic_fetchadd_int(&vp->v_usecount, 1);
3082 VNASSERT(old >= 0, vp, ("%s: wrong use count %d", __func__, old));
3085 old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
3086 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old));
3088 refcount_release(&vp->v_holdcnt);
3094 vrefl(struct vnode *vp)
3097 ASSERT_VI_LOCKED(vp, __func__);
3098 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3099 if (__predict_false(vp->v_type == VCHR)) {
3100 vref_vchr(vp, true);
3107 vrefact(struct vnode *vp)
3110 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3112 int old = atomic_fetchadd_int(&vp->v_usecount, 1);
3113 VNASSERT(old > 0, vp, ("%s: wrong use count %d", __func__, old));
3115 refcount_acquire(&vp->v_usecount);
3120 vrefactn(struct vnode *vp, u_int n)
3123 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3125 int old = atomic_fetchadd_int(&vp->v_usecount, n);
3126 VNASSERT(old > 0, vp, ("%s: wrong use count %d", __func__, old));
3128 atomic_add_int(&vp->v_usecount, n);
3133 * Return reference count of a vnode.
3135 * The results of this call are only guaranteed when some mechanism is used to
3136 * stop other processes from gaining references to the vnode. This may be the
3137 * case if the caller holds the only reference. This is also useful when stale
3138 * data is acceptable as race conditions may be accounted for by some other
3142 vrefcnt(struct vnode *vp)
3145 return (vp->v_usecount);
3149 vlazy(struct vnode *vp)
3153 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__));
3155 if ((vp->v_mflag & VMP_LAZYLIST) != 0)
3158 * We may get here for inactive routines after the vnode got doomed.
3160 if (VN_IS_DOOMED(vp))
3163 mtx_lock(&mp->mnt_listmtx);
3164 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
3165 vp->v_mflag |= VMP_LAZYLIST;
3166 TAILQ_INSERT_TAIL(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3167 mp->mnt_lazyvnodelistsize++;
3169 mtx_unlock(&mp->mnt_listmtx);
3173 * This routine is only meant to be called from vgonel prior to dooming
3177 vunlazy_gone(struct vnode *vp)
3181 ASSERT_VOP_ELOCKED(vp, __func__);
3182 ASSERT_VI_LOCKED(vp, __func__);
3183 VNPASS(!VN_IS_DOOMED(vp), vp);
3185 if (vp->v_mflag & VMP_LAZYLIST) {
3187 mtx_lock(&mp->mnt_listmtx);
3188 VNPASS(vp->v_mflag & VMP_LAZYLIST, vp);
3189 vp->v_mflag &= ~VMP_LAZYLIST;
3190 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3191 mp->mnt_lazyvnodelistsize--;
3192 mtx_unlock(&mp->mnt_listmtx);
3197 vdefer_inactive(struct vnode *vp)
3200 ASSERT_VI_LOCKED(vp, __func__);
3201 VNASSERT(vp->v_holdcnt > 0, vp,
3202 ("%s: vnode without hold count", __func__));
3203 if (VN_IS_DOOMED(vp)) {
3207 if (vp->v_iflag & VI_DEFINACT) {
3208 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count"));
3212 if (vp->v_usecount > 0) {
3213 vp->v_iflag &= ~VI_OWEINACT;
3218 vp->v_iflag |= VI_DEFINACT;
3220 counter_u64_add(deferred_inact, 1);
3224 vdefer_inactive_unlocked(struct vnode *vp)
3228 if ((vp->v_iflag & VI_OWEINACT) == 0) {
3232 vdefer_inactive(vp);
3235 enum vput_op { VRELE, VPUT, VUNREF };
3238 * Handle ->v_usecount transitioning to 0.
3240 * By releasing the last usecount we take ownership of the hold count which
3241 * provides liveness of the vnode, meaning we have to vdrop.
3243 * If the vnode is of type VCHR we may need to decrement si_usecount, see
3244 * v_decr_devcount for details.
3246 * For all vnodes we may need to perform inactive processing. It requires an
3247 * exclusive lock on the vnode, while it is legal to call here with only a
3248 * shared lock (or no locks). If locking the vnode in an expected manner fails,
3249 * inactive processing gets deferred to the syncer.
3251 * XXX Some filesystems pass in an exclusively locked vnode and strongly depend
3252 * on the lock being held all the way until VOP_INACTIVE. This in particular
3253 * happens with UFS which adds half-constructed vnodes to the hash, where they
3254 * can be found by other code.
3257 vput_final(struct vnode *vp, enum vput_op func)
3262 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3263 VNPASS(vp->v_holdcnt > 0, vp);
3266 if (__predict_false(vp->v_type == VCHR && func != VRELE))
3267 v_decr_devcount(vp);
3270 * By the time we got here someone else might have transitioned
3271 * the count back to > 0.
3273 if (vp->v_usecount > 0)
3277 * If the vnode is doomed vgone already performed inactive processing
3280 if (VN_IS_DOOMED(vp))
3283 if (__predict_true(VOP_NEED_INACTIVE(vp) == 0))
3286 if (vp->v_iflag & VI_DOINGINACT)
3290 * Locking operations here will drop the interlock and possibly the
3291 * vnode lock, opening a window where the vnode can get doomed all the
3292 * while ->v_usecount is 0. Set VI_OWEINACT to let vgone know to
3295 vp->v_iflag |= VI_OWEINACT;
3296 want_unlock = false;
3300 switch (VOP_ISLOCKED(vp)) {
3306 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
3311 * The lock has at least one sharer, but we have no way
3312 * to conclude whether this is us. Play it safe and
3321 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
3322 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
3328 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
3329 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
3340 vdefer_inactive(vp);
3350 * Decrement ->v_usecount for a vnode.
3352 * Releasing the last use count requires additional processing, see vput_final
3353 * above for details.
3355 * Note that releasing use count without the vnode lock requires special casing
3356 * for VCHR, see v_decr_devcount for details.
3358 * Comment above each variant denotes lock state on entry and exit.
3361 static void __noinline
3362 vrele_vchr(struct vnode *vp)
3365 if (refcount_release_if_not_last(&vp->v_usecount))
3368 if (!refcount_release(&vp->v_usecount)) {
3372 v_decr_devcount(vp);
3374 vput_final(vp, VRELE);
3379 * out: same as passed in
3382 vrele(struct vnode *vp)
3385 ASSERT_VI_UNLOCKED(vp, __func__);
3386 if (__predict_false(vp->v_type == VCHR)) {
3390 if (!refcount_release(&vp->v_usecount))
3392 vput_final(vp, VRELE);
3400 vput(struct vnode *vp)
3403 ASSERT_VOP_LOCKED(vp, __func__);
3404 ASSERT_VI_UNLOCKED(vp, __func__);
3405 if (!refcount_release(&vp->v_usecount)) {
3409 vput_final(vp, VPUT);
3417 vunref(struct vnode *vp)
3420 ASSERT_VOP_LOCKED(vp, __func__);
3421 ASSERT_VI_UNLOCKED(vp, __func__);
3422 if (!refcount_release(&vp->v_usecount))
3424 vput_final(vp, VUNREF);
3428 vhold(struct vnode *vp)
3433 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3434 old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3435 VNASSERT(old >= 0 && (old & VHOLD_ALL_FLAGS) == 0, vp,
3436 ("%s: wrong hold count %d", __func__, old));
3446 vholdl(struct vnode *vp)
3449 ASSERT_VI_LOCKED(vp, __func__);
3450 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3455 vholdnz(struct vnode *vp)
3458 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3460 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3461 VNASSERT(old > 0 && (old & VHOLD_ALL_FLAGS) == 0, vp,
3462 ("%s: wrong hold count %d", __func__, old));
3464 atomic_add_int(&vp->v_holdcnt, 1);
3469 * Grab a hold count unless the vnode is freed.
3471 * Only use this routine if vfs smr is the only protection you have against
3472 * freeing the vnode.
3474 * The code loops trying to add a hold count as long as the VHOLD_NO_SMR flag
3475 * is not set. After the flag is set the vnode becomes immutable to anyone but
3476 * the thread which managed to set the flag.
3478 * It may be tempting to replace the loop with:
3479 * count = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3480 * if (count & VHOLD_NO_SMR) {
3481 * backpedal and error out;
3484 * However, while this is more performant, it hinders debugging by eliminating
3485 * the previously mentioned invariant.
3488 vhold_smr(struct vnode *vp)
3492 VFS_SMR_ASSERT_ENTERED();
3494 count = atomic_load_int(&vp->v_holdcnt);
3496 if (count & VHOLD_NO_SMR) {
3497 VNASSERT((count & ~VHOLD_NO_SMR) == 0, vp,
3498 ("non-zero hold count with flags %d\n", count));
3502 VNASSERT(count >= 0, vp, ("invalid hold count %d\n", count));
3503 if (atomic_fcmpset_int(&vp->v_holdcnt, &count, count + 1))
3508 static void __noinline
3509 vdbatch_process(struct vdbatch *vd)
3514 mtx_assert(&vd->lock, MA_OWNED);
3515 MPASS(curthread->td_pinned > 0);
3516 MPASS(vd->index == VDBATCH_SIZE);
3518 mtx_lock(&vnode_list_mtx);
3520 freevnodes += vd->freevnodes;
3521 for (i = 0; i < VDBATCH_SIZE; i++) {
3523 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist);
3524 TAILQ_INSERT_TAIL(&vnode_list, vp, v_vnodelist);
3525 MPASS(vp->v_dbatchcpu != NOCPU);
3526 vp->v_dbatchcpu = NOCPU;
3528 mtx_unlock(&vnode_list_mtx);
3530 bzero(vd->tab, sizeof(vd->tab));
3536 vdbatch_enqueue(struct vnode *vp)
3540 ASSERT_VI_LOCKED(vp, __func__);
3541 VNASSERT(!VN_IS_DOOMED(vp), vp,
3542 ("%s: deferring requeue of a doomed vnode", __func__));
3547 if (vp->v_dbatchcpu != NOCPU) {
3555 mtx_lock(&vd->lock);
3556 MPASS(vd->index < VDBATCH_SIZE);
3557 MPASS(vd->tab[vd->index] == NULL);
3559 * A hack: we depend on being pinned so that we know what to put in
3562 vp->v_dbatchcpu = curcpu;
3563 vd->tab[vd->index] = vp;
3566 if (vd->index == VDBATCH_SIZE)
3567 vdbatch_process(vd);
3568 mtx_unlock(&vd->lock);
3573 * This routine must only be called for vnodes which are about to be
3574 * deallocated. Supporting dequeue for arbitrary vndoes would require
3575 * validating that the locked batch matches.
3578 vdbatch_dequeue(struct vnode *vp)
3584 VNASSERT(vp->v_type == VBAD || vp->v_type == VNON, vp,
3585 ("%s: called for a used vnode\n", __func__));
3587 cpu = vp->v_dbatchcpu;
3591 vd = DPCPU_ID_PTR(cpu, vd);
3592 mtx_lock(&vd->lock);
3593 for (i = 0; i < vd->index; i++) {
3594 if (vd->tab[i] != vp)
3596 vp->v_dbatchcpu = NOCPU;
3598 vd->tab[i] = vd->tab[vd->index];
3599 vd->tab[vd->index] = NULL;
3602 mtx_unlock(&vd->lock);
3604 * Either we dequeued the vnode above or the target CPU beat us to it.
3606 MPASS(vp->v_dbatchcpu == NOCPU);
3610 * Drop the hold count of the vnode. If this is the last reference to
3611 * the vnode we place it on the free list unless it has been vgone'd
3612 * (marked VIRF_DOOMED) in which case we will free it.
3614 * Because the vnode vm object keeps a hold reference on the vnode if
3615 * there is at least one resident non-cached page, the vnode cannot
3616 * leave the active list without the page cleanup done.
3619 vdrop_deactivate(struct vnode *vp)
3623 ASSERT_VI_LOCKED(vp, __func__);
3625 * Mark a vnode as free: remove it from its active list
3626 * and put it up for recycling on the freelist.
3628 VNASSERT(!VN_IS_DOOMED(vp), vp,
3629 ("vdrop: returning doomed vnode"));
3630 VNASSERT(vp->v_op != NULL, vp,
3631 ("vdrop: vnode already reclaimed."));
3632 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
3633 ("vnode with VI_OWEINACT set"));
3634 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp,
3635 ("vnode with VI_DEFINACT set"));
3636 if (vp->v_mflag & VMP_LAZYLIST) {
3638 mtx_lock(&mp->mnt_listmtx);
3639 VNASSERT(vp->v_mflag & VMP_LAZYLIST, vp, ("lost VMP_LAZYLIST"));
3641 * Don't remove the vnode from the lazy list if another thread
3642 * has increased the hold count. It may have re-enqueued the
3643 * vnode to the lazy list and is now responsible for its
3646 if (vp->v_holdcnt == 0) {
3647 vp->v_mflag &= ~VMP_LAZYLIST;
3648 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3649 mp->mnt_lazyvnodelistsize--;
3651 mtx_unlock(&mp->mnt_listmtx);
3653 vdbatch_enqueue(vp);
3657 vdrop(struct vnode *vp)
3660 ASSERT_VI_UNLOCKED(vp, __func__);
3661 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3662 if (refcount_release_if_not_last(&vp->v_holdcnt))
3669 vdropl(struct vnode *vp)
3672 ASSERT_VI_LOCKED(vp, __func__);
3673 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3674 if (!refcount_release(&vp->v_holdcnt)) {
3678 if (!VN_IS_DOOMED(vp)) {
3679 vdrop_deactivate(vp);
3683 * We may be racing against vhold_smr.
3685 * If they win we can just pretend we never got this far, they will
3688 if (!atomic_cmpset_int(&vp->v_holdcnt, 0, VHOLD_NO_SMR)) {
3690 * We lost the aforementioned race. Note that any subsequent
3691 * access is invalid as they might have managed to vdropl on
3700 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
3701 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
3704 vinactivef(struct vnode *vp)
3706 struct vm_object *obj;
3708 ASSERT_VOP_ELOCKED(vp, "vinactive");
3709 ASSERT_VI_LOCKED(vp, "vinactive");
3710 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
3711 ("vinactive: recursed on VI_DOINGINACT"));
3712 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3713 vp->v_iflag |= VI_DOINGINACT;
3714 vp->v_iflag &= ~VI_OWEINACT;
3717 * Before moving off the active list, we must be sure that any
3718 * modified pages are converted into the vnode's dirty
3719 * buffers, since these will no longer be checked once the
3720 * vnode is on the inactive list.
3722 * The write-out of the dirty pages is asynchronous. At the
3723 * point that VOP_INACTIVE() is called, there could still be
3724 * pending I/O and dirty pages in the object.
3726 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
3727 vm_object_mightbedirty(obj)) {
3728 VM_OBJECT_WLOCK(obj);
3729 vm_object_page_clean(obj, 0, 0, 0);
3730 VM_OBJECT_WUNLOCK(obj);
3732 VOP_INACTIVE(vp, curthread);
3734 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
3735 ("vinactive: lost VI_DOINGINACT"));
3736 vp->v_iflag &= ~VI_DOINGINACT;
3740 vinactive(struct vnode *vp)
3743 ASSERT_VOP_ELOCKED(vp, "vinactive");
3744 ASSERT_VI_LOCKED(vp, "vinactive");
3745 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3747 if ((vp->v_iflag & VI_OWEINACT) == 0)
3749 if (vp->v_iflag & VI_DOINGINACT)
3751 if (vp->v_usecount > 0) {
3752 vp->v_iflag &= ~VI_OWEINACT;
3759 * Remove any vnodes in the vnode table belonging to mount point mp.
3761 * If FORCECLOSE is not specified, there should not be any active ones,
3762 * return error if any are found (nb: this is a user error, not a
3763 * system error). If FORCECLOSE is specified, detach any active vnodes
3766 * If WRITECLOSE is set, only flush out regular file vnodes open for
3769 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
3771 * `rootrefs' specifies the base reference count for the root vnode
3772 * of this filesystem. The root vnode is considered busy if its
3773 * v_usecount exceeds this value. On a successful return, vflush(, td)
3774 * will call vrele() on the root vnode exactly rootrefs times.
3775 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
3779 static int busyprt = 0; /* print out busy vnodes */
3780 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
3784 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
3786 struct vnode *vp, *mvp, *rootvp = NULL;
3788 int busy = 0, error;
3790 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
3793 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
3794 ("vflush: bad args"));
3796 * Get the filesystem root vnode. We can vput() it
3797 * immediately, since with rootrefs > 0, it won't go away.
3799 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
3800 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
3807 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
3809 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
3812 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3816 * Skip over a vnodes marked VV_SYSTEM.
3818 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
3824 * If WRITECLOSE is set, flush out unlinked but still open
3825 * files (even if open only for reading) and regular file
3826 * vnodes open for writing.
3828 if (flags & WRITECLOSE) {
3829 if (vp->v_object != NULL) {
3830 VM_OBJECT_WLOCK(vp->v_object);
3831 vm_object_page_clean(vp->v_object, 0, 0, 0);
3832 VM_OBJECT_WUNLOCK(vp->v_object);
3834 error = VOP_FSYNC(vp, MNT_WAIT, td);
3838 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3841 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3844 if ((vp->v_type == VNON ||
3845 (error == 0 && vattr.va_nlink > 0)) &&
3846 (vp->v_writecount <= 0 || vp->v_type != VREG)) {
3854 * With v_usecount == 0, all we need to do is clear out the
3855 * vnode data structures and we are done.
3857 * If FORCECLOSE is set, forcibly close the vnode.
3859 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
3865 vn_printf(vp, "vflush: busy vnode ");
3871 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
3873 * If just the root vnode is busy, and if its refcount
3874 * is equal to `rootrefs', then go ahead and kill it.
3877 KASSERT(busy > 0, ("vflush: not busy"));
3878 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
3879 ("vflush: usecount %d < rootrefs %d",
3880 rootvp->v_usecount, rootrefs));
3881 if (busy == 1 && rootvp->v_usecount == rootrefs) {
3882 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
3890 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
3894 for (; rootrefs > 0; rootrefs--)
3900 * Recycle an unused vnode to the front of the free list.
3903 vrecycle(struct vnode *vp)
3908 recycled = vrecyclel(vp);
3914 * vrecycle, with the vp interlock held.
3917 vrecyclel(struct vnode *vp)
3921 ASSERT_VOP_ELOCKED(vp, __func__);
3922 ASSERT_VI_LOCKED(vp, __func__);
3923 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3925 if (vp->v_usecount == 0) {
3933 * Eliminate all activity associated with a vnode
3934 * in preparation for reuse.
3937 vgone(struct vnode *vp)
3945 notify_lowervp_vfs_dummy(struct mount *mp __unused,
3946 struct vnode *lowervp __unused)
3951 * Notify upper mounts about reclaimed or unlinked vnode.
3954 vfs_notify_upper(struct vnode *vp, int event)
3956 static struct vfsops vgonel_vfsops = {
3957 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
3958 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
3960 struct mount *mp, *ump, *mmp;
3965 if (TAILQ_EMPTY(&mp->mnt_uppers))
3968 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
3969 mmp->mnt_op = &vgonel_vfsops;
3970 mmp->mnt_kern_flag |= MNTK_MARKER;
3972 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
3973 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
3974 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
3975 ump = TAILQ_NEXT(ump, mnt_upper_link);
3978 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
3981 case VFS_NOTIFY_UPPER_RECLAIM:
3982 VFS_RECLAIM_LOWERVP(ump, vp);
3984 case VFS_NOTIFY_UPPER_UNLINK:
3985 VFS_UNLINK_LOWERVP(ump, vp);
3988 KASSERT(0, ("invalid event %d", event));
3992 ump = TAILQ_NEXT(mmp, mnt_upper_link);
3993 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
3996 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
3997 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
3998 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
3999 wakeup(&mp->mnt_uppers);
4005 * vgone, with the vp interlock held.
4008 vgonel(struct vnode *vp)
4013 bool active, oweinact;
4015 ASSERT_VOP_ELOCKED(vp, "vgonel");
4016 ASSERT_VI_LOCKED(vp, "vgonel");
4017 VNASSERT(vp->v_holdcnt, vp,
4018 ("vgonel: vp %p has no reference.", vp));
4019 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
4023 * Don't vgonel if we're already doomed.
4025 if (vp->v_irflag & VIRF_DOOMED)
4028 * Paired with freevnode.
4030 vn_seqc_write_begin_locked(vp);
4032 vp->v_irflag |= VIRF_DOOMED;
4035 * Check to see if the vnode is in use. If so, we have to call
4036 * VOP_CLOSE() and VOP_INACTIVE().
4038 active = vp->v_usecount > 0;
4039 oweinact = (vp->v_iflag & VI_OWEINACT) != 0;
4041 * If we need to do inactive VI_OWEINACT will be set.
4043 if (vp->v_iflag & VI_DEFINACT) {
4044 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count"));
4045 vp->v_iflag &= ~VI_DEFINACT;
4048 VNASSERT(vp->v_holdcnt > 0, vp, ("vnode without hold count"));
4051 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
4054 * If purging an active vnode, it must be closed and
4055 * deactivated before being reclaimed.
4058 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
4059 if (oweinact || active) {
4064 if (vp->v_type == VSOCK)
4065 vfs_unp_reclaim(vp);
4068 * Clean out any buffers associated with the vnode.
4069 * If the flush fails, just toss the buffers.
4072 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
4073 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
4074 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
4075 while (vinvalbuf(vp, 0, 0, 0) != 0)
4079 BO_LOCK(&vp->v_bufobj);
4080 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
4081 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
4082 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
4083 vp->v_bufobj.bo_clean.bv_cnt == 0,
4084 ("vp %p bufobj not invalidated", vp));
4087 * For VMIO bufobj, BO_DEAD is set later, or in
4088 * vm_object_terminate() after the object's page queue is
4091 object = vp->v_bufobj.bo_object;
4093 vp->v_bufobj.bo_flag |= BO_DEAD;
4094 BO_UNLOCK(&vp->v_bufobj);
4097 * Handle the VM part. Tmpfs handles v_object on its own (the
4098 * OBJT_VNODE check). Nullfs or other bypassing filesystems
4099 * should not touch the object borrowed from the lower vnode
4100 * (the handle check).
4102 if (object != NULL && object->type == OBJT_VNODE &&
4103 object->handle == vp)
4104 vnode_destroy_vobject(vp);
4107 * Reclaim the vnode.
4109 if (VOP_RECLAIM(vp, td))
4110 panic("vgone: cannot reclaim");
4112 vn_finished_secondary_write(mp);
4113 VNASSERT(vp->v_object == NULL, vp,
4114 ("vop_reclaim left v_object vp=%p", vp));
4116 * Clear the advisory locks and wake up waiting threads.
4118 (void)VOP_ADVLOCKPURGE(vp);
4121 * Delete from old mount point vnode list.
4126 * Done with purge, reset to the standard lock and invalidate
4130 vp->v_vnlock = &vp->v_lock;
4131 vp->v_op = &dead_vnodeops;
4136 * Calculate the total number of references to a special device.
4139 vcount(struct vnode *vp)
4144 count = vp->v_rdev->si_usecount;
4150 * Print out a description of a vnode.
4152 static const char * const typename[] =
4153 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
4156 _Static_assert((VHOLD_ALL_FLAGS & ~VHOLD_NO_SMR) == 0,
4157 "new hold count flag not added to vn_printf");
4160 vn_printf(struct vnode *vp, const char *fmt, ...)
4163 char buf[256], buf2[16];
4170 printf("%p: ", (void *)vp);
4171 printf("type %s\n", typename[vp->v_type]);
4172 holdcnt = atomic_load_int(&vp->v_holdcnt);
4173 printf(" usecount %d, writecount %d, refcount %d seqc users %d",
4174 vp->v_usecount, vp->v_writecount, holdcnt & ~VHOLD_ALL_FLAGS,
4176 switch (vp->v_type) {
4178 printf(" mountedhere %p\n", vp->v_mountedhere);
4181 printf(" rdev %p\n", vp->v_rdev);
4184 printf(" socket %p\n", vp->v_unpcb);
4187 printf(" fifoinfo %p\n", vp->v_fifoinfo);
4195 if (holdcnt & VHOLD_NO_SMR)
4196 strlcat(buf, "|VHOLD_NO_SMR", sizeof(buf));
4197 printf(" hold count flags (%s)\n", buf + 1);
4201 if (vp->v_irflag & VIRF_DOOMED)
4202 strlcat(buf, "|VIRF_DOOMED", sizeof(buf));
4203 flags = vp->v_irflag & ~(VIRF_DOOMED);
4205 snprintf(buf2, sizeof(buf2), "|VIRF(0x%lx)", flags);
4206 strlcat(buf, buf2, sizeof(buf));
4208 if (vp->v_vflag & VV_ROOT)
4209 strlcat(buf, "|VV_ROOT", sizeof(buf));
4210 if (vp->v_vflag & VV_ISTTY)
4211 strlcat(buf, "|VV_ISTTY", sizeof(buf));
4212 if (vp->v_vflag & VV_NOSYNC)
4213 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
4214 if (vp->v_vflag & VV_ETERNALDEV)
4215 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
4216 if (vp->v_vflag & VV_CACHEDLABEL)
4217 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
4218 if (vp->v_vflag & VV_VMSIZEVNLOCK)
4219 strlcat(buf, "|VV_VMSIZEVNLOCK", sizeof(buf));
4220 if (vp->v_vflag & VV_COPYONWRITE)
4221 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
4222 if (vp->v_vflag & VV_SYSTEM)
4223 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
4224 if (vp->v_vflag & VV_PROCDEP)
4225 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
4226 if (vp->v_vflag & VV_NOKNOTE)
4227 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
4228 if (vp->v_vflag & VV_DELETED)
4229 strlcat(buf, "|VV_DELETED", sizeof(buf));
4230 if (vp->v_vflag & VV_MD)
4231 strlcat(buf, "|VV_MD", sizeof(buf));
4232 if (vp->v_vflag & VV_FORCEINSMQ)
4233 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
4234 if (vp->v_vflag & VV_READLINK)
4235 strlcat(buf, "|VV_READLINK", sizeof(buf));
4236 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
4237 VV_CACHEDLABEL | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
4238 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
4240 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
4241 strlcat(buf, buf2, sizeof(buf));
4243 if (vp->v_iflag & VI_TEXT_REF)
4244 strlcat(buf, "|VI_TEXT_REF", sizeof(buf));
4245 if (vp->v_iflag & VI_MOUNT)
4246 strlcat(buf, "|VI_MOUNT", sizeof(buf));
4247 if (vp->v_iflag & VI_DOINGINACT)
4248 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
4249 if (vp->v_iflag & VI_OWEINACT)
4250 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
4251 if (vp->v_iflag & VI_DEFINACT)
4252 strlcat(buf, "|VI_DEFINACT", sizeof(buf));
4253 flags = vp->v_iflag & ~(VI_TEXT_REF | VI_MOUNT | VI_DOINGINACT |
4254 VI_OWEINACT | VI_DEFINACT);
4256 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
4257 strlcat(buf, buf2, sizeof(buf));
4259 if (vp->v_mflag & VMP_LAZYLIST)
4260 strlcat(buf, "|VMP_LAZYLIST", sizeof(buf));
4261 flags = vp->v_mflag & ~(VMP_LAZYLIST);
4263 snprintf(buf2, sizeof(buf2), "|VMP(0x%lx)", flags);
4264 strlcat(buf, buf2, sizeof(buf));
4266 printf(" flags (%s)\n", buf + 1);
4267 if (mtx_owned(VI_MTX(vp)))
4268 printf(" VI_LOCKed");
4269 if (vp->v_object != NULL)
4270 printf(" v_object %p ref %d pages %d "
4271 "cleanbuf %d dirtybuf %d\n",
4272 vp->v_object, vp->v_object->ref_count,
4273 vp->v_object->resident_page_count,
4274 vp->v_bufobj.bo_clean.bv_cnt,
4275 vp->v_bufobj.bo_dirty.bv_cnt);
4277 lockmgr_printinfo(vp->v_vnlock);
4278 if (vp->v_data != NULL)
4284 * List all of the locked vnodes in the system.
4285 * Called when debugging the kernel.
4287 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
4293 * Note: because this is DDB, we can't obey the locking semantics
4294 * for these structures, which means we could catch an inconsistent
4295 * state and dereference a nasty pointer. Not much to be done
4298 db_printf("Locked vnodes\n");
4299 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4300 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4301 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
4302 vn_printf(vp, "vnode ");
4308 * Show details about the given vnode.
4310 DB_SHOW_COMMAND(vnode, db_show_vnode)
4316 vp = (struct vnode *)addr;
4317 vn_printf(vp, "vnode ");
4321 * Show details about the given mount point.
4323 DB_SHOW_COMMAND(mount, db_show_mount)
4334 /* No address given, print short info about all mount points. */
4335 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4336 db_printf("%p %s on %s (%s)\n", mp,
4337 mp->mnt_stat.f_mntfromname,
4338 mp->mnt_stat.f_mntonname,
4339 mp->mnt_stat.f_fstypename);
4343 db_printf("\nMore info: show mount <addr>\n");
4347 mp = (struct mount *)addr;
4348 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
4349 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
4352 mflags = mp->mnt_flag;
4353 #define MNT_FLAG(flag) do { \
4354 if (mflags & (flag)) { \
4355 if (buf[0] != '\0') \
4356 strlcat(buf, ", ", sizeof(buf)); \
4357 strlcat(buf, (#flag) + 4, sizeof(buf)); \
4358 mflags &= ~(flag); \
4361 MNT_FLAG(MNT_RDONLY);
4362 MNT_FLAG(MNT_SYNCHRONOUS);
4363 MNT_FLAG(MNT_NOEXEC);
4364 MNT_FLAG(MNT_NOSUID);
4365 MNT_FLAG(MNT_NFS4ACLS);
4366 MNT_FLAG(MNT_UNION);
4367 MNT_FLAG(MNT_ASYNC);
4368 MNT_FLAG(MNT_SUIDDIR);
4369 MNT_FLAG(MNT_SOFTDEP);
4370 MNT_FLAG(MNT_NOSYMFOLLOW);
4371 MNT_FLAG(MNT_GJOURNAL);
4372 MNT_FLAG(MNT_MULTILABEL);
4374 MNT_FLAG(MNT_NOATIME);
4375 MNT_FLAG(MNT_NOCLUSTERR);
4376 MNT_FLAG(MNT_NOCLUSTERW);
4378 MNT_FLAG(MNT_EXRDONLY);
4379 MNT_FLAG(MNT_EXPORTED);
4380 MNT_FLAG(MNT_DEFEXPORTED);
4381 MNT_FLAG(MNT_EXPORTANON);
4382 MNT_FLAG(MNT_EXKERB);
4383 MNT_FLAG(MNT_EXPUBLIC);
4384 MNT_FLAG(MNT_LOCAL);
4385 MNT_FLAG(MNT_QUOTA);
4386 MNT_FLAG(MNT_ROOTFS);
4388 MNT_FLAG(MNT_IGNORE);
4389 MNT_FLAG(MNT_UPDATE);
4390 MNT_FLAG(MNT_DELEXPORT);
4391 MNT_FLAG(MNT_RELOAD);
4392 MNT_FLAG(MNT_FORCE);
4393 MNT_FLAG(MNT_SNAPSHOT);
4394 MNT_FLAG(MNT_BYFSID);
4398 strlcat(buf, ", ", sizeof(buf));
4399 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
4400 "0x%016jx", mflags);
4402 db_printf(" mnt_flag = %s\n", buf);
4405 flags = mp->mnt_kern_flag;
4406 #define MNT_KERN_FLAG(flag) do { \
4407 if (flags & (flag)) { \
4408 if (buf[0] != '\0') \
4409 strlcat(buf, ", ", sizeof(buf)); \
4410 strlcat(buf, (#flag) + 5, sizeof(buf)); \
4414 MNT_KERN_FLAG(MNTK_UNMOUNTF);
4415 MNT_KERN_FLAG(MNTK_ASYNC);
4416 MNT_KERN_FLAG(MNTK_SOFTDEP);
4417 MNT_KERN_FLAG(MNTK_DRAINING);
4418 MNT_KERN_FLAG(MNTK_REFEXPIRE);
4419 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
4420 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
4421 MNT_KERN_FLAG(MNTK_NO_IOPF);
4422 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
4423 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
4424 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
4425 MNT_KERN_FLAG(MNTK_MARKER);
4426 MNT_KERN_FLAG(MNTK_USES_BCACHE);
4427 MNT_KERN_FLAG(MNTK_NOASYNC);
4428 MNT_KERN_FLAG(MNTK_UNMOUNT);
4429 MNT_KERN_FLAG(MNTK_MWAIT);
4430 MNT_KERN_FLAG(MNTK_SUSPEND);
4431 MNT_KERN_FLAG(MNTK_SUSPEND2);
4432 MNT_KERN_FLAG(MNTK_SUSPENDED);
4433 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
4434 MNT_KERN_FLAG(MNTK_NOKNOTE);
4435 #undef MNT_KERN_FLAG
4438 strlcat(buf, ", ", sizeof(buf));
4439 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
4442 db_printf(" mnt_kern_flag = %s\n", buf);
4444 db_printf(" mnt_opt = ");
4445 opt = TAILQ_FIRST(mp->mnt_opt);
4447 db_printf("%s", opt->name);
4448 opt = TAILQ_NEXT(opt, link);
4449 while (opt != NULL) {
4450 db_printf(", %s", opt->name);
4451 opt = TAILQ_NEXT(opt, link);
4457 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
4458 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
4459 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
4460 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
4461 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
4462 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
4463 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
4464 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
4465 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
4466 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
4467 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
4468 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
4470 db_printf(" mnt_cred = { uid=%u ruid=%u",
4471 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
4472 if (jailed(mp->mnt_cred))
4473 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
4475 db_printf(" mnt_ref = %d (with %d in the struct)\n",
4476 vfs_mount_fetch_counter(mp, MNT_COUNT_REF), mp->mnt_ref);
4477 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
4478 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
4479 db_printf(" mnt_lazyvnodelistsize = %d\n",
4480 mp->mnt_lazyvnodelistsize);
4481 db_printf(" mnt_writeopcount = %d (with %d in the struct)\n",
4482 vfs_mount_fetch_counter(mp, MNT_COUNT_WRITEOPCOUNT), mp->mnt_writeopcount);
4483 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
4484 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
4485 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
4486 db_printf(" mnt_lockref = %d (with %d in the struct)\n",
4487 vfs_mount_fetch_counter(mp, MNT_COUNT_LOCKREF), mp->mnt_lockref);
4488 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
4489 db_printf(" mnt_secondary_accwrites = %d\n",
4490 mp->mnt_secondary_accwrites);
4491 db_printf(" mnt_gjprovider = %s\n",
4492 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
4493 db_printf(" mnt_vfs_ops = %d\n", mp->mnt_vfs_ops);
4495 db_printf("\n\nList of active vnodes\n");
4496 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4497 if (vp->v_type != VMARKER && vp->v_holdcnt > 0) {
4498 vn_printf(vp, "vnode ");
4503 db_printf("\n\nList of inactive vnodes\n");
4504 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4505 if (vp->v_type != VMARKER && vp->v_holdcnt == 0) {
4506 vn_printf(vp, "vnode ");
4515 * Fill in a struct xvfsconf based on a struct vfsconf.
4518 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
4520 struct xvfsconf xvfsp;
4522 bzero(&xvfsp, sizeof(xvfsp));
4523 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4524 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4525 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4526 xvfsp.vfc_flags = vfsp->vfc_flags;
4528 * These are unused in userland, we keep them
4529 * to not break binary compatibility.
4531 xvfsp.vfc_vfsops = NULL;
4532 xvfsp.vfc_next = NULL;
4533 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4536 #ifdef COMPAT_FREEBSD32
4538 uint32_t vfc_vfsops;
4539 char vfc_name[MFSNAMELEN];
4540 int32_t vfc_typenum;
4541 int32_t vfc_refcount;
4547 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
4549 struct xvfsconf32 xvfsp;
4551 bzero(&xvfsp, sizeof(xvfsp));
4552 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4553 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4554 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4555 xvfsp.vfc_flags = vfsp->vfc_flags;
4556 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4561 * Top level filesystem related information gathering.
4564 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
4566 struct vfsconf *vfsp;
4571 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4572 #ifdef COMPAT_FREEBSD32
4573 if (req->flags & SCTL_MASK32)
4574 error = vfsconf2x32(req, vfsp);
4577 error = vfsconf2x(req, vfsp);
4585 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
4586 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
4587 "S,xvfsconf", "List of all configured filesystems");
4589 #ifndef BURN_BRIDGES
4590 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
4593 vfs_sysctl(SYSCTL_HANDLER_ARGS)
4595 int *name = (int *)arg1 - 1; /* XXX */
4596 u_int namelen = arg2 + 1; /* XXX */
4597 struct vfsconf *vfsp;
4599 log(LOG_WARNING, "userland calling deprecated sysctl, "
4600 "please rebuild world\n");
4602 #if 1 || defined(COMPAT_PRELITE2)
4603 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
4605 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
4609 case VFS_MAXTYPENUM:
4612 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
4615 return (ENOTDIR); /* overloaded */
4617 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4618 if (vfsp->vfc_typenum == name[2])
4623 return (EOPNOTSUPP);
4624 #ifdef COMPAT_FREEBSD32
4625 if (req->flags & SCTL_MASK32)
4626 return (vfsconf2x32(req, vfsp));
4629 return (vfsconf2x(req, vfsp));
4631 return (EOPNOTSUPP);
4634 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
4635 CTLFLAG_MPSAFE, vfs_sysctl,
4636 "Generic filesystem");
4638 #if 1 || defined(COMPAT_PRELITE2)
4641 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
4644 struct vfsconf *vfsp;
4645 struct ovfsconf ovfs;
4648 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4649 bzero(&ovfs, sizeof(ovfs));
4650 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
4651 strcpy(ovfs.vfc_name, vfsp->vfc_name);
4652 ovfs.vfc_index = vfsp->vfc_typenum;
4653 ovfs.vfc_refcount = vfsp->vfc_refcount;
4654 ovfs.vfc_flags = vfsp->vfc_flags;
4655 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
4665 #endif /* 1 || COMPAT_PRELITE2 */
4666 #endif /* !BURN_BRIDGES */
4668 #define KINFO_VNODESLOP 10
4671 * Dump vnode list (via sysctl).
4675 sysctl_vnode(SYSCTL_HANDLER_ARGS)
4683 * Stale numvnodes access is not fatal here.
4686 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
4688 /* Make an estimate */
4689 return (SYSCTL_OUT(req, 0, len));
4691 error = sysctl_wire_old_buffer(req, 0);
4694 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
4696 mtx_lock(&mountlist_mtx);
4697 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4698 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
4701 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4705 xvn[n].xv_size = sizeof *xvn;
4706 xvn[n].xv_vnode = vp;
4707 xvn[n].xv_id = 0; /* XXX compat */
4708 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
4710 XV_COPY(writecount);
4716 xvn[n].xv_flag = vp->v_vflag;
4718 switch (vp->v_type) {
4725 if (vp->v_rdev == NULL) {
4729 xvn[n].xv_dev = dev2udev(vp->v_rdev);
4732 xvn[n].xv_socket = vp->v_socket;
4735 xvn[n].xv_fifo = vp->v_fifoinfo;
4740 /* shouldn't happen? */
4748 mtx_lock(&mountlist_mtx);
4753 mtx_unlock(&mountlist_mtx);
4755 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
4760 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
4761 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
4766 unmount_or_warn(struct mount *mp)
4770 error = dounmount(mp, MNT_FORCE, curthread);
4772 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
4776 printf("%d)\n", error);
4781 * Unmount all filesystems. The list is traversed in reverse order
4782 * of mounting to avoid dependencies.
4785 vfs_unmountall(void)
4787 struct mount *mp, *tmp;
4789 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
4792 * Since this only runs when rebooting, it is not interlocked.
4794 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
4798 * Forcibly unmounting "/dev" before "/" would prevent clean
4799 * unmount of the latter.
4801 if (mp == rootdevmp)
4804 unmount_or_warn(mp);
4807 if (rootdevmp != NULL)
4808 unmount_or_warn(rootdevmp);
4812 vfs_deferred_inactive(struct vnode *vp, int lkflags)
4815 ASSERT_VI_LOCKED(vp, __func__);
4816 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, ("VI_DEFINACT still set"));
4817 if ((vp->v_iflag & VI_OWEINACT) == 0) {
4821 if (vn_lock(vp, lkflags) == 0) {
4828 vdefer_inactive_unlocked(vp);
4832 vfs_periodic_inactive_filter(struct vnode *vp, void *arg)
4835 return (vp->v_iflag & VI_DEFINACT);
4838 static void __noinline
4839 vfs_periodic_inactive(struct mount *mp, int flags)
4841 struct vnode *vp, *mvp;
4844 lkflags = LK_EXCLUSIVE | LK_INTERLOCK;
4845 if (flags != MNT_WAIT)
4846 lkflags |= LK_NOWAIT;
4848 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_inactive_filter, NULL) {
4849 if ((vp->v_iflag & VI_DEFINACT) == 0) {
4853 vp->v_iflag &= ~VI_DEFINACT;
4854 vfs_deferred_inactive(vp, lkflags);
4859 vfs_want_msync(struct vnode *vp)
4861 struct vm_object *obj;
4864 * This test may be performed without any locks held.
4865 * We rely on vm_object's type stability.
4867 if (vp->v_vflag & VV_NOSYNC)
4870 return (obj != NULL && vm_object_mightbedirty(obj));
4874 vfs_periodic_msync_inactive_filter(struct vnode *vp, void *arg __unused)
4877 if (vp->v_vflag & VV_NOSYNC)
4879 if (vp->v_iflag & VI_DEFINACT)
4881 return (vfs_want_msync(vp));
4884 static void __noinline
4885 vfs_periodic_msync_inactive(struct mount *mp, int flags)
4887 struct vnode *vp, *mvp;
4888 struct vm_object *obj;
4890 int lkflags, objflags;
4895 lkflags = LK_EXCLUSIVE | LK_INTERLOCK;
4896 if (flags != MNT_WAIT) {
4897 lkflags |= LK_NOWAIT;
4898 objflags = OBJPC_NOSYNC;
4900 objflags = OBJPC_SYNC;
4903 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_msync_inactive_filter, NULL) {
4905 if (vp->v_iflag & VI_DEFINACT) {
4906 vp->v_iflag &= ~VI_DEFINACT;
4909 if (!vfs_want_msync(vp)) {
4911 vfs_deferred_inactive(vp, lkflags);
4916 if (vget(vp, lkflags, td) == 0) {
4918 if (obj != NULL && (vp->v_vflag & VV_NOSYNC) == 0) {
4919 VM_OBJECT_WLOCK(obj);
4920 vm_object_page_clean(obj, 0, 0, objflags);
4921 VM_OBJECT_WUNLOCK(obj);
4928 vdefer_inactive_unlocked(vp);
4934 vfs_periodic(struct mount *mp, int flags)
4937 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
4939 if ((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0)
4940 vfs_periodic_inactive(mp, flags);
4942 vfs_periodic_msync_inactive(mp, flags);
4946 destroy_vpollinfo_free(struct vpollinfo *vi)
4949 knlist_destroy(&vi->vpi_selinfo.si_note);
4950 mtx_destroy(&vi->vpi_lock);
4951 uma_zfree(vnodepoll_zone, vi);
4955 destroy_vpollinfo(struct vpollinfo *vi)
4958 knlist_clear(&vi->vpi_selinfo.si_note, 1);
4959 seldrain(&vi->vpi_selinfo);
4960 destroy_vpollinfo_free(vi);
4964 * Initialize per-vnode helper structure to hold poll-related state.
4967 v_addpollinfo(struct vnode *vp)
4969 struct vpollinfo *vi;
4971 if (vp->v_pollinfo != NULL)
4973 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
4974 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
4975 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
4976 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
4978 if (vp->v_pollinfo != NULL) {
4980 destroy_vpollinfo_free(vi);
4983 vp->v_pollinfo = vi;
4988 * Record a process's interest in events which might happen to
4989 * a vnode. Because poll uses the historic select-style interface
4990 * internally, this routine serves as both the ``check for any
4991 * pending events'' and the ``record my interest in future events''
4992 * functions. (These are done together, while the lock is held,
4993 * to avoid race conditions.)
4996 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
5000 mtx_lock(&vp->v_pollinfo->vpi_lock);
5001 if (vp->v_pollinfo->vpi_revents & events) {
5003 * This leaves events we are not interested
5004 * in available for the other process which
5005 * which presumably had requested them
5006 * (otherwise they would never have been
5009 events &= vp->v_pollinfo->vpi_revents;
5010 vp->v_pollinfo->vpi_revents &= ~events;
5012 mtx_unlock(&vp->v_pollinfo->vpi_lock);
5015 vp->v_pollinfo->vpi_events |= events;
5016 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
5017 mtx_unlock(&vp->v_pollinfo->vpi_lock);
5022 * Routine to create and manage a filesystem syncer vnode.
5024 #define sync_close ((int (*)(struct vop_close_args *))nullop)
5025 static int sync_fsync(struct vop_fsync_args *);
5026 static int sync_inactive(struct vop_inactive_args *);
5027 static int sync_reclaim(struct vop_reclaim_args *);
5029 static struct vop_vector sync_vnodeops = {
5030 .vop_bypass = VOP_EOPNOTSUPP,
5031 .vop_close = sync_close, /* close */
5032 .vop_fsync = sync_fsync, /* fsync */
5033 .vop_inactive = sync_inactive, /* inactive */
5034 .vop_need_inactive = vop_stdneed_inactive, /* need_inactive */
5035 .vop_reclaim = sync_reclaim, /* reclaim */
5036 .vop_lock1 = vop_stdlock, /* lock */
5037 .vop_unlock = vop_stdunlock, /* unlock */
5038 .vop_islocked = vop_stdislocked, /* islocked */
5040 VFS_VOP_VECTOR_REGISTER(sync_vnodeops);
5043 * Create a new filesystem syncer vnode for the specified mount point.
5046 vfs_allocate_syncvnode(struct mount *mp)
5050 static long start, incr, next;
5053 /* Allocate a new vnode */
5054 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
5056 panic("vfs_allocate_syncvnode: getnewvnode() failed");
5058 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5059 vp->v_vflag |= VV_FORCEINSMQ;
5060 error = insmntque(vp, mp);
5062 panic("vfs_allocate_syncvnode: insmntque() failed");
5063 vp->v_vflag &= ~VV_FORCEINSMQ;
5066 * Place the vnode onto the syncer worklist. We attempt to
5067 * scatter them about on the list so that they will go off
5068 * at evenly distributed times even if all the filesystems
5069 * are mounted at once.
5072 if (next == 0 || next > syncer_maxdelay) {
5076 start = syncer_maxdelay / 2;
5077 incr = syncer_maxdelay;
5083 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
5084 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
5085 mtx_lock(&sync_mtx);
5087 if (mp->mnt_syncer == NULL) {
5088 mp->mnt_syncer = vp;
5091 mtx_unlock(&sync_mtx);
5094 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5101 vfs_deallocate_syncvnode(struct mount *mp)
5105 mtx_lock(&sync_mtx);
5106 vp = mp->mnt_syncer;
5108 mp->mnt_syncer = NULL;
5109 mtx_unlock(&sync_mtx);
5115 * Do a lazy sync of the filesystem.
5118 sync_fsync(struct vop_fsync_args *ap)
5120 struct vnode *syncvp = ap->a_vp;
5121 struct mount *mp = syncvp->v_mount;
5126 * We only need to do something if this is a lazy evaluation.
5128 if (ap->a_waitfor != MNT_LAZY)
5132 * Move ourselves to the back of the sync list.
5134 bo = &syncvp->v_bufobj;
5136 vn_syncer_add_to_worklist(bo, syncdelay);
5140 * Walk the list of vnodes pushing all that are dirty and
5141 * not already on the sync list.
5143 if (vfs_busy(mp, MBF_NOWAIT) != 0)
5145 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
5149 save = curthread_pflags_set(TDP_SYNCIO);
5151 * The filesystem at hand may be idle with free vnodes stored in the
5152 * batch. Return them instead of letting them stay there indefinitely.
5154 vfs_periodic(mp, MNT_NOWAIT);
5155 error = VFS_SYNC(mp, MNT_LAZY);
5156 curthread_pflags_restore(save);
5157 vn_finished_write(mp);
5163 * The syncer vnode is no referenced.
5166 sync_inactive(struct vop_inactive_args *ap)
5174 * The syncer vnode is no longer needed and is being decommissioned.
5176 * Modifications to the worklist must be protected by sync_mtx.
5179 sync_reclaim(struct vop_reclaim_args *ap)
5181 struct vnode *vp = ap->a_vp;
5186 mtx_lock(&sync_mtx);
5187 if (vp->v_mount->mnt_syncer == vp)
5188 vp->v_mount->mnt_syncer = NULL;
5189 if (bo->bo_flag & BO_ONWORKLST) {
5190 LIST_REMOVE(bo, bo_synclist);
5191 syncer_worklist_len--;
5193 bo->bo_flag &= ~BO_ONWORKLST;
5195 mtx_unlock(&sync_mtx);
5202 vn_need_pageq_flush(struct vnode *vp)
5204 struct vm_object *obj;
5207 MPASS(mtx_owned(VI_MTX(vp)));
5209 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
5210 vm_object_mightbedirty(obj))
5216 * Check if vnode represents a disk device
5219 vn_isdisk(struct vnode *vp, int *errp)
5223 if (vp->v_type != VCHR) {
5229 if (vp->v_rdev == NULL)
5231 else if (vp->v_rdev->si_devsw == NULL)
5233 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
5239 return (error == 0);
5243 * Common filesystem object access control check routine. Accepts a
5244 * vnode's type, "mode", uid and gid, requested access mode, credentials,
5245 * and optional call-by-reference privused argument allowing vaccess()
5246 * to indicate to the caller whether privilege was used to satisfy the
5247 * request (obsoleted). Returns 0 on success, or an errno on failure.
5250 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
5251 accmode_t accmode, struct ucred *cred, int *privused)
5253 accmode_t dac_granted;
5254 accmode_t priv_granted;
5256 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
5257 ("invalid bit in accmode"));
5258 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
5259 ("VAPPEND without VWRITE"));
5262 * Look for a normal, non-privileged way to access the file/directory
5263 * as requested. If it exists, go with that.
5266 if (privused != NULL)
5271 /* Check the owner. */
5272 if (cred->cr_uid == file_uid) {
5273 dac_granted |= VADMIN;
5274 if (file_mode & S_IXUSR)
5275 dac_granted |= VEXEC;
5276 if (file_mode & S_IRUSR)
5277 dac_granted |= VREAD;
5278 if (file_mode & S_IWUSR)
5279 dac_granted |= (VWRITE | VAPPEND);
5281 if ((accmode & dac_granted) == accmode)
5287 /* Otherwise, check the groups (first match) */
5288 if (groupmember(file_gid, cred)) {
5289 if (file_mode & S_IXGRP)
5290 dac_granted |= VEXEC;
5291 if (file_mode & S_IRGRP)
5292 dac_granted |= VREAD;
5293 if (file_mode & S_IWGRP)
5294 dac_granted |= (VWRITE | VAPPEND);
5296 if ((accmode & dac_granted) == accmode)
5302 /* Otherwise, check everyone else. */
5303 if (file_mode & S_IXOTH)
5304 dac_granted |= VEXEC;
5305 if (file_mode & S_IROTH)
5306 dac_granted |= VREAD;
5307 if (file_mode & S_IWOTH)
5308 dac_granted |= (VWRITE | VAPPEND);
5309 if ((accmode & dac_granted) == accmode)
5314 * Build a privilege mask to determine if the set of privileges
5315 * satisfies the requirements when combined with the granted mask
5316 * from above. For each privilege, if the privilege is required,
5317 * bitwise or the request type onto the priv_granted mask.
5323 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
5324 * requests, instead of PRIV_VFS_EXEC.
5326 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
5327 !priv_check_cred(cred, PRIV_VFS_LOOKUP))
5328 priv_granted |= VEXEC;
5331 * Ensure that at least one execute bit is on. Otherwise,
5332 * a privileged user will always succeed, and we don't want
5333 * this to happen unless the file really is executable.
5335 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
5336 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
5337 !priv_check_cred(cred, PRIV_VFS_EXEC))
5338 priv_granted |= VEXEC;
5341 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
5342 !priv_check_cred(cred, PRIV_VFS_READ))
5343 priv_granted |= VREAD;
5345 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
5346 !priv_check_cred(cred, PRIV_VFS_WRITE))
5347 priv_granted |= (VWRITE | VAPPEND);
5349 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
5350 !priv_check_cred(cred, PRIV_VFS_ADMIN))
5351 priv_granted |= VADMIN;
5353 if ((accmode & (priv_granted | dac_granted)) == accmode) {
5354 /* XXX audit: privilege used */
5355 if (privused != NULL)
5360 return ((accmode & VADMIN) ? EPERM : EACCES);
5364 * Credential check based on process requesting service, and per-attribute
5368 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
5369 struct thread *td, accmode_t accmode)
5373 * Kernel-invoked always succeeds.
5379 * Do not allow privileged processes in jail to directly manipulate
5380 * system attributes.
5382 switch (attrnamespace) {
5383 case EXTATTR_NAMESPACE_SYSTEM:
5384 /* Potentially should be: return (EPERM); */
5385 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
5386 case EXTATTR_NAMESPACE_USER:
5387 return (VOP_ACCESS(vp, accmode, cred, td));
5393 #ifdef DEBUG_VFS_LOCKS
5395 * This only exists to suppress warnings from unlocked specfs accesses. It is
5396 * no longer ok to have an unlocked VFS.
5398 #define IGNORE_LOCK(vp) (KERNEL_PANICKED() || (vp) == NULL || \
5399 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
5401 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
5402 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
5403 "Drop into debugger on lock violation");
5405 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
5406 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
5407 0, "Check for interlock across VOPs");
5409 int vfs_badlock_print = 1; /* Print lock violations. */
5410 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
5411 0, "Print lock violations");
5413 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */
5414 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode,
5415 0, "Print vnode details on lock violations");
5418 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
5419 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
5420 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
5424 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
5428 if (vfs_badlock_backtrace)
5431 if (vfs_badlock_vnode)
5432 vn_printf(vp, "vnode ");
5433 if (vfs_badlock_print)
5434 printf("%s: %p %s\n", str, (void *)vp, msg);
5435 if (vfs_badlock_ddb)
5436 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
5440 assert_vi_locked(struct vnode *vp, const char *str)
5443 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
5444 vfs_badlock("interlock is not locked but should be", str, vp);
5448 assert_vi_unlocked(struct vnode *vp, const char *str)
5451 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
5452 vfs_badlock("interlock is locked but should not be", str, vp);
5456 assert_vop_locked(struct vnode *vp, const char *str)
5460 if (!IGNORE_LOCK(vp)) {
5461 locked = VOP_ISLOCKED(vp);
5462 if (locked == 0 || locked == LK_EXCLOTHER)
5463 vfs_badlock("is not locked but should be", str, vp);
5468 assert_vop_unlocked(struct vnode *vp, const char *str)
5471 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
5472 vfs_badlock("is locked but should not be", str, vp);
5476 assert_vop_elocked(struct vnode *vp, const char *str)
5479 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
5480 vfs_badlock("is not exclusive locked but should be", str, vp);
5482 #endif /* DEBUG_VFS_LOCKS */
5485 vop_rename_fail(struct vop_rename_args *ap)
5488 if (ap->a_tvp != NULL)
5490 if (ap->a_tdvp == ap->a_tvp)
5499 vop_rename_pre(void *ap)
5501 struct vop_rename_args *a = ap;
5503 #ifdef DEBUG_VFS_LOCKS
5505 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
5506 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
5507 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
5508 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
5510 /* Check the source (from). */
5511 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
5512 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
5513 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
5514 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
5515 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
5517 /* Check the target. */
5519 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
5520 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
5523 * It may be tempting to add vn_seqc_write_begin/end calls here and
5524 * in vop_rename_post but that's not going to work out since some
5525 * filesystems relookup vnodes mid-rename. This is probably a bug.
5527 * For now filesystems are expected to do the relevant calls after they
5528 * decide what vnodes to operate on.
5530 if (a->a_tdvp != a->a_fdvp)
5532 if (a->a_tvp != a->a_fvp)
5539 #ifdef DEBUG_VFS_LOCKS
5541 vop_strategy_pre(void *ap)
5543 struct vop_strategy_args *a;
5550 * Cluster ops lock their component buffers but not the IO container.
5552 if ((bp->b_flags & B_CLUSTER) != 0)
5555 if (!KERNEL_PANICKED() && !BUF_ISLOCKED(bp)) {
5556 if (vfs_badlock_print)
5558 "VOP_STRATEGY: bp is not locked but should be\n");
5559 if (vfs_badlock_ddb)
5560 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
5565 vop_lock_pre(void *ap)
5567 struct vop_lock1_args *a = ap;
5569 if ((a->a_flags & LK_INTERLOCK) == 0)
5570 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
5572 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
5576 vop_lock_post(void *ap, int rc)
5578 struct vop_lock1_args *a = ap;
5580 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
5581 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
5582 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
5586 vop_unlock_pre(void *ap)
5588 struct vop_unlock_args *a = ap;
5590 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
5594 vop_need_inactive_pre(void *ap)
5596 struct vop_need_inactive_args *a = ap;
5598 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5602 vop_need_inactive_post(void *ap, int rc)
5604 struct vop_need_inactive_args *a = ap;
5606 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5611 vop_create_pre(void *ap)
5613 struct vop_create_args *a;
5618 vn_seqc_write_begin(dvp);
5622 vop_create_post(void *ap, int rc)
5624 struct vop_create_args *a;
5629 vn_seqc_write_end(dvp);
5631 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5635 vop_whiteout_pre(void *ap)
5637 struct vop_whiteout_args *a;
5642 vn_seqc_write_begin(dvp);
5646 vop_whiteout_post(void *ap, int rc)
5648 struct vop_whiteout_args *a;
5653 vn_seqc_write_end(dvp);
5657 vop_deleteextattr_pre(void *ap)
5659 struct vop_deleteextattr_args *a;
5664 vn_seqc_write_begin(vp);
5668 vop_deleteextattr_post(void *ap, int rc)
5670 struct vop_deleteextattr_args *a;
5675 vn_seqc_write_end(vp);
5677 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5681 vop_link_pre(void *ap)
5683 struct vop_link_args *a;
5684 struct vnode *vp, *tdvp;
5689 vn_seqc_write_begin(vp);
5690 vn_seqc_write_begin(tdvp);
5694 vop_link_post(void *ap, int rc)
5696 struct vop_link_args *a;
5697 struct vnode *vp, *tdvp;
5702 vn_seqc_write_end(vp);
5703 vn_seqc_write_end(tdvp);
5705 VFS_KNOTE_LOCKED(vp, NOTE_LINK);
5706 VFS_KNOTE_LOCKED(tdvp, NOTE_WRITE);
5711 vop_mkdir_pre(void *ap)
5713 struct vop_mkdir_args *a;
5718 vn_seqc_write_begin(dvp);
5722 vop_mkdir_post(void *ap, int rc)
5724 struct vop_mkdir_args *a;
5729 vn_seqc_write_end(dvp);
5731 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE | NOTE_LINK);
5735 vop_mknod_pre(void *ap)
5737 struct vop_mknod_args *a;
5742 vn_seqc_write_begin(dvp);
5746 vop_mknod_post(void *ap, int rc)
5748 struct vop_mknod_args *a;
5753 vn_seqc_write_end(dvp);
5755 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5759 vop_reclaim_post(void *ap, int rc)
5761 struct vop_reclaim_args *a;
5766 ASSERT_VOP_IN_SEQC(vp);
5768 VFS_KNOTE_LOCKED(vp, NOTE_REVOKE);
5772 vop_remove_pre(void *ap)
5774 struct vop_remove_args *a;
5775 struct vnode *dvp, *vp;
5780 vn_seqc_write_begin(dvp);
5781 vn_seqc_write_begin(vp);
5785 vop_remove_post(void *ap, int rc)
5787 struct vop_remove_args *a;
5788 struct vnode *dvp, *vp;
5793 vn_seqc_write_end(dvp);
5794 vn_seqc_write_end(vp);
5796 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5797 VFS_KNOTE_LOCKED(vp, NOTE_DELETE);
5802 vop_rename_post(void *ap, int rc)
5804 struct vop_rename_args *a = ap;
5809 if (a->a_fdvp == a->a_tdvp) {
5810 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR)
5812 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5813 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5815 hint |= NOTE_EXTEND;
5816 if (a->a_fvp->v_type == VDIR)
5818 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5820 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL &&
5821 a->a_tvp->v_type == VDIR)
5823 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5826 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
5828 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
5830 if (a->a_tdvp != a->a_fdvp)
5832 if (a->a_tvp != a->a_fvp)
5840 vop_rmdir_pre(void *ap)
5842 struct vop_rmdir_args *a;
5843 struct vnode *dvp, *vp;
5848 vn_seqc_write_begin(dvp);
5849 vn_seqc_write_begin(vp);
5853 vop_rmdir_post(void *ap, int rc)
5855 struct vop_rmdir_args *a;
5856 struct vnode *dvp, *vp;
5861 vn_seqc_write_end(dvp);
5862 vn_seqc_write_end(vp);
5864 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE | NOTE_LINK);
5865 VFS_KNOTE_LOCKED(vp, NOTE_DELETE);
5870 vop_setattr_pre(void *ap)
5872 struct vop_setattr_args *a;
5877 vn_seqc_write_begin(vp);
5881 vop_setattr_post(void *ap, int rc)
5883 struct vop_setattr_args *a;
5888 vn_seqc_write_end(vp);
5890 VFS_KNOTE_LOCKED(vp, NOTE_ATTRIB);
5894 vop_setacl_pre(void *ap)
5896 struct vop_setacl_args *a;
5901 vn_seqc_write_begin(vp);
5905 vop_setacl_post(void *ap, int rc __unused)
5907 struct vop_setacl_args *a;
5912 vn_seqc_write_end(vp);
5916 vop_setextattr_pre(void *ap)
5918 struct vop_setextattr_args *a;
5923 vn_seqc_write_begin(vp);
5927 vop_setextattr_post(void *ap, int rc)
5929 struct vop_setextattr_args *a;
5934 vn_seqc_write_end(vp);
5936 VFS_KNOTE_LOCKED(vp, NOTE_ATTRIB);
5940 vop_symlink_pre(void *ap)
5942 struct vop_symlink_args *a;
5947 vn_seqc_write_begin(dvp);
5951 vop_symlink_post(void *ap, int rc)
5953 struct vop_symlink_args *a;
5958 vn_seqc_write_end(dvp);
5960 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5964 vop_open_post(void *ap, int rc)
5966 struct vop_open_args *a = ap;
5969 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN);
5973 vop_close_post(void *ap, int rc)
5975 struct vop_close_args *a = ap;
5977 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */
5978 !VN_IS_DOOMED(a->a_vp))) {
5979 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ?
5980 NOTE_CLOSE_WRITE : NOTE_CLOSE);
5985 vop_read_post(void *ap, int rc)
5987 struct vop_read_args *a = ap;
5990 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5994 vop_readdir_post(void *ap, int rc)
5996 struct vop_readdir_args *a = ap;
5999 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
6002 static struct knlist fs_knlist;
6005 vfs_event_init(void *arg)
6007 knlist_init_mtx(&fs_knlist, NULL);
6009 /* XXX - correct order? */
6010 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
6013 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
6016 KNOTE_UNLOCKED(&fs_knlist, event);
6019 static int filt_fsattach(struct knote *kn);
6020 static void filt_fsdetach(struct knote *kn);
6021 static int filt_fsevent(struct knote *kn, long hint);
6023 struct filterops fs_filtops = {
6025 .f_attach = filt_fsattach,
6026 .f_detach = filt_fsdetach,
6027 .f_event = filt_fsevent
6031 filt_fsattach(struct knote *kn)
6034 kn->kn_flags |= EV_CLEAR;
6035 knlist_add(&fs_knlist, kn, 0);
6040 filt_fsdetach(struct knote *kn)
6043 knlist_remove(&fs_knlist, kn, 0);
6047 filt_fsevent(struct knote *kn, long hint)
6050 kn->kn_fflags |= hint;
6051 return (kn->kn_fflags != 0);
6055 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
6061 error = SYSCTL_IN(req, &vc, sizeof(vc));
6064 if (vc.vc_vers != VFS_CTL_VERS1)
6066 mp = vfs_getvfs(&vc.vc_fsid);
6069 /* ensure that a specific sysctl goes to the right filesystem. */
6070 if (strcmp(vc.vc_fstypename, "*") != 0 &&
6071 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
6075 VCTLTOREQ(&vc, req);
6076 error = VFS_SYSCTL(mp, vc.vc_op, req);
6081 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | CTLFLAG_WR,
6082 NULL, 0, sysctl_vfs_ctl, "",
6086 * Function to initialize a va_filerev field sensibly.
6087 * XXX: Wouldn't a random number make a lot more sense ??
6090 init_va_filerev(void)
6095 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
6098 static int filt_vfsread(struct knote *kn, long hint);
6099 static int filt_vfswrite(struct knote *kn, long hint);
6100 static int filt_vfsvnode(struct knote *kn, long hint);
6101 static void filt_vfsdetach(struct knote *kn);
6102 static struct filterops vfsread_filtops = {
6104 .f_detach = filt_vfsdetach,
6105 .f_event = filt_vfsread
6107 static struct filterops vfswrite_filtops = {
6109 .f_detach = filt_vfsdetach,
6110 .f_event = filt_vfswrite
6112 static struct filterops vfsvnode_filtops = {
6114 .f_detach = filt_vfsdetach,
6115 .f_event = filt_vfsvnode
6119 vfs_knllock(void *arg)
6121 struct vnode *vp = arg;
6123 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
6127 vfs_knlunlock(void *arg)
6129 struct vnode *vp = arg;
6135 vfs_knl_assert_locked(void *arg)
6137 #ifdef DEBUG_VFS_LOCKS
6138 struct vnode *vp = arg;
6140 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
6145 vfs_knl_assert_unlocked(void *arg)
6147 #ifdef DEBUG_VFS_LOCKS
6148 struct vnode *vp = arg;
6150 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
6155 vfs_kqfilter(struct vop_kqfilter_args *ap)
6157 struct vnode *vp = ap->a_vp;
6158 struct knote *kn = ap->a_kn;
6161 switch (kn->kn_filter) {
6163 kn->kn_fop = &vfsread_filtops;
6166 kn->kn_fop = &vfswrite_filtops;
6169 kn->kn_fop = &vfsvnode_filtops;
6175 kn->kn_hook = (caddr_t)vp;
6178 if (vp->v_pollinfo == NULL)
6180 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
6182 knlist_add(knl, kn, 0);
6188 * Detach knote from vnode
6191 filt_vfsdetach(struct knote *kn)
6193 struct vnode *vp = (struct vnode *)kn->kn_hook;
6195 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
6196 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
6202 filt_vfsread(struct knote *kn, long hint)
6204 struct vnode *vp = (struct vnode *)kn->kn_hook;
6209 * filesystem is gone, so set the EOF flag and schedule
6210 * the knote for deletion.
6212 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
6214 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
6219 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
6223 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
6224 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
6231 filt_vfswrite(struct knote *kn, long hint)
6233 struct vnode *vp = (struct vnode *)kn->kn_hook;
6238 * filesystem is gone, so set the EOF flag and schedule
6239 * the knote for deletion.
6241 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
6242 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
6250 filt_vfsvnode(struct knote *kn, long hint)
6252 struct vnode *vp = (struct vnode *)kn->kn_hook;
6256 if (kn->kn_sfflags & hint)
6257 kn->kn_fflags |= hint;
6258 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
6259 kn->kn_flags |= EV_EOF;
6263 res = (kn->kn_fflags != 0);
6269 * Returns whether the directory is empty or not.
6270 * If it is empty, the return value is 0; otherwise
6271 * the return value is an error value (which may
6275 vfs_emptydir(struct vnode *vp)
6279 struct dirent *dirent, *dp, *endp;
6285 ASSERT_VOP_LOCKED(vp, "vfs_emptydir");
6287 dirent = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK);
6288 iov.iov_base = dirent;
6289 iov.iov_len = sizeof(struct dirent);
6294 uio.uio_resid = sizeof(struct dirent);
6295 uio.uio_segflg = UIO_SYSSPACE;
6296 uio.uio_rw = UIO_READ;
6297 uio.uio_td = curthread;
6299 while (eof == 0 && error == 0) {
6300 error = VOP_READDIR(vp, &uio, curthread->td_ucred, &eof,
6304 endp = (void *)((uint8_t *)dirent +
6305 sizeof(struct dirent) - uio.uio_resid);
6306 for (dp = dirent; dp < endp;
6307 dp = (void *)((uint8_t *)dp + GENERIC_DIRSIZ(dp))) {
6308 if (dp->d_type == DT_WHT)
6310 if (dp->d_namlen == 0)
6312 if (dp->d_type != DT_DIR &&
6313 dp->d_type != DT_UNKNOWN) {
6317 if (dp->d_namlen > 2) {
6321 if (dp->d_namlen == 1 &&
6322 dp->d_name[0] != '.') {
6326 if (dp->d_namlen == 2 &&
6327 dp->d_name[1] != '.') {
6331 uio.uio_resid = sizeof(struct dirent);
6334 free(dirent, M_TEMP);
6339 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
6343 if (dp->d_reclen > ap->a_uio->uio_resid)
6344 return (ENAMETOOLONG);
6345 error = uiomove(dp, dp->d_reclen, ap->a_uio);
6347 if (ap->a_ncookies != NULL) {
6348 if (ap->a_cookies != NULL)
6349 free(ap->a_cookies, M_TEMP);
6350 ap->a_cookies = NULL;
6351 *ap->a_ncookies = 0;
6355 if (ap->a_ncookies == NULL)
6358 KASSERT(ap->a_cookies,
6359 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
6361 *ap->a_cookies = realloc(*ap->a_cookies,
6362 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
6363 (*ap->a_cookies)[*ap->a_ncookies] = off;
6364 *ap->a_ncookies += 1;
6369 * The purpose of this routine is to remove granularity from accmode_t,
6370 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
6371 * VADMIN and VAPPEND.
6373 * If it returns 0, the caller is supposed to continue with the usual
6374 * access checks using 'accmode' as modified by this routine. If it
6375 * returns nonzero value, the caller is supposed to return that value
6378 * Note that after this routine runs, accmode may be zero.
6381 vfs_unixify_accmode(accmode_t *accmode)
6384 * There is no way to specify explicit "deny" rule using
6385 * file mode or POSIX.1e ACLs.
6387 if (*accmode & VEXPLICIT_DENY) {
6393 * None of these can be translated into usual access bits.
6394 * Also, the common case for NFSv4 ACLs is to not contain
6395 * either of these bits. Caller should check for VWRITE
6396 * on the containing directory instead.
6398 if (*accmode & (VDELETE_CHILD | VDELETE))
6401 if (*accmode & VADMIN_PERMS) {
6402 *accmode &= ~VADMIN_PERMS;
6407 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
6408 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
6410 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
6416 * Clear out a doomed vnode (if any) and replace it with a new one as long
6417 * as the fs is not being unmounted. Return the root vnode to the caller.
6419 static int __noinline
6420 vfs_cache_root_fallback(struct mount *mp, int flags, struct vnode **vpp)
6426 if (mp->mnt_rootvnode != NULL) {
6428 vp = mp->mnt_rootvnode;
6430 if (!VN_IS_DOOMED(vp)) {
6433 error = vn_lock(vp, flags);
6442 * Clear the old one.
6444 mp->mnt_rootvnode = NULL;
6448 vfs_op_barrier_wait(mp);
6452 error = VFS_CACHEDROOT(mp, flags, vpp);
6455 if (mp->mnt_vfs_ops == 0) {
6457 if (mp->mnt_vfs_ops != 0) {
6461 if (mp->mnt_rootvnode == NULL) {
6463 mp->mnt_rootvnode = *vpp;
6465 if (mp->mnt_rootvnode != *vpp) {
6466 if (!VN_IS_DOOMED(mp->mnt_rootvnode)) {
6467 panic("%s: mismatch between vnode returned "
6468 " by VFS_CACHEDROOT and the one cached "
6470 __func__, *vpp, mp->mnt_rootvnode);
6480 vfs_cache_root(struct mount *mp, int flags, struct vnode **vpp)
6485 if (!vfs_op_thread_enter(mp))
6486 return (vfs_cache_root_fallback(mp, flags, vpp));
6487 vp = atomic_load_ptr(&mp->mnt_rootvnode);
6488 if (vp == NULL || VN_IS_DOOMED(vp)) {
6489 vfs_op_thread_exit(mp);
6490 return (vfs_cache_root_fallback(mp, flags, vpp));
6493 vfs_op_thread_exit(mp);
6494 error = vn_lock(vp, flags);
6497 return (vfs_cache_root_fallback(mp, flags, vpp));
6504 vfs_cache_root_clear(struct mount *mp)
6509 * ops > 0 guarantees there is nobody who can see this vnode
6511 MPASS(mp->mnt_vfs_ops > 0);
6512 vp = mp->mnt_rootvnode;
6514 vn_seqc_write_begin(vp);
6515 mp->mnt_rootvnode = NULL;
6520 vfs_cache_root_set(struct mount *mp, struct vnode *vp)
6523 MPASS(mp->mnt_vfs_ops > 0);
6525 mp->mnt_rootvnode = vp;
6529 * These are helper functions for filesystems to traverse all
6530 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
6532 * This interface replaces MNT_VNODE_FOREACH.
6536 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
6541 kern_yield(PRI_USER);
6543 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6544 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL;
6545 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
6546 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */
6547 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp))
6550 if (VN_IS_DOOMED(vp)) {
6557 __mnt_vnode_markerfree_all(mvp, mp);
6558 /* MNT_IUNLOCK(mp); -- done in above function */
6559 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
6562 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
6563 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
6569 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
6573 *mvp = vn_alloc_marker(mp);
6577 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
6578 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */
6579 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp))
6582 if (VN_IS_DOOMED(vp)) {
6591 vn_free_marker(*mvp);
6595 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
6601 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
6609 mtx_assert(MNT_MTX(mp), MA_OWNED);
6611 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6612 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
6615 vn_free_marker(*mvp);
6620 * These are helper functions for filesystems to traverse their
6621 * lazy vnodes. See MNT_VNODE_FOREACH_LAZY() in sys/mount.h
6624 mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp)
6627 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6632 vn_free_marker(*mvp);
6637 * Relock the mp mount vnode list lock with the vp vnode interlock in the
6638 * conventional lock order during mnt_vnode_next_lazy iteration.
6640 * On entry, the mount vnode list lock is held and the vnode interlock is not.
6641 * The list lock is dropped and reacquired. On success, both locks are held.
6642 * On failure, the mount vnode list lock is held but the vnode interlock is
6643 * not, and the procedure may have yielded.
6646 mnt_vnode_next_lazy_relock(struct vnode *mvp, struct mount *mp,
6650 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER &&
6651 TAILQ_NEXT(mvp, v_lazylist) != NULL, mvp,
6652 ("%s: bad marker", __func__));
6653 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp,
6654 ("%s: inappropriate vnode", __func__));
6655 ASSERT_VI_UNLOCKED(vp, __func__);
6656 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6658 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, mvp, v_lazylist);
6659 TAILQ_INSERT_BEFORE(vp, mvp, v_lazylist);
6662 * Note we may be racing against vdrop which transitioned the hold
6663 * count to 0 and now waits for the ->mnt_listmtx lock. This is fine,
6664 * if we are the only user after we get the interlock we will just
6668 mtx_unlock(&mp->mnt_listmtx);
6670 if (VN_IS_DOOMED(vp)) {
6671 VNPASS((vp->v_mflag & VMP_LAZYLIST) == 0, vp);
6674 VNPASS(vp->v_mflag & VMP_LAZYLIST, vp);
6676 * There is nothing to do if we are the last user.
6678 if (!refcount_release_if_not_last(&vp->v_holdcnt))
6680 mtx_lock(&mp->mnt_listmtx);
6685 mtx_lock(&mp->mnt_listmtx);
6689 static struct vnode *
6690 mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6695 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6696 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6698 vp = TAILQ_NEXT(*mvp, v_lazylist);
6699 while (vp != NULL) {
6700 if (vp->v_type == VMARKER) {
6701 vp = TAILQ_NEXT(vp, v_lazylist);
6705 * See if we want to process the vnode. Note we may encounter a
6706 * long string of vnodes we don't care about and hog the list
6707 * as a result. Check for it and requeue the marker.
6709 VNPASS(!VN_IS_DOOMED(vp), vp);
6710 if (!cb(vp, cbarg)) {
6711 if (!should_yield()) {
6712 vp = TAILQ_NEXT(vp, v_lazylist);
6715 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp,
6717 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp,
6719 mtx_unlock(&mp->mnt_listmtx);
6720 kern_yield(PRI_USER);
6721 mtx_lock(&mp->mnt_listmtx);
6725 * Try-lock because this is the wrong lock order.
6727 if (!VI_TRYLOCK(vp) &&
6728 !mnt_vnode_next_lazy_relock(*mvp, mp, vp))
6730 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
6731 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
6732 ("alien vnode on the lazy list %p %p", vp, mp));
6733 VNPASS(vp->v_mount == mp, vp);
6734 VNPASS(!VN_IS_DOOMED(vp), vp);
6737 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist);
6739 /* Check if we are done */
6741 mtx_unlock(&mp->mnt_listmtx);
6742 mnt_vnode_markerfree_lazy(mvp, mp);
6745 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, v_lazylist);
6746 mtx_unlock(&mp->mnt_listmtx);
6747 ASSERT_VI_LOCKED(vp, "lazy iter");
6752 __mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6757 kern_yield(PRI_USER);
6758 mtx_lock(&mp->mnt_listmtx);
6759 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg));
6763 __mnt_vnode_first_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6768 if (TAILQ_EMPTY(&mp->mnt_lazyvnodelist))
6771 *mvp = vn_alloc_marker(mp);
6776 mtx_lock(&mp->mnt_listmtx);
6777 vp = TAILQ_FIRST(&mp->mnt_lazyvnodelist);
6779 mtx_unlock(&mp->mnt_listmtx);
6780 mnt_vnode_markerfree_lazy(mvp, mp);
6783 TAILQ_INSERT_BEFORE(vp, *mvp, v_lazylist);
6784 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg));
6788 __mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp)
6794 mtx_lock(&mp->mnt_listmtx);
6795 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist);
6796 mtx_unlock(&mp->mnt_listmtx);
6797 mnt_vnode_markerfree_lazy(mvp, mp);
6801 vn_dir_check_exec(struct vnode *vp, struct componentname *cnp)
6804 if ((cnp->cn_flags & NOEXECCHECK) != 0) {
6805 cnp->cn_flags &= ~NOEXECCHECK;
6809 return (VOP_ACCESS(vp, VEXEC, cnp->cn_cred, cnp->cn_thread));
6813 vn_seqc_write_begin_locked(struct vnode *vp)
6816 ASSERT_VI_LOCKED(vp, __func__);
6817 VNPASS(vp->v_holdcnt > 0, vp);
6818 VNPASS(vp->v_seqc_users >= 0, vp);
6820 if (vp->v_seqc_users == 1)
6821 seqc_sleepable_write_begin(&vp->v_seqc);
6825 vn_seqc_write_begin(struct vnode *vp)
6829 vn_seqc_write_begin_locked(vp);
6834 vn_seqc_write_end_locked(struct vnode *vp)
6837 ASSERT_VI_LOCKED(vp, __func__);
6838 VNPASS(vp->v_seqc_users > 0, vp);
6840 if (vp->v_seqc_users == 0)
6841 seqc_sleepable_write_end(&vp->v_seqc);
6845 vn_seqc_write_end(struct vnode *vp)
6849 vn_seqc_write_end_locked(vp);