2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
38 * External virtual filesystem routines
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include "opt_compat.h"
46 #include "opt_watchdog.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/condvar.h>
54 #include <sys/dirent.h>
55 #include <sys/event.h>
56 #include <sys/eventhandler.h>
57 #include <sys/extattr.h>
59 #include <sys/fcntl.h>
62 #include <sys/kernel.h>
63 #include <sys/kthread.h>
64 #include <sys/lockf.h>
65 #include <sys/malloc.h>
66 #include <sys/mount.h>
67 #include <sys/namei.h>
68 #include <sys/pctrie.h>
70 #include <sys/reboot.h>
71 #include <sys/rwlock.h>
72 #include <sys/sched.h>
73 #include <sys/sleepqueue.h>
76 #include <sys/sysctl.h>
77 #include <sys/syslog.h>
78 #include <sys/vmmeter.h>
79 #include <sys/vnode.h>
80 #include <sys/watchdog.h>
82 #include <machine/stdarg.h>
84 #include <security/mac/mac_framework.h>
87 #include <vm/vm_object.h>
88 #include <vm/vm_extern.h>
90 #include <vm/vm_map.h>
91 #include <vm/vm_page.h>
92 #include <vm/vm_kern.h>
99 static void delmntque(struct vnode *vp);
100 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
101 int slpflag, int slptimeo);
102 static void syncer_shutdown(void *arg, int howto);
103 static int vtryrecycle(struct vnode *vp);
104 static void v_incr_usecount(struct vnode *);
105 static void v_decr_usecount(struct vnode *);
106 static void v_decr_useonly(struct vnode *);
107 static void v_upgrade_usecount(struct vnode *);
108 static void vnlru_free(int);
109 static void vgonel(struct vnode *);
110 static void vfs_knllock(void *arg);
111 static void vfs_knlunlock(void *arg);
112 static void vfs_knl_assert_locked(void *arg);
113 static void vfs_knl_assert_unlocked(void *arg);
114 static void destroy_vpollinfo(struct vpollinfo *vi);
117 * Number of vnodes in existence. Increased whenever getnewvnode()
118 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
120 static unsigned long numvnodes;
122 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
123 "Number of vnodes in existence");
125 static u_long vnodes_created;
126 SYSCTL_ULONG(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
127 0, "Number of vnodes created by getnewvnode");
130 * Conversion tables for conversion from vnode types to inode formats
133 enum vtype iftovt_tab[16] = {
134 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
135 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
137 int vttoif_tab[10] = {
138 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
139 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
143 * List of vnodes that are ready for recycling.
145 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
148 * Free vnode target. Free vnodes may simply be files which have been stat'd
149 * but not read. This is somewhat common, and a small cache of such files
150 * should be kept to avoid recreation costs.
152 static u_long wantfreevnodes;
153 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
154 /* Number of vnodes in the free list. */
155 static u_long freevnodes;
156 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
157 "Number of vnodes in the free list");
159 static int vlru_allow_cache_src;
160 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
161 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
163 static u_long recycles_count;
164 SYSCTL_ULONG(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 0,
165 "Number of vnodes recycled to avoid exceding kern.maxvnodes");
168 * Various variables used for debugging the new implementation of
170 * XXX these are probably of (very) limited utility now.
172 static int reassignbufcalls;
173 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
174 "Number of calls to reassignbuf");
176 static u_long free_owe_inact;
177 SYSCTL_ULONG(_vfs, OID_AUTO, free_owe_inact, CTLFLAG_RD, &free_owe_inact, 0,
178 "Number of times free vnodes kept on active list due to VFS "
179 "owing inactivation");
182 * Cache for the mount type id assigned to NFS. This is used for
183 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
185 int nfs_mount_type = -1;
187 /* To keep more than one thread at a time from running vfs_getnewfsid */
188 static struct mtx mntid_mtx;
191 * Lock for any access to the following:
196 static struct mtx vnode_free_list_mtx;
198 /* Publicly exported FS */
199 struct nfs_public nfs_pub;
201 static uma_zone_t buf_trie_zone;
203 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
204 static uma_zone_t vnode_zone;
205 static uma_zone_t vnodepoll_zone;
208 * The workitem queue.
210 * It is useful to delay writes of file data and filesystem metadata
211 * for tens of seconds so that quickly created and deleted files need
212 * not waste disk bandwidth being created and removed. To realize this,
213 * we append vnodes to a "workitem" queue. When running with a soft
214 * updates implementation, most pending metadata dependencies should
215 * not wait for more than a few seconds. Thus, mounted on block devices
216 * are delayed only about a half the time that file data is delayed.
217 * Similarly, directory updates are more critical, so are only delayed
218 * about a third the time that file data is delayed. Thus, there are
219 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
220 * one each second (driven off the filesystem syncer process). The
221 * syncer_delayno variable indicates the next queue that is to be processed.
222 * Items that need to be processed soon are placed in this queue:
224 * syncer_workitem_pending[syncer_delayno]
226 * A delay of fifteen seconds is done by placing the request fifteen
227 * entries later in the queue:
229 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
232 static int syncer_delayno;
233 static long syncer_mask;
234 LIST_HEAD(synclist, bufobj);
235 static struct synclist *syncer_workitem_pending;
237 * The sync_mtx protects:
242 * syncer_workitem_pending
243 * syncer_worklist_len
246 static struct mtx sync_mtx;
247 static struct cv sync_wakeup;
249 #define SYNCER_MAXDELAY 32
250 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
251 static int syncdelay = 30; /* max time to delay syncing data */
252 static int filedelay = 30; /* time to delay syncing files */
253 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
254 "Time to delay syncing files (in seconds)");
255 static int dirdelay = 29; /* time to delay syncing directories */
256 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
257 "Time to delay syncing directories (in seconds)");
258 static int metadelay = 28; /* time to delay syncing metadata */
259 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
260 "Time to delay syncing metadata (in seconds)");
261 static int rushjob; /* number of slots to run ASAP */
262 static int stat_rush_requests; /* number of times I/O speeded up */
263 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
264 "Number of times I/O speeded up (rush requests)");
267 * When shutting down the syncer, run it at four times normal speed.
269 #define SYNCER_SHUTDOWN_SPEEDUP 4
270 static int sync_vnode_count;
271 static int syncer_worklist_len;
272 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
276 * Number of vnodes we want to exist at any one time. This is mostly used
277 * to size hash tables in vnode-related code. It is normally not used in
278 * getnewvnode(), as wantfreevnodes is normally nonzero.)
280 * XXX desiredvnodes is historical cruft and should not exist.
285 sysctl_update_desiredvnodes(SYSCTL_HANDLER_ARGS)
287 int error, old_desiredvnodes;
289 old_desiredvnodes = desiredvnodes;
290 if ((error = sysctl_handle_int(oidp, arg1, arg2, req)) != 0)
292 if (old_desiredvnodes != desiredvnodes) {
293 vfs_hash_changesize(desiredvnodes);
294 cache_changesize(desiredvnodes);
299 SYSCTL_PROC(_kern, KERN_MAXVNODES, maxvnodes,
300 CTLTYPE_INT | CTLFLAG_MPSAFE | CTLFLAG_RW, &desiredvnodes, 0,
301 sysctl_update_desiredvnodes, "I", "Maximum number of vnodes");
302 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
303 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
304 static int vnlru_nowhere;
305 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
306 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
308 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
312 * Support for the bufobj clean & dirty pctrie.
315 buf_trie_alloc(struct pctrie *ptree)
318 return uma_zalloc(buf_trie_zone, M_NOWAIT);
322 buf_trie_free(struct pctrie *ptree, void *node)
325 uma_zfree(buf_trie_zone, node);
327 PCTRIE_DEFINE(BUF, buf, b_lblkno, buf_trie_alloc, buf_trie_free);
330 * Initialize the vnode management data structures.
332 * Reevaluate the following cap on the number of vnodes after the physical
333 * memory size exceeds 512GB. In the limit, as the physical memory size
334 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
336 #ifndef MAXVNODES_MAX
337 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
340 vntblinit(void *dummy __unused)
343 int physvnodes, virtvnodes;
346 * Desiredvnodes is a function of the physical memory size and the
347 * kernel's heap size. Generally speaking, it scales with the
348 * physical memory size. The ratio of desiredvnodes to physical pages
349 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
350 * marginal ratio of desiredvnodes to physical pages is one to
351 * sixteen. However, desiredvnodes is limited by the kernel's heap
352 * size. The memory required by desiredvnodes vnodes and vm objects
353 * may not exceed one seventh of the kernel's heap size.
355 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
356 cnt.v_page_count) / 16;
357 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
358 sizeof(struct vnode)));
359 desiredvnodes = min(physvnodes, virtvnodes);
360 if (desiredvnodes > MAXVNODES_MAX) {
362 printf("Reducing kern.maxvnodes %d -> %d\n",
363 desiredvnodes, MAXVNODES_MAX);
364 desiredvnodes = MAXVNODES_MAX;
366 wantfreevnodes = desiredvnodes / 4;
367 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
368 TAILQ_INIT(&vnode_free_list);
369 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
370 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
371 NULL, NULL, UMA_ALIGN_PTR, 0);
372 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
373 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
375 * Preallocate enough nodes to support one-per buf so that
376 * we can not fail an insert. reassignbuf() callers can not
377 * tolerate the insertion failure.
379 buf_trie_zone = uma_zcreate("BUF TRIE", pctrie_node_size(),
380 NULL, NULL, pctrie_zone_init, NULL, UMA_ALIGN_PTR,
381 UMA_ZONE_NOFREE | UMA_ZONE_VM);
382 uma_prealloc(buf_trie_zone, nbuf);
384 * Initialize the filesystem syncer.
386 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
388 syncer_maxdelay = syncer_mask + 1;
389 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
390 cv_init(&sync_wakeup, "syncer");
391 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
395 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
399 * Mark a mount point as busy. Used to synchronize access and to delay
400 * unmounting. Eventually, mountlist_mtx is not released on failure.
402 * vfs_busy() is a custom lock, it can block the caller.
403 * vfs_busy() only sleeps if the unmount is active on the mount point.
404 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
405 * vnode belonging to mp.
407 * Lookup uses vfs_busy() to traverse mount points.
409 * / vnode lock A / vnode lock (/var) D
410 * /var vnode lock B /log vnode lock(/var/log) E
411 * vfs_busy lock C vfs_busy lock F
413 * Within each file system, the lock order is C->A->B and F->D->E.
415 * When traversing across mounts, the system follows that lock order:
421 * The lookup() process for namei("/var") illustrates the process:
422 * VOP_LOOKUP() obtains B while A is held
423 * vfs_busy() obtains a shared lock on F while A and B are held
424 * vput() releases lock on B
425 * vput() releases lock on A
426 * VFS_ROOT() obtains lock on D while shared lock on F is held
427 * vfs_unbusy() releases shared lock on F
428 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
429 * Attempt to lock A (instead of vp_crossmp) while D is held would
430 * violate the global order, causing deadlocks.
432 * dounmount() locks B while F is drained.
435 vfs_busy(struct mount *mp, int flags)
438 MPASS((flags & ~MBF_MASK) == 0);
439 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
444 * If mount point is currenly being unmounted, sleep until the
445 * mount point fate is decided. If thread doing the unmounting fails,
446 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
447 * that this mount point has survived the unmount attempt and vfs_busy
448 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
449 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
450 * about to be really destroyed. vfs_busy needs to release its
451 * reference on the mount point in this case and return with ENOENT,
452 * telling the caller that mount mount it tried to busy is no longer
455 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
456 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
459 CTR1(KTR_VFS, "%s: failed busying before sleeping",
463 if (flags & MBF_MNTLSTLOCK)
464 mtx_unlock(&mountlist_mtx);
465 mp->mnt_kern_flag |= MNTK_MWAIT;
466 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
467 if (flags & MBF_MNTLSTLOCK)
468 mtx_lock(&mountlist_mtx);
471 if (flags & MBF_MNTLSTLOCK)
472 mtx_unlock(&mountlist_mtx);
479 * Free a busy filesystem.
482 vfs_unbusy(struct mount *mp)
485 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
488 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
490 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
491 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
492 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
493 mp->mnt_kern_flag &= ~MNTK_DRAINING;
494 wakeup(&mp->mnt_lockref);
500 * Lookup a mount point by filesystem identifier.
503 vfs_getvfs(fsid_t *fsid)
507 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
508 mtx_lock(&mountlist_mtx);
509 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
510 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
511 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
513 mtx_unlock(&mountlist_mtx);
517 mtx_unlock(&mountlist_mtx);
518 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
519 return ((struct mount *) 0);
523 * Lookup a mount point by filesystem identifier, busying it before
526 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
527 * cache for popular filesystem identifiers. The cache is lockess, using
528 * the fact that struct mount's are never freed. In worst case we may
529 * get pointer to unmounted or even different filesystem, so we have to
530 * check what we got, and go slow way if so.
533 vfs_busyfs(fsid_t *fsid)
535 #define FSID_CACHE_SIZE 256
536 typedef struct mount * volatile vmp_t;
537 static vmp_t cache[FSID_CACHE_SIZE];
542 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
543 hash = fsid->val[0] ^ fsid->val[1];
544 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
547 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
548 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
550 if (vfs_busy(mp, 0) != 0) {
554 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
555 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
561 mtx_lock(&mountlist_mtx);
562 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
563 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
564 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
565 error = vfs_busy(mp, MBF_MNTLSTLOCK);
568 mtx_unlock(&mountlist_mtx);
575 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
576 mtx_unlock(&mountlist_mtx);
577 return ((struct mount *) 0);
581 * Check if a user can access privileged mount options.
584 vfs_suser(struct mount *mp, struct thread *td)
589 * If the thread is jailed, but this is not a jail-friendly file
590 * system, deny immediately.
592 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
596 * If the file system was mounted outside the jail of the calling
597 * thread, deny immediately.
599 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
603 * If file system supports delegated administration, we don't check
604 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
605 * by the file system itself.
606 * If this is not the user that did original mount, we check for
607 * the PRIV_VFS_MOUNT_OWNER privilege.
609 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
610 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
611 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
618 * Get a new unique fsid. Try to make its val[0] unique, since this value
619 * will be used to create fake device numbers for stat(). Also try (but
620 * not so hard) make its val[0] unique mod 2^16, since some emulators only
621 * support 16-bit device numbers. We end up with unique val[0]'s for the
622 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
624 * Keep in mind that several mounts may be running in parallel. Starting
625 * the search one past where the previous search terminated is both a
626 * micro-optimization and a defense against returning the same fsid to
630 vfs_getnewfsid(struct mount *mp)
632 static uint16_t mntid_base;
637 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
638 mtx_lock(&mntid_mtx);
639 mtype = mp->mnt_vfc->vfc_typenum;
640 tfsid.val[1] = mtype;
641 mtype = (mtype & 0xFF) << 24;
643 tfsid.val[0] = makedev(255,
644 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
646 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
650 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
651 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
652 mtx_unlock(&mntid_mtx);
656 * Knob to control the precision of file timestamps:
658 * 0 = seconds only; nanoseconds zeroed.
659 * 1 = seconds and nanoseconds, accurate within 1/HZ.
660 * 2 = seconds and nanoseconds, truncated to microseconds.
661 * >=3 = seconds and nanoseconds, maximum precision.
663 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
665 static int timestamp_precision = TSP_USEC;
666 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
667 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
668 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
669 "3+: sec + ns (max. precision))");
672 * Get a current timestamp.
675 vfs_timestamp(struct timespec *tsp)
679 switch (timestamp_precision) {
681 tsp->tv_sec = time_second;
689 TIMEVAL_TO_TIMESPEC(&tv, tsp);
699 * Set vnode attributes to VNOVAL
702 vattr_null(struct vattr *vap)
706 vap->va_size = VNOVAL;
707 vap->va_bytes = VNOVAL;
708 vap->va_mode = VNOVAL;
709 vap->va_nlink = VNOVAL;
710 vap->va_uid = VNOVAL;
711 vap->va_gid = VNOVAL;
712 vap->va_fsid = VNOVAL;
713 vap->va_fileid = VNOVAL;
714 vap->va_blocksize = VNOVAL;
715 vap->va_rdev = VNOVAL;
716 vap->va_atime.tv_sec = VNOVAL;
717 vap->va_atime.tv_nsec = VNOVAL;
718 vap->va_mtime.tv_sec = VNOVAL;
719 vap->va_mtime.tv_nsec = VNOVAL;
720 vap->va_ctime.tv_sec = VNOVAL;
721 vap->va_ctime.tv_nsec = VNOVAL;
722 vap->va_birthtime.tv_sec = VNOVAL;
723 vap->va_birthtime.tv_nsec = VNOVAL;
724 vap->va_flags = VNOVAL;
725 vap->va_gen = VNOVAL;
730 * This routine is called when we have too many vnodes. It attempts
731 * to free <count> vnodes and will potentially free vnodes that still
732 * have VM backing store (VM backing store is typically the cause
733 * of a vnode blowout so we want to do this). Therefore, this operation
734 * is not considered cheap.
736 * A number of conditions may prevent a vnode from being reclaimed.
737 * the buffer cache may have references on the vnode, a directory
738 * vnode may still have references due to the namei cache representing
739 * underlying files, or the vnode may be in active use. It is not
740 * desireable to reuse such vnodes. These conditions may cause the
741 * number of vnodes to reach some minimum value regardless of what
742 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
745 vlrureclaim(struct mount *mp)
754 * Calculate the trigger point, don't allow user
755 * screwups to blow us up. This prevents us from
756 * recycling vnodes with lots of resident pages. We
757 * aren't trying to free memory, we are trying to
760 usevnodes = desiredvnodes;
763 trigger = cnt.v_page_count * 2 / usevnodes;
765 vn_start_write(NULL, &mp, V_WAIT);
767 count = mp->mnt_nvnodelistsize / 10 + 1;
769 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
770 while (vp != NULL && vp->v_type == VMARKER)
771 vp = TAILQ_NEXT(vp, v_nmntvnodes);
774 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
775 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
780 * If it's been deconstructed already, it's still
781 * referenced, or it exceeds the trigger, skip it.
783 if (vp->v_usecount ||
784 (!vlru_allow_cache_src &&
785 !LIST_EMPTY(&(vp)->v_cache_src)) ||
786 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
787 vp->v_object->resident_page_count > trigger)) {
793 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
795 goto next_iter_mntunlocked;
799 * v_usecount may have been bumped after VOP_LOCK() dropped
800 * the vnode interlock and before it was locked again.
802 * It is not necessary to recheck VI_DOOMED because it can
803 * only be set by another thread that holds both the vnode
804 * lock and vnode interlock. If another thread has the
805 * vnode lock before we get to VOP_LOCK() and obtains the
806 * vnode interlock after VOP_LOCK() drops the vnode
807 * interlock, the other thread will be unable to drop the
808 * vnode lock before our VOP_LOCK() call fails.
810 if (vp->v_usecount ||
811 (!vlru_allow_cache_src &&
812 !LIST_EMPTY(&(vp)->v_cache_src)) ||
813 (vp->v_object != NULL &&
814 vp->v_object->resident_page_count > trigger)) {
815 VOP_UNLOCK(vp, LK_INTERLOCK);
817 goto next_iter_mntunlocked;
819 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
820 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
821 atomic_add_long(&recycles_count, 1);
826 next_iter_mntunlocked:
835 kern_yield(PRI_USER);
840 vn_finished_write(mp);
845 * Attempt to keep the free list at wantfreevnodes length.
848 vnlru_free(int count)
852 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
853 for (; count > 0; count--) {
854 vp = TAILQ_FIRST(&vnode_free_list);
856 * The list can be modified while the free_list_mtx
857 * has been dropped and vp could be NULL here.
861 VNASSERT(vp->v_op != NULL, vp,
862 ("vnlru_free: vnode already reclaimed."));
863 KASSERT((vp->v_iflag & VI_FREE) != 0,
864 ("Removing vnode not on freelist"));
865 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
866 ("Mangling active vnode"));
867 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
869 * Don't recycle if we can't get the interlock.
871 if (!VI_TRYLOCK(vp)) {
872 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
875 VNASSERT((vp->v_iflag & VI_FREE) != 0 && vp->v_holdcnt == 0,
876 vp, ("vp inconsistent on freelist"));
879 * The clear of VI_FREE prevents activation of the
880 * vnode. There is no sense in putting the vnode on
881 * the mount point active list, only to remove it
882 * later during recycling. Inline the relevant part
883 * of vholdl(), to avoid triggering assertions or
887 vp->v_iflag &= ~VI_FREE;
890 mtx_unlock(&vnode_free_list_mtx);
894 * If the recycled succeeded this vdrop will actually free
895 * the vnode. If not it will simply place it back on
899 mtx_lock(&vnode_free_list_mtx);
903 * Attempt to recycle vnodes in a context that is always safe to block.
904 * Calling vlrurecycle() from the bowels of filesystem code has some
905 * interesting deadlock problems.
907 static struct proc *vnlruproc;
908 static int vnlruproc_sig;
913 struct mount *mp, *nmp;
915 struct proc *p = vnlruproc;
917 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
921 kproc_suspend_check(p);
922 mtx_lock(&vnode_free_list_mtx);
923 if (freevnodes > wantfreevnodes)
924 vnlru_free(freevnodes - wantfreevnodes);
925 if (numvnodes <= desiredvnodes * 9 / 10) {
927 wakeup(&vnlruproc_sig);
928 msleep(vnlruproc, &vnode_free_list_mtx,
929 PVFS|PDROP, "vlruwt", hz);
932 mtx_unlock(&vnode_free_list_mtx);
934 mtx_lock(&mountlist_mtx);
935 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
936 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
937 nmp = TAILQ_NEXT(mp, mnt_list);
940 done += vlrureclaim(mp);
941 mtx_lock(&mountlist_mtx);
942 nmp = TAILQ_NEXT(mp, mnt_list);
945 mtx_unlock(&mountlist_mtx);
948 /* These messages are temporary debugging aids */
949 if (vnlru_nowhere < 5)
950 printf("vnlru process getting nowhere..\n");
951 else if (vnlru_nowhere == 5)
952 printf("vnlru process messages stopped.\n");
955 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
957 kern_yield(PRI_USER);
961 static struct kproc_desc vnlru_kp = {
966 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
970 * Routines having to do with the management of the vnode table.
974 * Try to recycle a freed vnode. We abort if anyone picks up a reference
975 * before we actually vgone(). This function must be called with the vnode
976 * held to prevent the vnode from being returned to the free list midway
980 vtryrecycle(struct vnode *vp)
984 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
985 VNASSERT(vp->v_holdcnt, vp,
986 ("vtryrecycle: Recycling vp %p without a reference.", vp));
988 * This vnode may found and locked via some other list, if so we
989 * can't recycle it yet.
991 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
993 "%s: impossible to recycle, vp %p lock is already held",
995 return (EWOULDBLOCK);
998 * Don't recycle if its filesystem is being suspended.
1000 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
1003 "%s: impossible to recycle, cannot start the write for %p",
1008 * If we got this far, we need to acquire the interlock and see if
1009 * anyone picked up this vnode from another list. If not, we will
1010 * mark it with DOOMED via vgonel() so that anyone who does find it
1011 * will skip over it.
1014 if (vp->v_usecount) {
1015 VOP_UNLOCK(vp, LK_INTERLOCK);
1016 vn_finished_write(vnmp);
1018 "%s: impossible to recycle, %p is already referenced",
1022 if ((vp->v_iflag & VI_DOOMED) == 0) {
1023 atomic_add_long(&recycles_count, 1);
1026 VOP_UNLOCK(vp, LK_INTERLOCK);
1027 vn_finished_write(vnmp);
1032 * Wait for available vnodes.
1035 getnewvnode_wait(int suspended)
1038 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
1039 if (numvnodes > desiredvnodes) {
1042 * File system is beeing suspended, we cannot risk a
1043 * deadlock here, so allocate new vnode anyway.
1045 if (freevnodes > wantfreevnodes)
1046 vnlru_free(freevnodes - wantfreevnodes);
1049 if (vnlruproc_sig == 0) {
1050 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1053 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1056 return (numvnodes > desiredvnodes ? ENFILE : 0);
1060 getnewvnode_reserve(u_int count)
1065 /* First try to be quick and racy. */
1066 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1067 td->td_vp_reserv += count;
1070 atomic_subtract_long(&numvnodes, count);
1072 mtx_lock(&vnode_free_list_mtx);
1074 if (getnewvnode_wait(0) == 0) {
1077 atomic_add_long(&numvnodes, 1);
1080 mtx_unlock(&vnode_free_list_mtx);
1084 getnewvnode_drop_reserve(void)
1089 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1090 td->td_vp_reserv = 0;
1094 * Return the next vnode from the free list.
1097 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1105 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1108 if (td->td_vp_reserv > 0) {
1109 td->td_vp_reserv -= 1;
1112 mtx_lock(&vnode_free_list_mtx);
1114 * Lend our context to reclaim vnodes if they've exceeded the max.
1116 if (freevnodes > wantfreevnodes)
1118 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1120 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1122 mtx_unlock(&vnode_free_list_mtx);
1126 atomic_add_long(&numvnodes, 1);
1127 mtx_unlock(&vnode_free_list_mtx);
1129 atomic_add_long(&vnodes_created, 1);
1130 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1134 vp->v_vnlock = &vp->v_lock;
1135 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1137 * By default, don't allow shared locks unless filesystems
1140 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE | LK_IS_VNODE);
1142 * Initialize bufobj.
1145 bo->__bo_vnode = vp;
1146 rw_init(BO_LOCKPTR(bo), "bufobj interlock");
1147 bo->bo_ops = &buf_ops_bio;
1148 bo->bo_private = vp;
1149 TAILQ_INIT(&bo->bo_clean.bv_hd);
1150 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1152 * Initialize namecache.
1154 LIST_INIT(&vp->v_cache_src);
1155 TAILQ_INIT(&vp->v_cache_dst);
1157 * Finalize various vnode identity bits.
1162 v_incr_usecount(vp);
1166 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1167 mac_vnode_associate_singlelabel(mp, vp);
1168 else if (mp == NULL && vops != &dead_vnodeops)
1169 printf("NULL mp in getnewvnode()\n");
1172 bo->bo_bsize = mp->mnt_stat.f_iosize;
1173 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1174 vp->v_vflag |= VV_NOKNOTE;
1176 rangelock_init(&vp->v_rl);
1179 * For the filesystems which do not use vfs_hash_insert(),
1180 * still initialize v_hash to have vfs_hash_index() useful.
1181 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1184 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1191 * Delete from old mount point vnode list, if on one.
1194 delmntque(struct vnode *vp)
1204 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1205 ("Active vnode list size %d > Vnode list size %d",
1206 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1207 active = vp->v_iflag & VI_ACTIVE;
1208 vp->v_iflag &= ~VI_ACTIVE;
1210 mtx_lock(&vnode_free_list_mtx);
1211 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1212 mp->mnt_activevnodelistsize--;
1213 mtx_unlock(&vnode_free_list_mtx);
1217 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1218 ("bad mount point vnode list size"));
1219 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1220 mp->mnt_nvnodelistsize--;
1226 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1230 vp->v_op = &dead_vnodeops;
1236 * Insert into list of vnodes for the new mount point, if available.
1239 insmntque1(struct vnode *vp, struct mount *mp,
1240 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1243 KASSERT(vp->v_mount == NULL,
1244 ("insmntque: vnode already on per mount vnode list"));
1245 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1246 ASSERT_VOP_ELOCKED(vp, "insmntque: non-locked vp");
1249 * We acquire the vnode interlock early to ensure that the
1250 * vnode cannot be recycled by another process releasing a
1251 * holdcnt on it before we get it on both the vnode list
1252 * and the active vnode list. The mount mutex protects only
1253 * manipulation of the vnode list and the vnode freelist
1254 * mutex protects only manipulation of the active vnode list.
1255 * Hence the need to hold the vnode interlock throughout.
1259 if (((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1260 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1261 mp->mnt_nvnodelistsize == 0)) &&
1262 (vp->v_vflag & VV_FORCEINSMQ) == 0) {
1271 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1272 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1273 ("neg mount point vnode list size"));
1274 mp->mnt_nvnodelistsize++;
1275 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1276 ("Activating already active vnode"));
1277 vp->v_iflag |= VI_ACTIVE;
1278 mtx_lock(&vnode_free_list_mtx);
1279 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1280 mp->mnt_activevnodelistsize++;
1281 mtx_unlock(&vnode_free_list_mtx);
1288 insmntque(struct vnode *vp, struct mount *mp)
1291 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1295 * Flush out and invalidate all buffers associated with a bufobj
1296 * Called with the underlying object locked.
1299 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1304 if (flags & V_SAVE) {
1305 error = bufobj_wwait(bo, slpflag, slptimeo);
1310 if (bo->bo_dirty.bv_cnt > 0) {
1312 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1315 * XXX We could save a lock/unlock if this was only
1316 * enabled under INVARIANTS
1319 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1320 panic("vinvalbuf: dirty bufs");
1324 * If you alter this loop please notice that interlock is dropped and
1325 * reacquired in flushbuflist. Special care is needed to ensure that
1326 * no race conditions occur from this.
1329 error = flushbuflist(&bo->bo_clean,
1330 flags, bo, slpflag, slptimeo);
1331 if (error == 0 && !(flags & V_CLEANONLY))
1332 error = flushbuflist(&bo->bo_dirty,
1333 flags, bo, slpflag, slptimeo);
1334 if (error != 0 && error != EAGAIN) {
1338 } while (error != 0);
1341 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1342 * have write I/O in-progress but if there is a VM object then the
1343 * VM object can also have read-I/O in-progress.
1346 bufobj_wwait(bo, 0, 0);
1348 if (bo->bo_object != NULL) {
1349 VM_OBJECT_WLOCK(bo->bo_object);
1350 vm_object_pip_wait(bo->bo_object, "bovlbx");
1351 VM_OBJECT_WUNLOCK(bo->bo_object);
1354 } while (bo->bo_numoutput > 0);
1358 * Destroy the copy in the VM cache, too.
1360 if (bo->bo_object != NULL &&
1361 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1362 VM_OBJECT_WLOCK(bo->bo_object);
1363 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1364 OBJPR_CLEANONLY : 0);
1365 VM_OBJECT_WUNLOCK(bo->bo_object);
1370 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1371 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1372 panic("vinvalbuf: flush failed");
1379 * Flush out and invalidate all buffers associated with a vnode.
1380 * Called with the underlying object locked.
1383 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1386 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1387 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1388 if (vp->v_object != NULL && vp->v_object->handle != vp)
1390 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1394 * Flush out buffers on the specified list.
1398 flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1401 struct buf *bp, *nbp;
1406 ASSERT_BO_WLOCKED(bo);
1409 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1410 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1411 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1417 lblkno = nbp->b_lblkno;
1418 xflags = nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN);
1421 error = BUF_TIMELOCK(bp,
1422 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_LOCKPTR(bo),
1423 "flushbuf", slpflag, slptimeo);
1426 return (error != ENOLCK ? error : EAGAIN);
1428 KASSERT(bp->b_bufobj == bo,
1429 ("bp %p wrong b_bufobj %p should be %p",
1430 bp, bp->b_bufobj, bo));
1431 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1437 * XXX Since there are no node locks for NFS, I
1438 * believe there is a slight chance that a delayed
1439 * write will occur while sleeping just above, so
1442 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1445 bp->b_flags |= B_ASYNC;
1448 return (EAGAIN); /* XXX: why not loop ? */
1451 bp->b_flags |= (B_INVAL | B_RELBUF);
1452 bp->b_flags &= ~B_ASYNC;
1456 (nbp->b_bufobj != bo ||
1457 nbp->b_lblkno != lblkno ||
1458 (nbp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1459 break; /* nbp invalid */
1465 * Truncate a file's buffer and pages to a specified length. This
1466 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1470 vtruncbuf(struct vnode *vp, struct ucred *cred, off_t length, int blksize)
1472 struct buf *bp, *nbp;
1477 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1478 vp, cred, blksize, (uintmax_t)length);
1481 * Round up to the *next* lbn.
1483 trunclbn = (length + blksize - 1) / blksize;
1485 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1492 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1493 if (bp->b_lblkno < trunclbn)
1496 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1497 BO_LOCKPTR(bo)) == ENOLCK)
1501 bp->b_flags |= (B_INVAL | B_RELBUF);
1502 bp->b_flags &= ~B_ASYNC;
1508 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1509 (nbp->b_vp != vp) ||
1510 (nbp->b_flags & B_DELWRI))) {
1516 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1517 if (bp->b_lblkno < trunclbn)
1520 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1521 BO_LOCKPTR(bo)) == ENOLCK)
1524 bp->b_flags |= (B_INVAL | B_RELBUF);
1525 bp->b_flags &= ~B_ASYNC;
1531 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1532 (nbp->b_vp != vp) ||
1533 (nbp->b_flags & B_DELWRI) == 0)) {
1542 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1543 if (bp->b_lblkno > 0)
1546 * Since we hold the vnode lock this should only
1547 * fail if we're racing with the buf daemon.
1550 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1551 BO_LOCKPTR(bo)) == ENOLCK) {
1554 VNASSERT((bp->b_flags & B_DELWRI), vp,
1555 ("buf(%p) on dirty queue without DELWRI", bp));
1564 bufobj_wwait(bo, 0, 0);
1566 vnode_pager_setsize(vp, length);
1572 buf_vlist_remove(struct buf *bp)
1576 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1577 ASSERT_BO_WLOCKED(bp->b_bufobj);
1578 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1579 (BX_VNDIRTY|BX_VNCLEAN),
1580 ("buf_vlist_remove: Buf %p is on two lists", bp));
1581 if (bp->b_xflags & BX_VNDIRTY)
1582 bv = &bp->b_bufobj->bo_dirty;
1584 bv = &bp->b_bufobj->bo_clean;
1585 BUF_PCTRIE_REMOVE(&bv->bv_root, bp->b_lblkno);
1586 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1588 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1592 * Add the buffer to the sorted clean or dirty block list.
1594 * NOTE: xflags is passed as a constant, optimizing this inline function!
1597 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1603 ASSERT_BO_WLOCKED(bo);
1604 KASSERT((xflags & BX_VNDIRTY) == 0 || (bo->bo_flag & BO_DEAD) == 0,
1605 ("dead bo %p", bo));
1606 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1607 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1608 bp->b_xflags |= xflags;
1609 if (xflags & BX_VNDIRTY)
1615 * Keep the list ordered. Optimize empty list insertion. Assume
1616 * we tend to grow at the tail so lookup_le should usually be cheaper
1619 if (bv->bv_cnt == 0 ||
1620 bp->b_lblkno > TAILQ_LAST(&bv->bv_hd, buflists)->b_lblkno)
1621 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1622 else if ((n = BUF_PCTRIE_LOOKUP_LE(&bv->bv_root, bp->b_lblkno)) == NULL)
1623 TAILQ_INSERT_HEAD(&bv->bv_hd, bp, b_bobufs);
1625 TAILQ_INSERT_AFTER(&bv->bv_hd, n, bp, b_bobufs);
1626 error = BUF_PCTRIE_INSERT(&bv->bv_root, bp);
1628 panic("buf_vlist_add: Preallocated nodes insufficient.");
1633 * Lookup a buffer using the splay tree. Note that we specifically avoid
1634 * shadow buffers used in background bitmap writes.
1636 * This code isn't quite efficient as it could be because we are maintaining
1637 * two sorted lists and do not know which list the block resides in.
1639 * During a "make buildworld" the desired buffer is found at one of
1640 * the roots more than 60% of the time. Thus, checking both roots
1641 * before performing either splay eliminates unnecessary splays on the
1642 * first tree splayed.
1645 gbincore(struct bufobj *bo, daddr_t lblkno)
1649 ASSERT_BO_LOCKED(bo);
1650 bp = BUF_PCTRIE_LOOKUP(&bo->bo_clean.bv_root, lblkno);
1653 return BUF_PCTRIE_LOOKUP(&bo->bo_dirty.bv_root, lblkno);
1657 * Associate a buffer with a vnode.
1660 bgetvp(struct vnode *vp, struct buf *bp)
1665 ASSERT_BO_WLOCKED(bo);
1666 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1668 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1669 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1670 ("bgetvp: bp already attached! %p", bp));
1676 * Insert onto list for new vnode.
1678 buf_vlist_add(bp, bo, BX_VNCLEAN);
1682 * Disassociate a buffer from a vnode.
1685 brelvp(struct buf *bp)
1690 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1691 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1694 * Delete from old vnode list, if on one.
1696 vp = bp->b_vp; /* XXX */
1699 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1700 buf_vlist_remove(bp);
1702 panic("brelvp: Buffer %p not on queue.", bp);
1703 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1704 bo->bo_flag &= ~BO_ONWORKLST;
1705 mtx_lock(&sync_mtx);
1706 LIST_REMOVE(bo, bo_synclist);
1707 syncer_worklist_len--;
1708 mtx_unlock(&sync_mtx);
1711 bp->b_bufobj = NULL;
1717 * Add an item to the syncer work queue.
1720 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1724 ASSERT_BO_WLOCKED(bo);
1726 mtx_lock(&sync_mtx);
1727 if (bo->bo_flag & BO_ONWORKLST)
1728 LIST_REMOVE(bo, bo_synclist);
1730 bo->bo_flag |= BO_ONWORKLST;
1731 syncer_worklist_len++;
1734 if (delay > syncer_maxdelay - 2)
1735 delay = syncer_maxdelay - 2;
1736 slot = (syncer_delayno + delay) & syncer_mask;
1738 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
1739 mtx_unlock(&sync_mtx);
1743 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1747 mtx_lock(&sync_mtx);
1748 len = syncer_worklist_len - sync_vnode_count;
1749 mtx_unlock(&sync_mtx);
1750 error = SYSCTL_OUT(req, &len, sizeof(len));
1754 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1755 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1757 static struct proc *updateproc;
1758 static void sched_sync(void);
1759 static struct kproc_desc up_kp = {
1764 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1767 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1772 *bo = LIST_FIRST(slp);
1775 vp = (*bo)->__bo_vnode; /* XXX */
1776 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1779 * We use vhold in case the vnode does not
1780 * successfully sync. vhold prevents the vnode from
1781 * going away when we unlock the sync_mtx so that
1782 * we can acquire the vnode interlock.
1785 mtx_unlock(&sync_mtx);
1787 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1789 mtx_lock(&sync_mtx);
1790 return (*bo == LIST_FIRST(slp));
1792 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1793 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1795 vn_finished_write(mp);
1797 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1799 * Put us back on the worklist. The worklist
1800 * routine will remove us from our current
1801 * position and then add us back in at a later
1804 vn_syncer_add_to_worklist(*bo, syncdelay);
1808 mtx_lock(&sync_mtx);
1812 static int first_printf = 1;
1815 * System filesystem synchronizer daemon.
1820 struct synclist *next, *slp;
1823 struct thread *td = curthread;
1825 int net_worklist_len;
1826 int syncer_final_iter;
1830 syncer_final_iter = 0;
1831 syncer_state = SYNCER_RUNNING;
1832 starttime = time_uptime;
1833 td->td_pflags |= TDP_NORUNNINGBUF;
1835 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1838 mtx_lock(&sync_mtx);
1840 if (syncer_state == SYNCER_FINAL_DELAY &&
1841 syncer_final_iter == 0) {
1842 mtx_unlock(&sync_mtx);
1843 kproc_suspend_check(td->td_proc);
1844 mtx_lock(&sync_mtx);
1846 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1847 if (syncer_state != SYNCER_RUNNING &&
1848 starttime != time_uptime) {
1850 printf("\nSyncing disks, vnodes remaining...");
1853 printf("%d ", net_worklist_len);
1855 starttime = time_uptime;
1858 * Push files whose dirty time has expired. Be careful
1859 * of interrupt race on slp queue.
1861 * Skip over empty worklist slots when shutting down.
1864 slp = &syncer_workitem_pending[syncer_delayno];
1865 syncer_delayno += 1;
1866 if (syncer_delayno == syncer_maxdelay)
1868 next = &syncer_workitem_pending[syncer_delayno];
1870 * If the worklist has wrapped since the
1871 * it was emptied of all but syncer vnodes,
1872 * switch to the FINAL_DELAY state and run
1873 * for one more second.
1875 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1876 net_worklist_len == 0 &&
1877 last_work_seen == syncer_delayno) {
1878 syncer_state = SYNCER_FINAL_DELAY;
1879 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1881 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1882 syncer_worklist_len > 0);
1885 * Keep track of the last time there was anything
1886 * on the worklist other than syncer vnodes.
1887 * Return to the SHUTTING_DOWN state if any
1890 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1891 last_work_seen = syncer_delayno;
1892 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1893 syncer_state = SYNCER_SHUTTING_DOWN;
1894 while (!LIST_EMPTY(slp)) {
1895 error = sync_vnode(slp, &bo, td);
1897 LIST_REMOVE(bo, bo_synclist);
1898 LIST_INSERT_HEAD(next, bo, bo_synclist);
1902 if (first_printf == 0)
1903 wdog_kern_pat(WD_LASTVAL);
1906 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1907 syncer_final_iter--;
1909 * The variable rushjob allows the kernel to speed up the
1910 * processing of the filesystem syncer process. A rushjob
1911 * value of N tells the filesystem syncer to process the next
1912 * N seconds worth of work on its queue ASAP. Currently rushjob
1913 * is used by the soft update code to speed up the filesystem
1914 * syncer process when the incore state is getting so far
1915 * ahead of the disk that the kernel memory pool is being
1916 * threatened with exhaustion.
1923 * Just sleep for a short period of time between
1924 * iterations when shutting down to allow some I/O
1927 * If it has taken us less than a second to process the
1928 * current work, then wait. Otherwise start right over
1929 * again. We can still lose time if any single round
1930 * takes more than two seconds, but it does not really
1931 * matter as we are just trying to generally pace the
1932 * filesystem activity.
1934 if (syncer_state != SYNCER_RUNNING ||
1935 time_uptime == starttime) {
1937 sched_prio(td, PPAUSE);
1940 if (syncer_state != SYNCER_RUNNING)
1941 cv_timedwait(&sync_wakeup, &sync_mtx,
1942 hz / SYNCER_SHUTDOWN_SPEEDUP);
1943 else if (time_uptime == starttime)
1944 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1949 * Request the syncer daemon to speed up its work.
1950 * We never push it to speed up more than half of its
1951 * normal turn time, otherwise it could take over the cpu.
1954 speedup_syncer(void)
1958 mtx_lock(&sync_mtx);
1959 if (rushjob < syncdelay / 2) {
1961 stat_rush_requests += 1;
1964 mtx_unlock(&sync_mtx);
1965 cv_broadcast(&sync_wakeup);
1970 * Tell the syncer to speed up its work and run though its work
1971 * list several times, then tell it to shut down.
1974 syncer_shutdown(void *arg, int howto)
1977 if (howto & RB_NOSYNC)
1979 mtx_lock(&sync_mtx);
1980 syncer_state = SYNCER_SHUTTING_DOWN;
1982 mtx_unlock(&sync_mtx);
1983 cv_broadcast(&sync_wakeup);
1984 kproc_shutdown(arg, howto);
1988 syncer_suspend(void)
1991 syncer_shutdown(updateproc, 0);
1998 mtx_lock(&sync_mtx);
2000 syncer_state = SYNCER_RUNNING;
2001 mtx_unlock(&sync_mtx);
2002 cv_broadcast(&sync_wakeup);
2003 kproc_resume(updateproc);
2007 * Reassign a buffer from one vnode to another.
2008 * Used to assign file specific control information
2009 * (indirect blocks) to the vnode to which they belong.
2012 reassignbuf(struct buf *bp)
2025 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2026 bp, bp->b_vp, bp->b_flags);
2028 * B_PAGING flagged buffers cannot be reassigned because their vp
2029 * is not fully linked in.
2031 if (bp->b_flags & B_PAGING)
2032 panic("cannot reassign paging buffer");
2035 * Delete from old vnode list, if on one.
2038 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2039 buf_vlist_remove(bp);
2041 panic("reassignbuf: Buffer %p not on queue.", bp);
2043 * If dirty, put on list of dirty buffers; otherwise insert onto list
2046 if (bp->b_flags & B_DELWRI) {
2047 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2048 switch (vp->v_type) {
2058 vn_syncer_add_to_worklist(bo, delay);
2060 buf_vlist_add(bp, bo, BX_VNDIRTY);
2062 buf_vlist_add(bp, bo, BX_VNCLEAN);
2064 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2065 mtx_lock(&sync_mtx);
2066 LIST_REMOVE(bo, bo_synclist);
2067 syncer_worklist_len--;
2068 mtx_unlock(&sync_mtx);
2069 bo->bo_flag &= ~BO_ONWORKLST;
2074 bp = TAILQ_FIRST(&bv->bv_hd);
2075 KASSERT(bp == NULL || bp->b_bufobj == bo,
2076 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2077 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2078 KASSERT(bp == NULL || bp->b_bufobj == bo,
2079 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2081 bp = TAILQ_FIRST(&bv->bv_hd);
2082 KASSERT(bp == NULL || bp->b_bufobj == bo,
2083 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2084 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2085 KASSERT(bp == NULL || bp->b_bufobj == bo,
2086 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2092 * Increment the use and hold counts on the vnode, taking care to reference
2093 * the driver's usecount if this is a chardev. The vholdl() will remove
2094 * the vnode from the free list if it is presently free. Requires the
2095 * vnode interlock and returns with it held.
2098 v_incr_usecount(struct vnode *vp)
2101 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2104 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2106 vp->v_rdev->si_usecount++;
2112 * Turn a holdcnt into a use+holdcnt such that only one call to
2113 * v_decr_usecount is needed.
2116 v_upgrade_usecount(struct vnode *vp)
2119 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2121 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2123 vp->v_rdev->si_usecount++;
2129 * Decrement the vnode use and hold count along with the driver's usecount
2130 * if this is a chardev. The vdropl() below releases the vnode interlock
2131 * as it may free the vnode.
2134 v_decr_usecount(struct vnode *vp)
2137 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2138 VNASSERT(vp->v_usecount > 0, vp,
2139 ("v_decr_usecount: negative usecount"));
2140 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2142 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2144 vp->v_rdev->si_usecount--;
2151 * Decrement only the use count and driver use count. This is intended to
2152 * be paired with a follow on vdropl() to release the remaining hold count.
2153 * In this way we may vgone() a vnode with a 0 usecount without risk of
2154 * having it end up on a free list because the hold count is kept above 0.
2157 v_decr_useonly(struct vnode *vp)
2160 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2161 VNASSERT(vp->v_usecount > 0, vp,
2162 ("v_decr_useonly: negative usecount"));
2163 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2165 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2167 vp->v_rdev->si_usecount--;
2173 * Grab a particular vnode from the free list, increment its
2174 * reference count and lock it. VI_DOOMED is set if the vnode
2175 * is being destroyed. Only callers who specify LK_RETRY will
2176 * see doomed vnodes. If inactive processing was delayed in
2177 * vput try to do it here.
2180 vget(struct vnode *vp, int flags, struct thread *td)
2185 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2186 ("vget: invalid lock operation"));
2187 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2189 if ((flags & LK_INTERLOCK) == 0)
2192 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2194 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2198 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2199 panic("vget: vn_lock failed to return ENOENT\n");
2201 /* Upgrade our holdcnt to a usecount. */
2202 v_upgrade_usecount(vp);
2204 * We don't guarantee that any particular close will
2205 * trigger inactive processing so just make a best effort
2206 * here at preventing a reference to a removed file. If
2207 * we don't succeed no harm is done.
2209 if (vp->v_iflag & VI_OWEINACT) {
2210 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2211 (flags & LK_NOWAIT) == 0)
2213 vp->v_iflag &= ~VI_OWEINACT;
2220 * Increase the reference count of a vnode.
2223 vref(struct vnode *vp)
2226 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2228 v_incr_usecount(vp);
2233 * Return reference count of a vnode.
2235 * The results of this call are only guaranteed when some mechanism other
2236 * than the VI lock is used to stop other processes from gaining references
2237 * to the vnode. This may be the case if the caller holds the only reference.
2238 * This is also useful when stale data is acceptable as race conditions may
2239 * be accounted for by some other means.
2242 vrefcnt(struct vnode *vp)
2247 usecnt = vp->v_usecount;
2253 #define VPUTX_VRELE 1
2254 #define VPUTX_VPUT 2
2255 #define VPUTX_VUNREF 3
2258 vputx(struct vnode *vp, int func)
2262 KASSERT(vp != NULL, ("vputx: null vp"));
2263 if (func == VPUTX_VUNREF)
2264 ASSERT_VOP_LOCKED(vp, "vunref");
2265 else if (func == VPUTX_VPUT)
2266 ASSERT_VOP_LOCKED(vp, "vput");
2268 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2269 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2272 /* Skip this v_writecount check if we're going to panic below. */
2273 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2274 ("vputx: missed vn_close"));
2277 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2278 vp->v_usecount == 1)) {
2279 if (func == VPUTX_VPUT)
2281 v_decr_usecount(vp);
2285 if (vp->v_usecount != 1) {
2286 vprint("vputx: negative ref count", vp);
2287 panic("vputx: negative ref cnt");
2289 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2291 * We want to hold the vnode until the inactive finishes to
2292 * prevent vgone() races. We drop the use count here and the
2293 * hold count below when we're done.
2297 * We must call VOP_INACTIVE with the node locked. Mark
2298 * as VI_DOINGINACT to avoid recursion.
2300 vp->v_iflag |= VI_OWEINACT;
2303 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2307 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2308 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2314 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2315 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2320 if (vp->v_usecount > 0)
2321 vp->v_iflag &= ~VI_OWEINACT;
2323 if (vp->v_iflag & VI_OWEINACT)
2324 vinactive(vp, curthread);
2325 if (func != VPUTX_VUNREF)
2332 * Vnode put/release.
2333 * If count drops to zero, call inactive routine and return to freelist.
2336 vrele(struct vnode *vp)
2339 vputx(vp, VPUTX_VRELE);
2343 * Release an already locked vnode. This give the same effects as
2344 * unlock+vrele(), but takes less time and avoids releasing and
2345 * re-aquiring the lock (as vrele() acquires the lock internally.)
2348 vput(struct vnode *vp)
2351 vputx(vp, VPUTX_VPUT);
2355 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2358 vunref(struct vnode *vp)
2361 vputx(vp, VPUTX_VUNREF);
2365 * Somebody doesn't want the vnode recycled.
2368 vhold(struct vnode *vp)
2377 * Increase the hold count and activate if this is the first reference.
2380 vholdl(struct vnode *vp)
2384 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2386 /* getnewvnode() calls v_incr_usecount() without holding interlock. */
2387 if (vp->v_type != VNON || vp->v_data != NULL)
2388 ASSERT_VI_LOCKED(vp, "vholdl");
2391 if ((vp->v_iflag & VI_FREE) == 0)
2393 VNASSERT(vp->v_holdcnt == 1, vp, ("vholdl: wrong hold count"));
2394 VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed."));
2396 * Remove a vnode from the free list, mark it as in use,
2397 * and put it on the active list.
2399 mtx_lock(&vnode_free_list_mtx);
2400 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2402 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2403 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2404 ("Activating already active vnode"));
2405 vp->v_iflag |= VI_ACTIVE;
2407 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2408 mp->mnt_activevnodelistsize++;
2409 mtx_unlock(&vnode_free_list_mtx);
2413 * Note that there is one less who cares about this vnode.
2414 * vdrop() is the opposite of vhold().
2417 vdrop(struct vnode *vp)
2425 * Drop the hold count of the vnode. If this is the last reference to
2426 * the vnode we place it on the free list unless it has been vgone'd
2427 * (marked VI_DOOMED) in which case we will free it.
2430 vdropl(struct vnode *vp)
2436 ASSERT_VI_LOCKED(vp, "vdropl");
2437 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2438 if (vp->v_holdcnt <= 0)
2439 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2441 if (vp->v_holdcnt > 0) {
2445 if ((vp->v_iflag & VI_DOOMED) == 0) {
2447 * Mark a vnode as free: remove it from its active list
2448 * and put it up for recycling on the freelist.
2450 VNASSERT(vp->v_op != NULL, vp,
2451 ("vdropl: vnode already reclaimed."));
2452 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2453 ("vnode already free"));
2454 VNASSERT(vp->v_holdcnt == 0, vp,
2455 ("vdropl: freeing when we shouldn't"));
2456 active = vp->v_iflag & VI_ACTIVE;
2457 if ((vp->v_iflag & VI_OWEINACT) == 0) {
2458 vp->v_iflag &= ~VI_ACTIVE;
2460 mtx_lock(&vnode_free_list_mtx);
2462 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2464 mp->mnt_activevnodelistsize--;
2466 if (vp->v_iflag & VI_AGE) {
2467 TAILQ_INSERT_HEAD(&vnode_free_list, vp,
2470 TAILQ_INSERT_TAIL(&vnode_free_list, vp,
2474 vp->v_iflag &= ~VI_AGE;
2475 vp->v_iflag |= VI_FREE;
2476 mtx_unlock(&vnode_free_list_mtx);
2478 atomic_add_long(&free_owe_inact, 1);
2484 * The vnode has been marked for destruction, so free it.
2486 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2487 atomic_subtract_long(&numvnodes, 1);
2489 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2490 ("cleaned vnode still on the free list."));
2491 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2492 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2493 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2494 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2495 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2496 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2497 VNASSERT(pctrie_is_empty(&bo->bo_clean.bv_root), vp,
2498 ("clean blk trie not empty"));
2499 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2500 VNASSERT(pctrie_is_empty(&bo->bo_dirty.bv_root), vp,
2501 ("dirty blk trie not empty"));
2502 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2503 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2504 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2507 mac_vnode_destroy(vp);
2509 if (vp->v_pollinfo != NULL)
2510 destroy_vpollinfo(vp->v_pollinfo);
2512 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2515 rangelock_destroy(&vp->v_rl);
2516 lockdestroy(vp->v_vnlock);
2517 mtx_destroy(&vp->v_interlock);
2518 rw_destroy(BO_LOCKPTR(bo));
2519 uma_zfree(vnode_zone, vp);
2523 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2524 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2525 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2526 * failed lock upgrade.
2529 vinactive(struct vnode *vp, struct thread *td)
2531 struct vm_object *obj;
2533 ASSERT_VOP_ELOCKED(vp, "vinactive");
2534 ASSERT_VI_LOCKED(vp, "vinactive");
2535 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2536 ("vinactive: recursed on VI_DOINGINACT"));
2537 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2538 vp->v_iflag |= VI_DOINGINACT;
2539 vp->v_iflag &= ~VI_OWEINACT;
2542 * Before moving off the active list, we must be sure that any
2543 * modified pages are on the vnode's dirty list since these will
2544 * no longer be checked once the vnode is on the inactive list.
2545 * Because the vnode vm object keeps a hold reference on the vnode
2546 * if there is at least one resident non-cached page, the vnode
2547 * cannot leave the active list without the page cleanup done.
2550 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
2551 VM_OBJECT_WLOCK(obj);
2552 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
2553 VM_OBJECT_WUNLOCK(obj);
2555 VOP_INACTIVE(vp, td);
2557 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2558 ("vinactive: lost VI_DOINGINACT"));
2559 vp->v_iflag &= ~VI_DOINGINACT;
2563 * Remove any vnodes in the vnode table belonging to mount point mp.
2565 * If FORCECLOSE is not specified, there should not be any active ones,
2566 * return error if any are found (nb: this is a user error, not a
2567 * system error). If FORCECLOSE is specified, detach any active vnodes
2570 * If WRITECLOSE is set, only flush out regular file vnodes open for
2573 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2575 * `rootrefs' specifies the base reference count for the root vnode
2576 * of this filesystem. The root vnode is considered busy if its
2577 * v_usecount exceeds this value. On a successful return, vflush(, td)
2578 * will call vrele() on the root vnode exactly rootrefs times.
2579 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2583 static int busyprt = 0; /* print out busy vnodes */
2584 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2588 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2590 struct vnode *vp, *mvp, *rootvp = NULL;
2592 int busy = 0, error;
2594 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2597 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2598 ("vflush: bad args"));
2600 * Get the filesystem root vnode. We can vput() it
2601 * immediately, since with rootrefs > 0, it won't go away.
2603 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2604 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2611 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2613 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2616 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2620 * Skip over a vnodes marked VV_SYSTEM.
2622 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2628 * If WRITECLOSE is set, flush out unlinked but still open
2629 * files (even if open only for reading) and regular file
2630 * vnodes open for writing.
2632 if (flags & WRITECLOSE) {
2633 if (vp->v_object != NULL) {
2634 VM_OBJECT_WLOCK(vp->v_object);
2635 vm_object_page_clean(vp->v_object, 0, 0, 0);
2636 VM_OBJECT_WUNLOCK(vp->v_object);
2638 error = VOP_FSYNC(vp, MNT_WAIT, td);
2642 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2645 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2648 if ((vp->v_type == VNON ||
2649 (error == 0 && vattr.va_nlink > 0)) &&
2650 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2658 * With v_usecount == 0, all we need to do is clear out the
2659 * vnode data structures and we are done.
2661 * If FORCECLOSE is set, forcibly close the vnode.
2663 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2664 VNASSERT(vp->v_usecount == 0 ||
2665 vp->v_op != &devfs_specops ||
2666 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2667 ("device VNODE %p is FORCECLOSED", vp));
2673 vprint("vflush: busy vnode", vp);
2679 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2681 * If just the root vnode is busy, and if its refcount
2682 * is equal to `rootrefs', then go ahead and kill it.
2685 KASSERT(busy > 0, ("vflush: not busy"));
2686 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2687 ("vflush: usecount %d < rootrefs %d",
2688 rootvp->v_usecount, rootrefs));
2689 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2690 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2692 VOP_UNLOCK(rootvp, 0);
2698 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2702 for (; rootrefs > 0; rootrefs--)
2708 * Recycle an unused vnode to the front of the free list.
2711 vrecycle(struct vnode *vp)
2715 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2716 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2719 if (vp->v_usecount == 0) {
2728 * Eliminate all activity associated with a vnode
2729 * in preparation for reuse.
2732 vgone(struct vnode *vp)
2740 notify_lowervp_vfs_dummy(struct mount *mp __unused,
2741 struct vnode *lowervp __unused)
2746 * Notify upper mounts about reclaimed or unlinked vnode.
2749 vfs_notify_upper(struct vnode *vp, int event)
2751 static struct vfsops vgonel_vfsops = {
2752 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
2753 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
2755 struct mount *mp, *ump, *mmp;
2762 if (TAILQ_EMPTY(&mp->mnt_uppers))
2765 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
2766 mmp->mnt_op = &vgonel_vfsops;
2767 mmp->mnt_kern_flag |= MNTK_MARKER;
2769 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
2770 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
2771 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
2772 ump = TAILQ_NEXT(ump, mnt_upper_link);
2775 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
2778 case VFS_NOTIFY_UPPER_RECLAIM:
2779 VFS_RECLAIM_LOWERVP(ump, vp);
2781 case VFS_NOTIFY_UPPER_UNLINK:
2782 VFS_UNLINK_LOWERVP(ump, vp);
2785 KASSERT(0, ("invalid event %d", event));
2789 ump = TAILQ_NEXT(mmp, mnt_upper_link);
2790 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
2793 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
2794 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
2795 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
2796 wakeup(&mp->mnt_uppers);
2803 * vgone, with the vp interlock held.
2806 vgonel(struct vnode *vp)
2813 ASSERT_VOP_ELOCKED(vp, "vgonel");
2814 ASSERT_VI_LOCKED(vp, "vgonel");
2815 VNASSERT(vp->v_holdcnt, vp,
2816 ("vgonel: vp %p has no reference.", vp));
2817 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2821 * Don't vgonel if we're already doomed.
2823 if (vp->v_iflag & VI_DOOMED)
2825 vp->v_iflag |= VI_DOOMED;
2828 * Check to see if the vnode is in use. If so, we have to call
2829 * VOP_CLOSE() and VOP_INACTIVE().
2831 active = vp->v_usecount;
2832 oweinact = (vp->v_iflag & VI_OWEINACT);
2834 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
2837 * If purging an active vnode, it must be closed and
2838 * deactivated before being reclaimed.
2841 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2842 if (oweinact || active) {
2844 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2848 if (vp->v_type == VSOCK)
2849 vfs_unp_reclaim(vp);
2852 * Clean out any buffers associated with the vnode.
2853 * If the flush fails, just toss the buffers.
2856 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2857 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2858 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0) {
2859 while (vinvalbuf(vp, 0, 0, 0) != 0)
2863 BO_LOCK(&vp->v_bufobj);
2864 KASSERT(TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd) &&
2865 vp->v_bufobj.bo_dirty.bv_cnt == 0 &&
2866 TAILQ_EMPTY(&vp->v_bufobj.bo_clean.bv_hd) &&
2867 vp->v_bufobj.bo_clean.bv_cnt == 0,
2868 ("vp %p bufobj not invalidated", vp));
2869 vp->v_bufobj.bo_flag |= BO_DEAD;
2870 BO_UNLOCK(&vp->v_bufobj);
2873 * Reclaim the vnode.
2875 if (VOP_RECLAIM(vp, td))
2876 panic("vgone: cannot reclaim");
2878 vn_finished_secondary_write(mp);
2879 VNASSERT(vp->v_object == NULL, vp,
2880 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2882 * Clear the advisory locks and wake up waiting threads.
2884 (void)VOP_ADVLOCKPURGE(vp);
2886 * Delete from old mount point vnode list.
2891 * Done with purge, reset to the standard lock and invalidate
2895 vp->v_vnlock = &vp->v_lock;
2896 vp->v_op = &dead_vnodeops;
2902 * Calculate the total number of references to a special device.
2905 vcount(struct vnode *vp)
2910 count = vp->v_rdev->si_usecount;
2916 * Same as above, but using the struct cdev *as argument
2919 count_dev(struct cdev *dev)
2924 count = dev->si_usecount;
2930 * Print out a description of a vnode.
2932 static char *typename[] =
2933 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2937 vn_printf(struct vnode *vp, const char *fmt, ...)
2940 char buf[256], buf2[16];
2946 printf("%p: ", (void *)vp);
2947 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2948 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2949 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2952 if (vp->v_vflag & VV_ROOT)
2953 strlcat(buf, "|VV_ROOT", sizeof(buf));
2954 if (vp->v_vflag & VV_ISTTY)
2955 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2956 if (vp->v_vflag & VV_NOSYNC)
2957 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2958 if (vp->v_vflag & VV_ETERNALDEV)
2959 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
2960 if (vp->v_vflag & VV_CACHEDLABEL)
2961 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2962 if (vp->v_vflag & VV_TEXT)
2963 strlcat(buf, "|VV_TEXT", sizeof(buf));
2964 if (vp->v_vflag & VV_COPYONWRITE)
2965 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2966 if (vp->v_vflag & VV_SYSTEM)
2967 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2968 if (vp->v_vflag & VV_PROCDEP)
2969 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2970 if (vp->v_vflag & VV_NOKNOTE)
2971 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2972 if (vp->v_vflag & VV_DELETED)
2973 strlcat(buf, "|VV_DELETED", sizeof(buf));
2974 if (vp->v_vflag & VV_MD)
2975 strlcat(buf, "|VV_MD", sizeof(buf));
2976 if (vp->v_vflag & VV_FORCEINSMQ)
2977 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
2978 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
2979 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2980 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
2982 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2983 strlcat(buf, buf2, sizeof(buf));
2985 if (vp->v_iflag & VI_MOUNT)
2986 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2987 if (vp->v_iflag & VI_AGE)
2988 strlcat(buf, "|VI_AGE", sizeof(buf));
2989 if (vp->v_iflag & VI_DOOMED)
2990 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2991 if (vp->v_iflag & VI_FREE)
2992 strlcat(buf, "|VI_FREE", sizeof(buf));
2993 if (vp->v_iflag & VI_ACTIVE)
2994 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
2995 if (vp->v_iflag & VI_DOINGINACT)
2996 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2997 if (vp->v_iflag & VI_OWEINACT)
2998 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2999 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
3000 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
3002 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3003 strlcat(buf, buf2, sizeof(buf));
3005 printf(" flags (%s)\n", buf + 1);
3006 if (mtx_owned(VI_MTX(vp)))
3007 printf(" VI_LOCKed");
3008 if (vp->v_object != NULL)
3009 printf(" v_object %p ref %d pages %d "
3010 "cleanbuf %d dirtybuf %d\n",
3011 vp->v_object, vp->v_object->ref_count,
3012 vp->v_object->resident_page_count,
3013 vp->v_bufobj.bo_dirty.bv_cnt,
3014 vp->v_bufobj.bo_clean.bv_cnt);
3016 lockmgr_printinfo(vp->v_vnlock);
3017 if (vp->v_data != NULL)
3023 * List all of the locked vnodes in the system.
3024 * Called when debugging the kernel.
3026 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3032 * Note: because this is DDB, we can't obey the locking semantics
3033 * for these structures, which means we could catch an inconsistent
3034 * state and dereference a nasty pointer. Not much to be done
3037 db_printf("Locked vnodes\n");
3038 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3039 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3040 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp))
3047 * Show details about the given vnode.
3049 DB_SHOW_COMMAND(vnode, db_show_vnode)
3055 vp = (struct vnode *)addr;
3056 vn_printf(vp, "vnode ");
3060 * Show details about the given mount point.
3062 DB_SHOW_COMMAND(mount, db_show_mount)
3073 /* No address given, print short info about all mount points. */
3074 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3075 db_printf("%p %s on %s (%s)\n", mp,
3076 mp->mnt_stat.f_mntfromname,
3077 mp->mnt_stat.f_mntonname,
3078 mp->mnt_stat.f_fstypename);
3082 db_printf("\nMore info: show mount <addr>\n");
3086 mp = (struct mount *)addr;
3087 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3088 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3091 mflags = mp->mnt_flag;
3092 #define MNT_FLAG(flag) do { \
3093 if (mflags & (flag)) { \
3094 if (buf[0] != '\0') \
3095 strlcat(buf, ", ", sizeof(buf)); \
3096 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3097 mflags &= ~(flag); \
3100 MNT_FLAG(MNT_RDONLY);
3101 MNT_FLAG(MNT_SYNCHRONOUS);
3102 MNT_FLAG(MNT_NOEXEC);
3103 MNT_FLAG(MNT_NOSUID);
3104 MNT_FLAG(MNT_NFS4ACLS);
3105 MNT_FLAG(MNT_UNION);
3106 MNT_FLAG(MNT_ASYNC);
3107 MNT_FLAG(MNT_SUIDDIR);
3108 MNT_FLAG(MNT_SOFTDEP);
3109 MNT_FLAG(MNT_NOSYMFOLLOW);
3110 MNT_FLAG(MNT_GJOURNAL);
3111 MNT_FLAG(MNT_MULTILABEL);
3113 MNT_FLAG(MNT_NOATIME);
3114 MNT_FLAG(MNT_NOCLUSTERR);
3115 MNT_FLAG(MNT_NOCLUSTERW);
3117 MNT_FLAG(MNT_EXRDONLY);
3118 MNT_FLAG(MNT_EXPORTED);
3119 MNT_FLAG(MNT_DEFEXPORTED);
3120 MNT_FLAG(MNT_EXPORTANON);
3121 MNT_FLAG(MNT_EXKERB);
3122 MNT_FLAG(MNT_EXPUBLIC);
3123 MNT_FLAG(MNT_LOCAL);
3124 MNT_FLAG(MNT_QUOTA);
3125 MNT_FLAG(MNT_ROOTFS);
3127 MNT_FLAG(MNT_IGNORE);
3128 MNT_FLAG(MNT_UPDATE);
3129 MNT_FLAG(MNT_DELEXPORT);
3130 MNT_FLAG(MNT_RELOAD);
3131 MNT_FLAG(MNT_FORCE);
3132 MNT_FLAG(MNT_SNAPSHOT);
3133 MNT_FLAG(MNT_BYFSID);
3137 strlcat(buf, ", ", sizeof(buf));
3138 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3139 "0x%016jx", mflags);
3141 db_printf(" mnt_flag = %s\n", buf);
3144 flags = mp->mnt_kern_flag;
3145 #define MNT_KERN_FLAG(flag) do { \
3146 if (flags & (flag)) { \
3147 if (buf[0] != '\0') \
3148 strlcat(buf, ", ", sizeof(buf)); \
3149 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3153 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3154 MNT_KERN_FLAG(MNTK_ASYNC);
3155 MNT_KERN_FLAG(MNTK_SOFTDEP);
3156 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3157 MNT_KERN_FLAG(MNTK_DRAINING);
3158 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3159 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3160 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3161 MNT_KERN_FLAG(MNTK_NO_IOPF);
3162 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3163 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3164 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3165 MNT_KERN_FLAG(MNTK_MARKER);
3166 MNT_KERN_FLAG(MNTK_USES_BCACHE);
3167 MNT_KERN_FLAG(MNTK_NOASYNC);
3168 MNT_KERN_FLAG(MNTK_UNMOUNT);
3169 MNT_KERN_FLAG(MNTK_MWAIT);
3170 MNT_KERN_FLAG(MNTK_SUSPEND);
3171 MNT_KERN_FLAG(MNTK_SUSPEND2);
3172 MNT_KERN_FLAG(MNTK_SUSPENDED);
3173 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3174 MNT_KERN_FLAG(MNTK_NOKNOTE);
3175 #undef MNT_KERN_FLAG
3178 strlcat(buf, ", ", sizeof(buf));
3179 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3182 db_printf(" mnt_kern_flag = %s\n", buf);
3184 db_printf(" mnt_opt = ");
3185 opt = TAILQ_FIRST(mp->mnt_opt);
3187 db_printf("%s", opt->name);
3188 opt = TAILQ_NEXT(opt, link);
3189 while (opt != NULL) {
3190 db_printf(", %s", opt->name);
3191 opt = TAILQ_NEXT(opt, link);
3197 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3198 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3199 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3200 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3201 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3202 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3203 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3204 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3205 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3206 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3207 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3208 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3210 db_printf(" mnt_cred = { uid=%u ruid=%u",
3211 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3212 if (jailed(mp->mnt_cred))
3213 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3215 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3216 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3217 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3218 db_printf(" mnt_activevnodelistsize = %d\n",
3219 mp->mnt_activevnodelistsize);
3220 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3221 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3222 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3223 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3224 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3225 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3226 db_printf(" mnt_secondary_accwrites = %d\n",
3227 mp->mnt_secondary_accwrites);
3228 db_printf(" mnt_gjprovider = %s\n",
3229 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3231 db_printf("\n\nList of active vnodes\n");
3232 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3233 if (vp->v_type != VMARKER) {
3234 vn_printf(vp, "vnode ");
3239 db_printf("\n\nList of inactive vnodes\n");
3240 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3241 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3242 vn_printf(vp, "vnode ");
3251 * Fill in a struct xvfsconf based on a struct vfsconf.
3254 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3256 struct xvfsconf xvfsp;
3258 bzero(&xvfsp, sizeof(xvfsp));
3259 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3260 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3261 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3262 xvfsp.vfc_flags = vfsp->vfc_flags;
3264 * These are unused in userland, we keep them
3265 * to not break binary compatibility.
3267 xvfsp.vfc_vfsops = NULL;
3268 xvfsp.vfc_next = NULL;
3269 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3272 #ifdef COMPAT_FREEBSD32
3274 uint32_t vfc_vfsops;
3275 char vfc_name[MFSNAMELEN];
3276 int32_t vfc_typenum;
3277 int32_t vfc_refcount;
3283 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3285 struct xvfsconf32 xvfsp;
3287 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3288 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3289 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3290 xvfsp.vfc_flags = vfsp->vfc_flags;
3291 xvfsp.vfc_vfsops = 0;
3293 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3298 * Top level filesystem related information gathering.
3301 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3303 struct vfsconf *vfsp;
3308 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3309 #ifdef COMPAT_FREEBSD32
3310 if (req->flags & SCTL_MASK32)
3311 error = vfsconf2x32(req, vfsp);
3314 error = vfsconf2x(req, vfsp);
3322 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD |
3323 CTLFLAG_MPSAFE, NULL, 0, sysctl_vfs_conflist,
3324 "S,xvfsconf", "List of all configured filesystems");
3326 #ifndef BURN_BRIDGES
3327 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3330 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3332 int *name = (int *)arg1 - 1; /* XXX */
3333 u_int namelen = arg2 + 1; /* XXX */
3334 struct vfsconf *vfsp;
3336 log(LOG_WARNING, "userland calling deprecated sysctl, "
3337 "please rebuild world\n");
3339 #if 1 || defined(COMPAT_PRELITE2)
3340 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3342 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3346 case VFS_MAXTYPENUM:
3349 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3352 return (ENOTDIR); /* overloaded */
3354 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3355 if (vfsp->vfc_typenum == name[2])
3360 return (EOPNOTSUPP);
3361 #ifdef COMPAT_FREEBSD32
3362 if (req->flags & SCTL_MASK32)
3363 return (vfsconf2x32(req, vfsp));
3366 return (vfsconf2x(req, vfsp));
3368 return (EOPNOTSUPP);
3371 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP |
3372 CTLFLAG_MPSAFE, vfs_sysctl,
3373 "Generic filesystem");
3375 #if 1 || defined(COMPAT_PRELITE2)
3378 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3381 struct vfsconf *vfsp;
3382 struct ovfsconf ovfs;
3385 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3386 bzero(&ovfs, sizeof(ovfs));
3387 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3388 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3389 ovfs.vfc_index = vfsp->vfc_typenum;
3390 ovfs.vfc_refcount = vfsp->vfc_refcount;
3391 ovfs.vfc_flags = vfsp->vfc_flags;
3392 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3402 #endif /* 1 || COMPAT_PRELITE2 */
3403 #endif /* !BURN_BRIDGES */
3405 #define KINFO_VNODESLOP 10
3408 * Dump vnode list (via sysctl).
3412 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3420 * Stale numvnodes access is not fatal here.
3423 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3425 /* Make an estimate */
3426 return (SYSCTL_OUT(req, 0, len));
3428 error = sysctl_wire_old_buffer(req, 0);
3431 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3433 mtx_lock(&mountlist_mtx);
3434 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3435 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3438 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3442 xvn[n].xv_size = sizeof *xvn;
3443 xvn[n].xv_vnode = vp;
3444 xvn[n].xv_id = 0; /* XXX compat */
3445 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3447 XV_COPY(writecount);
3453 xvn[n].xv_flag = vp->v_vflag;
3455 switch (vp->v_type) {
3462 if (vp->v_rdev == NULL) {
3466 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3469 xvn[n].xv_socket = vp->v_socket;
3472 xvn[n].xv_fifo = vp->v_fifoinfo;
3477 /* shouldn't happen? */
3485 mtx_lock(&mountlist_mtx);
3490 mtx_unlock(&mountlist_mtx);
3492 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3497 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE | CTLFLAG_RD |
3498 CTLFLAG_MPSAFE, 0, 0, sysctl_vnode, "S,xvnode",
3503 * Unmount all filesystems. The list is traversed in reverse order
3504 * of mounting to avoid dependencies.
3507 vfs_unmountall(void)
3513 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3517 * Since this only runs when rebooting, it is not interlocked.
3519 while(!TAILQ_EMPTY(&mountlist)) {
3520 mp = TAILQ_LAST(&mountlist, mntlist);
3522 error = dounmount(mp, MNT_FORCE, td);
3524 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3526 * XXX: Due to the way in which we mount the root
3527 * file system off of devfs, devfs will generate a
3528 * "busy" warning when we try to unmount it before
3529 * the root. Don't print a warning as a result in
3530 * order to avoid false positive errors that may
3531 * cause needless upset.
3533 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3534 printf("unmount of %s failed (",
3535 mp->mnt_stat.f_mntonname);
3539 printf("%d)\n", error);
3542 /* The unmount has removed mp from the mountlist */
3548 * perform msync on all vnodes under a mount point
3549 * the mount point must be locked.
3552 vfs_msync(struct mount *mp, int flags)
3554 struct vnode *vp, *mvp;
3555 struct vm_object *obj;
3557 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3558 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
3560 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3561 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3563 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3565 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3572 VM_OBJECT_WLOCK(obj);
3573 vm_object_page_clean(obj, 0, 0,
3575 OBJPC_SYNC : OBJPC_NOSYNC);
3576 VM_OBJECT_WUNLOCK(obj);
3586 destroy_vpollinfo_free(struct vpollinfo *vi)
3589 knlist_destroy(&vi->vpi_selinfo.si_note);
3590 mtx_destroy(&vi->vpi_lock);
3591 uma_zfree(vnodepoll_zone, vi);
3595 destroy_vpollinfo(struct vpollinfo *vi)
3598 knlist_clear(&vi->vpi_selinfo.si_note, 1);
3599 seldrain(&vi->vpi_selinfo);
3600 destroy_vpollinfo_free(vi);
3604 * Initalize per-vnode helper structure to hold poll-related state.
3607 v_addpollinfo(struct vnode *vp)
3609 struct vpollinfo *vi;
3611 if (vp->v_pollinfo != NULL)
3613 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3614 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3615 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3616 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3618 if (vp->v_pollinfo != NULL) {
3620 destroy_vpollinfo_free(vi);
3623 vp->v_pollinfo = vi;
3628 * Record a process's interest in events which might happen to
3629 * a vnode. Because poll uses the historic select-style interface
3630 * internally, this routine serves as both the ``check for any
3631 * pending events'' and the ``record my interest in future events''
3632 * functions. (These are done together, while the lock is held,
3633 * to avoid race conditions.)
3636 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3640 mtx_lock(&vp->v_pollinfo->vpi_lock);
3641 if (vp->v_pollinfo->vpi_revents & events) {
3643 * This leaves events we are not interested
3644 * in available for the other process which
3645 * which presumably had requested them
3646 * (otherwise they would never have been
3649 events &= vp->v_pollinfo->vpi_revents;
3650 vp->v_pollinfo->vpi_revents &= ~events;
3652 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3655 vp->v_pollinfo->vpi_events |= events;
3656 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3657 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3662 * Routine to create and manage a filesystem syncer vnode.
3664 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3665 static int sync_fsync(struct vop_fsync_args *);
3666 static int sync_inactive(struct vop_inactive_args *);
3667 static int sync_reclaim(struct vop_reclaim_args *);
3669 static struct vop_vector sync_vnodeops = {
3670 .vop_bypass = VOP_EOPNOTSUPP,
3671 .vop_close = sync_close, /* close */
3672 .vop_fsync = sync_fsync, /* fsync */
3673 .vop_inactive = sync_inactive, /* inactive */
3674 .vop_reclaim = sync_reclaim, /* reclaim */
3675 .vop_lock1 = vop_stdlock, /* lock */
3676 .vop_unlock = vop_stdunlock, /* unlock */
3677 .vop_islocked = vop_stdislocked, /* islocked */
3681 * Create a new filesystem syncer vnode for the specified mount point.
3684 vfs_allocate_syncvnode(struct mount *mp)
3688 static long start, incr, next;
3691 /* Allocate a new vnode */
3692 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3694 panic("vfs_allocate_syncvnode: getnewvnode() failed");
3696 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3697 vp->v_vflag |= VV_FORCEINSMQ;
3698 error = insmntque(vp, mp);
3700 panic("vfs_allocate_syncvnode: insmntque() failed");
3701 vp->v_vflag &= ~VV_FORCEINSMQ;
3704 * Place the vnode onto the syncer worklist. We attempt to
3705 * scatter them about on the list so that they will go off
3706 * at evenly distributed times even if all the filesystems
3707 * are mounted at once.
3710 if (next == 0 || next > syncer_maxdelay) {
3714 start = syncer_maxdelay / 2;
3715 incr = syncer_maxdelay;
3721 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3722 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3723 mtx_lock(&sync_mtx);
3725 if (mp->mnt_syncer == NULL) {
3726 mp->mnt_syncer = vp;
3729 mtx_unlock(&sync_mtx);
3732 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3739 vfs_deallocate_syncvnode(struct mount *mp)
3743 mtx_lock(&sync_mtx);
3744 vp = mp->mnt_syncer;
3746 mp->mnt_syncer = NULL;
3747 mtx_unlock(&sync_mtx);
3753 * Do a lazy sync of the filesystem.
3756 sync_fsync(struct vop_fsync_args *ap)
3758 struct vnode *syncvp = ap->a_vp;
3759 struct mount *mp = syncvp->v_mount;
3764 * We only need to do something if this is a lazy evaluation.
3766 if (ap->a_waitfor != MNT_LAZY)
3770 * Move ourselves to the back of the sync list.
3772 bo = &syncvp->v_bufobj;
3774 vn_syncer_add_to_worklist(bo, syncdelay);
3778 * Walk the list of vnodes pushing all that are dirty and
3779 * not already on the sync list.
3781 if (vfs_busy(mp, MBF_NOWAIT) != 0)
3783 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3787 save = curthread_pflags_set(TDP_SYNCIO);
3788 vfs_msync(mp, MNT_NOWAIT);
3789 error = VFS_SYNC(mp, MNT_LAZY);
3790 curthread_pflags_restore(save);
3791 vn_finished_write(mp);
3797 * The syncer vnode is no referenced.
3800 sync_inactive(struct vop_inactive_args *ap)
3808 * The syncer vnode is no longer needed and is being decommissioned.
3810 * Modifications to the worklist must be protected by sync_mtx.
3813 sync_reclaim(struct vop_reclaim_args *ap)
3815 struct vnode *vp = ap->a_vp;
3820 mtx_lock(&sync_mtx);
3821 if (vp->v_mount->mnt_syncer == vp)
3822 vp->v_mount->mnt_syncer = NULL;
3823 if (bo->bo_flag & BO_ONWORKLST) {
3824 LIST_REMOVE(bo, bo_synclist);
3825 syncer_worklist_len--;
3827 bo->bo_flag &= ~BO_ONWORKLST;
3829 mtx_unlock(&sync_mtx);
3836 * Check if vnode represents a disk device
3839 vn_isdisk(struct vnode *vp, int *errp)
3845 if (vp->v_type != VCHR)
3847 else if (vp->v_rdev == NULL)
3849 else if (vp->v_rdev->si_devsw == NULL)
3851 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3856 return (error == 0);
3860 * Common filesystem object access control check routine. Accepts a
3861 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3862 * and optional call-by-reference privused argument allowing vaccess()
3863 * to indicate to the caller whether privilege was used to satisfy the
3864 * request (obsoleted). Returns 0 on success, or an errno on failure.
3867 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3868 accmode_t accmode, struct ucred *cred, int *privused)
3870 accmode_t dac_granted;
3871 accmode_t priv_granted;
3873 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3874 ("invalid bit in accmode"));
3875 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3876 ("VAPPEND without VWRITE"));
3879 * Look for a normal, non-privileged way to access the file/directory
3880 * as requested. If it exists, go with that.
3883 if (privused != NULL)
3888 /* Check the owner. */
3889 if (cred->cr_uid == file_uid) {
3890 dac_granted |= VADMIN;
3891 if (file_mode & S_IXUSR)
3892 dac_granted |= VEXEC;
3893 if (file_mode & S_IRUSR)
3894 dac_granted |= VREAD;
3895 if (file_mode & S_IWUSR)
3896 dac_granted |= (VWRITE | VAPPEND);
3898 if ((accmode & dac_granted) == accmode)
3904 /* Otherwise, check the groups (first match) */
3905 if (groupmember(file_gid, cred)) {
3906 if (file_mode & S_IXGRP)
3907 dac_granted |= VEXEC;
3908 if (file_mode & S_IRGRP)
3909 dac_granted |= VREAD;
3910 if (file_mode & S_IWGRP)
3911 dac_granted |= (VWRITE | VAPPEND);
3913 if ((accmode & dac_granted) == accmode)
3919 /* Otherwise, check everyone else. */
3920 if (file_mode & S_IXOTH)
3921 dac_granted |= VEXEC;
3922 if (file_mode & S_IROTH)
3923 dac_granted |= VREAD;
3924 if (file_mode & S_IWOTH)
3925 dac_granted |= (VWRITE | VAPPEND);
3926 if ((accmode & dac_granted) == accmode)
3931 * Build a privilege mask to determine if the set of privileges
3932 * satisfies the requirements when combined with the granted mask
3933 * from above. For each privilege, if the privilege is required,
3934 * bitwise or the request type onto the priv_granted mask.
3940 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3941 * requests, instead of PRIV_VFS_EXEC.
3943 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3944 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3945 priv_granted |= VEXEC;
3948 * Ensure that at least one execute bit is on. Otherwise,
3949 * a privileged user will always succeed, and we don't want
3950 * this to happen unless the file really is executable.
3952 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3953 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
3954 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3955 priv_granted |= VEXEC;
3958 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3959 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3960 priv_granted |= VREAD;
3962 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3963 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3964 priv_granted |= (VWRITE | VAPPEND);
3966 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3967 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3968 priv_granted |= VADMIN;
3970 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3971 /* XXX audit: privilege used */
3972 if (privused != NULL)
3977 return ((accmode & VADMIN) ? EPERM : EACCES);
3981 * Credential check based on process requesting service, and per-attribute
3985 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3986 struct thread *td, accmode_t accmode)
3990 * Kernel-invoked always succeeds.
3996 * Do not allow privileged processes in jail to directly manipulate
3997 * system attributes.
3999 switch (attrnamespace) {
4000 case EXTATTR_NAMESPACE_SYSTEM:
4001 /* Potentially should be: return (EPERM); */
4002 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
4003 case EXTATTR_NAMESPACE_USER:
4004 return (VOP_ACCESS(vp, accmode, cred, td));
4010 #ifdef DEBUG_VFS_LOCKS
4012 * This only exists to supress warnings from unlocked specfs accesses. It is
4013 * no longer ok to have an unlocked VFS.
4015 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4016 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4018 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4019 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4020 "Drop into debugger on lock violation");
4022 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4023 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4024 0, "Check for interlock across VOPs");
4026 int vfs_badlock_print = 1; /* Print lock violations. */
4027 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4028 0, "Print lock violations");
4031 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4032 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4033 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4037 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4041 if (vfs_badlock_backtrace)
4044 if (vfs_badlock_print)
4045 printf("%s: %p %s\n", str, (void *)vp, msg);
4046 if (vfs_badlock_ddb)
4047 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4051 assert_vi_locked(struct vnode *vp, const char *str)
4054 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4055 vfs_badlock("interlock is not locked but should be", str, vp);
4059 assert_vi_unlocked(struct vnode *vp, const char *str)
4062 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4063 vfs_badlock("interlock is locked but should not be", str, vp);
4067 assert_vop_locked(struct vnode *vp, const char *str)
4071 if (!IGNORE_LOCK(vp)) {
4072 locked = VOP_ISLOCKED(vp);
4073 if (locked == 0 || locked == LK_EXCLOTHER)
4074 vfs_badlock("is not locked but should be", str, vp);
4079 assert_vop_unlocked(struct vnode *vp, const char *str)
4082 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4083 vfs_badlock("is locked but should not be", str, vp);
4087 assert_vop_elocked(struct vnode *vp, const char *str)
4090 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4091 vfs_badlock("is not exclusive locked but should be", str, vp);
4096 assert_vop_elocked_other(struct vnode *vp, const char *str)
4099 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
4100 vfs_badlock("is not exclusive locked by another thread",
4105 assert_vop_slocked(struct vnode *vp, const char *str)
4108 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
4109 vfs_badlock("is not locked shared but should be", str, vp);
4112 #endif /* DEBUG_VFS_LOCKS */
4115 vop_rename_fail(struct vop_rename_args *ap)
4118 if (ap->a_tvp != NULL)
4120 if (ap->a_tdvp == ap->a_tvp)
4129 vop_rename_pre(void *ap)
4131 struct vop_rename_args *a = ap;
4133 #ifdef DEBUG_VFS_LOCKS
4135 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4136 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4137 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4138 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4140 /* Check the source (from). */
4141 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4142 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4143 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4144 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4145 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4147 /* Check the target. */
4149 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4150 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4152 if (a->a_tdvp != a->a_fdvp)
4154 if (a->a_tvp != a->a_fvp)
4162 vop_strategy_pre(void *ap)
4164 #ifdef DEBUG_VFS_LOCKS
4165 struct vop_strategy_args *a;
4172 * Cluster ops lock their component buffers but not the IO container.
4174 if ((bp->b_flags & B_CLUSTER) != 0)
4177 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4178 if (vfs_badlock_print)
4180 "VOP_STRATEGY: bp is not locked but should be\n");
4181 if (vfs_badlock_ddb)
4182 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4188 vop_lock_pre(void *ap)
4190 #ifdef DEBUG_VFS_LOCKS
4191 struct vop_lock1_args *a = ap;
4193 if ((a->a_flags & LK_INTERLOCK) == 0)
4194 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4196 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4201 vop_lock_post(void *ap, int rc)
4203 #ifdef DEBUG_VFS_LOCKS
4204 struct vop_lock1_args *a = ap;
4206 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4207 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4208 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4213 vop_unlock_pre(void *ap)
4215 #ifdef DEBUG_VFS_LOCKS
4216 struct vop_unlock_args *a = ap;
4218 if (a->a_flags & LK_INTERLOCK)
4219 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4220 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4225 vop_unlock_post(void *ap, int rc)
4227 #ifdef DEBUG_VFS_LOCKS
4228 struct vop_unlock_args *a = ap;
4230 if (a->a_flags & LK_INTERLOCK)
4231 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4236 vop_create_post(void *ap, int rc)
4238 struct vop_create_args *a = ap;
4241 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4245 vop_deleteextattr_post(void *ap, int rc)
4247 struct vop_deleteextattr_args *a = ap;
4250 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4254 vop_link_post(void *ap, int rc)
4256 struct vop_link_args *a = ap;
4259 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4260 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4265 vop_mkdir_post(void *ap, int rc)
4267 struct vop_mkdir_args *a = ap;
4270 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4274 vop_mknod_post(void *ap, int rc)
4276 struct vop_mknod_args *a = ap;
4279 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4283 vop_remove_post(void *ap, int rc)
4285 struct vop_remove_args *a = ap;
4288 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4289 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4294 vop_rename_post(void *ap, int rc)
4296 struct vop_rename_args *a = ap;
4299 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4300 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4301 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4303 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4305 if (a->a_tdvp != a->a_fdvp)
4307 if (a->a_tvp != a->a_fvp)
4315 vop_rmdir_post(void *ap, int rc)
4317 struct vop_rmdir_args *a = ap;
4320 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4321 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4326 vop_setattr_post(void *ap, int rc)
4328 struct vop_setattr_args *a = ap;
4331 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4335 vop_setextattr_post(void *ap, int rc)
4337 struct vop_setextattr_args *a = ap;
4340 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4344 vop_symlink_post(void *ap, int rc)
4346 struct vop_symlink_args *a = ap;
4349 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4352 static struct knlist fs_knlist;
4355 vfs_event_init(void *arg)
4357 knlist_init_mtx(&fs_knlist, NULL);
4359 /* XXX - correct order? */
4360 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4363 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4366 KNOTE_UNLOCKED(&fs_knlist, event);
4369 static int filt_fsattach(struct knote *kn);
4370 static void filt_fsdetach(struct knote *kn);
4371 static int filt_fsevent(struct knote *kn, long hint);
4373 struct filterops fs_filtops = {
4375 .f_attach = filt_fsattach,
4376 .f_detach = filt_fsdetach,
4377 .f_event = filt_fsevent
4381 filt_fsattach(struct knote *kn)
4384 kn->kn_flags |= EV_CLEAR;
4385 knlist_add(&fs_knlist, kn, 0);
4390 filt_fsdetach(struct knote *kn)
4393 knlist_remove(&fs_knlist, kn, 0);
4397 filt_fsevent(struct knote *kn, long hint)
4400 kn->kn_fflags |= hint;
4401 return (kn->kn_fflags != 0);
4405 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4411 error = SYSCTL_IN(req, &vc, sizeof(vc));
4414 if (vc.vc_vers != VFS_CTL_VERS1)
4416 mp = vfs_getvfs(&vc.vc_fsid);
4419 /* ensure that a specific sysctl goes to the right filesystem. */
4420 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4421 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4425 VCTLTOREQ(&vc, req);
4426 error = VFS_SYSCTL(mp, vc.vc_op, req);
4431 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4432 NULL, 0, sysctl_vfs_ctl, "",
4436 * Function to initialize a va_filerev field sensibly.
4437 * XXX: Wouldn't a random number make a lot more sense ??
4440 init_va_filerev(void)
4445 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4448 static int filt_vfsread(struct knote *kn, long hint);
4449 static int filt_vfswrite(struct knote *kn, long hint);
4450 static int filt_vfsvnode(struct knote *kn, long hint);
4451 static void filt_vfsdetach(struct knote *kn);
4452 static struct filterops vfsread_filtops = {
4454 .f_detach = filt_vfsdetach,
4455 .f_event = filt_vfsread
4457 static struct filterops vfswrite_filtops = {
4459 .f_detach = filt_vfsdetach,
4460 .f_event = filt_vfswrite
4462 static struct filterops vfsvnode_filtops = {
4464 .f_detach = filt_vfsdetach,
4465 .f_event = filt_vfsvnode
4469 vfs_knllock(void *arg)
4471 struct vnode *vp = arg;
4473 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4477 vfs_knlunlock(void *arg)
4479 struct vnode *vp = arg;
4485 vfs_knl_assert_locked(void *arg)
4487 #ifdef DEBUG_VFS_LOCKS
4488 struct vnode *vp = arg;
4490 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4495 vfs_knl_assert_unlocked(void *arg)
4497 #ifdef DEBUG_VFS_LOCKS
4498 struct vnode *vp = arg;
4500 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4505 vfs_kqfilter(struct vop_kqfilter_args *ap)
4507 struct vnode *vp = ap->a_vp;
4508 struct knote *kn = ap->a_kn;
4511 switch (kn->kn_filter) {
4513 kn->kn_fop = &vfsread_filtops;
4516 kn->kn_fop = &vfswrite_filtops;
4519 kn->kn_fop = &vfsvnode_filtops;
4525 kn->kn_hook = (caddr_t)vp;
4528 if (vp->v_pollinfo == NULL)
4530 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4532 knlist_add(knl, kn, 0);
4538 * Detach knote from vnode
4541 filt_vfsdetach(struct knote *kn)
4543 struct vnode *vp = (struct vnode *)kn->kn_hook;
4545 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4546 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4552 filt_vfsread(struct knote *kn, long hint)
4554 struct vnode *vp = (struct vnode *)kn->kn_hook;
4559 * filesystem is gone, so set the EOF flag and schedule
4560 * the knote for deletion.
4562 if (hint == NOTE_REVOKE) {
4564 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4569 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4573 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4574 res = (kn->kn_data != 0);
4581 filt_vfswrite(struct knote *kn, long hint)
4583 struct vnode *vp = (struct vnode *)kn->kn_hook;
4588 * filesystem is gone, so set the EOF flag and schedule
4589 * the knote for deletion.
4591 if (hint == NOTE_REVOKE)
4592 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4600 filt_vfsvnode(struct knote *kn, long hint)
4602 struct vnode *vp = (struct vnode *)kn->kn_hook;
4606 if (kn->kn_sfflags & hint)
4607 kn->kn_fflags |= hint;
4608 if (hint == NOTE_REVOKE) {
4609 kn->kn_flags |= EV_EOF;
4613 res = (kn->kn_fflags != 0);
4619 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4623 if (dp->d_reclen > ap->a_uio->uio_resid)
4624 return (ENAMETOOLONG);
4625 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4627 if (ap->a_ncookies != NULL) {
4628 if (ap->a_cookies != NULL)
4629 free(ap->a_cookies, M_TEMP);
4630 ap->a_cookies = NULL;
4631 *ap->a_ncookies = 0;
4635 if (ap->a_ncookies == NULL)
4638 KASSERT(ap->a_cookies,
4639 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4641 *ap->a_cookies = realloc(*ap->a_cookies,
4642 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4643 (*ap->a_cookies)[*ap->a_ncookies] = off;
4648 * Mark for update the access time of the file if the filesystem
4649 * supports VOP_MARKATIME. This functionality is used by execve and
4650 * mmap, so we want to avoid the I/O implied by directly setting
4651 * va_atime for the sake of efficiency.
4654 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4659 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4660 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4661 (void)VOP_MARKATIME(vp);
4665 * The purpose of this routine is to remove granularity from accmode_t,
4666 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4667 * VADMIN and VAPPEND.
4669 * If it returns 0, the caller is supposed to continue with the usual
4670 * access checks using 'accmode' as modified by this routine. If it
4671 * returns nonzero value, the caller is supposed to return that value
4674 * Note that after this routine runs, accmode may be zero.
4677 vfs_unixify_accmode(accmode_t *accmode)
4680 * There is no way to specify explicit "deny" rule using
4681 * file mode or POSIX.1e ACLs.
4683 if (*accmode & VEXPLICIT_DENY) {
4689 * None of these can be translated into usual access bits.
4690 * Also, the common case for NFSv4 ACLs is to not contain
4691 * either of these bits. Caller should check for VWRITE
4692 * on the containing directory instead.
4694 if (*accmode & (VDELETE_CHILD | VDELETE))
4697 if (*accmode & VADMIN_PERMS) {
4698 *accmode &= ~VADMIN_PERMS;
4703 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4704 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4706 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
4712 * These are helper functions for filesystems to traverse all
4713 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
4715 * This interface replaces MNT_VNODE_FOREACH.
4718 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
4721 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
4726 kern_yield(PRI_USER);
4728 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4729 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
4730 while (vp != NULL && (vp->v_type == VMARKER ||
4731 (vp->v_iflag & VI_DOOMED) != 0))
4732 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4734 /* Check if we are done */
4736 __mnt_vnode_markerfree_all(mvp, mp);
4737 /* MNT_IUNLOCK(mp); -- done in above function */
4738 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4741 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4742 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4749 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
4753 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4756 (*mvp)->v_type = VMARKER;
4758 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
4759 while (vp != NULL && (vp->v_type == VMARKER ||
4760 (vp->v_iflag & VI_DOOMED) != 0))
4761 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4763 /* Check if we are done */
4767 free(*mvp, M_VNODE_MARKER);
4771 (*mvp)->v_mount = mp;
4772 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4780 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
4788 mtx_assert(MNT_MTX(mp), MA_OWNED);
4790 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4791 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4794 free(*mvp, M_VNODE_MARKER);
4799 * These are helper functions for filesystems to traverse their
4800 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
4803 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4806 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4811 free(*mvp, M_VNODE_MARKER);
4815 static struct vnode *
4816 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4818 struct vnode *vp, *nvp;
4820 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
4821 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4823 vp = TAILQ_NEXT(*mvp, v_actfreelist);
4824 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4825 while (vp != NULL) {
4826 if (vp->v_type == VMARKER) {
4827 vp = TAILQ_NEXT(vp, v_actfreelist);
4830 if (!VI_TRYLOCK(vp)) {
4831 if (mp_ncpus == 1 || should_yield()) {
4832 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4833 mtx_unlock(&vnode_free_list_mtx);
4835 mtx_lock(&vnode_free_list_mtx);
4840 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
4841 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
4842 ("alien vnode on the active list %p %p", vp, mp));
4843 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
4845 nvp = TAILQ_NEXT(vp, v_actfreelist);
4850 /* Check if we are done */
4852 mtx_unlock(&vnode_free_list_mtx);
4853 mnt_vnode_markerfree_active(mvp, mp);
4856 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4857 mtx_unlock(&vnode_free_list_mtx);
4858 ASSERT_VI_LOCKED(vp, "active iter");
4859 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
4864 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4868 kern_yield(PRI_USER);
4869 mtx_lock(&vnode_free_list_mtx);
4870 return (mnt_vnode_next_active(mvp, mp));
4874 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
4878 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4882 (*mvp)->v_type = VMARKER;
4883 (*mvp)->v_mount = mp;
4885 mtx_lock(&vnode_free_list_mtx);
4886 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
4888 mtx_unlock(&vnode_free_list_mtx);
4889 mnt_vnode_markerfree_active(mvp, mp);
4892 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4893 return (mnt_vnode_next_active(mvp, mp));
4897 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4903 mtx_lock(&vnode_free_list_mtx);
4904 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4905 mtx_unlock(&vnode_free_list_mtx);
4906 mnt_vnode_markerfree_active(mvp, mp);