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/refcount.h>
72 #include <sys/rwlock.h>
73 #include <sys/sched.h>
74 #include <sys/sleepqueue.h>
77 #include <sys/sysctl.h>
78 #include <sys/syslog.h>
79 #include <sys/vmmeter.h>
80 #include <sys/vnode.h>
81 #include <sys/watchdog.h>
83 #include <machine/stdarg.h>
85 #include <security/mac/mac_framework.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_extern.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_kern.h>
100 static void delmntque(struct vnode *vp);
101 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
102 int slpflag, int slptimeo);
103 static void syncer_shutdown(void *arg, int howto);
104 static int vtryrecycle(struct vnode *vp);
105 static void v_init_counters(struct vnode *);
106 static void v_incr_usecount(struct vnode *);
107 static void v_incr_devcount(struct vnode *);
108 static void v_decr_devcount(struct vnode *);
109 static void vnlru_free(int);
110 static void vgonel(struct vnode *);
111 static void vfs_knllock(void *arg);
112 static void vfs_knlunlock(void *arg);
113 static void vfs_knl_assert_locked(void *arg);
114 static void vfs_knl_assert_unlocked(void *arg);
115 static void destroy_vpollinfo(struct vpollinfo *vi);
118 * Number of vnodes in existence. Increased whenever getnewvnode()
119 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
121 static unsigned long numvnodes;
123 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
124 "Number of vnodes in existence");
126 static u_long vnodes_created;
127 SYSCTL_ULONG(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
128 0, "Number of vnodes created by getnewvnode");
131 * Conversion tables for conversion from vnode types to inode formats
134 enum vtype iftovt_tab[16] = {
135 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
136 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
138 int vttoif_tab[10] = {
139 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
140 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
144 * List of vnodes that are ready for recycling.
146 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
149 * Free vnode target. Free vnodes may simply be files which have been stat'd
150 * but not read. This is somewhat common, and a small cache of such files
151 * should be kept to avoid recreation costs.
153 static u_long wantfreevnodes;
154 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
155 /* Number of vnodes in the free list. */
156 static u_long freevnodes;
157 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
158 "Number of vnodes in the free list");
160 static int vlru_allow_cache_src;
161 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
162 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
164 static u_long recycles_count;
165 SYSCTL_ULONG(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 0,
166 "Number of vnodes recycled to avoid exceding kern.maxvnodes");
169 * Various variables used for debugging the new implementation of
171 * XXX these are probably of (very) limited utility now.
173 static int reassignbufcalls;
174 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
175 "Number of calls to reassignbuf");
177 static u_long free_owe_inact;
178 SYSCTL_ULONG(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact, 0,
179 "Number of times free vnodes kept on active list due to VFS "
180 "owing inactivation");
183 * Cache for the mount type id assigned to NFS. This is used for
184 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
186 int nfs_mount_type = -1;
188 /* To keep more than one thread at a time from running vfs_getnewfsid */
189 static struct mtx mntid_mtx;
192 * Lock for any access to the following:
197 static struct mtx vnode_free_list_mtx;
199 /* Publicly exported FS */
200 struct nfs_public nfs_pub;
202 static uma_zone_t buf_trie_zone;
204 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
205 static uma_zone_t vnode_zone;
206 static uma_zone_t vnodepoll_zone;
209 * The workitem queue.
211 * It is useful to delay writes of file data and filesystem metadata
212 * for tens of seconds so that quickly created and deleted files need
213 * not waste disk bandwidth being created and removed. To realize this,
214 * we append vnodes to a "workitem" queue. When running with a soft
215 * updates implementation, most pending metadata dependencies should
216 * not wait for more than a few seconds. Thus, mounted on block devices
217 * are delayed only about a half the time that file data is delayed.
218 * Similarly, directory updates are more critical, so are only delayed
219 * about a third the time that file data is delayed. Thus, there are
220 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
221 * one each second (driven off the filesystem syncer process). The
222 * syncer_delayno variable indicates the next queue that is to be processed.
223 * Items that need to be processed soon are placed in this queue:
225 * syncer_workitem_pending[syncer_delayno]
227 * A delay of fifteen seconds is done by placing the request fifteen
228 * entries later in the queue:
230 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
233 static int syncer_delayno;
234 static long syncer_mask;
235 LIST_HEAD(synclist, bufobj);
236 static struct synclist *syncer_workitem_pending;
238 * The sync_mtx protects:
243 * syncer_workitem_pending
244 * syncer_worklist_len
247 static struct mtx sync_mtx;
248 static struct cv sync_wakeup;
250 #define SYNCER_MAXDELAY 32
251 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
252 static int syncdelay = 30; /* max time to delay syncing data */
253 static int filedelay = 30; /* time to delay syncing files */
254 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
255 "Time to delay syncing files (in seconds)");
256 static int dirdelay = 29; /* time to delay syncing directories */
257 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
258 "Time to delay syncing directories (in seconds)");
259 static int metadelay = 28; /* time to delay syncing metadata */
260 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
261 "Time to delay syncing metadata (in seconds)");
262 static int rushjob; /* number of slots to run ASAP */
263 static int stat_rush_requests; /* number of times I/O speeded up */
264 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
265 "Number of times I/O speeded up (rush requests)");
268 * When shutting down the syncer, run it at four times normal speed.
270 #define SYNCER_SHUTDOWN_SPEEDUP 4
271 static int sync_vnode_count;
272 static int syncer_worklist_len;
273 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
277 * Number of vnodes we want to exist at any one time. This is mostly used
278 * to size hash tables in vnode-related code. It is normally not used in
279 * getnewvnode(), as wantfreevnodes is normally nonzero.)
281 * XXX desiredvnodes is historical cruft and should not exist.
284 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
285 &desiredvnodes, 0, "Maximum number of vnodes");
286 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
287 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
288 static int vnlru_nowhere;
289 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
290 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
292 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
296 * Support for the bufobj clean & dirty pctrie.
299 buf_trie_alloc(struct pctrie *ptree)
302 return uma_zalloc(buf_trie_zone, M_NOWAIT);
306 buf_trie_free(struct pctrie *ptree, void *node)
309 uma_zfree(buf_trie_zone, node);
311 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
314 * Initialize the vnode management data structures.
316 * Reevaluate the following cap on the number of vnodes after the physical
317 * memory size exceeds 512GB. In the limit, as the physical memory size
318 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
320 #ifndef MAXVNODES_MAX
321 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
324 vntblinit(void *dummy __unused)
327 int physvnodes, virtvnodes;
330 * Desiredvnodes is a function of the physical memory size and the
331 * kernel's heap size. Generally speaking, it scales with the
332 * physical memory size. The ratio of desiredvnodes to physical pages
333 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
334 * marginal ratio of desiredvnodes to physical pages is one to
335 * sixteen. However, desiredvnodes is limited by the kernel's heap
336 * size. The memory required by desiredvnodes vnodes and vm objects
337 * may not exceed one seventh of the kernel's heap size.
339 physvnodes = maxproc + vm_cnt.v_page_count / 16 + 3 * min(98304 * 4,
340 vm_cnt.v_page_count) / 16;
341 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
342 sizeof(struct vnode)));
343 desiredvnodes = min(physvnodes, virtvnodes);
344 if (desiredvnodes > MAXVNODES_MAX) {
346 printf("Reducing kern.maxvnodes %d -> %d\n",
347 desiredvnodes, MAXVNODES_MAX);
348 desiredvnodes = MAXVNODES_MAX;
350 wantfreevnodes = desiredvnodes / 4;
351 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
352 TAILQ_INIT(&vnode_free_list);
353 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
354 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
355 NULL, NULL, UMA_ALIGN_PTR, 0);
356 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
357 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
359 * Preallocate enough nodes to support one-per buf so that
360 * we can not fail an insert. reassignbuf() callers can not
361 * tolerate the insertion failure.
363 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
364 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
365 UMA_ZONE_NOFREE | UMA_ZONE_VM);
366 uma_prealloc(buf_trie_zone, nbuf);
368 * Initialize the filesystem syncer.
370 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
372 syncer_maxdelay = syncer_mask + 1;
373 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
374 cv_init(&sync_wakeup, "syncer");
375 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
379 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
383 * Mark a mount point as busy. Used to synchronize access and to delay
384 * unmounting. Eventually, mountlist_mtx is not released on failure.
386 * vfs_busy() is a custom lock, it can block the caller.
387 * vfs_busy() only sleeps if the unmount is active on the mount point.
388 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
389 * vnode belonging to mp.
391 * Lookup uses vfs_busy() to traverse mount points.
393 * / vnode lock A / vnode lock (/var) D
394 * /var vnode lock B /log vnode lock(/var/log) E
395 * vfs_busy lock C vfs_busy lock F
397 * Within each file system, the lock order is C->A->B and F->D->E.
399 * When traversing across mounts, the system follows that lock order:
405 * The lookup() process for namei("/var") illustrates the process:
406 * VOP_LOOKUP() obtains B while A is held
407 * vfs_busy() obtains a shared lock on F while A and B are held
408 * vput() releases lock on B
409 * vput() releases lock on A
410 * VFS_ROOT() obtains lock on D while shared lock on F is held
411 * vfs_unbusy() releases shared lock on F
412 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
413 * Attempt to lock A (instead of vp_crossmp) while D is held would
414 * violate the global order, causing deadlocks.
416 * dounmount() locks B while F is drained.
419 vfs_busy(struct mount *mp, int flags)
422 MPASS((flags & ~MBF_MASK) == 0);
423 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
428 * If mount point is currenly being unmounted, sleep until the
429 * mount point fate is decided. If thread doing the unmounting fails,
430 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
431 * that this mount point has survived the unmount attempt and vfs_busy
432 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
433 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
434 * about to be really destroyed. vfs_busy needs to release its
435 * reference on the mount point in this case and return with ENOENT,
436 * telling the caller that mount mount it tried to busy is no longer
439 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
440 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
443 CTR1(KTR_VFS, "%s: failed busying before sleeping",
447 if (flags & MBF_MNTLSTLOCK)
448 mtx_unlock(&mountlist_mtx);
449 mp->mnt_kern_flag |= MNTK_MWAIT;
450 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
451 if (flags & MBF_MNTLSTLOCK)
452 mtx_lock(&mountlist_mtx);
455 if (flags & MBF_MNTLSTLOCK)
456 mtx_unlock(&mountlist_mtx);
463 * Free a busy filesystem.
466 vfs_unbusy(struct mount *mp)
469 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
472 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
474 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
475 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
476 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
477 mp->mnt_kern_flag &= ~MNTK_DRAINING;
478 wakeup(&mp->mnt_lockref);
484 * Lookup a mount point by filesystem identifier.
487 vfs_getvfs(fsid_t *fsid)
491 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
492 mtx_lock(&mountlist_mtx);
493 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
494 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
495 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
497 mtx_unlock(&mountlist_mtx);
501 mtx_unlock(&mountlist_mtx);
502 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
503 return ((struct mount *) 0);
507 * Lookup a mount point by filesystem identifier, busying it before
510 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
511 * cache for popular filesystem identifiers. The cache is lockess, using
512 * the fact that struct mount's are never freed. In worst case we may
513 * get pointer to unmounted or even different filesystem, so we have to
514 * check what we got, and go slow way if so.
517 vfs_busyfs(fsid_t *fsid)
519 #define FSID_CACHE_SIZE 256
520 typedef struct mount * volatile vmp_t;
521 static vmp_t cache[FSID_CACHE_SIZE];
526 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
527 hash = fsid->val[0] ^ fsid->val[1];
528 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
531 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
532 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
534 if (vfs_busy(mp, 0) != 0) {
538 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
539 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
545 mtx_lock(&mountlist_mtx);
546 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
547 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
548 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
549 error = vfs_busy(mp, MBF_MNTLSTLOCK);
552 mtx_unlock(&mountlist_mtx);
559 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
560 mtx_unlock(&mountlist_mtx);
561 return ((struct mount *) 0);
565 * Check if a user can access privileged mount options.
568 vfs_suser(struct mount *mp, struct thread *td)
573 * If the thread is jailed, but this is not a jail-friendly file
574 * system, deny immediately.
576 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
580 * If the file system was mounted outside the jail of the calling
581 * thread, deny immediately.
583 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
587 * If file system supports delegated administration, we don't check
588 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
589 * by the file system itself.
590 * If this is not the user that did original mount, we check for
591 * the PRIV_VFS_MOUNT_OWNER privilege.
593 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
594 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
595 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
602 * Get a new unique fsid. Try to make its val[0] unique, since this value
603 * will be used to create fake device numbers for stat(). Also try (but
604 * not so hard) make its val[0] unique mod 2^16, since some emulators only
605 * support 16-bit device numbers. We end up with unique val[0]'s for the
606 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
608 * Keep in mind that several mounts may be running in parallel. Starting
609 * the search one past where the previous search terminated is both a
610 * micro-optimization and a defense against returning the same fsid to
614 vfs_getnewfsid(struct mount *mp)
616 static uint16_t mntid_base;
621 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
622 mtx_lock(&mntid_mtx);
623 mtype = mp->mnt_vfc->vfc_typenum;
624 tfsid.val[1] = mtype;
625 mtype = (mtype & 0xFF) << 24;
627 tfsid.val[0] = makedev(255,
628 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
630 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
634 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
635 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
636 mtx_unlock(&mntid_mtx);
640 * Knob to control the precision of file timestamps:
642 * 0 = seconds only; nanoseconds zeroed.
643 * 1 = seconds and nanoseconds, accurate within 1/HZ.
644 * 2 = seconds and nanoseconds, truncated to microseconds.
645 * >=3 = seconds and nanoseconds, maximum precision.
647 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
649 static int timestamp_precision = TSP_USEC;
650 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
651 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
652 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
653 "3+: sec + ns (max. precision))");
656 * Get a current timestamp.
659 vfs_timestamp(struct timespec *tsp)
663 switch (timestamp_precision) {
665 tsp->tv_sec = time_second;
673 TIMEVAL_TO_TIMESPEC(&tv, tsp);
683 * Set vnode attributes to VNOVAL
686 vattr_null(struct vattr *vap)
690 vap->va_size = VNOVAL;
691 vap->va_bytes = VNOVAL;
692 vap->va_mode = VNOVAL;
693 vap->va_nlink = VNOVAL;
694 vap->va_uid = VNOVAL;
695 vap->va_gid = VNOVAL;
696 vap->va_fsid = VNOVAL;
697 vap->va_fileid = VNOVAL;
698 vap->va_blocksize = VNOVAL;
699 vap->va_rdev = VNOVAL;
700 vap->va_atime.tv_sec = VNOVAL;
701 vap->va_atime.tv_nsec = VNOVAL;
702 vap->va_mtime.tv_sec = VNOVAL;
703 vap->va_mtime.tv_nsec = VNOVAL;
704 vap->va_ctime.tv_sec = VNOVAL;
705 vap->va_ctime.tv_nsec = VNOVAL;
706 vap->va_birthtime.tv_sec = VNOVAL;
707 vap->va_birthtime.tv_nsec = VNOVAL;
708 vap->va_flags = VNOVAL;
709 vap->va_gen = VNOVAL;
714 * This routine is called when we have too many vnodes. It attempts
715 * to free <count> vnodes and will potentially free vnodes that still
716 * have VM backing store (VM backing store is typically the cause
717 * of a vnode blowout so we want to do this). Therefore, this operation
718 * is not considered cheap.
720 * A number of conditions may prevent a vnode from being reclaimed.
721 * the buffer cache may have references on the vnode, a directory
722 * vnode may still have references due to the namei cache representing
723 * underlying files, or the vnode may be in active use. It is not
724 * desireable to reuse such vnodes. These conditions may cause the
725 * number of vnodes to reach some minimum value regardless of what
726 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
729 vlrureclaim(struct mount *mp)
738 * Calculate the trigger point, don't allow user
739 * screwups to blow us up. This prevents us from
740 * recycling vnodes with lots of resident pages. We
741 * aren't trying to free memory, we are trying to
744 usevnodes = desiredvnodes;
747 trigger = vm_cnt.v_page_count * 2 / usevnodes;
749 vn_start_write(NULL, &mp, V_WAIT);
751 count = mp->mnt_nvnodelistsize / 10 + 1;
753 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
754 while (vp != NULL && vp->v_type == VMARKER)
755 vp = TAILQ_NEXT(vp, v_nmntvnodes);
758 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
759 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
764 * If it's been deconstructed already, it's still
765 * referenced, or it exceeds the trigger, skip it.
767 if (vp->v_usecount ||
768 (!vlru_allow_cache_src &&
769 !LIST_EMPTY(&(vp)->v_cache_src)) ||
770 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
771 vp->v_object->resident_page_count > trigger)) {
777 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
779 goto next_iter_mntunlocked;
783 * v_usecount may have been bumped after VOP_LOCK() dropped
784 * the vnode interlock and before it was locked again.
786 * It is not necessary to recheck VI_DOOMED because it can
787 * only be set by another thread that holds both the vnode
788 * lock and vnode interlock. If another thread has the
789 * vnode lock before we get to VOP_LOCK() and obtains the
790 * vnode interlock after VOP_LOCK() drops the vnode
791 * interlock, the other thread will be unable to drop the
792 * vnode lock before our VOP_LOCK() call fails.
794 if (vp->v_usecount ||
795 (!vlru_allow_cache_src &&
796 !LIST_EMPTY(&(vp)->v_cache_src)) ||
797 (vp->v_object != NULL &&
798 vp->v_object->resident_page_count > trigger)) {
799 VOP_UNLOCK(vp, LK_INTERLOCK);
801 goto next_iter_mntunlocked;
803 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
804 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
805 atomic_add_long(&recycles_count, 1);
810 next_iter_mntunlocked:
819 kern_yield(PRI_USER);
824 vn_finished_write(mp);
829 * Attempt to keep the free list at wantfreevnodes length.
832 vnlru_free(int count)
836 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
837 for (; count > 0; count--) {
838 vp = TAILQ_FIRST(&vnode_free_list);
840 * The list can be modified while the free_list_mtx
841 * has been dropped and vp could be NULL here.
845 VNASSERT(vp->v_op != NULL, vp,
846 ("vnlru_free: vnode already reclaimed."));
847 KASSERT((vp->v_iflag & VI_FREE) != 0,
848 ("Removing vnode not on freelist"));
849 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
850 ("Mangling active vnode"));
851 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
853 * Don't recycle if we can't get the interlock.
855 if (!VI_TRYLOCK(vp)) {
856 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
859 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
860 vp, ("vp inconsistent on freelist"));
863 * The clear of VI_FREE prevents activation of the
864 * vnode. There is no sense in putting the vnode on
865 * the mount point active list, only to remove it
866 * later during recycling. Inline the relevant part
867 * of vholdl(), to avoid triggering assertions or
871 vp->v_iflag &= ~VI_FREE;
872 refcount_acquire(&vp->v_holdcnt);
874 mtx_unlock(&vnode_free_list_mtx);
878 * If the recycled succeeded this vdrop will actually free
879 * the vnode. If not it will simply place it back on
883 mtx_lock(&vnode_free_list_mtx);
887 * Attempt to recycle vnodes in a context that is always safe to block.
888 * Calling vlrurecycle() from the bowels of filesystem code has some
889 * interesting deadlock problems.
891 static struct proc *vnlruproc;
892 static int vnlruproc_sig;
897 struct mount *mp, *nmp;
899 struct proc *p = vnlruproc;
901 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
905 kproc_suspend_check(p);
906 mtx_lock(&vnode_free_list_mtx);
907 if (freevnodes > wantfreevnodes)
908 vnlru_free(freevnodes - wantfreevnodes);
909 if (numvnodes <= desiredvnodes * 9 / 10) {
911 wakeup(&vnlruproc_sig);
912 msleep(vnlruproc, &vnode_free_list_mtx,
913 PVFS|PDROP, "vlruwt", hz);
916 mtx_unlock(&vnode_free_list_mtx);
918 mtx_lock(&mountlist_mtx);
919 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
920 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
921 nmp = TAILQ_NEXT(mp, mnt_list);
924 done += vlrureclaim(mp);
925 mtx_lock(&mountlist_mtx);
926 nmp = TAILQ_NEXT(mp, mnt_list);
929 mtx_unlock(&mountlist_mtx);
932 /* These messages are temporary debugging aids */
933 if (vnlru_nowhere < 5)
934 printf("vnlru process getting nowhere..\n");
935 else if (vnlru_nowhere == 5)
936 printf("vnlru process messages stopped.\n");
939 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
941 kern_yield(PRI_USER);
945 static struct kproc_desc vnlru_kp = {
950 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
954 * Routines having to do with the management of the vnode table.
958 * Try to recycle a freed vnode. We abort if anyone picks up a reference
959 * before we actually vgone(). This function must be called with the vnode
960 * held to prevent the vnode from being returned to the free list midway
964 vtryrecycle(struct vnode *vp)
968 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
969 VNASSERT(vp->v_holdcnt, vp,
970 ("vtryrecycle: Recycling vp %p without a reference.", vp));
972 * This vnode may found and locked via some other list, if so we
973 * can't recycle it yet.
975 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
977 "%s: impossible to recycle, vp %p lock is already held",
979 return (EWOULDBLOCK);
982 * Don't recycle if its filesystem is being suspended.
984 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
987 "%s: impossible to recycle, cannot start the write for %p",
992 * If we got this far, we need to acquire the interlock and see if
993 * anyone picked up this vnode from another list. If not, we will
994 * mark it with DOOMED via vgonel() so that anyone who does find it
998 if (vp->v_usecount) {
999 VOP_UNLOCK(vp, LK_INTERLOCK);
1000 vn_finished_write(vnmp);
1002 "%s: impossible to recycle, %p is already referenced",
1006 if ((vp->v_iflag & VI_DOOMED) == 0) {
1007 atomic_add_long(&recycles_count, 1);
1010 VOP_UNLOCK(vp, LK_INTERLOCK);
1011 vn_finished_write(vnmp);
1016 * Wait for available vnodes.
1019 getnewvnode_wait(int suspended)
1022 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1023 if (numvnodes > desiredvnodes) {
1026 * File system is beeing suspended, we cannot risk a
1027 * deadlock here, so allocate new vnode anyway.
1029 if (freevnodes > wantfreevnodes)
1030 vnlru_free(freevnodes - wantfreevnodes);
1033 if (vnlruproc_sig == 0) {
1034 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1037 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1040 return (numvnodes > desiredvnodes ? ENFILE : 0);
1044 getnewvnode_reserve(u_int count)
1049 /* First try to be quick and racy. */
1050 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1051 td->td_vp_reserv += count;
1054 atomic_subtract_long(&numvnodes, count);
1056 mtx_lock(&vnode_free_list_mtx);
1058 if (getnewvnode_wait(0) == 0) {
1061 atomic_add_long(&numvnodes, 1);
1064 mtx_unlock(&vnode_free_list_mtx);
1068 getnewvnode_drop_reserve(void)
1073 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1074 td->td_vp_reserv = 0;
1078 * Return the next vnode from the free list.
1081 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1089 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1092 if (td->td_vp_reserv > 0) {
1093 td->td_vp_reserv -= 1;
1096 mtx_lock(&vnode_free_list_mtx);
1098 * Lend our context to reclaim vnodes if they've exceeded the max.
1100 if (freevnodes > wantfreevnodes)
1102 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1104 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1106 mtx_unlock(&vnode_free_list_mtx);
1110 atomic_add_long(&numvnodes, 1);
1111 mtx_unlock(&vnode_free_list_mtx);
1113 atomic_add_long(&vnodes_created, 1);
1114 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1118 vp->v_vnlock = &vp->v_lock;
1119 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1121 * By default, don't allow shared locks unless filesystems
1124 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE | LK_IS_VNODE);
1126 * Initialize bufobj.
1129 bo->__bo_vnode = vp;
1130 rw_init(BO_LOCKPTR(bo), "bufobj interlock");
1131 bo->bo_ops = &buf_ops_bio;
1132 bo->bo_private = vp;
1133 TAILQ_INIT(&bo->bo_clean.bv_hd);
1134 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1136 * Initialize namecache.
1138 LIST_INIT(&vp->v_cache_src);
1139 TAILQ_INIT(&vp->v_cache_dst);
1141 * Finalize various vnode identity bits.
1146 v_init_counters(vp);
1150 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1151 mac_vnode_associate_singlelabel(mp, vp);
1152 else if (mp == NULL && vops != &dead_vnodeops)
1153 printf("NULL mp in getnewvnode()\n");
1156 bo->bo_bsize = mp->mnt_stat.f_iosize;
1157 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1158 vp->v_vflag |= VV_NOKNOTE;
1160 rangelock_init(&vp->v_rl);
1163 * For the filesystems which do not use vfs_hash_insert(),
1164 * still initialize v_hash to have vfs_hash_index() useful.
1165 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1168 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1175 * Delete from old mount point vnode list, if on one.
1178 delmntque(struct vnode *vp)
1188 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1189 ("Active vnode list size %d > Vnode list size %d",
1190 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1191 active = vp->v_iflag & VI_ACTIVE;
1192 vp->v_iflag &= ~VI_ACTIVE;
1194 mtx_lock(&vnode_free_list_mtx);
1195 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1196 mp->mnt_activevnodelistsize--;
1197 mtx_unlock(&vnode_free_list_mtx);
1201 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1202 ("bad mount point vnode list size"));
1203 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1204 mp->mnt_nvnodelistsize--;
1210 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1214 vp->v_op = &dead_vnodeops;
1220 * Insert into list of vnodes for the new mount point, if available.
1223 insmntque1(struct vnode *vp, struct mount *mp,
1224 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1227 KASSERT(vp->v_mount == NULL,
1228 ("insmntque: vnode already on per mount vnode list"));
1229 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1230 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1233 * We acquire the vnode interlock early to ensure that the
1234 * vnode cannot be recycled by another process releasing a
1235 * holdcnt on it before we get it on both the vnode list
1236 * and the active vnode list. The mount mutex protects only
1237 * manipulation of the vnode list and the vnode freelist
1238 * mutex protects only manipulation of the active vnode list.
1239 * Hence the need to hold the vnode interlock throughout.
1243 if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1244 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1245 mp->mnt_nvnodelistsize == 0)) &&
1246 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1255 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1256 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1257 ("neg mount point vnode list size"));
1258 mp->mnt_nvnodelistsize++;
1259 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1260 ("Activating already active vnode"));
1261 vp->v_iflag |= VI_ACTIVE;
1262 mtx_lock(&vnode_free_list_mtx);
1263 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1264 mp->mnt_activevnodelistsize++;
1265 mtx_unlock(&vnode_free_list_mtx);
1272 insmntque(struct vnode *vp, struct mount *mp)
1275 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1279 * Flush out and invalidate all buffers associated with a bufobj
1280 * Called with the underlying object locked.
1283 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1288 if (flags & V_SAVE) {
1289 error = bufobj_wwait(bo, slpflag, slptimeo);
1294 if (bo->bo_dirty.bv_cnt > 0) {
1296 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1299 * XXX We could save a lock/unlock if this was only
1300 * enabled under INVARIANTS
1303 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1304 panic("vinvalbuf: dirty bufs");
1308 * If you alter this loop please notice that interlock is dropped and
1309 * reacquired in flushbuflist. Special care is needed to ensure that
1310 * no race conditions occur from this.
1313 error = flushbuflist(&bo->bo_clean,
1314 flags, bo, slpflag, slptimeo);
1315 if (error == 0 && !(flags & V_CLEANONLY))
1316 error = flushbuflist(&bo->bo_dirty,
1317 flags, bo, slpflag, slptimeo);
1318 if (error != 0 && error != EAGAIN) {
1322 } while (error != 0);
1325 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1326 * have write I/O in-progress but if there is a VM object then the
1327 * VM object can also have read-I/O in-progress.
1330 bufobj_wwait(bo, 0, 0);
1332 if (bo->bo_object != NULL) {
1333 VM_OBJECT_WLOCK(bo->bo_object);
1334 vm_object_pip_wait(bo->bo_object, "bovlbx");
1335 VM_OBJECT_WUNLOCK(bo->bo_object);
1338 } while (bo->bo_numoutput > 0);
1342 * Destroy the copy in the VM cache, too.
1344 if (bo->bo_object != NULL &&
1345 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1346 VM_OBJECT_WLOCK(bo->bo_object);
1347 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1348 OBJPR_CLEANONLY : 0);
1349 VM_OBJECT_WUNLOCK(bo->bo_object);
1354 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1355 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1356 panic("vinvalbuf: flush failed");
1363 * Flush out and invalidate all buffers associated with a vnode.
1364 * Called with the underlying object locked.
1367 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1370 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1371 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1372 if (vp->v_object != NULL && vp->v_object->handle != vp)
1374 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1378 * Flush out buffers on the specified list.
1382 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1385 struct buf *bp, *nbp;
1390 ASSERT_BO_WLOCKED(bo);
1393 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1394 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1395 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1401 lblkno = nbp->b_lblkno;
1402 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1405 error = BUF_TIMELOCK(bp,
1406 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1407 "flushbuf", slpflag, slptimeo);
1410 return (error != ENOLCK ? error : EAGAIN);
1412 KASSERT(bp->b_bufobj == bo,
1413 ("bp %p wrong b_bufobj %p should be %p",
1414 bp, bp->b_bufobj, bo));
1415 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1421 * XXX Since there are no node locks for NFS, I
1422 * believe there is a slight chance that a delayed
1423 * write will occur while sleeping just above, so
1426 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1429 bp->b_flags |= B_ASYNC;
1432 return (EAGAIN); /* XXX: why not loop ? */
1435 bp->b_flags |= (B_INVAL | B_RELBUF);
1436 bp->b_flags &= ~B_ASYNC;
1440 (nbp->b_bufobj != bo ||
1441 nbp->b_lblkno != lblkno ||
1442 (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1443 break; /* nbp invalid */
1449 * Truncate a file's buffer and pages to a specified length. This
1450 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1454 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
1456 struct buf *bp, *nbp;
1461 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1462 vp, cred, blksize, (uintmax_t)length);
1465 * Round up to the *next* lbn.
1467 trunclbn = (length + blksize - 1) / blksize;
1469 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1476 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1477 if (bp->b_lblkno < trunclbn)
1480 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1481 BO_LOCKPTR(bo)) == ENOLCK)
1485 bp->b_flags |= (B_INVAL | B_RELBUF);
1486 bp->b_flags &= ~B_ASYNC;
1492 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1493 (nbp->b_vp != vp) ||
1494 (nbp->b_flags & B_DELWRI))) {
1500 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1501 if (bp->b_lblkno < trunclbn)
1504 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1505 BO_LOCKPTR(bo)) == ENOLCK)
1508 bp->b_flags |= (B_INVAL | B_RELBUF);
1509 bp->b_flags &= ~B_ASYNC;
1515 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1516 (nbp->b_vp != vp) ||
1517 (nbp->b_flags & B_DELWRI) == 0)) {
1526 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1527 if (bp->b_lblkno > 0)
1530 * Since we hold the vnode lock this should only
1531 * fail if we're racing with the buf daemon.
1534 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1535 BO_LOCKPTR(bo)) == ENOLCK) {
1538 VNASSERT((bp->b_flags & B_DELWRI), vp,
1539 ("buf(%p) on dirty queue without DELWRI", bp));
1548 bufobj_wwait(bo, 0, 0);
1550 vnode_pager_setsize(vp, length);
1556 buf_vlist_remove(struct buf *bp)
1560 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1561 ASSERT_BO_WLOCKED(bp->b_bufobj);
1562 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1563 (BX_VNDIRTY|BX_VNCLEAN),
1564 ("buf_vlist_remove: Buf %p is on two lists", bp));
1565 if (bp->b_xflags & BX_VNDIRTY)
1566 bv = &bp->b_bufobj->bo_dirty;
1568 bv = &bp->b_bufobj->bo_clean;
1569 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
1570 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1572 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1576 * Add the buffer to the sorted clean or dirty block list.
1578 * NOTE: xflags is passed as a constant, optimizing this inline function!
1581 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1587 ASSERT_BO_WLOCKED(bo);
1588 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
1589 ("dead bo %p", bo));
1590 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1591 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1592 bp->b_xflags |= xflags;
1593 if (xflags & BX_VNDIRTY)
1599 * Keep the list ordered. Optimize empty list insertion. Assume
1600 * we tend to grow at the tail so lookup_le should usually be cheaper
1603 if (bv->bv_cnt == 0 ||
1604 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
1605 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1606 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
1607 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
1609 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
1610 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
1612 panic("buf_vlist_add: Preallocated nodes insufficient.");
1617 * Look up a buffer using the buffer tries.
1620 gbincore(struct bufobj *bo, daddr_t lblkno)
1624 ASSERT_BO_LOCKED(bo);
1625 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
1628 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
1632 * Associate a buffer with a vnode.
1635 bgetvp(struct vnode *vp, struct buf *bp)
1640 ASSERT_BO_WLOCKED(bo);
1641 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1643 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1644 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1645 ("bgetvp: bp already attached! %p", bp));
1651 * Insert onto list for new vnode.
1653 buf_vlist_add(bp, bo, BX_VNCLEAN);
1657 * Disassociate a buffer from a vnode.
1660 brelvp(struct buf *bp)
1665 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1666 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1669 * Delete from old vnode list, if on one.
1671 vp = bp->b_vp; /* XXX */
1674 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1675 buf_vlist_remove(bp);
1677 panic("brelvp: Buffer %p not on queue.", bp);
1678 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1679 bo->bo_flag &= ~BO_ONWORKLST;
1680 mtx_lock(&sync_mtx);
1681 LIST_REMOVE(bo, bo_synclist);
1682 syncer_worklist_len--;
1683 mtx_unlock(&sync_mtx);
1686 bp->b_bufobj = NULL;
1692 * Add an item to the syncer work queue.
1695 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1699 ASSERT_BO_WLOCKED(bo);
1701 mtx_lock(&sync_mtx);
1702 if (bo->bo_flag & BO_ONWORKLST)
1703 LIST_REMOVE(bo, bo_synclist);
1705 bo->bo_flag |= BO_ONWORKLST;
1706 syncer_worklist_len++;
1709 if (delay > syncer_maxdelay - 2)
1710 delay = syncer_maxdelay - 2;
1711 slot = (syncer_delayno + delay) & syncer_mask;
1713 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
1714 mtx_unlock(&sync_mtx);
1718 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1722 mtx_lock(&sync_mtx);
1723 len = syncer_worklist_len - sync_vnode_count;
1724 mtx_unlock(&sync_mtx);
1725 error = SYSCTL_OUT(req, &len, sizeof(len));
1729 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1730 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1732 static struct proc *updateproc;
1733 static void sched_sync(void);
1734 static struct kproc_desc up_kp = {
1739 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1742 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1747 *bo = LIST_FIRST(slp);
1750 vp = (*bo)->__bo_vnode; /* XXX */
1751 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1754 * We use vhold in case the vnode does not
1755 * successfully sync. vhold prevents the vnode from
1756 * going away when we unlock the sync_mtx so that
1757 * we can acquire the vnode interlock.
1760 mtx_unlock(&sync_mtx);
1762 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1764 mtx_lock(&sync_mtx);
1765 return (*bo == LIST_FIRST(slp));
1767 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1768 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1770 vn_finished_write(mp);
1772 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1774 * Put us back on the worklist. The worklist
1775 * routine will remove us from our current
1776 * position and then add us back in at a later
1779 vn_syncer_add_to_worklist(*bo, syncdelay);
1783 mtx_lock(&sync_mtx);
1787 static int first_printf = 1;
1790 * System filesystem synchronizer daemon.
1795 struct synclist *next, *slp;
1798 struct thread *td = curthread;
1800 int net_worklist_len;
1801 int syncer_final_iter;
1805 syncer_final_iter = 0;
1806 syncer_state = SYNCER_RUNNING;
1807 starttime = time_uptime;
1808 td->td_pflags |= TDP_NORUNNINGBUF;
1810 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1813 mtx_lock(&sync_mtx);
1815 if (syncer_state == SYNCER_FINAL_DELAY &&
1816 syncer_final_iter == 0) {
1817 mtx_unlock(&sync_mtx);
1818 kproc_suspend_check(td->td_proc);
1819 mtx_lock(&sync_mtx);
1821 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1822 if (syncer_state != SYNCER_RUNNING &&
1823 starttime != time_uptime) {
1825 printf("\nSyncing disks, vnodes remaining...");
1828 printf("%d ", net_worklist_len);
1830 starttime = time_uptime;
1833 * Push files whose dirty time has expired. Be careful
1834 * of interrupt race on slp queue.
1836 * Skip over empty worklist slots when shutting down.
1839 slp = &syncer_workitem_pending[syncer_delayno];
1840 syncer_delayno += 1;
1841 if (syncer_delayno == syncer_maxdelay)
1843 next = &syncer_workitem_pending[syncer_delayno];
1845 * If the worklist has wrapped since the
1846 * it was emptied of all but syncer vnodes,
1847 * switch to the FINAL_DELAY state and run
1848 * for one more second.
1850 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1851 net_worklist_len == 0 &&
1852 last_work_seen == syncer_delayno) {
1853 syncer_state = SYNCER_FINAL_DELAY;
1854 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1856 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1857 syncer_worklist_len > 0);
1860 * Keep track of the last time there was anything
1861 * on the worklist other than syncer vnodes.
1862 * Return to the SHUTTING_DOWN state if any
1865 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1866 last_work_seen = syncer_delayno;
1867 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1868 syncer_state = SYNCER_SHUTTING_DOWN;
1869 while (!LIST_EMPTY(slp)) {
1870 error = sync_vnode(slp, &bo, td);
1872 LIST_REMOVE(bo, bo_synclist);
1873 LIST_INSERT_HEAD(next, bo, bo_synclist);
1877 if (first_printf == 0) {
1879 * Drop the sync mutex, because some watchdog
1880 * drivers need to sleep while patting
1882 mtx_unlock(&sync_mtx);
1883 wdog_kern_pat(WD_LASTVAL);
1884 mtx_lock(&sync_mtx);
1888 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1889 syncer_final_iter--;
1891 * The variable rushjob allows the kernel to speed up the
1892 * processing of the filesystem syncer process. A rushjob
1893 * value of N tells the filesystem syncer to process the next
1894 * N seconds worth of work on its queue ASAP. Currently rushjob
1895 * is used by the soft update code to speed up the filesystem
1896 * syncer process when the incore state is getting so far
1897 * ahead of the disk that the kernel memory pool is being
1898 * threatened with exhaustion.
1905 * Just sleep for a short period of time between
1906 * iterations when shutting down to allow some I/O
1909 * If it has taken us less than a second to process the
1910 * current work, then wait. Otherwise start right over
1911 * again. We can still lose time if any single round
1912 * takes more than two seconds, but it does not really
1913 * matter as we are just trying to generally pace the
1914 * filesystem activity.
1916 if (syncer_state != SYNCER_RUNNING ||
1917 time_uptime == starttime) {
1919 sched_prio(td, PPAUSE);
1922 if (syncer_state != SYNCER_RUNNING)
1923 cv_timedwait(&sync_wakeup, &sync_mtx,
1924 hz / SYNCER_SHUTDOWN_SPEEDUP);
1925 else if (time_uptime == starttime)
1926 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1931 * Request the syncer daemon to speed up its work.
1932 * We never push it to speed up more than half of its
1933 * normal turn time, otherwise it could take over the cpu.
1936 speedup_syncer(void)
1940 mtx_lock(&sync_mtx);
1941 if (rushjob < syncdelay / 2) {
1943 stat_rush_requests += 1;
1946 mtx_unlock(&sync_mtx);
1947 cv_broadcast(&sync_wakeup);
1952 * Tell the syncer to speed up its work and run though its work
1953 * list several times, then tell it to shut down.
1956 syncer_shutdown(void *arg, int howto)
1959 if (howto & RB_NOSYNC)
1961 mtx_lock(&sync_mtx);
1962 syncer_state = SYNCER_SHUTTING_DOWN;
1964 mtx_unlock(&sync_mtx);
1965 cv_broadcast(&sync_wakeup);
1966 kproc_shutdown(arg, howto);
1970 syncer_suspend(void)
1973 syncer_shutdown(updateproc, 0);
1980 mtx_lock(&sync_mtx);
1982 syncer_state = SYNCER_RUNNING;
1983 mtx_unlock(&sync_mtx);
1984 cv_broadcast(&sync_wakeup);
1985 kproc_resume(updateproc);
1989 * Reassign a buffer from one vnode to another.
1990 * Used to assign file specific control information
1991 * (indirect blocks) to the vnode to which they belong.
1994 reassignbuf(struct buf *bp)
2007 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2008 bp, bp->b_vp, bp->b_flags);
2010 * B_PAGING flagged buffers cannot be reassigned because their vp
2011 * is not fully linked in.
2013 if (bp->b_flags & B_PAGING)
2014 panic("cannot reassign paging buffer");
2017 * Delete from old vnode list, if on one.
2020 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2021 buf_vlist_remove(bp);
2023 panic("reassignbuf: Buffer %p not on queue.", bp);
2025 * If dirty, put on list of dirty buffers; otherwise insert onto list
2028 if (bp->b_flags & B_DELWRI) {
2029 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2030 switch (vp->v_type) {
2040 vn_syncer_add_to_worklist(bo, delay);
2042 buf_vlist_add(bp, bo, BX_VNDIRTY);
2044 buf_vlist_add(bp, bo, BX_VNCLEAN);
2046 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2047 mtx_lock(&sync_mtx);
2048 LIST_REMOVE(bo, bo_synclist);
2049 syncer_worklist_len--;
2050 mtx_unlock(&sync_mtx);
2051 bo->bo_flag &= ~BO_ONWORKLST;
2056 bp = TAILQ_FIRST(&bv->bv_hd);
2057 KASSERT(bp == NULL || bp->b_bufobj == bo,
2058 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2059 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2060 KASSERT(bp == NULL || bp->b_bufobj == bo,
2061 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2063 bp = TAILQ_FIRST(&bv->bv_hd);
2064 KASSERT(bp == NULL || bp->b_bufobj == bo,
2065 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2066 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2067 KASSERT(bp == NULL || bp->b_bufobj == bo,
2068 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2074 * A temporary hack until refcount_* APIs are sorted out.
2077 vfs_refcount_acquire_if_not_zero(volatile u_int *count)
2085 if (atomic_cmpset_int(count, old, old + 1))
2091 vfs_refcount_release_if_not_last(volatile u_int *count)
2099 if (atomic_cmpset_int(count, old, old - 1))
2105 v_init_counters(struct vnode *vp)
2108 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2109 vp, ("%s called for an initialized vnode", __FUNCTION__));
2110 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2112 refcount_init(&vp->v_holdcnt, 1);
2113 refcount_init(&vp->v_usecount, 1);
2117 * Increment the use and hold counts on the vnode, taking care to reference
2118 * the driver's usecount if this is a chardev. The _vhold() will remove
2119 * the vnode from the free list if it is presently free.
2122 v_incr_usecount(struct vnode *vp)
2125 ASSERT_VI_UNLOCKED(vp, __func__);
2126 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2128 if (vp->v_type == VCHR) {
2131 if (vp->v_iflag & VI_OWEINACT) {
2132 VNASSERT(vp->v_usecount == 0, vp,
2133 ("vnode with usecount and VI_OWEINACT set"));
2134 vp->v_iflag &= ~VI_OWEINACT;
2136 refcount_acquire(&vp->v_usecount);
2137 v_incr_devcount(vp);
2143 if (vfs_refcount_acquire_if_not_zero(&vp->v_usecount)) {
2144 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2145 ("vnode with usecount and VI_OWEINACT set"));
2148 if (vp->v_iflag & VI_OWEINACT)
2149 vp->v_iflag &= ~VI_OWEINACT;
2150 refcount_acquire(&vp->v_usecount);
2156 * Increment si_usecount of the associated device, if any.
2159 v_incr_devcount(struct vnode *vp)
2162 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2163 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2165 vp->v_rdev->si_usecount++;
2171 * Decrement si_usecount of the associated device, if any.
2174 v_decr_devcount(struct vnode *vp)
2177 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2178 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2180 vp->v_rdev->si_usecount--;
2186 * Grab a particular vnode from the free list, increment its
2187 * reference count and lock it. VI_DOOMED is set if the vnode
2188 * is being destroyed. Only callers who specify LK_RETRY will
2189 * see doomed vnodes. If inactive processing was delayed in
2190 * vput try to do it here.
2192 * Notes on lockless counter manipulation:
2193 * _vhold, vputx and other routines make various decisions based
2194 * on either holdcnt or usecount being 0. As long as either contuner
2195 * is not transitioning 0->1 nor 1->0, the manipulation can be done
2196 * with atomic operations. Otherwise the interlock is taken.
2199 vget(struct vnode *vp, int flags, struct thread *td)
2201 int error, oweinact;
2203 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2204 ("vget: invalid lock operation"));
2206 if ((flags & LK_INTERLOCK) != 0)
2207 ASSERT_VI_LOCKED(vp, __func__);
2209 ASSERT_VI_UNLOCKED(vp, __func__);
2210 if ((flags & LK_VNHELD) != 0)
2211 VNASSERT((vp->v_holdcnt > 0), vp,
2212 ("vget: LK_VNHELD passed but vnode not held"));
2214 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2216 if ((flags & LK_VNHELD) == 0)
2217 _vhold(vp, (flags & LK_INTERLOCK) != 0);
2219 if ((error = vn_lock(vp, flags)) != 0) {
2221 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2225 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2226 panic("vget: vn_lock failed to return ENOENT\n");
2228 * We don't guarantee that any particular close will
2229 * trigger inactive processing so just make a best effort
2230 * here at preventing a reference to a removed file. If
2231 * we don't succeed no harm is done.
2233 * Upgrade our holdcnt to a usecount.
2235 if (vp->v_type != VCHR &&
2236 vfs_refcount_acquire_if_not_zero(&vp->v_usecount)) {
2237 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2238 ("vnode with usecount and VI_OWEINACT set"));
2241 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2245 vp->v_iflag &= ~VI_OWEINACT;
2247 refcount_acquire(&vp->v_usecount);
2248 v_incr_devcount(vp);
2249 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2250 (flags & LK_NOWAIT) == 0)
2258 * Increase the reference count of a vnode.
2261 vref(struct vnode *vp)
2264 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2265 v_incr_usecount(vp);
2269 * Return reference count of a vnode.
2271 * The results of this call are only guaranteed when some mechanism is used to
2272 * stop other processes from gaining references to the vnode. This may be the
2273 * case if the caller holds the only reference. This is also useful when stale
2274 * data is acceptable as race conditions may be accounted for by some other
2278 vrefcnt(struct vnode *vp)
2281 return (vp->v_usecount);
2284 #define VPUTX_VRELE 1
2285 #define VPUTX_VPUT 2
2286 #define VPUTX_VUNREF 3
2289 * Decrement the use and hold counts for a vnode.
2291 * See an explanation near vget() as to why atomic operation is safe.
2294 vputx(struct vnode *vp, int func)
2298 KASSERT(vp != NULL, ("vputx: null vp"));
2299 if (func == VPUTX_VUNREF)
2300 ASSERT_VOP_LOCKED(vp, "vunref");
2301 else if (func == VPUTX_VPUT)
2302 ASSERT_VOP_LOCKED(vp, "vput");
2304 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2305 ASSERT_VI_UNLOCKED(vp, __func__);
2306 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2308 if (vp->v_type != VCHR &&
2309 vfs_refcount_release_if_not_last(&vp->v_usecount)) {
2310 if (func == VPUTX_VPUT)
2319 * We want to hold the vnode until the inactive finishes to
2320 * prevent vgone() races. We drop the use count here and the
2321 * hold count below when we're done.
2323 if (!refcount_release(&vp->v_usecount) ||
2324 (vp->v_iflag & VI_DOINGINACT)) {
2325 if (func == VPUTX_VPUT)
2327 v_decr_devcount(vp);
2332 v_decr_devcount(vp);
2336 if (vp->v_usecount != 0) {
2337 vprint("vputx: usecount not zero", vp);
2338 panic("vputx: usecount not zero");
2341 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2344 * We must call VOP_INACTIVE with the node locked. Mark
2345 * as VI_DOINGINACT to avoid recursion.
2347 vp->v_iflag |= VI_OWEINACT;
2350 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2354 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2355 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2361 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2362 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2367 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp,
2368 ("vnode with usecount and VI_OWEINACT set"));
2370 if (vp->v_iflag & VI_OWEINACT)
2371 vinactive(vp, curthread);
2372 if (func != VPUTX_VUNREF)
2379 * Vnode put/release.
2380 * If count drops to zero, call inactive routine and return to freelist.
2383 vrele(struct vnode *vp)
2386 vputx(vp, VPUTX_VRELE);
2390 * Release an already locked vnode. This give the same effects as
2391 * unlock+vrele(), but takes less time and avoids releasing and
2392 * re-aquiring the lock (as vrele() acquires the lock internally.)
2395 vput(struct vnode *vp)
2398 vputx(vp, VPUTX_VPUT);
2402 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2405 vunref(struct vnode *vp)
2408 vputx(vp, VPUTX_VUNREF);
2412 * Increase the hold count and activate if this is the first reference.
2415 _vhold(struct vnode *vp, bool locked)
2420 ASSERT_VI_LOCKED(vp, __func__);
2422 ASSERT_VI_UNLOCKED(vp, __func__);
2423 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2424 if (!locked && vfs_refcount_acquire_if_not_zero(&vp->v_holdcnt)) {
2425 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2426 ("_vhold: vnode with holdcnt is free"));
2432 if ((vp->v_iflag & VI_FREE) == 0) {
2433 refcount_acquire(&vp->v_holdcnt);
2438 VNASSERT(vp->v_holdcnt == 0, vp,
2439 ("%s: wrong hold count", __func__));
2440 VNASSERT(vp->v_op != NULL, vp,
2441 ("%s: vnode already reclaimed.", __func__));
2443 * Remove a vnode from the free list, mark it as in use,
2444 * and put it on the active list.
2446 mtx_lock(&vnode_free_list_mtx);
2447 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2449 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2450 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2451 ("Activating already active vnode"));
2452 vp->v_iflag |= VI_ACTIVE;
2454 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2455 mp->mnt_activevnodelistsize++;
2456 mtx_unlock(&vnode_free_list_mtx);
2457 refcount_acquire(&vp->v_holdcnt);
2463 * Drop the hold count of the vnode. If this is the last reference to
2464 * the vnode we place it on the free list unless it has been vgone'd
2465 * (marked VI_DOOMED) in which case we will free it.
2468 _vdrop(struct vnode *vp, bool locked)
2475 ASSERT_VI_LOCKED(vp, __func__);
2477 ASSERT_VI_UNLOCKED(vp, __func__);
2478 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2479 if ((int)vp->v_holdcnt <= 0)
2480 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2481 if (vfs_refcount_release_if_not_last(&vp->v_holdcnt)) {
2489 if (refcount_release(&vp->v_holdcnt) == 0) {
2493 if ((vp->v_iflag & VI_DOOMED) == 0) {
2495 * Mark a vnode as free: remove it from its active list
2496 * and put it up for recycling on the freelist.
2498 VNASSERT(vp->v_op != NULL, vp,
2499 ("vdropl: vnode already reclaimed."));
2500 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2501 ("vnode already free"));
2502 VNASSERT(vp->v_holdcnt == 0, vp,
2503 ("vdropl: freeing when we shouldn't"));
2504 active = vp->v_iflag & VI_ACTIVE;
2505 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2506 vp->v_iflag &= ~VI_ACTIVE;
2508 mtx_lock(&vnode_free_list_mtx);
2510 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2512 mp->mnt_activevnodelistsize--;
2514 if (vp->v_iflag & VI_AGE) {
2515 TAILQ_INSERT_HEAD(&vnode_free_list, vp,
2518 TAILQ_INSERT_TAIL(&vnode_free_list, vp,
2522 vp->v_iflag &= ~VI_AGE;
2523 vp->v_iflag |= VI_FREE;
2524 mtx_unlock(&vnode_free_list_mtx);
2526 atomic_add_long(&free_owe_inact, 1);
2532 * The vnode has been marked for destruction, so free it.
2534 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2535 atomic_subtract_long(&numvnodes, 1);
2537 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2538 ("cleaned vnode still on the free list."));
2539 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2540 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2541 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2542 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2543 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2544 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2545 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
2546 ("clean blk trie not empty"));
2547 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2548 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
2549 ("dirty blk trie not empty"));
2550 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2551 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2552 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2555 mac_vnode_destroy(vp);
2557 if (vp->v_pollinfo != NULL)
2558 destroy_vpollinfo(vp->v_pollinfo);
2560 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2563 rangelock_destroy(&vp->v_rl);
2564 lockdestroy(vp->v_vnlock);
2565 mtx_destroy(&vp->v_interlock);
2566 rw_destroy(BO_LOCKPTR(bo));
2567 uma_zfree(vnode_zone, vp);
2571 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2572 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2573 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2574 * failed lock upgrade.
2577 vinactive(struct vnode *vp, struct thread *td)
2579 struct vm_object *obj;
2581 ASSERT_VOP_ELOCKED(vp, "vinactive");
2582 ASSERT_VI_LOCKED(vp, "vinactive");
2583 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2584 ("vinactive: recursed on VI_DOINGINACT"));
2585 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2586 vp->v_iflag |= VI_DOINGINACT;
2587 vp->v_iflag &= ~VI_OWEINACT;
2590 * Before moving off the active list, we must be sure that any
2591 * modified pages are on the vnode's dirty list since these will
2592 * no longer be checked once the vnode is on the inactive list.
2593 * Because the vnode vm object keeps a hold reference on the vnode
2594 * if there is at least one resident non-cached page, the vnode
2595 * cannot leave the active list without the page cleanup done.
2598 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
2599 VM_OBJECT_WLOCK(obj);
2600 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
2601 VM_OBJECT_WUNLOCK(obj);
2603 VOP_INACTIVE(vp, td);
2605 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2606 ("vinactive: lost VI_DOINGINACT"));
2607 vp->v_iflag &= ~VI_DOINGINACT;
2611 * Remove any vnodes in the vnode table belonging to mount point mp.
2613 * If FORCECLOSE is not specified, there should not be any active ones,
2614 * return error if any are found (nb: this is a user error, not a
2615 * system error). If FORCECLOSE is specified, detach any active vnodes
2618 * If WRITECLOSE is set, only flush out regular file vnodes open for
2621 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2623 * `rootrefs' specifies the base reference count for the root vnode
2624 * of this filesystem. The root vnode is considered busy if its
2625 * v_usecount exceeds this value. On a successful return, vflush(, td)
2626 * will call vrele() on the root vnode exactly rootrefs times.
2627 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2631 static int busyprt = 0; /* print out busy vnodes */
2632 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2636 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2638 struct vnode *vp, *mvp, *rootvp = NULL;
2640 int busy = 0, error;
2642 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2645 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2646 ("vflush: bad args"));
2648 * Get the filesystem root vnode. We can vput() it
2649 * immediately, since with rootrefs > 0, it won't go away.
2651 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2652 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2659 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2661 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2664 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2668 * Skip over a vnodes marked VV_SYSTEM.
2670 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2676 * If WRITECLOSE is set, flush out unlinked but still open
2677 * files (even if open only for reading) and regular file
2678 * vnodes open for writing.
2680 if (flags & WRITECLOSE) {
2681 if (vp->v_object != NULL) {
2682 VM_OBJECT_WLOCK(vp->v_object);
2683 vm_object_page_clean(vp->v_object, 0, 0, 0);
2684 VM_OBJECT_WUNLOCK(vp->v_object);
2686 error = VOP_FSYNC(vp, MNT_WAIT, td);
2690 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2693 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2696 if ((vp->v_type == VNON ||
2697 (error == 0 && vattr.va_nlink > 0)) &&
2698 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2706 * With v_usecount == 0, all we need to do is clear out the
2707 * vnode data structures and we are done.
2709 * If FORCECLOSE is set, forcibly close the vnode.
2711 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2717 vprint("vflush: busy vnode", vp);
2723 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2725 * If just the root vnode is busy, and if its refcount
2726 * is equal to `rootrefs', then go ahead and kill it.
2729 KASSERT(busy > 0, ("vflush: not busy"));
2730 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2731 ("vflush: usecount %d < rootrefs %d",
2732 rootvp->v_usecount, rootrefs));
2733 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2734 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2736 VOP_UNLOCK(rootvp, 0);
2742 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2746 for (; rootrefs > 0; rootrefs--)
2752 * Recycle an unused vnode to the front of the free list.
2755 vrecycle(struct vnode *vp)
2759 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2760 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2763 if (vp->v_usecount == 0) {
2772 * Eliminate all activity associated with a vnode
2773 * in preparation for reuse.
2776 vgone(struct vnode *vp)
2784 notify_lowervp_vfs_dummy(struct mount *mp __unused,
2785 struct vnode *lowervp __unused)
2790 * Notify upper mounts about reclaimed or unlinked vnode.
2793 vfs_notify_upper(struct vnode *vp, int event)
2795 static struct vfsops vgonel_vfsops = {
2796 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
2797 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
2799 struct mount *mp, *ump, *mmp;
2806 if (TAILQ_EMPTY(&mp->mnt_uppers))
2809 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
2810 mmp->mnt_op = &vgonel_vfsops;
2811 mmp->mnt_kern_flag |= MNTK_MARKER;
2813 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
2814 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
2815 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
2816 ump = TAILQ_NEXT(ump, mnt_upper_link);
2819 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
2822 case VFS_NOTIFY_UPPER_RECLAIM:
2823 VFS_RECLAIM_LOWERVP(ump, vp);
2825 case VFS_NOTIFY_UPPER_UNLINK:
2826 VFS_UNLINK_LOWERVP(ump, vp);
2829 KASSERT(0, ("invalid event %d", event));
2833 ump = TAILQ_NEXT(mmp, mnt_upper_link);
2834 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
2837 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
2838 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
2839 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
2840 wakeup(&mp->mnt_uppers);
2847 * vgone, with the vp interlock held.
2850 vgonel(struct vnode *vp)
2857 ASSERT_VOP_ELOCKED(vp, "vgonel");
2858 ASSERT_VI_LOCKED(vp, "vgonel");
2859 VNASSERT(vp->v_holdcnt, vp,
2860 ("vgonel: vp %p has no reference.", vp));
2861 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2865 * Don't vgonel if we're already doomed.
2867 if (vp->v_iflag & VI_DOOMED)
2869 vp->v_iflag |= VI_DOOMED;
2872 * Check to see if the vnode is in use. If so, we have to call
2873 * VOP_CLOSE() and VOP_INACTIVE().
2875 active = vp->v_usecount;
2876 oweinact = (vp->v_iflag & VI_OWEINACT);
2878 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
2881 * If purging an active vnode, it must be closed and
2882 * deactivated before being reclaimed.
2885 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2886 if (oweinact || active) {
2888 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2892 if (vp->v_type == VSOCK)
2893 vfs_unp_reclaim(vp);
2896 * Clean out any buffers associated with the vnode.
2897 * If the flush fails, just toss the buffers.
2900 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2901 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2902 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
2903 while (vinvalbuf(vp, 0, 0, 0) != 0)
2907 BO_LOCK(&vp->v_bufobj);
2908 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
2909 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
2910 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
2911 vp->v_bufobj.bo_clean.bv_cnt == 0,
2912 ("vp %p bufobj not invalidated", vp));
2913 vp->v_bufobj.bo_flag |= BO_DEAD;
2914 BO_UNLOCK(&vp->v_bufobj);
2917 * Reclaim the vnode.
2919 if (VOP_RECLAIM(vp, td))
2920 panic("vgone: cannot reclaim");
2922 vn_finished_secondary_write(mp);
2923 VNASSERT(vp->v_object == NULL, vp,
2924 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2926 * Clear the advisory locks and wake up waiting threads.
2928 (void)VOP_ADVLOCKPURGE(vp);
2930 * Delete from old mount point vnode list.
2935 * Done with purge, reset to the standard lock and invalidate
2939 vp->v_vnlock = &vp->v_lock;
2940 vp->v_op = &dead_vnodeops;
2946 * Calculate the total number of references to a special device.
2949 vcount(struct vnode *vp)
2954 count = vp->v_rdev->si_usecount;
2960 * Same as above, but using the struct cdev *as argument
2963 count_dev(struct cdev *dev)
2968 count = dev->si_usecount;
2974 * Print out a description of a vnode.
2976 static char *typename[] =
2977 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2981 vn_printf(struct vnode *vp, const char *fmt, ...)
2984 char buf[256], buf2[16];
2990 printf("%p: ", (void *)vp);
2991 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2992 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2993 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2996 if (vp->v_vflag & VV_ROOT)
2997 strlcat(buf, "|VV_ROOT", sizeof(buf));
2998 if (vp->v_vflag & VV_ISTTY)
2999 strlcat(buf, "|VV_ISTTY", sizeof(buf));
3000 if (vp->v_vflag & VV_NOSYNC)
3001 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
3002 if (vp->v_vflag & VV_ETERNALDEV)
3003 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
3004 if (vp->v_vflag & VV_CACHEDLABEL)
3005 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
3006 if (vp->v_vflag & VV_TEXT)
3007 strlcat(buf, "|VV_TEXT", sizeof(buf));
3008 if (vp->v_vflag & VV_COPYONWRITE)
3009 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
3010 if (vp->v_vflag & VV_SYSTEM)
3011 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
3012 if (vp->v_vflag & VV_PROCDEP)
3013 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
3014 if (vp->v_vflag & VV_NOKNOTE)
3015 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
3016 if (vp->v_vflag & VV_DELETED)
3017 strlcat(buf, "|VV_DELETED", sizeof(buf));
3018 if (vp->v_vflag & VV_MD)
3019 strlcat(buf, "|VV_MD", sizeof(buf));
3020 if (vp->v_vflag & VV_FORCEINSMQ)
3021 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
3022 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
3023 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
3024 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
3026 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
3027 strlcat(buf, buf2, sizeof(buf));
3029 if (vp->v_iflag & VI_MOUNT)
3030 strlcat(buf, "|VI_MOUNT", sizeof(buf));
3031 if (vp->v_iflag & VI_AGE)
3032 strlcat(buf, "|VI_AGE", sizeof(buf));
3033 if (vp->v_iflag & VI_DOOMED)
3034 strlcat(buf, "|VI_DOOMED", sizeof(buf));
3035 if (vp->v_iflag & VI_FREE)
3036 strlcat(buf, "|VI_FREE", sizeof(buf));
3037 if (vp->v_iflag & VI_ACTIVE)
3038 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3039 if (vp->v_iflag & VI_DOINGINACT)
3040 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3041 if (vp->v_iflag & VI_OWEINACT)
3042 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3043 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
3044 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
3046 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3047 strlcat(buf, buf2, sizeof(buf));
3049 printf(" flags (%s)\n", buf + 1);
3050 if (mtx_owned(VI_MTX(vp)))
3051 printf(" VI_LOCKed");
3052 if (vp->v_object != NULL)
3053 printf(" v_object %p ref %d pages %d "
3054 "cleanbuf %d dirtybuf %d\n",
3055 vp->v_object, vp->v_object->ref_count,
3056 vp->v_object->resident_page_count,
3057 vp->v_bufobj.bo_dirty.bv_cnt,
3058 vp->v_bufobj.bo_clean.bv_cnt);
3060 lockmgr_printinfo(vp->v_vnlock);
3061 if (vp->v_data != NULL)
3067 * List all of the locked vnodes in the system.
3068 * Called when debugging the kernel.
3070 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3076 * Note: because this is DDB, we can't obey the locking semantics
3077 * for these structures, which means we could catch an inconsistent
3078 * state and dereference a nasty pointer. Not much to be done
3081 db_printf("Locked vnodes\n");
3082 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3083 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3084 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3091 * Show details about the given vnode.
3093 DB_SHOW_COMMAND(vnode, db_show_vnode)
3099 vp = (struct vnode *)addr;
3100 vn_printf(vp, "vnode ");
3104 * Show details about the given mount point.
3106 DB_SHOW_COMMAND(mount, db_show_mount)
3117 /* No address given, print short info about all mount points. */
3118 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3119 db_printf("%p %s on %s (%s)\n", mp,
3120 mp->mnt_stat.f_mntfromname,
3121 mp->mnt_stat.f_mntonname,
3122 mp->mnt_stat.f_fstypename);
3126 db_printf("\nMore info: show mount <addr>\n");
3130 mp = (struct mount *)addr;
3131 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3132 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3135 mflags = mp->mnt_flag;
3136 #define MNT_FLAG(flag) do { \
3137 if (mflags & (flag)) { \
3138 if (buf[0] != '\0') \
3139 strlcat(buf, ", ", sizeof(buf)); \
3140 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3141 mflags &= ~(flag); \
3144 MNT_FLAG(MNT_RDONLY);
3145 MNT_FLAG(MNT_SYNCHRONOUS);
3146 MNT_FLAG(MNT_NOEXEC);
3147 MNT_FLAG(MNT_NOSUID);
3148 MNT_FLAG(MNT_NFS4ACLS);
3149 MNT_FLAG(MNT_UNION);
3150 MNT_FLAG(MNT_ASYNC);
3151 MNT_FLAG(MNT_SUIDDIR);
3152 MNT_FLAG(MNT_SOFTDEP);
3153 MNT_FLAG(MNT_NOSYMFOLLOW);
3154 MNT_FLAG(MNT_GJOURNAL);
3155 MNT_FLAG(MNT_MULTILABEL);
3157 MNT_FLAG(MNT_NOATIME);
3158 MNT_FLAG(MNT_NOCLUSTERR);
3159 MNT_FLAG(MNT_NOCLUSTERW);
3161 MNT_FLAG(MNT_EXRDONLY);
3162 MNT_FLAG(MNT_EXPORTED);
3163 MNT_FLAG(MNT_DEFEXPORTED);
3164 MNT_FLAG(MNT_EXPORTANON);
3165 MNT_FLAG(MNT_EXKERB);
3166 MNT_FLAG(MNT_EXPUBLIC);
3167 MNT_FLAG(MNT_LOCAL);
3168 MNT_FLAG(MNT_QUOTA);
3169 MNT_FLAG(MNT_ROOTFS);
3171 MNT_FLAG(MNT_IGNORE);
3172 MNT_FLAG(MNT_UPDATE);
3173 MNT_FLAG(MNT_DELEXPORT);
3174 MNT_FLAG(MNT_RELOAD);
3175 MNT_FLAG(MNT_FORCE);
3176 MNT_FLAG(MNT_SNAPSHOT);
3177 MNT_FLAG(MNT_BYFSID);
3181 strlcat(buf, ", ", sizeof(buf));
3182 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3183 "0x%016jx", mflags);
3185 db_printf(" mnt_flag = %s\n", buf);
3188 flags = mp->mnt_kern_flag;
3189 #define MNT_KERN_FLAG(flag) do { \
3190 if (flags & (flag)) { \
3191 if (buf[0] != '\0') \
3192 strlcat(buf, ", ", sizeof(buf)); \
3193 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3197 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3198 MNT_KERN_FLAG(MNTK_ASYNC);
3199 MNT_KERN_FLAG(MNTK_SOFTDEP);
3200 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3201 MNT_KERN_FLAG(MNTK_DRAINING);
3202 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3203 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3204 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3205 MNT_KERN_FLAG(MNTK_NO_IOPF);
3206 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3207 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3208 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3209 MNT_KERN_FLAG(MNTK_MARKER);
3210 MNT_KERN_FLAG(MNTK_USES_BCACHE);
3211 MNT_KERN_FLAG(MNTK_NOASYNC);
3212 MNT_KERN_FLAG(MNTK_UNMOUNT);
3213 MNT_KERN_FLAG(MNTK_MWAIT);
3214 MNT_KERN_FLAG(MNTK_SUSPEND);
3215 MNT_KERN_FLAG(MNTK_SUSPEND2);
3216 MNT_KERN_FLAG(MNTK_SUSPENDED);
3217 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3218 MNT_KERN_FLAG(MNTK_NOKNOTE);
3219 #undef MNT_KERN_FLAG
3222 strlcat(buf, ", ", sizeof(buf));
3223 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3226 db_printf(" mnt_kern_flag = %s\n", buf);
3228 db_printf(" mnt_opt = ");
3229 opt = TAILQ_FIRST(mp->mnt_opt);
3231 db_printf("%s", opt->name);
3232 opt = TAILQ_NEXT(opt, link);
3233 while (opt != NULL) {
3234 db_printf(", %s", opt->name);
3235 opt = TAILQ_NEXT(opt, link);
3241 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3242 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3243 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3244 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3245 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3246 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3247 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3248 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3249 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3250 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3251 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3252 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3254 db_printf(" mnt_cred = { uid=%u ruid=%u",
3255 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3256 if (jailed(mp->mnt_cred))
3257 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3259 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3260 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3261 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3262 db_printf(" mnt_activevnodelistsize = %d\n",
3263 mp->mnt_activevnodelistsize);
3264 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3265 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3266 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3267 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3268 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3269 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3270 db_printf(" mnt_secondary_accwrites = %d\n",
3271 mp->mnt_secondary_accwrites);
3272 db_printf(" mnt_gjprovider = %s\n",
3273 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3275 db_printf("\n\nList of active vnodes\n");
3276 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3277 if (vp->v_type != VMARKER) {
3278 vn_printf(vp, "vnode ");
3283 db_printf("\n\nList of inactive vnodes\n");
3284 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3285 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3286 vn_printf(vp, "vnode ");
3295 * Fill in a struct xvfsconf based on a struct vfsconf.
3298 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3300 struct xvfsconf xvfsp;
3302 bzero(&xvfsp, sizeof(xvfsp));
3303 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3304 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3305 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3306 xvfsp.vfc_flags = vfsp->vfc_flags;
3308 * These are unused in userland, we keep them
3309 * to not break binary compatibility.
3311 xvfsp.vfc_vfsops = NULL;
3312 xvfsp.vfc_next = NULL;
3313 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3316 #ifdef COMPAT_FREEBSD32
3318 uint32_t vfc_vfsops;
3319 char vfc_name[MFSNAMELEN];
3320 int32_t vfc_typenum;
3321 int32_t vfc_refcount;
3327 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3329 struct xvfsconf32 xvfsp;
3331 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3332 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3333 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3334 xvfsp.vfc_flags = vfsp->vfc_flags;
3335 xvfsp.vfc_vfsops = 0;
3337 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3342 * Top level filesystem related information gathering.
3345 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3347 struct vfsconf *vfsp;
3352 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3353 #ifdef COMPAT_FREEBSD32
3354 if (req->flags & SCTL_MASK32)
3355 error = vfsconf2x32(req, vfsp);
3358 error = vfsconf2x(req, vfsp);
3366 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3367 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3368 "S,xvfsconf", "List of all configured filesystems");
3370 #ifndef BURN_BRIDGES
3371 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3374 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3376 int *name = (int *)arg1 - 1; /* XXX */
3377 u_int namelen = arg2 + 1; /* XXX */
3378 struct vfsconf *vfsp;
3380 log(LOG_WARNING, "userland calling deprecated sysctl, "
3381 "please rebuild world\n");
3383 #if 1 || defined(COMPAT_PRELITE2)
3384 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3386 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3390 case VFS_MAXTYPENUM:
3393 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3396 return (ENOTDIR); /* overloaded */
3398 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3399 if (vfsp->vfc_typenum == name[2])
3404 return (EOPNOTSUPP);
3405 #ifdef COMPAT_FREEBSD32
3406 if (req->flags & SCTL_MASK32)
3407 return (vfsconf2x32(req, vfsp));
3410 return (vfsconf2x(req, vfsp));
3412 return (EOPNOTSUPP);
3415 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
3416 CTLFLAG_MPSAFE, vfs_sysctl,
3417 "Generic filesystem");
3419 #if 1 || defined(COMPAT_PRELITE2)
3422 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3425 struct vfsconf *vfsp;
3426 struct ovfsconf ovfs;
3429 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3430 bzero(&ovfs, sizeof(ovfs));
3431 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3432 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3433 ovfs.vfc_index = vfsp->vfc_typenum;
3434 ovfs.vfc_refcount = vfsp->vfc_refcount;
3435 ovfs.vfc_flags = vfsp->vfc_flags;
3436 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3446 #endif /* 1 || COMPAT_PRELITE2 */
3447 #endif /* !BURN_BRIDGES */
3449 #define KINFO_VNODESLOP 10
3452 * Dump vnode list (via sysctl).
3456 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3464 * Stale numvnodes access is not fatal here.
3467 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3469 /* Make an estimate */
3470 return (SYSCTL_OUT(req, 0, len));
3472 error = sysctl_wire_old_buffer(req, 0);
3475 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3477 mtx_lock(&mountlist_mtx);
3478 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3479 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3482 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3486 xvn[n].xv_size = sizeof *xvn;
3487 xvn[n].xv_vnode = vp;
3488 xvn[n].xv_id = 0; /* XXX compat */
3489 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3491 XV_COPY(writecount);
3497 xvn[n].xv_flag = vp->v_vflag;
3499 switch (vp->v_type) {
3506 if (vp->v_rdev == NULL) {
3510 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3513 xvn[n].xv_socket = vp->v_socket;
3516 xvn[n].xv_fifo = vp->v_fifoinfo;
3521 /* shouldn't happen? */
3529 mtx_lock(&mountlist_mtx);
3534 mtx_unlock(&mountlist_mtx);
3536 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3541 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
3542 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
3547 unmount_or_warn(struct mount *mp)
3551 error = dounmount(mp, MNT_FORCE, curthread);
3553 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
3557 printf("%d)\n", error);
3562 * Unmount all filesystems. The list is traversed in reverse order
3563 * of mounting to avoid dependencies.
3566 vfs_unmountall(void)
3568 struct mount *mp, *tmp;
3570 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3573 * Since this only runs when rebooting, it is not interlocked.
3575 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
3579 * Forcibly unmounting "/dev" before "/" would prevent clean
3580 * unmount of the latter.
3582 if (mp == rootdevmp)
3585 unmount_or_warn(mp);
3588 if (rootdevmp != NULL)
3589 unmount_or_warn(rootdevmp);
3593 * perform msync on all vnodes under a mount point
3594 * the mount point must be locked.
3597 vfs_msync(struct mount *mp, int flags)
3599 struct vnode *vp, *mvp;
3600 struct vm_object *obj;
3602 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3603 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
3605 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3606 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3608 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3610 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3617 VM_OBJECT_WLOCK(obj);
3618 vm_object_page_clean(obj, 0, 0,
3620 OBJPC_SYNC : OBJPC_NOSYNC);
3621 VM_OBJECT_WUNLOCK(obj);
3631 destroy_vpollinfo_free(struct vpollinfo *vi)
3634 knlist_destroy(&vi->vpi_selinfo.si_note);
3635 mtx_destroy(&vi->vpi_lock);
3636 uma_zfree(vnodepoll_zone, vi);
3640 destroy_vpollinfo(struct vpollinfo *vi)
3643 knlist_clear(&vi->vpi_selinfo.si_note, 1);
3644 seldrain(&vi->vpi_selinfo);
3645 destroy_vpollinfo_free(vi);
3649 * Initalize per-vnode helper structure to hold poll-related state.
3652 v_addpollinfo(struct vnode *vp)
3654 struct vpollinfo *vi;
3656 if (vp->v_pollinfo != NULL)
3658 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
3659 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3660 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3661 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3663 if (vp->v_pollinfo != NULL) {
3665 destroy_vpollinfo_free(vi);
3668 vp->v_pollinfo = vi;
3673 * Record a process's interest in events which might happen to
3674 * a vnode. Because poll uses the historic select-style interface
3675 * internally, this routine serves as both the ``check for any
3676 * pending events'' and the ``record my interest in future events''
3677 * functions. (These are done together, while the lock is held,
3678 * to avoid race conditions.)
3681 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3685 mtx_lock(&vp->v_pollinfo->vpi_lock);
3686 if (vp->v_pollinfo->vpi_revents & events) {
3688 * This leaves events we are not interested
3689 * in available for the other process which
3690 * which presumably had requested them
3691 * (otherwise they would never have been
3694 events &= vp->v_pollinfo->vpi_revents;
3695 vp->v_pollinfo->vpi_revents &= ~events;
3697 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3700 vp->v_pollinfo->vpi_events |= events;
3701 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3702 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3707 * Routine to create and manage a filesystem syncer vnode.
3709 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3710 static int sync_fsync(struct vop_fsync_args *);
3711 static int sync_inactive(struct vop_inactive_args *);
3712 static int sync_reclaim(struct vop_reclaim_args *);
3714 static struct vop_vector sync_vnodeops = {
3715 .vop_bypass = VOP_EOPNOTSUPP,
3716 .vop_close = sync_close, /* close */
3717 .vop_fsync = sync_fsync, /* fsync */
3718 .vop_inactive = sync_inactive, /* inactive */
3719 .vop_reclaim = sync_reclaim, /* reclaim */
3720 .vop_lock1 = vop_stdlock, /* lock */
3721 .vop_unlock = vop_stdunlock, /* unlock */
3722 .vop_islocked = vop_stdislocked, /* islocked */
3726 * Create a new filesystem syncer vnode for the specified mount point.
3729 vfs_allocate_syncvnode(struct mount *mp)
3733 static long start, incr, next;
3736 /* Allocate a new vnode */
3737 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3739 panic("vfs_allocate_syncvnode: getnewvnode() failed");
3741 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3742 vp->v_vflag |= VV_FORCEINSMQ;
3743 error = insmntque(vp, mp);
3745 panic("vfs_allocate_syncvnode: insmntque() failed");
3746 vp->v_vflag &= ~VV_FORCEINSMQ;
3749 * Place the vnode onto the syncer worklist. We attempt to
3750 * scatter them about on the list so that they will go off
3751 * at evenly distributed times even if all the filesystems
3752 * are mounted at once.
3755 if (next == 0 || next > syncer_maxdelay) {
3759 start = syncer_maxdelay / 2;
3760 incr = syncer_maxdelay;
3766 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3767 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3768 mtx_lock(&sync_mtx);
3770 if (mp->mnt_syncer == NULL) {
3771 mp->mnt_syncer = vp;
3774 mtx_unlock(&sync_mtx);
3777 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3784 vfs_deallocate_syncvnode(struct mount *mp)
3788 mtx_lock(&sync_mtx);
3789 vp = mp->mnt_syncer;
3791 mp->mnt_syncer = NULL;
3792 mtx_unlock(&sync_mtx);
3798 * Do a lazy sync of the filesystem.
3801 sync_fsync(struct vop_fsync_args *ap)
3803 struct vnode *syncvp = ap->a_vp;
3804 struct mount *mp = syncvp->v_mount;
3809 * We only need to do something if this is a lazy evaluation.
3811 if (ap->a_waitfor != MNT_LAZY)
3815 * Move ourselves to the back of the sync list.
3817 bo = &syncvp->v_bufobj;
3819 vn_syncer_add_to_worklist(bo, syncdelay);
3823 * Walk the list of vnodes pushing all that are dirty and
3824 * not already on the sync list.
3826 if (vfs_busy(mp, MBF_NOWAIT) != 0)
3828 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3832 save = curthread_pflags_set(TDP_SYNCIO);
3833 vfs_msync(mp, MNT_NOWAIT);
3834 error = VFS_SYNC(mp, MNT_LAZY);
3835 curthread_pflags_restore(save);
3836 vn_finished_write(mp);
3842 * The syncer vnode is no referenced.
3845 sync_inactive(struct vop_inactive_args *ap)
3853 * The syncer vnode is no longer needed and is being decommissioned.
3855 * Modifications to the worklist must be protected by sync_mtx.
3858 sync_reclaim(struct vop_reclaim_args *ap)
3860 struct vnode *vp = ap->a_vp;
3865 mtx_lock(&sync_mtx);
3866 if (vp->v_mount->mnt_syncer == vp)
3867 vp->v_mount->mnt_syncer = NULL;
3868 if (bo->bo_flag & BO_ONWORKLST) {
3869 LIST_REMOVE(bo, bo_synclist);
3870 syncer_worklist_len--;
3872 bo->bo_flag &= ~BO_ONWORKLST;
3874 mtx_unlock(&sync_mtx);
3881 * Check if vnode represents a disk device
3884 vn_isdisk(struct vnode *vp, int *errp)
3888 if (vp->v_type != VCHR) {
3894 if (vp->v_rdev == NULL)
3896 else if (vp->v_rdev->si_devsw == NULL)
3898 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3904 return (error == 0);
3908 * Common filesystem object access control check routine. Accepts a
3909 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3910 * and optional call-by-reference privused argument allowing vaccess()
3911 * to indicate to the caller whether privilege was used to satisfy the
3912 * request (obsoleted). Returns 0 on success, or an errno on failure.
3915 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3916 accmode_t accmode, struct ucred *cred, int *privused)
3918 accmode_t dac_granted;
3919 accmode_t priv_granted;
3921 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3922 ("invalid bit in accmode"));
3923 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3924 ("VAPPEND without VWRITE"));
3927 * Look for a normal, non-privileged way to access the file/directory
3928 * as requested. If it exists, go with that.
3931 if (privused != NULL)
3936 /* Check the owner. */
3937 if (cred->cr_uid == file_uid) {
3938 dac_granted |= VADMIN;
3939 if (file_mode & S_IXUSR)
3940 dac_granted |= VEXEC;
3941 if (file_mode & S_IRUSR)
3942 dac_granted |= VREAD;
3943 if (file_mode & S_IWUSR)
3944 dac_granted |= (VWRITE | VAPPEND);
3946 if ((accmode & dac_granted) == accmode)
3952 /* Otherwise, check the groups (first match) */
3953 if (groupmember(file_gid, cred)) {
3954 if (file_mode & S_IXGRP)
3955 dac_granted |= VEXEC;
3956 if (file_mode & S_IRGRP)
3957 dac_granted |= VREAD;
3958 if (file_mode & S_IWGRP)
3959 dac_granted |= (VWRITE | VAPPEND);
3961 if ((accmode & dac_granted) == accmode)
3967 /* Otherwise, check everyone else. */
3968 if (file_mode & S_IXOTH)
3969 dac_granted |= VEXEC;
3970 if (file_mode & S_IROTH)
3971 dac_granted |= VREAD;
3972 if (file_mode & S_IWOTH)
3973 dac_granted |= (VWRITE | VAPPEND);
3974 if ((accmode & dac_granted) == accmode)
3979 * Build a privilege mask to determine if the set of privileges
3980 * satisfies the requirements when combined with the granted mask
3981 * from above. For each privilege, if the privilege is required,
3982 * bitwise or the request type onto the priv_granted mask.
3988 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3989 * requests, instead of PRIV_VFS_EXEC.
3991 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3992 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3993 priv_granted |= VEXEC;
3996 * Ensure that at least one execute bit is on. Otherwise,
3997 * a privileged user will always succeed, and we don't want
3998 * this to happen unless the file really is executable.
4000 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4001 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
4002 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
4003 priv_granted |= VEXEC;
4006 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
4007 !priv_check_cred(cred, PRIV_VFS_READ, 0))
4008 priv_granted |= VREAD;
4010 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
4011 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
4012 priv_granted |= (VWRITE | VAPPEND);
4014 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
4015 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
4016 priv_granted |= VADMIN;
4018 if ((accmode & (priv_granted | dac_granted)) == accmode) {
4019 /* XXX audit: privilege used */
4020 if (privused != NULL)
4025 return ((accmode & VADMIN) ? EPERM : EACCES);
4029 * Credential check based on process requesting service, and per-attribute
4033 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
4034 struct thread *td, accmode_t accmode)
4038 * Kernel-invoked always succeeds.
4044 * Do not allow privileged processes in jail to directly manipulate
4045 * system attributes.
4047 switch (attrnamespace) {
4048 case EXTATTR_NAMESPACE_SYSTEM:
4049 /* Potentially should be: return (EPERM); */
4050 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
4051 case EXTATTR_NAMESPACE_USER:
4052 return (VOP_ACCESS(vp, accmode, cred, td));
4058 #ifdef DEBUG_VFS_LOCKS
4060 * This only exists to supress warnings from unlocked specfs accesses. It is
4061 * no longer ok to have an unlocked VFS.
4063 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4064 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4066 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4067 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4068 "Drop into debugger on lock violation");
4070 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4071 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4072 0, "Check for interlock across VOPs");
4074 int vfs_badlock_print = 1; /* Print lock violations. */
4075 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4076 0, "Print lock violations");
4079 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4080 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4081 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4085 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4089 if (vfs_badlock_backtrace)
4092 if (vfs_badlock_print)
4093 printf("%s: %p %s\n", str, (void *)vp, msg);
4094 if (vfs_badlock_ddb)
4095 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4099 assert_vi_locked(struct vnode *vp, const char *str)
4102 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4103 vfs_badlock("interlock is not locked but should be", str, vp);
4107 assert_vi_unlocked(struct vnode *vp, const char *str)
4110 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4111 vfs_badlock("interlock is locked but should not be", str, vp);
4115 assert_vop_locked(struct vnode *vp, const char *str)
4119 if (!IGNORE_LOCK(vp)) {
4120 locked = VOP_ISLOCKED(vp);
4121 if (locked == 0 || locked == LK_EXCLOTHER)
4122 vfs_badlock("is not locked but should be", str, vp);
4127 assert_vop_unlocked(struct vnode *vp, const char *str)
4130 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4131 vfs_badlock("is locked but should not be", str, vp);
4135 assert_vop_elocked(struct vnode *vp, const char *str)
4138 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4139 vfs_badlock("is not exclusive locked but should be", str, vp);
4144 assert_vop_elocked_other(struct vnode *vp, const char *str)
4147 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
4148 vfs_badlock("is not exclusive locked by another thread",
4153 assert_vop_slocked(struct vnode *vp, const char *str)
4156 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
4157 vfs_badlock("is not locked shared but should be", str, vp);
4160 #endif /* DEBUG_VFS_LOCKS */
4163 vop_rename_fail(struct vop_rename_args *ap)
4166 if (ap->a_tvp != NULL)
4168 if (ap->a_tdvp == ap->a_tvp)
4177 vop_rename_pre(void *ap)
4179 struct vop_rename_args *a = ap;
4181 #ifdef DEBUG_VFS_LOCKS
4183 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4184 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4185 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4186 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4188 /* Check the source (from). */
4189 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4190 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4191 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4192 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4193 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4195 /* Check the target. */
4197 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4198 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4200 if (a->a_tdvp != a->a_fdvp)
4202 if (a->a_tvp != a->a_fvp)
4210 vop_strategy_pre(void *ap)
4212 #ifdef DEBUG_VFS_LOCKS
4213 struct vop_strategy_args *a;
4220 * Cluster ops lock their component buffers but not the IO container.
4222 if ((bp->b_flags & B_CLUSTER) != 0)
4225 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4226 if (vfs_badlock_print)
4228 "VOP_STRATEGY: bp is not locked but should be\n");
4229 if (vfs_badlock_ddb)
4230 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4236 vop_lock_pre(void *ap)
4238 #ifdef DEBUG_VFS_LOCKS
4239 struct vop_lock1_args *a = ap;
4241 if ((a->a_flags & LK_INTERLOCK) == 0)
4242 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4244 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4249 vop_lock_post(void *ap, int rc)
4251 #ifdef DEBUG_VFS_LOCKS
4252 struct vop_lock1_args *a = ap;
4254 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4255 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4256 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4261 vop_unlock_pre(void *ap)
4263 #ifdef DEBUG_VFS_LOCKS
4264 struct vop_unlock_args *a = ap;
4266 if (a->a_flags & LK_INTERLOCK)
4267 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4268 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4273 vop_unlock_post(void *ap, int rc)
4275 #ifdef DEBUG_VFS_LOCKS
4276 struct vop_unlock_args *a = ap;
4278 if (a->a_flags & LK_INTERLOCK)
4279 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4284 vop_create_post(void *ap, int rc)
4286 struct vop_create_args *a = ap;
4289 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4293 vop_deleteextattr_post(void *ap, int rc)
4295 struct vop_deleteextattr_args *a = ap;
4298 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4302 vop_link_post(void *ap, int rc)
4304 struct vop_link_args *a = ap;
4307 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4308 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4313 vop_mkdir_post(void *ap, int rc)
4315 struct vop_mkdir_args *a = ap;
4318 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4322 vop_mknod_post(void *ap, int rc)
4324 struct vop_mknod_args *a = ap;
4327 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4331 vop_remove_post(void *ap, int rc)
4333 struct vop_remove_args *a = ap;
4336 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4337 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4342 vop_rename_post(void *ap, int rc)
4344 struct vop_rename_args *a = ap;
4347 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4348 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4349 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4351 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4353 if (a->a_tdvp != a->a_fdvp)
4355 if (a->a_tvp != a->a_fvp)
4363 vop_rmdir_post(void *ap, int rc)
4365 struct vop_rmdir_args *a = ap;
4368 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4369 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4374 vop_setattr_post(void *ap, int rc)
4376 struct vop_setattr_args *a = ap;
4379 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4383 vop_setextattr_post(void *ap, int rc)
4385 struct vop_setextattr_args *a = ap;
4388 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4392 vop_symlink_post(void *ap, int rc)
4394 struct vop_symlink_args *a = ap;
4397 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4400 static struct knlist fs_knlist;
4403 vfs_event_init(void *arg)
4405 knlist_init_mtx(&fs_knlist, NULL);
4407 /* XXX - correct order? */
4408 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4411 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4414 KNOTE_UNLOCKED(&fs_knlist, event);
4417 static int filt_fsattach(struct knote *kn);
4418 static void filt_fsdetach(struct knote *kn);
4419 static int filt_fsevent(struct knote *kn, long hint);
4421 struct filterops fs_filtops = {
4423 .f_attach = filt_fsattach,
4424 .f_detach = filt_fsdetach,
4425 .f_event = filt_fsevent
4429 filt_fsattach(struct knote *kn)
4432 kn->kn_flags |= EV_CLEAR;
4433 knlist_add(&fs_knlist, kn, 0);
4438 filt_fsdetach(struct knote *kn)
4441 knlist_remove(&fs_knlist, kn, 0);
4445 filt_fsevent(struct knote *kn, long hint)
4448 kn->kn_fflags |= hint;
4449 return (kn->kn_fflags != 0);
4453 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4459 error = SYSCTL_IN(req, &vc, sizeof(vc));
4462 if (vc.vc_vers != VFS_CTL_VERS1)
4464 mp = vfs_getvfs(&vc.vc_fsid);
4467 /* ensure that a specific sysctl goes to the right filesystem. */
4468 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4469 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4473 VCTLTOREQ(&vc, req);
4474 error = VFS_SYSCTL(mp, vc.vc_op, req);
4479 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4480 NULL, 0, sysctl_vfs_ctl, "",
4484 * Function to initialize a va_filerev field sensibly.
4485 * XXX: Wouldn't a random number make a lot more sense ??
4488 init_va_filerev(void)
4493 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4496 static int filt_vfsread(struct knote *kn, long hint);
4497 static int filt_vfswrite(struct knote *kn, long hint);
4498 static int filt_vfsvnode(struct knote *kn, long hint);
4499 static void filt_vfsdetach(struct knote *kn);
4500 static struct filterops vfsread_filtops = {
4502 .f_detach = filt_vfsdetach,
4503 .f_event = filt_vfsread
4505 static struct filterops vfswrite_filtops = {
4507 .f_detach = filt_vfsdetach,
4508 .f_event = filt_vfswrite
4510 static struct filterops vfsvnode_filtops = {
4512 .f_detach = filt_vfsdetach,
4513 .f_event = filt_vfsvnode
4517 vfs_knllock(void *arg)
4519 struct vnode *vp = arg;
4521 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4525 vfs_knlunlock(void *arg)
4527 struct vnode *vp = arg;
4533 vfs_knl_assert_locked(void *arg)
4535 #ifdef DEBUG_VFS_LOCKS
4536 struct vnode *vp = arg;
4538 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4543 vfs_knl_assert_unlocked(void *arg)
4545 #ifdef DEBUG_VFS_LOCKS
4546 struct vnode *vp = arg;
4548 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4553 vfs_kqfilter(struct vop_kqfilter_args *ap)
4555 struct vnode *vp = ap->a_vp;
4556 struct knote *kn = ap->a_kn;
4559 switch (kn->kn_filter) {
4561 kn->kn_fop = &vfsread_filtops;
4564 kn->kn_fop = &vfswrite_filtops;
4567 kn->kn_fop = &vfsvnode_filtops;
4573 kn->kn_hook = (caddr_t)vp;
4576 if (vp->v_pollinfo == NULL)
4578 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4580 knlist_add(knl, kn, 0);
4586 * Detach knote from vnode
4589 filt_vfsdetach(struct knote *kn)
4591 struct vnode *vp = (struct vnode *)kn->kn_hook;
4593 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4594 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4600 filt_vfsread(struct knote *kn, long hint)
4602 struct vnode *vp = (struct vnode *)kn->kn_hook;
4607 * filesystem is gone, so set the EOF flag and schedule
4608 * the knote for deletion.
4610 if (hint == NOTE_REVOKE) {
4612 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4617 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4621 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4622 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
4629 filt_vfswrite(struct knote *kn, long hint)
4631 struct vnode *vp = (struct vnode *)kn->kn_hook;
4636 * filesystem is gone, so set the EOF flag and schedule
4637 * the knote for deletion.
4639 if (hint == NOTE_REVOKE)
4640 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4648 filt_vfsvnode(struct knote *kn, long hint)
4650 struct vnode *vp = (struct vnode *)kn->kn_hook;
4654 if (kn->kn_sfflags & hint)
4655 kn->kn_fflags |= hint;
4656 if (hint == NOTE_REVOKE) {
4657 kn->kn_flags |= EV_EOF;
4661 res = (kn->kn_fflags != 0);
4667 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4671 if (dp->d_reclen > ap->a_uio->uio_resid)
4672 return (ENAMETOOLONG);
4673 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4675 if (ap->a_ncookies != NULL) {
4676 if (ap->a_cookies != NULL)
4677 free(ap->a_cookies, M_TEMP);
4678 ap->a_cookies = NULL;
4679 *ap->a_ncookies = 0;
4683 if (ap->a_ncookies == NULL)
4686 KASSERT(ap->a_cookies,
4687 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4689 *ap->a_cookies = realloc(*ap->a_cookies,
4690 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4691 (*ap->a_cookies)[*ap->a_ncookies] = off;
4696 * Mark for update the access time of the file if the filesystem
4697 * supports VOP_MARKATIME. This functionality is used by execve and
4698 * mmap, so we want to avoid the I/O implied by directly setting
4699 * va_atime for the sake of efficiency.
4702 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4707 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4708 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4709 (void)VOP_MARKATIME(vp);
4713 * The purpose of this routine is to remove granularity from accmode_t,
4714 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4715 * VADMIN and VAPPEND.
4717 * If it returns 0, the caller is supposed to continue with the usual
4718 * access checks using 'accmode' as modified by this routine. If it
4719 * returns nonzero value, the caller is supposed to return that value
4722 * Note that after this routine runs, accmode may be zero.
4725 vfs_unixify_accmode(accmode_t *accmode)
4728 * There is no way to specify explicit "deny" rule using
4729 * file mode or POSIX.1e ACLs.
4731 if (*accmode & VEXPLICIT_DENY) {
4737 * None of these can be translated into usual access bits.
4738 * Also, the common case for NFSv4 ACLs is to not contain
4739 * either of these bits. Caller should check for VWRITE
4740 * on the containing directory instead.
4742 if (*accmode & (VDELETE_CHILD | VDELETE))
4745 if (*accmode & VADMIN_PERMS) {
4746 *accmode &= ~VADMIN_PERMS;
4751 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4752 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4754 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
4760 * These are helper functions for filesystems to traverse all
4761 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
4763 * This interface replaces MNT_VNODE_FOREACH.
4766 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
4769 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
4774 kern_yield(PRI_USER);
4776 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4777 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
4778 while (vp != NULL && (vp->v_type == VMARKER ||
4779 (vp->v_iflag & VI_DOOMED) != 0))
4780 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4782 /* Check if we are done */
4784 __mnt_vnode_markerfree_all(mvp, mp);
4785 /* MNT_IUNLOCK(mp); -- done in above function */
4786 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4789 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4790 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4797 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
4801 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4804 (*mvp)->v_type = VMARKER;
4806 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
4807 while (vp != NULL && (vp->v_type == VMARKER ||
4808 (vp->v_iflag & VI_DOOMED) != 0))
4809 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4811 /* Check if we are done */
4815 free(*mvp, M_VNODE_MARKER);
4819 (*mvp)->v_mount = mp;
4820 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4828 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
4836 mtx_assert(MNT_MTX(mp), MA_OWNED);
4838 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4839 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4842 free(*mvp, M_VNODE_MARKER);
4847 * These are helper functions for filesystems to traverse their
4848 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
4851 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4854 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4859 free(*mvp, M_VNODE_MARKER);
4863 static struct vnode *
4864 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4866 struct vnode *vp, *nvp;
4868 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
4869 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4871 vp = TAILQ_NEXT(*mvp, v_actfreelist);
4872 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4873 while (vp != NULL) {
4874 if (vp->v_type == VMARKER) {
4875 vp = TAILQ_NEXT(vp, v_actfreelist);
4878 if (!VI_TRYLOCK(vp)) {
4879 if (mp_ncpus == 1 || should_yield()) {
4880 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4881 mtx_unlock(&vnode_free_list_mtx);
4883 mtx_lock(&vnode_free_list_mtx);
4888 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
4889 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
4890 ("alien vnode on the active list %p %p", vp, mp));
4891 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
4893 nvp = TAILQ_NEXT(vp, v_actfreelist);
4898 /* Check if we are done */
4900 mtx_unlock(&vnode_free_list_mtx);
4901 mnt_vnode_markerfree_active(mvp, mp);
4904 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4905 mtx_unlock(&vnode_free_list_mtx);
4906 ASSERT_VI_LOCKED(vp, "active iter");
4907 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
4912 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4916 kern_yield(PRI_USER);
4917 mtx_lock(&vnode_free_list_mtx);
4918 return (mnt_vnode_next_active(mvp, mp));
4922 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
4926 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4930 (*mvp)->v_type = VMARKER;
4931 (*mvp)->v_mount = mp;
4933 mtx_lock(&vnode_free_list_mtx);
4934 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
4936 mtx_unlock(&vnode_free_list_mtx);
4937 mnt_vnode_markerfree_active(mvp, mp);
4940 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4941 return (mnt_vnode_next_active(mvp, mp));
4945 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4951 mtx_lock(&vnode_free_list_mtx);
4952 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4953 mtx_unlock(&vnode_free_list_mtx);
4954 mnt_vnode_markerfree_active(mvp, mp);