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 * 3. 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_usecount_locked(struct vnode *);
108 static void v_incr_devcount(struct vnode *);
109 static void v_decr_devcount(struct vnode *);
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 vnlru_return_batches(struct vfsops *mnt_op);
116 static void destroy_vpollinfo(struct vpollinfo *vi);
119 * Number of vnodes in existence. Increased whenever getnewvnode()
120 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
122 static unsigned long numvnodes;
124 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
125 "Number of vnodes in existence");
127 static u_long vnodes_created;
128 SYSCTL_ULONG(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
129 0, "Number of vnodes created by getnewvnode");
131 static u_long mnt_free_list_batch = 128;
132 SYSCTL_ULONG(_vfs, OID_AUTO, mnt_free_list_batch, CTLFLAG_RW,
133 &mnt_free_list_batch, 0, "Limit of vnodes held on mnt's free list");
136 * Conversion tables for conversion from vnode types to inode formats
139 enum vtype iftovt_tab[16] = {
140 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
141 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
143 int vttoif_tab[10] = {
144 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
145 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
149 * List of vnodes that are ready for recycling.
151 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
154 * "Free" vnode target. Free vnodes are rarely completely free, but are
155 * just ones that are cheap to recycle. Usually they are for files which
156 * have been stat'd but not read; these usually have inode and namecache
157 * data attached to them. This target is the preferred minimum size of a
158 * sub-cache consisting mostly of such files. The system balances the size
159 * of this sub-cache with its complement to try to prevent either from
160 * thrashing while the other is relatively inactive. The targets express
161 * a preference for the best balance.
163 * "Above" this target there are 2 further targets (watermarks) related
164 * to recyling of free vnodes. In the best-operating case, the cache is
165 * exactly full, the free list has size between vlowat and vhiwat above the
166 * free target, and recycling from it and normal use maintains this state.
167 * Sometimes the free list is below vlowat or even empty, but this state
168 * is even better for immediate use provided the cache is not full.
169 * Otherwise, vnlru_proc() runs to reclaim enough vnodes (usually non-free
170 * ones) to reach one of these states. The watermarks are currently hard-
171 * coded as 4% and 9% of the available space higher. These and the default
172 * of 25% for wantfreevnodes are too large if the memory size is large.
173 * E.g., 9% of 75% of MAXVNODES is more than 566000 vnodes to reclaim
174 * whenever vnlru_proc() becomes active.
176 static u_long wantfreevnodes;
177 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW,
178 &wantfreevnodes, 0, "Target for minimum number of \"free\" vnodes");
179 static u_long freevnodes;
180 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD,
181 &freevnodes, 0, "Number of \"free\" vnodes");
183 static u_long recycles_count;
184 SYSCTL_ULONG(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 0,
185 "Number of vnodes recycled to meet vnode cache targets");
188 * Various variables used for debugging the new implementation of
190 * XXX these are probably of (very) limited utility now.
192 static int reassignbufcalls;
193 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
194 "Number of calls to reassignbuf");
196 static u_long free_owe_inact;
197 SYSCTL_ULONG(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact, 0,
198 "Number of times free vnodes kept on active list due to VFS "
199 "owing inactivation");
201 /* To keep more than one thread at a time from running vfs_getnewfsid */
202 static struct mtx mntid_mtx;
205 * Lock for any access to the following:
210 static struct mtx vnode_free_list_mtx;
212 /* Publicly exported FS */
213 struct nfs_public nfs_pub;
215 static uma_zone_t buf_trie_zone;
217 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
218 static uma_zone_t vnode_zone;
219 static uma_zone_t vnodepoll_zone;
222 * The workitem queue.
224 * It is useful to delay writes of file data and filesystem metadata
225 * for tens of seconds so that quickly created and deleted files need
226 * not waste disk bandwidth being created and removed. To realize this,
227 * we append vnodes to a "workitem" queue. When running with a soft
228 * updates implementation, most pending metadata dependencies should
229 * not wait for more than a few seconds. Thus, mounted on block devices
230 * are delayed only about a half the time that file data is delayed.
231 * Similarly, directory updates are more critical, so are only delayed
232 * about a third the time that file data is delayed. Thus, there are
233 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
234 * one each second (driven off the filesystem syncer process). The
235 * syncer_delayno variable indicates the next queue that is to be processed.
236 * Items that need to be processed soon are placed in this queue:
238 * syncer_workitem_pending[syncer_delayno]
240 * A delay of fifteen seconds is done by placing the request fifteen
241 * entries later in the queue:
243 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
246 static int syncer_delayno;
247 static long syncer_mask;
248 LIST_HEAD(synclist, bufobj);
249 static struct synclist *syncer_workitem_pending;
251 * The sync_mtx protects:
256 * syncer_workitem_pending
257 * syncer_worklist_len
260 static struct mtx sync_mtx;
261 static struct cv sync_wakeup;
263 #define SYNCER_MAXDELAY 32
264 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
265 static int syncdelay = 30; /* max time to delay syncing data */
266 static int filedelay = 30; /* time to delay syncing files */
267 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
268 "Time to delay syncing files (in seconds)");
269 static int dirdelay = 29; /* time to delay syncing directories */
270 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
271 "Time to delay syncing directories (in seconds)");
272 static int metadelay = 28; /* time to delay syncing metadata */
273 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
274 "Time to delay syncing metadata (in seconds)");
275 static int rushjob; /* number of slots to run ASAP */
276 static int stat_rush_requests; /* number of times I/O speeded up */
277 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
278 "Number of times I/O speeded up (rush requests)");
281 * When shutting down the syncer, run it at four times normal speed.
283 #define SYNCER_SHUTDOWN_SPEEDUP 4
284 static int sync_vnode_count;
285 static int syncer_worklist_len;
286 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
289 /* Target for maximum number of vnodes. */
291 static int gapvnodes; /* gap between wanted and desired */
292 static int vhiwat; /* enough extras after expansion */
293 static int vlowat; /* minimal extras before expansion */
294 static int vstir; /* nonzero to stir non-free vnodes */
295 static volatile int vsmalltrigger = 8; /* pref to keep if > this many pages */
298 sysctl_update_desiredvnodes(SYSCTL_HANDLER_ARGS)
300 int error, old_desiredvnodes;
302 old_desiredvnodes = desiredvnodes;
303 if ((error = sysctl_handle_int(oidp, arg1, arg2, req)) != 0)
305 if (old_desiredvnodes != desiredvnodes) {
306 wantfreevnodes = desiredvnodes / 4;
307 /* XXX locking seems to be incomplete. */
308 vfs_hash_changesize(desiredvnodes);
309 cache_changesize(desiredvnodes);
314 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
315 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &desiredvnodes, 0,
316 sysctl_update_desiredvnodes, "I", "Target for maximum number of vnodes");
317 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
318 &wantfreevnodes, 0, "Old name for vfs.wantfreevnodes (legacy)");
319 static int vnlru_nowhere;
320 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
321 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
323 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
327 * Support for the bufobj clean & dirty pctrie.
330 buf_trie_alloc(struct pctrie *ptree)
333 return uma_zalloc(buf_trie_zone, M_NOWAIT);
337 buf_trie_free(struct pctrie *ptree, void *node)
340 uma_zfree(buf_trie_zone, node);
342 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
345 * Initialize the vnode management data structures.
347 * Reevaluate the following cap on the number of vnodes after the physical
348 * memory size exceeds 512GB. In the limit, as the physical memory size
349 * grows, the ratio of the memory size in KB to to vnodes approaches 64:1.
351 #ifndef MAXVNODES_MAX
352 #define MAXVNODES_MAX (512 * 1024 * 1024 / 64) /* 8M */
356 * Initialize a vnode as it first enters the zone.
359 vnode_init(void *mem, int size, int flags)
369 vp->v_vnlock = &vp->v_lock;
370 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
372 * By default, don't allow shared locks unless filesystems opt-in.
374 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT,
375 LK_NOSHARE | LK_IS_VNODE);
380 rw_init(BO_LOCKPTR(bo), "bufobj interlock");
382 TAILQ_INIT(&bo->bo_clean.bv_hd);
383 TAILQ_INIT(&bo->bo_dirty.bv_hd);
385 * Initialize namecache.
387 LIST_INIT(&vp->v_cache_src);
388 TAILQ_INIT(&vp->v_cache_dst);
390 * Initialize rangelocks.
392 rangelock_init(&vp->v_rl);
397 * Free a vnode when it is cleared from the zone.
400 vnode_fini(void *mem, int size)
406 rangelock_destroy(&vp->v_rl);
407 lockdestroy(vp->v_vnlock);
408 mtx_destroy(&vp->v_interlock);
410 rw_destroy(BO_LOCKPTR(bo));
414 * Provide the size of NFS nclnode and NFS fh for calculation of the
415 * vnode memory consumption. The size is specified directly to
416 * eliminate dependency on NFS-private header.
418 * Other filesystems may use bigger or smaller (like UFS and ZFS)
419 * private inode data, but the NFS-based estimation is ample enough.
420 * Still, we care about differences in the size between 64- and 32-bit
423 * Namecache structure size is heuristically
424 * sizeof(struct namecache_ts) + CACHE_PATH_CUTOFF + 1.
427 #define NFS_NCLNODE_SZ (528 + 64)
430 #define NFS_NCLNODE_SZ (360 + 32)
435 vntblinit(void *dummy __unused)
438 int physvnodes, virtvnodes;
441 * Desiredvnodes is a function of the physical memory size and the
442 * kernel's heap size. Generally speaking, it scales with the
443 * physical memory size. The ratio of desiredvnodes to the physical
444 * memory size is 1:16 until desiredvnodes exceeds 98,304.
446 * marginal ratio of desiredvnodes to the physical memory size is
447 * 1:64. However, desiredvnodes is limited by the kernel's heap
448 * size. The memory required by desiredvnodes vnodes and vm objects
449 * must not exceed 1/10th of the kernel's heap size.
451 physvnodes = maxproc + pgtok(vm_cnt.v_page_count) / 64 +
452 3 * min(98304 * 16, pgtok(vm_cnt.v_page_count)) / 64;
453 virtvnodes = vm_kmem_size / (10 * (sizeof(struct vm_object) +
454 sizeof(struct vnode) + NC_SZ * ncsizefactor + NFS_NCLNODE_SZ));
455 desiredvnodes = min(physvnodes, virtvnodes);
456 if (desiredvnodes > MAXVNODES_MAX) {
458 printf("Reducing kern.maxvnodes %d -> %d\n",
459 desiredvnodes, MAXVNODES_MAX);
460 desiredvnodes = MAXVNODES_MAX;
462 wantfreevnodes = desiredvnodes / 4;
463 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
464 TAILQ_INIT(&vnode_free_list);
465 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
466 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
467 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0);
468 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
469 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
471 * Preallocate enough nodes to support one-per buf so that
472 * we can not fail an insert. reassignbuf() callers can not
473 * tolerate the insertion failure.
475 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
476 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
477 UMA_ZONE_NOFREE | UMA_ZONE_VM);
478 uma_prealloc(buf_trie_zone, nbuf);
480 * Initialize the filesystem syncer.
482 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
484 syncer_maxdelay = syncer_mask + 1;
485 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
486 cv_init(&sync_wakeup, "syncer");
487 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
491 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
495 * Mark a mount point as busy. Used to synchronize access and to delay
496 * unmounting. Eventually, mountlist_mtx is not released on failure.
498 * vfs_busy() is a custom lock, it can block the caller.
499 * vfs_busy() only sleeps if the unmount is active on the mount point.
500 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
501 * vnode belonging to mp.
503 * Lookup uses vfs_busy() to traverse mount points.
505 * / vnode lock A / vnode lock (/var) D
506 * /var vnode lock B /log vnode lock(/var/log) E
507 * vfs_busy lock C vfs_busy lock F
509 * Within each file system, the lock order is C->A->B and F->D->E.
511 * When traversing across mounts, the system follows that lock order:
517 * The lookup() process for namei("/var") illustrates the process:
518 * VOP_LOOKUP() obtains B while A is held
519 * vfs_busy() obtains a shared lock on F while A and B are held
520 * vput() releases lock on B
521 * vput() releases lock on A
522 * VFS_ROOT() obtains lock on D while shared lock on F is held
523 * vfs_unbusy() releases shared lock on F
524 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
525 * Attempt to lock A (instead of vp_crossmp) while D is held would
526 * violate the global order, causing deadlocks.
528 * dounmount() locks B while F is drained.
531 vfs_busy(struct mount *mp, int flags)
534 MPASS((flags & ~MBF_MASK) == 0);
535 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
540 * If mount point is currently being unmounted, sleep until the
541 * mount point fate is decided. If thread doing the unmounting fails,
542 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
543 * that this mount point has survived the unmount attempt and vfs_busy
544 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
545 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
546 * about to be really destroyed. vfs_busy needs to release its
547 * reference on the mount point in this case and return with ENOENT,
548 * telling the caller that mount mount it tried to busy is no longer
551 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
552 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
555 CTR1(KTR_VFS, "%s: failed busying before sleeping",
559 if (flags & MBF_MNTLSTLOCK)
560 mtx_unlock(&mountlist_mtx);
561 mp->mnt_kern_flag |= MNTK_MWAIT;
562 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
563 if (flags & MBF_MNTLSTLOCK)
564 mtx_lock(&mountlist_mtx);
567 if (flags & MBF_MNTLSTLOCK)
568 mtx_unlock(&mountlist_mtx);
575 * Free a busy filesystem.
578 vfs_unbusy(struct mount *mp)
581 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
584 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
586 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
587 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
588 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
589 mp->mnt_kern_flag &= ~MNTK_DRAINING;
590 wakeup(&mp->mnt_lockref);
596 * Lookup a mount point by filesystem identifier.
599 vfs_getvfs(fsid_t *fsid)
603 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
604 mtx_lock(&mountlist_mtx);
605 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
606 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
607 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
609 mtx_unlock(&mountlist_mtx);
613 mtx_unlock(&mountlist_mtx);
614 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
615 return ((struct mount *) 0);
619 * Lookup a mount point by filesystem identifier, busying it before
622 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
623 * cache for popular filesystem identifiers. The cache is lockess, using
624 * the fact that struct mount's are never freed. In worst case we may
625 * get pointer to unmounted or even different filesystem, so we have to
626 * check what we got, and go slow way if so.
629 vfs_busyfs(fsid_t *fsid)
631 #define FSID_CACHE_SIZE 256
632 typedef struct mount * volatile vmp_t;
633 static vmp_t cache[FSID_CACHE_SIZE];
638 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
639 hash = fsid->val[0] ^ fsid->val[1];
640 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
643 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
644 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
646 if (vfs_busy(mp, 0) != 0) {
650 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
651 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
657 mtx_lock(&mountlist_mtx);
658 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
659 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
660 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
661 error = vfs_busy(mp, MBF_MNTLSTLOCK);
664 mtx_unlock(&mountlist_mtx);
671 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
672 mtx_unlock(&mountlist_mtx);
673 return ((struct mount *) 0);
677 * Check if a user can access privileged mount options.
680 vfs_suser(struct mount *mp, struct thread *td)
685 * If the thread is jailed, but this is not a jail-friendly file
686 * system, deny immediately.
688 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
692 * If the file system was mounted outside the jail of the calling
693 * thread, deny immediately.
695 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
699 * If file system supports delegated administration, we don't check
700 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
701 * by the file system itself.
702 * If this is not the user that did original mount, we check for
703 * the PRIV_VFS_MOUNT_OWNER privilege.
705 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
706 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
707 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
714 * Get a new unique fsid. Try to make its val[0] unique, since this value
715 * will be used to create fake device numbers for stat(). Also try (but
716 * not so hard) make its val[0] unique mod 2^16, since some emulators only
717 * support 16-bit device numbers. We end up with unique val[0]'s for the
718 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
720 * Keep in mind that several mounts may be running in parallel. Starting
721 * the search one past where the previous search terminated is both a
722 * micro-optimization and a defense against returning the same fsid to
726 vfs_getnewfsid(struct mount *mp)
728 static uint16_t mntid_base;
733 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
734 mtx_lock(&mntid_mtx);
735 mtype = mp->mnt_vfc->vfc_typenum;
736 tfsid.val[1] = mtype;
737 mtype = (mtype & 0xFF) << 24;
739 tfsid.val[0] = makedev(255,
740 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
742 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
746 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
747 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
748 mtx_unlock(&mntid_mtx);
752 * Knob to control the precision of file timestamps:
754 * 0 = seconds only; nanoseconds zeroed.
755 * 1 = seconds and nanoseconds, accurate within 1/HZ.
756 * 2 = seconds and nanoseconds, truncated to microseconds.
757 * >=3 = seconds and nanoseconds, maximum precision.
759 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
761 static int timestamp_precision = TSP_USEC;
762 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
763 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
764 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
765 "3+: sec + ns (max. precision))");
768 * Get a current timestamp.
771 vfs_timestamp(struct timespec *tsp)
775 switch (timestamp_precision) {
777 tsp->tv_sec = time_second;
785 TIMEVAL_TO_TIMESPEC(&tv, tsp);
795 * Set vnode attributes to VNOVAL
798 vattr_null(struct vattr *vap)
802 vap->va_size = VNOVAL;
803 vap->va_bytes = VNOVAL;
804 vap->va_mode = VNOVAL;
805 vap->va_nlink = VNOVAL;
806 vap->va_uid = VNOVAL;
807 vap->va_gid = VNOVAL;
808 vap->va_fsid = VNOVAL;
809 vap->va_fileid = VNOVAL;
810 vap->va_blocksize = VNOVAL;
811 vap->va_rdev = VNOVAL;
812 vap->va_atime.tv_sec = VNOVAL;
813 vap->va_atime.tv_nsec = VNOVAL;
814 vap->va_mtime.tv_sec = VNOVAL;
815 vap->va_mtime.tv_nsec = VNOVAL;
816 vap->va_ctime.tv_sec = VNOVAL;
817 vap->va_ctime.tv_nsec = VNOVAL;
818 vap->va_birthtime.tv_sec = VNOVAL;
819 vap->va_birthtime.tv_nsec = VNOVAL;
820 vap->va_flags = VNOVAL;
821 vap->va_gen = VNOVAL;
826 * This routine is called when we have too many vnodes. It attempts
827 * to free <count> vnodes and will potentially free vnodes that still
828 * have VM backing store (VM backing store is typically the cause
829 * of a vnode blowout so we want to do this). Therefore, this operation
830 * is not considered cheap.
832 * A number of conditions may prevent a vnode from being reclaimed.
833 * the buffer cache may have references on the vnode, a directory
834 * vnode may still have references due to the namei cache representing
835 * underlying files, or the vnode may be in active use. It is not
836 * desirable to reuse such vnodes. These conditions may cause the
837 * number of vnodes to reach some minimum value regardless of what
838 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
841 vlrureclaim(struct mount *mp, int reclaim_nc_src, int trigger)
844 int count, done, target;
847 vn_start_write(NULL, &mp, V_WAIT);
849 count = mp->mnt_nvnodelistsize;
850 target = count * (int64_t)gapvnodes / imax(desiredvnodes, 1);
851 target = target / 10 + 1;
852 while (count != 0 && done < target) {
853 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
854 while (vp != NULL && vp->v_type == VMARKER)
855 vp = TAILQ_NEXT(vp, v_nmntvnodes);
859 * XXX LRU is completely broken for non-free vnodes. First
860 * by calling here in mountpoint order, then by moving
861 * unselected vnodes to the end here, and most grossly by
862 * removing the vlruvp() function that was supposed to
863 * maintain the order. (This function was born broken
864 * since syncer problems prevented it doing anything.) The
865 * order is closer to LRC (C = Created).
867 * LRU reclaiming of vnodes seems to have last worked in
868 * FreeBSD-3 where LRU wasn't mentioned under any spelling.
869 * Then there was no hold count, and inactive vnodes were
870 * simply put on the free list in LRU order. The separate
871 * lists also break LRU. We prefer to reclaim from the
872 * free list for technical reasons. This tends to thrash
873 * the free list to keep very unrecently used held vnodes.
874 * The problem is mitigated by keeping the free list large.
876 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
877 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
882 * If it's been deconstructed already, it's still
883 * referenced, or it exceeds the trigger, skip it.
884 * Also skip free vnodes. We are trying to make space
885 * to expand the free list, not reduce it.
887 if (vp->v_usecount ||
888 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
889 ((vp->v_iflag & VI_FREE) != 0) ||
890 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
891 vp->v_object->resident_page_count > trigger)) {
897 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
899 goto next_iter_mntunlocked;
903 * v_usecount may have been bumped after VOP_LOCK() dropped
904 * the vnode interlock and before it was locked again.
906 * It is not necessary to recheck VI_DOOMED because it can
907 * only be set by another thread that holds both the vnode
908 * lock and vnode interlock. If another thread has the
909 * vnode lock before we get to VOP_LOCK() and obtains the
910 * vnode interlock after VOP_LOCK() drops the vnode
911 * interlock, the other thread will be unable to drop the
912 * vnode lock before our VOP_LOCK() call fails.
914 if (vp->v_usecount ||
915 (!reclaim_nc_src && !LIST_EMPTY(&vp->v_cache_src)) ||
916 (vp->v_iflag & VI_FREE) != 0 ||
917 (vp->v_object != NULL &&
918 vp->v_object->resident_page_count > trigger)) {
919 VOP_UNLOCK(vp, LK_INTERLOCK);
921 goto next_iter_mntunlocked;
923 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
924 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
925 atomic_add_long(&recycles_count, 1);
930 next_iter_mntunlocked:
939 kern_yield(PRI_USER);
944 vn_finished_write(mp);
948 static int max_vnlru_free = 10000; /* limit on vnode free requests per call */
949 SYSCTL_INT(_debug, OID_AUTO, max_vnlru_free, CTLFLAG_RW, &max_vnlru_free,
951 "limit on vnode free requests per call to the vnlru_free routine");
954 * Attempt to reduce the free list by the requested amount.
957 vnlru_free_locked(int count, struct vfsops *mnt_op)
963 tried_batches = false;
964 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
965 if (count > max_vnlru_free)
966 count = max_vnlru_free;
967 for (; count > 0; count--) {
968 vp = TAILQ_FIRST(&vnode_free_list);
970 * The list can be modified while the free_list_mtx
971 * has been dropped and vp could be NULL here.
976 mtx_unlock(&vnode_free_list_mtx);
977 vnlru_return_batches(mnt_op);
978 tried_batches = true;
979 mtx_lock(&vnode_free_list_mtx);
983 VNASSERT(vp->v_op != NULL, vp,
984 ("vnlru_free: vnode already reclaimed."));
985 KASSERT((vp->v_iflag & VI_FREE) != 0,
986 ("Removing vnode not on freelist"));
987 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
988 ("Mangling active vnode"));
989 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
992 * Don't recycle if our vnode is from different type
993 * of mount point. Note that mp is type-safe, the
994 * check does not reach unmapped address even if
995 * vnode is reclaimed.
996 * Don't recycle if we can't get the interlock without
999 if ((mnt_op != NULL && (mp = vp->v_mount) != NULL &&
1000 mp->mnt_op != mnt_op) || !VI_TRYLOCK(vp)) {
1001 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
1004 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
1005 vp, ("vp inconsistent on freelist"));
1008 * The clear of VI_FREE prevents activation of the
1009 * vnode. There is no sense in putting the vnode on
1010 * the mount point active list, only to remove it
1011 * later during recycling. Inline the relevant part
1012 * of vholdl(), to avoid triggering assertions or
1016 vp->v_iflag &= ~VI_FREE;
1017 refcount_acquire(&vp->v_holdcnt);
1019 mtx_unlock(&vnode_free_list_mtx);
1023 * If the recycled succeeded this vdrop will actually free
1024 * the vnode. If not it will simply place it back on
1028 mtx_lock(&vnode_free_list_mtx);
1033 vnlru_free(int count, struct vfsops *mnt_op)
1036 mtx_lock(&vnode_free_list_mtx);
1037 vnlru_free_locked(count, mnt_op);
1038 mtx_unlock(&vnode_free_list_mtx);
1042 /* XXX some names and initialization are bad for limits and watermarks. */
1048 gapvnodes = imax(desiredvnodes - wantfreevnodes, 100);
1049 vhiwat = gapvnodes / 11; /* 9% -- just under the 10% in vlrureclaim() */
1050 vlowat = vhiwat / 2;
1051 if (numvnodes > desiredvnodes)
1053 space = desiredvnodes - numvnodes;
1054 if (freevnodes > wantfreevnodes)
1055 space += freevnodes - wantfreevnodes;
1060 vnlru_return_batch_locked(struct mount *mp)
1064 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
1066 if (mp->mnt_tmpfreevnodelistsize == 0)
1069 TAILQ_FOREACH(vp, &mp->mnt_tmpfreevnodelist, v_actfreelist) {
1070 VNASSERT((vp->v_mflag & VMP_TMPMNTFREELIST) != 0, vp,
1071 ("vnode without VMP_TMPMNTFREELIST on mnt_tmpfreevnodelist"));
1072 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
1074 mtx_lock(&vnode_free_list_mtx);
1075 TAILQ_CONCAT(&vnode_free_list, &mp->mnt_tmpfreevnodelist, v_actfreelist);
1076 freevnodes += mp->mnt_tmpfreevnodelistsize;
1077 mtx_unlock(&vnode_free_list_mtx);
1078 mp->mnt_tmpfreevnodelistsize = 0;
1082 vnlru_return_batch(struct mount *mp)
1085 mtx_lock(&mp->mnt_listmtx);
1086 vnlru_return_batch_locked(mp);
1087 mtx_unlock(&mp->mnt_listmtx);
1091 vnlru_return_batches(struct vfsops *mnt_op)
1093 struct mount *mp, *nmp;
1096 mtx_lock(&mountlist_mtx);
1097 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1098 need_unbusy = false;
1099 if (mnt_op != NULL && mp->mnt_op != mnt_op)
1101 if (mp->mnt_tmpfreevnodelistsize == 0)
1103 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) == 0) {
1104 vnlru_return_batch(mp);
1106 mtx_lock(&mountlist_mtx);
1109 nmp = TAILQ_NEXT(mp, mnt_list);
1113 mtx_unlock(&mountlist_mtx);
1117 * Attempt to recycle vnodes in a context that is always safe to block.
1118 * Calling vlrurecycle() from the bowels of filesystem code has some
1119 * interesting deadlock problems.
1121 static struct proc *vnlruproc;
1122 static int vnlruproc_sig;
1127 struct mount *mp, *nmp;
1128 unsigned long ofreevnodes, onumvnodes;
1129 int done, force, reclaim_nc_src, trigger, usevnodes;
1131 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, vnlruproc,
1132 SHUTDOWN_PRI_FIRST);
1136 kproc_suspend_check(vnlruproc);
1137 mtx_lock(&vnode_free_list_mtx);
1139 * If numvnodes is too large (due to desiredvnodes being
1140 * adjusted using its sysctl, or emergency growth), first
1141 * try to reduce it by discarding from the free list.
1143 if (numvnodes > desiredvnodes)
1144 vnlru_free_locked(numvnodes - desiredvnodes, NULL);
1146 * Sleep if the vnode cache is in a good state. This is
1147 * when it is not over-full and has space for about a 4%
1148 * or 9% expansion (by growing its size or inexcessively
1149 * reducing its free list). Otherwise, try to reclaim
1150 * space for a 10% expansion.
1152 if (vstir && force == 0) {
1156 if (vspace() >= vlowat && force == 0) {
1158 wakeup(&vnlruproc_sig);
1159 msleep(vnlruproc, &vnode_free_list_mtx,
1160 PVFS|PDROP, "vlruwt", hz);
1163 mtx_unlock(&vnode_free_list_mtx);
1165 ofreevnodes = freevnodes;
1166 onumvnodes = numvnodes;
1168 * Calculate parameters for recycling. These are the same
1169 * throughout the loop to give some semblance of fairness.
1170 * The trigger point is to avoid recycling vnodes with lots
1171 * of resident pages. We aren't trying to free memory; we
1172 * are trying to recycle or at least free vnodes.
1174 if (numvnodes <= desiredvnodes)
1175 usevnodes = numvnodes - freevnodes;
1177 usevnodes = numvnodes;
1181 * The trigger value is is chosen to give a conservatively
1182 * large value to ensure that it alone doesn't prevent
1183 * making progress. The value can easily be so large that
1184 * it is effectively infinite in some congested and
1185 * misconfigured cases, and this is necessary. Normally
1186 * it is about 8 to 100 (pages), which is quite large.
1188 trigger = vm_cnt.v_page_count * 2 / usevnodes;
1190 trigger = vsmalltrigger;
1191 reclaim_nc_src = force >= 3;
1192 mtx_lock(&mountlist_mtx);
1193 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1194 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
1195 nmp = TAILQ_NEXT(mp, mnt_list);
1198 done += vlrureclaim(mp, reclaim_nc_src, trigger);
1199 mtx_lock(&mountlist_mtx);
1200 nmp = TAILQ_NEXT(mp, mnt_list);
1203 mtx_unlock(&mountlist_mtx);
1204 if (onumvnodes > desiredvnodes && numvnodes <= desiredvnodes)
1207 if (force == 0 || force == 1) {
1217 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
1219 kern_yield(PRI_USER);
1221 * After becoming active to expand above low water, keep
1222 * active until above high water.
1224 force = vspace() < vhiwat;
1228 static struct kproc_desc vnlru_kp = {
1233 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
1237 * Routines having to do with the management of the vnode table.
1241 * Try to recycle a freed vnode. We abort if anyone picks up a reference
1242 * before we actually vgone(). This function must be called with the vnode
1243 * held to prevent the vnode from being returned to the free list midway
1247 vtryrecycle(struct vnode *vp)
1251 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
1252 VNASSERT(vp->v_holdcnt, vp,
1253 ("vtryrecycle: Recycling vp %p without a reference.", vp));
1255 * This vnode may found and locked via some other list, if so we
1256 * can't recycle it yet.
1258 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1260 "%s: impossible to recycle, vp %p lock is already held",
1262 return (EWOULDBLOCK);
1265 * Don't recycle if its filesystem is being suspended.
1267 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1270 "%s: impossible to recycle, cannot start the write for %p",
1275 * If we got this far, we need to acquire the interlock and see if
1276 * anyone picked up this vnode from another list. If not, we will
1277 * mark it with DOOMED via vgonel() so that anyone who does find it
1278 * will skip over it.
1281 if (vp->v_usecount) {
1282 VOP_UNLOCK(vp, LK_INTERLOCK);
1283 vn_finished_write(vnmp);
1285 "%s: impossible to recycle, %p is already referenced",
1289 if ((vp->v_iflag & VI_DOOMED) == 0) {
1290 atomic_add_long(&recycles_count, 1);
1293 VOP_UNLOCK(vp, LK_INTERLOCK);
1294 vn_finished_write(vnmp);
1302 if (vspace() < vlowat && vnlruproc_sig == 0) {
1309 * Wait if necessary for space for a new vnode.
1312 getnewvnode_wait(int suspended)
1315 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1316 if (numvnodes >= desiredvnodes) {
1319 * The file system is being suspended. We cannot
1320 * risk a deadlock here, so allow allocation of
1321 * another vnode even if this would give too many.
1325 if (vnlruproc_sig == 0) {
1326 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1329 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1332 /* Post-adjust like the pre-adjust in getnewvnode(). */
1333 if (numvnodes + 1 > desiredvnodes && freevnodes > 1)
1334 vnlru_free_locked(1, NULL);
1335 return (numvnodes >= desiredvnodes ? ENFILE : 0);
1339 * This hack is fragile, and probably not needed any more now that the
1340 * watermark handling works.
1343 getnewvnode_reserve(u_int count)
1347 /* Pre-adjust like the pre-adjust in getnewvnode(), with any count. */
1348 /* XXX no longer so quick, but this part is not racy. */
1349 mtx_lock(&vnode_free_list_mtx);
1350 if (numvnodes + count > desiredvnodes && freevnodes > wantfreevnodes)
1351 vnlru_free_locked(ulmin(numvnodes + count - desiredvnodes,
1352 freevnodes - wantfreevnodes), NULL);
1353 mtx_unlock(&vnode_free_list_mtx);
1356 /* First try to be quick and racy. */
1357 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1358 td->td_vp_reserv += count;
1359 vcheckspace(); /* XXX no longer so quick, but more racy */
1362 atomic_subtract_long(&numvnodes, count);
1364 mtx_lock(&vnode_free_list_mtx);
1366 if (getnewvnode_wait(0) == 0) {
1369 atomic_add_long(&numvnodes, 1);
1373 mtx_unlock(&vnode_free_list_mtx);
1377 * This hack is fragile, especially if desiredvnodes or wantvnodes are
1378 * misconfgured or changed significantly. Reducing desiredvnodes below
1379 * the reserved amount should cause bizarre behaviour like reducing it
1380 * below the number of active vnodes -- the system will try to reduce
1381 * numvnodes to match, but should fail, so the subtraction below should
1385 getnewvnode_drop_reserve(void)
1390 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1391 td->td_vp_reserv = 0;
1395 * Return the next vnode from the free list.
1398 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1403 struct lock_object *lo;
1404 static int cyclecount;
1407 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1410 if (td->td_vp_reserv > 0) {
1411 td->td_vp_reserv -= 1;
1414 mtx_lock(&vnode_free_list_mtx);
1415 if (numvnodes < desiredvnodes)
1417 else if (cyclecount++ >= freevnodes) {
1422 * Grow the vnode cache if it will not be above its target max
1423 * after growing. Otherwise, if the free list is nonempty, try
1424 * to reclaim 1 item from it before growing the cache (possibly
1425 * above its target max if the reclamation failed or is delayed).
1426 * Otherwise, wait for some space. In all cases, schedule
1427 * vnlru_proc() if we are getting short of space. The watermarks
1428 * should be chosen so that we never wait or even reclaim from
1429 * the free list to below its target minimum.
1431 if (numvnodes + 1 <= desiredvnodes)
1433 else if (freevnodes > 0)
1434 vnlru_free_locked(1, NULL);
1436 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1438 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1440 mtx_unlock(&vnode_free_list_mtx);
1446 atomic_add_long(&numvnodes, 1);
1447 mtx_unlock(&vnode_free_list_mtx);
1449 atomic_add_long(&vnodes_created, 1);
1450 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK);
1452 * Locks are given the generic name "vnode" when created.
1453 * Follow the historic practice of using the filesystem
1454 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc.
1456 * Locks live in a witness group keyed on their name. Thus,
1457 * when a lock is renamed, it must also move from the witness
1458 * group of its old name to the witness group of its new name.
1460 * The change only needs to be made when the vnode moves
1461 * from one filesystem type to another. We ensure that each
1462 * filesystem use a single static name pointer for its tag so
1463 * that we can compare pointers rather than doing a strcmp().
1465 lo = &vp->v_vnlock->lock_object;
1466 if (lo->lo_name != tag) {
1468 WITNESS_DESTROY(lo);
1469 WITNESS_INIT(lo, tag);
1472 * By default, don't allow shared locks unless filesystems opt-in.
1474 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE;
1476 * Finalize various vnode identity bits.
1478 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp));
1479 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp));
1480 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp));
1484 v_init_counters(vp);
1485 vp->v_bufobj.bo_ops = &buf_ops_bio;
1488 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1489 mac_vnode_associate_singlelabel(mp, vp);
1490 else if (mp == NULL && vops != &dead_vnodeops)
1491 printf("NULL mp in getnewvnode()\n");
1494 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize;
1495 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1496 vp->v_vflag |= VV_NOKNOTE;
1500 * For the filesystems which do not use vfs_hash_insert(),
1501 * still initialize v_hash to have vfs_hash_index() useful.
1502 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1505 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1512 * Delete from old mount point vnode list, if on one.
1515 delmntque(struct vnode *vp)
1525 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1526 ("Active vnode list size %d > Vnode list size %d",
1527 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1528 active = vp->v_iflag & VI_ACTIVE;
1529 vp->v_iflag &= ~VI_ACTIVE;
1531 mtx_lock(&mp->mnt_listmtx);
1532 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1533 mp->mnt_activevnodelistsize--;
1534 mtx_unlock(&mp->mnt_listmtx);
1538 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1539 ("bad mount point vnode list size"));
1540 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1541 mp->mnt_nvnodelistsize--;
1547 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1551 vp->v_op = &dead_vnodeops;
1557 * Insert into list of vnodes for the new mount point, if available.
1560 insmntque1(struct vnode *vp, struct mount *mp,
1561 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1564 KASSERT(vp->v_mount == NULL,
1565 ("insmntque: vnode already on per mount vnode list"));
1566 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1567 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1570 * We acquire the vnode interlock early to ensure that the
1571 * vnode cannot be recycled by another process releasing a
1572 * holdcnt on it before we get it on both the vnode list
1573 * and the active vnode list. The mount mutex protects only
1574 * manipulation of the vnode list and the vnode freelist
1575 * mutex protects only manipulation of the active vnode list.
1576 * Hence the need to hold the vnode interlock throughout.
1580 if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1581 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1582 mp->mnt_nvnodelistsize == 0)) &&
1583 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1592 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1593 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1594 ("neg mount point vnode list size"));
1595 mp->mnt_nvnodelistsize++;
1596 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1597 ("Activating already active vnode"));
1598 vp->v_iflag |= VI_ACTIVE;
1599 mtx_lock(&mp->mnt_listmtx);
1600 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1601 mp->mnt_activevnodelistsize++;
1602 mtx_unlock(&mp->mnt_listmtx);
1609 insmntque(struct vnode *vp, struct mount *mp)
1612 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1616 * Flush out and invalidate all buffers associated with a bufobj
1617 * Called with the underlying object locked.
1620 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1625 if (flags & V_SAVE) {
1626 error = bufobj_wwait(bo, slpflag, slptimeo);
1631 if (bo->bo_dirty.bv_cnt > 0) {
1633 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1636 * XXX We could save a lock/unlock if this was only
1637 * enabled under INVARIANTS
1640 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1641 panic("vinvalbuf: dirty bufs");
1645 * If you alter this loop please notice that interlock is dropped and
1646 * reacquired in flushbuflist. Special care is needed to ensure that
1647 * no race conditions occur from this.
1650 error = flushbuflist(&bo->bo_clean,
1651 flags, bo, slpflag, slptimeo);
1652 if (error == 0 && !(flags & V_CLEANONLY))
1653 error = flushbuflist(&bo->bo_dirty,
1654 flags, bo, slpflag, slptimeo);
1655 if (error != 0 && error != EAGAIN) {
1659 } while (error != 0);
1662 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1663 * have write I/O in-progress but if there is a VM object then the
1664 * VM object can also have read-I/O in-progress.
1667 bufobj_wwait(bo, 0, 0);
1669 if (bo->bo_object != NULL) {
1670 VM_OBJECT_WLOCK(bo->bo_object);
1671 vm_object_pip_wait(bo->bo_object, "bovlbx");
1672 VM_OBJECT_WUNLOCK(bo->bo_object);
1675 } while (bo->bo_numoutput > 0);
1679 * Destroy the copy in the VM cache, too.
1681 if (bo->bo_object != NULL &&
1682 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1683 VM_OBJECT_WLOCK(bo->bo_object);
1684 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1685 OBJPR_CLEANONLY : 0);
1686 VM_OBJECT_WUNLOCK(bo->bo_object);
1691 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1692 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1693 panic("vinvalbuf: flush failed");
1700 * Flush out and invalidate all buffers associated with a vnode.
1701 * Called with the underlying object locked.
1704 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1707 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1708 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1709 if (vp->v_object != NULL && vp->v_object->handle != vp)
1711 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1715 * Flush out buffers on the specified list.
1719 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1722 struct buf *bp, *nbp;
1727 ASSERT_BO_WLOCKED(bo);
1730 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1731 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1732 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1738 lblkno = nbp->b_lblkno;
1739 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1742 error = BUF_TIMELOCK(bp,
1743 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1744 "flushbuf", slpflag, slptimeo);
1747 return (error != ENOLCK ? error : EAGAIN);
1749 KASSERT(bp->b_bufobj == bo,
1750 ("bp %p wrong b_bufobj %p should be %p",
1751 bp, bp->b_bufobj, bo));
1753 * XXX Since there are no node locks for NFS, I
1754 * believe there is a slight chance that a delayed
1755 * write will occur while sleeping just above, so
1758 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1761 bp->b_flags |= B_ASYNC;
1764 return (EAGAIN); /* XXX: why not loop ? */
1767 bp->b_flags |= (B_INVAL | B_RELBUF);
1768 bp->b_flags &= ~B_ASYNC;
1771 nbp = gbincore(bo, lblkno);
1772 if (nbp == NULL || (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1774 break; /* nbp invalid */
1780 bnoreuselist(struct bufv *bufv, struct bufobj *bo, daddr_t startn, daddr_t endn)
1786 ASSERT_BO_LOCKED(bo);
1788 for (lblkno = startn;;) {
1790 bp = BUF_PCTRIE_LOOKUP_GE(&bufv->bv_root, lblkno);
1791 if (bp == NULL || bp->b_lblkno >= endn ||
1792 bp->b_lblkno < startn)
1794 error = BUF_TIMELOCK(bp, LK_EXCLUSIVE | LK_SLEEPFAIL |
1795 LK_INTERLOCK, BO_LOCKPTR(bo), "brlsfl", 0, 0);
1798 if (error == ENOLCK)
1802 KASSERT(bp->b_bufobj == bo,
1803 ("bp %p wrong b_bufobj %p should be %p",
1804 bp, bp->b_bufobj, bo));
1805 lblkno = bp->b_lblkno + 1;
1806 if ((bp->b_flags & B_MANAGED) == 0)
1808 bp->b_flags |= B_RELBUF;
1810 * In the VMIO case, use the B_NOREUSE flag to hint that the
1811 * pages backing each buffer in the range are unlikely to be
1812 * reused. Dirty buffers will have the hint applied once
1813 * they've been written.
1815 if (bp->b_vp->v_object != NULL)
1816 bp->b_flags |= B_NOREUSE;
1824 * Truncate a file's buffer and pages to a specified length. This
1825 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1829 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
1831 struct buf *bp, *nbp;
1836 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1837 vp, cred, blksize, (uintmax_t)length);
1840 * Round up to the *next* lbn.
1842 trunclbn = howmany(length, blksize);
1844 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1851 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1852 if (bp->b_lblkno < trunclbn)
1855 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1856 BO_LOCKPTR(bo)) == ENOLCK)
1860 bp->b_flags |= (B_INVAL | B_RELBUF);
1861 bp->b_flags &= ~B_ASYNC;
1867 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1868 (nbp->b_vp != vp) ||
1869 (nbp->b_flags & B_DELWRI))) {
1875 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1876 if (bp->b_lblkno < trunclbn)
1879 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1880 BO_LOCKPTR(bo)) == ENOLCK)
1883 bp->b_flags |= (B_INVAL | B_RELBUF);
1884 bp->b_flags &= ~B_ASYNC;
1890 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1891 (nbp->b_vp != vp) ||
1892 (nbp->b_flags & B_DELWRI) == 0)) {
1901 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1902 if (bp->b_lblkno > 0)
1905 * Since we hold the vnode lock this should only
1906 * fail if we're racing with the buf daemon.
1909 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1910 BO_LOCKPTR(bo)) == ENOLCK) {
1913 VNASSERT((bp->b_flags & B_DELWRI), vp,
1914 ("buf(%p) on dirty queue without DELWRI", bp));
1923 bufobj_wwait(bo, 0, 0);
1925 vnode_pager_setsize(vp, length);
1931 buf_vlist_remove(struct buf *bp)
1935 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1936 ASSERT_BO_WLOCKED(bp->b_bufobj);
1937 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1938 (BX_VNDIRTY|BX_VNCLEAN),
1939 ("buf_vlist_remove: Buf %p is on two lists", bp));
1940 if (bp->b_xflags & BX_VNDIRTY)
1941 bv = &bp->b_bufobj->bo_dirty;
1943 bv = &bp->b_bufobj->bo_clean;
1944 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
1945 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1947 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1951 * Add the buffer to the sorted clean or dirty block list.
1953 * NOTE: xflags is passed as a constant, optimizing this inline function!
1956 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1962 ASSERT_BO_WLOCKED(bo);
1963 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
1964 ("dead bo %p", bo));
1965 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1966 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1967 bp->b_xflags |= xflags;
1968 if (xflags & BX_VNDIRTY)
1974 * Keep the list ordered. Optimize empty list insertion. Assume
1975 * we tend to grow at the tail so lookup_le should usually be cheaper
1978 if (bv->bv_cnt == 0 ||
1979 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
1980 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1981 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
1982 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
1984 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
1985 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
1987 panic("buf_vlist_add: Preallocated nodes insufficient.");
1992 * Look up a buffer using the buffer tries.
1995 gbincore(struct bufobj *bo, daddr_t lblkno)
1999 ASSERT_BO_LOCKED(bo);
2000 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
2003 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
2007 * Associate a buffer with a vnode.
2010 bgetvp(struct vnode *vp, struct buf *bp)
2015 ASSERT_BO_WLOCKED(bo);
2016 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
2018 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
2019 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
2020 ("bgetvp: bp already attached! %p", bp));
2026 * Insert onto list for new vnode.
2028 buf_vlist_add(bp, bo, BX_VNCLEAN);
2032 * Disassociate a buffer from a vnode.
2035 brelvp(struct buf *bp)
2040 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
2041 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
2044 * Delete from old vnode list, if on one.
2046 vp = bp->b_vp; /* XXX */
2049 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2050 buf_vlist_remove(bp);
2052 panic("brelvp: Buffer %p not on queue.", bp);
2053 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2054 bo->bo_flag &= ~BO_ONWORKLST;
2055 mtx_lock(&sync_mtx);
2056 LIST_REMOVE(bo, bo_synclist);
2057 syncer_worklist_len--;
2058 mtx_unlock(&sync_mtx);
2061 bp->b_bufobj = NULL;
2067 * Add an item to the syncer work queue.
2070 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
2074 ASSERT_BO_WLOCKED(bo);
2076 mtx_lock(&sync_mtx);
2077 if (bo->bo_flag & BO_ONWORKLST)
2078 LIST_REMOVE(bo, bo_synclist);
2080 bo->bo_flag |= BO_ONWORKLST;
2081 syncer_worklist_len++;
2084 if (delay > syncer_maxdelay - 2)
2085 delay = syncer_maxdelay - 2;
2086 slot = (syncer_delayno + delay) & syncer_mask;
2088 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
2089 mtx_unlock(&sync_mtx);
2093 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
2097 mtx_lock(&sync_mtx);
2098 len = syncer_worklist_len - sync_vnode_count;
2099 mtx_unlock(&sync_mtx);
2100 error = SYSCTL_OUT(req, &len, sizeof(len));
2104 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
2105 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
2107 static struct proc *updateproc;
2108 static void sched_sync(void);
2109 static struct kproc_desc up_kp = {
2114 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
2117 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
2122 *bo = LIST_FIRST(slp);
2126 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
2129 * We use vhold in case the vnode does not
2130 * successfully sync. vhold prevents the vnode from
2131 * going away when we unlock the sync_mtx so that
2132 * we can acquire the vnode interlock.
2135 mtx_unlock(&sync_mtx);
2137 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
2139 mtx_lock(&sync_mtx);
2140 return (*bo == LIST_FIRST(slp));
2142 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2143 (void) VOP_FSYNC(vp, MNT_LAZY, td);
2145 vn_finished_write(mp);
2147 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
2149 * Put us back on the worklist. The worklist
2150 * routine will remove us from our current
2151 * position and then add us back in at a later
2154 vn_syncer_add_to_worklist(*bo, syncdelay);
2158 mtx_lock(&sync_mtx);
2162 static int first_printf = 1;
2165 * System filesystem synchronizer daemon.
2170 struct synclist *next, *slp;
2173 struct thread *td = curthread;
2175 int net_worklist_len;
2176 int syncer_final_iter;
2180 syncer_final_iter = 0;
2181 syncer_state = SYNCER_RUNNING;
2182 starttime = time_uptime;
2183 td->td_pflags |= TDP_NORUNNINGBUF;
2185 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
2188 mtx_lock(&sync_mtx);
2190 if (syncer_state == SYNCER_FINAL_DELAY &&
2191 syncer_final_iter == 0) {
2192 mtx_unlock(&sync_mtx);
2193 kproc_suspend_check(td->td_proc);
2194 mtx_lock(&sync_mtx);
2196 net_worklist_len = syncer_worklist_len - sync_vnode_count;
2197 if (syncer_state != SYNCER_RUNNING &&
2198 starttime != time_uptime) {
2200 printf("\nSyncing disks, vnodes remaining... ");
2203 printf("%d ", net_worklist_len);
2205 starttime = time_uptime;
2208 * Push files whose dirty time has expired. Be careful
2209 * of interrupt race on slp queue.
2211 * Skip over empty worklist slots when shutting down.
2214 slp = &syncer_workitem_pending[syncer_delayno];
2215 syncer_delayno += 1;
2216 if (syncer_delayno == syncer_maxdelay)
2218 next = &syncer_workitem_pending[syncer_delayno];
2220 * If the worklist has wrapped since the
2221 * it was emptied of all but syncer vnodes,
2222 * switch to the FINAL_DELAY state and run
2223 * for one more second.
2225 if (syncer_state == SYNCER_SHUTTING_DOWN &&
2226 net_worklist_len == 0 &&
2227 last_work_seen == syncer_delayno) {
2228 syncer_state = SYNCER_FINAL_DELAY;
2229 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
2231 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
2232 syncer_worklist_len > 0);
2235 * Keep track of the last time there was anything
2236 * on the worklist other than syncer vnodes.
2237 * Return to the SHUTTING_DOWN state if any
2240 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
2241 last_work_seen = syncer_delayno;
2242 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
2243 syncer_state = SYNCER_SHUTTING_DOWN;
2244 while (!LIST_EMPTY(slp)) {
2245 error = sync_vnode(slp, &bo, td);
2247 LIST_REMOVE(bo, bo_synclist);
2248 LIST_INSERT_HEAD(next, bo, bo_synclist);
2252 if (first_printf == 0) {
2254 * Drop the sync mutex, because some watchdog
2255 * drivers need to sleep while patting
2257 mtx_unlock(&sync_mtx);
2258 wdog_kern_pat(WD_LASTVAL);
2259 mtx_lock(&sync_mtx);
2263 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
2264 syncer_final_iter--;
2266 * The variable rushjob allows the kernel to speed up the
2267 * processing of the filesystem syncer process. A rushjob
2268 * value of N tells the filesystem syncer to process the next
2269 * N seconds worth of work on its queue ASAP. Currently rushjob
2270 * is used by the soft update code to speed up the filesystem
2271 * syncer process when the incore state is getting so far
2272 * ahead of the disk that the kernel memory pool is being
2273 * threatened with exhaustion.
2280 * Just sleep for a short period of time between
2281 * iterations when shutting down to allow some I/O
2284 * If it has taken us less than a second to process the
2285 * current work, then wait. Otherwise start right over
2286 * again. We can still lose time if any single round
2287 * takes more than two seconds, but it does not really
2288 * matter as we are just trying to generally pace the
2289 * filesystem activity.
2291 if (syncer_state != SYNCER_RUNNING ||
2292 time_uptime == starttime) {
2294 sched_prio(td, PPAUSE);
2297 if (syncer_state != SYNCER_RUNNING)
2298 cv_timedwait(&sync_wakeup, &sync_mtx,
2299 hz / SYNCER_SHUTDOWN_SPEEDUP);
2300 else if (time_uptime == starttime)
2301 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2306 * Request the syncer daemon to speed up its work.
2307 * We never push it to speed up more than half of its
2308 * normal turn time, otherwise it could take over the cpu.
2311 speedup_syncer(void)
2315 mtx_lock(&sync_mtx);
2316 if (rushjob < syncdelay / 2) {
2318 stat_rush_requests += 1;
2321 mtx_unlock(&sync_mtx);
2322 cv_broadcast(&sync_wakeup);
2327 * Tell the syncer to speed up its work and run though its work
2328 * list several times, then tell it to shut down.
2331 syncer_shutdown(void *arg, int howto)
2334 if (howto & RB_NOSYNC)
2336 mtx_lock(&sync_mtx);
2337 syncer_state = SYNCER_SHUTTING_DOWN;
2339 mtx_unlock(&sync_mtx);
2340 cv_broadcast(&sync_wakeup);
2341 kproc_shutdown(arg, howto);
2345 syncer_suspend(void)
2348 syncer_shutdown(updateproc, 0);
2355 mtx_lock(&sync_mtx);
2357 syncer_state = SYNCER_RUNNING;
2358 mtx_unlock(&sync_mtx);
2359 cv_broadcast(&sync_wakeup);
2360 kproc_resume(updateproc);
2364 * Reassign a buffer from one vnode to another.
2365 * Used to assign file specific control information
2366 * (indirect blocks) to the vnode to which they belong.
2369 reassignbuf(struct buf *bp)
2382 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2383 bp, bp->b_vp, bp->b_flags);
2385 * B_PAGING flagged buffers cannot be reassigned because their vp
2386 * is not fully linked in.
2388 if (bp->b_flags & B_PAGING)
2389 panic("cannot reassign paging buffer");
2392 * Delete from old vnode list, if on one.
2395 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2396 buf_vlist_remove(bp);
2398 panic("reassignbuf: Buffer %p not on queue.", bp);
2400 * If dirty, put on list of dirty buffers; otherwise insert onto list
2403 if (bp->b_flags & B_DELWRI) {
2404 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2405 switch (vp->v_type) {
2415 vn_syncer_add_to_worklist(bo, delay);
2417 buf_vlist_add(bp, bo, BX_VNDIRTY);
2419 buf_vlist_add(bp, bo, BX_VNCLEAN);
2421 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2422 mtx_lock(&sync_mtx);
2423 LIST_REMOVE(bo, bo_synclist);
2424 syncer_worklist_len--;
2425 mtx_unlock(&sync_mtx);
2426 bo->bo_flag &= ~BO_ONWORKLST;
2431 bp = TAILQ_FIRST(&bv->bv_hd);
2432 KASSERT(bp == NULL || bp->b_bufobj == bo,
2433 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2434 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2435 KASSERT(bp == NULL || bp->b_bufobj == bo,
2436 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2438 bp = TAILQ_FIRST(&bv->bv_hd);
2439 KASSERT(bp == NULL || bp->b_bufobj == bo,
2440 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2441 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2442 KASSERT(bp == NULL || bp->b_bufobj == bo,
2443 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2449 * A temporary hack until refcount_* APIs are sorted out.
2452 vfs_refcount_acquire_if_not_zero(volatile u_int *count)
2460 if (atomic_cmpset_int(count, old, old + 1))
2466 vfs_refcount_release_if_not_last(volatile u_int *count)
2474 if (atomic_cmpset_int(count, old, old - 1))
2480 v_init_counters(struct vnode *vp)
2483 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2484 vp, ("%s called for an initialized vnode", __FUNCTION__));
2485 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2487 refcount_init(&vp->v_holdcnt, 1);
2488 refcount_init(&vp->v_usecount, 1);
2492 v_incr_usecount_locked(struct vnode *vp)
2495 ASSERT_VI_LOCKED(vp, __func__);
2496 if ((vp->v_iflag & VI_OWEINACT) != 0) {
2497 VNASSERT(vp->v_usecount == 0, vp,
2498 ("vnode with usecount and VI_OWEINACT set"));
2499 vp->v_iflag &= ~VI_OWEINACT;
2501 refcount_acquire(&vp->v_usecount);
2502 v_incr_devcount(vp);
2506 * Increment the use count on the vnode, taking care to reference
2507 * the driver's usecount if this is a chardev.
2510 v_incr_usecount(struct vnode *vp)
2513 ASSERT_VI_UNLOCKED(vp, __func__);
2514 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2516 if (vp->v_type != VCHR &&
2517 vfs_refcount_acquire_if_not_zero(&vp->v_usecount)) {
2518 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2519 ("vnode with usecount and VI_OWEINACT set"));
2522 v_incr_usecount_locked(vp);
2528 * Increment si_usecount of the associated device, if any.
2531 v_incr_devcount(struct vnode *vp)
2534 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2535 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2537 vp->v_rdev->si_usecount++;
2543 * Decrement si_usecount of the associated device, if any.
2546 v_decr_devcount(struct vnode *vp)
2549 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2550 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2552 vp->v_rdev->si_usecount--;
2558 * Grab a particular vnode from the free list, increment its
2559 * reference count and lock it. VI_DOOMED is set if the vnode
2560 * is being destroyed. Only callers who specify LK_RETRY will
2561 * see doomed vnodes. If inactive processing was delayed in
2562 * vput try to do it here.
2564 * Notes on lockless counter manipulation:
2565 * _vhold, vputx and other routines make various decisions based
2566 * on either holdcnt or usecount being 0. As long as either counter
2567 * is not transitioning 0->1 nor 1->0, the manipulation can be done
2568 * with atomic operations. Otherwise the interlock is taken covering
2569 * both the atomic and additional actions.
2572 vget(struct vnode *vp, int flags, struct thread *td)
2574 int error, oweinact;
2576 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2577 ("vget: invalid lock operation"));
2579 if ((flags & LK_INTERLOCK) != 0)
2580 ASSERT_VI_LOCKED(vp, __func__);
2582 ASSERT_VI_UNLOCKED(vp, __func__);
2583 if ((flags & LK_VNHELD) != 0)
2584 VNASSERT((vp->v_holdcnt > 0), vp,
2585 ("vget: LK_VNHELD passed but vnode not held"));
2587 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2589 if ((flags & LK_VNHELD) == 0)
2590 _vhold(vp, (flags & LK_INTERLOCK) != 0);
2592 if ((error = vn_lock(vp, flags)) != 0) {
2594 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2598 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2599 panic("vget: vn_lock failed to return ENOENT\n");
2601 * We don't guarantee that any particular close will
2602 * trigger inactive processing so just make a best effort
2603 * here at preventing a reference to a removed file. If
2604 * we don't succeed no harm is done.
2606 * Upgrade our holdcnt to a usecount.
2608 if (vp->v_type == VCHR ||
2609 !vfs_refcount_acquire_if_not_zero(&vp->v_usecount)) {
2611 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2615 vp->v_iflag &= ~VI_OWEINACT;
2617 refcount_acquire(&vp->v_usecount);
2618 v_incr_devcount(vp);
2619 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2620 (flags & LK_NOWAIT) == 0)
2628 * Increase the reference (use) and hold count of a vnode.
2629 * This will also remove the vnode from the free list if it is presently free.
2632 vref(struct vnode *vp)
2635 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2637 v_incr_usecount(vp);
2641 vrefl(struct vnode *vp)
2644 ASSERT_VI_LOCKED(vp, __func__);
2645 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2647 v_incr_usecount_locked(vp);
2651 vrefact(struct vnode *vp)
2654 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2655 if (__predict_false(vp->v_type == VCHR)) {
2656 VNASSERT(vp->v_holdcnt > 0 && vp->v_usecount > 0, vp,
2657 ("%s: wrong ref counts", __func__));
2662 int old = atomic_fetchadd_int(&vp->v_holdcnt, 1);
2663 VNASSERT(old > 0, vp, ("%s: wrong hold count", __func__));
2664 old = atomic_fetchadd_int(&vp->v_usecount, 1);
2665 VNASSERT(old > 0, vp, ("%s: wrong use count", __func__));
2667 refcount_acquire(&vp->v_holdcnt);
2668 refcount_acquire(&vp->v_usecount);
2673 * Return reference count of a vnode.
2675 * The results of this call are only guaranteed when some mechanism is used to
2676 * stop other processes from gaining references to the vnode. This may be the
2677 * case if the caller holds the only reference. This is also useful when stale
2678 * data is acceptable as race conditions may be accounted for by some other
2682 vrefcnt(struct vnode *vp)
2685 return (vp->v_usecount);
2688 #define VPUTX_VRELE 1
2689 #define VPUTX_VPUT 2
2690 #define VPUTX_VUNREF 3
2693 * Decrement the use and hold counts for a vnode.
2695 * See an explanation near vget() as to why atomic operation is safe.
2698 vputx(struct vnode *vp, int func)
2702 KASSERT(vp != NULL, ("vputx: null vp"));
2703 if (func == VPUTX_VUNREF)
2704 ASSERT_VOP_LOCKED(vp, "vunref");
2705 else if (func == VPUTX_VPUT)
2706 ASSERT_VOP_LOCKED(vp, "vput");
2708 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2709 ASSERT_VI_UNLOCKED(vp, __func__);
2710 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2712 if (vp->v_type != VCHR &&
2713 vfs_refcount_release_if_not_last(&vp->v_usecount)) {
2714 if (func == VPUTX_VPUT)
2723 * We want to hold the vnode until the inactive finishes to
2724 * prevent vgone() races. We drop the use count here and the
2725 * hold count below when we're done.
2727 if (!refcount_release(&vp->v_usecount) ||
2728 (vp->v_iflag & VI_DOINGINACT)) {
2729 if (func == VPUTX_VPUT)
2731 v_decr_devcount(vp);
2736 v_decr_devcount(vp);
2740 if (vp->v_usecount != 0) {
2741 vn_printf(vp, "vputx: usecount not zero for vnode ");
2742 panic("vputx: usecount not zero");
2745 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2748 * We must call VOP_INACTIVE with the node locked. Mark
2749 * as VI_DOINGINACT to avoid recursion.
2751 vp->v_iflag |= VI_OWEINACT;
2754 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2758 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2759 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2765 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2766 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2771 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp,
2772 ("vnode with usecount and VI_OWEINACT set"));
2774 if (vp->v_iflag & VI_OWEINACT)
2775 vinactive(vp, curthread);
2776 if (func != VPUTX_VUNREF)
2783 * Vnode put/release.
2784 * If count drops to zero, call inactive routine and return to freelist.
2787 vrele(struct vnode *vp)
2790 vputx(vp, VPUTX_VRELE);
2794 * Release an already locked vnode. This give the same effects as
2795 * unlock+vrele(), but takes less time and avoids releasing and
2796 * re-aquiring the lock (as vrele() acquires the lock internally.)
2799 vput(struct vnode *vp)
2802 vputx(vp, VPUTX_VPUT);
2806 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2809 vunref(struct vnode *vp)
2812 vputx(vp, VPUTX_VUNREF);
2816 * Increase the hold count and activate if this is the first reference.
2819 _vhold(struct vnode *vp, bool locked)
2824 ASSERT_VI_LOCKED(vp, __func__);
2826 ASSERT_VI_UNLOCKED(vp, __func__);
2827 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2828 if (!locked && vfs_refcount_acquire_if_not_zero(&vp->v_holdcnt)) {
2829 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2830 ("_vhold: vnode with holdcnt is free"));
2836 if ((vp->v_iflag & VI_FREE) == 0) {
2837 refcount_acquire(&vp->v_holdcnt);
2842 VNASSERT(vp->v_holdcnt == 0, vp,
2843 ("%s: wrong hold count", __func__));
2844 VNASSERT(vp->v_op != NULL, vp,
2845 ("%s: vnode already reclaimed.", __func__));
2847 * Remove a vnode from the free list, mark it as in use,
2848 * and put it on the active list.
2851 mtx_lock(&mp->mnt_listmtx);
2852 if ((vp->v_mflag & VMP_TMPMNTFREELIST) != 0) {
2853 TAILQ_REMOVE(&mp->mnt_tmpfreevnodelist, vp, v_actfreelist);
2854 mp->mnt_tmpfreevnodelistsize--;
2855 vp->v_mflag &= ~VMP_TMPMNTFREELIST;
2857 mtx_lock(&vnode_free_list_mtx);
2858 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2860 mtx_unlock(&vnode_free_list_mtx);
2862 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2863 ("Activating already active vnode"));
2864 vp->v_iflag &= ~VI_FREE;
2865 vp->v_iflag |= VI_ACTIVE;
2866 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2867 mp->mnt_activevnodelistsize++;
2868 mtx_unlock(&mp->mnt_listmtx);
2869 refcount_acquire(&vp->v_holdcnt);
2875 * Drop the hold count of the vnode. If this is the last reference to
2876 * the vnode we place it on the free list unless it has been vgone'd
2877 * (marked VI_DOOMED) in which case we will free it.
2879 * Because the vnode vm object keeps a hold reference on the vnode if
2880 * there is at least one resident non-cached page, the vnode cannot
2881 * leave the active list without the page cleanup done.
2884 _vdrop(struct vnode *vp, bool locked)
2891 ASSERT_VI_LOCKED(vp, __func__);
2893 ASSERT_VI_UNLOCKED(vp, __func__);
2894 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2895 if ((int)vp->v_holdcnt <= 0)
2896 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2897 if (vfs_refcount_release_if_not_last(&vp->v_holdcnt)) {
2905 if (refcount_release(&vp->v_holdcnt) == 0) {
2909 if ((vp->v_iflag & VI_DOOMED) == 0) {
2911 * Mark a vnode as free: remove it from its active list
2912 * and put it up for recycling on the freelist.
2914 VNASSERT(vp->v_op != NULL, vp,
2915 ("vdropl: vnode already reclaimed."));
2916 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2917 ("vnode already free"));
2918 VNASSERT(vp->v_holdcnt == 0, vp,
2919 ("vdropl: freeing when we shouldn't"));
2920 active = vp->v_iflag & VI_ACTIVE;
2921 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2922 vp->v_iflag &= ~VI_ACTIVE;
2924 mtx_lock(&mp->mnt_listmtx);
2926 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2928 mp->mnt_activevnodelistsize--;
2930 TAILQ_INSERT_TAIL(&mp->mnt_tmpfreevnodelist, vp,
2932 mp->mnt_tmpfreevnodelistsize++;
2933 vp->v_iflag |= VI_FREE;
2934 vp->v_mflag |= VMP_TMPMNTFREELIST;
2936 if (mp->mnt_tmpfreevnodelistsize >= mnt_free_list_batch)
2937 vnlru_return_batch_locked(mp);
2938 mtx_unlock(&mp->mnt_listmtx);
2941 atomic_add_long(&free_owe_inact, 1);
2946 * The vnode has been marked for destruction, so free it.
2948 * The vnode will be returned to the zone where it will
2949 * normally remain until it is needed for another vnode. We
2950 * need to cleanup (or verify that the cleanup has already
2951 * been done) any residual data left from its current use
2952 * so as not to contaminate the freshly allocated vnode.
2954 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2955 atomic_subtract_long(&numvnodes, 1);
2957 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2958 ("cleaned vnode still on the free list."));
2959 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2960 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2961 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2962 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2963 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2964 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2965 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
2966 ("clean blk trie not empty"));
2967 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2968 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
2969 ("dirty blk trie not empty"));
2970 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2971 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2972 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2973 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp,
2974 ("Dangling rangelock waiters"));
2977 mac_vnode_destroy(vp);
2979 if (vp->v_pollinfo != NULL) {
2980 destroy_vpollinfo(vp->v_pollinfo);
2981 vp->v_pollinfo = NULL;
2984 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2987 bzero(&vp->v_un, sizeof(vp->v_un));
2988 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
2992 uma_zfree(vnode_zone, vp);
2996 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2997 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2998 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2999 * failed lock upgrade.
3002 vinactive(struct vnode *vp, struct thread *td)
3004 struct vm_object *obj;
3006 ASSERT_VOP_ELOCKED(vp, "vinactive");
3007 ASSERT_VI_LOCKED(vp, "vinactive");
3008 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
3009 ("vinactive: recursed on VI_DOINGINACT"));
3010 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3011 vp->v_iflag |= VI_DOINGINACT;
3012 vp->v_iflag &= ~VI_OWEINACT;
3015 * Before moving off the active list, we must be sure that any
3016 * modified pages are converted into the vnode's dirty
3017 * buffers, since these will no longer be checked once the
3018 * vnode is on the inactive list.
3020 * The write-out of the dirty pages is asynchronous. At the
3021 * point that VOP_INACTIVE() is called, there could still be
3022 * pending I/O and dirty pages in the object.
3025 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
3026 VM_OBJECT_WLOCK(obj);
3027 vm_object_page_clean(obj, 0, 0, 0);
3028 VM_OBJECT_WUNLOCK(obj);
3030 VOP_INACTIVE(vp, td);
3032 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
3033 ("vinactive: lost VI_DOINGINACT"));
3034 vp->v_iflag &= ~VI_DOINGINACT;
3038 * Remove any vnodes in the vnode table belonging to mount point mp.
3040 * If FORCECLOSE is not specified, there should not be any active ones,
3041 * return error if any are found (nb: this is a user error, not a
3042 * system error). If FORCECLOSE is specified, detach any active vnodes
3045 * If WRITECLOSE is set, only flush out regular file vnodes open for
3048 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
3050 * `rootrefs' specifies the base reference count for the root vnode
3051 * of this filesystem. The root vnode is considered busy if its
3052 * v_usecount exceeds this value. On a successful return, vflush(, td)
3053 * will call vrele() on the root vnode exactly rootrefs times.
3054 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
3058 static int busyprt = 0; /* print out busy vnodes */
3059 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
3063 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
3065 struct vnode *vp, *mvp, *rootvp = NULL;
3067 int busy = 0, error;
3069 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
3072 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
3073 ("vflush: bad args"));
3075 * Get the filesystem root vnode. We can vput() it
3076 * immediately, since with rootrefs > 0, it won't go away.
3078 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
3079 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
3086 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
3088 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
3091 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3095 * Skip over a vnodes marked VV_SYSTEM.
3097 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
3103 * If WRITECLOSE is set, flush out unlinked but still open
3104 * files (even if open only for reading) and regular file
3105 * vnodes open for writing.
3107 if (flags & WRITECLOSE) {
3108 if (vp->v_object != NULL) {
3109 VM_OBJECT_WLOCK(vp->v_object);
3110 vm_object_page_clean(vp->v_object, 0, 0, 0);
3111 VM_OBJECT_WUNLOCK(vp->v_object);
3113 error = VOP_FSYNC(vp, MNT_WAIT, td);
3117 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
3120 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
3123 if ((vp->v_type == VNON ||
3124 (error == 0 && vattr.va_nlink > 0)) &&
3125 (vp->v_writecount == 0 || vp->v_type != VREG)) {
3133 * With v_usecount == 0, all we need to do is clear out the
3134 * vnode data structures and we are done.
3136 * If FORCECLOSE is set, forcibly close the vnode.
3138 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
3144 vn_printf(vp, "vflush: busy vnode ");
3150 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
3152 * If just the root vnode is busy, and if its refcount
3153 * is equal to `rootrefs', then go ahead and kill it.
3156 KASSERT(busy > 0, ("vflush: not busy"));
3157 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
3158 ("vflush: usecount %d < rootrefs %d",
3159 rootvp->v_usecount, rootrefs));
3160 if (busy == 1 && rootvp->v_usecount == rootrefs) {
3161 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
3163 VOP_UNLOCK(rootvp, 0);
3169 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
3173 for (; rootrefs > 0; rootrefs--)
3179 * Recycle an unused vnode to the front of the free list.
3182 vrecycle(struct vnode *vp)
3187 recycled = vrecyclel(vp);
3193 * vrecycle, with the vp interlock held.
3196 vrecyclel(struct vnode *vp)
3200 ASSERT_VOP_ELOCKED(vp, __func__);
3201 ASSERT_VI_LOCKED(vp, __func__);
3202 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3204 if (vp->v_usecount == 0) {
3212 * Eliminate all activity associated with a vnode
3213 * in preparation for reuse.
3216 vgone(struct vnode *vp)
3224 notify_lowervp_vfs_dummy(struct mount *mp __unused,
3225 struct vnode *lowervp __unused)
3230 * Notify upper mounts about reclaimed or unlinked vnode.
3233 vfs_notify_upper(struct vnode *vp, int event)
3235 static struct vfsops vgonel_vfsops = {
3236 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
3237 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
3239 struct mount *mp, *ump, *mmp;
3246 if (TAILQ_EMPTY(&mp->mnt_uppers))
3249 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
3250 mmp->mnt_op = &vgonel_vfsops;
3251 mmp->mnt_kern_flag |= MNTK_MARKER;
3253 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
3254 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
3255 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
3256 ump = TAILQ_NEXT(ump, mnt_upper_link);
3259 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
3262 case VFS_NOTIFY_UPPER_RECLAIM:
3263 VFS_RECLAIM_LOWERVP(ump, vp);
3265 case VFS_NOTIFY_UPPER_UNLINK:
3266 VFS_UNLINK_LOWERVP(ump, vp);
3269 KASSERT(0, ("invalid event %d", event));
3273 ump = TAILQ_NEXT(mmp, mnt_upper_link);
3274 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
3277 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
3278 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
3279 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
3280 wakeup(&mp->mnt_uppers);
3287 * vgone, with the vp interlock held.
3290 vgonel(struct vnode *vp)
3297 ASSERT_VOP_ELOCKED(vp, "vgonel");
3298 ASSERT_VI_LOCKED(vp, "vgonel");
3299 VNASSERT(vp->v_holdcnt, vp,
3300 ("vgonel: vp %p has no reference.", vp));
3301 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3305 * Don't vgonel if we're already doomed.
3307 if (vp->v_iflag & VI_DOOMED)
3309 vp->v_iflag |= VI_DOOMED;
3312 * Check to see if the vnode is in use. If so, we have to call
3313 * VOP_CLOSE() and VOP_INACTIVE().
3315 active = vp->v_usecount;
3316 oweinact = (vp->v_iflag & VI_OWEINACT);
3318 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
3321 * If purging an active vnode, it must be closed and
3322 * deactivated before being reclaimed.
3325 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
3326 if (oweinact || active) {
3328 if ((vp->v_iflag & VI_DOINGINACT) == 0)
3332 if (vp->v_type == VSOCK)
3333 vfs_unp_reclaim(vp);
3336 * Clean out any buffers associated with the vnode.
3337 * If the flush fails, just toss the buffers.
3340 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
3341 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
3342 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
3343 while (vinvalbuf(vp, 0, 0, 0) != 0)
3347 BO_LOCK(&vp->v_bufobj);
3348 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
3349 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
3350 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
3351 vp->v_bufobj.bo_clean.bv_cnt == 0,
3352 ("vp %p bufobj not invalidated", vp));
3355 * For VMIO bufobj, BO_DEAD is set in vm_object_terminate()
3356 * after the object's page queue is flushed.
3358 if (vp->v_bufobj.bo_object == NULL)
3359 vp->v_bufobj.bo_flag |= BO_DEAD;
3360 BO_UNLOCK(&vp->v_bufobj);
3363 * Reclaim the vnode.
3365 if (VOP_RECLAIM(vp, td))
3366 panic("vgone: cannot reclaim");
3368 vn_finished_secondary_write(mp);
3369 VNASSERT(vp->v_object == NULL, vp,
3370 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
3372 * Clear the advisory locks and wake up waiting threads.
3374 (void)VOP_ADVLOCKPURGE(vp);
3377 * Delete from old mount point vnode list.
3382 * Done with purge, reset to the standard lock and invalidate
3386 vp->v_vnlock = &vp->v_lock;
3387 vp->v_op = &dead_vnodeops;
3393 * Calculate the total number of references to a special device.
3396 vcount(struct vnode *vp)
3401 count = vp->v_rdev->si_usecount;
3407 * Same as above, but using the struct cdev *as argument
3410 count_dev(struct cdev *dev)
3415 count = dev->si_usecount;
3421 * Print out a description of a vnode.
3423 static char *typename[] =
3424 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
3428 vn_printf(struct vnode *vp, const char *fmt, ...)
3431 char buf[256], buf2[16];
3437 printf("%p: ", (void *)vp);
3438 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
3439 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
3440 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
3443 if (vp->v_vflag & VV_ROOT)
3444 strlcat(buf, "|VV_ROOT", sizeof(buf));
3445 if (vp->v_vflag & VV_ISTTY)
3446 strlcat(buf, "|VV_ISTTY", sizeof(buf));
3447 if (vp->v_vflag & VV_NOSYNC)
3448 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
3449 if (vp->v_vflag & VV_ETERNALDEV)
3450 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
3451 if (vp->v_vflag & VV_CACHEDLABEL)
3452 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
3453 if (vp->v_vflag & VV_TEXT)
3454 strlcat(buf, "|VV_TEXT", sizeof(buf));
3455 if (vp->v_vflag & VV_COPYONWRITE)
3456 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
3457 if (vp->v_vflag & VV_SYSTEM)
3458 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
3459 if (vp->v_vflag & VV_PROCDEP)
3460 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
3461 if (vp->v_vflag & VV_NOKNOTE)
3462 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
3463 if (vp->v_vflag & VV_DELETED)
3464 strlcat(buf, "|VV_DELETED", sizeof(buf));
3465 if (vp->v_vflag & VV_MD)
3466 strlcat(buf, "|VV_MD", sizeof(buf));
3467 if (vp->v_vflag & VV_FORCEINSMQ)
3468 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
3469 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
3470 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
3471 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
3473 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
3474 strlcat(buf, buf2, sizeof(buf));
3476 if (vp->v_iflag & VI_MOUNT)
3477 strlcat(buf, "|VI_MOUNT", sizeof(buf));
3478 if (vp->v_iflag & VI_DOOMED)
3479 strlcat(buf, "|VI_DOOMED", sizeof(buf));
3480 if (vp->v_iflag & VI_FREE)
3481 strlcat(buf, "|VI_FREE", sizeof(buf));
3482 if (vp->v_iflag & VI_ACTIVE)
3483 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3484 if (vp->v_iflag & VI_DOINGINACT)
3485 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3486 if (vp->v_iflag & VI_OWEINACT)
3487 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3488 flags = vp->v_iflag & ~(VI_MOUNT | VI_DOOMED | VI_FREE |
3489 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
3491 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3492 strlcat(buf, buf2, sizeof(buf));
3494 printf(" flags (%s)\n", buf + 1);
3495 if (mtx_owned(VI_MTX(vp)))
3496 printf(" VI_LOCKed");
3497 if (vp->v_object != NULL)
3498 printf(" v_object %p ref %d pages %d "
3499 "cleanbuf %d dirtybuf %d\n",
3500 vp->v_object, vp->v_object->ref_count,
3501 vp->v_object->resident_page_count,
3502 vp->v_bufobj.bo_clean.bv_cnt,
3503 vp->v_bufobj.bo_dirty.bv_cnt);
3505 lockmgr_printinfo(vp->v_vnlock);
3506 if (vp->v_data != NULL)
3512 * List all of the locked vnodes in the system.
3513 * Called when debugging the kernel.
3515 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3521 * Note: because this is DDB, we can't obey the locking semantics
3522 * for these structures, which means we could catch an inconsistent
3523 * state and dereference a nasty pointer. Not much to be done
3526 db_printf("Locked vnodes\n");
3527 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3528 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3529 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3530 vn_printf(vp, "vnode ");
3536 * Show details about the given vnode.
3538 DB_SHOW_COMMAND(vnode, db_show_vnode)
3544 vp = (struct vnode *)addr;
3545 vn_printf(vp, "vnode ");
3549 * Show details about the given mount point.
3551 DB_SHOW_COMMAND(mount, db_show_mount)
3562 /* No address given, print short info about all mount points. */
3563 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3564 db_printf("%p %s on %s (%s)\n", mp,
3565 mp->mnt_stat.f_mntfromname,
3566 mp->mnt_stat.f_mntonname,
3567 mp->mnt_stat.f_fstypename);
3571 db_printf("\nMore info: show mount <addr>\n");
3575 mp = (struct mount *)addr;
3576 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3577 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3580 mflags = mp->mnt_flag;
3581 #define MNT_FLAG(flag) do { \
3582 if (mflags & (flag)) { \
3583 if (buf[0] != '\0') \
3584 strlcat(buf, ", ", sizeof(buf)); \
3585 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3586 mflags &= ~(flag); \
3589 MNT_FLAG(MNT_RDONLY);
3590 MNT_FLAG(MNT_SYNCHRONOUS);
3591 MNT_FLAG(MNT_NOEXEC);
3592 MNT_FLAG(MNT_NOSUID);
3593 MNT_FLAG(MNT_NFS4ACLS);
3594 MNT_FLAG(MNT_UNION);
3595 MNT_FLAG(MNT_ASYNC);
3596 MNT_FLAG(MNT_SUIDDIR);
3597 MNT_FLAG(MNT_SOFTDEP);
3598 MNT_FLAG(MNT_NOSYMFOLLOW);
3599 MNT_FLAG(MNT_GJOURNAL);
3600 MNT_FLAG(MNT_MULTILABEL);
3602 MNT_FLAG(MNT_NOATIME);
3603 MNT_FLAG(MNT_NOCLUSTERR);
3604 MNT_FLAG(MNT_NOCLUSTERW);
3606 MNT_FLAG(MNT_EXRDONLY);
3607 MNT_FLAG(MNT_EXPORTED);
3608 MNT_FLAG(MNT_DEFEXPORTED);
3609 MNT_FLAG(MNT_EXPORTANON);
3610 MNT_FLAG(MNT_EXKERB);
3611 MNT_FLAG(MNT_EXPUBLIC);
3612 MNT_FLAG(MNT_LOCAL);
3613 MNT_FLAG(MNT_QUOTA);
3614 MNT_FLAG(MNT_ROOTFS);
3616 MNT_FLAG(MNT_IGNORE);
3617 MNT_FLAG(MNT_UPDATE);
3618 MNT_FLAG(MNT_DELEXPORT);
3619 MNT_FLAG(MNT_RELOAD);
3620 MNT_FLAG(MNT_FORCE);
3621 MNT_FLAG(MNT_SNAPSHOT);
3622 MNT_FLAG(MNT_BYFSID);
3626 strlcat(buf, ", ", sizeof(buf));
3627 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3628 "0x%016jx", mflags);
3630 db_printf(" mnt_flag = %s\n", buf);
3633 flags = mp->mnt_kern_flag;
3634 #define MNT_KERN_FLAG(flag) do { \
3635 if (flags & (flag)) { \
3636 if (buf[0] != '\0') \
3637 strlcat(buf, ", ", sizeof(buf)); \
3638 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3642 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3643 MNT_KERN_FLAG(MNTK_ASYNC);
3644 MNT_KERN_FLAG(MNTK_SOFTDEP);
3645 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3646 MNT_KERN_FLAG(MNTK_DRAINING);
3647 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3648 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3649 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3650 MNT_KERN_FLAG(MNTK_NO_IOPF);
3651 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3652 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3653 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3654 MNT_KERN_FLAG(MNTK_MARKER);
3655 MNT_KERN_FLAG(MNTK_USES_BCACHE);
3656 MNT_KERN_FLAG(MNTK_NOASYNC);
3657 MNT_KERN_FLAG(MNTK_UNMOUNT);
3658 MNT_KERN_FLAG(MNTK_MWAIT);
3659 MNT_KERN_FLAG(MNTK_SUSPEND);
3660 MNT_KERN_FLAG(MNTK_SUSPEND2);
3661 MNT_KERN_FLAG(MNTK_SUSPENDED);
3662 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3663 MNT_KERN_FLAG(MNTK_NOKNOTE);
3664 #undef MNT_KERN_FLAG
3667 strlcat(buf, ", ", sizeof(buf));
3668 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3671 db_printf(" mnt_kern_flag = %s\n", buf);
3673 db_printf(" mnt_opt = ");
3674 opt = TAILQ_FIRST(mp->mnt_opt);
3676 db_printf("%s", opt->name);
3677 opt = TAILQ_NEXT(opt, link);
3678 while (opt != NULL) {
3679 db_printf(", %s", opt->name);
3680 opt = TAILQ_NEXT(opt, link);
3686 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3687 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3688 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3689 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3690 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3691 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3692 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3693 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3694 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3695 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3696 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3697 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3699 db_printf(" mnt_cred = { uid=%u ruid=%u",
3700 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3701 if (jailed(mp->mnt_cred))
3702 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3704 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3705 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3706 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3707 db_printf(" mnt_activevnodelistsize = %d\n",
3708 mp->mnt_activevnodelistsize);
3709 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3710 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3711 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3712 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3713 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3714 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3715 db_printf(" mnt_secondary_accwrites = %d\n",
3716 mp->mnt_secondary_accwrites);
3717 db_printf(" mnt_gjprovider = %s\n",
3718 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3720 db_printf("\n\nList of active vnodes\n");
3721 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3722 if (vp->v_type != VMARKER) {
3723 vn_printf(vp, "vnode ");
3728 db_printf("\n\nList of inactive vnodes\n");
3729 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3730 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3731 vn_printf(vp, "vnode ");
3740 * Fill in a struct xvfsconf based on a struct vfsconf.
3743 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3745 struct xvfsconf xvfsp;
3747 bzero(&xvfsp, sizeof(xvfsp));
3748 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3749 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3750 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3751 xvfsp.vfc_flags = vfsp->vfc_flags;
3753 * These are unused in userland, we keep them
3754 * to not break binary compatibility.
3756 xvfsp.vfc_vfsops = NULL;
3757 xvfsp.vfc_next = NULL;
3758 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3761 #ifdef COMPAT_FREEBSD32
3763 uint32_t vfc_vfsops;
3764 char vfc_name[MFSNAMELEN];
3765 int32_t vfc_typenum;
3766 int32_t vfc_refcount;
3772 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3774 struct xvfsconf32 xvfsp;
3776 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3777 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3778 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3779 xvfsp.vfc_flags = vfsp->vfc_flags;
3780 xvfsp.vfc_vfsops = 0;
3782 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3787 * Top level filesystem related information gathering.
3790 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3792 struct vfsconf *vfsp;
3797 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3798 #ifdef COMPAT_FREEBSD32
3799 if (req->flags & SCTL_MASK32)
3800 error = vfsconf2x32(req, vfsp);
3803 error = vfsconf2x(req, vfsp);
3811 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3812 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3813 "S,xvfsconf", "List of all configured filesystems");
3815 #ifndef BURN_BRIDGES
3816 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3819 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3821 int *name = (int *)arg1 - 1; /* XXX */
3822 u_int namelen = arg2 + 1; /* XXX */
3823 struct vfsconf *vfsp;
3825 log(LOG_WARNING, "userland calling deprecated sysctl, "
3826 "please rebuild world\n");
3828 #if 1 || defined(COMPAT_PRELITE2)
3829 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3831 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3835 case VFS_MAXTYPENUM:
3838 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3841 return (ENOTDIR); /* overloaded */
3843 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3844 if (vfsp->vfc_typenum == name[2])
3849 return (EOPNOTSUPP);
3850 #ifdef COMPAT_FREEBSD32
3851 if (req->flags & SCTL_MASK32)
3852 return (vfsconf2x32(req, vfsp));
3855 return (vfsconf2x(req, vfsp));
3857 return (EOPNOTSUPP);
3860 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
3861 CTLFLAG_MPSAFE, vfs_sysctl,
3862 "Generic filesystem");
3864 #if 1 || defined(COMPAT_PRELITE2)
3867 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3870 struct vfsconf *vfsp;
3871 struct ovfsconf ovfs;
3874 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3875 bzero(&ovfs, sizeof(ovfs));
3876 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3877 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3878 ovfs.vfc_index = vfsp->vfc_typenum;
3879 ovfs.vfc_refcount = vfsp->vfc_refcount;
3880 ovfs.vfc_flags = vfsp->vfc_flags;
3881 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3891 #endif /* 1 || COMPAT_PRELITE2 */
3892 #endif /* !BURN_BRIDGES */
3894 #define KINFO_VNODESLOP 10
3897 * Dump vnode list (via sysctl).
3901 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3909 * Stale numvnodes access is not fatal here.
3912 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3914 /* Make an estimate */
3915 return (SYSCTL_OUT(req, 0, len));
3917 error = sysctl_wire_old_buffer(req, 0);
3920 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3922 mtx_lock(&mountlist_mtx);
3923 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3924 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3927 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3931 xvn[n].xv_size = sizeof *xvn;
3932 xvn[n].xv_vnode = vp;
3933 xvn[n].xv_id = 0; /* XXX compat */
3934 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3936 XV_COPY(writecount);
3942 xvn[n].xv_flag = vp->v_vflag;
3944 switch (vp->v_type) {
3951 if (vp->v_rdev == NULL) {
3955 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3958 xvn[n].xv_socket = vp->v_socket;
3961 xvn[n].xv_fifo = vp->v_fifoinfo;
3966 /* shouldn't happen? */
3974 mtx_lock(&mountlist_mtx);
3979 mtx_unlock(&mountlist_mtx);
3981 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3986 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
3987 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
3992 unmount_or_warn(struct mount *mp)
3996 error = dounmount(mp, MNT_FORCE, curthread);
3998 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
4002 printf("%d)\n", error);
4007 * Unmount all filesystems. The list is traversed in reverse order
4008 * of mounting to avoid dependencies.
4011 vfs_unmountall(void)
4013 struct mount *mp, *tmp;
4015 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
4018 * Since this only runs when rebooting, it is not interlocked.
4020 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
4024 * Forcibly unmounting "/dev" before "/" would prevent clean
4025 * unmount of the latter.
4027 if (mp == rootdevmp)
4030 unmount_or_warn(mp);
4033 if (rootdevmp != NULL)
4034 unmount_or_warn(rootdevmp);
4038 * perform msync on all vnodes under a mount point
4039 * the mount point must be locked.
4042 vfs_msync(struct mount *mp, int flags)
4044 struct vnode *vp, *mvp;
4045 struct vm_object *obj;
4047 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
4049 vnlru_return_batch(mp);
4051 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
4053 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
4054 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
4056 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
4058 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
4065 VM_OBJECT_WLOCK(obj);
4066 vm_object_page_clean(obj, 0, 0,
4068 OBJPC_SYNC : OBJPC_NOSYNC);
4069 VM_OBJECT_WUNLOCK(obj);
4079 destroy_vpollinfo_free(struct vpollinfo *vi)
4082 knlist_destroy(&vi->vpi_selinfo.si_note);
4083 mtx_destroy(&vi->vpi_lock);
4084 uma_zfree(vnodepoll_zone, vi);
4088 destroy_vpollinfo(struct vpollinfo *vi)
4091 knlist_clear(&vi->vpi_selinfo.si_note, 1);
4092 seldrain(&vi->vpi_selinfo);
4093 destroy_vpollinfo_free(vi);
4097 * Initialize per-vnode helper structure to hold poll-related state.
4100 v_addpollinfo(struct vnode *vp)
4102 struct vpollinfo *vi;
4104 if (vp->v_pollinfo != NULL)
4106 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
4107 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
4108 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
4109 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
4111 if (vp->v_pollinfo != NULL) {
4113 destroy_vpollinfo_free(vi);
4116 vp->v_pollinfo = vi;
4121 * Record a process's interest in events which might happen to
4122 * a vnode. Because poll uses the historic select-style interface
4123 * internally, this routine serves as both the ``check for any
4124 * pending events'' and the ``record my interest in future events''
4125 * functions. (These are done together, while the lock is held,
4126 * to avoid race conditions.)
4129 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
4133 mtx_lock(&vp->v_pollinfo->vpi_lock);
4134 if (vp->v_pollinfo->vpi_revents & events) {
4136 * This leaves events we are not interested
4137 * in available for the other process which
4138 * which presumably had requested them
4139 * (otherwise they would never have been
4142 events &= vp->v_pollinfo->vpi_revents;
4143 vp->v_pollinfo->vpi_revents &= ~events;
4145 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4148 vp->v_pollinfo->vpi_events |= events;
4149 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
4150 mtx_unlock(&vp->v_pollinfo->vpi_lock);
4155 * Routine to create and manage a filesystem syncer vnode.
4157 #define sync_close ((int (*)(struct vop_close_args *))nullop)
4158 static int sync_fsync(struct vop_fsync_args *);
4159 static int sync_inactive(struct vop_inactive_args *);
4160 static int sync_reclaim(struct vop_reclaim_args *);
4162 static struct vop_vector sync_vnodeops = {
4163 .vop_bypass = VOP_EOPNOTSUPP,
4164 .vop_close = sync_close, /* close */
4165 .vop_fsync = sync_fsync, /* fsync */
4166 .vop_inactive = sync_inactive, /* inactive */
4167 .vop_reclaim = sync_reclaim, /* reclaim */
4168 .vop_lock1 = vop_stdlock, /* lock */
4169 .vop_unlock = vop_stdunlock, /* unlock */
4170 .vop_islocked = vop_stdislocked, /* islocked */
4174 * Create a new filesystem syncer vnode for the specified mount point.
4177 vfs_allocate_syncvnode(struct mount *mp)
4181 static long start, incr, next;
4184 /* Allocate a new vnode */
4185 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
4187 panic("vfs_allocate_syncvnode: getnewvnode() failed");
4189 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4190 vp->v_vflag |= VV_FORCEINSMQ;
4191 error = insmntque(vp, mp);
4193 panic("vfs_allocate_syncvnode: insmntque() failed");
4194 vp->v_vflag &= ~VV_FORCEINSMQ;
4197 * Place the vnode onto the syncer worklist. We attempt to
4198 * scatter them about on the list so that they will go off
4199 * at evenly distributed times even if all the filesystems
4200 * are mounted at once.
4203 if (next == 0 || next > syncer_maxdelay) {
4207 start = syncer_maxdelay / 2;
4208 incr = syncer_maxdelay;
4214 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
4215 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
4216 mtx_lock(&sync_mtx);
4218 if (mp->mnt_syncer == NULL) {
4219 mp->mnt_syncer = vp;
4222 mtx_unlock(&sync_mtx);
4225 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4232 vfs_deallocate_syncvnode(struct mount *mp)
4236 mtx_lock(&sync_mtx);
4237 vp = mp->mnt_syncer;
4239 mp->mnt_syncer = NULL;
4240 mtx_unlock(&sync_mtx);
4246 * Do a lazy sync of the filesystem.
4249 sync_fsync(struct vop_fsync_args *ap)
4251 struct vnode *syncvp = ap->a_vp;
4252 struct mount *mp = syncvp->v_mount;
4257 * We only need to do something if this is a lazy evaluation.
4259 if (ap->a_waitfor != MNT_LAZY)
4263 * Move ourselves to the back of the sync list.
4265 bo = &syncvp->v_bufobj;
4267 vn_syncer_add_to_worklist(bo, syncdelay);
4271 * Walk the list of vnodes pushing all that are dirty and
4272 * not already on the sync list.
4274 if (vfs_busy(mp, MBF_NOWAIT) != 0)
4276 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
4280 save = curthread_pflags_set(TDP_SYNCIO);
4281 vfs_msync(mp, MNT_NOWAIT);
4282 error = VFS_SYNC(mp, MNT_LAZY);
4283 curthread_pflags_restore(save);
4284 vn_finished_write(mp);
4290 * The syncer vnode is no referenced.
4293 sync_inactive(struct vop_inactive_args *ap)
4301 * The syncer vnode is no longer needed and is being decommissioned.
4303 * Modifications to the worklist must be protected by sync_mtx.
4306 sync_reclaim(struct vop_reclaim_args *ap)
4308 struct vnode *vp = ap->a_vp;
4313 mtx_lock(&sync_mtx);
4314 if (vp->v_mount->mnt_syncer == vp)
4315 vp->v_mount->mnt_syncer = NULL;
4316 if (bo->bo_flag & BO_ONWORKLST) {
4317 LIST_REMOVE(bo, bo_synclist);
4318 syncer_worklist_len--;
4320 bo->bo_flag &= ~BO_ONWORKLST;
4322 mtx_unlock(&sync_mtx);
4329 * Check if vnode represents a disk device
4332 vn_isdisk(struct vnode *vp, int *errp)
4336 if (vp->v_type != VCHR) {
4342 if (vp->v_rdev == NULL)
4344 else if (vp->v_rdev->si_devsw == NULL)
4346 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
4352 return (error == 0);
4356 * Common filesystem object access control check routine. Accepts a
4357 * vnode's type, "mode", uid and gid, requested access mode, credentials,
4358 * and optional call-by-reference privused argument allowing vaccess()
4359 * to indicate to the caller whether privilege was used to satisfy the
4360 * request (obsoleted). Returns 0 on success, or an errno on failure.
4363 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
4364 accmode_t accmode, struct ucred *cred, int *privused)
4366 accmode_t dac_granted;
4367 accmode_t priv_granted;
4369 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
4370 ("invalid bit in accmode"));
4371 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
4372 ("VAPPEND without VWRITE"));
4375 * Look for a normal, non-privileged way to access the file/directory
4376 * as requested. If it exists, go with that.
4379 if (privused != NULL)
4384 /* Check the owner. */
4385 if (cred->cr_uid == file_uid) {
4386 dac_granted |= VADMIN;
4387 if (file_mode & S_IXUSR)
4388 dac_granted |= VEXEC;
4389 if (file_mode & S_IRUSR)
4390 dac_granted |= VREAD;
4391 if (file_mode & S_IWUSR)
4392 dac_granted |= (VWRITE | VAPPEND);
4394 if ((accmode & dac_granted) == accmode)
4400 /* Otherwise, check the groups (first match) */
4401 if (groupmember(file_gid, cred)) {
4402 if (file_mode & S_IXGRP)
4403 dac_granted |= VEXEC;
4404 if (file_mode & S_IRGRP)
4405 dac_granted |= VREAD;
4406 if (file_mode & S_IWGRP)
4407 dac_granted |= (VWRITE | VAPPEND);
4409 if ((accmode & dac_granted) == accmode)
4415 /* Otherwise, check everyone else. */
4416 if (file_mode & S_IXOTH)
4417 dac_granted |= VEXEC;
4418 if (file_mode & S_IROTH)
4419 dac_granted |= VREAD;
4420 if (file_mode & S_IWOTH)
4421 dac_granted |= (VWRITE | VAPPEND);
4422 if ((accmode & dac_granted) == accmode)
4427 * Build a privilege mask to determine if the set of privileges
4428 * satisfies the requirements when combined with the granted mask
4429 * from above. For each privilege, if the privilege is required,
4430 * bitwise or the request type onto the priv_granted mask.
4436 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
4437 * requests, instead of PRIV_VFS_EXEC.
4439 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4440 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
4441 priv_granted |= VEXEC;
4444 * Ensure that at least one execute bit is on. Otherwise,
4445 * a privileged user will always succeed, and we don't want
4446 * this to happen unless the file really is executable.
4448 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4449 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
4450 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
4451 priv_granted |= VEXEC;
4454 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
4455 !priv_check_cred(cred, PRIV_VFS_READ, 0))
4456 priv_granted |= VREAD;
4458 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
4459 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
4460 priv_granted |= (VWRITE | VAPPEND);
4462 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
4463 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
4464 priv_granted |= VADMIN;
4466 if ((accmode & (priv_granted | dac_granted)) == accmode) {
4467 /* XXX audit: privilege used */
4468 if (privused != NULL)
4473 return ((accmode & VADMIN) ? EPERM : EACCES);
4477 * Credential check based on process requesting service, and per-attribute
4481 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
4482 struct thread *td, accmode_t accmode)
4486 * Kernel-invoked always succeeds.
4492 * Do not allow privileged processes in jail to directly manipulate
4493 * system attributes.
4495 switch (attrnamespace) {
4496 case EXTATTR_NAMESPACE_SYSTEM:
4497 /* Potentially should be: return (EPERM); */
4498 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
4499 case EXTATTR_NAMESPACE_USER:
4500 return (VOP_ACCESS(vp, accmode, cred, td));
4506 #ifdef DEBUG_VFS_LOCKS
4508 * This only exists to suppress warnings from unlocked specfs accesses. It is
4509 * no longer ok to have an unlocked VFS.
4511 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4512 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4514 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4515 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4516 "Drop into debugger on lock violation");
4518 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4519 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4520 0, "Check for interlock across VOPs");
4522 int vfs_badlock_print = 1; /* Print lock violations. */
4523 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4524 0, "Print lock violations");
4526 int vfs_badlock_vnode = 1; /* Print vnode details on lock violations. */
4527 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_vnode, CTLFLAG_RW, &vfs_badlock_vnode,
4528 0, "Print vnode details on lock violations");
4531 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4532 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4533 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4537 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4541 if (vfs_badlock_backtrace)
4544 if (vfs_badlock_vnode)
4545 vn_printf(vp, "vnode ");
4546 if (vfs_badlock_print)
4547 printf("%s: %p %s\n", str, (void *)vp, msg);
4548 if (vfs_badlock_ddb)
4549 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4553 assert_vi_locked(struct vnode *vp, const char *str)
4556 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4557 vfs_badlock("interlock is not locked but should be", str, vp);
4561 assert_vi_unlocked(struct vnode *vp, const char *str)
4564 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4565 vfs_badlock("interlock is locked but should not be", str, vp);
4569 assert_vop_locked(struct vnode *vp, const char *str)
4573 if (!IGNORE_LOCK(vp)) {
4574 locked = VOP_ISLOCKED(vp);
4575 if (locked == 0 || locked == LK_EXCLOTHER)
4576 vfs_badlock("is not locked but should be", str, vp);
4581 assert_vop_unlocked(struct vnode *vp, const char *str)
4584 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4585 vfs_badlock("is locked but should not be", str, vp);
4589 assert_vop_elocked(struct vnode *vp, const char *str)
4592 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4593 vfs_badlock("is not exclusive locked but should be", str, vp);
4595 #endif /* DEBUG_VFS_LOCKS */
4598 vop_rename_fail(struct vop_rename_args *ap)
4601 if (ap->a_tvp != NULL)
4603 if (ap->a_tdvp == ap->a_tvp)
4612 vop_rename_pre(void *ap)
4614 struct vop_rename_args *a = ap;
4616 #ifdef DEBUG_VFS_LOCKS
4618 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4619 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4620 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4621 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4623 /* Check the source (from). */
4624 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4625 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4626 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4627 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4628 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4630 /* Check the target. */
4632 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4633 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4635 if (a->a_tdvp != a->a_fdvp)
4637 if (a->a_tvp != a->a_fvp)
4644 #ifdef DEBUG_VFS_LOCKS
4646 vop_strategy_pre(void *ap)
4648 struct vop_strategy_args *a;
4655 * Cluster ops lock their component buffers but not the IO container.
4657 if ((bp->b_flags & B_CLUSTER) != 0)
4660 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4661 if (vfs_badlock_print)
4663 "VOP_STRATEGY: bp is not locked but should be\n");
4664 if (vfs_badlock_ddb)
4665 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4670 vop_lock_pre(void *ap)
4672 struct vop_lock1_args *a = ap;
4674 if ((a->a_flags & LK_INTERLOCK) == 0)
4675 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4677 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4681 vop_lock_post(void *ap, int rc)
4683 struct vop_lock1_args *a = ap;
4685 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4686 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4687 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4691 vop_unlock_pre(void *ap)
4693 struct vop_unlock_args *a = ap;
4695 if (a->a_flags & LK_INTERLOCK)
4696 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4697 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4701 vop_unlock_post(void *ap, int rc)
4703 struct vop_unlock_args *a = ap;
4705 if (a->a_flags & LK_INTERLOCK)
4706 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4711 vop_create_post(void *ap, int rc)
4713 struct vop_create_args *a = ap;
4716 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4720 vop_deleteextattr_post(void *ap, int rc)
4722 struct vop_deleteextattr_args *a = ap;
4725 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4729 vop_link_post(void *ap, int rc)
4731 struct vop_link_args *a = ap;
4734 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4735 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4740 vop_mkdir_post(void *ap, int rc)
4742 struct vop_mkdir_args *a = ap;
4745 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4749 vop_mknod_post(void *ap, int rc)
4751 struct vop_mknod_args *a = ap;
4754 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4758 vop_reclaim_post(void *ap, int rc)
4760 struct vop_reclaim_args *a = ap;
4763 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE);
4767 vop_remove_post(void *ap, int rc)
4769 struct vop_remove_args *a = ap;
4772 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4773 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4778 vop_rename_post(void *ap, int rc)
4780 struct vop_rename_args *a = ap;
4785 if (a->a_fdvp == a->a_tdvp) {
4786 if (a->a_tvp != NULL && a->a_tvp->v_type == VDIR)
4788 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
4789 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
4791 hint |= NOTE_EXTEND;
4792 if (a->a_fvp->v_type == VDIR)
4794 VFS_KNOTE_UNLOCKED(a->a_fdvp, hint);
4796 if (a->a_fvp->v_type == VDIR && a->a_tvp != NULL &&
4797 a->a_tvp->v_type == VDIR)
4799 VFS_KNOTE_UNLOCKED(a->a_tdvp, hint);
4802 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4804 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4806 if (a->a_tdvp != a->a_fdvp)
4808 if (a->a_tvp != a->a_fvp)
4816 vop_rmdir_post(void *ap, int rc)
4818 struct vop_rmdir_args *a = ap;
4821 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4822 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4827 vop_setattr_post(void *ap, int rc)
4829 struct vop_setattr_args *a = ap;
4832 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4836 vop_setextattr_post(void *ap, int rc)
4838 struct vop_setextattr_args *a = ap;
4841 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4845 vop_symlink_post(void *ap, int rc)
4847 struct vop_symlink_args *a = ap;
4850 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4854 vop_open_post(void *ap, int rc)
4856 struct vop_open_args *a = ap;
4859 VFS_KNOTE_LOCKED(a->a_vp, NOTE_OPEN);
4863 vop_close_post(void *ap, int rc)
4865 struct vop_close_args *a = ap;
4867 if (!rc && (a->a_cred != NOCRED || /* filter out revokes */
4868 (a->a_vp->v_iflag & VI_DOOMED) == 0)) {
4869 VFS_KNOTE_LOCKED(a->a_vp, (a->a_fflag & FWRITE) != 0 ?
4870 NOTE_CLOSE_WRITE : NOTE_CLOSE);
4875 vop_read_post(void *ap, int rc)
4877 struct vop_read_args *a = ap;
4880 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
4884 vop_readdir_post(void *ap, int rc)
4886 struct vop_readdir_args *a = ap;
4889 VFS_KNOTE_LOCKED(a->a_vp, NOTE_READ);
4892 static struct knlist fs_knlist;
4895 vfs_event_init(void *arg)
4897 knlist_init_mtx(&fs_knlist, NULL);
4899 /* XXX - correct order? */
4900 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4903 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4906 KNOTE_UNLOCKED(&fs_knlist, event);
4909 static int filt_fsattach(struct knote *kn);
4910 static void filt_fsdetach(struct knote *kn);
4911 static int filt_fsevent(struct knote *kn, long hint);
4913 struct filterops fs_filtops = {
4915 .f_attach = filt_fsattach,
4916 .f_detach = filt_fsdetach,
4917 .f_event = filt_fsevent
4921 filt_fsattach(struct knote *kn)
4924 kn->kn_flags |= EV_CLEAR;
4925 knlist_add(&fs_knlist, kn, 0);
4930 filt_fsdetach(struct knote *kn)
4933 knlist_remove(&fs_knlist, kn, 0);
4937 filt_fsevent(struct knote *kn, long hint)
4940 kn->kn_fflags |= hint;
4941 return (kn->kn_fflags != 0);
4945 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4951 error = SYSCTL_IN(req, &vc, sizeof(vc));
4954 if (vc.vc_vers != VFS_CTL_VERS1)
4956 mp = vfs_getvfs(&vc.vc_fsid);
4959 /* ensure that a specific sysctl goes to the right filesystem. */
4960 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4961 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4965 VCTLTOREQ(&vc, req);
4966 error = VFS_SYSCTL(mp, vc.vc_op, req);
4971 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4972 NULL, 0, sysctl_vfs_ctl, "",
4976 * Function to initialize a va_filerev field sensibly.
4977 * XXX: Wouldn't a random number make a lot more sense ??
4980 init_va_filerev(void)
4985 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4988 static int filt_vfsread(struct knote *kn, long hint);
4989 static int filt_vfswrite(struct knote *kn, long hint);
4990 static int filt_vfsvnode(struct knote *kn, long hint);
4991 static void filt_vfsdetach(struct knote *kn);
4992 static struct filterops vfsread_filtops = {
4994 .f_detach = filt_vfsdetach,
4995 .f_event = filt_vfsread
4997 static struct filterops vfswrite_filtops = {
4999 .f_detach = filt_vfsdetach,
5000 .f_event = filt_vfswrite
5002 static struct filterops vfsvnode_filtops = {
5004 .f_detach = filt_vfsdetach,
5005 .f_event = filt_vfsvnode
5009 vfs_knllock(void *arg)
5011 struct vnode *vp = arg;
5013 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
5017 vfs_knlunlock(void *arg)
5019 struct vnode *vp = arg;
5025 vfs_knl_assert_locked(void *arg)
5027 #ifdef DEBUG_VFS_LOCKS
5028 struct vnode *vp = arg;
5030 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
5035 vfs_knl_assert_unlocked(void *arg)
5037 #ifdef DEBUG_VFS_LOCKS
5038 struct vnode *vp = arg;
5040 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
5045 vfs_kqfilter(struct vop_kqfilter_args *ap)
5047 struct vnode *vp = ap->a_vp;
5048 struct knote *kn = ap->a_kn;
5051 switch (kn->kn_filter) {
5053 kn->kn_fop = &vfsread_filtops;
5056 kn->kn_fop = &vfswrite_filtops;
5059 kn->kn_fop = &vfsvnode_filtops;
5065 kn->kn_hook = (caddr_t)vp;
5068 if (vp->v_pollinfo == NULL)
5070 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
5072 knlist_add(knl, kn, 0);
5078 * Detach knote from vnode
5081 filt_vfsdetach(struct knote *kn)
5083 struct vnode *vp = (struct vnode *)kn->kn_hook;
5085 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
5086 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
5092 filt_vfsread(struct knote *kn, long hint)
5094 struct vnode *vp = (struct vnode *)kn->kn_hook;
5099 * filesystem is gone, so set the EOF flag and schedule
5100 * the knote for deletion.
5102 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5104 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5109 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
5113 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
5114 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
5121 filt_vfswrite(struct knote *kn, long hint)
5123 struct vnode *vp = (struct vnode *)kn->kn_hook;
5128 * filesystem is gone, so set the EOF flag and schedule
5129 * the knote for deletion.
5131 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
5132 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
5140 filt_vfsvnode(struct knote *kn, long hint)
5142 struct vnode *vp = (struct vnode *)kn->kn_hook;
5146 if (kn->kn_sfflags & hint)
5147 kn->kn_fflags |= hint;
5148 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
5149 kn->kn_flags |= EV_EOF;
5153 res = (kn->kn_fflags != 0);
5159 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
5163 if (dp->d_reclen > ap->a_uio->uio_resid)
5164 return (ENAMETOOLONG);
5165 error = uiomove(dp, dp->d_reclen, ap->a_uio);
5167 if (ap->a_ncookies != NULL) {
5168 if (ap->a_cookies != NULL)
5169 free(ap->a_cookies, M_TEMP);
5170 ap->a_cookies = NULL;
5171 *ap->a_ncookies = 0;
5175 if (ap->a_ncookies == NULL)
5178 KASSERT(ap->a_cookies,
5179 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
5181 *ap->a_cookies = realloc(*ap->a_cookies,
5182 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
5183 (*ap->a_cookies)[*ap->a_ncookies] = off;
5184 *ap->a_ncookies += 1;
5189 * Mark for update the access time of the file if the filesystem
5190 * supports VOP_MARKATIME. This functionality is used by execve and
5191 * mmap, so we want to avoid the I/O implied by directly setting
5192 * va_atime for the sake of efficiency.
5195 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
5200 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
5201 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
5202 (void)VOP_MARKATIME(vp);
5206 * The purpose of this routine is to remove granularity from accmode_t,
5207 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
5208 * VADMIN and VAPPEND.
5210 * If it returns 0, the caller is supposed to continue with the usual
5211 * access checks using 'accmode' as modified by this routine. If it
5212 * returns nonzero value, the caller is supposed to return that value
5215 * Note that after this routine runs, accmode may be zero.
5218 vfs_unixify_accmode(accmode_t *accmode)
5221 * There is no way to specify explicit "deny" rule using
5222 * file mode or POSIX.1e ACLs.
5224 if (*accmode & VEXPLICIT_DENY) {
5230 * None of these can be translated into usual access bits.
5231 * Also, the common case for NFSv4 ACLs is to not contain
5232 * either of these bits. Caller should check for VWRITE
5233 * on the containing directory instead.
5235 if (*accmode & (VDELETE_CHILD | VDELETE))
5238 if (*accmode & VADMIN_PERMS) {
5239 *accmode &= ~VADMIN_PERMS;
5244 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
5245 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
5247 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
5253 * These are helper functions for filesystems to traverse all
5254 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
5256 * This interface replaces MNT_VNODE_FOREACH.
5259 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
5262 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
5267 kern_yield(PRI_USER);
5269 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5270 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
5271 while (vp != NULL && (vp->v_type == VMARKER ||
5272 (vp->v_iflag & VI_DOOMED) != 0))
5273 vp = TAILQ_NEXT(vp, v_nmntvnodes);
5275 /* Check if we are done */
5277 __mnt_vnode_markerfree_all(mvp, mp);
5278 /* MNT_IUNLOCK(mp); -- done in above function */
5279 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
5282 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
5283 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
5290 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
5294 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
5297 (*mvp)->v_type = VMARKER;
5299 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
5300 while (vp != NULL && (vp->v_type == VMARKER ||
5301 (vp->v_iflag & VI_DOOMED) != 0))
5302 vp = TAILQ_NEXT(vp, v_nmntvnodes);
5304 /* Check if we are done */
5308 free(*mvp, M_VNODE_MARKER);
5312 (*mvp)->v_mount = mp;
5313 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
5321 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
5329 mtx_assert(MNT_MTX(mp), MA_OWNED);
5331 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5332 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
5335 free(*mvp, M_VNODE_MARKER);
5340 * These are helper functions for filesystems to traverse their
5341 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
5344 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
5347 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5352 free(*mvp, M_VNODE_MARKER);
5356 static struct vnode *
5357 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
5359 struct vnode *vp, *nvp;
5361 mtx_assert(&mp->mnt_listmtx, MA_OWNED);
5362 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
5364 vp = TAILQ_NEXT(*mvp, v_actfreelist);
5365 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
5366 while (vp != NULL) {
5367 if (vp->v_type == VMARKER) {
5368 vp = TAILQ_NEXT(vp, v_actfreelist);
5371 if (!VI_TRYLOCK(vp)) {
5372 if (mp_ncpus == 1 || should_yield()) {
5373 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
5374 mtx_unlock(&mp->mnt_listmtx);
5376 mtx_lock(&mp->mnt_listmtx);
5381 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
5382 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
5383 ("alien vnode on the active list %p %p", vp, mp));
5384 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
5386 nvp = TAILQ_NEXT(vp, v_actfreelist);
5391 /* Check if we are done */
5393 mtx_unlock(&mp->mnt_listmtx);
5394 mnt_vnode_markerfree_active(mvp, mp);
5397 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
5398 mtx_unlock(&mp->mnt_listmtx);
5399 ASSERT_VI_LOCKED(vp, "active iter");
5400 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
5405 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
5409 kern_yield(PRI_USER);
5410 mtx_lock(&mp->mnt_listmtx);
5411 return (mnt_vnode_next_active(mvp, mp));
5415 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
5419 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
5423 (*mvp)->v_type = VMARKER;
5424 (*mvp)->v_mount = mp;
5426 mtx_lock(&mp->mnt_listmtx);
5427 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
5429 mtx_unlock(&mp->mnt_listmtx);
5430 mnt_vnode_markerfree_active(mvp, mp);
5433 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
5434 return (mnt_vnode_next_active(mvp, mp));
5438 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
5444 mtx_lock(&mp->mnt_listmtx);
5445 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
5446 mtx_unlock(&mp->mnt_listmtx);
5447 mnt_vnode_markerfree_active(mvp, mp);