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");
125 static u_long vnodes_created;
126 SYSCTL_ULONG(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
127 0, "Number of vnodes created by getnewvnode");
130 * Conversion tables for conversion from vnode types to inode formats
133 enum vtype iftovt_tab[16] = {
134 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
135 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
137 int vttoif_tab[10] = {
138 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
139 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
143 * List of vnodes that are ready for recycling.
145 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
148 * Free vnode target. Free vnodes may simply be files which have been stat'd
149 * but not read. This is somewhat common, and a small cache of such files
150 * should be kept to avoid recreation costs.
152 static u_long wantfreevnodes;
153 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
154 /* Number of vnodes in the free list. */
155 static u_long freevnodes;
156 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
157 "Number of vnodes in the free list");
159 static int vlru_allow_cache_src;
160 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
161 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
163 static u_long recycles_count;
164 SYSCTL_ULONG(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 0,
165 "Number of vnodes recycled to avoid exceding kern.maxvnodes");
168 * Various variables used for debugging the new implementation of
170 * XXX these are probably of (very) limited utility now.
172 static int reassignbufcalls;
173 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
174 "Number of calls to reassignbuf");
177 * Cache for the mount type id assigned to NFS. This is used for
178 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
180 int nfs_mount_type = -1;
182 /* To keep more than one thread at a time from running vfs_getnewfsid */
183 static struct mtx mntid_mtx;
186 * Lock for any access to the following:
191 static struct mtx vnode_free_list_mtx;
193 /* Publicly exported FS */
194 struct nfs_public nfs_pub;
196 static uma_zone_t buf_trie_zone;
198 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
199 static uma_zone_t vnode_zone;
200 static uma_zone_t vnodepoll_zone;
203 * The workitem queue.
205 * It is useful to delay writes of file data and filesystem metadata
206 * for tens of seconds so that quickly created and deleted files need
207 * not waste disk bandwidth being created and removed. To realize this,
208 * we append vnodes to a "workitem" queue. When running with a soft
209 * updates implementation, most pending metadata dependencies should
210 * not wait for more than a few seconds. Thus, mounted on block devices
211 * are delayed only about a half the time that file data is delayed.
212 * Similarly, directory updates are more critical, so are only delayed
213 * about a third the time that file data is delayed. Thus, there are
214 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
215 * one each second (driven off the filesystem syncer process). The
216 * syncer_delayno variable indicates the next queue that is to be processed.
217 * Items that need to be processed soon are placed in this queue:
219 * syncer_workitem_pending[syncer_delayno]
221 * A delay of fifteen seconds is done by placing the request fifteen
222 * entries later in the queue:
224 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
227 static int syncer_delayno;
228 static long syncer_mask;
229 LIST_HEAD(synclist, bufobj);
230 static struct synclist *syncer_workitem_pending;
232 * The sync_mtx protects:
237 * syncer_workitem_pending
238 * syncer_worklist_len
241 static struct mtx sync_mtx;
242 static struct cv sync_wakeup;
244 #define SYNCER_MAXDELAY 32
245 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
246 static int syncdelay = 30; /* max time to delay syncing data */
247 static int filedelay = 30; /* time to delay syncing files */
248 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
249 "Time to delay syncing files (in seconds)");
250 static int dirdelay = 29; /* time to delay syncing directories */
251 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
252 "Time to delay syncing directories (in seconds)");
253 static int metadelay = 28; /* time to delay syncing metadata */
254 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
255 "Time to delay syncing metadata (in seconds)");
256 static int rushjob; /* number of slots to run ASAP */
257 static int stat_rush_requests; /* number of times I/O speeded up */
258 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
259 "Number of times I/O speeded up (rush requests)");
262 * When shutting down the syncer, run it at four times normal speed.
264 #define SYNCER_SHUTDOWN_SPEEDUP 4
265 static int sync_vnode_count;
266 static int syncer_worklist_len;
267 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
271 * Number of vnodes we want to exist at any one time. This is mostly used
272 * to size hash tables in vnode-related code. It is normally not used in
273 * getnewvnode(), as wantfreevnodes is normally nonzero.)
275 * XXX desiredvnodes is historical cruft and should not exist.
278 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
279 &desiredvnodes, 0, "Maximum number of vnodes");
280 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
281 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
282 static int vnlru_nowhere;
283 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
284 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
286 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
290 * Support for the bufobj clean & dirty pctrie.
293 buf_trie_alloc(struct pctrie *ptree)
296 return uma_zalloc(buf_trie_zone, M_NOWAIT);
300 buf_trie_free(struct pctrie *ptree, void *node)
303 uma_zfree(buf_trie_zone, node);
305 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
308 * Initialize the vnode management data structures.
310 * Reevaluate the following cap on the number of vnodes after the physical
311 * memory size exceeds 512GB. In the limit, as the physical memory size
312 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
314 #ifndef MAXVNODES_MAX
315 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
318 vntblinit(void *dummy __unused)
321 int physvnodes, virtvnodes;
324 * Desiredvnodes is a function of the physical memory size and the
325 * kernel's heap size. Generally speaking, it scales with the
326 * physical memory size. The ratio of desiredvnodes to physical pages
327 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
328 * marginal ratio of desiredvnodes to physical pages is one to
329 * sixteen. However, desiredvnodes is limited by the kernel's heap
330 * size. The memory required by desiredvnodes vnodes and vm objects
331 * may not exceed one seventh of the kernel's heap size.
333 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
334 cnt.v_page_count) / 16;
335 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
336 sizeof(struct vnode)));
337 desiredvnodes = min(physvnodes, virtvnodes);
338 if (desiredvnodes > MAXVNODES_MAX) {
340 printf("Reducing kern.maxvnodes %d -> %d\n",
341 desiredvnodes, MAXVNODES_MAX);
342 desiredvnodes = MAXVNODES_MAX;
344 wantfreevnodes = desiredvnodes / 4;
345 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
346 TAILQ_INIT(&vnode_free_list);
347 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
348 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
349 NULL, NULL, UMA_ALIGN_PTR, 0);
350 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
351 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
353 * Preallocate enough nodes to support one-per buf so that
354 * we can not fail an insert. reassignbuf() callers can not
355 * tolerate the insertion failure.
357 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
358 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
359 UMA_ZONE_NOFREE | UMA_ZONE_VM);
360 uma_prealloc(buf_trie_zone, nbuf);
362 * Initialize the filesystem syncer.
364 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
366 syncer_maxdelay = syncer_mask + 1;
367 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
368 cv_init(&sync_wakeup, "syncer");
369 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
373 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
377 * Mark a mount point as busy. Used to synchronize access and to delay
378 * unmounting. Eventually, mountlist_mtx is not released on failure.
380 * vfs_busy() is a custom lock, it can block the caller.
381 * vfs_busy() only sleeps if the unmount is active on the mount point.
382 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
383 * vnode belonging to mp.
385 * Lookup uses vfs_busy() to traverse mount points.
387 * / vnode lock A / vnode lock (/var) D
388 * /var vnode lock B /log vnode lock(/var/log) E
389 * vfs_busy lock C vfs_busy lock F
391 * Within each file system, the lock order is C->A->B and F->D->E.
393 * When traversing across mounts, the system follows that lock order:
399 * The lookup() process for namei("/var") illustrates the process:
400 * VOP_LOOKUP() obtains B while A is held
401 * vfs_busy() obtains a shared lock on F while A and B are held
402 * vput() releases lock on B
403 * vput() releases lock on A
404 * VFS_ROOT() obtains lock on D while shared lock on F is held
405 * vfs_unbusy() releases shared lock on F
406 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
407 * Attempt to lock A (instead of vp_crossmp) while D is held would
408 * violate the global order, causing deadlocks.
410 * dounmount() locks B while F is drained.
413 vfs_busy(struct mount *mp, int flags)
416 MPASS((flags & ~MBF_MASK) == 0);
417 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
422 * If mount point is currenly being unmounted, sleep until the
423 * mount point fate is decided. If thread doing the unmounting fails,
424 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
425 * that this mount point has survived the unmount attempt and vfs_busy
426 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
427 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
428 * about to be really destroyed. vfs_busy needs to release its
429 * reference on the mount point in this case and return with ENOENT,
430 * telling the caller that mount mount it tried to busy is no longer
433 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
434 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
437 CTR1(KTR_VFS, "%s: failed busying before sleeping",
441 if (flags & MBF_MNTLSTLOCK)
442 mtx_unlock(&mountlist_mtx);
443 mp->mnt_kern_flag |= MNTK_MWAIT;
444 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
445 if (flags & MBF_MNTLSTLOCK)
446 mtx_lock(&mountlist_mtx);
449 if (flags & MBF_MNTLSTLOCK)
450 mtx_unlock(&mountlist_mtx);
457 * Free a busy filesystem.
460 vfs_unbusy(struct mount *mp)
463 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
466 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
468 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
469 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
470 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
471 mp->mnt_kern_flag &= ~MNTK_DRAINING;
472 wakeup(&mp->mnt_lockref);
478 * Lookup a mount point by filesystem identifier.
481 vfs_getvfs(fsid_t *fsid)
485 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
486 mtx_lock(&mountlist_mtx);
487 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
488 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
489 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
491 mtx_unlock(&mountlist_mtx);
495 mtx_unlock(&mountlist_mtx);
496 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
497 return ((struct mount *) 0);
501 * Lookup a mount point by filesystem identifier, busying it before
504 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
505 * cache for popular filesystem identifiers. The cache is lockess, using
506 * the fact that struct mount's are never freed. In worst case we may
507 * get pointer to unmounted or even different filesystem, so we have to
508 * check what we got, and go slow way if so.
511 vfs_busyfs(fsid_t *fsid)
513 #define FSID_CACHE_SIZE 256
514 typedef struct mount * volatile vmp_t;
515 static vmp_t cache[FSID_CACHE_SIZE];
520 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
521 hash = fsid->val[0] ^ fsid->val[1];
522 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
525 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
526 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
528 if (vfs_busy(mp, 0) != 0) {
532 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
533 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
539 mtx_lock(&mountlist_mtx);
540 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
541 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
542 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
543 error = vfs_busy(mp, MBF_MNTLSTLOCK);
546 mtx_unlock(&mountlist_mtx);
553 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
554 mtx_unlock(&mountlist_mtx);
555 return ((struct mount *) 0);
559 * Check if a user can access privileged mount options.
562 vfs_suser(struct mount *mp, struct thread *td)
567 * If the thread is jailed, but this is not a jail-friendly file
568 * system, deny immediately.
570 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
574 * If the file system was mounted outside the jail of the calling
575 * thread, deny immediately.
577 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
581 * If file system supports delegated administration, we don't check
582 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
583 * by the file system itself.
584 * If this is not the user that did original mount, we check for
585 * the PRIV_VFS_MOUNT_OWNER privilege.
587 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
588 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
589 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
596 * Get a new unique fsid. Try to make its val[0] unique, since this value
597 * will be used to create fake device numbers for stat(). Also try (but
598 * not so hard) make its val[0] unique mod 2^16, since some emulators only
599 * support 16-bit device numbers. We end up with unique val[0]'s for the
600 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
602 * Keep in mind that several mounts may be running in parallel. Starting
603 * the search one past where the previous search terminated is both a
604 * micro-optimization and a defense against returning the same fsid to
608 vfs_getnewfsid(struct mount *mp)
610 static uint16_t mntid_base;
615 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
616 mtx_lock(&mntid_mtx);
617 mtype = mp->mnt_vfc->vfc_typenum;
618 tfsid.val[1] = mtype;
619 mtype = (mtype & 0xFF) << 24;
621 tfsid.val[0] = makedev(255,
622 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
624 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
628 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
629 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
630 mtx_unlock(&mntid_mtx);
634 * Knob to control the precision of file timestamps:
636 * 0 = seconds only; nanoseconds zeroed.
637 * 1 = seconds and nanoseconds, accurate within 1/HZ.
638 * 2 = seconds and nanoseconds, truncated to microseconds.
639 * >=3 = seconds and nanoseconds, maximum precision.
641 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
643 static int timestamp_precision = TSP_USEC;
644 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
645 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
646 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
647 "3+: sec + ns (max. precision))");
650 * Get a current timestamp.
653 vfs_timestamp(struct timespec *tsp)
657 switch (timestamp_precision) {
659 tsp->tv_sec = time_second;
667 TIMEVAL_TO_TIMESPEC(&tv, tsp);
677 * Set vnode attributes to VNOVAL
680 vattr_null(struct vattr *vap)
684 vap->va_size = VNOVAL;
685 vap->va_bytes = VNOVAL;
686 vap->va_mode = VNOVAL;
687 vap->va_nlink = VNOVAL;
688 vap->va_uid = VNOVAL;
689 vap->va_gid = VNOVAL;
690 vap->va_fsid = VNOVAL;
691 vap->va_fileid = VNOVAL;
692 vap->va_blocksize = VNOVAL;
693 vap->va_rdev = VNOVAL;
694 vap->va_atime.tv_sec = VNOVAL;
695 vap->va_atime.tv_nsec = VNOVAL;
696 vap->va_mtime.tv_sec = VNOVAL;
697 vap->va_mtime.tv_nsec = VNOVAL;
698 vap->va_ctime.tv_sec = VNOVAL;
699 vap->va_ctime.tv_nsec = VNOVAL;
700 vap->va_birthtime.tv_sec = VNOVAL;
701 vap->va_birthtime.tv_nsec = VNOVAL;
702 vap->va_flags = VNOVAL;
703 vap->va_gen = VNOVAL;
708 * This routine is called when we have too many vnodes. It attempts
709 * to free <count> vnodes and will potentially free vnodes that still
710 * have VM backing store (VM backing store is typically the cause
711 * of a vnode blowout so we want to do this). Therefore, this operation
712 * is not considered cheap.
714 * A number of conditions may prevent a vnode from being reclaimed.
715 * the buffer cache may have references on the vnode, a directory
716 * vnode may still have references due to the namei cache representing
717 * underlying files, or the vnode may be in active use. It is not
718 * desireable to reuse such vnodes. These conditions may cause the
719 * number of vnodes to reach some minimum value regardless of what
720 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
723 vlrureclaim(struct mount *mp)
732 * Calculate the trigger point, don't allow user
733 * screwups to blow us up. This prevents us from
734 * recycling vnodes with lots of resident pages. We
735 * aren't trying to free memory, we are trying to
738 usevnodes = desiredvnodes;
741 trigger = cnt.v_page_count * 2 / usevnodes;
743 vn_start_write(NULL, &mp, V_WAIT);
745 count = mp->mnt_nvnodelistsize / 10 + 1;
747 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
748 while (vp != NULL && vp->v_type == VMARKER)
749 vp = TAILQ_NEXT(vp, v_nmntvnodes);
752 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
753 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
758 * If it's been deconstructed already, it's still
759 * referenced, or it exceeds the trigger, skip it.
761 if (vp->v_usecount ||
762 (!vlru_allow_cache_src &&
763 !LIST_EMPTY(&(vp)->v_cache_src)) ||
764 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
765 vp->v_object->resident_page_count > trigger)) {
771 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
773 goto next_iter_mntunlocked;
777 * v_usecount may have been bumped after VOP_LOCK() dropped
778 * the vnode interlock and before it was locked again.
780 * It is not necessary to recheck VI_DOOMED because it can
781 * only be set by another thread that holds both the vnode
782 * lock and vnode interlock. If another thread has the
783 * vnode lock before we get to VOP_LOCK() and obtains the
784 * vnode interlock after VOP_LOCK() drops the vnode
785 * interlock, the other thread will be unable to drop the
786 * vnode lock before our VOP_LOCK() call fails.
788 if (vp->v_usecount ||
789 (!vlru_allow_cache_src &&
790 !LIST_EMPTY(&(vp)->v_cache_src)) ||
791 (vp->v_object != NULL &&
792 vp->v_object->resident_page_count > trigger)) {
793 VOP_UNLOCK(vp, LK_INTERLOCK);
795 goto next_iter_mntunlocked;
797 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
798 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
799 atomic_add_long(&recycles_count, 1);
804 next_iter_mntunlocked:
813 kern_yield(PRI_USER);
818 vn_finished_write(mp);
823 * Attempt to keep the free list at wantfreevnodes length.
826 vnlru_free(int count)
830 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
831 for (; count > 0; count--) {
832 vp = TAILQ_FIRST(&vnode_free_list);
834 * The list can be modified while the free_list_mtx
835 * has been dropped and vp could be NULL here.
839 VNASSERT(vp->v_op != NULL, vp,
840 ("vnlru_free: vnode already reclaimed."));
841 KASSERT((vp->v_iflag & VI_FREE) != 0,
842 ("Removing vnode not on freelist"));
843 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
844 ("Mangling active vnode"));
845 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
847 * Don't recycle if we can't get the interlock.
849 if (!VI_TRYLOCK(vp)) {
850 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
853 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
854 vp, ("vp inconsistent on freelist"));
857 * The clear of VI_FREE prevents activation of the
858 * vnode. There is no sense in putting the vnode on
859 * the mount point active list, only to remove it
860 * later during recycling. Inline the relevant part
861 * of vholdl(), to avoid triggering assertions or
865 vp->v_iflag &= ~VI_FREE;
868 mtx_unlock(&vnode_free_list_mtx);
872 * If the recycled succeeded this vdrop will actually free
873 * the vnode. If not it will simply place it back on
877 mtx_lock(&vnode_free_list_mtx);
881 * Attempt to recycle vnodes in a context that is always safe to block.
882 * Calling vlrurecycle() from the bowels of filesystem code has some
883 * interesting deadlock problems.
885 static struct proc *vnlruproc;
886 static int vnlruproc_sig;
891 struct mount *mp, *nmp;
893 struct proc *p = vnlruproc;
895 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
899 kproc_suspend_check(p);
900 mtx_lock(&vnode_free_list_mtx);
901 if (freevnodes > wantfreevnodes)
902 vnlru_free(freevnodes - wantfreevnodes);
903 if (numvnodes <= desiredvnodes * 9 / 10) {
905 wakeup(&vnlruproc_sig);
906 msleep(vnlruproc, &vnode_free_list_mtx,
907 PVFS|PDROP, "vlruwt", hz);
910 mtx_unlock(&vnode_free_list_mtx);
912 mtx_lock(&mountlist_mtx);
913 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
914 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
915 nmp = TAILQ_NEXT(mp, mnt_list);
918 done += vlrureclaim(mp);
919 mtx_lock(&mountlist_mtx);
920 nmp = TAILQ_NEXT(mp, mnt_list);
923 mtx_unlock(&mountlist_mtx);
926 /* These messages are temporary debugging aids */
927 if (vnlru_nowhere < 5)
928 printf("vnlru process getting nowhere..\n");
929 else if (vnlru_nowhere == 5)
930 printf("vnlru process messages stopped.\n");
933 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
935 kern_yield(PRI_USER);
939 static struct kproc_desc vnlru_kp = {
944 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
948 * Routines having to do with the management of the vnode table.
952 * Try to recycle a freed vnode. We abort if anyone picks up a reference
953 * before we actually vgone(). This function must be called with the vnode
954 * held to prevent the vnode from being returned to the free list midway
958 vtryrecycle(struct vnode *vp)
962 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
963 VNASSERT(vp->v_holdcnt, vp,
964 ("vtryrecycle: Recycling vp %p without a reference.", vp));
966 * This vnode may found and locked via some other list, if so we
967 * can't recycle it yet.
969 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
971 "%s: impossible to recycle, vp %p lock is already held",
973 return (EWOULDBLOCK);
976 * Don't recycle if its filesystem is being suspended.
978 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
981 "%s: impossible to recycle, cannot start the write for %p",
986 * If we got this far, we need to acquire the interlock and see if
987 * anyone picked up this vnode from another list. If not, we will
988 * mark it with DOOMED via vgonel() so that anyone who does find it
992 if (vp->v_usecount) {
993 VOP_UNLOCK(vp, LK_INTERLOCK);
994 vn_finished_write(vnmp);
996 "%s: impossible to recycle, %p is already referenced",
1000 if ((vp->v_iflag & VI_DOOMED) == 0) {
1001 atomic_add_long(&recycles_count, 1);
1004 VOP_UNLOCK(vp, LK_INTERLOCK);
1005 vn_finished_write(vnmp);
1010 * Wait for available vnodes.
1013 getnewvnode_wait(int suspended)
1016 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1017 if (numvnodes > desiredvnodes) {
1020 * File system is beeing suspended, we cannot risk a
1021 * deadlock here, so allocate new vnode anyway.
1023 if (freevnodes > wantfreevnodes)
1024 vnlru_free(freevnodes - wantfreevnodes);
1027 if (vnlruproc_sig == 0) {
1028 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1031 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1034 return (numvnodes > desiredvnodes ? ENFILE : 0);
1038 getnewvnode_reserve(u_int count)
1043 /* First try to be quick and racy. */
1044 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1045 td->td_vp_reserv += count;
1048 atomic_subtract_long(&numvnodes, count);
1050 mtx_lock(&vnode_free_list_mtx);
1052 if (getnewvnode_wait(0) == 0) {
1055 atomic_add_long(&numvnodes, 1);
1058 mtx_unlock(&vnode_free_list_mtx);
1062 getnewvnode_drop_reserve(void)
1067 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1068 td->td_vp_reserv = 0;
1072 * Return the next vnode from the free list.
1075 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1083 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1086 if (td->td_vp_reserv > 0) {
1087 td->td_vp_reserv -= 1;
1090 mtx_lock(&vnode_free_list_mtx);
1092 * Lend our context to reclaim vnodes if they've exceeded the max.
1094 if (freevnodes > wantfreevnodes)
1096 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1098 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1100 mtx_unlock(&vnode_free_list_mtx);
1104 atomic_add_long(&numvnodes, 1);
1105 mtx_unlock(&vnode_free_list_mtx);
1107 atomic_add_long(&vnodes_created, 1);
1108 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1112 vp->v_vnlock = &vp->v_lock;
1113 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1115 * By default, don't allow shared locks unless filesystems
1118 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE | LK_IS_VNODE);
1120 * Initialize bufobj.
1123 bo->__bo_vnode = vp;
1124 rw_init(BO_LOCKPTR(bo), "bufobj interlock");
1125 bo->bo_ops = &buf_ops_bio;
1126 bo->bo_private = vp;
1127 TAILQ_INIT(&bo->bo_clean.bv_hd);
1128 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1130 * Initialize namecache.
1132 LIST_INIT(&vp->v_cache_src);
1133 TAILQ_INIT(&vp->v_cache_dst);
1135 * Finalize various vnode identity bits.
1140 v_incr_usecount(vp);
1144 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1145 mac_vnode_associate_singlelabel(mp, vp);
1146 else if (mp == NULL && vops != &dead_vnodeops)
1147 printf("NULL mp in getnewvnode()\n");
1150 bo->bo_bsize = mp->mnt_stat.f_iosize;
1151 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1152 vp->v_vflag |= VV_NOKNOTE;
1154 rangelock_init(&vp->v_rl);
1157 * For the filesystems which do not use vfs_hash_insert(),
1158 * still initialize v_hash to have vfs_hash_index() useful.
1159 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1162 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1169 * Delete from old mount point vnode list, if on one.
1172 delmntque(struct vnode *vp)
1182 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1183 ("Active vnode list size %d > Vnode list size %d",
1184 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1185 active = vp->v_iflag & VI_ACTIVE;
1186 vp->v_iflag &= ~VI_ACTIVE;
1188 mtx_lock(&vnode_free_list_mtx);
1189 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1190 mp->mnt_activevnodelistsize--;
1191 mtx_unlock(&vnode_free_list_mtx);
1195 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1196 ("bad mount point vnode list size"));
1197 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1198 mp->mnt_nvnodelistsize--;
1204 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1208 vp->v_op = &dead_vnodeops;
1214 * Insert into list of vnodes for the new mount point, if available.
1217 insmntque1(struct vnode *vp, struct mount *mp,
1218 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1221 KASSERT(vp->v_mount == NULL,
1222 ("insmntque: vnode already on per mount vnode list"));
1223 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1224 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1227 * We acquire the vnode interlock early to ensure that the
1228 * vnode cannot be recycled by another process releasing a
1229 * holdcnt on it before we get it on both the vnode list
1230 * and the active vnode list. The mount mutex protects only
1231 * manipulation of the vnode list and the vnode freelist
1232 * mutex protects only manipulation of the active vnode list.
1233 * Hence the need to hold the vnode interlock throughout.
1237 if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1238 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1239 mp->mnt_nvnodelistsize == 0)) &&
1240 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1249 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1250 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1251 ("neg mount point vnode list size"));
1252 mp->mnt_nvnodelistsize++;
1253 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1254 ("Activating already active vnode"));
1255 vp->v_iflag |= VI_ACTIVE;
1256 mtx_lock(&vnode_free_list_mtx);
1257 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1258 mp->mnt_activevnodelistsize++;
1259 mtx_unlock(&vnode_free_list_mtx);
1266 insmntque(struct vnode *vp, struct mount *mp)
1269 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1273 * Flush out and invalidate all buffers associated with a bufobj
1274 * Called with the underlying object locked.
1277 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1282 if (flags & V_SAVE) {
1283 error = bufobj_wwait(bo, slpflag, slptimeo);
1288 if (bo->bo_dirty.bv_cnt > 0) {
1290 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1293 * XXX We could save a lock/unlock if this was only
1294 * enabled under INVARIANTS
1297 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1298 panic("vinvalbuf: dirty bufs");
1302 * If you alter this loop please notice that interlock is dropped and
1303 * reacquired in flushbuflist. Special care is needed to ensure that
1304 * no race conditions occur from this.
1307 error = flushbuflist(&bo->bo_clean,
1308 flags, bo, slpflag, slptimeo);
1309 if (error == 0 && !(flags & V_CLEANONLY))
1310 error = flushbuflist(&bo->bo_dirty,
1311 flags, bo, slpflag, slptimeo);
1312 if (error != 0 && error != EAGAIN) {
1316 } while (error != 0);
1319 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1320 * have write I/O in-progress but if there is a VM object then the
1321 * VM object can also have read-I/O in-progress.
1324 bufobj_wwait(bo, 0, 0);
1326 if (bo->bo_object != NULL) {
1327 VM_OBJECT_WLOCK(bo->bo_object);
1328 vm_object_pip_wait(bo->bo_object, "bovlbx");
1329 VM_OBJECT_WUNLOCK(bo->bo_object);
1332 } while (bo->bo_numoutput > 0);
1336 * Destroy the copy in the VM cache, too.
1338 if (bo->bo_object != NULL &&
1339 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1340 VM_OBJECT_WLOCK(bo->bo_object);
1341 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1342 OBJPR_CLEANONLY : 0);
1343 VM_OBJECT_WUNLOCK(bo->bo_object);
1348 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1349 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1350 panic("vinvalbuf: flush failed");
1357 * Flush out and invalidate all buffers associated with a vnode.
1358 * Called with the underlying object locked.
1361 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1364 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1365 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1366 if (vp->v_object != NULL && vp->v_object->handle != vp)
1368 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1372 * Flush out buffers on the specified list.
1376 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1379 struct buf *bp, *nbp;
1384 ASSERT_BO_WLOCKED(bo);
1387 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1388 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1389 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1395 lblkno = nbp->b_lblkno;
1396 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1399 error = BUF_TIMELOCK(bp,
1400 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1401 "flushbuf", slpflag, slptimeo);
1404 return (error != ENOLCK ? error : EAGAIN);
1406 KASSERT(bp->b_bufobj == bo,
1407 ("bp %p wrong b_bufobj %p should be %p",
1408 bp, bp->b_bufobj, bo));
1409 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1415 * XXX Since there are no node locks for NFS, I
1416 * believe there is a slight chance that a delayed
1417 * write will occur while sleeping just above, so
1420 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1423 bp->b_flags |= B_ASYNC;
1426 return (EAGAIN); /* XXX: why not loop ? */
1429 bp->b_flags |= (B_INVAL | B_RELBUF);
1430 bp->b_flags &= ~B_ASYNC;
1434 (nbp->b_bufobj != bo ||
1435 nbp->b_lblkno != lblkno ||
1436 (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1437 break; /* nbp invalid */
1443 * Truncate a file's buffer and pages to a specified length. This
1444 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1448 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
1450 struct buf *bp, *nbp;
1455 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1456 vp, cred, blksize, (uintmax_t)length);
1459 * Round up to the *next* lbn.
1461 trunclbn = (length + blksize - 1) / blksize;
1463 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1470 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1471 if (bp->b_lblkno < trunclbn)
1474 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1475 BO_LOCKPTR(bo)) == ENOLCK)
1479 bp->b_flags |= (B_INVAL | B_RELBUF);
1480 bp->b_flags &= ~B_ASYNC;
1486 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1487 (nbp->b_vp != vp) ||
1488 (nbp->b_flags & B_DELWRI))) {
1494 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1495 if (bp->b_lblkno < trunclbn)
1498 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1499 BO_LOCKPTR(bo)) == ENOLCK)
1502 bp->b_flags |= (B_INVAL | B_RELBUF);
1503 bp->b_flags &= ~B_ASYNC;
1509 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1510 (nbp->b_vp != vp) ||
1511 (nbp->b_flags & B_DELWRI) == 0)) {
1520 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1521 if (bp->b_lblkno > 0)
1524 * Since we hold the vnode lock this should only
1525 * fail if we're racing with the buf daemon.
1528 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1529 BO_LOCKPTR(bo)) == ENOLCK) {
1532 VNASSERT((bp->b_flags & B_DELWRI), vp,
1533 ("buf(%p) on dirty queue without DELWRI", bp));
1542 bufobj_wwait(bo, 0, 0);
1544 vnode_pager_setsize(vp, length);
1550 buf_vlist_remove(struct buf *bp)
1554 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1555 ASSERT_BO_WLOCKED(bp->b_bufobj);
1556 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1557 (BX_VNDIRTY|BX_VNCLEAN),
1558 ("buf_vlist_remove: Buf %p is on two lists", bp));
1559 if (bp->b_xflags & BX_VNDIRTY)
1560 bv = &bp->b_bufobj->bo_dirty;
1562 bv = &bp->b_bufobj->bo_clean;
1563 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
1564 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1566 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1570 * Add the buffer to the sorted clean or dirty block list.
1572 * NOTE: xflags is passed as a constant, optimizing this inline function!
1575 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1581 ASSERT_BO_WLOCKED(bo);
1582 KASSERT((bo->bo_flag & BO_DEAD) == 0, ("dead bo %p", bo));
1583 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1584 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1585 bp->b_xflags |= xflags;
1586 if (xflags & BX_VNDIRTY)
1592 * Keep the list ordered. Optimize empty list insertion. Assume
1593 * we tend to grow at the tail so lookup_le should usually be cheaper
1596 if (bv->bv_cnt == 0 ||
1597 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
1598 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1599 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
1600 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
1602 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
1603 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
1605 panic("buf_vlist_add: Preallocated nodes insufficient.");
1610 * Lookup a buffer using the splay tree. Note that we specifically avoid
1611 * shadow buffers used in background bitmap writes.
1613 * This code isn't quite efficient as it could be because we are maintaining
1614 * two sorted lists and do not know which list the block resides in.
1616 * During a "make buildworld" the desired buffer is found at one of
1617 * the roots more than 60% of the time. Thus, checking both roots
1618 * before performing either splay eliminates unnecessary splays on the
1619 * first tree splayed.
1622 gbincore(struct bufobj *bo, daddr_t lblkno)
1626 ASSERT_BO_LOCKED(bo);
1627 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
1630 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
1634 * Associate a buffer with a vnode.
1637 bgetvp(struct vnode *vp, struct buf *bp)
1642 ASSERT_BO_WLOCKED(bo);
1643 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1645 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1646 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1647 ("bgetvp: bp already attached! %p", bp));
1653 * Insert onto list for new vnode.
1655 buf_vlist_add(bp, bo, BX_VNCLEAN);
1659 * Disassociate a buffer from a vnode.
1662 brelvp(struct buf *bp)
1667 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1668 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1671 * Delete from old vnode list, if on one.
1673 vp = bp->b_vp; /* XXX */
1676 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1677 buf_vlist_remove(bp);
1679 panic("brelvp: Buffer %p not on queue.", bp);
1680 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1681 bo->bo_flag &= ~BO_ONWORKLST;
1682 mtx_lock(&sync_mtx);
1683 LIST_REMOVE(bo, bo_synclist);
1684 syncer_worklist_len--;
1685 mtx_unlock(&sync_mtx);
1688 bp->b_bufobj = NULL;
1694 * Add an item to the syncer work queue.
1697 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1701 ASSERT_BO_WLOCKED(bo);
1703 mtx_lock(&sync_mtx);
1704 if (bo->bo_flag & BO_ONWORKLST)
1705 LIST_REMOVE(bo, bo_synclist);
1707 bo->bo_flag |= BO_ONWORKLST;
1708 syncer_worklist_len++;
1711 if (delay > syncer_maxdelay - 2)
1712 delay = syncer_maxdelay - 2;
1713 slot = (syncer_delayno + delay) & syncer_mask;
1715 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
1716 mtx_unlock(&sync_mtx);
1720 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1724 mtx_lock(&sync_mtx);
1725 len = syncer_worklist_len - sync_vnode_count;
1726 mtx_unlock(&sync_mtx);
1727 error = SYSCTL_OUT(req, &len, sizeof(len));
1731 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1732 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1734 static struct proc *updateproc;
1735 static void sched_sync(void);
1736 static struct kproc_desc up_kp = {
1741 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1744 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1749 *bo = LIST_FIRST(slp);
1752 vp = (*bo)->__bo_vnode; /* XXX */
1753 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1756 * We use vhold in case the vnode does not
1757 * successfully sync. vhold prevents the vnode from
1758 * going away when we unlock the sync_mtx so that
1759 * we can acquire the vnode interlock.
1762 mtx_unlock(&sync_mtx);
1764 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1766 mtx_lock(&sync_mtx);
1767 return (*bo == LIST_FIRST(slp));
1769 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1770 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1772 vn_finished_write(mp);
1774 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1776 * Put us back on the worklist. The worklist
1777 * routine will remove us from our current
1778 * position and then add us back in at a later
1781 vn_syncer_add_to_worklist(*bo, syncdelay);
1785 mtx_lock(&sync_mtx);
1789 static int first_printf = 1;
1792 * System filesystem synchronizer daemon.
1797 struct synclist *next, *slp;
1800 struct thread *td = curthread;
1802 int net_worklist_len;
1803 int syncer_final_iter;
1807 syncer_final_iter = 0;
1808 syncer_state = SYNCER_RUNNING;
1809 starttime = time_uptime;
1810 td->td_pflags |= TDP_NORUNNINGBUF;
1812 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1815 mtx_lock(&sync_mtx);
1817 if (syncer_state == SYNCER_FINAL_DELAY &&
1818 syncer_final_iter == 0) {
1819 mtx_unlock(&sync_mtx);
1820 kproc_suspend_check(td->td_proc);
1821 mtx_lock(&sync_mtx);
1823 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1824 if (syncer_state != SYNCER_RUNNING &&
1825 starttime != time_uptime) {
1827 printf("\nSyncing disks, vnodes remaining...");
1830 printf("%d ", net_worklist_len);
1832 starttime = time_uptime;
1835 * Push files whose dirty time has expired. Be careful
1836 * of interrupt race on slp queue.
1838 * Skip over empty worklist slots when shutting down.
1841 slp = &syncer_workitem_pending[syncer_delayno];
1842 syncer_delayno += 1;
1843 if (syncer_delayno == syncer_maxdelay)
1845 next = &syncer_workitem_pending[syncer_delayno];
1847 * If the worklist has wrapped since the
1848 * it was emptied of all but syncer vnodes,
1849 * switch to the FINAL_DELAY state and run
1850 * for one more second.
1852 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1853 net_worklist_len == 0 &&
1854 last_work_seen == syncer_delayno) {
1855 syncer_state = SYNCER_FINAL_DELAY;
1856 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1858 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1859 syncer_worklist_len > 0);
1862 * Keep track of the last time there was anything
1863 * on the worklist other than syncer vnodes.
1864 * Return to the SHUTTING_DOWN state if any
1867 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1868 last_work_seen = syncer_delayno;
1869 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1870 syncer_state = SYNCER_SHUTTING_DOWN;
1871 while (!LIST_EMPTY(slp)) {
1872 error = sync_vnode(slp, &bo, td);
1874 LIST_REMOVE(bo, bo_synclist);
1875 LIST_INSERT_HEAD(next, bo, bo_synclist);
1879 if (first_printf == 0)
1880 wdog_kern_pat(WD_LASTVAL);
1883 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1884 syncer_final_iter--;
1886 * The variable rushjob allows the kernel to speed up the
1887 * processing of the filesystem syncer process. A rushjob
1888 * value of N tells the filesystem syncer to process the next
1889 * N seconds worth of work on its queue ASAP. Currently rushjob
1890 * is used by the soft update code to speed up the filesystem
1891 * syncer process when the incore state is getting so far
1892 * ahead of the disk that the kernel memory pool is being
1893 * threatened with exhaustion.
1900 * Just sleep for a short period of time between
1901 * iterations when shutting down to allow some I/O
1904 * If it has taken us less than a second to process the
1905 * current work, then wait. Otherwise start right over
1906 * again. We can still lose time if any single round
1907 * takes more than two seconds, but it does not really
1908 * matter as we are just trying to generally pace the
1909 * filesystem activity.
1911 if (syncer_state != SYNCER_RUNNING ||
1912 time_uptime == starttime) {
1914 sched_prio(td, PPAUSE);
1917 if (syncer_state != SYNCER_RUNNING)
1918 cv_timedwait(&sync_wakeup, &sync_mtx,
1919 hz / SYNCER_SHUTDOWN_SPEEDUP);
1920 else if (time_uptime == starttime)
1921 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1926 * Request the syncer daemon to speed up its work.
1927 * We never push it to speed up more than half of its
1928 * normal turn time, otherwise it could take over the cpu.
1931 speedup_syncer(void)
1935 mtx_lock(&sync_mtx);
1936 if (rushjob < syncdelay / 2) {
1938 stat_rush_requests += 1;
1941 mtx_unlock(&sync_mtx);
1942 cv_broadcast(&sync_wakeup);
1947 * Tell the syncer to speed up its work and run though its work
1948 * list several times, then tell it to shut down.
1951 syncer_shutdown(void *arg, int howto)
1954 if (howto & RB_NOSYNC)
1956 mtx_lock(&sync_mtx);
1957 syncer_state = SYNCER_SHUTTING_DOWN;
1959 mtx_unlock(&sync_mtx);
1960 cv_broadcast(&sync_wakeup);
1961 kproc_shutdown(arg, howto);
1965 syncer_suspend(void)
1968 syncer_shutdown(updateproc, 0);
1975 mtx_lock(&sync_mtx);
1977 syncer_state = SYNCER_RUNNING;
1978 mtx_unlock(&sync_mtx);
1979 cv_broadcast(&sync_wakeup);
1980 kproc_resume(updateproc);
1984 * Reassign a buffer from one vnode to another.
1985 * Used to assign file specific control information
1986 * (indirect blocks) to the vnode to which they belong.
1989 reassignbuf(struct buf *bp)
2002 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2003 bp, bp->b_vp, bp->b_flags);
2005 * B_PAGING flagged buffers cannot be reassigned because their vp
2006 * is not fully linked in.
2008 if (bp->b_flags & B_PAGING)
2009 panic("cannot reassign paging buffer");
2012 * Delete from old vnode list, if on one.
2015 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2016 buf_vlist_remove(bp);
2018 panic("reassignbuf: Buffer %p not on queue.", bp);
2020 * If dirty, put on list of dirty buffers; otherwise insert onto list
2023 if (bp->b_flags & B_DELWRI) {
2024 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2025 switch (vp->v_type) {
2035 vn_syncer_add_to_worklist(bo, delay);
2037 buf_vlist_add(bp, bo, BX_VNDIRTY);
2039 buf_vlist_add(bp, bo, BX_VNCLEAN);
2041 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2042 mtx_lock(&sync_mtx);
2043 LIST_REMOVE(bo, bo_synclist);
2044 syncer_worklist_len--;
2045 mtx_unlock(&sync_mtx);
2046 bo->bo_flag &= ~BO_ONWORKLST;
2051 bp = TAILQ_FIRST(&bv->bv_hd);
2052 KASSERT(bp == NULL || bp->b_bufobj == bo,
2053 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2054 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2055 KASSERT(bp == NULL || bp->b_bufobj == bo,
2056 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2058 bp = TAILQ_FIRST(&bv->bv_hd);
2059 KASSERT(bp == NULL || bp->b_bufobj == bo,
2060 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2061 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2062 KASSERT(bp == NULL || bp->b_bufobj == bo,
2063 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2069 * Increment the use and hold counts on the vnode, taking care to reference
2070 * the driver's usecount if this is a chardev. The vholdl() will remove
2071 * the vnode from the free list if it is presently free. Requires the
2072 * vnode interlock and returns with it held.
2075 v_incr_usecount(struct vnode *vp)
2078 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2081 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2083 vp->v_rdev->si_usecount++;
2089 * Turn a holdcnt into a use+holdcnt such that only one call to
2090 * v_decr_usecount is needed.
2093 v_upgrade_usecount(struct vnode *vp)
2096 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2098 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2100 vp->v_rdev->si_usecount++;
2106 * Decrement the vnode use and hold count along with the driver's usecount
2107 * if this is a chardev. The vdropl() below releases the vnode interlock
2108 * as it may free the vnode.
2111 v_decr_usecount(struct vnode *vp)
2114 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2115 VNASSERT(vp->v_usecount > 0, vp,
2116 ("v_decr_usecount: negative usecount"));
2117 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2119 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2121 vp->v_rdev->si_usecount--;
2128 * Decrement only the use count and driver use count. This is intended to
2129 * be paired with a follow on vdropl() to release the remaining hold count.
2130 * In this way we may vgone() a vnode with a 0 usecount without risk of
2131 * having it end up on a free list because the hold count is kept above 0.
2134 v_decr_useonly(struct vnode *vp)
2137 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2138 VNASSERT(vp->v_usecount > 0, vp,
2139 ("v_decr_useonly: negative usecount"));
2140 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2142 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2144 vp->v_rdev->si_usecount--;
2150 * Grab a particular vnode from the free list, increment its
2151 * reference count and lock it. VI_DOOMED is set if the vnode
2152 * is being destroyed. Only callers who specify LK_RETRY will
2153 * see doomed vnodes. If inactive processing was delayed in
2154 * vput try to do it here.
2157 vget(struct vnode *vp, int flags, struct thread *td)
2162 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2163 ("vget: invalid lock operation"));
2164 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2166 if ((flags & LK_INTERLOCK) == 0)
2169 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2171 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2175 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2176 panic("vget: vn_lock failed to return ENOENT\n");
2178 /* Upgrade our holdcnt to a usecount. */
2179 v_upgrade_usecount(vp);
2181 * We don't guarantee that any particular close will
2182 * trigger inactive processing so just make a best effort
2183 * here at preventing a reference to a removed file. If
2184 * we don't succeed no harm is done.
2186 if (vp->v_iflag & VI_OWEINACT) {
2187 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2188 (flags & LK_NOWAIT) == 0)
2190 vp->v_iflag &= ~VI_OWEINACT;
2197 * Increase the reference count of a vnode.
2200 vref(struct vnode *vp)
2203 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2205 v_incr_usecount(vp);
2210 * Return reference count of a vnode.
2212 * The results of this call are only guaranteed when some mechanism other
2213 * than the VI lock is used to stop other processes from gaining references
2214 * to the vnode. This may be the case if the caller holds the only reference.
2215 * This is also useful when stale data is acceptable as race conditions may
2216 * be accounted for by some other means.
2219 vrefcnt(struct vnode *vp)
2224 usecnt = vp->v_usecount;
2230 #define VPUTX_VRELE 1
2231 #define VPUTX_VPUT 2
2232 #define VPUTX_VUNREF 3
2235 vputx(struct vnode *vp, int func)
2239 KASSERT(vp != NULL, ("vputx: null vp"));
2240 if (func == VPUTX_VUNREF)
2241 ASSERT_VOP_LOCKED(vp, "vunref");
2242 else if (func == VPUTX_VPUT)
2243 ASSERT_VOP_LOCKED(vp, "vput");
2245 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2246 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2249 /* Skip this v_writecount check if we're going to panic below. */
2250 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2251 ("vputx: missed vn_close"));
2254 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2255 vp->v_usecount == 1)) {
2256 if (func == VPUTX_VPUT)
2258 v_decr_usecount(vp);
2262 if (vp->v_usecount != 1) {
2263 vprint("vputx: negative ref count", vp);
2264 panic("vputx: negative ref cnt");
2266 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2268 * We want to hold the vnode until the inactive finishes to
2269 * prevent vgone() races. We drop the use count here and the
2270 * hold count below when we're done.
2274 * We must call VOP_INACTIVE with the node locked. Mark
2275 * as VI_DOINGINACT to avoid recursion.
2277 vp->v_iflag |= VI_OWEINACT;
2280 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2284 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2285 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2291 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2292 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2297 if (vp->v_usecount > 0)
2298 vp->v_iflag &= ~VI_OWEINACT;
2300 if (vp->v_iflag & VI_OWEINACT)
2301 vinactive(vp, curthread);
2302 if (func != VPUTX_VUNREF)
2309 * Vnode put/release.
2310 * If count drops to zero, call inactive routine and return to freelist.
2313 vrele(struct vnode *vp)
2316 vputx(vp, VPUTX_VRELE);
2320 * Release an already locked vnode. This give the same effects as
2321 * unlock+vrele(), but takes less time and avoids releasing and
2322 * re-aquiring the lock (as vrele() acquires the lock internally.)
2325 vput(struct vnode *vp)
2328 vputx(vp, VPUTX_VPUT);
2332 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2335 vunref(struct vnode *vp)
2338 vputx(vp, VPUTX_VUNREF);
2342 * Somebody doesn't want the vnode recycled.
2345 vhold(struct vnode *vp)
2354 * Increase the hold count and activate if this is the first reference.
2357 vholdl(struct vnode *vp)
2361 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2363 /* getnewvnode() calls v_incr_usecount() without holding interlock. */
2364 if (vp->v_type != VNON || vp->v_data != NULL) {
2365 ASSERT_VI_LOCKED(vp, "vholdl");
2366 VNASSERT(vp->v_holdcnt > 0 || (vp->v_iflag & VI_FREE) != 0,
2367 vp, ("vholdl: free vnode is held"));
2371 if ((vp->v_iflag & VI_FREE) == 0)
2373 VNASSERT(vp->v_holdcnt == 1, vp, ("vholdl: wrong hold count"));
2374 VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed."));
2376 * Remove a vnode from the free list, mark it as in use,
2377 * and put it on the active list.
2379 mtx_lock(&vnode_free_list_mtx);
2380 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2382 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2383 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2384 ("Activating already active vnode"));
2385 vp->v_iflag |= VI_ACTIVE;
2387 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2388 mp->mnt_activevnodelistsize++;
2389 mtx_unlock(&vnode_free_list_mtx);
2393 * Note that there is one less who cares about this vnode.
2394 * vdrop() is the opposite of vhold().
2397 vdrop(struct vnode *vp)
2405 * Drop the hold count of the vnode. If this is the last reference to
2406 * the vnode we place it on the free list unless it has been vgone'd
2407 * (marked VI_DOOMED) in which case we will free it.
2410 vdropl(struct vnode *vp)
2416 ASSERT_VI_LOCKED(vp, "vdropl");
2417 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2418 if (vp->v_holdcnt <= 0)
2419 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2421 if (vp->v_holdcnt > 0) {
2425 if ((vp->v_iflag & VI_DOOMED) == 0) {
2427 * Mark a vnode as free: remove it from its active list
2428 * and put it up for recycling on the freelist.
2430 VNASSERT(vp->v_op != NULL, vp,
2431 ("vdropl: vnode already reclaimed."));
2432 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2433 ("vnode already free"));
2434 VNASSERT(vp->v_holdcnt == 0, vp,
2435 ("vdropl: freeing when we shouldn't"));
2436 active = vp->v_iflag & VI_ACTIVE;
2437 vp->v_iflag &= ~VI_ACTIVE;
2439 mtx_lock(&vnode_free_list_mtx);
2441 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2443 mp->mnt_activevnodelistsize--;
2445 if (vp->v_iflag & VI_AGE) {
2446 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_actfreelist);
2448 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
2451 vp->v_iflag &= ~VI_AGE;
2452 vp->v_iflag |= VI_FREE;
2453 mtx_unlock(&vnode_free_list_mtx);
2458 * The vnode has been marked for destruction, so free it.
2460 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2461 atomic_subtract_long(&numvnodes, 1);
2463 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2464 ("cleaned vnode still on the free list."));
2465 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2466 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2467 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2468 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2469 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2470 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2471 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
2472 ("clean blk trie not empty"));
2473 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2474 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
2475 ("dirty blk trie not empty"));
2476 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2477 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2478 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2481 mac_vnode_destroy(vp);
2483 if (vp->v_pollinfo != NULL)
2484 destroy_vpollinfo(vp->v_pollinfo);
2486 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2489 rangelock_destroy(&vp->v_rl);
2490 lockdestroy(vp->v_vnlock);
2491 mtx_destroy(&vp->v_interlock);
2492 rw_destroy(BO_LOCKPTR(bo));
2493 uma_zfree(vnode_zone, vp);
2497 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2498 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2499 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2500 * failed lock upgrade.
2503 vinactive(struct vnode *vp, struct thread *td)
2505 struct vm_object *obj;
2507 ASSERT_VOP_ELOCKED(vp, "vinactive");
2508 ASSERT_VI_LOCKED(vp, "vinactive");
2509 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2510 ("vinactive: recursed on VI_DOINGINACT"));
2511 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2512 vp->v_iflag |= VI_DOINGINACT;
2513 vp->v_iflag &= ~VI_OWEINACT;
2516 * Before moving off the active list, we must be sure that any
2517 * modified pages are on the vnode's dirty list since these will
2518 * no longer be checked once the vnode is on the inactive list.
2519 * Because the vnode vm object keeps a hold reference on the vnode
2520 * if there is at least one resident non-cached page, the vnode
2521 * cannot leave the active list without the page cleanup done.
2524 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
2525 VM_OBJECT_WLOCK(obj);
2526 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
2527 VM_OBJECT_WUNLOCK(obj);
2529 VOP_INACTIVE(vp, td);
2531 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2532 ("vinactive: lost VI_DOINGINACT"));
2533 vp->v_iflag &= ~VI_DOINGINACT;
2537 * Remove any vnodes in the vnode table belonging to mount point mp.
2539 * If FORCECLOSE is not specified, there should not be any active ones,
2540 * return error if any are found (nb: this is a user error, not a
2541 * system error). If FORCECLOSE is specified, detach any active vnodes
2544 * If WRITECLOSE is set, only flush out regular file vnodes open for
2547 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2549 * `rootrefs' specifies the base reference count for the root vnode
2550 * of this filesystem. The root vnode is considered busy if its
2551 * v_usecount exceeds this value. On a successful return, vflush(, td)
2552 * will call vrele() on the root vnode exactly rootrefs times.
2553 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2557 static int busyprt = 0; /* print out busy vnodes */
2558 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2562 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2564 struct vnode *vp, *mvp, *rootvp = NULL;
2566 int busy = 0, error;
2568 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2571 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2572 ("vflush: bad args"));
2574 * Get the filesystem root vnode. We can vput() it
2575 * immediately, since with rootrefs > 0, it won't go away.
2577 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2578 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2585 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2587 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2590 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2594 * Skip over a vnodes marked VV_SYSTEM.
2596 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2602 * If WRITECLOSE is set, flush out unlinked but still open
2603 * files (even if open only for reading) and regular file
2604 * vnodes open for writing.
2606 if (flags & WRITECLOSE) {
2607 if (vp->v_object != NULL) {
2608 VM_OBJECT_WLOCK(vp->v_object);
2609 vm_object_page_clean(vp->v_object, 0, 0, 0);
2610 VM_OBJECT_WUNLOCK(vp->v_object);
2612 error = VOP_FSYNC(vp, MNT_WAIT, td);
2616 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2619 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2622 if ((vp->v_type == VNON ||
2623 (error == 0 && vattr.va_nlink > 0)) &&
2624 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2632 * With v_usecount == 0, all we need to do is clear out the
2633 * vnode data structures and we are done.
2635 * If FORCECLOSE is set, forcibly close the vnode.
2637 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2638 VNASSERT(vp->v_usecount == 0 ||
2639 vp->v_op != &devfs_specops ||
2640 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2641 ("device VNODE %p is FORCECLOSED", vp));
2647 vprint("vflush: busy vnode", vp);
2653 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2655 * If just the root vnode is busy, and if its refcount
2656 * is equal to `rootrefs', then go ahead and kill it.
2659 KASSERT(busy > 0, ("vflush: not busy"));
2660 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2661 ("vflush: usecount %d < rootrefs %d",
2662 rootvp->v_usecount, rootrefs));
2663 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2664 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2666 VOP_UNLOCK(rootvp, 0);
2672 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2676 for (; rootrefs > 0; rootrefs--)
2682 * Recycle an unused vnode to the front of the free list.
2685 vrecycle(struct vnode *vp)
2689 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2690 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2693 if (vp->v_usecount == 0) {
2702 * Eliminate all activity associated with a vnode
2703 * in preparation for reuse.
2706 vgone(struct vnode *vp)
2714 notify_lowervp_vfs_dummy(struct mount *mp __unused,
2715 struct vnode *lowervp __unused)
2720 * Notify upper mounts about reclaimed or unlinked vnode.
2723 vfs_notify_upper(struct vnode *vp, int event)
2725 static struct vfsops vgonel_vfsops = {
2726 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
2727 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
2729 struct mount *mp, *ump, *mmp;
2736 if (TAILQ_EMPTY(&mp->mnt_uppers))
2739 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
2740 mmp->mnt_op = &vgonel_vfsops;
2741 mmp->mnt_kern_flag |= MNTK_MARKER;
2743 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
2744 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
2745 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
2746 ump = TAILQ_NEXT(ump, mnt_upper_link);
2749 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
2752 case VFS_NOTIFY_UPPER_RECLAIM:
2753 VFS_RECLAIM_LOWERVP(ump, vp);
2755 case VFS_NOTIFY_UPPER_UNLINK:
2756 VFS_UNLINK_LOWERVP(ump, vp);
2759 KASSERT(0, ("invalid event %d", event));
2763 ump = TAILQ_NEXT(mmp, mnt_upper_link);
2764 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
2767 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
2768 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
2769 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
2770 wakeup(&mp->mnt_uppers);
2777 * vgone, with the vp interlock held.
2780 vgonel(struct vnode *vp)
2787 ASSERT_VOP_ELOCKED(vp, "vgonel");
2788 ASSERT_VI_LOCKED(vp, "vgonel");
2789 VNASSERT(vp->v_holdcnt, vp,
2790 ("vgonel: vp %p has no reference.", vp));
2791 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2795 * Don't vgonel if we're already doomed.
2797 if (vp->v_iflag & VI_DOOMED)
2799 vp->v_iflag |= VI_DOOMED;
2802 * Check to see if the vnode is in use. If so, we have to call
2803 * VOP_CLOSE() and VOP_INACTIVE().
2805 active = vp->v_usecount;
2806 oweinact = (vp->v_iflag & VI_OWEINACT);
2808 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
2811 * If purging an active vnode, it must be closed and
2812 * deactivated before being reclaimed.
2815 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2816 if (oweinact || active) {
2818 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2822 if (vp->v_type == VSOCK)
2823 vfs_unp_reclaim(vp);
2826 * Clean out any buffers associated with the vnode.
2827 * If the flush fails, just toss the buffers.
2830 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2831 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2832 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
2833 while (vinvalbuf(vp, 0, 0, 0) != 0)
2837 BO_LOCK(&vp->v_bufobj);
2838 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
2839 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
2840 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
2841 vp->v_bufobj.bo_clean.bv_cnt == 0,
2842 ("vp %p bufobj not invalidated", vp));
2843 vp->v_bufobj.bo_flag |= BO_DEAD;
2844 BO_UNLOCK(&vp->v_bufobj);
2848 * Reclaim the vnode.
2850 if (VOP_RECLAIM(vp, td))
2851 panic("vgone: cannot reclaim");
2853 vn_finished_secondary_write(mp);
2854 VNASSERT(vp->v_object == NULL, vp,
2855 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2857 * Clear the advisory locks and wake up waiting threads.
2859 (void)VOP_ADVLOCKPURGE(vp);
2861 * Delete from old mount point vnode list.
2866 * Done with purge, reset to the standard lock and invalidate
2870 vp->v_vnlock = &vp->v_lock;
2871 vp->v_op = &dead_vnodeops;
2877 * Calculate the total number of references to a special device.
2880 vcount(struct vnode *vp)
2885 count = vp->v_rdev->si_usecount;
2891 * Same as above, but using the struct cdev *as argument
2894 count_dev(struct cdev *dev)
2899 count = dev->si_usecount;
2905 * Print out a description of a vnode.
2907 static char *typename[] =
2908 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2912 vn_printf(struct vnode *vp, const char *fmt, ...)
2915 char buf[256], buf2[16];
2921 printf("%p: ", (void *)vp);
2922 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2923 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2924 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2927 if (vp->v_vflag & VV_ROOT)
2928 strlcat(buf, "|VV_ROOT", sizeof(buf));
2929 if (vp->v_vflag & VV_ISTTY)
2930 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2931 if (vp->v_vflag & VV_NOSYNC)
2932 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2933 if (vp->v_vflag & VV_ETERNALDEV)
2934 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
2935 if (vp->v_vflag & VV_CACHEDLABEL)
2936 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2937 if (vp->v_vflag & VV_TEXT)
2938 strlcat(buf, "|VV_TEXT", sizeof(buf));
2939 if (vp->v_vflag & VV_COPYONWRITE)
2940 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2941 if (vp->v_vflag & VV_SYSTEM)
2942 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2943 if (vp->v_vflag & VV_PROCDEP)
2944 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2945 if (vp->v_vflag & VV_NOKNOTE)
2946 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2947 if (vp->v_vflag & VV_DELETED)
2948 strlcat(buf, "|VV_DELETED", sizeof(buf));
2949 if (vp->v_vflag & VV_MD)
2950 strlcat(buf, "|VV_MD", sizeof(buf));
2951 if (vp->v_vflag & VV_FORCEINSMQ)
2952 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
2953 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
2954 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2955 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
2957 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2958 strlcat(buf, buf2, sizeof(buf));
2960 if (vp->v_iflag & VI_MOUNT)
2961 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2962 if (vp->v_iflag & VI_AGE)
2963 strlcat(buf, "|VI_AGE", sizeof(buf));
2964 if (vp->v_iflag & VI_DOOMED)
2965 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2966 if (vp->v_iflag & VI_FREE)
2967 strlcat(buf, "|VI_FREE", sizeof(buf));
2968 if (vp->v_iflag & VI_ACTIVE)
2969 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
2970 if (vp->v_iflag & VI_DOINGINACT)
2971 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2972 if (vp->v_iflag & VI_OWEINACT)
2973 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2974 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2975 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
2977 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2978 strlcat(buf, buf2, sizeof(buf));
2980 printf(" flags (%s)\n", buf + 1);
2981 if (mtx_owned(VI_MTX(vp)))
2982 printf(" VI_LOCKed");
2983 if (vp->v_object != NULL)
2984 printf(" v_object %p ref %d pages %d "
2985 "cleanbuf %d dirtybuf %d\n",
2986 vp->v_object, vp->v_object->ref_count,
2987 vp->v_object->resident_page_count,
2988 vp->v_bufobj.bo_dirty.bv_cnt,
2989 vp->v_bufobj.bo_clean.bv_cnt);
2991 lockmgr_printinfo(vp->v_vnlock);
2992 if (vp->v_data != NULL)
2998 * List all of the locked vnodes in the system.
2999 * Called when debugging the kernel.
3001 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3007 * Note: because this is DDB, we can't obey the locking semantics
3008 * for these structures, which means we could catch an inconsistent
3009 * state and dereference a nasty pointer. Not much to be done
3012 db_printf("Locked vnodes\n");
3013 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3014 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3015 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3022 * Show details about the given vnode.
3024 DB_SHOW_COMMAND(vnode, db_show_vnode)
3030 vp = (struct vnode *)addr;
3031 vn_printf(vp, "vnode ");
3035 * Show details about the given mount point.
3037 DB_SHOW_COMMAND(mount, db_show_mount)
3048 /* No address given, print short info about all mount points. */
3049 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3050 db_printf("%p %s on %s (%s)\n", mp,
3051 mp->mnt_stat.f_mntfromname,
3052 mp->mnt_stat.f_mntonname,
3053 mp->mnt_stat.f_fstypename);
3057 db_printf("\nMore info: show mount <addr>\n");
3061 mp = (struct mount *)addr;
3062 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3063 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3066 mflags = mp->mnt_flag;
3067 #define MNT_FLAG(flag) do { \
3068 if (mflags & (flag)) { \
3069 if (buf[0] != '\0') \
3070 strlcat(buf, ", ", sizeof(buf)); \
3071 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3072 mflags &= ~(flag); \
3075 MNT_FLAG(MNT_RDONLY);
3076 MNT_FLAG(MNT_SYNCHRONOUS);
3077 MNT_FLAG(MNT_NOEXEC);
3078 MNT_FLAG(MNT_NOSUID);
3079 MNT_FLAG(MNT_NFS4ACLS);
3080 MNT_FLAG(MNT_UNION);
3081 MNT_FLAG(MNT_ASYNC);
3082 MNT_FLAG(MNT_SUIDDIR);
3083 MNT_FLAG(MNT_SOFTDEP);
3084 MNT_FLAG(MNT_NOSYMFOLLOW);
3085 MNT_FLAG(MNT_GJOURNAL);
3086 MNT_FLAG(MNT_MULTILABEL);
3088 MNT_FLAG(MNT_NOATIME);
3089 MNT_FLAG(MNT_NOCLUSTERR);
3090 MNT_FLAG(MNT_NOCLUSTERW);
3092 MNT_FLAG(MNT_EXRDONLY);
3093 MNT_FLAG(MNT_EXPORTED);
3094 MNT_FLAG(MNT_DEFEXPORTED);
3095 MNT_FLAG(MNT_EXPORTANON);
3096 MNT_FLAG(MNT_EXKERB);
3097 MNT_FLAG(MNT_EXPUBLIC);
3098 MNT_FLAG(MNT_LOCAL);
3099 MNT_FLAG(MNT_QUOTA);
3100 MNT_FLAG(MNT_ROOTFS);
3102 MNT_FLAG(MNT_IGNORE);
3103 MNT_FLAG(MNT_UPDATE);
3104 MNT_FLAG(MNT_DELEXPORT);
3105 MNT_FLAG(MNT_RELOAD);
3106 MNT_FLAG(MNT_FORCE);
3107 MNT_FLAG(MNT_SNAPSHOT);
3108 MNT_FLAG(MNT_BYFSID);
3112 strlcat(buf, ", ", sizeof(buf));
3113 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3114 "0x%016jx", mflags);
3116 db_printf(" mnt_flag = %s\n", buf);
3119 flags = mp->mnt_kern_flag;
3120 #define MNT_KERN_FLAG(flag) do { \
3121 if (flags & (flag)) { \
3122 if (buf[0] != '\0') \
3123 strlcat(buf, ", ", sizeof(buf)); \
3124 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3128 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3129 MNT_KERN_FLAG(MNTK_ASYNC);
3130 MNT_KERN_FLAG(MNTK_SOFTDEP);
3131 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3132 MNT_KERN_FLAG(MNTK_DRAINING);
3133 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3134 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3135 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3136 MNT_KERN_FLAG(MNTK_NO_IOPF);
3137 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3138 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3139 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3140 MNT_KERN_FLAG(MNTK_MARKER);
3141 MNT_KERN_FLAG(MNTK_USES_BCACHE);
3142 MNT_KERN_FLAG(MNTK_NOASYNC);
3143 MNT_KERN_FLAG(MNTK_UNMOUNT);
3144 MNT_KERN_FLAG(MNTK_MWAIT);
3145 MNT_KERN_FLAG(MNTK_SUSPEND);
3146 MNT_KERN_FLAG(MNTK_SUSPEND2);
3147 MNT_KERN_FLAG(MNTK_SUSPENDED);
3148 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3149 MNT_KERN_FLAG(MNTK_NOKNOTE);
3150 #undef MNT_KERN_FLAG
3153 strlcat(buf, ", ", sizeof(buf));
3154 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3157 db_printf(" mnt_kern_flag = %s\n", buf);
3159 db_printf(" mnt_opt = ");
3160 opt = TAILQ_FIRST(mp->mnt_opt);
3162 db_printf("%s", opt->name);
3163 opt = TAILQ_NEXT(opt, link);
3164 while (opt != NULL) {
3165 db_printf(", %s", opt->name);
3166 opt = TAILQ_NEXT(opt, link);
3172 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3173 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3174 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3175 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3176 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3177 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3178 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3179 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3180 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3181 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3182 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3183 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3185 db_printf(" mnt_cred = { uid=%u ruid=%u",
3186 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3187 if (jailed(mp->mnt_cred))
3188 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3190 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3191 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3192 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3193 db_printf(" mnt_activevnodelistsize = %d\n",
3194 mp->mnt_activevnodelistsize);
3195 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3196 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3197 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3198 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3199 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3200 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3201 db_printf(" mnt_secondary_accwrites = %d\n",
3202 mp->mnt_secondary_accwrites);
3203 db_printf(" mnt_gjprovider = %s\n",
3204 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3206 db_printf("\n\nList of active vnodes\n");
3207 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3208 if (vp->v_type != VMARKER) {
3209 vn_printf(vp, "vnode ");
3214 db_printf("\n\nList of inactive vnodes\n");
3215 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3216 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3217 vn_printf(vp, "vnode ");
3226 * Fill in a struct xvfsconf based on a struct vfsconf.
3229 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3231 struct xvfsconf xvfsp;
3233 bzero(&xvfsp, sizeof(xvfsp));
3234 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3235 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3236 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3237 xvfsp.vfc_flags = vfsp->vfc_flags;
3239 * These are unused in userland, we keep them
3240 * to not break binary compatibility.
3242 xvfsp.vfc_vfsops = NULL;
3243 xvfsp.vfc_next = NULL;
3244 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3247 #ifdef COMPAT_FREEBSD32
3249 uint32_t vfc_vfsops;
3250 char vfc_name[MFSNAMELEN];
3251 int32_t vfc_typenum;
3252 int32_t vfc_refcount;
3258 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3260 struct xvfsconf32 xvfsp;
3262 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3263 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3264 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3265 xvfsp.vfc_flags = vfsp->vfc_flags;
3266 xvfsp.vfc_vfsops = 0;
3268 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3273 * Top level filesystem related information gathering.
3276 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3278 struct vfsconf *vfsp;
3283 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3284 #ifdef COMPAT_FREEBSD32
3285 if (req->flags & SCTL_MASK32)
3286 error = vfsconf2x32(req, vfsp);
3289 error = vfsconf2x(req, vfsp);
3297 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3298 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3299 "S,xvfsconf", "List of all configured filesystems");
3301 #ifndef BURN_BRIDGES
3302 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3305 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3307 int *name = (int *)arg1 - 1; /* XXX */
3308 u_int namelen = arg2 + 1; /* XXX */
3309 struct vfsconf *vfsp;
3311 log(LOG_WARNING, "userland calling deprecated sysctl, "
3312 "please rebuild world\n");
3314 #if 1 || defined(COMPAT_PRELITE2)
3315 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3317 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3321 case VFS_MAXTYPENUM:
3324 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3327 return (ENOTDIR); /* overloaded */
3329 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3330 if (vfsp->vfc_typenum == name[2])
3335 return (EOPNOTSUPP);
3336 #ifdef COMPAT_FREEBSD32
3337 if (req->flags & SCTL_MASK32)
3338 return (vfsconf2x32(req, vfsp));
3341 return (vfsconf2x(req, vfsp));
3343 return (EOPNOTSUPP);
3346 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
3347 CTLFLAG_MPSAFE, vfs_sysctl,
3348 "Generic filesystem");
3350 #if 1 || defined(COMPAT_PRELITE2)
3353 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3356 struct vfsconf *vfsp;
3357 struct ovfsconf ovfs;
3360 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3361 bzero(&ovfs, sizeof(ovfs));
3362 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3363 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3364 ovfs.vfc_index = vfsp->vfc_typenum;
3365 ovfs.vfc_refcount = vfsp->vfc_refcount;
3366 ovfs.vfc_flags = vfsp->vfc_flags;
3367 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3377 #endif /* 1 || COMPAT_PRELITE2 */
3378 #endif /* !BURN_BRIDGES */
3380 #define KINFO_VNODESLOP 10
3383 * Dump vnode list (via sysctl).
3387 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3395 * Stale numvnodes access is not fatal here.
3398 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3400 /* Make an estimate */
3401 return (SYSCTL_OUT(req, 0, len));
3403 error = sysctl_wire_old_buffer(req, 0);
3406 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3408 mtx_lock(&mountlist_mtx);
3409 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3410 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3413 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3417 xvn[n].xv_size = sizeof *xvn;
3418 xvn[n].xv_vnode = vp;
3419 xvn[n].xv_id = 0; /* XXX compat */
3420 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3422 XV_COPY(writecount);
3428 xvn[n].xv_flag = vp->v_vflag;
3430 switch (vp->v_type) {
3437 if (vp->v_rdev == NULL) {
3441 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3444 xvn[n].xv_socket = vp->v_socket;
3447 xvn[n].xv_fifo = vp->v_fifoinfo;
3452 /* shouldn't happen? */
3460 mtx_lock(&mountlist_mtx);
3465 mtx_unlock(&mountlist_mtx);
3467 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3472 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
3473 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
3478 * Unmount all filesystems. The list is traversed in reverse order
3479 * of mounting to avoid dependencies.
3482 vfs_unmountall(void)
3488 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3492 * Since this only runs when rebooting, it is not interlocked.
3494 while(!TAILQ_EMPTY(&mountlist)) {
3495 mp = TAILQ_LAST(&mountlist, mntlist);
3496 error = dounmount(mp, MNT_FORCE, td);
3498 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3500 * XXX: Due to the way in which we mount the root
3501 * file system off of devfs, devfs will generate a
3502 * "busy" warning when we try to unmount it before
3503 * the root. Don't print a warning as a result in
3504 * order to avoid false positive errors that may
3505 * cause needless upset.
3507 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3508 printf("unmount of %s failed (",
3509 mp->mnt_stat.f_mntonname);
3513 printf("%d)\n", error);
3516 /* The unmount has removed mp from the mountlist */
3522 * perform msync on all vnodes under a mount point
3523 * the mount point must be locked.
3526 vfs_msync(struct mount *mp, int flags)
3528 struct vnode *vp, *mvp;
3529 struct vm_object *obj;
3531 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3532 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
3534 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3535 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3537 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3539 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3546 VM_OBJECT_WLOCK(obj);
3547 vm_object_page_clean(obj, 0, 0,
3549 OBJPC_SYNC : OBJPC_NOSYNC);
3550 VM_OBJECT_WUNLOCK(obj);
3560 destroy_vpollinfo_free(struct vpollinfo *vi)
3563 knlist_destroy(&vi->vpi_selinfo.si_note);
3564 mtx_destroy(&vi->vpi_lock);
3565 uma_zfree(vnodepoll_zone, vi);
3569 destroy_vpollinfo(struct vpollinfo *vi)
3572 knlist_clear(&vi->vpi_selinfo.si_note, 1);
3573 seldrain(&vi->vpi_selinfo);
3574 destroy_vpollinfo_free(vi);
3578 * Initalize per-vnode helper structure to hold poll-related state.
3581 v_addpollinfo(struct vnode *vp)
3583 struct vpollinfo *vi;
3585 if (vp->v_pollinfo != NULL)
3587 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3588 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3589 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3590 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3592 if (vp->v_pollinfo != NULL) {
3594 destroy_vpollinfo_free(vi);
3597 vp->v_pollinfo = vi;
3602 * Record a process's interest in events which might happen to
3603 * a vnode. Because poll uses the historic select-style interface
3604 * internally, this routine serves as both the ``check for any
3605 * pending events'' and the ``record my interest in future events''
3606 * functions. (These are done together, while the lock is held,
3607 * to avoid race conditions.)
3610 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3614 mtx_lock(&vp->v_pollinfo->vpi_lock);
3615 if (vp->v_pollinfo->vpi_revents & events) {
3617 * This leaves events we are not interested
3618 * in available for the other process which
3619 * which presumably had requested them
3620 * (otherwise they would never have been
3623 events &= vp->v_pollinfo->vpi_revents;
3624 vp->v_pollinfo->vpi_revents &= ~events;
3626 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3629 vp->v_pollinfo->vpi_events |= events;
3630 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3631 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3636 * Routine to create and manage a filesystem syncer vnode.
3638 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3639 static int sync_fsync(struct vop_fsync_args *);
3640 static int sync_inactive(struct vop_inactive_args *);
3641 static int sync_reclaim(struct vop_reclaim_args *);
3643 static struct vop_vector sync_vnodeops = {
3644 .vop_bypass = VOP_EOPNOTSUPP,
3645 .vop_close = sync_close, /* close */
3646 .vop_fsync = sync_fsync, /* fsync */
3647 .vop_inactive = sync_inactive, /* inactive */
3648 .vop_reclaim = sync_reclaim, /* reclaim */
3649 .vop_lock1 = vop_stdlock, /* lock */
3650 .vop_unlock = vop_stdunlock, /* unlock */
3651 .vop_islocked = vop_stdislocked, /* islocked */
3655 * Create a new filesystem syncer vnode for the specified mount point.
3658 vfs_allocate_syncvnode(struct mount *mp)
3662 static long start, incr, next;
3665 /* Allocate a new vnode */
3666 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3668 panic("vfs_allocate_syncvnode: getnewvnode() failed");
3670 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3671 vp->v_vflag |= VV_FORCEINSMQ;
3672 error = insmntque(vp, mp);
3674 panic("vfs_allocate_syncvnode: insmntque() failed");
3675 vp->v_vflag &= ~VV_FORCEINSMQ;
3678 * Place the vnode onto the syncer worklist. We attempt to
3679 * scatter them about on the list so that they will go off
3680 * at evenly distributed times even if all the filesystems
3681 * are mounted at once.
3684 if (next == 0 || next > syncer_maxdelay) {
3688 start = syncer_maxdelay / 2;
3689 incr = syncer_maxdelay;
3695 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3696 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3697 mtx_lock(&sync_mtx);
3699 if (mp->mnt_syncer == NULL) {
3700 mp->mnt_syncer = vp;
3703 mtx_unlock(&sync_mtx);
3706 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3713 vfs_deallocate_syncvnode(struct mount *mp)
3717 mtx_lock(&sync_mtx);
3718 vp = mp->mnt_syncer;
3720 mp->mnt_syncer = NULL;
3721 mtx_unlock(&sync_mtx);
3727 * Do a lazy sync of the filesystem.
3730 sync_fsync(struct vop_fsync_args *ap)
3732 struct vnode *syncvp = ap->a_vp;
3733 struct mount *mp = syncvp->v_mount;
3738 * We only need to do something if this is a lazy evaluation.
3740 if (ap->a_waitfor != MNT_LAZY)
3744 * Move ourselves to the back of the sync list.
3746 bo = &syncvp->v_bufobj;
3748 vn_syncer_add_to_worklist(bo, syncdelay);
3752 * Walk the list of vnodes pushing all that are dirty and
3753 * not already on the sync list.
3755 if (vfs_busy(mp, MBF_NOWAIT) != 0)
3757 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3761 save = curthread_pflags_set(TDP_SYNCIO);
3762 vfs_msync(mp, MNT_NOWAIT);
3763 error = VFS_SYNC(mp, MNT_LAZY);
3764 curthread_pflags_restore(save);
3765 vn_finished_write(mp);
3771 * The syncer vnode is no referenced.
3774 sync_inactive(struct vop_inactive_args *ap)
3782 * The syncer vnode is no longer needed and is being decommissioned.
3784 * Modifications to the worklist must be protected by sync_mtx.
3787 sync_reclaim(struct vop_reclaim_args *ap)
3789 struct vnode *vp = ap->a_vp;
3794 mtx_lock(&sync_mtx);
3795 if (vp->v_mount->mnt_syncer == vp)
3796 vp->v_mount->mnt_syncer = NULL;
3797 if (bo->bo_flag & BO_ONWORKLST) {
3798 LIST_REMOVE(bo, bo_synclist);
3799 syncer_worklist_len--;
3801 bo->bo_flag &= ~BO_ONWORKLST;
3803 mtx_unlock(&sync_mtx);
3810 * Check if vnode represents a disk device
3813 vn_isdisk(struct vnode *vp, int *errp)
3819 if (vp->v_type != VCHR)
3821 else if (vp->v_rdev == NULL)
3823 else if (vp->v_rdev->si_devsw == NULL)
3825 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3830 return (error == 0);
3834 * Common filesystem object access control check routine. Accepts a
3835 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3836 * and optional call-by-reference privused argument allowing vaccess()
3837 * to indicate to the caller whether privilege was used to satisfy the
3838 * request (obsoleted). Returns 0 on success, or an errno on failure.
3841 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3842 accmode_t accmode, struct ucred *cred, int *privused)
3844 accmode_t dac_granted;
3845 accmode_t priv_granted;
3847 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3848 ("invalid bit in accmode"));
3849 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3850 ("VAPPEND without VWRITE"));
3853 * Look for a normal, non-privileged way to access the file/directory
3854 * as requested. If it exists, go with that.
3857 if (privused != NULL)
3862 /* Check the owner. */
3863 if (cred->cr_uid == file_uid) {
3864 dac_granted |= VADMIN;
3865 if (file_mode & S_IXUSR)
3866 dac_granted |= VEXEC;
3867 if (file_mode & S_IRUSR)
3868 dac_granted |= VREAD;
3869 if (file_mode & S_IWUSR)
3870 dac_granted |= (VWRITE | VAPPEND);
3872 if ((accmode & dac_granted) == accmode)
3878 /* Otherwise, check the groups (first match) */
3879 if (groupmember(file_gid, cred)) {
3880 if (file_mode & S_IXGRP)
3881 dac_granted |= VEXEC;
3882 if (file_mode & S_IRGRP)
3883 dac_granted |= VREAD;
3884 if (file_mode & S_IWGRP)
3885 dac_granted |= (VWRITE | VAPPEND);
3887 if ((accmode & dac_granted) == accmode)
3893 /* Otherwise, check everyone else. */
3894 if (file_mode & S_IXOTH)
3895 dac_granted |= VEXEC;
3896 if (file_mode & S_IROTH)
3897 dac_granted |= VREAD;
3898 if (file_mode & S_IWOTH)
3899 dac_granted |= (VWRITE | VAPPEND);
3900 if ((accmode & dac_granted) == accmode)
3905 * Build a privilege mask to determine if the set of privileges
3906 * satisfies the requirements when combined with the granted mask
3907 * from above. For each privilege, if the privilege is required,
3908 * bitwise or the request type onto the priv_granted mask.
3914 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3915 * requests, instead of PRIV_VFS_EXEC.
3917 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3918 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3919 priv_granted |= VEXEC;
3922 * Ensure that at least one execute bit is on. Otherwise,
3923 * a privileged user will always succeed, and we don't want
3924 * this to happen unless the file really is executable.
3926 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3927 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
3928 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3929 priv_granted |= VEXEC;
3932 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3933 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3934 priv_granted |= VREAD;
3936 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3937 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3938 priv_granted |= (VWRITE | VAPPEND);
3940 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3941 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3942 priv_granted |= VADMIN;
3944 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3945 /* XXX audit: privilege used */
3946 if (privused != NULL)
3951 return ((accmode & VADMIN) ? EPERM : EACCES);
3955 * Credential check based on process requesting service, and per-attribute
3959 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3960 struct thread *td, accmode_t accmode)
3964 * Kernel-invoked always succeeds.
3970 * Do not allow privileged processes in jail to directly manipulate
3971 * system attributes.
3973 switch (attrnamespace) {
3974 case EXTATTR_NAMESPACE_SYSTEM:
3975 /* Potentially should be: return (EPERM); */
3976 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3977 case EXTATTR_NAMESPACE_USER:
3978 return (VOP_ACCESS(vp, accmode, cred, td));
3984 #ifdef DEBUG_VFS_LOCKS
3986 * This only exists to supress warnings from unlocked specfs accesses. It is
3987 * no longer ok to have an unlocked VFS.
3989 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3990 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3992 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3993 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
3994 "Drop into debugger on lock violation");
3996 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3997 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
3998 0, "Check for interlock across VOPs");
4000 int vfs_badlock_print = 1; /* Print lock violations. */
4001 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4002 0, "Print lock violations");
4005 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4006 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4007 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4011 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4015 if (vfs_badlock_backtrace)
4018 if (vfs_badlock_print)
4019 printf("%s: %p %s\n", str, (void *)vp, msg);
4020 if (vfs_badlock_ddb)
4021 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4025 assert_vi_locked(struct vnode *vp, const char *str)
4028 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4029 vfs_badlock("interlock is not locked but should be", str, vp);
4033 assert_vi_unlocked(struct vnode *vp, const char *str)
4036 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4037 vfs_badlock("interlock is locked but should not be", str, vp);
4041 assert_vop_locked(struct vnode *vp, const char *str)
4045 if (!IGNORE_LOCK(vp)) {
4046 locked = VOP_ISLOCKED(vp);
4047 if (locked == 0 || locked == LK_EXCLOTHER)
4048 vfs_badlock("is not locked but should be", str, vp);
4053 assert_vop_unlocked(struct vnode *vp, const char *str)
4056 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4057 vfs_badlock("is locked but should not be", str, vp);
4061 assert_vop_elocked(struct vnode *vp, const char *str)
4064 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4065 vfs_badlock("is not exclusive locked but should be", str, vp);
4070 assert_vop_elocked_other(struct vnode *vp, const char *str)
4073 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
4074 vfs_badlock("is not exclusive locked by another thread",
4079 assert_vop_slocked(struct vnode *vp, const char *str)
4082 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
4083 vfs_badlock("is not locked shared but should be", str, vp);
4086 #endif /* DEBUG_VFS_LOCKS */
4089 vop_rename_fail(struct vop_rename_args *ap)
4092 if (ap->a_tvp != NULL)
4094 if (ap->a_tdvp == ap->a_tvp)
4103 vop_rename_pre(void *ap)
4105 struct vop_rename_args *a = ap;
4107 #ifdef DEBUG_VFS_LOCKS
4109 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4110 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4111 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4112 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4114 /* Check the source (from). */
4115 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4116 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4117 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4118 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4119 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4121 /* Check the target. */
4123 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4124 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4126 if (a->a_tdvp != a->a_fdvp)
4128 if (a->a_tvp != a->a_fvp)
4136 vop_strategy_pre(void *ap)
4138 #ifdef DEBUG_VFS_LOCKS
4139 struct vop_strategy_args *a;
4146 * Cluster ops lock their component buffers but not the IO container.
4148 if ((bp->b_flags & B_CLUSTER) != 0)
4151 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4152 if (vfs_badlock_print)
4154 "VOP_STRATEGY: bp is not locked but should be\n");
4155 if (vfs_badlock_ddb)
4156 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4162 vop_lock_pre(void *ap)
4164 #ifdef DEBUG_VFS_LOCKS
4165 struct vop_lock1_args *a = ap;
4167 if ((a->a_flags & LK_INTERLOCK) == 0)
4168 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4170 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4175 vop_lock_post(void *ap, int rc)
4177 #ifdef DEBUG_VFS_LOCKS
4178 struct vop_lock1_args *a = ap;
4180 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4181 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4182 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4187 vop_unlock_pre(void *ap)
4189 #ifdef DEBUG_VFS_LOCKS
4190 struct vop_unlock_args *a = ap;
4192 if (a->a_flags & LK_INTERLOCK)
4193 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4194 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4199 vop_unlock_post(void *ap, int rc)
4201 #ifdef DEBUG_VFS_LOCKS
4202 struct vop_unlock_args *a = ap;
4204 if (a->a_flags & LK_INTERLOCK)
4205 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4210 vop_create_post(void *ap, int rc)
4212 struct vop_create_args *a = ap;
4215 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4219 vop_deleteextattr_post(void *ap, int rc)
4221 struct vop_deleteextattr_args *a = ap;
4224 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4228 vop_link_post(void *ap, int rc)
4230 struct vop_link_args *a = ap;
4233 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4234 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4239 vop_mkdir_post(void *ap, int rc)
4241 struct vop_mkdir_args *a = ap;
4244 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4248 vop_mknod_post(void *ap, int rc)
4250 struct vop_mknod_args *a = ap;
4253 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4257 vop_remove_post(void *ap, int rc)
4259 struct vop_remove_args *a = ap;
4262 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4263 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4268 vop_rename_post(void *ap, int rc)
4270 struct vop_rename_args *a = ap;
4273 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4274 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4275 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4277 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4279 if (a->a_tdvp != a->a_fdvp)
4281 if (a->a_tvp != a->a_fvp)
4289 vop_rmdir_post(void *ap, int rc)
4291 struct vop_rmdir_args *a = ap;
4294 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4295 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4300 vop_setattr_post(void *ap, int rc)
4302 struct vop_setattr_args *a = ap;
4305 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4309 vop_setextattr_post(void *ap, int rc)
4311 struct vop_setextattr_args *a = ap;
4314 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4318 vop_symlink_post(void *ap, int rc)
4320 struct vop_symlink_args *a = ap;
4323 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4326 static struct knlist fs_knlist;
4329 vfs_event_init(void *arg)
4331 knlist_init_mtx(&fs_knlist, NULL);
4333 /* XXX - correct order? */
4334 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4337 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4340 KNOTE_UNLOCKED(&fs_knlist, event);
4343 static int filt_fsattach(struct knote *kn);
4344 static void filt_fsdetach(struct knote *kn);
4345 static int filt_fsevent(struct knote *kn, long hint);
4347 struct filterops fs_filtops = {
4349 .f_attach = filt_fsattach,
4350 .f_detach = filt_fsdetach,
4351 .f_event = filt_fsevent
4355 filt_fsattach(struct knote *kn)
4358 kn->kn_flags |= EV_CLEAR;
4359 knlist_add(&fs_knlist, kn, 0);
4364 filt_fsdetach(struct knote *kn)
4367 knlist_remove(&fs_knlist, kn, 0);
4371 filt_fsevent(struct knote *kn, long hint)
4374 kn->kn_fflags |= hint;
4375 return (kn->kn_fflags != 0);
4379 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4385 error = SYSCTL_IN(req, &vc, sizeof(vc));
4388 if (vc.vc_vers != VFS_CTL_VERS1)
4390 mp = vfs_getvfs(&vc.vc_fsid);
4393 /* ensure that a specific sysctl goes to the right filesystem. */
4394 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4395 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4399 VCTLTOREQ(&vc, req);
4400 error = VFS_SYSCTL(mp, vc.vc_op, req);
4405 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4406 NULL, 0, sysctl_vfs_ctl, "",
4410 * Function to initialize a va_filerev field sensibly.
4411 * XXX: Wouldn't a random number make a lot more sense ??
4414 init_va_filerev(void)
4419 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4422 static int filt_vfsread(struct knote *kn, long hint);
4423 static int filt_vfswrite(struct knote *kn, long hint);
4424 static int filt_vfsvnode(struct knote *kn, long hint);
4425 static void filt_vfsdetach(struct knote *kn);
4426 static struct filterops vfsread_filtops = {
4428 .f_detach = filt_vfsdetach,
4429 .f_event = filt_vfsread
4431 static struct filterops vfswrite_filtops = {
4433 .f_detach = filt_vfsdetach,
4434 .f_event = filt_vfswrite
4436 static struct filterops vfsvnode_filtops = {
4438 .f_detach = filt_vfsdetach,
4439 .f_event = filt_vfsvnode
4443 vfs_knllock(void *arg)
4445 struct vnode *vp = arg;
4447 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4451 vfs_knlunlock(void *arg)
4453 struct vnode *vp = arg;
4459 vfs_knl_assert_locked(void *arg)
4461 #ifdef DEBUG_VFS_LOCKS
4462 struct vnode *vp = arg;
4464 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4469 vfs_knl_assert_unlocked(void *arg)
4471 #ifdef DEBUG_VFS_LOCKS
4472 struct vnode *vp = arg;
4474 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4479 vfs_kqfilter(struct vop_kqfilter_args *ap)
4481 struct vnode *vp = ap->a_vp;
4482 struct knote *kn = ap->a_kn;
4485 switch (kn->kn_filter) {
4487 kn->kn_fop = &vfsread_filtops;
4490 kn->kn_fop = &vfswrite_filtops;
4493 kn->kn_fop = &vfsvnode_filtops;
4499 kn->kn_hook = (caddr_t)vp;
4502 if (vp->v_pollinfo == NULL)
4504 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4506 knlist_add(knl, kn, 0);
4512 * Detach knote from vnode
4515 filt_vfsdetach(struct knote *kn)
4517 struct vnode *vp = (struct vnode *)kn->kn_hook;
4519 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4520 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4526 filt_vfsread(struct knote *kn, long hint)
4528 struct vnode *vp = (struct vnode *)kn->kn_hook;
4533 * filesystem is gone, so set the EOF flag and schedule
4534 * the knote for deletion.
4536 if (hint == NOTE_REVOKE) {
4538 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4543 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4547 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4548 res = (kn->kn_data != 0);
4555 filt_vfswrite(struct knote *kn, long hint)
4557 struct vnode *vp = (struct vnode *)kn->kn_hook;
4562 * filesystem is gone, so set the EOF flag and schedule
4563 * the knote for deletion.
4565 if (hint == NOTE_REVOKE)
4566 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4574 filt_vfsvnode(struct knote *kn, long hint)
4576 struct vnode *vp = (struct vnode *)kn->kn_hook;
4580 if (kn->kn_sfflags & hint)
4581 kn->kn_fflags |= hint;
4582 if (hint == NOTE_REVOKE) {
4583 kn->kn_flags |= EV_EOF;
4587 res = (kn->kn_fflags != 0);
4593 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4597 if (dp->d_reclen > ap->a_uio->uio_resid)
4598 return (ENAMETOOLONG);
4599 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4601 if (ap->a_ncookies != NULL) {
4602 if (ap->a_cookies != NULL)
4603 free(ap->a_cookies, M_TEMP);
4604 ap->a_cookies = NULL;
4605 *ap->a_ncookies = 0;
4609 if (ap->a_ncookies == NULL)
4612 KASSERT(ap->a_cookies,
4613 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4615 *ap->a_cookies = realloc(*ap->a_cookies,
4616 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4617 (*ap->a_cookies)[*ap->a_ncookies] = off;
4622 * Mark for update the access time of the file if the filesystem
4623 * supports VOP_MARKATIME. This functionality is used by execve and
4624 * mmap, so we want to avoid the I/O implied by directly setting
4625 * va_atime for the sake of efficiency.
4628 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4633 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4634 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4635 (void)VOP_MARKATIME(vp);
4639 * The purpose of this routine is to remove granularity from accmode_t,
4640 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4641 * VADMIN and VAPPEND.
4643 * If it returns 0, the caller is supposed to continue with the usual
4644 * access checks using 'accmode' as modified by this routine. If it
4645 * returns nonzero value, the caller is supposed to return that value
4648 * Note that after this routine runs, accmode may be zero.
4651 vfs_unixify_accmode(accmode_t *accmode)
4654 * There is no way to specify explicit "deny" rule using
4655 * file mode or POSIX.1e ACLs.
4657 if (*accmode & VEXPLICIT_DENY) {
4663 * None of these can be translated into usual access bits.
4664 * Also, the common case for NFSv4 ACLs is to not contain
4665 * either of these bits. Caller should check for VWRITE
4666 * on the containing directory instead.
4668 if (*accmode & (VDELETE_CHILD | VDELETE))
4671 if (*accmode & VADMIN_PERMS) {
4672 *accmode &= ~VADMIN_PERMS;
4677 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4678 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4680 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
4686 * These are helper functions for filesystems to traverse all
4687 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
4689 * This interface replaces MNT_VNODE_FOREACH.
4692 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
4695 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
4700 kern_yield(PRI_USER);
4702 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4703 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
4704 while (vp != NULL && (vp->v_type == VMARKER ||
4705 (vp->v_iflag & VI_DOOMED) != 0))
4706 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4708 /* Check if we are done */
4710 __mnt_vnode_markerfree_all(mvp, mp);
4711 /* MNT_IUNLOCK(mp); -- done in above function */
4712 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4715 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4716 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4723 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
4727 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4730 (*mvp)->v_type = VMARKER;
4732 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
4733 while (vp != NULL && (vp->v_type == VMARKER ||
4734 (vp->v_iflag & VI_DOOMED) != 0))
4735 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4737 /* Check if we are done */
4741 free(*mvp, M_VNODE_MARKER);
4745 (*mvp)->v_mount = mp;
4746 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4754 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
4762 mtx_assert(MNT_MTX(mp), MA_OWNED);
4764 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4765 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4768 free(*mvp, M_VNODE_MARKER);
4773 * These are helper functions for filesystems to traverse their
4774 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
4777 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4780 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4785 free(*mvp, M_VNODE_MARKER);
4789 static struct vnode *
4790 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4792 struct vnode *vp, *nvp;
4794 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
4795 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4797 vp = TAILQ_NEXT(*mvp, v_actfreelist);
4798 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4799 while (vp != NULL) {
4800 if (vp->v_type == VMARKER) {
4801 vp = TAILQ_NEXT(vp, v_actfreelist);
4804 if (!VI_TRYLOCK(vp)) {
4805 if (mp_ncpus == 1 || should_yield()) {
4806 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4807 mtx_unlock(&vnode_free_list_mtx);
4809 mtx_lock(&vnode_free_list_mtx);
4814 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
4815 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
4816 ("alien vnode on the active list %p %p", vp, mp));
4817 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
4819 nvp = TAILQ_NEXT(vp, v_actfreelist);
4824 /* Check if we are done */
4826 mtx_unlock(&vnode_free_list_mtx);
4827 mnt_vnode_markerfree_active(mvp, mp);
4830 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4831 mtx_unlock(&vnode_free_list_mtx);
4832 ASSERT_VI_LOCKED(vp, "active iter");
4833 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
4838 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4842 kern_yield(PRI_USER);
4843 mtx_lock(&vnode_free_list_mtx);
4844 return (mnt_vnode_next_active(mvp, mp));
4848 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
4852 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4856 (*mvp)->v_type = VMARKER;
4857 (*mvp)->v_mount = mp;
4859 mtx_lock(&vnode_free_list_mtx);
4860 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
4862 mtx_unlock(&vnode_free_list_mtx);
4863 mnt_vnode_markerfree_active(mvp, mp);
4866 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4867 return (mnt_vnode_next_active(mvp, mp));
4871 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4877 mtx_lock(&vnode_free_list_mtx);
4878 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4879 mtx_unlock(&vnode_free_list_mtx);
4880 mnt_vnode_markerfree_active(mvp, mp);