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>
81 #include <sys/sysctl.h>
82 #include <sys/syslog.h>
83 #include <sys/vmmeter.h>
84 #include <sys/vnode.h>
85 #include <sys/watchdog.h>
87 #include <machine/stdarg.h>
89 #include <security/mac/mac_framework.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_extern.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_page.h>
97 #include <vm/vm_kern.h>
104 static void delmntque(struct vnode *vp);
105 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
106 int slpflag, int slptimeo);
107 static void syncer_shutdown(void *arg, int howto);
108 static int vtryrecycle(struct vnode *vp);
109 static void v_init_counters(struct vnode *);
110 static void v_incr_devcount(struct vnode *);
111 static void v_decr_devcount(struct vnode *);
112 static void vgonel(struct vnode *);
113 static void vfs_knllock(void *arg);
114 static void vfs_knlunlock(void *arg);
115 static void vfs_knl_assert_locked(void *arg);
116 static void vfs_knl_assert_unlocked(void *arg);
117 static void vnlru_return_batches(struct vfsops *mnt_op);
118 static void destroy_vpollinfo(struct vpollinfo *vi);
119 static int v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
120 daddr_t startlbn, daddr_t endlbn);
123 * These fences are intended for cases where some synchronization is
124 * needed between access of v_iflags and lockless vnode refcount (v_holdcnt
125 * and v_usecount) updates. Access to v_iflags is generally synchronized
126 * by the interlock, but we have some internal assertions that check vnode
127 * flags without acquiring the lock. Thus, these fences are INVARIANTS-only
131 #define VNODE_REFCOUNT_FENCE_ACQ() atomic_thread_fence_acq()
132 #define VNODE_REFCOUNT_FENCE_REL() atomic_thread_fence_rel()
134 #define VNODE_REFCOUNT_FENCE_ACQ()
135 #define VNODE_REFCOUNT_FENCE_REL()
139 * Number of vnodes in existence. Increased whenever getnewvnode()
140 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
142 static unsigned long numvnodes;
144 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
145 "Number of vnodes in existence");
147 static counter_u64_t vnodes_created;
148 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
149 "Number of vnodes created by getnewvnode");
151 static u_long mnt_free_list_batch = 128;
152 SYSCTL_ULONG(_vfs, OID_AUTO, mnt_free_list_batch, CTLFLAG_RW,
153 &mnt_free_list_batch, 0, "Limit of vnodes held on mnt's free list");
156 * Conversion tables for conversion from vnode types to inode formats
159 enum vtype iftovt_tab[16] = {
160 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
161 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
163 int vttoif_tab[10] = {
164 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
165 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
169 * List of vnodes that are ready for recycling.
171 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
174 * "Free" vnode target. Free vnodes are rarely completely free, but are
175 * just ones that are cheap to recycle. Usually they are for files which
176 * have been stat'd but not read; these usually have inode and namecache
177 * data attached to them. This target is the preferred minimum size of a
178 * sub-cache consisting mostly of such files. The system balances the size
179 * of this sub-cache with its complement to try to prevent either from
180 * thrashing while the other is relatively inactive. The targets express
181 * a preference for the best balance.
183 * "Above" this target there are 2 further targets (watermarks) related
184 * to recyling of free vnodes. In the best-operating case, the cache is
185 * exactly full, the free list has size between vlowat and vhiwat above the
186 * free target, and recycling from it and normal use maintains this state.
187 * Sometimes the free list is below vlowat or even empty, but this state
188 * is even better for immediate use provided the cache is not full.
189 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free
190 * ones) to reach one of these states. The watermarks are currently hard-
191 * coded as 4% and 9% of the available space higher. These and the default
192 * of 25% for wantfreevnodes are too large if the memory size is large.
193 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim
194 * whenever vnlru_proc() becomes active.
196 static u_long wantfreevnodes;
197 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW,
198 &wantfreevnodes, 0, "Target for minimum number of \"free\" vnodes");
199 static u_long freevnodes;
200 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD,
201 &freevnodes, 0, "Number of \"free\" vnodes");
203 static counter_u64_t recycles_count;
204 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count,
205 "Number of vnodes recycled to meet vnode cache targets");
208 * Various variables used for debugging the new implementation of
210 * XXX these are probably of (very) limited utility now.
212 static int reassignbufcalls;
213 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
214 "Number of calls to reassignbuf");
216 static counter_u64_t free_owe_inact;
217 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact,
218 "Number of times free vnodes kept on active list due to VFS "
219 "owing inactivation");
221 /* To keep more than one thread at a time from running vfs_getnewfsid */
222 static struct mtx mntid_mtx;
225 * Lock for any access to the following:
230 static struct mtx vnode_free_list_mtx;
232 /* Publicly exported FS */
233 struct nfs_public nfs_pub;
235 static uma_zone_t buf_trie_zone;
237 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
238 static uma_zone_t vnode_zone;
239 static uma_zone_t vnodepoll_zone;
242 * The workitem queue.
244 * It is useful to delay writes of file data and filesystem metadata
245 * for tens of seconds so that quickly created and deleted files need
246 * not waste disk bandwidth being created and removed. To realize this,
247 * we append vnodes to a "workitem" queue. When running with a soft
248 * updates implementation, most pending metadata dependencies should
249 * not wait for more than a few seconds. Thus, mounted on block devices
250 * are delayed only about a half the time that file data is delayed.
251 * Similarly, directory updates are more critical, so are only delayed
252 * about a third the time that file data is delayed. Thus, there are
253 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
254 * one each second (driven off the filesystem syncer process). The
255 * syncer_delayno variable indicates the next queue that is to be processed.
256 * Items that need to be processed soon are placed in this queue:
258 * syncer_workitem_pending[syncer_delayno]
260 * A delay of fifteen seconds is done by placing the request fifteen
261 * entries later in the queue:
263 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
266 static int syncer_delayno;
267 static long syncer_mask;
268 LIST_HEAD(synclist, bufobj);
269 static struct synclist *syncer_workitem_pending;
271 * The sync_mtx protects:
276 * syncer_workitem_pending
277 * syncer_worklist_len
280 static struct mtx sync_mtx;
281 static struct cv sync_wakeup;
283 #define SYNCER_MAXDELAY 32
284 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
285 static int syncdelay = 30; /* max time to delay syncing data */
286 static int filedelay = 30; /* time to delay syncing files */
287 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
288 "Time to delay syncing files (in seconds)");
289 static int dirdelay = 29; /* time to delay syncing directories */
290 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
291 "Time to delay syncing directories (in seconds)");
292 static int metadelay = 28; /* time to delay syncing metadata */
293 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
294 "Time to delay syncing metadata (in seconds)");
295 static int rushjob; /* number of slots to run ASAP */
296 static int stat_rush_requests; /* number of times I/O speeded up */
297 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
298 "Number of times I/O speeded up (rush requests)");
301 * When shutting down the syncer, run it at four times normal speed.
303 #define SYNCER_SHUTDOWN_SPEEDUP 4
304 static int sync_vnode_count;
305 static int syncer_worklist_len;
306 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
309 /* Target for maximum number of vnodes. */
311 static int gapvnodes; /* gap between wanted and desired */
312 static int vhiwat; /* enough extras after expansion */
313 static int vlowat; /* minimal extras before expansion */
314 static int vstir; /* nonzero to stir non-free vnodes */
315 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */
318 sysctl_update_desiredvnodes(SYSCTL_HANDLER_ARGS)
320 int error, old_desiredvnodes;
322 old_desiredvnodes = desiredvnodes;
323 if ((error = sysctl_handle_int(oidp, arg1, arg2, req)) != 0)
325 if (old_desiredvnodes != desiredvnodes) {
326 wantfreevnodes = desiredvnodes / 4;
327 /* XXX locking seems to be incomplete. */
328 vfs_hash_changesize(desiredvnodes);
329 cache_changesize(desiredvnodes);
334 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
335 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &desiredvnodes, 0,
336 sysctl_update_desiredvnodes, "I", "Target for maximum number of vnodes");
337 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
338 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)");
339 static int vnlru_nowhere;
340 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
341 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
344 sysctl_try_reclaim_vnode(SYSCTL_HANDLER_ARGS)
349 unsigned long ndflags;
352 if (req->newptr == NULL)
354 if (req->newlen > PATH_MAX)
357 buf = malloc(PATH_MAX + 1, M_TEMP, M_WAITOK);
358 error = SYSCTL_IN(req, buf, req->newlen);
362 buf[req->newlen] = '\0';
364 ndflags = LOCKLEAF | NOFOLLOW | AUDITVNODE1 | NOCACHE | SAVENAME;
365 NDINIT(&nd, LOOKUP, ndflags, UIO_SYSSPACE, buf, curthread);
366 if ((error = namei(&nd)) != 0)
370 if ((vp->v_iflag & VI_DOOMED) != 0) {
372 * This vnode is being recycled. Return != 0 to let the caller
373 * know that the sysctl had no effect. Return EAGAIN because a
374 * subsequent call will likely succeed (since namei will create
375 * a new vnode if necessary)
381 counter_u64_add(recycles_count, 1);
391 sysctl_ftry_reclaim_vnode(SYSCTL_HANDLER_ARGS)
393 struct thread *td = curthread;
399 if (req->newptr == NULL)
402 error = sysctl_handle_int(oidp, &fd, 0, req);
405 error = getvnode(curthread, fd, &cap_fcntl_rights, &fp);
410 error = vn_lock(vp, LK_EXCLUSIVE);
414 counter_u64_add(recycles_count, 1);
422 SYSCTL_PROC(_debug, OID_AUTO, try_reclaim_vnode,
423 CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
424 sysctl_try_reclaim_vnode, "A", "Try to reclaim a vnode by its pathname");
425 SYSCTL_PROC(_debug, OID_AUTO, ftry_reclaim_vnode,
426 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
427 sysctl_ftry_reclaim_vnode, "I",
428 "Try to reclaim a vnode by its file descriptor");
430 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
434 * Support for the bufobj clean & dirty pctrie.
437 buf_trie_alloc(struct pctrie *ptree)
440 return uma_zalloc(buf_trie_zone, M_NOWAIT);
444 buf_trie_free(struct pctrie *ptree, void *node)
447 uma_zfree(buf_trie_zone, node);
449 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
452 * Initialize the vnode management data structures.
454 * Reevaluate the following cap on the number of vnodes after the physical
455 * memory size exceeds 512GB. In the limit, as the physical memory size
456 * grows, the ratio of the memory size in KB to vnodes approaches 64:1.
458 #ifndef MAXVNODES_MAX
459 #define MAXVNODES_MAX (512 * 1024 * 1024 / 64) /* 8M */
463 * Initialize a vnode as it first enters the zone.
466 vnode_init(void *mem, int size, int flags)
475 vp->v_vnlock = &vp->v_lock;
476 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
478 * By default, don't allow shared locks unless filesystems opt-in.
480 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT,
481 LK_NOSHARE | LK_IS_VNODE);
485 bufobj_init(&vp->v_bufobj, vp);
487 * Initialize namecache.
489 LIST_INIT(&vp->v_cache_src);
490 TAILQ_INIT(&vp->v_cache_dst);
492 * Initialize rangelocks.
494 rangelock_init(&vp->v_rl);
499 * Free a vnode when it is cleared from the zone.
502 vnode_fini(void *mem, int size)
508 rangelock_destroy(&vp->v_rl);
509 lockdestroy(vp->v_vnlock);
510 mtx_destroy(&vp->v_interlock);
512 rw_destroy(BO_LOCKPTR(bo));
516 * Provide the size of NFS nclnode and NFS fh for calculation of the
517 * vnode memory consumption. The size is specified directly to
518 * eliminate dependency on NFS-private header.
520 * Other filesystems may use bigger or smaller (like UFS and ZFS)
521 * private inode data, but the NFS-based estimation is ample enough.
522 * Still, we care about differences in the size between 64- and 32-bit
525 * Namecache structure size is heuristically
526 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1.
529 #define NFS_NCLNODE_SZ (528 + 64)
532 #define NFS_NCLNODE_SZ (360 + 32)
537 vntblinit(void *dummy __unused)
540 int physvnodes, virtvnodes;
543 * Desiredvnodes is a function of the physical memory size and the
544 * kernel's heap size. Generally speaking, it scales with the
545 * physical memory size. The ratio of desiredvnodes to the physical
546 * memory size is 1:16 until desiredvnodes exceeds 98,304.
548 * marginal ratio of desiredvnodes to the physical memory size is
549 * 1:64. However, desiredvnodes is limited by the kernel's heap
550 * size. The memory required by desiredvnodes vnodes and vm objects
551 * must not exceed 1/10th of the kernel's heap size.
553 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 +
554 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64;
555 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) +
556 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ));
557 desiredvnodes = min(physvnodes, virtvnodes);
558 if (desiredvnodes > MAXVNODES_MAX) {
560 printf("Reducing kern.maxvnodes %d -> %d\n",
561 desiredvnodes, MAXVNODES_MAX);
562 desiredvnodes = MAXVNODES_MAX;
564 wantfreevnodes = desiredvnodes / 4;
565 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
566 TAILQ_INIT(&vnode_free_list);
567 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
568 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
569 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0);
570 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
571 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
573 * Preallocate enough nodes to support one-per buf so that
574 * we can not fail an insert. reassignbuf() callers can not
575 * tolerate the insertion failure.
577 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
578 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
579 UMA_ZONE_NOFREE | UMA_ZONE_VM);
580 uma_prealloc(buf_trie_zone, nbuf);
582 vnodes_created = counter_u64_alloc(M_WAITOK);
583 recycles_count = counter_u64_alloc(M_WAITOK);
584 free_owe_inact = counter_u64_alloc(M_WAITOK);
587 * Initialize the filesystem syncer.
589 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
591 syncer_maxdelay = syncer_mask + 1;
592 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
593 cv_init(&sync_wakeup, "syncer");
594 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
598 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
602 * Mark a mount point as busy. Used to synchronize access and to delay
603 * unmounting. Eventually, mountlist_mtx is not released on failure.
605 * vfs_busy() is a custom lock, it can block the caller.
606 * vfs_busy() only sleeps if the unmount is active on the mount point.
607 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
608 * vnode belonging to mp.
610 * Lookup uses vfs_busy() to traverse mount points.
612 * / vnode lock A / vnode lock (/var) D
613 * /var vnode lock B /log vnode lock(/var/log) E
614 * vfs_busy lock C vfs_busy lock F
616 * Within each file system, the lock order is C->A->B and F->D->E.
618 * When traversing across mounts, the system follows that lock order:
624 * The lookup() process for namei("/var") illustrates the process:
625 * VOP_LOOKUP() obtains B while A is held
626 * vfs_busy() obtains a shared lock on F while A and B are held
627 * vput() releases lock on B
628 * vput() releases lock on A
629 * VFS_ROOT() obtains lock on D while shared lock on F is held
630 * vfs_unbusy() releases shared lock on F
631 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
632 * Attempt to lock A (instead of vp_crossmp) while D is held would
633 * violate the global order, causing deadlocks.
635 * dounmount() locks B while F is drained.
638 vfs_busy(struct mount *mp, int flags)
641 MPASS((flags & ~MBF_MASK) == 0);
642 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
647 * If mount point is currently being unmounted, sleep until the
648 * mount point fate is decided. If thread doing the unmounting fails,
649 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
650 * that this mount point has survived the unmount attempt and vfs_busy
651 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
652 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
653 * about to be really destroyed. vfs_busy needs to release its
654 * reference on the mount point in this case and return with ENOENT,
655 * telling the caller that mount mount it tried to busy is no longer
658 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
659 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
662 CTR1(KTR_VFS, "%s: failed busying before sleeping",
666 if (flags & MBF_MNTLSTLOCK)
667 mtx_unlock(&mountlist_mtx);
668 mp->mnt_kern_flag |= MNTK_MWAIT;
669 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
670 if (flags & MBF_MNTLSTLOCK)
671 mtx_lock(&mountlist_mtx);
674 if (flags & MBF_MNTLSTLOCK)
675 mtx_unlock(&mountlist_mtx);
682 * Free a busy filesystem.
685 vfs_unbusy(struct mount *mp)
688 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
691 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
693 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
694 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
695 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
696 mp->mnt_kern_flag &= ~MNTK_DRAINING;
697 wakeup(&mp->mnt_lockref);
703 * Lookup a mount point by filesystem identifier.
706 vfs_getvfs(fsid_t *fsid)
710 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
711 mtx_lock(&mountlist_mtx);
712 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
713 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
714 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
716 mtx_unlock(&mountlist_mtx);
720 mtx_unlock(&mountlist_mtx);
721 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
722 return ((struct mount *) 0);
726 * Lookup a mount point by filesystem identifier, busying it before
729 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
730 * cache for popular filesystem identifiers. The cache is lockess, using
731 * the fact that struct mount's are never freed. In worst case we may
732 * get pointer to unmounted or even different filesystem, so we have to
733 * check what we got, and go slow way if so.
736 vfs_busyfs(fsid_t *fsid)
738 #define FSID_CACHE_SIZE 256
739 typedef struct mount * volatile vmp_t;
740 static vmp_t cache[FSID_CACHE_SIZE];
745 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
746 hash = fsid->val[0] ^ fsid->val[1];
747 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
750 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
751 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
753 if (vfs_busy(mp, 0) != 0) {
757 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
758 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
764 mtx_lock(&mountlist_mtx);
765 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
766 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
767 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
768 error = vfs_busy(mp, MBF_MNTLSTLOCK);
771 mtx_unlock(&mountlist_mtx);
778 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
779 mtx_unlock(&mountlist_mtx);
780 return ((struct mount *) 0);
784 * Check if a user can access privileged mount options.
787 vfs_suser(struct mount *mp, struct thread *td)
791 if (jailed(td->td_ucred)) {
793 * If the jail of the calling thread lacks permission for
794 * this type of file system, deny immediately.
796 if (!prison_allow(td->td_ucred, mp->mnt_vfc->vfc_prison_flag))
800 * If the file system was mounted outside the jail of the
801 * calling thread, deny immediately.
803 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
808 * If file system supports delegated administration, we don't check
809 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
810 * by the file system itself.
811 * If this is not the user that did original mount, we check for
812 * the PRIV_VFS_MOUNT_OWNER privilege.
814 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
815 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
816 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
823 * Get a new unique fsid. Try to make its val[0] unique, since this value
824 * will be used to create fake device numbers for stat(). Also try (but
825 * not so hard) make its val[0] unique mod 2^16, since some emulators only
826 * support 16-bit device numbers. We end up with unique val[0]'s for the
827 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
829 * Keep in mind that several mounts may be running in parallel. Starting
830 * the search one past where the previous search terminated is both a
831 * micro-optimization and a defense against returning the same fsid to
835 vfs_getnewfsid(struct mount *mp)
837 static uint16_t mntid_base;
842 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
843 mtx_lock(&mntid_mtx);
844 mtype = mp->mnt_vfc->vfc_typenum;
845 tfsid.val[1] = mtype;
846 mtype = (mtype & 0xFF) << 24;
848 tfsid.val[0] = makedev(255,
849 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
851 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
855 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
856 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
857 mtx_unlock(&mntid_mtx);
861 * Knob to control the precision of file timestamps:
863 * 0 = seconds only; nanoseconds zeroed.
864 * 1 = seconds and nanoseconds, accurate within 1/HZ.
865 * 2 = seconds and nanoseconds, truncated to microseconds.
866 * >=3 = seconds and nanoseconds, maximum precision.
868 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
870 static int timestamp_precision = TSP_USEC;
871 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
872 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
873 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, "
874 "3+: sec + ns (max. precision))");
877 * Get a current timestamp.
880 vfs_timestamp(struct timespec *tsp)
884 switch (timestamp_precision) {
886 tsp->tv_sec = time_second;
894 TIMEVAL_TO_TIMESPEC(&tv, tsp);
904 * Set vnode attributes to VNOVAL
907 vattr_null(struct vattr *vap)
911 vap->va_size = VNOVAL;
912 vap->va_bytes = VNOVAL;
913 vap->va_mode = VNOVAL;
914 vap->va_nlink = VNOVAL;
915 vap->va_uid = VNOVAL;
916 vap->va_gid = VNOVAL;
917 vap->va_fsid = VNOVAL;
918 vap->va_fileid = VNOVAL;
919 vap->va_blocksize = VNOVAL;
920 vap->va_rdev = VNOVAL;
921 vap->va_atime.tv_sec = VNOVAL;
922 vap->va_atime.tv_nsec = VNOVAL;
923 vap->va_mtime.tv_sec = VNOVAL;
924 vap->va_mtime.tv_nsec = VNOVAL;
925 vap->va_ctime.tv_sec = VNOVAL;
926 vap->va_ctime.tv_nsec = VNOVAL;
927 vap->va_birthtime.tv_sec = VNOVAL;
928 vap->va_birthtime.tv_nsec = VNOVAL;
929 vap->va_flags = VNOVAL;
930 vap->va_gen = VNOVAL;
935 * This routine is called when we have too many vnodes. It attempts
936 * to free <count> vnodes and will potentially free vnodes that still
937 * have VM backing store (VM backing store is typically the cause
938 * of a vnode blowout so we want to do this). Therefore, this operation
939 * is not considered cheap.
941 * A number of conditions may prevent a vnode from being reclaimed.
942 * the buffer cache may have references on the vnode, a directory
943 * vnode may still have references due to the namei cache representing
944 * underlying files, or the vnode may be in active use. It is not
945 * desirable to reuse such vnodes. These conditions may cause the
946 * number of vnodes to reach some minimum value regardless of what
947 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
949 * @param mp Try to reclaim vnodes from this mountpoint
950 * @param reclaim_nc_src Only reclaim directories with outgoing namecache
951 * entries if this argument is strue
952 * @param trigger Only reclaim vnodes with fewer than this many resident
954 * @return The number of vnodes that were reclaimed.
957 vlrureclaim(struct mount *mp, bool reclaim_nc_src, int trigger)
960 int count, done, target;
963 vn_start_write(NULL, &mp, V_WAIT);
965 count = mp->mnt_nvnodelistsize;
966 target = count * (int64_t)gapvnodes / imax(desiredvnodes, 1);
967 target = target / 10 + 1;
968 while (count != 0 && done < target) {
969 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
970 while (vp != NULL && vp->v_type == VMARKER)
971 vp = TAILQ_NEXT(vp, v_nmntvnodes);
975 * XXX LRU is completely broken for non-free vnodes. First
976 * by calling here in mountpoint order, then by moving
977 * unselected vnodes to the end here, and most grossly by
978 * removing the vlruvp() function that was supposed to
979 * maintain the order. (This function was born broken
980 * since syncer problems prevented it doing anything.) The
981 * order is closer to LRC (C = Created).
983 * LRU reclaiming of vnodes seems to have last worked in
984 * FreeBSD-3 where LRU wasn't mentioned under any spelling.
985 * Then there was no hold count, and inactive vnodes were
986 * simply put on the free list in LRU order. The separate
987 * lists also break LRU. We prefer to reclaim from the
988 * free list for technical reasons. This tends to thrash
989 * the free list to keep very unrecently used held vnodes.
990 * The problem is mitigated by keeping the free list large.
992 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
993 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
998 * If it's been deconstructed already, it's still
999 * referenced, or it exceeds the trigger, skip it.
1000 * Also skip free vnodes. We are trying to make space
1001 * to expand the free list, not reduce it.
1003 if (vp->v_usecount ||
1004 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
1005 ((vp->v_iflag & VI_FREE) != 0) ||
1006 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
1007 vp->v_object->resident_page_count > trigger)) {
1013 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
1015 goto next_iter_mntunlocked;
1019 * v_usecount may have been bumped after VOP_LOCK() dropped
1020 * the vnode interlock and before it was locked again.
1022 * It is not necessary to recheck VI_DOOMED because it can
1023 * only be set by another thread that holds both the vnode
1024 * lock and vnode interlock. If another thread has the
1025 * vnode lock before we get to VOP_LOCK() and obtains the
1026 * vnode interlock after VOP_LOCK() drops the vnode
1027 * interlock, the other thread will be unable to drop the
1028 * vnode lock before our VOP_LOCK() call fails.
1030 if (vp->v_usecount ||
1031 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
1032 (vp->v_iflag & VI_FREE) != 0 ||
1033 (vp->v_object != NULL &&
1034 vp->v_object->resident_page_count > trigger)) {
1037 goto next_iter_mntunlocked;
1039 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
1040 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
1041 counter_u64_add(recycles_count, 1);
1046 next_iter_mntunlocked:
1047 if (!should_yield())
1051 if (!should_yield())
1055 kern_yield(PRI_USER);
1060 vn_finished_write(mp);
1064 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */
1065 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free,
1067 "limit on vnode free requests per call to the vnlru_free routine");
1070 * Attempt to reduce the free list by the requested amount.
1073 vnlru_free_locked(int count, struct vfsops *mnt_op)
1079 tried_batches = false;
1080 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1081 if (count > max_vnlru_free)
1082 count = max_vnlru_free;
1083 for (; count > 0; count--) {
1084 vp = TAILQ_FIRST(&vnode_free_list);
1086 * The list can be modified while the free_list_mtx
1087 * has been dropped and vp could be NULL here.
1092 mtx_unlock(&vnode_free_list_mtx);
1093 vnlru_return_batches(mnt_op);
1094 tried_batches = true;
1095 mtx_lock(&vnode_free_list_mtx);
1099 VNASSERT(vp->v_op != NULL, vp,
1100 ("vnlru_free: vnode already reclaimed."));
1101 KASSERT((vp->v_iflag & VI_FREE) != 0,
1102 ("Removing vnode not on freelist"));
1103 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1104 ("Mangling active vnode"));
1105 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
1108 * Don't recycle if our vnode is from different type
1109 * of mount point. Note that mp is type-safe, the
1110 * check does not reach unmapped address even if
1111 * vnode is reclaimed.
1112 * Don't recycle if we can't get the interlock without
1115 if ((mnt_op != NULL && (mp = vp->v_mount) != NULL &&
1116 mp->mnt_op != mnt_op) || !VI_TRYLOCK(vp)) {
1117 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
1120 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
1121 vp, ("vp inconsistent on freelist"));
1124 * The clear of VI_FREE prevents activation of the
1125 * vnode. There is no sense in putting the vnode on
1126 * the mount point active list, only to remove it
1127 * later during recycling. Inline the relevant part
1128 * of vholdl(), to avoid triggering assertions or
1132 vp->v_iflag &= ~VI_FREE;
1133 VNODE_REFCOUNT_FENCE_REL();
1134 refcount_acquire(&vp->v_holdcnt);
1136 mtx_unlock(&vnode_free_list_mtx);
1140 * If the recycled succeeded this vdrop will actually free
1141 * the vnode. If not it will simply place it back on
1145 mtx_lock(&vnode_free_list_mtx);
1150 vnlru_free(int count, struct vfsops *mnt_op)
1153 mtx_lock(&vnode_free_list_mtx);
1154 vnlru_free_locked(count, mnt_op);
1155 mtx_unlock(&vnode_free_list_mtx);
1159 /* XXX some names and initialization are bad for limits and watermarks. */
1165 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100);
1166 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */
1167 vlowat = vhiwat / 2;
1168 if (numvnodes > desiredvnodes)
1170 space = desiredvnodes - numvnodes;
1171 if (freevnodes > wantfreevnodes)
1172 space += freevnodes - wantfreevnodes;
1177 vnlru_return_batch_locked(struct mount *mp)
1181 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
1183 if (mp->mnt_tmpfreevnodelistsize == 0)
1186 TAILQ_FOREACH(vp, &mp->mnt_tmpfreevnodelist, v_actfreelist) {
1187 VNASSERT((vp->v_mflag & VMP_TMPMNTFREELIST) != 0, vp,
1188 ("vnode without VMP_TMPMNTFREELIST on mnt_tmpfreevnodelist"));
1189 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
1191 mtx_lock(&vnode_free_list_mtx);
1192 TAILQ_CONCAT(&vnode_free_list, &mp->mnt_tmpfreevnodelist, v_actfreelist);
1193 freevnodes += mp->mnt_tmpfreevnodelistsize;
1194 mtx_unlock(&vnode_free_list_mtx);
1195 mp->mnt_tmpfreevnodelistsize = 0;
1199 vnlru_return_batch(struct mount *mp)
1202 mtx_lock(&mp->mnt_listmtx);
1203 vnlru_return_batch_locked(mp);
1204 mtx_unlock(&mp->mnt_listmtx);
1208 vnlru_return_batches(struct vfsops *mnt_op)
1210 struct mount *mp, *nmp;
1213 mtx_lock(&mountlist_mtx);
1214 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1215 need_unbusy = false;
1216 if (mnt_op != NULL && mp->mnt_op != mnt_op)
1218 if (mp->mnt_tmpfreevnodelistsize == 0)
1220 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) == 0) {
1221 vnlru_return_batch(mp);
1223 mtx_lock(&mountlist_mtx);
1226 nmp = TAILQ_NEXT(mp, mnt_list);
1230 mtx_unlock(&mountlist_mtx);
1234 * Attempt to recycle vnodes in a context that is always safe to block.
1235 * Calling vlrurecycle() from the bowels of filesystem code has some
1236 * interesting deadlock problems.
1238 static struct proc *vnlruproc;
1239 static int vnlruproc_sig;
1244 struct mount *mp, *nmp;
1245 unsigned long onumvnodes;
1246 int done, force, trigger, usevnodes;
1247 bool reclaim_nc_src;
1249 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc,
1250 SHUTDOWN_PRI_FIRST);
1254 kproc_suspend_check(vnlruproc);
1255 mtx_lock(&vnode_free_list_mtx);
1257 * If numvnodes is too large (due to desiredvnodes being
1258 * adjusted using its sysctl, or emergency growth), first
1259 * try to reduce it by discarding from the free list.
1261 if (numvnodes > desiredvnodes)
1262 vnlru_free_locked(numvnodes - desiredvnodes, NULL);
1264 * Sleep if the vnode cache is in a good state. This is
1265 * when it is not over-full and has space for about a 4%
1266 * or 9% expansion (by growing its size or inexcessively
1267 * reducing its free list). Otherwise, try to reclaim
1268 * space for a 10% expansion.
1270 if (vstir && force == 0) {
1274 if (vspace() >= vlowat && force == 0) {
1276 wakeup(&vnlruproc_sig);
1277 msleep(vnlruproc, &vnode_free_list_mtx,
1278 PVFS|PDROP, "vlruwt", hz);
1281 mtx_unlock(&vnode_free_list_mtx);
1283 onumvnodes = numvnodes;
1285 * Calculate parameters for recycling. These are the same
1286 * throughout the loop to give some semblance of fairness.
1287 * The trigger point is to avoid recycling vnodes with lots
1288 * of resident pages. We aren't trying to free memory; we
1289 * are trying to recycle or at least free vnodes.
1291 if (numvnodes <= desiredvnodes)
1292 usevnodes = numvnodes - freevnodes;
1294 usevnodes = numvnodes;
1298 * The trigger value is is chosen to give a conservatively
1299 * large value to ensure that it alone doesn't prevent
1300 * making progress. The value can easily be so large that
1301 * it is effectively infinite in some congested and
1302 * misconfigured cases, and this is necessary. Normally
1303 * it is about 8 to 100 (pages), which is quite large.
1305 trigger = vm_cnt.v_page_count * 2 / usevnodes;
1307 trigger = vsmalltrigger;
1308 reclaim_nc_src = force >= 3;
1309 mtx_lock(&mountlist_mtx);
1310 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1311 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
1312 nmp = TAILQ_NEXT(mp, mnt_list);
1315 done += vlrureclaim(mp, reclaim_nc_src, trigger);
1316 mtx_lock(&mountlist_mtx);
1317 nmp = TAILQ_NEXT(mp, mnt_list);
1320 mtx_unlock(&mountlist_mtx);
1321 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes)
1322 uma_reclaim(UMA_RECLAIM_DRAIN);
1324 if (force == 0 || force == 1) {
1334 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
1336 kern_yield(PRI_USER);
1338 * After becoming active to expand above low water, keep
1339 * active until above high water.
1341 force = vspace() < vhiwat;
1345 static struct kproc_desc vnlru_kp = {
1350 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
1354 * Routines having to do with the management of the vnode table.
1358 * Try to recycle a freed vnode. We abort if anyone picks up a reference
1359 * before we actually vgone(). This function must be called with the vnode
1360 * held to prevent the vnode from being returned to the free list midway
1364 vtryrecycle(struct vnode *vp)
1368 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
1369 VNASSERT(vp->v_holdcnt, vp,
1370 ("vtryrecycle: Recycling vp %p without a reference.", vp));
1372 * This vnode may found and locked via some other list, if so we
1373 * can't recycle it yet.
1375 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1377 "%s: impossible to recycle, vp %p lock is already held",
1379 return (EWOULDBLOCK);
1382 * Don't recycle if its filesystem is being suspended.
1384 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1387 "%s: impossible to recycle, cannot start the write for %p",
1392 * If we got this far, we need to acquire the interlock and see if
1393 * anyone picked up this vnode from another list. If not, we will
1394 * mark it with DOOMED via vgonel() so that anyone who does find it
1395 * will skip over it.
1398 if (vp->v_usecount) {
1401 vn_finished_write(vnmp);
1403 "%s: impossible to recycle, %p is already referenced",
1407 if ((vp->v_iflag & VI_DOOMED) == 0) {
1408 counter_u64_add(recycles_count, 1);
1413 vn_finished_write(vnmp);
1421 if (vspace() < vlowat && vnlruproc_sig == 0) {
1428 * Wait if necessary for space for a new vnode.
1431 getnewvnode_wait(int suspended)
1434 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1435 if (numvnodes >= desiredvnodes) {
1438 * The file system is being suspended. We cannot
1439 * risk a deadlock here, so allow allocation of
1440 * another vnode even if this would give too many.
1444 if (vnlruproc_sig == 0) {
1445 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1448 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1451 /* Post-adjust like the pre-adjust in getnewvnode(). */
1452 if (numvnodes + 1 > desiredvnodes && freevnodes > 1)
1453 vnlru_free_locked(1, NULL);
1454 return (numvnodes >= desiredvnodes ? ENFILE : 0);
1458 * This hack is fragile, and probably not needed any more now that the
1459 * watermark handling works.
1462 getnewvnode_reserve(u_int count)
1466 /* Pre-adjust like the pre-adjust in getnewvnode(), with any count. */
1467 /* XXX no longer so quick, but this part is not racy. */
1468 mtx_lock(&vnode_free_list_mtx);
1469 if (numvnodes + count > desiredvnodes && freevnodes > wantfreevnodes)
1470 vnlru_free_locked(ulmin(numvnodes + count - desiredvnodes,
1471 freevnodes - wantfreevnodes), NULL);
1472 mtx_unlock(&vnode_free_list_mtx);
1475 /* First try to be quick and racy. */
1476 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1477 td->td_vp_reserv += count;
1478 vcheckspace(); /* XXX no longer so quick, but more racy */
1481 atomic_subtract_long(&numvnodes, count);
1483 mtx_lock(&vnode_free_list_mtx);
1485 if (getnewvnode_wait(0) == 0) {
1488 atomic_add_long(&numvnodes, 1);
1492 mtx_unlock(&vnode_free_list_mtx);
1496 * This hack is fragile, especially if desiredvnodes or wantvnodes are
1497 * misconfgured or changed significantly. Reducing desiredvnodes below
1498 * the reserved amount should cause bizarre behaviour like reducing it
1499 * below the number of active vnodes -- the system will try to reduce
1500 * numvnodes to match, but should fail, so the subtraction below should
1504 getnewvnode_drop_reserve(void)
1509 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1510 td->td_vp_reserv = 0;
1514 * Return the next vnode from the free list.
1517 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1522 struct lock_object *lo;
1523 static int cyclecount;
1526 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1529 if (td->td_vp_reserv > 0) {
1530 td->td_vp_reserv -= 1;
1533 mtx_lock(&vnode_free_list_mtx);
1534 if (numvnodes < desiredvnodes)
1536 else if (cyclecount++ >= freevnodes) {
1541 * Grow the vnode cache if it will not be above its target max
1542 * after growing. Otherwise, if the free list is nonempty, try
1543 * to reclaim 1 item from it before growing the cache (possibly
1544 * above its target max if the reclamation failed or is delayed).
1545 * Otherwise, wait for some space. In all cases, schedule
1546 * vnlru_proc() if we are getting short of space. The watermarks
1547 * should be chosen so that we never wait or even reclaim from
1548 * the free list to below its target minimum.
1550 if (numvnodes + 1 <= desiredvnodes)
1552 else if (freevnodes > 0)
1553 vnlru_free_locked(1, NULL);
1555 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1557 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1559 mtx_unlock(&vnode_free_list_mtx);
1565 atomic_add_long(&numvnodes, 1);
1566 mtx_unlock(&vnode_free_list_mtx);
1568 counter_u64_add(vnodes_created, 1);
1569 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK);
1571 * Locks are given the generic name "vnode" when created.
1572 * Follow the historic practice of using the filesystem
1573 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc.
1575 * Locks live in a witness group keyed on their name. Thus,
1576 * when a lock is renamed, it must also move from the witness
1577 * group of its old name to the witness group of its new name.
1579 * The change only needs to be made when the vnode moves
1580 * from one filesystem type to another. We ensure that each
1581 * filesystem use a single static name pointer for its tag so
1582 * that we can compare pointers rather than doing a strcmp().
1584 lo = &vp->v_vnlock->lock_object;
1585 if (lo->lo_name != tag) {
1587 WITNESS_DESTROY(lo);
1588 WITNESS_INIT(lo, tag);
1591 * By default, don't allow shared locks unless filesystems opt-in.
1593 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE;
1595 * Finalize various vnode identity bits.
1597 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp));
1598 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp));
1599 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp));
1603 v_init_counters(vp);
1604 vp->v_bufobj.bo_ops = &buf_ops_bio;
1606 if (mp == NULL && vops != &dead_vnodeops)
1607 printf("NULL mp in getnewvnode(9), tag %s\n", tag);
1611 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1612 mac_vnode_associate_singlelabel(mp, vp);
1615 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize;
1616 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1617 vp->v_vflag |= VV_NOKNOTE;
1621 * For the filesystems which do not use vfs_hash_insert(),
1622 * still initialize v_hash to have vfs_hash_index() useful.
1623 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1626 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1633 * Delete from old mount point vnode list, if on one.
1636 delmntque(struct vnode *vp)
1646 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1647 ("Active vnode list size %d > Vnode list size %d",
1648 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1649 active = vp->v_iflag & VI_ACTIVE;
1650 vp->v_iflag &= ~VI_ACTIVE;
1652 mtx_lock(&mp->mnt_listmtx);
1653 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1654 mp->mnt_activevnodelistsize--;
1655 mtx_unlock(&mp->mnt_listmtx);
1659 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1660 ("bad mount point vnode list size"));
1661 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1662 mp->mnt_nvnodelistsize--;
1668 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1672 vp->v_op = &dead_vnodeops;
1678 * Insert into list of vnodes for the new mount point, if available.
1681 insmntque1(struct vnode *vp, struct mount *mp,
1682 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1685 KASSERT(vp->v_mount == NULL,
1686 ("insmntque: vnode already on per mount vnode list"));
1687 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1688 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1691 * We acquire the vnode interlock early to ensure that the
1692 * vnode cannot be recycled by another process releasing a
1693 * holdcnt on it before we get it on both the vnode list
1694 * and the active vnode list. The mount mutex protects only
1695 * manipulation of the vnode list and the vnode freelist
1696 * mutex protects only manipulation of the active vnode list.
1697 * Hence the need to hold the vnode interlock throughout.
1701 if (((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 &&
1702 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1703 mp->mnt_nvnodelistsize == 0)) &&
1704 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1713 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1714 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1715 ("neg mount point vnode list size"));
1716 mp->mnt_nvnodelistsize++;
1717 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1718 ("Activating already active vnode"));
1719 vp->v_iflag |= VI_ACTIVE;
1720 mtx_lock(&mp->mnt_listmtx);
1721 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1722 mp->mnt_activevnodelistsize++;
1723 mtx_unlock(&mp->mnt_listmtx);
1730 insmntque(struct vnode *vp, struct mount *mp)
1733 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1737 * Flush out and invalidate all buffers associated with a bufobj
1738 * Called with the underlying object locked.
1741 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1746 if (flags & V_SAVE) {
1747 error = bufobj_wwait(bo, slpflag, slptimeo);
1752 if (bo->bo_dirty.bv_cnt > 0) {
1754 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1757 * XXX We could save a lock/unlock if this was only
1758 * enabled under INVARIANTS
1761 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1762 panic("vinvalbuf: dirty bufs");
1766 * If you alter this loop please notice that interlock is dropped and
1767 * reacquired in flushbuflist. Special care is needed to ensure that
1768 * no race conditions occur from this.
1771 error = flushbuflist(&bo->bo_clean,
1772 flags, bo, slpflag, slptimeo);
1773 if (error == 0 && !(flags & V_CLEANONLY))
1774 error = flushbuflist(&bo->bo_dirty,
1775 flags, bo, slpflag, slptimeo);
1776 if (error != 0 && error != EAGAIN) {
1780 } while (error != 0);
1783 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1784 * have write I/O in-progress but if there is a VM object then the
1785 * VM object can also have read-I/O in-progress.
1788 bufobj_wwait(bo, 0, 0);
1789 if ((flags & V_VMIO) == 0 && bo->bo_object != NULL) {
1791 vm_object_pip_wait_unlocked(bo->bo_object, "bovlbx");
1794 } while (bo->bo_numoutput > 0);
1798 * Destroy the copy in the VM cache, too.
1800 if (bo->bo_object != NULL &&
1801 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) {
1802 VM_OBJECT_WLOCK(bo->bo_object);
1803 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1804 OBJPR_CLEANONLY : 0);
1805 VM_OBJECT_WUNLOCK(bo->bo_object);
1810 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO |
1811 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 ||
1812 bo->bo_clean.bv_cnt > 0))
1813 panic("vinvalbuf: flush failed");
1814 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 &&
1815 bo->bo_dirty.bv_cnt > 0)
1816 panic("vinvalbuf: flush dirty failed");
1823 * Flush out and invalidate all buffers associated with a vnode.
1824 * Called with the underlying object locked.
1827 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1830 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1831 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1832 if (vp->v_object != NULL && vp->v_object->handle != vp)
1834 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1838 * Flush out buffers on the specified list.
1842 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1845 struct buf *bp, *nbp;
1850 ASSERT_BO_WLOCKED(bo);
1853 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1855 * If we are flushing both V_NORMAL and V_ALT buffers then
1856 * do not skip any buffers. If we are flushing only V_NORMAL
1857 * buffers then skip buffers marked as BX_ALTDATA. If we are
1858 * flushing only V_ALT buffers then skip buffers not marked
1861 if (((flags & (V_NORMAL | V_ALT)) != (V_NORMAL | V_ALT)) &&
1862 (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA) != 0) ||
1863 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0))) {
1867 lblkno = nbp->b_lblkno;
1868 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1871 error = BUF_TIMELOCK(bp,
1872 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1873 "flushbuf", slpflag, slptimeo);
1876 return (error != ENOLCK ? error : EAGAIN);
1878 KASSERT(bp->b_bufobj == bo,
1879 ("bp %p wrong b_bufobj %p should be %p",
1880 bp, bp->b_bufobj, bo));
1882 * XXX Since there are no node locks for NFS, I
1883 * believe there is a slight chance that a delayed
1884 * write will occur while sleeping just above, so
1887 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1890 bp->b_flags |= B_ASYNC;
1893 return (EAGAIN); /* XXX: why not loop ? */
1896 bp->b_flags |= (B_INVAL | B_RELBUF);
1897 bp->b_flags &= ~B_ASYNC;
1902 nbp = gbincore(bo, lblkno);
1903 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1905 break; /* nbp invalid */
1911 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn)
1917 ASSERT_BO_LOCKED(bo);
1919 for (lblkno = startn;;) {
1921 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno);
1922 if (bp == NULL || bp->b_lblkno >= endn ||
1923 bp->b_lblkno < startn)
1925 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1926 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0);
1929 if (error == ENOLCK)
1933 KASSERT(bp->b_bufobj == bo,
1934 ("bp %p wrong b_bufobj %p should be %p",
1935 bp, bp->b_bufobj, bo));
1936 lblkno = bp->b_lblkno + 1;
1937 if ((bp->b_flags & B_MANAGED) == 0)
1939 bp->b_flags |= B_RELBUF;
1941 * In the VMIO case, use the B_NOREUSE flag to hint that the
1942 * pages backing each buffer in the range are unlikely to be
1943 * reused. Dirty buffers will have the hint applied once
1944 * they've been written.
1946 if ((bp->b_flags & B_VMIO) != 0)
1947 bp->b_flags |= B_NOREUSE;
1955 * Truncate a file's buffer and pages to a specified length. This
1956 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1960 vtruncbuf(struct vnode *vp, off_t length, int blksize)
1962 struct buf *bp, *nbp;
1966 CTR4(KTR_VFS, "%s: vp %p with block %d:%ju", __func__,
1967 vp, blksize, (uintmax_t)length);
1970 * Round up to the *next* lbn.
1972 startlbn = howmany(length, blksize);
1974 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1980 while (v_inval_buf_range_locked(vp, bo, startlbn, INT64_MAX) == EAGAIN)
1985 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1986 if (bp->b_lblkno > 0)
1989 * Since we hold the vnode lock this should only
1990 * fail if we're racing with the buf daemon.
1993 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1994 BO_LOCKPTR(bo)) == ENOLCK)
1995 goto restart_unlocked;
1997 VNASSERT((bp->b_flags & B_DELWRI), vp,
1998 ("buf(%p) on dirty queue without DELWRI", bp));
2007 bufobj_wwait(bo, 0, 0);
2009 vnode_pager_setsize(vp, length);
2015 * Invalidate the cached pages of a file's buffer within the range of block
2016 * numbers [startlbn, endlbn).
2019 v_inval_buf_range(struct vnode *vp, daddr_t startlbn, daddr_t endlbn,
2025 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range");
2027 start = blksize * startlbn;
2028 end = blksize * endlbn;
2032 MPASS(blksize == bo->bo_bsize);
2034 while (v_inval_buf_range_locked(vp, bo, startlbn, endlbn) == EAGAIN)
2038 vn_pages_remove(vp, OFF_TO_IDX(start), OFF_TO_IDX(end + PAGE_SIZE - 1));
2042 v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
2043 daddr_t startlbn, daddr_t endlbn)
2045 struct buf *bp, *nbp;
2048 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range_locked");
2049 ASSERT_BO_LOCKED(bo);
2053 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
2054 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2057 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2058 BO_LOCKPTR(bo)) == ENOLCK) {
2064 bp->b_flags |= B_INVAL | B_RELBUF;
2065 bp->b_flags &= ~B_ASYNC;
2071 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
2073 (nbp->b_flags & B_DELWRI) != 0))
2077 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2078 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2081 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2082 BO_LOCKPTR(bo)) == ENOLCK) {
2087 bp->b_flags |= B_INVAL | B_RELBUF;
2088 bp->b_flags &= ~B_ASYNC;
2094 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
2095 (nbp->b_vp != vp) ||
2096 (nbp->b_flags & B_DELWRI) == 0))
2104 buf_vlist_remove(struct buf *bp)
2108 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2109 ASSERT_BO_WLOCKED(bp->b_bufobj);
2110 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
2111 (BX_VNDIRTY|BX_VNCLEAN),
2112 ("buf_vlist_remove: Buf %p is on two lists", bp));
2113 if (bp->b_xflags & BX_VNDIRTY)
2114 bv = &bp->b_bufobj->bo_dirty;
2116 bv = &bp->b_bufobj->bo_clean;
2117 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
2118 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
2120 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
2124 * Add the buffer to the sorted clean or dirty block list.
2126 * NOTE: xflags is passed as a constant, optimizing this inline function!
2129 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
2135 ASSERT_BO_WLOCKED(bo);
2136 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
2137 ("dead bo %p", bo));
2138 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
2139 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
2140 bp->b_xflags |= xflags;
2141 if (xflags & BX_VNDIRTY)
2147 * Keep the list ordered. Optimize empty list insertion. Assume
2148 * we tend to grow at the tail so lookup_le should usually be cheaper
2151 if (bv->bv_cnt == 0 ||
2152 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
2153 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
2154 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
2155 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
2157 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
2158 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
2160 panic("buf_vlist_add: Preallocated nodes insufficient.");
2165 * Look up a buffer using the buffer tries.
2168 gbincore(struct bufobj *bo, daddr_t lblkno)
2172 ASSERT_BO_LOCKED(bo);
2173 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
2176 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
2180 * Associate a buffer with a vnode.
2183 bgetvp(struct vnode *vp, struct buf *bp)
2188 ASSERT_BO_WLOCKED(bo);
2189 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
2191 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
2192 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
2193 ("bgetvp: bp already attached! %p", bp));
2199 * Insert onto list for new vnode.
2201 buf_vlist_add(bp, bo, BX_VNCLEAN);
2205 * Disassociate a buffer from a vnode.
2208 brelvp(struct buf *bp)
2213 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2214 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
2217 * Delete from old vnode list, if on one.
2219 vp = bp->b_vp; /* XXX */
2222 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2223 buf_vlist_remove(bp);
2225 panic("brelvp: Buffer %p not on queue.", bp);
2226 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2227 bo->bo_flag &= ~BO_ONWORKLST;
2228 mtx_lock(&sync_mtx);
2229 LIST_REMOVE(bo, bo_synclist);
2230 syncer_worklist_len--;
2231 mtx_unlock(&sync_mtx);
2234 bp->b_bufobj = NULL;
2240 * Add an item to the syncer work queue.
2243 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
2247 ASSERT_BO_WLOCKED(bo);
2249 mtx_lock(&sync_mtx);
2250 if (bo->bo_flag & BO_ONWORKLST)
2251 LIST_REMOVE(bo, bo_synclist);
2253 bo->bo_flag |= BO_ONWORKLST;
2254 syncer_worklist_len++;
2257 if (delay > syncer_maxdelay - 2)
2258 delay = syncer_maxdelay - 2;
2259 slot = (syncer_delayno + delay) & syncer_mask;
2261 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
2262 mtx_unlock(&sync_mtx);
2266 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
2270 mtx_lock(&sync_mtx);
2271 len = syncer_worklist_len - sync_vnode_count;
2272 mtx_unlock(&sync_mtx);
2273 error = SYSCTL_OUT(req, &len, sizeof(len));
2277 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
2278 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
2280 static struct proc *updateproc;
2281 static void sched_sync(void);
2282 static struct kproc_desc up_kp = {
2287 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
2290 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
2295 *bo = LIST_FIRST(slp);
2299 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
2302 * We use vhold in case the vnode does not
2303 * successfully sync. vhold prevents the vnode from
2304 * going away when we unlock the sync_mtx so that
2305 * we can acquire the vnode interlock.
2308 mtx_unlock(&sync_mtx);
2310 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2312 mtx_lock(&sync_mtx);
2313 return (*bo == LIST_FIRST(slp));
2315 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2316 (void) VOP_FSYNC(vp, MNT_LAZY, td);
2318 vn_finished_write(mp);
2320 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
2322 * Put us back on the worklist. The worklist
2323 * routine will remove us from our current
2324 * position and then add us back in at a later
2327 vn_syncer_add_to_worklist(*bo, syncdelay);
2331 mtx_lock(&sync_mtx);
2335 static int first_printf = 1;
2338 * System filesystem synchronizer daemon.
2343 struct synclist *next, *slp;
2346 struct thread *td = curthread;
2348 int net_worklist_len;
2349 int syncer_final_iter;
2353 syncer_final_iter = 0;
2354 syncer_state = SYNCER_RUNNING;
2355 starttime = time_uptime;
2356 td->td_pflags |= TDP_NORUNNINGBUF;
2358 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
2361 mtx_lock(&sync_mtx);
2363 if (syncer_state == SYNCER_FINAL_DELAY &&
2364 syncer_final_iter == 0) {
2365 mtx_unlock(&sync_mtx);
2366 kproc_suspend_check(td->td_proc);
2367 mtx_lock(&sync_mtx);
2369 net_worklist_len = syncer_worklist_len - sync_vnode_count;
2370 if (syncer_state != SYNCER_RUNNING &&
2371 starttime != time_uptime) {
2373 printf("\nSyncing disks, vnodes remaining... ");
2376 printf("%d ", net_worklist_len);
2378 starttime = time_uptime;
2381 * Push files whose dirty time has expired. Be careful
2382 * of interrupt race on slp queue.
2384 * Skip over empty worklist slots when shutting down.
2387 slp = &syncer_workitem_pending[syncer_delayno];
2388 syncer_delayno += 1;
2389 if (syncer_delayno == syncer_maxdelay)
2391 next = &syncer_workitem_pending[syncer_delayno];
2393 * If the worklist has wrapped since the
2394 * it was emptied of all but syncer vnodes,
2395 * switch to the FINAL_DELAY state and run
2396 * for one more second.
2398 if (syncer_state == SYNCER_SHUTTING_DOWN &&
2399 net_worklist_len == 0 &&
2400 last_work_seen == syncer_delayno) {
2401 syncer_state = SYNCER_FINAL_DELAY;
2402 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
2404 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
2405 syncer_worklist_len > 0);
2408 * Keep track of the last time there was anything
2409 * on the worklist other than syncer vnodes.
2410 * Return to the SHUTTING_DOWN state if any
2413 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
2414 last_work_seen = syncer_delayno;
2415 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
2416 syncer_state = SYNCER_SHUTTING_DOWN;
2417 while (!LIST_EMPTY(slp)) {
2418 error = sync_vnode(slp, &bo, td);
2420 LIST_REMOVE(bo, bo_synclist);
2421 LIST_INSERT_HEAD(next, bo, bo_synclist);
2425 if (first_printf == 0) {
2427 * Drop the sync mutex, because some watchdog
2428 * drivers need to sleep while patting
2430 mtx_unlock(&sync_mtx);
2431 wdog_kern_pat(WD_LASTVAL);
2432 mtx_lock(&sync_mtx);
2436 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
2437 syncer_final_iter--;
2439 * The variable rushjob allows the kernel to speed up the
2440 * processing of the filesystem syncer process. A rushjob
2441 * value of N tells the filesystem syncer to process the next
2442 * N seconds worth of work on its queue ASAP. Currently rushjob
2443 * is used by the soft update code to speed up the filesystem
2444 * syncer process when the incore state is getting so far
2445 * ahead of the disk that the kernel memory pool is being
2446 * threatened with exhaustion.
2453 * Just sleep for a short period of time between
2454 * iterations when shutting down to allow some I/O
2457 * If it has taken us less than a second to process the
2458 * current work, then wait. Otherwise start right over
2459 * again. We can still lose time if any single round
2460 * takes more than two seconds, but it does not really
2461 * matter as we are just trying to generally pace the
2462 * filesystem activity.
2464 if (syncer_state != SYNCER_RUNNING ||
2465 time_uptime == starttime) {
2467 sched_prio(td, PPAUSE);
2470 if (syncer_state != SYNCER_RUNNING)
2471 cv_timedwait(&sync_wakeup, &sync_mtx,
2472 hz / SYNCER_SHUTDOWN_SPEEDUP);
2473 else if (time_uptime == starttime)
2474 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2479 * Request the syncer daemon to speed up its work.
2480 * We never push it to speed up more than half of its
2481 * normal turn time, otherwise it could take over the cpu.
2484 speedup_syncer(void)
2488 mtx_lock(&sync_mtx);
2489 if (rushjob < syncdelay / 2) {
2491 stat_rush_requests += 1;
2494 mtx_unlock(&sync_mtx);
2495 cv_broadcast(&sync_wakeup);
2500 * Tell the syncer to speed up its work and run though its work
2501 * list several times, then tell it to shut down.
2504 syncer_shutdown(void *arg, int howto)
2507 if (howto & RB_NOSYNC)
2509 mtx_lock(&sync_mtx);
2510 syncer_state = SYNCER_SHUTTING_DOWN;
2512 mtx_unlock(&sync_mtx);
2513 cv_broadcast(&sync_wakeup);
2514 kproc_shutdown(arg, howto);
2518 syncer_suspend(void)
2521 syncer_shutdown(updateproc, 0);
2528 mtx_lock(&sync_mtx);
2530 syncer_state = SYNCER_RUNNING;
2531 mtx_unlock(&sync_mtx);
2532 cv_broadcast(&sync_wakeup);
2533 kproc_resume(updateproc);
2537 * Reassign a buffer from one vnode to another.
2538 * Used to assign file specific control information
2539 * (indirect blocks) to the vnode to which they belong.
2542 reassignbuf(struct buf *bp)
2555 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2556 bp, bp->b_vp, bp->b_flags);
2558 * B_PAGING flagged buffers cannot be reassigned because their vp
2559 * is not fully linked in.
2561 if (bp->b_flags & B_PAGING)
2562 panic("cannot reassign paging buffer");
2565 * Delete from old vnode list, if on one.
2568 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2569 buf_vlist_remove(bp);
2571 panic("reassignbuf: Buffer %p not on queue.", bp);
2573 * If dirty, put on list of dirty buffers; otherwise insert onto list
2576 if (bp->b_flags & B_DELWRI) {
2577 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2578 switch (vp->v_type) {
2588 vn_syncer_add_to_worklist(bo, delay);
2590 buf_vlist_add(bp, bo, BX_VNDIRTY);
2592 buf_vlist_add(bp, bo, BX_VNCLEAN);
2594 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2595 mtx_lock(&sync_mtx);
2596 LIST_REMOVE(bo, bo_synclist);
2597 syncer_worklist_len--;
2598 mtx_unlock(&sync_mtx);
2599 bo->bo_flag &= ~BO_ONWORKLST;
2604 bp = TAILQ_FIRST(&bv->bv_hd);
2605 KASSERT(bp == NULL || bp->b_bufobj == bo,
2606 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2607 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2608 KASSERT(bp == NULL || bp->b_bufobj == bo,
2609 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2611 bp = TAILQ_FIRST(&bv->bv_hd);
2612 KASSERT(bp == NULL || bp->b_bufobj == bo,
2613 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2614 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2615 KASSERT(bp == NULL || bp->b_bufobj == bo,
2616 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2622 v_init_counters(struct vnode *vp)
2625 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2626 vp, ("%s called for an initialized vnode", __FUNCTION__));
2627 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2629 refcount_init(&vp->v_holdcnt, 1);
2630 refcount_init(&vp->v_usecount, 1);
2634 * Increment si_usecount of the associated device, if any.
2637 v_incr_devcount(struct vnode *vp)
2640 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2641 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2643 vp->v_rdev->si_usecount++;
2649 * Decrement si_usecount of the associated device, if any.
2652 v_decr_devcount(struct vnode *vp)
2655 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2656 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2658 vp->v_rdev->si_usecount--;
2664 * Grab a particular vnode from the free list, increment its
2665 * reference count and lock it. VI_DOOMED is set if the vnode
2666 * is being destroyed. Only callers who specify LK_RETRY will
2667 * see doomed vnodes. If inactive processing was delayed in
2668 * vput try to do it here.
2670 * Notes on lockless counter manipulation:
2671 * _vhold, vputx and other routines make various decisions based
2672 * on either holdcnt or usecount being 0. As long as either counter
2673 * is not transitioning 0->1 nor 1->0, the manipulation can be done
2674 * with atomic operations. Otherwise the interlock is taken covering
2675 * both the atomic and additional actions.
2677 static enum vgetstate
2678 _vget_prep(struct vnode *vp, bool interlock)
2682 if (__predict_true(vp->v_type != VCHR)) {
2683 if (refcount_acquire_if_not_zero(&vp->v_usecount)) {
2686 _vhold(vp, interlock);
2692 if (vp->v_usecount == 0) {
2696 v_incr_devcount(vp);
2697 refcount_acquire(&vp->v_usecount);
2707 vget_prep(struct vnode *vp)
2710 return (_vget_prep(vp, false));
2714 vget(struct vnode *vp, int flags, struct thread *td)
2718 MPASS(td == curthread);
2720 vs = _vget_prep(vp, (flags & LK_INTERLOCK) != 0);
2721 return (vget_finish(vp, flags, vs));
2725 vget_finish(struct vnode *vp, int flags, enum vgetstate vs)
2727 int error, oweinact;
2729 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2730 ("%s: invalid lock operation", __func__));
2732 if ((flags & LK_INTERLOCK) != 0)
2733 ASSERT_VI_LOCKED(vp, __func__);
2735 ASSERT_VI_UNLOCKED(vp, __func__);
2736 VNASSERT(vp->v_holdcnt > 0, vp, ("%s: vnode not held", __func__));
2737 if (vs == VGET_USECOUNT) {
2738 VNASSERT(vp->v_usecount > 0, vp,
2739 ("%s: vnode without usecount when VGET_USECOUNT was passed",
2743 if ((error = vn_lock(vp, flags)) != 0) {
2744 if (vs == VGET_USECOUNT)
2748 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2753 if (vs == VGET_USECOUNT) {
2754 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2755 ("%s: vnode with usecount and VI_OWEINACT set", __func__));
2760 * We hold the vnode. If the usecount is 0 it will be utilized to keep
2761 * the vnode around. Otherwise someone else lended their hold count and
2762 * we have to drop ours.
2764 if (vp->v_type != VCHR &&
2765 refcount_acquire_if_not_zero(&vp->v_usecount)) {
2767 int old = atomic_fetchadd_int(&vp->v_holdcnt, -1) - 1;
2768 VNASSERT(old > 0, vp, ("%s: wrong hold count", __func__));
2770 refcount_release(&vp->v_holdcnt);
2772 VNODE_REFCOUNT_FENCE_ACQ();
2773 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2774 ("%s: vnode with usecount and VI_OWEINACT set", __func__));
2779 * We don't guarantee that any particular close will
2780 * trigger inactive processing so just make a best effort
2781 * here at preventing a reference to a removed file. If
2782 * we don't succeed no harm is done.
2784 * Upgrade our holdcnt to a usecount.
2787 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2791 vp->v_iflag &= ~VI_OWEINACT;
2792 VNODE_REFCOUNT_FENCE_REL();
2794 if (vp->v_usecount > 0)
2795 refcount_release(&vp->v_holdcnt);
2796 v_incr_devcount(vp);
2797 refcount_acquire(&vp->v_usecount);
2798 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2799 (flags & LK_NOWAIT) == 0)
2800 vinactive(vp, curthread);
2806 * Increase the reference (use) and hold count of a vnode.
2807 * This will also remove the vnode from the free list if it is presently free.
2810 vref(struct vnode *vp)
2813 ASSERT_VI_UNLOCKED(vp, __func__);
2814 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2815 if (vp->v_type != VCHR &&
2816 refcount_acquire_if_not_zero(&vp->v_usecount)) {
2817 VNODE_REFCOUNT_FENCE_ACQ();
2818 VNASSERT(vp->v_holdcnt > 0, vp,
2819 ("%s: active vnode not held", __func__));
2820 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2821 ("%s: vnode with usecount and VI_OWEINACT set", __func__));
2830 vrefl(struct vnode *vp)
2833 ASSERT_VI_LOCKED(vp, __func__);
2834 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2835 if (vp->v_usecount == 0)
2837 if ((vp->v_iflag & VI_OWEINACT) != 0) {
2838 vp->v_iflag &= ~VI_OWEINACT;
2839 VNODE_REFCOUNT_FENCE_REL();
2841 v_incr_devcount(vp);
2842 refcount_acquire(&vp->v_usecount);
2846 vrefact(struct vnode *vp)
2849 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2850 if (__predict_false(vp->v_type == VCHR)) {
2851 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp,
2852 ("%s: wrong ref counts", __func__));
2857 int old = atomic_fetchadd_int(&vp->v_usecount, 1);
2858 VNASSERT(old > 0, vp, ("%s: wrong use count", __func__));
2860 refcount_acquire(&vp->v_usecount);
2865 * Return reference count of a vnode.
2867 * The results of this call are only guaranteed when some mechanism is used to
2868 * stop other processes from gaining references to the vnode. This may be the
2869 * case if the caller holds the only reference. This is also useful when stale
2870 * data is acceptable as race conditions may be accounted for by some other
2874 vrefcnt(struct vnode *vp)
2877 return (vp->v_usecount);
2880 #define VPUTX_VRELE 1
2881 #define VPUTX_VPUT 2
2882 #define VPUTX_VUNREF 3
2885 * Decrement the use and hold counts for a vnode.
2887 * See an explanation near vget() as to why atomic operation is safe.
2890 vputx(struct vnode *vp, int func)
2894 KASSERT(vp != NULL, ("vputx: null vp"));
2895 if (func == VPUTX_VUNREF)
2896 ASSERT_VOP_LOCKED(vp, "vunref");
2897 else if (func == VPUTX_VPUT)
2898 ASSERT_VOP_LOCKED(vp, "vput");
2900 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2901 ASSERT_VI_UNLOCKED(vp, __func__);
2902 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp,
2903 ("%s: wrong ref counts", __func__));
2905 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2908 * It is an invariant that all VOP_* calls operate on a held vnode.
2909 * We may be only having an implicit hold stemming from our usecount,
2910 * which we are about to release. If we unlock the vnode afterwards we
2911 * open a time window where someone else dropped the last usecount and
2912 * proceeded to free the vnode before our unlock finished. For this
2913 * reason we unlock the vnode early. This is a little bit wasteful as
2914 * it may be the vnode is exclusively locked and inactive processing is
2915 * needed, in which case we are adding work.
2917 if (func == VPUTX_VPUT)
2920 if (vp->v_type != VCHR &&
2921 refcount_release_if_not_last(&vp->v_usecount))
2927 * We want to hold the vnode until the inactive finishes to
2928 * prevent vgone() races. We drop the use count here and the
2929 * hold count below when we're done.
2931 v_decr_devcount(vp);
2932 if (!refcount_release(&vp->v_usecount)) {
2936 if (vp->v_iflag & VI_DOINGINACT) {
2943 if (vp->v_usecount != 0) {
2944 vn_printf(vp, "vputx: usecount not zero for vnode ");
2945 panic("vputx: usecount not zero");
2948 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2951 * Check if the fs wants to perform inactive processing. Note we
2952 * may be only holding the interlock, in which case it is possible
2953 * someone else called vgone on the vnode and ->v_data is now NULL.
2954 * Since vgone performs inactive on its own there is nothing to do
2955 * here but to drop our hold count.
2957 if (__predict_false(vp->v_iflag & VI_DOOMED) ||
2958 VOP_NEED_INACTIVE(vp) == 0) {
2964 * We must call VOP_INACTIVE with the node locked. Mark
2965 * as VI_DOINGINACT to avoid recursion.
2967 vp->v_iflag |= VI_OWEINACT;
2970 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2974 error = VOP_LOCK(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_NOWAIT);
2978 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2979 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2984 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp,
2985 ("vnode with usecount and VI_OWEINACT set"));
2987 if (vp->v_iflag & VI_OWEINACT)
2988 vinactive(vp, curthread);
2989 if (func != VPUTX_VUNREF)
2996 * Vnode put/release.
2997 * If count drops to zero, call inactive routine and return to freelist.
3000 vrele(struct vnode *vp)
3003 vputx(vp, VPUTX_VRELE);
3007 * Release an already locked vnode. This give the same effects as
3008 * unlock+vrele(), but takes less time and avoids releasing and
3009 * re-aquiring the lock (as vrele() acquires the lock internally.)
3012 vput(struct vnode *vp)
3015 vputx(vp, VPUTX_VPUT);
3019 * Release an exclusively locked vnode. Do not unlock the vnode lock.
3022 vunref(struct vnode *vp)
3025 vputx(vp, VPUTX_VUNREF);
3029 * Increase the hold count and activate if this is the first reference.
3032 _vhold(struct vnode *vp, bool locked)
3037 ASSERT_VI_LOCKED(vp, __func__);
3039 ASSERT_VI_UNLOCKED(vp, __func__);
3040 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3042 if (refcount_acquire_if_not_zero(&vp->v_holdcnt)) {
3043 VNODE_REFCOUNT_FENCE_ACQ();
3044 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
3045 ("_vhold: vnode with holdcnt is free"));
3050 if ((vp->v_iflag & VI_FREE) == 0) {
3051 refcount_acquire(&vp->v_holdcnt);
3056 VNASSERT(vp->v_holdcnt == 0, vp,
3057 ("%s: wrong hold count", __func__));
3058 VNASSERT(vp->v_op != NULL, vp,
3059 ("%s: vnode already reclaimed.", __func__));
3061 * Remove a vnode from the free list, mark it as in use,
3062 * and put it on the active list.
3064 VNASSERT(vp->v_mount != NULL, vp,
3065 ("_vhold: vnode not on per mount vnode list"));
3067 mtx_lock(&mp->mnt_listmtx);
3068 if ((vp->v_mflag & VMP_TMPMNTFREELIST) != 0) {
3069 TAILQ_REMOVE(&mp->mnt_tmpfreevnodelist, vp, v_actfreelist);
3070 mp->mnt_tmpfreevnodelistsize--;
3071 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
3073 mtx_lock(&vnode_free_list_mtx);
3074 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
3076 mtx_unlock(&vnode_free_list_mtx);
3078 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
3079 ("Activating already active vnode"));
3080 vp->v_iflag &= ~VI_FREE;
3081 vp->v_iflag |= VI_ACTIVE;
3082 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
3083 mp->mnt_activevnodelistsize++;
3084 mtx_unlock(&mp->mnt_listmtx);
3085 refcount_acquire(&vp->v_holdcnt);
3091 vholdnz(struct vnode *vp)
3094 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3096 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
3097 VNASSERT(old > 0, vp, ("%s: wrong hold count", __func__));
3099 atomic_add_int(&vp->v_holdcnt, 1);
3104 * Drop the hold count of the vnode. If this is the last reference to
3105 * the vnode we place it on the free list unless it has been vgone'd
3106 * (marked VI_DOOMED) in which case we will free it.
3108 * Because the vnode vm object keeps a hold reference on the vnode if
3109 * there is at least one resident non-cached page, the vnode cannot
3110 * leave the active list without the page cleanup done.
3113 _vdrop(struct vnode *vp, bool locked)
3120 ASSERT_VI_LOCKED(vp, __func__);
3122 ASSERT_VI_UNLOCKED(vp, __func__);
3123 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3124 if (__predict_false((int)vp->v_holdcnt <= 0)) {
3125 vn_printf(vp, "vdrop: holdcnt %d", vp->v_holdcnt);
3126 panic("vdrop: wrong holdcnt");
3129 if (refcount_release_if_not_last(&vp->v_holdcnt))
3133 if (refcount_release(&vp->v_holdcnt) == 0) {
3137 if ((vp->v_iflag & VI_DOOMED) == 0) {
3139 * Mark a vnode as free: remove it from its active list
3140 * and put it up for recycling on the freelist.
3142 VNASSERT(vp->v_op != NULL, vp,
3143 ("vdropl: vnode already reclaimed."));
3144 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
3145 ("vnode already free"));
3146 VNASSERT(vp->v_holdcnt == 0, vp,
3147 ("vdropl: freeing when we shouldn't"));
3148 active = vp->v_iflag & VI_ACTIVE;
3149 if ((vp->v_iflag & VI_OWEINACT) == 0) {
3150 vp->v_iflag &= ~VI_ACTIVE;
3153 mtx_lock(&mp->mnt_listmtx);
3155 TAILQ_REMOVE(&mp->mnt_activevnodelist,
3157 mp->mnt_activevnodelistsize--;
3159 TAILQ_INSERT_TAIL(&mp->mnt_tmpfreevnodelist,
3161 mp->mnt_tmpfreevnodelistsize++;
3162 vp->v_iflag |= VI_FREE;
3163 vp->v_mflag |= VMP_TMPMNTFREELIST;
3165 if (mp->mnt_tmpfreevnodelistsize >=
3166 mnt_free_list_batch)
3167 vnlru_return_batch_locked(mp);
3168 mtx_unlock(&mp->mnt_listmtx);
3170 VNASSERT(active == 0, vp,
3171 ("vdropl: active vnode not on per mount "
3173 mtx_lock(&vnode_free_list_mtx);
3174 TAILQ_INSERT_TAIL(&vnode_free_list, vp,
3177 vp->v_iflag |= VI_FREE;
3179 mtx_unlock(&vnode_free_list_mtx);
3183 counter_u64_add(free_owe_inact, 1);
3188 * The vnode has been marked for destruction, so free it.
3190 * The vnode will be returned to the zone where it will
3191 * normally remain until it is needed for another vnode. We
3192 * need to cleanup (or verify that the cleanup has already
3193 * been done) any residual data left from its current use
3194 * so as not to contaminate the freshly allocated vnode.
3196 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
3197 atomic_subtract_long(&numvnodes, 1);
3199 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
3200 ("cleaned vnode still on the free list."));
3201 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
3202 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
3203 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
3204 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
3205 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
3206 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
3207 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
3208 ("clean blk trie not empty"));
3209 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
3210 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
3211 ("dirty blk trie not empty"));
3212 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
3213 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
3214 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
3215 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp,
3216 ("Dangling rangelock waiters"));
3219 mac_vnode_destroy(vp);
3221 if (vp->v_pollinfo != NULL) {
3222 destroy_vpollinfo(vp->v_pollinfo);
3223 vp->v_pollinfo = NULL;
3226 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
3229 vp->v_mountedhere = NULL;
3232 vp->v_fifoinfo = NULL;
3233 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
3237 uma_zfree(vnode_zone, vp);
3241 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
3242 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
3243 * OWEINACT tracks whether a vnode missed a call to inactive due to a
3244 * failed lock upgrade.
3247 vinactive(struct vnode *vp, struct thread *td)
3249 struct vm_object *obj;
3251 ASSERT_VOP_ELOCKED(vp, "vinactive");
3252 ASSERT_VI_LOCKED(vp, "vinactive");
3253 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
3254 ("vinactive: recursed on VI_DOINGINACT"));
3255 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3256 vp->v_iflag |= VI_DOINGINACT;
3257 vp->v_iflag &= ~VI_OWEINACT;
3260 * Before moving off the active list, we must be sure that any
3261 * modified pages are converted into the vnode's dirty
3262 * buffers, since these will no longer be checked once the
3263 * vnode is on the inactive list.
3265 * The write-out of the dirty pages is asynchronous. At the
3266 * point that VOP_INACTIVE() is called, there could still be
3267 * pending I/O and dirty pages in the object.
3269 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
3270 (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
3271 VM_OBJECT_WLOCK(obj);
3272 vm_object_page_clean(obj, 0, 0, 0);
3273 VM_OBJECT_WUNLOCK(obj);
3275 VOP_INACTIVE(vp, td);
3277 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
3278 ("vinactive: lost VI_DOINGINACT"));
3279 vp->v_iflag &= ~VI_DOINGINACT;
3283 * Remove any vnodes in the vnode table belonging to mount point mp.
3285 * If FORCECLOSE is not specified, there should not be any active ones,
3286 * return error if any are found (nb: this is a user error, not a
3287 * system error). If FORCECLOSE is specified, detach any active vnodes
3290 * If WRITECLOSE is set, only flush out regular file vnodes open for
3293 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
3295 * `rootrefs' specifies the base reference count for the root vnode
3296 * of this filesystem. The root vnode is considered busy if its
3297 * v_usecount exceeds this value. On a successful return, vflush(, td)
3298 * will call vrele() on the root vnode exactly rootrefs times.
3299 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
3303 static int busyprt = 0; /* print out busy vnodes */
3304 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
3308 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
3310 struct vnode *vp, *mvp, *rootvp = NULL;
3312 int busy = 0, error;
3314 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
3317 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
3318 ("vflush: bad args"));
3320 * Get the filesystem root vnode. We can vput() it
3321 * immediately, since with rootrefs > 0, it won't go away.
3323 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
3324 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
3331 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
3333 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
3336 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3340 * Skip over a vnodes marked VV_SYSTEM.
3342 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
3348 * If WRITECLOSE is set, flush out unlinked but still open
3349 * files (even if open only for reading) and regular file
3350 * vnodes open for writing.
3352 if (flags & WRITECLOSE) {
3353 if (vp->v_object != NULL) {
3354 VM_OBJECT_WLOCK(vp->v_object);
3355 vm_object_page_clean(vp->v_object, 0, 0, 0);
3356 VM_OBJECT_WUNLOCK(vp->v_object);
3358 error = VOP_FSYNC(vp, MNT_WAIT, td);
3362 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3365 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3368 if ((vp->v_type == VNON ||
3369 (error == 0 && vattr.va_nlink > 0)) &&
3370 (vp->v_writecount <= 0 || vp->v_type != VREG)) {
3378 * With v_usecount == 0, all we need to do is clear out the
3379 * vnode data structures and we are done.
3381 * If FORCECLOSE is set, forcibly close the vnode.
3383 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
3389 vn_printf(vp, "vflush: busy vnode ");
3395 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
3397 * If just the root vnode is busy, and if its refcount
3398 * is equal to `rootrefs', then go ahead and kill it.
3401 KASSERT(busy > 0, ("vflush: not busy"));
3402 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
3403 ("vflush: usecount %d < rootrefs %d",
3404 rootvp->v_usecount, rootrefs));
3405 if (busy == 1 && rootvp->v_usecount == rootrefs) {
3406 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
3408 VOP_UNLOCK(rootvp, 0);
3414 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
3418 for (; rootrefs > 0; rootrefs--)
3424 * Recycle an unused vnode to the front of the free list.
3427 vrecycle(struct vnode *vp)
3432 recycled = vrecyclel(vp);
3438 * vrecycle, with the vp interlock held.
3441 vrecyclel(struct vnode *vp)
3445 ASSERT_VOP_ELOCKED(vp, __func__);
3446 ASSERT_VI_LOCKED(vp, __func__);
3447 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3449 if (vp->v_usecount == 0) {
3457 * Eliminate all activity associated with a vnode
3458 * in preparation for reuse.
3461 vgone(struct vnode *vp)
3469 notify_lowervp_vfs_dummy(struct mount *mp __unused,
3470 struct vnode *lowervp __unused)
3475 * Notify upper mounts about reclaimed or unlinked vnode.
3478 vfs_notify_upper(struct vnode *vp, int event)
3480 static struct vfsops vgonel_vfsops = {
3481 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
3482 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
3484 struct mount *mp, *ump, *mmp;
3491 if (TAILQ_EMPTY(&mp->mnt_uppers))
3494 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
3495 mmp->mnt_op = &vgonel_vfsops;
3496 mmp->mnt_kern_flag |= MNTK_MARKER;
3498 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
3499 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
3500 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
3501 ump = TAILQ_NEXT(ump, mnt_upper_link);
3504 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
3507 case VFS_NOTIFY_UPPER_RECLAIM:
3508 VFS_RECLAIM_LOWERVP(ump, vp);
3510 case VFS_NOTIFY_UPPER_UNLINK:
3511 VFS_UNLINK_LOWERVP(ump, vp);
3514 KASSERT(0, ("invalid event %d", event));
3518 ump = TAILQ_NEXT(mmp, mnt_upper_link);
3519 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
3522 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
3523 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
3524 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
3525 wakeup(&mp->mnt_uppers);
3532 * vgone, with the vp interlock held.
3535 vgonel(struct vnode *vp)
3540 bool active, oweinact;
3542 ASSERT_VOP_ELOCKED(vp, "vgonel");
3543 ASSERT_VI_LOCKED(vp, "vgonel");
3544 VNASSERT(vp->v_holdcnt, vp,
3545 ("vgonel: vp %p has no reference.", vp));
3546 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3550 * Don't vgonel if we're already doomed.
3552 if (vp->v_iflag & VI_DOOMED)
3554 vp->v_iflag |= VI_DOOMED;
3557 * Check to see if the vnode is in use. If so, we have to call
3558 * VOP_CLOSE() and VOP_INACTIVE().
3560 active = vp->v_usecount > 0;
3561 oweinact = (vp->v_iflag & VI_OWEINACT) != 0;
3563 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
3566 * If purging an active vnode, it must be closed and
3567 * deactivated before being reclaimed.
3570 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
3571 if (oweinact || active) {
3573 if ((vp->v_iflag & VI_DOINGINACT) == 0)
3577 if (vp->v_type == VSOCK)
3578 vfs_unp_reclaim(vp);
3581 * Clean out any buffers associated with the vnode.
3582 * If the flush fails, just toss the buffers.
3585 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
3586 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
3587 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
3588 while (vinvalbuf(vp, 0, 0, 0) != 0)
3592 BO_LOCK(&vp->v_bufobj);
3593 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
3594 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3595 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
3596 vp->v_bufobj.bo_clean.bv_cnt == 0,
3597 ("vp %p bufobj not invalidated", vp));
3600 * For VMIO bufobj, BO_DEAD is set later, or in
3601 * vm_object_terminate() after the object's page queue is
3604 object = vp->v_bufobj.bo_object;
3606 vp->v_bufobj.bo_flag |= BO_DEAD;
3607 BO_UNLOCK(&vp->v_bufobj);
3610 * Handle the VM part. Tmpfs handles v_object on its own (the
3611 * OBJT_VNODE check). Nullfs or other bypassing filesystems
3612 * should not touch the object borrowed from the lower vnode
3613 * (the handle check).
3615 if (object != NULL && object->type == OBJT_VNODE &&
3616 object->handle == vp)
3617 vnode_destroy_vobject(vp);
3620 * Reclaim the vnode.
3622 if (VOP_RECLAIM(vp, td))
3623 panic("vgone: cannot reclaim");
3625 vn_finished_secondary_write(mp);
3626 VNASSERT(vp->v_object == NULL, vp,
3627 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
3629 * Clear the advisory locks and wake up waiting threads.
3631 (void)VOP_ADVLOCKPURGE(vp);
3634 * Delete from old mount point vnode list.
3639 * Done with purge, reset to the standard lock and invalidate
3643 vp->v_vnlock = &vp->v_lock;
3644 vp->v_op = &dead_vnodeops;
3650 * Calculate the total number of references to a special device.
3653 vcount(struct vnode *vp)
3658 count = vp->v_rdev->si_usecount;
3664 * Same as above, but using the struct cdev *as argument
3667 count_dev(struct cdev *dev)
3672 count = dev->si_usecount;
3678 * Print out a description of a vnode.
3680 static char *typename[] =
3681 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
3685 vn_printf(struct vnode *vp, const char *fmt, ...)
3688 char buf[256], buf2[16];
3694 printf("%p: ", (void *)vp);
3695 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
3696 printf(" usecount %d, writecount %d, refcount %d",
3697 vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
3698 switch (vp->v_type) {
3700 printf(" mountedhere %p\n", vp->v_mountedhere);
3703 printf(" rdev %p\n", vp->v_rdev);
3706 printf(" socket %p\n", vp->v_unpcb);
3709 printf(" fifoinfo %p\n", vp->v_fifoinfo);
3717 if (vp->v_vflag & VV_ROOT)
3718 strlcat(buf, "|VV_ROOT", sizeof(buf));
3719 if (vp->v_vflag & VV_ISTTY)
3720 strlcat(buf, "|VV_ISTTY", sizeof(buf));
3721 if (vp->v_vflag & VV_NOSYNC)
3722 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
3723 if (vp->v_vflag & VV_ETERNALDEV)
3724 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
3725 if (vp->v_vflag & VV_CACHEDLABEL)
3726 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
3727 if (vp->v_vflag & VV_COPYONWRITE)
3728 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
3729 if (vp->v_vflag & VV_SYSTEM)
3730 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
3731 if (vp->v_vflag & VV_PROCDEP)
3732 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
3733 if (vp->v_vflag & VV_NOKNOTE)
3734 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
3735 if (vp->v_vflag & VV_DELETED)
3736 strlcat(buf, "|VV_DELETED", sizeof(buf));
3737 if (vp->v_vflag & VV_MD)
3738 strlcat(buf, "|VV_MD", sizeof(buf));
3739 if (vp->v_vflag & VV_FORCEINSMQ)
3740 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
3741 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
3742 VV_CACHEDLABEL | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
3743 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
3745 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
3746 strlcat(buf, buf2, sizeof(buf));
3748 if (vp->v_iflag & VI_MOUNT)
3749 strlcat(buf, "|VI_MOUNT", sizeof(buf));
3750 if (vp->v_iflag & VI_DOOMED)
3751 strlcat(buf, "|VI_DOOMED", sizeof(buf));
3752 if (vp->v_iflag & VI_FREE)
3753 strlcat(buf, "|VI_FREE", sizeof(buf));
3754 if (vp->v_iflag & VI_ACTIVE)
3755 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3756 if (vp->v_iflag & VI_DOINGINACT)
3757 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3758 if (vp->v_iflag & VI_OWEINACT)
3759 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3760 flags = vp->v_iflag & ~(VI_MOUNT | VI_DOOMED | VI_FREE |
3761 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
3763 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3764 strlcat(buf, buf2, sizeof(buf));
3766 printf(" flags (%s)\n", buf + 1);
3767 if (mtx_owned(VI_MTX(vp)))
3768 printf(" VI_LOCKed");
3769 if (vp->v_object != NULL)
3770 printf(" v_object %p ref %d pages %d "
3771 "cleanbuf %d dirtybuf %d\n",
3772 vp->v_object, vp->v_object->ref_count,
3773 vp->v_object->resident_page_count,
3774 vp->v_bufobj.bo_clean.bv_cnt,
3775 vp->v_bufobj.bo_dirty.bv_cnt);
3777 lockmgr_printinfo(vp->v_vnlock);
3778 if (vp->v_data != NULL)
3784 * List all of the locked vnodes in the system.
3785 * Called when debugging the kernel.
3787 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3793 * Note: because this is DDB, we can't obey the locking semantics
3794 * for these structures, which means we could catch an inconsistent
3795 * state and dereference a nasty pointer. Not much to be done
3798 db_printf("Locked vnodes\n");
3799 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3800 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3801 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3802 vn_printf(vp, "vnode ");
3808 * Show details about the given vnode.
3810 DB_SHOW_COMMAND(vnode, db_show_vnode)
3816 vp = (struct vnode *)addr;
3817 vn_printf(vp, "vnode ");
3821 * Show details about the given mount point.
3823 DB_SHOW_COMMAND(mount, db_show_mount)
3834 /* No address given, print short info about all mount points. */
3835 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3836 db_printf("%p %s on %s (%s)\n", mp,
3837 mp->mnt_stat.f_mntfromname,
3838 mp->mnt_stat.f_mntonname,
3839 mp->mnt_stat.f_fstypename);
3843 db_printf("\nMore info: show mount <addr>\n");
3847 mp = (struct mount *)addr;
3848 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3849 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3852 mflags = mp->mnt_flag;
3853 #define MNT_FLAG(flag) do { \
3854 if (mflags & (flag)) { \
3855 if (buf[0] != '\0') \
3856 strlcat(buf, ", ", sizeof(buf)); \
3857 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3858 mflags &= ~(flag); \
3861 MNT_FLAG(MNT_RDONLY);
3862 MNT_FLAG(MNT_SYNCHRONOUS);
3863 MNT_FLAG(MNT_NOEXEC);
3864 MNT_FLAG(MNT_NOSUID);
3865 MNT_FLAG(MNT_NFS4ACLS);
3866 MNT_FLAG(MNT_UNION);
3867 MNT_FLAG(MNT_ASYNC);
3868 MNT_FLAG(MNT_SUIDDIR);
3869 MNT_FLAG(MNT_SOFTDEP);
3870 MNT_FLAG(MNT_NOSYMFOLLOW);
3871 MNT_FLAG(MNT_GJOURNAL);
3872 MNT_FLAG(MNT_MULTILABEL);
3874 MNT_FLAG(MNT_NOATIME);
3875 MNT_FLAG(MNT_NOCLUSTERR);
3876 MNT_FLAG(MNT_NOCLUSTERW);
3878 MNT_FLAG(MNT_EXRDONLY);
3879 MNT_FLAG(MNT_EXPORTED);
3880 MNT_FLAG(MNT_DEFEXPORTED);
3881 MNT_FLAG(MNT_EXPORTANON);
3882 MNT_FLAG(MNT_EXKERB);
3883 MNT_FLAG(MNT_EXPUBLIC);
3884 MNT_FLAG(MNT_LOCAL);
3885 MNT_FLAG(MNT_QUOTA);
3886 MNT_FLAG(MNT_ROOTFS);
3888 MNT_FLAG(MNT_IGNORE);
3889 MNT_FLAG(MNT_UPDATE);
3890 MNT_FLAG(MNT_DELEXPORT);
3891 MNT_FLAG(MNT_RELOAD);
3892 MNT_FLAG(MNT_FORCE);
3893 MNT_FLAG(MNT_SNAPSHOT);
3894 MNT_FLAG(MNT_BYFSID);
3898 strlcat(buf, ", ", sizeof(buf));
3899 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3900 "0x%016jx", mflags);
3902 db_printf(" mnt_flag = %s\n", buf);
3905 flags = mp->mnt_kern_flag;
3906 #define MNT_KERN_FLAG(flag) do { \
3907 if (flags & (flag)) { \
3908 if (buf[0] != '\0') \
3909 strlcat(buf, ", ", sizeof(buf)); \
3910 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3914 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3915 MNT_KERN_FLAG(MNTK_ASYNC);
3916 MNT_KERN_FLAG(MNTK_SOFTDEP);
3917 MNT_KERN_FLAG(MNTK_DRAINING);
3918 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3919 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3920 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3921 MNT_KERN_FLAG(MNTK_NO_IOPF);
3922 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3923 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3924 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3925 MNT_KERN_FLAG(MNTK_MARKER);
3926 MNT_KERN_FLAG(MNTK_USES_BCACHE);
3927 MNT_KERN_FLAG(MNTK_NOASYNC);
3928 MNT_KERN_FLAG(MNTK_UNMOUNT);
3929 MNT_KERN_FLAG(MNTK_MWAIT);
3930 MNT_KERN_FLAG(MNTK_SUSPEND);
3931 MNT_KERN_FLAG(MNTK_SUSPEND2);
3932 MNT_KERN_FLAG(MNTK_SUSPENDED);
3933 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3934 MNT_KERN_FLAG(MNTK_NOKNOTE);
3935 #undef MNT_KERN_FLAG
3938 strlcat(buf, ", ", sizeof(buf));
3939 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3942 db_printf(" mnt_kern_flag = %s\n", buf);
3944 db_printf(" mnt_opt = ");
3945 opt = TAILQ_FIRST(mp->mnt_opt);
3947 db_printf("%s", opt->name);
3948 opt = TAILQ_NEXT(opt, link);
3949 while (opt != NULL) {
3950 db_printf(", %s", opt->name);
3951 opt = TAILQ_NEXT(opt, link);
3957 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3958 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3959 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3960 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3961 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3962 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3963 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3964 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3965 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3966 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3967 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3968 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3970 db_printf(" mnt_cred = { uid=%u ruid=%u",
3971 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3972 if (jailed(mp->mnt_cred))
3973 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3975 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3976 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3977 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3978 db_printf(" mnt_activevnodelistsize = %d\n",
3979 mp->mnt_activevnodelistsize);
3980 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3981 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3982 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3983 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3984 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3985 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3986 db_printf(" mnt_secondary_accwrites = %d\n",
3987 mp->mnt_secondary_accwrites);
3988 db_printf(" mnt_gjprovider = %s\n",
3989 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3991 db_printf("\n\nList of active vnodes\n");
3992 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3993 if (vp->v_type != VMARKER) {
3994 vn_printf(vp, "vnode ");
3999 db_printf("\n\nList of inactive vnodes\n");
4000 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4001 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
4002 vn_printf(vp, "vnode ");
4011 * Fill in a struct xvfsconf based on a struct vfsconf.
4014 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
4016 struct xvfsconf xvfsp;
4018 bzero(&xvfsp, sizeof(xvfsp));
4019 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4020 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4021 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4022 xvfsp.vfc_flags = vfsp->vfc_flags;
4024 * These are unused in userland, we keep them
4025 * to not break binary compatibility.
4027 xvfsp.vfc_vfsops = NULL;
4028 xvfsp.vfc_next = NULL;
4029 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4032 #ifdef COMPAT_FREEBSD32
4034 uint32_t vfc_vfsops;
4035 char vfc_name[MFSNAMELEN];
4036 int32_t vfc_typenum;
4037 int32_t vfc_refcount;
4043 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
4045 struct xvfsconf32 xvfsp;
4047 bzero(&xvfsp, sizeof(xvfsp));
4048 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
4049 xvfsp.vfc_typenum = vfsp->vfc_typenum;
4050 xvfsp.vfc_refcount = vfsp->vfc_refcount;
4051 xvfsp.vfc_flags = vfsp->vfc_flags;
4052 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
4057 * Top level filesystem related information gathering.
4060 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
4062 struct vfsconf *vfsp;
4067 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4068 #ifdef COMPAT_FREEBSD32
4069 if (req->flags & SCTL_MASK32)
4070 error = vfsconf2x32(req, vfsp);
4073 error = vfsconf2x(req, vfsp);
4081 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
4082 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
4083 "S,xvfsconf", "List of all configured filesystems");
4085 #ifndef BURN_BRIDGES
4086 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
4089 vfs_sysctl(SYSCTL_HANDLER_ARGS)
4091 int *name = (int *)arg1 - 1; /* XXX */
4092 u_int namelen = arg2 + 1; /* XXX */
4093 struct vfsconf *vfsp;
4095 log(LOG_WARNING, "userland calling deprecated sysctl, "
4096 "please rebuild world\n");
4098 #if 1 || defined(COMPAT_PRELITE2)
4099 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
4101 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
4105 case VFS_MAXTYPENUM:
4108 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
4111 return (ENOTDIR); /* overloaded */
4113 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4114 if (vfsp->vfc_typenum == name[2])
4119 return (EOPNOTSUPP);
4120 #ifdef COMPAT_FREEBSD32
4121 if (req->flags & SCTL_MASK32)
4122 return (vfsconf2x32(req, vfsp));
4125 return (vfsconf2x(req, vfsp));
4127 return (EOPNOTSUPP);
4130 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
4131 CTLFLAG_MPSAFE, vfs_sysctl,
4132 "Generic filesystem");
4134 #if 1 || defined(COMPAT_PRELITE2)
4137 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
4140 struct vfsconf *vfsp;
4141 struct ovfsconf ovfs;
4144 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4145 bzero(&ovfs, sizeof(ovfs));
4146 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
4147 strcpy(ovfs.vfc_name, vfsp->vfc_name);
4148 ovfs.vfc_index = vfsp->vfc_typenum;
4149 ovfs.vfc_refcount = vfsp->vfc_refcount;
4150 ovfs.vfc_flags = vfsp->vfc_flags;
4151 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
4161 #endif /* 1 || COMPAT_PRELITE2 */
4162 #endif /* !BURN_BRIDGES */
4164 #define KINFO_VNODESLOP 10
4167 * Dump vnode list (via sysctl).
4171 sysctl_vnode(SYSCTL_HANDLER_ARGS)
4179 * Stale numvnodes access is not fatal here.
4182 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
4184 /* Make an estimate */
4185 return (SYSCTL_OUT(req, 0, len));
4187 error = sysctl_wire_old_buffer(req, 0);
4190 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
4192 mtx_lock(&mountlist_mtx);
4193 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4194 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
4197 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4201 xvn[n].xv_size = sizeof *xvn;
4202 xvn[n].xv_vnode = vp;
4203 xvn[n].xv_id = 0; /* XXX compat */
4204 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
4206 XV_COPY(writecount);
4212 xvn[n].xv_flag = vp->v_vflag;
4214 switch (vp->v_type) {
4221 if (vp->v_rdev == NULL) {
4225 xvn[n].xv_dev = dev2udev(vp->v_rdev);
4228 xvn[n].xv_socket = vp->v_socket;
4231 xvn[n].xv_fifo = vp->v_fifoinfo;
4236 /* shouldn't happen? */
4244 mtx_lock(&mountlist_mtx);
4249 mtx_unlock(&mountlist_mtx);
4251 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
4256 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
4257 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
4262 unmount_or_warn(struct mount *mp)
4266 error = dounmount(mp, MNT_FORCE, curthread);
4268 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
4272 printf("%d)\n", error);
4277 * Unmount all filesystems. The list is traversed in reverse order
4278 * of mounting to avoid dependencies.
4281 vfs_unmountall(void)
4283 struct mount *mp, *tmp;
4285 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
4288 * Since this only runs when rebooting, it is not interlocked.
4290 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
4294 * Forcibly unmounting "/dev" before "/" would prevent clean
4295 * unmount of the latter.
4297 if (mp == rootdevmp)
4300 unmount_or_warn(mp);
4303 if (rootdevmp != NULL)
4304 unmount_or_warn(rootdevmp);
4308 * perform msync on all vnodes under a mount point
4309 * the mount point must be locked.
4312 vfs_msync(struct mount *mp, int flags)
4314 struct vnode *vp, *mvp;
4315 struct vm_object *obj;
4317 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
4319 vnlru_return_batch(mp);
4321 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
4323 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
4324 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
4326 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
4328 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
4335 VM_OBJECT_WLOCK(obj);
4336 vm_object_page_clean(obj, 0, 0,
4338 OBJPC_SYNC : OBJPC_NOSYNC);
4339 VM_OBJECT_WUNLOCK(obj);
4349 destroy_vpollinfo_free(struct vpollinfo *vi)
4352 knlist_destroy(&vi->vpi_selinfo.si_note);
4353 mtx_destroy(&vi->vpi_lock);
4354 uma_zfree(vnodepoll_zone, vi);
4358 destroy_vpollinfo(struct vpollinfo *vi)
4361 knlist_clear(&vi->vpi_selinfo.si_note, 1);
4362 seldrain(&vi->vpi_selinfo);
4363 destroy_vpollinfo_free(vi);
4367 * Initialize per-vnode helper structure to hold poll-related state.
4370 v_addpollinfo(struct vnode *vp)
4372 struct vpollinfo *vi;
4374 if (vp->v_pollinfo != NULL)
4376 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
4377 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
4378 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
4379 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
4381 if (vp->v_pollinfo != NULL) {
4383 destroy_vpollinfo_free(vi);
4386 vp->v_pollinfo = vi;
4391 * Record a process's interest in events which might happen to
4392 * a vnode. Because poll uses the historic select-style interface
4393 * internally, this routine serves as both the ``check for any
4394 * pending events'' and the ``record my interest in future events''
4395 * functions. (These are done together, while the lock is held,
4396 * to avoid race conditions.)
4399 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
4403 mtx_lock(&vp->v_pollinfo->vpi_lock);
4404 if (vp->v_pollinfo->vpi_revents & events) {
4406 * This leaves events we are not interested
4407 * in available for the other process which
4408 * which presumably had requested them
4409 * (otherwise they would never have been
4412 events &= vp->v_pollinfo->vpi_revents;
4413 vp->v_pollinfo->vpi_revents &= ~events;
4415 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4418 vp->v_pollinfo->vpi_events |= events;
4419 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
4420 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4425 * Routine to create and manage a filesystem syncer vnode.
4427 #define sync_close ((int (*)(struct vop_close_args *))nullop)
4428 static int sync_fsync(struct vop_fsync_args *);
4429 static int sync_inactive(struct vop_inactive_args *);
4430 static int sync_reclaim(struct vop_reclaim_args *);
4432 static struct vop_vector sync_vnodeops = {
4433 .vop_bypass = VOP_EOPNOTSUPP,
4434 .vop_close = sync_close, /* close */
4435 .vop_fsync = sync_fsync, /* fsync */
4436 .vop_inactive = sync_inactive, /* inactive */
4437 .vop_need_inactive = vop_stdneed_inactive, /* need_inactive */
4438 .vop_reclaim = sync_reclaim, /* reclaim */
4439 .vop_lock1 = vop_stdlock, /* lock */
4440 .vop_unlock = vop_stdunlock, /* unlock */
4441 .vop_islocked = vop_stdislocked, /* islocked */
4445 * Create a new filesystem syncer vnode for the specified mount point.
4448 vfs_allocate_syncvnode(struct mount *mp)
4452 static long start, incr, next;
4455 /* Allocate a new vnode */
4456 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
4458 panic("vfs_allocate_syncvnode: getnewvnode() failed");
4460 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4461 vp->v_vflag |= VV_FORCEINSMQ;
4462 error = insmntque(vp, mp);
4464 panic("vfs_allocate_syncvnode: insmntque() failed");
4465 vp->v_vflag &= ~VV_FORCEINSMQ;
4468 * Place the vnode onto the syncer worklist. We attempt to
4469 * scatter them about on the list so that they will go off
4470 * at evenly distributed times even if all the filesystems
4471 * are mounted at once.
4474 if (next == 0 || next > syncer_maxdelay) {
4478 start = syncer_maxdelay / 2;
4479 incr = syncer_maxdelay;
4485 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
4486 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
4487 mtx_lock(&sync_mtx);
4489 if (mp->mnt_syncer == NULL) {
4490 mp->mnt_syncer = vp;
4493 mtx_unlock(&sync_mtx);
4496 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4503 vfs_deallocate_syncvnode(struct mount *mp)
4507 mtx_lock(&sync_mtx);
4508 vp = mp->mnt_syncer;
4510 mp->mnt_syncer = NULL;
4511 mtx_unlock(&sync_mtx);
4517 * Do a lazy sync of the filesystem.
4520 sync_fsync(struct vop_fsync_args *ap)
4522 struct vnode *syncvp = ap->a_vp;
4523 struct mount *mp = syncvp->v_mount;
4528 * We only need to do something if this is a lazy evaluation.
4530 if (ap->a_waitfor != MNT_LAZY)
4534 * Move ourselves to the back of the sync list.
4536 bo = &syncvp->v_bufobj;
4538 vn_syncer_add_to_worklist(bo, syncdelay);
4542 * Walk the list of vnodes pushing all that are dirty and
4543 * not already on the sync list.
4545 if (vfs_busy(mp, MBF_NOWAIT) != 0)
4547 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
4551 save = curthread_pflags_set(TDP_SYNCIO);
4552 vfs_msync(mp, MNT_NOWAIT);
4553 error = VFS_SYNC(mp, MNT_LAZY);
4554 curthread_pflags_restore(save);
4555 vn_finished_write(mp);
4561 * The syncer vnode is no referenced.
4564 sync_inactive(struct vop_inactive_args *ap)
4572 * The syncer vnode is no longer needed and is being decommissioned.
4574 * Modifications to the worklist must be protected by sync_mtx.
4577 sync_reclaim(struct vop_reclaim_args *ap)
4579 struct vnode *vp = ap->a_vp;
4584 mtx_lock(&sync_mtx);
4585 if (vp->v_mount->mnt_syncer == vp)
4586 vp->v_mount->mnt_syncer = NULL;
4587 if (bo->bo_flag & BO_ONWORKLST) {
4588 LIST_REMOVE(bo, bo_synclist);
4589 syncer_worklist_len--;
4591 bo->bo_flag &= ~BO_ONWORKLST;
4593 mtx_unlock(&sync_mtx);
4600 vn_need_pageq_flush(struct vnode *vp)
4602 struct vm_object *obj;
4605 MPASS(mtx_owned(VI_MTX(vp)));
4607 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
4608 (obj->flags & OBJ_MIGHTBEDIRTY) != 0)
4614 * Check if vnode represents a disk device
4617 vn_isdisk(struct vnode *vp, int *errp)
4621 if (vp->v_type != VCHR) {
4627 if (vp->v_rdev == NULL)
4629 else if (vp->v_rdev->si_devsw == NULL)
4631 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
4637 return (error == 0);
4641 * Common filesystem object access control check routine. Accepts a
4642 * vnode's type, "mode", uid and gid, requested access mode, credentials,
4643 * and optional call-by-reference privused argument allowing vaccess()
4644 * to indicate to the caller whether privilege was used to satisfy the
4645 * request (obsoleted). Returns 0 on success, or an errno on failure.
4648 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
4649 accmode_t accmode, struct ucred *cred, int *privused)
4651 accmode_t dac_granted;
4652 accmode_t priv_granted;
4654 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
4655 ("invalid bit in accmode"));
4656 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
4657 ("VAPPEND without VWRITE"));
4660 * Look for a normal, non-privileged way to access the file/directory
4661 * as requested. If it exists, go with that.
4664 if (privused != NULL)
4669 /* Check the owner. */
4670 if (cred->cr_uid == file_uid) {
4671 dac_granted |= VADMIN;
4672 if (file_mode & S_IXUSR)
4673 dac_granted |= VEXEC;
4674 if (file_mode & S_IRUSR)
4675 dac_granted |= VREAD;
4676 if (file_mode & S_IWUSR)
4677 dac_granted |= (VWRITE | VAPPEND);
4679 if ((accmode & dac_granted) == accmode)
4685 /* Otherwise, check the groups (first match) */
4686 if (groupmember(file_gid, cred)) {
4687 if (file_mode & S_IXGRP)
4688 dac_granted |= VEXEC;
4689 if (file_mode & S_IRGRP)
4690 dac_granted |= VREAD;
4691 if (file_mode & S_IWGRP)
4692 dac_granted |= (VWRITE | VAPPEND);
4694 if ((accmode & dac_granted) == accmode)
4700 /* Otherwise, check everyone else. */
4701 if (file_mode & S_IXOTH)
4702 dac_granted |= VEXEC;
4703 if (file_mode & S_IROTH)
4704 dac_granted |= VREAD;
4705 if (file_mode & S_IWOTH)
4706 dac_granted |= (VWRITE | VAPPEND);
4707 if ((accmode & dac_granted) == accmode)
4712 * Build a privilege mask to determine if the set of privileges
4713 * satisfies the requirements when combined with the granted mask
4714 * from above. For each privilege, if the privilege is required,
4715 * bitwise or the request type onto the priv_granted mask.
4721 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
4722 * requests, instead of PRIV_VFS_EXEC.
4724 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4725 !priv_check_cred(cred, PRIV_VFS_LOOKUP))
4726 priv_granted |= VEXEC;
4729 * Ensure that at least one execute bit is on. Otherwise,
4730 * a privileged user will always succeed, and we don't want
4731 * this to happen unless the file really is executable.
4733 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4734 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
4735 !priv_check_cred(cred, PRIV_VFS_EXEC))
4736 priv_granted |= VEXEC;
4739 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
4740 !priv_check_cred(cred, PRIV_VFS_READ))
4741 priv_granted |= VREAD;
4743 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
4744 !priv_check_cred(cred, PRIV_VFS_WRITE))
4745 priv_granted |= (VWRITE | VAPPEND);
4747 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
4748 !priv_check_cred(cred, PRIV_VFS_ADMIN))
4749 priv_granted |= VADMIN;
4751 if ((accmode & (priv_granted | dac_granted)) == accmode) {
4752 /* XXX audit: privilege used */
4753 if (privused != NULL)
4758 return ((accmode & VADMIN) ? EPERM : EACCES);
4762 * Credential check based on process requesting service, and per-attribute
4766 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
4767 struct thread *td, accmode_t accmode)
4771 * Kernel-invoked always succeeds.
4777 * Do not allow privileged processes in jail to directly manipulate
4778 * system attributes.
4780 switch (attrnamespace) {
4781 case EXTATTR_NAMESPACE_SYSTEM:
4782 /* Potentially should be: return (EPERM); */
4783 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM));
4784 case EXTATTR_NAMESPACE_USER:
4785 return (VOP_ACCESS(vp, accmode, cred, td));
4791 #ifdef DEBUG_VFS_LOCKS
4793 * This only exists to suppress warnings from unlocked specfs accesses. It is
4794 * no longer ok to have an unlocked VFS.
4796 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4797 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4799 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4800 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4801 "Drop into debugger on lock violation");
4803 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4804 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4805 0, "Check for interlock across VOPs");
4807 int vfs_badlock_print = 1; /* Print lock violations. */
4808 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4809 0, "Print lock violations");
4811 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */
4812 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode,
4813 0, "Print vnode details on lock violations");
4816 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4817 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4818 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4822 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4826 if (vfs_badlock_backtrace)
4829 if (vfs_badlock_vnode)
4830 vn_printf(vp, "vnode ");
4831 if (vfs_badlock_print)
4832 printf("%s: %p %s\n", str, (void *)vp, msg);
4833 if (vfs_badlock_ddb)
4834 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4838 assert_vi_locked(struct vnode *vp, const char *str)
4841 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4842 vfs_badlock("interlock is not locked but should be", str, vp);
4846 assert_vi_unlocked(struct vnode *vp, const char *str)
4849 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4850 vfs_badlock("interlock is locked but should not be", str, vp);
4854 assert_vop_locked(struct vnode *vp, const char *str)
4858 if (!IGNORE_LOCK(vp)) {
4859 locked = VOP_ISLOCKED(vp);
4860 if (locked == 0 || locked == LK_EXCLOTHER)
4861 vfs_badlock("is not locked but should be", str, vp);
4866 assert_vop_unlocked(struct vnode *vp, const char *str)
4869 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4870 vfs_badlock("is locked but should not be", str, vp);
4874 assert_vop_elocked(struct vnode *vp, const char *str)
4877 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4878 vfs_badlock("is not exclusive locked but should be", str, vp);
4880 #endif /* DEBUG_VFS_LOCKS */
4883 vop_rename_fail(struct vop_rename_args *ap)
4886 if (ap->a_tvp != NULL)
4888 if (ap->a_tdvp == ap->a_tvp)
4897 vop_rename_pre(void *ap)
4899 struct vop_rename_args *a = ap;
4901 #ifdef DEBUG_VFS_LOCKS
4903 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4904 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4905 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4906 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4908 /* Check the source (from). */
4909 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4910 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4911 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4912 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4913 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4915 /* Check the target. */
4917 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4918 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4920 if (a->a_tdvp != a->a_fdvp)
4922 if (a->a_tvp != a->a_fvp)
4929 #ifdef DEBUG_VFS_LOCKS
4931 vop_strategy_pre(void *ap)
4933 struct vop_strategy_args *a;
4940 * Cluster ops lock their component buffers but not the IO container.
4942 if ((bp->b_flags & B_CLUSTER) != 0)
4945 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4946 if (vfs_badlock_print)
4948 "VOP_STRATEGY: bp is not locked but should be\n");
4949 if (vfs_badlock_ddb)
4950 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4955 vop_lock_pre(void *ap)
4957 struct vop_lock1_args *a = ap;
4959 if ((a->a_flags & LK_INTERLOCK) == 0)
4960 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4962 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4966 vop_lock_post(void *ap, int rc)
4968 struct vop_lock1_args *a = ap;
4970 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4971 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4972 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4976 vop_unlock_pre(void *ap)
4978 struct vop_unlock_args *a = ap;
4980 if (a->a_flags & LK_INTERLOCK)
4981 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4982 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4986 vop_unlock_post(void *ap, int rc)
4988 struct vop_unlock_args *a = ap;
4990 if (a->a_flags & LK_INTERLOCK)
4991 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4995 vop_need_inactive_pre(void *ap)
4997 struct vop_need_inactive_args *a = ap;
4999 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5003 vop_need_inactive_post(void *ap, int rc)
5005 struct vop_need_inactive_args *a = ap;
5007 ASSERT_VI_LOCKED(a->a_vp, "VOP_NEED_INACTIVE");
5012 vop_create_post(void *ap, int rc)
5014 struct vop_create_args *a = ap;
5017 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
5021 vop_deleteextattr_post(void *ap, int rc)
5023 struct vop_deleteextattr_args *a = ap;
5026 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5030 vop_link_post(void *ap, int rc)
5032 struct vop_link_args *a = ap;
5035 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
5036 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
5041 vop_mkdir_post(void *ap, int rc)
5043 struct vop_mkdir_args *a = ap;
5046 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
5050 vop_mknod_post(void *ap, int rc)
5052 struct vop_mknod_args *a = ap;
5055 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
5059 vop_reclaim_post(void *ap, int rc)
5061 struct vop_reclaim_args *a = ap;
5064 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE);
5068 vop_remove_post(void *ap, int rc)
5070 struct vop_remove_args *a = ap;
5073 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
5074 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
5079 vop_rename_post(void *ap, int rc)
5081 struct vop_rename_args *a = ap;
5086 if (a->a_fdvp == a->a_tdvp) {
5087 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR)
5089 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5090 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5092 hint |= NOTE_EXTEND;
5093 if (a->a_fvp->v_type == VDIR)
5095 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
5097 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL &&
5098 a->a_tvp->v_type == VDIR)
5100 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
5103 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
5105 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
5107 if (a->a_tdvp != a->a_fdvp)
5109 if (a->a_tvp != a->a_fvp)
5117 vop_rmdir_post(void *ap, int rc)
5119 struct vop_rmdir_args *a = ap;
5122 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
5123 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
5128 vop_setattr_post(void *ap, int rc)
5130 struct vop_setattr_args *a = ap;
5133 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5137 vop_setextattr_post(void *ap, int rc)
5139 struct vop_setextattr_args *a = ap;
5142 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5146 vop_symlink_post(void *ap, int rc)
5148 struct vop_symlink_args *a = ap;
5151 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
5155 vop_open_post(void *ap, int rc)
5157 struct vop_open_args *a = ap;
5160 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN);
5164 vop_close_post(void *ap, int rc)
5166 struct vop_close_args *a = ap;
5168 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */
5169 (a->a_vp->v_iflag & VI_DOOMED) == 0)) {
5170 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ?
5171 NOTE_CLOSE_WRITE : NOTE_CLOSE);
5176 vop_read_post(void *ap, int rc)
5178 struct vop_read_args *a = ap;
5181 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5185 vop_readdir_post(void *ap, int rc)
5187 struct vop_readdir_args *a = ap;
5190 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5193 static struct knlist fs_knlist;
5196 vfs_event_init(void *arg)
5198 knlist_init_mtx(&fs_knlist, NULL);
5200 /* XXX - correct order? */
5201 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
5204 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
5207 KNOTE_UNLOCKED(&fs_knlist, event);
5210 static int filt_fsattach(struct knote *kn);
5211 static void filt_fsdetach(struct knote *kn);
5212 static int filt_fsevent(struct knote *kn, long hint);
5214 struct filterops fs_filtops = {
5216 .f_attach = filt_fsattach,
5217 .f_detach = filt_fsdetach,
5218 .f_event = filt_fsevent
5222 filt_fsattach(struct knote *kn)
5225 kn->kn_flags |= EV_CLEAR;
5226 knlist_add(&fs_knlist, kn, 0);
5231 filt_fsdetach(struct knote *kn)
5234 knlist_remove(&fs_knlist, kn, 0);
5238 filt_fsevent(struct knote *kn, long hint)
5241 kn->kn_fflags |= hint;
5242 return (kn->kn_fflags != 0);
5246 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
5252 error = SYSCTL_IN(req, &vc, sizeof(vc));
5255 if (vc.vc_vers != VFS_CTL_VERS1)
5257 mp = vfs_getvfs(&vc.vc_fsid);
5260 /* ensure that a specific sysctl goes to the right filesystem. */
5261 if (strcmp(vc.vc_fstypename, "*") != 0 &&
5262 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
5266 VCTLTOREQ(&vc, req);
5267 error = VFS_SYSCTL(mp, vc.vc_op, req);
5272 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
5273 NULL, 0, sysctl_vfs_ctl, "",
5277 * Function to initialize a va_filerev field sensibly.
5278 * XXX: Wouldn't a random number make a lot more sense ??
5281 init_va_filerev(void)
5286 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
5289 static int filt_vfsread(struct knote *kn, long hint);
5290 static int filt_vfswrite(struct knote *kn, long hint);
5291 static int filt_vfsvnode(struct knote *kn, long hint);
5292 static void filt_vfsdetach(struct knote *kn);
5293 static struct filterops vfsread_filtops = {
5295 .f_detach = filt_vfsdetach,
5296 .f_event = filt_vfsread
5298 static struct filterops vfswrite_filtops = {
5300 .f_detach = filt_vfsdetach,
5301 .f_event = filt_vfswrite
5303 static struct filterops vfsvnode_filtops = {
5305 .f_detach = filt_vfsdetach,
5306 .f_event = filt_vfsvnode
5310 vfs_knllock(void *arg)
5312 struct vnode *vp = arg;
5314 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5318 vfs_knlunlock(void *arg)
5320 struct vnode *vp = arg;
5326 vfs_knl_assert_locked(void *arg)
5328 #ifdef DEBUG_VFS_LOCKS
5329 struct vnode *vp = arg;
5331 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
5336 vfs_knl_assert_unlocked(void *arg)
5338 #ifdef DEBUG_VFS_LOCKS
5339 struct vnode *vp = arg;
5341 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
5346 vfs_kqfilter(struct vop_kqfilter_args *ap)
5348 struct vnode *vp = ap->a_vp;
5349 struct knote *kn = ap->a_kn;
5352 switch (kn->kn_filter) {
5354 kn->kn_fop = &vfsread_filtops;
5357 kn->kn_fop = &vfswrite_filtops;
5360 kn->kn_fop = &vfsvnode_filtops;
5366 kn->kn_hook = (caddr_t)vp;
5369 if (vp->v_pollinfo == NULL)
5371 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
5373 knlist_add(knl, kn, 0);
5379 * Detach knote from vnode
5382 filt_vfsdetach(struct knote *kn)
5384 struct vnode *vp = (struct vnode *)kn->kn_hook;
5386 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
5387 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
5393 filt_vfsread(struct knote *kn, long hint)
5395 struct vnode *vp = (struct vnode *)kn->kn_hook;
5400 * filesystem is gone, so set the EOF flag and schedule
5401 * the knote for deletion.
5403 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5405 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5410 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
5414 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
5415 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
5422 filt_vfswrite(struct knote *kn, long hint)
5424 struct vnode *vp = (struct vnode *)kn->kn_hook;
5429 * filesystem is gone, so set the EOF flag and schedule
5430 * the knote for deletion.
5432 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
5433 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5441 filt_vfsvnode(struct knote *kn, long hint)
5443 struct vnode *vp = (struct vnode *)kn->kn_hook;
5447 if (kn->kn_sfflags & hint)
5448 kn->kn_fflags |= hint;
5449 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5450 kn->kn_flags |= EV_EOF;
5454 res = (kn->kn_fflags != 0);
5460 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
5464 if (dp->d_reclen > ap->a_uio->uio_resid)
5465 return (ENAMETOOLONG);
5466 error = uiomove(dp, dp->d_reclen, ap->a_uio);
5468 if (ap->a_ncookies != NULL) {
5469 if (ap->a_cookies != NULL)
5470 free(ap->a_cookies, M_TEMP);
5471 ap->a_cookies = NULL;
5472 *ap->a_ncookies = 0;
5476 if (ap->a_ncookies == NULL)
5479 KASSERT(ap->a_cookies,
5480 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
5482 *ap->a_cookies = realloc(*ap->a_cookies,
5483 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
5484 (*ap->a_cookies)[*ap->a_ncookies] = off;
5485 *ap->a_ncookies += 1;
5490 * Mark for update the access time of the file if the filesystem
5491 * supports VOP_MARKATIME. This functionality is used by execve and
5492 * mmap, so we want to avoid the I/O implied by directly setting
5493 * va_atime for the sake of efficiency.
5496 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
5501 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
5502 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
5503 (void)VOP_MARKATIME(vp);
5507 * The purpose of this routine is to remove granularity from accmode_t,
5508 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
5509 * VADMIN and VAPPEND.
5511 * If it returns 0, the caller is supposed to continue with the usual
5512 * access checks using 'accmode' as modified by this routine. If it
5513 * returns nonzero value, the caller is supposed to return that value
5516 * Note that after this routine runs, accmode may be zero.
5519 vfs_unixify_accmode(accmode_t *accmode)
5522 * There is no way to specify explicit "deny" rule using
5523 * file mode or POSIX.1e ACLs.
5525 if (*accmode & VEXPLICIT_DENY) {
5531 * None of these can be translated into usual access bits.
5532 * Also, the common case for NFSv4 ACLs is to not contain
5533 * either of these bits. Caller should check for VWRITE
5534 * on the containing directory instead.
5536 if (*accmode & (VDELETE_CHILD | VDELETE))
5539 if (*accmode & VADMIN_PERMS) {
5540 *accmode &= ~VADMIN_PERMS;
5545 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
5546 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
5548 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
5554 * These are helper functions for filesystems to traverse all
5555 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
5557 * This interface replaces MNT_VNODE_FOREACH.
5560 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
5563 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
5568 kern_yield(PRI_USER);
5570 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5571 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL;
5572 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
5573 /* Allow a racy peek at VI_DOOMED to save a lock acquisition. */
5574 if (vp->v_type == VMARKER || (vp->v_iflag & VI_DOOMED) != 0)
5577 if ((vp->v_iflag & VI_DOOMED) != 0) {
5584 __mnt_vnode_markerfree_all(mvp, mp);
5585 /* MNT_IUNLOCK(mp); -- done in above function */
5586 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
5589 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
5590 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
5596 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
5600 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
5603 (*mvp)->v_mount = mp;
5604 (*mvp)->v_type = VMARKER;
5606 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
5607 /* Allow a racy peek at VI_DOOMED to save a lock acquisition. */
5608 if (vp->v_type == VMARKER || (vp->v_iflag & VI_DOOMED) != 0)
5611 if ((vp->v_iflag & VI_DOOMED) != 0) {
5620 free(*mvp, M_VNODE_MARKER);
5624 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
5630 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
5638 mtx_assert(MNT_MTX(mp), MA_OWNED);
5640 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5641 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
5644 free(*mvp, M_VNODE_MARKER);
5649 * These are helper functions for filesystems to traverse their
5650 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
5653 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
5656 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5661 free(*mvp, M_VNODE_MARKER);
5666 * Relock the mp mount vnode list lock with the vp vnode interlock in the
5667 * conventional lock order during mnt_vnode_next_active iteration.
5669 * On entry, the mount vnode list lock is held and the vnode interlock is not.
5670 * The list lock is dropped and reacquired. On success, both locks are held.
5671 * On failure, the mount vnode list lock is held but the vnode interlock is
5672 * not, and the procedure may have yielded.
5675 mnt_vnode_next_active_relock(struct vnode *mvp, struct mount *mp,
5678 const struct vnode *tmp;
5681 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER &&
5682 TAILQ_NEXT(mvp, v_actfreelist) != NULL, mvp,
5683 ("%s: bad marker", __func__));
5684 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp,
5685 ("%s: inappropriate vnode", __func__));
5686 ASSERT_VI_UNLOCKED(vp, __func__);
5687 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5691 TAILQ_REMOVE(&mp->mnt_activevnodelist, mvp, v_actfreelist);
5692 TAILQ_INSERT_BEFORE(vp, mvp, v_actfreelist);
5695 * Use a hold to prevent vp from disappearing while the mount vnode
5696 * list lock is dropped and reacquired. Normally a hold would be
5697 * acquired with vhold(), but that might try to acquire the vnode
5698 * interlock, which would be a LOR with the mount vnode list lock.
5700 held = refcount_acquire_if_not_zero(&vp->v_holdcnt);
5701 mtx_unlock(&mp->mnt_listmtx);
5705 if (!refcount_release_if_not_last(&vp->v_holdcnt)) {
5709 mtx_lock(&mp->mnt_listmtx);
5712 * Determine whether the vnode is still the next one after the marker,
5713 * excepting any other markers. If the vnode has not been doomed by
5714 * vgone() then the hold should have ensured that it remained on the
5715 * active list. If it has been doomed but is still on the active list,
5716 * don't abort, but rather skip over it (avoid spinning on doomed
5721 tmp = TAILQ_NEXT(tmp, v_actfreelist);
5722 } while (tmp != NULL && tmp->v_type == VMARKER);
5724 mtx_unlock(&mp->mnt_listmtx);
5733 mtx_lock(&mp->mnt_listmtx);
5736 ASSERT_VI_LOCKED(vp, __func__);
5738 ASSERT_VI_UNLOCKED(vp, __func__);
5739 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5743 static struct vnode *
5744 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
5746 struct vnode *vp, *nvp;
5748 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5749 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5751 vp = TAILQ_NEXT(*mvp, v_actfreelist);
5752 while (vp != NULL) {
5753 if (vp->v_type == VMARKER) {
5754 vp = TAILQ_NEXT(vp, v_actfreelist);
5758 * Try-lock because this is the wrong lock order. If that does
5759 * not succeed, drop the mount vnode list lock and try to
5760 * reacquire it and the vnode interlock in the right order.
5762 if (!VI_TRYLOCK(vp) &&
5763 !mnt_vnode_next_active_relock(*mvp, mp, vp))
5765 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
5766 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
5767 ("alien vnode on the active list %p %p", vp, mp));
5768 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
5770 nvp = TAILQ_NEXT(vp, v_actfreelist);
5774 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
5776 /* Check if we are done */
5778 mtx_unlock(&mp->mnt_listmtx);
5779 mnt_vnode_markerfree_active(mvp, mp);
5782 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
5783 mtx_unlock(&mp->mnt_listmtx);
5784 ASSERT_VI_LOCKED(vp, "active iter");
5785 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
5790 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
5794 kern_yield(PRI_USER);
5795 mtx_lock(&mp->mnt_listmtx);
5796 return (mnt_vnode_next_active(mvp, mp));
5800 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
5804 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
5808 (*mvp)->v_type = VMARKER;
5809 (*mvp)->v_mount = mp;
5811 mtx_lock(&mp->mnt_listmtx);
5812 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
5814 mtx_unlock(&mp->mnt_listmtx);
5815 mnt_vnode_markerfree_active(mvp, mp);
5818 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
5819 return (mnt_vnode_next_active(mvp, mp));
5823 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
5829 mtx_lock(&mp->mnt_listmtx);
5830 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
5831 mtx_unlock(&mp->mnt_listmtx);
5832 mnt_vnode_markerfree_active(mvp, mp);