2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
40 * External virtual filesystem routines
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
47 #include "opt_watchdog.h"
49 #include <sys/param.h>
50 #include <sys/systm.h>
53 #include <sys/capsicum.h>
54 #include <sys/condvar.h>
56 #include <sys/counter.h>
57 #include <sys/dirent.h>
58 #include <sys/event.h>
59 #include <sys/eventhandler.h>
60 #include <sys/extattr.h>
62 #include <sys/fcntl.h>
65 #include <sys/kernel.h>
66 #include <sys/kthread.h>
67 #include <sys/lockf.h>
68 #include <sys/malloc.h>
69 #include <sys/mount.h>
70 #include <sys/namei.h>
71 #include <sys/pctrie.h>
73 #include <sys/reboot.h>
74 #include <sys/refcount.h>
75 #include <sys/rwlock.h>
76 #include <sys/sched.h>
77 #include <sys/sleepqueue.h>
80 #include <sys/sysctl.h>
81 #include <sys/syslog.h>
82 #include <sys/vmmeter.h>
83 #include <sys/vnode.h>
84 #include <sys/watchdog.h>
86 #include <machine/stdarg.h>
88 #include <security/mac/mac_framework.h>
91 #include <vm/vm_object.h>
92 #include <vm/vm_extern.h>
94 #include <vm/vm_map.h>
95 #include <vm/vm_page.h>
96 #include <vm/vm_kern.h>
103 static void delmntque(struct vnode *vp);
104 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
105 int slpflag, int slptimeo);
106 static void syncer_shutdown(void *arg, int howto);
107 static int vtryrecycle(struct vnode *vp);
108 static void v_init_counters(struct vnode *);
109 static void v_incr_usecount(struct vnode *);
110 static void v_incr_usecount_locked(struct vnode *);
111 static void v_incr_devcount(struct vnode *);
112 static void v_decr_devcount(struct vnode *);
113 static void vgonel(struct vnode *);
114 static void vfs_knllock(void *arg);
115 static void vfs_knlunlock(void *arg);
116 static void vfs_knl_assert_locked(void *arg);
117 static void vfs_knl_assert_unlocked(void *arg);
118 static void vnlru_return_batches(struct vfsops *mnt_op);
119 static void destroy_vpollinfo(struct vpollinfo *vi);
120 static int v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
121 daddr_t startlbn, daddr_t endlbn);
124 * These fences are intended for cases where some synchronization is
125 * needed between access of v_iflags and lockless vnode refcount (v_holdcnt
126 * and v_usecount) updates. Access to v_iflags is generally synchronized
127 * by the interlock, but we have some internal assertions that check vnode
128 * flags without acquiring the lock. Thus, these fences are INVARIANTS-only
132 #define VNODE_REFCOUNT_FENCE_ACQ() atomic_thread_fence_acq()
133 #define VNODE_REFCOUNT_FENCE_REL() atomic_thread_fence_rel()
135 #define VNODE_REFCOUNT_FENCE_ACQ()
136 #define VNODE_REFCOUNT_FENCE_REL()
140 * Number of vnodes in existence. Increased whenever getnewvnode()
141 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
143 static unsigned long numvnodes;
145 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
146 "Number of vnodes in existence");
148 static counter_u64_t vnodes_created;
149 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
150 "Number of vnodes created by getnewvnode");
152 static u_long mnt_free_list_batch = 128;
153 SYSCTL_ULONG(_vfs, OID_AUTO, mnt_free_list_batch, CTLFLAG_RW,
154 &mnt_free_list_batch, 0, "Limit of vnodes held on mnt's free list");
157 * Conversion tables for conversion from vnode types to inode formats
160 enum vtype iftovt_tab[16] = {
161 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
162 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
164 int vttoif_tab[10] = {
165 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
166 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
170 * List of vnodes that are ready for recycling.
172 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
175 * "Free" vnode target. Free vnodes are rarely completely free, but are
176 * just ones that are cheap to recycle. Usually they are for files which
177 * have been stat'd but not read; these usually have inode and namecache
178 * data attached to them. This target is the preferred minimum size of a
179 * sub-cache consisting mostly of such files. The system balances the size
180 * of this sub-cache with its complement to try to prevent either from
181 * thrashing while the other is relatively inactive. The targets express
182 * a preference for the best balance.
184 * "Above" this target there are 2 further targets (watermarks) related
185 * to recyling of free vnodes. In the best-operating case, the cache is
186 * exactly full, the free list has size between vlowat and vhiwat above the
187 * free target, and recycling from it and normal use maintains this state.
188 * Sometimes the free list is below vlowat or even empty, but this state
189 * is even better for immediate use provided the cache is not full.
190 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free
191 * ones) to reach one of these states. The watermarks are currently hard-
192 * coded as 4% and 9% of the available space higher. These and the default
193 * of 25% for wantfreevnodes are too large if the memory size is large.
194 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim
195 * whenever vnlru_proc() becomes active.
197 static u_long wantfreevnodes;
198 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW,
199 &wantfreevnodes, 0, "Target for minimum number of \"free\" vnodes");
200 static u_long freevnodes;
201 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD,
202 &freevnodes, 0, "Number of \"free\" vnodes");
204 static counter_u64_t recycles_count;
205 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count,
206 "Number of vnodes recycled to meet vnode cache targets");
209 * Various variables used for debugging the new implementation of
211 * XXX these are probably of (very) limited utility now.
213 static int reassignbufcalls;
214 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
215 "Number of calls to reassignbuf");
217 static counter_u64_t free_owe_inact;
218 SYSCTL_COUNTER_U64(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact,
219 "Number of times free vnodes kept on active list due to VFS "
220 "owing inactivation");
222 /* To keep more than one thread at a time from running vfs_getnewfsid */
223 static struct mtx mntid_mtx;
226 * Lock for any access to the following:
231 static struct mtx vnode_free_list_mtx;
233 /* Publicly exported FS */
234 struct nfs_public nfs_pub;
236 static uma_zone_t buf_trie_zone;
238 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
239 static uma_zone_t vnode_zone;
240 static uma_zone_t vnodepoll_zone;
243 * The workitem queue.
245 * It is useful to delay writes of file data and filesystem metadata
246 * for tens of seconds so that quickly created and deleted files need
247 * not waste disk bandwidth being created and removed. To realize this,
248 * we append vnodes to a "workitem" queue. When running with a soft
249 * updates implementation, most pending metadata dependencies should
250 * not wait for more than a few seconds. Thus, mounted on block devices
251 * are delayed only about a half the time that file data is delayed.
252 * Similarly, directory updates are more critical, so are only delayed
253 * about a third the time that file data is delayed. Thus, there are
254 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
255 * one each second (driven off the filesystem syncer process). The
256 * syncer_delayno variable indicates the next queue that is to be processed.
257 * Items that need to be processed soon are placed in this queue:
259 * syncer_workitem_pending[syncer_delayno]
261 * A delay of fifteen seconds is done by placing the request fifteen
262 * entries later in the queue:
264 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
267 static int syncer_delayno;
268 static long syncer_mask;
269 LIST_HEAD(synclist, bufobj);
270 static struct synclist *syncer_workitem_pending;
272 * The sync_mtx protects:
277 * syncer_workitem_pending
278 * syncer_worklist_len
281 static struct mtx sync_mtx;
282 static struct cv sync_wakeup;
284 #define SYNCER_MAXDELAY 32
285 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
286 static int syncdelay = 30; /* max time to delay syncing data */
287 static int filedelay = 30; /* time to delay syncing files */
288 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
289 "Time to delay syncing files (in seconds)");
290 static int dirdelay = 29; /* time to delay syncing directories */
291 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
292 "Time to delay syncing directories (in seconds)");
293 static int metadelay = 28; /* time to delay syncing metadata */
294 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
295 "Time to delay syncing metadata (in seconds)");
296 static int rushjob; /* number of slots to run ASAP */
297 static int stat_rush_requests; /* number of times I/O speeded up */
298 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
299 "Number of times I/O speeded up (rush requests)");
302 * When shutting down the syncer, run it at four times normal speed.
304 #define SYNCER_SHUTDOWN_SPEEDUP 4
305 static int sync_vnode_count;
306 static int syncer_worklist_len;
307 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
310 /* Target for maximum number of vnodes. */
312 static int gapvnodes; /* gap between wanted and desired */
313 static int vhiwat; /* enough extras after expansion */
314 static int vlowat; /* minimal extras before expansion */
315 static int vstir; /* nonzero to stir non-free vnodes */
316 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */
319 sysctl_update_desiredvnodes(SYSCTL_HANDLER_ARGS)
321 int error, old_desiredvnodes;
323 old_desiredvnodes = desiredvnodes;
324 if ((error = sysctl_handle_int(oidp, arg1, arg2, req)) != 0)
326 if (old_desiredvnodes != desiredvnodes) {
327 wantfreevnodes = desiredvnodes / 4;
328 /* XXX locking seems to be incomplete. */
329 vfs_hash_changesize(desiredvnodes);
330 cache_changesize(desiredvnodes);
335 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
336 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &desiredvnodes, 0,
337 sysctl_update_desiredvnodes, "I", "Target for maximum number of vnodes");
338 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
339 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)");
340 static int vnlru_nowhere;
341 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
342 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
345 sysctl_try_reclaim_vnode(SYSCTL_HANDLER_ARGS)
350 unsigned long ndflags;
353 if (req->newptr == NULL)
355 if (req->newlen >= PATH_MAX)
358 buf = malloc(PATH_MAX, M_TEMP, M_WAITOK);
359 error = SYSCTL_IN(req, buf, req->newlen);
363 buf[req->newlen] = '\0';
365 ndflags = LOCKLEAF | NOFOLLOW | AUDITVNODE1 | NOCACHE | SAVENAME;
366 NDINIT(&nd, LOOKUP, ndflags, UIO_SYSSPACE, buf, curthread);
367 if ((error = namei(&nd)) != 0)
371 if ((vp->v_iflag & VI_DOOMED) != 0) {
373 * This vnode is being recycled. Return != 0 to let the caller
374 * know that the sysctl had no effect. Return EAGAIN because a
375 * subsequent call will likely succeed (since namei will create
376 * a new vnode if necessary)
382 counter_u64_add(recycles_count, 1);
392 sysctl_ftry_reclaim_vnode(SYSCTL_HANDLER_ARGS)
394 struct thread *td = curthread;
400 if (req->newptr == NULL)
403 error = sysctl_handle_int(oidp, &fd, 0, req);
406 error = getvnode(curthread, fd, &cap_fcntl_rights, &fp);
411 error = vn_lock(vp, LK_EXCLUSIVE);
415 counter_u64_add(recycles_count, 1);
423 SYSCTL_PROC(_debug, OID_AUTO, try_reclaim_vnode,
424 CTLTYPE_STRING | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
425 sysctl_try_reclaim_vnode, "A", "Try to reclaim a vnode by its pathname");
426 SYSCTL_PROC(_debug, OID_AUTO, ftry_reclaim_vnode,
427 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_WR, NULL, 0,
428 sysctl_ftry_reclaim_vnode, "I",
429 "Try to reclaim a vnode by its file descriptor");
431 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
435 * Support for the bufobj clean & dirty pctrie.
438 buf_trie_alloc(struct pctrie *ptree)
441 return uma_zalloc(buf_trie_zone, M_NOWAIT);
445 buf_trie_free(struct pctrie *ptree, void *node)
448 uma_zfree(buf_trie_zone, node);
450 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
453 * Initialize the vnode management data structures.
455 * Reevaluate the following cap on the number of vnodes after the physical
456 * memory size exceeds 512GB. In the limit, as the physical memory size
457 * grows, the ratio of the memory size in KB to vnodes approaches 64:1.
459 #ifndef MAXVNODES_MAX
460 #define MAXVNODES_MAX (512 * 1024 * 1024 / 64) /* 8M */
464 * Initialize a vnode as it first enters the zone.
467 vnode_init(void *mem, int size, int flags)
476 vp->v_vnlock = &vp->v_lock;
477 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
479 * By default, don't allow shared locks unless filesystems opt-in.
481 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT,
482 LK_NOSHARE | LK_IS_VNODE);
486 bufobj_init(&vp->v_bufobj, vp);
488 * Initialize namecache.
490 LIST_INIT(&vp->v_cache_src);
491 TAILQ_INIT(&vp->v_cache_dst);
493 * Initialize rangelocks.
495 rangelock_init(&vp->v_rl);
500 * Free a vnode when it is cleared from the zone.
503 vnode_fini(void *mem, int size)
509 rangelock_destroy(&vp->v_rl);
510 lockdestroy(vp->v_vnlock);
511 mtx_destroy(&vp->v_interlock);
513 rw_destroy(BO_LOCKPTR(bo));
517 * Provide the size of NFS nclnode and NFS fh for calculation of the
518 * vnode memory consumption. The size is specified directly to
519 * eliminate dependency on NFS-private header.
521 * Other filesystems may use bigger or smaller (like UFS and ZFS)
522 * private inode data, but the NFS-based estimation is ample enough.
523 * Still, we care about differences in the size between 64- and 32-bit
526 * Namecache structure size is heuristically
527 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1.
530 #define NFS_NCLNODE_SZ (528 + 64)
533 #define NFS_NCLNODE_SZ (360 + 32)
538 vntblinit(void *dummy __unused)
541 int physvnodes, virtvnodes;
544 * Desiredvnodes is a function of the physical memory size and the
545 * kernel's heap size. Generally speaking, it scales with the
546 * physical memory size. The ratio of desiredvnodes to the physical
547 * memory size is 1:16 until desiredvnodes exceeds 98,304.
549 * marginal ratio of desiredvnodes to the physical memory size is
550 * 1:64. However, desiredvnodes is limited by the kernel's heap
551 * size. The memory required by desiredvnodes vnodes and vm objects
552 * must not exceed 1/10th of the kernel's heap size.
554 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 +
555 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64;
556 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) +
557 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ));
558 desiredvnodes = min(physvnodes, virtvnodes);
559 if (desiredvnodes > MAXVNODES_MAX) {
561 printf("Reducing kern.maxvnodes %d -> %d\n",
562 desiredvnodes, MAXVNODES_MAX);
563 desiredvnodes = MAXVNODES_MAX;
565 wantfreevnodes = desiredvnodes / 4;
566 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
567 TAILQ_INIT(&vnode_free_list);
568 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
569 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
570 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0);
571 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
572 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
574 * Preallocate enough nodes to support one-per buf so that
575 * we can not fail an insert. reassignbuf() callers can not
576 * tolerate the insertion failure.
578 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
579 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
580 UMA_ZONE_NOFREE | UMA_ZONE_VM);
581 uma_prealloc(buf_trie_zone, nbuf);
583 vnodes_created = counter_u64_alloc(M_WAITOK);
584 recycles_count = counter_u64_alloc(M_WAITOK);
585 free_owe_inact = counter_u64_alloc(M_WAITOK);
588 * Initialize the filesystem syncer.
590 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
592 syncer_maxdelay = syncer_mask + 1;
593 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
594 cv_init(&sync_wakeup, "syncer");
595 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
599 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
603 * Mark a mount point as busy. Used to synchronize access and to delay
604 * unmounting. Eventually, mountlist_mtx is not released on failure.
606 * vfs_busy() is a custom lock, it can block the caller.
607 * vfs_busy() only sleeps if the unmount is active on the mount point.
608 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
609 * vnode belonging to mp.
611 * Lookup uses vfs_busy() to traverse mount points.
613 * / vnode lock A / vnode lock (/var) D
614 * /var vnode lock B /log vnode lock(/var/log) E
615 * vfs_busy lock C vfs_busy lock F
617 * Within each file system, the lock order is C->A->B and F->D->E.
619 * When traversing across mounts, the system follows that lock order:
625 * The lookup() process for namei("/var") illustrates the process:
626 * VOP_LOOKUP() obtains B while A is held
627 * vfs_busy() obtains a shared lock on F while A and B are held
628 * vput() releases lock on B
629 * vput() releases lock on A
630 * VFS_ROOT() obtains lock on D while shared lock on F is held
631 * vfs_unbusy() releases shared lock on F
632 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
633 * Attempt to lock A (instead of vp_crossmp) while D is held would
634 * violate the global order, causing deadlocks.
636 * dounmount() locks B while F is drained.
639 vfs_busy(struct mount *mp, int flags)
642 MPASS((flags & ~MBF_MASK) == 0);
643 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
648 * If mount point is currently being unmounted, sleep until the
649 * mount point fate is decided. If thread doing the unmounting fails,
650 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
651 * that this mount point has survived the unmount attempt and vfs_busy
652 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
653 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
654 * about to be really destroyed. vfs_busy needs to release its
655 * reference on the mount point in this case and return with ENOENT,
656 * telling the caller that mount mount it tried to busy is no longer
659 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
660 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
663 CTR1(KTR_VFS, "%s: failed busying before sleeping",
667 if (flags & MBF_MNTLSTLOCK)
668 mtx_unlock(&mountlist_mtx);
669 mp->mnt_kern_flag |= MNTK_MWAIT;
670 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
671 if (flags & MBF_MNTLSTLOCK)
672 mtx_lock(&mountlist_mtx);
675 if (flags & MBF_MNTLSTLOCK)
676 mtx_unlock(&mountlist_mtx);
683 * Free a busy filesystem.
686 vfs_unbusy(struct mount *mp)
689 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
692 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
694 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
695 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
696 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
697 mp->mnt_kern_flag &= ~MNTK_DRAINING;
698 wakeup(&mp->mnt_lockref);
704 * Lookup a mount point by filesystem identifier.
707 vfs_getvfs(fsid_t *fsid)
711 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
712 mtx_lock(&mountlist_mtx);
713 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
714 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0) {
716 mtx_unlock(&mountlist_mtx);
720 mtx_unlock(&mountlist_mtx);
721 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
722 return ((struct mount *) 0);
726 * Lookup a mount point by filesystem identifier, busying it before
729 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
730 * cache for popular filesystem identifiers. The cache is lockess, using
731 * the fact that struct mount's are never freed. In worst case we may
732 * get pointer to unmounted or even different filesystem, so we have to
733 * check what we got, and go slow way if so.
736 vfs_busyfs(fsid_t *fsid)
738 #define FSID_CACHE_SIZE 256
739 typedef struct mount * volatile vmp_t;
740 static vmp_t cache[FSID_CACHE_SIZE];
745 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
746 hash = fsid->val[0] ^ fsid->val[1];
747 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
749 if (mp == NULL || fsidcmp(&mp->mnt_stat.f_fsid, fsid) != 0)
751 if (vfs_busy(mp, 0) != 0) {
755 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0)
761 mtx_lock(&mountlist_mtx);
762 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
763 if (fsidcmp(&mp->mnt_stat.f_fsid, fsid) == 0) {
764 error = vfs_busy(mp, MBF_MNTLSTLOCK);
767 mtx_unlock(&mountlist_mtx);
774 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
775 mtx_unlock(&mountlist_mtx);
776 return ((struct mount *) 0);
780 * Check if a user can access privileged mount options.
783 vfs_suser(struct mount *mp, struct thread *td)
787 if (jailed(td->td_ucred)) {
789 * If the jail of the calling thread lacks permission for
790 * this type of file system, deny immediately.
792 if (!prison_allow(td->td_ucred, mp->mnt_vfc->vfc_prison_flag))
796 * If the file system was mounted outside the jail of the
797 * calling thread, deny immediately.
799 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
804 * If file system supports delegated administration, we don't check
805 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
806 * by the file system itself.
807 * If this is not the user that did original mount, we check for
808 * the PRIV_VFS_MOUNT_OWNER privilege.
810 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
811 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
812 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
819 * Get a new unique fsid. Try to make its val[0] unique, since this value
820 * will be used to create fake device numbers for stat(). Also try (but
821 * not so hard) make its val[0] unique mod 2^16, since some emulators only
822 * support 16-bit device numbers. We end up with unique val[0]'s for the
823 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
825 * Keep in mind that several mounts may be running in parallel. Starting
826 * the search one past where the previous search terminated is both a
827 * micro-optimization and a defense against returning the same fsid to
831 vfs_getnewfsid(struct mount *mp)
833 static uint16_t mntid_base;
838 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
839 mtx_lock(&mntid_mtx);
840 mtype = mp->mnt_vfc->vfc_typenum;
841 tfsid.val[1] = mtype;
842 mtype = (mtype & 0xFF) << 24;
844 tfsid.val[0] = makedev(255,
845 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
847 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
851 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
852 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
853 mtx_unlock(&mntid_mtx);
857 * Knob to control the precision of file timestamps:
859 * 0 = seconds only; nanoseconds zeroed.
860 * 1 = seconds and nanoseconds, accurate within 1/HZ.
861 * 2 = seconds and nanoseconds, truncated to microseconds.
862 * >=3 = seconds and nanoseconds, maximum precision.
864 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
866 static int timestamp_precision = TSP_USEC;
867 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
868 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
869 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to us, "
870 "3+: sec + ns (max. precision))");
873 * Get a current timestamp.
876 vfs_timestamp(struct timespec *tsp)
880 switch (timestamp_precision) {
882 tsp->tv_sec = time_second;
890 TIMEVAL_TO_TIMESPEC(&tv, tsp);
900 * Set vnode attributes to VNOVAL
903 vattr_null(struct vattr *vap)
907 vap->va_size = VNOVAL;
908 vap->va_bytes = VNOVAL;
909 vap->va_mode = VNOVAL;
910 vap->va_nlink = VNOVAL;
911 vap->va_uid = VNOVAL;
912 vap->va_gid = VNOVAL;
913 vap->va_fsid = VNOVAL;
914 vap->va_fileid = VNOVAL;
915 vap->va_blocksize = VNOVAL;
916 vap->va_rdev = VNOVAL;
917 vap->va_atime.tv_sec = VNOVAL;
918 vap->va_atime.tv_nsec = VNOVAL;
919 vap->va_mtime.tv_sec = VNOVAL;
920 vap->va_mtime.tv_nsec = VNOVAL;
921 vap->va_ctime.tv_sec = VNOVAL;
922 vap->va_ctime.tv_nsec = VNOVAL;
923 vap->va_birthtime.tv_sec = VNOVAL;
924 vap->va_birthtime.tv_nsec = VNOVAL;
925 vap->va_flags = VNOVAL;
926 vap->va_gen = VNOVAL;
931 * This routine is called when we have too many vnodes. It attempts
932 * to free <count> vnodes and will potentially free vnodes that still
933 * have VM backing store (VM backing store is typically the cause
934 * of a vnode blowout so we want to do this). Therefore, this operation
935 * is not considered cheap.
937 * A number of conditions may prevent a vnode from being reclaimed.
938 * the buffer cache may have references on the vnode, a directory
939 * vnode may still have references due to the namei cache representing
940 * underlying files, or the vnode may be in active use. It is not
941 * desirable to reuse such vnodes. These conditions may cause the
942 * number of vnodes to reach some minimum value regardless of what
943 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
945 * @param mp Try to reclaim vnodes from this mountpoint
946 * @param reclaim_nc_src Only reclaim directories with outgoing namecache
947 * entries if this argument is strue
948 * @param trigger Only reclaim vnodes with fewer than this many resident
950 * @return The number of vnodes that were reclaimed.
953 vlrureclaim(struct mount *mp, bool reclaim_nc_src, int trigger)
956 int count, done, target;
959 vn_start_write(NULL, &mp, V_WAIT);
961 count = mp->mnt_nvnodelistsize;
962 target = count * (int64_t)gapvnodes / imax(desiredvnodes, 1);
963 target = target / 10 + 1;
964 while (count != 0 && done < target) {
965 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
966 while (vp != NULL && vp->v_type == VMARKER)
967 vp = TAILQ_NEXT(vp, v_nmntvnodes);
971 * XXX LRU is completely broken for non-free vnodes. First
972 * by calling here in mountpoint order, then by moving
973 * unselected vnodes to the end here, and most grossly by
974 * removing the vlruvp() function that was supposed to
975 * maintain the order. (This function was born broken
976 * since syncer problems prevented it doing anything.) The
977 * order is closer to LRC (C = Created).
979 * LRU reclaiming of vnodes seems to have last worked in
980 * FreeBSD-3 where LRU wasn't mentioned under any spelling.
981 * Then there was no hold count, and inactive vnodes were
982 * simply put on the free list in LRU order. The separate
983 * lists also break LRU. We prefer to reclaim from the
984 * free list for technical reasons. This tends to thrash
985 * the free list to keep very unrecently used held vnodes.
986 * The problem is mitigated by keeping the free list large.
988 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
989 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
994 * If it's been deconstructed already, it's still
995 * referenced, or it exceeds the trigger, skip it.
996 * Also skip free vnodes. We are trying to make space
997 * to expand the free list, not reduce it.
999 if (vp->v_usecount ||
1000 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
1001 ((vp->v_iflag & VI_FREE) != 0) ||
1002 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
1003 vp->v_object->resident_page_count > trigger)) {
1009 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
1011 goto next_iter_mntunlocked;
1015 * v_usecount may have been bumped after VOP_LOCK() dropped
1016 * the vnode interlock and before it was locked again.
1018 * It is not necessary to recheck VI_DOOMED because it can
1019 * only be set by another thread that holds both the vnode
1020 * lock and vnode interlock. If another thread has the
1021 * vnode lock before we get to VOP_LOCK() and obtains the
1022 * vnode interlock after VOP_LOCK() drops the vnode
1023 * interlock, the other thread will be unable to drop the
1024 * vnode lock before our VOP_LOCK() call fails.
1026 if (vp->v_usecount ||
1027 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
1028 (vp->v_iflag & VI_FREE) != 0 ||
1029 (vp->v_object != NULL &&
1030 vp->v_object->resident_page_count > trigger)) {
1031 VOP_UNLOCK(vp, LK_INTERLOCK);
1033 goto next_iter_mntunlocked;
1035 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
1036 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
1037 counter_u64_add(recycles_count, 1);
1042 next_iter_mntunlocked:
1043 if (!should_yield())
1047 if (!should_yield())
1051 kern_yield(PRI_USER);
1056 vn_finished_write(mp);
1060 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */
1061 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free,
1063 "limit on vnode free requests per call to the vnlru_free routine");
1066 * Attempt to reduce the free list by the requested amount.
1069 vnlru_free_locked(int count, struct vfsops *mnt_op)
1075 tried_batches = false;
1076 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1077 if (count > max_vnlru_free)
1078 count = max_vnlru_free;
1079 for (; count > 0; count--) {
1080 vp = TAILQ_FIRST(&vnode_free_list);
1082 * The list can be modified while the free_list_mtx
1083 * has been dropped and vp could be NULL here.
1088 mtx_unlock(&vnode_free_list_mtx);
1089 vnlru_return_batches(mnt_op);
1090 tried_batches = true;
1091 mtx_lock(&vnode_free_list_mtx);
1095 VNASSERT(vp->v_op != NULL, vp,
1096 ("vnlru_free: vnode already reclaimed."));
1097 KASSERT((vp->v_iflag & VI_FREE) != 0,
1098 ("Removing vnode not on freelist"));
1099 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1100 ("Mangling active vnode"));
1101 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
1104 * Don't recycle if our vnode is from different type
1105 * of mount point. Note that mp is type-safe, the
1106 * check does not reach unmapped address even if
1107 * vnode is reclaimed.
1108 * Don't recycle if we can't get the interlock without
1111 if ((mnt_op != NULL && (mp = vp->v_mount) != NULL &&
1112 mp->mnt_op != mnt_op) || !VI_TRYLOCK(vp)) {
1113 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
1116 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
1117 vp, ("vp inconsistent on freelist"));
1120 * The clear of VI_FREE prevents activation of the
1121 * vnode. There is no sense in putting the vnode on
1122 * the mount point active list, only to remove it
1123 * later during recycling. Inline the relevant part
1124 * of vholdl(), to avoid triggering assertions or
1128 vp->v_iflag &= ~VI_FREE;
1129 VNODE_REFCOUNT_FENCE_REL();
1130 refcount_acquire(&vp->v_holdcnt);
1132 mtx_unlock(&vnode_free_list_mtx);
1136 * If the recycled succeeded this vdrop will actually free
1137 * the vnode. If not it will simply place it back on
1141 mtx_lock(&vnode_free_list_mtx);
1146 vnlru_free(int count, struct vfsops *mnt_op)
1149 mtx_lock(&vnode_free_list_mtx);
1150 vnlru_free_locked(count, mnt_op);
1151 mtx_unlock(&vnode_free_list_mtx);
1155 /* XXX some names and initialization are bad for limits and watermarks. */
1161 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100);
1162 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */
1163 vlowat = vhiwat / 2;
1164 if (numvnodes > desiredvnodes)
1166 space = desiredvnodes - numvnodes;
1167 if (freevnodes > wantfreevnodes)
1168 space += freevnodes - wantfreevnodes;
1173 vnlru_return_batch_locked(struct mount *mp)
1177 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
1179 if (mp->mnt_tmpfreevnodelistsize == 0)
1182 TAILQ_FOREACH(vp, &mp->mnt_tmpfreevnodelist, v_actfreelist) {
1183 VNASSERT((vp->v_mflag & VMP_TMPMNTFREELIST) != 0, vp,
1184 ("vnode without VMP_TMPMNTFREELIST on mnt_tmpfreevnodelist"));
1185 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
1187 mtx_lock(&vnode_free_list_mtx);
1188 TAILQ_CONCAT(&vnode_free_list, &mp->mnt_tmpfreevnodelist, v_actfreelist);
1189 freevnodes += mp->mnt_tmpfreevnodelistsize;
1190 mtx_unlock(&vnode_free_list_mtx);
1191 mp->mnt_tmpfreevnodelistsize = 0;
1195 vnlru_return_batch(struct mount *mp)
1198 mtx_lock(&mp->mnt_listmtx);
1199 vnlru_return_batch_locked(mp);
1200 mtx_unlock(&mp->mnt_listmtx);
1204 vnlru_return_batches(struct vfsops *mnt_op)
1206 struct mount *mp, *nmp;
1209 mtx_lock(&mountlist_mtx);
1210 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1211 need_unbusy = false;
1212 if (mnt_op != NULL && mp->mnt_op != mnt_op)
1214 if (mp->mnt_tmpfreevnodelistsize == 0)
1216 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) == 0) {
1217 vnlru_return_batch(mp);
1219 mtx_lock(&mountlist_mtx);
1222 nmp = TAILQ_NEXT(mp, mnt_list);
1226 mtx_unlock(&mountlist_mtx);
1230 * Attempt to recycle vnodes in a context that is always safe to block.
1231 * Calling vlrurecycle() from the bowels of filesystem code has some
1232 * interesting deadlock problems.
1234 static struct proc *vnlruproc;
1235 static int vnlruproc_sig;
1240 struct mount *mp, *nmp;
1241 unsigned long onumvnodes;
1242 int done, force, trigger, usevnodes, vsp;
1243 bool reclaim_nc_src;
1245 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc,
1246 SHUTDOWN_PRI_FIRST);
1250 kproc_suspend_check(vnlruproc);
1251 mtx_lock(&vnode_free_list_mtx);
1253 * If numvnodes is too large (due to desiredvnodes being
1254 * adjusted using its sysctl, or emergency growth), first
1255 * try to reduce it by discarding from the free list.
1257 if (numvnodes > desiredvnodes)
1258 vnlru_free_locked(numvnodes - desiredvnodes, NULL);
1260 * Sleep if the vnode cache is in a good state. This is
1261 * when it is not over-full and has space for about a 4%
1262 * or 9% expansion (by growing its size or inexcessively
1263 * reducing its free list). Otherwise, try to reclaim
1264 * space for a 10% expansion.
1266 if (vstir && force == 0) {
1271 if (vsp >= vlowat && force == 0) {
1273 wakeup(&vnlruproc_sig);
1274 msleep(vnlruproc, &vnode_free_list_mtx,
1275 PVFS|PDROP, "vlruwt", hz);
1278 mtx_unlock(&vnode_free_list_mtx);
1280 onumvnodes = numvnodes;
1282 * Calculate parameters for recycling. These are the same
1283 * throughout the loop to give some semblance of fairness.
1284 * The trigger point is to avoid recycling vnodes with lots
1285 * of resident pages. We aren't trying to free memory; we
1286 * are trying to recycle or at least free vnodes.
1288 if (numvnodes <= desiredvnodes)
1289 usevnodes = numvnodes - freevnodes;
1291 usevnodes = numvnodes;
1295 * The trigger value is is chosen to give a conservatively
1296 * large value to ensure that it alone doesn't prevent
1297 * making progress. The value can easily be so large that
1298 * it is effectively infinite in some congested and
1299 * misconfigured cases, and this is necessary. Normally
1300 * it is about 8 to 100 (pages), which is quite large.
1302 trigger = vm_cnt.v_page_count * 2 / usevnodes;
1304 trigger = vsmalltrigger;
1305 reclaim_nc_src = force >= 3;
1306 mtx_lock(&mountlist_mtx);
1307 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1308 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
1309 nmp = TAILQ_NEXT(mp, mnt_list);
1312 done += vlrureclaim(mp, reclaim_nc_src, trigger);
1313 mtx_lock(&mountlist_mtx);
1314 nmp = TAILQ_NEXT(mp, mnt_list);
1317 mtx_unlock(&mountlist_mtx);
1318 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes)
1321 if (force == 0 || force == 1) {
1331 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
1333 kern_yield(PRI_USER);
1335 * After becoming active to expand above low water, keep
1336 * active until above high water.
1339 force = vsp < vhiwat;
1343 static struct kproc_desc vnlru_kp = {
1348 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
1352 * Routines having to do with the management of the vnode table.
1356 * Try to recycle a freed vnode. We abort if anyone picks up a reference
1357 * before we actually vgone(). This function must be called with the vnode
1358 * held to prevent the vnode from being returned to the free list midway
1362 vtryrecycle(struct vnode *vp)
1366 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
1367 VNASSERT(vp->v_holdcnt, vp,
1368 ("vtryrecycle: Recycling vp %p without a reference.", vp));
1370 * This vnode may found and locked via some other list, if so we
1371 * can't recycle it yet.
1373 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1375 "%s: impossible to recycle, vp %p lock is already held",
1377 return (EWOULDBLOCK);
1380 * Don't recycle if its filesystem is being suspended.
1382 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1385 "%s: impossible to recycle, cannot start the write for %p",
1390 * If we got this far, we need to acquire the interlock and see if
1391 * anyone picked up this vnode from another list. If not, we will
1392 * mark it with DOOMED via vgonel() so that anyone who does find it
1393 * will skip over it.
1396 if (vp->v_usecount) {
1397 VOP_UNLOCK(vp, LK_INTERLOCK);
1398 vn_finished_write(vnmp);
1400 "%s: impossible to recycle, %p is already referenced",
1404 if ((vp->v_iflag & VI_DOOMED) == 0) {
1405 counter_u64_add(recycles_count, 1);
1408 VOP_UNLOCK(vp, LK_INTERLOCK);
1409 vn_finished_write(vnmp);
1419 if (vsp < vlowat && vnlruproc_sig == 0) {
1426 * Wait if necessary for space for a new vnode.
1429 getnewvnode_wait(int suspended)
1432 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1433 if (numvnodes >= desiredvnodes) {
1436 * The file system is being suspended. We cannot
1437 * risk a deadlock here, so allow allocation of
1438 * another vnode even if this would give too many.
1442 if (vnlruproc_sig == 0) {
1443 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1446 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1449 /* Post-adjust like the pre-adjust in getnewvnode(). */
1450 if (numvnodes + 1 > desiredvnodes && freevnodes > 1)
1451 vnlru_free_locked(1, NULL);
1452 return (numvnodes >= desiredvnodes ? ENFILE : 0);
1456 * This hack is fragile, and probably not needed any more now that the
1457 * watermark handling works.
1460 getnewvnode_reserve(u_int count)
1464 /* Pre-adjust like the pre-adjust in getnewvnode(), with any count. */
1465 /* XXX no longer so quick, but this part is not racy. */
1466 mtx_lock(&vnode_free_list_mtx);
1467 if (numvnodes + count > desiredvnodes && freevnodes > wantfreevnodes)
1468 vnlru_free_locked(ulmin(numvnodes + count - desiredvnodes,
1469 freevnodes - wantfreevnodes), NULL);
1470 mtx_unlock(&vnode_free_list_mtx);
1473 /* First try to be quick and racy. */
1474 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1475 td->td_vp_reserv += count;
1476 vcheckspace(); /* XXX no longer so quick, but more racy */
1479 atomic_subtract_long(&numvnodes, count);
1481 mtx_lock(&vnode_free_list_mtx);
1483 if (getnewvnode_wait(0) == 0) {
1486 atomic_add_long(&numvnodes, 1);
1490 mtx_unlock(&vnode_free_list_mtx);
1494 * This hack is fragile, especially if desiredvnodes or wantvnodes are
1495 * misconfgured or changed significantly. Reducing desiredvnodes below
1496 * the reserved amount should cause bizarre behaviour like reducing it
1497 * below the number of active vnodes -- the system will try to reduce
1498 * numvnodes to match, but should fail, so the subtraction below should
1502 getnewvnode_drop_reserve(void)
1507 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1508 td->td_vp_reserv = 0;
1512 * Return the next vnode from the free list.
1515 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1520 struct lock_object *lo;
1521 static int cyclecount;
1524 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1527 if (td->td_vp_reserv > 0) {
1528 td->td_vp_reserv -= 1;
1531 mtx_lock(&vnode_free_list_mtx);
1532 if (numvnodes < desiredvnodes)
1534 else if (cyclecount++ >= freevnodes) {
1539 * Grow the vnode cache if it will not be above its target max
1540 * after growing. Otherwise, if the free list is nonempty, try
1541 * to reclaim 1 item from it before growing the cache (possibly
1542 * above its target max if the reclamation failed or is delayed).
1543 * Otherwise, wait for some space. In all cases, schedule
1544 * vnlru_proc() if we are getting short of space. The watermarks
1545 * should be chosen so that we never wait or even reclaim from
1546 * the free list to below its target minimum.
1548 if (numvnodes + 1 <= desiredvnodes)
1550 else if (freevnodes > 0)
1551 vnlru_free_locked(1, NULL);
1553 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1555 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1557 mtx_unlock(&vnode_free_list_mtx);
1563 atomic_add_long(&numvnodes, 1);
1564 mtx_unlock(&vnode_free_list_mtx);
1566 counter_u64_add(vnodes_created, 1);
1567 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK);
1569 * Locks are given the generic name "vnode" when created.
1570 * Follow the historic practice of using the filesystem
1571 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc.
1573 * Locks live in a witness group keyed on their name. Thus,
1574 * when a lock is renamed, it must also move from the witness
1575 * group of its old name to the witness group of its new name.
1577 * The change only needs to be made when the vnode moves
1578 * from one filesystem type to another. We ensure that each
1579 * filesystem use a single static name pointer for its tag so
1580 * that we can compare pointers rather than doing a strcmp().
1582 lo = &vp->v_vnlock->lock_object;
1583 if (lo->lo_name != tag) {
1585 WITNESS_DESTROY(lo);
1586 WITNESS_INIT(lo, tag);
1589 * By default, don't allow shared locks unless filesystems opt-in.
1591 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE;
1593 * Finalize various vnode identity bits.
1595 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp));
1596 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp));
1597 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp));
1601 v_init_counters(vp);
1602 vp->v_bufobj.bo_ops = &buf_ops_bio;
1604 if (mp == NULL && vops != &dead_vnodeops)
1605 printf("NULL mp in getnewvnode(9), tag %s\n", tag);
1609 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1610 mac_vnode_associate_singlelabel(mp, vp);
1613 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize;
1614 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1615 vp->v_vflag |= VV_NOKNOTE;
1619 * For the filesystems which do not use vfs_hash_insert(),
1620 * still initialize v_hash to have vfs_hash_index() useful.
1621 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1624 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1631 * Delete from old mount point vnode list, if on one.
1634 delmntque(struct vnode *vp)
1644 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1645 ("Active vnode list size %d > Vnode list size %d",
1646 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1647 active = vp->v_iflag & VI_ACTIVE;
1648 vp->v_iflag &= ~VI_ACTIVE;
1650 mtx_lock(&mp->mnt_listmtx);
1651 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1652 mp->mnt_activevnodelistsize--;
1653 mtx_unlock(&mp->mnt_listmtx);
1657 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1658 ("bad mount point vnode list size"));
1659 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1660 mp->mnt_nvnodelistsize--;
1666 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1670 vp->v_op = &dead_vnodeops;
1676 * Insert into list of vnodes for the new mount point, if available.
1679 insmntque1(struct vnode *vp, struct mount *mp,
1680 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1683 KASSERT(vp->v_mount == NULL,
1684 ("insmntque: vnode already on per mount vnode list"));
1685 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1686 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1689 * We acquire the vnode interlock early to ensure that the
1690 * vnode cannot be recycled by another process releasing a
1691 * holdcnt on it before we get it on both the vnode list
1692 * and the active vnode list. The mount mutex protects only
1693 * manipulation of the vnode list and the vnode freelist
1694 * mutex protects only manipulation of the active vnode list.
1695 * Hence the need to hold the vnode interlock throughout.
1699 if (((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 &&
1700 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1701 mp->mnt_nvnodelistsize == 0)) &&
1702 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1711 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1712 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1713 ("neg mount point vnode list size"));
1714 mp->mnt_nvnodelistsize++;
1715 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1716 ("Activating already active vnode"));
1717 vp->v_iflag |= VI_ACTIVE;
1718 mtx_lock(&mp->mnt_listmtx);
1719 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1720 mp->mnt_activevnodelistsize++;
1721 mtx_unlock(&mp->mnt_listmtx);
1728 insmntque(struct vnode *vp, struct mount *mp)
1731 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1735 * Flush out and invalidate all buffers associated with a bufobj
1736 * Called with the underlying object locked.
1739 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1744 if (flags & V_SAVE) {
1745 error = bufobj_wwait(bo, slpflag, slptimeo);
1750 if (bo->bo_dirty.bv_cnt > 0) {
1752 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1755 * XXX We could save a lock/unlock if this was only
1756 * enabled under INVARIANTS
1759 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1760 panic("vinvalbuf: dirty bufs");
1764 * If you alter this loop please notice that interlock is dropped and
1765 * reacquired in flushbuflist. Special care is needed to ensure that
1766 * no race conditions occur from this.
1769 error = flushbuflist(&bo->bo_clean,
1770 flags, bo, slpflag, slptimeo);
1771 if (error == 0 && !(flags & V_CLEANONLY))
1772 error = flushbuflist(&bo->bo_dirty,
1773 flags, bo, slpflag, slptimeo);
1774 if (error != 0 && error != EAGAIN) {
1778 } while (error != 0);
1781 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1782 * have write I/O in-progress but if there is a VM object then the
1783 * VM object can also have read-I/O in-progress.
1786 bufobj_wwait(bo, 0, 0);
1787 if ((flags & V_VMIO) == 0) {
1789 if (bo->bo_object != NULL) {
1790 VM_OBJECT_WLOCK(bo->bo_object);
1791 vm_object_pip_wait(bo->bo_object, "bovlbx");
1792 VM_OBJECT_WUNLOCK(bo->bo_object);
1796 } while (bo->bo_numoutput > 0);
1800 * Destroy the copy in the VM cache, too.
1802 if (bo->bo_object != NULL &&
1803 (flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0) {
1804 VM_OBJECT_WLOCK(bo->bo_object);
1805 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1806 OBJPR_CLEANONLY : 0);
1807 VM_OBJECT_WUNLOCK(bo->bo_object);
1812 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO |
1813 V_ALLOWCLEAN)) == 0 && (bo->bo_dirty.bv_cnt > 0 ||
1814 bo->bo_clean.bv_cnt > 0))
1815 panic("vinvalbuf: flush failed");
1816 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY | V_VMIO)) == 0 &&
1817 bo->bo_dirty.bv_cnt > 0)
1818 panic("vinvalbuf: flush dirty failed");
1825 * Flush out and invalidate all buffers associated with a vnode.
1826 * Called with the underlying object locked.
1829 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1832 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1833 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1834 if (vp->v_object != NULL && vp->v_object->handle != vp)
1836 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1840 * Flush out buffers on the specified list.
1844 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1847 struct buf *bp, *nbp;
1852 ASSERT_BO_WLOCKED(bo);
1855 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1856 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1857 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1861 lblkno = nbp->b_lblkno;
1862 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1865 error = BUF_TIMELOCK(bp,
1866 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1867 "flushbuf", slpflag, slptimeo);
1870 return (error != ENOLCK ? error : EAGAIN);
1872 KASSERT(bp->b_bufobj == bo,
1873 ("bp %p wrong b_bufobj %p should be %p",
1874 bp, bp->b_bufobj, bo));
1876 * XXX Since there are no node locks for NFS, I
1877 * believe there is a slight chance that a delayed
1878 * write will occur while sleeping just above, so
1881 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1884 bp->b_flags |= B_ASYNC;
1887 return (EAGAIN); /* XXX: why not loop ? */
1890 bp->b_flags |= (B_INVAL | B_RELBUF);
1891 bp->b_flags &= ~B_ASYNC;
1896 nbp = gbincore(bo, lblkno);
1897 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1899 break; /* nbp invalid */
1905 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn)
1911 ASSERT_BO_LOCKED(bo);
1913 for (lblkno = startn;;) {
1915 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno);
1916 if (bp == NULL || bp->b_lblkno >= endn ||
1917 bp->b_lblkno < startn)
1919 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1920 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0);
1923 if (error == ENOLCK)
1927 KASSERT(bp->b_bufobj == bo,
1928 ("bp %p wrong b_bufobj %p should be %p",
1929 bp, bp->b_bufobj, bo));
1930 lblkno = bp->b_lblkno + 1;
1931 if ((bp->b_flags & B_MANAGED) == 0)
1933 bp->b_flags |= B_RELBUF;
1935 * In the VMIO case, use the B_NOREUSE flag to hint that the
1936 * pages backing each buffer in the range are unlikely to be
1937 * reused. Dirty buffers will have the hint applied once
1938 * they've been written.
1940 if ((bp->b_flags & B_VMIO) != 0)
1941 bp->b_flags |= B_NOREUSE;
1949 * Truncate a file's buffer and pages to a specified length. This
1950 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1954 vtruncbuf(struct vnode *vp, off_t length, int blksize)
1956 struct buf *bp, *nbp;
1960 CTR4(KTR_VFS, "%s: vp %p with block %d:%ju", __func__,
1961 vp, blksize, (uintmax_t)length);
1964 * Round up to the *next* lbn.
1966 startlbn = howmany(length, blksize);
1968 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1974 while (v_inval_buf_range_locked(vp, bo, startlbn, INT64_MAX) == EAGAIN)
1979 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1980 if (bp->b_lblkno > 0)
1983 * Since we hold the vnode lock this should only
1984 * fail if we're racing with the buf daemon.
1987 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1988 BO_LOCKPTR(bo)) == ENOLCK)
1989 goto restart_unlocked;
1991 VNASSERT((bp->b_flags & B_DELWRI), vp,
1992 ("buf(%p) on dirty queue without DELWRI", bp));
2001 bufobj_wwait(bo, 0, 0);
2003 vnode_pager_setsize(vp, length);
2009 * Invalidate the cached pages of a file's buffer within the range of block
2010 * numbers [startlbn, endlbn).
2013 v_inval_buf_range(struct vnode *vp, daddr_t startlbn, daddr_t endlbn,
2019 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range");
2021 start = blksize * startlbn;
2022 end = blksize * endlbn;
2026 MPASS(blksize == bo->bo_bsize);
2028 while (v_inval_buf_range_locked(vp, bo, startlbn, endlbn) == EAGAIN)
2032 vn_pages_remove(vp, OFF_TO_IDX(start), OFF_TO_IDX(end + PAGE_SIZE - 1));
2036 v_inval_buf_range_locked(struct vnode *vp, struct bufobj *bo,
2037 daddr_t startlbn, daddr_t endlbn)
2039 struct buf *bp, *nbp;
2042 ASSERT_VOP_LOCKED(vp, "v_inval_buf_range_locked");
2043 ASSERT_BO_LOCKED(bo);
2047 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
2048 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2051 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2052 BO_LOCKPTR(bo)) == ENOLCK) {
2058 bp->b_flags |= B_INVAL | B_RELBUF;
2059 bp->b_flags &= ~B_ASYNC;
2065 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
2067 (nbp->b_flags & B_DELWRI) != 0))
2071 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
2072 if (bp->b_lblkno < startlbn || bp->b_lblkno >= endlbn)
2075 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
2076 BO_LOCKPTR(bo)) == ENOLCK) {
2081 bp->b_flags |= B_INVAL | B_RELBUF;
2082 bp->b_flags &= ~B_ASYNC;
2088 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
2089 (nbp->b_vp != vp) ||
2090 (nbp->b_flags & B_DELWRI) == 0))
2098 buf_vlist_remove(struct buf *bp)
2102 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
2103 ASSERT_BO_WLOCKED(bp->b_bufobj);
2104 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
2105 (BX_VNDIRTY|BX_VNCLEAN),
2106 ("buf_vlist_remove: Buf %p is on two lists", bp));
2107 if (bp->b_xflags & BX_VNDIRTY)
2108 bv = &bp->b_bufobj->bo_dirty;
2110 bv = &bp->b_bufobj->bo_clean;
2111 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
2112 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
2114 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
2118 * Add the buffer to the sorted clean or dirty block list.
2120 * NOTE: xflags is passed as a constant, optimizing this inline function!
2123 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
2129 ASSERT_BO_WLOCKED(bo);
2130 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
2131 ("dead bo %p", bo));
2132 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
2133 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
2134 bp->b_xflags |= xflags;
2135 if (xflags & BX_VNDIRTY)
2141 * Keep the list ordered. Optimize empty list insertion. Assume
2142 * we tend to grow at the tail so lookup_le should usually be cheaper
2145 if (bv->bv_cnt == 0 ||
2146 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
2147 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
2148 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
2149 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
2151 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
2152 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
2154 panic("buf_vlist_add: Preallocated nodes insufficient.");
2159 * Look up a buffer using the buffer tries.
2162 gbincore(struct bufobj *bo, daddr_t lblkno)
2166 ASSERT_BO_LOCKED(bo);
2167 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
2170 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
2174 * Associate a buffer with a vnode.
2177 bgetvp(struct vnode *vp, struct buf *bp)
2182 ASSERT_BO_WLOCKED(bo);
2183 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
2185 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
2186 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
2187 ("bgetvp: bp already attached! %p", bp));
2193 * Insert onto list for new vnode.
2195 buf_vlist_add(bp, bo, BX_VNCLEAN);
2199 * Disassociate a buffer from a vnode.
2202 brelvp(struct buf *bp)
2207 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2208 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
2211 * Delete from old vnode list, if on one.
2213 vp = bp->b_vp; /* XXX */
2216 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2217 buf_vlist_remove(bp);
2219 panic("brelvp: Buffer %p not on queue.", bp);
2220 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2221 bo->bo_flag &= ~BO_ONWORKLST;
2222 mtx_lock(&sync_mtx);
2223 LIST_REMOVE(bo, bo_synclist);
2224 syncer_worklist_len--;
2225 mtx_unlock(&sync_mtx);
2228 bp->b_bufobj = NULL;
2234 * Add an item to the syncer work queue.
2237 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
2241 ASSERT_BO_WLOCKED(bo);
2243 mtx_lock(&sync_mtx);
2244 if (bo->bo_flag & BO_ONWORKLST)
2245 LIST_REMOVE(bo, bo_synclist);
2247 bo->bo_flag |= BO_ONWORKLST;
2248 syncer_worklist_len++;
2251 if (delay > syncer_maxdelay - 2)
2252 delay = syncer_maxdelay - 2;
2253 slot = (syncer_delayno + delay) & syncer_mask;
2255 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
2256 mtx_unlock(&sync_mtx);
2260 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
2264 mtx_lock(&sync_mtx);
2265 len = syncer_worklist_len - sync_vnode_count;
2266 mtx_unlock(&sync_mtx);
2267 error = SYSCTL_OUT(req, &len, sizeof(len));
2271 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
2272 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
2274 static struct proc *updateproc;
2275 static void sched_sync(void);
2276 static struct kproc_desc up_kp = {
2281 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
2284 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
2289 *bo = LIST_FIRST(slp);
2293 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
2296 * We use vhold in case the vnode does not
2297 * successfully sync. vhold prevents the vnode from
2298 * going away when we unlock the sync_mtx so that
2299 * we can acquire the vnode interlock.
2302 mtx_unlock(&sync_mtx);
2304 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2306 mtx_lock(&sync_mtx);
2307 return (*bo == LIST_FIRST(slp));
2309 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2310 (void) VOP_FSYNC(vp, MNT_LAZY, td);
2312 vn_finished_write(mp);
2314 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
2316 * Put us back on the worklist. The worklist
2317 * routine will remove us from our current
2318 * position and then add us back in at a later
2321 vn_syncer_add_to_worklist(*bo, syncdelay);
2325 mtx_lock(&sync_mtx);
2329 static int first_printf = 1;
2332 * System filesystem synchronizer daemon.
2337 struct synclist *next, *slp;
2340 struct thread *td = curthread;
2342 int net_worklist_len;
2343 int syncer_final_iter;
2347 syncer_final_iter = 0;
2348 syncer_state = SYNCER_RUNNING;
2349 starttime = time_uptime;
2350 td->td_pflags |= TDP_NORUNNINGBUF;
2352 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
2355 mtx_lock(&sync_mtx);
2357 if (syncer_state == SYNCER_FINAL_DELAY &&
2358 syncer_final_iter == 0) {
2359 mtx_unlock(&sync_mtx);
2360 kproc_suspend_check(td->td_proc);
2361 mtx_lock(&sync_mtx);
2363 net_worklist_len = syncer_worklist_len - sync_vnode_count;
2364 if (syncer_state != SYNCER_RUNNING &&
2365 starttime != time_uptime) {
2367 printf("\nSyncing disks, vnodes remaining... ");
2370 printf("%d ", net_worklist_len);
2372 starttime = time_uptime;
2375 * Push files whose dirty time has expired. Be careful
2376 * of interrupt race on slp queue.
2378 * Skip over empty worklist slots when shutting down.
2381 slp = &syncer_workitem_pending[syncer_delayno];
2382 syncer_delayno += 1;
2383 if (syncer_delayno == syncer_maxdelay)
2385 next = &syncer_workitem_pending[syncer_delayno];
2387 * If the worklist has wrapped since the
2388 * it was emptied of all but syncer vnodes,
2389 * switch to the FINAL_DELAY state and run
2390 * for one more second.
2392 if (syncer_state == SYNCER_SHUTTING_DOWN &&
2393 net_worklist_len == 0 &&
2394 last_work_seen == syncer_delayno) {
2395 syncer_state = SYNCER_FINAL_DELAY;
2396 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
2398 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
2399 syncer_worklist_len > 0);
2402 * Keep track of the last time there was anything
2403 * on the worklist other than syncer vnodes.
2404 * Return to the SHUTTING_DOWN state if any
2407 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
2408 last_work_seen = syncer_delayno;
2409 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
2410 syncer_state = SYNCER_SHUTTING_DOWN;
2411 while (!LIST_EMPTY(slp)) {
2412 error = sync_vnode(slp, &bo, td);
2414 LIST_REMOVE(bo, bo_synclist);
2415 LIST_INSERT_HEAD(next, bo, bo_synclist);
2419 if (first_printf == 0) {
2421 * Drop the sync mutex, because some watchdog
2422 * drivers need to sleep while patting
2424 mtx_unlock(&sync_mtx);
2425 wdog_kern_pat(WD_LASTVAL);
2426 mtx_lock(&sync_mtx);
2430 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
2431 syncer_final_iter--;
2433 * The variable rushjob allows the kernel to speed up the
2434 * processing of the filesystem syncer process. A rushjob
2435 * value of N tells the filesystem syncer to process the next
2436 * N seconds worth of work on its queue ASAP. Currently rushjob
2437 * is used by the soft update code to speed up the filesystem
2438 * syncer process when the incore state is getting so far
2439 * ahead of the disk that the kernel memory pool is being
2440 * threatened with exhaustion.
2447 * Just sleep for a short period of time between
2448 * iterations when shutting down to allow some I/O
2451 * If it has taken us less than a second to process the
2452 * current work, then wait. Otherwise start right over
2453 * again. We can still lose time if any single round
2454 * takes more than two seconds, but it does not really
2455 * matter as we are just trying to generally pace the
2456 * filesystem activity.
2458 if (syncer_state != SYNCER_RUNNING ||
2459 time_uptime == starttime) {
2461 sched_prio(td, PPAUSE);
2464 if (syncer_state != SYNCER_RUNNING)
2465 cv_timedwait(&sync_wakeup, &sync_mtx,
2466 hz / SYNCER_SHUTDOWN_SPEEDUP);
2467 else if (time_uptime == starttime)
2468 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2473 * Request the syncer daemon to speed up its work.
2474 * We never push it to speed up more than half of its
2475 * normal turn time, otherwise it could take over the cpu.
2478 speedup_syncer(void)
2482 mtx_lock(&sync_mtx);
2483 if (rushjob < syncdelay / 2) {
2485 stat_rush_requests += 1;
2488 mtx_unlock(&sync_mtx);
2489 cv_broadcast(&sync_wakeup);
2494 * Tell the syncer to speed up its work and run though its work
2495 * list several times, then tell it to shut down.
2498 syncer_shutdown(void *arg, int howto)
2501 if (howto & RB_NOSYNC)
2503 mtx_lock(&sync_mtx);
2504 syncer_state = SYNCER_SHUTTING_DOWN;
2506 mtx_unlock(&sync_mtx);
2507 cv_broadcast(&sync_wakeup);
2508 kproc_shutdown(arg, howto);
2512 syncer_suspend(void)
2515 syncer_shutdown(updateproc, 0);
2522 mtx_lock(&sync_mtx);
2524 syncer_state = SYNCER_RUNNING;
2525 mtx_unlock(&sync_mtx);
2526 cv_broadcast(&sync_wakeup);
2527 kproc_resume(updateproc);
2531 * Reassign a buffer from one vnode to another.
2532 * Used to assign file specific control information
2533 * (indirect blocks) to the vnode to which they belong.
2536 reassignbuf(struct buf *bp)
2549 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2550 bp, bp->b_vp, bp->b_flags);
2552 * B_PAGING flagged buffers cannot be reassigned because their vp
2553 * is not fully linked in.
2555 if (bp->b_flags & B_PAGING)
2556 panic("cannot reassign paging buffer");
2559 * Delete from old vnode list, if on one.
2562 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2563 buf_vlist_remove(bp);
2565 panic("reassignbuf: Buffer %p not on queue.", bp);
2567 * If dirty, put on list of dirty buffers; otherwise insert onto list
2570 if (bp->b_flags & B_DELWRI) {
2571 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2572 switch (vp->v_type) {
2582 vn_syncer_add_to_worklist(bo, delay);
2584 buf_vlist_add(bp, bo, BX_VNDIRTY);
2586 buf_vlist_add(bp, bo, BX_VNCLEAN);
2588 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2589 mtx_lock(&sync_mtx);
2590 LIST_REMOVE(bo, bo_synclist);
2591 syncer_worklist_len--;
2592 mtx_unlock(&sync_mtx);
2593 bo->bo_flag &= ~BO_ONWORKLST;
2598 bp = TAILQ_FIRST(&bv->bv_hd);
2599 KASSERT(bp == NULL || bp->b_bufobj == bo,
2600 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2601 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2602 KASSERT(bp == NULL || bp->b_bufobj == bo,
2603 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2605 bp = TAILQ_FIRST(&bv->bv_hd);
2606 KASSERT(bp == NULL || bp->b_bufobj == bo,
2607 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2608 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2609 KASSERT(bp == NULL || bp->b_bufobj == bo,
2610 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2616 v_init_counters(struct vnode *vp)
2619 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2620 vp, ("%s called for an initialized vnode", __FUNCTION__));
2621 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2623 refcount_init(&vp->v_holdcnt, 1);
2624 refcount_init(&vp->v_usecount, 1);
2628 v_incr_usecount_locked(struct vnode *vp)
2631 ASSERT_VI_LOCKED(vp, __func__);
2632 if ((vp->v_iflag & VI_OWEINACT) != 0) {
2633 VNASSERT(vp->v_usecount == 0, vp,
2634 ("vnode with usecount and VI_OWEINACT set"));
2635 vp->v_iflag &= ~VI_OWEINACT;
2637 refcount_acquire(&vp->v_usecount);
2638 v_incr_devcount(vp);
2642 * Increment the use count on the vnode, taking care to reference
2643 * the driver's usecount if this is a chardev.
2646 v_incr_usecount(struct vnode *vp)
2649 ASSERT_VI_UNLOCKED(vp, __func__);
2650 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2652 if (vp->v_type != VCHR &&
2653 refcount_acquire_if_not_zero(&vp->v_usecount)) {
2654 VNODE_REFCOUNT_FENCE_ACQ();
2655 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2656 ("vnode with usecount and VI_OWEINACT set"));
2659 v_incr_usecount_locked(vp);
2665 * Increment si_usecount of the associated device, if any.
2668 v_incr_devcount(struct vnode *vp)
2671 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2672 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2674 vp->v_rdev->si_usecount++;
2680 * Decrement si_usecount of the associated device, if any.
2683 v_decr_devcount(struct vnode *vp)
2686 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2687 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2689 vp->v_rdev->si_usecount--;
2695 * Grab a particular vnode from the free list, increment its
2696 * reference count and lock it. VI_DOOMED is set if the vnode
2697 * is being destroyed. Only callers who specify LK_RETRY will
2698 * see doomed vnodes. If inactive processing was delayed in
2699 * vput try to do it here.
2701 * Notes on lockless counter manipulation:
2702 * _vhold, vputx and other routines make various decisions based
2703 * on either holdcnt or usecount being 0. As long as either counter
2704 * is not transitioning 0->1 nor 1->0, the manipulation can be done
2705 * with atomic operations. Otherwise the interlock is taken covering
2706 * both the atomic and additional actions.
2709 vget(struct vnode *vp, int flags, struct thread *td)
2711 int error, oweinact;
2713 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2714 ("vget: invalid lock operation"));
2716 if ((flags & LK_INTERLOCK) != 0)
2717 ASSERT_VI_LOCKED(vp, __func__);
2719 ASSERT_VI_UNLOCKED(vp, __func__);
2720 if ((flags & LK_VNHELD) != 0)
2721 VNASSERT((vp->v_holdcnt > 0), vp,
2722 ("vget: LK_VNHELD passed but vnode not held"));
2724 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2726 if ((flags & LK_VNHELD) == 0)
2727 _vhold(vp, (flags & LK_INTERLOCK) != 0);
2729 if ((error = vn_lock(vp, flags)) != 0) {
2731 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2735 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2736 panic("vget: vn_lock failed to return ENOENT\n");
2738 * We don't guarantee that any particular close will
2739 * trigger inactive processing so just make a best effort
2740 * here at preventing a reference to a removed file. If
2741 * we don't succeed no harm is done.
2743 * Upgrade our holdcnt to a usecount.
2745 if (vp->v_type == VCHR ||
2746 !refcount_acquire_if_not_zero(&vp->v_usecount)) {
2748 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2752 vp->v_iflag &= ~VI_OWEINACT;
2753 VNODE_REFCOUNT_FENCE_REL();
2755 refcount_acquire(&vp->v_usecount);
2756 v_incr_devcount(vp);
2757 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2758 (flags & LK_NOWAIT) == 0)
2766 * Increase the reference (use) and hold count of a vnode.
2767 * This will also remove the vnode from the free list if it is presently free.
2770 vref(struct vnode *vp)
2773 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2775 v_incr_usecount(vp);
2779 vrefl(struct vnode *vp)
2782 ASSERT_VI_LOCKED(vp, __func__);
2783 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2785 v_incr_usecount_locked(vp);
2789 vrefact(struct vnode *vp)
2792 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2793 if (__predict_false(vp->v_type == VCHR)) {
2794 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp,
2795 ("%s: wrong ref counts", __func__));
2800 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
2801 VNASSERT(old > 0, vp, ("%s: wrong hold count", __func__));
2802 old = atomic_fetchadd_int(&vp->v_usecount, 1);
2803 VNASSERT(old > 0, vp, ("%s: wrong use count", __func__));
2805 refcount_acquire(&vp->v_holdcnt);
2806 refcount_acquire(&vp->v_usecount);
2811 * Return reference count of a vnode.
2813 * The results of this call are only guaranteed when some mechanism is used to
2814 * stop other processes from gaining references to the vnode. This may be the
2815 * case if the caller holds the only reference. This is also useful when stale
2816 * data is acceptable as race conditions may be accounted for by some other
2820 vrefcnt(struct vnode *vp)
2823 return (vp->v_usecount);
2826 #define VPUTX_VRELE 1
2827 #define VPUTX_VPUT 2
2828 #define VPUTX_VUNREF 3
2831 * Decrement the use and hold counts for a vnode.
2833 * See an explanation near vget() as to why atomic operation is safe.
2836 vputx(struct vnode *vp, int func)
2840 KASSERT(vp != NULL, ("vputx: null vp"));
2841 if (func == VPUTX_VUNREF)
2842 ASSERT_VOP_LOCKED(vp, "vunref");
2843 else if (func == VPUTX_VPUT)
2844 ASSERT_VOP_LOCKED(vp, "vput");
2846 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2847 ASSERT_VI_UNLOCKED(vp, __func__);
2848 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2850 if (vp->v_type != VCHR &&
2851 refcount_release_if_not_last(&vp->v_usecount)) {
2852 if (func == VPUTX_VPUT)
2861 * We want to hold the vnode until the inactive finishes to
2862 * prevent vgone() races. We drop the use count here and the
2863 * hold count below when we're done.
2865 if (!refcount_release(&vp->v_usecount) ||
2866 (vp->v_iflag & VI_DOINGINACT)) {
2867 if (func == VPUTX_VPUT)
2869 v_decr_devcount(vp);
2874 v_decr_devcount(vp);
2878 if (vp->v_usecount != 0) {
2879 vn_printf(vp, "vputx: usecount not zero for vnode ");
2880 panic("vputx: usecount not zero");
2883 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2886 * We must call VOP_INACTIVE with the node locked. Mark
2887 * as VI_DOINGINACT to avoid recursion.
2889 vp->v_iflag |= VI_OWEINACT;
2892 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2896 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2897 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2903 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2904 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2909 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp,
2910 ("vnode with usecount and VI_OWEINACT set"));
2912 if (vp->v_iflag & VI_OWEINACT)
2913 vinactive(vp, curthread);
2914 if (func != VPUTX_VUNREF)
2921 * Vnode put/release.
2922 * If count drops to zero, call inactive routine and return to freelist.
2925 vrele(struct vnode *vp)
2928 vputx(vp, VPUTX_VRELE);
2932 * Release an already locked vnode. This give the same effects as
2933 * unlock+vrele(), but takes less time and avoids releasing and
2934 * re-aquiring the lock (as vrele() acquires the lock internally.)
2937 vput(struct vnode *vp)
2940 vputx(vp, VPUTX_VPUT);
2944 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2947 vunref(struct vnode *vp)
2950 vputx(vp, VPUTX_VUNREF);
2954 * Increase the hold count and activate if this is the first reference.
2957 _vhold(struct vnode *vp, bool locked)
2962 ASSERT_VI_LOCKED(vp, __func__);
2964 ASSERT_VI_UNLOCKED(vp, __func__);
2965 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2967 if (refcount_acquire_if_not_zero(&vp->v_holdcnt)) {
2968 VNODE_REFCOUNT_FENCE_ACQ();
2969 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2970 ("_vhold: vnode with holdcnt is free"));
2975 if ((vp->v_iflag & VI_FREE) == 0) {
2976 refcount_acquire(&vp->v_holdcnt);
2981 VNASSERT(vp->v_holdcnt == 0, vp,
2982 ("%s: wrong hold count", __func__));
2983 VNASSERT(vp->v_op != NULL, vp,
2984 ("%s: vnode already reclaimed.", __func__));
2986 * Remove a vnode from the free list, mark it as in use,
2987 * and put it on the active list.
2989 VNASSERT(vp->v_mount != NULL, vp,
2990 ("_vhold: vnode not on per mount vnode list"));
2992 mtx_lock(&mp->mnt_listmtx);
2993 if ((vp->v_mflag & VMP_TMPMNTFREELIST) != 0) {
2994 TAILQ_REMOVE(&mp->mnt_tmpfreevnodelist, vp, v_actfreelist);
2995 mp->mnt_tmpfreevnodelistsize--;
2996 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
2998 mtx_lock(&vnode_free_list_mtx);
2999 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
3001 mtx_unlock(&vnode_free_list_mtx);
3003 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
3004 ("Activating already active vnode"));
3005 vp->v_iflag &= ~VI_FREE;
3006 vp->v_iflag |= VI_ACTIVE;
3007 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
3008 mp->mnt_activevnodelistsize++;
3009 mtx_unlock(&mp->mnt_listmtx);
3010 refcount_acquire(&vp->v_holdcnt);
3016 * Drop the hold count of the vnode. If this is the last reference to
3017 * the vnode we place it on the free list unless it has been vgone'd
3018 * (marked VI_DOOMED) in which case we will free it.
3020 * Because the vnode vm object keeps a hold reference on the vnode if
3021 * there is at least one resident non-cached page, the vnode cannot
3022 * leave the active list without the page cleanup done.
3025 _vdrop(struct vnode *vp, bool locked)
3032 ASSERT_VI_LOCKED(vp, __func__);
3034 ASSERT_VI_UNLOCKED(vp, __func__);
3035 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3036 if ((int)vp->v_holdcnt <= 0)
3037 panic("vdrop: holdcnt %d", vp->v_holdcnt);
3039 if (refcount_release_if_not_last(&vp->v_holdcnt))
3043 if (refcount_release(&vp->v_holdcnt) == 0) {
3047 if ((vp->v_iflag & VI_DOOMED) == 0) {
3049 * Mark a vnode as free: remove it from its active list
3050 * and put it up for recycling on the freelist.
3052 VNASSERT(vp->v_op != NULL, vp,
3053 ("vdropl: vnode already reclaimed."));
3054 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
3055 ("vnode already free"));
3056 VNASSERT(vp->v_holdcnt == 0, vp,
3057 ("vdropl: freeing when we shouldn't"));
3058 active = vp->v_iflag & VI_ACTIVE;
3059 if ((vp->v_iflag & VI_OWEINACT) == 0) {
3060 vp->v_iflag &= ~VI_ACTIVE;
3063 mtx_lock(&mp->mnt_listmtx);
3065 TAILQ_REMOVE(&mp->mnt_activevnodelist,
3067 mp->mnt_activevnodelistsize--;
3069 TAILQ_INSERT_TAIL(&mp->mnt_tmpfreevnodelist,
3071 mp->mnt_tmpfreevnodelistsize++;
3072 vp->v_iflag |= VI_FREE;
3073 vp->v_mflag |= VMP_TMPMNTFREELIST;
3075 if (mp->mnt_tmpfreevnodelistsize >=
3076 mnt_free_list_batch)
3077 vnlru_return_batch_locked(mp);
3078 mtx_unlock(&mp->mnt_listmtx);
3080 VNASSERT(active == 0, vp,
3081 ("vdropl: active vnode not on per mount "
3083 mtx_lock(&vnode_free_list_mtx);
3084 TAILQ_INSERT_TAIL(&vnode_free_list, vp,
3087 vp->v_iflag |= VI_FREE;
3089 mtx_unlock(&vnode_free_list_mtx);
3093 counter_u64_add(free_owe_inact, 1);
3098 * The vnode has been marked for destruction, so free it.
3100 * The vnode will be returned to the zone where it will
3101 * normally remain until it is needed for another vnode. We
3102 * need to cleanup (or verify that the cleanup has already
3103 * been done) any residual data left from its current use
3104 * so as not to contaminate the freshly allocated vnode.
3106 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
3107 atomic_subtract_long(&numvnodes, 1);
3109 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
3110 ("cleaned vnode still on the free list."));
3111 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
3112 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
3113 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
3114 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
3115 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
3116 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
3117 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
3118 ("clean blk trie not empty"));
3119 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
3120 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
3121 ("dirty blk trie not empty"));
3122 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
3123 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
3124 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
3125 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp,
3126 ("Dangling rangelock waiters"));
3129 mac_vnode_destroy(vp);
3131 if (vp->v_pollinfo != NULL) {
3132 destroy_vpollinfo(vp->v_pollinfo);
3133 vp->v_pollinfo = NULL;
3136 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
3139 vp->v_mountedhere = NULL;
3142 vp->v_fifoinfo = NULL;
3143 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
3147 uma_zfree(vnode_zone, vp);
3151 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
3152 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
3153 * OWEINACT tracks whether a vnode missed a call to inactive due to a
3154 * failed lock upgrade.
3157 vinactive(struct vnode *vp, struct thread *td)
3159 struct vm_object *obj;
3161 ASSERT_VOP_ELOCKED(vp, "vinactive");
3162 ASSERT_VI_LOCKED(vp, "vinactive");
3163 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
3164 ("vinactive: recursed on VI_DOINGINACT"));
3165 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3166 vp->v_iflag |= VI_DOINGINACT;
3167 vp->v_iflag &= ~VI_OWEINACT;
3170 * Before moving off the active list, we must be sure that any
3171 * modified pages are converted into the vnode's dirty
3172 * buffers, since these will no longer be checked once the
3173 * vnode is on the inactive list.
3175 * The write-out of the dirty pages is asynchronous. At the
3176 * point that VOP_INACTIVE() is called, there could still be
3177 * pending I/O and dirty pages in the object.
3179 if ((obj = vp->v_object) != NULL && (vp->v_vflag & VV_NOSYNC) == 0 &&
3180 (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
3181 VM_OBJECT_WLOCK(obj);
3182 vm_object_page_clean(obj, 0, 0, 0);
3183 VM_OBJECT_WUNLOCK(obj);
3185 VOP_INACTIVE(vp, td);
3187 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
3188 ("vinactive: lost VI_DOINGINACT"));
3189 vp->v_iflag &= ~VI_DOINGINACT;
3193 * Remove any vnodes in the vnode table belonging to mount point mp.
3195 * If FORCECLOSE is not specified, there should not be any active ones,
3196 * return error if any are found (nb: this is a user error, not a
3197 * system error). If FORCECLOSE is specified, detach any active vnodes
3200 * If WRITECLOSE is set, only flush out regular file vnodes open for
3203 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
3205 * `rootrefs' specifies the base reference count for the root vnode
3206 * of this filesystem. The root vnode is considered busy if its
3207 * v_usecount exceeds this value. On a successful return, vflush(, td)
3208 * will call vrele() on the root vnode exactly rootrefs times.
3209 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
3213 static int busyprt = 0; /* print out busy vnodes */
3214 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
3218 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
3220 struct vnode *vp, *mvp, *rootvp = NULL;
3222 int busy = 0, error;
3224 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
3227 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
3228 ("vflush: bad args"));
3230 * Get the filesystem root vnode. We can vput() it
3231 * immediately, since with rootrefs > 0, it won't go away.
3233 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
3234 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
3241 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
3243 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
3246 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3250 * Skip over a vnodes marked VV_SYSTEM.
3252 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
3258 * If WRITECLOSE is set, flush out unlinked but still open
3259 * files (even if open only for reading) and regular file
3260 * vnodes open for writing.
3262 if (flags & WRITECLOSE) {
3263 if (vp->v_object != NULL) {
3264 VM_OBJECT_WLOCK(vp->v_object);
3265 vm_object_page_clean(vp->v_object, 0, 0, 0);
3266 VM_OBJECT_WUNLOCK(vp->v_object);
3268 error = VOP_FSYNC(vp, MNT_WAIT, td);
3272 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3275 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3278 if ((vp->v_type == VNON ||
3279 (error == 0 && vattr.va_nlink > 0)) &&
3280 (vp->v_writecount <= 0 || vp->v_type != VREG)) {
3288 * With v_usecount == 0, all we need to do is clear out the
3289 * vnode data structures and we are done.
3291 * If FORCECLOSE is set, forcibly close the vnode.
3293 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
3299 vn_printf(vp, "vflush: busy vnode ");
3305 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
3307 * If just the root vnode is busy, and if its refcount
3308 * is equal to `rootrefs', then go ahead and kill it.
3311 KASSERT(busy > 0, ("vflush: not busy"));
3312 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
3313 ("vflush: usecount %d < rootrefs %d",
3314 rootvp->v_usecount, rootrefs));
3315 if (busy == 1 && rootvp->v_usecount == rootrefs) {
3316 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
3318 VOP_UNLOCK(rootvp, 0);
3324 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
3328 for (; rootrefs > 0; rootrefs--)
3334 * Recycle an unused vnode to the front of the free list.
3337 vrecycle(struct vnode *vp)
3342 recycled = vrecyclel(vp);
3348 * vrecycle, with the vp interlock held.
3351 vrecyclel(struct vnode *vp)
3355 ASSERT_VOP_ELOCKED(vp, __func__);
3356 ASSERT_VI_LOCKED(vp, __func__);
3357 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3359 if (vp->v_usecount == 0) {
3367 * Eliminate all activity associated with a vnode
3368 * in preparation for reuse.
3371 vgone(struct vnode *vp)
3379 notify_lowervp_vfs_dummy(struct mount *mp __unused,
3380 struct vnode *lowervp __unused)
3385 * Notify upper mounts about reclaimed or unlinked vnode.
3388 vfs_notify_upper(struct vnode *vp, int event)
3390 static struct vfsops vgonel_vfsops = {
3391 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
3392 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
3394 struct mount *mp, *ump, *mmp;
3401 if (TAILQ_EMPTY(&mp->mnt_uppers))
3404 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
3405 mmp->mnt_op = &vgonel_vfsops;
3406 mmp->mnt_kern_flag |= MNTK_MARKER;
3408 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
3409 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
3410 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
3411 ump = TAILQ_NEXT(ump, mnt_upper_link);
3414 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
3417 case VFS_NOTIFY_UPPER_RECLAIM:
3418 VFS_RECLAIM_LOWERVP(ump, vp);
3420 case VFS_NOTIFY_UPPER_UNLINK:
3421 VFS_UNLINK_LOWERVP(ump, vp);
3424 KASSERT(0, ("invalid event %d", event));
3428 ump = TAILQ_NEXT(mmp, mnt_upper_link);
3429 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
3432 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
3433 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
3434 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
3435 wakeup(&mp->mnt_uppers);
3442 * vgone, with the vp interlock held.
3445 vgonel(struct vnode *vp)
3452 ASSERT_VOP_ELOCKED(vp, "vgonel");
3453 ASSERT_VI_LOCKED(vp, "vgonel");
3454 VNASSERT(vp->v_holdcnt, vp,
3455 ("vgonel: vp %p has no reference.", vp));
3456 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3460 * Don't vgonel if we're already doomed.
3462 if (vp->v_iflag & VI_DOOMED)
3464 vp->v_iflag |= VI_DOOMED;
3467 * Check to see if the vnode is in use. If so, we have to call
3468 * VOP_CLOSE() and VOP_INACTIVE().
3470 active = vp->v_usecount;
3471 oweinact = (vp->v_iflag & VI_OWEINACT);
3473 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
3476 * If purging an active vnode, it must be closed and
3477 * deactivated before being reclaimed.
3480 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
3481 if (oweinact || active) {
3483 if ((vp->v_iflag & VI_DOINGINACT) == 0)
3487 if (vp->v_type == VSOCK)
3488 vfs_unp_reclaim(vp);
3491 * Clean out any buffers associated with the vnode.
3492 * If the flush fails, just toss the buffers.
3495 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
3496 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
3497 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
3498 while (vinvalbuf(vp, 0, 0, 0) != 0)
3502 BO_LOCK(&vp->v_bufobj);
3503 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
3504 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3505 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
3506 vp->v_bufobj.bo_clean.bv_cnt == 0,
3507 ("vp %p bufobj not invalidated", vp));
3510 * For VMIO bufobj, BO_DEAD is set in vm_object_terminate()
3511 * after the object's page queue is flushed.
3513 if (vp->v_bufobj.bo_object == NULL)
3514 vp->v_bufobj.bo_flag |= BO_DEAD;
3515 BO_UNLOCK(&vp->v_bufobj);
3518 * Reclaim the vnode.
3520 if (VOP_RECLAIM(vp, td))
3521 panic("vgone: cannot reclaim");
3523 vn_finished_secondary_write(mp);
3524 VNASSERT(vp->v_object == NULL, vp,
3525 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
3527 * Clear the advisory locks and wake up waiting threads.
3529 (void)VOP_ADVLOCKPURGE(vp);
3532 * Delete from old mount point vnode list.
3537 * Done with purge, reset to the standard lock and invalidate
3541 vp->v_vnlock = &vp->v_lock;
3542 vp->v_op = &dead_vnodeops;
3548 * Calculate the total number of references to a special device.
3551 vcount(struct vnode *vp)
3556 count = vp->v_rdev->si_usecount;
3562 * Same as above, but using the struct cdev *as argument
3565 count_dev(struct cdev *dev)
3570 count = dev->si_usecount;
3576 * Print out a description of a vnode.
3578 static char *typename[] =
3579 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
3583 vn_printf(struct vnode *vp, const char *fmt, ...)
3586 char buf[256], buf2[16];
3592 printf("%p: ", (void *)vp);
3593 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
3594 printf(" usecount %d, writecount %d, refcount %d",
3595 vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
3596 switch (vp->v_type) {
3598 printf(" mountedhere %p\n", vp->v_mountedhere);
3601 printf(" rdev %p\n", vp->v_rdev);
3604 printf(" socket %p\n", vp->v_unpcb);
3607 printf(" fifoinfo %p\n", vp->v_fifoinfo);
3615 if (vp->v_vflag & VV_ROOT)
3616 strlcat(buf, "|VV_ROOT", sizeof(buf));
3617 if (vp->v_vflag & VV_ISTTY)
3618 strlcat(buf, "|VV_ISTTY", sizeof(buf));
3619 if (vp->v_vflag & VV_NOSYNC)
3620 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
3621 if (vp->v_vflag & VV_ETERNALDEV)
3622 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
3623 if (vp->v_vflag & VV_CACHEDLABEL)
3624 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
3625 if (vp->v_vflag & VV_COPYONWRITE)
3626 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
3627 if (vp->v_vflag & VV_SYSTEM)
3628 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
3629 if (vp->v_vflag & VV_PROCDEP)
3630 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
3631 if (vp->v_vflag & VV_NOKNOTE)
3632 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
3633 if (vp->v_vflag & VV_DELETED)
3634 strlcat(buf, "|VV_DELETED", sizeof(buf));
3635 if (vp->v_vflag & VV_MD)
3636 strlcat(buf, "|VV_MD", sizeof(buf));
3637 if (vp->v_vflag & VV_FORCEINSMQ)
3638 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
3639 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
3640 VV_CACHEDLABEL | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
3641 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
3643 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
3644 strlcat(buf, buf2, sizeof(buf));
3646 if (vp->v_iflag & VI_MOUNT)
3647 strlcat(buf, "|VI_MOUNT", sizeof(buf));
3648 if (vp->v_iflag & VI_DOOMED)
3649 strlcat(buf, "|VI_DOOMED", sizeof(buf));
3650 if (vp->v_iflag & VI_FREE)
3651 strlcat(buf, "|VI_FREE", sizeof(buf));
3652 if (vp->v_iflag & VI_ACTIVE)
3653 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3654 if (vp->v_iflag & VI_DOINGINACT)
3655 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3656 if (vp->v_iflag & VI_OWEINACT)
3657 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3658 if (vp->v_iflag & VI_TEXT_REF)
3659 strlcat(buf, "|VI_TEXT_REF", sizeof(buf));
3660 flags = vp->v_iflag & ~(VI_MOUNT | VI_DOOMED | VI_FREE |
3661 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT | VI_TEXT_REF);
3663 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3664 strlcat(buf, buf2, sizeof(buf));
3666 printf(" flags (%s)\n", buf + 1);
3667 if (mtx_owned(VI_MTX(vp)))
3668 printf(" VI_LOCKed");
3669 if (vp->v_object != NULL)
3670 printf(" v_object %p ref %d pages %d "
3671 "cleanbuf %d dirtybuf %d\n",
3672 vp->v_object, vp->v_object->ref_count,
3673 vp->v_object->resident_page_count,
3674 vp->v_bufobj.bo_clean.bv_cnt,
3675 vp->v_bufobj.bo_dirty.bv_cnt);
3677 lockmgr_printinfo(vp->v_vnlock);
3678 if (vp->v_data != NULL)
3684 * List all of the locked vnodes in the system.
3685 * Called when debugging the kernel.
3687 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3693 * Note: because this is DDB, we can't obey the locking semantics
3694 * for these structures, which means we could catch an inconsistent
3695 * state and dereference a nasty pointer. Not much to be done
3698 db_printf("Locked vnodes\n");
3699 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3700 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3701 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3702 vn_printf(vp, "vnode ");
3708 * Show details about the given vnode.
3710 DB_SHOW_COMMAND(vnode, db_show_vnode)
3716 vp = (struct vnode *)addr;
3717 vn_printf(vp, "vnode ");
3721 * Show details about the given mount point.
3723 DB_SHOW_COMMAND(mount, db_show_mount)
3734 /* No address given, print short info about all mount points. */
3735 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3736 db_printf("%p %s on %s (%s)\n", mp,
3737 mp->mnt_stat.f_mntfromname,
3738 mp->mnt_stat.f_mntonname,
3739 mp->mnt_stat.f_fstypename);
3743 db_printf("\nMore info: show mount <addr>\n");
3747 mp = (struct mount *)addr;
3748 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3749 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3752 mflags = mp->mnt_flag;
3753 #define MNT_FLAG(flag) do { \
3754 if (mflags & (flag)) { \
3755 if (buf[0] != '\0') \
3756 strlcat(buf, ", ", sizeof(buf)); \
3757 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3758 mflags &= ~(flag); \
3761 MNT_FLAG(MNT_RDONLY);
3762 MNT_FLAG(MNT_SYNCHRONOUS);
3763 MNT_FLAG(MNT_NOEXEC);
3764 MNT_FLAG(MNT_NOSUID);
3765 MNT_FLAG(MNT_NFS4ACLS);
3766 MNT_FLAG(MNT_UNION);
3767 MNT_FLAG(MNT_ASYNC);
3768 MNT_FLAG(MNT_SUIDDIR);
3769 MNT_FLAG(MNT_SOFTDEP);
3770 MNT_FLAG(MNT_NOSYMFOLLOW);
3771 MNT_FLAG(MNT_GJOURNAL);
3772 MNT_FLAG(MNT_MULTILABEL);
3774 MNT_FLAG(MNT_NOATIME);
3775 MNT_FLAG(MNT_NOCLUSTERR);
3776 MNT_FLAG(MNT_NOCLUSTERW);
3778 MNT_FLAG(MNT_EXRDONLY);
3779 MNT_FLAG(MNT_EXPORTED);
3780 MNT_FLAG(MNT_DEFEXPORTED);
3781 MNT_FLAG(MNT_EXPORTANON);
3782 MNT_FLAG(MNT_EXKERB);
3783 MNT_FLAG(MNT_EXPUBLIC);
3784 MNT_FLAG(MNT_LOCAL);
3785 MNT_FLAG(MNT_QUOTA);
3786 MNT_FLAG(MNT_ROOTFS);
3788 MNT_FLAG(MNT_IGNORE);
3789 MNT_FLAG(MNT_UPDATE);
3790 MNT_FLAG(MNT_DELEXPORT);
3791 MNT_FLAG(MNT_RELOAD);
3792 MNT_FLAG(MNT_FORCE);
3793 MNT_FLAG(MNT_SNAPSHOT);
3794 MNT_FLAG(MNT_BYFSID);
3798 strlcat(buf, ", ", sizeof(buf));
3799 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3800 "0x%016jx", mflags);
3802 db_printf(" mnt_flag = %s\n", buf);
3805 flags = mp->mnt_kern_flag;
3806 #define MNT_KERN_FLAG(flag) do { \
3807 if (flags & (flag)) { \
3808 if (buf[0] != '\0') \
3809 strlcat(buf, ", ", sizeof(buf)); \
3810 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3814 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3815 MNT_KERN_FLAG(MNTK_ASYNC);
3816 MNT_KERN_FLAG(MNTK_SOFTDEP);
3817 MNT_KERN_FLAG(MNTK_DRAINING);
3818 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3819 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3820 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3821 MNT_KERN_FLAG(MNTK_NO_IOPF);
3822 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3823 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3824 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3825 MNT_KERN_FLAG(MNTK_MARKER);
3826 MNT_KERN_FLAG(MNTK_USES_BCACHE);
3827 MNT_KERN_FLAG(MNTK_NOASYNC);
3828 MNT_KERN_FLAG(MNTK_UNMOUNT);
3829 MNT_KERN_FLAG(MNTK_MWAIT);
3830 MNT_KERN_FLAG(MNTK_SUSPEND);
3831 MNT_KERN_FLAG(MNTK_SUSPEND2);
3832 MNT_KERN_FLAG(MNTK_SUSPENDED);
3833 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3834 MNT_KERN_FLAG(MNTK_NOKNOTE);
3835 #undef MNT_KERN_FLAG
3838 strlcat(buf, ", ", sizeof(buf));
3839 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3842 db_printf(" mnt_kern_flag = %s\n", buf);
3844 db_printf(" mnt_opt = ");
3845 opt = TAILQ_FIRST(mp->mnt_opt);
3847 db_printf("%s", opt->name);
3848 opt = TAILQ_NEXT(opt, link);
3849 while (opt != NULL) {
3850 db_printf(", %s", opt->name);
3851 opt = TAILQ_NEXT(opt, link);
3857 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3858 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3859 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3860 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3861 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3862 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3863 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3864 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3865 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3866 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3867 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3868 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3870 db_printf(" mnt_cred = { uid=%u ruid=%u",
3871 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3872 if (jailed(mp->mnt_cred))
3873 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3875 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3876 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3877 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3878 db_printf(" mnt_activevnodelistsize = %d\n",
3879 mp->mnt_activevnodelistsize);
3880 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3881 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3882 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3883 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3884 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3885 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3886 db_printf(" mnt_secondary_accwrites = %d\n",
3887 mp->mnt_secondary_accwrites);
3888 db_printf(" mnt_gjprovider = %s\n",
3889 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3891 db_printf("\n\nList of active vnodes\n");
3892 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3893 if (vp->v_type != VMARKER) {
3894 vn_printf(vp, "vnode ");
3899 db_printf("\n\nList of inactive vnodes\n");
3900 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3901 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3902 vn_printf(vp, "vnode ");
3911 * Fill in a struct xvfsconf based on a struct vfsconf.
3914 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3916 struct xvfsconf xvfsp;
3918 bzero(&xvfsp, sizeof(xvfsp));
3919 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3920 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3921 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3922 xvfsp.vfc_flags = vfsp->vfc_flags;
3924 * These are unused in userland, we keep them
3925 * to not break binary compatibility.
3927 xvfsp.vfc_vfsops = NULL;
3928 xvfsp.vfc_next = NULL;
3929 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3932 #ifdef COMPAT_FREEBSD32
3934 uint32_t vfc_vfsops;
3935 char vfc_name[MFSNAMELEN];
3936 int32_t vfc_typenum;
3937 int32_t vfc_refcount;
3943 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3945 struct xvfsconf32 xvfsp;
3947 bzero(&xvfsp, sizeof(xvfsp));
3948 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3949 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3950 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3951 xvfsp.vfc_flags = vfsp->vfc_flags;
3952 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3957 * Top level filesystem related information gathering.
3960 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3962 struct vfsconf *vfsp;
3967 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3968 #ifdef COMPAT_FREEBSD32
3969 if (req->flags & SCTL_MASK32)
3970 error = vfsconf2x32(req, vfsp);
3973 error = vfsconf2x(req, vfsp);
3981 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3982 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3983 "S,xvfsconf", "List of all configured filesystems");
3985 #ifndef BURN_BRIDGES
3986 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3989 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3991 int *name = (int *)arg1 - 1; /* XXX */
3992 u_int namelen = arg2 + 1; /* XXX */
3993 struct vfsconf *vfsp;
3995 log(LOG_WARNING, "userland calling deprecated sysctl, "
3996 "please rebuild world\n");
3998 #if 1 || defined(COMPAT_PRELITE2)
3999 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
4001 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
4005 case VFS_MAXTYPENUM:
4008 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
4011 return (ENOTDIR); /* overloaded */
4013 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4014 if (vfsp->vfc_typenum == name[2])
4019 return (EOPNOTSUPP);
4020 #ifdef COMPAT_FREEBSD32
4021 if (req->flags & SCTL_MASK32)
4022 return (vfsconf2x32(req, vfsp));
4025 return (vfsconf2x(req, vfsp));
4027 return (EOPNOTSUPP);
4030 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
4031 CTLFLAG_MPSAFE, vfs_sysctl,
4032 "Generic filesystem");
4034 #if 1 || defined(COMPAT_PRELITE2)
4037 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
4040 struct vfsconf *vfsp;
4041 struct ovfsconf ovfs;
4044 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
4045 bzero(&ovfs, sizeof(ovfs));
4046 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
4047 strcpy(ovfs.vfc_name, vfsp->vfc_name);
4048 ovfs.vfc_index = vfsp->vfc_typenum;
4049 ovfs.vfc_refcount = vfsp->vfc_refcount;
4050 ovfs.vfc_flags = vfsp->vfc_flags;
4051 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
4061 #endif /* 1 || COMPAT_PRELITE2 */
4062 #endif /* !BURN_BRIDGES */
4064 #define KINFO_VNODESLOP 10
4067 * Dump vnode list (via sysctl).
4071 sysctl_vnode(SYSCTL_HANDLER_ARGS)
4079 * Stale numvnodes access is not fatal here.
4082 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
4084 /* Make an estimate */
4085 return (SYSCTL_OUT(req, 0, len));
4087 error = sysctl_wire_old_buffer(req, 0);
4090 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
4092 mtx_lock(&mountlist_mtx);
4093 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
4094 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
4097 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
4101 xvn[n].xv_size = sizeof *xvn;
4102 xvn[n].xv_vnode = vp;
4103 xvn[n].xv_id = 0; /* XXX compat */
4104 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
4106 XV_COPY(writecount);
4112 xvn[n].xv_flag = vp->v_vflag;
4114 switch (vp->v_type) {
4121 if (vp->v_rdev == NULL) {
4125 xvn[n].xv_dev = dev2udev(vp->v_rdev);
4128 xvn[n].xv_socket = vp->v_socket;
4131 xvn[n].xv_fifo = vp->v_fifoinfo;
4136 /* shouldn't happen? */
4144 mtx_lock(&mountlist_mtx);
4149 mtx_unlock(&mountlist_mtx);
4151 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
4156 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
4157 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
4162 unmount_or_warn(struct mount *mp)
4166 error = dounmount(mp, MNT_FORCE, curthread);
4168 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
4172 printf("%d)\n", error);
4177 * Unmount all filesystems. The list is traversed in reverse order
4178 * of mounting to avoid dependencies.
4181 vfs_unmountall(void)
4183 struct mount *mp, *tmp;
4185 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
4188 * Since this only runs when rebooting, it is not interlocked.
4190 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
4194 * Forcibly unmounting "/dev" before "/" would prevent clean
4195 * unmount of the latter.
4197 if (mp == rootdevmp)
4200 unmount_or_warn(mp);
4203 if (rootdevmp != NULL)
4204 unmount_or_warn(rootdevmp);
4208 * perform msync on all vnodes under a mount point
4209 * the mount point must be locked.
4212 vfs_msync(struct mount *mp, int flags)
4214 struct vnode *vp, *mvp;
4215 struct vm_object *obj;
4217 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
4219 vnlru_return_batch(mp);
4221 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
4223 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
4224 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
4226 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
4228 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
4235 VM_OBJECT_WLOCK(obj);
4236 vm_object_page_clean(obj, 0, 0,
4238 OBJPC_SYNC : OBJPC_NOSYNC);
4239 VM_OBJECT_WUNLOCK(obj);
4249 destroy_vpollinfo_free(struct vpollinfo *vi)
4252 knlist_destroy(&vi->vpi_selinfo.si_note);
4253 mtx_destroy(&vi->vpi_lock);
4254 uma_zfree(vnodepoll_zone, vi);
4258 destroy_vpollinfo(struct vpollinfo *vi)
4261 knlist_clear(&vi->vpi_selinfo.si_note, 1);
4262 seldrain(&vi->vpi_selinfo);
4263 destroy_vpollinfo_free(vi);
4267 * Initialize per-vnode helper structure to hold poll-related state.
4270 v_addpollinfo(struct vnode *vp)
4272 struct vpollinfo *vi;
4274 if (vp->v_pollinfo != NULL)
4276 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
4277 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
4278 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
4279 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
4281 if (vp->v_pollinfo != NULL) {
4283 destroy_vpollinfo_free(vi);
4286 vp->v_pollinfo = vi;
4291 * Record a process's interest in events which might happen to
4292 * a vnode. Because poll uses the historic select-style interface
4293 * internally, this routine serves as both the ``check for any
4294 * pending events'' and the ``record my interest in future events''
4295 * functions. (These are done together, while the lock is held,
4296 * to avoid race conditions.)
4299 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
4303 mtx_lock(&vp->v_pollinfo->vpi_lock);
4304 if (vp->v_pollinfo->vpi_revents & events) {
4306 * This leaves events we are not interested
4307 * in available for the other process which
4308 * which presumably had requested them
4309 * (otherwise they would never have been
4312 events &= vp->v_pollinfo->vpi_revents;
4313 vp->v_pollinfo->vpi_revents &= ~events;
4315 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4318 vp->v_pollinfo->vpi_events |= events;
4319 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
4320 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4325 * Routine to create and manage a filesystem syncer vnode.
4327 #define sync_close ((int (*)(struct vop_close_args *))nullop)
4328 static int sync_fsync(struct vop_fsync_args *);
4329 static int sync_inactive(struct vop_inactive_args *);
4330 static int sync_reclaim(struct vop_reclaim_args *);
4332 static struct vop_vector sync_vnodeops = {
4333 .vop_bypass = VOP_EOPNOTSUPP,
4334 .vop_close = sync_close, /* close */
4335 .vop_fsync = sync_fsync, /* fsync */
4336 .vop_inactive = sync_inactive, /* inactive */
4337 .vop_reclaim = sync_reclaim, /* reclaim */
4338 .vop_lock1 = vop_stdlock, /* lock */
4339 .vop_unlock = vop_stdunlock, /* unlock */
4340 .vop_islocked = vop_stdislocked, /* islocked */
4344 * Create a new filesystem syncer vnode for the specified mount point.
4347 vfs_allocate_syncvnode(struct mount *mp)
4351 static long start, incr, next;
4354 /* Allocate a new vnode */
4355 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
4357 panic("vfs_allocate_syncvnode: getnewvnode() failed");
4359 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4360 vp->v_vflag |= VV_FORCEINSMQ;
4361 error = insmntque(vp, mp);
4363 panic("vfs_allocate_syncvnode: insmntque() failed");
4364 vp->v_vflag &= ~VV_FORCEINSMQ;
4367 * Place the vnode onto the syncer worklist. We attempt to
4368 * scatter them about on the list so that they will go off
4369 * at evenly distributed times even if all the filesystems
4370 * are mounted at once.
4373 if (next == 0 || next > syncer_maxdelay) {
4377 start = syncer_maxdelay / 2;
4378 incr = syncer_maxdelay;
4384 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
4385 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
4386 mtx_lock(&sync_mtx);
4388 if (mp->mnt_syncer == NULL) {
4389 mp->mnt_syncer = vp;
4392 mtx_unlock(&sync_mtx);
4395 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4402 vfs_deallocate_syncvnode(struct mount *mp)
4406 mtx_lock(&sync_mtx);
4407 vp = mp->mnt_syncer;
4409 mp->mnt_syncer = NULL;
4410 mtx_unlock(&sync_mtx);
4416 * Do a lazy sync of the filesystem.
4419 sync_fsync(struct vop_fsync_args *ap)
4421 struct vnode *syncvp = ap->a_vp;
4422 struct mount *mp = syncvp->v_mount;
4427 * We only need to do something if this is a lazy evaluation.
4429 if (ap->a_waitfor != MNT_LAZY)
4433 * Move ourselves to the back of the sync list.
4435 bo = &syncvp->v_bufobj;
4437 vn_syncer_add_to_worklist(bo, syncdelay);
4441 * Walk the list of vnodes pushing all that are dirty and
4442 * not already on the sync list.
4444 if (vfs_busy(mp, MBF_NOWAIT) != 0)
4446 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
4450 save = curthread_pflags_set(TDP_SYNCIO);
4451 vfs_msync(mp, MNT_NOWAIT);
4452 error = VFS_SYNC(mp, MNT_LAZY);
4453 curthread_pflags_restore(save);
4454 vn_finished_write(mp);
4460 * The syncer vnode is no referenced.
4463 sync_inactive(struct vop_inactive_args *ap)
4471 * The syncer vnode is no longer needed and is being decommissioned.
4473 * Modifications to the worklist must be protected by sync_mtx.
4476 sync_reclaim(struct vop_reclaim_args *ap)
4478 struct vnode *vp = ap->a_vp;
4483 mtx_lock(&sync_mtx);
4484 if (vp->v_mount->mnt_syncer == vp)
4485 vp->v_mount->mnt_syncer = NULL;
4486 if (bo->bo_flag & BO_ONWORKLST) {
4487 LIST_REMOVE(bo, bo_synclist);
4488 syncer_worklist_len--;
4490 bo->bo_flag &= ~BO_ONWORKLST;
4492 mtx_unlock(&sync_mtx);
4499 * Check if vnode represents a disk device
4502 vn_isdisk(struct vnode *vp, int *errp)
4506 if (vp->v_type != VCHR) {
4512 if (vp->v_rdev == NULL)
4514 else if (vp->v_rdev->si_devsw == NULL)
4516 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
4522 return (error == 0);
4526 * Common filesystem object access control check routine. Accepts a
4527 * vnode's type, "mode", uid and gid, requested access mode, credentials,
4528 * and optional call-by-reference privused argument allowing vaccess()
4529 * to indicate to the caller whether privilege was used to satisfy the
4530 * request (obsoleted). Returns 0 on success, or an errno on failure.
4533 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
4534 accmode_t accmode, struct ucred *cred, int *privused)
4536 accmode_t dac_granted;
4537 accmode_t priv_granted;
4539 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
4540 ("invalid bit in accmode"));
4541 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
4542 ("VAPPEND without VWRITE"));
4545 * Look for a normal, non-privileged way to access the file/directory
4546 * as requested. If it exists, go with that.
4549 if (privused != NULL)
4554 /* Check the owner. */
4555 if (cred->cr_uid == file_uid) {
4556 dac_granted |= VADMIN;
4557 if (file_mode & S_IXUSR)
4558 dac_granted |= VEXEC;
4559 if (file_mode & S_IRUSR)
4560 dac_granted |= VREAD;
4561 if (file_mode & S_IWUSR)
4562 dac_granted |= (VWRITE | VAPPEND);
4564 if ((accmode & dac_granted) == accmode)
4570 /* Otherwise, check the groups (first match) */
4571 if (groupmember(file_gid, cred)) {
4572 if (file_mode & S_IXGRP)
4573 dac_granted |= VEXEC;
4574 if (file_mode & S_IRGRP)
4575 dac_granted |= VREAD;
4576 if (file_mode & S_IWGRP)
4577 dac_granted |= (VWRITE | VAPPEND);
4579 if ((accmode & dac_granted) == accmode)
4585 /* Otherwise, check everyone else. */
4586 if (file_mode & S_IXOTH)
4587 dac_granted |= VEXEC;
4588 if (file_mode & S_IROTH)
4589 dac_granted |= VREAD;
4590 if (file_mode & S_IWOTH)
4591 dac_granted |= (VWRITE | VAPPEND);
4592 if ((accmode & dac_granted) == accmode)
4597 * Build a privilege mask to determine if the set of privileges
4598 * satisfies the requirements when combined with the granted mask
4599 * from above. For each privilege, if the privilege is required,
4600 * bitwise or the request type onto the priv_granted mask.
4606 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
4607 * requests, instead of PRIV_VFS_EXEC.
4609 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4610 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
4611 priv_granted |= VEXEC;
4614 * Ensure that at least one execute bit is on. Otherwise,
4615 * a privileged user will always succeed, and we don't want
4616 * this to happen unless the file really is executable.
4618 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4619 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
4620 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
4621 priv_granted |= VEXEC;
4624 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
4625 !priv_check_cred(cred, PRIV_VFS_READ, 0))
4626 priv_granted |= VREAD;
4628 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
4629 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
4630 priv_granted |= (VWRITE | VAPPEND);
4632 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
4633 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
4634 priv_granted |= VADMIN;
4636 if ((accmode & (priv_granted | dac_granted)) == accmode) {
4637 /* XXX audit: privilege used */
4638 if (privused != NULL)
4643 return ((accmode & VADMIN) ? EPERM : EACCES);
4647 * Credential check based on process requesting service, and per-attribute
4651 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
4652 struct thread *td, accmode_t accmode)
4656 * Kernel-invoked always succeeds.
4662 * Do not allow privileged processes in jail to directly manipulate
4663 * system attributes.
4665 switch (attrnamespace) {
4666 case EXTATTR_NAMESPACE_SYSTEM:
4667 /* Potentially should be: return (EPERM); */
4668 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
4669 case EXTATTR_NAMESPACE_USER:
4670 return (VOP_ACCESS(vp, accmode, cred, td));
4676 #ifdef DEBUG_VFS_LOCKS
4678 * This only exists to suppress warnings from unlocked specfs accesses. It is
4679 * no longer ok to have an unlocked VFS.
4681 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4682 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4684 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4685 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4686 "Drop into debugger on lock violation");
4688 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4689 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4690 0, "Check for interlock across VOPs");
4692 int vfs_badlock_print = 1; /* Print lock violations. */
4693 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4694 0, "Print lock violations");
4696 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */
4697 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode,
4698 0, "Print vnode details on lock violations");
4701 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4702 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4703 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4707 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4711 if (vfs_badlock_backtrace)
4714 if (vfs_badlock_vnode)
4715 vn_printf(vp, "vnode ");
4716 if (vfs_badlock_print)
4717 printf("%s: %p %s\n", str, (void *)vp, msg);
4718 if (vfs_badlock_ddb)
4719 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4723 assert_vi_locked(struct vnode *vp, const char *str)
4726 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4727 vfs_badlock("interlock is not locked but should be", str, vp);
4731 assert_vi_unlocked(struct vnode *vp, const char *str)
4734 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4735 vfs_badlock("interlock is locked but should not be", str, vp);
4739 assert_vop_locked(struct vnode *vp, const char *str)
4743 if (!IGNORE_LOCK(vp)) {
4744 locked = VOP_ISLOCKED(vp);
4745 if (locked == 0 || locked == LK_EXCLOTHER)
4746 vfs_badlock("is not locked but should be", str, vp);
4751 assert_vop_unlocked(struct vnode *vp, const char *str)
4754 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4755 vfs_badlock("is locked but should not be", str, vp);
4759 assert_vop_elocked(struct vnode *vp, const char *str)
4762 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4763 vfs_badlock("is not exclusive locked but should be", str, vp);
4765 #endif /* DEBUG_VFS_LOCKS */
4768 vop_rename_fail(struct vop_rename_args *ap)
4771 if (ap->a_tvp != NULL)
4773 if (ap->a_tdvp == ap->a_tvp)
4782 vop_rename_pre(void *ap)
4784 struct vop_rename_args *a = ap;
4786 #ifdef DEBUG_VFS_LOCKS
4788 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4789 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4790 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4791 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4793 /* Check the source (from). */
4794 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4795 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4796 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4797 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4798 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4800 /* Check the target. */
4802 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4803 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4805 if (a->a_tdvp != a->a_fdvp)
4807 if (a->a_tvp != a->a_fvp)
4814 #ifdef DEBUG_VFS_LOCKS
4816 vop_strategy_pre(void *ap)
4818 struct vop_strategy_args *a;
4825 * Cluster ops lock their component buffers but not the IO container.
4827 if ((bp->b_flags & B_CLUSTER) != 0)
4830 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4831 if (vfs_badlock_print)
4833 "VOP_STRATEGY: bp is not locked but should be\n");
4834 if (vfs_badlock_ddb)
4835 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4840 vop_lock_pre(void *ap)
4842 struct vop_lock1_args *a = ap;
4844 if ((a->a_flags & LK_INTERLOCK) == 0)
4845 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4847 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4851 vop_lock_post(void *ap, int rc)
4853 struct vop_lock1_args *a = ap;
4855 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4856 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4857 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4861 vop_unlock_pre(void *ap)
4863 struct vop_unlock_args *a = ap;
4865 if (a->a_flags & LK_INTERLOCK)
4866 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4867 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4871 vop_unlock_post(void *ap, int rc)
4873 struct vop_unlock_args *a = ap;
4875 if (a->a_flags & LK_INTERLOCK)
4876 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4881 vop_create_post(void *ap, int rc)
4883 struct vop_create_args *a = ap;
4886 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4890 vop_deleteextattr_post(void *ap, int rc)
4892 struct vop_deleteextattr_args *a = ap;
4895 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4899 vop_link_post(void *ap, int rc)
4901 struct vop_link_args *a = ap;
4904 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4905 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4910 vop_mkdir_post(void *ap, int rc)
4912 struct vop_mkdir_args *a = ap;
4915 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4919 vop_mknod_post(void *ap, int rc)
4921 struct vop_mknod_args *a = ap;
4924 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4928 vop_reclaim_post(void *ap, int rc)
4930 struct vop_reclaim_args *a = ap;
4933 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE);
4937 vop_remove_post(void *ap, int rc)
4939 struct vop_remove_args *a = ap;
4942 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4943 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4948 vop_rename_post(void *ap, int rc)
4950 struct vop_rename_args *a = ap;
4955 if (a->a_fdvp == a->a_tdvp) {
4956 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR)
4958 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
4959 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
4961 hint |= NOTE_EXTEND;
4962 if (a->a_fvp->v_type == VDIR)
4964 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
4966 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL &&
4967 a->a_tvp->v_type == VDIR)
4969 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
4972 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4974 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4976 if (a->a_tdvp != a->a_fdvp)
4978 if (a->a_tvp != a->a_fvp)
4986 vop_rmdir_post(void *ap, int rc)
4988 struct vop_rmdir_args *a = ap;
4991 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4992 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4997 vop_setattr_post(void *ap, int rc)
4999 struct vop_setattr_args *a = ap;
5002 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5006 vop_setextattr_post(void *ap, int rc)
5008 struct vop_setextattr_args *a = ap;
5011 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
5015 vop_symlink_post(void *ap, int rc)
5017 struct vop_symlink_args *a = ap;
5020 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
5024 vop_open_post(void *ap, int rc)
5026 struct vop_open_args *a = ap;
5029 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN);
5033 vop_close_post(void *ap, int rc)
5035 struct vop_close_args *a = ap;
5037 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */
5038 (a->a_vp->v_iflag & VI_DOOMED) == 0)) {
5039 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ?
5040 NOTE_CLOSE_WRITE : NOTE_CLOSE);
5045 vop_read_post(void *ap, int rc)
5047 struct vop_read_args *a = ap;
5050 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5054 vop_readdir_post(void *ap, int rc)
5056 struct vop_readdir_args *a = ap;
5059 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
5062 static struct knlist fs_knlist;
5065 vfs_event_init(void *arg)
5067 knlist_init_mtx(&fs_knlist, NULL);
5069 /* XXX - correct order? */
5070 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
5073 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
5076 KNOTE_UNLOCKED(&fs_knlist, event);
5079 static int filt_fsattach(struct knote *kn);
5080 static void filt_fsdetach(struct knote *kn);
5081 static int filt_fsevent(struct knote *kn, long hint);
5083 struct filterops fs_filtops = {
5085 .f_attach = filt_fsattach,
5086 .f_detach = filt_fsdetach,
5087 .f_event = filt_fsevent
5091 filt_fsattach(struct knote *kn)
5094 kn->kn_flags |= EV_CLEAR;
5095 knlist_add(&fs_knlist, kn, 0);
5100 filt_fsdetach(struct knote *kn)
5103 knlist_remove(&fs_knlist, kn, 0);
5107 filt_fsevent(struct knote *kn, long hint)
5110 kn->kn_fflags |= hint;
5111 return (kn->kn_fflags != 0);
5115 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
5121 error = SYSCTL_IN(req, &vc, sizeof(vc));
5124 if (vc.vc_vers != VFS_CTL_VERS1)
5126 mp = vfs_getvfs(&vc.vc_fsid);
5129 /* ensure that a specific sysctl goes to the right filesystem. */
5130 if (strcmp(vc.vc_fstypename, "*") != 0 &&
5131 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
5135 VCTLTOREQ(&vc, req);
5136 error = VFS_SYSCTL(mp, vc.vc_op, req);
5141 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
5142 NULL, 0, sysctl_vfs_ctl, "",
5146 * Function to initialize a va_filerev field sensibly.
5147 * XXX: Wouldn't a random number make a lot more sense ??
5150 init_va_filerev(void)
5155 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
5158 static int filt_vfsread(struct knote *kn, long hint);
5159 static int filt_vfswrite(struct knote *kn, long hint);
5160 static int filt_vfsvnode(struct knote *kn, long hint);
5161 static void filt_vfsdetach(struct knote *kn);
5162 static struct filterops vfsread_filtops = {
5164 .f_detach = filt_vfsdetach,
5165 .f_event = filt_vfsread
5167 static struct filterops vfswrite_filtops = {
5169 .f_detach = filt_vfsdetach,
5170 .f_event = filt_vfswrite
5172 static struct filterops vfsvnode_filtops = {
5174 .f_detach = filt_vfsdetach,
5175 .f_event = filt_vfsvnode
5179 vfs_knllock(void *arg)
5181 struct vnode *vp = arg;
5183 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5187 vfs_knlunlock(void *arg)
5189 struct vnode *vp = arg;
5195 vfs_knl_assert_locked(void *arg)
5197 #ifdef DEBUG_VFS_LOCKS
5198 struct vnode *vp = arg;
5200 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
5205 vfs_knl_assert_unlocked(void *arg)
5207 #ifdef DEBUG_VFS_LOCKS
5208 struct vnode *vp = arg;
5210 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
5215 vfs_kqfilter(struct vop_kqfilter_args *ap)
5217 struct vnode *vp = ap->a_vp;
5218 struct knote *kn = ap->a_kn;
5221 switch (kn->kn_filter) {
5223 kn->kn_fop = &vfsread_filtops;
5226 kn->kn_fop = &vfswrite_filtops;
5229 kn->kn_fop = &vfsvnode_filtops;
5235 kn->kn_hook = (caddr_t)vp;
5238 if (vp->v_pollinfo == NULL)
5240 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
5242 knlist_add(knl, kn, 0);
5248 * Detach knote from vnode
5251 filt_vfsdetach(struct knote *kn)
5253 struct vnode *vp = (struct vnode *)kn->kn_hook;
5255 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
5256 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
5262 filt_vfsread(struct knote *kn, long hint)
5264 struct vnode *vp = (struct vnode *)kn->kn_hook;
5269 * filesystem is gone, so set the EOF flag and schedule
5270 * the knote for deletion.
5272 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5274 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5279 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
5283 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
5284 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
5291 filt_vfswrite(struct knote *kn, long hint)
5293 struct vnode *vp = (struct vnode *)kn->kn_hook;
5298 * filesystem is gone, so set the EOF flag and schedule
5299 * the knote for deletion.
5301 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
5302 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5310 filt_vfsvnode(struct knote *kn, long hint)
5312 struct vnode *vp = (struct vnode *)kn->kn_hook;
5316 if (kn->kn_sfflags & hint)
5317 kn->kn_fflags |= hint;
5318 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5319 kn->kn_flags |= EV_EOF;
5323 res = (kn->kn_fflags != 0);
5329 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
5333 if (dp->d_reclen > ap->a_uio->uio_resid)
5334 return (ENAMETOOLONG);
5335 error = uiomove(dp, dp->d_reclen, ap->a_uio);
5337 if (ap->a_ncookies != NULL) {
5338 if (ap->a_cookies != NULL)
5339 free(ap->a_cookies, M_TEMP);
5340 ap->a_cookies = NULL;
5341 *ap->a_ncookies = 0;
5345 if (ap->a_ncookies == NULL)
5348 KASSERT(ap->a_cookies,
5349 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
5351 *ap->a_cookies = realloc(*ap->a_cookies,
5352 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
5353 (*ap->a_cookies)[*ap->a_ncookies] = off;
5354 *ap->a_ncookies += 1;
5359 * Mark for update the access time of the file if the filesystem
5360 * supports VOP_MARKATIME. This functionality is used by execve and
5361 * mmap, so we want to avoid the I/O implied by directly setting
5362 * va_atime for the sake of efficiency.
5365 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
5370 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
5371 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
5372 (void)VOP_MARKATIME(vp);
5376 * The purpose of this routine is to remove granularity from accmode_t,
5377 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
5378 * VADMIN and VAPPEND.
5380 * If it returns 0, the caller is supposed to continue with the usual
5381 * access checks using 'accmode' as modified by this routine. If it
5382 * returns nonzero value, the caller is supposed to return that value
5385 * Note that after this routine runs, accmode may be zero.
5388 vfs_unixify_accmode(accmode_t *accmode)
5391 * There is no way to specify explicit "deny" rule using
5392 * file mode or POSIX.1e ACLs.
5394 if (*accmode & VEXPLICIT_DENY) {
5400 * None of these can be translated into usual access bits.
5401 * Also, the common case for NFSv4 ACLs is to not contain
5402 * either of these bits. Caller should check for VWRITE
5403 * on the containing directory instead.
5405 if (*accmode & (VDELETE_CHILD | VDELETE))
5408 if (*accmode & VADMIN_PERMS) {
5409 *accmode &= ~VADMIN_PERMS;
5414 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
5415 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
5417 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
5423 * These are helper functions for filesystems to traverse all
5424 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
5426 * This interface replaces MNT_VNODE_FOREACH.
5429 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
5432 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
5437 kern_yield(PRI_USER);
5439 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5440 for (vp = TAILQ_NEXT(*mvp, v_nmntvnodes); vp != NULL;
5441 vp = TAILQ_NEXT(vp, v_nmntvnodes)) {
5442 /* Allow a racy peek at VI_DOOMED to save a lock acquisition. */
5443 if (vp->v_type == VMARKER || (vp->v_iflag & VI_DOOMED) != 0)
5446 if ((vp->v_iflag & VI_DOOMED) != 0) {
5453 __mnt_vnode_markerfree_all(mvp, mp);
5454 /* MNT_IUNLOCK(mp); -- done in above function */
5455 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
5458 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
5459 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
5465 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
5469 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
5472 (*mvp)->v_mount = mp;
5473 (*mvp)->v_type = VMARKER;
5475 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
5476 /* Allow a racy peek at VI_DOOMED to save a lock acquisition. */
5477 if (vp->v_type == VMARKER || (vp->v_iflag & VI_DOOMED) != 0)
5480 if ((vp->v_iflag & VI_DOOMED) != 0) {
5489 free(*mvp, M_VNODE_MARKER);
5493 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
5499 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
5507 mtx_assert(MNT_MTX(mp), MA_OWNED);
5509 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5510 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
5513 free(*mvp, M_VNODE_MARKER);
5518 * These are helper functions for filesystems to traverse their
5519 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
5522 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
5525 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5530 free(*mvp, M_VNODE_MARKER);
5535 * Relock the mp mount vnode list lock with the vp vnode interlock in the
5536 * conventional lock order during mnt_vnode_next_active iteration.
5538 * On entry, the mount vnode list lock is held and the vnode interlock is not.
5539 * The list lock is dropped and reacquired. On success, both locks are held.
5540 * On failure, the mount vnode list lock is held but the vnode interlock is
5541 * not, and the procedure may have yielded.
5544 mnt_vnode_next_active_relock(struct vnode *mvp, struct mount *mp,
5547 const struct vnode *tmp;
5550 VNASSERT(mvp->v_mount == mp && mvp->v_type == VMARKER &&
5551 TAILQ_NEXT(mvp, v_actfreelist) != NULL, mvp,
5552 ("%s: bad marker", __func__));
5553 VNASSERT(vp->v_mount == mp && vp->v_type != VMARKER, vp,
5554 ("%s: inappropriate vnode", __func__));
5555 ASSERT_VI_UNLOCKED(vp, __func__);
5556 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5560 TAILQ_REMOVE(&mp->mnt_activevnodelist, mvp, v_actfreelist);
5561 TAILQ_INSERT_BEFORE(vp, mvp, v_actfreelist);
5564 * Use a hold to prevent vp from disappearing while the mount vnode
5565 * list lock is dropped and reacquired. Normally a hold would be
5566 * acquired with vhold(), but that might try to acquire the vnode
5567 * interlock, which would be a LOR with the mount vnode list lock.
5569 held = refcount_acquire_if_not_zero(&vp->v_holdcnt);
5570 mtx_unlock(&mp->mnt_listmtx);
5574 if (!refcount_release_if_not_last(&vp->v_holdcnt)) {
5578 mtx_lock(&mp->mnt_listmtx);
5581 * Determine whether the vnode is still the next one after the marker,
5582 * excepting any other markers. If the vnode has not been doomed by
5583 * vgone() then the hold should have ensured that it remained on the
5584 * active list. If it has been doomed but is still on the active list,
5585 * don't abort, but rather skip over it (avoid spinning on doomed
5590 tmp = TAILQ_NEXT(tmp, v_actfreelist);
5591 } while (tmp != NULL && tmp->v_type == VMARKER);
5593 mtx_unlock(&mp->mnt_listmtx);
5602 mtx_lock(&mp->mnt_listmtx);
5605 ASSERT_VI_LOCKED(vp, __func__);
5607 ASSERT_VI_UNLOCKED(vp, __func__);
5608 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5612 static struct vnode *
5613 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
5615 struct vnode *vp, *nvp;
5617 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5618 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5620 vp = TAILQ_NEXT(*mvp, v_actfreelist);
5621 while (vp != NULL) {
5622 if (vp->v_type == VMARKER) {
5623 vp = TAILQ_NEXT(vp, v_actfreelist);
5627 * Try-lock because this is the wrong lock order. If that does
5628 * not succeed, drop the mount vnode list lock and try to
5629 * reacquire it and the vnode interlock in the right order.
5631 if (!VI_TRYLOCK(vp) &&
5632 !mnt_vnode_next_active_relock(*mvp, mp, vp))
5634 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
5635 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
5636 ("alien vnode on the active list %p %p", vp, mp));
5637 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
5639 nvp = TAILQ_NEXT(vp, v_actfreelist);
5643 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
5645 /* Check if we are done */
5647 mtx_unlock(&mp->mnt_listmtx);
5648 mnt_vnode_markerfree_active(mvp, mp);
5651 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
5652 mtx_unlock(&mp->mnt_listmtx);
5653 ASSERT_VI_LOCKED(vp, "active iter");
5654 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
5659 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
5663 kern_yield(PRI_USER);
5664 mtx_lock(&mp->mnt_listmtx);
5665 return (mnt_vnode_next_active(mvp, mp));
5669 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
5673 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
5677 (*mvp)->v_type = VMARKER;
5678 (*mvp)->v_mount = mp;
5680 mtx_lock(&mp->mnt_listmtx);
5681 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
5683 mtx_unlock(&mp->mnt_listmtx);
5684 mnt_vnode_markerfree_active(mvp, mp);
5687 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
5688 return (mnt_vnode_next_active(mvp, mp));
5692 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
5698 mtx_lock(&mp->mnt_listmtx);
5699 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
5700 mtx_unlock(&mp->mnt_listmtx);
5701 mnt_vnode_markerfree_active(mvp, mp);
5705 vn_dir_check_exec(struct vnode *vp, struct componentname *cnp)
5708 if ((cnp->cn_flags & NOEXECCHECK) != 0) {
5709 cnp->cn_flags &= ~NOEXECCHECK;
5713 return (VOP_ACCESS(vp, VEXEC, cnp->cn_cred, cnp->cn_thread));