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, 0);
668 uma_zone_set_smr(vnode_zone, vfs_smr);
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_abort(struct vnode *vp, enum vgetstate vs)
2929 __assert_unreachable();
2934 vget(struct vnode *vp, int flags, struct thread *td)
2938 MPASS(td == curthread);
2941 return (vget_finish(vp, flags, vs));
2944 static void __noinline
2945 vget_finish_vchr(struct vnode *vp)
2948 VNASSERT(vp->v_type == VCHR, vp, ("type != VCHR)"));
2951 * See the comment in vget_finish before usecount bump.
2953 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2955 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
2956 VNASSERT(old > 0, vp, ("%s: wrong hold count %d", __func__, old));
2958 refcount_release(&vp->v_holdcnt);
2964 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2966 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
2967 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old));
2969 refcount_release(&vp->v_holdcnt);
2974 v_incr_devcount(vp);
2975 refcount_acquire(&vp->v_usecount);
2980 vget_finish(struct vnode *vp, int flags, enum vgetstate vs)
2984 if ((flags & LK_INTERLOCK) != 0)
2985 ASSERT_VI_LOCKED(vp, __func__);
2987 ASSERT_VI_UNLOCKED(vp, __func__);
2988 VNPASS(vs == VGET_HOLDCNT || vs == VGET_USECOUNT, vp);
2989 VNPASS(vp->v_holdcnt > 0, vp);
2990 VNPASS(vs == VGET_HOLDCNT || vp->v_usecount > 0, vp);
2992 error = vn_lock(vp, flags);
2993 if (__predict_false(error != 0)) {
2995 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
3000 vget_finish_ref(vp, vs);
3005 vget_finish_ref(struct vnode *vp, enum vgetstate vs)
3009 VNPASS(vs == VGET_HOLDCNT || vs == VGET_USECOUNT, vp);
3010 VNPASS(vp->v_holdcnt > 0, vp);
3011 VNPASS(vs == VGET_HOLDCNT || vp->v_usecount > 0, vp);
3013 if (vs == VGET_USECOUNT)
3016 if (__predict_false(vp->v_type == VCHR)) {
3017 vget_finish_vchr(vp);
3022 * We hold the vnode. If the usecount is 0 it will be utilized to keep
3023 * the vnode around. Otherwise someone else lended their hold count and
3024 * we have to drop ours.
3026 old = atomic_fetchadd_int(&vp->v_usecount, 1);
3027 VNASSERT(old >= 0, vp, ("%s: wrong use count %d", __func__, old));
3030 old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
3031 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old));
3033 refcount_release(&vp->v_holdcnt);
3039 * Increase the reference (use) and hold count of a vnode.
3040 * This will also remove the vnode from the free list if it is presently free.
3042 static void __noinline
3043 vref_vchr(struct vnode *vp, bool interlock)
3047 * See the comment in vget_finish before usecount bump.
3050 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
3051 VNODE_REFCOUNT_FENCE_ACQ();
3052 VNASSERT(vp->v_holdcnt > 0, vp,
3053 ("%s: active vnode not held", __func__));
3058 * By the time we get here the vnode might have been doomed, at
3059 * which point the 0->1 use count transition is no longer
3060 * protected by the interlock. Since it can't bounce back to
3061 * VCHR and requires vref semantics, punt it back
3063 if (__predict_false(vp->v_type == VBAD)) {
3069 VNASSERT(vp->v_type == VCHR, vp, ("type != VCHR)"));
3070 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
3071 VNODE_REFCOUNT_FENCE_ACQ();
3072 VNASSERT(vp->v_holdcnt > 0, vp,
3073 ("%s: active vnode not held", __func__));
3079 v_incr_devcount(vp);
3080 refcount_acquire(&vp->v_usecount);
3087 vref(struct vnode *vp)
3091 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3092 if (__predict_false(vp->v_type == VCHR)) {
3093 vref_vchr(vp, false);
3097 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
3098 VNODE_REFCOUNT_FENCE_ACQ();
3099 VNASSERT(vp->v_holdcnt > 0, vp,
3100 ("%s: active vnode not held", __func__));
3105 * See the comment in vget_finish.
3107 old = atomic_fetchadd_int(&vp->v_usecount, 1);
3108 VNASSERT(old >= 0, vp, ("%s: wrong use count %d", __func__, old));
3111 old = atomic_fetchadd_int(&vp->v_holdcnt, -1);
3112 VNASSERT(old > 1, vp, ("%s: wrong hold count %d", __func__, old));
3114 refcount_release(&vp->v_holdcnt);
3120 vrefl(struct vnode *vp)
3123 ASSERT_VI_LOCKED(vp, __func__);
3124 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3125 if (__predict_false(vp->v_type == VCHR)) {
3126 vref_vchr(vp, true);
3133 vrefact(struct vnode *vp)
3136 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3138 int old = atomic_fetchadd_int(&vp->v_usecount, 1);
3139 VNASSERT(old > 0, vp, ("%s: wrong use count %d", __func__, old));
3141 refcount_acquire(&vp->v_usecount);
3146 vrefactn(struct vnode *vp, u_int n)
3149 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3151 int old = atomic_fetchadd_int(&vp->v_usecount, n);
3152 VNASSERT(old > 0, vp, ("%s: wrong use count %d", __func__, old));
3154 atomic_add_int(&vp->v_usecount, n);
3159 * Return reference count of a vnode.
3161 * The results of this call are only guaranteed when some mechanism is used to
3162 * stop other processes from gaining references to the vnode. This may be the
3163 * case if the caller holds the only reference. This is also useful when stale
3164 * data is acceptable as race conditions may be accounted for by some other
3168 vrefcnt(struct vnode *vp)
3171 return (vp->v_usecount);
3175 vlazy(struct vnode *vp)
3179 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__));
3181 if ((vp->v_mflag & VMP_LAZYLIST) != 0)
3184 * We may get here for inactive routines after the vnode got doomed.
3186 if (VN_IS_DOOMED(vp))
3189 mtx_lock(&mp->mnt_listmtx);
3190 if ((vp->v_mflag & VMP_LAZYLIST) == 0) {
3191 vp->v_mflag |= VMP_LAZYLIST;
3192 TAILQ_INSERT_TAIL(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3193 mp->mnt_lazyvnodelistsize++;
3195 mtx_unlock(&mp->mnt_listmtx);
3199 * This routine is only meant to be called from vgonel prior to dooming
3203 vunlazy_gone(struct vnode *vp)
3207 ASSERT_VOP_ELOCKED(vp, __func__);
3208 ASSERT_VI_LOCKED(vp, __func__);
3209 VNPASS(!VN_IS_DOOMED(vp), vp);
3211 if (vp->v_mflag & VMP_LAZYLIST) {
3213 mtx_lock(&mp->mnt_listmtx);
3214 VNPASS(vp->v_mflag & VMP_LAZYLIST, vp);
3215 vp->v_mflag &= ~VMP_LAZYLIST;
3216 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3217 mp->mnt_lazyvnodelistsize--;
3218 mtx_unlock(&mp->mnt_listmtx);
3223 vdefer_inactive(struct vnode *vp)
3226 ASSERT_VI_LOCKED(vp, __func__);
3227 VNASSERT(vp->v_holdcnt > 0, vp,
3228 ("%s: vnode without hold count", __func__));
3229 if (VN_IS_DOOMED(vp)) {
3233 if (vp->v_iflag & VI_DEFINACT) {
3234 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count"));
3238 if (vp->v_usecount > 0) {
3239 vp->v_iflag &= ~VI_OWEINACT;
3244 vp->v_iflag |= VI_DEFINACT;
3246 counter_u64_add(deferred_inact, 1);
3250 vdefer_inactive_unlocked(struct vnode *vp)
3254 if ((vp->v_iflag & VI_OWEINACT) == 0) {
3258 vdefer_inactive(vp);
3261 enum vput_op { VRELE, VPUT, VUNREF };
3264 * Handle ->v_usecount transitioning to 0.
3266 * By releasing the last usecount we take ownership of the hold count which
3267 * provides liveness of the vnode, meaning we have to vdrop.
3269 * If the vnode is of type VCHR we may need to decrement si_usecount, see
3270 * v_decr_devcount for details.
3272 * For all vnodes we may need to perform inactive processing. It requires an
3273 * exclusive lock on the vnode, while it is legal to call here with only a
3274 * shared lock (or no locks). If locking the vnode in an expected manner fails,
3275 * inactive processing gets deferred to the syncer.
3277 * XXX Some filesystems pass in an exclusively locked vnode and strongly depend
3278 * on the lock being held all the way until VOP_INACTIVE. This in particular
3279 * happens with UFS which adds half-constructed vnodes to the hash, where they
3280 * can be found by other code.
3283 vput_final(struct vnode *vp, enum vput_op func)
3288 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3289 VNPASS(vp->v_holdcnt > 0, vp);
3292 if (__predict_false(vp->v_type == VCHR && func != VRELE))
3293 v_decr_devcount(vp);
3296 * By the time we got here someone else might have transitioned
3297 * the count back to > 0.
3299 if (vp->v_usecount > 0)
3303 * If the vnode is doomed vgone already performed inactive processing
3306 if (VN_IS_DOOMED(vp))
3309 if (__predict_true(VOP_NEED_INACTIVE(vp) == 0))
3312 if (vp->v_iflag & VI_DOINGINACT)
3316 * Locking operations here will drop the interlock and possibly the
3317 * vnode lock, opening a window where the vnode can get doomed all the
3318 * while ->v_usecount is 0. Set VI_OWEINACT to let vgone know to
3321 vp->v_iflag |= VI_OWEINACT;
3322 want_unlock = false;
3326 switch (VOP_ISLOCKED(vp)) {
3332 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
3337 * The lock has at least one sharer, but we have no way
3338 * to conclude whether this is us. Play it safe and
3347 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
3348 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
3354 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
3355 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
3366 vdefer_inactive(vp);
3376 * Decrement ->v_usecount for a vnode.
3378 * Releasing the last use count requires additional processing, see vput_final
3379 * above for details.
3381 * Note that releasing use count without the vnode lock requires special casing
3382 * for VCHR, see v_decr_devcount for details.
3384 * Comment above each variant denotes lock state on entry and exit.
3387 static void __noinline
3388 vrele_vchr(struct vnode *vp)
3391 if (refcount_release_if_not_last(&vp->v_usecount))
3394 if (!refcount_release(&vp->v_usecount)) {
3398 v_decr_devcount(vp);
3400 vput_final(vp, VRELE);
3405 * out: same as passed in
3408 vrele(struct vnode *vp)
3411 ASSERT_VI_UNLOCKED(vp, __func__);
3412 if (__predict_false(vp->v_type == VCHR)) {
3416 if (!refcount_release(&vp->v_usecount))
3418 vput_final(vp, VRELE);
3426 vput(struct vnode *vp)
3429 ASSERT_VOP_LOCKED(vp, __func__);
3430 ASSERT_VI_UNLOCKED(vp, __func__);
3431 if (!refcount_release(&vp->v_usecount)) {
3435 vput_final(vp, VPUT);
3443 vunref(struct vnode *vp)
3446 ASSERT_VOP_LOCKED(vp, __func__);
3447 ASSERT_VI_UNLOCKED(vp, __func__);
3448 if (!refcount_release(&vp->v_usecount))
3450 vput_final(vp, VUNREF);
3454 vhold(struct vnode *vp)
3459 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3460 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));
3472 vholdl(struct vnode *vp)
3475 ASSERT_VI_LOCKED(vp, __func__);
3476 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3481 vholdnz(struct vnode *vp)
3484 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3486 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3487 VNASSERT(old > 0 && (old & VHOLD_ALL_FLAGS) == 0, vp,
3488 ("%s: wrong hold count %d", __func__, old));
3490 atomic_add_int(&vp->v_holdcnt, 1);
3495 * Grab a hold count unless the vnode is freed.
3497 * Only use this routine if vfs smr is the only protection you have against
3498 * freeing the vnode.
3500 * The code loops trying to add a hold count as long as the VHOLD_NO_SMR flag
3501 * is not set. After the flag is set the vnode becomes immutable to anyone but
3502 * the thread which managed to set the flag.
3504 * It may be tempting to replace the loop with:
3505 * count = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3506 * if (count & VHOLD_NO_SMR) {
3507 * backpedal and error out;
3510 * However, while this is more performant, it hinders debugging by eliminating
3511 * the previously mentioned invariant.
3514 vhold_smr(struct vnode *vp)
3518 VFS_SMR_ASSERT_ENTERED();
3520 count = atomic_load_int(&vp->v_holdcnt);
3522 if (count & VHOLD_NO_SMR) {
3523 VNASSERT((count & ~VHOLD_NO_SMR) == 0, vp,
3524 ("non-zero hold count with flags %d\n", count));
3528 VNASSERT(count >= 0, vp, ("invalid hold count %d\n", count));
3529 if (atomic_fcmpset_int(&vp->v_holdcnt, &count, count + 1))
3534 static void __noinline
3535 vdbatch_process(struct vdbatch *vd)
3540 mtx_assert(&vd->lock, MA_OWNED);
3541 MPASS(curthread->td_pinned > 0);
3542 MPASS(vd->index == VDBATCH_SIZE);
3544 mtx_lock(&vnode_list_mtx);
3546 freevnodes += vd->freevnodes;
3547 for (i = 0; i < VDBATCH_SIZE; i++) {
3549 TAILQ_REMOVE(&vnode_list, vp, v_vnodelist);
3550 TAILQ_INSERT_TAIL(&vnode_list, vp, v_vnodelist);
3551 MPASS(vp->v_dbatchcpu != NOCPU);
3552 vp->v_dbatchcpu = NOCPU;
3554 mtx_unlock(&vnode_list_mtx);
3556 bzero(vd->tab, sizeof(vd->tab));
3562 vdbatch_enqueue(struct vnode *vp)
3566 ASSERT_VI_LOCKED(vp, __func__);
3567 VNASSERT(!VN_IS_DOOMED(vp), vp,
3568 ("%s: deferring requeue of a doomed vnode", __func__));
3573 if (vp->v_dbatchcpu != NOCPU) {
3581 mtx_lock(&vd->lock);
3582 MPASS(vd->index < VDBATCH_SIZE);
3583 MPASS(vd->tab[vd->index] == NULL);
3585 * A hack: we depend on being pinned so that we know what to put in
3588 vp->v_dbatchcpu = curcpu;
3589 vd->tab[vd->index] = vp;
3592 if (vd->index == VDBATCH_SIZE)
3593 vdbatch_process(vd);
3594 mtx_unlock(&vd->lock);
3599 * This routine must only be called for vnodes which are about to be
3600 * deallocated. Supporting dequeue for arbitrary vndoes would require
3601 * validating that the locked batch matches.
3604 vdbatch_dequeue(struct vnode *vp)
3610 VNASSERT(vp->v_type == VBAD || vp->v_type == VNON, vp,
3611 ("%s: called for a used vnode\n", __func__));
3613 cpu = vp->v_dbatchcpu;
3617 vd = DPCPU_ID_PTR(cpu, vd);
3618 mtx_lock(&vd->lock);
3619 for (i = 0; i < vd->index; i++) {
3620 if (vd->tab[i] != vp)
3622 vp->v_dbatchcpu = NOCPU;
3624 vd->tab[i] = vd->tab[vd->index];
3625 vd->tab[vd->index] = NULL;
3628 mtx_unlock(&vd->lock);
3630 * Either we dequeued the vnode above or the target CPU beat us to it.
3632 MPASS(vp->v_dbatchcpu == NOCPU);
3636 * Drop the hold count of the vnode. If this is the last reference to
3637 * the vnode we place it on the free list unless it has been vgone'd
3638 * (marked VIRF_DOOMED) in which case we will free it.
3640 * Because the vnode vm object keeps a hold reference on the vnode if
3641 * there is at least one resident non-cached page, the vnode cannot
3642 * leave the active list without the page cleanup done.
3645 vdrop_deactivate(struct vnode *vp)
3649 ASSERT_VI_LOCKED(vp, __func__);
3651 * Mark a vnode as free: remove it from its active list
3652 * and put it up for recycling on the freelist.
3654 VNASSERT(!VN_IS_DOOMED(vp), vp,
3655 ("vdrop: returning doomed vnode"));
3656 VNASSERT(vp->v_op != NULL, vp,
3657 ("vdrop: vnode already reclaimed."));
3658 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
3659 ("vnode with VI_OWEINACT set"));
3660 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp,
3661 ("vnode with VI_DEFINACT set"));
3662 if (vp->v_mflag & VMP_LAZYLIST) {
3664 mtx_lock(&mp->mnt_listmtx);
3665 VNASSERT(vp->v_mflag & VMP_LAZYLIST, vp, ("lost VMP_LAZYLIST"));
3667 * Don't remove the vnode from the lazy list if another thread
3668 * has increased the hold count. It may have re-enqueued the
3669 * vnode to the lazy list and is now responsible for its
3672 if (vp->v_holdcnt == 0) {
3673 vp->v_mflag &= ~VMP_LAZYLIST;
3674 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, vp, v_lazylist);
3675 mp->mnt_lazyvnodelistsize--;
3677 mtx_unlock(&mp->mnt_listmtx);
3679 vdbatch_enqueue(vp);
3683 vdrop(struct vnode *vp)
3686 ASSERT_VI_UNLOCKED(vp, __func__);
3687 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3688 if (refcount_release_if_not_last(&vp->v_holdcnt))
3695 vdropl(struct vnode *vp)
3698 ASSERT_VI_LOCKED(vp, __func__);
3699 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3700 if (!refcount_release(&vp->v_holdcnt)) {
3704 if (!VN_IS_DOOMED(vp)) {
3705 vdrop_deactivate(vp);
3709 * We may be racing against vhold_smr.
3711 * If they win we can just pretend we never got this far, they will
3714 if (!atomic_cmpset_int(&vp->v_holdcnt, 0, VHOLD_NO_SMR)) {
3716 * We lost the aforementioned race. Note that any subsequent
3717 * access is invalid as they might have managed to vdropl on
3726 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
3727 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
3730 vinactivef(struct vnode *vp)
3732 struct vm_object *obj;
3734 ASSERT_VOP_ELOCKED(vp, "vinactive");
3735 ASSERT_VI_LOCKED(vp, "vinactive");
3736 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
3737 ("vinactive: recursed on VI_DOINGINACT"));
3738 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3739 vp->v_iflag |= VI_DOINGINACT;
3740 vp->v_iflag &= ~VI_OWEINACT;
3743 * Before moving off the active list, we must be sure that any
3744 * modified pages are converted into the vnode's dirty
3745 * buffers, since these will no longer be checked once the
3746 * vnode is on the inactive list.
3748 * The write-out of the dirty pages is asynchronous. At the
3749 * point that VOP_INACTIVE() is called, there could still be
3750 * pending I/O and dirty pages in the object.
3752 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
3753 vm_object_mightbedirty(obj)) {
3754 VM_OBJECT_WLOCK(obj);
3755 vm_object_page_clean(obj, 0, 0, 0);
3756 VM_OBJECT_WUNLOCK(obj);
3758 VOP_INACTIVE(vp, curthread);
3760 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
3761 ("vinactive: lost VI_DOINGINACT"));
3762 vp->v_iflag &= ~VI_DOINGINACT;
3766 vinactive(struct vnode *vp)
3769 ASSERT_VOP_ELOCKED(vp, "vinactive");
3770 ASSERT_VI_LOCKED(vp, "vinactive");
3771 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3773 if ((vp->v_iflag & VI_OWEINACT) == 0)
3775 if (vp->v_iflag & VI_DOINGINACT)
3777 if (vp->v_usecount > 0) {
3778 vp->v_iflag &= ~VI_OWEINACT;
3785 * Remove any vnodes in the vnode table belonging to mount point mp.
3787 * If FORCECLOSE is not specified, there should not be any active ones,
3788 * return error if any are found (nb: this is a user error, not a
3789 * system error). If FORCECLOSE is specified, detach any active vnodes
3792 * If WRITECLOSE is set, only flush out regular file vnodes open for
3795 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
3797 * `rootrefs' specifies the base reference count for the root vnode
3798 * of this filesystem. The root vnode is considered busy if its
3799 * v_usecount exceeds this value. On a successful return, vflush(, td)
3800 * will call vrele() on the root vnode exactly rootrefs times.
3801 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
3805 static int busyprt = 0; /* print out busy vnodes */
3806 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
3810 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
3812 struct vnode *vp, *mvp, *rootvp = NULL;
3814 int busy = 0, error;
3816 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
3819 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
3820 ("vflush: bad args"));
3822 * Get the filesystem root vnode. We can vput() it
3823 * immediately, since with rootrefs > 0, it won't go away.
3825 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
3826 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
3833 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
3835 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
3838 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3842 * Skip over a vnodes marked VV_SYSTEM.
3844 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
3850 * If WRITECLOSE is set, flush out unlinked but still open
3851 * files (even if open only for reading) and regular file
3852 * vnodes open for writing.
3854 if (flags & WRITECLOSE) {
3855 if (vp->v_object != NULL) {
3856 VM_OBJECT_WLOCK(vp->v_object);
3857 vm_object_page_clean(vp->v_object, 0, 0, 0);
3858 VM_OBJECT_WUNLOCK(vp->v_object);
3860 error = VOP_FSYNC(vp, MNT_WAIT, td);
3864 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3867 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3870 if ((vp->v_type == VNON ||
3871 (error == 0 && vattr.va_nlink > 0)) &&
3872 (vp->v_writecount <= 0 || vp->v_type != VREG)) {
3880 * With v_usecount == 0, all we need to do is clear out the
3881 * vnode data structures and we are done.
3883 * If FORCECLOSE is set, forcibly close the vnode.
3885 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
3891 vn_printf(vp, "vflush: busy vnode ");
3897 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
3899 * If just the root vnode is busy, and if its refcount
3900 * is equal to `rootrefs', then go ahead and kill it.
3903 KASSERT(busy > 0, ("vflush: not busy"));
3904 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
3905 ("vflush: usecount %d < rootrefs %d",
3906 rootvp->v_usecount, rootrefs));
3907 if (busy == 1 && rootvp->v_usecount == rootrefs) {
3908 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
3916 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
3920 for (; rootrefs > 0; rootrefs--)
3926 * Recycle an unused vnode to the front of the free list.
3929 vrecycle(struct vnode *vp)
3934 recycled = vrecyclel(vp);
3940 * vrecycle, with the vp interlock held.
3943 vrecyclel(struct vnode *vp)
3947 ASSERT_VOP_ELOCKED(vp, __func__);
3948 ASSERT_VI_LOCKED(vp, __func__);
3949 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3951 if (vp->v_usecount == 0) {
3959 * Eliminate all activity associated with a vnode
3960 * in preparation for reuse.
3963 vgone(struct vnode *vp)
3971 notify_lowervp_vfs_dummy(struct mount *mp __unused,
3972 struct vnode *lowervp __unused)
3977 * Notify upper mounts about reclaimed or unlinked vnode.
3980 vfs_notify_upper(struct vnode *vp, int event)
3982 static struct vfsops vgonel_vfsops = {
3983 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
3984 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
3986 struct mount *mp, *ump, *mmp;
3991 if (TAILQ_EMPTY(&mp->mnt_uppers))
3994 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
3995 mmp->mnt_op = &vgonel_vfsops;
3996 mmp->mnt_kern_flag |= MNTK_MARKER;
3998 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
3999 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
4000 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
4001 ump = TAILQ_NEXT(ump, mnt_upper_link);
4004 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
4007 case VFS_NOTIFY_UPPER_RECLAIM:
4008 VFS_RECLAIM_LOWERVP(ump, vp);
4010 case VFS_NOTIFY_UPPER_UNLINK:
4011 VFS_UNLINK_LOWERVP(ump, vp);
4014 KASSERT(0, ("invalid event %d", event));
4018 ump = TAILQ_NEXT(mmp, mnt_upper_link);
4019 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
4022 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
4023 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
4024 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
4025 wakeup(&mp->mnt_uppers);
4031 * vgone, with the vp interlock held.
4034 vgonel(struct vnode *vp)
4039 bool active, oweinact;
4041 ASSERT_VOP_ELOCKED(vp, "vgonel");
4042 ASSERT_VI_LOCKED(vp, "vgonel");
4043 VNASSERT(vp->v_holdcnt, vp,
4044 ("vgonel: vp %p has no reference.", vp));
4045 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
4049 * Don't vgonel if we're already doomed.
4051 if (vp->v_irflag & VIRF_DOOMED)
4054 * Paired with freevnode.
4056 vn_seqc_write_begin_locked(vp);
4058 vp->v_irflag |= VIRF_DOOMED;
4061 * Check to see if the vnode is in use. If so, we have to call
4062 * VOP_CLOSE() and VOP_INACTIVE().
4064 active = vp->v_usecount > 0;
4065 oweinact = (vp->v_iflag & VI_OWEINACT) != 0;
4067 * If we need to do inactive VI_OWEINACT will be set.
4069 if (vp->v_iflag & VI_DEFINACT) {
4070 VNASSERT(vp->v_holdcnt > 1, vp, ("lost hold count"));
4071 vp->v_iflag &= ~VI_DEFINACT;
4074 VNASSERT(vp->v_holdcnt > 0, vp, ("vnode without hold count"));
4077 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
4080 * If purging an active vnode, it must be closed and
4081 * deactivated before being reclaimed.
4084 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
4085 if (oweinact || active) {
4090 if (vp->v_type == VSOCK)
4091 vfs_unp_reclaim(vp);
4094 * Clean out any buffers associated with the vnode.
4095 * If the flush fails, just toss the buffers.
4098 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
4099 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
4100 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
4101 while (vinvalbuf(vp, 0, 0, 0) != 0)
4105 BO_LOCK(&vp->v_bufobj);
4106 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
4107 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
4108 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
4109 vp->v_bufobj.bo_clean.bv_cnt == 0,
4110 ("vp %p bufobj not invalidated", vp));
4113 * For VMIO bufobj, BO_DEAD is set later, or in
4114 * vm_object_terminate() after the object's page queue is
4117 object = vp->v_bufobj.bo_object;
4119 vp->v_bufobj.bo_flag |= BO_DEAD;
4120 BO_UNLOCK(&vp->v_bufobj);
4123 * Handle the VM part. Tmpfs handles v_object on its own (the
4124 * OBJT_VNODE check). Nullfs or other bypassing filesystems
4125 * should not touch the object borrowed from the lower vnode
4126 * (the handle check).
4128 if (object != NULL && object->type == OBJT_VNODE &&
4129 object->handle == vp)
4130 vnode_destroy_vobject(vp);
4133 * Reclaim the vnode.
4135 if (VOP_RECLAIM(vp, td))
4136 panic("vgone: cannot reclaim");
4138 vn_finished_secondary_write(mp);
4139 VNASSERT(vp->v_object == NULL, vp,
4140 ("vop_reclaim left v_object vp=%p", vp));
4142 * Clear the advisory locks and wake up waiting threads.
4144 (void)VOP_ADVLOCKPURGE(vp);
4147 * Delete from old mount point vnode list.
4152 * Done with purge, reset to the standard lock and invalidate
4156 vp->v_vnlock = &vp->v_lock;
4157 vp->v_op = &dead_vnodeops;
4162 * Calculate the total number of references to a special device.
4165 vcount(struct vnode *vp)
4170 count = vp->v_rdev->si_usecount;
4176 * Print out a description of a vnode.
4178 static const char * const typename[] =
4179 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
4182 _Static_assert((VHOLD_ALL_FLAGS & ~VHOLD_NO_SMR) == 0,
4183 "new hold count flag not added to vn_printf");
4186 vn_printf(struct vnode *vp, const char *fmt, ...)
4189 char buf[256], buf2[16];
4196 printf("%p: ", (void *)vp);
4197 printf("type %s\n", typename[vp->v_type]);
4198 holdcnt = atomic_load_int(&vp->v_holdcnt);
4199 printf(" usecount %d, writecount %d, refcount %d seqc users %d",
4200 vp->v_usecount, vp->v_writecount, holdcnt & ~VHOLD_ALL_FLAGS,
4202 switch (vp->v_type) {
4204 printf(" mountedhere %p\n", vp->v_mountedhere);
4207 printf(" rdev %p\n", vp->v_rdev);
4210 printf(" socket %p\n", vp->v_unpcb);
4213 printf(" fifoinfo %p\n", vp->v_fifoinfo);
4221 if (holdcnt & VHOLD_NO_SMR)
4222 strlcat(buf, "|VHOLD_NO_SMR", sizeof(buf));
4223 printf(" hold count flags (%s)\n", buf + 1);
4227 if (vp->v_irflag & VIRF_DOOMED)
4228 strlcat(buf, "|VIRF_DOOMED", sizeof(buf));
4229 flags = vp->v_irflag & ~(VIRF_DOOMED);
4231 snprintf(buf2, sizeof(buf2), "|VIRF(0x%lx)", flags);
4232 strlcat(buf, buf2, sizeof(buf));
4234 if (vp->v_vflag & VV_ROOT)
4235 strlcat(buf, "|VV_ROOT", sizeof(buf));
4236 if (vp->v_vflag & VV_ISTTY)
4237 strlcat(buf, "|VV_ISTTY", sizeof(buf));
4238 if (vp->v_vflag & VV_NOSYNC)
4239 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
4240 if (vp->v_vflag & VV_ETERNALDEV)
4241 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
4242 if (vp->v_vflag & VV_CACHEDLABEL)
4243 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
4244 if (vp->v_vflag & VV_VMSIZEVNLOCK)
4245 strlcat(buf, "|VV_VMSIZEVNLOCK", sizeof(buf));
4246 if (vp->v_vflag & VV_COPYONWRITE)
4247 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
4248 if (vp->v_vflag & VV_SYSTEM)
4249 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
4250 if (vp->v_vflag & VV_PROCDEP)
4251 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
4252 if (vp->v_vflag & VV_NOKNOTE)
4253 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
4254 if (vp->v_vflag & VV_DELETED)
4255 strlcat(buf, "|VV_DELETED", sizeof(buf));
4256 if (vp->v_vflag & VV_MD)
4257 strlcat(buf, "|VV_MD", sizeof(buf));
4258 if (vp->v_vflag & VV_FORCEINSMQ)
4259 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
4260 if (vp->v_vflag & VV_READLINK)
4261 strlcat(buf, "|VV_READLINK", sizeof(buf));
4262 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
4263 VV_CACHEDLABEL | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
4264 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
4266 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
4267 strlcat(buf, buf2, sizeof(buf));
4269 if (vp->v_iflag & VI_TEXT_REF)
4270 strlcat(buf, "|VI_TEXT_REF", sizeof(buf));
4271 if (vp->v_iflag & VI_MOUNT)
4272 strlcat(buf, "|VI_MOUNT", sizeof(buf));
4273 if (vp->v_iflag & VI_DOINGINACT)
4274 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
4275 if (vp->v_iflag & VI_OWEINACT)
4276 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
4277 if (vp->v_iflag & VI_DEFINACT)
4278 strlcat(buf, "|VI_DEFINACT", sizeof(buf));
4279 flags = vp->v_iflag & ~(VI_TEXT_REF | VI_MOUNT | VI_DOINGINACT |
4280 VI_OWEINACT | VI_DEFINACT);
4282 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
4283 strlcat(buf, buf2, sizeof(buf));
4285 if (vp->v_mflag & VMP_LAZYLIST)
4286 strlcat(buf, "|VMP_LAZYLIST", sizeof(buf));
4287 flags = vp->v_mflag & ~(VMP_LAZYLIST);
4289 snprintf(buf2, sizeof(buf2), "|VMP(0x%lx)", flags);
4290 strlcat(buf, buf2, sizeof(buf));
4292 printf(" flags (%s)\n", buf + 1);
4293 if (mtx_owned(VI_MTX(vp)))
4294 printf(" VI_LOCKed");
4295 if (vp->v_object != NULL)
4296 printf(" v_object %p ref %d pages %d "
4297 "cleanbuf %d dirtybuf %d\n",
4298 vp->v_object, vp->v_object->ref_count,
4299 vp->v_object->resident_page_count,
4300 vp->v_bufobj.bo_clean.bv_cnt,
4301 vp->v_bufobj.bo_dirty.bv_cnt);
4303 lockmgr_printinfo(vp->v_vnlock);
4304 if (vp->v_data != NULL)
4310 * List all of the locked vnodes in the system.
4311 * Called when debugging the kernel.
4313 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
4319 * Note: because this is DDB, we can't obey the locking semantics
4320 * for these structures, which means we could catch an inconsistent
4321 * state and dereference a nasty pointer. Not much to be done
4324 db_printf("Locked vnodes\n");
4325 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4326 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4327 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
4328 vn_printf(vp, "vnode ");
4334 * Show details about the given vnode.
4336 DB_SHOW_COMMAND(vnode, db_show_vnode)
4342 vp = (struct vnode *)addr;
4343 vn_printf(vp, "vnode ");
4347 * Show details about the given mount point.
4349 DB_SHOW_COMMAND(mount, db_show_mount)
4360 /* No address given, print short info about all mount points. */
4361 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4362 db_printf("%p %s on %s (%s)\n", mp,
4363 mp->mnt_stat.f_mntfromname,
4364 mp->mnt_stat.f_mntonname,
4365 mp->mnt_stat.f_fstypename);
4369 db_printf("\nMore info: show mount <addr>\n");
4373 mp = (struct mount *)addr;
4374 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
4375 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
4378 mflags = mp->mnt_flag;
4379 #define MNT_FLAG(flag) do { \
4380 if (mflags & (flag)) { \
4381 if (buf[0] != '\0') \
4382 strlcat(buf, ", ", sizeof(buf)); \
4383 strlcat(buf, (#flag) + 4, sizeof(buf)); \
4384 mflags &= ~(flag); \
4387 MNT_FLAG(MNT_RDONLY);
4388 MNT_FLAG(MNT_SYNCHRONOUS);
4389 MNT_FLAG(MNT_NOEXEC);
4390 MNT_FLAG(MNT_NOSUID);
4391 MNT_FLAG(MNT_NFS4ACLS);
4392 MNT_FLAG(MNT_UNION);
4393 MNT_FLAG(MNT_ASYNC);
4394 MNT_FLAG(MNT_SUIDDIR);
4395 MNT_FLAG(MNT_SOFTDEP);
4396 MNT_FLAG(MNT_NOSYMFOLLOW);
4397 MNT_FLAG(MNT_GJOURNAL);
4398 MNT_FLAG(MNT_MULTILABEL);
4400 MNT_FLAG(MNT_NOATIME);
4401 MNT_FLAG(MNT_NOCLUSTERR);
4402 MNT_FLAG(MNT_NOCLUSTERW);
4404 MNT_FLAG(MNT_EXRDONLY);
4405 MNT_FLAG(MNT_EXPORTED);
4406 MNT_FLAG(MNT_DEFEXPORTED);
4407 MNT_FLAG(MNT_EXPORTANON);
4408 MNT_FLAG(MNT_EXKERB);
4409 MNT_FLAG(MNT_EXPUBLIC);
4410 MNT_FLAG(MNT_LOCAL);
4411 MNT_FLAG(MNT_QUOTA);
4412 MNT_FLAG(MNT_ROOTFS);
4414 MNT_FLAG(MNT_IGNORE);
4415 MNT_FLAG(MNT_UPDATE);
4416 MNT_FLAG(MNT_DELEXPORT);
4417 MNT_FLAG(MNT_RELOAD);
4418 MNT_FLAG(MNT_FORCE);
4419 MNT_FLAG(MNT_SNAPSHOT);
4420 MNT_FLAG(MNT_BYFSID);
4424 strlcat(buf, ", ", sizeof(buf));
4425 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
4426 "0x%016jx", mflags);
4428 db_printf(" mnt_flag = %s\n", buf);
4431 flags = mp->mnt_kern_flag;
4432 #define MNT_KERN_FLAG(flag) do { \
4433 if (flags & (flag)) { \
4434 if (buf[0] != '\0') \
4435 strlcat(buf, ", ", sizeof(buf)); \
4436 strlcat(buf, (#flag) + 5, sizeof(buf)); \
4440 MNT_KERN_FLAG(MNTK_UNMOUNTF);
4441 MNT_KERN_FLAG(MNTK_ASYNC);
4442 MNT_KERN_FLAG(MNTK_SOFTDEP);
4443 MNT_KERN_FLAG(MNTK_DRAINING);
4444 MNT_KERN_FLAG(MNTK_REFEXPIRE);
4445 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
4446 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
4447 MNT_KERN_FLAG(MNTK_NO_IOPF);
4448 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
4449 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
4450 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
4451 MNT_KERN_FLAG(MNTK_MARKER);
4452 MNT_KERN_FLAG(MNTK_USES_BCACHE);
4453 MNT_KERN_FLAG(MNTK_FPLOOKUP);
4454 MNT_KERN_FLAG(MNTK_NOASYNC);
4455 MNT_KERN_FLAG(MNTK_UNMOUNT);
4456 MNT_KERN_FLAG(MNTK_MWAIT);
4457 MNT_KERN_FLAG(MNTK_SUSPEND);
4458 MNT_KERN_FLAG(MNTK_SUSPEND2);
4459 MNT_KERN_FLAG(MNTK_SUSPENDED);
4460 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
4461 MNT_KERN_FLAG(MNTK_NOKNOTE);
4462 #undef MNT_KERN_FLAG
4465 strlcat(buf, ", ", sizeof(buf));
4466 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
4469 db_printf(" mnt_kern_flag = %s\n", buf);
4471 db_printf(" mnt_opt = ");
4472 opt = TAILQ_FIRST(mp->mnt_opt);
4474 db_printf("%s", opt->name);
4475 opt = TAILQ_NEXT(opt, link);
4476 while (opt != NULL) {
4477 db_printf(", %s", opt->name);
4478 opt = TAILQ_NEXT(opt, link);
4484 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
4485 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
4486 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
4487 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
4488 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
4489 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
4490 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
4491 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
4492 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
4493 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
4494 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
4495 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
4497 db_printf(" mnt_cred = { uid=%u ruid=%u",
4498 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
4499 if (jailed(mp->mnt_cred))
4500 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
4502 db_printf(" mnt_ref = %d (with %d in the struct)\n",
4503 vfs_mount_fetch_counter(mp, MNT_COUNT_REF), mp->mnt_ref);
4504 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
4505 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
4506 db_printf(" mnt_lazyvnodelistsize = %d\n",
4507 mp->mnt_lazyvnodelistsize);
4508 db_printf(" mnt_writeopcount = %d (with %d in the struct)\n",
4509 vfs_mount_fetch_counter(mp, MNT_COUNT_WRITEOPCOUNT), mp->mnt_writeopcount);
4510 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
4511 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
4512 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
4513 db_printf(" mnt_lockref = %d (with %d in the struct)\n",
4514 vfs_mount_fetch_counter(mp, MNT_COUNT_LOCKREF), mp->mnt_lockref);
4515 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
4516 db_printf(" mnt_secondary_accwrites = %d\n",
4517 mp->mnt_secondary_accwrites);
4518 db_printf(" mnt_gjprovider = %s\n",
4519 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
4520 db_printf(" mnt_vfs_ops = %d\n", mp->mnt_vfs_ops);
4522 db_printf("\n\nList of active vnodes\n");
4523 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4524 if (vp->v_type != VMARKER && vp->v_holdcnt > 0) {
4525 vn_printf(vp, "vnode ");
4530 db_printf("\n\nList of inactive vnodes\n");
4531 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4532 if (vp->v_type != VMARKER && vp->v_holdcnt == 0) {
4533 vn_printf(vp, "vnode ");
4542 * Fill in a struct xvfsconf based on a struct vfsconf.
4545 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
4547 struct xvfsconf xvfsp;
4549 bzero(&xvfsp, sizeof(xvfsp));
4550 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4551 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4552 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4553 xvfsp.vfc_flags = vfsp->vfc_flags;
4555 * These are unused in userland, we keep them
4556 * to not break binary compatibility.
4558 xvfsp.vfc_vfsops = NULL;
4559 xvfsp.vfc_next = NULL;
4560 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4563 #ifdef COMPAT_FREEBSD32
4565 uint32_t vfc_vfsops;
4566 char vfc_name[MFSNAMELEN];
4567 int32_t vfc_typenum;
4568 int32_t vfc_refcount;
4574 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
4576 struct xvfsconf32 xvfsp;
4578 bzero(&xvfsp, sizeof(xvfsp));
4579 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4580 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4581 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4582 xvfsp.vfc_flags = vfsp->vfc_flags;
4583 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4588 * Top level filesystem related information gathering.
4591 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
4593 struct vfsconf *vfsp;
4598 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4599 #ifdef COMPAT_FREEBSD32
4600 if (req->flags & SCTL_MASK32)
4601 error = vfsconf2x32(req, vfsp);
4604 error = vfsconf2x(req, vfsp);
4612 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
4613 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
4614 "S,xvfsconf", "List of all configured filesystems");
4616 #ifndef BURN_BRIDGES
4617 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
4620 vfs_sysctl(SYSCTL_HANDLER_ARGS)
4622 int *name = (int *)arg1 - 1; /* XXX */
4623 u_int namelen = arg2 + 1; /* XXX */
4624 struct vfsconf *vfsp;
4626 log(LOG_WARNING, "userland calling deprecated sysctl, "
4627 "please rebuild world\n");
4629 #if 1 || defined(COMPAT_PRELITE2)
4630 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
4632 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
4636 case VFS_MAXTYPENUM:
4639 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
4642 return (ENOTDIR); /* overloaded */
4644 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4645 if (vfsp->vfc_typenum == name[2])
4650 return (EOPNOTSUPP);
4651 #ifdef COMPAT_FREEBSD32
4652 if (req->flags & SCTL_MASK32)
4653 return (vfsconf2x32(req, vfsp));
4656 return (vfsconf2x(req, vfsp));
4658 return (EOPNOTSUPP);
4661 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
4662 CTLFLAG_MPSAFE, vfs_sysctl,
4663 "Generic filesystem");
4665 #if 1 || defined(COMPAT_PRELITE2)
4668 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
4671 struct vfsconf *vfsp;
4672 struct ovfsconf ovfs;
4675 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4676 bzero(&ovfs, sizeof(ovfs));
4677 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
4678 strcpy(ovfs.vfc_name, vfsp->vfc_name);
4679 ovfs.vfc_index = vfsp->vfc_typenum;
4680 ovfs.vfc_refcount = vfsp->vfc_refcount;
4681 ovfs.vfc_flags = vfsp->vfc_flags;
4682 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
4692 #endif /* 1 || COMPAT_PRELITE2 */
4693 #endif /* !BURN_BRIDGES */
4695 #define KINFO_VNODESLOP 10
4698 * Dump vnode list (via sysctl).
4702 sysctl_vnode(SYSCTL_HANDLER_ARGS)
4710 * Stale numvnodes access is not fatal here.
4713 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
4715 /* Make an estimate */
4716 return (SYSCTL_OUT(req, 0, len));
4718 error = sysctl_wire_old_buffer(req, 0);
4721 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
4723 mtx_lock(&mountlist_mtx);
4724 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4725 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
4728 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4732 xvn[n].xv_size = sizeof *xvn;
4733 xvn[n].xv_vnode = vp;
4734 xvn[n].xv_id = 0; /* XXX compat */
4735 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
4737 XV_COPY(writecount);
4743 xvn[n].xv_flag = vp->v_vflag;
4745 switch (vp->v_type) {
4752 if (vp->v_rdev == NULL) {
4756 xvn[n].xv_dev = dev2udev(vp->v_rdev);
4759 xvn[n].xv_socket = vp->v_socket;
4762 xvn[n].xv_fifo = vp->v_fifoinfo;
4767 /* shouldn't happen? */
4775 mtx_lock(&mountlist_mtx);
4780 mtx_unlock(&mountlist_mtx);
4782 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
4787 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
4788 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
4793 unmount_or_warn(struct mount *mp)
4797 error = dounmount(mp, MNT_FORCE, curthread);
4799 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
4803 printf("%d)\n", error);
4808 * Unmount all filesystems. The list is traversed in reverse order
4809 * of mounting to avoid dependencies.
4812 vfs_unmountall(void)
4814 struct mount *mp, *tmp;
4816 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
4819 * Since this only runs when rebooting, it is not interlocked.
4821 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
4825 * Forcibly unmounting "/dev" before "/" would prevent clean
4826 * unmount of the latter.
4828 if (mp == rootdevmp)
4831 unmount_or_warn(mp);
4834 if (rootdevmp != NULL)
4835 unmount_or_warn(rootdevmp);
4839 vfs_deferred_inactive(struct vnode *vp, int lkflags)
4842 ASSERT_VI_LOCKED(vp, __func__);
4843 VNASSERT((vp->v_iflag & VI_DEFINACT) == 0, vp, ("VI_DEFINACT still set"));
4844 if ((vp->v_iflag & VI_OWEINACT) == 0) {
4848 if (vn_lock(vp, lkflags) == 0) {
4855 vdefer_inactive_unlocked(vp);
4859 vfs_periodic_inactive_filter(struct vnode *vp, void *arg)
4862 return (vp->v_iflag & VI_DEFINACT);
4865 static void __noinline
4866 vfs_periodic_inactive(struct mount *mp, int flags)
4868 struct vnode *vp, *mvp;
4871 lkflags = LK_EXCLUSIVE | LK_INTERLOCK;
4872 if (flags != MNT_WAIT)
4873 lkflags |= LK_NOWAIT;
4875 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_inactive_filter, NULL) {
4876 if ((vp->v_iflag & VI_DEFINACT) == 0) {
4880 vp->v_iflag &= ~VI_DEFINACT;
4881 vfs_deferred_inactive(vp, lkflags);
4886 vfs_want_msync(struct vnode *vp)
4888 struct vm_object *obj;
4891 * This test may be performed without any locks held.
4892 * We rely on vm_object's type stability.
4894 if (vp->v_vflag & VV_NOSYNC)
4897 return (obj != NULL && vm_object_mightbedirty(obj));
4901 vfs_periodic_msync_inactive_filter(struct vnode *vp, void *arg __unused)
4904 if (vp->v_vflag & VV_NOSYNC)
4906 if (vp->v_iflag & VI_DEFINACT)
4908 return (vfs_want_msync(vp));
4911 static void __noinline
4912 vfs_periodic_msync_inactive(struct mount *mp, int flags)
4914 struct vnode *vp, *mvp;
4915 struct vm_object *obj;
4917 int lkflags, objflags;
4922 lkflags = LK_EXCLUSIVE | LK_INTERLOCK;
4923 if (flags != MNT_WAIT) {
4924 lkflags |= LK_NOWAIT;
4925 objflags = OBJPC_NOSYNC;
4927 objflags = OBJPC_SYNC;
4930 MNT_VNODE_FOREACH_LAZY(vp, mp, mvp, vfs_periodic_msync_inactive_filter, NULL) {
4932 if (vp->v_iflag & VI_DEFINACT) {
4933 vp->v_iflag &= ~VI_DEFINACT;
4936 if (!vfs_want_msync(vp)) {
4938 vfs_deferred_inactive(vp, lkflags);
4943 if (vget(vp, lkflags, td) == 0) {
4945 if (obj != NULL && (vp->v_vflag & VV_NOSYNC) == 0) {
4946 VM_OBJECT_WLOCK(obj);
4947 vm_object_page_clean(obj, 0, 0, objflags);
4948 VM_OBJECT_WUNLOCK(obj);
4955 vdefer_inactive_unlocked(vp);
4961 vfs_periodic(struct mount *mp, int flags)
4964 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
4966 if ((mp->mnt_kern_flag & MNTK_NOMSYNC) != 0)
4967 vfs_periodic_inactive(mp, flags);
4969 vfs_periodic_msync_inactive(mp, flags);
4973 destroy_vpollinfo_free(struct vpollinfo *vi)
4976 knlist_destroy(&vi->vpi_selinfo.si_note);
4977 mtx_destroy(&vi->vpi_lock);
4978 uma_zfree(vnodepoll_zone, vi);
4982 destroy_vpollinfo(struct vpollinfo *vi)
4985 knlist_clear(&vi->vpi_selinfo.si_note, 1);
4986 seldrain(&vi->vpi_selinfo);
4987 destroy_vpollinfo_free(vi);
4991 * Initialize per-vnode helper structure to hold poll-related state.
4994 v_addpollinfo(struct vnode *vp)
4996 struct vpollinfo *vi;
4998 if (vp->v_pollinfo != NULL)
5000 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
5001 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
5002 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
5003 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
5005 if (vp->v_pollinfo != NULL) {
5007 destroy_vpollinfo_free(vi);
5010 vp->v_pollinfo = vi;
5015 * Record a process's interest in events which might happen to
5016 * a vnode. Because poll uses the historic select-style interface
5017 * internally, this routine serves as both the ``check for any
5018 * pending events'' and the ``record my interest in future events''
5019 * functions. (These are done together, while the lock is held,
5020 * to avoid race conditions.)
5023 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
5027 mtx_lock(&vp->v_pollinfo->vpi_lock);
5028 if (vp->v_pollinfo->vpi_revents & events) {
5030 * This leaves events we are not interested
5031 * in available for the other process which
5032 * which presumably had requested them
5033 * (otherwise they would never have been
5036 events &= vp->v_pollinfo->vpi_revents;
5037 vp->v_pollinfo->vpi_revents &= ~events;
5039 mtx_unlock(&vp->v_pollinfo->vpi_lock);
5042 vp->v_pollinfo->vpi_events |= events;
5043 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
5044 mtx_unlock(&vp->v_pollinfo->vpi_lock);
5049 * Routine to create and manage a filesystem syncer vnode.
5051 #define sync_close ((int (*)(struct vop_close_args *))nullop)
5052 static int sync_fsync(struct vop_fsync_args *);
5053 static int sync_inactive(struct vop_inactive_args *);
5054 static int sync_reclaim(struct vop_reclaim_args *);
5056 static struct vop_vector sync_vnodeops = {
5057 .vop_bypass = VOP_EOPNOTSUPP,
5058 .vop_close = sync_close, /* close */
5059 .vop_fsync = sync_fsync, /* fsync */
5060 .vop_inactive = sync_inactive, /* inactive */
5061 .vop_need_inactive = vop_stdneed_inactive, /* need_inactive */
5062 .vop_reclaim = sync_reclaim, /* reclaim */
5063 .vop_lock1 = vop_stdlock, /* lock */
5064 .vop_unlock = vop_stdunlock, /* unlock */
5065 .vop_islocked = vop_stdislocked, /* islocked */
5067 VFS_VOP_VECTOR_REGISTER(sync_vnodeops);
5070 * Create a new filesystem syncer vnode for the specified mount point.
5073 vfs_allocate_syncvnode(struct mount *mp)
5077 static long start, incr, next;
5080 /* Allocate a new vnode */
5081 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
5083 panic("vfs_allocate_syncvnode: getnewvnode() failed");
5085 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5086 vp->v_vflag |= VV_FORCEINSMQ;
5087 error = insmntque(vp, mp);
5089 panic("vfs_allocate_syncvnode: insmntque() failed");
5090 vp->v_vflag &= ~VV_FORCEINSMQ;
5093 * Place the vnode onto the syncer worklist. We attempt to
5094 * scatter them about on the list so that they will go off
5095 * at evenly distributed times even if all the filesystems
5096 * are mounted at once.
5099 if (next == 0 || next > syncer_maxdelay) {
5103 start = syncer_maxdelay / 2;
5104 incr = syncer_maxdelay;
5110 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
5111 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
5112 mtx_lock(&sync_mtx);
5114 if (mp->mnt_syncer == NULL) {
5115 mp->mnt_syncer = vp;
5118 mtx_unlock(&sync_mtx);
5121 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5128 vfs_deallocate_syncvnode(struct mount *mp)
5132 mtx_lock(&sync_mtx);
5133 vp = mp->mnt_syncer;
5135 mp->mnt_syncer = NULL;
5136 mtx_unlock(&sync_mtx);
5142 * Do a lazy sync of the filesystem.
5145 sync_fsync(struct vop_fsync_args *ap)
5147 struct vnode *syncvp = ap->a_vp;
5148 struct mount *mp = syncvp->v_mount;
5153 * We only need to do something if this is a lazy evaluation.
5155 if (ap->a_waitfor != MNT_LAZY)
5159 * Move ourselves to the back of the sync list.
5161 bo = &syncvp->v_bufobj;
5163 vn_syncer_add_to_worklist(bo, syncdelay);
5167 * Walk the list of vnodes pushing all that are dirty and
5168 * not already on the sync list.
5170 if (vfs_busy(mp, MBF_NOWAIT) != 0)
5172 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
5176 save = curthread_pflags_set(TDP_SYNCIO);
5178 * The filesystem at hand may be idle with free vnodes stored in the
5179 * batch. Return them instead of letting them stay there indefinitely.
5181 vfs_periodic(mp, MNT_NOWAIT);
5182 error = VFS_SYNC(mp, MNT_LAZY);
5183 curthread_pflags_restore(save);
5184 vn_finished_write(mp);
5190 * The syncer vnode is no referenced.
5193 sync_inactive(struct vop_inactive_args *ap)
5201 * The syncer vnode is no longer needed and is being decommissioned.
5203 * Modifications to the worklist must be protected by sync_mtx.
5206 sync_reclaim(struct vop_reclaim_args *ap)
5208 struct vnode *vp = ap->a_vp;
5213 mtx_lock(&sync_mtx);
5214 if (vp->v_mount->mnt_syncer == vp)
5215 vp->v_mount->mnt_syncer = NULL;
5216 if (bo->bo_flag & BO_ONWORKLST) {
5217 LIST_REMOVE(bo, bo_synclist);
5218 syncer_worklist_len--;
5220 bo->bo_flag &= ~BO_ONWORKLST;
5222 mtx_unlock(&sync_mtx);
5229 vn_need_pageq_flush(struct vnode *vp)
5231 struct vm_object *obj;
5234 MPASS(mtx_owned(VI_MTX(vp)));
5236 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
5237 vm_object_mightbedirty(obj))
5243 * Check if vnode represents a disk device
5246 vn_isdisk(struct vnode *vp, int *errp)
5250 if (vp->v_type != VCHR) {
5256 if (vp->v_rdev == NULL)
5258 else if (vp->v_rdev->si_devsw == NULL)
5260 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
5266 return (error == 0);
5270 * VOP_FPLOOKUP_VEXEC routines are subject to special circumstances, see
5271 * the comment above cache_fplookup for details.
5273 * We never deny as priv_check_cred calls are not yet supported, see vaccess.
5276 vaccess_vexec_smr(mode_t file_mode, uid_t file_uid, gid_t file_gid, struct ucred *cred)
5279 VFS_SMR_ASSERT_ENTERED();
5281 /* Check the owner. */
5282 if (cred->cr_uid == file_uid) {
5283 if (file_mode & S_IXUSR)
5288 /* Otherwise, check the groups (first match) */
5289 if (groupmember(file_gid, cred)) {
5290 if (file_mode & S_IXGRP)
5295 /* Otherwise, check everyone else. */
5296 if (file_mode & S_IXOTH)
5302 * Common filesystem object access control check routine. Accepts a
5303 * vnode's type, "mode", uid and gid, requested access mode, credentials,
5304 * and optional call-by-reference privused argument allowing vaccess()
5305 * to indicate to the caller whether privilege was used to satisfy the
5306 * request (obsoleted). Returns 0 on success, or an errno on failure.
5309 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
5310 accmode_t accmode, struct ucred *cred, int *privused)
5312 accmode_t dac_granted;
5313 accmode_t priv_granted;
5315 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
5316 ("invalid bit in accmode"));
5317 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
5318 ("VAPPEND without VWRITE"));
5321 * Look for a normal, non-privileged way to access the file/directory
5322 * as requested. If it exists, go with that.
5325 if (privused != NULL)
5330 /* Check the owner. */
5331 if (cred->cr_uid == file_uid) {
5332 dac_granted |= VADMIN;
5333 if (file_mode & S_IXUSR)
5334 dac_granted |= VEXEC;
5335 if (file_mode & S_IRUSR)
5336 dac_granted |= VREAD;
5337 if (file_mode & S_IWUSR)
5338 dac_granted |= (VWRITE | VAPPEND);
5340 if ((accmode & dac_granted) == accmode)
5346 /* Otherwise, check the groups (first match) */
5347 if (groupmember(file_gid, cred)) {
5348 if (file_mode & S_IXGRP)
5349 dac_granted |= VEXEC;
5350 if (file_mode & S_IRGRP)
5351 dac_granted |= VREAD;
5352 if (file_mode & S_IWGRP)
5353 dac_granted |= (VWRITE | VAPPEND);
5355 if ((accmode & dac_granted) == accmode)
5361 /* Otherwise, check everyone else. */
5362 if (file_mode & S_IXOTH)
5363 dac_granted |= VEXEC;
5364 if (file_mode & S_IROTH)
5365 dac_granted |= VREAD;
5366 if (file_mode & S_IWOTH)
5367 dac_granted |= (VWRITE | VAPPEND);
5368 if ((accmode & dac_granted) == accmode)
5373 * Build a privilege mask to determine if the set of privileges
5374 * satisfies the requirements when combined with the granted mask
5375 * from above. For each privilege, if the privilege is required,
5376 * bitwise or the request type onto the priv_granted mask.
5382 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
5383 * requests, instead of PRIV_VFS_EXEC.
5385 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
5386 !priv_check_cred(cred, PRIV_VFS_LOOKUP))
5387 priv_granted |= VEXEC;
5390 * Ensure that at least one execute bit is on. Otherwise,
5391 * a privileged user will always succeed, and we don't want
5392 * this to happen unless the file really is executable.
5394 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
5395 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
5396 !priv_check_cred(cred, PRIV_VFS_EXEC))
5397 priv_granted |= VEXEC;
5400 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
5401 !priv_check_cred(cred, PRIV_VFS_READ))
5402 priv_granted |= VREAD;
5404 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
5405 !priv_check_cred(cred, PRIV_VFS_WRITE))
5406 priv_granted |= (VWRITE | VAPPEND);
5408 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
5409 !priv_check_cred(cred, PRIV_VFS_ADMIN))
5410 priv_granted |= VADMIN;
5412 if ((accmode & (priv_granted | dac_granted)) == accmode) {
5413 /* XXX audit: privilege used */
5414 if (privused != NULL)
5419 return ((accmode & VADMIN) ? EPERM : EACCES);
5423 * Credential check based on process requesting service, and per-attribute
5427 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
5428 struct thread *td, accmode_t accmode)
5432 * Kernel-invoked always succeeds.
5438 * Do not allow privileged processes in jail to directly manipulate
5439 * system attributes.
5441 switch (attrnamespace) {
5442 case EXTATTR_NAMESPACE_SYSTEM:
5443 /* Potentially should be: return (EPERM); */
5444 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
5445 case EXTATTR_NAMESPACE_USER:
5446 return (VOP_ACCESS(vp, accmode, cred, td));
5452 #ifdef DEBUG_VFS_LOCKS
5454 * This only exists to suppress warnings from unlocked specfs accesses. It is
5455 * no longer ok to have an unlocked VFS.
5457 #define IGNORE_LOCK(vp) (KERNEL_PANICKED() || (vp) == NULL || \
5458 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
5460 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
5461 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
5462 "Drop into debugger on lock violation");
5464 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
5465 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
5466 0, "Check for interlock across VOPs");
5468 int vfs_badlock_print = 1; /* Print lock violations. */
5469 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
5470 0, "Print lock violations");
5472 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */
5473 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode,
5474 0, "Print vnode details on lock violations");
5477 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
5478 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
5479 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
5483 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
5487 if (vfs_badlock_backtrace)
5490 if (vfs_badlock_vnode)
5491 vn_printf(vp, "vnode ");
5492 if (vfs_badlock_print)
5493 printf("%s: %p %s\n", str, (void *)vp, msg);
5494 if (vfs_badlock_ddb)
5495 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
5499 assert_vi_locked(struct vnode *vp, const char *str)
5502 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
5503 vfs_badlock("interlock is not locked but should be", str, vp);
5507 assert_vi_unlocked(struct vnode *vp, const char *str)
5510 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
5511 vfs_badlock("interlock is locked but should not be", str, vp);
5515 assert_vop_locked(struct vnode *vp, const char *str)
5519 if (!IGNORE_LOCK(vp)) {
5520 locked = VOP_ISLOCKED(vp);
5521 if (locked == 0 || locked == LK_EXCLOTHER)
5522 vfs_badlock("is not locked but should be", str, vp);
5527 assert_vop_unlocked(struct vnode *vp, const char *str)
5530 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
5531 vfs_badlock("is locked but should not be", str, vp);
5535 assert_vop_elocked(struct vnode *vp, const char *str)
5538 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
5539 vfs_badlock("is not exclusive locked but should be", str, vp);
5541 #endif /* DEBUG_VFS_LOCKS */
5544 vop_rename_fail(struct vop_rename_args *ap)
5547 if (ap->a_tvp != NULL)
5549 if (ap->a_tdvp == ap->a_tvp)
5558 vop_rename_pre(void *ap)
5560 struct vop_rename_args *a = ap;
5562 #ifdef DEBUG_VFS_LOCKS
5564 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
5565 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
5566 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
5567 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
5569 /* Check the source (from). */
5570 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
5571 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
5572 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
5573 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
5574 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
5576 /* Check the target. */
5578 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
5579 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
5582 * It may be tempting to add vn_seqc_write_begin/end calls here and
5583 * in vop_rename_post but that's not going to work out since some
5584 * filesystems relookup vnodes mid-rename. This is probably a bug.
5586 * For now filesystems are expected to do the relevant calls after they
5587 * decide what vnodes to operate on.
5589 if (a->a_tdvp != a->a_fdvp)
5591 if (a->a_tvp != a->a_fvp)
5598 #ifdef DEBUG_VFS_LOCKS
5600 vop_fplookup_vexec_debugpre(void *ap __unused)
5603 VFS_SMR_ASSERT_ENTERED();
5607 vop_fplookup_vexec_debugpost(void *ap __unused, int rc __unused)
5610 VFS_SMR_ASSERT_ENTERED();
5614 vop_strategy_debugpre(void *ap)
5616 struct vop_strategy_args *a;
5623 * Cluster ops lock their component buffers but not the IO container.
5625 if ((bp->b_flags & B_CLUSTER) != 0)
5628 if (!KERNEL_PANICKED() && !BUF_ISLOCKED(bp)) {
5629 if (vfs_badlock_print)
5631 "VOP_STRATEGY: bp is not locked but should be\n");
5632 if (vfs_badlock_ddb)
5633 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
5638 vop_lock_debugpre(void *ap)
5640 struct vop_lock1_args *a = ap;
5642 if ((a->a_flags & LK_INTERLOCK) == 0)
5643 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
5645 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
5649 vop_lock_debugpost(void *ap, int rc)
5651 struct vop_lock1_args *a = ap;
5653 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
5654 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
5655 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
5659 vop_unlock_debugpre(void *ap)
5661 struct vop_unlock_args *a = ap;
5663 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
5667 vop_need_inactive_debugpre(void *ap)
5669 struct vop_need_inactive_args *a = ap;
5671 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5675 vop_need_inactive_debugpost(void *ap, int rc)
5677 struct vop_need_inactive_args *a = ap;
5679 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5684 vop_create_pre(void *ap)
5686 struct vop_create_args *a;
5691 vn_seqc_write_begin(dvp);
5695 vop_create_post(void *ap, int rc)
5697 struct vop_create_args *a;
5702 vn_seqc_write_end(dvp);
5704 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5708 vop_whiteout_pre(void *ap)
5710 struct vop_whiteout_args *a;
5715 vn_seqc_write_begin(dvp);
5719 vop_whiteout_post(void *ap, int rc)
5721 struct vop_whiteout_args *a;
5726 vn_seqc_write_end(dvp);
5730 vop_deleteextattr_pre(void *ap)
5732 struct vop_deleteextattr_args *a;
5737 vn_seqc_write_begin(vp);
5741 vop_deleteextattr_post(void *ap, int rc)
5743 struct vop_deleteextattr_args *a;
5748 vn_seqc_write_end(vp);
5750 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5754 vop_link_pre(void *ap)
5756 struct vop_link_args *a;
5757 struct vnode *vp, *tdvp;
5762 vn_seqc_write_begin(vp);
5763 vn_seqc_write_begin(tdvp);
5767 vop_link_post(void *ap, int rc)
5769 struct vop_link_args *a;
5770 struct vnode *vp, *tdvp;
5775 vn_seqc_write_end(vp);
5776 vn_seqc_write_end(tdvp);
5778 VFS_KNOTE_LOCKED(vp, NOTE_LINK);
5779 VFS_KNOTE_LOCKED(tdvp, NOTE_WRITE);
5784 vop_mkdir_pre(void *ap)
5786 struct vop_mkdir_args *a;
5791 vn_seqc_write_begin(dvp);
5795 vop_mkdir_post(void *ap, int rc)
5797 struct vop_mkdir_args *a;
5802 vn_seqc_write_end(dvp);
5804 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE | NOTE_LINK);
5808 vop_mknod_pre(void *ap)
5810 struct vop_mknod_args *a;
5815 vn_seqc_write_begin(dvp);
5819 vop_mknod_post(void *ap, int rc)
5821 struct vop_mknod_args *a;
5826 vn_seqc_write_end(dvp);
5828 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5832 vop_reclaim_post(void *ap, int rc)
5834 struct vop_reclaim_args *a;
5839 ASSERT_VOP_IN_SEQC(vp);
5841 VFS_KNOTE_LOCKED(vp, NOTE_REVOKE);
5845 vop_remove_pre(void *ap)
5847 struct vop_remove_args *a;
5848 struct vnode *dvp, *vp;
5853 vn_seqc_write_begin(dvp);
5854 vn_seqc_write_begin(vp);
5858 vop_remove_post(void *ap, int rc)
5860 struct vop_remove_args *a;
5861 struct vnode *dvp, *vp;
5866 vn_seqc_write_end(dvp);
5867 vn_seqc_write_end(vp);
5869 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
5870 VFS_KNOTE_LOCKED(vp, NOTE_DELETE);
5875 vop_rename_post(void *ap, int rc)
5877 struct vop_rename_args *a = ap;
5882 if (a->a_fdvp == a->a_tdvp) {
5883 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR)
5885 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5886 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5888 hint |= NOTE_EXTEND;
5889 if (a->a_fvp->v_type == VDIR)
5891 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5893 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL &&
5894 a->a_tvp->v_type == VDIR)
5896 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5899 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
5901 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
5903 if (a->a_tdvp != a->a_fdvp)
5905 if (a->a_tvp != a->a_fvp)
5913 vop_rmdir_pre(void *ap)
5915 struct vop_rmdir_args *a;
5916 struct vnode *dvp, *vp;
5921 vn_seqc_write_begin(dvp);
5922 vn_seqc_write_begin(vp);
5926 vop_rmdir_post(void *ap, int rc)
5928 struct vop_rmdir_args *a;
5929 struct vnode *dvp, *vp;
5934 vn_seqc_write_end(dvp);
5935 vn_seqc_write_end(vp);
5937 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE | NOTE_LINK);
5938 VFS_KNOTE_LOCKED(vp, NOTE_DELETE);
5943 vop_setattr_pre(void *ap)
5945 struct vop_setattr_args *a;
5950 vn_seqc_write_begin(vp);
5954 vop_setattr_post(void *ap, int rc)
5956 struct vop_setattr_args *a;
5961 vn_seqc_write_end(vp);
5963 VFS_KNOTE_LOCKED(vp, NOTE_ATTRIB);
5967 vop_setacl_pre(void *ap)
5969 struct vop_setacl_args *a;
5974 vn_seqc_write_begin(vp);
5978 vop_setacl_post(void *ap, int rc __unused)
5980 struct vop_setacl_args *a;
5985 vn_seqc_write_end(vp);
5989 vop_setextattr_pre(void *ap)
5991 struct vop_setextattr_args *a;
5996 vn_seqc_write_begin(vp);
6000 vop_setextattr_post(void *ap, int rc)
6002 struct vop_setextattr_args *a;
6007 vn_seqc_write_end(vp);
6009 VFS_KNOTE_LOCKED(vp, NOTE_ATTRIB);
6013 vop_symlink_pre(void *ap)
6015 struct vop_symlink_args *a;
6020 vn_seqc_write_begin(dvp);
6024 vop_symlink_post(void *ap, int rc)
6026 struct vop_symlink_args *a;
6031 vn_seqc_write_end(dvp);
6033 VFS_KNOTE_LOCKED(dvp, NOTE_WRITE);
6037 vop_open_post(void *ap, int rc)
6039 struct vop_open_args *a = ap;
6042 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN);
6046 vop_close_post(void *ap, int rc)
6048 struct vop_close_args *a = ap;
6050 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */
6051 !VN_IS_DOOMED(a->a_vp))) {
6052 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ?
6053 NOTE_CLOSE_WRITE : NOTE_CLOSE);
6058 vop_read_post(void *ap, int rc)
6060 struct vop_read_args *a = ap;
6063 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
6067 vop_readdir_post(void *ap, int rc)
6069 struct vop_readdir_args *a = ap;
6072 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
6075 static struct knlist fs_knlist;
6078 vfs_event_init(void *arg)
6080 knlist_init_mtx(&fs_knlist, NULL);
6082 /* XXX - correct order? */
6083 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
6086 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
6089 KNOTE_UNLOCKED(&fs_knlist, event);
6092 static int filt_fsattach(struct knote *kn);
6093 static void filt_fsdetach(struct knote *kn);
6094 static int filt_fsevent(struct knote *kn, long hint);
6096 struct filterops fs_filtops = {
6098 .f_attach = filt_fsattach,
6099 .f_detach = filt_fsdetach,
6100 .f_event = filt_fsevent
6104 filt_fsattach(struct knote *kn)
6107 kn->kn_flags |= EV_CLEAR;
6108 knlist_add(&fs_knlist, kn, 0);
6113 filt_fsdetach(struct knote *kn)
6116 knlist_remove(&fs_knlist, kn, 0);
6120 filt_fsevent(struct knote *kn, long hint)
6123 kn->kn_fflags |= hint;
6124 return (kn->kn_fflags != 0);
6128 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
6134 error = SYSCTL_IN(req, &vc, sizeof(vc));
6137 if (vc.vc_vers != VFS_CTL_VERS1)
6139 mp = vfs_getvfs(&vc.vc_fsid);
6142 /* ensure that a specific sysctl goes to the right filesystem. */
6143 if (strcmp(vc.vc_fstypename, "*") != 0 &&
6144 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
6148 VCTLTOREQ(&vc, req);
6149 error = VFS_SYSCTL(mp, vc.vc_op, req);
6154 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_MPSAFE | CTLFLAG_WR,
6155 NULL, 0, sysctl_vfs_ctl, "",
6159 * Function to initialize a va_filerev field sensibly.
6160 * XXX: Wouldn't a random number make a lot more sense ??
6163 init_va_filerev(void)
6168 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
6171 static int filt_vfsread(struct knote *kn, long hint);
6172 static int filt_vfswrite(struct knote *kn, long hint);
6173 static int filt_vfsvnode(struct knote *kn, long hint);
6174 static void filt_vfsdetach(struct knote *kn);
6175 static struct filterops vfsread_filtops = {
6177 .f_detach = filt_vfsdetach,
6178 .f_event = filt_vfsread
6180 static struct filterops vfswrite_filtops = {
6182 .f_detach = filt_vfsdetach,
6183 .f_event = filt_vfswrite
6185 static struct filterops vfsvnode_filtops = {
6187 .f_detach = filt_vfsdetach,
6188 .f_event = filt_vfsvnode
6192 vfs_knllock(void *arg)
6194 struct vnode *vp = arg;
6196 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
6200 vfs_knlunlock(void *arg)
6202 struct vnode *vp = arg;
6208 vfs_knl_assert_locked(void *arg)
6210 #ifdef DEBUG_VFS_LOCKS
6211 struct vnode *vp = arg;
6213 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
6218 vfs_knl_assert_unlocked(void *arg)
6220 #ifdef DEBUG_VFS_LOCKS
6221 struct vnode *vp = arg;
6223 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
6228 vfs_kqfilter(struct vop_kqfilter_args *ap)
6230 struct vnode *vp = ap->a_vp;
6231 struct knote *kn = ap->a_kn;
6234 switch (kn->kn_filter) {
6236 kn->kn_fop = &vfsread_filtops;
6239 kn->kn_fop = &vfswrite_filtops;
6242 kn->kn_fop = &vfsvnode_filtops;
6248 kn->kn_hook = (caddr_t)vp;
6251 if (vp->v_pollinfo == NULL)
6253 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
6255 knlist_add(knl, kn, 0);
6261 * Detach knote from vnode
6264 filt_vfsdetach(struct knote *kn)
6266 struct vnode *vp = (struct vnode *)kn->kn_hook;
6268 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
6269 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
6275 filt_vfsread(struct knote *kn, long hint)
6277 struct vnode *vp = (struct vnode *)kn->kn_hook;
6282 * filesystem is gone, so set the EOF flag and schedule
6283 * the knote for deletion.
6285 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
6287 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
6292 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
6296 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
6297 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
6304 filt_vfswrite(struct knote *kn, long hint)
6306 struct vnode *vp = (struct vnode *)kn->kn_hook;
6311 * filesystem is gone, so set the EOF flag and schedule
6312 * the knote for deletion.
6314 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
6315 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
6323 filt_vfsvnode(struct knote *kn, long hint)
6325 struct vnode *vp = (struct vnode *)kn->kn_hook;
6329 if (kn->kn_sfflags & hint)
6330 kn->kn_fflags |= hint;
6331 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
6332 kn->kn_flags |= EV_EOF;
6336 res = (kn->kn_fflags != 0);
6342 * Returns whether the directory is empty or not.
6343 * If it is empty, the return value is 0; otherwise
6344 * the return value is an error value (which may
6348 vfs_emptydir(struct vnode *vp)
6352 struct dirent *dirent, *dp, *endp;
6358 ASSERT_VOP_LOCKED(vp, "vfs_emptydir");
6360 dirent = malloc(sizeof(struct dirent), M_TEMP, M_WAITOK);
6361 iov.iov_base = dirent;
6362 iov.iov_len = sizeof(struct dirent);
6367 uio.uio_resid = sizeof(struct dirent);
6368 uio.uio_segflg = UIO_SYSSPACE;
6369 uio.uio_rw = UIO_READ;
6370 uio.uio_td = curthread;
6372 while (eof == 0 && error == 0) {
6373 error = VOP_READDIR(vp, &uio, curthread->td_ucred, &eof,
6377 endp = (void *)((uint8_t *)dirent +
6378 sizeof(struct dirent) - uio.uio_resid);
6379 for (dp = dirent; dp < endp;
6380 dp = (void *)((uint8_t *)dp + GENERIC_DIRSIZ(dp))) {
6381 if (dp->d_type == DT_WHT)
6383 if (dp->d_namlen == 0)
6385 if (dp->d_type != DT_DIR &&
6386 dp->d_type != DT_UNKNOWN) {
6390 if (dp->d_namlen > 2) {
6394 if (dp->d_namlen == 1 &&
6395 dp->d_name[0] != '.') {
6399 if (dp->d_namlen == 2 &&
6400 dp->d_name[1] != '.') {
6404 uio.uio_resid = sizeof(struct dirent);
6407 free(dirent, M_TEMP);
6412 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
6416 if (dp->d_reclen > ap->a_uio->uio_resid)
6417 return (ENAMETOOLONG);
6418 error = uiomove(dp, dp->d_reclen, ap->a_uio);
6420 if (ap->a_ncookies != NULL) {
6421 if (ap->a_cookies != NULL)
6422 free(ap->a_cookies, M_TEMP);
6423 ap->a_cookies = NULL;
6424 *ap->a_ncookies = 0;
6428 if (ap->a_ncookies == NULL)
6431 KASSERT(ap->a_cookies,
6432 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
6434 *ap->a_cookies = realloc(*ap->a_cookies,
6435 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
6436 (*ap->a_cookies)[*ap->a_ncookies] = off;
6437 *ap->a_ncookies += 1;
6442 * The purpose of this routine is to remove granularity from accmode_t,
6443 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
6444 * VADMIN and VAPPEND.
6446 * If it returns 0, the caller is supposed to continue with the usual
6447 * access checks using 'accmode' as modified by this routine. If it
6448 * returns nonzero value, the caller is supposed to return that value
6451 * Note that after this routine runs, accmode may be zero.
6454 vfs_unixify_accmode(accmode_t *accmode)
6457 * There is no way to specify explicit "deny" rule using
6458 * file mode or POSIX.1e ACLs.
6460 if (*accmode & VEXPLICIT_DENY) {
6466 * None of these can be translated into usual access bits.
6467 * Also, the common case for NFSv4 ACLs is to not contain
6468 * either of these bits. Caller should check for VWRITE
6469 * on the containing directory instead.
6471 if (*accmode & (VDELETE_CHILD | VDELETE))
6474 if (*accmode & VADMIN_PERMS) {
6475 *accmode &= ~VADMIN_PERMS;
6480 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
6481 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
6483 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
6489 * Clear out a doomed vnode (if any) and replace it with a new one as long
6490 * as the fs is not being unmounted. Return the root vnode to the caller.
6492 static int __noinline
6493 vfs_cache_root_fallback(struct mount *mp, int flags, struct vnode **vpp)
6499 if (mp->mnt_rootvnode != NULL) {
6501 vp = mp->mnt_rootvnode;
6503 if (!VN_IS_DOOMED(vp)) {
6506 error = vn_lock(vp, flags);
6515 * Clear the old one.
6517 mp->mnt_rootvnode = NULL;
6521 vfs_op_barrier_wait(mp);
6525 error = VFS_CACHEDROOT(mp, flags, vpp);
6528 if (mp->mnt_vfs_ops == 0) {
6530 if (mp->mnt_vfs_ops != 0) {
6534 if (mp->mnt_rootvnode == NULL) {
6536 mp->mnt_rootvnode = *vpp;
6538 if (mp->mnt_rootvnode != *vpp) {
6539 if (!VN_IS_DOOMED(mp->mnt_rootvnode)) {
6540 panic("%s: mismatch between vnode returned "
6541 " by VFS_CACHEDROOT and the one cached "
6543 __func__, *vpp, mp->mnt_rootvnode);
6553 vfs_cache_root(struct mount *mp, int flags, struct vnode **vpp)
6558 if (!vfs_op_thread_enter(mp))
6559 return (vfs_cache_root_fallback(mp, flags, vpp));
6560 vp = atomic_load_ptr(&mp->mnt_rootvnode);
6561 if (vp == NULL || VN_IS_DOOMED(vp)) {
6562 vfs_op_thread_exit(mp);
6563 return (vfs_cache_root_fallback(mp, flags, vpp));
6566 vfs_op_thread_exit(mp);
6567 error = vn_lock(vp, flags);
6570 return (vfs_cache_root_fallback(mp, flags, vpp));
6577 vfs_cache_root_clear(struct mount *mp)
6582 * ops > 0 guarantees there is nobody who can see this vnode
6584 MPASS(mp->mnt_vfs_ops > 0);
6585 vp = mp->mnt_rootvnode;
6587 vn_seqc_write_begin(vp);
6588 mp->mnt_rootvnode = NULL;
6593 vfs_cache_root_set(struct mount *mp, struct vnode *vp)
6596 MPASS(mp->mnt_vfs_ops > 0);
6598 mp->mnt_rootvnode = vp;
6602 * These are helper functions for filesystems to traverse all
6603 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
6605 * This interface replaces MNT_VNODE_FOREACH.
6609 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
6614 kern_yield(PRI_USER);
6616 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6617 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL;
6618 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
6619 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */
6620 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp))
6623 if (VN_IS_DOOMED(vp)) {
6630 __mnt_vnode_markerfree_all(mvp, mp);
6631 /* MNT_IUNLOCK(mp); -- done in above function */
6632 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
6635 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
6636 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
6642 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
6646 *mvp = vn_alloc_marker(mp);
6650 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
6651 /* Allow a racy peek at VIRF_DOOMED to save a lock acquisition. */
6652 if (vp->v_type == VMARKER || VN_IS_DOOMED(vp))
6655 if (VN_IS_DOOMED(vp)) {
6664 vn_free_marker(*mvp);
6668 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
6674 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
6682 mtx_assert(MNT_MTX(mp), MA_OWNED);
6684 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6685 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
6688 vn_free_marker(*mvp);
6693 * These are helper functions for filesystems to traverse their
6694 * lazy vnodes. See MNT_VNODE_FOREACH_LAZY() in sys/mount.h
6697 mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp)
6700 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6705 vn_free_marker(*mvp);
6710 * Relock the mp mount vnode list lock with the vp vnode interlock in the
6711 * conventional lock order during mnt_vnode_next_lazy iteration.
6713 * On entry, the mount vnode list lock is held and the vnode interlock is not.
6714 * The list lock is dropped and reacquired. On success, both locks are held.
6715 * On failure, the mount vnode list lock is held but the vnode interlock is
6716 * not, and the procedure may have yielded.
6719 mnt_vnode_next_lazy_relock(struct vnode *mvp, struct mount *mp,
6723 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER &&
6724 TAILQ_NEXT(mvp, v_lazylist) != NULL, mvp,
6725 ("%s: bad marker", __func__));
6726 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp,
6727 ("%s: inappropriate vnode", __func__));
6728 ASSERT_VI_UNLOCKED(vp, __func__);
6729 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6731 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, mvp, v_lazylist);
6732 TAILQ_INSERT_BEFORE(vp, mvp, v_lazylist);
6735 * Note we may be racing against vdrop which transitioned the hold
6736 * count to 0 and now waits for the ->mnt_listmtx lock. This is fine,
6737 * if we are the only user after we get the interlock we will just
6741 mtx_unlock(&mp->mnt_listmtx);
6743 if (VN_IS_DOOMED(vp)) {
6744 VNPASS((vp->v_mflag & VMP_LAZYLIST) == 0, vp);
6747 VNPASS(vp->v_mflag & VMP_LAZYLIST, vp);
6749 * There is nothing to do if we are the last user.
6751 if (!refcount_release_if_not_last(&vp->v_holdcnt))
6753 mtx_lock(&mp->mnt_listmtx);
6758 mtx_lock(&mp->mnt_listmtx);
6762 static struct vnode *
6763 mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6768 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
6769 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
6771 vp = TAILQ_NEXT(*mvp, v_lazylist);
6772 while (vp != NULL) {
6773 if (vp->v_type == VMARKER) {
6774 vp = TAILQ_NEXT(vp, v_lazylist);
6778 * See if we want to process the vnode. Note we may encounter a
6779 * long string of vnodes we don't care about and hog the list
6780 * as a result. Check for it and requeue the marker.
6782 VNPASS(!VN_IS_DOOMED(vp), vp);
6783 if (!cb(vp, cbarg)) {
6784 if (!should_yield()) {
6785 vp = TAILQ_NEXT(vp, v_lazylist);
6788 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp,
6790 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp,
6792 mtx_unlock(&mp->mnt_listmtx);
6793 kern_yield(PRI_USER);
6794 mtx_lock(&mp->mnt_listmtx);
6798 * Try-lock because this is the wrong lock order.
6800 if (!VI_TRYLOCK(vp) &&
6801 !mnt_vnode_next_lazy_relock(*mvp, mp, vp))
6803 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
6804 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
6805 ("alien vnode on the lazy list %p %p", vp, mp));
6806 VNPASS(vp->v_mount == mp, vp);
6807 VNPASS(!VN_IS_DOOMED(vp), vp);
6810 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist);
6812 /* Check if we are done */
6814 mtx_unlock(&mp->mnt_listmtx);
6815 mnt_vnode_markerfree_lazy(mvp, mp);
6818 TAILQ_INSERT_AFTER(&mp->mnt_lazyvnodelist, vp, *mvp, v_lazylist);
6819 mtx_unlock(&mp->mnt_listmtx);
6820 ASSERT_VI_LOCKED(vp, "lazy iter");
6825 __mnt_vnode_next_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6830 kern_yield(PRI_USER);
6831 mtx_lock(&mp->mnt_listmtx);
6832 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg));
6836 __mnt_vnode_first_lazy(struct vnode **mvp, struct mount *mp, mnt_lazy_cb_t *cb,
6841 if (TAILQ_EMPTY(&mp->mnt_lazyvnodelist))
6844 *mvp = vn_alloc_marker(mp);
6849 mtx_lock(&mp->mnt_listmtx);
6850 vp = TAILQ_FIRST(&mp->mnt_lazyvnodelist);
6852 mtx_unlock(&mp->mnt_listmtx);
6853 mnt_vnode_markerfree_lazy(mvp, mp);
6856 TAILQ_INSERT_BEFORE(vp, *mvp, v_lazylist);
6857 return (mnt_vnode_next_lazy(mvp, mp, cb, cbarg));
6861 __mnt_vnode_markerfree_lazy(struct vnode **mvp, struct mount *mp)
6867 mtx_lock(&mp->mnt_listmtx);
6868 TAILQ_REMOVE(&mp->mnt_lazyvnodelist, *mvp, v_lazylist);
6869 mtx_unlock(&mp->mnt_listmtx);
6870 mnt_vnode_markerfree_lazy(mvp, mp);
6874 vn_dir_check_exec(struct vnode *vp, struct componentname *cnp)
6877 if ((cnp->cn_flags & NOEXECCHECK) != 0) {
6878 cnp->cn_flags &= ~NOEXECCHECK;
6882 return (VOP_ACCESS(vp, VEXEC, cnp->cn_cred, cnp->cn_thread));
6886 vn_seqc_write_begin_locked(struct vnode *vp)
6889 ASSERT_VI_LOCKED(vp, __func__);
6890 VNPASS(vp->v_holdcnt > 0, vp);
6891 VNPASS(vp->v_seqc_users >= 0, vp);
6893 if (vp->v_seqc_users == 1)
6894 seqc_sleepable_write_begin(&vp->v_seqc);
6898 vn_seqc_write_begin(struct vnode *vp)
6902 vn_seqc_write_begin_locked(vp);
6907 vn_seqc_write_end_locked(struct vnode *vp)
6910 ASSERT_VI_LOCKED(vp, __func__);
6911 VNPASS(vp->v_seqc_users > 0, vp);
6913 if (vp->v_seqc_users == 0)
6914 seqc_sleepable_write_end(&vp->v_seqc);
6918 vn_seqc_write_end(struct vnode *vp)
6922 vn_seqc_write_end_locked(vp);