2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
38 * External virtual filesystem routines
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include "opt_compat.h"
46 #include "opt_watchdog.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/condvar.h>
54 #include <sys/dirent.h>
55 #include <sys/event.h>
56 #include <sys/eventhandler.h>
57 #include <sys/extattr.h>
59 #include <sys/fcntl.h>
62 #include <sys/kernel.h>
63 #include <sys/kthread.h>
64 #include <sys/lockf.h>
65 #include <sys/malloc.h>
66 #include <sys/mount.h>
67 #include <sys/namei.h>
68 #include <sys/pctrie.h>
70 #include <sys/reboot.h>
71 #include <sys/rwlock.h>
72 #include <sys/sched.h>
73 #include <sys/sleepqueue.h>
76 #include <sys/sysctl.h>
77 #include <sys/syslog.h>
78 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/watchdog.h>
82 #include <machine/stdarg.h>
84 #include <security/mac/mac_framework.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_extern.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_kern.h>
99 static void delmntque(struct vnode *vp);
100 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
101 int slpflag, int slptimeo);
102 static void syncer_shutdown(void *arg, int howto);
103 static int vtryrecycle(struct vnode *vp);
104 static void v_incr_usecount(struct vnode *);
105 static void v_decr_usecount(struct vnode *);
106 static void v_decr_useonly(struct vnode *);
107 static void v_upgrade_usecount(struct vnode *);
108 static void vnlru_free(int);
109 static void vgonel(struct vnode *);
110 static void vfs_knllock(void *arg);
111 static void vfs_knlunlock(void *arg);
112 static void vfs_knl_assert_locked(void *arg);
113 static void vfs_knl_assert_unlocked(void *arg);
114 static void destroy_vpollinfo(struct vpollinfo *vi);
117 * Number of vnodes in existence. Increased whenever getnewvnode()
118 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
120 static unsigned long numvnodes;
122 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
123 "Number of vnodes in existence");
126 * Conversion tables for conversion from vnode types to inode formats
129 enum vtype iftovt_tab[16] = {
130 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
131 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
133 int vttoif_tab[10] = {
134 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
135 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
139 * List of vnodes that are ready for recycling.
141 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
144 * Free vnode target. Free vnodes may simply be files which have been stat'd
145 * but not read. This is somewhat common, and a small cache of such files
146 * should be kept to avoid recreation costs.
148 static u_long wantfreevnodes;
149 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
150 /* Number of vnodes in the free list. */
151 static u_long freevnodes;
152 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
153 "Number of vnodes in the free list");
155 static int vlru_allow_cache_src;
156 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
157 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
160 * Various variables used for debugging the new implementation of
162 * XXX these are probably of (very) limited utility now.
164 static int reassignbufcalls;
165 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
166 "Number of calls to reassignbuf");
169 * Cache for the mount type id assigned to NFS. This is used for
170 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
172 int nfs_mount_type = -1;
174 /* To keep more than one thread at a time from running vfs_getnewfsid */
175 static struct mtx mntid_mtx;
178 * Lock for any access to the following:
183 static struct mtx vnode_free_list_mtx;
185 /* Publicly exported FS */
186 struct nfs_public nfs_pub;
188 static uma_zone_t buf_trie_zone;
190 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
191 static uma_zone_t vnode_zone;
192 static uma_zone_t vnodepoll_zone;
195 * The workitem queue.
197 * It is useful to delay writes of file data and filesystem metadata
198 * for tens of seconds so that quickly created and deleted files need
199 * not waste disk bandwidth being created and removed. To realize this,
200 * we append vnodes to a "workitem" queue. When running with a soft
201 * updates implementation, most pending metadata dependencies should
202 * not wait for more than a few seconds. Thus, mounted on block devices
203 * are delayed only about a half the time that file data is delayed.
204 * Similarly, directory updates are more critical, so are only delayed
205 * about a third the time that file data is delayed. Thus, there are
206 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
207 * one each second (driven off the filesystem syncer process). The
208 * syncer_delayno variable indicates the next queue that is to be processed.
209 * Items that need to be processed soon are placed in this queue:
211 * syncer_workitem_pending[syncer_delayno]
213 * A delay of fifteen seconds is done by placing the request fifteen
214 * entries later in the queue:
216 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
219 static int syncer_delayno;
220 static long syncer_mask;
221 LIST_HEAD(synclist, bufobj);
222 static struct synclist *syncer_workitem_pending;
224 * The sync_mtx protects:
229 * syncer_workitem_pending
230 * syncer_worklist_len
233 static struct mtx sync_mtx;
234 static struct cv sync_wakeup;
236 #define SYNCER_MAXDELAY 32
237 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
238 static int syncdelay = 30; /* max time to delay syncing data */
239 static int filedelay = 30; /* time to delay syncing files */
240 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
241 "Time to delay syncing files (in seconds)");
242 static int dirdelay = 29; /* time to delay syncing directories */
243 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
244 "Time to delay syncing directories (in seconds)");
245 static int metadelay = 28; /* time to delay syncing metadata */
246 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
247 "Time to delay syncing metadata (in seconds)");
248 static int rushjob; /* number of slots to run ASAP */
249 static int stat_rush_requests; /* number of times I/O speeded up */
250 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
251 "Number of times I/O speeded up (rush requests)");
254 * When shutting down the syncer, run it at four times normal speed.
256 #define SYNCER_SHUTDOWN_SPEEDUP 4
257 static int sync_vnode_count;
258 static int syncer_worklist_len;
259 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
263 * Number of vnodes we want to exist at any one time. This is mostly used
264 * to size hash tables in vnode-related code. It is normally not used in
265 * getnewvnode(), as wantfreevnodes is normally nonzero.)
267 * XXX desiredvnodes is historical cruft and should not exist.
270 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
271 &desiredvnodes, 0, "Maximum number of vnodes");
272 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
273 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
274 static int vnlru_nowhere;
275 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
276 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
278 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
282 * Support for the bufobj clean & dirty pctrie.
285 buf_trie_alloc(struct pctrie *ptree)
288 return uma_zalloc(buf_trie_zone, M_NOWAIT);
292 buf_trie_free(struct pctrie *ptree, void *node)
295 uma_zfree(buf_trie_zone, node);
297 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
300 * Initialize the vnode management data structures.
302 * Reevaluate the following cap on the number of vnodes after the physical
303 * memory size exceeds 512GB. In the limit, as the physical memory size
304 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
306 #ifndef MAXVNODES_MAX
307 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
310 vntblinit(void *dummy __unused)
313 int physvnodes, virtvnodes;
316 * Desiredvnodes is a function of the physical memory size and the
317 * kernel's heap size. Generally speaking, it scales with the
318 * physical memory size. The ratio of desiredvnodes to physical pages
319 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
320 * marginal ratio of desiredvnodes to physical pages is one to
321 * sixteen. However, desiredvnodes is limited by the kernel's heap
322 * size. The memory required by desiredvnodes vnodes and vm objects
323 * may not exceed one seventh of the kernel's heap size.
325 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
326 cnt.v_page_count) / 16;
327 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
328 sizeof(struct vnode)));
329 desiredvnodes = min(physvnodes, virtvnodes);
330 if (desiredvnodes > MAXVNODES_MAX) {
332 printf("Reducing kern.maxvnodes %d -> %d\n",
333 desiredvnodes, MAXVNODES_MAX);
334 desiredvnodes = MAXVNODES_MAX;
336 wantfreevnodes = desiredvnodes / 4;
337 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
338 TAILQ_INIT(&vnode_free_list);
339 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
340 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
341 NULL, NULL, UMA_ALIGN_PTR, 0);
342 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
343 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
345 * Preallocate enough nodes to support one-per buf so that
346 * we can not fail an insert. reassignbuf() callers can not
347 * tolerate the insertion failure.
349 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
350 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
351 UMA_ZONE_NOFREE | UMA_ZONE_VM);
352 uma_prealloc(buf_trie_zone, nbuf);
354 * Initialize the filesystem syncer.
356 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
358 syncer_maxdelay = syncer_mask + 1;
359 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
360 cv_init(&sync_wakeup, "syncer");
361 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
365 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
369 * Mark a mount point as busy. Used to synchronize access and to delay
370 * unmounting. Eventually, mountlist_mtx is not released on failure.
372 * vfs_busy() is a custom lock, it can block the caller.
373 * vfs_busy() only sleeps if the unmount is active on the mount point.
374 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
375 * vnode belonging to mp.
377 * Lookup uses vfs_busy() to traverse mount points.
379 * / vnode lock A / vnode lock (/var) D
380 * /var vnode lock B /log vnode lock(/var/log) E
381 * vfs_busy lock C vfs_busy lock F
383 * Within each file system, the lock order is C->A->B and F->D->E.
385 * When traversing across mounts, the system follows that lock order:
391 * The lookup() process for namei("/var") illustrates the process:
392 * VOP_LOOKUP() obtains B while A is held
393 * vfs_busy() obtains a shared lock on F while A and B are held
394 * vput() releases lock on B
395 * vput() releases lock on A
396 * VFS_ROOT() obtains lock on D while shared lock on F is held
397 * vfs_unbusy() releases shared lock on F
398 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
399 * Attempt to lock A (instead of vp_crossmp) while D is held would
400 * violate the global order, causing deadlocks.
402 * dounmount() locks B while F is drained.
405 vfs_busy(struct mount *mp, int flags)
408 MPASS((flags & ~MBF_MASK) == 0);
409 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
414 * If mount point is currenly being unmounted, sleep until the
415 * mount point fate is decided. If thread doing the unmounting fails,
416 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
417 * that this mount point has survived the unmount attempt and vfs_busy
418 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
419 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
420 * about to be really destroyed. vfs_busy needs to release its
421 * reference on the mount point in this case and return with ENOENT,
422 * telling the caller that mount mount it tried to busy is no longer
425 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
426 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
429 CTR1(KTR_VFS, "%s: failed busying before sleeping",
433 if (flags & MBF_MNTLSTLOCK)
434 mtx_unlock(&mountlist_mtx);
435 mp->mnt_kern_flag |= MNTK_MWAIT;
436 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
437 if (flags & MBF_MNTLSTLOCK)
438 mtx_lock(&mountlist_mtx);
441 if (flags & MBF_MNTLSTLOCK)
442 mtx_unlock(&mountlist_mtx);
449 * Free a busy filesystem.
452 vfs_unbusy(struct mount *mp)
455 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
458 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
460 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
461 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
462 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
463 mp->mnt_kern_flag &= ~MNTK_DRAINING;
464 wakeup(&mp->mnt_lockref);
470 * Lookup a mount point by filesystem identifier.
473 vfs_getvfs(fsid_t *fsid)
477 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
478 mtx_lock(&mountlist_mtx);
479 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
480 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
481 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
483 mtx_unlock(&mountlist_mtx);
487 mtx_unlock(&mountlist_mtx);
488 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
489 return ((struct mount *) 0);
493 * Lookup a mount point by filesystem identifier, busying it before
496 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
497 * cache for popular filesystem identifiers. The cache is lockess, using
498 * the fact that struct mount's are never freed. In worst case we may
499 * get pointer to unmounted or even different filesystem, so we have to
500 * check what we got, and go slow way if so.
503 vfs_busyfs(fsid_t *fsid)
505 #define FSID_CACHE_SIZE 256
506 typedef struct mount * volatile vmp_t;
507 static vmp_t cache[FSID_CACHE_SIZE];
512 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
513 hash = fsid->val[0] ^ fsid->val[1];
514 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
517 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
518 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
520 if (vfs_busy(mp, 0) != 0) {
524 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
525 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
531 mtx_lock(&mountlist_mtx);
532 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
533 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
534 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
535 error = vfs_busy(mp, MBF_MNTLSTLOCK);
538 mtx_unlock(&mountlist_mtx);
545 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
546 mtx_unlock(&mountlist_mtx);
547 return ((struct mount *) 0);
551 * Check if a user can access privileged mount options.
554 vfs_suser(struct mount *mp, struct thread *td)
559 * If the thread is jailed, but this is not a jail-friendly file
560 * system, deny immediately.
562 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
566 * If the file system was mounted outside the jail of the calling
567 * thread, deny immediately.
569 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
573 * If file system supports delegated administration, we don't check
574 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
575 * by the file system itself.
576 * If this is not the user that did original mount, we check for
577 * the PRIV_VFS_MOUNT_OWNER privilege.
579 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
580 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
581 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
588 * Get a new unique fsid. Try to make its val[0] unique, since this value
589 * will be used to create fake device numbers for stat(). Also try (but
590 * not so hard) make its val[0] unique mod 2^16, since some emulators only
591 * support 16-bit device numbers. We end up with unique val[0]'s for the
592 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
594 * Keep in mind that several mounts may be running in parallel. Starting
595 * the search one past where the previous search terminated is both a
596 * micro-optimization and a defense against returning the same fsid to
600 vfs_getnewfsid(struct mount *mp)
602 static uint16_t mntid_base;
607 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
608 mtx_lock(&mntid_mtx);
609 mtype = mp->mnt_vfc->vfc_typenum;
610 tfsid.val[1] = mtype;
611 mtype = (mtype & 0xFF) << 24;
613 tfsid.val[0] = makedev(255,
614 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
616 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
620 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
621 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
622 mtx_unlock(&mntid_mtx);
626 * Knob to control the precision of file timestamps:
628 * 0 = seconds only; nanoseconds zeroed.
629 * 1 = seconds and nanoseconds, accurate within 1/HZ.
630 * 2 = seconds and nanoseconds, truncated to microseconds.
631 * >=3 = seconds and nanoseconds, maximum precision.
633 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
635 static int timestamp_precision = TSP_SEC;
636 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
637 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
638 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
639 "3+: sec + ns (max. precision))");
642 * Get a current timestamp.
645 vfs_timestamp(struct timespec *tsp)
649 switch (timestamp_precision) {
651 tsp->tv_sec = time_second;
659 TIMEVAL_TO_TIMESPEC(&tv, tsp);
669 * Set vnode attributes to VNOVAL
672 vattr_null(struct vattr *vap)
676 vap->va_size = VNOVAL;
677 vap->va_bytes = VNOVAL;
678 vap->va_mode = VNOVAL;
679 vap->va_nlink = VNOVAL;
680 vap->va_uid = VNOVAL;
681 vap->va_gid = VNOVAL;
682 vap->va_fsid = VNOVAL;
683 vap->va_fileid = VNOVAL;
684 vap->va_blocksize = VNOVAL;
685 vap->va_rdev = VNOVAL;
686 vap->va_atime.tv_sec = VNOVAL;
687 vap->va_atime.tv_nsec = VNOVAL;
688 vap->va_mtime.tv_sec = VNOVAL;
689 vap->va_mtime.tv_nsec = VNOVAL;
690 vap->va_ctime.tv_sec = VNOVAL;
691 vap->va_ctime.tv_nsec = VNOVAL;
692 vap->va_birthtime.tv_sec = VNOVAL;
693 vap->va_birthtime.tv_nsec = VNOVAL;
694 vap->va_flags = VNOVAL;
695 vap->va_gen = VNOVAL;
700 * This routine is called when we have too many vnodes. It attempts
701 * to free <count> vnodes and will potentially free vnodes that still
702 * have VM backing store (VM backing store is typically the cause
703 * of a vnode blowout so we want to do this). Therefore, this operation
704 * is not considered cheap.
706 * A number of conditions may prevent a vnode from being reclaimed.
707 * the buffer cache may have references on the vnode, a directory
708 * vnode may still have references due to the namei cache representing
709 * underlying files, or the vnode may be in active use. It is not
710 * desireable to reuse such vnodes. These conditions may cause the
711 * number of vnodes to reach some minimum value regardless of what
712 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
715 vlrureclaim(struct mount *mp)
724 * Calculate the trigger point, don't allow user
725 * screwups to blow us up. This prevents us from
726 * recycling vnodes with lots of resident pages. We
727 * aren't trying to free memory, we are trying to
730 usevnodes = desiredvnodes;
733 trigger = cnt.v_page_count * 2 / usevnodes;
735 vn_start_write(NULL, &mp, V_WAIT);
737 count = mp->mnt_nvnodelistsize / 10 + 1;
739 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
740 while (vp != NULL && vp->v_type == VMARKER)
741 vp = TAILQ_NEXT(vp, v_nmntvnodes);
744 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
745 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
750 * If it's been deconstructed already, it's still
751 * referenced, or it exceeds the trigger, skip it.
753 if (vp->v_usecount ||
754 (!vlru_allow_cache_src &&
755 !LIST_EMPTY(&(vp)->v_cache_src)) ||
756 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
757 vp->v_object->resident_page_count > trigger)) {
763 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
765 goto next_iter_mntunlocked;
769 * v_usecount may have been bumped after VOP_LOCK() dropped
770 * the vnode interlock and before it was locked again.
772 * It is not necessary to recheck VI_DOOMED because it can
773 * only be set by another thread that holds both the vnode
774 * lock and vnode interlock. If another thread has the
775 * vnode lock before we get to VOP_LOCK() and obtains the
776 * vnode interlock after VOP_LOCK() drops the vnode
777 * interlock, the other thread will be unable to drop the
778 * vnode lock before our VOP_LOCK() call fails.
780 if (vp->v_usecount ||
781 (!vlru_allow_cache_src &&
782 !LIST_EMPTY(&(vp)->v_cache_src)) ||
783 (vp->v_object != NULL &&
784 vp->v_object->resident_page_count > trigger)) {
785 VOP_UNLOCK(vp, LK_INTERLOCK);
787 goto next_iter_mntunlocked;
789 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
790 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
795 next_iter_mntunlocked:
804 kern_yield(PRI_USER);
809 vn_finished_write(mp);
814 * Attempt to keep the free list at wantfreevnodes length.
817 vnlru_free(int count)
821 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
822 for (; count > 0; count--) {
823 vp = TAILQ_FIRST(&vnode_free_list);
825 * The list can be modified while the free_list_mtx
826 * has been dropped and vp could be NULL here.
830 VNASSERT(vp->v_op != NULL, vp,
831 ("vnlru_free: vnode already reclaimed."));
832 KASSERT((vp->v_iflag & VI_FREE) != 0,
833 ("Removing vnode not on freelist"));
834 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
835 ("Mangling active vnode"));
836 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
838 * Don't recycle if we can't get the interlock.
840 if (!VI_TRYLOCK(vp)) {
841 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
844 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
845 vp, ("vp inconsistent on freelist"));
848 * The clear of VI_FREE prevents activation of the
849 * vnode. There is no sense in putting the vnode on
850 * the mount point active list, only to remove it
851 * later during recycling. Inline the relevant part
852 * of vholdl(), to avoid triggering assertions or
856 vp->v_iflag &= ~VI_FREE;
859 mtx_unlock(&vnode_free_list_mtx);
863 * If the recycled succeeded this vdrop will actually free
864 * the vnode. If not it will simply place it back on
868 mtx_lock(&vnode_free_list_mtx);
872 * Attempt to recycle vnodes in a context that is always safe to block.
873 * Calling vlrurecycle() from the bowels of filesystem code has some
874 * interesting deadlock problems.
876 static struct proc *vnlruproc;
877 static int vnlruproc_sig;
882 struct mount *mp, *nmp;
884 struct proc *p = vnlruproc;
886 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
890 kproc_suspend_check(p);
891 mtx_lock(&vnode_free_list_mtx);
892 if (freevnodes > wantfreevnodes)
893 vnlru_free(freevnodes - wantfreevnodes);
894 if (numvnodes <= desiredvnodes * 9 / 10) {
896 wakeup(&vnlruproc_sig);
897 msleep(vnlruproc, &vnode_free_list_mtx,
898 PVFS|PDROP, "vlruwt", hz);
901 mtx_unlock(&vnode_free_list_mtx);
903 mtx_lock(&mountlist_mtx);
904 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
905 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
906 nmp = TAILQ_NEXT(mp, mnt_list);
909 done += vlrureclaim(mp);
910 mtx_lock(&mountlist_mtx);
911 nmp = TAILQ_NEXT(mp, mnt_list);
914 mtx_unlock(&mountlist_mtx);
917 /* These messages are temporary debugging aids */
918 if (vnlru_nowhere < 5)
919 printf("vnlru process getting nowhere..\n");
920 else if (vnlru_nowhere == 5)
921 printf("vnlru process messages stopped.\n");
924 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
926 kern_yield(PRI_USER);
930 static struct kproc_desc vnlru_kp = {
935 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
939 * Routines having to do with the management of the vnode table.
943 * Try to recycle a freed vnode. We abort if anyone picks up a reference
944 * before we actually vgone(). This function must be called with the vnode
945 * held to prevent the vnode from being returned to the free list midway
949 vtryrecycle(struct vnode *vp)
953 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
954 VNASSERT(vp->v_holdcnt, vp,
955 ("vtryrecycle: Recycling vp %p without a reference.", vp));
957 * This vnode may found and locked via some other list, if so we
958 * can't recycle it yet.
960 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
962 "%s: impossible to recycle, vp %p lock is already held",
964 return (EWOULDBLOCK);
967 * Don't recycle if its filesystem is being suspended.
969 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
972 "%s: impossible to recycle, cannot start the write for %p",
977 * If we got this far, we need to acquire the interlock and see if
978 * anyone picked up this vnode from another list. If not, we will
979 * mark it with DOOMED via vgonel() so that anyone who does find it
983 if (vp->v_usecount) {
984 VOP_UNLOCK(vp, LK_INTERLOCK);
985 vn_finished_write(vnmp);
987 "%s: impossible to recycle, %p is already referenced",
991 if ((vp->v_iflag & VI_DOOMED) == 0)
993 VOP_UNLOCK(vp, LK_INTERLOCK);
994 vn_finished_write(vnmp);
999 * Wait for available vnodes.
1002 getnewvnode_wait(int suspended)
1005 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1006 if (numvnodes > desiredvnodes) {
1009 * File system is beeing suspended, we cannot risk a
1010 * deadlock here, so allocate new vnode anyway.
1012 if (freevnodes > wantfreevnodes)
1013 vnlru_free(freevnodes - wantfreevnodes);
1016 if (vnlruproc_sig == 0) {
1017 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1020 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1023 return (numvnodes > desiredvnodes ? ENFILE : 0);
1027 getnewvnode_reserve(u_int count)
1032 /* First try to be quick and racy. */
1033 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1034 td->td_vp_reserv += count;
1037 atomic_subtract_long(&numvnodes, count);
1039 mtx_lock(&vnode_free_list_mtx);
1041 if (getnewvnode_wait(0) == 0) {
1044 atomic_add_long(&numvnodes, 1);
1047 mtx_unlock(&vnode_free_list_mtx);
1051 getnewvnode_drop_reserve(void)
1056 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1057 td->td_vp_reserv = 0;
1061 * Return the next vnode from the free list.
1064 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1072 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1075 if (td->td_vp_reserv > 0) {
1076 td->td_vp_reserv -= 1;
1079 mtx_lock(&vnode_free_list_mtx);
1081 * Lend our context to reclaim vnodes if they've exceeded the max.
1083 if (freevnodes > wantfreevnodes)
1085 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1087 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1089 mtx_unlock(&vnode_free_list_mtx);
1093 atomic_add_long(&numvnodes, 1);
1094 mtx_unlock(&vnode_free_list_mtx);
1096 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1100 vp->v_vnlock = &vp->v_lock;
1101 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1103 * By default, don't allow shared locks unless filesystems
1106 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE | LK_IS_VNODE);
1108 * Initialize bufobj.
1111 bo->__bo_vnode = vp;
1112 rw_init(BO_LOCKPTR(bo), "bufobj interlock");
1113 bo->bo_ops = &buf_ops_bio;
1114 bo->bo_private = vp;
1115 TAILQ_INIT(&bo->bo_clean.bv_hd);
1116 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1118 * Initialize namecache.
1120 LIST_INIT(&vp->v_cache_src);
1121 TAILQ_INIT(&vp->v_cache_dst);
1123 * Finalize various vnode identity bits.
1128 v_incr_usecount(vp);
1132 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1133 mac_vnode_associate_singlelabel(mp, vp);
1134 else if (mp == NULL && vops != &dead_vnodeops)
1135 printf("NULL mp in getnewvnode()\n");
1138 bo->bo_bsize = mp->mnt_stat.f_iosize;
1139 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1140 vp->v_vflag |= VV_NOKNOTE;
1142 rangelock_init(&vp->v_rl);
1145 * For the filesystems which do not use vfs_hash_insert(),
1146 * still initialize v_hash to have vfs_hash_index() useful.
1147 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1150 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1157 * Delete from old mount point vnode list, if on one.
1160 delmntque(struct vnode *vp)
1170 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1171 ("Active vnode list size %d > Vnode list size %d",
1172 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1173 active = vp->v_iflag & VI_ACTIVE;
1174 vp->v_iflag &= ~VI_ACTIVE;
1176 mtx_lock(&vnode_free_list_mtx);
1177 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1178 mp->mnt_activevnodelistsize--;
1179 mtx_unlock(&vnode_free_list_mtx);
1183 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1184 ("bad mount point vnode list size"));
1185 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1186 mp->mnt_nvnodelistsize--;
1192 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1196 vp->v_op = &dead_vnodeops;
1202 * Insert into list of vnodes for the new mount point, if available.
1205 insmntque1(struct vnode *vp, struct mount *mp,
1206 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1209 KASSERT(vp->v_mount == NULL,
1210 ("insmntque: vnode already on per mount vnode list"));
1211 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1212 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1215 * We acquire the vnode interlock early to ensure that the
1216 * vnode cannot be recycled by another process releasing a
1217 * holdcnt on it before we get it on both the vnode list
1218 * and the active vnode list. The mount mutex protects only
1219 * manipulation of the vnode list and the vnode freelist
1220 * mutex protects only manipulation of the active vnode list.
1221 * Hence the need to hold the vnode interlock throughout.
1225 if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1226 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1227 mp->mnt_nvnodelistsize == 0)) &&
1228 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1237 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1238 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1239 ("neg mount point vnode list size"));
1240 mp->mnt_nvnodelistsize++;
1241 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1242 ("Activating already active vnode"));
1243 vp->v_iflag |= VI_ACTIVE;
1244 mtx_lock(&vnode_free_list_mtx);
1245 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1246 mp->mnt_activevnodelistsize++;
1247 mtx_unlock(&vnode_free_list_mtx);
1254 insmntque(struct vnode *vp, struct mount *mp)
1257 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1261 * Flush out and invalidate all buffers associated with a bufobj
1262 * Called with the underlying object locked.
1265 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1270 if (flags & V_SAVE) {
1271 error = bufobj_wwait(bo, slpflag, slptimeo);
1276 if (bo->bo_dirty.bv_cnt > 0) {
1278 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1281 * XXX We could save a lock/unlock if this was only
1282 * enabled under INVARIANTS
1285 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1286 panic("vinvalbuf: dirty bufs");
1290 * If you alter this loop please notice that interlock is dropped and
1291 * reacquired in flushbuflist. Special care is needed to ensure that
1292 * no race conditions occur from this.
1295 error = flushbuflist(&bo->bo_clean,
1296 flags, bo, slpflag, slptimeo);
1297 if (error == 0 && !(flags & V_CLEANONLY))
1298 error = flushbuflist(&bo->bo_dirty,
1299 flags, bo, slpflag, slptimeo);
1300 if (error != 0 && error != EAGAIN) {
1304 } while (error != 0);
1307 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1308 * have write I/O in-progress but if there is a VM object then the
1309 * VM object can also have read-I/O in-progress.
1312 bufobj_wwait(bo, 0, 0);
1314 if (bo->bo_object != NULL) {
1315 VM_OBJECT_WLOCK(bo->bo_object);
1316 vm_object_pip_wait(bo->bo_object, "bovlbx");
1317 VM_OBJECT_WUNLOCK(bo->bo_object);
1320 } while (bo->bo_numoutput > 0);
1324 * Destroy the copy in the VM cache, too.
1326 if (bo->bo_object != NULL &&
1327 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1328 VM_OBJECT_WLOCK(bo->bo_object);
1329 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1330 OBJPR_CLEANONLY : 0);
1331 VM_OBJECT_WUNLOCK(bo->bo_object);
1336 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1337 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1338 panic("vinvalbuf: flush failed");
1345 * Flush out and invalidate all buffers associated with a vnode.
1346 * Called with the underlying object locked.
1349 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1352 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1353 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1354 if (vp->v_object != NULL && vp->v_object->handle != vp)
1356 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1360 * Flush out buffers on the specified list.
1364 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1367 struct buf *bp, *nbp;
1372 ASSERT_BO_WLOCKED(bo);
1375 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1376 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1377 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1383 lblkno = nbp->b_lblkno;
1384 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1387 error = BUF_TIMELOCK(bp,
1388 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1389 "flushbuf", slpflag, slptimeo);
1392 return (error != ENOLCK ? error : EAGAIN);
1394 KASSERT(bp->b_bufobj == bo,
1395 ("bp %p wrong b_bufobj %p should be %p",
1396 bp, bp->b_bufobj, bo));
1397 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1403 * XXX Since there are no node locks for NFS, I
1404 * believe there is a slight chance that a delayed
1405 * write will occur while sleeping just above, so
1408 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1411 bp->b_flags |= B_ASYNC;
1414 return (EAGAIN); /* XXX: why not loop ? */
1417 bp->b_flags |= (B_INVAL | B_RELBUF);
1418 bp->b_flags &= ~B_ASYNC;
1422 (nbp->b_bufobj != bo ||
1423 nbp->b_lblkno != lblkno ||
1424 (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1425 break; /* nbp invalid */
1431 * Truncate a file's buffer and pages to a specified length. This
1432 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1436 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
1438 struct buf *bp, *nbp;
1443 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1444 vp, cred, blksize, (uintmax_t)length);
1447 * Round up to the *next* lbn.
1449 trunclbn = (length + blksize - 1) / blksize;
1451 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1458 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1459 if (bp->b_lblkno < trunclbn)
1462 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1463 BO_LOCKPTR(bo)) == ENOLCK)
1467 bp->b_flags |= (B_INVAL | B_RELBUF);
1468 bp->b_flags &= ~B_ASYNC;
1474 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1475 (nbp->b_vp != vp) ||
1476 (nbp->b_flags & B_DELWRI))) {
1482 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1483 if (bp->b_lblkno < trunclbn)
1486 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1487 BO_LOCKPTR(bo)) == ENOLCK)
1490 bp->b_flags |= (B_INVAL | B_RELBUF);
1491 bp->b_flags &= ~B_ASYNC;
1497 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1498 (nbp->b_vp != vp) ||
1499 (nbp->b_flags & B_DELWRI) == 0)) {
1508 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1509 if (bp->b_lblkno > 0)
1512 * Since we hold the vnode lock this should only
1513 * fail if we're racing with the buf daemon.
1516 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1517 BO_LOCKPTR(bo)) == ENOLCK) {
1520 VNASSERT((bp->b_flags & B_DELWRI), vp,
1521 ("buf(%p) on dirty queue without DELWRI", bp));
1530 bufobj_wwait(bo, 0, 0);
1532 vnode_pager_setsize(vp, length);
1538 buf_vlist_remove(struct buf *bp)
1542 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1543 ASSERT_BO_WLOCKED(bp->b_bufobj);
1544 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1545 (BX_VNDIRTY|BX_VNCLEAN),
1546 ("buf_vlist_remove: Buf %p is on two lists", bp));
1547 if (bp->b_xflags & BX_VNDIRTY)
1548 bv = &bp->b_bufobj->bo_dirty;
1550 bv = &bp->b_bufobj->bo_clean;
1551 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
1552 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1554 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1558 * Add the buffer to the sorted clean or dirty block list.
1560 * NOTE: xflags is passed as a constant, optimizing this inline function!
1563 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1569 ASSERT_BO_WLOCKED(bo);
1570 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1571 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1572 bp->b_xflags |= xflags;
1573 if (xflags & BX_VNDIRTY)
1579 * Keep the list ordered. Optimize empty list insertion. Assume
1580 * we tend to grow at the tail so lookup_le should usually be cheaper
1583 if (bv->bv_cnt == 0 ||
1584 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
1585 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1586 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
1587 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
1589 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
1590 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
1592 panic("buf_vlist_add: Preallocated nodes insufficient.");
1597 * Lookup a buffer using the splay tree. Note that we specifically avoid
1598 * shadow buffers used in background bitmap writes.
1600 * This code isn't quite efficient as it could be because we are maintaining
1601 * two sorted lists and do not know which list the block resides in.
1603 * During a "make buildworld" the desired buffer is found at one of
1604 * the roots more than 60% of the time. Thus, checking both roots
1605 * before performing either splay eliminates unnecessary splays on the
1606 * first tree splayed.
1609 gbincore(struct bufobj *bo, daddr_t lblkno)
1613 ASSERT_BO_LOCKED(bo);
1614 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
1617 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
1621 * Associate a buffer with a vnode.
1624 bgetvp(struct vnode *vp, struct buf *bp)
1629 ASSERT_BO_WLOCKED(bo);
1630 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1632 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1633 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1634 ("bgetvp: bp already attached! %p", bp));
1640 * Insert onto list for new vnode.
1642 buf_vlist_add(bp, bo, BX_VNCLEAN);
1646 * Disassociate a buffer from a vnode.
1649 brelvp(struct buf *bp)
1654 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1655 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1658 * Delete from old vnode list, if on one.
1660 vp = bp->b_vp; /* XXX */
1663 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1664 buf_vlist_remove(bp);
1666 panic("brelvp: Buffer %p not on queue.", bp);
1667 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1668 bo->bo_flag &= ~BO_ONWORKLST;
1669 mtx_lock(&sync_mtx);
1670 LIST_REMOVE(bo, bo_synclist);
1671 syncer_worklist_len--;
1672 mtx_unlock(&sync_mtx);
1675 bp->b_bufobj = NULL;
1681 * Add an item to the syncer work queue.
1684 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1688 ASSERT_BO_WLOCKED(bo);
1690 mtx_lock(&sync_mtx);
1691 if (bo->bo_flag & BO_ONWORKLST)
1692 LIST_REMOVE(bo, bo_synclist);
1694 bo->bo_flag |= BO_ONWORKLST;
1695 syncer_worklist_len++;
1698 if (delay > syncer_maxdelay - 2)
1699 delay = syncer_maxdelay - 2;
1700 slot = (syncer_delayno + delay) & syncer_mask;
1702 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
1703 mtx_unlock(&sync_mtx);
1707 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1711 mtx_lock(&sync_mtx);
1712 len = syncer_worklist_len - sync_vnode_count;
1713 mtx_unlock(&sync_mtx);
1714 error = SYSCTL_OUT(req, &len, sizeof(len));
1718 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1719 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1721 static struct proc *updateproc;
1722 static void sched_sync(void);
1723 static struct kproc_desc up_kp = {
1728 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1731 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1736 *bo = LIST_FIRST(slp);
1739 vp = (*bo)->__bo_vnode; /* XXX */
1740 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1743 * We use vhold in case the vnode does not
1744 * successfully sync. vhold prevents the vnode from
1745 * going away when we unlock the sync_mtx so that
1746 * we can acquire the vnode interlock.
1749 mtx_unlock(&sync_mtx);
1751 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1753 mtx_lock(&sync_mtx);
1754 return (*bo == LIST_FIRST(slp));
1756 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1757 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1759 vn_finished_write(mp);
1761 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1763 * Put us back on the worklist. The worklist
1764 * routine will remove us from our current
1765 * position and then add us back in at a later
1768 vn_syncer_add_to_worklist(*bo, syncdelay);
1772 mtx_lock(&sync_mtx);
1777 * System filesystem synchronizer daemon.
1782 struct synclist *next, *slp;
1785 struct thread *td = curthread;
1787 int net_worklist_len;
1788 int syncer_final_iter;
1793 syncer_final_iter = 0;
1795 syncer_state = SYNCER_RUNNING;
1796 starttime = time_uptime;
1797 td->td_pflags |= TDP_NORUNNINGBUF;
1799 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1802 mtx_lock(&sync_mtx);
1804 if (syncer_state == SYNCER_FINAL_DELAY &&
1805 syncer_final_iter == 0) {
1806 mtx_unlock(&sync_mtx);
1807 kproc_suspend_check(td->td_proc);
1808 mtx_lock(&sync_mtx);
1810 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1811 if (syncer_state != SYNCER_RUNNING &&
1812 starttime != time_uptime) {
1814 printf("\nSyncing disks, vnodes remaining...");
1817 printf("%d ", net_worklist_len);
1819 starttime = time_uptime;
1822 * Push files whose dirty time has expired. Be careful
1823 * of interrupt race on slp queue.
1825 * Skip over empty worklist slots when shutting down.
1828 slp = &syncer_workitem_pending[syncer_delayno];
1829 syncer_delayno += 1;
1830 if (syncer_delayno == syncer_maxdelay)
1832 next = &syncer_workitem_pending[syncer_delayno];
1834 * If the worklist has wrapped since the
1835 * it was emptied of all but syncer vnodes,
1836 * switch to the FINAL_DELAY state and run
1837 * for one more second.
1839 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1840 net_worklist_len == 0 &&
1841 last_work_seen == syncer_delayno) {
1842 syncer_state = SYNCER_FINAL_DELAY;
1843 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1845 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1846 syncer_worklist_len > 0);
1849 * Keep track of the last time there was anything
1850 * on the worklist other than syncer vnodes.
1851 * Return to the SHUTTING_DOWN state if any
1854 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1855 last_work_seen = syncer_delayno;
1856 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1857 syncer_state = SYNCER_SHUTTING_DOWN;
1858 while (!LIST_EMPTY(slp)) {
1859 error = sync_vnode(slp, &bo, td);
1861 LIST_REMOVE(bo, bo_synclist);
1862 LIST_INSERT_HEAD(next, bo, bo_synclist);
1866 if (first_printf == 0)
1867 wdog_kern_pat(WD_LASTVAL);
1870 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1871 syncer_final_iter--;
1873 * The variable rushjob allows the kernel to speed up the
1874 * processing of the filesystem syncer process. A rushjob
1875 * value of N tells the filesystem syncer to process the next
1876 * N seconds worth of work on its queue ASAP. Currently rushjob
1877 * is used by the soft update code to speed up the filesystem
1878 * syncer process when the incore state is getting so far
1879 * ahead of the disk that the kernel memory pool is being
1880 * threatened with exhaustion.
1887 * Just sleep for a short period of time between
1888 * iterations when shutting down to allow some I/O
1891 * If it has taken us less than a second to process the
1892 * current work, then wait. Otherwise start right over
1893 * again. We can still lose time if any single round
1894 * takes more than two seconds, but it does not really
1895 * matter as we are just trying to generally pace the
1896 * filesystem activity.
1898 if (syncer_state != SYNCER_RUNNING ||
1899 time_uptime == starttime) {
1901 sched_prio(td, PPAUSE);
1904 if (syncer_state != SYNCER_RUNNING)
1905 cv_timedwait(&sync_wakeup, &sync_mtx,
1906 hz / SYNCER_SHUTDOWN_SPEEDUP);
1907 else if (time_uptime == starttime)
1908 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1913 * Request the syncer daemon to speed up its work.
1914 * We never push it to speed up more than half of its
1915 * normal turn time, otherwise it could take over the cpu.
1918 speedup_syncer(void)
1922 mtx_lock(&sync_mtx);
1923 if (rushjob < syncdelay / 2) {
1925 stat_rush_requests += 1;
1928 mtx_unlock(&sync_mtx);
1929 cv_broadcast(&sync_wakeup);
1934 * Tell the syncer to speed up its work and run though its work
1935 * list several times, then tell it to shut down.
1938 syncer_shutdown(void *arg, int howto)
1941 if (howto & RB_NOSYNC)
1943 mtx_lock(&sync_mtx);
1944 syncer_state = SYNCER_SHUTTING_DOWN;
1946 mtx_unlock(&sync_mtx);
1947 cv_broadcast(&sync_wakeup);
1948 kproc_shutdown(arg, howto);
1952 * Reassign a buffer from one vnode to another.
1953 * Used to assign file specific control information
1954 * (indirect blocks) to the vnode to which they belong.
1957 reassignbuf(struct buf *bp)
1970 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
1971 bp, bp->b_vp, bp->b_flags);
1973 * B_PAGING flagged buffers cannot be reassigned because their vp
1974 * is not fully linked in.
1976 if (bp->b_flags & B_PAGING)
1977 panic("cannot reassign paging buffer");
1980 * Delete from old vnode list, if on one.
1983 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1984 buf_vlist_remove(bp);
1986 panic("reassignbuf: Buffer %p not on queue.", bp);
1988 * If dirty, put on list of dirty buffers; otherwise insert onto list
1991 if (bp->b_flags & B_DELWRI) {
1992 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
1993 switch (vp->v_type) {
2003 vn_syncer_add_to_worklist(bo, delay);
2005 buf_vlist_add(bp, bo, BX_VNDIRTY);
2007 buf_vlist_add(bp, bo, BX_VNCLEAN);
2009 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2010 mtx_lock(&sync_mtx);
2011 LIST_REMOVE(bo, bo_synclist);
2012 syncer_worklist_len--;
2013 mtx_unlock(&sync_mtx);
2014 bo->bo_flag &= ~BO_ONWORKLST;
2019 bp = TAILQ_FIRST(&bv->bv_hd);
2020 KASSERT(bp == NULL || bp->b_bufobj == bo,
2021 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2022 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2023 KASSERT(bp == NULL || bp->b_bufobj == bo,
2024 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2026 bp = TAILQ_FIRST(&bv->bv_hd);
2027 KASSERT(bp == NULL || bp->b_bufobj == bo,
2028 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2029 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2030 KASSERT(bp == NULL || bp->b_bufobj == bo,
2031 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2037 * Increment the use and hold counts on the vnode, taking care to reference
2038 * the driver's usecount if this is a chardev. The vholdl() will remove
2039 * the vnode from the free list if it is presently free. Requires the
2040 * vnode interlock and returns with it held.
2043 v_incr_usecount(struct vnode *vp)
2046 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2049 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2051 vp->v_rdev->si_usecount++;
2057 * Turn a holdcnt into a use+holdcnt such that only one call to
2058 * v_decr_usecount is needed.
2061 v_upgrade_usecount(struct vnode *vp)
2064 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2066 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2068 vp->v_rdev->si_usecount++;
2074 * Decrement the vnode use and hold count along with the driver's usecount
2075 * if this is a chardev. The vdropl() below releases the vnode interlock
2076 * as it may free the vnode.
2079 v_decr_usecount(struct vnode *vp)
2082 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2083 VNASSERT(vp->v_usecount > 0, vp,
2084 ("v_decr_usecount: negative usecount"));
2085 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2087 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2089 vp->v_rdev->si_usecount--;
2096 * Decrement only the use count and driver use count. This is intended to
2097 * be paired with a follow on vdropl() to release the remaining hold count.
2098 * In this way we may vgone() a vnode with a 0 usecount without risk of
2099 * having it end up on a free list because the hold count is kept above 0.
2102 v_decr_useonly(struct vnode *vp)
2105 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2106 VNASSERT(vp->v_usecount > 0, vp,
2107 ("v_decr_useonly: negative usecount"));
2108 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2110 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2112 vp->v_rdev->si_usecount--;
2118 * Grab a particular vnode from the free list, increment its
2119 * reference count and lock it. VI_DOOMED is set if the vnode
2120 * is being destroyed. Only callers who specify LK_RETRY will
2121 * see doomed vnodes. If inactive processing was delayed in
2122 * vput try to do it here.
2125 vget(struct vnode *vp, int flags, struct thread *td)
2130 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2131 ("vget: invalid lock operation"));
2132 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2134 if ((flags & LK_INTERLOCK) == 0)
2137 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2139 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2143 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2144 panic("vget: vn_lock failed to return ENOENT\n");
2146 /* Upgrade our holdcnt to a usecount. */
2147 v_upgrade_usecount(vp);
2149 * We don't guarantee that any particular close will
2150 * trigger inactive processing so just make a best effort
2151 * here at preventing a reference to a removed file. If
2152 * we don't succeed no harm is done.
2154 if (vp->v_iflag & VI_OWEINACT) {
2155 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2156 (flags & LK_NOWAIT) == 0)
2158 vp->v_iflag &= ~VI_OWEINACT;
2165 * Increase the reference count of a vnode.
2168 vref(struct vnode *vp)
2171 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2173 v_incr_usecount(vp);
2178 * Return reference count of a vnode.
2180 * The results of this call are only guaranteed when some mechanism other
2181 * than the VI lock is used to stop other processes from gaining references
2182 * to the vnode. This may be the case if the caller holds the only reference.
2183 * This is also useful when stale data is acceptable as race conditions may
2184 * be accounted for by some other means.
2187 vrefcnt(struct vnode *vp)
2192 usecnt = vp->v_usecount;
2198 #define VPUTX_VRELE 1
2199 #define VPUTX_VPUT 2
2200 #define VPUTX_VUNREF 3
2203 vputx(struct vnode *vp, int func)
2207 KASSERT(vp != NULL, ("vputx: null vp"));
2208 if (func == VPUTX_VUNREF)
2209 ASSERT_VOP_LOCKED(vp, "vunref");
2210 else if (func == VPUTX_VPUT)
2211 ASSERT_VOP_LOCKED(vp, "vput");
2213 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2214 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2217 /* Skip this v_writecount check if we're going to panic below. */
2218 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2219 ("vputx: missed vn_close"));
2222 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2223 vp->v_usecount == 1)) {
2224 if (func == VPUTX_VPUT)
2226 v_decr_usecount(vp);
2230 if (vp->v_usecount != 1) {
2231 vprint("vputx: negative ref count", vp);
2232 panic("vputx: negative ref cnt");
2234 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2236 * We want to hold the vnode until the inactive finishes to
2237 * prevent vgone() races. We drop the use count here and the
2238 * hold count below when we're done.
2242 * We must call VOP_INACTIVE with the node locked. Mark
2243 * as VI_DOINGINACT to avoid recursion.
2245 vp->v_iflag |= VI_OWEINACT;
2248 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2252 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2253 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2259 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2260 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2265 if (vp->v_usecount > 0)
2266 vp->v_iflag &= ~VI_OWEINACT;
2268 if (vp->v_iflag & VI_OWEINACT)
2269 vinactive(vp, curthread);
2270 if (func != VPUTX_VUNREF)
2277 * Vnode put/release.
2278 * If count drops to zero, call inactive routine and return to freelist.
2281 vrele(struct vnode *vp)
2284 vputx(vp, VPUTX_VRELE);
2288 * Release an already locked vnode. This give the same effects as
2289 * unlock+vrele(), but takes less time and avoids releasing and
2290 * re-aquiring the lock (as vrele() acquires the lock internally.)
2293 vput(struct vnode *vp)
2296 vputx(vp, VPUTX_VPUT);
2300 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2303 vunref(struct vnode *vp)
2306 vputx(vp, VPUTX_VUNREF);
2310 * Somebody doesn't want the vnode recycled.
2313 vhold(struct vnode *vp)
2322 * Increase the hold count and activate if this is the first reference.
2325 vholdl(struct vnode *vp)
2329 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2331 /* getnewvnode() calls v_incr_usecount() without holding interlock. */
2332 if (vp->v_type != VNON || vp->v_data != NULL) {
2333 ASSERT_VI_LOCKED(vp, "vholdl");
2334 VNASSERT(vp->v_holdcnt > 0 || (vp->v_iflag & VI_FREE) != 0,
2335 vp, ("vholdl: free vnode is held"));
2339 if ((vp->v_iflag & VI_FREE) == 0)
2341 VNASSERT(vp->v_holdcnt == 1, vp, ("vholdl: wrong hold count"));
2342 VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed."));
2344 * Remove a vnode from the free list, mark it as in use,
2345 * and put it on the active list.
2347 mtx_lock(&vnode_free_list_mtx);
2348 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2350 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2351 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2352 ("Activating already active vnode"));
2353 vp->v_iflag |= VI_ACTIVE;
2355 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2356 mp->mnt_activevnodelistsize++;
2357 mtx_unlock(&vnode_free_list_mtx);
2361 * Note that there is one less who cares about this vnode.
2362 * vdrop() is the opposite of vhold().
2365 vdrop(struct vnode *vp)
2373 * Drop the hold count of the vnode. If this is the last reference to
2374 * the vnode we place it on the free list unless it has been vgone'd
2375 * (marked VI_DOOMED) in which case we will free it.
2378 vdropl(struct vnode *vp)
2384 ASSERT_VI_LOCKED(vp, "vdropl");
2385 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2386 if (vp->v_holdcnt <= 0)
2387 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2389 if (vp->v_holdcnt > 0) {
2393 if ((vp->v_iflag & VI_DOOMED) == 0) {
2395 * Mark a vnode as free: remove it from its active list
2396 * and put it up for recycling on the freelist.
2398 VNASSERT(vp->v_op != NULL, vp,
2399 ("vdropl: vnode already reclaimed."));
2400 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2401 ("vnode already free"));
2402 VNASSERT(vp->v_holdcnt == 0, vp,
2403 ("vdropl: freeing when we shouldn't"));
2404 active = vp->v_iflag & VI_ACTIVE;
2405 vp->v_iflag &= ~VI_ACTIVE;
2407 mtx_lock(&vnode_free_list_mtx);
2409 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2411 mp->mnt_activevnodelistsize--;
2413 if (vp->v_iflag & VI_AGE) {
2414 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_actfreelist);
2416 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
2419 vp->v_iflag &= ~VI_AGE;
2420 vp->v_iflag |= VI_FREE;
2421 mtx_unlock(&vnode_free_list_mtx);
2426 * The vnode has been marked for destruction, so free it.
2428 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2429 atomic_subtract_long(&numvnodes, 1);
2431 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2432 ("cleaned vnode still on the free list."));
2433 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2434 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2435 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2436 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2437 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2438 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2439 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
2440 ("clean blk trie not empty"));
2441 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2442 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
2443 ("dirty blk trie not empty"));
2444 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2445 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2446 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2449 mac_vnode_destroy(vp);
2451 if (vp->v_pollinfo != NULL)
2452 destroy_vpollinfo(vp->v_pollinfo);
2454 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2457 rangelock_destroy(&vp->v_rl);
2458 lockdestroy(vp->v_vnlock);
2459 mtx_destroy(&vp->v_interlock);
2460 rw_destroy(BO_LOCKPTR(bo));
2461 uma_zfree(vnode_zone, vp);
2465 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2466 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2467 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2468 * failed lock upgrade.
2471 vinactive(struct vnode *vp, struct thread *td)
2473 struct vm_object *obj;
2475 ASSERT_VOP_ELOCKED(vp, "vinactive");
2476 ASSERT_VI_LOCKED(vp, "vinactive");
2477 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2478 ("vinactive: recursed on VI_DOINGINACT"));
2479 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2480 vp->v_iflag |= VI_DOINGINACT;
2481 vp->v_iflag &= ~VI_OWEINACT;
2484 * Before moving off the active list, we must be sure that any
2485 * modified pages are on the vnode's dirty list since these will
2486 * no longer be checked once the vnode is on the inactive list.
2487 * Because the vnode vm object keeps a hold reference on the vnode
2488 * if there is at least one resident non-cached page, the vnode
2489 * cannot leave the active list without the page cleanup done.
2492 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
2493 VM_OBJECT_WLOCK(obj);
2494 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
2495 VM_OBJECT_WUNLOCK(obj);
2497 VOP_INACTIVE(vp, td);
2499 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2500 ("vinactive: lost VI_DOINGINACT"));
2501 vp->v_iflag &= ~VI_DOINGINACT;
2505 * Remove any vnodes in the vnode table belonging to mount point mp.
2507 * If FORCECLOSE is not specified, there should not be any active ones,
2508 * return error if any are found (nb: this is a user error, not a
2509 * system error). If FORCECLOSE is specified, detach any active vnodes
2512 * If WRITECLOSE is set, only flush out regular file vnodes open for
2515 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2517 * `rootrefs' specifies the base reference count for the root vnode
2518 * of this filesystem. The root vnode is considered busy if its
2519 * v_usecount exceeds this value. On a successful return, vflush(, td)
2520 * will call vrele() on the root vnode exactly rootrefs times.
2521 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2525 static int busyprt = 0; /* print out busy vnodes */
2526 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2530 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2532 struct vnode *vp, *mvp, *rootvp = NULL;
2534 int busy = 0, error;
2536 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2539 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2540 ("vflush: bad args"));
2542 * Get the filesystem root vnode. We can vput() it
2543 * immediately, since with rootrefs > 0, it won't go away.
2545 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2546 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2553 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2555 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2558 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2562 * Skip over a vnodes marked VV_SYSTEM.
2564 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2570 * If WRITECLOSE is set, flush out unlinked but still open
2571 * files (even if open only for reading) and regular file
2572 * vnodes open for writing.
2574 if (flags & WRITECLOSE) {
2575 if (vp->v_object != NULL) {
2576 VM_OBJECT_WLOCK(vp->v_object);
2577 vm_object_page_clean(vp->v_object, 0, 0, 0);
2578 VM_OBJECT_WUNLOCK(vp->v_object);
2580 error = VOP_FSYNC(vp, MNT_WAIT, td);
2584 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2587 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2590 if ((vp->v_type == VNON ||
2591 (error == 0 && vattr.va_nlink > 0)) &&
2592 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2600 * With v_usecount == 0, all we need to do is clear out the
2601 * vnode data structures and we are done.
2603 * If FORCECLOSE is set, forcibly close the vnode.
2605 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2606 VNASSERT(vp->v_usecount == 0 ||
2607 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2608 ("device VNODE %p is FORCECLOSED", vp));
2614 vprint("vflush: busy vnode", vp);
2620 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2622 * If just the root vnode is busy, and if its refcount
2623 * is equal to `rootrefs', then go ahead and kill it.
2626 KASSERT(busy > 0, ("vflush: not busy"));
2627 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2628 ("vflush: usecount %d < rootrefs %d",
2629 rootvp->v_usecount, rootrefs));
2630 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2631 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2633 VOP_UNLOCK(rootvp, 0);
2639 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2643 for (; rootrefs > 0; rootrefs--)
2649 * Recycle an unused vnode to the front of the free list.
2652 vrecycle(struct vnode *vp)
2656 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2657 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2660 if (vp->v_usecount == 0) {
2669 * Eliminate all activity associated with a vnode
2670 * in preparation for reuse.
2673 vgone(struct vnode *vp)
2681 notify_lowervp_vfs_dummy(struct mount *mp __unused,
2682 struct vnode *lowervp __unused)
2687 * Notify upper mounts about reclaimed or unlinked vnode.
2690 vfs_notify_upper(struct vnode *vp, int event)
2692 static struct vfsops vgonel_vfsops = {
2693 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
2694 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
2696 struct mount *mp, *ump, *mmp;
2703 if (TAILQ_EMPTY(&mp->mnt_uppers))
2706 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
2707 mmp->mnt_op = &vgonel_vfsops;
2708 mmp->mnt_kern_flag |= MNTK_MARKER;
2710 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
2711 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
2712 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
2713 ump = TAILQ_NEXT(ump, mnt_upper_link);
2716 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
2719 case VFS_NOTIFY_UPPER_RECLAIM:
2720 VFS_RECLAIM_LOWERVP(ump, vp);
2722 case VFS_NOTIFY_UPPER_UNLINK:
2723 VFS_UNLINK_LOWERVP(ump, vp);
2726 KASSERT(0, ("invalid event %d", event));
2730 ump = TAILQ_NEXT(mmp, mnt_upper_link);
2731 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
2734 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
2735 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
2736 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
2737 wakeup(&mp->mnt_uppers);
2744 * vgone, with the vp interlock held.
2747 vgonel(struct vnode *vp)
2754 ASSERT_VOP_ELOCKED(vp, "vgonel");
2755 ASSERT_VI_LOCKED(vp, "vgonel");
2756 VNASSERT(vp->v_holdcnt, vp,
2757 ("vgonel: vp %p has no reference.", vp));
2758 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2762 * Don't vgonel if we're already doomed.
2764 if (vp->v_iflag & VI_DOOMED)
2766 vp->v_iflag |= VI_DOOMED;
2769 * Check to see if the vnode is in use. If so, we have to call
2770 * VOP_CLOSE() and VOP_INACTIVE().
2772 active = vp->v_usecount;
2773 oweinact = (vp->v_iflag & VI_OWEINACT);
2775 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
2778 * Clean out any buffers associated with the vnode.
2779 * If the flush fails, just toss the buffers.
2782 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2783 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2784 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2785 vinvalbuf(vp, 0, 0, 0);
2788 * If purging an active vnode, it must be closed and
2789 * deactivated before being reclaimed.
2792 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2793 if (oweinact || active) {
2795 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2799 if (vp->v_type == VSOCK)
2800 vfs_unp_reclaim(vp);
2802 * Reclaim the vnode.
2804 if (VOP_RECLAIM(vp, td))
2805 panic("vgone: cannot reclaim");
2807 vn_finished_secondary_write(mp);
2808 VNASSERT(vp->v_object == NULL, vp,
2809 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2811 * Clear the advisory locks and wake up waiting threads.
2813 (void)VOP_ADVLOCKPURGE(vp);
2815 * Delete from old mount point vnode list.
2820 * Done with purge, reset to the standard lock and invalidate
2824 vp->v_vnlock = &vp->v_lock;
2825 vp->v_op = &dead_vnodeops;
2831 * Calculate the total number of references to a special device.
2834 vcount(struct vnode *vp)
2839 count = vp->v_rdev->si_usecount;
2845 * Same as above, but using the struct cdev *as argument
2848 count_dev(struct cdev *dev)
2853 count = dev->si_usecount;
2859 * Print out a description of a vnode.
2861 static char *typename[] =
2862 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2866 vn_printf(struct vnode *vp, const char *fmt, ...)
2869 char buf[256], buf2[16];
2875 printf("%p: ", (void *)vp);
2876 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2877 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2878 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2881 if (vp->v_vflag & VV_ROOT)
2882 strlcat(buf, "|VV_ROOT", sizeof(buf));
2883 if (vp->v_vflag & VV_ISTTY)
2884 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2885 if (vp->v_vflag & VV_NOSYNC)
2886 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2887 if (vp->v_vflag & VV_ETERNALDEV)
2888 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
2889 if (vp->v_vflag & VV_CACHEDLABEL)
2890 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2891 if (vp->v_vflag & VV_TEXT)
2892 strlcat(buf, "|VV_TEXT", sizeof(buf));
2893 if (vp->v_vflag & VV_COPYONWRITE)
2894 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2895 if (vp->v_vflag & VV_SYSTEM)
2896 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2897 if (vp->v_vflag & VV_PROCDEP)
2898 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2899 if (vp->v_vflag & VV_NOKNOTE)
2900 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2901 if (vp->v_vflag & VV_DELETED)
2902 strlcat(buf, "|VV_DELETED", sizeof(buf));
2903 if (vp->v_vflag & VV_MD)
2904 strlcat(buf, "|VV_MD", sizeof(buf));
2905 if (vp->v_vflag & VV_FORCEINSMQ)
2906 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
2907 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
2908 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2909 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
2911 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2912 strlcat(buf, buf2, sizeof(buf));
2914 if (vp->v_iflag & VI_MOUNT)
2915 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2916 if (vp->v_iflag & VI_AGE)
2917 strlcat(buf, "|VI_AGE", sizeof(buf));
2918 if (vp->v_iflag & VI_DOOMED)
2919 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2920 if (vp->v_iflag & VI_FREE)
2921 strlcat(buf, "|VI_FREE", sizeof(buf));
2922 if (vp->v_iflag & VI_ACTIVE)
2923 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
2924 if (vp->v_iflag & VI_DOINGINACT)
2925 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2926 if (vp->v_iflag & VI_OWEINACT)
2927 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2928 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2929 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
2931 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2932 strlcat(buf, buf2, sizeof(buf));
2934 printf(" flags (%s)\n", buf + 1);
2935 if (mtx_owned(VI_MTX(vp)))
2936 printf(" VI_LOCKed");
2937 if (vp->v_object != NULL)
2938 printf(" v_object %p ref %d pages %d "
2939 "cleanbuf %d dirtybuf %d\n",
2940 vp->v_object, vp->v_object->ref_count,
2941 vp->v_object->resident_page_count,
2942 vp->v_bufobj.bo_dirty.bv_cnt,
2943 vp->v_bufobj.bo_clean.bv_cnt);
2945 lockmgr_printinfo(vp->v_vnlock);
2946 if (vp->v_data != NULL)
2952 * List all of the locked vnodes in the system.
2953 * Called when debugging the kernel.
2955 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2961 * Note: because this is DDB, we can't obey the locking semantics
2962 * for these structures, which means we could catch an inconsistent
2963 * state and dereference a nasty pointer. Not much to be done
2966 db_printf("Locked vnodes\n");
2967 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2968 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2969 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
2976 * Show details about the given vnode.
2978 DB_SHOW_COMMAND(vnode, db_show_vnode)
2984 vp = (struct vnode *)addr;
2985 vn_printf(vp, "vnode ");
2989 * Show details about the given mount point.
2991 DB_SHOW_COMMAND(mount, db_show_mount)
3002 /* No address given, print short info about all mount points. */
3003 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3004 db_printf("%p %s on %s (%s)\n", mp,
3005 mp->mnt_stat.f_mntfromname,
3006 mp->mnt_stat.f_mntonname,
3007 mp->mnt_stat.f_fstypename);
3011 db_printf("\nMore info: show mount <addr>\n");
3015 mp = (struct mount *)addr;
3016 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3017 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3020 mflags = mp->mnt_flag;
3021 #define MNT_FLAG(flag) do { \
3022 if (mflags & (flag)) { \
3023 if (buf[0] != '\0') \
3024 strlcat(buf, ", ", sizeof(buf)); \
3025 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3026 mflags &= ~(flag); \
3029 MNT_FLAG(MNT_RDONLY);
3030 MNT_FLAG(MNT_SYNCHRONOUS);
3031 MNT_FLAG(MNT_NOEXEC);
3032 MNT_FLAG(MNT_NOSUID);
3033 MNT_FLAG(MNT_NFS4ACLS);
3034 MNT_FLAG(MNT_UNION);
3035 MNT_FLAG(MNT_ASYNC);
3036 MNT_FLAG(MNT_SUIDDIR);
3037 MNT_FLAG(MNT_SOFTDEP);
3038 MNT_FLAG(MNT_NOSYMFOLLOW);
3039 MNT_FLAG(MNT_GJOURNAL);
3040 MNT_FLAG(MNT_MULTILABEL);
3042 MNT_FLAG(MNT_NOATIME);
3043 MNT_FLAG(MNT_NOCLUSTERR);
3044 MNT_FLAG(MNT_NOCLUSTERW);
3046 MNT_FLAG(MNT_EXRDONLY);
3047 MNT_FLAG(MNT_EXPORTED);
3048 MNT_FLAG(MNT_DEFEXPORTED);
3049 MNT_FLAG(MNT_EXPORTANON);
3050 MNT_FLAG(MNT_EXKERB);
3051 MNT_FLAG(MNT_EXPUBLIC);
3052 MNT_FLAG(MNT_LOCAL);
3053 MNT_FLAG(MNT_QUOTA);
3054 MNT_FLAG(MNT_ROOTFS);
3056 MNT_FLAG(MNT_IGNORE);
3057 MNT_FLAG(MNT_UPDATE);
3058 MNT_FLAG(MNT_DELEXPORT);
3059 MNT_FLAG(MNT_RELOAD);
3060 MNT_FLAG(MNT_FORCE);
3061 MNT_FLAG(MNT_SNAPSHOT);
3062 MNT_FLAG(MNT_BYFSID);
3066 strlcat(buf, ", ", sizeof(buf));
3067 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3068 "0x%016jx", mflags);
3070 db_printf(" mnt_flag = %s\n", buf);
3073 flags = mp->mnt_kern_flag;
3074 #define MNT_KERN_FLAG(flag) do { \
3075 if (flags & (flag)) { \
3076 if (buf[0] != '\0') \
3077 strlcat(buf, ", ", sizeof(buf)); \
3078 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3082 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3083 MNT_KERN_FLAG(MNTK_ASYNC);
3084 MNT_KERN_FLAG(MNTK_SOFTDEP);
3085 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3086 MNT_KERN_FLAG(MNTK_DRAINING);
3087 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3088 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3089 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3090 MNT_KERN_FLAG(MNTK_NO_IOPF);
3091 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3092 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3093 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3094 MNT_KERN_FLAG(MNTK_MARKER);
3095 MNT_KERN_FLAG(MNTK_NOASYNC);
3096 MNT_KERN_FLAG(MNTK_UNMOUNT);
3097 MNT_KERN_FLAG(MNTK_MWAIT);
3098 MNT_KERN_FLAG(MNTK_SUSPEND);
3099 MNT_KERN_FLAG(MNTK_SUSPEND2);
3100 MNT_KERN_FLAG(MNTK_SUSPENDED);
3101 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3102 MNT_KERN_FLAG(MNTK_NOKNOTE);
3103 #undef MNT_KERN_FLAG
3106 strlcat(buf, ", ", sizeof(buf));
3107 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3110 db_printf(" mnt_kern_flag = %s\n", buf);
3112 db_printf(" mnt_opt = ");
3113 opt = TAILQ_FIRST(mp->mnt_opt);
3115 db_printf("%s", opt->name);
3116 opt = TAILQ_NEXT(opt, link);
3117 while (opt != NULL) {
3118 db_printf(", %s", opt->name);
3119 opt = TAILQ_NEXT(opt, link);
3125 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3126 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3127 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3128 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3129 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3130 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3131 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3132 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3133 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3134 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3135 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3136 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3138 db_printf(" mnt_cred = { uid=%u ruid=%u",
3139 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3140 if (jailed(mp->mnt_cred))
3141 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3143 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3144 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3145 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3146 db_printf(" mnt_activevnodelistsize = %d\n",
3147 mp->mnt_activevnodelistsize);
3148 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3149 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3150 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3151 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3152 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3153 db_printf(" mnt_secondary_accwrites = %d\n",
3154 mp->mnt_secondary_accwrites);
3155 db_printf(" mnt_gjprovider = %s\n",
3156 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3158 db_printf("\n\nList of active vnodes\n");
3159 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3160 if (vp->v_type != VMARKER) {
3161 vn_printf(vp, "vnode ");
3166 db_printf("\n\nList of inactive vnodes\n");
3167 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3168 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3169 vn_printf(vp, "vnode ");
3178 * Fill in a struct xvfsconf based on a struct vfsconf.
3181 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3183 struct xvfsconf xvfsp;
3185 bzero(&xvfsp, sizeof(xvfsp));
3186 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3187 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3188 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3189 xvfsp.vfc_flags = vfsp->vfc_flags;
3191 * These are unused in userland, we keep them
3192 * to not break binary compatibility.
3194 xvfsp.vfc_vfsops = NULL;
3195 xvfsp.vfc_next = NULL;
3196 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3199 #ifdef COMPAT_FREEBSD32
3201 uint32_t vfc_vfsops;
3202 char vfc_name[MFSNAMELEN];
3203 int32_t vfc_typenum;
3204 int32_t vfc_refcount;
3210 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3212 struct xvfsconf32 xvfsp;
3214 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3215 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3216 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3217 xvfsp.vfc_flags = vfsp->vfc_flags;
3218 xvfsp.vfc_vfsops = 0;
3220 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3225 * Top level filesystem related information gathering.
3228 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3230 struct vfsconf *vfsp;
3235 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3236 #ifdef COMPAT_FREEBSD32
3237 if (req->flags & SCTL_MASK32)
3238 error = vfsconf2x32(req, vfsp);
3241 error = vfsconf2x(req, vfsp);
3249 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3250 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3251 "S,xvfsconf", "List of all configured filesystems");
3253 #ifndef BURN_BRIDGES
3254 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3257 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3259 int *name = (int *)arg1 - 1; /* XXX */
3260 u_int namelen = arg2 + 1; /* XXX */
3261 struct vfsconf *vfsp;
3263 log(LOG_WARNING, "userland calling deprecated sysctl, "
3264 "please rebuild world\n");
3266 #if 1 || defined(COMPAT_PRELITE2)
3267 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3269 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3273 case VFS_MAXTYPENUM:
3276 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3279 return (ENOTDIR); /* overloaded */
3281 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3282 if (vfsp->vfc_typenum == name[2])
3287 return (EOPNOTSUPP);
3288 #ifdef COMPAT_FREEBSD32
3289 if (req->flags & SCTL_MASK32)
3290 return (vfsconf2x32(req, vfsp));
3293 return (vfsconf2x(req, vfsp));
3295 return (EOPNOTSUPP);
3298 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
3299 CTLFLAG_MPSAFE, vfs_sysctl,
3300 "Generic filesystem");
3302 #if 1 || defined(COMPAT_PRELITE2)
3305 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3308 struct vfsconf *vfsp;
3309 struct ovfsconf ovfs;
3312 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3313 bzero(&ovfs, sizeof(ovfs));
3314 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3315 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3316 ovfs.vfc_index = vfsp->vfc_typenum;
3317 ovfs.vfc_refcount = vfsp->vfc_refcount;
3318 ovfs.vfc_flags = vfsp->vfc_flags;
3319 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3329 #endif /* 1 || COMPAT_PRELITE2 */
3330 #endif /* !BURN_BRIDGES */
3332 #define KINFO_VNODESLOP 10
3335 * Dump vnode list (via sysctl).
3339 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3347 * Stale numvnodes access is not fatal here.
3350 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3352 /* Make an estimate */
3353 return (SYSCTL_OUT(req, 0, len));
3355 error = sysctl_wire_old_buffer(req, 0);
3358 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3360 mtx_lock(&mountlist_mtx);
3361 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3362 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3365 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3369 xvn[n].xv_size = sizeof *xvn;
3370 xvn[n].xv_vnode = vp;
3371 xvn[n].xv_id = 0; /* XXX compat */
3372 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3374 XV_COPY(writecount);
3380 xvn[n].xv_flag = vp->v_vflag;
3382 switch (vp->v_type) {
3389 if (vp->v_rdev == NULL) {
3393 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3396 xvn[n].xv_socket = vp->v_socket;
3399 xvn[n].xv_fifo = vp->v_fifoinfo;
3404 /* shouldn't happen? */
3412 mtx_lock(&mountlist_mtx);
3417 mtx_unlock(&mountlist_mtx);
3419 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3424 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
3425 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
3430 * Unmount all filesystems. The list is traversed in reverse order
3431 * of mounting to avoid dependencies.
3434 vfs_unmountall(void)
3440 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3444 * Since this only runs when rebooting, it is not interlocked.
3446 while(!TAILQ_EMPTY(&mountlist)) {
3447 mp = TAILQ_LAST(&mountlist, mntlist);
3448 error = dounmount(mp, MNT_FORCE, td);
3450 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3452 * XXX: Due to the way in which we mount the root
3453 * file system off of devfs, devfs will generate a
3454 * "busy" warning when we try to unmount it before
3455 * the root. Don't print a warning as a result in
3456 * order to avoid false positive errors that may
3457 * cause needless upset.
3459 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3460 printf("unmount of %s failed (",
3461 mp->mnt_stat.f_mntonname);
3465 printf("%d)\n", error);
3468 /* The unmount has removed mp from the mountlist */
3474 * perform msync on all vnodes under a mount point
3475 * the mount point must be locked.
3478 vfs_msync(struct mount *mp, int flags)
3480 struct vnode *vp, *mvp;
3481 struct vm_object *obj;
3483 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3484 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
3486 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3487 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3489 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3491 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3498 VM_OBJECT_WLOCK(obj);
3499 vm_object_page_clean(obj, 0, 0,
3501 OBJPC_SYNC : OBJPC_NOSYNC);
3502 VM_OBJECT_WUNLOCK(obj);
3512 destroy_vpollinfo_free(struct vpollinfo *vi)
3515 knlist_destroy(&vi->vpi_selinfo.si_note);
3516 mtx_destroy(&vi->vpi_lock);
3517 uma_zfree(vnodepoll_zone, vi);
3521 destroy_vpollinfo(struct vpollinfo *vi)
3524 knlist_clear(&vi->vpi_selinfo.si_note, 1);
3525 seldrain(&vi->vpi_selinfo);
3526 destroy_vpollinfo_free(vi);
3530 * Initalize per-vnode helper structure to hold poll-related state.
3533 v_addpollinfo(struct vnode *vp)
3535 struct vpollinfo *vi;
3537 if (vp->v_pollinfo != NULL)
3539 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3540 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3541 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3542 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3544 if (vp->v_pollinfo != NULL) {
3546 destroy_vpollinfo_free(vi);
3549 vp->v_pollinfo = vi;
3554 * Record a process's interest in events which might happen to
3555 * a vnode. Because poll uses the historic select-style interface
3556 * internally, this routine serves as both the ``check for any
3557 * pending events'' and the ``record my interest in future events''
3558 * functions. (These are done together, while the lock is held,
3559 * to avoid race conditions.)
3562 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3566 mtx_lock(&vp->v_pollinfo->vpi_lock);
3567 if (vp->v_pollinfo->vpi_revents & events) {
3569 * This leaves events we are not interested
3570 * in available for the other process which
3571 * which presumably had requested them
3572 * (otherwise they would never have been
3575 events &= vp->v_pollinfo->vpi_revents;
3576 vp->v_pollinfo->vpi_revents &= ~events;
3578 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3581 vp->v_pollinfo->vpi_events |= events;
3582 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3583 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3588 * Routine to create and manage a filesystem syncer vnode.
3590 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3591 static int sync_fsync(struct vop_fsync_args *);
3592 static int sync_inactive(struct vop_inactive_args *);
3593 static int sync_reclaim(struct vop_reclaim_args *);
3595 static struct vop_vector sync_vnodeops = {
3596 .vop_bypass = VOP_EOPNOTSUPP,
3597 .vop_close = sync_close, /* close */
3598 .vop_fsync = sync_fsync, /* fsync */
3599 .vop_inactive = sync_inactive, /* inactive */
3600 .vop_reclaim = sync_reclaim, /* reclaim */
3601 .vop_lock1 = vop_stdlock, /* lock */
3602 .vop_unlock = vop_stdunlock, /* unlock */
3603 .vop_islocked = vop_stdislocked, /* islocked */
3607 * Create a new filesystem syncer vnode for the specified mount point.
3610 vfs_allocate_syncvnode(struct mount *mp)
3614 static long start, incr, next;
3617 /* Allocate a new vnode */
3618 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3620 panic("vfs_allocate_syncvnode: getnewvnode() failed");
3622 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3623 vp->v_vflag |= VV_FORCEINSMQ;
3624 error = insmntque(vp, mp);
3626 panic("vfs_allocate_syncvnode: insmntque() failed");
3627 vp->v_vflag &= ~VV_FORCEINSMQ;
3630 * Place the vnode onto the syncer worklist. We attempt to
3631 * scatter them about on the list so that they will go off
3632 * at evenly distributed times even if all the filesystems
3633 * are mounted at once.
3636 if (next == 0 || next > syncer_maxdelay) {
3640 start = syncer_maxdelay / 2;
3641 incr = syncer_maxdelay;
3647 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3648 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3649 mtx_lock(&sync_mtx);
3651 if (mp->mnt_syncer == NULL) {
3652 mp->mnt_syncer = vp;
3655 mtx_unlock(&sync_mtx);
3658 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3665 vfs_deallocate_syncvnode(struct mount *mp)
3669 mtx_lock(&sync_mtx);
3670 vp = mp->mnt_syncer;
3672 mp->mnt_syncer = NULL;
3673 mtx_unlock(&sync_mtx);
3679 * Do a lazy sync of the filesystem.
3682 sync_fsync(struct vop_fsync_args *ap)
3684 struct vnode *syncvp = ap->a_vp;
3685 struct mount *mp = syncvp->v_mount;
3690 * We only need to do something if this is a lazy evaluation.
3692 if (ap->a_waitfor != MNT_LAZY)
3696 * Move ourselves to the back of the sync list.
3698 bo = &syncvp->v_bufobj;
3700 vn_syncer_add_to_worklist(bo, syncdelay);
3704 * Walk the list of vnodes pushing all that are dirty and
3705 * not already on the sync list.
3707 if (vfs_busy(mp, MBF_NOWAIT) != 0)
3709 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3713 save = curthread_pflags_set(TDP_SYNCIO);
3714 vfs_msync(mp, MNT_NOWAIT);
3715 error = VFS_SYNC(mp, MNT_LAZY);
3716 curthread_pflags_restore(save);
3717 vn_finished_write(mp);
3723 * The syncer vnode is no referenced.
3726 sync_inactive(struct vop_inactive_args *ap)
3734 * The syncer vnode is no longer needed and is being decommissioned.
3736 * Modifications to the worklist must be protected by sync_mtx.
3739 sync_reclaim(struct vop_reclaim_args *ap)
3741 struct vnode *vp = ap->a_vp;
3746 mtx_lock(&sync_mtx);
3747 if (vp->v_mount->mnt_syncer == vp)
3748 vp->v_mount->mnt_syncer = NULL;
3749 if (bo->bo_flag & BO_ONWORKLST) {
3750 LIST_REMOVE(bo, bo_synclist);
3751 syncer_worklist_len--;
3753 bo->bo_flag &= ~BO_ONWORKLST;
3755 mtx_unlock(&sync_mtx);
3762 * Check if vnode represents a disk device
3765 vn_isdisk(struct vnode *vp, int *errp)
3771 if (vp->v_type != VCHR)
3773 else if (vp->v_rdev == NULL)
3775 else if (vp->v_rdev->si_devsw == NULL)
3777 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3782 return (error == 0);
3786 * Common filesystem object access control check routine. Accepts a
3787 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3788 * and optional call-by-reference privused argument allowing vaccess()
3789 * to indicate to the caller whether privilege was used to satisfy the
3790 * request (obsoleted). Returns 0 on success, or an errno on failure.
3793 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3794 accmode_t accmode, struct ucred *cred, int *privused)
3796 accmode_t dac_granted;
3797 accmode_t priv_granted;
3799 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3800 ("invalid bit in accmode"));
3801 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3802 ("VAPPEND without VWRITE"));
3805 * Look for a normal, non-privileged way to access the file/directory
3806 * as requested. If it exists, go with that.
3809 if (privused != NULL)
3814 /* Check the owner. */
3815 if (cred->cr_uid == file_uid) {
3816 dac_granted |= VADMIN;
3817 if (file_mode & S_IXUSR)
3818 dac_granted |= VEXEC;
3819 if (file_mode & S_IRUSR)
3820 dac_granted |= VREAD;
3821 if (file_mode & S_IWUSR)
3822 dac_granted |= (VWRITE | VAPPEND);
3824 if ((accmode & dac_granted) == accmode)
3830 /* Otherwise, check the groups (first match) */
3831 if (groupmember(file_gid, cred)) {
3832 if (file_mode & S_IXGRP)
3833 dac_granted |= VEXEC;
3834 if (file_mode & S_IRGRP)
3835 dac_granted |= VREAD;
3836 if (file_mode & S_IWGRP)
3837 dac_granted |= (VWRITE | VAPPEND);
3839 if ((accmode & dac_granted) == accmode)
3845 /* Otherwise, check everyone else. */
3846 if (file_mode & S_IXOTH)
3847 dac_granted |= VEXEC;
3848 if (file_mode & S_IROTH)
3849 dac_granted |= VREAD;
3850 if (file_mode & S_IWOTH)
3851 dac_granted |= (VWRITE | VAPPEND);
3852 if ((accmode & dac_granted) == accmode)
3857 * Build a privilege mask to determine if the set of privileges
3858 * satisfies the requirements when combined with the granted mask
3859 * from above. For each privilege, if the privilege is required,
3860 * bitwise or the request type onto the priv_granted mask.
3866 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3867 * requests, instead of PRIV_VFS_EXEC.
3869 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3870 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3871 priv_granted |= VEXEC;
3874 * Ensure that at least one execute bit is on. Otherwise,
3875 * a privileged user will always succeed, and we don't want
3876 * this to happen unless the file really is executable.
3878 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3879 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
3880 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3881 priv_granted |= VEXEC;
3884 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3885 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3886 priv_granted |= VREAD;
3888 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3889 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3890 priv_granted |= (VWRITE | VAPPEND);
3892 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3893 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3894 priv_granted |= VADMIN;
3896 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3897 /* XXX audit: privilege used */
3898 if (privused != NULL)
3903 return ((accmode & VADMIN) ? EPERM : EACCES);
3907 * Credential check based on process requesting service, and per-attribute
3911 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3912 struct thread *td, accmode_t accmode)
3916 * Kernel-invoked always succeeds.
3922 * Do not allow privileged processes in jail to directly manipulate
3923 * system attributes.
3925 switch (attrnamespace) {
3926 case EXTATTR_NAMESPACE_SYSTEM:
3927 /* Potentially should be: return (EPERM); */
3928 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3929 case EXTATTR_NAMESPACE_USER:
3930 return (VOP_ACCESS(vp, accmode, cred, td));
3936 #ifdef DEBUG_VFS_LOCKS
3938 * This only exists to supress warnings from unlocked specfs accesses. It is
3939 * no longer ok to have an unlocked VFS.
3941 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3942 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3944 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3945 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
3946 "Drop into debugger on lock violation");
3948 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3949 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
3950 0, "Check for interlock across VOPs");
3952 int vfs_badlock_print = 1; /* Print lock violations. */
3953 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
3954 0, "Print lock violations");
3957 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3958 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
3959 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
3963 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3967 if (vfs_badlock_backtrace)
3970 if (vfs_badlock_print)
3971 printf("%s: %p %s\n", str, (void *)vp, msg);
3972 if (vfs_badlock_ddb)
3973 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3977 assert_vi_locked(struct vnode *vp, const char *str)
3980 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3981 vfs_badlock("interlock is not locked but should be", str, vp);
3985 assert_vi_unlocked(struct vnode *vp, const char *str)
3988 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3989 vfs_badlock("interlock is locked but should not be", str, vp);
3993 assert_vop_locked(struct vnode *vp, const char *str)
3997 if (!IGNORE_LOCK(vp)) {
3998 locked = VOP_ISLOCKED(vp);
3999 if (locked == 0 || locked == LK_EXCLOTHER)
4000 vfs_badlock("is not locked but should be", str, vp);
4005 assert_vop_unlocked(struct vnode *vp, const char *str)
4008 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4009 vfs_badlock("is locked but should not be", str, vp);
4013 assert_vop_elocked(struct vnode *vp, const char *str)
4016 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4017 vfs_badlock("is not exclusive locked but should be", str, vp);
4022 assert_vop_elocked_other(struct vnode *vp, const char *str)
4025 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
4026 vfs_badlock("is not exclusive locked by another thread",
4031 assert_vop_slocked(struct vnode *vp, const char *str)
4034 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
4035 vfs_badlock("is not locked shared but should be", str, vp);
4038 #endif /* DEBUG_VFS_LOCKS */
4041 vop_rename_fail(struct vop_rename_args *ap)
4044 if (ap->a_tvp != NULL)
4046 if (ap->a_tdvp == ap->a_tvp)
4055 vop_rename_pre(void *ap)
4057 struct vop_rename_args *a = ap;
4059 #ifdef DEBUG_VFS_LOCKS
4061 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4062 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4063 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4064 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4066 /* Check the source (from). */
4067 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4068 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4069 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4070 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4071 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4073 /* Check the target. */
4075 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4076 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4078 if (a->a_tdvp != a->a_fdvp)
4080 if (a->a_tvp != a->a_fvp)
4088 vop_strategy_pre(void *ap)
4090 #ifdef DEBUG_VFS_LOCKS
4091 struct vop_strategy_args *a;
4098 * Cluster ops lock their component buffers but not the IO container.
4100 if ((bp->b_flags & B_CLUSTER) != 0)
4103 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4104 if (vfs_badlock_print)
4106 "VOP_STRATEGY: bp is not locked but should be\n");
4107 if (vfs_badlock_ddb)
4108 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4114 vop_lock_pre(void *ap)
4116 #ifdef DEBUG_VFS_LOCKS
4117 struct vop_lock1_args *a = ap;
4119 if ((a->a_flags & LK_INTERLOCK) == 0)
4120 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4122 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4127 vop_lock_post(void *ap, int rc)
4129 #ifdef DEBUG_VFS_LOCKS
4130 struct vop_lock1_args *a = ap;
4132 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4133 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4134 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4139 vop_unlock_pre(void *ap)
4141 #ifdef DEBUG_VFS_LOCKS
4142 struct vop_unlock_args *a = ap;
4144 if (a->a_flags & LK_INTERLOCK)
4145 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4146 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4151 vop_unlock_post(void *ap, int rc)
4153 #ifdef DEBUG_VFS_LOCKS
4154 struct vop_unlock_args *a = ap;
4156 if (a->a_flags & LK_INTERLOCK)
4157 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4162 vop_create_post(void *ap, int rc)
4164 struct vop_create_args *a = ap;
4167 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4171 vop_deleteextattr_post(void *ap, int rc)
4173 struct vop_deleteextattr_args *a = ap;
4176 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4180 vop_link_post(void *ap, int rc)
4182 struct vop_link_args *a = ap;
4185 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4186 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4191 vop_mkdir_post(void *ap, int rc)
4193 struct vop_mkdir_args *a = ap;
4196 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4200 vop_mknod_post(void *ap, int rc)
4202 struct vop_mknod_args *a = ap;
4205 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4209 vop_remove_post(void *ap, int rc)
4211 struct vop_remove_args *a = ap;
4214 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4215 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4220 vop_rename_post(void *ap, int rc)
4222 struct vop_rename_args *a = ap;
4225 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4226 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4227 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4229 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4231 if (a->a_tdvp != a->a_fdvp)
4233 if (a->a_tvp != a->a_fvp)
4241 vop_rmdir_post(void *ap, int rc)
4243 struct vop_rmdir_args *a = ap;
4246 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4247 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4252 vop_setattr_post(void *ap, int rc)
4254 struct vop_setattr_args *a = ap;
4257 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4261 vop_setextattr_post(void *ap, int rc)
4263 struct vop_setextattr_args *a = ap;
4266 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4270 vop_symlink_post(void *ap, int rc)
4272 struct vop_symlink_args *a = ap;
4275 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4278 static struct knlist fs_knlist;
4281 vfs_event_init(void *arg)
4283 knlist_init_mtx(&fs_knlist, NULL);
4285 /* XXX - correct order? */
4286 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4289 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4292 KNOTE_UNLOCKED(&fs_knlist, event);
4295 static int filt_fsattach(struct knote *kn);
4296 static void filt_fsdetach(struct knote *kn);
4297 static int filt_fsevent(struct knote *kn, long hint);
4299 struct filterops fs_filtops = {
4301 .f_attach = filt_fsattach,
4302 .f_detach = filt_fsdetach,
4303 .f_event = filt_fsevent
4307 filt_fsattach(struct knote *kn)
4310 kn->kn_flags |= EV_CLEAR;
4311 knlist_add(&fs_knlist, kn, 0);
4316 filt_fsdetach(struct knote *kn)
4319 knlist_remove(&fs_knlist, kn, 0);
4323 filt_fsevent(struct knote *kn, long hint)
4326 kn->kn_fflags |= hint;
4327 return (kn->kn_fflags != 0);
4331 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4337 error = SYSCTL_IN(req, &vc, sizeof(vc));
4340 if (vc.vc_vers != VFS_CTL_VERS1)
4342 mp = vfs_getvfs(&vc.vc_fsid);
4345 /* ensure that a specific sysctl goes to the right filesystem. */
4346 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4347 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4351 VCTLTOREQ(&vc, req);
4352 error = VFS_SYSCTL(mp, vc.vc_op, req);
4357 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4358 NULL, 0, sysctl_vfs_ctl, "",
4362 * Function to initialize a va_filerev field sensibly.
4363 * XXX: Wouldn't a random number make a lot more sense ??
4366 init_va_filerev(void)
4371 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4374 static int filt_vfsread(struct knote *kn, long hint);
4375 static int filt_vfswrite(struct knote *kn, long hint);
4376 static int filt_vfsvnode(struct knote *kn, long hint);
4377 static void filt_vfsdetach(struct knote *kn);
4378 static struct filterops vfsread_filtops = {
4380 .f_detach = filt_vfsdetach,
4381 .f_event = filt_vfsread
4383 static struct filterops vfswrite_filtops = {
4385 .f_detach = filt_vfsdetach,
4386 .f_event = filt_vfswrite
4388 static struct filterops vfsvnode_filtops = {
4390 .f_detach = filt_vfsdetach,
4391 .f_event = filt_vfsvnode
4395 vfs_knllock(void *arg)
4397 struct vnode *vp = arg;
4399 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4403 vfs_knlunlock(void *arg)
4405 struct vnode *vp = arg;
4411 vfs_knl_assert_locked(void *arg)
4413 #ifdef DEBUG_VFS_LOCKS
4414 struct vnode *vp = arg;
4416 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4421 vfs_knl_assert_unlocked(void *arg)
4423 #ifdef DEBUG_VFS_LOCKS
4424 struct vnode *vp = arg;
4426 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4431 vfs_kqfilter(struct vop_kqfilter_args *ap)
4433 struct vnode *vp = ap->a_vp;
4434 struct knote *kn = ap->a_kn;
4437 switch (kn->kn_filter) {
4439 kn->kn_fop = &vfsread_filtops;
4442 kn->kn_fop = &vfswrite_filtops;
4445 kn->kn_fop = &vfsvnode_filtops;
4451 kn->kn_hook = (caddr_t)vp;
4454 if (vp->v_pollinfo == NULL)
4456 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4458 knlist_add(knl, kn, 0);
4464 * Detach knote from vnode
4467 filt_vfsdetach(struct knote *kn)
4469 struct vnode *vp = (struct vnode *)kn->kn_hook;
4471 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4472 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4478 filt_vfsread(struct knote *kn, long hint)
4480 struct vnode *vp = (struct vnode *)kn->kn_hook;
4485 * filesystem is gone, so set the EOF flag and schedule
4486 * the knote for deletion.
4488 if (hint == NOTE_REVOKE) {
4490 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4495 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4499 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4500 res = (kn->kn_data != 0);
4507 filt_vfswrite(struct knote *kn, long hint)
4509 struct vnode *vp = (struct vnode *)kn->kn_hook;
4514 * filesystem is gone, so set the EOF flag and schedule
4515 * the knote for deletion.
4517 if (hint == NOTE_REVOKE)
4518 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4526 filt_vfsvnode(struct knote *kn, long hint)
4528 struct vnode *vp = (struct vnode *)kn->kn_hook;
4532 if (kn->kn_sfflags & hint)
4533 kn->kn_fflags |= hint;
4534 if (hint == NOTE_REVOKE) {
4535 kn->kn_flags |= EV_EOF;
4539 res = (kn->kn_fflags != 0);
4545 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4549 if (dp->d_reclen > ap->a_uio->uio_resid)
4550 return (ENAMETOOLONG);
4551 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4553 if (ap->a_ncookies != NULL) {
4554 if (ap->a_cookies != NULL)
4555 free(ap->a_cookies, M_TEMP);
4556 ap->a_cookies = NULL;
4557 *ap->a_ncookies = 0;
4561 if (ap->a_ncookies == NULL)
4564 KASSERT(ap->a_cookies,
4565 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4567 *ap->a_cookies = realloc(*ap->a_cookies,
4568 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4569 (*ap->a_cookies)[*ap->a_ncookies] = off;
4574 * Mark for update the access time of the file if the filesystem
4575 * supports VOP_MARKATIME. This functionality is used by execve and
4576 * mmap, so we want to avoid the I/O implied by directly setting
4577 * va_atime for the sake of efficiency.
4580 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4585 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4586 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4587 (void)VOP_MARKATIME(vp);
4591 * The purpose of this routine is to remove granularity from accmode_t,
4592 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4593 * VADMIN and VAPPEND.
4595 * If it returns 0, the caller is supposed to continue with the usual
4596 * access checks using 'accmode' as modified by this routine. If it
4597 * returns nonzero value, the caller is supposed to return that value
4600 * Note that after this routine runs, accmode may be zero.
4603 vfs_unixify_accmode(accmode_t *accmode)
4606 * There is no way to specify explicit "deny" rule using
4607 * file mode or POSIX.1e ACLs.
4609 if (*accmode & VEXPLICIT_DENY) {
4615 * None of these can be translated into usual access bits.
4616 * Also, the common case for NFSv4 ACLs is to not contain
4617 * either of these bits. Caller should check for VWRITE
4618 * on the containing directory instead.
4620 if (*accmode & (VDELETE_CHILD | VDELETE))
4623 if (*accmode & VADMIN_PERMS) {
4624 *accmode &= ~VADMIN_PERMS;
4629 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4630 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4632 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
4638 * These are helper functions for filesystems to traverse all
4639 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
4641 * This interface replaces MNT_VNODE_FOREACH.
4644 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
4647 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
4652 kern_yield(PRI_USER);
4654 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4655 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
4656 while (vp != NULL && (vp->v_type == VMARKER ||
4657 (vp->v_iflag & VI_DOOMED) != 0))
4658 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4660 /* Check if we are done */
4662 __mnt_vnode_markerfree_all(mvp, mp);
4663 /* MNT_IUNLOCK(mp); -- done in above function */
4664 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4667 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4668 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4675 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
4679 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4682 (*mvp)->v_type = VMARKER;
4684 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
4685 while (vp != NULL && (vp->v_type == VMARKER ||
4686 (vp->v_iflag & VI_DOOMED) != 0))
4687 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4689 /* Check if we are done */
4693 free(*mvp, M_VNODE_MARKER);
4697 (*mvp)->v_mount = mp;
4698 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4706 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
4714 mtx_assert(MNT_MTX(mp), MA_OWNED);
4716 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4717 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4720 free(*mvp, M_VNODE_MARKER);
4725 * These are helper functions for filesystems to traverse their
4726 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
4729 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4732 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4737 free(*mvp, M_VNODE_MARKER);
4741 static struct vnode *
4742 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4744 struct vnode *vp, *nvp;
4746 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
4747 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4749 vp = TAILQ_NEXT(*mvp, v_actfreelist);
4750 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4751 while (vp != NULL) {
4752 if (vp->v_type == VMARKER) {
4753 vp = TAILQ_NEXT(vp, v_actfreelist);
4756 if (!VI_TRYLOCK(vp)) {
4757 if (mp_ncpus == 1 || should_yield()) {
4758 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4759 mtx_unlock(&vnode_free_list_mtx);
4761 mtx_lock(&vnode_free_list_mtx);
4766 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
4767 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
4768 ("alien vnode on the active list %p %p", vp, mp));
4769 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
4771 nvp = TAILQ_NEXT(vp, v_actfreelist);
4776 /* Check if we are done */
4778 mtx_unlock(&vnode_free_list_mtx);
4779 mnt_vnode_markerfree_active(mvp, mp);
4782 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4783 mtx_unlock(&vnode_free_list_mtx);
4784 ASSERT_VI_LOCKED(vp, "active iter");
4785 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
4790 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4794 kern_yield(PRI_USER);
4795 mtx_lock(&vnode_free_list_mtx);
4796 return (mnt_vnode_next_active(mvp, mp));
4800 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
4804 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4808 (*mvp)->v_type = VMARKER;
4809 (*mvp)->v_mount = mp;
4811 mtx_lock(&vnode_free_list_mtx);
4812 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
4814 mtx_unlock(&vnode_free_list_mtx);
4815 mnt_vnode_markerfree_active(mvp, mp);
4818 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4819 return (mnt_vnode_next_active(mvp, mp));
4823 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4829 mtx_lock(&vnode_free_list_mtx);
4830 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4831 mtx_unlock(&vnode_free_list_mtx);
4832 mnt_vnode_markerfree_active(mvp, mp);