2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
38 * External virtual filesystem routines
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include "opt_compat.h"
46 #include "opt_watchdog.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/condvar.h>
54 #include <sys/dirent.h>
55 #include <sys/event.h>
56 #include <sys/eventhandler.h>
57 #include <sys/extattr.h>
59 #include <sys/fcntl.h>
62 #include <sys/kernel.h>
63 #include <sys/kthread.h>
64 #include <sys/lockf.h>
65 #include <sys/malloc.h>
66 #include <sys/mount.h>
67 #include <sys/namei.h>
68 #include <sys/pctrie.h>
70 #include <sys/reboot.h>
71 #include <sys/rwlock.h>
72 #include <sys/sched.h>
73 #include <sys/sleepqueue.h>
76 #include <sys/sysctl.h>
77 #include <sys/syslog.h>
78 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/watchdog.h>
82 #include <machine/stdarg.h>
84 #include <security/mac/mac_framework.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_extern.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_kern.h>
99 static void delmntque(struct vnode *vp);
100 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
101 int slpflag, int slptimeo);
102 static void syncer_shutdown(void *arg, int howto);
103 static int vtryrecycle(struct vnode *vp);
104 static void v_incr_usecount(struct vnode *);
105 static void v_decr_usecount(struct vnode *);
106 static void v_decr_useonly(struct vnode *);
107 static void v_upgrade_usecount(struct vnode *);
108 static void vnlru_free(int);
109 static void vgonel(struct vnode *);
110 static void vfs_knllock(void *arg);
111 static void vfs_knlunlock(void *arg);
112 static void vfs_knl_assert_locked(void *arg);
113 static void vfs_knl_assert_unlocked(void *arg);
114 static void destroy_vpollinfo(struct vpollinfo *vi);
117 * Number of vnodes in existence. Increased whenever getnewvnode()
118 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
120 static unsigned long numvnodes;
122 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
123 "Number of vnodes in existence");
125 static u_long vnodes_created;
126 SYSCTL_ULONG(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
127 0, "Number of vnodes created by getnewvnode");
130 * Conversion tables for conversion from vnode types to inode formats
133 enum vtype iftovt_tab[16] = {
134 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
135 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
137 int vttoif_tab[10] = {
138 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
139 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
143 * List of vnodes that are ready for recycling.
145 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
148 * Free vnode target. Free vnodes may simply be files which have been stat'd
149 * but not read. This is somewhat common, and a small cache of such files
150 * should be kept to avoid recreation costs.
152 static u_long wantfreevnodes;
153 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
154 /* Number of vnodes in the free list. */
155 static u_long freevnodes;
156 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
157 "Number of vnodes in the free list");
159 static int vlru_allow_cache_src;
160 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
161 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
163 static u_long recycles_count;
164 SYSCTL_ULONG(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 0,
165 "Number of vnodes recycled to avoid exceding kern.maxvnodes");
168 * Various variables used for debugging the new implementation of
170 * XXX these are probably of (very) limited utility now.
172 static int reassignbufcalls;
173 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
174 "Number of calls to reassignbuf");
176 static u_long free_owe_inact;
177 SYSCTL_ULONG(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact, 0,
178 "Number of times free vnodes kept on active list due to VFS "
179 "owing inactivation");
182 * Cache for the mount type id assigned to NFS. This is used for
183 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
185 int nfs_mount_type = -1;
187 /* To keep more than one thread at a time from running vfs_getnewfsid */
188 static struct mtx mntid_mtx;
191 * Lock for any access to the following:
196 static struct mtx vnode_free_list_mtx;
198 /* Publicly exported FS */
199 struct nfs_public nfs_pub;
201 static uma_zone_t buf_trie_zone;
203 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
204 static uma_zone_t vnode_zone;
205 static uma_zone_t vnodepoll_zone;
208 * The workitem queue.
210 * It is useful to delay writes of file data and filesystem metadata
211 * for tens of seconds so that quickly created and deleted files need
212 * not waste disk bandwidth being created and removed. To realize this,
213 * we append vnodes to a "workitem" queue. When running with a soft
214 * updates implementation, most pending metadata dependencies should
215 * not wait for more than a few seconds. Thus, mounted on block devices
216 * are delayed only about a half the time that file data is delayed.
217 * Similarly, directory updates are more critical, so are only delayed
218 * about a third the time that file data is delayed. Thus, there are
219 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
220 * one each second (driven off the filesystem syncer process). The
221 * syncer_delayno variable indicates the next queue that is to be processed.
222 * Items that need to be processed soon are placed in this queue:
224 * syncer_workitem_pending[syncer_delayno]
226 * A delay of fifteen seconds is done by placing the request fifteen
227 * entries later in the queue:
229 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
232 static int syncer_delayno;
233 static long syncer_mask;
234 LIST_HEAD(synclist, bufobj);
235 static struct synclist *syncer_workitem_pending;
237 * The sync_mtx protects:
242 * syncer_workitem_pending
243 * syncer_worklist_len
246 static struct mtx sync_mtx;
247 static struct cv sync_wakeup;
249 #define SYNCER_MAXDELAY 32
250 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
251 static int syncdelay = 30; /* max time to delay syncing data */
252 static int filedelay = 30; /* time to delay syncing files */
253 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
254 "Time to delay syncing files (in seconds)");
255 static int dirdelay = 29; /* time to delay syncing directories */
256 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
257 "Time to delay syncing directories (in seconds)");
258 static int metadelay = 28; /* time to delay syncing metadata */
259 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
260 "Time to delay syncing metadata (in seconds)");
261 static int rushjob; /* number of slots to run ASAP */
262 static int stat_rush_requests; /* number of times I/O speeded up */
263 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
264 "Number of times I/O speeded up (rush requests)");
267 * When shutting down the syncer, run it at four times normal speed.
269 #define SYNCER_SHUTDOWN_SPEEDUP 4
270 static int sync_vnode_count;
271 static int syncer_worklist_len;
272 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
276 * Number of vnodes we want to exist at any one time. This is mostly used
277 * to size hash tables in vnode-related code. It is normally not used in
278 * getnewvnode(), as wantfreevnodes is normally nonzero.)
280 * XXX desiredvnodes is historical cruft and should not exist.
285 sysctl_update_desiredvnodes(SYSCTL_HANDLER_ARGS)
287 int error, old_desiredvnodes;
289 old_desiredvnodes = desiredvnodes;
290 if ((error = sysctl_handle_int(oidp, arg1, arg2, req)) != 0)
292 if (old_desiredvnodes != desiredvnodes) {
293 vfs_hash_changesize(desiredvnodes);
294 cache_changesize(desiredvnodes);
299 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
300 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &desiredvnodes, 0,
301 sysctl_update_desiredvnodes, "I", "Maximum number of vnodes");
302 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
303 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
304 static int vnlru_nowhere;
305 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
306 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
308 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
312 * Support for the bufobj clean & dirty pctrie.
315 buf_trie_alloc(struct pctrie *ptree)
318 return uma_zalloc(buf_trie_zone, M_NOWAIT);
322 buf_trie_free(struct pctrie *ptree, void *node)
325 uma_zfree(buf_trie_zone, node);
327 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
330 * Initialize the vnode management data structures.
332 * Reevaluate the following cap on the number of vnodes after the physical
333 * memory size exceeds 512GB. In the limit, as the physical memory size
334 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
336 #ifndef MAXVNODES_MAX
337 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
341 * Initialize a vnode as it first enters the zone.
344 vnode_init(void *mem, int size, int flags)
354 vp->v_vnlock = &vp->v_lock;
355 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
357 * By default, don't allow shared locks unless filesystems opt-in.
359 lockinit(vp->v_vnlock, PVFS, "vnode", VLKTIMEOUT,
360 LK_NOSHARE | LK_IS_VNODE);
366 rw_init(BO_LOCKPTR(bo), "bufobj interlock");
368 TAILQ_INIT(&bo->bo_clean.bv_hd);
369 TAILQ_INIT(&bo->bo_dirty.bv_hd);
371 * Initialize namecache.
373 LIST_INIT(&vp->v_cache_src);
374 TAILQ_INIT(&vp->v_cache_dst);
376 * Initialize rangelocks.
378 rangelock_init(&vp->v_rl);
383 * Free a vnode when it is cleared from the zone.
386 vnode_fini(void *mem, int size)
392 rangelock_destroy(&vp->v_rl);
393 lockdestroy(vp->v_vnlock);
394 mtx_destroy(&vp->v_interlock);
396 rw_destroy(BO_LOCKPTR(bo));
400 vntblinit(void *dummy __unused)
403 int physvnodes, virtvnodes;
406 * Desiredvnodes is a function of the physical memory size and the
407 * kernel's heap size. Generally speaking, it scales with the
408 * physical memory size. The ratio of desiredvnodes to physical pages
409 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
410 * marginal ratio of desiredvnodes to physical pages is one to
411 * sixteen. However, desiredvnodes is limited by the kernel's heap
412 * size. The memory required by desiredvnodes vnodes and vm objects
413 * may not exceed one seventh of the kernel's heap size.
415 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
416 cnt.v_page_count) / 16;
417 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
418 sizeof(struct vnode)));
419 desiredvnodes = min(physvnodes, virtvnodes);
420 if (desiredvnodes > MAXVNODES_MAX) {
422 printf("Reducing kern.maxvnodes %d -> %d\n",
423 desiredvnodes, MAXVNODES_MAX);
424 desiredvnodes = MAXVNODES_MAX;
426 wantfreevnodes = desiredvnodes / 4;
427 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
428 TAILQ_INIT(&vnode_free_list);
429 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
430 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
431 vnode_init, vnode_fini, UMA_ALIGN_PTR, 0);
432 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
433 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
435 * Preallocate enough nodes to support one-per buf so that
436 * we can not fail an insert. reassignbuf() callers can not
437 * tolerate the insertion failure.
439 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
440 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
441 UMA_ZONE_NOFREE | UMA_ZONE_VM);
442 uma_prealloc(buf_trie_zone, nbuf);
444 * Initialize the filesystem syncer.
446 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
448 syncer_maxdelay = syncer_mask + 1;
449 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
450 cv_init(&sync_wakeup, "syncer");
451 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
455 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
459 * Mark a mount point as busy. Used to synchronize access and to delay
460 * unmounting. Eventually, mountlist_mtx is not released on failure.
462 * vfs_busy() is a custom lock, it can block the caller.
463 * vfs_busy() only sleeps if the unmount is active on the mount point.
464 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
465 * vnode belonging to mp.
467 * Lookup uses vfs_busy() to traverse mount points.
469 * / vnode lock A / vnode lock (/var) D
470 * /var vnode lock B /log vnode lock(/var/log) E
471 * vfs_busy lock C vfs_busy lock F
473 * Within each file system, the lock order is C->A->B and F->D->E.
475 * When traversing across mounts, the system follows that lock order:
481 * The lookup() process for namei("/var") illustrates the process:
482 * VOP_LOOKUP() obtains B while A is held
483 * vfs_busy() obtains a shared lock on F while A and B are held
484 * vput() releases lock on B
485 * vput() releases lock on A
486 * VFS_ROOT() obtains lock on D while shared lock on F is held
487 * vfs_unbusy() releases shared lock on F
488 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
489 * Attempt to lock A (instead of vp_crossmp) while D is held would
490 * violate the global order, causing deadlocks.
492 * dounmount() locks B while F is drained.
495 vfs_busy(struct mount *mp, int flags)
498 MPASS((flags & ~MBF_MASK) == 0);
499 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
504 * If mount point is currenly being unmounted, sleep until the
505 * mount point fate is decided. If thread doing the unmounting fails,
506 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
507 * that this mount point has survived the unmount attempt and vfs_busy
508 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
509 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
510 * about to be really destroyed. vfs_busy needs to release its
511 * reference on the mount point in this case and return with ENOENT,
512 * telling the caller that mount mount it tried to busy is no longer
515 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
516 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
519 CTR1(KTR_VFS, "%s: failed busying before sleeping",
523 if (flags & MBF_MNTLSTLOCK)
524 mtx_unlock(&mountlist_mtx);
525 mp->mnt_kern_flag |= MNTK_MWAIT;
526 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
527 if (flags & MBF_MNTLSTLOCK)
528 mtx_lock(&mountlist_mtx);
531 if (flags & MBF_MNTLSTLOCK)
532 mtx_unlock(&mountlist_mtx);
539 * Free a busy filesystem.
542 vfs_unbusy(struct mount *mp)
545 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
548 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
550 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
551 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
552 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
553 mp->mnt_kern_flag &= ~MNTK_DRAINING;
554 wakeup(&mp->mnt_lockref);
560 * Lookup a mount point by filesystem identifier.
563 vfs_getvfs(fsid_t *fsid)
567 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
568 mtx_lock(&mountlist_mtx);
569 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
570 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
571 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
573 mtx_unlock(&mountlist_mtx);
577 mtx_unlock(&mountlist_mtx);
578 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
579 return ((struct mount *) 0);
583 * Lookup a mount point by filesystem identifier, busying it before
586 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
587 * cache for popular filesystem identifiers. The cache is lockess, using
588 * the fact that struct mount's are never freed. In worst case we may
589 * get pointer to unmounted or even different filesystem, so we have to
590 * check what we got, and go slow way if so.
593 vfs_busyfs(fsid_t *fsid)
595 #define FSID_CACHE_SIZE 256
596 typedef struct mount * volatile vmp_t;
597 static vmp_t cache[FSID_CACHE_SIZE];
602 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
603 hash = fsid->val[0] ^ fsid->val[1];
604 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
607 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
608 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
610 if (vfs_busy(mp, 0) != 0) {
614 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
615 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
621 mtx_lock(&mountlist_mtx);
622 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
623 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
624 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
625 error = vfs_busy(mp, MBF_MNTLSTLOCK);
628 mtx_unlock(&mountlist_mtx);
635 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
636 mtx_unlock(&mountlist_mtx);
637 return ((struct mount *) 0);
641 * Check if a user can access privileged mount options.
644 vfs_suser(struct mount *mp, struct thread *td)
649 * If the thread is jailed, but this is not a jail-friendly file
650 * system, deny immediately.
652 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
656 * If the file system was mounted outside the jail of the calling
657 * thread, deny immediately.
659 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
663 * If file system supports delegated administration, we don't check
664 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
665 * by the file system itself.
666 * If this is not the user that did original mount, we check for
667 * the PRIV_VFS_MOUNT_OWNER privilege.
669 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
670 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
671 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
678 * Get a new unique fsid. Try to make its val[0] unique, since this value
679 * will be used to create fake device numbers for stat(). Also try (but
680 * not so hard) make its val[0] unique mod 2^16, since some emulators only
681 * support 16-bit device numbers. We end up with unique val[0]'s for the
682 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
684 * Keep in mind that several mounts may be running in parallel. Starting
685 * the search one past where the previous search terminated is both a
686 * micro-optimization and a defense against returning the same fsid to
690 vfs_getnewfsid(struct mount *mp)
692 static uint16_t mntid_base;
697 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
698 mtx_lock(&mntid_mtx);
699 mtype = mp->mnt_vfc->vfc_typenum;
700 tfsid.val[1] = mtype;
701 mtype = (mtype & 0xFF) << 24;
703 tfsid.val[0] = makedev(255,
704 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
706 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
710 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
711 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
712 mtx_unlock(&mntid_mtx);
716 * Knob to control the precision of file timestamps:
718 * 0 = seconds only; nanoseconds zeroed.
719 * 1 = seconds and nanoseconds, accurate within 1/HZ.
720 * 2 = seconds and nanoseconds, truncated to microseconds.
721 * >=3 = seconds and nanoseconds, maximum precision.
723 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
725 static int timestamp_precision = TSP_USEC;
726 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
727 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
728 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
729 "3+: sec + ns (max. precision))");
732 * Get a current timestamp.
735 vfs_timestamp(struct timespec *tsp)
739 switch (timestamp_precision) {
741 tsp->tv_sec = time_second;
749 TIMEVAL_TO_TIMESPEC(&tv, tsp);
759 * Set vnode attributes to VNOVAL
762 vattr_null(struct vattr *vap)
766 vap->va_size = VNOVAL;
767 vap->va_bytes = VNOVAL;
768 vap->va_mode = VNOVAL;
769 vap->va_nlink = VNOVAL;
770 vap->va_uid = VNOVAL;
771 vap->va_gid = VNOVAL;
772 vap->va_fsid = VNOVAL;
773 vap->va_fileid = VNOVAL;
774 vap->va_blocksize = VNOVAL;
775 vap->va_rdev = VNOVAL;
776 vap->va_atime.tv_sec = VNOVAL;
777 vap->va_atime.tv_nsec = VNOVAL;
778 vap->va_mtime.tv_sec = VNOVAL;
779 vap->va_mtime.tv_nsec = VNOVAL;
780 vap->va_ctime.tv_sec = VNOVAL;
781 vap->va_ctime.tv_nsec = VNOVAL;
782 vap->va_birthtime.tv_sec = VNOVAL;
783 vap->va_birthtime.tv_nsec = VNOVAL;
784 vap->va_flags = VNOVAL;
785 vap->va_gen = VNOVAL;
790 * This routine is called when we have too many vnodes. It attempts
791 * to free <count> vnodes and will potentially free vnodes that still
792 * have VM backing store (VM backing store is typically the cause
793 * of a vnode blowout so we want to do this). Therefore, this operation
794 * is not considered cheap.
796 * A number of conditions may prevent a vnode from being reclaimed.
797 * the buffer cache may have references on the vnode, a directory
798 * vnode may still have references due to the namei cache representing
799 * underlying files, or the vnode may be in active use. It is not
800 * desireable to reuse such vnodes. These conditions may cause the
801 * number of vnodes to reach some minimum value regardless of what
802 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
805 vlrureclaim(struct mount *mp)
814 * Calculate the trigger point, don't allow user
815 * screwups to blow us up. This prevents us from
816 * recycling vnodes with lots of resident pages. We
817 * aren't trying to free memory, we are trying to
820 usevnodes = desiredvnodes;
823 trigger = cnt.v_page_count * 2 / usevnodes;
825 vn_start_write(NULL, &mp, V_WAIT);
827 count = mp->mnt_nvnodelistsize / 10 + 1;
829 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
830 while (vp != NULL && vp->v_type == VMARKER)
831 vp = TAILQ_NEXT(vp, v_nmntvnodes);
834 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
835 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
840 * If it's been deconstructed already, it's still
841 * referenced, or it exceeds the trigger, skip it.
843 if (vp->v_usecount ||
844 (!vlru_allow_cache_src &&
845 !LIST_EMPTY(&(vp)->v_cache_src)) ||
846 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
847 vp->v_object->resident_page_count > trigger)) {
853 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
855 goto next_iter_mntunlocked;
859 * v_usecount may have been bumped after VOP_LOCK() dropped
860 * the vnode interlock and before it was locked again.
862 * It is not necessary to recheck VI_DOOMED because it can
863 * only be set by another thread that holds both the vnode
864 * lock and vnode interlock. If another thread has the
865 * vnode lock before we get to VOP_LOCK() and obtains the
866 * vnode interlock after VOP_LOCK() drops the vnode
867 * interlock, the other thread will be unable to drop the
868 * vnode lock before our VOP_LOCK() call fails.
870 if (vp->v_usecount ||
871 (!vlru_allow_cache_src &&
872 !LIST_EMPTY(&(vp)->v_cache_src)) ||
873 (vp->v_object != NULL &&
874 vp->v_object->resident_page_count > trigger)) {
875 VOP_UNLOCK(vp, LK_INTERLOCK);
877 goto next_iter_mntunlocked;
879 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
880 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
881 atomic_add_long(&recycles_count, 1);
886 next_iter_mntunlocked:
895 kern_yield(PRI_USER);
900 vn_finished_write(mp);
905 * Attempt to keep the free list at wantfreevnodes length.
908 vnlru_free(int count)
912 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
913 for (; count > 0; count--) {
914 vp = TAILQ_FIRST(&vnode_free_list);
916 * The list can be modified while the free_list_mtx
917 * has been dropped and vp could be NULL here.
921 VNASSERT(vp->v_op != NULL, vp,
922 ("vnlru_free: vnode already reclaimed."));
923 KASSERT((vp->v_iflag & VI_FREE) != 0,
924 ("Removing vnode not on freelist"));
925 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
926 ("Mangling active vnode"));
927 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
929 * Don't recycle if we can't get the interlock.
931 if (!VI_TRYLOCK(vp)) {
932 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
935 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
936 vp, ("vp inconsistent on freelist"));
939 * The clear of VI_FREE prevents activation of the
940 * vnode. There is no sense in putting the vnode on
941 * the mount point active list, only to remove it
942 * later during recycling. Inline the relevant part
943 * of vholdl(), to avoid triggering assertions or
947 vp->v_iflag &= ~VI_FREE;
950 mtx_unlock(&vnode_free_list_mtx);
954 * If the recycled succeeded this vdrop will actually free
955 * the vnode. If not it will simply place it back on
959 mtx_lock(&vnode_free_list_mtx);
963 * Attempt to recycle vnodes in a context that is always safe to block.
964 * Calling vlrurecycle() from the bowels of filesystem code has some
965 * interesting deadlock problems.
967 static struct proc *vnlruproc;
968 static int vnlruproc_sig;
973 struct mount *mp, *nmp;
975 struct proc *p = vnlruproc;
977 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
981 kproc_suspend_check(p);
982 mtx_lock(&vnode_free_list_mtx);
983 if (freevnodes > wantfreevnodes)
984 vnlru_free(freevnodes - wantfreevnodes);
985 if (numvnodes <= desiredvnodes * 9 / 10) {
987 wakeup(&vnlruproc_sig);
988 msleep(vnlruproc, &vnode_free_list_mtx,
989 PVFS|PDROP, "vlruwt", hz);
992 mtx_unlock(&vnode_free_list_mtx);
994 mtx_lock(&mountlist_mtx);
995 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
996 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
997 nmp = TAILQ_NEXT(mp, mnt_list);
1000 done += vlrureclaim(mp);
1001 mtx_lock(&mountlist_mtx);
1002 nmp = TAILQ_NEXT(mp, mnt_list);
1005 mtx_unlock(&mountlist_mtx);
1008 /* These messages are temporary debugging aids */
1009 if (vnlru_nowhere < 5)
1010 printf("vnlru process getting nowhere..\n");
1011 else if (vnlru_nowhere == 5)
1012 printf("vnlru process messages stopped.\n");
1015 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
1017 kern_yield(PRI_USER);
1021 static struct kproc_desc vnlru_kp = {
1026 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
1030 * Routines having to do with the management of the vnode table.
1034 * Try to recycle a freed vnode. We abort if anyone picks up a reference
1035 * before we actually vgone(). This function must be called with the vnode
1036 * held to prevent the vnode from being returned to the free list midway
1040 vtryrecycle(struct vnode *vp)
1044 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
1045 VNASSERT(vp->v_holdcnt, vp,
1046 ("vtryrecycle: Recycling vp %p without a reference.", vp));
1048 * This vnode may found and locked via some other list, if so we
1049 * can't recycle it yet.
1051 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
1053 "%s: impossible to recycle, vp %p lock is already held",
1055 return (EWOULDBLOCK);
1058 * Don't recycle if its filesystem is being suspended.
1060 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1063 "%s: impossible to recycle, cannot start the write for %p",
1068 * If we got this far, we need to acquire the interlock and see if
1069 * anyone picked up this vnode from another list. If not, we will
1070 * mark it with DOOMED via vgonel() so that anyone who does find it
1071 * will skip over it.
1074 if (vp->v_usecount) {
1075 VOP_UNLOCK(vp, LK_INTERLOCK);
1076 vn_finished_write(vnmp);
1078 "%s: impossible to recycle, %p is already referenced",
1082 if ((vp->v_iflag & VI_DOOMED) == 0) {
1083 atomic_add_long(&recycles_count, 1);
1086 VOP_UNLOCK(vp, LK_INTERLOCK);
1087 vn_finished_write(vnmp);
1092 * Wait for available vnodes.
1095 getnewvnode_wait(int suspended)
1098 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1099 if (numvnodes > desiredvnodes) {
1102 * File system is beeing suspended, we cannot risk a
1103 * deadlock here, so allocate new vnode anyway.
1105 if (freevnodes > wantfreevnodes)
1106 vnlru_free(freevnodes - wantfreevnodes);
1109 if (vnlruproc_sig == 0) {
1110 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1113 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1116 return (numvnodes > desiredvnodes ? ENFILE : 0);
1120 getnewvnode_reserve(u_int count)
1125 /* First try to be quick and racy. */
1126 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1127 td->td_vp_reserv += count;
1130 atomic_subtract_long(&numvnodes, count);
1132 mtx_lock(&vnode_free_list_mtx);
1134 if (getnewvnode_wait(0) == 0) {
1137 atomic_add_long(&numvnodes, 1);
1140 mtx_unlock(&vnode_free_list_mtx);
1144 getnewvnode_drop_reserve(void)
1149 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1150 td->td_vp_reserv = 0;
1154 * Return the next vnode from the free list.
1157 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1162 struct lock_object *lo;
1165 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1168 if (td->td_vp_reserv > 0) {
1169 td->td_vp_reserv -= 1;
1172 mtx_lock(&vnode_free_list_mtx);
1174 * Lend our context to reclaim vnodes if they've exceeded the max.
1176 if (freevnodes > wantfreevnodes)
1178 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1180 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1182 mtx_unlock(&vnode_free_list_mtx);
1186 atomic_add_long(&numvnodes, 1);
1187 mtx_unlock(&vnode_free_list_mtx);
1189 atomic_add_long(&vnodes_created, 1);
1190 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK);
1192 * Locks are given the generic name "vnode" when created.
1193 * Follow the historic practice of using the filesystem
1194 * name when they allocated, e.g., "zfs", "ufs", "nfs, etc.
1196 * Locks live in a witness group keyed on their name. Thus,
1197 * when a lock is renamed, it must also move from the witness
1198 * group of its old name to the witness group of its new name.
1200 * The change only needs to be made when the vnode moves
1201 * from one filesystem type to another. We ensure that each
1202 * filesystem use a single static name pointer for its tag so
1203 * that we can compare pointers rather than doing a strcmp().
1205 lo = &vp->v_vnlock->lock_object;
1206 if (lo->lo_name != tag) {
1208 WITNESS_DESTROY(lo);
1209 WITNESS_INIT(lo, tag);
1212 * By default, don't allow shared locks unless filesystems opt-in.
1214 vp->v_vnlock->lock_object.lo_flags |= LK_NOSHARE;
1216 * Finalize various vnode identity bits.
1218 KASSERT(vp->v_object == NULL, ("stale v_object %p", vp));
1219 KASSERT(vp->v_lockf == NULL, ("stale v_lockf %p", vp));
1220 KASSERT(vp->v_pollinfo == NULL, ("stale v_pollinfo %p", vp));
1224 v_incr_usecount(vp);
1225 vp->v_bufobj.bo_ops = &buf_ops_bio;
1228 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1229 mac_vnode_associate_singlelabel(mp, vp);
1230 else if (mp == NULL && vops != &dead_vnodeops)
1231 printf("NULL mp in getnewvnode()\n");
1234 vp->v_bufobj.bo_bsize = mp->mnt_stat.f_iosize;
1235 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1236 vp->v_vflag |= VV_NOKNOTE;
1240 * For the filesystems which do not use vfs_hash_insert(),
1241 * still initialize v_hash to have vfs_hash_index() useful.
1242 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1245 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1252 * Delete from old mount point vnode list, if on one.
1255 delmntque(struct vnode *vp)
1265 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1266 ("Active vnode list size %d > Vnode list size %d",
1267 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1268 active = vp->v_iflag & VI_ACTIVE;
1269 vp->v_iflag &= ~VI_ACTIVE;
1271 mtx_lock(&vnode_free_list_mtx);
1272 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1273 mp->mnt_activevnodelistsize--;
1274 mtx_unlock(&vnode_free_list_mtx);
1278 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1279 ("bad mount point vnode list size"));
1280 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1281 mp->mnt_nvnodelistsize--;
1287 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1291 vp->v_op = &dead_vnodeops;
1297 * Insert into list of vnodes for the new mount point, if available.
1300 insmntque1(struct vnode *vp, struct mount *mp,
1301 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1304 KASSERT(vp->v_mount == NULL,
1305 ("insmntque: vnode already on per mount vnode list"));
1306 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1307 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1310 * We acquire the vnode interlock early to ensure that the
1311 * vnode cannot be recycled by another process releasing a
1312 * holdcnt on it before we get it on both the vnode list
1313 * and the active vnode list. The mount mutex protects only
1314 * manipulation of the vnode list and the vnode freelist
1315 * mutex protects only manipulation of the active vnode list.
1316 * Hence the need to hold the vnode interlock throughout.
1320 if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1321 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1322 mp->mnt_nvnodelistsize == 0)) &&
1323 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1332 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1333 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1334 ("neg mount point vnode list size"));
1335 mp->mnt_nvnodelistsize++;
1336 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1337 ("Activating already active vnode"));
1338 vp->v_iflag |= VI_ACTIVE;
1339 mtx_lock(&vnode_free_list_mtx);
1340 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1341 mp->mnt_activevnodelistsize++;
1342 mtx_unlock(&vnode_free_list_mtx);
1349 insmntque(struct vnode *vp, struct mount *mp)
1352 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1356 * Flush out and invalidate all buffers associated with a bufobj
1357 * Called with the underlying object locked.
1360 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1365 if (flags & V_SAVE) {
1366 error = bufobj_wwait(bo, slpflag, slptimeo);
1371 if (bo->bo_dirty.bv_cnt > 0) {
1373 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1376 * XXX We could save a lock/unlock if this was only
1377 * enabled under INVARIANTS
1380 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1381 panic("vinvalbuf: dirty bufs");
1385 * If you alter this loop please notice that interlock is dropped and
1386 * reacquired in flushbuflist. Special care is needed to ensure that
1387 * no race conditions occur from this.
1390 error = flushbuflist(&bo->bo_clean,
1391 flags, bo, slpflag, slptimeo);
1392 if (error == 0 && !(flags & V_CLEANONLY))
1393 error = flushbuflist(&bo->bo_dirty,
1394 flags, bo, slpflag, slptimeo);
1395 if (error != 0 && error != EAGAIN) {
1399 } while (error != 0);
1402 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1403 * have write I/O in-progress but if there is a VM object then the
1404 * VM object can also have read-I/O in-progress.
1407 bufobj_wwait(bo, 0, 0);
1409 if (bo->bo_object != NULL) {
1410 VM_OBJECT_WLOCK(bo->bo_object);
1411 vm_object_pip_wait(bo->bo_object, "bovlbx");
1412 VM_OBJECT_WUNLOCK(bo->bo_object);
1415 } while (bo->bo_numoutput > 0);
1419 * Destroy the copy in the VM cache, too.
1421 if (bo->bo_object != NULL &&
1422 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1423 VM_OBJECT_WLOCK(bo->bo_object);
1424 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1425 OBJPR_CLEANONLY : 0);
1426 VM_OBJECT_WUNLOCK(bo->bo_object);
1431 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1432 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1433 panic("vinvalbuf: flush failed");
1440 * Flush out and invalidate all buffers associated with a vnode.
1441 * Called with the underlying object locked.
1444 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1447 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1448 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1449 if (vp->v_object != NULL && vp->v_object->handle != vp)
1451 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1455 * Flush out buffers on the specified list.
1459 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1462 struct buf *bp, *nbp;
1467 ASSERT_BO_WLOCKED(bo);
1470 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1471 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1472 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1478 lblkno = nbp->b_lblkno;
1479 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1482 error = BUF_TIMELOCK(bp,
1483 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1484 "flushbuf", slpflag, slptimeo);
1487 return (error != ENOLCK ? error : EAGAIN);
1489 KASSERT(bp->b_bufobj == bo,
1490 ("bp %p wrong b_bufobj %p should be %p",
1491 bp, bp->b_bufobj, bo));
1492 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1498 * XXX Since there are no node locks for NFS, I
1499 * believe there is a slight chance that a delayed
1500 * write will occur while sleeping just above, so
1503 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1506 bp->b_flags |= B_ASYNC;
1509 return (EAGAIN); /* XXX: why not loop ? */
1512 bp->b_flags |= (B_INVAL | B_RELBUF);
1513 bp->b_flags &= ~B_ASYNC;
1517 (nbp->b_bufobj != bo ||
1518 nbp->b_lblkno != lblkno ||
1519 (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1520 break; /* nbp invalid */
1526 * Truncate a file's buffer and pages to a specified length. This
1527 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1531 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
1533 struct buf *bp, *nbp;
1538 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1539 vp, cred, blksize, (uintmax_t)length);
1542 * Round up to the *next* lbn.
1544 trunclbn = (length + blksize - 1) / blksize;
1546 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1553 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1554 if (bp->b_lblkno < trunclbn)
1557 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1558 BO_LOCKPTR(bo)) == ENOLCK)
1562 bp->b_flags |= (B_INVAL | B_RELBUF);
1563 bp->b_flags &= ~B_ASYNC;
1569 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1570 (nbp->b_vp != vp) ||
1571 (nbp->b_flags & B_DELWRI))) {
1577 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1578 if (bp->b_lblkno < trunclbn)
1581 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1582 BO_LOCKPTR(bo)) == ENOLCK)
1585 bp->b_flags |= (B_INVAL | B_RELBUF);
1586 bp->b_flags &= ~B_ASYNC;
1592 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1593 (nbp->b_vp != vp) ||
1594 (nbp->b_flags & B_DELWRI) == 0)) {
1603 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1604 if (bp->b_lblkno > 0)
1607 * Since we hold the vnode lock this should only
1608 * fail if we're racing with the buf daemon.
1611 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1612 BO_LOCKPTR(bo)) == ENOLCK) {
1615 VNASSERT((bp->b_flags & B_DELWRI), vp,
1616 ("buf(%p) on dirty queue without DELWRI", bp));
1625 bufobj_wwait(bo, 0, 0);
1627 vnode_pager_setsize(vp, length);
1633 buf_vlist_remove(struct buf *bp)
1637 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1638 ASSERT_BO_WLOCKED(bp->b_bufobj);
1639 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1640 (BX_VNDIRTY|BX_VNCLEAN),
1641 ("buf_vlist_remove: Buf %p is on two lists", bp));
1642 if (bp->b_xflags & BX_VNDIRTY)
1643 bv = &bp->b_bufobj->bo_dirty;
1645 bv = &bp->b_bufobj->bo_clean;
1646 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
1647 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1649 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1653 * Add the buffer to the sorted clean or dirty block list.
1655 * NOTE: xflags is passed as a constant, optimizing this inline function!
1658 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1664 ASSERT_BO_WLOCKED(bo);
1665 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
1666 ("dead bo %p", bo));
1667 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1668 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1669 bp->b_xflags |= xflags;
1670 if (xflags & BX_VNDIRTY)
1676 * Keep the list ordered. Optimize empty list insertion. Assume
1677 * we tend to grow at the tail so lookup_le should usually be cheaper
1680 if (bv->bv_cnt == 0 ||
1681 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
1682 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1683 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
1684 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
1686 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
1687 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
1689 panic("buf_vlist_add: Preallocated nodes insufficient.");
1694 * Lookup a buffer using the splay tree. Note that we specifically avoid
1695 * shadow buffers used in background bitmap writes.
1697 * This code isn't quite efficient as it could be because we are maintaining
1698 * two sorted lists and do not know which list the block resides in.
1700 * During a "make buildworld" the desired buffer is found at one of
1701 * the roots more than 60% of the time. Thus, checking both roots
1702 * before performing either splay eliminates unnecessary splays on the
1703 * first tree splayed.
1706 gbincore(struct bufobj *bo, daddr_t lblkno)
1710 ASSERT_BO_LOCKED(bo);
1711 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
1714 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
1718 * Associate a buffer with a vnode.
1721 bgetvp(struct vnode *vp, struct buf *bp)
1726 ASSERT_BO_WLOCKED(bo);
1727 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1729 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1730 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1731 ("bgetvp: bp already attached! %p", bp));
1737 * Insert onto list for new vnode.
1739 buf_vlist_add(bp, bo, BX_VNCLEAN);
1743 * Disassociate a buffer from a vnode.
1746 brelvp(struct buf *bp)
1751 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1752 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1755 * Delete from old vnode list, if on one.
1757 vp = bp->b_vp; /* XXX */
1760 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1761 buf_vlist_remove(bp);
1763 panic("brelvp: Buffer %p not on queue.", bp);
1764 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1765 bo->bo_flag &= ~BO_ONWORKLST;
1766 mtx_lock(&sync_mtx);
1767 LIST_REMOVE(bo, bo_synclist);
1768 syncer_worklist_len--;
1769 mtx_unlock(&sync_mtx);
1772 bp->b_bufobj = NULL;
1778 * Add an item to the syncer work queue.
1781 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1785 ASSERT_BO_WLOCKED(bo);
1787 mtx_lock(&sync_mtx);
1788 if (bo->bo_flag & BO_ONWORKLST)
1789 LIST_REMOVE(bo, bo_synclist);
1791 bo->bo_flag |= BO_ONWORKLST;
1792 syncer_worklist_len++;
1795 if (delay > syncer_maxdelay - 2)
1796 delay = syncer_maxdelay - 2;
1797 slot = (syncer_delayno + delay) & syncer_mask;
1799 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
1800 mtx_unlock(&sync_mtx);
1804 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1808 mtx_lock(&sync_mtx);
1809 len = syncer_worklist_len - sync_vnode_count;
1810 mtx_unlock(&sync_mtx);
1811 error = SYSCTL_OUT(req, &len, sizeof(len));
1815 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1816 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1818 static struct proc *updateproc;
1819 static void sched_sync(void);
1820 static struct kproc_desc up_kp = {
1825 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1828 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1833 *bo = LIST_FIRST(slp);
1836 vp = (*bo)->__bo_vnode; /* XXX */
1837 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1840 * We use vhold in case the vnode does not
1841 * successfully sync. vhold prevents the vnode from
1842 * going away when we unlock the sync_mtx so that
1843 * we can acquire the vnode interlock.
1846 mtx_unlock(&sync_mtx);
1848 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1850 mtx_lock(&sync_mtx);
1851 return (*bo == LIST_FIRST(slp));
1853 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1854 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1856 vn_finished_write(mp);
1858 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1860 * Put us back on the worklist. The worklist
1861 * routine will remove us from our current
1862 * position and then add us back in at a later
1865 vn_syncer_add_to_worklist(*bo, syncdelay);
1869 mtx_lock(&sync_mtx);
1873 static int first_printf = 1;
1876 * System filesystem synchronizer daemon.
1881 struct synclist *next, *slp;
1884 struct thread *td = curthread;
1886 int net_worklist_len;
1887 int syncer_final_iter;
1891 syncer_final_iter = 0;
1892 syncer_state = SYNCER_RUNNING;
1893 starttime = time_uptime;
1894 td->td_pflags |= TDP_NORUNNINGBUF;
1896 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1899 mtx_lock(&sync_mtx);
1901 if (syncer_state == SYNCER_FINAL_DELAY &&
1902 syncer_final_iter == 0) {
1903 mtx_unlock(&sync_mtx);
1904 kproc_suspend_check(td->td_proc);
1905 mtx_lock(&sync_mtx);
1907 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1908 if (syncer_state != SYNCER_RUNNING &&
1909 starttime != time_uptime) {
1911 printf("\nSyncing disks, vnodes remaining...");
1914 printf("%d ", net_worklist_len);
1916 starttime = time_uptime;
1919 * Push files whose dirty time has expired. Be careful
1920 * of interrupt race on slp queue.
1922 * Skip over empty worklist slots when shutting down.
1925 slp = &syncer_workitem_pending[syncer_delayno];
1926 syncer_delayno += 1;
1927 if (syncer_delayno == syncer_maxdelay)
1929 next = &syncer_workitem_pending[syncer_delayno];
1931 * If the worklist has wrapped since the
1932 * it was emptied of all but syncer vnodes,
1933 * switch to the FINAL_DELAY state and run
1934 * for one more second.
1936 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1937 net_worklist_len == 0 &&
1938 last_work_seen == syncer_delayno) {
1939 syncer_state = SYNCER_FINAL_DELAY;
1940 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1942 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1943 syncer_worklist_len > 0);
1946 * Keep track of the last time there was anything
1947 * on the worklist other than syncer vnodes.
1948 * Return to the SHUTTING_DOWN state if any
1951 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1952 last_work_seen = syncer_delayno;
1953 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1954 syncer_state = SYNCER_SHUTTING_DOWN;
1955 while (!LIST_EMPTY(slp)) {
1956 error = sync_vnode(slp, &bo, td);
1958 LIST_REMOVE(bo, bo_synclist);
1959 LIST_INSERT_HEAD(next, bo, bo_synclist);
1963 if (first_printf == 0)
1964 wdog_kern_pat(WD_LASTVAL);
1967 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1968 syncer_final_iter--;
1970 * The variable rushjob allows the kernel to speed up the
1971 * processing of the filesystem syncer process. A rushjob
1972 * value of N tells the filesystem syncer to process the next
1973 * N seconds worth of work on its queue ASAP. Currently rushjob
1974 * is used by the soft update code to speed up the filesystem
1975 * syncer process when the incore state is getting so far
1976 * ahead of the disk that the kernel memory pool is being
1977 * threatened with exhaustion.
1984 * Just sleep for a short period of time between
1985 * iterations when shutting down to allow some I/O
1988 * If it has taken us less than a second to process the
1989 * current work, then wait. Otherwise start right over
1990 * again. We can still lose time if any single round
1991 * takes more than two seconds, but it does not really
1992 * matter as we are just trying to generally pace the
1993 * filesystem activity.
1995 if (syncer_state != SYNCER_RUNNING ||
1996 time_uptime == starttime) {
1998 sched_prio(td, PPAUSE);
2001 if (syncer_state != SYNCER_RUNNING)
2002 cv_timedwait(&sync_wakeup, &sync_mtx,
2003 hz / SYNCER_SHUTDOWN_SPEEDUP);
2004 else if (time_uptime == starttime)
2005 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2010 * Request the syncer daemon to speed up its work.
2011 * We never push it to speed up more than half of its
2012 * normal turn time, otherwise it could take over the cpu.
2015 speedup_syncer(void)
2019 mtx_lock(&sync_mtx);
2020 if (rushjob < syncdelay / 2) {
2022 stat_rush_requests += 1;
2025 mtx_unlock(&sync_mtx);
2026 cv_broadcast(&sync_wakeup);
2031 * Tell the syncer to speed up its work and run though its work
2032 * list several times, then tell it to shut down.
2035 syncer_shutdown(void *arg, int howto)
2038 if (howto & RB_NOSYNC)
2040 mtx_lock(&sync_mtx);
2041 syncer_state = SYNCER_SHUTTING_DOWN;
2043 mtx_unlock(&sync_mtx);
2044 cv_broadcast(&sync_wakeup);
2045 kproc_shutdown(arg, howto);
2049 syncer_suspend(void)
2052 syncer_shutdown(updateproc, 0);
2059 mtx_lock(&sync_mtx);
2061 syncer_state = SYNCER_RUNNING;
2062 mtx_unlock(&sync_mtx);
2063 cv_broadcast(&sync_wakeup);
2064 kproc_resume(updateproc);
2068 * Reassign a buffer from one vnode to another.
2069 * Used to assign file specific control information
2070 * (indirect blocks) to the vnode to which they belong.
2073 reassignbuf(struct buf *bp)
2086 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2087 bp, bp->b_vp, bp->b_flags);
2089 * B_PAGING flagged buffers cannot be reassigned because their vp
2090 * is not fully linked in.
2092 if (bp->b_flags & B_PAGING)
2093 panic("cannot reassign paging buffer");
2096 * Delete from old vnode list, if on one.
2099 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2100 buf_vlist_remove(bp);
2102 panic("reassignbuf: Buffer %p not on queue.", bp);
2104 * If dirty, put on list of dirty buffers; otherwise insert onto list
2107 if (bp->b_flags & B_DELWRI) {
2108 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2109 switch (vp->v_type) {
2119 vn_syncer_add_to_worklist(bo, delay);
2121 buf_vlist_add(bp, bo, BX_VNDIRTY);
2123 buf_vlist_add(bp, bo, BX_VNCLEAN);
2125 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2126 mtx_lock(&sync_mtx);
2127 LIST_REMOVE(bo, bo_synclist);
2128 syncer_worklist_len--;
2129 mtx_unlock(&sync_mtx);
2130 bo->bo_flag &= ~BO_ONWORKLST;
2135 bp = TAILQ_FIRST(&bv->bv_hd);
2136 KASSERT(bp == NULL || bp->b_bufobj == bo,
2137 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2138 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2139 KASSERT(bp == NULL || bp->b_bufobj == bo,
2140 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2142 bp = TAILQ_FIRST(&bv->bv_hd);
2143 KASSERT(bp == NULL || bp->b_bufobj == bo,
2144 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2145 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2146 KASSERT(bp == NULL || bp->b_bufobj == bo,
2147 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2153 * Increment the use and hold counts on the vnode, taking care to reference
2154 * the driver's usecount if this is a chardev. The vholdl() will remove
2155 * the vnode from the free list if it is presently free. Requires the
2156 * vnode interlock and returns with it held.
2159 v_incr_usecount(struct vnode *vp)
2162 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2165 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2167 vp->v_rdev->si_usecount++;
2173 * Turn a holdcnt into a use+holdcnt such that only one call to
2174 * v_decr_usecount is needed.
2177 v_upgrade_usecount(struct vnode *vp)
2180 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2182 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2184 vp->v_rdev->si_usecount++;
2190 * Decrement the vnode use and hold count along with the driver's usecount
2191 * if this is a chardev. The vdropl() below releases the vnode interlock
2192 * as it may free the vnode.
2195 v_decr_usecount(struct vnode *vp)
2198 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2199 VNASSERT(vp->v_usecount > 0, vp,
2200 ("v_decr_usecount: negative usecount"));
2201 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2203 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2205 vp->v_rdev->si_usecount--;
2212 * Decrement only the use count and driver use count. This is intended to
2213 * be paired with a follow on vdropl() to release the remaining hold count.
2214 * In this way we may vgone() a vnode with a 0 usecount without risk of
2215 * having it end up on a free list because the hold count is kept above 0.
2218 v_decr_useonly(struct vnode *vp)
2221 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2222 VNASSERT(vp->v_usecount > 0, vp,
2223 ("v_decr_useonly: negative usecount"));
2224 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2226 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2228 vp->v_rdev->si_usecount--;
2234 * Grab a particular vnode from the free list, increment its
2235 * reference count and lock it. VI_DOOMED is set if the vnode
2236 * is being destroyed. Only callers who specify LK_RETRY will
2237 * see doomed vnodes. If inactive processing was delayed in
2238 * vput try to do it here.
2241 vget(struct vnode *vp, int flags, struct thread *td)
2246 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2247 ("vget: invalid lock operation"));
2248 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2250 if ((flags & LK_INTERLOCK) == 0)
2253 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2255 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2259 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2260 panic("vget: vn_lock failed to return ENOENT\n");
2262 /* Upgrade our holdcnt to a usecount. */
2263 v_upgrade_usecount(vp);
2265 * We don't guarantee that any particular close will
2266 * trigger inactive processing so just make a best effort
2267 * here at preventing a reference to a removed file. If
2268 * we don't succeed no harm is done.
2270 if (vp->v_iflag & VI_OWEINACT) {
2271 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2272 (flags & LK_NOWAIT) == 0)
2274 vp->v_iflag &= ~VI_OWEINACT;
2281 * Increase the reference count of a vnode.
2284 vref(struct vnode *vp)
2287 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2289 v_incr_usecount(vp);
2294 * Return reference count of a vnode.
2296 * The results of this call are only guaranteed when some mechanism other
2297 * than the VI lock is used to stop other processes from gaining references
2298 * to the vnode. This may be the case if the caller holds the only reference.
2299 * This is also useful when stale data is acceptable as race conditions may
2300 * be accounted for by some other means.
2303 vrefcnt(struct vnode *vp)
2308 usecnt = vp->v_usecount;
2314 #define VPUTX_VRELE 1
2315 #define VPUTX_VPUT 2
2316 #define VPUTX_VUNREF 3
2319 vputx(struct vnode *vp, int func)
2323 KASSERT(vp != NULL, ("vputx: null vp"));
2324 if (func == VPUTX_VUNREF)
2325 ASSERT_VOP_LOCKED(vp, "vunref");
2326 else if (func == VPUTX_VPUT)
2327 ASSERT_VOP_LOCKED(vp, "vput");
2329 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2330 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2333 /* Skip this v_writecount check if we're going to panic below. */
2334 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2335 ("vputx: missed vn_close"));
2338 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2339 vp->v_usecount == 1)) {
2340 if (func == VPUTX_VPUT)
2342 v_decr_usecount(vp);
2346 if (vp->v_usecount != 1) {
2347 vprint("vputx: negative ref count", vp);
2348 panic("vputx: negative ref cnt");
2350 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2352 * We want to hold the vnode until the inactive finishes to
2353 * prevent vgone() races. We drop the use count here and the
2354 * hold count below when we're done.
2358 * We must call VOP_INACTIVE with the node locked. Mark
2359 * as VI_DOINGINACT to avoid recursion.
2361 vp->v_iflag |= VI_OWEINACT;
2364 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2368 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2369 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2375 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2376 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2381 if (vp->v_usecount > 0)
2382 vp->v_iflag &= ~VI_OWEINACT;
2384 if (vp->v_iflag & VI_OWEINACT)
2385 vinactive(vp, curthread);
2386 if (func != VPUTX_VUNREF)
2393 * Vnode put/release.
2394 * If count drops to zero, call inactive routine and return to freelist.
2397 vrele(struct vnode *vp)
2400 vputx(vp, VPUTX_VRELE);
2404 * Release an already locked vnode. This give the same effects as
2405 * unlock+vrele(), but takes less time and avoids releasing and
2406 * re-aquiring the lock (as vrele() acquires the lock internally.)
2409 vput(struct vnode *vp)
2412 vputx(vp, VPUTX_VPUT);
2416 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2419 vunref(struct vnode *vp)
2422 vputx(vp, VPUTX_VUNREF);
2426 * Somebody doesn't want the vnode recycled.
2429 vhold(struct vnode *vp)
2438 * Increase the hold count and activate if this is the first reference.
2441 vholdl(struct vnode *vp)
2445 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2447 /* getnewvnode() calls v_incr_usecount() without holding interlock. */
2448 if (vp->v_type != VNON || vp->v_data != NULL)
2449 ASSERT_VI_LOCKED(vp, "vholdl");
2452 if ((vp->v_iflag & VI_FREE) == 0)
2454 VNASSERT(vp->v_holdcnt == 1, vp, ("vholdl: wrong hold count"));
2455 VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed."));
2457 * Remove a vnode from the free list, mark it as in use,
2458 * and put it on the active list.
2460 mtx_lock(&vnode_free_list_mtx);
2461 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2463 vp->v_iflag &= ~VI_FREE;
2464 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2465 ("Activating already active vnode"));
2466 vp->v_iflag |= VI_ACTIVE;
2468 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2469 mp->mnt_activevnodelistsize++;
2470 mtx_unlock(&vnode_free_list_mtx);
2474 * Note that there is one less who cares about this vnode.
2475 * vdrop() is the opposite of vhold().
2478 vdrop(struct vnode *vp)
2486 * Drop the hold count of the vnode. If this is the last reference to
2487 * the vnode we place it on the free list unless it has been vgone'd
2488 * (marked VI_DOOMED) in which case we will free it.
2490 * Because the vnode vm object keeps a hold reference on the vnode if
2491 * there is at least one resident non-cached page, the vnode cannot
2492 * leave the active list without the page cleanup done.
2495 vdropl(struct vnode *vp)
2501 ASSERT_VI_LOCKED(vp, "vdropl");
2502 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2503 if (vp->v_holdcnt <= 0)
2504 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2506 if (vp->v_holdcnt > 0) {
2510 if ((vp->v_iflag & VI_DOOMED) == 0) {
2512 * Mark a vnode as free: remove it from its active list
2513 * and put it up for recycling on the freelist.
2515 VNASSERT(vp->v_op != NULL, vp,
2516 ("vdropl: vnode already reclaimed."));
2517 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2518 ("vnode already free"));
2519 VNASSERT(vp->v_holdcnt == 0, vp,
2520 ("vdropl: freeing when we shouldn't"));
2521 active = vp->v_iflag & VI_ACTIVE;
2522 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2523 vp->v_iflag &= ~VI_ACTIVE;
2525 mtx_lock(&vnode_free_list_mtx);
2527 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2529 mp->mnt_activevnodelistsize--;
2531 TAILQ_INSERT_TAIL(&vnode_free_list, vp,
2534 vp->v_iflag |= VI_FREE;
2535 mtx_unlock(&vnode_free_list_mtx);
2537 atomic_add_long(&free_owe_inact, 1);
2543 * The vnode has been marked for destruction, so free it.
2545 * The vnode will be returned to the zone where it will
2546 * normally remain until it is needed for another vnode. We
2547 * need to cleanup (or verify that the cleanup has already
2548 * been done) any residual data left from its current use
2549 * so as not to contaminate the freshly allocated vnode.
2551 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2552 atomic_subtract_long(&numvnodes, 1);
2554 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2555 ("cleaned vnode still on the free list."));
2556 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2557 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2558 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2559 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2560 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2561 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2562 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
2563 ("clean blk trie not empty"));
2564 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2565 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
2566 ("dirty blk trie not empty"));
2567 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2568 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2569 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2570 VNASSERT(TAILQ_EMPTY(&vp->v_rl.rl_waiters), vp,
2571 ("Dangling rangelock waiters"));
2574 mac_vnode_destroy(vp);
2576 if (vp->v_pollinfo != NULL) {
2577 destroy_vpollinfo(vp->v_pollinfo);
2578 vp->v_pollinfo = NULL;
2581 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2584 bzero(&vp->v_un, sizeof(vp->v_un));
2585 vp->v_lasta = vp->v_clen = vp->v_cstart = vp->v_lastw = 0;
2589 uma_zfree(vnode_zone, vp);
2593 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2594 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2595 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2596 * failed lock upgrade.
2599 vinactive(struct vnode *vp, struct thread *td)
2601 struct vm_object *obj;
2603 ASSERT_VOP_ELOCKED(vp, "vinactive");
2604 ASSERT_VI_LOCKED(vp, "vinactive");
2605 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2606 ("vinactive: recursed on VI_DOINGINACT"));
2607 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2608 vp->v_iflag |= VI_DOINGINACT;
2609 vp->v_iflag &= ~VI_OWEINACT;
2612 * Before moving off the active list, we must be sure that any
2613 * modified pages are converted into the vnode's dirty
2614 * buffers, since these will no longer be checked once the
2615 * vnode is on the inactive list.
2617 * The write-out of the dirty pages is asynchronous. At the
2618 * point that VOP_INACTIVE() is called, there could still be
2619 * pending I/O and dirty pages in the object.
2622 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
2623 VM_OBJECT_WLOCK(obj);
2624 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
2625 VM_OBJECT_WUNLOCK(obj);
2627 VOP_INACTIVE(vp, td);
2629 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2630 ("vinactive: lost VI_DOINGINACT"));
2631 vp->v_iflag &= ~VI_DOINGINACT;
2635 * Remove any vnodes in the vnode table belonging to mount point mp.
2637 * If FORCECLOSE is not specified, there should not be any active ones,
2638 * return error if any are found (nb: this is a user error, not a
2639 * system error). If FORCECLOSE is specified, detach any active vnodes
2642 * If WRITECLOSE is set, only flush out regular file vnodes open for
2645 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2647 * `rootrefs' specifies the base reference count for the root vnode
2648 * of this filesystem. The root vnode is considered busy if its
2649 * v_usecount exceeds this value. On a successful return, vflush(, td)
2650 * will call vrele() on the root vnode exactly rootrefs times.
2651 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2655 static int busyprt = 0; /* print out busy vnodes */
2656 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2660 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2662 struct vnode *vp, *mvp, *rootvp = NULL;
2664 int busy = 0, error;
2666 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2669 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2670 ("vflush: bad args"));
2672 * Get the filesystem root vnode. We can vput() it
2673 * immediately, since with rootrefs > 0, it won't go away.
2675 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2676 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2683 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2685 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2688 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2692 * Skip over a vnodes marked VV_SYSTEM.
2694 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2700 * If WRITECLOSE is set, flush out unlinked but still open
2701 * files (even if open only for reading) and regular file
2702 * vnodes open for writing.
2704 if (flags & WRITECLOSE) {
2705 if (vp->v_object != NULL) {
2706 VM_OBJECT_WLOCK(vp->v_object);
2707 vm_object_page_clean(vp->v_object, 0, 0, 0);
2708 VM_OBJECT_WUNLOCK(vp->v_object);
2710 error = VOP_FSYNC(vp, MNT_WAIT, td);
2714 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2717 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2720 if ((vp->v_type == VNON ||
2721 (error == 0 && vattr.va_nlink > 0)) &&
2722 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2730 * With v_usecount == 0, all we need to do is clear out the
2731 * vnode data structures and we are done.
2733 * If FORCECLOSE is set, forcibly close the vnode.
2735 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2741 vprint("vflush: busy vnode", vp);
2747 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2749 * If just the root vnode is busy, and if its refcount
2750 * is equal to `rootrefs', then go ahead and kill it.
2753 KASSERT(busy > 0, ("vflush: not busy"));
2754 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2755 ("vflush: usecount %d < rootrefs %d",
2756 rootvp->v_usecount, rootrefs));
2757 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2758 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2760 VOP_UNLOCK(rootvp, 0);
2766 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2770 for (; rootrefs > 0; rootrefs--)
2776 * Recycle an unused vnode to the front of the free list.
2779 vrecycle(struct vnode *vp)
2783 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2784 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2787 if (vp->v_usecount == 0) {
2796 * Eliminate all activity associated with a vnode
2797 * in preparation for reuse.
2800 vgone(struct vnode *vp)
2808 notify_lowervp_vfs_dummy(struct mount *mp __unused,
2809 struct vnode *lowervp __unused)
2814 * Notify upper mounts about reclaimed or unlinked vnode.
2817 vfs_notify_upper(struct vnode *vp, int event)
2819 static struct vfsops vgonel_vfsops = {
2820 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
2821 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
2823 struct mount *mp, *ump, *mmp;
2830 if (TAILQ_EMPTY(&mp->mnt_uppers))
2833 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
2834 mmp->mnt_op = &vgonel_vfsops;
2835 mmp->mnt_kern_flag |= MNTK_MARKER;
2837 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
2838 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
2839 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
2840 ump = TAILQ_NEXT(ump, mnt_upper_link);
2843 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
2846 case VFS_NOTIFY_UPPER_RECLAIM:
2847 VFS_RECLAIM_LOWERVP(ump, vp);
2849 case VFS_NOTIFY_UPPER_UNLINK:
2850 VFS_UNLINK_LOWERVP(ump, vp);
2853 KASSERT(0, ("invalid event %d", event));
2857 ump = TAILQ_NEXT(mmp, mnt_upper_link);
2858 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
2861 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
2862 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
2863 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
2864 wakeup(&mp->mnt_uppers);
2871 * vgone, with the vp interlock held.
2874 vgonel(struct vnode *vp)
2881 ASSERT_VOP_ELOCKED(vp, "vgonel");
2882 ASSERT_VI_LOCKED(vp, "vgonel");
2883 VNASSERT(vp->v_holdcnt, vp,
2884 ("vgonel: vp %p has no reference.", vp));
2885 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2889 * Don't vgonel if we're already doomed.
2891 if (vp->v_iflag & VI_DOOMED)
2893 vp->v_iflag |= VI_DOOMED;
2896 * Check to see if the vnode is in use. If so, we have to call
2897 * VOP_CLOSE() and VOP_INACTIVE().
2899 active = vp->v_usecount;
2900 oweinact = (vp->v_iflag & VI_OWEINACT);
2902 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
2905 * If purging an active vnode, it must be closed and
2906 * deactivated before being reclaimed.
2909 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2910 if (oweinact || active) {
2912 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2916 if (vp->v_type == VSOCK)
2917 vfs_unp_reclaim(vp);
2920 * Clean out any buffers associated with the vnode.
2921 * If the flush fails, just toss the buffers.
2924 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2925 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2926 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
2927 while (vinvalbuf(vp, 0, 0, 0) != 0)
2931 BO_LOCK(&vp->v_bufobj);
2932 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
2933 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
2934 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
2935 vp->v_bufobj.bo_clean.bv_cnt == 0,
2936 ("vp %p bufobj not invalidated", vp));
2937 vp->v_bufobj.bo_flag |= BO_DEAD;
2938 BO_UNLOCK(&vp->v_bufobj);
2941 * Reclaim the vnode.
2943 if (VOP_RECLAIM(vp, td))
2944 panic("vgone: cannot reclaim");
2946 vn_finished_secondary_write(mp);
2947 VNASSERT(vp->v_object == NULL, vp,
2948 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2950 * Clear the advisory locks and wake up waiting threads.
2952 (void)VOP_ADVLOCKPURGE(vp);
2955 * Delete from old mount point vnode list.
2960 * Done with purge, reset to the standard lock and invalidate
2964 vp->v_vnlock = &vp->v_lock;
2965 vp->v_op = &dead_vnodeops;
2971 * Calculate the total number of references to a special device.
2974 vcount(struct vnode *vp)
2979 count = vp->v_rdev->si_usecount;
2985 * Same as above, but using the struct cdev *as argument
2988 count_dev(struct cdev *dev)
2993 count = dev->si_usecount;
2999 * Print out a description of a vnode.
3001 static char *typename[] =
3002 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
3006 vn_printf(struct vnode *vp, const char *fmt, ...)
3009 char buf[256], buf2[16];
3015 printf("%p: ", (void *)vp);
3016 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
3017 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
3018 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
3021 if (vp->v_vflag & VV_ROOT)
3022 strlcat(buf, "|VV_ROOT", sizeof(buf));
3023 if (vp->v_vflag & VV_ISTTY)
3024 strlcat(buf, "|VV_ISTTY", sizeof(buf));
3025 if (vp->v_vflag & VV_NOSYNC)
3026 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
3027 if (vp->v_vflag & VV_ETERNALDEV)
3028 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
3029 if (vp->v_vflag & VV_CACHEDLABEL)
3030 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
3031 if (vp->v_vflag & VV_TEXT)
3032 strlcat(buf, "|VV_TEXT", sizeof(buf));
3033 if (vp->v_vflag & VV_COPYONWRITE)
3034 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
3035 if (vp->v_vflag & VV_SYSTEM)
3036 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
3037 if (vp->v_vflag & VV_PROCDEP)
3038 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
3039 if (vp->v_vflag & VV_NOKNOTE)
3040 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
3041 if (vp->v_vflag & VV_DELETED)
3042 strlcat(buf, "|VV_DELETED", sizeof(buf));
3043 if (vp->v_vflag & VV_MD)
3044 strlcat(buf, "|VV_MD", sizeof(buf));
3045 if (vp->v_vflag & VV_FORCEINSMQ)
3046 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
3047 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
3048 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
3049 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
3051 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
3052 strlcat(buf, buf2, sizeof(buf));
3054 if (vp->v_iflag & VI_MOUNT)
3055 strlcat(buf, "|VI_MOUNT", sizeof(buf));
3056 if (vp->v_iflag & VI_DOOMED)
3057 strlcat(buf, "|VI_DOOMED", sizeof(buf));
3058 if (vp->v_iflag & VI_FREE)
3059 strlcat(buf, "|VI_FREE", sizeof(buf));
3060 if (vp->v_iflag & VI_ACTIVE)
3061 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3062 if (vp->v_iflag & VI_DOINGINACT)
3063 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3064 if (vp->v_iflag & VI_OWEINACT)
3065 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3066 flags = vp->v_iflag & ~(VI_MOUNT | VI_DOOMED | VI_FREE |
3067 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
3069 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3070 strlcat(buf, buf2, sizeof(buf));
3072 printf(" flags (%s)\n", buf + 1);
3073 if (mtx_owned(VI_MTX(vp)))
3074 printf(" VI_LOCKed");
3075 if (vp->v_object != NULL)
3076 printf(" v_object %p ref %d pages %d "
3077 "cleanbuf %d dirtybuf %d\n",
3078 vp->v_object, vp->v_object->ref_count,
3079 vp->v_object->resident_page_count,
3080 vp->v_bufobj.bo_clean.bv_cnt,
3081 vp->v_bufobj.bo_dirty.bv_cnt);
3083 lockmgr_printinfo(vp->v_vnlock);
3084 if (vp->v_data != NULL)
3090 * List all of the locked vnodes in the system.
3091 * Called when debugging the kernel.
3093 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3099 * Note: because this is DDB, we can't obey the locking semantics
3100 * for these structures, which means we could catch an inconsistent
3101 * state and dereference a nasty pointer. Not much to be done
3104 db_printf("Locked vnodes\n");
3105 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3106 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3107 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3114 * Show details about the given vnode.
3116 DB_SHOW_COMMAND(vnode, db_show_vnode)
3122 vp = (struct vnode *)addr;
3123 vn_printf(vp, "vnode ");
3127 * Show details about the given mount point.
3129 DB_SHOW_COMMAND(mount, db_show_mount)
3140 /* No address given, print short info about all mount points. */
3141 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3142 db_printf("%p %s on %s (%s)\n", mp,
3143 mp->mnt_stat.f_mntfromname,
3144 mp->mnt_stat.f_mntonname,
3145 mp->mnt_stat.f_fstypename);
3149 db_printf("\nMore info: show mount <addr>\n");
3153 mp = (struct mount *)addr;
3154 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3155 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3158 mflags = mp->mnt_flag;
3159 #define MNT_FLAG(flag) do { \
3160 if (mflags & (flag)) { \
3161 if (buf[0] != '\0') \
3162 strlcat(buf, ", ", sizeof(buf)); \
3163 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3164 mflags &= ~(flag); \
3167 MNT_FLAG(MNT_RDONLY);
3168 MNT_FLAG(MNT_SYNCHRONOUS);
3169 MNT_FLAG(MNT_NOEXEC);
3170 MNT_FLAG(MNT_NOSUID);
3171 MNT_FLAG(MNT_NFS4ACLS);
3172 MNT_FLAG(MNT_UNION);
3173 MNT_FLAG(MNT_ASYNC);
3174 MNT_FLAG(MNT_SUIDDIR);
3175 MNT_FLAG(MNT_SOFTDEP);
3176 MNT_FLAG(MNT_NOSYMFOLLOW);
3177 MNT_FLAG(MNT_GJOURNAL);
3178 MNT_FLAG(MNT_MULTILABEL);
3180 MNT_FLAG(MNT_NOATIME);
3181 MNT_FLAG(MNT_NOCLUSTERR);
3182 MNT_FLAG(MNT_NOCLUSTERW);
3184 MNT_FLAG(MNT_EXRDONLY);
3185 MNT_FLAG(MNT_EXPORTED);
3186 MNT_FLAG(MNT_DEFEXPORTED);
3187 MNT_FLAG(MNT_EXPORTANON);
3188 MNT_FLAG(MNT_EXKERB);
3189 MNT_FLAG(MNT_EXPUBLIC);
3190 MNT_FLAG(MNT_LOCAL);
3191 MNT_FLAG(MNT_QUOTA);
3192 MNT_FLAG(MNT_ROOTFS);
3194 MNT_FLAG(MNT_IGNORE);
3195 MNT_FLAG(MNT_UPDATE);
3196 MNT_FLAG(MNT_DELEXPORT);
3197 MNT_FLAG(MNT_RELOAD);
3198 MNT_FLAG(MNT_FORCE);
3199 MNT_FLAG(MNT_SNAPSHOT);
3200 MNT_FLAG(MNT_BYFSID);
3204 strlcat(buf, ", ", sizeof(buf));
3205 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3206 "0x%016jx", mflags);
3208 db_printf(" mnt_flag = %s\n", buf);
3211 flags = mp->mnt_kern_flag;
3212 #define MNT_KERN_FLAG(flag) do { \
3213 if (flags & (flag)) { \
3214 if (buf[0] != '\0') \
3215 strlcat(buf, ", ", sizeof(buf)); \
3216 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3220 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3221 MNT_KERN_FLAG(MNTK_ASYNC);
3222 MNT_KERN_FLAG(MNTK_SOFTDEP);
3223 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3224 MNT_KERN_FLAG(MNTK_DRAINING);
3225 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3226 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3227 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3228 MNT_KERN_FLAG(MNTK_NO_IOPF);
3229 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3230 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3231 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3232 MNT_KERN_FLAG(MNTK_MARKER);
3233 MNT_KERN_FLAG(MNTK_USES_BCACHE);
3234 MNT_KERN_FLAG(MNTK_NOASYNC);
3235 MNT_KERN_FLAG(MNTK_UNMOUNT);
3236 MNT_KERN_FLAG(MNTK_MWAIT);
3237 MNT_KERN_FLAG(MNTK_SUSPEND);
3238 MNT_KERN_FLAG(MNTK_SUSPEND2);
3239 MNT_KERN_FLAG(MNTK_SUSPENDED);
3240 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3241 MNT_KERN_FLAG(MNTK_NOKNOTE);
3242 #undef MNT_KERN_FLAG
3245 strlcat(buf, ", ", sizeof(buf));
3246 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3249 db_printf(" mnt_kern_flag = %s\n", buf);
3251 db_printf(" mnt_opt = ");
3252 opt = TAILQ_FIRST(mp->mnt_opt);
3254 db_printf("%s", opt->name);
3255 opt = TAILQ_NEXT(opt, link);
3256 while (opt != NULL) {
3257 db_printf(", %s", opt->name);
3258 opt = TAILQ_NEXT(opt, link);
3264 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3265 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3266 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3267 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3268 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3269 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3270 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3271 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3272 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3273 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3274 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3275 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3277 db_printf(" mnt_cred = { uid=%u ruid=%u",
3278 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3279 if (jailed(mp->mnt_cred))
3280 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3282 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3283 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3284 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3285 db_printf(" mnt_activevnodelistsize = %d\n",
3286 mp->mnt_activevnodelistsize);
3287 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3288 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3289 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3290 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3291 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3292 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3293 db_printf(" mnt_secondary_accwrites = %d\n",
3294 mp->mnt_secondary_accwrites);
3295 db_printf(" mnt_gjprovider = %s\n",
3296 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3298 db_printf("\n\nList of active vnodes\n");
3299 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3300 if (vp->v_type != VMARKER) {
3301 vn_printf(vp, "vnode ");
3306 db_printf("\n\nList of inactive vnodes\n");
3307 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3308 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3309 vn_printf(vp, "vnode ");
3318 * Fill in a struct xvfsconf based on a struct vfsconf.
3321 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3323 struct xvfsconf xvfsp;
3325 bzero(&xvfsp, sizeof(xvfsp));
3326 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3327 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3328 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3329 xvfsp.vfc_flags = vfsp->vfc_flags;
3331 * These are unused in userland, we keep them
3332 * to not break binary compatibility.
3334 xvfsp.vfc_vfsops = NULL;
3335 xvfsp.vfc_next = NULL;
3336 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3339 #ifdef COMPAT_FREEBSD32
3341 uint32_t vfc_vfsops;
3342 char vfc_name[MFSNAMELEN];
3343 int32_t vfc_typenum;
3344 int32_t vfc_refcount;
3350 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3352 struct xvfsconf32 xvfsp;
3354 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3355 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3356 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3357 xvfsp.vfc_flags = vfsp->vfc_flags;
3358 xvfsp.vfc_vfsops = 0;
3360 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3365 * Top level filesystem related information gathering.
3368 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3370 struct vfsconf *vfsp;
3375 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3376 #ifdef COMPAT_FREEBSD32
3377 if (req->flags & SCTL_MASK32)
3378 error = vfsconf2x32(req, vfsp);
3381 error = vfsconf2x(req, vfsp);
3389 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3390 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3391 "S,xvfsconf", "List of all configured filesystems");
3393 #ifndef BURN_BRIDGES
3394 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3397 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3399 int *name = (int *)arg1 - 1; /* XXX */
3400 u_int namelen = arg2 + 1; /* XXX */
3401 struct vfsconf *vfsp;
3403 log(LOG_WARNING, "userland calling deprecated sysctl, "
3404 "please rebuild world\n");
3406 #if 1 || defined(COMPAT_PRELITE2)
3407 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3409 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3413 case VFS_MAXTYPENUM:
3416 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3419 return (ENOTDIR); /* overloaded */
3421 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3422 if (vfsp->vfc_typenum == name[2])
3427 return (EOPNOTSUPP);
3428 #ifdef COMPAT_FREEBSD32
3429 if (req->flags & SCTL_MASK32)
3430 return (vfsconf2x32(req, vfsp));
3433 return (vfsconf2x(req, vfsp));
3435 return (EOPNOTSUPP);
3438 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
3439 CTLFLAG_MPSAFE, vfs_sysctl,
3440 "Generic filesystem");
3442 #if 1 || defined(COMPAT_PRELITE2)
3445 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3448 struct vfsconf *vfsp;
3449 struct ovfsconf ovfs;
3452 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3453 bzero(&ovfs, sizeof(ovfs));
3454 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3455 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3456 ovfs.vfc_index = vfsp->vfc_typenum;
3457 ovfs.vfc_refcount = vfsp->vfc_refcount;
3458 ovfs.vfc_flags = vfsp->vfc_flags;
3459 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3469 #endif /* 1 || COMPAT_PRELITE2 */
3470 #endif /* !BURN_BRIDGES */
3472 #define KINFO_VNODESLOP 10
3475 * Dump vnode list (via sysctl).
3479 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3487 * Stale numvnodes access is not fatal here.
3490 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3492 /* Make an estimate */
3493 return (SYSCTL_OUT(req, 0, len));
3495 error = sysctl_wire_old_buffer(req, 0);
3498 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3500 mtx_lock(&mountlist_mtx);
3501 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3502 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3505 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3509 xvn[n].xv_size = sizeof *xvn;
3510 xvn[n].xv_vnode = vp;
3511 xvn[n].xv_id = 0; /* XXX compat */
3512 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3514 XV_COPY(writecount);
3520 xvn[n].xv_flag = vp->v_vflag;
3522 switch (vp->v_type) {
3529 if (vp->v_rdev == NULL) {
3533 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3536 xvn[n].xv_socket = vp->v_socket;
3539 xvn[n].xv_fifo = vp->v_fifoinfo;
3544 /* shouldn't happen? */
3552 mtx_lock(&mountlist_mtx);
3557 mtx_unlock(&mountlist_mtx);
3559 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3564 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
3565 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
3570 unmount_or_warn(struct mount *mp)
3574 error = dounmount(mp, MNT_FORCE, curthread);
3575 if (error != 0 && strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3576 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
3580 printf("%d)\n", error);
3585 * Unmount all filesystems. The list is traversed in reverse order
3586 * of mounting to avoid dependencies.
3589 vfs_unmountall(void)
3591 struct mount *mp, *tmp;
3593 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3596 * Since this only runs when rebooting, it is not interlocked.
3598 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
3602 * Forcibly unmounting "/dev" before "/" would prevent clean
3603 * unmount of the latter.
3605 if (mp == rootdevmp)
3608 unmount_or_warn(mp);
3611 if (rootdevmp != NULL)
3612 unmount_or_warn(rootdevmp);
3616 * perform msync on all vnodes under a mount point
3617 * the mount point must be locked.
3620 vfs_msync(struct mount *mp, int flags)
3622 struct vnode *vp, *mvp;
3623 struct vm_object *obj;
3625 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3626 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
3628 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3629 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3631 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3633 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3640 VM_OBJECT_WLOCK(obj);
3641 vm_object_page_clean(obj, 0, 0,
3643 OBJPC_SYNC : OBJPC_NOSYNC);
3644 VM_OBJECT_WUNLOCK(obj);
3654 destroy_vpollinfo_free(struct vpollinfo *vi)
3657 knlist_destroy(&vi->vpi_selinfo.si_note);
3658 mtx_destroy(&vi->vpi_lock);
3659 uma_zfree(vnodepoll_zone, vi);
3663 destroy_vpollinfo(struct vpollinfo *vi)
3666 knlist_clear(&vi->vpi_selinfo.si_note, 1);
3667 seldrain(&vi->vpi_selinfo);
3668 destroy_vpollinfo_free(vi);
3672 * Initalize per-vnode helper structure to hold poll-related state.
3675 v_addpollinfo(struct vnode *vp)
3677 struct vpollinfo *vi;
3679 if (vp->v_pollinfo != NULL)
3681 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3682 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3683 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3684 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3686 if (vp->v_pollinfo != NULL) {
3688 destroy_vpollinfo_free(vi);
3691 vp->v_pollinfo = vi;
3696 * Record a process's interest in events which might happen to
3697 * a vnode. Because poll uses the historic select-style interface
3698 * internally, this routine serves as both the ``check for any
3699 * pending events'' and the ``record my interest in future events''
3700 * functions. (These are done together, while the lock is held,
3701 * to avoid race conditions.)
3704 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3708 mtx_lock(&vp->v_pollinfo->vpi_lock);
3709 if (vp->v_pollinfo->vpi_revents & events) {
3711 * This leaves events we are not interested
3712 * in available for the other process which
3713 * which presumably had requested them
3714 * (otherwise they would never have been
3717 events &= vp->v_pollinfo->vpi_revents;
3718 vp->v_pollinfo->vpi_revents &= ~events;
3720 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3723 vp->v_pollinfo->vpi_events |= events;
3724 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3725 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3730 * Routine to create and manage a filesystem syncer vnode.
3732 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3733 static int sync_fsync(struct vop_fsync_args *);
3734 static int sync_inactive(struct vop_inactive_args *);
3735 static int sync_reclaim(struct vop_reclaim_args *);
3737 static struct vop_vector sync_vnodeops = {
3738 .vop_bypass = VOP_EOPNOTSUPP,
3739 .vop_close = sync_close, /* close */
3740 .vop_fsync = sync_fsync, /* fsync */
3741 .vop_inactive = sync_inactive, /* inactive */
3742 .vop_reclaim = sync_reclaim, /* reclaim */
3743 .vop_lock1 = vop_stdlock, /* lock */
3744 .vop_unlock = vop_stdunlock, /* unlock */
3745 .vop_islocked = vop_stdislocked, /* islocked */
3749 * Create a new filesystem syncer vnode for the specified mount point.
3752 vfs_allocate_syncvnode(struct mount *mp)
3756 static long start, incr, next;
3759 /* Allocate a new vnode */
3760 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3762 panic("vfs_allocate_syncvnode: getnewvnode() failed");
3764 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3765 vp->v_vflag |= VV_FORCEINSMQ;
3766 error = insmntque(vp, mp);
3768 panic("vfs_allocate_syncvnode: insmntque() failed");
3769 vp->v_vflag &= ~VV_FORCEINSMQ;
3772 * Place the vnode onto the syncer worklist. We attempt to
3773 * scatter them about on the list so that they will go off
3774 * at evenly distributed times even if all the filesystems
3775 * are mounted at once.
3778 if (next == 0 || next > syncer_maxdelay) {
3782 start = syncer_maxdelay / 2;
3783 incr = syncer_maxdelay;
3789 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3790 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3791 mtx_lock(&sync_mtx);
3793 if (mp->mnt_syncer == NULL) {
3794 mp->mnt_syncer = vp;
3797 mtx_unlock(&sync_mtx);
3800 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3807 vfs_deallocate_syncvnode(struct mount *mp)
3811 mtx_lock(&sync_mtx);
3812 vp = mp->mnt_syncer;
3814 mp->mnt_syncer = NULL;
3815 mtx_unlock(&sync_mtx);
3821 * Do a lazy sync of the filesystem.
3824 sync_fsync(struct vop_fsync_args *ap)
3826 struct vnode *syncvp = ap->a_vp;
3827 struct mount *mp = syncvp->v_mount;
3832 * We only need to do something if this is a lazy evaluation.
3834 if (ap->a_waitfor != MNT_LAZY)
3838 * Move ourselves to the back of the sync list.
3840 bo = &syncvp->v_bufobj;
3842 vn_syncer_add_to_worklist(bo, syncdelay);
3846 * Walk the list of vnodes pushing all that are dirty and
3847 * not already on the sync list.
3849 if (vfs_busy(mp, MBF_NOWAIT) != 0)
3851 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3855 save = curthread_pflags_set(TDP_SYNCIO);
3856 vfs_msync(mp, MNT_NOWAIT);
3857 error = VFS_SYNC(mp, MNT_LAZY);
3858 curthread_pflags_restore(save);
3859 vn_finished_write(mp);
3865 * The syncer vnode is no referenced.
3868 sync_inactive(struct vop_inactive_args *ap)
3876 * The syncer vnode is no longer needed and is being decommissioned.
3878 * Modifications to the worklist must be protected by sync_mtx.
3881 sync_reclaim(struct vop_reclaim_args *ap)
3883 struct vnode *vp = ap->a_vp;
3888 mtx_lock(&sync_mtx);
3889 if (vp->v_mount->mnt_syncer == vp)
3890 vp->v_mount->mnt_syncer = NULL;
3891 if (bo->bo_flag & BO_ONWORKLST) {
3892 LIST_REMOVE(bo, bo_synclist);
3893 syncer_worklist_len--;
3895 bo->bo_flag &= ~BO_ONWORKLST;
3897 mtx_unlock(&sync_mtx);
3904 * Check if vnode represents a disk device
3907 vn_isdisk(struct vnode *vp, int *errp)
3911 if (vp->v_type != VCHR) {
3917 if (vp->v_rdev == NULL)
3919 else if (vp->v_rdev->si_devsw == NULL)
3921 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3927 return (error == 0);
3931 * Common filesystem object access control check routine. Accepts a
3932 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3933 * and optional call-by-reference privused argument allowing vaccess()
3934 * to indicate to the caller whether privilege was used to satisfy the
3935 * request (obsoleted). Returns 0 on success, or an errno on failure.
3938 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3939 accmode_t accmode, struct ucred *cred, int *privused)
3941 accmode_t dac_granted;
3942 accmode_t priv_granted;
3944 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3945 ("invalid bit in accmode"));
3946 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3947 ("VAPPEND without VWRITE"));
3950 * Look for a normal, non-privileged way to access the file/directory
3951 * as requested. If it exists, go with that.
3954 if (privused != NULL)
3959 /* Check the owner. */
3960 if (cred->cr_uid == file_uid) {
3961 dac_granted |= VADMIN;
3962 if (file_mode & S_IXUSR)
3963 dac_granted |= VEXEC;
3964 if (file_mode & S_IRUSR)
3965 dac_granted |= VREAD;
3966 if (file_mode & S_IWUSR)
3967 dac_granted |= (VWRITE | VAPPEND);
3969 if ((accmode & dac_granted) == accmode)
3975 /* Otherwise, check the groups (first match) */
3976 if (groupmember(file_gid, cred)) {
3977 if (file_mode & S_IXGRP)
3978 dac_granted |= VEXEC;
3979 if (file_mode & S_IRGRP)
3980 dac_granted |= VREAD;
3981 if (file_mode & S_IWGRP)
3982 dac_granted |= (VWRITE | VAPPEND);
3984 if ((accmode & dac_granted) == accmode)
3990 /* Otherwise, check everyone else. */
3991 if (file_mode & S_IXOTH)
3992 dac_granted |= VEXEC;
3993 if (file_mode & S_IROTH)
3994 dac_granted |= VREAD;
3995 if (file_mode & S_IWOTH)
3996 dac_granted |= (VWRITE | VAPPEND);
3997 if ((accmode & dac_granted) == accmode)
4002 * Build a privilege mask to determine if the set of privileges
4003 * satisfies the requirements when combined with the granted mask
4004 * from above. For each privilege, if the privilege is required,
4005 * bitwise or the request type onto the priv_granted mask.
4011 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
4012 * requests, instead of PRIV_VFS_EXEC.
4014 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4015 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
4016 priv_granted |= VEXEC;
4019 * Ensure that at least one execute bit is on. Otherwise,
4020 * a privileged user will always succeed, and we don't want
4021 * this to happen unless the file really is executable.
4023 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4024 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
4025 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
4026 priv_granted |= VEXEC;
4029 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
4030 !priv_check_cred(cred, PRIV_VFS_READ, 0))
4031 priv_granted |= VREAD;
4033 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
4034 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
4035 priv_granted |= (VWRITE | VAPPEND);
4037 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
4038 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
4039 priv_granted |= VADMIN;
4041 if ((accmode & (priv_granted | dac_granted)) == accmode) {
4042 /* XXX audit: privilege used */
4043 if (privused != NULL)
4048 return ((accmode & VADMIN) ? EPERM : EACCES);
4052 * Credential check based on process requesting service, and per-attribute
4056 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
4057 struct thread *td, accmode_t accmode)
4061 * Kernel-invoked always succeeds.
4067 * Do not allow privileged processes in jail to directly manipulate
4068 * system attributes.
4070 switch (attrnamespace) {
4071 case EXTATTR_NAMESPACE_SYSTEM:
4072 /* Potentially should be: return (EPERM); */
4073 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
4074 case EXTATTR_NAMESPACE_USER:
4075 return (VOP_ACCESS(vp, accmode, cred, td));
4081 #ifdef DEBUG_VFS_LOCKS
4083 * This only exists to supress warnings from unlocked specfs accesses. It is
4084 * no longer ok to have an unlocked VFS.
4086 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4087 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4089 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4090 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4091 "Drop into debugger on lock violation");
4093 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4094 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4095 0, "Check for interlock across VOPs");
4097 int vfs_badlock_print = 1; /* Print lock violations. */
4098 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4099 0, "Print lock violations");
4102 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4103 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4104 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4108 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4112 if (vfs_badlock_backtrace)
4115 if (vfs_badlock_print)
4116 printf("%s: %p %s\n", str, (void *)vp, msg);
4117 if (vfs_badlock_ddb)
4118 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4122 assert_vi_locked(struct vnode *vp, const char *str)
4125 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4126 vfs_badlock("interlock is not locked but should be", str, vp);
4130 assert_vi_unlocked(struct vnode *vp, const char *str)
4133 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4134 vfs_badlock("interlock is locked but should not be", str, vp);
4138 assert_vop_locked(struct vnode *vp, const char *str)
4142 if (!IGNORE_LOCK(vp)) {
4143 locked = VOP_ISLOCKED(vp);
4144 if (locked == 0 || locked == LK_EXCLOTHER)
4145 vfs_badlock("is not locked but should be", str, vp);
4150 assert_vop_unlocked(struct vnode *vp, const char *str)
4153 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4154 vfs_badlock("is locked but should not be", str, vp);
4158 assert_vop_elocked(struct vnode *vp, const char *str)
4161 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4162 vfs_badlock("is not exclusive locked but should be", str, vp);
4167 assert_vop_elocked_other(struct vnode *vp, const char *str)
4170 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
4171 vfs_badlock("is not exclusive locked by another thread",
4176 assert_vop_slocked(struct vnode *vp, const char *str)
4179 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
4180 vfs_badlock("is not locked shared but should be", str, vp);
4183 #endif /* DEBUG_VFS_LOCKS */
4186 vop_rename_fail(struct vop_rename_args *ap)
4189 if (ap->a_tvp != NULL)
4191 if (ap->a_tdvp == ap->a_tvp)
4200 vop_rename_pre(void *ap)
4202 struct vop_rename_args *a = ap;
4204 #ifdef DEBUG_VFS_LOCKS
4206 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4207 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4208 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4209 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4211 /* Check the source (from). */
4212 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4213 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4214 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4215 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4216 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4218 /* Check the target. */
4220 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4221 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4223 if (a->a_tdvp != a->a_fdvp)
4225 if (a->a_tvp != a->a_fvp)
4233 vop_strategy_pre(void *ap)
4235 #ifdef DEBUG_VFS_LOCKS
4236 struct vop_strategy_args *a;
4243 * Cluster ops lock their component buffers but not the IO container.
4245 if ((bp->b_flags & B_CLUSTER) != 0)
4248 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4249 if (vfs_badlock_print)
4251 "VOP_STRATEGY: bp is not locked but should be\n");
4252 if (vfs_badlock_ddb)
4253 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4259 vop_lock_pre(void *ap)
4261 #ifdef DEBUG_VFS_LOCKS
4262 struct vop_lock1_args *a = ap;
4264 if ((a->a_flags & LK_INTERLOCK) == 0)
4265 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4267 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4272 vop_lock_post(void *ap, int rc)
4274 #ifdef DEBUG_VFS_LOCKS
4275 struct vop_lock1_args *a = ap;
4277 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4278 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4279 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4284 vop_unlock_pre(void *ap)
4286 #ifdef DEBUG_VFS_LOCKS
4287 struct vop_unlock_args *a = ap;
4289 if (a->a_flags & LK_INTERLOCK)
4290 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4291 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4296 vop_unlock_post(void *ap, int rc)
4298 #ifdef DEBUG_VFS_LOCKS
4299 struct vop_unlock_args *a = ap;
4301 if (a->a_flags & LK_INTERLOCK)
4302 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4307 vop_create_post(void *ap, int rc)
4309 struct vop_create_args *a = ap;
4312 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4316 vop_deleteextattr_post(void *ap, int rc)
4318 struct vop_deleteextattr_args *a = ap;
4321 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4325 vop_link_post(void *ap, int rc)
4327 struct vop_link_args *a = ap;
4330 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4331 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4336 vop_mkdir_post(void *ap, int rc)
4338 struct vop_mkdir_args *a = ap;
4341 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4345 vop_mknod_post(void *ap, int rc)
4347 struct vop_mknod_args *a = ap;
4350 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4354 vop_remove_post(void *ap, int rc)
4356 struct vop_remove_args *a = ap;
4359 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4360 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4365 vop_rename_post(void *ap, int rc)
4367 struct vop_rename_args *a = ap;
4370 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4371 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4372 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4374 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4376 if (a->a_tdvp != a->a_fdvp)
4378 if (a->a_tvp != a->a_fvp)
4386 vop_rmdir_post(void *ap, int rc)
4388 struct vop_rmdir_args *a = ap;
4391 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4392 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4397 vop_setattr_post(void *ap, int rc)
4399 struct vop_setattr_args *a = ap;
4402 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4406 vop_setextattr_post(void *ap, int rc)
4408 struct vop_setextattr_args *a = ap;
4411 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4415 vop_symlink_post(void *ap, int rc)
4417 struct vop_symlink_args *a = ap;
4420 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4423 static struct knlist fs_knlist;
4426 vfs_event_init(void *arg)
4428 knlist_init_mtx(&fs_knlist, NULL);
4430 /* XXX - correct order? */
4431 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4434 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4437 KNOTE_UNLOCKED(&fs_knlist, event);
4440 static int filt_fsattach(struct knote *kn);
4441 static void filt_fsdetach(struct knote *kn);
4442 static int filt_fsevent(struct knote *kn, long hint);
4444 struct filterops fs_filtops = {
4446 .f_attach = filt_fsattach,
4447 .f_detach = filt_fsdetach,
4448 .f_event = filt_fsevent
4452 filt_fsattach(struct knote *kn)
4455 kn->kn_flags |= EV_CLEAR;
4456 knlist_add(&fs_knlist, kn, 0);
4461 filt_fsdetach(struct knote *kn)
4464 knlist_remove(&fs_knlist, kn, 0);
4468 filt_fsevent(struct knote *kn, long hint)
4471 kn->kn_fflags |= hint;
4472 return (kn->kn_fflags != 0);
4476 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4482 error = SYSCTL_IN(req, &vc, sizeof(vc));
4485 if (vc.vc_vers != VFS_CTL_VERS1)
4487 mp = vfs_getvfs(&vc.vc_fsid);
4490 /* ensure that a specific sysctl goes to the right filesystem. */
4491 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4492 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4496 VCTLTOREQ(&vc, req);
4497 error = VFS_SYSCTL(mp, vc.vc_op, req);
4502 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4503 NULL, 0, sysctl_vfs_ctl, "",
4507 * Function to initialize a va_filerev field sensibly.
4508 * XXX: Wouldn't a random number make a lot more sense ??
4511 init_va_filerev(void)
4516 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4519 static int filt_vfsread(struct knote *kn, long hint);
4520 static int filt_vfswrite(struct knote *kn, long hint);
4521 static int filt_vfsvnode(struct knote *kn, long hint);
4522 static void filt_vfsdetach(struct knote *kn);
4523 static struct filterops vfsread_filtops = {
4525 .f_detach = filt_vfsdetach,
4526 .f_event = filt_vfsread
4528 static struct filterops vfswrite_filtops = {
4530 .f_detach = filt_vfsdetach,
4531 .f_event = filt_vfswrite
4533 static struct filterops vfsvnode_filtops = {
4535 .f_detach = filt_vfsdetach,
4536 .f_event = filt_vfsvnode
4540 vfs_knllock(void *arg)
4542 struct vnode *vp = arg;
4544 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4548 vfs_knlunlock(void *arg)
4550 struct vnode *vp = arg;
4556 vfs_knl_assert_locked(void *arg)
4558 #ifdef DEBUG_VFS_LOCKS
4559 struct vnode *vp = arg;
4561 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4566 vfs_knl_assert_unlocked(void *arg)
4568 #ifdef DEBUG_VFS_LOCKS
4569 struct vnode *vp = arg;
4571 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4576 vfs_kqfilter(struct vop_kqfilter_args *ap)
4578 struct vnode *vp = ap->a_vp;
4579 struct knote *kn = ap->a_kn;
4582 switch (kn->kn_filter) {
4584 kn->kn_fop = &vfsread_filtops;
4587 kn->kn_fop = &vfswrite_filtops;
4590 kn->kn_fop = &vfsvnode_filtops;
4596 kn->kn_hook = (caddr_t)vp;
4599 if (vp->v_pollinfo == NULL)
4601 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4603 knlist_add(knl, kn, 0);
4609 * Detach knote from vnode
4612 filt_vfsdetach(struct knote *kn)
4614 struct vnode *vp = (struct vnode *)kn->kn_hook;
4616 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4617 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4623 filt_vfsread(struct knote *kn, long hint)
4625 struct vnode *vp = (struct vnode *)kn->kn_hook;
4630 * filesystem is gone, so set the EOF flag and schedule
4631 * the knote for deletion.
4633 if (hint == NOTE_REVOKE) {
4635 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4640 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4644 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4645 res = (kn->kn_data != 0);
4652 filt_vfswrite(struct knote *kn, long hint)
4654 struct vnode *vp = (struct vnode *)kn->kn_hook;
4659 * filesystem is gone, so set the EOF flag and schedule
4660 * the knote for deletion.
4662 if (hint == NOTE_REVOKE)
4663 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4671 filt_vfsvnode(struct knote *kn, long hint)
4673 struct vnode *vp = (struct vnode *)kn->kn_hook;
4677 if (kn->kn_sfflags & hint)
4678 kn->kn_fflags |= hint;
4679 if (hint == NOTE_REVOKE) {
4680 kn->kn_flags |= EV_EOF;
4684 res = (kn->kn_fflags != 0);
4690 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4694 if (dp->d_reclen > ap->a_uio->uio_resid)
4695 return (ENAMETOOLONG);
4696 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4698 if (ap->a_ncookies != NULL) {
4699 if (ap->a_cookies != NULL)
4700 free(ap->a_cookies, M_TEMP);
4701 ap->a_cookies = NULL;
4702 *ap->a_ncookies = 0;
4706 if (ap->a_ncookies == NULL)
4709 KASSERT(ap->a_cookies,
4710 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4712 *ap->a_cookies = realloc(*ap->a_cookies,
4713 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4714 (*ap->a_cookies)[*ap->a_ncookies] = off;
4719 * Mark for update the access time of the file if the filesystem
4720 * supports VOP_MARKATIME. This functionality is used by execve and
4721 * mmap, so we want to avoid the I/O implied by directly setting
4722 * va_atime for the sake of efficiency.
4725 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4730 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4731 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4732 (void)VOP_MARKATIME(vp);
4736 * The purpose of this routine is to remove granularity from accmode_t,
4737 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4738 * VADMIN and VAPPEND.
4740 * If it returns 0, the caller is supposed to continue with the usual
4741 * access checks using 'accmode' as modified by this routine. If it
4742 * returns nonzero value, the caller is supposed to return that value
4745 * Note that after this routine runs, accmode may be zero.
4748 vfs_unixify_accmode(accmode_t *accmode)
4751 * There is no way to specify explicit "deny" rule using
4752 * file mode or POSIX.1e ACLs.
4754 if (*accmode & VEXPLICIT_DENY) {
4760 * None of these can be translated into usual access bits.
4761 * Also, the common case for NFSv4 ACLs is to not contain
4762 * either of these bits. Caller should check for VWRITE
4763 * on the containing directory instead.
4765 if (*accmode & (VDELETE_CHILD | VDELETE))
4768 if (*accmode & VADMIN_PERMS) {
4769 *accmode &= ~VADMIN_PERMS;
4774 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4775 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4777 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
4783 * These are helper functions for filesystems to traverse all
4784 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
4786 * This interface replaces MNT_VNODE_FOREACH.
4789 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
4792 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
4797 kern_yield(PRI_USER);
4799 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4800 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
4801 while (vp != NULL && (vp->v_type == VMARKER ||
4802 (vp->v_iflag & VI_DOOMED) != 0))
4803 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4805 /* Check if we are done */
4807 __mnt_vnode_markerfree_all(mvp, mp);
4808 /* MNT_IUNLOCK(mp); -- done in above function */
4809 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4812 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4813 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4820 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
4824 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4827 (*mvp)->v_type = VMARKER;
4829 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
4830 while (vp != NULL && (vp->v_type == VMARKER ||
4831 (vp->v_iflag & VI_DOOMED) != 0))
4832 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4834 /* Check if we are done */
4838 free(*mvp, M_VNODE_MARKER);
4842 (*mvp)->v_mount = mp;
4843 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4851 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
4859 mtx_assert(MNT_MTX(mp), MA_OWNED);
4861 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4862 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4865 free(*mvp, M_VNODE_MARKER);
4870 * These are helper functions for filesystems to traverse their
4871 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
4874 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4877 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4882 free(*mvp, M_VNODE_MARKER);
4886 static struct vnode *
4887 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4889 struct vnode *vp, *nvp;
4891 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
4892 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4894 vp = TAILQ_NEXT(*mvp, v_actfreelist);
4895 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4896 while (vp != NULL) {
4897 if (vp->v_type == VMARKER) {
4898 vp = TAILQ_NEXT(vp, v_actfreelist);
4901 if (!VI_TRYLOCK(vp)) {
4902 if (mp_ncpus == 1 || should_yield()) {
4903 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4904 mtx_unlock(&vnode_free_list_mtx);
4906 mtx_lock(&vnode_free_list_mtx);
4911 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
4912 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
4913 ("alien vnode on the active list %p %p", vp, mp));
4914 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
4916 nvp = TAILQ_NEXT(vp, v_actfreelist);
4921 /* Check if we are done */
4923 mtx_unlock(&vnode_free_list_mtx);
4924 mnt_vnode_markerfree_active(mvp, mp);
4927 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4928 mtx_unlock(&vnode_free_list_mtx);
4929 ASSERT_VI_LOCKED(vp, "active iter");
4930 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
4935 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4939 kern_yield(PRI_USER);
4940 mtx_lock(&vnode_free_list_mtx);
4941 return (mnt_vnode_next_active(mvp, mp));
4945 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
4949 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4953 (*mvp)->v_type = VMARKER;
4954 (*mvp)->v_mount = mp;
4956 mtx_lock(&vnode_free_list_mtx);
4957 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
4959 mtx_unlock(&vnode_free_list_mtx);
4960 mnt_vnode_markerfree_active(mvp, mp);
4963 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4964 return (mnt_vnode_next_active(mvp, mp));
4968 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4974 mtx_lock(&vnode_free_list_mtx);
4975 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4976 mtx_unlock(&vnode_free_list_mtx);
4977 mnt_vnode_markerfree_active(mvp, mp);