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");
126 static u_long vnodes_created;
127 SYSCTL_ULONG(_vfs, OID_AUTO, vnodes_created, CTLFLAG_RD, &vnodes_created,
128 0, "Number of vnodes created by getnewvnode");
131 * Conversion tables for conversion from vnode types to inode formats
134 enum vtype iftovt_tab[16] = {
135 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
136 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
138 int vttoif_tab[10] = {
139 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
140 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
144 * List of vnodes that are ready for recycling.
146 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
149 * Free vnode target. Free vnodes may simply be files which have been stat'd
150 * but not read. This is somewhat common, and a small cache of such files
151 * should be kept to avoid recreation costs.
153 static u_long wantfreevnodes;
154 SYSCTL_ULONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
155 /* Number of vnodes in the free list. */
156 static u_long freevnodes;
157 SYSCTL_ULONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
158 "Number of vnodes in the free list");
160 static int vlru_allow_cache_src;
161 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
162 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
164 static u_long recycles_count;
165 SYSCTL_ULONG(_vfs, OID_AUTO, recycles, CTLFLAG_RD, &recycles_count, 0,
166 "Number of vnodes recycled to avoid exceding kern.maxvnodes");
169 * Various variables used for debugging the new implementation of
171 * XXX these are probably of (very) limited utility now.
173 static int reassignbufcalls;
174 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
175 "Number of calls to reassignbuf");
178 * Cache for the mount type id assigned to NFS. This is used for
179 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
181 int nfs_mount_type = -1;
183 /* To keep more than one thread at a time from running vfs_getnewfsid */
184 static struct mtx mntid_mtx;
187 * Lock for any access to the following:
192 static struct mtx vnode_free_list_mtx;
194 /* Publicly exported FS */
195 struct nfs_public nfs_pub;
197 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
198 static uma_zone_t vnode_zone;
199 static uma_zone_t vnodepoll_zone;
202 * The workitem queue.
204 * It is useful to delay writes of file data and filesystem metadata
205 * for tens of seconds so that quickly created and deleted files need
206 * not waste disk bandwidth being created and removed. To realize this,
207 * we append vnodes to a "workitem" queue. When running with a soft
208 * updates implementation, most pending metadata dependencies should
209 * not wait for more than a few seconds. Thus, mounted on block devices
210 * are delayed only about a half the time that file data is delayed.
211 * Similarly, directory updates are more critical, so are only delayed
212 * about a third the time that file data is delayed. Thus, there are
213 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
214 * one each second (driven off the filesystem syncer process). The
215 * syncer_delayno variable indicates the next queue that is to be processed.
216 * Items that need to be processed soon are placed in this queue:
218 * syncer_workitem_pending[syncer_delayno]
220 * A delay of fifteen seconds is done by placing the request fifteen
221 * entries later in the queue:
223 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
226 static int syncer_delayno;
227 static long syncer_mask;
228 LIST_HEAD(synclist, bufobj);
229 static struct synclist *syncer_workitem_pending[2];
231 * The sync_mtx protects:
236 * syncer_workitem_pending
237 * syncer_worklist_len
240 static struct mtx sync_mtx;
241 static struct cv sync_wakeup;
243 #define SYNCER_MAXDELAY 32
244 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
245 static int syncdelay = 30; /* max time to delay syncing data */
246 static int filedelay = 30; /* time to delay syncing files */
247 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
248 "Time to delay syncing files (in seconds)");
249 static int dirdelay = 29; /* time to delay syncing directories */
250 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
251 "Time to delay syncing directories (in seconds)");
252 static int metadelay = 28; /* time to delay syncing metadata */
253 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
254 "Time to delay syncing metadata (in seconds)");
255 static int rushjob; /* number of slots to run ASAP */
256 static int stat_rush_requests; /* number of times I/O speeded up */
257 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
258 "Number of times I/O speeded up (rush requests)");
261 * When shutting down the syncer, run it at four times normal speed.
263 #define SYNCER_SHUTDOWN_SPEEDUP 4
264 static int sync_vnode_count;
265 static int syncer_worklist_len;
266 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
270 * Number of vnodes we want to exist at any one time. This is mostly used
271 * to size hash tables in vnode-related code. It is normally not used in
272 * getnewvnode(), as wantfreevnodes is normally nonzero.)
274 * XXX desiredvnodes is historical cruft and should not exist.
277 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
278 &desiredvnodes, 0, "Maximum number of vnodes");
279 SYSCTL_ULONG(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
280 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
281 static int vnlru_nowhere;
282 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
283 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
286 * Macros to control when a vnode is freed and recycled. All require
287 * the vnode interlock.
289 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
290 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
291 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
293 /* Shift count for (uintptr_t)vp to initialize vp->v_hash. */
297 * Initialize the vnode management data structures.
299 * Reevaluate the following cap on the number of vnodes after the physical
300 * memory size exceeds 512GB. In the limit, as the physical memory size
301 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
303 #ifndef MAXVNODES_MAX
304 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
307 vntblinit(void *dummy __unused)
310 int physvnodes, virtvnodes;
313 * Desiredvnodes is a function of the physical memory size and the
314 * kernel's heap size. Generally speaking, it scales with the
315 * physical memory size. The ratio of desiredvnodes to physical pages
316 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
317 * marginal ratio of desiredvnodes to physical pages is one to
318 * sixteen. However, desiredvnodes is limited by the kernel's heap
319 * size. The memory required by desiredvnodes vnodes and vm objects
320 * may not exceed one seventh of the kernel's heap size.
322 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
323 cnt.v_page_count) / 16;
324 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
325 sizeof(struct vnode)));
326 desiredvnodes = min(physvnodes, virtvnodes);
327 if (desiredvnodes > MAXVNODES_MAX) {
329 printf("Reducing kern.maxvnodes %d -> %d\n",
330 desiredvnodes, MAXVNODES_MAX);
331 desiredvnodes = MAXVNODES_MAX;
333 wantfreevnodes = desiredvnodes / 4;
334 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
335 TAILQ_INIT(&vnode_free_list);
336 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
337 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
338 NULL, NULL, UMA_ALIGN_PTR, 0);
339 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
340 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
342 * Initialize the filesystem syncer.
344 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
346 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
348 syncer_maxdelay = syncer_mask + 1;
349 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
350 cv_init(&sync_wakeup, "syncer");
351 for (i = 1; i <= sizeof(struct vnode); i <<= 1)
355 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
359 * Mark a mount point as busy. Used to synchronize access and to delay
360 * unmounting. Eventually, mountlist_mtx is not released on failure.
362 * vfs_busy() is a custom lock, it can block the caller.
363 * vfs_busy() only sleeps if the unmount is active on the mount point.
364 * For a mountpoint mp, vfs_busy-enforced lock is before lock of any
365 * vnode belonging to mp.
367 * Lookup uses vfs_busy() to traverse mount points.
369 * / vnode lock A / vnode lock (/var) D
370 * /var vnode lock B /log vnode lock(/var/log) E
371 * vfs_busy lock C vfs_busy lock F
373 * Within each file system, the lock order is C->A->B and F->D->E.
375 * When traversing across mounts, the system follows that lock order:
381 * The lookup() process for namei("/var") illustrates the process:
382 * VOP_LOOKUP() obtains B while A is held
383 * vfs_busy() obtains a shared lock on F while A and B are held
384 * vput() releases lock on B
385 * vput() releases lock on A
386 * VFS_ROOT() obtains lock on D while shared lock on F is held
387 * vfs_unbusy() releases shared lock on F
388 * vn_lock() obtains lock on deadfs vnode vp_crossmp instead of A.
389 * Attempt to lock A (instead of vp_crossmp) while D is held would
390 * violate the global order, causing deadlocks.
392 * dounmount() locks B while F is drained.
395 vfs_busy(struct mount *mp, int flags)
398 MPASS((flags & ~MBF_MASK) == 0);
399 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
404 * If mount point is currenly being unmounted, sleep until the
405 * mount point fate is decided. If thread doing the unmounting fails,
406 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
407 * that this mount point has survived the unmount attempt and vfs_busy
408 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
409 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
410 * about to be really destroyed. vfs_busy needs to release its
411 * reference on the mount point in this case and return with ENOENT,
412 * telling the caller that mount mount it tried to busy is no longer
415 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
416 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
419 CTR1(KTR_VFS, "%s: failed busying before sleeping",
423 if (flags & MBF_MNTLSTLOCK)
424 mtx_unlock(&mountlist_mtx);
425 mp->mnt_kern_flag |= MNTK_MWAIT;
426 msleep(mp, MNT_MTX(mp), PVFS | PDROP, "vfs_busy", 0);
427 if (flags & MBF_MNTLSTLOCK)
428 mtx_lock(&mountlist_mtx);
431 if (flags & MBF_MNTLSTLOCK)
432 mtx_unlock(&mountlist_mtx);
439 * Free a busy filesystem.
442 vfs_unbusy(struct mount *mp)
445 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
448 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
450 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
451 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
452 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
453 mp->mnt_kern_flag &= ~MNTK_DRAINING;
454 wakeup(&mp->mnt_lockref);
460 * Lookup a mount point by filesystem identifier.
463 vfs_getvfs(fsid_t *fsid)
467 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
468 mtx_lock(&mountlist_mtx);
469 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
470 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
471 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
473 mtx_unlock(&mountlist_mtx);
477 mtx_unlock(&mountlist_mtx);
478 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
479 return ((struct mount *) 0);
483 * Lookup a mount point by filesystem identifier, busying it before
486 * To avoid congestion on mountlist_mtx, implement simple direct-mapped
487 * cache for popular filesystem identifiers. The cache is lockess, using
488 * the fact that struct mount's are never freed. In worst case we may
489 * get pointer to unmounted or even different filesystem, so we have to
490 * check what we got, and go slow way if so.
493 vfs_busyfs(fsid_t *fsid)
495 #define FSID_CACHE_SIZE 256
496 typedef struct mount * volatile vmp_t;
497 static vmp_t cache[FSID_CACHE_SIZE];
502 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
503 hash = fsid->val[0] ^ fsid->val[1];
504 hash = (hash >> 16 ^ hash) & (FSID_CACHE_SIZE - 1);
507 mp->mnt_stat.f_fsid.val[0] != fsid->val[0] ||
508 mp->mnt_stat.f_fsid.val[1] != fsid->val[1])
510 if (vfs_busy(mp, 0) != 0) {
514 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
515 mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
521 mtx_lock(&mountlist_mtx);
522 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
523 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
524 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
525 error = vfs_busy(mp, MBF_MNTLSTLOCK);
528 mtx_unlock(&mountlist_mtx);
535 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
536 mtx_unlock(&mountlist_mtx);
537 return ((struct mount *) 0);
541 * Check if a user can access privileged mount options.
544 vfs_suser(struct mount *mp, struct thread *td)
549 * If the thread is jailed, but this is not a jail-friendly file
550 * system, deny immediately.
552 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
556 * If the file system was mounted outside the jail of the calling
557 * thread, deny immediately.
559 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
563 * If file system supports delegated administration, we don't check
564 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
565 * by the file system itself.
566 * If this is not the user that did original mount, we check for
567 * the PRIV_VFS_MOUNT_OWNER privilege.
569 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
570 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
571 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
578 * Get a new unique fsid. Try to make its val[0] unique, since this value
579 * will be used to create fake device numbers for stat(). Also try (but
580 * not so hard) make its val[0] unique mod 2^16, since some emulators only
581 * support 16-bit device numbers. We end up with unique val[0]'s for the
582 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
584 * Keep in mind that several mounts may be running in parallel. Starting
585 * the search one past where the previous search terminated is both a
586 * micro-optimization and a defense against returning the same fsid to
590 vfs_getnewfsid(struct mount *mp)
592 static uint16_t mntid_base;
597 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
598 mtx_lock(&mntid_mtx);
599 mtype = mp->mnt_vfc->vfc_typenum;
600 tfsid.val[1] = mtype;
601 mtype = (mtype & 0xFF) << 24;
603 tfsid.val[0] = makedev(255,
604 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
606 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
610 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
611 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
612 mtx_unlock(&mntid_mtx);
616 * Knob to control the precision of file timestamps:
618 * 0 = seconds only; nanoseconds zeroed.
619 * 1 = seconds and nanoseconds, accurate within 1/HZ.
620 * 2 = seconds and nanoseconds, truncated to microseconds.
621 * >=3 = seconds and nanoseconds, maximum precision.
623 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
625 static int timestamp_precision = TSP_USEC;
626 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
627 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
628 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
629 "3+: sec + ns (max. precision))");
632 * Get a current timestamp.
635 vfs_timestamp(struct timespec *tsp)
639 switch (timestamp_precision) {
641 tsp->tv_sec = time_second;
649 TIMEVAL_TO_TIMESPEC(&tv, tsp);
659 * Set vnode attributes to VNOVAL
662 vattr_null(struct vattr *vap)
666 vap->va_size = VNOVAL;
667 vap->va_bytes = VNOVAL;
668 vap->va_mode = VNOVAL;
669 vap->va_nlink = VNOVAL;
670 vap->va_uid = VNOVAL;
671 vap->va_gid = VNOVAL;
672 vap->va_fsid = VNOVAL;
673 vap->va_fileid = VNOVAL;
674 vap->va_blocksize = VNOVAL;
675 vap->va_rdev = VNOVAL;
676 vap->va_atime.tv_sec = VNOVAL;
677 vap->va_atime.tv_nsec = VNOVAL;
678 vap->va_mtime.tv_sec = VNOVAL;
679 vap->va_mtime.tv_nsec = VNOVAL;
680 vap->va_ctime.tv_sec = VNOVAL;
681 vap->va_ctime.tv_nsec = VNOVAL;
682 vap->va_birthtime.tv_sec = VNOVAL;
683 vap->va_birthtime.tv_nsec = VNOVAL;
684 vap->va_flags = VNOVAL;
685 vap->va_gen = VNOVAL;
690 * This routine is called when we have too many vnodes. It attempts
691 * to free <count> vnodes and will potentially free vnodes that still
692 * have VM backing store (VM backing store is typically the cause
693 * of a vnode blowout so we want to do this). Therefore, this operation
694 * is not considered cheap.
696 * A number of conditions may prevent a vnode from being reclaimed.
697 * the buffer cache may have references on the vnode, a directory
698 * vnode may still have references due to the namei cache representing
699 * underlying files, or the vnode may be in active use. It is not
700 * desireable to reuse such vnodes. These conditions may cause the
701 * number of vnodes to reach some minimum value regardless of what
702 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
705 vlrureclaim(struct mount *mp)
714 * Calculate the trigger point, don't allow user
715 * screwups to blow us up. This prevents us from
716 * recycling vnodes with lots of resident pages. We
717 * aren't trying to free memory, we are trying to
720 usevnodes = desiredvnodes;
723 trigger = cnt.v_page_count * 2 / usevnodes;
725 vn_start_write(NULL, &mp, V_WAIT);
727 count = mp->mnt_nvnodelistsize / 10 + 1;
729 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
730 while (vp != NULL && vp->v_type == VMARKER)
731 vp = TAILQ_NEXT(vp, v_nmntvnodes);
734 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
735 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
740 * If it's been deconstructed already, it's still
741 * referenced, or it exceeds the trigger, skip it.
743 if (vp->v_usecount ||
744 (!vlru_allow_cache_src &&
745 !LIST_EMPTY(&(vp)->v_cache_src)) ||
746 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
747 vp->v_object->resident_page_count > trigger)) {
753 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
755 goto next_iter_mntunlocked;
759 * v_usecount may have been bumped after VOP_LOCK() dropped
760 * the vnode interlock and before it was locked again.
762 * It is not necessary to recheck VI_DOOMED because it can
763 * only be set by another thread that holds both the vnode
764 * lock and vnode interlock. If another thread has the
765 * vnode lock before we get to VOP_LOCK() and obtains the
766 * vnode interlock after VOP_LOCK() drops the vnode
767 * interlock, the other thread will be unable to drop the
768 * vnode lock before our VOP_LOCK() call fails.
770 if (vp->v_usecount ||
771 (!vlru_allow_cache_src &&
772 !LIST_EMPTY(&(vp)->v_cache_src)) ||
773 (vp->v_object != NULL &&
774 vp->v_object->resident_page_count > trigger)) {
775 VOP_UNLOCK(vp, LK_INTERLOCK);
776 goto next_iter_mntunlocked;
778 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
779 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
780 atomic_add_long(&recycles_count, 1);
785 next_iter_mntunlocked:
794 kern_yield(PRI_UNCHANGED);
799 vn_finished_write(mp);
804 * Attempt to keep the free list at wantfreevnodes length.
807 vnlru_free(int count)
812 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
813 for (; count > 0; count--) {
814 vp = TAILQ_FIRST(&vnode_free_list);
816 * The list can be modified while the free_list_mtx
817 * has been dropped and vp could be NULL here.
821 VNASSERT(vp->v_op != NULL, vp,
822 ("vnlru_free: vnode already reclaimed."));
823 KASSERT((vp->v_iflag & VI_FREE) != 0,
824 ("Removing vnode not on freelist"));
825 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
826 ("Mangling active vnode"));
827 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
829 * Don't recycle if we can't get the interlock.
831 if (!VI_TRYLOCK(vp)) {
832 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
835 VNASSERT(VCANRECYCLE(vp), vp,
836 ("vp inconsistent on freelist"));
838 vp->v_iflag &= ~VI_FREE;
840 mtx_unlock(&vnode_free_list_mtx);
842 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
844 VFS_UNLOCK_GIANT(vfslocked);
846 * If the recycled succeeded this vdrop will actually free
847 * the vnode. If not it will simply place it back on
851 mtx_lock(&vnode_free_list_mtx);
855 * Attempt to recycle vnodes in a context that is always safe to block.
856 * Calling vlrurecycle() from the bowels of filesystem code has some
857 * interesting deadlock problems.
859 static struct proc *vnlruproc;
860 static int vnlruproc_sig;
865 struct mount *mp, *nmp;
867 struct proc *p = vnlruproc;
869 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
873 kproc_suspend_check(p);
874 mtx_lock(&vnode_free_list_mtx);
875 if (freevnodes > wantfreevnodes)
876 vnlru_free(freevnodes - wantfreevnodes);
877 if (numvnodes <= desiredvnodes * 9 / 10) {
879 wakeup(&vnlruproc_sig);
880 msleep(vnlruproc, &vnode_free_list_mtx,
881 PVFS|PDROP, "vlruwt", hz);
884 mtx_unlock(&vnode_free_list_mtx);
886 mtx_lock(&mountlist_mtx);
887 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
888 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
889 nmp = TAILQ_NEXT(mp, mnt_list);
892 vfslocked = VFS_LOCK_GIANT(mp);
893 done += vlrureclaim(mp);
894 VFS_UNLOCK_GIANT(vfslocked);
895 mtx_lock(&mountlist_mtx);
896 nmp = TAILQ_NEXT(mp, mnt_list);
899 mtx_unlock(&mountlist_mtx);
902 /* These messages are temporary debugging aids */
903 if (vnlru_nowhere < 5)
904 printf("vnlru process getting nowhere..\n");
905 else if (vnlru_nowhere == 5)
906 printf("vnlru process messages stopped.\n");
909 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
911 kern_yield(PRI_UNCHANGED);
915 static struct kproc_desc vnlru_kp = {
920 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
924 * Routines having to do with the management of the vnode table.
928 * Try to recycle a freed vnode. We abort if anyone picks up a reference
929 * before we actually vgone(). This function must be called with the vnode
930 * held to prevent the vnode from being returned to the free list midway
934 vtryrecycle(struct vnode *vp)
938 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
939 VNASSERT(vp->v_holdcnt, vp,
940 ("vtryrecycle: Recycling vp %p without a reference.", vp));
942 * This vnode may found and locked via some other list, if so we
943 * can't recycle it yet.
945 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
947 "%s: impossible to recycle, vp %p lock is already held",
949 return (EWOULDBLOCK);
952 * Don't recycle if its filesystem is being suspended.
954 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
957 "%s: impossible to recycle, cannot start the write for %p",
962 * If we got this far, we need to acquire the interlock and see if
963 * anyone picked up this vnode from another list. If not, we will
964 * mark it with DOOMED via vgonel() so that anyone who does find it
968 if (vp->v_usecount) {
969 VOP_UNLOCK(vp, LK_INTERLOCK);
970 vn_finished_write(vnmp);
972 "%s: impossible to recycle, %p is already referenced",
976 if ((vp->v_iflag & VI_DOOMED) == 0) {
977 atomic_add_long(&recycles_count, 1);
980 VOP_UNLOCK(vp, LK_INTERLOCK);
981 vn_finished_write(vnmp);
986 * Wait for available vnodes.
989 getnewvnode_wait(int suspended)
992 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
993 if (numvnodes > desiredvnodes) {
996 * File system is beeing suspended, we cannot risk a
997 * deadlock here, so allocate new vnode anyway.
999 if (freevnodes > wantfreevnodes)
1000 vnlru_free(freevnodes - wantfreevnodes);
1003 if (vnlruproc_sig == 0) {
1004 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
1007 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
1010 return (numvnodes > desiredvnodes ? ENFILE : 0);
1014 getnewvnode_reserve(u_int count)
1019 /* First try to be quick and racy. */
1020 if (atomic_fetchadd_long(&numvnodes, count) + count <= desiredvnodes) {
1021 td->td_vp_reserv += count;
1024 atomic_subtract_long(&numvnodes, count);
1026 mtx_lock(&vnode_free_list_mtx);
1028 if (getnewvnode_wait(0) == 0) {
1031 atomic_add_long(&numvnodes, 1);
1034 mtx_unlock(&vnode_free_list_mtx);
1038 getnewvnode_drop_reserve(void)
1043 atomic_subtract_long(&numvnodes, td->td_vp_reserv);
1044 td->td_vp_reserv = 0;
1048 * Return the next vnode from the free list.
1051 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
1059 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
1062 if (td->td_vp_reserv > 0) {
1063 td->td_vp_reserv -= 1;
1066 mtx_lock(&vnode_free_list_mtx);
1068 * Lend our context to reclaim vnodes if they've exceeded the max.
1070 if (freevnodes > wantfreevnodes)
1072 error = getnewvnode_wait(mp != NULL && (mp->mnt_kern_flag &
1074 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
1076 mtx_unlock(&vnode_free_list_mtx);
1080 atomic_add_long(&numvnodes, 1);
1081 mtx_unlock(&vnode_free_list_mtx);
1083 atomic_add_long(&vnodes_created, 1);
1084 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
1088 vp->v_vnlock = &vp->v_lock;
1089 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
1091 * By default, don't allow shared locks unless filesystems
1094 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
1096 * Initialize bufobj.
1099 bo->__bo_vnode = vp;
1100 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
1101 bo->bo_ops = &buf_ops_bio;
1102 bo->bo_private = vp;
1103 TAILQ_INIT(&bo->bo_clean.bv_hd);
1104 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1106 * Initialize namecache.
1108 LIST_INIT(&vp->v_cache_src);
1109 TAILQ_INIT(&vp->v_cache_dst);
1111 * Finalize various vnode identity bits.
1116 v_incr_usecount(vp);
1120 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1121 mac_vnode_associate_singlelabel(mp, vp);
1122 else if (mp == NULL && vops != &dead_vnodeops)
1123 printf("NULL mp in getnewvnode()\n");
1126 bo->bo_bsize = mp->mnt_stat.f_iosize;
1127 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1128 vp->v_vflag |= VV_NOKNOTE;
1130 rangelock_init(&vp->v_rl);
1133 * For the filesystems which do not use vfs_hash_insert(),
1134 * still initialize v_hash to have vfs_hash_index() useful.
1135 * E.g., nullfs uses vfs_hash_index() on the lower vnode for
1138 vp->v_hash = (uintptr_t)vp >> vnsz2log;
1145 * Delete from old mount point vnode list, if on one.
1148 delmntque(struct vnode *vp)
1158 KASSERT(mp->mnt_activevnodelistsize <= mp->mnt_nvnodelistsize,
1159 ("Active vnode list size %d > Vnode list size %d",
1160 mp->mnt_activevnodelistsize, mp->mnt_nvnodelistsize));
1161 active = vp->v_iflag & VI_ACTIVE;
1162 vp->v_iflag &= ~VI_ACTIVE;
1164 mtx_lock(&vnode_free_list_mtx);
1165 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp, v_actfreelist);
1166 mp->mnt_activevnodelistsize--;
1167 mtx_unlock(&vnode_free_list_mtx);
1171 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1172 ("bad mount point vnode list size"));
1173 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1174 mp->mnt_nvnodelistsize--;
1180 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1184 vp->v_op = &dead_vnodeops;
1185 /* XXX non mp-safe fs may still call insmntque with vnode
1187 if (!VOP_ISLOCKED(vp))
1188 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1194 * Insert into list of vnodes for the new mount point, if available.
1197 insmntque1(struct vnode *vp, struct mount *mp,
1198 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1202 KASSERT(vp->v_mount == NULL,
1203 ("insmntque: vnode already on per mount vnode list"));
1204 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1205 #ifdef DEBUG_VFS_LOCKS
1206 if (!VFS_NEEDSGIANT(mp))
1207 ASSERT_VOP_ELOCKED(vp,
1208 "insmntque: mp-safe fs and non-locked vp");
1211 * We acquire the vnode interlock early to ensure that the
1212 * vnode cannot be recycled by another process releasing a
1213 * holdcnt on it before we get it on both the vnode list
1214 * and the active vnode list. The mount mutex protects only
1215 * manipulation of the vnode list and the vnode freelist
1216 * mutex protects only manipulation of the active vnode list.
1217 * Hence the need to hold the vnode interlock throughout.
1221 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1222 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1223 mp->mnt_nvnodelistsize == 0)) {
1224 locked = VOP_ISLOCKED(vp);
1225 if (!locked || (locked == LK_EXCLUSIVE &&
1226 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1236 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1237 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1238 ("neg mount point vnode list size"));
1239 mp->mnt_nvnodelistsize++;
1240 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
1241 ("Activating already active vnode"));
1242 vp->v_iflag |= VI_ACTIVE;
1243 mtx_lock(&vnode_free_list_mtx);
1244 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
1245 mp->mnt_activevnodelistsize++;
1246 mtx_unlock(&vnode_free_list_mtx);
1253 insmntque(struct vnode *vp, struct mount *mp)
1256 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1260 * Flush out and invalidate all buffers associated with a bufobj
1261 * Called with the underlying object locked.
1264 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1269 if (flags & V_SAVE) {
1270 error = bufobj_wwait(bo, slpflag, slptimeo);
1275 if (bo->bo_dirty.bv_cnt > 0) {
1277 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1280 * XXX We could save a lock/unlock if this was only
1281 * enabled under INVARIANTS
1284 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1285 panic("vinvalbuf: dirty bufs");
1289 * If you alter this loop please notice that interlock is dropped and
1290 * reacquired in flushbuflist. Special care is needed to ensure that
1291 * no race conditions occur from this.
1294 error = flushbuflist(&bo->bo_clean,
1295 flags, bo, slpflag, slptimeo);
1296 if (error == 0 && !(flags & V_CLEANONLY))
1297 error = flushbuflist(&bo->bo_dirty,
1298 flags, bo, slpflag, slptimeo);
1299 if (error != 0 && error != EAGAIN) {
1303 } while (error != 0);
1306 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1307 * have write I/O in-progress but if there is a VM object then the
1308 * VM object can also have read-I/O in-progress.
1311 bufobj_wwait(bo, 0, 0);
1313 if (bo->bo_object != NULL) {
1314 VM_OBJECT_LOCK(bo->bo_object);
1315 vm_object_pip_wait(bo->bo_object, "bovlbx");
1316 VM_OBJECT_UNLOCK(bo->bo_object);
1319 } while (bo->bo_numoutput > 0);
1323 * Destroy the copy in the VM cache, too.
1325 if (bo->bo_object != NULL &&
1326 (flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0) {
1327 VM_OBJECT_LOCK(bo->bo_object);
1328 vm_object_page_remove(bo->bo_object, 0, 0, (flags & V_SAVE) ?
1329 OBJPR_CLEANONLY : 0);
1330 VM_OBJECT_UNLOCK(bo->bo_object);
1335 if ((flags & (V_ALT | V_NORMAL | V_CLEANONLY)) == 0 &&
1336 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1337 panic("vinvalbuf: flush failed");
1344 * Flush out and invalidate all buffers associated with a vnode.
1345 * Called with the underlying object locked.
1348 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1351 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1352 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1353 if (vp->v_object != NULL && vp->v_object->handle != vp)
1355 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1359 * Flush out buffers on the specified list.
1363 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1366 struct buf *bp, *nbp;
1371 ASSERT_BO_LOCKED(bo);
1374 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1375 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1376 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1382 lblkno = nbp->b_lblkno;
1383 xflags = nbp->b_xflags &
1384 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1387 error = BUF_TIMELOCK(bp,
1388 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1389 "flushbuf", slpflag, slptimeo);
1392 return (error != ENOLCK ? error : EAGAIN);
1394 KASSERT(bp->b_bufobj == bo,
1395 ("bp %p wrong b_bufobj %p should be %p",
1396 bp, bp->b_bufobj, bo));
1397 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1403 * XXX Since there are no node locks for NFS, I
1404 * believe there is a slight chance that a delayed
1405 * write will occur while sleeping just above, so
1408 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1413 bp->b_flags |= B_ASYNC;
1416 return (EAGAIN); /* XXX: why not loop ? */
1421 bp->b_flags |= (B_INVAL | B_RELBUF);
1422 bp->b_flags &= ~B_ASYNC;
1426 (nbp->b_bufobj != bo ||
1427 nbp->b_lblkno != lblkno ||
1429 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1430 break; /* nbp invalid */
1436 * Truncate a file's buffer and pages to a specified length. This
1437 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1441 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1442 off_t length, int blksize)
1444 struct buf *bp, *nbp;
1449 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1450 vp, cred, blksize, (uintmax_t)length);
1453 * Round up to the *next* lbn.
1455 trunclbn = (length + blksize - 1) / blksize;
1457 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1464 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1465 if (bp->b_lblkno < trunclbn)
1468 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1469 BO_MTX(bo)) == ENOLCK)
1475 bp->b_flags |= (B_INVAL | B_RELBUF);
1476 bp->b_flags &= ~B_ASYNC;
1482 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1483 (nbp->b_vp != vp) ||
1484 (nbp->b_flags & B_DELWRI))) {
1490 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1491 if (bp->b_lblkno < trunclbn)
1494 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1495 BO_MTX(bo)) == ENOLCK)
1500 bp->b_flags |= (B_INVAL | B_RELBUF);
1501 bp->b_flags &= ~B_ASYNC;
1507 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1508 (nbp->b_vp != vp) ||
1509 (nbp->b_flags & B_DELWRI) == 0)) {
1518 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1519 if (bp->b_lblkno > 0)
1522 * Since we hold the vnode lock this should only
1523 * fail if we're racing with the buf daemon.
1526 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1527 BO_MTX(bo)) == ENOLCK) {
1530 VNASSERT((bp->b_flags & B_DELWRI), vp,
1531 ("buf(%p) on dirty queue without DELWRI", bp));
1542 bufobj_wwait(bo, 0, 0);
1544 vnode_pager_setsize(vp, length);
1550 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1553 * NOTE: We have to deal with the special case of a background bitmap
1554 * buffer, a situation where two buffers will have the same logical
1555 * block offset. We want (1) only the foreground buffer to be accessed
1556 * in a lookup and (2) must differentiate between the foreground and
1557 * background buffer in the splay tree algorithm because the splay
1558 * tree cannot normally handle multiple entities with the same 'index'.
1559 * We accomplish this by adding differentiating flags to the splay tree's
1564 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1567 struct buf *lefttreemax, *righttreemin, *y;
1571 lefttreemax = righttreemin = &dummy;
1573 if (lblkno < root->b_lblkno ||
1574 (lblkno == root->b_lblkno &&
1575 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1576 if ((y = root->b_left) == NULL)
1578 if (lblkno < y->b_lblkno) {
1580 root->b_left = y->b_right;
1583 if ((y = root->b_left) == NULL)
1586 /* Link into the new root's right tree. */
1587 righttreemin->b_left = root;
1588 righttreemin = root;
1589 } else if (lblkno > root->b_lblkno ||
1590 (lblkno == root->b_lblkno &&
1591 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1592 if ((y = root->b_right) == NULL)
1594 if (lblkno > y->b_lblkno) {
1596 root->b_right = y->b_left;
1599 if ((y = root->b_right) == NULL)
1602 /* Link into the new root's left tree. */
1603 lefttreemax->b_right = root;
1610 /* Assemble the new root. */
1611 lefttreemax->b_right = root->b_left;
1612 righttreemin->b_left = root->b_right;
1613 root->b_left = dummy.b_right;
1614 root->b_right = dummy.b_left;
1619 buf_vlist_remove(struct buf *bp)
1624 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1625 ASSERT_BO_LOCKED(bp->b_bufobj);
1626 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1627 (BX_VNDIRTY|BX_VNCLEAN),
1628 ("buf_vlist_remove: Buf %p is on two lists", bp));
1629 if (bp->b_xflags & BX_VNDIRTY)
1630 bv = &bp->b_bufobj->bo_dirty;
1632 bv = &bp->b_bufobj->bo_clean;
1633 if (bp != bv->bv_root) {
1634 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1635 KASSERT(root == bp, ("splay lookup failed in remove"));
1637 if (bp->b_left == NULL) {
1640 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1641 root->b_right = bp->b_right;
1644 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1646 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1650 * Add the buffer to the sorted clean or dirty block list using a
1651 * splay tree algorithm.
1653 * NOTE: xflags is passed as a constant, optimizing this inline function!
1656 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1661 ASSERT_BO_LOCKED(bo);
1662 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1663 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1664 bp->b_xflags |= xflags;
1665 if (xflags & BX_VNDIRTY)
1670 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1674 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1675 } else if (bp->b_lblkno < root->b_lblkno ||
1676 (bp->b_lblkno == root->b_lblkno &&
1677 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1678 bp->b_left = root->b_left;
1680 root->b_left = NULL;
1681 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1683 bp->b_right = root->b_right;
1685 root->b_right = NULL;
1686 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1693 * Lookup a buffer using the splay tree. Note that we specifically avoid
1694 * shadow buffers used in background bitmap writes.
1696 * This code isn't quite efficient as it could be because we are maintaining
1697 * two sorted lists and do not know which list the block resides in.
1699 * During a "make buildworld" the desired buffer is found at one of
1700 * the roots more than 60% of the time. Thus, checking both roots
1701 * before performing either splay eliminates unnecessary splays on the
1702 * first tree splayed.
1705 gbincore(struct bufobj *bo, daddr_t lblkno)
1709 ASSERT_BO_LOCKED(bo);
1710 if ((bp = bo->bo_clean.bv_root) != NULL &&
1711 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1713 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1714 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1716 if ((bp = bo->bo_clean.bv_root) != NULL) {
1717 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1718 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1721 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1722 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1723 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1730 * Associate a buffer with a vnode.
1733 bgetvp(struct vnode *vp, struct buf *bp)
1738 ASSERT_BO_LOCKED(bo);
1739 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1741 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1742 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1743 ("bgetvp: bp already attached! %p", bp));
1746 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1747 bp->b_flags |= B_NEEDSGIANT;
1751 * Insert onto list for new vnode.
1753 buf_vlist_add(bp, bo, BX_VNCLEAN);
1757 * Disassociate a buffer from a vnode.
1760 brelvp(struct buf *bp)
1765 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1766 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1769 * Delete from old vnode list, if on one.
1771 vp = bp->b_vp; /* XXX */
1774 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1775 buf_vlist_remove(bp);
1777 panic("brelvp: Buffer %p not on queue.", bp);
1778 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1779 bo->bo_flag &= ~BO_ONWORKLST;
1780 mtx_lock(&sync_mtx);
1781 LIST_REMOVE(bo, bo_synclist);
1782 syncer_worklist_len--;
1783 mtx_unlock(&sync_mtx);
1785 bp->b_flags &= ~B_NEEDSGIANT;
1787 bp->b_bufobj = NULL;
1793 * Add an item to the syncer work queue.
1796 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1800 ASSERT_BO_LOCKED(bo);
1802 mtx_lock(&sync_mtx);
1803 if (bo->bo_flag & BO_ONWORKLST)
1804 LIST_REMOVE(bo, bo_synclist);
1806 bo->bo_flag |= BO_ONWORKLST;
1807 syncer_worklist_len++;
1810 if (delay > syncer_maxdelay - 2)
1811 delay = syncer_maxdelay - 2;
1812 slot = (syncer_delayno + delay) & syncer_mask;
1814 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1816 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1818 mtx_unlock(&sync_mtx);
1822 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1826 mtx_lock(&sync_mtx);
1827 len = syncer_worklist_len - sync_vnode_count;
1828 mtx_unlock(&sync_mtx);
1829 error = SYSCTL_OUT(req, &len, sizeof(len));
1833 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1834 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1836 static struct proc *updateproc;
1837 static void sched_sync(void);
1838 static struct kproc_desc up_kp = {
1843 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1846 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1851 *bo = LIST_FIRST(slp);
1854 vp = (*bo)->__bo_vnode; /* XXX */
1855 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1858 * We use vhold in case the vnode does not
1859 * successfully sync. vhold prevents the vnode from
1860 * going away when we unlock the sync_mtx so that
1861 * we can acquire the vnode interlock.
1864 mtx_unlock(&sync_mtx);
1866 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1868 mtx_lock(&sync_mtx);
1869 return (*bo == LIST_FIRST(slp));
1871 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1872 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1874 vn_finished_write(mp);
1876 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1878 * Put us back on the worklist. The worklist
1879 * routine will remove us from our current
1880 * position and then add us back in at a later
1883 vn_syncer_add_to_worklist(*bo, syncdelay);
1887 mtx_lock(&sync_mtx);
1892 * System filesystem synchronizer daemon.
1897 struct synclist *gnext, *next;
1898 struct synclist *gslp, *slp;
1901 struct thread *td = curthread;
1903 int net_worklist_len;
1904 int syncer_final_iter;
1909 syncer_final_iter = 0;
1911 syncer_state = SYNCER_RUNNING;
1912 starttime = time_uptime;
1913 td->td_pflags |= TDP_NORUNNINGBUF;
1915 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1918 mtx_lock(&sync_mtx);
1920 if (syncer_state == SYNCER_FINAL_DELAY &&
1921 syncer_final_iter == 0) {
1922 mtx_unlock(&sync_mtx);
1923 kproc_suspend_check(td->td_proc);
1924 mtx_lock(&sync_mtx);
1926 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1927 if (syncer_state != SYNCER_RUNNING &&
1928 starttime != time_uptime) {
1930 printf("\nSyncing disks, vnodes remaining...");
1933 printf("%d ", net_worklist_len);
1935 starttime = time_uptime;
1938 * Push files whose dirty time has expired. Be careful
1939 * of interrupt race on slp queue.
1941 * Skip over empty worklist slots when shutting down.
1944 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1945 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1946 syncer_delayno += 1;
1947 if (syncer_delayno == syncer_maxdelay)
1949 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1950 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1952 * If the worklist has wrapped since the
1953 * it was emptied of all but syncer vnodes,
1954 * switch to the FINAL_DELAY state and run
1955 * for one more second.
1957 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1958 net_worklist_len == 0 &&
1959 last_work_seen == syncer_delayno) {
1960 syncer_state = SYNCER_FINAL_DELAY;
1961 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1963 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1964 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1967 * Keep track of the last time there was anything
1968 * on the worklist other than syncer vnodes.
1969 * Return to the SHUTTING_DOWN state if any
1972 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1973 last_work_seen = syncer_delayno;
1974 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1975 syncer_state = SYNCER_SHUTTING_DOWN;
1976 while (!LIST_EMPTY(slp)) {
1977 error = sync_vnode(slp, &bo, td);
1979 LIST_REMOVE(bo, bo_synclist);
1980 LIST_INSERT_HEAD(next, bo, bo_synclist);
1984 if (first_printf == 0)
1985 wdog_kern_pat(WD_LASTVAL);
1988 if (!LIST_EMPTY(gslp)) {
1989 mtx_unlock(&sync_mtx);
1991 mtx_lock(&sync_mtx);
1992 while (!LIST_EMPTY(gslp)) {
1993 error = sync_vnode(gslp, &bo, td);
1995 LIST_REMOVE(bo, bo_synclist);
1996 LIST_INSERT_HEAD(gnext, bo,
2003 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
2004 syncer_final_iter--;
2006 * The variable rushjob allows the kernel to speed up the
2007 * processing of the filesystem syncer process. A rushjob
2008 * value of N tells the filesystem syncer to process the next
2009 * N seconds worth of work on its queue ASAP. Currently rushjob
2010 * is used by the soft update code to speed up the filesystem
2011 * syncer process when the incore state is getting so far
2012 * ahead of the disk that the kernel memory pool is being
2013 * threatened with exhaustion.
2020 * Just sleep for a short period of time between
2021 * iterations when shutting down to allow some I/O
2024 * If it has taken us less than a second to process the
2025 * current work, then wait. Otherwise start right over
2026 * again. We can still lose time if any single round
2027 * takes more than two seconds, but it does not really
2028 * matter as we are just trying to generally pace the
2029 * filesystem activity.
2031 if (syncer_state != SYNCER_RUNNING ||
2032 time_uptime == starttime) {
2034 sched_prio(td, PPAUSE);
2037 if (syncer_state != SYNCER_RUNNING)
2038 cv_timedwait(&sync_wakeup, &sync_mtx,
2039 hz / SYNCER_SHUTDOWN_SPEEDUP);
2040 else if (time_uptime == starttime)
2041 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
2046 * Request the syncer daemon to speed up its work.
2047 * We never push it to speed up more than half of its
2048 * normal turn time, otherwise it could take over the cpu.
2051 speedup_syncer(void)
2055 mtx_lock(&sync_mtx);
2056 if (rushjob < syncdelay / 2) {
2058 stat_rush_requests += 1;
2061 mtx_unlock(&sync_mtx);
2062 cv_broadcast(&sync_wakeup);
2067 * Tell the syncer to speed up its work and run though its work
2068 * list several times, then tell it to shut down.
2071 syncer_shutdown(void *arg, int howto)
2074 if (howto & RB_NOSYNC)
2076 mtx_lock(&sync_mtx);
2077 syncer_state = SYNCER_SHUTTING_DOWN;
2079 mtx_unlock(&sync_mtx);
2080 cv_broadcast(&sync_wakeup);
2081 kproc_shutdown(arg, howto);
2085 * Reassign a buffer from one vnode to another.
2086 * Used to assign file specific control information
2087 * (indirect blocks) to the vnode to which they belong.
2090 reassignbuf(struct buf *bp)
2103 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
2104 bp, bp->b_vp, bp->b_flags);
2106 * B_PAGING flagged buffers cannot be reassigned because their vp
2107 * is not fully linked in.
2109 if (bp->b_flags & B_PAGING)
2110 panic("cannot reassign paging buffer");
2113 * Delete from old vnode list, if on one.
2116 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
2117 buf_vlist_remove(bp);
2119 panic("reassignbuf: Buffer %p not on queue.", bp);
2121 * If dirty, put on list of dirty buffers; otherwise insert onto list
2124 if (bp->b_flags & B_DELWRI) {
2125 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
2126 switch (vp->v_type) {
2136 vn_syncer_add_to_worklist(bo, delay);
2138 buf_vlist_add(bp, bo, BX_VNDIRTY);
2140 buf_vlist_add(bp, bo, BX_VNCLEAN);
2142 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2143 mtx_lock(&sync_mtx);
2144 LIST_REMOVE(bo, bo_synclist);
2145 syncer_worklist_len--;
2146 mtx_unlock(&sync_mtx);
2147 bo->bo_flag &= ~BO_ONWORKLST;
2152 bp = TAILQ_FIRST(&bv->bv_hd);
2153 KASSERT(bp == NULL || bp->b_bufobj == bo,
2154 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2155 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2156 KASSERT(bp == NULL || bp->b_bufobj == bo,
2157 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2159 bp = TAILQ_FIRST(&bv->bv_hd);
2160 KASSERT(bp == NULL || bp->b_bufobj == bo,
2161 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2162 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2163 KASSERT(bp == NULL || bp->b_bufobj == bo,
2164 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2170 * Increment the use and hold counts on the vnode, taking care to reference
2171 * the driver's usecount if this is a chardev. The vholdl() will remove
2172 * the vnode from the free list if it is presently free. Requires the
2173 * vnode interlock and returns with it held.
2176 v_incr_usecount(struct vnode *vp)
2179 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2181 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2183 vp->v_rdev->si_usecount++;
2190 * Turn a holdcnt into a use+holdcnt such that only one call to
2191 * v_decr_usecount is needed.
2194 v_upgrade_usecount(struct vnode *vp)
2197 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2199 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2201 vp->v_rdev->si_usecount++;
2207 * Decrement the vnode use and hold count along with the driver's usecount
2208 * if this is a chardev. The vdropl() below releases the vnode interlock
2209 * as it may free the vnode.
2212 v_decr_usecount(struct vnode *vp)
2215 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2216 VNASSERT(vp->v_usecount > 0, vp,
2217 ("v_decr_usecount: negative usecount"));
2218 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2220 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2222 vp->v_rdev->si_usecount--;
2229 * Decrement only the use count and driver use count. This is intended to
2230 * be paired with a follow on vdropl() to release the remaining hold count.
2231 * In this way we may vgone() a vnode with a 0 usecount without risk of
2232 * having it end up on a free list because the hold count is kept above 0.
2235 v_decr_useonly(struct vnode *vp)
2238 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2239 VNASSERT(vp->v_usecount > 0, vp,
2240 ("v_decr_useonly: negative usecount"));
2241 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2243 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2245 vp->v_rdev->si_usecount--;
2251 * Grab a particular vnode from the free list, increment its
2252 * reference count and lock it. VI_DOOMED is set if the vnode
2253 * is being destroyed. Only callers who specify LK_RETRY will
2254 * see doomed vnodes. If inactive processing was delayed in
2255 * vput try to do it here.
2258 vget(struct vnode *vp, int flags, struct thread *td)
2263 VFS_ASSERT_GIANT(vp->v_mount);
2264 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2265 ("vget: invalid lock operation"));
2266 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2268 if ((flags & LK_INTERLOCK) == 0)
2271 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2273 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2277 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2278 panic("vget: vn_lock failed to return ENOENT\n");
2280 /* Upgrade our holdcnt to a usecount. */
2281 v_upgrade_usecount(vp);
2283 * We don't guarantee that any particular close will
2284 * trigger inactive processing so just make a best effort
2285 * here at preventing a reference to a removed file. If
2286 * we don't succeed no harm is done.
2288 if (vp->v_iflag & VI_OWEINACT) {
2289 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2290 (flags & LK_NOWAIT) == 0)
2292 vp->v_iflag &= ~VI_OWEINACT;
2299 * Increase the reference count of a vnode.
2302 vref(struct vnode *vp)
2305 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2307 v_incr_usecount(vp);
2312 * Return reference count of a vnode.
2314 * The results of this call are only guaranteed when some mechanism other
2315 * than the VI lock is used to stop other processes from gaining references
2316 * to the vnode. This may be the case if the caller holds the only reference.
2317 * This is also useful when stale data is acceptable as race conditions may
2318 * be accounted for by some other means.
2321 vrefcnt(struct vnode *vp)
2326 usecnt = vp->v_usecount;
2332 #define VPUTX_VRELE 1
2333 #define VPUTX_VPUT 2
2334 #define VPUTX_VUNREF 3
2337 vputx(struct vnode *vp, int func)
2341 KASSERT(vp != NULL, ("vputx: null vp"));
2342 if (func == VPUTX_VUNREF)
2343 ASSERT_VOP_LOCKED(vp, "vunref");
2344 else if (func == VPUTX_VPUT)
2345 ASSERT_VOP_LOCKED(vp, "vput");
2347 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2348 VFS_ASSERT_GIANT(vp->v_mount);
2349 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2352 /* Skip this v_writecount check if we're going to panic below. */
2353 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2354 ("vputx: missed vn_close"));
2357 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2358 vp->v_usecount == 1)) {
2359 if (func == VPUTX_VPUT)
2361 v_decr_usecount(vp);
2365 if (vp->v_usecount != 1) {
2366 vprint("vputx: negative ref count", vp);
2367 panic("vputx: negative ref cnt");
2369 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2371 * We want to hold the vnode until the inactive finishes to
2372 * prevent vgone() races. We drop the use count here and the
2373 * hold count below when we're done.
2377 * We must call VOP_INACTIVE with the node locked. Mark
2378 * as VI_DOINGINACT to avoid recursion.
2380 vp->v_iflag |= VI_OWEINACT;
2383 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2387 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2388 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2394 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2395 error = VOP_LOCK(vp, LK_TRYUPGRADE | LK_INTERLOCK);
2400 if (vp->v_usecount > 0)
2401 vp->v_iflag &= ~VI_OWEINACT;
2403 if (vp->v_iflag & VI_OWEINACT)
2404 vinactive(vp, curthread);
2405 if (func != VPUTX_VUNREF)
2412 * Vnode put/release.
2413 * If count drops to zero, call inactive routine and return to freelist.
2416 vrele(struct vnode *vp)
2419 vputx(vp, VPUTX_VRELE);
2423 * Release an already locked vnode. This give the same effects as
2424 * unlock+vrele(), but takes less time and avoids releasing and
2425 * re-aquiring the lock (as vrele() acquires the lock internally.)
2428 vput(struct vnode *vp)
2431 vputx(vp, VPUTX_VPUT);
2435 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2438 vunref(struct vnode *vp)
2441 vputx(vp, VPUTX_VUNREF);
2445 * Somebody doesn't want the vnode recycled.
2448 vhold(struct vnode *vp)
2457 * Increase the hold count and activate if this is the first reference.
2460 vholdl(struct vnode *vp)
2464 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2466 if (!VSHOULDBUSY(vp))
2468 ASSERT_VI_LOCKED(vp, "vholdl");
2469 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
2470 VNASSERT(vp->v_op != NULL, vp, ("vholdl: vnode already reclaimed."));
2472 * Remove a vnode from the free list, mark it as in use,
2473 * and put it on the active list.
2475 mtx_lock(&vnode_free_list_mtx);
2476 TAILQ_REMOVE(&vnode_free_list, vp, v_actfreelist);
2478 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2479 KASSERT((vp->v_iflag & VI_ACTIVE) == 0,
2480 ("Activating already active vnode"));
2481 vp->v_iflag |= VI_ACTIVE;
2483 TAILQ_INSERT_HEAD(&mp->mnt_activevnodelist, vp, v_actfreelist);
2484 mp->mnt_activevnodelistsize++;
2485 mtx_unlock(&vnode_free_list_mtx);
2489 * Note that there is one less who cares about this vnode.
2490 * vdrop() is the opposite of vhold().
2493 vdrop(struct vnode *vp)
2501 * Drop the hold count of the vnode. If this is the last reference to
2502 * the vnode we place it on the free list unless it has been vgone'd
2503 * (marked VI_DOOMED) in which case we will free it.
2506 vdropl(struct vnode *vp)
2512 ASSERT_VI_LOCKED(vp, "vdropl");
2513 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2514 if (vp->v_holdcnt <= 0)
2515 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2517 if (vp->v_holdcnt > 0) {
2521 if ((vp->v_iflag & VI_DOOMED) == 0) {
2523 * Mark a vnode as free: remove it from its active list
2524 * and put it up for recycling on the freelist.
2526 VNASSERT(vp->v_op != NULL, vp,
2527 ("vdropl: vnode already reclaimed."));
2528 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2529 ("vnode already free"));
2530 VNASSERT(VSHOULDFREE(vp), vp,
2531 ("vdropl: freeing when we shouldn't"));
2532 active = vp->v_iflag & VI_ACTIVE;
2533 vp->v_iflag &= ~VI_ACTIVE;
2535 mtx_lock(&vnode_free_list_mtx);
2537 TAILQ_REMOVE(&mp->mnt_activevnodelist, vp,
2539 mp->mnt_activevnodelistsize--;
2541 if (vp->v_iflag & VI_AGE) {
2542 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_actfreelist);
2544 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_actfreelist);
2547 vp->v_iflag &= ~VI_AGE;
2548 vp->v_iflag |= VI_FREE;
2549 mtx_unlock(&vnode_free_list_mtx);
2554 * The vnode has been marked for destruction, so free it.
2556 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__, vp);
2557 atomic_subtract_long(&numvnodes, 1);
2559 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
2560 ("cleaned vnode still on the free list."));
2561 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
2562 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
2563 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
2564 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
2565 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
2566 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
2567 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
2568 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
2569 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
2570 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
2571 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
2572 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
2575 mac_vnode_destroy(vp);
2577 if (vp->v_pollinfo != NULL)
2578 destroy_vpollinfo(vp->v_pollinfo);
2580 /* XXX Elsewhere we detect an already freed vnode via NULL v_op. */
2583 rangelock_destroy(&vp->v_rl);
2584 lockdestroy(vp->v_vnlock);
2585 mtx_destroy(&vp->v_interlock);
2586 mtx_destroy(BO_MTX(bo));
2587 uma_zfree(vnode_zone, vp);
2591 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2592 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2593 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2594 * failed lock upgrade.
2597 vinactive(struct vnode *vp, struct thread *td)
2599 struct vm_object *obj;
2601 ASSERT_VOP_ELOCKED(vp, "vinactive");
2602 ASSERT_VI_LOCKED(vp, "vinactive");
2603 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2604 ("vinactive: recursed on VI_DOINGINACT"));
2605 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2606 vp->v_iflag |= VI_DOINGINACT;
2607 vp->v_iflag &= ~VI_OWEINACT;
2610 * Before moving off the active list, we must be sure that any
2611 * modified pages are on the vnode's dirty list since these will
2612 * no longer be checked once the vnode is on the inactive list.
2613 * Because the vnode vm object keeps a hold reference on the vnode
2614 * if there is at least one resident non-cached page, the vnode
2615 * cannot leave the active list without the page cleanup done.
2618 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0) {
2619 VM_OBJECT_LOCK(obj);
2620 vm_object_page_clean(obj, 0, 0, OBJPC_NOSYNC);
2621 VM_OBJECT_UNLOCK(obj);
2623 VOP_INACTIVE(vp, td);
2625 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2626 ("vinactive: lost VI_DOINGINACT"));
2627 vp->v_iflag &= ~VI_DOINGINACT;
2631 * Remove any vnodes in the vnode table belonging to mount point mp.
2633 * If FORCECLOSE is not specified, there should not be any active ones,
2634 * return error if any are found (nb: this is a user error, not a
2635 * system error). If FORCECLOSE is specified, detach any active vnodes
2638 * If WRITECLOSE is set, only flush out regular file vnodes open for
2641 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2643 * `rootrefs' specifies the base reference count for the root vnode
2644 * of this filesystem. The root vnode is considered busy if its
2645 * v_usecount exceeds this value. On a successful return, vflush(, td)
2646 * will call vrele() on the root vnode exactly rootrefs times.
2647 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2651 static int busyprt = 0; /* print out busy vnodes */
2652 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2656 vflush(struct mount *mp, int rootrefs, int flags, struct thread *td)
2658 struct vnode *vp, *mvp, *rootvp = NULL;
2660 int busy = 0, error;
2662 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2665 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2666 ("vflush: bad args"));
2668 * Get the filesystem root vnode. We can vput() it
2669 * immediately, since with rootrefs > 0, it won't go away.
2671 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2672 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2679 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2681 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2684 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2688 * Skip over a vnodes marked VV_SYSTEM.
2690 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2696 * If WRITECLOSE is set, flush out unlinked but still open
2697 * files (even if open only for reading) and regular file
2698 * vnodes open for writing.
2700 if (flags & WRITECLOSE) {
2701 if (vp->v_object != NULL) {
2702 VM_OBJECT_LOCK(vp->v_object);
2703 vm_object_page_clean(vp->v_object, 0, 0, 0);
2704 VM_OBJECT_UNLOCK(vp->v_object);
2706 error = VOP_FSYNC(vp, MNT_WAIT, td);
2710 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2713 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2716 if ((vp->v_type == VNON ||
2717 (error == 0 && vattr.va_nlink > 0)) &&
2718 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2726 * With v_usecount == 0, all we need to do is clear out the
2727 * vnode data structures and we are done.
2729 * If FORCECLOSE is set, forcibly close the vnode.
2731 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2732 VNASSERT(vp->v_usecount == 0 ||
2733 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2734 ("device VNODE %p is FORCECLOSED", vp));
2740 vprint("vflush: busy vnode", vp);
2746 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2748 * If just the root vnode is busy, and if its refcount
2749 * is equal to `rootrefs', then go ahead and kill it.
2752 KASSERT(busy > 0, ("vflush: not busy"));
2753 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2754 ("vflush: usecount %d < rootrefs %d",
2755 rootvp->v_usecount, rootrefs));
2756 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2757 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2759 VOP_UNLOCK(rootvp, 0);
2765 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2769 for (; rootrefs > 0; rootrefs--)
2775 * Recycle an unused vnode to the front of the free list.
2778 vrecycle(struct vnode *vp, struct thread *td)
2782 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2783 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2786 if (vp->v_usecount == 0) {
2795 * Eliminate all activity associated with a vnode
2796 * in preparation for reuse.
2799 vgone(struct vnode *vp)
2807 notify_lowervp_vfs_dummy(struct mount *mp __unused,
2808 struct vnode *lowervp __unused)
2813 * Notify upper mounts about reclaimed or unlinked vnode.
2816 vfs_notify_upper(struct vnode *vp, int event)
2818 static struct vfsops vgonel_vfsops = {
2819 .vfs_reclaim_lowervp = notify_lowervp_vfs_dummy,
2820 .vfs_unlink_lowervp = notify_lowervp_vfs_dummy,
2822 struct mount *mp, *ump, *mmp;
2829 if (TAILQ_EMPTY(&mp->mnt_uppers))
2832 mmp = malloc(sizeof(struct mount), M_TEMP, M_WAITOK | M_ZERO);
2833 mmp->mnt_op = &vgonel_vfsops;
2834 mmp->mnt_kern_flag |= MNTK_MARKER;
2836 mp->mnt_kern_flag |= MNTK_VGONE_UPPER;
2837 for (ump = TAILQ_FIRST(&mp->mnt_uppers); ump != NULL;) {
2838 if ((ump->mnt_kern_flag & MNTK_MARKER) != 0) {
2839 ump = TAILQ_NEXT(ump, mnt_upper_link);
2842 TAILQ_INSERT_AFTER(&mp->mnt_uppers, ump, mmp, mnt_upper_link);
2845 case VFS_NOTIFY_UPPER_RECLAIM:
2846 VFS_RECLAIM_LOWERVP(ump, vp);
2848 case VFS_NOTIFY_UPPER_UNLINK:
2849 VFS_UNLINK_LOWERVP(ump, vp);
2852 KASSERT(0, ("invalid event %d", event));
2856 ump = TAILQ_NEXT(mmp, mnt_upper_link);
2857 TAILQ_REMOVE(&mp->mnt_uppers, mmp, mnt_upper_link);
2860 mp->mnt_kern_flag &= ~MNTK_VGONE_UPPER;
2861 if ((mp->mnt_kern_flag & MNTK_VGONE_WAITER) != 0) {
2862 mp->mnt_kern_flag &= ~MNTK_VGONE_WAITER;
2863 wakeup(&mp->mnt_uppers);
2870 * vgone, with the vp interlock held.
2873 vgonel(struct vnode *vp)
2880 ASSERT_VOP_ELOCKED(vp, "vgonel");
2881 ASSERT_VI_LOCKED(vp, "vgonel");
2882 VNASSERT(vp->v_holdcnt, vp,
2883 ("vgonel: vp %p has no reference.", vp));
2884 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2888 * Don't vgonel if we're already doomed.
2890 if (vp->v_iflag & VI_DOOMED)
2892 vp->v_iflag |= VI_DOOMED;
2895 * Check to see if the vnode is in use. If so, we have to call
2896 * VOP_CLOSE() and VOP_INACTIVE().
2898 active = vp->v_usecount;
2899 oweinact = (vp->v_iflag & VI_OWEINACT);
2901 vfs_notify_upper(vp, VFS_NOTIFY_UPPER_RECLAIM);
2904 * Clean out any buffers associated with the vnode.
2905 * If the flush fails, just toss the buffers.
2908 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2909 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2910 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2911 vinvalbuf(vp, 0, 0, 0);
2914 * If purging an active vnode, it must be closed and
2915 * deactivated before being reclaimed.
2918 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2919 if (oweinact || active) {
2921 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2925 if (vp->v_type == VSOCK)
2926 vfs_unp_reclaim(vp);
2928 * Reclaim the vnode.
2930 if (VOP_RECLAIM(vp, td))
2931 panic("vgone: cannot reclaim");
2933 vn_finished_secondary_write(mp);
2934 VNASSERT(vp->v_object == NULL, vp,
2935 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2937 * Clear the advisory locks and wake up waiting threads.
2939 (void)VOP_ADVLOCKPURGE(vp);
2941 * Delete from old mount point vnode list.
2946 * Done with purge, reset to the standard lock and invalidate
2950 vp->v_vnlock = &vp->v_lock;
2951 vp->v_op = &dead_vnodeops;
2957 * Calculate the total number of references to a special device.
2960 vcount(struct vnode *vp)
2965 count = vp->v_rdev->si_usecount;
2971 * Same as above, but using the struct cdev *as argument
2974 count_dev(struct cdev *dev)
2979 count = dev->si_usecount;
2985 * Print out a description of a vnode.
2987 static char *typename[] =
2988 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2992 vn_printf(struct vnode *vp, const char *fmt, ...)
2995 char buf[256], buf2[16];
3001 printf("%p: ", (void *)vp);
3002 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
3003 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
3004 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
3007 if (vp->v_vflag & VV_ROOT)
3008 strlcat(buf, "|VV_ROOT", sizeof(buf));
3009 if (vp->v_vflag & VV_ISTTY)
3010 strlcat(buf, "|VV_ISTTY", sizeof(buf));
3011 if (vp->v_vflag & VV_NOSYNC)
3012 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
3013 if (vp->v_vflag & VV_ETERNALDEV)
3014 strlcat(buf, "|VV_ETERNALDEV", sizeof(buf));
3015 if (vp->v_vflag & VV_CACHEDLABEL)
3016 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
3017 if (vp->v_vflag & VV_TEXT)
3018 strlcat(buf, "|VV_TEXT", sizeof(buf));
3019 if (vp->v_vflag & VV_COPYONWRITE)
3020 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
3021 if (vp->v_vflag & VV_SYSTEM)
3022 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
3023 if (vp->v_vflag & VV_PROCDEP)
3024 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
3025 if (vp->v_vflag & VV_NOKNOTE)
3026 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
3027 if (vp->v_vflag & VV_DELETED)
3028 strlcat(buf, "|VV_DELETED", sizeof(buf));
3029 if (vp->v_vflag & VV_MD)
3030 strlcat(buf, "|VV_MD", sizeof(buf));
3031 if (vp->v_vflag & VV_FORCEINSMQ)
3032 strlcat(buf, "|VV_FORCEINSMQ", sizeof(buf));
3033 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC | VV_ETERNALDEV |
3034 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
3035 VV_NOKNOTE | VV_DELETED | VV_MD | VV_FORCEINSMQ);
3037 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
3038 strlcat(buf, buf2, sizeof(buf));
3040 if (vp->v_iflag & VI_MOUNT)
3041 strlcat(buf, "|VI_MOUNT", sizeof(buf));
3042 if (vp->v_iflag & VI_AGE)
3043 strlcat(buf, "|VI_AGE", sizeof(buf));
3044 if (vp->v_iflag & VI_DOOMED)
3045 strlcat(buf, "|VI_DOOMED", sizeof(buf));
3046 if (vp->v_iflag & VI_FREE)
3047 strlcat(buf, "|VI_FREE", sizeof(buf));
3048 if (vp->v_iflag & VI_ACTIVE)
3049 strlcat(buf, "|VI_ACTIVE", sizeof(buf));
3050 if (vp->v_iflag & VI_DOINGINACT)
3051 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
3052 if (vp->v_iflag & VI_OWEINACT)
3053 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
3054 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
3055 VI_ACTIVE | VI_DOINGINACT | VI_OWEINACT);
3057 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
3058 strlcat(buf, buf2, sizeof(buf));
3060 printf(" flags (%s)\n", buf + 1);
3061 if (mtx_owned(VI_MTX(vp)))
3062 printf(" VI_LOCKed");
3063 if (vp->v_object != NULL)
3064 printf(" v_object %p ref %d pages %d "
3065 "cleanbuf %d dirtybuf %d\n",
3066 vp->v_object, vp->v_object->ref_count,
3067 vp->v_object->resident_page_count,
3068 vp->v_bufobj.bo_dirty.bv_cnt,
3069 vp->v_bufobj.bo_clean.bv_cnt);
3071 lockmgr_printinfo(vp->v_vnlock);
3072 if (vp->v_data != NULL)
3078 * List all of the locked vnodes in the system.
3079 * Called when debugging the kernel.
3081 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
3083 struct mount *mp, *nmp;
3087 * Note: because this is DDB, we can't obey the locking semantics
3088 * for these structures, which means we could catch an inconsistent
3089 * state and dereference a nasty pointer. Not much to be done
3092 db_printf("Locked vnodes\n");
3093 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
3094 nmp = TAILQ_NEXT(mp, mnt_list);
3095 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3096 if (vp->v_type != VMARKER &&
3100 nmp = TAILQ_NEXT(mp, mnt_list);
3105 * Show details about the given vnode.
3107 DB_SHOW_COMMAND(vnode, db_show_vnode)
3113 vp = (struct vnode *)addr;
3114 vn_printf(vp, "vnode ");
3118 * Show details about the given mount point.
3120 DB_SHOW_COMMAND(mount, db_show_mount)
3131 /* No address given, print short info about all mount points. */
3132 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3133 db_printf("%p %s on %s (%s)\n", mp,
3134 mp->mnt_stat.f_mntfromname,
3135 mp->mnt_stat.f_mntonname,
3136 mp->mnt_stat.f_fstypename);
3140 db_printf("\nMore info: show mount <addr>\n");
3144 mp = (struct mount *)addr;
3145 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
3146 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
3149 mflags = mp->mnt_flag;
3150 #define MNT_FLAG(flag) do { \
3151 if (mflags & (flag)) { \
3152 if (buf[0] != '\0') \
3153 strlcat(buf, ", ", sizeof(buf)); \
3154 strlcat(buf, (#flag) + 4, sizeof(buf)); \
3155 mflags &= ~(flag); \
3158 MNT_FLAG(MNT_RDONLY);
3159 MNT_FLAG(MNT_SYNCHRONOUS);
3160 MNT_FLAG(MNT_NOEXEC);
3161 MNT_FLAG(MNT_NOSUID);
3162 MNT_FLAG(MNT_NFS4ACLS);
3163 MNT_FLAG(MNT_UNION);
3164 MNT_FLAG(MNT_ASYNC);
3165 MNT_FLAG(MNT_SUIDDIR);
3166 MNT_FLAG(MNT_SOFTDEP);
3167 MNT_FLAG(MNT_NOSYMFOLLOW);
3168 MNT_FLAG(MNT_GJOURNAL);
3169 MNT_FLAG(MNT_MULTILABEL);
3171 MNT_FLAG(MNT_NOATIME);
3172 MNT_FLAG(MNT_NOCLUSTERR);
3173 MNT_FLAG(MNT_NOCLUSTERW);
3175 MNT_FLAG(MNT_EXRDONLY);
3176 MNT_FLAG(MNT_EXPORTED);
3177 MNT_FLAG(MNT_DEFEXPORTED);
3178 MNT_FLAG(MNT_EXPORTANON);
3179 MNT_FLAG(MNT_EXKERB);
3180 MNT_FLAG(MNT_EXPUBLIC);
3181 MNT_FLAG(MNT_LOCAL);
3182 MNT_FLAG(MNT_QUOTA);
3183 MNT_FLAG(MNT_ROOTFS);
3185 MNT_FLAG(MNT_IGNORE);
3186 MNT_FLAG(MNT_UPDATE);
3187 MNT_FLAG(MNT_DELEXPORT);
3188 MNT_FLAG(MNT_RELOAD);
3189 MNT_FLAG(MNT_FORCE);
3190 MNT_FLAG(MNT_SNAPSHOT);
3191 MNT_FLAG(MNT_BYFSID);
3195 strlcat(buf, ", ", sizeof(buf));
3196 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3197 "0x%016jx", mflags);
3199 db_printf(" mnt_flag = %s\n", buf);
3202 flags = mp->mnt_kern_flag;
3203 #define MNT_KERN_FLAG(flag) do { \
3204 if (flags & (flag)) { \
3205 if (buf[0] != '\0') \
3206 strlcat(buf, ", ", sizeof(buf)); \
3207 strlcat(buf, (#flag) + 5, sizeof(buf)); \
3211 MNT_KERN_FLAG(MNTK_UNMOUNTF);
3212 MNT_KERN_FLAG(MNTK_ASYNC);
3213 MNT_KERN_FLAG(MNTK_SOFTDEP);
3214 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
3215 MNT_KERN_FLAG(MNTK_DRAINING);
3216 MNT_KERN_FLAG(MNTK_REFEXPIRE);
3217 MNT_KERN_FLAG(MNTK_EXTENDED_SHARED);
3218 MNT_KERN_FLAG(MNTK_SHARED_WRITES);
3219 MNT_KERN_FLAG(MNTK_NO_IOPF);
3220 MNT_KERN_FLAG(MNTK_VGONE_UPPER);
3221 MNT_KERN_FLAG(MNTK_VGONE_WAITER);
3222 MNT_KERN_FLAG(MNTK_LOOKUP_EXCL_DOTDOT);
3223 MNT_KERN_FLAG(MNTK_MARKER);
3224 MNT_KERN_FLAG(MNTK_NOASYNC);
3225 MNT_KERN_FLAG(MNTK_UNMOUNT);
3226 MNT_KERN_FLAG(MNTK_MWAIT);
3227 MNT_KERN_FLAG(MNTK_SUSPEND);
3228 MNT_KERN_FLAG(MNTK_SUSPEND2);
3229 MNT_KERN_FLAG(MNTK_SUSPENDED);
3230 MNT_KERN_FLAG(MNTK_MPSAFE);
3231 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
3232 MNT_KERN_FLAG(MNTK_NOKNOTE);
3233 #undef MNT_KERN_FLAG
3236 strlcat(buf, ", ", sizeof(buf));
3237 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
3240 db_printf(" mnt_kern_flag = %s\n", buf);
3242 db_printf(" mnt_opt = ");
3243 opt = TAILQ_FIRST(mp->mnt_opt);
3245 db_printf("%s", opt->name);
3246 opt = TAILQ_NEXT(opt, link);
3247 while (opt != NULL) {
3248 db_printf(", %s", opt->name);
3249 opt = TAILQ_NEXT(opt, link);
3255 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
3256 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
3257 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
3258 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
3259 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
3260 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
3261 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
3262 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
3263 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
3264 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
3265 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
3266 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
3268 db_printf(" mnt_cred = { uid=%u ruid=%u",
3269 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
3270 if (jailed(mp->mnt_cred))
3271 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
3273 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
3274 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
3275 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
3276 db_printf(" mnt_activevnodelistsize = %d\n",
3277 mp->mnt_activevnodelistsize);
3278 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
3279 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
3280 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
3281 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
3282 db_printf(" mnt_lockref = %d\n", mp->mnt_lockref);
3283 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
3284 db_printf(" mnt_secondary_accwrites = %d\n",
3285 mp->mnt_secondary_accwrites);
3286 db_printf(" mnt_gjprovider = %s\n",
3287 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
3289 db_printf("\n\nList of active vnodes\n");
3290 TAILQ_FOREACH(vp, &mp->mnt_activevnodelist, v_actfreelist) {
3291 if (vp->v_type != VMARKER) {
3292 vn_printf(vp, "vnode ");
3297 db_printf("\n\nList of inactive vnodes\n");
3298 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3299 if (vp->v_type != VMARKER && (vp->v_iflag & VI_ACTIVE) == 0) {
3300 vn_printf(vp, "vnode ");
3309 * Fill in a struct xvfsconf based on a struct vfsconf.
3312 vfsconf2x(struct sysctl_req *req, struct vfsconf *vfsp)
3314 struct xvfsconf xvfsp;
3316 bzero(&xvfsp, sizeof(xvfsp));
3317 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3318 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3319 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3320 xvfsp.vfc_flags = vfsp->vfc_flags;
3322 * These are unused in userland, we keep them
3323 * to not break binary compatibility.
3325 xvfsp.vfc_vfsops = NULL;
3326 xvfsp.vfc_next = NULL;
3327 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3330 #ifdef COMPAT_FREEBSD32
3332 uint32_t vfc_vfsops;
3333 char vfc_name[MFSNAMELEN];
3334 int32_t vfc_typenum;
3335 int32_t vfc_refcount;
3341 vfsconf2x32(struct sysctl_req *req, struct vfsconf *vfsp)
3343 struct xvfsconf32 xvfsp;
3345 strcpy(xvfsp.vfc_name, vfsp->vfc_name);
3346 xvfsp.vfc_typenum = vfsp->vfc_typenum;
3347 xvfsp.vfc_refcount = vfsp->vfc_refcount;
3348 xvfsp.vfc_flags = vfsp->vfc_flags;
3349 xvfsp.vfc_vfsops = 0;
3351 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3356 * Top level filesystem related information gathering.
3359 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
3361 struct vfsconf *vfsp;
3365 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3366 #ifdef COMPAT_FREEBSD32
3367 if (req->flags & SCTL_MASK32)
3368 error = vfsconf2x32(req, vfsp);
3371 error = vfsconf2x(req, vfsp);
3378 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLTYPE_OPAQUE | CTLFLAG_RD,
3379 NULL, 0, sysctl_vfs_conflist,
3380 "S,xvfsconf", "List of all configured filesystems");
3382 #ifndef BURN_BRIDGES
3383 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3386 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3388 int *name = (int *)arg1 - 1; /* XXX */
3389 u_int namelen = arg2 + 1; /* XXX */
3390 struct vfsconf *vfsp;
3392 printf("WARNING: userland calling deprecated sysctl, "
3393 "please rebuild world\n");
3395 #if 1 || defined(COMPAT_PRELITE2)
3396 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3398 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3402 case VFS_MAXTYPENUM:
3405 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3408 return (ENOTDIR); /* overloaded */
3409 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
3410 if (vfsp->vfc_typenum == name[2])
3413 return (EOPNOTSUPP);
3414 #ifdef COMPAT_FREEBSD32
3415 if (req->flags & SCTL_MASK32)
3416 return (vfsconf2x32(req, vfsp));
3419 return (vfsconf2x(req, vfsp));
3421 return (EOPNOTSUPP);
3424 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
3425 vfs_sysctl, "Generic filesystem");
3427 #if 1 || defined(COMPAT_PRELITE2)
3430 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3433 struct vfsconf *vfsp;
3434 struct ovfsconf ovfs;
3436 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3437 bzero(&ovfs, sizeof(ovfs));
3438 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3439 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3440 ovfs.vfc_index = vfsp->vfc_typenum;
3441 ovfs.vfc_refcount = vfsp->vfc_refcount;
3442 ovfs.vfc_flags = vfsp->vfc_flags;
3443 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3450 #endif /* 1 || COMPAT_PRELITE2 */
3451 #endif /* !BURN_BRIDGES */
3453 #define KINFO_VNODESLOP 10
3456 * Dump vnode list (via sysctl).
3460 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3468 * Stale numvnodes access is not fatal here.
3471 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3473 /* Make an estimate */
3474 return (SYSCTL_OUT(req, 0, len));
3476 error = sysctl_wire_old_buffer(req, 0);
3479 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3481 mtx_lock(&mountlist_mtx);
3482 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3483 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3486 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3490 xvn[n].xv_size = sizeof *xvn;
3491 xvn[n].xv_vnode = vp;
3492 xvn[n].xv_id = 0; /* XXX compat */
3493 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3495 XV_COPY(writecount);
3501 xvn[n].xv_flag = vp->v_vflag;
3503 switch (vp->v_type) {
3510 if (vp->v_rdev == NULL) {
3514 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3517 xvn[n].xv_socket = vp->v_socket;
3520 xvn[n].xv_fifo = vp->v_fifoinfo;
3525 /* shouldn't happen? */
3533 mtx_lock(&mountlist_mtx);
3538 mtx_unlock(&mountlist_mtx);
3540 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3545 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3546 0, 0, sysctl_vnode, "S,xvnode", "");
3550 * Unmount all filesystems. The list is traversed in reverse order
3551 * of mounting to avoid dependencies.
3554 vfs_unmountall(void)
3560 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3564 * Since this only runs when rebooting, it is not interlocked.
3566 while(!TAILQ_EMPTY(&mountlist)) {
3567 mp = TAILQ_LAST(&mountlist, mntlist);
3568 error = dounmount(mp, MNT_FORCE, td);
3570 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3572 * XXX: Due to the way in which we mount the root
3573 * file system off of devfs, devfs will generate a
3574 * "busy" warning when we try to unmount it before
3575 * the root. Don't print a warning as a result in
3576 * order to avoid false positive errors that may
3577 * cause needless upset.
3579 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3580 printf("unmount of %s failed (",
3581 mp->mnt_stat.f_mntonname);
3585 printf("%d)\n", error);
3588 /* The unmount has removed mp from the mountlist */
3594 * perform msync on all vnodes under a mount point
3595 * the mount point must be locked.
3598 vfs_msync(struct mount *mp, int flags)
3600 struct vnode *vp, *mvp;
3601 struct vm_object *obj;
3603 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3604 MNT_VNODE_FOREACH_ACTIVE(vp, mp, mvp) {
3606 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3607 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3609 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3611 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3618 VM_OBJECT_LOCK(obj);
3619 vm_object_page_clean(obj, 0, 0,
3621 OBJPC_SYNC : OBJPC_NOSYNC);
3622 VM_OBJECT_UNLOCK(obj);
3632 destroy_vpollinfo_free(struct vpollinfo *vi)
3635 knlist_destroy(&vi->vpi_selinfo.si_note);
3636 mtx_destroy(&vi->vpi_lock);
3637 uma_zfree(vnodepoll_zone, vi);
3641 destroy_vpollinfo(struct vpollinfo *vi)
3644 knlist_clear(&vi->vpi_selinfo.si_note, 1);
3645 seldrain(&vi->vpi_selinfo);
3646 destroy_vpollinfo_free(vi);
3650 * Initalize per-vnode helper structure to hold poll-related state.
3653 v_addpollinfo(struct vnode *vp)
3655 struct vpollinfo *vi;
3657 if (vp->v_pollinfo != NULL)
3659 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3660 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3661 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3662 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3664 if (vp->v_pollinfo != NULL) {
3666 destroy_vpollinfo_free(vi);
3669 vp->v_pollinfo = vi;
3674 * Record a process's interest in events which might happen to
3675 * a vnode. Because poll uses the historic select-style interface
3676 * internally, this routine serves as both the ``check for any
3677 * pending events'' and the ``record my interest in future events''
3678 * functions. (These are done together, while the lock is held,
3679 * to avoid race conditions.)
3682 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3686 mtx_lock(&vp->v_pollinfo->vpi_lock);
3687 if (vp->v_pollinfo->vpi_revents & events) {
3689 * This leaves events we are not interested
3690 * in available for the other process which
3691 * which presumably had requested them
3692 * (otherwise they would never have been
3695 events &= vp->v_pollinfo->vpi_revents;
3696 vp->v_pollinfo->vpi_revents &= ~events;
3698 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3701 vp->v_pollinfo->vpi_events |= events;
3702 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3703 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3708 * Routine to create and manage a filesystem syncer vnode.
3710 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3711 static int sync_fsync(struct vop_fsync_args *);
3712 static int sync_inactive(struct vop_inactive_args *);
3713 static int sync_reclaim(struct vop_reclaim_args *);
3715 static struct vop_vector sync_vnodeops = {
3716 .vop_bypass = VOP_EOPNOTSUPP,
3717 .vop_close = sync_close, /* close */
3718 .vop_fsync = sync_fsync, /* fsync */
3719 .vop_inactive = sync_inactive, /* inactive */
3720 .vop_reclaim = sync_reclaim, /* reclaim */
3721 .vop_lock1 = vop_stdlock, /* lock */
3722 .vop_unlock = vop_stdunlock, /* unlock */
3723 .vop_islocked = vop_stdislocked, /* islocked */
3727 * Create a new filesystem syncer vnode for the specified mount point.
3730 vfs_allocate_syncvnode(struct mount *mp)
3734 static long start, incr, next;
3737 /* Allocate a new vnode */
3738 error = getnewvnode("syncer", mp, &sync_vnodeops, &vp);
3740 panic("vfs_allocate_syncvnode: getnewvnode() failed");
3742 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3743 vp->v_vflag |= VV_FORCEINSMQ;
3744 error = insmntque(vp, mp);
3746 panic("vfs_allocate_syncvnode: insmntque() failed");
3747 vp->v_vflag &= ~VV_FORCEINSMQ;
3750 * Place the vnode onto the syncer worklist. We attempt to
3751 * scatter them about on the list so that they will go off
3752 * at evenly distributed times even if all the filesystems
3753 * are mounted at once.
3756 if (next == 0 || next > syncer_maxdelay) {
3760 start = syncer_maxdelay / 2;
3761 incr = syncer_maxdelay;
3767 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3768 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3769 mtx_lock(&sync_mtx);
3771 if (mp->mnt_syncer == NULL) {
3772 mp->mnt_syncer = vp;
3775 mtx_unlock(&sync_mtx);
3778 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3785 vfs_deallocate_syncvnode(struct mount *mp)
3789 mtx_lock(&sync_mtx);
3790 vp = mp->mnt_syncer;
3792 mp->mnt_syncer = NULL;
3793 mtx_unlock(&sync_mtx);
3799 * Do a lazy sync of the filesystem.
3802 sync_fsync(struct vop_fsync_args *ap)
3804 struct vnode *syncvp = ap->a_vp;
3805 struct mount *mp = syncvp->v_mount;
3810 * We only need to do something if this is a lazy evaluation.
3812 if (ap->a_waitfor != MNT_LAZY)
3816 * Move ourselves to the back of the sync list.
3818 bo = &syncvp->v_bufobj;
3820 vn_syncer_add_to_worklist(bo, syncdelay);
3824 * Walk the list of vnodes pushing all that are dirty and
3825 * not already on the sync list.
3827 if (vfs_busy(mp, MBF_NOWAIT) != 0)
3829 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3833 save = curthread_pflags_set(TDP_SYNCIO);
3834 vfs_msync(mp, MNT_NOWAIT);
3835 error = VFS_SYNC(mp, MNT_LAZY);
3836 curthread_pflags_restore(save);
3837 vn_finished_write(mp);
3843 * The syncer vnode is no referenced.
3846 sync_inactive(struct vop_inactive_args *ap)
3854 * The syncer vnode is no longer needed and is being decommissioned.
3856 * Modifications to the worklist must be protected by sync_mtx.
3859 sync_reclaim(struct vop_reclaim_args *ap)
3861 struct vnode *vp = ap->a_vp;
3866 mtx_lock(&sync_mtx);
3867 if (vp->v_mount->mnt_syncer == vp)
3868 vp->v_mount->mnt_syncer = NULL;
3869 if (bo->bo_flag & BO_ONWORKLST) {
3870 LIST_REMOVE(bo, bo_synclist);
3871 syncer_worklist_len--;
3873 bo->bo_flag &= ~BO_ONWORKLST;
3875 mtx_unlock(&sync_mtx);
3882 * Check if vnode represents a disk device
3885 vn_isdisk(struct vnode *vp, int *errp)
3891 if (vp->v_type != VCHR)
3893 else if (vp->v_rdev == NULL)
3895 else if (vp->v_rdev->si_devsw == NULL)
3897 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3902 return (error == 0);
3906 * Common filesystem object access control check routine. Accepts a
3907 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3908 * and optional call-by-reference privused argument allowing vaccess()
3909 * to indicate to the caller whether privilege was used to satisfy the
3910 * request (obsoleted). Returns 0 on success, or an errno on failure.
3913 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3914 accmode_t accmode, struct ucred *cred, int *privused)
3916 accmode_t dac_granted;
3917 accmode_t priv_granted;
3919 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3920 ("invalid bit in accmode"));
3921 KASSERT((accmode & VAPPEND) == 0 || (accmode & VWRITE),
3922 ("VAPPEND without VWRITE"));
3925 * Look for a normal, non-privileged way to access the file/directory
3926 * as requested. If it exists, go with that.
3929 if (privused != NULL)
3934 /* Check the owner. */
3935 if (cred->cr_uid == file_uid) {
3936 dac_granted |= VADMIN;
3937 if (file_mode & S_IXUSR)
3938 dac_granted |= VEXEC;
3939 if (file_mode & S_IRUSR)
3940 dac_granted |= VREAD;
3941 if (file_mode & S_IWUSR)
3942 dac_granted |= (VWRITE | VAPPEND);
3944 if ((accmode & dac_granted) == accmode)
3950 /* Otherwise, check the groups (first match) */
3951 if (groupmember(file_gid, cred)) {
3952 if (file_mode & S_IXGRP)
3953 dac_granted |= VEXEC;
3954 if (file_mode & S_IRGRP)
3955 dac_granted |= VREAD;
3956 if (file_mode & S_IWGRP)
3957 dac_granted |= (VWRITE | VAPPEND);
3959 if ((accmode & dac_granted) == accmode)
3965 /* Otherwise, check everyone else. */
3966 if (file_mode & S_IXOTH)
3967 dac_granted |= VEXEC;
3968 if (file_mode & S_IROTH)
3969 dac_granted |= VREAD;
3970 if (file_mode & S_IWOTH)
3971 dac_granted |= (VWRITE | VAPPEND);
3972 if ((accmode & dac_granted) == accmode)
3977 * Build a privilege mask to determine if the set of privileges
3978 * satisfies the requirements when combined with the granted mask
3979 * from above. For each privilege, if the privilege is required,
3980 * bitwise or the request type onto the priv_granted mask.
3986 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3987 * requests, instead of PRIV_VFS_EXEC.
3989 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3990 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3991 priv_granted |= VEXEC;
3994 * Ensure that at least one execute bit is on. Otherwise,
3995 * a privileged user will always succeed, and we don't want
3996 * this to happen unless the file really is executable.
3998 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3999 (file_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) != 0 &&
4000 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
4001 priv_granted |= VEXEC;
4004 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
4005 !priv_check_cred(cred, PRIV_VFS_READ, 0))
4006 priv_granted |= VREAD;
4008 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
4009 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
4010 priv_granted |= (VWRITE | VAPPEND);
4012 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
4013 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
4014 priv_granted |= VADMIN;
4016 if ((accmode & (priv_granted | dac_granted)) == accmode) {
4017 /* XXX audit: privilege used */
4018 if (privused != NULL)
4023 return ((accmode & VADMIN) ? EPERM : EACCES);
4027 * Credential check based on process requesting service, and per-attribute
4031 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
4032 struct thread *td, accmode_t accmode)
4036 * Kernel-invoked always succeeds.
4042 * Do not allow privileged processes in jail to directly manipulate
4043 * system attributes.
4045 switch (attrnamespace) {
4046 case EXTATTR_NAMESPACE_SYSTEM:
4047 /* Potentially should be: return (EPERM); */
4048 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
4049 case EXTATTR_NAMESPACE_USER:
4050 return (VOP_ACCESS(vp, accmode, cred, td));
4056 #ifdef DEBUG_VFS_LOCKS
4058 * This only exists to supress warnings from unlocked specfs accesses. It is
4059 * no longer ok to have an unlocked VFS.
4061 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
4062 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
4064 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
4065 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
4066 "Drop into debugger on lock violation");
4068 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
4069 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
4070 0, "Check for interlock across VOPs");
4072 int vfs_badlock_print = 1; /* Print lock violations. */
4073 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
4074 0, "Print lock violations");
4077 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
4078 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
4079 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
4083 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
4087 if (vfs_badlock_backtrace)
4090 if (vfs_badlock_print)
4091 printf("%s: %p %s\n", str, (void *)vp, msg);
4092 if (vfs_badlock_ddb)
4093 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4097 assert_vi_locked(struct vnode *vp, const char *str)
4100 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
4101 vfs_badlock("interlock is not locked but should be", str, vp);
4105 assert_vi_unlocked(struct vnode *vp, const char *str)
4108 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
4109 vfs_badlock("interlock is locked but should not be", str, vp);
4113 assert_vop_locked(struct vnode *vp, const char *str)
4117 if (!IGNORE_LOCK(vp)) {
4118 locked = VOP_ISLOCKED(vp);
4119 if (locked == 0 || locked == LK_EXCLOTHER)
4120 vfs_badlock("is not locked but should be", str, vp);
4125 assert_vop_unlocked(struct vnode *vp, const char *str)
4128 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
4129 vfs_badlock("is locked but should not be", str, vp);
4133 assert_vop_elocked(struct vnode *vp, const char *str)
4136 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
4137 vfs_badlock("is not exclusive locked but should be", str, vp);
4142 assert_vop_elocked_other(struct vnode *vp, const char *str)
4145 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
4146 vfs_badlock("is not exclusive locked by another thread",
4151 assert_vop_slocked(struct vnode *vp, const char *str)
4154 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
4155 vfs_badlock("is not locked shared but should be", str, vp);
4158 #endif /* DEBUG_VFS_LOCKS */
4161 vop_rename_fail(struct vop_rename_args *ap)
4164 if (ap->a_tvp != NULL)
4166 if (ap->a_tdvp == ap->a_tvp)
4175 vop_rename_pre(void *ap)
4177 struct vop_rename_args *a = ap;
4179 #ifdef DEBUG_VFS_LOCKS
4181 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
4182 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
4183 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
4184 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
4186 /* Check the source (from). */
4187 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
4188 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
4189 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
4190 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
4191 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
4193 /* Check the target. */
4195 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
4196 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
4198 if (a->a_tdvp != a->a_fdvp)
4200 if (a->a_tvp != a->a_fvp)
4208 vop_strategy_pre(void *ap)
4210 #ifdef DEBUG_VFS_LOCKS
4211 struct vop_strategy_args *a;
4218 * Cluster ops lock their component buffers but not the IO container.
4220 if ((bp->b_flags & B_CLUSTER) != 0)
4223 if (panicstr == NULL && !BUF_ISLOCKED(bp)) {
4224 if (vfs_badlock_print)
4226 "VOP_STRATEGY: bp is not locked but should be\n");
4227 if (vfs_badlock_ddb)
4228 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
4234 vop_lock_pre(void *ap)
4236 #ifdef DEBUG_VFS_LOCKS
4237 struct vop_lock1_args *a = ap;
4239 if ((a->a_flags & LK_INTERLOCK) == 0)
4240 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4242 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
4247 vop_lock_post(void *ap, int rc)
4249 #ifdef DEBUG_VFS_LOCKS
4250 struct vop_lock1_args *a = ap;
4252 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
4253 if (rc == 0 && (a->a_flags & LK_EXCLOTHER) == 0)
4254 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
4259 vop_unlock_pre(void *ap)
4261 #ifdef DEBUG_VFS_LOCKS
4262 struct vop_unlock_args *a = ap;
4264 if (a->a_flags & LK_INTERLOCK)
4265 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
4266 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
4271 vop_unlock_post(void *ap, int rc)
4273 #ifdef DEBUG_VFS_LOCKS
4274 struct vop_unlock_args *a = ap;
4276 if (a->a_flags & LK_INTERLOCK)
4277 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
4282 vop_create_post(void *ap, int rc)
4284 struct vop_create_args *a = ap;
4287 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4291 vop_deleteextattr_post(void *ap, int rc)
4293 struct vop_deleteextattr_args *a = ap;
4296 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4300 vop_link_post(void *ap, int rc)
4302 struct vop_link_args *a = ap;
4305 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
4306 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
4311 vop_mkdir_post(void *ap, int rc)
4313 struct vop_mkdir_args *a = ap;
4316 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4320 vop_mknod_post(void *ap, int rc)
4322 struct vop_mknod_args *a = ap;
4325 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4329 vop_remove_post(void *ap, int rc)
4331 struct vop_remove_args *a = ap;
4334 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4335 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4340 vop_rename_post(void *ap, int rc)
4342 struct vop_rename_args *a = ap;
4345 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4346 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4347 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4349 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4351 if (a->a_tdvp != a->a_fdvp)
4353 if (a->a_tvp != a->a_fvp)
4361 vop_rmdir_post(void *ap, int rc)
4363 struct vop_rmdir_args *a = ap;
4366 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4367 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4372 vop_setattr_post(void *ap, int rc)
4374 struct vop_setattr_args *a = ap;
4377 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4381 vop_setextattr_post(void *ap, int rc)
4383 struct vop_setextattr_args *a = ap;
4386 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4390 vop_symlink_post(void *ap, int rc)
4392 struct vop_symlink_args *a = ap;
4395 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4398 static struct knlist fs_knlist;
4401 vfs_event_init(void *arg)
4403 knlist_init_mtx(&fs_knlist, NULL);
4405 /* XXX - correct order? */
4406 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4409 vfs_event_signal(fsid_t *fsid, uint32_t event, intptr_t data __unused)
4412 KNOTE_UNLOCKED(&fs_knlist, event);
4415 static int filt_fsattach(struct knote *kn);
4416 static void filt_fsdetach(struct knote *kn);
4417 static int filt_fsevent(struct knote *kn, long hint);
4419 struct filterops fs_filtops = {
4421 .f_attach = filt_fsattach,
4422 .f_detach = filt_fsdetach,
4423 .f_event = filt_fsevent
4427 filt_fsattach(struct knote *kn)
4430 kn->kn_flags |= EV_CLEAR;
4431 knlist_add(&fs_knlist, kn, 0);
4436 filt_fsdetach(struct knote *kn)
4439 knlist_remove(&fs_knlist, kn, 0);
4443 filt_fsevent(struct knote *kn, long hint)
4446 kn->kn_fflags |= hint;
4447 return (kn->kn_fflags != 0);
4451 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4457 error = SYSCTL_IN(req, &vc, sizeof(vc));
4460 if (vc.vc_vers != VFS_CTL_VERS1)
4462 mp = vfs_getvfs(&vc.vc_fsid);
4465 /* ensure that a specific sysctl goes to the right filesystem. */
4466 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4467 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4471 VCTLTOREQ(&vc, req);
4472 error = VFS_SYSCTL(mp, vc.vc_op, req);
4477 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLTYPE_OPAQUE | CTLFLAG_WR,
4478 NULL, 0, sysctl_vfs_ctl, "",
4482 * Function to initialize a va_filerev field sensibly.
4483 * XXX: Wouldn't a random number make a lot more sense ??
4486 init_va_filerev(void)
4491 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4494 static int filt_vfsread(struct knote *kn, long hint);
4495 static int filt_vfswrite(struct knote *kn, long hint);
4496 static int filt_vfsvnode(struct knote *kn, long hint);
4497 static void filt_vfsdetach(struct knote *kn);
4498 static struct filterops vfsread_filtops = {
4500 .f_detach = filt_vfsdetach,
4501 .f_event = filt_vfsread
4503 static struct filterops vfswrite_filtops = {
4505 .f_detach = filt_vfsdetach,
4506 .f_event = filt_vfswrite
4508 static struct filterops vfsvnode_filtops = {
4510 .f_detach = filt_vfsdetach,
4511 .f_event = filt_vfsvnode
4515 vfs_knllock(void *arg)
4517 struct vnode *vp = arg;
4519 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4523 vfs_knlunlock(void *arg)
4525 struct vnode *vp = arg;
4531 vfs_knl_assert_locked(void *arg)
4533 #ifdef DEBUG_VFS_LOCKS
4534 struct vnode *vp = arg;
4536 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4541 vfs_knl_assert_unlocked(void *arg)
4543 #ifdef DEBUG_VFS_LOCKS
4544 struct vnode *vp = arg;
4546 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4551 vfs_kqfilter(struct vop_kqfilter_args *ap)
4553 struct vnode *vp = ap->a_vp;
4554 struct knote *kn = ap->a_kn;
4557 switch (kn->kn_filter) {
4559 kn->kn_fop = &vfsread_filtops;
4562 kn->kn_fop = &vfswrite_filtops;
4565 kn->kn_fop = &vfsvnode_filtops;
4571 kn->kn_hook = (caddr_t)vp;
4574 if (vp->v_pollinfo == NULL)
4576 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4578 knlist_add(knl, kn, 0);
4584 * Detach knote from vnode
4587 filt_vfsdetach(struct knote *kn)
4589 struct vnode *vp = (struct vnode *)kn->kn_hook;
4591 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4592 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4598 filt_vfsread(struct knote *kn, long hint)
4600 struct vnode *vp = (struct vnode *)kn->kn_hook;
4605 * filesystem is gone, so set the EOF flag and schedule
4606 * the knote for deletion.
4608 if (hint == NOTE_REVOKE) {
4610 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4615 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4619 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4620 res = (kn->kn_data != 0);
4627 filt_vfswrite(struct knote *kn, long hint)
4629 struct vnode *vp = (struct vnode *)kn->kn_hook;
4634 * filesystem is gone, so set the EOF flag and schedule
4635 * the knote for deletion.
4637 if (hint == NOTE_REVOKE)
4638 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4646 filt_vfsvnode(struct knote *kn, long hint)
4648 struct vnode *vp = (struct vnode *)kn->kn_hook;
4652 if (kn->kn_sfflags & hint)
4653 kn->kn_fflags |= hint;
4654 if (hint == NOTE_REVOKE) {
4655 kn->kn_flags |= EV_EOF;
4659 res = (kn->kn_fflags != 0);
4665 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4669 if (dp->d_reclen > ap->a_uio->uio_resid)
4670 return (ENAMETOOLONG);
4671 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4673 if (ap->a_ncookies != NULL) {
4674 if (ap->a_cookies != NULL)
4675 free(ap->a_cookies, M_TEMP);
4676 ap->a_cookies = NULL;
4677 *ap->a_ncookies = 0;
4681 if (ap->a_ncookies == NULL)
4684 KASSERT(ap->a_cookies,
4685 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4687 *ap->a_cookies = realloc(*ap->a_cookies,
4688 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4689 (*ap->a_cookies)[*ap->a_ncookies] = off;
4690 *ap->a_ncookies += 1;
4695 * Mark for update the access time of the file if the filesystem
4696 * supports VOP_MARKATIME. This functionality is used by execve and
4697 * mmap, so we want to avoid the I/O implied by directly setting
4698 * va_atime for the sake of efficiency.
4701 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4706 VFS_ASSERT_GIANT(mp);
4707 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4708 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4709 (void)VOP_MARKATIME(vp);
4713 * The purpose of this routine is to remove granularity from accmode_t,
4714 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4715 * VADMIN and VAPPEND.
4717 * If it returns 0, the caller is supposed to continue with the usual
4718 * access checks using 'accmode' as modified by this routine. If it
4719 * returns nonzero value, the caller is supposed to return that value
4722 * Note that after this routine runs, accmode may be zero.
4725 vfs_unixify_accmode(accmode_t *accmode)
4728 * There is no way to specify explicit "deny" rule using
4729 * file mode or POSIX.1e ACLs.
4731 if (*accmode & VEXPLICIT_DENY) {
4737 * None of these can be translated into usual access bits.
4738 * Also, the common case for NFSv4 ACLs is to not contain
4739 * either of these bits. Caller should check for VWRITE
4740 * on the containing directory instead.
4742 if (*accmode & (VDELETE_CHILD | VDELETE))
4745 if (*accmode & VADMIN_PERMS) {
4746 *accmode &= ~VADMIN_PERMS;
4751 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4752 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4754 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);
4760 * These are helper functions for filesystems to traverse all
4761 * their vnodes. See MNT_VNODE_FOREACH_ALL() in sys/mount.h.
4763 * This interface replaces MNT_VNODE_FOREACH.
4766 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker");
4769 __mnt_vnode_next_all(struct vnode **mvp, struct mount *mp)
4774 kern_yield(PRI_UNCHANGED);
4776 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4777 vp = TAILQ_NEXT(*mvp, v_nmntvnodes);
4778 while (vp != NULL && (vp->v_type == VMARKER ||
4779 (vp->v_iflag & VI_DOOMED) != 0))
4780 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4782 /* Check if we are done */
4784 __mnt_vnode_markerfree_all(mvp, mp);
4785 /* MNT_IUNLOCK(mp); -- done in above function */
4786 mtx_assert(MNT_MTX(mp), MA_NOTOWNED);
4789 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4790 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4797 __mnt_vnode_first_all(struct vnode **mvp, struct mount *mp)
4801 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4804 (*mvp)->v_type = VMARKER;
4806 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
4807 while (vp != NULL && (vp->v_type == VMARKER ||
4808 (vp->v_iflag & VI_DOOMED) != 0))
4809 vp = TAILQ_NEXT(vp, v_nmntvnodes);
4811 /* Check if we are done */
4815 free(*mvp, M_VNODE_MARKER);
4819 (*mvp)->v_mount = mp;
4820 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes);
4828 __mnt_vnode_markerfree_all(struct vnode **mvp, struct mount *mp)
4836 mtx_assert(MNT_MTX(mp), MA_OWNED);
4838 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4839 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes);
4842 free(*mvp, M_VNODE_MARKER);
4847 * These are helper functions for filesystems to traverse their
4848 * active vnodes. See MNT_VNODE_FOREACH_ACTIVE() in sys/mount.h
4851 mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4854 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4859 free(*mvp, M_VNODE_MARKER);
4864 #define ALWAYS_YIELD (mp_ncpus == 1)
4866 #define ALWAYS_YIELD 1
4869 static struct vnode *
4870 mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4872 struct vnode *vp, *nvp;
4874 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
4875 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch"));
4877 vp = TAILQ_NEXT(*mvp, v_actfreelist);
4878 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4879 while (vp != NULL) {
4880 if (vp->v_type == VMARKER) {
4881 vp = TAILQ_NEXT(vp, v_actfreelist);
4884 if (!VI_TRYLOCK(vp)) {
4885 if (ALWAYS_YIELD || should_yield()) {
4886 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4887 mtx_unlock(&vnode_free_list_mtx);
4889 mtx_lock(&vnode_free_list_mtx);
4894 KASSERT(vp->v_type != VMARKER, ("locked marker %p", vp));
4895 KASSERT(vp->v_mount == mp || vp->v_mount == NULL,
4896 ("alien vnode on the active list %p %p", vp, mp));
4897 if (vp->v_mount == mp && (vp->v_iflag & VI_DOOMED) == 0)
4899 nvp = TAILQ_NEXT(vp, v_actfreelist);
4904 /* Check if we are done */
4906 mtx_unlock(&vnode_free_list_mtx);
4907 mnt_vnode_markerfree_active(mvp, mp);
4910 TAILQ_INSERT_AFTER(&mp->mnt_activevnodelist, vp, *mvp, v_actfreelist);
4911 mtx_unlock(&vnode_free_list_mtx);
4912 ASSERT_VI_LOCKED(vp, "active iter");
4913 KASSERT((vp->v_iflag & VI_ACTIVE) != 0, ("Non-active vp %p", vp));
4919 __mnt_vnode_next_active(struct vnode **mvp, struct mount *mp)
4923 kern_yield(PRI_UNCHANGED);
4924 mtx_lock(&vnode_free_list_mtx);
4925 return (mnt_vnode_next_active(mvp, mp));
4929 __mnt_vnode_first_active(struct vnode **mvp, struct mount *mp)
4933 *mvp = malloc(sizeof(struct vnode), M_VNODE_MARKER, M_WAITOK | M_ZERO);
4937 (*mvp)->v_type = VMARKER;
4938 (*mvp)->v_mount = mp;
4940 mtx_lock(&vnode_free_list_mtx);
4941 vp = TAILQ_FIRST(&mp->mnt_activevnodelist);
4943 mtx_unlock(&vnode_free_list_mtx);
4944 mnt_vnode_markerfree_active(mvp, mp);
4947 TAILQ_INSERT_BEFORE(vp, *mvp, v_actfreelist);
4948 return (mnt_vnode_next_active(mvp, mp));
4952 __mnt_vnode_markerfree_active(struct vnode **mvp, struct mount *mp)
4958 mtx_lock(&vnode_free_list_mtx);
4959 TAILQ_REMOVE(&mp->mnt_activevnodelist, *mvp, v_actfreelist);
4960 mtx_unlock(&vnode_free_list_mtx);
4961 mnt_vnode_markerfree_active(mvp, mp);