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>
69 #include <sys/reboot.h>
70 #include <sys/sched.h>
71 #include <sys/sleepqueue.h>
74 #include <sys/sysctl.h>
75 #include <sys/syslog.h>
76 #include <sys/vmmeter.h>
77 #include <sys/vnode.h>
78 #include <sys/watchdog.h>
80 #include <machine/stdarg.h>
82 #include <security/mac/mac_framework.h>
85 #include <vm/vm_object.h>
86 #include <vm/vm_extern.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_page.h>
90 #include <vm/vm_kern.h>
100 static void delmntque(struct vnode *vp);
101 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
102 int slpflag, int slptimeo);
103 static void syncer_shutdown(void *arg, int howto);
104 static int vtryrecycle(struct vnode *vp);
105 static void v_incr_usecount(struct vnode *);
106 static void v_decr_usecount(struct vnode *);
107 static void v_decr_useonly(struct vnode *);
108 static void v_upgrade_usecount(struct vnode *);
109 static void vnlru_free(int);
110 static void vgonel(struct vnode *);
111 static void vfs_knllock(void *arg);
112 static void vfs_knlunlock(void *arg);
113 static void vfs_knl_assert_locked(void *arg);
114 static void vfs_knl_assert_unlocked(void *arg);
115 static void destroy_vpollinfo(struct vpollinfo *vi);
118 * Number of vnodes in existence. Increased whenever getnewvnode()
119 * allocates a new vnode, decreased in vdropl() for VI_DOOMED vnode.
121 static unsigned long numvnodes;
123 SYSCTL_ULONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
124 "Number of vnodes in existence");
127 * Conversion tables for conversion from vnode types to inode formats
130 enum vtype iftovt_tab[16] = {
131 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
132 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
134 int vttoif_tab[10] = {
135 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
136 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
140 * List of vnodes that are ready for recycling.
142 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
145 * Free vnode target. Free vnodes may simply be files which have been stat'd
146 * but not read. This is somewhat common, and a small cache of such files
147 * should be kept to avoid recreation costs.
149 static u_long wantfreevnodes;
150 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
151 /* Number of vnodes in the free list. */
152 static u_long freevnodes;
153 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
154 "Number of vnodes in the free list");
156 static int vlru_allow_cache_src;
157 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
158 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
161 * Various variables used for debugging the new implementation of
163 * XXX these are probably of (very) limited utility now.
165 static int reassignbufcalls;
166 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
167 "Number of calls to reassignbuf");
170 * Cache for the mount type id assigned to NFS. This is used for
171 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
173 int nfs_mount_type = -1;
175 /* To keep more than one thread at a time from running vfs_getnewfsid */
176 static struct mtx mntid_mtx;
179 * Lock for any access to the following:
184 static struct mtx vnode_free_list_mtx;
186 /* Publicly exported FS */
187 struct nfs_public nfs_pub;
189 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
190 static uma_zone_t vnode_zone;
191 static uma_zone_t vnodepoll_zone;
194 * The workitem queue.
196 * It is useful to delay writes of file data and filesystem metadata
197 * for tens of seconds so that quickly created and deleted files need
198 * not waste disk bandwidth being created and removed. To realize this,
199 * we append vnodes to a "workitem" queue. When running with a soft
200 * updates implementation, most pending metadata dependencies should
201 * not wait for more than a few seconds. Thus, mounted on block devices
202 * are delayed only about a half the time that file data is delayed.
203 * Similarly, directory updates are more critical, so are only delayed
204 * about a third the time that file data is delayed. Thus, there are
205 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
206 * one each second (driven off the filesystem syncer process). The
207 * syncer_delayno variable indicates the next queue that is to be processed.
208 * Items that need to be processed soon are placed in this queue:
210 * syncer_workitem_pending[syncer_delayno]
212 * A delay of fifteen seconds is done by placing the request fifteen
213 * entries later in the queue:
215 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
218 static int syncer_delayno;
219 static long syncer_mask;
220 LIST_HEAD(synclist, bufobj);
221 static struct synclist *syncer_workitem_pending[2];
223 * The sync_mtx protects:
228 * syncer_workitem_pending
229 * syncer_worklist_len
232 static struct mtx sync_mtx;
233 static struct cv sync_wakeup;
235 #define SYNCER_MAXDELAY 32
236 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
237 static int syncdelay = 30; /* max time to delay syncing data */
238 static int filedelay = 30; /* time to delay syncing files */
239 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
240 "Time to delay syncing files (in seconds)");
241 static int dirdelay = 29; /* time to delay syncing directories */
242 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
243 "Time to delay syncing directories (in seconds)");
244 static int metadelay = 28; /* time to delay syncing metadata */
245 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
246 "Time to delay syncing metadata (in seconds)");
247 static int rushjob; /* number of slots to run ASAP */
248 static int stat_rush_requests; /* number of times I/O speeded up */
249 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
250 "Number of times I/O speeded up (rush requests)");
253 * When shutting down the syncer, run it at four times normal speed.
255 #define SYNCER_SHUTDOWN_SPEEDUP 4
256 static int sync_vnode_count;
257 static int syncer_worklist_len;
258 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
262 * Number of vnodes we want to exist at any one time. This is mostly used
263 * to size hash tables in vnode-related code. It is normally not used in
264 * getnewvnode(), as wantfreevnodes is normally nonzero.)
266 * XXX desiredvnodes is historical cruft and should not exist.
269 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
270 &desiredvnodes, 0, "Maximum number of vnodes");
271 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
272 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
273 static int vnlru_nowhere;
274 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
275 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
278 * Macros to control when a vnode is freed and recycled. All require
279 * the vnode interlock.
281 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
282 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
283 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
285 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
289 * Initialize the vnode management data structures.
291 * Reevaluate the following cap on the number of vnodes after the physical
292 * memory size exceeds 512GB. In the limit, as the physical memory size
293 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
295 #ifndef MAXVNODES_MAX
296 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
299 vntblinit(void *dummy __unused)
302 int physvnodes, virtvnodes;
305 * Desiredvnodes is a function of the physical memory size and the
306 * kernel's heap size. Generally speaking, it scales with the
307 * physical memory size. The ratio of desiredvnodes to physical pages
308 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
309 * marginal ratio of desiredvnodes to physical pages is one to
310 * sixteen. However, desiredvnodes is limited by the kernel's heap
311 * size. The memory required by desiredvnodes vnodes and vm objects
312 * may not exceed one seventh of the kernel's heap size.
314 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
315 cnt.v_page_count) / 16;
316 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
317 sizeof(struct vnode)));
318 desiredvnodes = min(physvnodes, virtvnodes);
319 if (desiredvnodes > MAXVNODES_MAX) {
321 printf("Reducing kern.maxvnodes %d -> %d\n",
322 desiredvnodes, MAXVNODES_MAX);
323 desiredvnodes = MAXVNODES_MAX;
325 wantfreevnodes = desiredvnodes / 4;
326 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
327 TAILQ_INIT(&vnode_free_list);
328 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
329 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
330 NULL, NULL, UMA_ALIGN_PTR, 0);
331 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
332 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
334 * Initialize the filesystem syncer.
336 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
338 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
340 syncer_maxdelay = syncer_mask + 1;
341 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
342 cv_init(&sync_wakeup, "syncer");
343 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
347 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
351 * Mark a mount point as busy. Used to synchronize access and to delay
352 * unmounting. Eventually, mountlist_mtx is not released on failure.
354 * vfs_busy() is a custom lock, it can block the caller.
355 * vfs_busy() only sleeps if the unmount is active on the mount point.
356 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
357 * vnode belonging to mp.
359 * Lookup uses vfs_busy() to traverse mount points.
361 * / vnode lock A / vnode lock (/var) D
362 * /var vnode lock B /log vnode lock(/var/log) E
363 * vfs_busy lock C vfs_busy lock F
365 * Within each file system, the lock order is C->A->B and F->D->E.
367 * When traversing across mounts, the system follows that lock order:
373 * The lookup() process for namei("/var") illustrates the process:
374 * VOP_LOOKUP() obtains B while A is held
375 * vfs_busy() obtains a shared lock on F while A and B are held
376 * vput() releases lock on B
377 * vput() releases lock on A
378 * VFS_ROOT() obtains lock on D while shared lock on F is held
379 * vfs_unbusy() releases shared lock on F
380 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
381 * Attempt to lock A (instead of vp_crossmp) while D is held would
382 * violate the global order, causing deadlocks.
384 * dounmount() locks B while F is drained.
387 vfs_busy(struct mount *mp, int flags)
390 MPASS((flags & ~MBF_MASK) == 0);
391 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
396 * If mount point is currenly being unmounted, sleep until the
397 * mount point fate is decided. If thread doing the unmounting fails,
398 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
399 * that this mount point has survived the unmount attempt and vfs_busy
400 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
401 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
402 * about to be really destroyed. vfs_busy needs to release its
403 * reference on the mount point in this case and return with ENOENT,
404 * telling the caller that mount mount it tried to busy is no longer
407 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
408 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
411 CTR1(KTR_VFS, "%s: failed busying before sleeping",
415 if (flags & MBF_MNTLSTLOCK)
416 mtx_unlock(&mountlist_mtx);
417 mp->mnt_kern_flag |= MNTK_MWAIT;
418 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
419 if (flags & MBF_MNTLSTLOCK)
420 mtx_lock(&mountlist_mtx);
423 if (flags & MBF_MNTLSTLOCK)
424 mtx_unlock(&mountlist_mtx);
431 * Free a busy filesystem.
434 vfs_unbusy(struct mount *mp)
437 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
440 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
442 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
443 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
444 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
445 mp->mnt_kern_flag &= ~MNTK_DRAINING;
446 wakeup(&mp->mnt_lockref);
452 * Lookup a mount point by filesystem identifier.
455 vfs_getvfs(fsid_t *fsid)
459 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
460 mtx_lock(&mountlist_mtx);
461 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
462 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
463 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
465 mtx_unlock(&mountlist_mtx);
469 mtx_unlock(&mountlist_mtx);
470 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
471 return ((struct mount *) 0);
475 * Lookup a mount point by filesystem identifier, busying it before
479 vfs_busyfs(fsid_t *fsid)
484 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
485 mtx_lock(&mountlist_mtx);
486 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
487 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
488 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
489 error = vfs_busy(mp, MBF_MNTLSTLOCK);
491 mtx_unlock(&mountlist_mtx);
497 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
498 mtx_unlock(&mountlist_mtx);
499 return ((struct mount *) 0);
503 * Check if a user can access privileged mount options.
506 vfs_suser(struct mount *mp, struct thread *td)
511 * If the thread is jailed, but this is not a jail-friendly file
512 * system, deny immediately.
514 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
518 * If the file system was mounted outside the jail of the calling
519 * thread, deny immediately.
521 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
525 * If file system supports delegated administration, we don't check
526 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
527 * by the file system itself.
528 * If this is not the user that did original mount, we check for
529 * the PRIV_VFS_MOUNT_OWNER privilege.
531 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
532 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
533 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
540 * Get a new unique fsid. Try to make its val[0] unique, since this value
541 * will be used to create fake device numbers for stat(). Also try (but
542 * not so hard) make its val[0] unique mod 2^16, since some emulators only
543 * support 16-bit device numbers. We end up with unique val[0]'s for the
544 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
546 * Keep in mind that several mounts may be running in parallel. Starting
547 * the search one past where the previous search terminated is both a
548 * micro-optimization and a defense against returning the same fsid to
552 vfs_getnewfsid(struct mount *mp)
554 static uint16_t mntid_base;
559 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
560 mtx_lock(&mntid_mtx);
561 mtype = mp->mnt_vfc->vfc_typenum;
562 tfsid.val[1] = mtype;
563 mtype = (mtype & 0xFF) << 24;
565 tfsid.val[0] = makedev(255,
566 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
568 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
572 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
573 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
574 mtx_unlock(&mntid_mtx);
578 * Knob to control the precision of file timestamps:
580 * 0 = seconds only; nanoseconds zeroed.
581 * 1 = seconds and nanoseconds, accurate within 1/HZ.
582 * 2 = seconds and nanoseconds, truncated to microseconds.
583 * >=3 = seconds and nanoseconds, maximum precision.
585 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
587 static int timestamp_precision = TSP_SEC;
588 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
589 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
590 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
591 "3+: sec + ns (max. precision))");
594 * Get a current timestamp.
597 vfs_timestamp(struct timespec *tsp)
601 switch (timestamp_precision) {
603 tsp->tv_sec = time_second;
611 TIMEVAL_TO_TIMESPEC(&tv, tsp);
621 * Set vnode attributes to VNOVAL
624 vattr_null(struct vattr *vap)
628 vap->va_size = VNOVAL;
629 vap->va_bytes = VNOVAL;
630 vap->va_mode = VNOVAL;
631 vap->va_nlink = VNOVAL;
632 vap->va_uid = VNOVAL;
633 vap->va_gid = VNOVAL;
634 vap->va_fsid = VNOVAL;
635 vap->va_fileid = VNOVAL;
636 vap->va_blocksize = VNOVAL;
637 vap->va_rdev = VNOVAL;
638 vap->va_atime.tv_sec = VNOVAL;
639 vap->va_atime.tv_nsec = VNOVAL;
640 vap->va_mtime.tv_sec = VNOVAL;
641 vap->va_mtime.tv_nsec = VNOVAL;
642 vap->va_ctime.tv_sec = VNOVAL;
643 vap->va_ctime.tv_nsec = VNOVAL;
644 vap->va_birthtime.tv_sec = VNOVAL;
645 vap->va_birthtime.tv_nsec = VNOVAL;
646 vap->va_flags = VNOVAL;
647 vap->va_gen = VNOVAL;
652 * This routine is called when we have too many vnodes. It attempts
653 * to free <count> vnodes and will potentially free vnodes that still
654 * have VM backing store (VM backing store is typically the cause
655 * of a vnode blowout so we want to do this). Therefore, this operation
656 * is not considered cheap.
658 * A number of conditions may prevent a vnode from being reclaimed.
659 * the buffer cache may have references on the vnode, a directory
660 * vnode may still have references due to the namei cache representing
661 * underlying files, or the vnode may be in active use. It is not
662 * desireable to reuse such vnodes. These conditions may cause the
663 * number of vnodes to reach some minimum value regardless of what
664 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
667 vlrureclaim(struct mount *mp)
676 * Calculate the trigger point, don't allow user
677 * screwups to blow us up. This prevents us from
678 * recycling vnodes with lots of resident pages. We
679 * aren't trying to free memory, we are trying to
682 usevnodes = desiredvnodes;
685 trigger = cnt.v_page_count * 2 / usevnodes;
687 vn_start_write(NULL, &mp, V_WAIT);
689 count = mp->mnt_nvnodelistsize / 10 + 1;
691 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
692 while (vp != NULL && vp->v_type == VMARKER)
693 vp = TAILQ_NEXT(vp, v_nmntvnodes);
696 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
697 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
702 * If it's been deconstructed already, it's still
703 * referenced, or it exceeds the trigger, skip it.
705 if (vp->v_usecount ||
706 (!vlru_allow_cache_src &&
707 !LIST_EMPTY(&(vp)->v_cache_src)) ||
708 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
709 vp->v_object->resident_page_count > trigger)) {
715 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
717 goto next_iter_mntunlocked;
721 * v_usecount may have been bumped after VOP_LOCK() dropped
722 * the vnode interlock and before it was locked again.
724 * It is not necessary to recheck VI_DOOMED because it can
725 * only be set by another thread that holds both the vnode
726 * lock and vnode interlock. If another thread has the
727 * vnode lock before we get to VOP_LOCK() and obtains the
728 * vnode interlock after VOP_LOCK() drops the vnode
729 * interlock, the other thread will be unable to drop the
730 * vnode lock before our VOP_LOCK() call fails.
732 if (vp->v_usecount ||
733 (!vlru_allow_cache_src &&
734 !LIST_EMPTY(&(vp)->v_cache_src)) ||
735 (vp->v_object != NULL &&
736 vp->v_object->resident_page_count > trigger)) {
737 VOP_UNLOCK(vp, LK_INTERLOCK);
738 goto next_iter_mntunlocked;
740 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
741 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
746 next_iter_mntunlocked:
755 kern_yield(PRI_UNCHANGED);
760 vn_finished_write(mp);
765 * Attempt to keep the free list at wantfreevnodes length.
768 vnlru_free(int count)
773 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
774 for (; count > 0; count--) {
775 vp = TAILQ_FIRST(&vnode_free_list);
777 * The list can be modified while the free_list_mtx
778 * has been dropped and vp could be NULL here.
782 VNASSERT(vp->v_op != NULL, vp,
783 ("vnlru_free: vnode already reclaimed."));
784 KASSERT((vp->v_iflag & VI_FREE) != 0,
785 ("Removing vnode not on freelist"));
786 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
787 ("Mangling active vnode"));
788 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
790 * Don't recycle if we can't get the interlock.
792 if (!VI_TRYLOCK(vp)) {
793 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
796 VNASSERT(VCANRECYCLE(vp), vp,
797 ("vp inconsistent on freelist"));
799 vp->v_iflag &= ~VI_FREE;
801 mtx_unlock(&vnode_free_list_mtx);
803 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
805 VFS_UNLOCK_GIANT(vfslocked);
807 * If the recycled succeeded this vdrop will actually free
808 * the vnode. If not it will simply place it back on
812 mtx_lock(&vnode_free_list_mtx);
816 * Attempt to recycle vnodes in a context that is always safe to block.
817 * Calling vlrurecycle() from the bowels of filesystem code has some
818 * interesting deadlock problems.
820 static struct proc *vnlruproc;
821 static int vnlruproc_sig;
826 struct mount *mp, *nmp;
828 struct proc *p = vnlruproc;
830 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
834 kproc_suspend_check(p);
835 mtx_lock(&vnode_free_list_mtx);
836 if (freevnodes > wantfreevnodes)
837 vnlru_free(freevnodes - wantfreevnodes);
838 if (numvnodes <= desiredvnodes * 9 / 10) {
840 wakeup(&vnlruproc_sig);
841 msleep(vnlruproc, &vnode_free_list_mtx,
842 PVFS|PDROP, "vlruwt", hz);
845 mtx_unlock(&vnode_free_list_mtx);
847 mtx_lock(&mountlist_mtx);
848 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
849 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
850 nmp = TAILQ_NEXT(mp, mnt_list);
853 vfslocked = VFS_LOCK_GIANT(mp);
854 done += vlrureclaim(mp);
855 VFS_UNLOCK_GIANT(vfslocked);
856 mtx_lock(&mountlist_mtx);
857 nmp = TAILQ_NEXT(mp, mnt_list);
860 mtx_unlock(&mountlist_mtx);
863 /* These messages are temporary debugging aids */
864 if (vnlru_nowhere < 5)
865 printf("vnlru process getting nowhere..\n");
866 else if (vnlru_nowhere == 5)
867 printf("vnlru process messages stopped.\n");
870 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
872 kern_yield(PRI_UNCHANGED);
876 static struct kproc_desc vnlru_kp = {
881 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
885 * Routines having to do with the management of the vnode table.
889 * Try to recycle a freed vnode. We abort if anyone picks up a reference
890 * before we actually vgone(). This function must be called with the vnode
891 * held to prevent the vnode from being returned to the free list midway
895 vtryrecycle(struct vnode *vp)
899 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
900 VNASSERT(vp->v_holdcnt, vp,
901 ("vtryrecycle: Recycling vp %p without a reference.", vp));
903 * This vnode may found and locked via some other list, if so we
904 * can't recycle it yet.
906 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
908 "%s: impossible to recycle, vp %p lock is already held",
910 return (EWOULDBLOCK);
913 * Don't recycle if its filesystem is being suspended.
915 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
918 "%s: impossible to recycle, cannot start the write for %p",
923 * If we got this far, we need to acquire the interlock and see if
924 * anyone picked up this vnode from another list. If not, we will
925 * mark it with DOOMED via vgonel() so that anyone who does find it
929 if (vp->v_usecount) {
930 VOP_UNLOCK(vp, LK_INTERLOCK);
931 vn_finished_write(vnmp);
933 "%s: impossible to recycle, %p is already referenced",
937 if ((vp->v_iflag & VI_DOOMED) == 0)
939 VOP_UNLOCK(vp, LK_INTERLOCK);
940 vn_finished_write(vnmp);
945 * Wait for available vnodes.
948 getnewvnode_wait(int suspended)
951 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
952 if (numvnodes > desiredvnodes) {
955 * File system is beeing suspended, we cannot risk a
956 * deadlock here, so allocate new vnode anyway.
958 if (freevnodes > wantfreevnodes)
959 vnlru_free(freevnodes - wantfreevnodes);
962 if (vnlruproc_sig == 0) {
963 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
966 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
969 return (numvnodes > desiredvnodes ? ENFILE : 0);
973 getnewvnode_reserve(u_int count)
978 mtx_lock(&vnode_free_list_mtx);
980 if (getnewvnode_wait(0) == 0) {
986 mtx_unlock(&vnode_free_list_mtx);
990 getnewvnode_drop_reserve(void)
995 mtx_lock(&vnode_free_list_mtx);
996 KASSERT(numvnodes >= td->td_vp_reserv, ("reserve too large"));
997 numvnodes -= td->td_vp_reserv;
998 mtx_unlock(&vnode_free_list_mtx);
999 td->td_vp_reserv = 0;
1003 * Return the next vnode from the free list.
1006 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1014 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1017 if (td->td_vp_reserv > 0) {
1018 td->td_vp_reserv -= 1;
1021 mtx_lock(&vnode_free_list_mtx);
1023 * Lend our context to reclaim vnodes if they've exceeded the max.
1025 if (freevnodes > wantfreevnodes)
1027 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1029 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1031 mtx_unlock(&vnode_free_list_mtx);
1036 mtx_unlock(&vnode_free_list_mtx);
1038 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1042 vp->v_vnlock = &vp->v_lock;
1043 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1045 * By default, don't allow shared locks unless filesystems
1048 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
1050 * Initialize bufobj.
1053 bo->__bo_vnode = vp;
1054 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
1055 bo->bo_ops = &buf_ops_bio;
1056 bo->bo_private = vp;
1057 TAILQ_INIT(&bo->bo_clean.bv_hd);
1058 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1060 * Initialize namecache.
1062 LIST_INIT(&vp->v_cache_src);
1063 TAILQ_INIT(&vp->v_cache_dst);
1065 * Finalize various vnode identity bits.
1070 v_incr_usecount(vp);
1074 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1075 mac_vnode_associate_singlelabel(mp, vp);
1076 else if (mp == NULL && vops != &dead_vnodeops)
1077 printf("NULL mp in getnewvnode()\n");
1080 bo->bo_bsize = mp->mnt_stat.f_iosize;
1081 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1082 vp->v_vflag |= VV_NOKNOTE;
1084 rangelock_init(&vp->v_rl);
1087 * For the filesystems which do not use vfs_hash_insert(),
1088 * still initialize v_hash to have vfs_hash_index() useful.
1089 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1092 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1099 * Delete from old mount point vnode list, if on one.
1102 delmntque(struct vnode *vp)
1112 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1113 ("Active vnode list size %d > Vnode list size %d",
1114 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1115 active = vp->v_iflag & VI_ACTIVE;
1116 vp->v_iflag &= ~VI_ACTIVE;
1118 mtx_lock(&vnode_free_list_mtx);
1119 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1120 mp->mnt_activevnodelistsize--;
1121 mtx_unlock(&vnode_free_list_mtx);
1125 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1126 ("bad mount point vnode list size"));
1127 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1128 mp->mnt_nvnodelistsize--;
1134 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1138 vp->v_op = &dead_vnodeops;
1139 /* XXX non mp-safe fs may still call insmntque with vnode
1141 if (!VOP_ISLOCKED(vp))
1142 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1148 * Insert into list of vnodes for the new mount point, if available.
1151 insmntque1(struct vnode *vp, struct mount *mp,
1152 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1156 KASSERT(vp->v_mount == NULL,
1157 ("insmntque: vnode already on per mount vnode list"));
1158 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1159 #ifdef DEBUG_VFS_LOCKS
1160 if (!VFS_NEEDSGIANT(mp))
1161 ASSERT_VOP_ELOCKED(vp,
1162 "insmntque: mp-safe fs and non-locked vp");
1165 * We acquire the vnode interlock early to ensure that the
1166 * vnode cannot be recycled by another process releasing a
1167 * holdcnt on it before we get it on both the vnode list
1168 * and the active vnode list. The mount mutex protects only
1169 * manipulation of the vnode list and the vnode freelist
1170 * mutex protects only manipulation of the active vnode list.
1171 * Hence the need to hold the vnode interlock throughout.
1175 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1176 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1177 mp->mnt_nvnodelistsize == 0)) {
1178 locked = VOP_ISLOCKED(vp);
1179 if (!locked || (locked == LK_EXCLUSIVE &&
1180 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1190 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1191 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1192 ("neg mount point vnode list size"));
1193 mp->mnt_nvnodelistsize++;
1194 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1195 ("Activating already active vnode"));
1196 vp->v_iflag |= VI_ACTIVE;
1197 mtx_lock(&vnode_free_list_mtx);
1198 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1199 mp->mnt_activevnodelistsize++;
1200 mtx_unlock(&vnode_free_list_mtx);
1207 insmntque(struct vnode *vp, struct mount *mp)
1210 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1214 * Flush out and invalidate all buffers associated with a bufobj
1215 * Called with the underlying object locked.
1218 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1223 if (flags & V_SAVE) {
1224 error = bufobj_wwait(bo, slpflag, slptimeo);
1229 if (bo->bo_dirty.bv_cnt > 0) {
1231 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1234 * XXX We could save a lock/unlock if this was only
1235 * enabled under INVARIANTS
1238 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1239 panic("vinvalbuf: dirty bufs");
1243 * If you alter this loop please notice that interlock is dropped and
1244 * reacquired in flushbuflist. Special care is needed to ensure that
1245 * no race conditions occur from this.
1248 error = flushbuflist(&bo->bo_clean,
1249 flags, bo, slpflag, slptimeo);
1250 if (error == 0 && !(flags & V_CLEANONLY))
1251 error = flushbuflist(&bo->bo_dirty,
1252 flags, bo, slpflag, slptimeo);
1253 if (error != 0 && error != EAGAIN) {
1257 } while (error != 0);
1260 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1261 * have write I/O in-progress but if there is a VM object then the
1262 * VM object can also have read-I/O in-progress.
1265 bufobj_wwait(bo, 0, 0);
1267 if (bo->bo_object != NULL) {
1268 VM_OBJECT_LOCK(bo->bo_object);
1269 vm_object_pip_wait(bo->bo_object, "bovlbx");
1270 VM_OBJECT_UNLOCK(bo->bo_object);
1273 } while (bo->bo_numoutput > 0);
1277 * Destroy the copy in the VM cache, too.
1279 if (bo->bo_object != NULL &&
1280 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1281 VM_OBJECT_LOCK(bo->bo_object);
1282 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1283 OBJPR_CLEANONLY : 0);
1284 VM_OBJECT_UNLOCK(bo->bo_object);
1289 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1290 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1291 panic("vinvalbuf: flush failed");
1298 * Flush out and invalidate all buffers associated with a vnode.
1299 * Called with the underlying object locked.
1302 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1305 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1306 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1307 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1311 * Flush out buffers on the specified list.
1315 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1318 struct buf *bp, *nbp;
1323 ASSERT_BO_LOCKED(bo);
1326 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1327 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1328 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1334 lblkno = nbp->b_lblkno;
1335 xflags = nbp->b_xflags &
1336 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1339 error = BUF_TIMELOCK(bp,
1340 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1341 "flushbuf", slpflag, slptimeo);
1344 return (error != ENOLCK ? error : EAGAIN);
1346 KASSERT(bp->b_bufobj == bo,
1347 ("bp %p wrong b_bufobj %p should be %p",
1348 bp, bp->b_bufobj, bo));
1349 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1355 * XXX Since there are no node locks for NFS, I
1356 * believe there is a slight chance that a delayed
1357 * write will occur while sleeping just above, so
1360 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1365 bp->b_flags |= B_ASYNC;
1368 return (EAGAIN); /* XXX: why not loop ? */
1373 bp->b_flags |= (B_INVAL | B_RELBUF);
1374 bp->b_flags &= ~B_ASYNC;
1378 (nbp->b_bufobj != bo ||
1379 nbp->b_lblkno != lblkno ||
1381 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1382 break; /* nbp invalid */
1388 * Truncate a file's buffer and pages to a specified length. This
1389 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1393 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1394 off_t length, int blksize)
1396 struct buf *bp, *nbp;
1401 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1402 vp, cred, blksize, (uintmax_t)length);
1405 * Round up to the *next* lbn.
1407 trunclbn = (length + blksize - 1) / blksize;
1409 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1416 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1417 if (bp->b_lblkno < trunclbn)
1420 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1421 BO_MTX(bo)) == ENOLCK)
1427 bp->b_flags |= (B_INVAL | B_RELBUF);
1428 bp->b_flags &= ~B_ASYNC;
1434 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1435 (nbp->b_vp != vp) ||
1436 (nbp->b_flags & B_DELWRI))) {
1442 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1443 if (bp->b_lblkno < trunclbn)
1446 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1447 BO_MTX(bo)) == ENOLCK)
1452 bp->b_flags |= (B_INVAL | B_RELBUF);
1453 bp->b_flags &= ~B_ASYNC;
1459 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1460 (nbp->b_vp != vp) ||
1461 (nbp->b_flags & B_DELWRI) == 0)) {
1470 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1471 if (bp->b_lblkno > 0)
1474 * Since we hold the vnode lock this should only
1475 * fail if we're racing with the buf daemon.
1478 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1479 BO_MTX(bo)) == ENOLCK) {
1482 VNASSERT((bp->b_flags & B_DELWRI), vp,
1483 ("buf(%p) on dirty queue without DELWRI", bp));
1494 bufobj_wwait(bo, 0, 0);
1496 vnode_pager_setsize(vp, length);
1502 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1505 * NOTE: We have to deal with the special case of a background bitmap
1506 * buffer, a situation where two buffers will have the same logical
1507 * block offset. We want (1) only the foreground buffer to be accessed
1508 * in a lookup and (2) must differentiate between the foreground and
1509 * background buffer in the splay tree algorithm because the splay
1510 * tree cannot normally handle multiple entities with the same 'index'.
1511 * We accomplish this by adding differentiating flags to the splay tree's
1516 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1519 struct buf *lefttreemax, *righttreemin, *y;
1523 lefttreemax = righttreemin = &dummy;
1525 if (lblkno < root->b_lblkno ||
1526 (lblkno == root->b_lblkno &&
1527 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1528 if ((y = root->b_left) == NULL)
1530 if (lblkno < y->b_lblkno) {
1532 root->b_left = y->b_right;
1535 if ((y = root->b_left) == NULL)
1538 /* Link into the new root's right tree. */
1539 righttreemin->b_left = root;
1540 righttreemin = root;
1541 } else if (lblkno > root->b_lblkno ||
1542 (lblkno == root->b_lblkno &&
1543 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1544 if ((y = root->b_right) == NULL)
1546 if (lblkno > y->b_lblkno) {
1548 root->b_right = y->b_left;
1551 if ((y = root->b_right) == NULL)
1554 /* Link into the new root's left tree. */
1555 lefttreemax->b_right = root;
1562 /* Assemble the new root. */
1563 lefttreemax->b_right = root->b_left;
1564 righttreemin->b_left = root->b_right;
1565 root->b_left = dummy.b_right;
1566 root->b_right = dummy.b_left;
1571 buf_vlist_remove(struct buf *bp)
1576 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1577 ASSERT_BO_LOCKED(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 if (bp != bv->bv_root) {
1586 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1587 KASSERT(root == bp, ("splay lookup failed in remove"));
1589 if (bp->b_left == NULL) {
1592 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1593 root->b_right = bp->b_right;
1596 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1598 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1602 * Add the buffer to the sorted clean or dirty block list using a
1603 * splay tree algorithm.
1605 * NOTE: xflags is passed as a constant, optimizing this inline function!
1608 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1613 ASSERT_BO_LOCKED(bo);
1614 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1615 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1616 bp->b_xflags |= xflags;
1617 if (xflags & BX_VNDIRTY)
1622 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1626 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1627 } else if (bp->b_lblkno < root->b_lblkno ||
1628 (bp->b_lblkno == root->b_lblkno &&
1629 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1630 bp->b_left = root->b_left;
1632 root->b_left = NULL;
1633 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1635 bp->b_right = root->b_right;
1637 root->b_right = NULL;
1638 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1645 * Lookup a buffer using the splay tree. Note that we specifically avoid
1646 * shadow buffers used in background bitmap writes.
1648 * This code isn't quite efficient as it could be because we are maintaining
1649 * two sorted lists and do not know which list the block resides in.
1651 * During a "make buildworld" the desired buffer is found at one of
1652 * the roots more than 60% of the time. Thus, checking both roots
1653 * before performing either splay eliminates unnecessary splays on the
1654 * first tree splayed.
1657 gbincore(struct bufobj *bo, daddr_t lblkno)
1661 ASSERT_BO_LOCKED(bo);
1662 if ((bp = bo->bo_clean.bv_root) != NULL &&
1663 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1665 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1666 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1668 if ((bp = bo->bo_clean.bv_root) != NULL) {
1669 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1670 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1673 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1674 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1675 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1682 * Associate a buffer with a vnode.
1685 bgetvp(struct vnode *vp, struct buf *bp)
1690 ASSERT_BO_LOCKED(bo);
1691 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1693 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1694 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1695 ("bgetvp: bp already attached! %p", bp));
1698 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1699 bp->b_flags |= B_NEEDSGIANT;
1703 * Insert onto list for new vnode.
1705 buf_vlist_add(bp, bo, BX_VNCLEAN);
1709 * Disassociate a buffer from a vnode.
1712 brelvp(struct buf *bp)
1717 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1718 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1721 * Delete from old vnode list, if on one.
1723 vp = bp->b_vp; /* XXX */
1726 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1727 buf_vlist_remove(bp);
1729 panic("brelvp: Buffer %p not on queue.", bp);
1730 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1731 bo->bo_flag &= ~BO_ONWORKLST;
1732 mtx_lock(&sync_mtx);
1733 LIST_REMOVE(bo, bo_synclist);
1734 syncer_worklist_len--;
1735 mtx_unlock(&sync_mtx);
1737 bp->b_flags &= ~B_NEEDSGIANT;
1739 bp->b_bufobj = NULL;
1745 * Add an item to the syncer work queue.
1748 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1752 ASSERT_BO_LOCKED(bo);
1754 mtx_lock(&sync_mtx);
1755 if (bo->bo_flag & BO_ONWORKLST)
1756 LIST_REMOVE(bo, bo_synclist);
1758 bo->bo_flag |= BO_ONWORKLST;
1759 syncer_worklist_len++;
1762 if (delay > syncer_maxdelay - 2)
1763 delay = syncer_maxdelay - 2;
1764 slot = (syncer_delayno + delay) & syncer_mask;
1766 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1768 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1770 mtx_unlock(&sync_mtx);
1774 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1778 mtx_lock(&sync_mtx);
1779 len = syncer_worklist_len - sync_vnode_count;
1780 mtx_unlock(&sync_mtx);
1781 error = SYSCTL_OUT(req, &len, sizeof(len));
1785 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1786 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1788 static struct proc *updateproc;
1789 static void sched_sync(void);
1790 static struct kproc_desc up_kp = {
1795 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1798 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1803 *bo = LIST_FIRST(slp);
1806 vp = (*bo)->__bo_vnode; /* XXX */
1807 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1810 * We use vhold in case the vnode does not
1811 * successfully sync. vhold prevents the vnode from
1812 * going away when we unlock the sync_mtx so that
1813 * we can acquire the vnode interlock.
1816 mtx_unlock(&sync_mtx);
1818 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1820 mtx_lock(&sync_mtx);
1821 return (*bo == LIST_FIRST(slp));
1823 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1824 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1826 vn_finished_write(mp);
1828 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1830 * Put us back on the worklist. The worklist
1831 * routine will remove us from our current
1832 * position and then add us back in at a later
1835 vn_syncer_add_to_worklist(*bo, syncdelay);
1839 mtx_lock(&sync_mtx);
1844 * System filesystem synchronizer daemon.
1849 struct synclist *gnext, *next;
1850 struct synclist *gslp, *slp;
1853 struct thread *td = curthread;
1855 int net_worklist_len;
1856 int syncer_final_iter;
1861 syncer_final_iter = 0;
1863 syncer_state = SYNCER_RUNNING;
1864 starttime = time_uptime;
1865 td->td_pflags |= TDP_NORUNNINGBUF;
1867 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1870 mtx_lock(&sync_mtx);
1872 if (syncer_state == SYNCER_FINAL_DELAY &&
1873 syncer_final_iter == 0) {
1874 mtx_unlock(&sync_mtx);
1875 kproc_suspend_check(td->td_proc);
1876 mtx_lock(&sync_mtx);
1878 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1879 if (syncer_state != SYNCER_RUNNING &&
1880 starttime != time_uptime) {
1882 printf("\nSyncing disks, vnodes remaining...");
1885 printf("%d ", net_worklist_len);
1887 starttime = time_uptime;
1890 * Push files whose dirty time has expired. Be careful
1891 * of interrupt race on slp queue.
1893 * Skip over empty worklist slots when shutting down.
1896 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1897 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1898 syncer_delayno += 1;
1899 if (syncer_delayno == syncer_maxdelay)
1901 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1902 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1904 * If the worklist has wrapped since the
1905 * it was emptied of all but syncer vnodes,
1906 * switch to the FINAL_DELAY state and run
1907 * for one more second.
1909 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1910 net_worklist_len == 0 &&
1911 last_work_seen == syncer_delayno) {
1912 syncer_state = SYNCER_FINAL_DELAY;
1913 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1915 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1916 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1919 * Keep track of the last time there was anything
1920 * on the worklist other than syncer vnodes.
1921 * Return to the SHUTTING_DOWN state if any
1924 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1925 last_work_seen = syncer_delayno;
1926 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1927 syncer_state = SYNCER_SHUTTING_DOWN;
1928 while (!LIST_EMPTY(slp)) {
1929 error = sync_vnode(slp, &bo, td);
1931 LIST_REMOVE(bo, bo_synclist);
1932 LIST_INSERT_HEAD(next, bo, bo_synclist);
1936 if (first_printf == 0)
1937 wdog_kern_pat(WD_LASTVAL);
1940 if (!LIST_EMPTY(gslp)) {
1941 mtx_unlock(&sync_mtx);
1943 mtx_lock(&sync_mtx);
1944 while (!LIST_EMPTY(gslp)) {
1945 error = sync_vnode(gslp, &bo, td);
1947 LIST_REMOVE(bo, bo_synclist);
1948 LIST_INSERT_HEAD(gnext, bo,
1955 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1956 syncer_final_iter--;
1958 * The variable rushjob allows the kernel to speed up the
1959 * processing of the filesystem syncer process. A rushjob
1960 * value of N tells the filesystem syncer to process the next
1961 * N seconds worth of work on its queue ASAP. Currently rushjob
1962 * is used by the soft update code to speed up the filesystem
1963 * syncer process when the incore state is getting so far
1964 * ahead of the disk that the kernel memory pool is being
1965 * threatened with exhaustion.
1972 * Just sleep for a short period of time between
1973 * iterations when shutting down to allow some I/O
1976 * If it has taken us less than a second to process the
1977 * current work, then wait. Otherwise start right over
1978 * again. We can still lose time if any single round
1979 * takes more than two seconds, but it does not really
1980 * matter as we are just trying to generally pace the
1981 * filesystem activity.
1983 if (syncer_state != SYNCER_RUNNING ||
1984 time_uptime == starttime) {
1986 sched_prio(td, PPAUSE);
1989 if (syncer_state != SYNCER_RUNNING)
1990 cv_timedwait(&sync_wakeup, &sync_mtx,
1991 hz / SYNCER_SHUTDOWN_SPEEDUP);
1992 else if (time_uptime == starttime)
1993 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1998 * Request the syncer daemon to speed up its work.
1999 * We never push it to speed up more than half of its
2000 * normal turn time, otherwise it could take over the cpu.
2003 speedup_syncer(void)
2007 mtx_lock(&sync_mtx);
2008 if (rushjob < syncdelay / 2) {
2010 stat_rush_requests += 1;
2013 mtx_unlock(&sync_mtx);
2014 cv_broadcast(&sync_wakeup);
2019 * Tell the syncer to speed up its work and run though its work
2020 * list several times, then tell it to shut down.
2023 syncer_shutdown(void *arg, int howto)
2026 if (howto & RB_NOSYNC)
2028 mtx_lock(&sync_mtx);
2029 syncer_state = SYNCER_SHUTTING_DOWN;
2031 mtx_unlock(&sync_mtx);
2032 cv_broadcast(&sync_wakeup);
2033 kproc_shutdown(arg, howto);
2037 * Reassign a buffer from one vnode to another.
2038 * Used to assign file specific control information
2039 * (indirect blocks) to the vnode to which they belong.
2042 reassignbuf(struct buf *bp)
2055 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2056 bp, bp->b_vp, bp->b_flags);
2058 * B_PAGING flagged buffers cannot be reassigned because their vp
2059 * is not fully linked in.
2061 if (bp->b_flags & B_PAGING)
2062 panic("cannot reassign paging buffer");
2065 * Delete from old vnode list, if on one.
2068 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2069 buf_vlist_remove(bp);
2071 panic("reassignbuf: Buffer %p not on queue.", bp);
2073 * If dirty, put on list of dirty buffers; otherwise insert onto list
2076 if (bp->b_flags & B_DELWRI) {
2077 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2078 switch (vp->v_type) {
2088 vn_syncer_add_to_worklist(bo, delay);
2090 buf_vlist_add(bp, bo, BX_VNDIRTY);
2092 buf_vlist_add(bp, bo, BX_VNCLEAN);
2094 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2095 mtx_lock(&sync_mtx);
2096 LIST_REMOVE(bo, bo_synclist);
2097 syncer_worklist_len--;
2098 mtx_unlock(&sync_mtx);
2099 bo->bo_flag &= ~BO_ONWORKLST;
2104 bp = TAILQ_FIRST(&bv->bv_hd);
2105 KASSERT(bp == NULL || bp->b_bufobj == bo,
2106 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2107 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2108 KASSERT(bp == NULL || bp->b_bufobj == bo,
2109 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2111 bp = TAILQ_FIRST(&bv->bv_hd);
2112 KASSERT(bp == NULL || bp->b_bufobj == bo,
2113 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2114 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2115 KASSERT(bp == NULL || bp->b_bufobj == bo,
2116 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2122 * Increment the use and hold counts on the vnode, taking care to reference
2123 * the driver's usecount if this is a chardev. The vholdl() will remove
2124 * the vnode from the free list if it is presently free. Requires the
2125 * vnode interlock and returns with it held.
2128 v_incr_usecount(struct vnode *vp)
2131 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2133 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2135 vp->v_rdev->si_usecount++;
2142 * Turn a holdcnt into a use+holdcnt such that only one call to
2143 * v_decr_usecount is needed.
2146 v_upgrade_usecount(struct vnode *vp)
2149 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2151 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2153 vp->v_rdev->si_usecount++;
2159 * Decrement the vnode use and hold count along with the driver's usecount
2160 * if this is a chardev. The vdropl() below releases the vnode interlock
2161 * as it may free the vnode.
2164 v_decr_usecount(struct vnode *vp)
2167 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2168 VNASSERT(vp->v_usecount > 0, vp,
2169 ("v_decr_usecount: negative usecount"));
2170 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2172 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2174 vp->v_rdev->si_usecount--;
2181 * Decrement only the use count and driver use count. This is intended to
2182 * be paired with a follow on vdropl() to release the remaining hold count.
2183 * In this way we may vgone() a vnode with a 0 usecount without risk of
2184 * having it end up on a free list because the hold count is kept above 0.
2187 v_decr_useonly(struct vnode *vp)
2190 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2191 VNASSERT(vp->v_usecount > 0, vp,
2192 ("v_decr_useonly: negative usecount"));
2193 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2195 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2197 vp->v_rdev->si_usecount--;
2203 * Grab a particular vnode from the free list, increment its
2204 * reference count and lock it. VI_DOOMED is set if the vnode
2205 * is being destroyed. Only callers who specify LK_RETRY will
2206 * see doomed vnodes. If inactive processing was delayed in
2207 * vput try to do it here.
2210 vget(struct vnode *vp, int flags, struct thread *td)
2215 VFS_ASSERT_GIANT(vp->v_mount);
2216 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2217 ("vget: invalid lock operation"));
2218 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2220 if ((flags & LK_INTERLOCK) == 0)
2223 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2225 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2229 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2230 panic("vget: vn_lock failed to return ENOENT\n");
2232 /* Upgrade our holdcnt to a usecount. */
2233 v_upgrade_usecount(vp);
2235 * We don't guarantee that any particular close will
2236 * trigger inactive processing so just make a best effort
2237 * here at preventing a reference to a removed file. If
2238 * we don't succeed no harm is done.
2240 if (vp->v_iflag & VI_OWEINACT) {
2241 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2242 (flags & LK_NOWAIT) == 0)
2244 vp->v_iflag &= ~VI_OWEINACT;
2251 * Increase the reference count of a vnode.
2254 vref(struct vnode *vp)
2257 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2259 v_incr_usecount(vp);
2264 * Return reference count of a vnode.
2266 * The results of this call are only guaranteed when some mechanism other
2267 * than the VI lock is used to stop other processes from gaining references
2268 * to the vnode. This may be the case if the caller holds the only reference.
2269 * This is also useful when stale data is acceptable as race conditions may
2270 * be accounted for by some other means.
2273 vrefcnt(struct vnode *vp)
2278 usecnt = vp->v_usecount;
2284 #define VPUTX_VRELE 1
2285 #define VPUTX_VPUT 2
2286 #define VPUTX_VUNREF 3
2289 vputx(struct vnode *vp, int func)
2293 KASSERT(vp != NULL, ("vputx: null vp"));
2294 if (func == VPUTX_VUNREF)
2295 ASSERT_VOP_LOCKED(vp, "vunref");
2296 else if (func == VPUTX_VPUT)
2297 ASSERT_VOP_LOCKED(vp, "vput");
2299 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2300 VFS_ASSERT_GIANT(vp->v_mount);
2301 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2304 /* Skip this v_writecount check if we're going to panic below. */
2305 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2306 ("vputx: missed vn_close"));
2309 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2310 vp->v_usecount == 1)) {
2311 if (func == VPUTX_VPUT)
2313 v_decr_usecount(vp);
2317 if (vp->v_usecount != 1) {
2318 vprint("vputx: negative ref count", vp);
2319 panic("vputx: negative ref cnt");
2321 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2323 * We want to hold the vnode until the inactive finishes to
2324 * prevent vgone() races. We drop the use count here and the
2325 * hold count below when we're done.
2329 * We must call VOP_INACTIVE with the node locked. Mark
2330 * as VI_DOINGINACT to avoid recursion.
2332 vp->v_iflag |= VI_OWEINACT;
2335 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2339 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2340 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2346 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
2350 if (vp->v_usecount > 0)
2351 vp->v_iflag &= ~VI_OWEINACT;
2353 if (vp->v_iflag & VI_OWEINACT)
2354 vinactive(vp, curthread);
2355 if (func != VPUTX_VUNREF)
2362 * Vnode put/release.
2363 * If count drops to zero, call inactive routine and return to freelist.
2366 vrele(struct vnode *vp)
2369 vputx(vp, VPUTX_VRELE);
2373 * Release an already locked vnode. This give the same effects as
2374 * unlock+vrele(), but takes less time and avoids releasing and
2375 * re-aquiring the lock (as vrele() acquires the lock internally.)
2378 vput(struct vnode *vp)
2381 vputx(vp, VPUTX_VPUT);
2385 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2388 vunref(struct vnode *vp)
2391 vputx(vp, VPUTX_VUNREF);
2395 * Somebody doesn't want the vnode recycled.
2398 vhold(struct vnode *vp)
2407 * Increase the hold count and activate if this is the first reference.
2410 vholdl(struct vnode *vp)
2414 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2416 if (!VSHOULDBUSY(vp))
2418 ASSERT_VI_LOCKED(vp, "vholdl");
2419 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
2420 VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed."));
2422 * Remove a vnode from the free list, mark it as in use,
2423 * and put it on the active list.
2425 mtx_lock(&vnode_free_list_mtx);
2426 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2428 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2429 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2430 ("Activating already active vnode"));
2431 vp->v_iflag |= VI_ACTIVE;
2433 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2434 mp->mnt_activevnodelistsize++;
2435 mtx_unlock(&vnode_free_list_mtx);
2439 * Note that there is one less who cares about this vnode.
2440 * vdrop() is the opposite of vhold().
2443 vdrop(struct vnode *vp)
2451 * Drop the hold count of the vnode. If this is the last reference to
2452 * the vnode we place it on the free list unless it has been vgone'd
2453 * (marked VI_DOOMED) in which case we will free it.
2456 vdropl(struct vnode *vp)
2462 ASSERT_VI_LOCKED(vp, "vdropl");
2463 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2464 if (vp->v_holdcnt <= 0)
2465 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2467 if (vp->v_holdcnt > 0) {
2471 if ((vp->v_iflag & VI_DOOMED) == 0) {
2473 * Mark a vnode as free: remove it from its active list
2474 * and put it up for recycling on the freelist.
2476 VNASSERT(vp->v_op != NULL, vp,
2477 ("vdropl: vnode already reclaimed."));
2478 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2479 ("vnode already free"));
2480 VNASSERT(VSHOULDFREE(vp), vp,
2481 ("vdropl: freeing when we shouldn't"));
2482 active = vp->v_iflag & VI_ACTIVE;
2483 vp->v_iflag &= ~VI_ACTIVE;
2485 mtx_lock(&vnode_free_list_mtx);
2487 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2489 mp->mnt_activevnodelistsize--;
2491 if (vp->v_iflag & VI_AGE) {
2492 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_actfreelist);
2494 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
2497 vp->v_iflag &= ~VI_AGE;
2498 vp->v_iflag |= VI_FREE;
2499 mtx_unlock(&vnode_free_list_mtx);
2504 * The vnode has been marked for destruction, so free it.
2506 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2507 mtx_lock(&vnode_free_list_mtx);
2509 mtx_unlock(&vnode_free_list_mtx);
2511 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2512 ("cleaned vnode still on the free list."));
2513 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2514 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2515 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2516 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2517 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2518 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2519 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
2520 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2521 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
2522 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2523 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2524 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2527 mac_vnode_destroy(vp);
2529 if (vp->v_pollinfo != NULL)
2530 destroy_vpollinfo(vp->v_pollinfo);
2532 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2535 rangelock_destroy(&vp->v_rl);
2536 lockdestroy(vp->v_vnlock);
2537 mtx_destroy(&vp->v_interlock);
2538 mtx_destroy(BO_MTX(bo));
2539 uma_zfree(vnode_zone, vp);
2543 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2544 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2545 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2546 * failed lock upgrade.
2549 vinactive(struct vnode *vp, struct thread *td)
2551 struct vm_object *obj;
2553 ASSERT_VOP_ELOCKED(vp, "vinactive");
2554 ASSERT_VI_LOCKED(vp, "vinactive");
2555 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2556 ("vinactive: recursed on VI_DOINGINACT"));
2557 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2558 vp->v_iflag |= VI_DOINGINACT;
2559 vp->v_iflag &= ~VI_OWEINACT;
2562 * Before moving off the active list, we must be sure that any
2563 * modified pages are on the vnode's dirty list since these will
2564 * no longer be checked once the vnode is on the inactive list.
2565 * Because the vnode vm object keeps a hold reference on the vnode
2566 * if there is at least one resident non-cached page, the vnode
2567 * cannot leave the active list without the page cleanup done.
2570 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
2571 VM_OBJECT_LOCK(obj);
2572 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
2573 VM_OBJECT_UNLOCK(obj);
2575 VOP_INACTIVE(vp, td);
2577 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2578 ("vinactive: lost VI_DOINGINACT"));
2579 vp->v_iflag &= ~VI_DOINGINACT;
2583 * Remove any vnodes in the vnode table belonging to mount point mp.
2585 * If FORCECLOSE is not specified, there should not be any active ones,
2586 * return error if any are found (nb: this is a user error, not a
2587 * system error). If FORCECLOSE is specified, detach any active vnodes
2590 * If WRITECLOSE is set, only flush out regular file vnodes open for
2593 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2595 * `rootrefs' specifies the base reference count for the root vnode
2596 * of this filesystem. The root vnode is considered busy if its
2597 * v_usecount exceeds this value. On a successful return, vflush(, td)
2598 * will call vrele() on the root vnode exactly rootrefs times.
2599 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2603 static int busyprt = 0; /* print out busy vnodes */
2604 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2608 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2610 struct vnode *vp, *mvp, *rootvp = NULL;
2612 int busy = 0, error;
2614 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2617 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2618 ("vflush: bad args"));
2620 * Get the filesystem root vnode. We can vput() it
2621 * immediately, since with rootrefs > 0, it won't go away.
2623 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2624 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2631 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2633 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2636 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2640 * Skip over a vnodes marked VV_SYSTEM.
2642 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2648 * If WRITECLOSE is set, flush out unlinked but still open
2649 * files (even if open only for reading) and regular file
2650 * vnodes open for writing.
2652 if (flags & WRITECLOSE) {
2653 if (vp->v_object != NULL) {
2654 VM_OBJECT_LOCK(vp->v_object);
2655 vm_object_page_clean(vp->v_object, 0, 0, 0);
2656 VM_OBJECT_UNLOCK(vp->v_object);
2658 error = VOP_FSYNC(vp, MNT_WAIT, td);
2662 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2665 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2668 if ((vp->v_type == VNON ||
2669 (error == 0 && vattr.va_nlink > 0)) &&
2670 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2678 * With v_usecount == 0, all we need to do is clear out the
2679 * vnode data structures and we are done.
2681 * If FORCECLOSE is set, forcibly close the vnode.
2683 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2684 VNASSERT(vp->v_usecount == 0 ||
2685 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2686 ("device VNODE %p is FORCECLOSED", vp));
2692 vprint("vflush: busy vnode", vp);
2698 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2700 * If just the root vnode is busy, and if its refcount
2701 * is equal to `rootrefs', then go ahead and kill it.
2704 KASSERT(busy > 0, ("vflush: not busy"));
2705 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2706 ("vflush: usecount %d < rootrefs %d",
2707 rootvp->v_usecount, rootrefs));
2708 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2709 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2711 VOP_UNLOCK(rootvp, 0);
2717 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2721 for (; rootrefs > 0; rootrefs--)
2727 * Recycle an unused vnode to the front of the free list.
2730 vrecycle(struct vnode *vp, struct thread *td)
2734 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2735 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2738 if (vp->v_usecount == 0) {
2747 * Eliminate all activity associated with a vnode
2748 * in preparation for reuse.
2751 vgone(struct vnode *vp)
2759 notify_lowervp_vfs_dummy(struct mount *mp __unused,
2760 struct vnode *lowervp __unused)
2765 * Notify upper mounts about reclaimed or unlinked vnode.
2768 vfs_notify_upper(struct vnode *vp, int event)
2770 static struct vfsops vgonel_vfsops = {
2771 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
2772 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
2774 struct mount *mp, *ump, *mmp;
2781 if (TAILQ_EMPTY(&mp->mnt_uppers))
2784 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
2785 mmp->mnt_op = &vgonel_vfsops;
2786 mmp->mnt_kern_flag |= MNTK_MARKER;
2788 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
2789 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
2790 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
2791 ump = TAILQ_NEXT(ump, mnt_upper_link);
2794 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
2797 case VFS_NOTIFY_UPPER_RECLAIM:
2798 VFS_RECLAIM_LOWERVP(ump, vp);
2800 case VFS_NOTIFY_UPPER_UNLINK:
2801 VFS_UNLINK_LOWERVP(ump, vp);
2804 KASSERT(0, ("invalid event %d", event));
2808 ump = TAILQ_NEXT(mmp, mnt_upper_link);
2809 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
2812 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
2813 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
2814 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
2815 wakeup(&mp->mnt_uppers);
2822 * vgone, with the vp interlock held.
2825 vgonel(struct vnode *vp)
2832 ASSERT_VOP_ELOCKED(vp, "vgonel");
2833 ASSERT_VI_LOCKED(vp, "vgonel");
2834 VNASSERT(vp->v_holdcnt, vp,
2835 ("vgonel: vp %p has no reference.", vp));
2836 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2840 * Don't vgonel if we're already doomed.
2842 if (vp->v_iflag & VI_DOOMED)
2844 vp->v_iflag |= VI_DOOMED;
2847 * Check to see if the vnode is in use. If so, we have to call
2848 * VOP_CLOSE() and VOP_INACTIVE().
2850 active = vp->v_usecount;
2851 oweinact = (vp->v_iflag & VI_OWEINACT);
2853 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
2856 * Clean out any buffers associated with the vnode.
2857 * If the flush fails, just toss the buffers.
2860 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2861 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2862 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2863 vinvalbuf(vp, 0, 0, 0);
2866 * If purging an active vnode, it must be closed and
2867 * deactivated before being reclaimed.
2870 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2871 if (oweinact || active) {
2873 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2877 if (vp->v_type == VSOCK)
2878 vfs_unp_reclaim(vp);
2880 * Reclaim the vnode.
2882 if (VOP_RECLAIM(vp, td))
2883 panic("vgone: cannot reclaim");
2885 vn_finished_secondary_write(mp);
2886 VNASSERT(vp->v_object == NULL, vp,
2887 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2889 * Clear the advisory locks and wake up waiting threads.
2891 (void)VOP_ADVLOCKPURGE(vp);
2893 * Delete from old mount point vnode list.
2898 * Done with purge, reset to the standard lock and invalidate
2902 vp->v_vnlock = &vp->v_lock;
2903 vp->v_op = &dead_vnodeops;
2909 * Calculate the total number of references to a special device.
2912 vcount(struct vnode *vp)
2917 count = vp->v_rdev->si_usecount;
2923 * Same as above, but using the struct cdev *as argument
2926 count_dev(struct cdev *dev)
2931 count = dev->si_usecount;
2937 * Print out a description of a vnode.
2939 static char *typename[] =
2940 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2944 vn_printf(struct vnode *vp, const char *fmt, ...)
2947 char buf[256], buf2[16];
2953 printf("%p: ", (void *)vp);
2954 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2955 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2956 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2959 if (vp->v_vflag & VV_ROOT)
2960 strlcat(buf, "|VV_ROOT", sizeof(buf));
2961 if (vp->v_vflag & VV_ISTTY)
2962 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2963 if (vp->v_vflag & VV_NOSYNC)
2964 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2965 if (vp->v_vflag & VV_ETERNALDEV)
2966 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
2967 if (vp->v_vflag & VV_CACHEDLABEL)
2968 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2969 if (vp->v_vflag & VV_TEXT)
2970 strlcat(buf, "|VV_TEXT", sizeof(buf));
2971 if (vp->v_vflag & VV_COPYONWRITE)
2972 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2973 if (vp->v_vflag & VV_SYSTEM)
2974 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2975 if (vp->v_vflag & VV_PROCDEP)
2976 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2977 if (vp->v_vflag & VV_NOKNOTE)
2978 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2979 if (vp->v_vflag & VV_DELETED)
2980 strlcat(buf, "|VV_DELETED", sizeof(buf));
2981 if (vp->v_vflag & VV_MD)
2982 strlcat(buf, "|VV_MD", sizeof(buf));
2983 if (vp->v_vflag & VV_FORCEINSMQ)
2984 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
2985 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
2986 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2987 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
2989 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2990 strlcat(buf, buf2, sizeof(buf));
2992 if (vp->v_iflag & VI_MOUNT)
2993 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2994 if (vp->v_iflag & VI_AGE)
2995 strlcat(buf, "|VI_AGE", sizeof(buf));
2996 if (vp->v_iflag & VI_DOOMED)
2997 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2998 if (vp->v_iflag & VI_FREE)
2999 strlcat(buf, "|VI_FREE", sizeof(buf));
3000 if (vp->v_iflag & VI_ACTIVE)
3001 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3002 if (vp->v_iflag & VI_DOINGINACT)
3003 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3004 if (vp->v_iflag & VI_OWEINACT)
3005 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3006 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
3007 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
3009 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3010 strlcat(buf, buf2, sizeof(buf));
3012 printf(" flags (%s)\n", buf + 1);
3013 if (mtx_owned(VI_MTX(vp)))
3014 printf(" VI_LOCKed");
3015 if (vp->v_object != NULL)
3016 printf(" v_object %p ref %d pages %d\n",
3017 vp->v_object, vp->v_object->ref_count,
3018 vp->v_object->resident_page_count);
3020 lockmgr_printinfo(vp->v_vnlock);
3021 if (vp->v_data != NULL)
3027 * List all of the locked vnodes in the system.
3028 * Called when debugging the kernel.
3030 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3032 struct mount *mp, *nmp;
3036 * Note: because this is DDB, we can't obey the locking semantics
3037 * for these structures, which means we could catch an inconsistent
3038 * state and dereference a nasty pointer. Not much to be done
3041 db_printf("Locked vnodes\n");
3042 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
3043 nmp = TAILQ_NEXT(mp, mnt_list);
3044 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3045 if (vp->v_type != VMARKER &&
3049 nmp = TAILQ_NEXT(mp, mnt_list);
3054 * Show details about the given vnode.
3056 DB_SHOW_COMMAND(vnode, db_show_vnode)
3062 vp = (struct vnode *)addr;
3063 vn_printf(vp, "vnode ");
3067 * Show details about the given mount point.
3069 DB_SHOW_COMMAND(mount, db_show_mount)
3080 /* No address given, print short info about all mount points. */
3081 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3082 db_printf("%p %s on %s (%s)\n", mp,
3083 mp->mnt_stat.f_mntfromname,
3084 mp->mnt_stat.f_mntonname,
3085 mp->mnt_stat.f_fstypename);
3089 db_printf("\nMore info: show mount <addr>\n");
3093 mp = (struct mount *)addr;
3094 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3095 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3098 mflags = mp->mnt_flag;
3099 #define MNT_FLAG(flag) do { \
3100 if (mflags & (flag)) { \
3101 if (buf[0] != '\0') \
3102 strlcat(buf, ", ", sizeof(buf)); \
3103 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3104 mflags &= ~(flag); \
3107 MNT_FLAG(MNT_RDONLY);
3108 MNT_FLAG(MNT_SYNCHRONOUS);
3109 MNT_FLAG(MNT_NOEXEC);
3110 MNT_FLAG(MNT_NOSUID);
3111 MNT_FLAG(MNT_NFS4ACLS);
3112 MNT_FLAG(MNT_UNION);
3113 MNT_FLAG(MNT_ASYNC);
3114 MNT_FLAG(MNT_SUIDDIR);
3115 MNT_FLAG(MNT_SOFTDEP);
3116 MNT_FLAG(MNT_NOSYMFOLLOW);
3117 MNT_FLAG(MNT_GJOURNAL);
3118 MNT_FLAG(MNT_MULTILABEL);
3120 MNT_FLAG(MNT_NOATIME);
3121 MNT_FLAG(MNT_NOCLUSTERR);
3122 MNT_FLAG(MNT_NOCLUSTERW);
3124 MNT_FLAG(MNT_EXRDONLY);
3125 MNT_FLAG(MNT_EXPORTED);
3126 MNT_FLAG(MNT_DEFEXPORTED);
3127 MNT_FLAG(MNT_EXPORTANON);
3128 MNT_FLAG(MNT_EXKERB);
3129 MNT_FLAG(MNT_EXPUBLIC);
3130 MNT_FLAG(MNT_LOCAL);
3131 MNT_FLAG(MNT_QUOTA);
3132 MNT_FLAG(MNT_ROOTFS);
3134 MNT_FLAG(MNT_IGNORE);
3135 MNT_FLAG(MNT_UPDATE);
3136 MNT_FLAG(MNT_DELEXPORT);
3137 MNT_FLAG(MNT_RELOAD);
3138 MNT_FLAG(MNT_FORCE);
3139 MNT_FLAG(MNT_SNAPSHOT);
3140 MNT_FLAG(MNT_BYFSID);
3144 strlcat(buf, ", ", sizeof(buf));
3145 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3146 "0x%016jx", mflags);
3148 db_printf(" mnt_flag = %s\n", buf);
3151 flags = mp->mnt_kern_flag;
3152 #define MNT_KERN_FLAG(flag) do { \
3153 if (flags & (flag)) { \
3154 if (buf[0] != '\0') \
3155 strlcat(buf, ", ", sizeof(buf)); \
3156 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3160 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3161 MNT_KERN_FLAG(MNTK_ASYNC);
3162 MNT_KERN_FLAG(MNTK_SOFTDEP);
3163 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3164 MNT_KERN_FLAG(MNTK_DRAINING);
3165 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3166 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3167 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3168 MNT_KERN_FLAG(MNTK_NO_IOPF);
3169 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3170 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3171 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3172 MNT_KERN_FLAG(MNTK_MARKER);
3173 MNT_KERN_FLAG(MNTK_NOASYNC);
3174 MNT_KERN_FLAG(MNTK_UNMOUNT);
3175 MNT_KERN_FLAG(MNTK_MWAIT);
3176 MNT_KERN_FLAG(MNTK_SUSPEND);
3177 MNT_KERN_FLAG(MNTK_SUSPEND2);
3178 MNT_KERN_FLAG(MNTK_SUSPENDED);
3179 MNT_KERN_FLAG(MNTK_MPSAFE);
3180 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3181 MNT_KERN_FLAG(MNTK_NOKNOTE);
3182 #undef MNT_KERN_FLAG
3185 strlcat(buf, ", ", sizeof(buf));
3186 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3189 db_printf(" mnt_kern_flag = %s\n", buf);
3191 db_printf(" mnt_opt = ");
3192 opt = TAILQ_FIRST(mp->mnt_opt);
3194 db_printf("%s", opt->name);
3195 opt = TAILQ_NEXT(opt, link);
3196 while (opt != NULL) {
3197 db_printf(", %s", opt->name);
3198 opt = TAILQ_NEXT(opt, link);
3204 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3205 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3206 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3207 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3208 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3209 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3210 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3211 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3212 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3213 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3214 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3215 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3217 db_printf(" mnt_cred = { uid=%u ruid=%u",
3218 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3219 if (jailed(mp->mnt_cred))
3220 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3222 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3223 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3224 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3225 db_printf(" mnt_activevnodelistsize = %d\n",
3226 mp->mnt_activevnodelistsize);
3227 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3228 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3229 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3230 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3231 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3232 db_printf(" mnt_secondary_accwrites = %d\n",
3233 mp->mnt_secondary_accwrites);
3234 db_printf(" mnt_gjprovider = %s\n",
3235 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3237 db_printf("\n\nList of active vnodes\n");
3238 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3239 if (vp->v_type != VMARKER) {
3240 vn_printf(vp, "vnode ");
3245 db_printf("\n\nList of inactive vnodes\n");
3246 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3247 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3248 vn_printf(vp, "vnode ");
3257 * Fill in a struct xvfsconf based on a struct vfsconf.
3260 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3262 struct xvfsconf xvfsp;
3264 bzero(&xvfsp, sizeof(xvfsp));
3265 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3266 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3267 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3268 xvfsp.vfc_flags = vfsp->vfc_flags;
3270 * These are unused in userland, we keep them
3271 * to not break binary compatibility.
3273 xvfsp.vfc_vfsops = NULL;
3274 xvfsp.vfc_next = NULL;
3275 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3278 #ifdef COMPAT_FREEBSD32
3280 uint32_t vfc_vfsops;
3281 char vfc_name[MFSNAMELEN];
3282 int32_t vfc_typenum;
3283 int32_t vfc_refcount;
3289 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3291 struct xvfsconf32 xvfsp;
3293 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3294 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3295 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3296 xvfsp.vfc_flags = vfsp->vfc_flags;
3297 xvfsp.vfc_vfsops = 0;
3299 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3304 * Top level filesystem related information gathering.
3307 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3309 struct vfsconf *vfsp;
3313 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3314 #ifdef COMPAT_FREEBSD32
3315 if (req->flags & SCTL_MASK32)
3316 error = vfsconf2x32(req, vfsp);
3319 error = vfsconf2x(req, vfsp);
3326 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD,
3327 NULL, 0, sysctl_vfs_conflist,
3328 "S,xvfsconf", "List of all configured filesystems");
3330 #ifndef BURN_BRIDGES
3331 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3334 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3336 int *name = (int *)arg1 - 1; /* XXX */
3337 u_int namelen = arg2 + 1; /* XXX */
3338 struct vfsconf *vfsp;
3340 printf("WARNING: userland calling deprecated sysctl, "
3341 "please rebuild world\n");
3343 #if 1 || defined(COMPAT_PRELITE2)
3344 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3346 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3350 case VFS_MAXTYPENUM:
3353 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3356 return (ENOTDIR); /* overloaded */
3357 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
3358 if (vfsp->vfc_typenum == name[2])
3361 return (EOPNOTSUPP);
3362 #ifdef COMPAT_FREEBSD32
3363 if (req->flags & SCTL_MASK32)
3364 return (vfsconf2x32(req, vfsp));
3367 return (vfsconf2x(req, vfsp));
3369 return (EOPNOTSUPP);
3372 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
3373 vfs_sysctl, "Generic filesystem");
3375 #if 1 || defined(COMPAT_PRELITE2)
3378 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3381 struct vfsconf *vfsp;
3382 struct ovfsconf ovfs;
3384 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3385 bzero(&ovfs, sizeof(ovfs));
3386 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3387 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3388 ovfs.vfc_index = vfsp->vfc_typenum;
3389 ovfs.vfc_refcount = vfsp->vfc_refcount;
3390 ovfs.vfc_flags = vfsp->vfc_flags;
3391 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3398 #endif /* 1 || COMPAT_PRELITE2 */
3399 #endif /* !BURN_BRIDGES */
3401 #define KINFO_VNODESLOP 10
3404 * Dump vnode list (via sysctl).
3408 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3416 * Stale numvnodes access is not fatal here.
3419 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3421 /* Make an estimate */
3422 return (SYSCTL_OUT(req, 0, len));
3424 error = sysctl_wire_old_buffer(req, 0);
3427 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3429 mtx_lock(&mountlist_mtx);
3430 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3431 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3434 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3438 xvn[n].xv_size = sizeof *xvn;
3439 xvn[n].xv_vnode = vp;
3440 xvn[n].xv_id = 0; /* XXX compat */
3441 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3443 XV_COPY(writecount);
3449 xvn[n].xv_flag = vp->v_vflag;
3451 switch (vp->v_type) {
3458 if (vp->v_rdev == NULL) {
3462 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3465 xvn[n].xv_socket = vp->v_socket;
3468 xvn[n].xv_fifo = vp->v_fifoinfo;
3473 /* shouldn't happen? */
3481 mtx_lock(&mountlist_mtx);
3486 mtx_unlock(&mountlist_mtx);
3488 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3493 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3494 0, 0, sysctl_vnode, "S,xvnode", "");
3498 * Unmount all filesystems. The list is traversed in reverse order
3499 * of mounting to avoid dependencies.
3502 vfs_unmountall(void)
3508 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3512 * Since this only runs when rebooting, it is not interlocked.
3514 while(!TAILQ_EMPTY(&mountlist)) {
3515 mp = TAILQ_LAST(&mountlist, mntlist);
3516 error = dounmount(mp, MNT_FORCE, td);
3518 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3520 * XXX: Due to the way in which we mount the root
3521 * file system off of devfs, devfs will generate a
3522 * "busy" warning when we try to unmount it before
3523 * the root. Don't print a warning as a result in
3524 * order to avoid false positive errors that may
3525 * cause needless upset.
3527 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3528 printf("unmount of %s failed (",
3529 mp->mnt_stat.f_mntonname);
3533 printf("%d)\n", error);
3536 /* The unmount has removed mp from the mountlist */
3542 * perform msync on all vnodes under a mount point
3543 * the mount point must be locked.
3546 vfs_msync(struct mount *mp, int flags)
3548 struct vnode *vp, *mvp;
3549 struct vm_object *obj;
3551 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3552 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
3554 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3555 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3557 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3559 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3566 VM_OBJECT_LOCK(obj);
3567 vm_object_page_clean(obj, 0, 0,
3569 OBJPC_SYNC : OBJPC_NOSYNC);
3570 VM_OBJECT_UNLOCK(obj);
3580 destroy_vpollinfo(struct vpollinfo *vi)
3582 seldrain(&vi->vpi_selinfo);
3583 knlist_destroy(&vi->vpi_selinfo.si_note);
3584 mtx_destroy(&vi->vpi_lock);
3585 uma_zfree(vnodepoll_zone, vi);
3589 * Initalize per-vnode helper structure to hold poll-related state.
3592 v_addpollinfo(struct vnode *vp)
3594 struct vpollinfo *vi;
3596 if (vp->v_pollinfo != NULL)
3598 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3599 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3600 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3601 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3603 if (vp->v_pollinfo != NULL) {
3605 destroy_vpollinfo(vi);
3608 vp->v_pollinfo = vi;
3613 * Record a process's interest in events which might happen to
3614 * a vnode. Because poll uses the historic select-style interface
3615 * internally, this routine serves as both the ``check for any
3616 * pending events'' and the ``record my interest in future events''
3617 * functions. (These are done together, while the lock is held,
3618 * to avoid race conditions.)
3621 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3625 mtx_lock(&vp->v_pollinfo->vpi_lock);
3626 if (vp->v_pollinfo->vpi_revents & events) {
3628 * This leaves events we are not interested
3629 * in available for the other process which
3630 * which presumably had requested them
3631 * (otherwise they would never have been
3634 events &= vp->v_pollinfo->vpi_revents;
3635 vp->v_pollinfo->vpi_revents &= ~events;
3637 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3640 vp->v_pollinfo->vpi_events |= events;
3641 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3642 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3647 * Routine to create and manage a filesystem syncer vnode.
3649 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3650 static int sync_fsync(struct vop_fsync_args *);
3651 static int sync_inactive(struct vop_inactive_args *);
3652 static int sync_reclaim(struct vop_reclaim_args *);
3654 static struct vop_vector sync_vnodeops = {
3655 .vop_bypass = VOP_EOPNOTSUPP,
3656 .vop_close = sync_close, /* close */
3657 .vop_fsync = sync_fsync, /* fsync */
3658 .vop_inactive = sync_inactive, /* inactive */
3659 .vop_reclaim = sync_reclaim, /* reclaim */
3660 .vop_lock1 = vop_stdlock, /* lock */
3661 .vop_unlock = vop_stdunlock, /* unlock */
3662 .vop_islocked = vop_stdislocked, /* islocked */
3666 * Create a new filesystem syncer vnode for the specified mount point.
3669 vfs_allocate_syncvnode(struct mount *mp)
3673 static long start, incr, next;
3676 /* Allocate a new vnode */
3677 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3679 panic("vfs_allocate_syncvnode: getnewvnode() failed");
3681 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3682 vp->v_vflag |= VV_FORCEINSMQ;
3683 error = insmntque(vp, mp);
3685 panic("vfs_allocate_syncvnode: insmntque() failed");
3686 vp->v_vflag &= ~VV_FORCEINSMQ;
3689 * Place the vnode onto the syncer worklist. We attempt to
3690 * scatter them about on the list so that they will go off
3691 * at evenly distributed times even if all the filesystems
3692 * are mounted at once.
3695 if (next == 0 || next > syncer_maxdelay) {
3699 start = syncer_maxdelay / 2;
3700 incr = syncer_maxdelay;
3706 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3707 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3708 mtx_lock(&sync_mtx);
3710 if (mp->mnt_syncer == NULL) {
3711 mp->mnt_syncer = vp;
3714 mtx_unlock(&sync_mtx);
3717 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3724 vfs_deallocate_syncvnode(struct mount *mp)
3728 mtx_lock(&sync_mtx);
3729 vp = mp->mnt_syncer;
3731 mp->mnt_syncer = NULL;
3732 mtx_unlock(&sync_mtx);
3738 * Do a lazy sync of the filesystem.
3741 sync_fsync(struct vop_fsync_args *ap)
3743 struct vnode *syncvp = ap->a_vp;
3744 struct mount *mp = syncvp->v_mount;
3749 * We only need to do something if this is a lazy evaluation.
3751 if (ap->a_waitfor != MNT_LAZY)
3755 * Move ourselves to the back of the sync list.
3757 bo = &syncvp->v_bufobj;
3759 vn_syncer_add_to_worklist(bo, syncdelay);
3763 * Walk the list of vnodes pushing all that are dirty and
3764 * not already on the sync list.
3766 mtx_lock(&mountlist_mtx);
3767 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
3768 mtx_unlock(&mountlist_mtx);
3771 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3775 save = curthread_pflags_set(TDP_SYNCIO);
3776 vfs_msync(mp, MNT_NOWAIT);
3777 error = VFS_SYNC(mp, MNT_LAZY);
3778 curthread_pflags_restore(save);
3779 vn_finished_write(mp);
3785 * The syncer vnode is no referenced.
3788 sync_inactive(struct vop_inactive_args *ap)
3796 * The syncer vnode is no longer needed and is being decommissioned.
3798 * Modifications to the worklist must be protected by sync_mtx.
3801 sync_reclaim(struct vop_reclaim_args *ap)
3803 struct vnode *vp = ap->a_vp;
3808 mtx_lock(&sync_mtx);
3809 if (vp->v_mount->mnt_syncer == vp)
3810 vp->v_mount->mnt_syncer = NULL;
3811 if (bo->bo_flag & BO_ONWORKLST) {
3812 LIST_REMOVE(bo, bo_synclist);
3813 syncer_worklist_len--;
3815 bo->bo_flag &= ~BO_ONWORKLST;
3817 mtx_unlock(&sync_mtx);
3824 * Check if vnode represents a disk device
3827 vn_isdisk(struct vnode *vp, int *errp)
3833 if (vp->v_type != VCHR)
3835 else if (vp->v_rdev == NULL)
3837 else if (vp->v_rdev->si_devsw == NULL)
3839 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3844 return (error == 0);
3848 * Common filesystem object access control check routine. Accepts a
3849 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3850 * and optional call-by-reference privused argument allowing vaccess()
3851 * to indicate to the caller whether privilege was used to satisfy the
3852 * request (obsoleted). Returns 0 on success, or an errno on failure.
3855 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3856 accmode_t accmode, struct ucred *cred, int *privused)
3858 accmode_t dac_granted;
3859 accmode_t priv_granted;
3861 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3862 ("invalid bit in accmode"));
3863 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3864 ("VAPPEND without VWRITE"));
3867 * Look for a normal, non-privileged way to access the file/directory
3868 * as requested. If it exists, go with that.
3871 if (privused != NULL)
3876 /* Check the owner. */
3877 if (cred->cr_uid == file_uid) {
3878 dac_granted |= VADMIN;
3879 if (file_mode & S_IXUSR)
3880 dac_granted |= VEXEC;
3881 if (file_mode & S_IRUSR)
3882 dac_granted |= VREAD;
3883 if (file_mode & S_IWUSR)
3884 dac_granted |= (VWRITE | VAPPEND);
3886 if ((accmode & dac_granted) == accmode)
3892 /* Otherwise, check the groups (first match) */
3893 if (groupmember(file_gid, cred)) {
3894 if (file_mode & S_IXGRP)
3895 dac_granted |= VEXEC;
3896 if (file_mode & S_IRGRP)
3897 dac_granted |= VREAD;
3898 if (file_mode & S_IWGRP)
3899 dac_granted |= (VWRITE | VAPPEND);
3901 if ((accmode & dac_granted) == accmode)
3907 /* Otherwise, check everyone else. */
3908 if (file_mode & S_IXOTH)
3909 dac_granted |= VEXEC;
3910 if (file_mode & S_IROTH)
3911 dac_granted |= VREAD;
3912 if (file_mode & S_IWOTH)
3913 dac_granted |= (VWRITE | VAPPEND);
3914 if ((accmode & dac_granted) == accmode)
3919 * Build a privilege mask to determine if the set of privileges
3920 * satisfies the requirements when combined with the granted mask
3921 * from above. For each privilege, if the privilege is required,
3922 * bitwise or the request type onto the priv_granted mask.
3928 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3929 * requests, instead of PRIV_VFS_EXEC.
3931 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3932 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3933 priv_granted |= VEXEC;
3936 * Ensure that at least one execute bit is on. Otherwise,
3937 * a privileged user will always succeed, and we don't want
3938 * this to happen unless the file really is executable.
3940 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3941 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
3942 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3943 priv_granted |= VEXEC;
3946 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3947 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3948 priv_granted |= VREAD;
3950 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3951 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3952 priv_granted |= (VWRITE | VAPPEND);
3954 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3955 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3956 priv_granted |= VADMIN;
3958 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3959 /* XXX audit: privilege used */
3960 if (privused != NULL)
3965 return ((accmode & VADMIN) ? EPERM : EACCES);
3969 * Credential check based on process requesting service, and per-attribute
3973 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3974 struct thread *td, accmode_t accmode)
3978 * Kernel-invoked always succeeds.
3984 * Do not allow privileged processes in jail to directly manipulate
3985 * system attributes.
3987 switch (attrnamespace) {
3988 case EXTATTR_NAMESPACE_SYSTEM:
3989 /* Potentially should be: return (EPERM); */
3990 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3991 case EXTATTR_NAMESPACE_USER:
3992 return (VOP_ACCESS(vp, accmode, cred, td));
3998 #ifdef DEBUG_VFS_LOCKS
4000 * This only exists to supress warnings from unlocked specfs accesses. It is
4001 * no longer ok to have an unlocked VFS.
4003 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4004 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4006 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4007 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4008 "Drop into debugger on lock violation");
4010 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4011 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4012 0, "Check for interlock across VOPs");
4014 int vfs_badlock_print = 1; /* Print lock violations. */
4015 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4016 0, "Print lock violations");
4019 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4020 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4021 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4025 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4029 if (vfs_badlock_backtrace)
4032 if (vfs_badlock_print)
4033 printf("%s: %p %s\n", str, (void *)vp, msg);
4034 if (vfs_badlock_ddb)
4035 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4039 assert_vi_locked(struct vnode *vp, const char *str)
4042 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4043 vfs_badlock("interlock is not locked but should be", str, vp);
4047 assert_vi_unlocked(struct vnode *vp, const char *str)
4050 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4051 vfs_badlock("interlock is locked but should not be", str, vp);
4055 assert_vop_locked(struct vnode *vp, const char *str)
4059 if (!IGNORE_LOCK(vp)) {
4060 locked = VOP_ISLOCKED(vp);
4061 if (locked == 0 || locked == LK_EXCLOTHER)
4062 vfs_badlock("is not locked but should be", str, vp);
4067 assert_vop_unlocked(struct vnode *vp, const char *str)
4070 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4071 vfs_badlock("is locked but should not be", str, vp);
4075 assert_vop_elocked(struct vnode *vp, const char *str)
4078 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4079 vfs_badlock("is not exclusive locked but should be", str, vp);
4084 assert_vop_elocked_other(struct vnode *vp, const char *str)
4087 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
4088 vfs_badlock("is not exclusive locked by another thread",
4093 assert_vop_slocked(struct vnode *vp, const char *str)
4096 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
4097 vfs_badlock("is not locked shared but should be", str, vp);
4100 #endif /* DEBUG_VFS_LOCKS */
4103 vop_rename_fail(struct vop_rename_args *ap)
4106 if (ap->a_tvp != NULL)
4108 if (ap->a_tdvp == ap->a_tvp)
4117 vop_rename_pre(void *ap)
4119 struct vop_rename_args *a = ap;
4121 #ifdef DEBUG_VFS_LOCKS
4123 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4124 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4125 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4126 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4128 /* Check the source (from). */
4129 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4130 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4131 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4132 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4133 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4135 /* Check the target. */
4137 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4138 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4140 if (a->a_tdvp != a->a_fdvp)
4142 if (a->a_tvp != a->a_fvp)
4150 vop_strategy_pre(void *ap)
4152 #ifdef DEBUG_VFS_LOCKS
4153 struct vop_strategy_args *a;
4160 * Cluster ops lock their component buffers but not the IO container.
4162 if ((bp->b_flags & B_CLUSTER) != 0)
4165 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4166 if (vfs_badlock_print)
4168 "VOP_STRATEGY: bp is not locked but should be\n");
4169 if (vfs_badlock_ddb)
4170 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4176 vop_lock_pre(void *ap)
4178 #ifdef DEBUG_VFS_LOCKS
4179 struct vop_lock1_args *a = ap;
4181 if ((a->a_flags & LK_INTERLOCK) == 0)
4182 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4184 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4189 vop_lock_post(void *ap, int rc)
4191 #ifdef DEBUG_VFS_LOCKS
4192 struct vop_lock1_args *a = ap;
4194 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4195 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4196 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4201 vop_unlock_pre(void *ap)
4203 #ifdef DEBUG_VFS_LOCKS
4204 struct vop_unlock_args *a = ap;
4206 if (a->a_flags & LK_INTERLOCK)
4207 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4208 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4213 vop_unlock_post(void *ap, int rc)
4215 #ifdef DEBUG_VFS_LOCKS
4216 struct vop_unlock_args *a = ap;
4218 if (a->a_flags & LK_INTERLOCK)
4219 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4224 vop_create_post(void *ap, int rc)
4226 struct vop_create_args *a = ap;
4229 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4233 vop_deleteextattr_post(void *ap, int rc)
4235 struct vop_deleteextattr_args *a = ap;
4238 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4242 vop_link_post(void *ap, int rc)
4244 struct vop_link_args *a = ap;
4247 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4248 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4253 vop_mkdir_post(void *ap, int rc)
4255 struct vop_mkdir_args *a = ap;
4258 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4262 vop_mknod_post(void *ap, int rc)
4264 struct vop_mknod_args *a = ap;
4267 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4271 vop_remove_post(void *ap, int rc)
4273 struct vop_remove_args *a = ap;
4276 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4277 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4282 vop_rename_post(void *ap, int rc)
4284 struct vop_rename_args *a = ap;
4287 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4288 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4289 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4291 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4293 if (a->a_tdvp != a->a_fdvp)
4295 if (a->a_tvp != a->a_fvp)
4303 vop_rmdir_post(void *ap, int rc)
4305 struct vop_rmdir_args *a = ap;
4308 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4309 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4314 vop_setattr_post(void *ap, int rc)
4316 struct vop_setattr_args *a = ap;
4319 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4323 vop_setextattr_post(void *ap, int rc)
4325 struct vop_setextattr_args *a = ap;
4328 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4332 vop_symlink_post(void *ap, int rc)
4334 struct vop_symlink_args *a = ap;
4337 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4340 static struct knlist fs_knlist;
4343 vfs_event_init(void *arg)
4345 knlist_init_mtx(&fs_knlist, NULL);
4347 /* XXX - correct order? */
4348 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4351 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4354 KNOTE_UNLOCKED(&fs_knlist, event);
4357 static int filt_fsattach(struct knote *kn);
4358 static void filt_fsdetach(struct knote *kn);
4359 static int filt_fsevent(struct knote *kn, long hint);
4361 struct filterops fs_filtops = {
4363 .f_attach = filt_fsattach,
4364 .f_detach = filt_fsdetach,
4365 .f_event = filt_fsevent
4369 filt_fsattach(struct knote *kn)
4372 kn->kn_flags |= EV_CLEAR;
4373 knlist_add(&fs_knlist, kn, 0);
4378 filt_fsdetach(struct knote *kn)
4381 knlist_remove(&fs_knlist, kn, 0);
4385 filt_fsevent(struct knote *kn, long hint)
4388 kn->kn_fflags |= hint;
4389 return (kn->kn_fflags != 0);
4393 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4399 error = SYSCTL_IN(req, &vc, sizeof(vc));
4402 if (vc.vc_vers != VFS_CTL_VERS1)
4404 mp = vfs_getvfs(&vc.vc_fsid);
4407 /* ensure that a specific sysctl goes to the right filesystem. */
4408 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4409 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4413 VCTLTOREQ(&vc, req);
4414 error = VFS_SYSCTL(mp, vc.vc_op, req);
4419 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4420 NULL, 0, sysctl_vfs_ctl, "",
4424 * Function to initialize a va_filerev field sensibly.
4425 * XXX: Wouldn't a random number make a lot more sense ??
4428 init_va_filerev(void)
4433 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4436 static int filt_vfsread(struct knote *kn, long hint);
4437 static int filt_vfswrite(struct knote *kn, long hint);
4438 static int filt_vfsvnode(struct knote *kn, long hint);
4439 static void filt_vfsdetach(struct knote *kn);
4440 static struct filterops vfsread_filtops = {
4442 .f_detach = filt_vfsdetach,
4443 .f_event = filt_vfsread
4445 static struct filterops vfswrite_filtops = {
4447 .f_detach = filt_vfsdetach,
4448 .f_event = filt_vfswrite
4450 static struct filterops vfsvnode_filtops = {
4452 .f_detach = filt_vfsdetach,
4453 .f_event = filt_vfsvnode
4457 vfs_knllock(void *arg)
4459 struct vnode *vp = arg;
4461 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4465 vfs_knlunlock(void *arg)
4467 struct vnode *vp = arg;
4473 vfs_knl_assert_locked(void *arg)
4475 #ifdef DEBUG_VFS_LOCKS
4476 struct vnode *vp = arg;
4478 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4483 vfs_knl_assert_unlocked(void *arg)
4485 #ifdef DEBUG_VFS_LOCKS
4486 struct vnode *vp = arg;
4488 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4493 vfs_kqfilter(struct vop_kqfilter_args *ap)
4495 struct vnode *vp = ap->a_vp;
4496 struct knote *kn = ap->a_kn;
4499 switch (kn->kn_filter) {
4501 kn->kn_fop = &vfsread_filtops;
4504 kn->kn_fop = &vfswrite_filtops;
4507 kn->kn_fop = &vfsvnode_filtops;
4513 kn->kn_hook = (caddr_t)vp;
4516 if (vp->v_pollinfo == NULL)
4518 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4519 knlist_add(knl, kn, 0);
4525 * Detach knote from vnode
4528 filt_vfsdetach(struct knote *kn)
4530 struct vnode *vp = (struct vnode *)kn->kn_hook;
4532 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4533 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4538 filt_vfsread(struct knote *kn, long hint)
4540 struct vnode *vp = (struct vnode *)kn->kn_hook;
4545 * filesystem is gone, so set the EOF flag and schedule
4546 * the knote for deletion.
4548 if (hint == NOTE_REVOKE) {
4550 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4555 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4559 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4560 res = (kn->kn_data != 0);
4567 filt_vfswrite(struct knote *kn, long hint)
4569 struct vnode *vp = (struct vnode *)kn->kn_hook;
4574 * filesystem is gone, so set the EOF flag and schedule
4575 * the knote for deletion.
4577 if (hint == NOTE_REVOKE)
4578 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4586 filt_vfsvnode(struct knote *kn, long hint)
4588 struct vnode *vp = (struct vnode *)kn->kn_hook;
4592 if (kn->kn_sfflags & hint)
4593 kn->kn_fflags |= hint;
4594 if (hint == NOTE_REVOKE) {
4595 kn->kn_flags |= EV_EOF;
4599 res = (kn->kn_fflags != 0);
4605 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4609 if (dp->d_reclen > ap->a_uio->uio_resid)
4610 return (ENAMETOOLONG);
4611 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4613 if (ap->a_ncookies != NULL) {
4614 if (ap->a_cookies != NULL)
4615 free(ap->a_cookies, M_TEMP);
4616 ap->a_cookies = NULL;
4617 *ap->a_ncookies = 0;
4621 if (ap->a_ncookies == NULL)
4624 KASSERT(ap->a_cookies,
4625 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4627 *ap->a_cookies = realloc(*ap->a_cookies,
4628 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4629 (*ap->a_cookies)[*ap->a_ncookies] = off;
4634 * Mark for update the access time of the file if the filesystem
4635 * supports VOP_MARKATIME. This functionality is used by execve and
4636 * mmap, so we want to avoid the I/O implied by directly setting
4637 * va_atime for the sake of efficiency.
4640 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4645 VFS_ASSERT_GIANT(mp);
4646 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4647 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4648 (void)VOP_MARKATIME(vp);
4652 * The purpose of this routine is to remove granularity from accmode_t,
4653 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4654 * VADMIN and VAPPEND.
4656 * If it returns 0, the caller is supposed to continue with the usual
4657 * access checks using 'accmode' as modified by this routine. If it
4658 * returns nonzero value, the caller is supposed to return that value
4661 * Note that after this routine runs, accmode may be zero.
4664 vfs_unixify_accmode(accmode_t *accmode)
4667 * There is no way to specify explicit "deny" rule using
4668 * file mode or POSIX.1e ACLs.
4670 if (*accmode & VEXPLICIT_DENY) {
4676 * None of these can be translated into usual access bits.
4677 * Also, the common case for NFSv4 ACLs is to not contain
4678 * either of these bits. Caller should check for VWRITE
4679 * on the containing directory instead.
4681 if (*accmode & (VDELETE_CHILD | VDELETE))
4684 if (*accmode & VADMIN_PERMS) {
4685 *accmode &= ~VADMIN_PERMS;
4690 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4691 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4693 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
4699 * These are helper functions for filesystems to traverse all
4700 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
4702 * This interface replaces MNT_VNODE_FOREACH.
4705 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
4708 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
4713 kern_yield(PRI_UNCHANGED);
4715 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4716 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
4717 while (vp != NULL && (vp->v_type == VMARKER ||
4718 (vp->v_iflag & VI_DOOMED) != 0))
4719 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4721 /* Check if we are done */
4723 __mnt_vnode_markerfree_all(mvp, mp);
4724 /* MNT_IUNLOCK(mp); -- done in above function */
4725 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4728 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4729 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4736 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
4740 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4743 (*mvp)->v_type = VMARKER;
4745 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
4746 while (vp != NULL && (vp->v_type == VMARKER ||
4747 (vp->v_iflag & VI_DOOMED) != 0))
4748 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4750 /* Check if we are done */
4754 free(*mvp, M_VNODE_MARKER);
4758 (*mvp)->v_mount = mp;
4759 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4767 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
4775 mtx_assert(MNT_MTX(mp), MA_OWNED);
4777 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4778 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4781 free(*mvp, M_VNODE_MARKER);
4786 * These are helper functions for filesystems to traverse their
4787 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
4790 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4793 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4798 free(*mvp, M_VNODE_MARKER);
4803 #define ALWAYS_YIELD (mp_ncpus == 1)
4805 #define ALWAYS_YIELD 1
4808 static struct vnode *
4809 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4811 struct vnode *vp, *nvp;
4813 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
4814 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4816 vp = TAILQ_NEXT(*mvp, v_actfreelist);
4817 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4818 while (vp != NULL) {
4819 if (vp->v_type == VMARKER) {
4820 vp = TAILQ_NEXT(vp, v_actfreelist);
4823 if (!VI_TRYLOCK(vp)) {
4824 if (ALWAYS_YIELD || should_yield()) {
4825 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4826 mtx_unlock(&vnode_free_list_mtx);
4828 mtx_lock(&vnode_free_list_mtx);
4833 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
4834 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
4835 ("alien vnode on the active list %p %p", vp, mp));
4836 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
4838 nvp = TAILQ_NEXT(vp, v_actfreelist);
4843 /* Check if we are done */
4845 mtx_unlock(&vnode_free_list_mtx);
4846 mnt_vnode_markerfree_active(mvp, mp);
4849 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4850 mtx_unlock(&vnode_free_list_mtx);
4851 ASSERT_VI_LOCKED(vp, "active iter");
4852 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
4858 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4862 kern_yield(PRI_UNCHANGED);
4863 mtx_lock(&vnode_free_list_mtx);
4864 return (mnt_vnode_next_active(mvp, mp));
4868 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
4872 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4876 (*mvp)->v_type = VMARKER;
4877 (*mvp)->v_mount = mp;
4879 mtx_lock(&vnode_free_list_mtx);
4880 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
4882 mtx_unlock(&vnode_free_list_mtx);
4883 mnt_vnode_markerfree_active(mvp, mp);
4886 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4887 return (mnt_vnode_next_active(mvp, mp));
4891 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4897 mtx_lock(&vnode_free_list_mtx);
4898 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4899 mtx_unlock(&vnode_free_list_mtx);
4900 mnt_vnode_markerfree_active(mvp, mp);