2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
38 * External virtual filesystem routines
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include "opt_compat.h"
46 #include "opt_watchdog.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/condvar.h>
54 #include <sys/dirent.h>
55 #include <sys/event.h>
56 #include <sys/eventhandler.h>
57 #include <sys/extattr.h>
59 #include <sys/fcntl.h>
62 #include <sys/kernel.h>
63 #include <sys/kthread.h>
64 #include <sys/lockf.h>
65 #include <sys/malloc.h>
66 #include <sys/mount.h>
67 #include <sys/namei.h>
68 #include <sys/pctrie.h>
70 #include <sys/reboot.h>
71 #include <sys/refcount.h>
72 #include <sys/rwlock.h>
73 #include <sys/sched.h>
74 #include <sys/sleepqueue.h>
77 #include <sys/sysctl.h>
78 #include <sys/syslog.h>
79 #include <sys/vmmeter.h>
80 #include <sys/vnode.h>
81 #include <sys/watchdog.h>
83 #include <machine/stdarg.h>
85 #include <security/mac/mac_framework.h>
88 #include <vm/vm_object.h>
89 #include <vm/vm_extern.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_page.h>
93 #include <vm/vm_kern.h>
100 static void delmntque(struct vnode *vp);
101 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
102 int slpflag, int slptimeo);
103 static void syncer_shutdown(void *arg, int howto);
104 static int vtryrecycle(struct vnode *vp);
105 static void v_init_counters(struct vnode *);
106 static void v_incr_usecount(struct vnode *);
107 static void v_incr_devcount(struct vnode *);
108 static void v_decr_devcount(struct vnode *);
109 static void vnlru_free(int);
110 static void vgonel(struct vnode *);
111 static void vfs_knllock(void *arg);
112 static void vfs_knlunlock(void *arg);
113 static void vfs_knl_assert_locked(void *arg);
114 static void vfs_knl_assert_unlocked(void *arg);
115 static void destroy_vpollinfo(struct vpollinfo *vi);
118 * Number of vnodes in existence. Increased whenever getnewvnode()
119 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
121 static unsigned long numvnodes;
123 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
124 "Number of vnodes in existence");
126 static u_long vnodes_created;
127 SYSCTL_ULONG(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
128 0, "Number of vnodes created by getnewvnode");
131 * Conversion tables for conversion from vnode types to inode formats
134 enum vtype iftovt_tab[16] = {
135 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
136 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
138 int vttoif_tab[10] = {
139 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
140 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
144 * List of vnodes that are ready for recycling.
146 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
149 * Free vnode target. Free vnodes may simply be files which have been stat'd
150 * but not read. This is somewhat common, and a small cache of such files
151 * should be kept to avoid recreation costs.
153 static u_long wantfreevnodes;
154 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
155 /* Number of vnodes in the free list. */
156 static u_long freevnodes;
157 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
158 "Number of vnodes in the free list");
160 static int vlru_allow_cache_src;
161 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
162 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
164 static u_long recycles_count;
165 SYSCTL_ULONG(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 0,
166 "Number of vnodes recycled to avoid exceding kern.maxvnodes");
169 * Various variables used for debugging the new implementation of
171 * XXX these are probably of (very) limited utility now.
173 static int reassignbufcalls;
174 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
175 "Number of calls to reassignbuf");
177 static u_long free_owe_inact;
178 SYSCTL_ULONG(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact, 0,
179 "Number of times free vnodes kept on active list due to VFS "
180 "owing inactivation");
183 * Cache for the mount type id assigned to NFS. This is used for
184 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
186 int nfs_mount_type = -1;
188 /* To keep more than one thread at a time from running vfs_getnewfsid */
189 static struct mtx mntid_mtx;
192 * Lock for any access to the following:
197 static struct mtx vnode_free_list_mtx;
199 /* Publicly exported FS */
200 struct nfs_public nfs_pub;
202 static uma_zone_t buf_trie_zone;
204 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
205 static uma_zone_t vnode_zone;
206 static uma_zone_t vnodepoll_zone;
209 * The workitem queue.
211 * It is useful to delay writes of file data and filesystem metadata
212 * for tens of seconds so that quickly created and deleted files need
213 * not waste disk bandwidth being created and removed. To realize this,
214 * we append vnodes to a "workitem" queue. When running with a soft
215 * updates implementation, most pending metadata dependencies should
216 * not wait for more than a few seconds. Thus, mounted on block devices
217 * are delayed only about a half the time that file data is delayed.
218 * Similarly, directory updates are more critical, so are only delayed
219 * about a third the time that file data is delayed. Thus, there are
220 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
221 * one each second (driven off the filesystem syncer process). The
222 * syncer_delayno variable indicates the next queue that is to be processed.
223 * Items that need to be processed soon are placed in this queue:
225 * syncer_workitem_pending[syncer_delayno]
227 * A delay of fifteen seconds is done by placing the request fifteen
228 * entries later in the queue:
230 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
233 static int syncer_delayno;
234 static long syncer_mask;
235 LIST_HEAD(synclist, bufobj);
236 static struct synclist *syncer_workitem_pending;
238 * The sync_mtx protects:
243 * syncer_workitem_pending
244 * syncer_worklist_len
247 static struct mtx sync_mtx;
248 static struct cv sync_wakeup;
250 #define SYNCER_MAXDELAY 32
251 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
252 static int syncdelay = 30; /* max time to delay syncing data */
253 static int filedelay = 30; /* time to delay syncing files */
254 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
255 "Time to delay syncing files (in seconds)");
256 static int dirdelay = 29; /* time to delay syncing directories */
257 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
258 "Time to delay syncing directories (in seconds)");
259 static int metadelay = 28; /* time to delay syncing metadata */
260 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
261 "Time to delay syncing metadata (in seconds)");
262 static int rushjob; /* number of slots to run ASAP */
263 static int stat_rush_requests; /* number of times I/O speeded up */
264 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
265 "Number of times I/O speeded up (rush requests)");
268 * When shutting down the syncer, run it at four times normal speed.
270 #define SYNCER_SHUTDOWN_SPEEDUP 4
271 static int sync_vnode_count;
272 static int syncer_worklist_len;
273 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
277 * Number of vnodes we want to exist at any one time. This is mostly used
278 * to size hash tables in vnode-related code. It is normally not used in
279 * getnewvnode(), as wantfreevnodes is normally nonzero.)
281 * XXX desiredvnodes is historical cruft and should not exist.
286 sysctl_update_desiredvnodes(SYSCTL_HANDLER_ARGS)
288 int error, old_desiredvnodes;
290 old_desiredvnodes = desiredvnodes;
291 if ((error = sysctl_handle_int(oidp, arg1, arg2, req)) != 0)
293 if (old_desiredvnodes != desiredvnodes) {
294 vfs_hash_changesize(desiredvnodes);
295 cache_changesize(desiredvnodes);
300 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
301 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &desiredvnodes, 0,
302 sysctl_update_desiredvnodes, "I", "Maximum number of vnodes");
303 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
304 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
305 static int vnlru_nowhere;
306 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
307 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
309 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
313 * Support for the bufobj clean & dirty pctrie.
316 buf_trie_alloc(struct pctrie *ptree)
319 return uma_zalloc(buf_trie_zone, M_NOWAIT);
323 buf_trie_free(struct pctrie *ptree, void *node)
326 uma_zfree(buf_trie_zone, node);
328 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
331 * Initialize the vnode management data structures.
333 * Reevaluate the following cap on the number of vnodes after the physical
334 * memory size exceeds 512GB. In the limit, as the physical memory size
335 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
337 #ifndef MAXVNODES_MAX
338 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
341 vntblinit(void *dummy __unused)
344 int physvnodes, virtvnodes;
347 * Desiredvnodes is a function of the physical memory size and the
348 * kernel's heap size. Generally speaking, it scales with the
349 * physical memory size. The ratio of desiredvnodes to physical pages
350 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
351 * marginal ratio of desiredvnodes to physical pages is one to
352 * sixteen. However, desiredvnodes is limited by the kernel's heap
353 * size. The memory required by desiredvnodes vnodes and vm objects
354 * may not exceed one seventh of the kernel's heap size.
356 physvnodes = maxproc + vm_cnt.v_page_count / 16 + 3 * min(98304 * 4,
357 vm_cnt.v_page_count) / 16;
358 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
359 sizeof(struct vnode)));
360 desiredvnodes = min(physvnodes, virtvnodes);
361 if (desiredvnodes > MAXVNODES_MAX) {
363 printf("Reducing kern.maxvnodes %d -> %d\n",
364 desiredvnodes, MAXVNODES_MAX);
365 desiredvnodes = MAXVNODES_MAX;
367 wantfreevnodes = desiredvnodes / 4;
368 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
369 TAILQ_INIT(&vnode_free_list);
370 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
371 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
372 NULL, NULL, UMA_ALIGN_PTR, 0);
373 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
374 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
376 * Preallocate enough nodes to support one-per buf so that
377 * we can not fail an insert. reassignbuf() callers can not
378 * tolerate the insertion failure.
380 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
381 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
382 UMA_ZONE_NOFREE | UMA_ZONE_VM);
383 uma_prealloc(buf_trie_zone, nbuf);
385 * Initialize the filesystem syncer.
387 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
389 syncer_maxdelay = syncer_mask + 1;
390 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
391 cv_init(&sync_wakeup, "syncer");
392 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
396 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
400 * Mark a mount point as busy. Used to synchronize access and to delay
401 * unmounting. Eventually, mountlist_mtx is not released on failure.
403 * vfs_busy() is a custom lock, it can block the caller.
404 * vfs_busy() only sleeps if the unmount is active on the mount point.
405 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
406 * vnode belonging to mp.
408 * Lookup uses vfs_busy() to traverse mount points.
410 * / vnode lock A / vnode lock (/var) D
411 * /var vnode lock B /log vnode lock(/var/log) E
412 * vfs_busy lock C vfs_busy lock F
414 * Within each file system, the lock order is C->A->B and F->D->E.
416 * When traversing across mounts, the system follows that lock order:
422 * The lookup() process for namei("/var") illustrates the process:
423 * VOP_LOOKUP() obtains B while A is held
424 * vfs_busy() obtains a shared lock on F while A and B are held
425 * vput() releases lock on B
426 * vput() releases lock on A
427 * VFS_ROOT() obtains lock on D while shared lock on F is held
428 * vfs_unbusy() releases shared lock on F
429 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
430 * Attempt to lock A (instead of vp_crossmp) while D is held would
431 * violate the global order, causing deadlocks.
433 * dounmount() locks B while F is drained.
436 vfs_busy(struct mount *mp, int flags)
439 MPASS((flags & ~MBF_MASK) == 0);
440 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
445 * If mount point is currenly being unmounted, sleep until the
446 * mount point fate is decided. If thread doing the unmounting fails,
447 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
448 * that this mount point has survived the unmount attempt and vfs_busy
449 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
450 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
451 * about to be really destroyed. vfs_busy needs to release its
452 * reference on the mount point in this case and return with ENOENT,
453 * telling the caller that mount mount it tried to busy is no longer
456 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
457 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
460 CTR1(KTR_VFS, "%s: failed busying before sleeping",
464 if (flags & MBF_MNTLSTLOCK)
465 mtx_unlock(&mountlist_mtx);
466 mp->mnt_kern_flag |= MNTK_MWAIT;
467 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
468 if (flags & MBF_MNTLSTLOCK)
469 mtx_lock(&mountlist_mtx);
472 if (flags & MBF_MNTLSTLOCK)
473 mtx_unlock(&mountlist_mtx);
480 * Free a busy filesystem.
483 vfs_unbusy(struct mount *mp)
486 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
489 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
491 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
492 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
493 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
494 mp->mnt_kern_flag &= ~MNTK_DRAINING;
495 wakeup(&mp->mnt_lockref);
501 * Lookup a mount point by filesystem identifier.
504 vfs_getvfs(fsid_t *fsid)
508 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
509 mtx_lock(&mountlist_mtx);
510 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
511 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
512 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
514 mtx_unlock(&mountlist_mtx);
518 mtx_unlock(&mountlist_mtx);
519 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
520 return ((struct mount *) 0);
524 * Lookup a mount point by filesystem identifier, busying it before
527 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
528 * cache for popular filesystem identifiers. The cache is lockess, using
529 * the fact that struct mount's are never freed. In worst case we may
530 * get pointer to unmounted or even different filesystem, so we have to
531 * check what we got, and go slow way if so.
534 vfs_busyfs(fsid_t *fsid)
536 #define FSID_CACHE_SIZE 256
537 typedef struct mount * volatile vmp_t;
538 static vmp_t cache[FSID_CACHE_SIZE];
543 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
544 hash = fsid->val[0] ^ fsid->val[1];
545 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
548 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
549 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
551 if (vfs_busy(mp, 0) != 0) {
555 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
556 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
562 mtx_lock(&mountlist_mtx);
563 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
564 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
565 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
566 error = vfs_busy(mp, MBF_MNTLSTLOCK);
569 mtx_unlock(&mountlist_mtx);
576 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
577 mtx_unlock(&mountlist_mtx);
578 return ((struct mount *) 0);
582 * Check if a user can access privileged mount options.
585 vfs_suser(struct mount *mp, struct thread *td)
590 * If the thread is jailed, but this is not a jail-friendly file
591 * system, deny immediately.
593 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
597 * If the file system was mounted outside the jail of the calling
598 * thread, deny immediately.
600 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
604 * If file system supports delegated administration, we don't check
605 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
606 * by the file system itself.
607 * If this is not the user that did original mount, we check for
608 * the PRIV_VFS_MOUNT_OWNER privilege.
610 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
611 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
612 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
619 * Get a new unique fsid. Try to make its val[0] unique, since this value
620 * will be used to create fake device numbers for stat(). Also try (but
621 * not so hard) make its val[0] unique mod 2^16, since some emulators only
622 * support 16-bit device numbers. We end up with unique val[0]'s for the
623 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
625 * Keep in mind that several mounts may be running in parallel. Starting
626 * the search one past where the previous search terminated is both a
627 * micro-optimization and a defense against returning the same fsid to
631 vfs_getnewfsid(struct mount *mp)
633 static uint16_t mntid_base;
638 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
639 mtx_lock(&mntid_mtx);
640 mtype = mp->mnt_vfc->vfc_typenum;
641 tfsid.val[1] = mtype;
642 mtype = (mtype & 0xFF) << 24;
644 tfsid.val[0] = makedev(255,
645 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
647 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
651 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
652 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
653 mtx_unlock(&mntid_mtx);
657 * Knob to control the precision of file timestamps:
659 * 0 = seconds only; nanoseconds zeroed.
660 * 1 = seconds and nanoseconds, accurate within 1/HZ.
661 * 2 = seconds and nanoseconds, truncated to microseconds.
662 * >=3 = seconds and nanoseconds, maximum precision.
664 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
666 static int timestamp_precision = TSP_USEC;
667 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
668 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
669 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
670 "3+: sec + ns (max. precision))");
673 * Get a current timestamp.
676 vfs_timestamp(struct timespec *tsp)
680 switch (timestamp_precision) {
682 tsp->tv_sec = time_second;
690 TIMEVAL_TO_TIMESPEC(&tv, tsp);
700 * Set vnode attributes to VNOVAL
703 vattr_null(struct vattr *vap)
707 vap->va_size = VNOVAL;
708 vap->va_bytes = VNOVAL;
709 vap->va_mode = VNOVAL;
710 vap->va_nlink = VNOVAL;
711 vap->va_uid = VNOVAL;
712 vap->va_gid = VNOVAL;
713 vap->va_fsid = VNOVAL;
714 vap->va_fileid = VNOVAL;
715 vap->va_blocksize = VNOVAL;
716 vap->va_rdev = VNOVAL;
717 vap->va_atime.tv_sec = VNOVAL;
718 vap->va_atime.tv_nsec = VNOVAL;
719 vap->va_mtime.tv_sec = VNOVAL;
720 vap->va_mtime.tv_nsec = VNOVAL;
721 vap->va_ctime.tv_sec = VNOVAL;
722 vap->va_ctime.tv_nsec = VNOVAL;
723 vap->va_birthtime.tv_sec = VNOVAL;
724 vap->va_birthtime.tv_nsec = VNOVAL;
725 vap->va_flags = VNOVAL;
726 vap->va_gen = VNOVAL;
731 * This routine is called when we have too many vnodes. It attempts
732 * to free <count> vnodes and will potentially free vnodes that still
733 * have VM backing store (VM backing store is typically the cause
734 * of a vnode blowout so we want to do this). Therefore, this operation
735 * is not considered cheap.
737 * A number of conditions may prevent a vnode from being reclaimed.
738 * the buffer cache may have references on the vnode, a directory
739 * vnode may still have references due to the namei cache representing
740 * underlying files, or the vnode may be in active use. It is not
741 * desireable to reuse such vnodes. These conditions may cause the
742 * number of vnodes to reach some minimum value regardless of what
743 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
746 vlrureclaim(struct mount *mp)
755 * Calculate the trigger point, don't allow user
756 * screwups to blow us up. This prevents us from
757 * recycling vnodes with lots of resident pages. We
758 * aren't trying to free memory, we are trying to
761 usevnodes = desiredvnodes;
764 trigger = vm_cnt.v_page_count * 2 / usevnodes;
766 vn_start_write(NULL, &mp, V_WAIT);
768 count = mp->mnt_nvnodelistsize / 10 + 1;
770 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
771 while (vp != NULL && vp->v_type == VMARKER)
772 vp = TAILQ_NEXT(vp, v_nmntvnodes);
775 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
776 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
781 * If it's been deconstructed already, it's still
782 * referenced, or it exceeds the trigger, skip it.
784 if (vp->v_usecount ||
785 (!vlru_allow_cache_src &&
786 !LIST_EMPTY(&(vp)->v_cache_src)) ||
787 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
788 vp->v_object->resident_page_count > trigger)) {
794 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
796 goto next_iter_mntunlocked;
800 * v_usecount may have been bumped after VOP_LOCK() dropped
801 * the vnode interlock and before it was locked again.
803 * It is not necessary to recheck VI_DOOMED because it can
804 * only be set by another thread that holds both the vnode
805 * lock and vnode interlock. If another thread has the
806 * vnode lock before we get to VOP_LOCK() and obtains the
807 * vnode interlock after VOP_LOCK() drops the vnode
808 * interlock, the other thread will be unable to drop the
809 * vnode lock before our VOP_LOCK() call fails.
811 if (vp->v_usecount ||
812 (!vlru_allow_cache_src &&
813 !LIST_EMPTY(&(vp)->v_cache_src)) ||
814 (vp->v_object != NULL &&
815 vp->v_object->resident_page_count > trigger)) {
816 VOP_UNLOCK(vp, LK_INTERLOCK);
818 goto next_iter_mntunlocked;
820 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
821 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
822 atomic_add_long(&recycles_count, 1);
827 next_iter_mntunlocked:
836 kern_yield(PRI_USER);
841 vn_finished_write(mp);
846 * Attempt to keep the free list at wantfreevnodes length.
849 vnlru_free(int count)
853 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
854 for (; count > 0; count--) {
855 vp = TAILQ_FIRST(&vnode_free_list);
857 * The list can be modified while the free_list_mtx
858 * has been dropped and vp could be NULL here.
862 VNASSERT(vp->v_op != NULL, vp,
863 ("vnlru_free: vnode already reclaimed."));
864 KASSERT((vp->v_iflag & VI_FREE) != 0,
865 ("Removing vnode not on freelist"));
866 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
867 ("Mangling active vnode"));
868 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
870 * Don't recycle if we can't get the interlock.
872 if (!VI_TRYLOCK(vp)) {
873 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
876 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
877 vp, ("vp inconsistent on freelist"));
880 * The clear of VI_FREE prevents activation of the
881 * vnode. There is no sense in putting the vnode on
882 * the mount point active list, only to remove it
883 * later during recycling. Inline the relevant part
884 * of vholdl(), to avoid triggering assertions or
888 vp->v_iflag &= ~VI_FREE;
889 refcount_acquire(&vp->v_holdcnt);
891 mtx_unlock(&vnode_free_list_mtx);
895 * If the recycled succeeded this vdrop will actually free
896 * the vnode. If not it will simply place it back on
900 mtx_lock(&vnode_free_list_mtx);
904 * Attempt to recycle vnodes in a context that is always safe to block.
905 * Calling vlrurecycle() from the bowels of filesystem code has some
906 * interesting deadlock problems.
908 static struct proc *vnlruproc;
909 static int vnlruproc_sig;
914 struct mount *mp, *nmp;
916 struct proc *p = vnlruproc;
918 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
922 kproc_suspend_check(p);
923 mtx_lock(&vnode_free_list_mtx);
924 if (freevnodes > wantfreevnodes)
925 vnlru_free(freevnodes - wantfreevnodes);
926 if (numvnodes <= desiredvnodes * 9 / 10) {
928 wakeup(&vnlruproc_sig);
929 msleep(vnlruproc, &vnode_free_list_mtx,
930 PVFS|PDROP, "vlruwt", hz);
933 mtx_unlock(&vnode_free_list_mtx);
935 mtx_lock(&mountlist_mtx);
936 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
937 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
938 nmp = TAILQ_NEXT(mp, mnt_list);
941 done += vlrureclaim(mp);
942 mtx_lock(&mountlist_mtx);
943 nmp = TAILQ_NEXT(mp, mnt_list);
946 mtx_unlock(&mountlist_mtx);
949 /* These messages are temporary debugging aids */
950 if (vnlru_nowhere < 5)
951 printf("vnlru process getting nowhere..\n");
952 else if (vnlru_nowhere == 5)
953 printf("vnlru process messages stopped.\n");
956 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
958 kern_yield(PRI_USER);
962 static struct kproc_desc vnlru_kp = {
967 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
971 * Routines having to do with the management of the vnode table.
975 * Try to recycle a freed vnode. We abort if anyone picks up a reference
976 * before we actually vgone(). This function must be called with the vnode
977 * held to prevent the vnode from being returned to the free list midway
981 vtryrecycle(struct vnode *vp)
985 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
986 VNASSERT(vp->v_holdcnt, vp,
987 ("vtryrecycle: Recycling vp %p without a reference.", vp));
989 * This vnode may found and locked via some other list, if so we
990 * can't recycle it yet.
992 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
994 "%s: impossible to recycle, vp %p lock is already held",
996 return (EWOULDBLOCK);
999 * Don't recycle if its filesystem is being suspended.
1001 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1004 "%s: impossible to recycle, cannot start the write for %p",
1009 * If we got this far, we need to acquire the interlock and see if
1010 * anyone picked up this vnode from another list. If not, we will
1011 * mark it with DOOMED via vgonel() so that anyone who does find it
1012 * will skip over it.
1015 if (vp->v_usecount) {
1016 VOP_UNLOCK(vp, LK_INTERLOCK);
1017 vn_finished_write(vnmp);
1019 "%s: impossible to recycle, %p is already referenced",
1023 if ((vp->v_iflag & VI_DOOMED) == 0) {
1024 atomic_add_long(&recycles_count, 1);
1027 VOP_UNLOCK(vp, LK_INTERLOCK);
1028 vn_finished_write(vnmp);
1033 * Wait for available vnodes.
1036 getnewvnode_wait(int suspended)
1039 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1040 if (numvnodes > desiredvnodes) {
1043 * File system is beeing suspended, we cannot risk a
1044 * deadlock here, so allocate new vnode anyway.
1046 if (freevnodes > wantfreevnodes)
1047 vnlru_free(freevnodes - wantfreevnodes);
1050 if (vnlruproc_sig == 0) {
1051 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1054 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1057 return (numvnodes > desiredvnodes ? ENFILE : 0);
1061 getnewvnode_reserve(u_int count)
1066 /* First try to be quick and racy. */
1067 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1068 td->td_vp_reserv += count;
1071 atomic_subtract_long(&numvnodes, count);
1073 mtx_lock(&vnode_free_list_mtx);
1075 if (getnewvnode_wait(0) == 0) {
1078 atomic_add_long(&numvnodes, 1);
1081 mtx_unlock(&vnode_free_list_mtx);
1085 getnewvnode_drop_reserve(void)
1090 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1091 td->td_vp_reserv = 0;
1095 * Return the next vnode from the free list.
1098 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1106 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1109 if (td->td_vp_reserv > 0) {
1110 td->td_vp_reserv -= 1;
1113 mtx_lock(&vnode_free_list_mtx);
1115 * Lend our context to reclaim vnodes if they've exceeded the max.
1117 if (freevnodes > wantfreevnodes)
1119 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1121 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1123 mtx_unlock(&vnode_free_list_mtx);
1127 atomic_add_long(&numvnodes, 1);
1128 mtx_unlock(&vnode_free_list_mtx);
1130 atomic_add_long(&vnodes_created, 1);
1131 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1135 vp->v_vnlock = &vp->v_lock;
1136 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1138 * By default, don't allow shared locks unless filesystems
1141 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE | LK_IS_VNODE);
1143 * Initialize bufobj.
1146 bo->__bo_vnode = vp;
1147 rw_init(BO_LOCKPTR(bo), "bufobj interlock");
1148 bo->bo_ops = &buf_ops_bio;
1149 bo->bo_private = vp;
1150 TAILQ_INIT(&bo->bo_clean.bv_hd);
1151 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1153 * Initialize namecache.
1155 LIST_INIT(&vp->v_cache_src);
1156 TAILQ_INIT(&vp->v_cache_dst);
1158 * Finalize various vnode identity bits.
1163 v_init_counters(vp);
1167 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1168 mac_vnode_associate_singlelabel(mp, vp);
1169 else if (mp == NULL && vops != &dead_vnodeops)
1170 printf("NULL mp in getnewvnode()\n");
1173 bo->bo_bsize = mp->mnt_stat.f_iosize;
1174 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1175 vp->v_vflag |= VV_NOKNOTE;
1177 rangelock_init(&vp->v_rl);
1180 * For the filesystems which do not use vfs_hash_insert(),
1181 * still initialize v_hash to have vfs_hash_index() useful.
1182 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1185 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1192 * Delete from old mount point vnode list, if on one.
1195 delmntque(struct vnode *vp)
1205 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1206 ("Active vnode list size %d > Vnode list size %d",
1207 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1208 active = vp->v_iflag & VI_ACTIVE;
1209 vp->v_iflag &= ~VI_ACTIVE;
1211 mtx_lock(&vnode_free_list_mtx);
1212 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1213 mp->mnt_activevnodelistsize--;
1214 mtx_unlock(&vnode_free_list_mtx);
1218 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1219 ("bad mount point vnode list size"));
1220 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1221 mp->mnt_nvnodelistsize--;
1227 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1231 vp->v_op = &dead_vnodeops;
1237 * Insert into list of vnodes for the new mount point, if available.
1240 insmntque1(struct vnode *vp, struct mount *mp,
1241 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1244 KASSERT(vp->v_mount == NULL,
1245 ("insmntque: vnode already on per mount vnode list"));
1246 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1247 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1250 * We acquire the vnode interlock early to ensure that the
1251 * vnode cannot be recycled by another process releasing a
1252 * holdcnt on it before we get it on both the vnode list
1253 * and the active vnode list. The mount mutex protects only
1254 * manipulation of the vnode list and the vnode freelist
1255 * mutex protects only manipulation of the active vnode list.
1256 * Hence the need to hold the vnode interlock throughout.
1260 if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1261 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1262 mp->mnt_nvnodelistsize == 0)) &&
1263 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1272 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1273 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1274 ("neg mount point vnode list size"));
1275 mp->mnt_nvnodelistsize++;
1276 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1277 ("Activating already active vnode"));
1278 vp->v_iflag |= VI_ACTIVE;
1279 mtx_lock(&vnode_free_list_mtx);
1280 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1281 mp->mnt_activevnodelistsize++;
1282 mtx_unlock(&vnode_free_list_mtx);
1289 insmntque(struct vnode *vp, struct mount *mp)
1292 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1296 * Flush out and invalidate all buffers associated with a bufobj
1297 * Called with the underlying object locked.
1300 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1305 if (flags & V_SAVE) {
1306 error = bufobj_wwait(bo, slpflag, slptimeo);
1311 if (bo->bo_dirty.bv_cnt > 0) {
1313 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1316 * XXX We could save a lock/unlock if this was only
1317 * enabled under INVARIANTS
1320 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1321 panic("vinvalbuf: dirty bufs");
1325 * If you alter this loop please notice that interlock is dropped and
1326 * reacquired in flushbuflist. Special care is needed to ensure that
1327 * no race conditions occur from this.
1330 error = flushbuflist(&bo->bo_clean,
1331 flags, bo, slpflag, slptimeo);
1332 if (error == 0 && !(flags & V_CLEANONLY))
1333 error = flushbuflist(&bo->bo_dirty,
1334 flags, bo, slpflag, slptimeo);
1335 if (error != 0 && error != EAGAIN) {
1339 } while (error != 0);
1342 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1343 * have write I/O in-progress but if there is a VM object then the
1344 * VM object can also have read-I/O in-progress.
1347 bufobj_wwait(bo, 0, 0);
1349 if (bo->bo_object != NULL) {
1350 VM_OBJECT_WLOCK(bo->bo_object);
1351 vm_object_pip_wait(bo->bo_object, "bovlbx");
1352 VM_OBJECT_WUNLOCK(bo->bo_object);
1355 } while (bo->bo_numoutput > 0);
1359 * Destroy the copy in the VM cache, too.
1361 if (bo->bo_object != NULL &&
1362 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1363 VM_OBJECT_WLOCK(bo->bo_object);
1364 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1365 OBJPR_CLEANONLY : 0);
1366 VM_OBJECT_WUNLOCK(bo->bo_object);
1371 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1372 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1373 panic("vinvalbuf: flush failed");
1380 * Flush out and invalidate all buffers associated with a vnode.
1381 * Called with the underlying object locked.
1384 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1387 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1388 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1389 if (vp->v_object != NULL && vp->v_object->handle != vp)
1391 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1395 * Flush out buffers on the specified list.
1399 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1402 struct buf *bp, *nbp;
1407 ASSERT_BO_WLOCKED(bo);
1410 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1411 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1412 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1418 lblkno = nbp->b_lblkno;
1419 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1422 error = BUF_TIMELOCK(bp,
1423 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1424 "flushbuf", slpflag, slptimeo);
1427 return (error != ENOLCK ? error : EAGAIN);
1429 KASSERT(bp->b_bufobj == bo,
1430 ("bp %p wrong b_bufobj %p should be %p",
1431 bp, bp->b_bufobj, bo));
1433 * XXX Since there are no node locks for NFS, I
1434 * believe there is a slight chance that a delayed
1435 * write will occur while sleeping just above, so
1438 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1441 bp->b_flags |= B_ASYNC;
1444 return (EAGAIN); /* XXX: why not loop ? */
1447 bp->b_flags |= (B_INVAL | B_RELBUF);
1448 bp->b_flags &= ~B_ASYNC;
1452 (nbp->b_bufobj != bo ||
1453 nbp->b_lblkno != lblkno ||
1454 (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1455 break; /* nbp invalid */
1461 * Truncate a file's buffer and pages to a specified length. This
1462 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1466 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
1468 struct buf *bp, *nbp;
1473 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1474 vp, cred, blksize, (uintmax_t)length);
1477 * Round up to the *next* lbn.
1479 trunclbn = (length + blksize - 1) / blksize;
1481 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1488 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1489 if (bp->b_lblkno < trunclbn)
1492 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1493 BO_LOCKPTR(bo)) == ENOLCK)
1497 bp->b_flags |= (B_INVAL | B_RELBUF);
1498 bp->b_flags &= ~B_ASYNC;
1504 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1505 (nbp->b_vp != vp) ||
1506 (nbp->b_flags & B_DELWRI))) {
1512 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1513 if (bp->b_lblkno < trunclbn)
1516 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1517 BO_LOCKPTR(bo)) == ENOLCK)
1520 bp->b_flags |= (B_INVAL | B_RELBUF);
1521 bp->b_flags &= ~B_ASYNC;
1527 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1528 (nbp->b_vp != vp) ||
1529 (nbp->b_flags & B_DELWRI) == 0)) {
1538 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1539 if (bp->b_lblkno > 0)
1542 * Since we hold the vnode lock this should only
1543 * fail if we're racing with the buf daemon.
1546 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1547 BO_LOCKPTR(bo)) == ENOLCK) {
1550 VNASSERT((bp->b_flags & B_DELWRI), vp,
1551 ("buf(%p) on dirty queue without DELWRI", bp));
1560 bufobj_wwait(bo, 0, 0);
1562 vnode_pager_setsize(vp, length);
1568 buf_vlist_remove(struct buf *bp)
1572 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1573 ASSERT_BO_WLOCKED(bp->b_bufobj);
1574 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1575 (BX_VNDIRTY|BX_VNCLEAN),
1576 ("buf_vlist_remove: Buf %p is on two lists", bp));
1577 if (bp->b_xflags & BX_VNDIRTY)
1578 bv = &bp->b_bufobj->bo_dirty;
1580 bv = &bp->b_bufobj->bo_clean;
1581 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
1582 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1584 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1588 * Add the buffer to the sorted clean or dirty block list.
1590 * NOTE: xflags is passed as a constant, optimizing this inline function!
1593 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1599 ASSERT_BO_WLOCKED(bo);
1600 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
1601 ("dead bo %p", bo));
1602 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1603 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1604 bp->b_xflags |= xflags;
1605 if (xflags & BX_VNDIRTY)
1611 * Keep the list ordered. Optimize empty list insertion. Assume
1612 * we tend to grow at the tail so lookup_le should usually be cheaper
1615 if (bv->bv_cnt == 0 ||
1616 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
1617 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1618 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
1619 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
1621 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
1622 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
1624 panic("buf_vlist_add: Preallocated nodes insufficient.");
1629 * Look up a buffer using the buffer tries.
1632 gbincore(struct bufobj *bo, daddr_t lblkno)
1636 ASSERT_BO_LOCKED(bo);
1637 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
1640 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
1644 * Associate a buffer with a vnode.
1647 bgetvp(struct vnode *vp, struct buf *bp)
1652 ASSERT_BO_WLOCKED(bo);
1653 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1655 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1656 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1657 ("bgetvp: bp already attached! %p", bp));
1663 * Insert onto list for new vnode.
1665 buf_vlist_add(bp, bo, BX_VNCLEAN);
1669 * Disassociate a buffer from a vnode.
1672 brelvp(struct buf *bp)
1677 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1678 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1681 * Delete from old vnode list, if on one.
1683 vp = bp->b_vp; /* XXX */
1686 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1687 buf_vlist_remove(bp);
1689 panic("brelvp: Buffer %p not on queue.", bp);
1690 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1691 bo->bo_flag &= ~BO_ONWORKLST;
1692 mtx_lock(&sync_mtx);
1693 LIST_REMOVE(bo, bo_synclist);
1694 syncer_worklist_len--;
1695 mtx_unlock(&sync_mtx);
1698 bp->b_bufobj = NULL;
1704 * Add an item to the syncer work queue.
1707 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1711 ASSERT_BO_WLOCKED(bo);
1713 mtx_lock(&sync_mtx);
1714 if (bo->bo_flag & BO_ONWORKLST)
1715 LIST_REMOVE(bo, bo_synclist);
1717 bo->bo_flag |= BO_ONWORKLST;
1718 syncer_worklist_len++;
1721 if (delay > syncer_maxdelay - 2)
1722 delay = syncer_maxdelay - 2;
1723 slot = (syncer_delayno + delay) & syncer_mask;
1725 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
1726 mtx_unlock(&sync_mtx);
1730 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1734 mtx_lock(&sync_mtx);
1735 len = syncer_worklist_len - sync_vnode_count;
1736 mtx_unlock(&sync_mtx);
1737 error = SYSCTL_OUT(req, &len, sizeof(len));
1741 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1742 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1744 static struct proc *updateproc;
1745 static void sched_sync(void);
1746 static struct kproc_desc up_kp = {
1751 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1754 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1759 *bo = LIST_FIRST(slp);
1762 vp = (*bo)->__bo_vnode; /* XXX */
1763 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1766 * We use vhold in case the vnode does not
1767 * successfully sync. vhold prevents the vnode from
1768 * going away when we unlock the sync_mtx so that
1769 * we can acquire the vnode interlock.
1772 mtx_unlock(&sync_mtx);
1774 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1776 mtx_lock(&sync_mtx);
1777 return (*bo == LIST_FIRST(slp));
1779 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1780 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1782 vn_finished_write(mp);
1784 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1786 * Put us back on the worklist. The worklist
1787 * routine will remove us from our current
1788 * position and then add us back in at a later
1791 vn_syncer_add_to_worklist(*bo, syncdelay);
1795 mtx_lock(&sync_mtx);
1799 static int first_printf = 1;
1802 * System filesystem synchronizer daemon.
1807 struct synclist *next, *slp;
1810 struct thread *td = curthread;
1812 int net_worklist_len;
1813 int syncer_final_iter;
1817 syncer_final_iter = 0;
1818 syncer_state = SYNCER_RUNNING;
1819 starttime = time_uptime;
1820 td->td_pflags |= TDP_NORUNNINGBUF;
1822 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1825 mtx_lock(&sync_mtx);
1827 if (syncer_state == SYNCER_FINAL_DELAY &&
1828 syncer_final_iter == 0) {
1829 mtx_unlock(&sync_mtx);
1830 kproc_suspend_check(td->td_proc);
1831 mtx_lock(&sync_mtx);
1833 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1834 if (syncer_state != SYNCER_RUNNING &&
1835 starttime != time_uptime) {
1837 printf("\nSyncing disks, vnodes remaining...");
1840 printf("%d ", net_worklist_len);
1842 starttime = time_uptime;
1845 * Push files whose dirty time has expired. Be careful
1846 * of interrupt race on slp queue.
1848 * Skip over empty worklist slots when shutting down.
1851 slp = &syncer_workitem_pending[syncer_delayno];
1852 syncer_delayno += 1;
1853 if (syncer_delayno == syncer_maxdelay)
1855 next = &syncer_workitem_pending[syncer_delayno];
1857 * If the worklist has wrapped since the
1858 * it was emptied of all but syncer vnodes,
1859 * switch to the FINAL_DELAY state and run
1860 * for one more second.
1862 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1863 net_worklist_len == 0 &&
1864 last_work_seen == syncer_delayno) {
1865 syncer_state = SYNCER_FINAL_DELAY;
1866 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1868 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1869 syncer_worklist_len > 0);
1872 * Keep track of the last time there was anything
1873 * on the worklist other than syncer vnodes.
1874 * Return to the SHUTTING_DOWN state if any
1877 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1878 last_work_seen = syncer_delayno;
1879 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1880 syncer_state = SYNCER_SHUTTING_DOWN;
1881 while (!LIST_EMPTY(slp)) {
1882 error = sync_vnode(slp, &bo, td);
1884 LIST_REMOVE(bo, bo_synclist);
1885 LIST_INSERT_HEAD(next, bo, bo_synclist);
1889 if (first_printf == 0) {
1891 * Drop the sync mutex, because some watchdog
1892 * drivers need to sleep while patting
1894 mtx_unlock(&sync_mtx);
1895 wdog_kern_pat(WD_LASTVAL);
1896 mtx_lock(&sync_mtx);
1900 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1901 syncer_final_iter--;
1903 * The variable rushjob allows the kernel to speed up the
1904 * processing of the filesystem syncer process. A rushjob
1905 * value of N tells the filesystem syncer to process the next
1906 * N seconds worth of work on its queue ASAP. Currently rushjob
1907 * is used by the soft update code to speed up the filesystem
1908 * syncer process when the incore state is getting so far
1909 * ahead of the disk that the kernel memory pool is being
1910 * threatened with exhaustion.
1917 * Just sleep for a short period of time between
1918 * iterations when shutting down to allow some I/O
1921 * If it has taken us less than a second to process the
1922 * current work, then wait. Otherwise start right over
1923 * again. We can still lose time if any single round
1924 * takes more than two seconds, but it does not really
1925 * matter as we are just trying to generally pace the
1926 * filesystem activity.
1928 if (syncer_state != SYNCER_RUNNING ||
1929 time_uptime == starttime) {
1931 sched_prio(td, PPAUSE);
1934 if (syncer_state != SYNCER_RUNNING)
1935 cv_timedwait(&sync_wakeup, &sync_mtx,
1936 hz / SYNCER_SHUTDOWN_SPEEDUP);
1937 else if (time_uptime == starttime)
1938 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1943 * Request the syncer daemon to speed up its work.
1944 * We never push it to speed up more than half of its
1945 * normal turn time, otherwise it could take over the cpu.
1948 speedup_syncer(void)
1952 mtx_lock(&sync_mtx);
1953 if (rushjob < syncdelay / 2) {
1955 stat_rush_requests += 1;
1958 mtx_unlock(&sync_mtx);
1959 cv_broadcast(&sync_wakeup);
1964 * Tell the syncer to speed up its work and run though its work
1965 * list several times, then tell it to shut down.
1968 syncer_shutdown(void *arg, int howto)
1971 if (howto & RB_NOSYNC)
1973 mtx_lock(&sync_mtx);
1974 syncer_state = SYNCER_SHUTTING_DOWN;
1976 mtx_unlock(&sync_mtx);
1977 cv_broadcast(&sync_wakeup);
1978 kproc_shutdown(arg, howto);
1982 syncer_suspend(void)
1985 syncer_shutdown(updateproc, 0);
1992 mtx_lock(&sync_mtx);
1994 syncer_state = SYNCER_RUNNING;
1995 mtx_unlock(&sync_mtx);
1996 cv_broadcast(&sync_wakeup);
1997 kproc_resume(updateproc);
2001 * Reassign a buffer from one vnode to another.
2002 * Used to assign file specific control information
2003 * (indirect blocks) to the vnode to which they belong.
2006 reassignbuf(struct buf *bp)
2019 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2020 bp, bp->b_vp, bp->b_flags);
2022 * B_PAGING flagged buffers cannot be reassigned because their vp
2023 * is not fully linked in.
2025 if (bp->b_flags & B_PAGING)
2026 panic("cannot reassign paging buffer");
2029 * Delete from old vnode list, if on one.
2032 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2033 buf_vlist_remove(bp);
2035 panic("reassignbuf: Buffer %p not on queue.", bp);
2037 * If dirty, put on list of dirty buffers; otherwise insert onto list
2040 if (bp->b_flags & B_DELWRI) {
2041 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2042 switch (vp->v_type) {
2052 vn_syncer_add_to_worklist(bo, delay);
2054 buf_vlist_add(bp, bo, BX_VNDIRTY);
2056 buf_vlist_add(bp, bo, BX_VNCLEAN);
2058 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2059 mtx_lock(&sync_mtx);
2060 LIST_REMOVE(bo, bo_synclist);
2061 syncer_worklist_len--;
2062 mtx_unlock(&sync_mtx);
2063 bo->bo_flag &= ~BO_ONWORKLST;
2068 bp = TAILQ_FIRST(&bv->bv_hd);
2069 KASSERT(bp == NULL || bp->b_bufobj == bo,
2070 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2071 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2072 KASSERT(bp == NULL || bp->b_bufobj == bo,
2073 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2075 bp = TAILQ_FIRST(&bv->bv_hd);
2076 KASSERT(bp == NULL || bp->b_bufobj == bo,
2077 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2078 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2079 KASSERT(bp == NULL || bp->b_bufobj == bo,
2080 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2086 * A temporary hack until refcount_* APIs are sorted out.
2089 vfs_refcount_acquire_if_not_zero(volatile u_int *count)
2097 if (atomic_cmpset_int(count, old, old + 1))
2103 vfs_refcount_release_if_not_last(volatile u_int *count)
2111 if (atomic_cmpset_int(count, old, old - 1))
2117 v_init_counters(struct vnode *vp)
2120 VNASSERT(vp->v_type == VNON && vp->v_data == NULL && vp->v_iflag == 0,
2121 vp, ("%s called for an initialized vnode", __FUNCTION__));
2122 ASSERT_VI_UNLOCKED(vp, __FUNCTION__);
2124 refcount_init(&vp->v_holdcnt, 1);
2125 refcount_init(&vp->v_usecount, 1);
2129 * Increment the use and hold counts on the vnode, taking care to reference
2130 * the driver's usecount if this is a chardev. The _vhold() will remove
2131 * the vnode from the free list if it is presently free.
2134 v_incr_usecount(struct vnode *vp)
2137 ASSERT_VI_UNLOCKED(vp, __func__);
2138 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2140 if (vp->v_type == VCHR) {
2143 if (vp->v_iflag & VI_OWEINACT) {
2144 VNASSERT(vp->v_usecount == 0, vp,
2145 ("vnode with usecount and VI_OWEINACT set"));
2146 vp->v_iflag &= ~VI_OWEINACT;
2148 refcount_acquire(&vp->v_usecount);
2149 v_incr_devcount(vp);
2155 if (vfs_refcount_acquire_if_not_zero(&vp->v_usecount)) {
2156 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2157 ("vnode with usecount and VI_OWEINACT set"));
2160 if (vp->v_iflag & VI_OWEINACT)
2161 vp->v_iflag &= ~VI_OWEINACT;
2162 refcount_acquire(&vp->v_usecount);
2168 * Increment si_usecount of the associated device, if any.
2171 v_incr_devcount(struct vnode *vp)
2174 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2175 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2177 vp->v_rdev->si_usecount++;
2183 * Decrement si_usecount of the associated device, if any.
2186 v_decr_devcount(struct vnode *vp)
2189 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2190 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2192 vp->v_rdev->si_usecount--;
2198 * Grab a particular vnode from the free list, increment its
2199 * reference count and lock it. VI_DOOMED is set if the vnode
2200 * is being destroyed. Only callers who specify LK_RETRY will
2201 * see doomed vnodes. If inactive processing was delayed in
2202 * vput try to do it here.
2204 * Notes on lockless counter manipulation:
2205 * _vhold, vputx and other routines make various decisions based
2206 * on either holdcnt or usecount being 0. As long as either contuner
2207 * is not transitioning 0->1 nor 1->0, the manipulation can be done
2208 * with atomic operations. Otherwise the interlock is taken.
2211 vget(struct vnode *vp, int flags, struct thread *td)
2213 int error, oweinact;
2215 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2216 ("vget: invalid lock operation"));
2218 if ((flags & LK_INTERLOCK) != 0)
2219 ASSERT_VI_LOCKED(vp, __func__);
2221 ASSERT_VI_UNLOCKED(vp, __func__);
2222 if ((flags & LK_VNHELD) != 0)
2223 VNASSERT((vp->v_holdcnt > 0), vp,
2224 ("vget: LK_VNHELD passed but vnode not held"));
2226 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2228 if ((flags & LK_VNHELD) == 0)
2229 _vhold(vp, (flags & LK_INTERLOCK) != 0);
2231 if ((error = vn_lock(vp, flags)) != 0) {
2233 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2237 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2238 panic("vget: vn_lock failed to return ENOENT\n");
2240 * We don't guarantee that any particular close will
2241 * trigger inactive processing so just make a best effort
2242 * here at preventing a reference to a removed file. If
2243 * we don't succeed no harm is done.
2245 * Upgrade our holdcnt to a usecount.
2247 if (vp->v_type != VCHR &&
2248 vfs_refcount_acquire_if_not_zero(&vp->v_usecount)) {
2249 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2250 ("vnode with usecount and VI_OWEINACT set"));
2253 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2257 vp->v_iflag &= ~VI_OWEINACT;
2259 refcount_acquire(&vp->v_usecount);
2260 v_incr_devcount(vp);
2261 if (oweinact && VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2262 (flags & LK_NOWAIT) == 0)
2270 * Increase the reference count of a vnode.
2273 vref(struct vnode *vp)
2276 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2277 v_incr_usecount(vp);
2281 * Return reference count of a vnode.
2283 * The results of this call are only guaranteed when some mechanism is used to
2284 * stop other processes from gaining references to the vnode. This may be the
2285 * case if the caller holds the only reference. This is also useful when stale
2286 * data is acceptable as race conditions may be accounted for by some other
2290 vrefcnt(struct vnode *vp)
2293 return (vp->v_usecount);
2296 #define VPUTX_VRELE 1
2297 #define VPUTX_VPUT 2
2298 #define VPUTX_VUNREF 3
2301 * Decrement the use and hold counts for a vnode.
2303 * See an explanation near vget() as to why atomic operation is safe.
2306 vputx(struct vnode *vp, int func)
2310 KASSERT(vp != NULL, ("vputx: null vp"));
2311 if (func == VPUTX_VUNREF)
2312 ASSERT_VOP_LOCKED(vp, "vunref");
2313 else if (func == VPUTX_VPUT)
2314 ASSERT_VOP_LOCKED(vp, "vput");
2316 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2317 ASSERT_VI_UNLOCKED(vp, __func__);
2318 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2320 if (vp->v_type != VCHR &&
2321 vfs_refcount_release_if_not_last(&vp->v_usecount)) {
2322 if (func == VPUTX_VPUT)
2331 * We want to hold the vnode until the inactive finishes to
2332 * prevent vgone() races. We drop the use count here and the
2333 * hold count below when we're done.
2335 if (!refcount_release(&vp->v_usecount) ||
2336 (vp->v_iflag & VI_DOINGINACT)) {
2337 if (func == VPUTX_VPUT)
2339 v_decr_devcount(vp);
2344 v_decr_devcount(vp);
2348 if (vp->v_usecount != 0) {
2349 vprint("vputx: usecount not zero", vp);
2350 panic("vputx: usecount not zero");
2353 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2356 * We must call VOP_INACTIVE with the node locked. Mark
2357 * as VI_DOINGINACT to avoid recursion.
2359 vp->v_iflag |= VI_OWEINACT;
2362 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2366 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2367 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2373 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2374 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2379 VNASSERT(vp->v_usecount == 0 || (vp->v_iflag & VI_OWEINACT) == 0, vp,
2380 ("vnode with usecount and VI_OWEINACT set"));
2382 if (vp->v_iflag & VI_OWEINACT)
2383 vinactive(vp, curthread);
2384 if (func != VPUTX_VUNREF)
2391 * Vnode put/release.
2392 * If count drops to zero, call inactive routine and return to freelist.
2395 vrele(struct vnode *vp)
2398 vputx(vp, VPUTX_VRELE);
2402 * Release an already locked vnode. This give the same effects as
2403 * unlock+vrele(), but takes less time and avoids releasing and
2404 * re-aquiring the lock (as vrele() acquires the lock internally.)
2407 vput(struct vnode *vp)
2410 vputx(vp, VPUTX_VPUT);
2414 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2417 vunref(struct vnode *vp)
2420 vputx(vp, VPUTX_VUNREF);
2424 * Increase the hold count and activate if this is the first reference.
2427 _vhold(struct vnode *vp, bool locked)
2432 ASSERT_VI_LOCKED(vp, __func__);
2434 ASSERT_VI_UNLOCKED(vp, __func__);
2435 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2436 if (!locked && vfs_refcount_acquire_if_not_zero(&vp->v_holdcnt)) {
2437 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2438 ("_vhold: vnode with holdcnt is free"));
2444 if ((vp->v_iflag & VI_FREE) == 0) {
2445 refcount_acquire(&vp->v_holdcnt);
2450 VNASSERT(vp->v_holdcnt == 0, vp,
2451 ("%s: wrong hold count", __func__));
2452 VNASSERT(vp->v_op != NULL, vp,
2453 ("%s: vnode already reclaimed.", __func__));
2455 * Remove a vnode from the free list, mark it as in use,
2456 * and put it on the active list.
2458 mtx_lock(&vnode_free_list_mtx);
2459 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2461 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2462 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2463 ("Activating already active vnode"));
2464 vp->v_iflag |= VI_ACTIVE;
2466 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2467 mp->mnt_activevnodelistsize++;
2468 mtx_unlock(&vnode_free_list_mtx);
2469 refcount_acquire(&vp->v_holdcnt);
2475 * Drop the hold count of the vnode. If this is the last reference to
2476 * the vnode we place it on the free list unless it has been vgone'd
2477 * (marked VI_DOOMED) in which case we will free it.
2480 _vdrop(struct vnode *vp, bool locked)
2487 ASSERT_VI_LOCKED(vp, __func__);
2489 ASSERT_VI_UNLOCKED(vp, __func__);
2490 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2491 if ((int)vp->v_holdcnt <= 0)
2492 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2493 if (vfs_refcount_release_if_not_last(&vp->v_holdcnt)) {
2501 if (refcount_release(&vp->v_holdcnt) == 0) {
2505 if ((vp->v_iflag & VI_DOOMED) == 0) {
2507 * Mark a vnode as free: remove it from its active list
2508 * and put it up for recycling on the freelist.
2510 VNASSERT(vp->v_op != NULL, vp,
2511 ("vdropl: vnode already reclaimed."));
2512 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2513 ("vnode already free"));
2514 VNASSERT(vp->v_holdcnt == 0, vp,
2515 ("vdropl: freeing when we shouldn't"));
2516 active = vp->v_iflag & VI_ACTIVE;
2517 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2518 vp->v_iflag &= ~VI_ACTIVE;
2520 mtx_lock(&vnode_free_list_mtx);
2522 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2524 mp->mnt_activevnodelistsize--;
2526 if (vp->v_iflag & VI_AGE) {
2527 TAILQ_INSERT_HEAD(&vnode_free_list, vp,
2530 TAILQ_INSERT_TAIL(&vnode_free_list, vp,
2534 vp->v_iflag &= ~VI_AGE;
2535 vp->v_iflag |= VI_FREE;
2536 mtx_unlock(&vnode_free_list_mtx);
2538 atomic_add_long(&free_owe_inact, 1);
2544 * The vnode has been marked for destruction, so free it.
2546 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2547 atomic_subtract_long(&numvnodes, 1);
2549 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2550 ("cleaned vnode still on the free list."));
2551 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2552 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2553 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2554 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2555 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2556 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2557 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
2558 ("clean blk trie not empty"));
2559 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2560 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
2561 ("dirty blk trie not empty"));
2562 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2563 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2564 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2567 mac_vnode_destroy(vp);
2569 if (vp->v_pollinfo != NULL)
2570 destroy_vpollinfo(vp->v_pollinfo);
2572 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2575 rangelock_destroy(&vp->v_rl);
2576 lockdestroy(vp->v_vnlock);
2577 mtx_destroy(&vp->v_interlock);
2578 rw_destroy(BO_LOCKPTR(bo));
2579 uma_zfree(vnode_zone, vp);
2583 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2584 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2585 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2586 * failed lock upgrade.
2589 vinactive(struct vnode *vp, struct thread *td)
2591 struct vm_object *obj;
2593 ASSERT_VOP_ELOCKED(vp, "vinactive");
2594 ASSERT_VI_LOCKED(vp, "vinactive");
2595 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2596 ("vinactive: recursed on VI_DOINGINACT"));
2597 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2598 vp->v_iflag |= VI_DOINGINACT;
2599 vp->v_iflag &= ~VI_OWEINACT;
2602 * Before moving off the active list, we must be sure that any
2603 * modified pages are on the vnode's dirty list since these will
2604 * no longer be checked once the vnode is on the inactive list.
2605 * Because the vnode vm object keeps a hold reference on the vnode
2606 * if there is at least one resident non-cached page, the vnode
2607 * cannot leave the active list without the page cleanup done.
2610 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
2611 VM_OBJECT_WLOCK(obj);
2612 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
2613 VM_OBJECT_WUNLOCK(obj);
2615 VOP_INACTIVE(vp, td);
2617 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2618 ("vinactive: lost VI_DOINGINACT"));
2619 vp->v_iflag &= ~VI_DOINGINACT;
2623 * Remove any vnodes in the vnode table belonging to mount point mp.
2625 * If FORCECLOSE is not specified, there should not be any active ones,
2626 * return error if any are found (nb: this is a user error, not a
2627 * system error). If FORCECLOSE is specified, detach any active vnodes
2630 * If WRITECLOSE is set, only flush out regular file vnodes open for
2633 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2635 * `rootrefs' specifies the base reference count for the root vnode
2636 * of this filesystem. The root vnode is considered busy if its
2637 * v_usecount exceeds this value. On a successful return, vflush(, td)
2638 * will call vrele() on the root vnode exactly rootrefs times.
2639 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2643 static int busyprt = 0; /* print out busy vnodes */
2644 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2648 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2650 struct vnode *vp, *mvp, *rootvp = NULL;
2652 int busy = 0, error;
2654 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2657 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2658 ("vflush: bad args"));
2660 * Get the filesystem root vnode. We can vput() it
2661 * immediately, since with rootrefs > 0, it won't go away.
2663 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2664 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2671 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2673 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2676 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2680 * Skip over a vnodes marked VV_SYSTEM.
2682 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2688 * If WRITECLOSE is set, flush out unlinked but still open
2689 * files (even if open only for reading) and regular file
2690 * vnodes open for writing.
2692 if (flags & WRITECLOSE) {
2693 if (vp->v_object != NULL) {
2694 VM_OBJECT_WLOCK(vp->v_object);
2695 vm_object_page_clean(vp->v_object, 0, 0, 0);
2696 VM_OBJECT_WUNLOCK(vp->v_object);
2698 error = VOP_FSYNC(vp, MNT_WAIT, td);
2702 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2705 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2708 if ((vp->v_type == VNON ||
2709 (error == 0 && vattr.va_nlink > 0)) &&
2710 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2718 * With v_usecount == 0, all we need to do is clear out the
2719 * vnode data structures and we are done.
2721 * If FORCECLOSE is set, forcibly close the vnode.
2723 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2729 vprint("vflush: busy vnode", vp);
2735 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2737 * If just the root vnode is busy, and if its refcount
2738 * is equal to `rootrefs', then go ahead and kill it.
2741 KASSERT(busy > 0, ("vflush: not busy"));
2742 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2743 ("vflush: usecount %d < rootrefs %d",
2744 rootvp->v_usecount, rootrefs));
2745 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2746 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2748 VOP_UNLOCK(rootvp, 0);
2754 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2758 for (; rootrefs > 0; rootrefs--)
2764 * Recycle an unused vnode to the front of the free list.
2767 vrecycle(struct vnode *vp)
2771 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2772 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2775 if (vp->v_usecount == 0) {
2784 * Eliminate all activity associated with a vnode
2785 * in preparation for reuse.
2788 vgone(struct vnode *vp)
2796 notify_lowervp_vfs_dummy(struct mount *mp __unused,
2797 struct vnode *lowervp __unused)
2802 * Notify upper mounts about reclaimed or unlinked vnode.
2805 vfs_notify_upper(struct vnode *vp, int event)
2807 static struct vfsops vgonel_vfsops = {
2808 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
2809 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
2811 struct mount *mp, *ump, *mmp;
2818 if (TAILQ_EMPTY(&mp->mnt_uppers))
2821 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
2822 mmp->mnt_op = &vgonel_vfsops;
2823 mmp->mnt_kern_flag |= MNTK_MARKER;
2825 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
2826 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
2827 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
2828 ump = TAILQ_NEXT(ump, mnt_upper_link);
2831 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
2834 case VFS_NOTIFY_UPPER_RECLAIM:
2835 VFS_RECLAIM_LOWERVP(ump, vp);
2837 case VFS_NOTIFY_UPPER_UNLINK:
2838 VFS_UNLINK_LOWERVP(ump, vp);
2841 KASSERT(0, ("invalid event %d", event));
2845 ump = TAILQ_NEXT(mmp, mnt_upper_link);
2846 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
2849 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
2850 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
2851 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
2852 wakeup(&mp->mnt_uppers);
2859 * vgone, with the vp interlock held.
2862 vgonel(struct vnode *vp)
2869 ASSERT_VOP_ELOCKED(vp, "vgonel");
2870 ASSERT_VI_LOCKED(vp, "vgonel");
2871 VNASSERT(vp->v_holdcnt, vp,
2872 ("vgonel: vp %p has no reference.", vp));
2873 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2877 * Don't vgonel if we're already doomed.
2879 if (vp->v_iflag & VI_DOOMED)
2881 vp->v_iflag |= VI_DOOMED;
2884 * Check to see if the vnode is in use. If so, we have to call
2885 * VOP_CLOSE() and VOP_INACTIVE().
2887 active = vp->v_usecount;
2888 oweinact = (vp->v_iflag & VI_OWEINACT);
2890 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
2893 * If purging an active vnode, it must be closed and
2894 * deactivated before being reclaimed.
2897 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2898 if (oweinact || active) {
2900 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2904 if (vp->v_type == VSOCK)
2905 vfs_unp_reclaim(vp);
2908 * Clean out any buffers associated with the vnode.
2909 * If the flush fails, just toss the buffers.
2912 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2913 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2914 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
2915 while (vinvalbuf(vp, 0, 0, 0) != 0)
2919 BO_LOCK(&vp->v_bufobj);
2920 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
2921 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
2922 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
2923 vp->v_bufobj.bo_clean.bv_cnt == 0,
2924 ("vp %p bufobj not invalidated", vp));
2925 vp->v_bufobj.bo_flag |= BO_DEAD;
2926 BO_UNLOCK(&vp->v_bufobj);
2929 * Reclaim the vnode.
2931 if (VOP_RECLAIM(vp, td))
2932 panic("vgone: cannot reclaim");
2934 vn_finished_secondary_write(mp);
2935 VNASSERT(vp->v_object == NULL, vp,
2936 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2938 * Clear the advisory locks and wake up waiting threads.
2940 (void)VOP_ADVLOCKPURGE(vp);
2942 * Delete from old mount point vnode list.
2947 * Done with purge, reset to the standard lock and invalidate
2951 vp->v_vnlock = &vp->v_lock;
2952 vp->v_op = &dead_vnodeops;
2958 * Calculate the total number of references to a special device.
2961 vcount(struct vnode *vp)
2966 count = vp->v_rdev->si_usecount;
2972 * Same as above, but using the struct cdev *as argument
2975 count_dev(struct cdev *dev)
2980 count = dev->si_usecount;
2986 * Print out a description of a vnode.
2988 static char *typename[] =
2989 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2993 vn_printf(struct vnode *vp, const char *fmt, ...)
2996 char buf[256], buf2[16];
3002 printf("%p: ", (void *)vp);
3003 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
3004 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
3005 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
3008 if (vp->v_vflag & VV_ROOT)
3009 strlcat(buf, "|VV_ROOT", sizeof(buf));
3010 if (vp->v_vflag & VV_ISTTY)
3011 strlcat(buf, "|VV_ISTTY", sizeof(buf));
3012 if (vp->v_vflag & VV_NOSYNC)
3013 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
3014 if (vp->v_vflag & VV_ETERNALDEV)
3015 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
3016 if (vp->v_vflag & VV_CACHEDLABEL)
3017 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
3018 if (vp->v_vflag & VV_TEXT)
3019 strlcat(buf, "|VV_TEXT", sizeof(buf));
3020 if (vp->v_vflag & VV_COPYONWRITE)
3021 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
3022 if (vp->v_vflag & VV_SYSTEM)
3023 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
3024 if (vp->v_vflag & VV_PROCDEP)
3025 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
3026 if (vp->v_vflag & VV_NOKNOTE)
3027 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
3028 if (vp->v_vflag & VV_DELETED)
3029 strlcat(buf, "|VV_DELETED", sizeof(buf));
3030 if (vp->v_vflag & VV_MD)
3031 strlcat(buf, "|VV_MD", sizeof(buf));
3032 if (vp->v_vflag & VV_FORCEINSMQ)
3033 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
3034 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
3035 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
3036 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
3038 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
3039 strlcat(buf, buf2, sizeof(buf));
3041 if (vp->v_iflag & VI_MOUNT)
3042 strlcat(buf, "|VI_MOUNT", sizeof(buf));
3043 if (vp->v_iflag & VI_AGE)
3044 strlcat(buf, "|VI_AGE", sizeof(buf));
3045 if (vp->v_iflag & VI_DOOMED)
3046 strlcat(buf, "|VI_DOOMED", sizeof(buf));
3047 if (vp->v_iflag & VI_FREE)
3048 strlcat(buf, "|VI_FREE", sizeof(buf));
3049 if (vp->v_iflag & VI_ACTIVE)
3050 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3051 if (vp->v_iflag & VI_DOINGINACT)
3052 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3053 if (vp->v_iflag & VI_OWEINACT)
3054 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3055 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
3056 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
3058 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3059 strlcat(buf, buf2, sizeof(buf));
3061 printf(" flags (%s)\n", buf + 1);
3062 if (mtx_owned(VI_MTX(vp)))
3063 printf(" VI_LOCKed");
3064 if (vp->v_object != NULL)
3065 printf(" v_object %p ref %d pages %d "
3066 "cleanbuf %d dirtybuf %d\n",
3067 vp->v_object, vp->v_object->ref_count,
3068 vp->v_object->resident_page_count,
3069 vp->v_bufobj.bo_clean.bv_cnt,
3070 vp->v_bufobj.bo_dirty.bv_cnt);
3072 lockmgr_printinfo(vp->v_vnlock);
3073 if (vp->v_data != NULL)
3079 * List all of the locked vnodes in the system.
3080 * Called when debugging the kernel.
3082 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3088 * Note: because this is DDB, we can't obey the locking semantics
3089 * for these structures, which means we could catch an inconsistent
3090 * state and dereference a nasty pointer. Not much to be done
3093 db_printf("Locked vnodes\n");
3094 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3095 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3096 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3103 * Show details about the given vnode.
3105 DB_SHOW_COMMAND(vnode, db_show_vnode)
3111 vp = (struct vnode *)addr;
3112 vn_printf(vp, "vnode ");
3116 * Show details about the given mount point.
3118 DB_SHOW_COMMAND(mount, db_show_mount)
3129 /* No address given, print short info about all mount points. */
3130 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3131 db_printf("%p %s on %s (%s)\n", mp,
3132 mp->mnt_stat.f_mntfromname,
3133 mp->mnt_stat.f_mntonname,
3134 mp->mnt_stat.f_fstypename);
3138 db_printf("\nMore info: show mount <addr>\n");
3142 mp = (struct mount *)addr;
3143 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3144 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3147 mflags = mp->mnt_flag;
3148 #define MNT_FLAG(flag) do { \
3149 if (mflags & (flag)) { \
3150 if (buf[0] != '\0') \
3151 strlcat(buf, ", ", sizeof(buf)); \
3152 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3153 mflags &= ~(flag); \
3156 MNT_FLAG(MNT_RDONLY);
3157 MNT_FLAG(MNT_SYNCHRONOUS);
3158 MNT_FLAG(MNT_NOEXEC);
3159 MNT_FLAG(MNT_NOSUID);
3160 MNT_FLAG(MNT_NFS4ACLS);
3161 MNT_FLAG(MNT_UNION);
3162 MNT_FLAG(MNT_ASYNC);
3163 MNT_FLAG(MNT_SUIDDIR);
3164 MNT_FLAG(MNT_SOFTDEP);
3165 MNT_FLAG(MNT_NOSYMFOLLOW);
3166 MNT_FLAG(MNT_GJOURNAL);
3167 MNT_FLAG(MNT_MULTILABEL);
3169 MNT_FLAG(MNT_NOATIME);
3170 MNT_FLAG(MNT_NOCLUSTERR);
3171 MNT_FLAG(MNT_NOCLUSTERW);
3173 MNT_FLAG(MNT_EXRDONLY);
3174 MNT_FLAG(MNT_EXPORTED);
3175 MNT_FLAG(MNT_DEFEXPORTED);
3176 MNT_FLAG(MNT_EXPORTANON);
3177 MNT_FLAG(MNT_EXKERB);
3178 MNT_FLAG(MNT_EXPUBLIC);
3179 MNT_FLAG(MNT_LOCAL);
3180 MNT_FLAG(MNT_QUOTA);
3181 MNT_FLAG(MNT_ROOTFS);
3183 MNT_FLAG(MNT_IGNORE);
3184 MNT_FLAG(MNT_UPDATE);
3185 MNT_FLAG(MNT_DELEXPORT);
3186 MNT_FLAG(MNT_RELOAD);
3187 MNT_FLAG(MNT_FORCE);
3188 MNT_FLAG(MNT_SNAPSHOT);
3189 MNT_FLAG(MNT_BYFSID);
3193 strlcat(buf, ", ", sizeof(buf));
3194 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3195 "0x%016jx", mflags);
3197 db_printf(" mnt_flag = %s\n", buf);
3200 flags = mp->mnt_kern_flag;
3201 #define MNT_KERN_FLAG(flag) do { \
3202 if (flags & (flag)) { \
3203 if (buf[0] != '\0') \
3204 strlcat(buf, ", ", sizeof(buf)); \
3205 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3209 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3210 MNT_KERN_FLAG(MNTK_ASYNC);
3211 MNT_KERN_FLAG(MNTK_SOFTDEP);
3212 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3213 MNT_KERN_FLAG(MNTK_DRAINING);
3214 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3215 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3216 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3217 MNT_KERN_FLAG(MNTK_NO_IOPF);
3218 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3219 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3220 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3221 MNT_KERN_FLAG(MNTK_MARKER);
3222 MNT_KERN_FLAG(MNTK_USES_BCACHE);
3223 MNT_KERN_FLAG(MNTK_NOASYNC);
3224 MNT_KERN_FLAG(MNTK_UNMOUNT);
3225 MNT_KERN_FLAG(MNTK_MWAIT);
3226 MNT_KERN_FLAG(MNTK_SUSPEND);
3227 MNT_KERN_FLAG(MNTK_SUSPEND2);
3228 MNT_KERN_FLAG(MNTK_SUSPENDED);
3229 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3230 MNT_KERN_FLAG(MNTK_NOKNOTE);
3231 #undef MNT_KERN_FLAG
3234 strlcat(buf, ", ", sizeof(buf));
3235 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3238 db_printf(" mnt_kern_flag = %s\n", buf);
3240 db_printf(" mnt_opt = ");
3241 opt = TAILQ_FIRST(mp->mnt_opt);
3243 db_printf("%s", opt->name);
3244 opt = TAILQ_NEXT(opt, link);
3245 while (opt != NULL) {
3246 db_printf(", %s", opt->name);
3247 opt = TAILQ_NEXT(opt, link);
3253 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3254 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3255 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3256 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3257 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3258 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3259 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3260 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3261 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3262 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3263 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3264 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3266 db_printf(" mnt_cred = { uid=%u ruid=%u",
3267 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3268 if (jailed(mp->mnt_cred))
3269 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3271 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3272 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3273 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3274 db_printf(" mnt_activevnodelistsize = %d\n",
3275 mp->mnt_activevnodelistsize);
3276 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3277 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3278 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3279 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3280 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3281 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3282 db_printf(" mnt_secondary_accwrites = %d\n",
3283 mp->mnt_secondary_accwrites);
3284 db_printf(" mnt_gjprovider = %s\n",
3285 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3287 db_printf("\n\nList of active vnodes\n");
3288 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3289 if (vp->v_type != VMARKER) {
3290 vn_printf(vp, "vnode ");
3295 db_printf("\n\nList of inactive vnodes\n");
3296 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3297 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3298 vn_printf(vp, "vnode ");
3307 * Fill in a struct xvfsconf based on a struct vfsconf.
3310 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3312 struct xvfsconf xvfsp;
3314 bzero(&xvfsp, sizeof(xvfsp));
3315 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3316 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3317 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3318 xvfsp.vfc_flags = vfsp->vfc_flags;
3320 * These are unused in userland, we keep them
3321 * to not break binary compatibility.
3323 xvfsp.vfc_vfsops = NULL;
3324 xvfsp.vfc_next = NULL;
3325 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3328 #ifdef COMPAT_FREEBSD32
3330 uint32_t vfc_vfsops;
3331 char vfc_name[MFSNAMELEN];
3332 int32_t vfc_typenum;
3333 int32_t vfc_refcount;
3339 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3341 struct xvfsconf32 xvfsp;
3343 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3344 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3345 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3346 xvfsp.vfc_flags = vfsp->vfc_flags;
3347 xvfsp.vfc_vfsops = 0;
3349 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3354 * Top level filesystem related information gathering.
3357 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3359 struct vfsconf *vfsp;
3364 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3365 #ifdef COMPAT_FREEBSD32
3366 if (req->flags & SCTL_MASK32)
3367 error = vfsconf2x32(req, vfsp);
3370 error = vfsconf2x(req, vfsp);
3378 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3379 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3380 "S,xvfsconf", "List of all configured filesystems");
3382 #ifndef BURN_BRIDGES
3383 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3386 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3388 int *name = (int *)arg1 - 1; /* XXX */
3389 u_int namelen = arg2 + 1; /* XXX */
3390 struct vfsconf *vfsp;
3392 log(LOG_WARNING, "userland calling deprecated sysctl, "
3393 "please rebuild world\n");
3395 #if 1 || defined(COMPAT_PRELITE2)
3396 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3398 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3402 case VFS_MAXTYPENUM:
3405 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3408 return (ENOTDIR); /* overloaded */
3410 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3411 if (vfsp->vfc_typenum == name[2])
3416 return (EOPNOTSUPP);
3417 #ifdef COMPAT_FREEBSD32
3418 if (req->flags & SCTL_MASK32)
3419 return (vfsconf2x32(req, vfsp));
3422 return (vfsconf2x(req, vfsp));
3424 return (EOPNOTSUPP);
3427 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
3428 CTLFLAG_MPSAFE, vfs_sysctl,
3429 "Generic filesystem");
3431 #if 1 || defined(COMPAT_PRELITE2)
3434 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3437 struct vfsconf *vfsp;
3438 struct ovfsconf ovfs;
3441 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3442 bzero(&ovfs, sizeof(ovfs));
3443 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3444 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3445 ovfs.vfc_index = vfsp->vfc_typenum;
3446 ovfs.vfc_refcount = vfsp->vfc_refcount;
3447 ovfs.vfc_flags = vfsp->vfc_flags;
3448 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3458 #endif /* 1 || COMPAT_PRELITE2 */
3459 #endif /* !BURN_BRIDGES */
3461 #define KINFO_VNODESLOP 10
3464 * Dump vnode list (via sysctl).
3468 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3476 * Stale numvnodes access is not fatal here.
3479 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3481 /* Make an estimate */
3482 return (SYSCTL_OUT(req, 0, len));
3484 error = sysctl_wire_old_buffer(req, 0);
3487 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3489 mtx_lock(&mountlist_mtx);
3490 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3491 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3494 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3498 xvn[n].xv_size = sizeof *xvn;
3499 xvn[n].xv_vnode = vp;
3500 xvn[n].xv_id = 0; /* XXX compat */
3501 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3503 XV_COPY(writecount);
3509 xvn[n].xv_flag = vp->v_vflag;
3511 switch (vp->v_type) {
3518 if (vp->v_rdev == NULL) {
3522 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3525 xvn[n].xv_socket = vp->v_socket;
3528 xvn[n].xv_fifo = vp->v_fifoinfo;
3533 /* shouldn't happen? */
3541 mtx_lock(&mountlist_mtx);
3546 mtx_unlock(&mountlist_mtx);
3548 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3553 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
3554 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
3559 unmount_or_warn(struct mount *mp)
3563 error = dounmount(mp, MNT_FORCE, curthread);
3565 printf("unmount of %s failed (", mp->mnt_stat.f_mntonname);
3569 printf("%d)\n", error);
3574 * Unmount all filesystems. The list is traversed in reverse order
3575 * of mounting to avoid dependencies.
3578 vfs_unmountall(void)
3580 struct mount *mp, *tmp;
3582 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3585 * Since this only runs when rebooting, it is not interlocked.
3587 TAILQ_FOREACH_REVERSE_SAFE(mp, &mountlist, mntlist, mnt_list, tmp) {
3591 * Forcibly unmounting "/dev" before "/" would prevent clean
3592 * unmount of the latter.
3594 if (mp == rootdevmp)
3597 unmount_or_warn(mp);
3600 if (rootdevmp != NULL)
3601 unmount_or_warn(rootdevmp);
3605 * perform msync on all vnodes under a mount point
3606 * the mount point must be locked.
3609 vfs_msync(struct mount *mp, int flags)
3611 struct vnode *vp, *mvp;
3612 struct vm_object *obj;
3614 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3615 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
3617 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3618 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3620 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3622 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3629 VM_OBJECT_WLOCK(obj);
3630 vm_object_page_clean(obj, 0, 0,
3632 OBJPC_SYNC : OBJPC_NOSYNC);
3633 VM_OBJECT_WUNLOCK(obj);
3643 destroy_vpollinfo_free(struct vpollinfo *vi)
3646 knlist_destroy(&vi->vpi_selinfo.si_note);
3647 mtx_destroy(&vi->vpi_lock);
3648 uma_zfree(vnodepoll_zone, vi);
3652 destroy_vpollinfo(struct vpollinfo *vi)
3655 knlist_clear(&vi->vpi_selinfo.si_note, 1);
3656 seldrain(&vi->vpi_selinfo);
3657 destroy_vpollinfo_free(vi);
3661 * Initalize per-vnode helper structure to hold poll-related state.
3664 v_addpollinfo(struct vnode *vp)
3666 struct vpollinfo *vi;
3668 if (vp->v_pollinfo != NULL)
3670 vi = uma_zalloc(vnodepoll_zone, M_WAITOK | M_ZERO);
3671 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3672 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3673 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3675 if (vp->v_pollinfo != NULL) {
3677 destroy_vpollinfo_free(vi);
3680 vp->v_pollinfo = vi;
3685 * Record a process's interest in events which might happen to
3686 * a vnode. Because poll uses the historic select-style interface
3687 * internally, this routine serves as both the ``check for any
3688 * pending events'' and the ``record my interest in future events''
3689 * functions. (These are done together, while the lock is held,
3690 * to avoid race conditions.)
3693 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3697 mtx_lock(&vp->v_pollinfo->vpi_lock);
3698 if (vp->v_pollinfo->vpi_revents & events) {
3700 * This leaves events we are not interested
3701 * in available for the other process which
3702 * which presumably had requested them
3703 * (otherwise they would never have been
3706 events &= vp->v_pollinfo->vpi_revents;
3707 vp->v_pollinfo->vpi_revents &= ~events;
3709 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3712 vp->v_pollinfo->vpi_events |= events;
3713 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3714 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3719 * Routine to create and manage a filesystem syncer vnode.
3721 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3722 static int sync_fsync(struct vop_fsync_args *);
3723 static int sync_inactive(struct vop_inactive_args *);
3724 static int sync_reclaim(struct vop_reclaim_args *);
3726 static struct vop_vector sync_vnodeops = {
3727 .vop_bypass = VOP_EOPNOTSUPP,
3728 .vop_close = sync_close, /* close */
3729 .vop_fsync = sync_fsync, /* fsync */
3730 .vop_inactive = sync_inactive, /* inactive */
3731 .vop_reclaim = sync_reclaim, /* reclaim */
3732 .vop_lock1 = vop_stdlock, /* lock */
3733 .vop_unlock = vop_stdunlock, /* unlock */
3734 .vop_islocked = vop_stdislocked, /* islocked */
3738 * Create a new filesystem syncer vnode for the specified mount point.
3741 vfs_allocate_syncvnode(struct mount *mp)
3745 static long start, incr, next;
3748 /* Allocate a new vnode */
3749 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3751 panic("vfs_allocate_syncvnode: getnewvnode() failed");
3753 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3754 vp->v_vflag |= VV_FORCEINSMQ;
3755 error = insmntque(vp, mp);
3757 panic("vfs_allocate_syncvnode: insmntque() failed");
3758 vp->v_vflag &= ~VV_FORCEINSMQ;
3761 * Place the vnode onto the syncer worklist. We attempt to
3762 * scatter them about on the list so that they will go off
3763 * at evenly distributed times even if all the filesystems
3764 * are mounted at once.
3767 if (next == 0 || next > syncer_maxdelay) {
3771 start = syncer_maxdelay / 2;
3772 incr = syncer_maxdelay;
3778 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3779 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3780 mtx_lock(&sync_mtx);
3782 if (mp->mnt_syncer == NULL) {
3783 mp->mnt_syncer = vp;
3786 mtx_unlock(&sync_mtx);
3789 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3796 vfs_deallocate_syncvnode(struct mount *mp)
3800 mtx_lock(&sync_mtx);
3801 vp = mp->mnt_syncer;
3803 mp->mnt_syncer = NULL;
3804 mtx_unlock(&sync_mtx);
3810 * Do a lazy sync of the filesystem.
3813 sync_fsync(struct vop_fsync_args *ap)
3815 struct vnode *syncvp = ap->a_vp;
3816 struct mount *mp = syncvp->v_mount;
3821 * We only need to do something if this is a lazy evaluation.
3823 if (ap->a_waitfor != MNT_LAZY)
3827 * Move ourselves to the back of the sync list.
3829 bo = &syncvp->v_bufobj;
3831 vn_syncer_add_to_worklist(bo, syncdelay);
3835 * Walk the list of vnodes pushing all that are dirty and
3836 * not already on the sync list.
3838 if (vfs_busy(mp, MBF_NOWAIT) != 0)
3840 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3844 save = curthread_pflags_set(TDP_SYNCIO);
3845 vfs_msync(mp, MNT_NOWAIT);
3846 error = VFS_SYNC(mp, MNT_LAZY);
3847 curthread_pflags_restore(save);
3848 vn_finished_write(mp);
3854 * The syncer vnode is no referenced.
3857 sync_inactive(struct vop_inactive_args *ap)
3865 * The syncer vnode is no longer needed and is being decommissioned.
3867 * Modifications to the worklist must be protected by sync_mtx.
3870 sync_reclaim(struct vop_reclaim_args *ap)
3872 struct vnode *vp = ap->a_vp;
3877 mtx_lock(&sync_mtx);
3878 if (vp->v_mount->mnt_syncer == vp)
3879 vp->v_mount->mnt_syncer = NULL;
3880 if (bo->bo_flag & BO_ONWORKLST) {
3881 LIST_REMOVE(bo, bo_synclist);
3882 syncer_worklist_len--;
3884 bo->bo_flag &= ~BO_ONWORKLST;
3886 mtx_unlock(&sync_mtx);
3893 * Check if vnode represents a disk device
3896 vn_isdisk(struct vnode *vp, int *errp)
3900 if (vp->v_type != VCHR) {
3906 if (vp->v_rdev == NULL)
3908 else if (vp->v_rdev->si_devsw == NULL)
3910 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3916 return (error == 0);
3920 * Common filesystem object access control check routine. Accepts a
3921 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3922 * and optional call-by-reference privused argument allowing vaccess()
3923 * to indicate to the caller whether privilege was used to satisfy the
3924 * request (obsoleted). Returns 0 on success, or an errno on failure.
3927 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3928 accmode_t accmode, struct ucred *cred, int *privused)
3930 accmode_t dac_granted;
3931 accmode_t priv_granted;
3933 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3934 ("invalid bit in accmode"));
3935 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3936 ("VAPPEND without VWRITE"));
3939 * Look for a normal, non-privileged way to access the file/directory
3940 * as requested. If it exists, go with that.
3943 if (privused != NULL)
3948 /* Check the owner. */
3949 if (cred->cr_uid == file_uid) {
3950 dac_granted |= VADMIN;
3951 if (file_mode & S_IXUSR)
3952 dac_granted |= VEXEC;
3953 if (file_mode & S_IRUSR)
3954 dac_granted |= VREAD;
3955 if (file_mode & S_IWUSR)
3956 dac_granted |= (VWRITE | VAPPEND);
3958 if ((accmode & dac_granted) == accmode)
3964 /* Otherwise, check the groups (first match) */
3965 if (groupmember(file_gid, cred)) {
3966 if (file_mode & S_IXGRP)
3967 dac_granted |= VEXEC;
3968 if (file_mode & S_IRGRP)
3969 dac_granted |= VREAD;
3970 if (file_mode & S_IWGRP)
3971 dac_granted |= (VWRITE | VAPPEND);
3973 if ((accmode & dac_granted) == accmode)
3979 /* Otherwise, check everyone else. */
3980 if (file_mode & S_IXOTH)
3981 dac_granted |= VEXEC;
3982 if (file_mode & S_IROTH)
3983 dac_granted |= VREAD;
3984 if (file_mode & S_IWOTH)
3985 dac_granted |= (VWRITE | VAPPEND);
3986 if ((accmode & dac_granted) == accmode)
3991 * Build a privilege mask to determine if the set of privileges
3992 * satisfies the requirements when combined with the granted mask
3993 * from above. For each privilege, if the privilege is required,
3994 * bitwise or the request type onto the priv_granted mask.
4000 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
4001 * requests, instead of PRIV_VFS_EXEC.
4003 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4004 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
4005 priv_granted |= VEXEC;
4008 * Ensure that at least one execute bit is on. Otherwise,
4009 * a privileged user will always succeed, and we don't want
4010 * this to happen unless the file really is executable.
4012 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
4013 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
4014 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
4015 priv_granted |= VEXEC;
4018 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
4019 !priv_check_cred(cred, PRIV_VFS_READ, 0))
4020 priv_granted |= VREAD;
4022 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
4023 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
4024 priv_granted |= (VWRITE | VAPPEND);
4026 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
4027 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
4028 priv_granted |= VADMIN;
4030 if ((accmode & (priv_granted | dac_granted)) == accmode) {
4031 /* XXX audit: privilege used */
4032 if (privused != NULL)
4037 return ((accmode & VADMIN) ? EPERM : EACCES);
4041 * Credential check based on process requesting service, and per-attribute
4045 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
4046 struct thread *td, accmode_t accmode)
4050 * Kernel-invoked always succeeds.
4056 * Do not allow privileged processes in jail to directly manipulate
4057 * system attributes.
4059 switch (attrnamespace) {
4060 case EXTATTR_NAMESPACE_SYSTEM:
4061 /* Potentially should be: return (EPERM); */
4062 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
4063 case EXTATTR_NAMESPACE_USER:
4064 return (VOP_ACCESS(vp, accmode, cred, td));
4070 #ifdef DEBUG_VFS_LOCKS
4072 * This only exists to supress warnings from unlocked specfs accesses. It is
4073 * no longer ok to have an unlocked VFS.
4075 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4076 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4078 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4079 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4080 "Drop into debugger on lock violation");
4082 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4083 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4084 0, "Check for interlock across VOPs");
4086 int vfs_badlock_print = 1; /* Print lock violations. */
4087 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4088 0, "Print lock violations");
4091 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4092 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4093 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4097 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4101 if (vfs_badlock_backtrace)
4104 if (vfs_badlock_print)
4105 printf("%s: %p %s\n", str, (void *)vp, msg);
4106 if (vfs_badlock_ddb)
4107 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4111 assert_vi_locked(struct vnode *vp, const char *str)
4114 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4115 vfs_badlock("interlock is not locked but should be", str, vp);
4119 assert_vi_unlocked(struct vnode *vp, const char *str)
4122 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4123 vfs_badlock("interlock is locked but should not be", str, vp);
4127 assert_vop_locked(struct vnode *vp, const char *str)
4131 if (!IGNORE_LOCK(vp)) {
4132 locked = VOP_ISLOCKED(vp);
4133 if (locked == 0 || locked == LK_EXCLOTHER)
4134 vfs_badlock("is not locked but should be", str, vp);
4139 assert_vop_unlocked(struct vnode *vp, const char *str)
4142 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4143 vfs_badlock("is locked but should not be", str, vp);
4147 assert_vop_elocked(struct vnode *vp, const char *str)
4150 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4151 vfs_badlock("is not exclusive locked but should be", str, vp);
4156 assert_vop_elocked_other(struct vnode *vp, const char *str)
4159 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
4160 vfs_badlock("is not exclusive locked by another thread",
4165 assert_vop_slocked(struct vnode *vp, const char *str)
4168 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
4169 vfs_badlock("is not locked shared but should be", str, vp);
4172 #endif /* DEBUG_VFS_LOCKS */
4175 vop_rename_fail(struct vop_rename_args *ap)
4178 if (ap->a_tvp != NULL)
4180 if (ap->a_tdvp == ap->a_tvp)
4189 vop_rename_pre(void *ap)
4191 struct vop_rename_args *a = ap;
4193 #ifdef DEBUG_VFS_LOCKS
4195 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4196 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4197 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4198 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4200 /* Check the source (from). */
4201 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4202 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4203 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4204 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4205 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4207 /* Check the target. */
4209 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4210 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4212 if (a->a_tdvp != a->a_fdvp)
4214 if (a->a_tvp != a->a_fvp)
4222 vop_strategy_pre(void *ap)
4224 #ifdef DEBUG_VFS_LOCKS
4225 struct vop_strategy_args *a;
4232 * Cluster ops lock their component buffers but not the IO container.
4234 if ((bp->b_flags & B_CLUSTER) != 0)
4237 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4238 if (vfs_badlock_print)
4240 "VOP_STRATEGY: bp is not locked but should be\n");
4241 if (vfs_badlock_ddb)
4242 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4248 vop_lock_pre(void *ap)
4250 #ifdef DEBUG_VFS_LOCKS
4251 struct vop_lock1_args *a = ap;
4253 if ((a->a_flags & LK_INTERLOCK) == 0)
4254 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4256 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4261 vop_lock_post(void *ap, int rc)
4263 #ifdef DEBUG_VFS_LOCKS
4264 struct vop_lock1_args *a = ap;
4266 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4267 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4268 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4273 vop_unlock_pre(void *ap)
4275 #ifdef DEBUG_VFS_LOCKS
4276 struct vop_unlock_args *a = ap;
4278 if (a->a_flags & LK_INTERLOCK)
4279 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4280 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4285 vop_unlock_post(void *ap, int rc)
4287 #ifdef DEBUG_VFS_LOCKS
4288 struct vop_unlock_args *a = ap;
4290 if (a->a_flags & LK_INTERLOCK)
4291 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4296 vop_create_post(void *ap, int rc)
4298 struct vop_create_args *a = ap;
4301 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4305 vop_deleteextattr_post(void *ap, int rc)
4307 struct vop_deleteextattr_args *a = ap;
4310 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4314 vop_link_post(void *ap, int rc)
4316 struct vop_link_args *a = ap;
4319 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4320 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4325 vop_mkdir_post(void *ap, int rc)
4327 struct vop_mkdir_args *a = ap;
4330 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4334 vop_mknod_post(void *ap, int rc)
4336 struct vop_mknod_args *a = ap;
4339 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4343 vop_reclaim_post(void *ap, int rc)
4345 struct vop_reclaim_args *a = ap;
4348 VFS_KNOTE_LOCKED(a->a_vp, NOTE_REVOKE);
4352 vop_remove_post(void *ap, int rc)
4354 struct vop_remove_args *a = ap;
4357 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4358 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4363 vop_rename_post(void *ap, int rc)
4365 struct vop_rename_args *a = ap;
4368 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4369 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4370 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4372 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4374 if (a->a_tdvp != a->a_fdvp)
4376 if (a->a_tvp != a->a_fvp)
4384 vop_rmdir_post(void *ap, int rc)
4386 struct vop_rmdir_args *a = ap;
4389 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4390 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4395 vop_setattr_post(void *ap, int rc)
4397 struct vop_setattr_args *a = ap;
4400 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4404 vop_setextattr_post(void *ap, int rc)
4406 struct vop_setextattr_args *a = ap;
4409 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4413 vop_symlink_post(void *ap, int rc)
4415 struct vop_symlink_args *a = ap;
4418 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4421 static struct knlist fs_knlist;
4424 vfs_event_init(void *arg)
4426 knlist_init_mtx(&fs_knlist, NULL);
4428 /* XXX - correct order? */
4429 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4432 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4435 KNOTE_UNLOCKED(&fs_knlist, event);
4438 static int filt_fsattach(struct knote *kn);
4439 static void filt_fsdetach(struct knote *kn);
4440 static int filt_fsevent(struct knote *kn, long hint);
4442 struct filterops fs_filtops = {
4444 .f_attach = filt_fsattach,
4445 .f_detach = filt_fsdetach,
4446 .f_event = filt_fsevent
4450 filt_fsattach(struct knote *kn)
4453 kn->kn_flags |= EV_CLEAR;
4454 knlist_add(&fs_knlist, kn, 0);
4459 filt_fsdetach(struct knote *kn)
4462 knlist_remove(&fs_knlist, kn, 0);
4466 filt_fsevent(struct knote *kn, long hint)
4469 kn->kn_fflags |= hint;
4470 return (kn->kn_fflags != 0);
4474 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4480 error = SYSCTL_IN(req, &vc, sizeof(vc));
4483 if (vc.vc_vers != VFS_CTL_VERS1)
4485 mp = vfs_getvfs(&vc.vc_fsid);
4488 /* ensure that a specific sysctl goes to the right filesystem. */
4489 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4490 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4494 VCTLTOREQ(&vc, req);
4495 error = VFS_SYSCTL(mp, vc.vc_op, req);
4500 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4501 NULL, 0, sysctl_vfs_ctl, "",
4505 * Function to initialize a va_filerev field sensibly.
4506 * XXX: Wouldn't a random number make a lot more sense ??
4509 init_va_filerev(void)
4514 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4517 static int filt_vfsread(struct knote *kn, long hint);
4518 static int filt_vfswrite(struct knote *kn, long hint);
4519 static int filt_vfsvnode(struct knote *kn, long hint);
4520 static void filt_vfsdetach(struct knote *kn);
4521 static struct filterops vfsread_filtops = {
4523 .f_detach = filt_vfsdetach,
4524 .f_event = filt_vfsread
4526 static struct filterops vfswrite_filtops = {
4528 .f_detach = filt_vfsdetach,
4529 .f_event = filt_vfswrite
4531 static struct filterops vfsvnode_filtops = {
4533 .f_detach = filt_vfsdetach,
4534 .f_event = filt_vfsvnode
4538 vfs_knllock(void *arg)
4540 struct vnode *vp = arg;
4542 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4546 vfs_knlunlock(void *arg)
4548 struct vnode *vp = arg;
4554 vfs_knl_assert_locked(void *arg)
4556 #ifdef DEBUG_VFS_LOCKS
4557 struct vnode *vp = arg;
4559 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4564 vfs_knl_assert_unlocked(void *arg)
4566 #ifdef DEBUG_VFS_LOCKS
4567 struct vnode *vp = arg;
4569 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4574 vfs_kqfilter(struct vop_kqfilter_args *ap)
4576 struct vnode *vp = ap->a_vp;
4577 struct knote *kn = ap->a_kn;
4580 switch (kn->kn_filter) {
4582 kn->kn_fop = &vfsread_filtops;
4585 kn->kn_fop = &vfswrite_filtops;
4588 kn->kn_fop = &vfsvnode_filtops;
4594 kn->kn_hook = (caddr_t)vp;
4597 if (vp->v_pollinfo == NULL)
4599 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4601 knlist_add(knl, kn, 0);
4607 * Detach knote from vnode
4610 filt_vfsdetach(struct knote *kn)
4612 struct vnode *vp = (struct vnode *)kn->kn_hook;
4614 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4615 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4621 filt_vfsread(struct knote *kn, long hint)
4623 struct vnode *vp = (struct vnode *)kn->kn_hook;
4628 * filesystem is gone, so set the EOF flag and schedule
4629 * the knote for deletion.
4631 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
4633 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4638 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4642 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4643 res = (kn->kn_sfflags & NOTE_FILE_POLL) != 0 || kn->kn_data != 0;
4650 filt_vfswrite(struct knote *kn, long hint)
4652 struct vnode *vp = (struct vnode *)kn->kn_hook;
4657 * filesystem is gone, so set the EOF flag and schedule
4658 * the knote for deletion.
4660 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD))
4661 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4669 filt_vfsvnode(struct knote *kn, long hint)
4671 struct vnode *vp = (struct vnode *)kn->kn_hook;
4675 if (kn->kn_sfflags & hint)
4676 kn->kn_fflags |= hint;
4677 if (hint == NOTE_REVOKE || (hint == 0 && vp->v_type == VBAD)) {
4678 kn->kn_flags |= EV_EOF;
4682 res = (kn->kn_fflags != 0);
4688 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4692 if (dp->d_reclen > ap->a_uio->uio_resid)
4693 return (ENAMETOOLONG);
4694 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4696 if (ap->a_ncookies != NULL) {
4697 if (ap->a_cookies != NULL)
4698 free(ap->a_cookies, M_TEMP);
4699 ap->a_cookies = NULL;
4700 *ap->a_ncookies = 0;
4704 if (ap->a_ncookies == NULL)
4707 KASSERT(ap->a_cookies,
4708 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4710 *ap->a_cookies = realloc(*ap->a_cookies,
4711 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4712 (*ap->a_cookies)[*ap->a_ncookies] = off;
4717 * Mark for update the access time of the file if the filesystem
4718 * supports VOP_MARKATIME. This functionality is used by execve and
4719 * mmap, so we want to avoid the I/O implied by directly setting
4720 * va_atime for the sake of efficiency.
4723 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4728 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4729 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4730 (void)VOP_MARKATIME(vp);
4734 * The purpose of this routine is to remove granularity from accmode_t,
4735 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4736 * VADMIN and VAPPEND.
4738 * If it returns 0, the caller is supposed to continue with the usual
4739 * access checks using 'accmode' as modified by this routine. If it
4740 * returns nonzero value, the caller is supposed to return that value
4743 * Note that after this routine runs, accmode may be zero.
4746 vfs_unixify_accmode(accmode_t *accmode)
4749 * There is no way to specify explicit "deny" rule using
4750 * file mode or POSIX.1e ACLs.
4752 if (*accmode & VEXPLICIT_DENY) {
4758 * None of these can be translated into usual access bits.
4759 * Also, the common case for NFSv4 ACLs is to not contain
4760 * either of these bits. Caller should check for VWRITE
4761 * on the containing directory instead.
4763 if (*accmode & (VDELETE_CHILD | VDELETE))
4766 if (*accmode & VADMIN_PERMS) {
4767 *accmode &= ~VADMIN_PERMS;
4772 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4773 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4775 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
4781 * These are helper functions for filesystems to traverse all
4782 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
4784 * This interface replaces MNT_VNODE_FOREACH.
4787 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
4790 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
4795 kern_yield(PRI_USER);
4797 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4798 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
4799 while (vp != NULL && (vp->v_type == VMARKER ||
4800 (vp->v_iflag & VI_DOOMED) != 0))
4801 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4803 /* Check if we are done */
4805 __mnt_vnode_markerfree_all(mvp, mp);
4806 /* MNT_IUNLOCK(mp); -- done in above function */
4807 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4810 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4811 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4818 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
4822 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4825 (*mvp)->v_type = VMARKER;
4827 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
4828 while (vp != NULL && (vp->v_type == VMARKER ||
4829 (vp->v_iflag & VI_DOOMED) != 0))
4830 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4832 /* Check if we are done */
4836 free(*mvp, M_VNODE_MARKER);
4840 (*mvp)->v_mount = mp;
4841 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4849 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
4857 mtx_assert(MNT_MTX(mp), MA_OWNED);
4859 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4860 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4863 free(*mvp, M_VNODE_MARKER);
4868 * These are helper functions for filesystems to traverse their
4869 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
4872 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4875 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4880 free(*mvp, M_VNODE_MARKER);
4884 static struct vnode *
4885 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4887 struct vnode *vp, *nvp;
4889 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
4890 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4892 vp = TAILQ_NEXT(*mvp, v_actfreelist);
4893 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4894 while (vp != NULL) {
4895 if (vp->v_type == VMARKER) {
4896 vp = TAILQ_NEXT(vp, v_actfreelist);
4899 if (!VI_TRYLOCK(vp)) {
4900 if (mp_ncpus == 1 || should_yield()) {
4901 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4902 mtx_unlock(&vnode_free_list_mtx);
4904 mtx_lock(&vnode_free_list_mtx);
4909 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
4910 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
4911 ("alien vnode on the active list %p %p", vp, mp));
4912 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
4914 nvp = TAILQ_NEXT(vp, v_actfreelist);
4919 /* Check if we are done */
4921 mtx_unlock(&vnode_free_list_mtx);
4922 mnt_vnode_markerfree_active(mvp, mp);
4925 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4926 mtx_unlock(&vnode_free_list_mtx);
4927 ASSERT_VI_LOCKED(vp, "active iter");
4928 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
4933 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4937 kern_yield(PRI_USER);
4938 mtx_lock(&vnode_free_list_mtx);
4939 return (mnt_vnode_next_active(mvp, mp));
4943 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
4947 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4951 (*mvp)->v_type = VMARKER;
4952 (*mvp)->v_mount = mp;
4954 mtx_lock(&vnode_free_list_mtx);
4955 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
4957 mtx_unlock(&vnode_free_list_mtx);
4958 mnt_vnode_markerfree_active(mvp, mp);
4961 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4962 return (mnt_vnode_next_active(mvp, mp));
4966 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4972 mtx_lock(&vnode_free_list_mtx);
4973 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4974 mtx_unlock(&vnode_free_list_mtx);
4975 mnt_vnode_markerfree_active(mvp, mp);