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/condvar.h>
55 #include <sys/counter.h>
56 #include <sys/dirent.h>
57 #include <sys/event.h>
58 #include <sys/eventhandler.h>
59 #include <sys/extattr.h>
61 #include <sys/fcntl.h>
64 #include <sys/kernel.h>
65 #include <sys/kthread.h>
66 #include <sys/lockf.h>
67 #include <sys/malloc.h>
68 #include <sys/mount.h>
69 #include <sys/namei.h>
70 #include <sys/pctrie.h>
72 #include <sys/reboot.h>
73 #include <sys/refcount.h>
74 #include <sys/rwlock.h>
75 #include <sys/sched.h>
76 #include <sys/sleepqueue.h>
79 #include <sys/sysctl.h>
80 #include <sys/syslog.h>
81 #include <sys/vmmeter.h>
82 #include <sys/vnode.h>
83 #include <sys/watchdog.h>
85 #include <machine/stdarg.h>
87 #include <security/mac/mac_framework.h>
90 #include <vm/vm_object.h>
91 #include <vm/vm_extern.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_page.h>
95 #include <vm/vm_kern.h>
102 static void delmntque(struct vnode *vp);
103 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
104 int slpflag, int slptimeo);
105 static void syncer_shutdown(void *arg, int howto);
106 static int vtryrecycle(struct vnode *vp);
107 static void v_init_counters(struct vnode *);
108 static void v_incr_usecount(struct vnode *);
109 static void v_incr_usecount_locked(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);
121 * These fences are intended for cases where some synchronization is
122 * needed between access of v_iflags and lockless vnode refcount (v_holdcnt
123 * and v_usecount) updates. Access to v_iflags is generally synchronized
124 * by the interlock, but we have some internal assertions that check vnode
125 * flags without acquiring the lock. Thus, these fences are INVARIANTS-only
129 #define VNODE_REFCOUNT_FENCE_ACQ() atomic_thread_fence_acq()
130 #define VNODE_REFCOUNT_FENCE_REL() atomic_thread_fence_rel()
132 #define VNODE_REFCOUNT_FENCE_ACQ()
133 #define VNODE_REFCOUNT_FENCE_REL()
137 * Number of vnodes in existence. Increased whenever getnewvnode()
138 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
140 static unsigned long numvnodes;
142 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
143 "Number of vnodes in existence");
145 static counter_u64_t vnodes_created;
146 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
147 "Number of vnodes created by getnewvnode");
149 static u_long mnt_free_list_batch = 128;
150 SYSCTL_ULONG(_vfs, OID_AUTO, mnt_free_list_batch, CTLFLAG_RW,
151 &mnt_free_list_batch, 0, "Limit of vnodes held on mnt's free list");
154 * Conversion tables for conversion from vnode types to inode formats
157 enum vtype iftovt_tab[16] = {
158 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
159 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
161 int vttoif_tab[10] = {
162 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
163 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
167 * List of vnodes that are ready for recycling.
169 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
172 * "Free" vnode target. Free vnodes are rarely completely free, but are
173 * just ones that are cheap to recycle. Usually they are for files which
174 * have been stat'd but not read; these usually have inode and namecache
175 * data attached to them. This target is the preferred minimum size of a
176 * sub-cache consisting mostly of such files. The system balances the size
177 * of this sub-cache with its complement to try to prevent either from
178 * thrashing while the other is relatively inactive. The targets express
179 * a preference for the best balance.
181 * "Above" this target there are 2 further targets (watermarks) related
182 * to recyling of free vnodes. In the best-operating case, the cache is
183 * exactly full, the free list has size between vlowat and vhiwat above the
184 * free target, and recycling from it and normal use maintains this state.
185 * Sometimes the free list is below vlowat or even empty, but this state
186 * is even better for immediate use provided the cache is not full.
187 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free
188 * ones) to reach one of these states. The watermarks are currently hard-
189 * coded as 4% and 9% of the available space higher. These and the default
190 * of 25% for wantfreevnodes are too large if the memory size is large.
191 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim
192 * whenever vnlru_proc() becomes active.
194 static u_long wantfreevnodes;
195 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW,
196 &wantfreevnodes, 0, "Target for minimum number of \"free\" vnodes");
197 static u_long freevnodes;
198 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD,
199 &freevnodes, 0, "Number of \"free\" vnodes");
201 static counter_u64_t recycles_count;
202 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count,
203 "Number of vnodes recycled to meet vnode cache targets");
206 * Various variables used for debugging the new implementation of
208 * XXX these are probably of (very) limited utility now.
210 static int reassignbufcalls;
211 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
212 "Number of calls to reassignbuf");
214 static counter_u64_t free_owe_inact;
215 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact,
216 "Number of times free vnodes kept on active list due to VFS "
217 "owing inactivation");
219 /* To keep more than one thread at a time from running vfs_getnewfsid */
220 static struct mtx mntid_mtx;
223 * Lock for any access to the following:
228 static struct mtx vnode_free_list_mtx;
230 /* Publicly exported FS */
231 struct nfs_public nfs_pub;
233 static uma_zone_t buf_trie_zone;
235 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
236 static uma_zone_t vnode_zone;
237 static uma_zone_t vnodepoll_zone;
240 * The workitem queue.
242 * It is useful to delay writes of file data and filesystem metadata
243 * for tens of seconds so that quickly created and deleted files need
244 * not waste disk bandwidth being created and removed. To realize this,
245 * we append vnodes to a "workitem" queue. When running with a soft
246 * updates implementation, most pending metadata dependencies should
247 * not wait for more than a few seconds. Thus, mounted on block devices
248 * are delayed only about a half the time that file data is delayed.
249 * Similarly, directory updates are more critical, so are only delayed
250 * about a third the time that file data is delayed. Thus, there are
251 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
252 * one each second (driven off the filesystem syncer process). The
253 * syncer_delayno variable indicates the next queue that is to be processed.
254 * Items that need to be processed soon are placed in this queue:
256 * syncer_workitem_pending[syncer_delayno]
258 * A delay of fifteen seconds is done by placing the request fifteen
259 * entries later in the queue:
261 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
264 static int syncer_delayno;
265 static long syncer_mask;
266 LIST_HEAD(synclist, bufobj);
267 static struct synclist *syncer_workitem_pending;
269 * The sync_mtx protects:
274 * syncer_workitem_pending
275 * syncer_worklist_len
278 static struct mtx sync_mtx;
279 static struct cv sync_wakeup;
281 #define SYNCER_MAXDELAY 32
282 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
283 static int syncdelay = 30; /* max time to delay syncing data */
284 static int filedelay = 30; /* time to delay syncing files */
285 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
286 "Time to delay syncing files (in seconds)");
287 static int dirdelay = 29; /* time to delay syncing directories */
288 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
289 "Time to delay syncing directories (in seconds)");
290 static int metadelay = 28; /* time to delay syncing metadata */
291 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
292 "Time to delay syncing metadata (in seconds)");
293 static int rushjob; /* number of slots to run ASAP */
294 static int stat_rush_requests; /* number of times I/O speeded up */
295 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
296 "Number of times I/O speeded up (rush requests)");
299 * When shutting down the syncer, run it at four times normal speed.
301 #define SYNCER_SHUTDOWN_SPEEDUP 4
302 static int sync_vnode_count;
303 static int syncer_worklist_len;
304 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
307 /* Target for maximum number of vnodes. */
309 static int gapvnodes; /* gap between wanted and desired */
310 static int vhiwat; /* enough extras after expansion */
311 static int vlowat; /* minimal extras before expansion */
312 static int vstir; /* nonzero to stir non-free vnodes */
313 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */
316 sysctl_update_desiredvnodes(SYSCTL_HANDLER_ARGS)
318 int error, old_desiredvnodes;
320 old_desiredvnodes = desiredvnodes;
321 if ((error = sysctl_handle_int(oidp, arg1, arg2, req)) != 0)
323 if (old_desiredvnodes != desiredvnodes) {
324 wantfreevnodes = desiredvnodes / 4;
325 /* XXX locking seems to be incomplete. */
326 vfs_hash_changesize(desiredvnodes);
327 cache_changesize(desiredvnodes);
332 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
333 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &desiredvnodes, 0,
334 sysctl_update_desiredvnodes, "I", "Target for maximum number of vnodes");
335 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
336 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)");
337 static int vnlru_nowhere;
338 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
339 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
341 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
345 * Support for the bufobj clean & dirty pctrie.
348 buf_trie_alloc(struct pctrie *ptree)
351 return uma_zalloc(buf_trie_zone, M_NOWAIT);
355 buf_trie_free(struct pctrie *ptree, void *node)
358 uma_zfree(buf_trie_zone, node);
360 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
363 * Initialize the vnode management data structures.
365 * Reevaluate the following cap on the number of vnodes after the physical
366 * memory size exceeds 512GB. In the limit, as the physical memory size
367 * grows, the ratio of the memory size in KB to vnodes approaches 64:1.
369 #ifndef MAXVNODES_MAX
370 #define MAXVNODES_MAX (512 * 1024 * 1024 / 64) /* 8M */
374 * Initialize a vnode as it first enters the zone.
377 vnode_init(void *mem, int size, int flags)
386 vp->v_vnlock = &vp->v_lock;
387 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
389 * By default, don't allow shared locks unless filesystems opt-in.
391 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT,
392 LK_NOSHARE | LK_IS_VNODE);
396 bufobj_init(&vp->v_bufobj, vp);
398 * Initialize namecache.
400 LIST_INIT(&vp->v_cache_src);
401 TAILQ_INIT(&vp->v_cache_dst);
403 * Initialize rangelocks.
405 rangelock_init(&vp->v_rl);
410 * Free a vnode when it is cleared from the zone.
413 vnode_fini(void *mem, int size)
419 rangelock_destroy(&vp->v_rl);
420 lockdestroy(vp->v_vnlock);
421 mtx_destroy(&vp->v_interlock);
423 rw_destroy(BO_LOCKPTR(bo));
427 * Provide the size of NFS nclnode and NFS fh for calculation of the
428 * vnode memory consumption. The size is specified directly to
429 * eliminate dependency on NFS-private header.
431 * Other filesystems may use bigger or smaller (like UFS and ZFS)
432 * private inode data, but the NFS-based estimation is ample enough.
433 * Still, we care about differences in the size between 64- and 32-bit
436 * Namecache structure size is heuristically
437 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1.
440 #define NFS_NCLNODE_SZ (528 + 64)
443 #define NFS_NCLNODE_SZ (360 + 32)
448 vntblinit(void *dummy __unused)
451 int physvnodes, virtvnodes;
454 * Desiredvnodes is a function of the physical memory size and the
455 * kernel's heap size. Generally speaking, it scales with the
456 * physical memory size. The ratio of desiredvnodes to the physical
457 * memory size is 1:16 until desiredvnodes exceeds 98,304.
459 * marginal ratio of desiredvnodes to the physical memory size is
460 * 1:64. However, desiredvnodes is limited by the kernel's heap
461 * size. The memory required by desiredvnodes vnodes and vm objects
462 * must not exceed 1/10th of the kernel's heap size.
464 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 +
465 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64;
466 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) +
467 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ));
468 desiredvnodes = min(physvnodes, virtvnodes);
469 if (desiredvnodes > MAXVNODES_MAX) {
471 printf("Reducing kern.maxvnodes %d -> %d\n",
472 desiredvnodes, MAXVNODES_MAX);
473 desiredvnodes = MAXVNODES_MAX;
475 wantfreevnodes = desiredvnodes / 4;
476 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
477 TAILQ_INIT(&vnode_free_list);
478 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
479 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
480 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0);
481 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
482 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
484 * Preallocate enough nodes to support one-per buf so that
485 * we can not fail an insert. reassignbuf() callers can not
486 * tolerate the insertion failure.
488 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
489 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
490 UMA_ZONE_NOFREE | UMA_ZONE_VM);
491 uma_prealloc(buf_trie_zone, nbuf);
493 vnodes_created = counter_u64_alloc(M_WAITOK);
494 recycles_count = counter_u64_alloc(M_WAITOK);
495 free_owe_inact = counter_u64_alloc(M_WAITOK);
498 * Initialize the filesystem syncer.
500 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
502 syncer_maxdelay = syncer_mask + 1;
503 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
504 cv_init(&sync_wakeup, "syncer");
505 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
509 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
513 * Mark a mount point as busy. Used to synchronize access and to delay
514 * unmounting. Eventually, mountlist_mtx is not released on failure.
516 * vfs_busy() is a custom lock, it can block the caller.
517 * vfs_busy() only sleeps if the unmount is active on the mount point.
518 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
519 * vnode belonging to mp.
521 * Lookup uses vfs_busy() to traverse mount points.
523 * / vnode lock A / vnode lock (/var) D
524 * /var vnode lock B /log vnode lock(/var/log) E
525 * vfs_busy lock C vfs_busy lock F
527 * Within each file system, the lock order is C->A->B and F->D->E.
529 * When traversing across mounts, the system follows that lock order:
535 * The lookup() process for namei("/var") illustrates the process:
536 * VOP_LOOKUP() obtains B while A is held
537 * vfs_busy() obtains a shared lock on F while A and B are held
538 * vput() releases lock on B
539 * vput() releases lock on A
540 * VFS_ROOT() obtains lock on D while shared lock on F is held
541 * vfs_unbusy() releases shared lock on F
542 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
543 * Attempt to lock A (instead of vp_crossmp) while D is held would
544 * violate the global order, causing deadlocks.
546 * dounmount() locks B while F is drained.
549 vfs_busy(struct mount *mp, int flags)
552 MPASS((flags & ~MBF_MASK) == 0);
553 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
558 * If mount point is currently being unmounted, sleep until the
559 * mount point fate is decided. If thread doing the unmounting fails,
560 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
561 * that this mount point has survived the unmount attempt and vfs_busy
562 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
563 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
564 * about to be really destroyed. vfs_busy needs to release its
565 * reference on the mount point in this case and return with ENOENT,
566 * telling the caller that mount mount it tried to busy is no longer
569 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
570 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
573 CTR1(KTR_VFS, "%s: failed busying before sleeping",
577 if (flags & MBF_MNTLSTLOCK)
578 mtx_unlock(&mountlist_mtx);
579 mp->mnt_kern_flag |= MNTK_MWAIT;
580 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
581 if (flags & MBF_MNTLSTLOCK)
582 mtx_lock(&mountlist_mtx);
585 if (flags & MBF_MNTLSTLOCK)
586 mtx_unlock(&mountlist_mtx);
593 * Free a busy filesystem.
596 vfs_unbusy(struct mount *mp)
599 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
602 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
604 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
605 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
606 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
607 mp->mnt_kern_flag &= ~MNTK_DRAINING;
608 wakeup(&mp->mnt_lockref);
614 * Lookup a mount point by filesystem identifier.
617 vfs_getvfs(fsid_t *fsid)
621 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
622 mtx_lock(&mountlist_mtx);
623 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
624 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
625 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
627 mtx_unlock(&mountlist_mtx);
631 mtx_unlock(&mountlist_mtx);
632 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
633 return ((struct mount *) 0);
637 * Lookup a mount point by filesystem identifier, busying it before
640 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
641 * cache for popular filesystem identifiers. The cache is lockess, using
642 * the fact that struct mount's are never freed. In worst case we may
643 * get pointer to unmounted or even different filesystem, so we have to
644 * check what we got, and go slow way if so.
647 vfs_busyfs(fsid_t *fsid)
649 #define FSID_CACHE_SIZE 256
650 typedef struct mount * volatile vmp_t;
651 static vmp_t cache[FSID_CACHE_SIZE];
656 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
657 hash = fsid->val[0] ^ fsid->val[1];
658 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
661 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
662 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
664 if (vfs_busy(mp, 0) != 0) {
668 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
669 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
675 mtx_lock(&mountlist_mtx);
676 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
677 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
678 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
679 error = vfs_busy(mp, MBF_MNTLSTLOCK);
682 mtx_unlock(&mountlist_mtx);
689 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
690 mtx_unlock(&mountlist_mtx);
691 return ((struct mount *) 0);
695 * Check if a user can access privileged mount options.
698 vfs_suser(struct mount *mp, struct thread *td)
702 if (jailed(td->td_ucred)) {
704 * If the jail of the calling thread lacks permission for
705 * this type of file system, deny immediately.
707 if (!prison_allow(td->td_ucred, mp->mnt_vfc->vfc_prison_flag))
711 * If the file system was mounted outside the jail of the
712 * calling thread, deny immediately.
714 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
719 * If file system supports delegated administration, we don't check
720 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
721 * by the file system itself.
722 * If this is not the user that did original mount, we check for
723 * the PRIV_VFS_MOUNT_OWNER privilege.
725 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
726 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
727 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
734 * Get a new unique fsid. Try to make its val[0] unique, since this value
735 * will be used to create fake device numbers for stat(). Also try (but
736 * not so hard) make its val[0] unique mod 2^16, since some emulators only
737 * support 16-bit device numbers. We end up with unique val[0]'s for the
738 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
740 * Keep in mind that several mounts may be running in parallel. Starting
741 * the search one past where the previous search terminated is both a
742 * micro-optimization and a defense against returning the same fsid to
746 vfs_getnewfsid(struct mount *mp)
748 static uint16_t mntid_base;
753 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
754 mtx_lock(&mntid_mtx);
755 mtype = mp->mnt_vfc->vfc_typenum;
756 tfsid.val[1] = mtype;
757 mtype = (mtype & 0xFF) << 24;
759 tfsid.val[0] = makedev(255,
760 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
762 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
766 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
767 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
768 mtx_unlock(&mntid_mtx);
772 * Knob to control the precision of file timestamps:
774 * 0 = seconds only; nanoseconds zeroed.
775 * 1 = seconds and nanoseconds, accurate within 1/HZ.
776 * 2 = seconds and nanoseconds, truncated to microseconds.
777 * >=3 = seconds and nanoseconds, maximum precision.
779 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
781 static int timestamp_precision = TSP_USEC;
782 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
783 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
784 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, "
785 "3+: sec + ns (max. precision))");
788 * Get a current timestamp.
791 vfs_timestamp(struct timespec *tsp)
795 switch (timestamp_precision) {
797 tsp->tv_sec = time_second;
805 TIMEVAL_TO_TIMESPEC(&tv, tsp);
815 * Set vnode attributes to VNOVAL
818 vattr_null(struct vattr *vap)
822 vap->va_size = VNOVAL;
823 vap->va_bytes = VNOVAL;
824 vap->va_mode = VNOVAL;
825 vap->va_nlink = VNOVAL;
826 vap->va_uid = VNOVAL;
827 vap->va_gid = VNOVAL;
828 vap->va_fsid = VNOVAL;
829 vap->va_fileid = VNOVAL;
830 vap->va_blocksize = VNOVAL;
831 vap->va_rdev = VNOVAL;
832 vap->va_atime.tv_sec = VNOVAL;
833 vap->va_atime.tv_nsec = VNOVAL;
834 vap->va_mtime.tv_sec = VNOVAL;
835 vap->va_mtime.tv_nsec = VNOVAL;
836 vap->va_ctime.tv_sec = VNOVAL;
837 vap->va_ctime.tv_nsec = VNOVAL;
838 vap->va_birthtime.tv_sec = VNOVAL;
839 vap->va_birthtime.tv_nsec = VNOVAL;
840 vap->va_flags = VNOVAL;
841 vap->va_gen = VNOVAL;
846 * This routine is called when we have too many vnodes. It attempts
847 * to free <count> vnodes and will potentially free vnodes that still
848 * have VM backing store (VM backing store is typically the cause
849 * of a vnode blowout so we want to do this). Therefore, this operation
850 * is not considered cheap.
852 * A number of conditions may prevent a vnode from being reclaimed.
853 * the buffer cache may have references on the vnode, a directory
854 * vnode may still have references due to the namei cache representing
855 * underlying files, or the vnode may be in active use. It is not
856 * desirable to reuse such vnodes. These conditions may cause the
857 * number of vnodes to reach some minimum value regardless of what
858 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
861 vlrureclaim(struct mount *mp, int reclaim_nc_src, int trigger)
864 int count, done, target;
867 vn_start_write(NULL, &mp, V_WAIT);
869 count = mp->mnt_nvnodelistsize;
870 target = count * (int64_t)gapvnodes / imax(desiredvnodes, 1);
871 target = target / 10 + 1;
872 while (count != 0 && done < target) {
873 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
874 while (vp != NULL && vp->v_type == VMARKER)
875 vp = TAILQ_NEXT(vp, v_nmntvnodes);
879 * XXX LRU is completely broken for non-free vnodes. First
880 * by calling here in mountpoint order, then by moving
881 * unselected vnodes to the end here, and most grossly by
882 * removing the vlruvp() function that was supposed to
883 * maintain the order. (This function was born broken
884 * since syncer problems prevented it doing anything.) The
885 * order is closer to LRC (C = Created).
887 * LRU reclaiming of vnodes seems to have last worked in
888 * FreeBSD-3 where LRU wasn't mentioned under any spelling.
889 * Then there was no hold count, and inactive vnodes were
890 * simply put on the free list in LRU order. The separate
891 * lists also break LRU. We prefer to reclaim from the
892 * free list for technical reasons. This tends to thrash
893 * the free list to keep very unrecently used held vnodes.
894 * The problem is mitigated by keeping the free list large.
896 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
897 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
902 * If it's been deconstructed already, it's still
903 * referenced, or it exceeds the trigger, skip it.
904 * Also skip free vnodes. We are trying to make space
905 * to expand the free list, not reduce it.
907 if (vp->v_usecount ||
908 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
909 ((vp->v_iflag & VI_FREE) != 0) ||
910 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
911 vp->v_object->resident_page_count > trigger)) {
917 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
919 goto next_iter_mntunlocked;
923 * v_usecount may have been bumped after VOP_LOCK() dropped
924 * the vnode interlock and before it was locked again.
926 * It is not necessary to recheck VI_DOOMED because it can
927 * only be set by another thread that holds both the vnode
928 * lock and vnode interlock. If another thread has the
929 * vnode lock before we get to VOP_LOCK() and obtains the
930 * vnode interlock after VOP_LOCK() drops the vnode
931 * interlock, the other thread will be unable to drop the
932 * vnode lock before our VOP_LOCK() call fails.
934 if (vp->v_usecount ||
935 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
936 (vp->v_iflag & VI_FREE) != 0 ||
937 (vp->v_object != NULL &&
938 vp->v_object->resident_page_count > trigger)) {
939 VOP_UNLOCK(vp, LK_INTERLOCK);
941 goto next_iter_mntunlocked;
943 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
944 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
945 counter_u64_add(recycles_count, 1);
950 next_iter_mntunlocked:
959 kern_yield(PRI_USER);
964 vn_finished_write(mp);
968 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */
969 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free,
971 "limit on vnode free requests per call to the vnlru_free routine");
974 * Attempt to reduce the free list by the requested amount.
977 vnlru_free_locked(int count, struct vfsops *mnt_op)
983 tried_batches = false;
984 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
985 if (count > max_vnlru_free)
986 count = max_vnlru_free;
987 for (; count > 0; count--) {
988 vp = TAILQ_FIRST(&vnode_free_list);
990 * The list can be modified while the free_list_mtx
991 * has been dropped and vp could be NULL here.
996 mtx_unlock(&vnode_free_list_mtx);
997 vnlru_return_batches(mnt_op);
998 tried_batches = true;
999 mtx_lock(&vnode_free_list_mtx);
1003 VNASSERT(vp->v_op != NULL, vp,
1004 ("vnlru_free: vnode already reclaimed."));
1005 KASSERT((vp->v_iflag & VI_FREE) != 0,
1006 ("Removing vnode not on freelist"));
1007 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1008 ("Mangling active vnode"));
1009 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
1012 * Don't recycle if our vnode is from different type
1013 * of mount point. Note that mp is type-safe, the
1014 * check does not reach unmapped address even if
1015 * vnode is reclaimed.
1016 * Don't recycle if we can't get the interlock without
1019 if ((mnt_op != NULL && (mp = vp->v_mount) != NULL &&
1020 mp->mnt_op != mnt_op) || !VI_TRYLOCK(vp)) {
1021 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
1024 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
1025 vp, ("vp inconsistent on freelist"));
1028 * The clear of VI_FREE prevents activation of the
1029 * vnode. There is no sense in putting the vnode on
1030 * the mount point active list, only to remove it
1031 * later during recycling. Inline the relevant part
1032 * of vholdl(), to avoid triggering assertions or
1036 vp->v_iflag &= ~VI_FREE;
1037 VNODE_REFCOUNT_FENCE_REL();
1038 refcount_acquire(&vp->v_holdcnt);
1040 mtx_unlock(&vnode_free_list_mtx);
1044 * If the recycled succeeded this vdrop will actually free
1045 * the vnode. If not it will simply place it back on
1049 mtx_lock(&vnode_free_list_mtx);
1054 vnlru_free(int count, struct vfsops *mnt_op)
1057 mtx_lock(&vnode_free_list_mtx);
1058 vnlru_free_locked(count, mnt_op);
1059 mtx_unlock(&vnode_free_list_mtx);
1063 /* XXX some names and initialization are bad for limits and watermarks. */
1069 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100);
1070 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */
1071 vlowat = vhiwat / 2;
1072 if (numvnodes > desiredvnodes)
1074 space = desiredvnodes - numvnodes;
1075 if (freevnodes > wantfreevnodes)
1076 space += freevnodes - wantfreevnodes;
1081 vnlru_return_batch_locked(struct mount *mp)
1085 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
1087 if (mp->mnt_tmpfreevnodelistsize == 0)
1090 TAILQ_FOREACH(vp, &mp->mnt_tmpfreevnodelist, v_actfreelist) {
1091 VNASSERT((vp->v_mflag & VMP_TMPMNTFREELIST) != 0, vp,
1092 ("vnode without VMP_TMPMNTFREELIST on mnt_tmpfreevnodelist"));
1093 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
1095 mtx_lock(&vnode_free_list_mtx);
1096 TAILQ_CONCAT(&vnode_free_list, &mp->mnt_tmpfreevnodelist, v_actfreelist);
1097 freevnodes += mp->mnt_tmpfreevnodelistsize;
1098 mtx_unlock(&vnode_free_list_mtx);
1099 mp->mnt_tmpfreevnodelistsize = 0;
1103 vnlru_return_batch(struct mount *mp)
1106 mtx_lock(&mp->mnt_listmtx);
1107 vnlru_return_batch_locked(mp);
1108 mtx_unlock(&mp->mnt_listmtx);
1112 vnlru_return_batches(struct vfsops *mnt_op)
1114 struct mount *mp, *nmp;
1117 mtx_lock(&mountlist_mtx);
1118 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1119 need_unbusy = false;
1120 if (mnt_op != NULL && mp->mnt_op != mnt_op)
1122 if (mp->mnt_tmpfreevnodelistsize == 0)
1124 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) == 0) {
1125 vnlru_return_batch(mp);
1127 mtx_lock(&mountlist_mtx);
1130 nmp = TAILQ_NEXT(mp, mnt_list);
1134 mtx_unlock(&mountlist_mtx);
1138 * Attempt to recycle vnodes in a context that is always safe to block.
1139 * Calling vlrurecycle() from the bowels of filesystem code has some
1140 * interesting deadlock problems.
1142 static struct proc *vnlruproc;
1143 static int vnlruproc_sig;
1148 struct mount *mp, *nmp;
1149 unsigned long onumvnodes;
1150 int done, force, reclaim_nc_src, trigger, usevnodes;
1152 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc,
1153 SHUTDOWN_PRI_FIRST);
1157 kproc_suspend_check(vnlruproc);
1158 mtx_lock(&vnode_free_list_mtx);
1160 * If numvnodes is too large (due to desiredvnodes being
1161 * adjusted using its sysctl, or emergency growth), first
1162 * try to reduce it by discarding from the free list.
1164 if (numvnodes > desiredvnodes)
1165 vnlru_free_locked(numvnodes - desiredvnodes, NULL);
1167 * Sleep if the vnode cache is in a good state. This is
1168 * when it is not over-full and has space for about a 4%
1169 * or 9% expansion (by growing its size or inexcessively
1170 * reducing its free list). Otherwise, try to reclaim
1171 * space for a 10% expansion.
1173 if (vstir && force == 0) {
1177 if (vspace() >= vlowat && force == 0) {
1179 wakeup(&vnlruproc_sig);
1180 msleep(vnlruproc, &vnode_free_list_mtx,
1181 PVFS|PDROP, "vlruwt", hz);
1184 mtx_unlock(&vnode_free_list_mtx);
1186 onumvnodes = numvnodes;
1188 * Calculate parameters for recycling. These are the same
1189 * throughout the loop to give some semblance of fairness.
1190 * The trigger point is to avoid recycling vnodes with lots
1191 * of resident pages. We aren't trying to free memory; we
1192 * are trying to recycle or at least free vnodes.
1194 if (numvnodes <= desiredvnodes)
1195 usevnodes = numvnodes - freevnodes;
1197 usevnodes = numvnodes;
1201 * The trigger value is is chosen to give a conservatively
1202 * large value to ensure that it alone doesn't prevent
1203 * making progress. The value can easily be so large that
1204 * it is effectively infinite in some congested and
1205 * misconfigured cases, and this is necessary. Normally
1206 * it is about 8 to 100 (pages), which is quite large.
1208 trigger = vm_cnt.v_page_count * 2 / usevnodes;
1210 trigger = vsmalltrigger;
1211 reclaim_nc_src = force >= 3;
1212 mtx_lock(&mountlist_mtx);
1213 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1214 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
1215 nmp = TAILQ_NEXT(mp, mnt_list);
1218 done += vlrureclaim(mp, reclaim_nc_src, trigger);
1219 mtx_lock(&mountlist_mtx);
1220 nmp = TAILQ_NEXT(mp, mnt_list);
1223 mtx_unlock(&mountlist_mtx);
1224 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes)
1227 if (force == 0 || force == 1) {
1237 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
1239 kern_yield(PRI_USER);
1241 * After becoming active to expand above low water, keep
1242 * active until above high water.
1244 force = vspace() < vhiwat;
1248 static struct kproc_desc vnlru_kp = {
1253 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
1257 * Routines having to do with the management of the vnode table.
1261 * Try to recycle a freed vnode. We abort if anyone picks up a reference
1262 * before we actually vgone(). This function must be called with the vnode
1263 * held to prevent the vnode from being returned to the free list midway
1267 vtryrecycle(struct vnode *vp)
1271 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
1272 VNASSERT(vp->v_holdcnt, vp,
1273 ("vtryrecycle: Recycling vp %p without a reference.", vp));
1275 * This vnode may found and locked via some other list, if so we
1276 * can't recycle it yet.
1278 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1280 "%s: impossible to recycle, vp %p lock is already held",
1282 return (EWOULDBLOCK);
1285 * Don't recycle if its filesystem is being suspended.
1287 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1290 "%s: impossible to recycle, cannot start the write for %p",
1295 * If we got this far, we need to acquire the interlock and see if
1296 * anyone picked up this vnode from another list. If not, we will
1297 * mark it with DOOMED via vgonel() so that anyone who does find it
1298 * will skip over it.
1301 if (vp->v_usecount) {
1302 VOP_UNLOCK(vp, LK_INTERLOCK);
1303 vn_finished_write(vnmp);
1305 "%s: impossible to recycle, %p is already referenced",
1309 if ((vp->v_iflag & VI_DOOMED) == 0) {
1310 counter_u64_add(recycles_count, 1);
1313 VOP_UNLOCK(vp, LK_INTERLOCK);
1314 vn_finished_write(vnmp);
1322 if (vspace() < vlowat && vnlruproc_sig == 0) {
1329 * Wait if necessary for space for a new vnode.
1332 getnewvnode_wait(int suspended)
1335 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1336 if (numvnodes >= desiredvnodes) {
1339 * The file system is being suspended. We cannot
1340 * risk a deadlock here, so allow allocation of
1341 * another vnode even if this would give too many.
1345 if (vnlruproc_sig == 0) {
1346 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1349 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1352 /* Post-adjust like the pre-adjust in getnewvnode(). */
1353 if (numvnodes + 1 > desiredvnodes && freevnodes > 1)
1354 vnlru_free_locked(1, NULL);
1355 return (numvnodes >= desiredvnodes ? ENFILE : 0);
1359 * This hack is fragile, and probably not needed any more now that the
1360 * watermark handling works.
1363 getnewvnode_reserve(u_int count)
1367 /* Pre-adjust like the pre-adjust in getnewvnode(), with any count. */
1368 /* XXX no longer so quick, but this part is not racy. */
1369 mtx_lock(&vnode_free_list_mtx);
1370 if (numvnodes + count > desiredvnodes && freevnodes > wantfreevnodes)
1371 vnlru_free_locked(ulmin(numvnodes + count - desiredvnodes,
1372 freevnodes - wantfreevnodes), NULL);
1373 mtx_unlock(&vnode_free_list_mtx);
1376 /* First try to be quick and racy. */
1377 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1378 td->td_vp_reserv += count;
1379 vcheckspace(); /* XXX no longer so quick, but more racy */
1382 atomic_subtract_long(&numvnodes, count);
1384 mtx_lock(&vnode_free_list_mtx);
1386 if (getnewvnode_wait(0) == 0) {
1389 atomic_add_long(&numvnodes, 1);
1393 mtx_unlock(&vnode_free_list_mtx);
1397 * This hack is fragile, especially if desiredvnodes or wantvnodes are
1398 * misconfgured or changed significantly. Reducing desiredvnodes below
1399 * the reserved amount should cause bizarre behaviour like reducing it
1400 * below the number of active vnodes -- the system will try to reduce
1401 * numvnodes to match, but should fail, so the subtraction below should
1405 getnewvnode_drop_reserve(void)
1410 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1411 td->td_vp_reserv = 0;
1415 * Return the next vnode from the free list.
1418 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1423 struct lock_object *lo;
1424 static int cyclecount;
1427 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1430 if (td->td_vp_reserv > 0) {
1431 td->td_vp_reserv -= 1;
1434 mtx_lock(&vnode_free_list_mtx);
1435 if (numvnodes < desiredvnodes)
1437 else if (cyclecount++ >= freevnodes) {
1442 * Grow the vnode cache if it will not be above its target max
1443 * after growing. Otherwise, if the free list is nonempty, try
1444 * to reclaim 1 item from it before growing the cache (possibly
1445 * above its target max if the reclamation failed or is delayed).
1446 * Otherwise, wait for some space. In all cases, schedule
1447 * vnlru_proc() if we are getting short of space. The watermarks
1448 * should be chosen so that we never wait or even reclaim from
1449 * the free list to below its target minimum.
1451 if (numvnodes + 1 <= desiredvnodes)
1453 else if (freevnodes > 0)
1454 vnlru_free_locked(1, NULL);
1456 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1458 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1460 mtx_unlock(&vnode_free_list_mtx);
1466 atomic_add_long(&numvnodes, 1);
1467 mtx_unlock(&vnode_free_list_mtx);
1469 counter_u64_add(vnodes_created, 1);
1470 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK);
1472 * Locks are given the generic name "vnode" when created.
1473 * Follow the historic practice of using the filesystem
1474 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc.
1476 * Locks live in a witness group keyed on their name. Thus,
1477 * when a lock is renamed, it must also move from the witness
1478 * group of its old name to the witness group of its new name.
1480 * The change only needs to be made when the vnode moves
1481 * from one filesystem type to another. We ensure that each
1482 * filesystem use a single static name pointer for its tag so
1483 * that we can compare pointers rather than doing a strcmp().
1485 lo = &vp->v_vnlock->lock_object;
1486 if (lo->lo_name != tag) {
1488 WITNESS_DESTROY(lo);
1489 WITNESS_INIT(lo, tag);
1492 * By default, don't allow shared locks unless filesystems opt-in.
1494 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE;
1496 * Finalize various vnode identity bits.
1498 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp));
1499 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp));
1500 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp));
1504 v_init_counters(vp);
1505 vp->v_bufobj.bo_ops = &buf_ops_bio;
1507 if (mp == NULL && vops != &dead_vnodeops)
1508 printf("NULL mp in getnewvnode(9), tag %s\n", tag);
1512 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1513 mac_vnode_associate_singlelabel(mp, vp);
1516 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize;
1517 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1518 vp->v_vflag |= VV_NOKNOTE;
1522 * For the filesystems which do not use vfs_hash_insert(),
1523 * still initialize v_hash to have vfs_hash_index() useful.
1524 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1527 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1534 * Delete from old mount point vnode list, if on one.
1537 delmntque(struct vnode *vp)
1547 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1548 ("Active vnode list size %d > Vnode list size %d",
1549 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1550 active = vp->v_iflag & VI_ACTIVE;
1551 vp->v_iflag &= ~VI_ACTIVE;
1553 mtx_lock(&mp->mnt_listmtx);
1554 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1555 mp->mnt_activevnodelistsize--;
1556 mtx_unlock(&mp->mnt_listmtx);
1560 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1561 ("bad mount point vnode list size"));
1562 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1563 mp->mnt_nvnodelistsize--;
1569 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1573 vp->v_op = &dead_vnodeops;
1579 * Insert into list of vnodes for the new mount point, if available.
1582 insmntque1(struct vnode *vp, struct mount *mp,
1583 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1586 KASSERT(vp->v_mount == NULL,
1587 ("insmntque: vnode already on per mount vnode list"));
1588 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1589 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1592 * We acquire the vnode interlock early to ensure that the
1593 * vnode cannot be recycled by another process releasing a
1594 * holdcnt on it before we get it on both the vnode list
1595 * and the active vnode list. The mount mutex protects only
1596 * manipulation of the vnode list and the vnode freelist
1597 * mutex protects only manipulation of the active vnode list.
1598 * Hence the need to hold the vnode interlock throughout.
1602 if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1603 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1604 mp->mnt_nvnodelistsize == 0)) &&
1605 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1614 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1615 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1616 ("neg mount point vnode list size"));
1617 mp->mnt_nvnodelistsize++;
1618 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1619 ("Activating already active vnode"));
1620 vp->v_iflag |= VI_ACTIVE;
1621 mtx_lock(&mp->mnt_listmtx);
1622 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1623 mp->mnt_activevnodelistsize++;
1624 mtx_unlock(&mp->mnt_listmtx);
1631 insmntque(struct vnode *vp, struct mount *mp)
1634 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1638 * Flush out and invalidate all buffers associated with a bufobj
1639 * Called with the underlying object locked.
1642 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1647 if (flags & V_SAVE) {
1648 error = bufobj_wwait(bo, slpflag, slptimeo);
1653 if (bo->bo_dirty.bv_cnt > 0) {
1655 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1658 * XXX We could save a lock/unlock if this was only
1659 * enabled under INVARIANTS
1662 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1663 panic("vinvalbuf: dirty bufs");
1667 * If you alter this loop please notice that interlock is dropped and
1668 * reacquired in flushbuflist. Special care is needed to ensure that
1669 * no race conditions occur from this.
1672 error = flushbuflist(&bo->bo_clean,
1673 flags, bo, slpflag, slptimeo);
1674 if (error == 0 && !(flags & V_CLEANONLY))
1675 error = flushbuflist(&bo->bo_dirty,
1676 flags, bo, slpflag, slptimeo);
1677 if (error != 0 && error != EAGAIN) {
1681 } while (error != 0);
1684 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1685 * have write I/O in-progress but if there is a VM object then the
1686 * VM object can also have read-I/O in-progress.
1689 bufobj_wwait(bo, 0, 0);
1690 if ((flags & V_VMIO) == 0) {
1692 if (bo->bo_object != NULL) {
1693 VM_OBJECT_WLOCK(bo->bo_object);
1694 vm_object_pip_wait(bo->bo_object, "bovlbx");
1695 VM_OBJECT_WUNLOCK(bo->bo_object);
1699 } while (bo->bo_numoutput > 0);
1703 * Destroy the copy in the VM cache, too.
1705 if (bo->bo_object != NULL &&
1706 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) {
1707 VM_OBJECT_WLOCK(bo->bo_object);
1708 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1709 OBJPR_CLEANONLY : 0);
1710 VM_OBJECT_WUNLOCK(bo->bo_object);
1715 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO |
1716 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 ||
1717 bo->bo_clean.bv_cnt > 0))
1718 panic("vinvalbuf: flush failed");
1719 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 &&
1720 bo->bo_dirty.bv_cnt > 0)
1721 panic("vinvalbuf: flush dirty failed");
1728 * Flush out and invalidate all buffers associated with a vnode.
1729 * Called with the underlying object locked.
1732 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1735 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1736 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1737 if (vp->v_object != NULL && vp->v_object->handle != vp)
1739 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1743 * Flush out buffers on the specified list.
1747 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1750 struct buf *bp, *nbp;
1755 ASSERT_BO_WLOCKED(bo);
1758 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1759 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1760 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1764 lblkno = nbp->b_lblkno;
1765 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1768 error = BUF_TIMELOCK(bp,
1769 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1770 "flushbuf", slpflag, slptimeo);
1773 return (error != ENOLCK ? error : EAGAIN);
1775 KASSERT(bp->b_bufobj == bo,
1776 ("bp %p wrong b_bufobj %p should be %p",
1777 bp, bp->b_bufobj, bo));
1779 * XXX Since there are no node locks for NFS, I
1780 * believe there is a slight chance that a delayed
1781 * write will occur while sleeping just above, so
1784 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1787 bp->b_flags |= B_ASYNC;
1790 return (EAGAIN); /* XXX: why not loop ? */
1793 bp->b_flags |= (B_INVAL | B_RELBUF);
1794 bp->b_flags &= ~B_ASYNC;
1799 nbp = gbincore(bo, lblkno);
1800 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1802 break; /* nbp invalid */
1808 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn)
1814 ASSERT_BO_LOCKED(bo);
1816 for (lblkno = startn;;) {
1818 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno);
1819 if (bp == NULL || bp->b_lblkno >= endn ||
1820 bp->b_lblkno < startn)
1822 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1823 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0);
1826 if (error == ENOLCK)
1830 KASSERT(bp->b_bufobj == bo,
1831 ("bp %p wrong b_bufobj %p should be %p",
1832 bp, bp->b_bufobj, bo));
1833 lblkno = bp->b_lblkno + 1;
1834 if ((bp->b_flags & B_MANAGED) == 0)
1836 bp->b_flags |= B_RELBUF;
1838 * In the VMIO case, use the B_NOREUSE flag to hint that the
1839 * pages backing each buffer in the range are unlikely to be
1840 * reused. Dirty buffers will have the hint applied once
1841 * they've been written.
1843 if (bp->b_vp->v_object != NULL)
1844 bp->b_flags |= B_NOREUSE;
1852 * Truncate a file's buffer and pages to a specified length. This
1853 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1857 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
1859 struct buf *bp, *nbp;
1864 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1865 vp, cred, blksize, (uintmax_t)length);
1868 * Round up to the *next* lbn.
1870 trunclbn = howmany(length, blksize);
1872 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1879 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1880 if (bp->b_lblkno < trunclbn)
1883 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1884 BO_LOCKPTR(bo)) == ENOLCK)
1888 bp->b_flags |= (B_INVAL | B_RELBUF);
1889 bp->b_flags &= ~B_ASYNC;
1895 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1896 (nbp->b_vp != vp) ||
1897 (nbp->b_flags & B_DELWRI))) {
1903 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1904 if (bp->b_lblkno < trunclbn)
1907 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1908 BO_LOCKPTR(bo)) == ENOLCK)
1911 bp->b_flags |= (B_INVAL | B_RELBUF);
1912 bp->b_flags &= ~B_ASYNC;
1918 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1919 (nbp->b_vp != vp) ||
1920 (nbp->b_flags & B_DELWRI) == 0)) {
1929 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1930 if (bp->b_lblkno > 0)
1933 * Since we hold the vnode lock this should only
1934 * fail if we're racing with the buf daemon.
1937 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1938 BO_LOCKPTR(bo)) == ENOLCK) {
1941 VNASSERT((bp->b_flags & B_DELWRI), vp,
1942 ("buf(%p) on dirty queue without DELWRI", bp));
1951 bufobj_wwait(bo, 0, 0);
1953 vnode_pager_setsize(vp, length);
1959 buf_vlist_remove(struct buf *bp)
1963 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1964 ASSERT_BO_WLOCKED(bp->b_bufobj);
1965 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1966 (BX_VNDIRTY|BX_VNCLEAN),
1967 ("buf_vlist_remove: Buf %p is on two lists", bp));
1968 if (bp->b_xflags & BX_VNDIRTY)
1969 bv = &bp->b_bufobj->bo_dirty;
1971 bv = &bp->b_bufobj->bo_clean;
1972 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
1973 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1975 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1979 * Add the buffer to the sorted clean or dirty block list.
1981 * NOTE: xflags is passed as a constant, optimizing this inline function!
1984 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1990 ASSERT_BO_WLOCKED(bo);
1991 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
1992 ("dead bo %p", bo));
1993 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1994 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1995 bp->b_xflags |= xflags;
1996 if (xflags & BX_VNDIRTY)
2002 * Keep the list ordered. Optimize empty list insertion. Assume
2003 * we tend to grow at the tail so lookup_le should usually be cheaper
2006 if (bv->bv_cnt == 0 ||
2007 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
2008 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
2009 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
2010 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
2012 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
2013 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
2015 panic("buf_vlist_add: Preallocated nodes insufficient.");
2020 * Look up a buffer using the buffer tries.
2023 gbincore(struct bufobj *bo, daddr_t lblkno)
2027 ASSERT_BO_LOCKED(bo);
2028 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
2031 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
2035 * Associate a buffer with a vnode.
2038 bgetvp(struct vnode *vp, struct buf *bp)
2043 ASSERT_BO_WLOCKED(bo);
2044 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
2046 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
2047 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
2048 ("bgetvp: bp already attached! %p", bp));
2054 * Insert onto list for new vnode.
2056 buf_vlist_add(bp, bo, BX_VNCLEAN);
2060 * Disassociate a buffer from a vnode.
2063 brelvp(struct buf *bp)
2068 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2069 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
2072 * Delete from old vnode list, if on one.
2074 vp = bp->b_vp; /* XXX */
2077 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2078 buf_vlist_remove(bp);
2080 panic("brelvp: Buffer %p not on queue.", bp);
2081 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2082 bo->bo_flag &= ~BO_ONWORKLST;
2083 mtx_lock(&sync_mtx);
2084 LIST_REMOVE(bo, bo_synclist);
2085 syncer_worklist_len--;
2086 mtx_unlock(&sync_mtx);
2089 bp->b_bufobj = NULL;
2095 * Add an item to the syncer work queue.
2098 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
2102 ASSERT_BO_WLOCKED(bo);
2104 mtx_lock(&sync_mtx);
2105 if (bo->bo_flag & BO_ONWORKLST)
2106 LIST_REMOVE(bo, bo_synclist);
2108 bo->bo_flag |= BO_ONWORKLST;
2109 syncer_worklist_len++;
2112 if (delay > syncer_maxdelay - 2)
2113 delay = syncer_maxdelay - 2;
2114 slot = (syncer_delayno + delay) & syncer_mask;
2116 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
2117 mtx_unlock(&sync_mtx);
2121 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
2125 mtx_lock(&sync_mtx);
2126 len = syncer_worklist_len - sync_vnode_count;
2127 mtx_unlock(&sync_mtx);
2128 error = SYSCTL_OUT(req, &len, sizeof(len));
2132 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
2133 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
2135 static struct proc *updateproc;
2136 static void sched_sync(void);
2137 static struct kproc_desc up_kp = {
2142 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
2145 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
2150 *bo = LIST_FIRST(slp);
2154 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
2157 * We use vhold in case the vnode does not
2158 * successfully sync. vhold prevents the vnode from
2159 * going away when we unlock the sync_mtx so that
2160 * we can acquire the vnode interlock.
2163 mtx_unlock(&sync_mtx);
2165 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2167 mtx_lock(&sync_mtx);
2168 return (*bo == LIST_FIRST(slp));
2170 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2171 (void) VOP_FSYNC(vp, MNT_LAZY, td);
2173 vn_finished_write(mp);
2175 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
2177 * Put us back on the worklist. The worklist
2178 * routine will remove us from our current
2179 * position and then add us back in at a later
2182 vn_syncer_add_to_worklist(*bo, syncdelay);
2186 mtx_lock(&sync_mtx);
2190 static int first_printf = 1;
2193 * System filesystem synchronizer daemon.
2198 struct synclist *next, *slp;
2201 struct thread *td = curthread;
2203 int net_worklist_len;
2204 int syncer_final_iter;
2208 syncer_final_iter = 0;
2209 syncer_state = SYNCER_RUNNING;
2210 starttime = time_uptime;
2211 td->td_pflags |= TDP_NORUNNINGBUF;
2213 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
2216 mtx_lock(&sync_mtx);
2218 if (syncer_state == SYNCER_FINAL_DELAY &&
2219 syncer_final_iter == 0) {
2220 mtx_unlock(&sync_mtx);
2221 kproc_suspend_check(td->td_proc);
2222 mtx_lock(&sync_mtx);
2224 net_worklist_len = syncer_worklist_len - sync_vnode_count;
2225 if (syncer_state != SYNCER_RUNNING &&
2226 starttime != time_uptime) {
2228 printf("\nSyncing disks, vnodes remaining... ");
2231 printf("%d ", net_worklist_len);
2233 starttime = time_uptime;
2236 * Push files whose dirty time has expired. Be careful
2237 * of interrupt race on slp queue.
2239 * Skip over empty worklist slots when shutting down.
2242 slp = &syncer_workitem_pending[syncer_delayno];
2243 syncer_delayno += 1;
2244 if (syncer_delayno == syncer_maxdelay)
2246 next = &syncer_workitem_pending[syncer_delayno];
2248 * If the worklist has wrapped since the
2249 * it was emptied of all but syncer vnodes,
2250 * switch to the FINAL_DELAY state and run
2251 * for one more second.
2253 if (syncer_state == SYNCER_SHUTTING_DOWN &&
2254 net_worklist_len == 0 &&
2255 last_work_seen == syncer_delayno) {
2256 syncer_state = SYNCER_FINAL_DELAY;
2257 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
2259 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
2260 syncer_worklist_len > 0);
2263 * Keep track of the last time there was anything
2264 * on the worklist other than syncer vnodes.
2265 * Return to the SHUTTING_DOWN state if any
2268 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
2269 last_work_seen = syncer_delayno;
2270 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
2271 syncer_state = SYNCER_SHUTTING_DOWN;
2272 while (!LIST_EMPTY(slp)) {
2273 error = sync_vnode(slp, &bo, td);
2275 LIST_REMOVE(bo, bo_synclist);
2276 LIST_INSERT_HEAD(next, bo, bo_synclist);
2280 if (first_printf == 0) {
2282 * Drop the sync mutex, because some watchdog
2283 * drivers need to sleep while patting
2285 mtx_unlock(&sync_mtx);
2286 wdog_kern_pat(WD_LASTVAL);
2287 mtx_lock(&sync_mtx);
2291 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
2292 syncer_final_iter--;
2294 * The variable rushjob allows the kernel to speed up the
2295 * processing of the filesystem syncer process. A rushjob
2296 * value of N tells the filesystem syncer to process the next
2297 * N seconds worth of work on its queue ASAP. Currently rushjob
2298 * is used by the soft update code to speed up the filesystem
2299 * syncer process when the incore state is getting so far
2300 * ahead of the disk that the kernel memory pool is being
2301 * threatened with exhaustion.
2308 * Just sleep for a short period of time between
2309 * iterations when shutting down to allow some I/O
2312 * If it has taken us less than a second to process the
2313 * current work, then wait. Otherwise start right over
2314 * again. We can still lose time if any single round
2315 * takes more than two seconds, but it does not really
2316 * matter as we are just trying to generally pace the
2317 * filesystem activity.
2319 if (syncer_state != SYNCER_RUNNING ||
2320 time_uptime == starttime) {
2322 sched_prio(td, PPAUSE);
2325 if (syncer_state != SYNCER_RUNNING)
2326 cv_timedwait(&sync_wakeup, &sync_mtx,
2327 hz / SYNCER_SHUTDOWN_SPEEDUP);
2328 else if (time_uptime == starttime)
2329 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2334 * Request the syncer daemon to speed up its work.
2335 * We never push it to speed up more than half of its
2336 * normal turn time, otherwise it could take over the cpu.
2339 speedup_syncer(void)
2343 mtx_lock(&sync_mtx);
2344 if (rushjob < syncdelay / 2) {
2346 stat_rush_requests += 1;
2349 mtx_unlock(&sync_mtx);
2350 cv_broadcast(&sync_wakeup);
2355 * Tell the syncer to speed up its work and run though its work
2356 * list several times, then tell it to shut down.
2359 syncer_shutdown(void *arg, int howto)
2362 if (howto & RB_NOSYNC)
2364 mtx_lock(&sync_mtx);
2365 syncer_state = SYNCER_SHUTTING_DOWN;
2367 mtx_unlock(&sync_mtx);
2368 cv_broadcast(&sync_wakeup);
2369 kproc_shutdown(arg, howto);
2373 syncer_suspend(void)
2376 syncer_shutdown(updateproc, 0);
2383 mtx_lock(&sync_mtx);
2385 syncer_state = SYNCER_RUNNING;
2386 mtx_unlock(&sync_mtx);
2387 cv_broadcast(&sync_wakeup);
2388 kproc_resume(updateproc);
2392 * Reassign a buffer from one vnode to another.
2393 * Used to assign file specific control information
2394 * (indirect blocks) to the vnode to which they belong.
2397 reassignbuf(struct buf *bp)
2410 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2411 bp, bp->b_vp, bp->b_flags);
2413 * B_PAGING flagged buffers cannot be reassigned because their vp
2414 * is not fully linked in.
2416 if (bp->b_flags & B_PAGING)
2417 panic("cannot reassign paging buffer");
2420 * Delete from old vnode list, if on one.
2423 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2424 buf_vlist_remove(bp);
2426 panic("reassignbuf: Buffer %p not on queue.", bp);
2428 * If dirty, put on list of dirty buffers; otherwise insert onto list
2431 if (bp->b_flags & B_DELWRI) {
2432 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2433 switch (vp->v_type) {
2443 vn_syncer_add_to_worklist(bo, delay);
2445 buf_vlist_add(bp, bo, BX_VNDIRTY);
2447 buf_vlist_add(bp, bo, BX_VNCLEAN);
2449 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2450 mtx_lock(&sync_mtx);
2451 LIST_REMOVE(bo, bo_synclist);
2452 syncer_worklist_len--;
2453 mtx_unlock(&sync_mtx);
2454 bo->bo_flag &= ~BO_ONWORKLST;
2459 bp = TAILQ_FIRST(&bv->bv_hd);
2460 KASSERT(bp == NULL || bp->b_bufobj == bo,
2461 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2462 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2463 KASSERT(bp == NULL || bp->b_bufobj == bo,
2464 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2466 bp = TAILQ_FIRST(&bv->bv_hd);
2467 KASSERT(bp == NULL || bp->b_bufobj == bo,
2468 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2469 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2470 KASSERT(bp == NULL || bp->b_bufobj == bo,
2471 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2477 v_init_counters(struct vnode *vp)
2480 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2481 vp, ("%s called for an initialized vnode", __FUNCTION__));
2482 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2484 refcount_init(&vp->v_holdcnt, 1);
2485 refcount_init(&vp->v_usecount, 1);
2489 v_incr_usecount_locked(struct vnode *vp)
2492 ASSERT_VI_LOCKED(vp, __func__);
2493 if ((vp->v_iflag & VI_OWEINACT) != 0) {
2494 VNASSERT(vp->v_usecount == 0, vp,
2495 ("vnode with usecount and VI_OWEINACT set"));
2496 vp->v_iflag &= ~VI_OWEINACT;
2498 refcount_acquire(&vp->v_usecount);
2499 v_incr_devcount(vp);
2503 * Increment the use count on the vnode, taking care to reference
2504 * the driver's usecount if this is a chardev.
2507 v_incr_usecount(struct vnode *vp)
2510 ASSERT_VI_UNLOCKED(vp, __func__);
2511 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2513 if (vp->v_type != VCHR &&
2514 refcount_acquire_if_not_zero(&vp->v_usecount)) {
2515 VNODE_REFCOUNT_FENCE_ACQ();
2516 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2517 ("vnode with usecount and VI_OWEINACT set"));
2520 v_incr_usecount_locked(vp);
2526 * Increment si_usecount of the associated device, if any.
2529 v_incr_devcount(struct vnode *vp)
2532 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2533 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2535 vp->v_rdev->si_usecount++;
2541 * Decrement si_usecount of the associated device, if any.
2544 v_decr_devcount(struct vnode *vp)
2547 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2548 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2550 vp->v_rdev->si_usecount--;
2556 * Grab a particular vnode from the free list, increment its
2557 * reference count and lock it. VI_DOOMED is set if the vnode
2558 * is being destroyed. Only callers who specify LK_RETRY will
2559 * see doomed vnodes. If inactive processing was delayed in
2560 * vput try to do it here.
2562 * Notes on lockless counter manipulation:
2563 * _vhold, vputx and other routines make various decisions based
2564 * on either holdcnt or usecount being 0. As long as either counter
2565 * is not transitioning 0->1 nor 1->0, the manipulation can be done
2566 * with atomic operations. Otherwise the interlock is taken covering
2567 * both the atomic and additional actions.
2570 vget(struct vnode *vp, int flags, struct thread *td)
2572 int error, oweinact;
2574 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2575 ("vget: invalid lock operation"));
2577 if ((flags & LK_INTERLOCK) != 0)
2578 ASSERT_VI_LOCKED(vp, __func__);
2580 ASSERT_VI_UNLOCKED(vp, __func__);
2581 if ((flags & LK_VNHELD) != 0)
2582 VNASSERT((vp->v_holdcnt > 0), vp,
2583 ("vget: LK_VNHELD passed but vnode not held"));
2585 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2587 if ((flags & LK_VNHELD) == 0)
2588 _vhold(vp, (flags & LK_INTERLOCK) != 0);
2590 if ((error = vn_lock(vp, flags)) != 0) {
2592 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2596 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2597 panic("vget: vn_lock failed to return ENOENT\n");
2599 * We don't guarantee that any particular close will
2600 * trigger inactive processing so just make a best effort
2601 * here at preventing a reference to a removed file. If
2602 * we don't succeed no harm is done.
2604 * Upgrade our holdcnt to a usecount.
2606 if (vp->v_type == VCHR ||
2607 !refcount_acquire_if_not_zero(&vp->v_usecount)) {
2609 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2613 vp->v_iflag &= ~VI_OWEINACT;
2614 VNODE_REFCOUNT_FENCE_REL();
2616 refcount_acquire(&vp->v_usecount);
2617 v_incr_devcount(vp);
2618 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2619 (flags & LK_NOWAIT) == 0)
2627 * Increase the reference (use) and hold count of a vnode.
2628 * This will also remove the vnode from the free list if it is presently free.
2631 vref(struct vnode *vp)
2634 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2636 v_incr_usecount(vp);
2640 vrefl(struct vnode *vp)
2643 ASSERT_VI_LOCKED(vp, __func__);
2644 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2646 v_incr_usecount_locked(vp);
2650 vrefact(struct vnode *vp)
2653 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2654 if (__predict_false(vp->v_type == VCHR)) {
2655 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp,
2656 ("%s: wrong ref counts", __func__));
2661 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
2662 VNASSERT(old > 0, vp, ("%s: wrong hold count", __func__));
2663 old = atomic_fetchadd_int(&vp->v_usecount, 1);
2664 VNASSERT(old > 0, vp, ("%s: wrong use count", __func__));
2666 refcount_acquire(&vp->v_holdcnt);
2667 refcount_acquire(&vp->v_usecount);
2672 * Return reference count of a vnode.
2674 * The results of this call are only guaranteed when some mechanism is used to
2675 * stop other processes from gaining references to the vnode. This may be the
2676 * case if the caller holds the only reference. This is also useful when stale
2677 * data is acceptable as race conditions may be accounted for by some other
2681 vrefcnt(struct vnode *vp)
2684 return (vp->v_usecount);
2687 #define VPUTX_VRELE 1
2688 #define VPUTX_VPUT 2
2689 #define VPUTX_VUNREF 3
2692 * Decrement the use and hold counts for a vnode.
2694 * See an explanation near vget() as to why atomic operation is safe.
2697 vputx(struct vnode *vp, int func)
2701 KASSERT(vp != NULL, ("vputx: null vp"));
2702 if (func == VPUTX_VUNREF)
2703 ASSERT_VOP_LOCKED(vp, "vunref");
2704 else if (func == VPUTX_VPUT)
2705 ASSERT_VOP_LOCKED(vp, "vput");
2707 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2708 ASSERT_VI_UNLOCKED(vp, __func__);
2709 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2711 if (vp->v_type != VCHR &&
2712 refcount_release_if_not_last(&vp->v_usecount)) {
2713 if (func == VPUTX_VPUT)
2722 * We want to hold the vnode until the inactive finishes to
2723 * prevent vgone() races. We drop the use count here and the
2724 * hold count below when we're done.
2726 if (!refcount_release(&vp->v_usecount) ||
2727 (vp->v_iflag & VI_DOINGINACT)) {
2728 if (func == VPUTX_VPUT)
2730 v_decr_devcount(vp);
2735 v_decr_devcount(vp);
2739 if (vp->v_usecount != 0) {
2740 vn_printf(vp, "vputx: usecount not zero for vnode ");
2741 panic("vputx: usecount not zero");
2744 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2747 * We must call VOP_INACTIVE with the node locked. Mark
2748 * as VI_DOINGINACT to avoid recursion.
2750 vp->v_iflag |= VI_OWEINACT;
2753 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2757 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2758 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2764 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2765 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2770 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp,
2771 ("vnode with usecount and VI_OWEINACT set"));
2773 if (vp->v_iflag & VI_OWEINACT)
2774 vinactive(vp, curthread);
2775 if (func != VPUTX_VUNREF)
2782 * Vnode put/release.
2783 * If count drops to zero, call inactive routine and return to freelist.
2786 vrele(struct vnode *vp)
2789 vputx(vp, VPUTX_VRELE);
2793 * Release an already locked vnode. This give the same effects as
2794 * unlock+vrele(), but takes less time and avoids releasing and
2795 * re-aquiring the lock (as vrele() acquires the lock internally.)
2798 vput(struct vnode *vp)
2801 vputx(vp, VPUTX_VPUT);
2805 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2808 vunref(struct vnode *vp)
2811 vputx(vp, VPUTX_VUNREF);
2815 * Increase the hold count and activate if this is the first reference.
2818 _vhold(struct vnode *vp, bool locked)
2823 ASSERT_VI_LOCKED(vp, __func__);
2825 ASSERT_VI_UNLOCKED(vp, __func__);
2826 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2828 if (refcount_acquire_if_not_zero(&vp->v_holdcnt)) {
2829 VNODE_REFCOUNT_FENCE_ACQ();
2830 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2831 ("_vhold: vnode with holdcnt is free"));
2836 if ((vp->v_iflag & VI_FREE) == 0) {
2837 refcount_acquire(&vp->v_holdcnt);
2842 VNASSERT(vp->v_holdcnt == 0, vp,
2843 ("%s: wrong hold count", __func__));
2844 VNASSERT(vp->v_op != NULL, vp,
2845 ("%s: vnode already reclaimed.", __func__));
2847 * Remove a vnode from the free list, mark it as in use,
2848 * and put it on the active list.
2850 VNASSERT(vp->v_mount != NULL, vp,
2851 ("_vhold: vnode not on per mount vnode list"));
2853 mtx_lock(&mp->mnt_listmtx);
2854 if ((vp->v_mflag & VMP_TMPMNTFREELIST) != 0) {
2855 TAILQ_REMOVE(&mp->mnt_tmpfreevnodelist, vp, v_actfreelist);
2856 mp->mnt_tmpfreevnodelistsize--;
2857 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
2859 mtx_lock(&vnode_free_list_mtx);
2860 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2862 mtx_unlock(&vnode_free_list_mtx);
2864 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2865 ("Activating already active vnode"));
2866 vp->v_iflag &= ~VI_FREE;
2867 vp->v_iflag |= VI_ACTIVE;
2868 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2869 mp->mnt_activevnodelistsize++;
2870 mtx_unlock(&mp->mnt_listmtx);
2871 refcount_acquire(&vp->v_holdcnt);
2877 * Drop the hold count of the vnode. If this is the last reference to
2878 * the vnode we place it on the free list unless it has been vgone'd
2879 * (marked VI_DOOMED) in which case we will free it.
2881 * Because the vnode vm object keeps a hold reference on the vnode if
2882 * there is at least one resident non-cached page, the vnode cannot
2883 * leave the active list without the page cleanup done.
2886 _vdrop(struct vnode *vp, bool locked)
2893 ASSERT_VI_LOCKED(vp, __func__);
2895 ASSERT_VI_UNLOCKED(vp, __func__);
2896 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2897 if ((int)vp->v_holdcnt <= 0)
2898 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2900 if (refcount_release_if_not_last(&vp->v_holdcnt))
2904 if (refcount_release(&vp->v_holdcnt) == 0) {
2908 if ((vp->v_iflag & VI_DOOMED) == 0) {
2910 * Mark a vnode as free: remove it from its active list
2911 * and put it up for recycling on the freelist.
2913 VNASSERT(vp->v_op != NULL, vp,
2914 ("vdropl: vnode already reclaimed."));
2915 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2916 ("vnode already free"));
2917 VNASSERT(vp->v_holdcnt == 0, vp,
2918 ("vdropl: freeing when we shouldn't"));
2919 active = vp->v_iflag & VI_ACTIVE;
2920 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2921 vp->v_iflag &= ~VI_ACTIVE;
2924 mtx_lock(&mp->mnt_listmtx);
2926 TAILQ_REMOVE(&mp->mnt_activevnodelist,
2928 mp->mnt_activevnodelistsize--;
2930 TAILQ_INSERT_TAIL(&mp->mnt_tmpfreevnodelist,
2932 mp->mnt_tmpfreevnodelistsize++;
2933 vp->v_iflag |= VI_FREE;
2934 vp->v_mflag |= VMP_TMPMNTFREELIST;
2936 if (mp->mnt_tmpfreevnodelistsize >=
2937 mnt_free_list_batch)
2938 vnlru_return_batch_locked(mp);
2939 mtx_unlock(&mp->mnt_listmtx);
2941 VNASSERT(active == 0, vp,
2942 ("vdropl: active vnode not on per mount "
2944 mtx_lock(&vnode_free_list_mtx);
2945 TAILQ_INSERT_TAIL(&vnode_free_list, vp,
2948 vp->v_iflag |= VI_FREE;
2950 mtx_unlock(&vnode_free_list_mtx);
2954 counter_u64_add(free_owe_inact, 1);
2959 * The vnode has been marked for destruction, so free it.
2961 * The vnode will be returned to the zone where it will
2962 * normally remain until it is needed for another vnode. We
2963 * need to cleanup (or verify that the cleanup has already
2964 * been done) any residual data left from its current use
2965 * so as not to contaminate the freshly allocated vnode.
2967 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2968 atomic_subtract_long(&numvnodes, 1);
2970 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2971 ("cleaned vnode still on the free list."));
2972 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2973 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2974 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2975 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2976 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2977 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2978 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
2979 ("clean blk trie not empty"));
2980 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2981 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
2982 ("dirty blk trie not empty"));
2983 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2984 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2985 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2986 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp,
2987 ("Dangling rangelock waiters"));
2990 mac_vnode_destroy(vp);
2992 if (vp->v_pollinfo != NULL) {
2993 destroy_vpollinfo(vp->v_pollinfo);
2994 vp->v_pollinfo = NULL;
2997 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
3000 vp->v_mountedhere = NULL;
3003 vp->v_fifoinfo = NULL;
3004 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
3008 uma_zfree(vnode_zone, vp);
3012 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
3013 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
3014 * OWEINACT tracks whether a vnode missed a call to inactive due to a
3015 * failed lock upgrade.
3018 vinactive(struct vnode *vp, struct thread *td)
3020 struct vm_object *obj;
3022 ASSERT_VOP_ELOCKED(vp, "vinactive");
3023 ASSERT_VI_LOCKED(vp, "vinactive");
3024 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
3025 ("vinactive: recursed on VI_DOINGINACT"));
3026 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3027 vp->v_iflag |= VI_DOINGINACT;
3028 vp->v_iflag &= ~VI_OWEINACT;
3031 * Before moving off the active list, we must be sure that any
3032 * modified pages are converted into the vnode's dirty
3033 * buffers, since these will no longer be checked once the
3034 * vnode is on the inactive list.
3036 * The write-out of the dirty pages is asynchronous. At the
3037 * point that VOP_INACTIVE() is called, there could still be
3038 * pending I/O and dirty pages in the object.
3040 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
3041 (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
3042 VM_OBJECT_WLOCK(obj);
3043 vm_object_page_clean(obj, 0, 0, 0);
3044 VM_OBJECT_WUNLOCK(obj);
3046 VOP_INACTIVE(vp, td);
3048 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
3049 ("vinactive: lost VI_DOINGINACT"));
3050 vp->v_iflag &= ~VI_DOINGINACT;
3054 * Remove any vnodes in the vnode table belonging to mount point mp.
3056 * If FORCECLOSE is not specified, there should not be any active ones,
3057 * return error if any are found (nb: this is a user error, not a
3058 * system error). If FORCECLOSE is specified, detach any active vnodes
3061 * If WRITECLOSE is set, only flush out regular file vnodes open for
3064 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
3066 * `rootrefs' specifies the base reference count for the root vnode
3067 * of this filesystem. The root vnode is considered busy if its
3068 * v_usecount exceeds this value. On a successful return, vflush(, td)
3069 * will call vrele() on the root vnode exactly rootrefs times.
3070 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
3074 static int busyprt = 0; /* print out busy vnodes */
3075 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
3079 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
3081 struct vnode *vp, *mvp, *rootvp = NULL;
3083 int busy = 0, error;
3085 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
3088 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
3089 ("vflush: bad args"));
3091 * Get the filesystem root vnode. We can vput() it
3092 * immediately, since with rootrefs > 0, it won't go away.
3094 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
3095 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
3102 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
3104 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
3107 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3111 * Skip over a vnodes marked VV_SYSTEM.
3113 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
3119 * If WRITECLOSE is set, flush out unlinked but still open
3120 * files (even if open only for reading) and regular file
3121 * vnodes open for writing.
3123 if (flags & WRITECLOSE) {
3124 if (vp->v_object != NULL) {
3125 VM_OBJECT_WLOCK(vp->v_object);
3126 vm_object_page_clean(vp->v_object, 0, 0, 0);
3127 VM_OBJECT_WUNLOCK(vp->v_object);
3129 error = VOP_FSYNC(vp, MNT_WAIT, td);
3133 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3136 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3139 if ((vp->v_type == VNON ||
3140 (error == 0 && vattr.va_nlink > 0)) &&
3141 (vp->v_writecount == 0 || vp->v_type != VREG)) {
3149 * With v_usecount == 0, all we need to do is clear out the
3150 * vnode data structures and we are done.
3152 * If FORCECLOSE is set, forcibly close the vnode.
3154 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
3160 vn_printf(vp, "vflush: busy vnode ");
3166 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
3168 * If just the root vnode is busy, and if its refcount
3169 * is equal to `rootrefs', then go ahead and kill it.
3172 KASSERT(busy > 0, ("vflush: not busy"));
3173 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
3174 ("vflush: usecount %d < rootrefs %d",
3175 rootvp->v_usecount, rootrefs));
3176 if (busy == 1 && rootvp->v_usecount == rootrefs) {
3177 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
3179 VOP_UNLOCK(rootvp, 0);
3185 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
3189 for (; rootrefs > 0; rootrefs--)
3195 * Recycle an unused vnode to the front of the free list.
3198 vrecycle(struct vnode *vp)
3203 recycled = vrecyclel(vp);
3209 * vrecycle, with the vp interlock held.
3212 vrecyclel(struct vnode *vp)
3216 ASSERT_VOP_ELOCKED(vp, __func__);
3217 ASSERT_VI_LOCKED(vp, __func__);
3218 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3220 if (vp->v_usecount == 0) {
3228 * Eliminate all activity associated with a vnode
3229 * in preparation for reuse.
3232 vgone(struct vnode *vp)
3240 notify_lowervp_vfs_dummy(struct mount *mp __unused,
3241 struct vnode *lowervp __unused)
3246 * Notify upper mounts about reclaimed or unlinked vnode.
3249 vfs_notify_upper(struct vnode *vp, int event)
3251 static struct vfsops vgonel_vfsops = {
3252 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
3253 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
3255 struct mount *mp, *ump, *mmp;
3262 if (TAILQ_EMPTY(&mp->mnt_uppers))
3265 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
3266 mmp->mnt_op = &vgonel_vfsops;
3267 mmp->mnt_kern_flag |= MNTK_MARKER;
3269 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
3270 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
3271 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
3272 ump = TAILQ_NEXT(ump, mnt_upper_link);
3275 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
3278 case VFS_NOTIFY_UPPER_RECLAIM:
3279 VFS_RECLAIM_LOWERVP(ump, vp);
3281 case VFS_NOTIFY_UPPER_UNLINK:
3282 VFS_UNLINK_LOWERVP(ump, vp);
3285 KASSERT(0, ("invalid event %d", event));
3289 ump = TAILQ_NEXT(mmp, mnt_upper_link);
3290 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
3293 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
3294 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
3295 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
3296 wakeup(&mp->mnt_uppers);
3303 * vgone, with the vp interlock held.
3306 vgonel(struct vnode *vp)
3313 ASSERT_VOP_ELOCKED(vp, "vgonel");
3314 ASSERT_VI_LOCKED(vp, "vgonel");
3315 VNASSERT(vp->v_holdcnt, vp,
3316 ("vgonel: vp %p has no reference.", vp));
3317 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3321 * Don't vgonel if we're already doomed.
3323 if (vp->v_iflag & VI_DOOMED)
3325 vp->v_iflag |= VI_DOOMED;
3328 * Check to see if the vnode is in use. If so, we have to call
3329 * VOP_CLOSE() and VOP_INACTIVE().
3331 active = vp->v_usecount;
3332 oweinact = (vp->v_iflag & VI_OWEINACT);
3334 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
3337 * If purging an active vnode, it must be closed and
3338 * deactivated before being reclaimed.
3341 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
3342 if (oweinact || active) {
3344 if ((vp->v_iflag & VI_DOINGINACT) == 0)
3348 if (vp->v_type == VSOCK)
3349 vfs_unp_reclaim(vp);
3352 * Clean out any buffers associated with the vnode.
3353 * If the flush fails, just toss the buffers.
3356 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
3357 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
3358 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
3359 while (vinvalbuf(vp, 0, 0, 0) != 0)
3363 BO_LOCK(&vp->v_bufobj);
3364 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
3365 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3366 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
3367 vp->v_bufobj.bo_clean.bv_cnt == 0,
3368 ("vp %p bufobj not invalidated", vp));
3371 * For VMIO bufobj, BO_DEAD is set in vm_object_terminate()
3372 * after the object's page queue is flushed.
3374 if (vp->v_bufobj.bo_object == NULL)
3375 vp->v_bufobj.bo_flag |= BO_DEAD;
3376 BO_UNLOCK(&vp->v_bufobj);
3379 * Reclaim the vnode.
3381 if (VOP_RECLAIM(vp, td))
3382 panic("vgone: cannot reclaim");
3384 vn_finished_secondary_write(mp);
3385 VNASSERT(vp->v_object == NULL, vp,
3386 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
3388 * Clear the advisory locks and wake up waiting threads.
3390 (void)VOP_ADVLOCKPURGE(vp);
3393 * Delete from old mount point vnode list.
3398 * Done with purge, reset to the standard lock and invalidate
3402 vp->v_vnlock = &vp->v_lock;
3403 vp->v_op = &dead_vnodeops;
3409 * Calculate the total number of references to a special device.
3412 vcount(struct vnode *vp)
3417 count = vp->v_rdev->si_usecount;
3423 * Same as above, but using the struct cdev *as argument
3426 count_dev(struct cdev *dev)
3431 count = dev->si_usecount;
3437 * Print out a description of a vnode.
3439 static char *typename[] =
3440 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
3444 vn_printf(struct vnode *vp, const char *fmt, ...)
3447 char buf[256], buf2[16];
3453 printf("%p: ", (void *)vp);
3454 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
3455 printf(" usecount %d, writecount %d, refcount %d",
3456 vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
3457 switch (vp->v_type) {
3459 printf(" mountedhere %p\n", vp->v_mountedhere);
3462 printf(" rdev %p\n", vp->v_rdev);
3465 printf(" socket %p\n", vp->v_unpcb);
3468 printf(" fifoinfo %p\n", vp->v_fifoinfo);
3476 if (vp->v_vflag & VV_ROOT)
3477 strlcat(buf, "|VV_ROOT", sizeof(buf));
3478 if (vp->v_vflag & VV_ISTTY)
3479 strlcat(buf, "|VV_ISTTY", sizeof(buf));
3480 if (vp->v_vflag & VV_NOSYNC)
3481 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
3482 if (vp->v_vflag & VV_ETERNALDEV)
3483 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
3484 if (vp->v_vflag & VV_CACHEDLABEL)
3485 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
3486 if (vp->v_vflag & VV_TEXT)
3487 strlcat(buf, "|VV_TEXT", sizeof(buf));
3488 if (vp->v_vflag & VV_COPYONWRITE)
3489 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
3490 if (vp->v_vflag & VV_SYSTEM)
3491 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
3492 if (vp->v_vflag & VV_PROCDEP)
3493 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
3494 if (vp->v_vflag & VV_NOKNOTE)
3495 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
3496 if (vp->v_vflag & VV_DELETED)
3497 strlcat(buf, "|VV_DELETED", sizeof(buf));
3498 if (vp->v_vflag & VV_MD)
3499 strlcat(buf, "|VV_MD", sizeof(buf));
3500 if (vp->v_vflag & VV_FORCEINSMQ)
3501 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
3502 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
3503 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
3504 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
3506 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
3507 strlcat(buf, buf2, sizeof(buf));
3509 if (vp->v_iflag & VI_MOUNT)
3510 strlcat(buf, "|VI_MOUNT", sizeof(buf));
3511 if (vp->v_iflag & VI_DOOMED)
3512 strlcat(buf, "|VI_DOOMED", sizeof(buf));
3513 if (vp->v_iflag & VI_FREE)
3514 strlcat(buf, "|VI_FREE", sizeof(buf));
3515 if (vp->v_iflag & VI_ACTIVE)
3516 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3517 if (vp->v_iflag & VI_DOINGINACT)
3518 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3519 if (vp->v_iflag & VI_OWEINACT)
3520 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3521 flags = vp->v_iflag & ~(VI_MOUNT | VI_DOOMED | VI_FREE |
3522 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
3524 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3525 strlcat(buf, buf2, sizeof(buf));
3527 printf(" flags (%s)\n", buf + 1);
3528 if (mtx_owned(VI_MTX(vp)))
3529 printf(" VI_LOCKed");
3530 if (vp->v_object != NULL)
3531 printf(" v_object %p ref %d pages %d "
3532 "cleanbuf %d dirtybuf %d\n",
3533 vp->v_object, vp->v_object->ref_count,
3534 vp->v_object->resident_page_count,
3535 vp->v_bufobj.bo_clean.bv_cnt,
3536 vp->v_bufobj.bo_dirty.bv_cnt);
3538 lockmgr_printinfo(vp->v_vnlock);
3539 if (vp->v_data != NULL)
3545 * List all of the locked vnodes in the system.
3546 * Called when debugging the kernel.
3548 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3554 * Note: because this is DDB, we can't obey the locking semantics
3555 * for these structures, which means we could catch an inconsistent
3556 * state and dereference a nasty pointer. Not much to be done
3559 db_printf("Locked vnodes\n");
3560 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3561 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3562 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3563 vn_printf(vp, "vnode ");
3569 * Show details about the given vnode.
3571 DB_SHOW_COMMAND(vnode, db_show_vnode)
3577 vp = (struct vnode *)addr;
3578 vn_printf(vp, "vnode ");
3582 * Show details about the given mount point.
3584 DB_SHOW_COMMAND(mount, db_show_mount)
3595 /* No address given, print short info about all mount points. */
3596 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3597 db_printf("%p %s on %s (%s)\n", mp,
3598 mp->mnt_stat.f_mntfromname,
3599 mp->mnt_stat.f_mntonname,
3600 mp->mnt_stat.f_fstypename);
3604 db_printf("\nMore info: show mount <addr>\n");
3608 mp = (struct mount *)addr;
3609 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3610 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3613 mflags = mp->mnt_flag;
3614 #define MNT_FLAG(flag) do { \
3615 if (mflags & (flag)) { \
3616 if (buf[0] != '\0') \
3617 strlcat(buf, ", ", sizeof(buf)); \
3618 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3619 mflags &= ~(flag); \
3622 MNT_FLAG(MNT_RDONLY);
3623 MNT_FLAG(MNT_SYNCHRONOUS);
3624 MNT_FLAG(MNT_NOEXEC);
3625 MNT_FLAG(MNT_NOSUID);
3626 MNT_FLAG(MNT_NFS4ACLS);
3627 MNT_FLAG(MNT_UNION);
3628 MNT_FLAG(MNT_ASYNC);
3629 MNT_FLAG(MNT_SUIDDIR);
3630 MNT_FLAG(MNT_SOFTDEP);
3631 MNT_FLAG(MNT_NOSYMFOLLOW);
3632 MNT_FLAG(MNT_GJOURNAL);
3633 MNT_FLAG(MNT_MULTILABEL);
3635 MNT_FLAG(MNT_NOATIME);
3636 MNT_FLAG(MNT_NOCLUSTERR);
3637 MNT_FLAG(MNT_NOCLUSTERW);
3639 MNT_FLAG(MNT_EXRDONLY);
3640 MNT_FLAG(MNT_EXPORTED);
3641 MNT_FLAG(MNT_DEFEXPORTED);
3642 MNT_FLAG(MNT_EXPORTANON);
3643 MNT_FLAG(MNT_EXKERB);
3644 MNT_FLAG(MNT_EXPUBLIC);
3645 MNT_FLAG(MNT_LOCAL);
3646 MNT_FLAG(MNT_QUOTA);
3647 MNT_FLAG(MNT_ROOTFS);
3649 MNT_FLAG(MNT_IGNORE);
3650 MNT_FLAG(MNT_UPDATE);
3651 MNT_FLAG(MNT_DELEXPORT);
3652 MNT_FLAG(MNT_RELOAD);
3653 MNT_FLAG(MNT_FORCE);
3654 MNT_FLAG(MNT_SNAPSHOT);
3655 MNT_FLAG(MNT_BYFSID);
3659 strlcat(buf, ", ", sizeof(buf));
3660 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3661 "0x%016jx", mflags);
3663 db_printf(" mnt_flag = %s\n", buf);
3666 flags = mp->mnt_kern_flag;
3667 #define MNT_KERN_FLAG(flag) do { \
3668 if (flags & (flag)) { \
3669 if (buf[0] != '\0') \
3670 strlcat(buf, ", ", sizeof(buf)); \
3671 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3675 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3676 MNT_KERN_FLAG(MNTK_ASYNC);
3677 MNT_KERN_FLAG(MNTK_SOFTDEP);
3678 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3679 MNT_KERN_FLAG(MNTK_DRAINING);
3680 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3681 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3682 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3683 MNT_KERN_FLAG(MNTK_NO_IOPF);
3684 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3685 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3686 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3687 MNT_KERN_FLAG(MNTK_MARKER);
3688 MNT_KERN_FLAG(MNTK_USES_BCACHE);
3689 MNT_KERN_FLAG(MNTK_NOASYNC);
3690 MNT_KERN_FLAG(MNTK_UNMOUNT);
3691 MNT_KERN_FLAG(MNTK_MWAIT);
3692 MNT_KERN_FLAG(MNTK_SUSPEND);
3693 MNT_KERN_FLAG(MNTK_SUSPEND2);
3694 MNT_KERN_FLAG(MNTK_SUSPENDED);
3695 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3696 MNT_KERN_FLAG(MNTK_NOKNOTE);
3697 #undef MNT_KERN_FLAG
3700 strlcat(buf, ", ", sizeof(buf));
3701 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3704 db_printf(" mnt_kern_flag = %s\n", buf);
3706 db_printf(" mnt_opt = ");
3707 opt = TAILQ_FIRST(mp->mnt_opt);
3709 db_printf("%s", opt->name);
3710 opt = TAILQ_NEXT(opt, link);
3711 while (opt != NULL) {
3712 db_printf(", %s", opt->name);
3713 opt = TAILQ_NEXT(opt, link);
3719 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3720 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3721 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3722 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3723 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3724 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3725 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3726 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3727 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3728 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3729 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3730 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3732 db_printf(" mnt_cred = { uid=%u ruid=%u",
3733 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3734 if (jailed(mp->mnt_cred))
3735 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3737 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3738 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3739 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3740 db_printf(" mnt_activevnodelistsize = %d\n",
3741 mp->mnt_activevnodelistsize);
3742 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3743 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3744 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3745 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3746 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3747 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3748 db_printf(" mnt_secondary_accwrites = %d\n",
3749 mp->mnt_secondary_accwrites);
3750 db_printf(" mnt_gjprovider = %s\n",
3751 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3753 db_printf("\n\nList of active vnodes\n");
3754 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3755 if (vp->v_type != VMARKER) {
3756 vn_printf(vp, "vnode ");
3761 db_printf("\n\nList of inactive vnodes\n");
3762 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3763 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3764 vn_printf(vp, "vnode ");
3773 * Fill in a struct xvfsconf based on a struct vfsconf.
3776 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3778 struct xvfsconf xvfsp;
3780 bzero(&xvfsp, sizeof(xvfsp));
3781 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3782 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3783 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3784 xvfsp.vfc_flags = vfsp->vfc_flags;
3786 * These are unused in userland, we keep them
3787 * to not break binary compatibility.
3789 xvfsp.vfc_vfsops = NULL;
3790 xvfsp.vfc_next = NULL;
3791 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3794 #ifdef COMPAT_FREEBSD32
3796 uint32_t vfc_vfsops;
3797 char vfc_name[MFSNAMELEN];
3798 int32_t vfc_typenum;
3799 int32_t vfc_refcount;
3805 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3807 struct xvfsconf32 xvfsp;
3809 bzero(&xvfsp, sizeof(xvfsp));
3810 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3811 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3812 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3813 xvfsp.vfc_flags = vfsp->vfc_flags;
3814 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3819 * Top level filesystem related information gathering.
3822 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3824 struct vfsconf *vfsp;
3829 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3830 #ifdef COMPAT_FREEBSD32
3831 if (req->flags & SCTL_MASK32)
3832 error = vfsconf2x32(req, vfsp);
3835 error = vfsconf2x(req, vfsp);
3843 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3844 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3845 "S,xvfsconf", "List of all configured filesystems");
3847 #ifndef BURN_BRIDGES
3848 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3851 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3853 int *name = (int *)arg1 - 1; /* XXX */
3854 u_int namelen = arg2 + 1; /* XXX */
3855 struct vfsconf *vfsp;
3857 log(LOG_WARNING, "userland calling deprecated sysctl, "
3858 "please rebuild world\n");
3860 #if 1 || defined(COMPAT_PRELITE2)
3861 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3863 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3867 case VFS_MAXTYPENUM:
3870 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3873 return (ENOTDIR); /* overloaded */
3875 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3876 if (vfsp->vfc_typenum == name[2])
3881 return (EOPNOTSUPP);
3882 #ifdef COMPAT_FREEBSD32
3883 if (req->flags & SCTL_MASK32)
3884 return (vfsconf2x32(req, vfsp));
3887 return (vfsconf2x(req, vfsp));
3889 return (EOPNOTSUPP);
3892 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
3893 CTLFLAG_MPSAFE, vfs_sysctl,
3894 "Generic filesystem");
3896 #if 1 || defined(COMPAT_PRELITE2)
3899 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3902 struct vfsconf *vfsp;
3903 struct ovfsconf ovfs;
3906 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3907 bzero(&ovfs, sizeof(ovfs));
3908 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3909 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3910 ovfs.vfc_index = vfsp->vfc_typenum;
3911 ovfs.vfc_refcount = vfsp->vfc_refcount;
3912 ovfs.vfc_flags = vfsp->vfc_flags;
3913 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3923 #endif /* 1 || COMPAT_PRELITE2 */
3924 #endif /* !BURN_BRIDGES */
3926 #define KINFO_VNODESLOP 10
3929 * Dump vnode list (via sysctl).
3933 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3941 * Stale numvnodes access is not fatal here.
3944 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3946 /* Make an estimate */
3947 return (SYSCTL_OUT(req, 0, len));
3949 error = sysctl_wire_old_buffer(req, 0);
3952 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3954 mtx_lock(&mountlist_mtx);
3955 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3956 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3959 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3963 xvn[n].xv_size = sizeof *xvn;
3964 xvn[n].xv_vnode = vp;
3965 xvn[n].xv_id = 0; /* XXX compat */
3966 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3968 XV_COPY(writecount);
3974 xvn[n].xv_flag = vp->v_vflag;
3976 switch (vp->v_type) {
3983 if (vp->v_rdev == NULL) {
3987 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3990 xvn[n].xv_socket = vp->v_socket;
3993 xvn[n].xv_fifo = vp->v_fifoinfo;
3998 /* shouldn't happen? */
4006 mtx_lock(&mountlist_mtx);
4011 mtx_unlock(&mountlist_mtx);
4013 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
4018 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
4019 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
4024 unmount_or_warn(struct mount *mp)
4028 error = dounmount(mp, MNT_FORCE, curthread);
4030 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
4034 printf("%d)\n", error);
4039 * Unmount all filesystems. The list is traversed in reverse order
4040 * of mounting to avoid dependencies.
4043 vfs_unmountall(void)
4045 struct mount *mp, *tmp;
4047 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
4050 * Since this only runs when rebooting, it is not interlocked.
4052 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
4056 * Forcibly unmounting "/dev" before "/" would prevent clean
4057 * unmount of the latter.
4059 if (mp == rootdevmp)
4062 unmount_or_warn(mp);
4065 if (rootdevmp != NULL)
4066 unmount_or_warn(rootdevmp);
4070 * perform msync on all vnodes under a mount point
4071 * the mount point must be locked.
4074 vfs_msync(struct mount *mp, int flags)
4076 struct vnode *vp, *mvp;
4077 struct vm_object *obj;
4079 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
4081 vnlru_return_batch(mp);
4083 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
4085 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
4086 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
4088 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
4090 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
4097 VM_OBJECT_WLOCK(obj);
4098 vm_object_page_clean(obj, 0, 0,
4100 OBJPC_SYNC : OBJPC_NOSYNC);
4101 VM_OBJECT_WUNLOCK(obj);
4111 destroy_vpollinfo_free(struct vpollinfo *vi)
4114 knlist_destroy(&vi->vpi_selinfo.si_note);
4115 mtx_destroy(&vi->vpi_lock);
4116 uma_zfree(vnodepoll_zone, vi);
4120 destroy_vpollinfo(struct vpollinfo *vi)
4123 knlist_clear(&vi->vpi_selinfo.si_note, 1);
4124 seldrain(&vi->vpi_selinfo);
4125 destroy_vpollinfo_free(vi);
4129 * Initialize per-vnode helper structure to hold poll-related state.
4132 v_addpollinfo(struct vnode *vp)
4134 struct vpollinfo *vi;
4136 if (vp->v_pollinfo != NULL)
4138 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
4139 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
4140 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
4141 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
4143 if (vp->v_pollinfo != NULL) {
4145 destroy_vpollinfo_free(vi);
4148 vp->v_pollinfo = vi;
4153 * Record a process's interest in events which might happen to
4154 * a vnode. Because poll uses the historic select-style interface
4155 * internally, this routine serves as both the ``check for any
4156 * pending events'' and the ``record my interest in future events''
4157 * functions. (These are done together, while the lock is held,
4158 * to avoid race conditions.)
4161 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
4165 mtx_lock(&vp->v_pollinfo->vpi_lock);
4166 if (vp->v_pollinfo->vpi_revents & events) {
4168 * This leaves events we are not interested
4169 * in available for the other process which
4170 * which presumably had requested them
4171 * (otherwise they would never have been
4174 events &= vp->v_pollinfo->vpi_revents;
4175 vp->v_pollinfo->vpi_revents &= ~events;
4177 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4180 vp->v_pollinfo->vpi_events |= events;
4181 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
4182 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4187 * Routine to create and manage a filesystem syncer vnode.
4189 #define sync_close ((int (*)(struct vop_close_args *))nullop)
4190 static int sync_fsync(struct vop_fsync_args *);
4191 static int sync_inactive(struct vop_inactive_args *);
4192 static int sync_reclaim(struct vop_reclaim_args *);
4194 static struct vop_vector sync_vnodeops = {
4195 .vop_bypass = VOP_EOPNOTSUPP,
4196 .vop_close = sync_close, /* close */
4197 .vop_fsync = sync_fsync, /* fsync */
4198 .vop_inactive = sync_inactive, /* inactive */
4199 .vop_reclaim = sync_reclaim, /* reclaim */
4200 .vop_lock1 = vop_stdlock, /* lock */
4201 .vop_unlock = vop_stdunlock, /* unlock */
4202 .vop_islocked = vop_stdislocked, /* islocked */
4206 * Create a new filesystem syncer vnode for the specified mount point.
4209 vfs_allocate_syncvnode(struct mount *mp)
4213 static long start, incr, next;
4216 /* Allocate a new vnode */
4217 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
4219 panic("vfs_allocate_syncvnode: getnewvnode() failed");
4221 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4222 vp->v_vflag |= VV_FORCEINSMQ;
4223 error = insmntque(vp, mp);
4225 panic("vfs_allocate_syncvnode: insmntque() failed");
4226 vp->v_vflag &= ~VV_FORCEINSMQ;
4229 * Place the vnode onto the syncer worklist. We attempt to
4230 * scatter them about on the list so that they will go off
4231 * at evenly distributed times even if all the filesystems
4232 * are mounted at once.
4235 if (next == 0 || next > syncer_maxdelay) {
4239 start = syncer_maxdelay / 2;
4240 incr = syncer_maxdelay;
4246 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
4247 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
4248 mtx_lock(&sync_mtx);
4250 if (mp->mnt_syncer == NULL) {
4251 mp->mnt_syncer = vp;
4254 mtx_unlock(&sync_mtx);
4257 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4264 vfs_deallocate_syncvnode(struct mount *mp)
4268 mtx_lock(&sync_mtx);
4269 vp = mp->mnt_syncer;
4271 mp->mnt_syncer = NULL;
4272 mtx_unlock(&sync_mtx);
4278 * Do a lazy sync of the filesystem.
4281 sync_fsync(struct vop_fsync_args *ap)
4283 struct vnode *syncvp = ap->a_vp;
4284 struct mount *mp = syncvp->v_mount;
4289 * We only need to do something if this is a lazy evaluation.
4291 if (ap->a_waitfor != MNT_LAZY)
4295 * Move ourselves to the back of the sync list.
4297 bo = &syncvp->v_bufobj;
4299 vn_syncer_add_to_worklist(bo, syncdelay);
4303 * Walk the list of vnodes pushing all that are dirty and
4304 * not already on the sync list.
4306 if (vfs_busy(mp, MBF_NOWAIT) != 0)
4308 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
4312 save = curthread_pflags_set(TDP_SYNCIO);
4313 vfs_msync(mp, MNT_NOWAIT);
4314 error = VFS_SYNC(mp, MNT_LAZY);
4315 curthread_pflags_restore(save);
4316 vn_finished_write(mp);
4322 * The syncer vnode is no referenced.
4325 sync_inactive(struct vop_inactive_args *ap)
4333 * The syncer vnode is no longer needed and is being decommissioned.
4335 * Modifications to the worklist must be protected by sync_mtx.
4338 sync_reclaim(struct vop_reclaim_args *ap)
4340 struct vnode *vp = ap->a_vp;
4345 mtx_lock(&sync_mtx);
4346 if (vp->v_mount->mnt_syncer == vp)
4347 vp->v_mount->mnt_syncer = NULL;
4348 if (bo->bo_flag & BO_ONWORKLST) {
4349 LIST_REMOVE(bo, bo_synclist);
4350 syncer_worklist_len--;
4352 bo->bo_flag &= ~BO_ONWORKLST;
4354 mtx_unlock(&sync_mtx);
4361 * Check if vnode represents a disk device
4364 vn_isdisk(struct vnode *vp, int *errp)
4368 if (vp->v_type != VCHR) {
4374 if (vp->v_rdev == NULL)
4376 else if (vp->v_rdev->si_devsw == NULL)
4378 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
4384 return (error == 0);
4388 * Common filesystem object access control check routine. Accepts a
4389 * vnode's type, "mode", uid and gid, requested access mode, credentials,
4390 * and optional call-by-reference privused argument allowing vaccess()
4391 * to indicate to the caller whether privilege was used to satisfy the
4392 * request (obsoleted). Returns 0 on success, or an errno on failure.
4395 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
4396 accmode_t accmode, struct ucred *cred, int *privused)
4398 accmode_t dac_granted;
4399 accmode_t priv_granted;
4401 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
4402 ("invalid bit in accmode"));
4403 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
4404 ("VAPPEND without VWRITE"));
4407 * Look for a normal, non-privileged way to access the file/directory
4408 * as requested. If it exists, go with that.
4411 if (privused != NULL)
4416 /* Check the owner. */
4417 if (cred->cr_uid == file_uid) {
4418 dac_granted |= VADMIN;
4419 if (file_mode & S_IXUSR)
4420 dac_granted |= VEXEC;
4421 if (file_mode & S_IRUSR)
4422 dac_granted |= VREAD;
4423 if (file_mode & S_IWUSR)
4424 dac_granted |= (VWRITE | VAPPEND);
4426 if ((accmode & dac_granted) == accmode)
4432 /* Otherwise, check the groups (first match) */
4433 if (groupmember(file_gid, cred)) {
4434 if (file_mode & S_IXGRP)
4435 dac_granted |= VEXEC;
4436 if (file_mode & S_IRGRP)
4437 dac_granted |= VREAD;
4438 if (file_mode & S_IWGRP)
4439 dac_granted |= (VWRITE | VAPPEND);
4441 if ((accmode & dac_granted) == accmode)
4447 /* Otherwise, check everyone else. */
4448 if (file_mode & S_IXOTH)
4449 dac_granted |= VEXEC;
4450 if (file_mode & S_IROTH)
4451 dac_granted |= VREAD;
4452 if (file_mode & S_IWOTH)
4453 dac_granted |= (VWRITE | VAPPEND);
4454 if ((accmode & dac_granted) == accmode)
4459 * Build a privilege mask to determine if the set of privileges
4460 * satisfies the requirements when combined with the granted mask
4461 * from above. For each privilege, if the privilege is required,
4462 * bitwise or the request type onto the priv_granted mask.
4468 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
4469 * requests, instead of PRIV_VFS_EXEC.
4471 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4472 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
4473 priv_granted |= VEXEC;
4476 * Ensure that at least one execute bit is on. Otherwise,
4477 * a privileged user will always succeed, and we don't want
4478 * this to happen unless the file really is executable.
4480 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4481 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
4482 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
4483 priv_granted |= VEXEC;
4486 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
4487 !priv_check_cred(cred, PRIV_VFS_READ, 0))
4488 priv_granted |= VREAD;
4490 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
4491 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
4492 priv_granted |= (VWRITE | VAPPEND);
4494 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
4495 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
4496 priv_granted |= VADMIN;
4498 if ((accmode & (priv_granted | dac_granted)) == accmode) {
4499 /* XXX audit: privilege used */
4500 if (privused != NULL)
4505 return ((accmode & VADMIN) ? EPERM : EACCES);
4509 * Credential check based on process requesting service, and per-attribute
4513 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
4514 struct thread *td, accmode_t accmode)
4518 * Kernel-invoked always succeeds.
4524 * Do not allow privileged processes in jail to directly manipulate
4525 * system attributes.
4527 switch (attrnamespace) {
4528 case EXTATTR_NAMESPACE_SYSTEM:
4529 /* Potentially should be: return (EPERM); */
4530 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
4531 case EXTATTR_NAMESPACE_USER:
4532 return (VOP_ACCESS(vp, accmode, cred, td));
4538 #ifdef DEBUG_VFS_LOCKS
4540 * This only exists to suppress warnings from unlocked specfs accesses. It is
4541 * no longer ok to have an unlocked VFS.
4543 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4544 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4546 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4547 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4548 "Drop into debugger on lock violation");
4550 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4551 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4552 0, "Check for interlock across VOPs");
4554 int vfs_badlock_print = 1; /* Print lock violations. */
4555 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4556 0, "Print lock violations");
4558 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */
4559 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode,
4560 0, "Print vnode details on lock violations");
4563 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4564 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4565 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4569 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4573 if (vfs_badlock_backtrace)
4576 if (vfs_badlock_vnode)
4577 vn_printf(vp, "vnode ");
4578 if (vfs_badlock_print)
4579 printf("%s: %p %s\n", str, (void *)vp, msg);
4580 if (vfs_badlock_ddb)
4581 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4585 assert_vi_locked(struct vnode *vp, const char *str)
4588 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4589 vfs_badlock("interlock is not locked but should be", str, vp);
4593 assert_vi_unlocked(struct vnode *vp, const char *str)
4596 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4597 vfs_badlock("interlock is locked but should not be", str, vp);
4601 assert_vop_locked(struct vnode *vp, const char *str)
4605 if (!IGNORE_LOCK(vp)) {
4606 locked = VOP_ISLOCKED(vp);
4607 if (locked == 0 || locked == LK_EXCLOTHER)
4608 vfs_badlock("is not locked but should be", str, vp);
4613 assert_vop_unlocked(struct vnode *vp, const char *str)
4616 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4617 vfs_badlock("is locked but should not be", str, vp);
4621 assert_vop_elocked(struct vnode *vp, const char *str)
4624 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4625 vfs_badlock("is not exclusive locked but should be", str, vp);
4627 #endif /* DEBUG_VFS_LOCKS */
4630 vop_rename_fail(struct vop_rename_args *ap)
4633 if (ap->a_tvp != NULL)
4635 if (ap->a_tdvp == ap->a_tvp)
4644 vop_rename_pre(void *ap)
4646 struct vop_rename_args *a = ap;
4648 #ifdef DEBUG_VFS_LOCKS
4650 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4651 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4652 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4653 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4655 /* Check the source (from). */
4656 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4657 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4658 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4659 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4660 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4662 /* Check the target. */
4664 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4665 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4667 if (a->a_tdvp != a->a_fdvp)
4669 if (a->a_tvp != a->a_fvp)
4676 #ifdef DEBUG_VFS_LOCKS
4678 vop_strategy_pre(void *ap)
4680 struct vop_strategy_args *a;
4687 * Cluster ops lock their component buffers but not the IO container.
4689 if ((bp->b_flags & B_CLUSTER) != 0)
4692 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4693 if (vfs_badlock_print)
4695 "VOP_STRATEGY: bp is not locked but should be\n");
4696 if (vfs_badlock_ddb)
4697 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4702 vop_lock_pre(void *ap)
4704 struct vop_lock1_args *a = ap;
4706 if ((a->a_flags & LK_INTERLOCK) == 0)
4707 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4709 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4713 vop_lock_post(void *ap, int rc)
4715 struct vop_lock1_args *a = ap;
4717 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4718 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4719 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4723 vop_unlock_pre(void *ap)
4725 struct vop_unlock_args *a = ap;
4727 if (a->a_flags & LK_INTERLOCK)
4728 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4729 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4733 vop_unlock_post(void *ap, int rc)
4735 struct vop_unlock_args *a = ap;
4737 if (a->a_flags & LK_INTERLOCK)
4738 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4743 vop_create_post(void *ap, int rc)
4745 struct vop_create_args *a = ap;
4748 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4752 vop_deleteextattr_post(void *ap, int rc)
4754 struct vop_deleteextattr_args *a = ap;
4757 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4761 vop_link_post(void *ap, int rc)
4763 struct vop_link_args *a = ap;
4766 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4767 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4772 vop_mkdir_post(void *ap, int rc)
4774 struct vop_mkdir_args *a = ap;
4777 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4781 vop_mknod_post(void *ap, int rc)
4783 struct vop_mknod_args *a = ap;
4786 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4790 vop_reclaim_post(void *ap, int rc)
4792 struct vop_reclaim_args *a = ap;
4795 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE);
4799 vop_remove_post(void *ap, int rc)
4801 struct vop_remove_args *a = ap;
4804 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4805 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4810 vop_rename_post(void *ap, int rc)
4812 struct vop_rename_args *a = ap;
4817 if (a->a_fdvp == a->a_tdvp) {
4818 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR)
4820 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
4821 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
4823 hint |= NOTE_EXTEND;
4824 if (a->a_fvp->v_type == VDIR)
4826 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
4828 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL &&
4829 a->a_tvp->v_type == VDIR)
4831 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
4834 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4836 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4838 if (a->a_tdvp != a->a_fdvp)
4840 if (a->a_tvp != a->a_fvp)
4848 vop_rmdir_post(void *ap, int rc)
4850 struct vop_rmdir_args *a = ap;
4853 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4854 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4859 vop_setattr_post(void *ap, int rc)
4861 struct vop_setattr_args *a = ap;
4864 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4868 vop_setextattr_post(void *ap, int rc)
4870 struct vop_setextattr_args *a = ap;
4873 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4877 vop_symlink_post(void *ap, int rc)
4879 struct vop_symlink_args *a = ap;
4882 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4886 vop_open_post(void *ap, int rc)
4888 struct vop_open_args *a = ap;
4891 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN);
4895 vop_close_post(void *ap, int rc)
4897 struct vop_close_args *a = ap;
4899 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */
4900 (a->a_vp->v_iflag & VI_DOOMED) == 0)) {
4901 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ?
4902 NOTE_CLOSE_WRITE : NOTE_CLOSE);
4907 vop_read_post(void *ap, int rc)
4909 struct vop_read_args *a = ap;
4912 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
4916 vop_readdir_post(void *ap, int rc)
4918 struct vop_readdir_args *a = ap;
4921 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
4924 static struct knlist fs_knlist;
4927 vfs_event_init(void *arg)
4929 knlist_init_mtx(&fs_knlist, NULL);
4931 /* XXX - correct order? */
4932 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4935 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4938 KNOTE_UNLOCKED(&fs_knlist, event);
4941 static int filt_fsattach(struct knote *kn);
4942 static void filt_fsdetach(struct knote *kn);
4943 static int filt_fsevent(struct knote *kn, long hint);
4945 struct filterops fs_filtops = {
4947 .f_attach = filt_fsattach,
4948 .f_detach = filt_fsdetach,
4949 .f_event = filt_fsevent
4953 filt_fsattach(struct knote *kn)
4956 kn->kn_flags |= EV_CLEAR;
4957 knlist_add(&fs_knlist, kn, 0);
4962 filt_fsdetach(struct knote *kn)
4965 knlist_remove(&fs_knlist, kn, 0);
4969 filt_fsevent(struct knote *kn, long hint)
4972 kn->kn_fflags |= hint;
4973 return (kn->kn_fflags != 0);
4977 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4983 error = SYSCTL_IN(req, &vc, sizeof(vc));
4986 if (vc.vc_vers != VFS_CTL_VERS1)
4988 mp = vfs_getvfs(&vc.vc_fsid);
4991 /* ensure that a specific sysctl goes to the right filesystem. */
4992 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4993 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4997 VCTLTOREQ(&vc, req);
4998 error = VFS_SYSCTL(mp, vc.vc_op, req);
5003 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
5004 NULL, 0, sysctl_vfs_ctl, "",
5008 * Function to initialize a va_filerev field sensibly.
5009 * XXX: Wouldn't a random number make a lot more sense ??
5012 init_va_filerev(void)
5017 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
5020 static int filt_vfsread(struct knote *kn, long hint);
5021 static int filt_vfswrite(struct knote *kn, long hint);
5022 static int filt_vfsvnode(struct knote *kn, long hint);
5023 static void filt_vfsdetach(struct knote *kn);
5024 static struct filterops vfsread_filtops = {
5026 .f_detach = filt_vfsdetach,
5027 .f_event = filt_vfsread
5029 static struct filterops vfswrite_filtops = {
5031 .f_detach = filt_vfsdetach,
5032 .f_event = filt_vfswrite
5034 static struct filterops vfsvnode_filtops = {
5036 .f_detach = filt_vfsdetach,
5037 .f_event = filt_vfsvnode
5041 vfs_knllock(void *arg)
5043 struct vnode *vp = arg;
5045 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5049 vfs_knlunlock(void *arg)
5051 struct vnode *vp = arg;
5057 vfs_knl_assert_locked(void *arg)
5059 #ifdef DEBUG_VFS_LOCKS
5060 struct vnode *vp = arg;
5062 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
5067 vfs_knl_assert_unlocked(void *arg)
5069 #ifdef DEBUG_VFS_LOCKS
5070 struct vnode *vp = arg;
5072 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
5077 vfs_kqfilter(struct vop_kqfilter_args *ap)
5079 struct vnode *vp = ap->a_vp;
5080 struct knote *kn = ap->a_kn;
5083 switch (kn->kn_filter) {
5085 kn->kn_fop = &vfsread_filtops;
5088 kn->kn_fop = &vfswrite_filtops;
5091 kn->kn_fop = &vfsvnode_filtops;
5097 kn->kn_hook = (caddr_t)vp;
5100 if (vp->v_pollinfo == NULL)
5102 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
5104 knlist_add(knl, kn, 0);
5110 * Detach knote from vnode
5113 filt_vfsdetach(struct knote *kn)
5115 struct vnode *vp = (struct vnode *)kn->kn_hook;
5117 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
5118 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
5124 filt_vfsread(struct knote *kn, long hint)
5126 struct vnode *vp = (struct vnode *)kn->kn_hook;
5131 * filesystem is gone, so set the EOF flag and schedule
5132 * the knote for deletion.
5134 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5136 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5141 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
5145 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
5146 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
5153 filt_vfswrite(struct knote *kn, long hint)
5155 struct vnode *vp = (struct vnode *)kn->kn_hook;
5160 * filesystem is gone, so set the EOF flag and schedule
5161 * the knote for deletion.
5163 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
5164 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5172 filt_vfsvnode(struct knote *kn, long hint)
5174 struct vnode *vp = (struct vnode *)kn->kn_hook;
5178 if (kn->kn_sfflags & hint)
5179 kn->kn_fflags |= hint;
5180 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5181 kn->kn_flags |= EV_EOF;
5185 res = (kn->kn_fflags != 0);
5191 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
5195 if (dp->d_reclen > ap->a_uio->uio_resid)
5196 return (ENAMETOOLONG);
5197 error = uiomove(dp, dp->d_reclen, ap->a_uio);
5199 if (ap->a_ncookies != NULL) {
5200 if (ap->a_cookies != NULL)
5201 free(ap->a_cookies, M_TEMP);
5202 ap->a_cookies = NULL;
5203 *ap->a_ncookies = 0;
5207 if (ap->a_ncookies == NULL)
5210 KASSERT(ap->a_cookies,
5211 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
5213 *ap->a_cookies = realloc(*ap->a_cookies,
5214 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
5215 (*ap->a_cookies)[*ap->a_ncookies] = off;
5216 *ap->a_ncookies += 1;
5221 * Mark for update the access time of the file if the filesystem
5222 * supports VOP_MARKATIME. This functionality is used by execve and
5223 * mmap, so we want to avoid the I/O implied by directly setting
5224 * va_atime for the sake of efficiency.
5227 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
5232 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
5233 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
5234 (void)VOP_MARKATIME(vp);
5238 * The purpose of this routine is to remove granularity from accmode_t,
5239 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
5240 * VADMIN and VAPPEND.
5242 * If it returns 0, the caller is supposed to continue with the usual
5243 * access checks using 'accmode' as modified by this routine. If it
5244 * returns nonzero value, the caller is supposed to return that value
5247 * Note that after this routine runs, accmode may be zero.
5250 vfs_unixify_accmode(accmode_t *accmode)
5253 * There is no way to specify explicit "deny" rule using
5254 * file mode or POSIX.1e ACLs.
5256 if (*accmode & VEXPLICIT_DENY) {
5262 * None of these can be translated into usual access bits.
5263 * Also, the common case for NFSv4 ACLs is to not contain
5264 * either of these bits. Caller should check for VWRITE
5265 * on the containing directory instead.
5267 if (*accmode & (VDELETE_CHILD | VDELETE))
5270 if (*accmode & VADMIN_PERMS) {
5271 *accmode &= ~VADMIN_PERMS;
5276 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
5277 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
5279 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
5285 * These are helper functions for filesystems to traverse all
5286 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
5288 * This interface replaces MNT_VNODE_FOREACH.
5291 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
5294 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
5299 kern_yield(PRI_USER);
5301 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5302 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL;
5303 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
5304 /* Allow a racy peek at VI_DOOMED to save a lock acquisition. */
5305 if (vp->v_type == VMARKER || (vp->v_iflag & VI_DOOMED) != 0)
5308 if ((vp->v_iflag & VI_DOOMED) != 0) {
5315 __mnt_vnode_markerfree_all(mvp, mp);
5316 /* MNT_IUNLOCK(mp); -- done in above function */
5317 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
5320 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
5321 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
5327 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
5331 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
5334 (*mvp)->v_mount = mp;
5335 (*mvp)->v_type = VMARKER;
5337 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
5338 /* Allow a racy peek at VI_DOOMED to save a lock acquisition. */
5339 if (vp->v_type == VMARKER || (vp->v_iflag & VI_DOOMED) != 0)
5342 if ((vp->v_iflag & VI_DOOMED) != 0) {
5351 free(*mvp, M_VNODE_MARKER);
5355 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
5361 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
5369 mtx_assert(MNT_MTX(mp), MA_OWNED);
5371 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5372 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
5375 free(*mvp, M_VNODE_MARKER);
5380 * These are helper functions for filesystems to traverse their
5381 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
5384 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
5387 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5392 free(*mvp, M_VNODE_MARKER);
5397 * Relock the mp mount vnode list lock with the vp vnode interlock in the
5398 * conventional lock order during mnt_vnode_next_active iteration.
5400 * On entry, the mount vnode list lock is held and the vnode interlock is not.
5401 * The list lock is dropped and reacquired. On success, both locks are held.
5402 * On failure, the mount vnode list lock is held but the vnode interlock is
5403 * not, and the procedure may have yielded.
5406 mnt_vnode_next_active_relock(struct vnode *mvp, struct mount *mp,
5409 const struct vnode *tmp;
5412 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER &&
5413 TAILQ_NEXT(mvp, v_actfreelist) != NULL, mvp,
5414 ("%s: bad marker", __func__));
5415 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp,
5416 ("%s: inappropriate vnode", __func__));
5417 ASSERT_VI_UNLOCKED(vp, __func__);
5418 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5422 TAILQ_REMOVE(&mp->mnt_activevnodelist, mvp, v_actfreelist);
5423 TAILQ_INSERT_BEFORE(vp, mvp, v_actfreelist);
5426 * Use a hold to prevent vp from disappearing while the mount vnode
5427 * list lock is dropped and reacquired. Normally a hold would be
5428 * acquired with vhold(), but that might try to acquire the vnode
5429 * interlock, which would be a LOR with the mount vnode list lock.
5431 held = refcount_acquire_if_not_zero(&vp->v_holdcnt);
5432 mtx_unlock(&mp->mnt_listmtx);
5436 if (!refcount_release_if_not_last(&vp->v_holdcnt)) {
5440 mtx_lock(&mp->mnt_listmtx);
5443 * Determine whether the vnode is still the next one after the marker,
5444 * excepting any other markers. If the vnode has not been doomed by
5445 * vgone() then the hold should have ensured that it remained on the
5446 * active list. If it has been doomed but is still on the active list,
5447 * don't abort, but rather skip over it (avoid spinning on doomed
5452 tmp = TAILQ_NEXT(tmp, v_actfreelist);
5453 } while (tmp != NULL && tmp->v_type == VMARKER);
5455 mtx_unlock(&mp->mnt_listmtx);
5464 mtx_lock(&mp->mnt_listmtx);
5467 ASSERT_VI_LOCKED(vp, __func__);
5469 ASSERT_VI_UNLOCKED(vp, __func__);
5470 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5474 static struct vnode *
5475 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
5477 struct vnode *vp, *nvp;
5479 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5480 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5482 vp = TAILQ_NEXT(*mvp, v_actfreelist);
5483 while (vp != NULL) {
5484 if (vp->v_type == VMARKER) {
5485 vp = TAILQ_NEXT(vp, v_actfreelist);
5489 * Try-lock because this is the wrong lock order. If that does
5490 * not succeed, drop the mount vnode list lock and try to
5491 * reacquire it and the vnode interlock in the right order.
5493 if (!VI_TRYLOCK(vp) &&
5494 !mnt_vnode_next_active_relock(*mvp, mp, vp))
5496 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
5497 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
5498 ("alien vnode on the active list %p %p", vp, mp));
5499 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
5501 nvp = TAILQ_NEXT(vp, v_actfreelist);
5505 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
5507 /* Check if we are done */
5509 mtx_unlock(&mp->mnt_listmtx);
5510 mnt_vnode_markerfree_active(mvp, mp);
5513 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
5514 mtx_unlock(&mp->mnt_listmtx);
5515 ASSERT_VI_LOCKED(vp, "active iter");
5516 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
5521 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
5525 kern_yield(PRI_USER);
5526 mtx_lock(&mp->mnt_listmtx);
5527 return (mnt_vnode_next_active(mvp, mp));
5531 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
5535 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
5539 (*mvp)->v_type = VMARKER;
5540 (*mvp)->v_mount = mp;
5542 mtx_lock(&mp->mnt_listmtx);
5543 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
5545 mtx_unlock(&mp->mnt_listmtx);
5546 mnt_vnode_markerfree_active(mvp, mp);
5549 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
5550 return (mnt_vnode_next_active(mvp, mp));
5554 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
5560 mtx_lock(&mp->mnt_listmtx);
5561 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
5562 mtx_unlock(&mp->mnt_listmtx);
5563 mnt_vnode_markerfree_active(mvp, mp);