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$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
50 #include <sys/condvar.h>
52 #include <sys/dirent.h>
53 #include <sys/event.h>
54 #include <sys/eventhandler.h>
55 #include <sys/extattr.h>
57 #include <sys/fcntl.h>
60 #include <sys/kernel.h>
61 #include <sys/kthread.h>
62 #include <sys/lockf.h>
63 #include <sys/malloc.h>
64 #include <sys/mount.h>
65 #include <sys/namei.h>
67 #include <sys/reboot.h>
68 #include <sys/sched.h>
69 #include <sys/sleepqueue.h>
71 #include <sys/sysctl.h>
72 #include <sys/syslog.h>
73 #include <sys/vmmeter.h>
74 #include <sys/vnode.h>
76 #include <machine/stdarg.h>
78 #include <security/mac/mac_framework.h>
81 #include <vm/vm_object.h>
82 #include <vm/vm_extern.h>
84 #include <vm/vm_map.h>
85 #include <vm/vm_page.h>
86 #include <vm/vm_kern.h>
96 static MALLOC_DEFINE(M_NETADDR, "subr_export_host", "Export host address structure");
98 static void delmntque(struct vnode *vp);
99 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
100 int slpflag, int slptimeo);
101 static void syncer_shutdown(void *arg, int howto);
102 static int vtryrecycle(struct vnode *vp);
103 static void vbusy(struct vnode *vp);
104 static void vinactive(struct vnode *, struct thread *);
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 vfree(struct vnode *);
110 static void vnlru_free(int);
111 static void vgonel(struct vnode *);
112 static void vfs_knllock(void *arg);
113 static void vfs_knlunlock(void *arg);
114 static void vfs_knl_assert_locked(void *arg);
115 static void vfs_knl_assert_unlocked(void *arg);
116 static void destroy_vpollinfo(struct vpollinfo *vi);
119 * Number of vnodes in existence. Increased whenever getnewvnode()
120 * allocates a new vnode, decreased on vdestroy() called on VI_DOOMed
123 static unsigned long numvnodes;
125 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
126 "Number of vnodes in existence");
129 * Conversion tables for conversion from vnode types to inode formats
132 enum vtype iftovt_tab[16] = {
133 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
134 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
136 int vttoif_tab[10] = {
137 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
138 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
142 * List of vnodes that are ready for recycling.
144 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
147 * Free vnode target. Free vnodes may simply be files which have been stat'd
148 * but not read. This is somewhat common, and a small cache of such files
149 * should be kept to avoid recreation costs.
151 static u_long wantfreevnodes;
152 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
153 /* Number of vnodes in the free list. */
154 static u_long freevnodes;
155 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0,
156 "Number of vnodes in the free list");
158 static int vlru_allow_cache_src;
159 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
160 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
163 * Various variables used for debugging the new implementation of
165 * XXX these are probably of (very) limited utility now.
167 static int reassignbufcalls;
168 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0,
169 "Number of calls to reassignbuf");
172 * Cache for the mount type id assigned to NFS. This is used for
173 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
175 int nfs_mount_type = -1;
177 /* To keep more than one thread at a time from running vfs_getnewfsid */
178 static struct mtx mntid_mtx;
181 * Lock for any access to the following:
186 static struct mtx vnode_free_list_mtx;
188 /* Publicly exported FS */
189 struct nfs_public nfs_pub;
191 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
192 static uma_zone_t vnode_zone;
193 static uma_zone_t vnodepoll_zone;
195 /* Set to 1 to print out reclaim of active vnodes */
199 * The workitem queue.
201 * It is useful to delay writes of file data and filesystem metadata
202 * for tens of seconds so that quickly created and deleted files need
203 * not waste disk bandwidth being created and removed. To realize this,
204 * we append vnodes to a "workitem" queue. When running with a soft
205 * updates implementation, most pending metadata dependencies should
206 * not wait for more than a few seconds. Thus, mounted on block devices
207 * are delayed only about a half the time that file data is delayed.
208 * Similarly, directory updates are more critical, so are only delayed
209 * about a third the time that file data is delayed. Thus, there are
210 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
211 * one each second (driven off the filesystem syncer process). The
212 * syncer_delayno variable indicates the next queue that is to be processed.
213 * Items that need to be processed soon are placed in this queue:
215 * syncer_workitem_pending[syncer_delayno]
217 * A delay of fifteen seconds is done by placing the request fifteen
218 * entries later in the queue:
220 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
223 static int syncer_delayno;
224 static long syncer_mask;
225 LIST_HEAD(synclist, bufobj);
226 static struct synclist *syncer_workitem_pending[2];
228 * The sync_mtx protects:
233 * syncer_workitem_pending
234 * syncer_worklist_len
237 static struct mtx sync_mtx;
238 static struct cv sync_wakeup;
240 #define SYNCER_MAXDELAY 32
241 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
242 static int syncdelay = 30; /* max time to delay syncing data */
243 static int filedelay = 30; /* time to delay syncing files */
244 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0,
245 "Time to delay syncing files (in seconds)");
246 static int dirdelay = 29; /* time to delay syncing directories */
247 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0,
248 "Time to delay syncing directories (in seconds)");
249 static int metadelay = 28; /* time to delay syncing metadata */
250 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0,
251 "Time to delay syncing metadata (in seconds)");
252 static int rushjob; /* number of slots to run ASAP */
253 static int stat_rush_requests; /* number of times I/O speeded up */
254 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0,
255 "Number of times I/O speeded up (rush requests)");
258 * When shutting down the syncer, run it at four times normal speed.
260 #define SYNCER_SHUTDOWN_SPEEDUP 4
261 static int sync_vnode_count;
262 static int syncer_worklist_len;
263 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
267 * Number of vnodes we want to exist at any one time. This is mostly used
268 * to size hash tables in vnode-related code. It is normally not used in
269 * getnewvnode(), as wantfreevnodes is normally nonzero.)
271 * XXX desiredvnodes is historical cruft and should not exist.
274 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
275 &desiredvnodes, 0, "Maximum number of vnodes");
276 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
277 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
278 static int vnlru_nowhere;
279 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
280 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
283 * Macros to control when a vnode is freed and recycled. All require
284 * the vnode interlock.
286 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
287 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
288 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
292 * Initialize the vnode management data structures.
294 * Reevaluate the following cap on the number of vnodes after the physical
295 * memory size exceeds 512GB. In the limit, as the physical memory size
296 * grows, the ratio of physical pages to vnodes approaches sixteen to one.
298 #ifndef MAXVNODES_MAX
299 #define MAXVNODES_MAX (512 * (1024 * 1024 * 1024 / (int)PAGE_SIZE / 16))
302 vntblinit(void *dummy __unused)
304 int physvnodes, virtvnodes;
307 * Desiredvnodes is a function of the physical memory size and the
308 * kernel's heap size. Generally speaking, it scales with the
309 * physical memory size. The ratio of desiredvnodes to physical pages
310 * is one to four until desiredvnodes exceeds 98,304. Thereafter, the
311 * marginal ratio of desiredvnodes to physical pages is one to
312 * sixteen. However, desiredvnodes is limited by the kernel's heap
313 * size. The memory required by desiredvnodes vnodes and vm objects
314 * may not exceed one seventh of the kernel's heap size.
316 physvnodes = maxproc + cnt.v_page_count / 16 + 3 * min(98304 * 4,
317 cnt.v_page_count) / 16;
318 virtvnodes = vm_kmem_size / (7 * (sizeof(struct vm_object) +
319 sizeof(struct vnode)));
320 desiredvnodes = min(physvnodes, virtvnodes);
321 if (desiredvnodes > MAXVNODES_MAX) {
323 printf("Reducing kern.maxvnodes %d -> %d\n",
324 desiredvnodes, MAXVNODES_MAX);
325 desiredvnodes = MAXVNODES_MAX;
327 wantfreevnodes = desiredvnodes / 4;
328 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
329 TAILQ_INIT(&vnode_free_list);
330 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
331 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
332 NULL, NULL, UMA_ALIGN_PTR, 0);
333 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
334 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
336 * Initialize the filesystem syncer.
338 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
340 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
342 syncer_maxdelay = syncer_mask + 1;
343 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
344 cv_init(&sync_wakeup, "syncer");
346 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
350 * Mark a mount point as busy. Used to synchronize access and to delay
351 * unmounting. Eventually, mountlist_mtx is not released on failure.
354 vfs_busy(struct mount *mp, int flags)
357 MPASS((flags & ~MBF_MASK) == 0);
358 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
363 * If mount point is currenly being unmounted, sleep until the
364 * mount point fate is decided. If thread doing the unmounting fails,
365 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
366 * that this mount point has survived the unmount attempt and vfs_busy
367 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
368 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
369 * about to be really destroyed. vfs_busy needs to release its
370 * reference on the mount point in this case and return with ENOENT,
371 * telling the caller that mount mount it tried to busy is no longer
374 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
375 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
378 CTR1(KTR_VFS, "%s: failed busying before sleeping",
382 if (flags & MBF_MNTLSTLOCK)
383 mtx_unlock(&mountlist_mtx);
384 mp->mnt_kern_flag |= MNTK_MWAIT;
385 msleep(mp, MNT_MTX(mp), PVFS, "vfs_busy", 0);
386 if (flags & MBF_MNTLSTLOCK)
387 mtx_lock(&mountlist_mtx);
389 if (flags & MBF_MNTLSTLOCK)
390 mtx_unlock(&mountlist_mtx);
397 * Free a busy filesystem.
400 vfs_unbusy(struct mount *mp)
403 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
406 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
408 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
409 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
410 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
411 mp->mnt_kern_flag &= ~MNTK_DRAINING;
412 wakeup(&mp->mnt_lockref);
418 * Lookup a mount point by filesystem identifier.
421 vfs_getvfs(fsid_t *fsid)
425 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
426 mtx_lock(&mountlist_mtx);
427 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
428 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
429 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
431 mtx_unlock(&mountlist_mtx);
435 mtx_unlock(&mountlist_mtx);
436 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
437 return ((struct mount *) 0);
441 * Lookup a mount point by filesystem identifier, busying it before
445 vfs_busyfs(fsid_t *fsid)
450 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
451 mtx_lock(&mountlist_mtx);
452 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
453 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
454 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
455 error = vfs_busy(mp, MBF_MNTLSTLOCK);
457 mtx_unlock(&mountlist_mtx);
463 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
464 mtx_unlock(&mountlist_mtx);
465 return ((struct mount *) 0);
469 * Check if a user can access privileged mount options.
472 vfs_suser(struct mount *mp, struct thread *td)
477 * If the thread is jailed, but this is not a jail-friendly file
478 * system, deny immediately.
480 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
484 * If the file system was mounted outside the jail of the calling
485 * thread, deny immediately.
487 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
491 * If file system supports delegated administration, we don't check
492 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
493 * by the file system itself.
494 * If this is not the user that did original mount, we check for
495 * the PRIV_VFS_MOUNT_OWNER privilege.
497 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
498 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
499 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
506 * Get a new unique fsid. Try to make its val[0] unique, since this value
507 * will be used to create fake device numbers for stat(). Also try (but
508 * not so hard) make its val[0] unique mod 2^16, since some emulators only
509 * support 16-bit device numbers. We end up with unique val[0]'s for the
510 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
512 * Keep in mind that several mounts may be running in parallel. Starting
513 * the search one past where the previous search terminated is both a
514 * micro-optimization and a defense against returning the same fsid to
518 vfs_getnewfsid(struct mount *mp)
520 static u_int16_t mntid_base;
525 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
526 mtx_lock(&mntid_mtx);
527 mtype = mp->mnt_vfc->vfc_typenum;
528 tfsid.val[1] = mtype;
529 mtype = (mtype & 0xFF) << 24;
531 tfsid.val[0] = makedev(255,
532 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
534 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
538 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
539 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
540 mtx_unlock(&mntid_mtx);
544 * Knob to control the precision of file timestamps:
546 * 0 = seconds only; nanoseconds zeroed.
547 * 1 = seconds and nanoseconds, accurate within 1/HZ.
548 * 2 = seconds and nanoseconds, truncated to microseconds.
549 * >=3 = seconds and nanoseconds, maximum precision.
551 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
553 static int timestamp_precision = TSP_SEC;
554 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
555 ×tamp_precision, 0, "File timestamp precision (0: seconds, "
556 "1: sec + ns accurate to 1/HZ, 2: sec + ns truncated to ms, "
557 "3+: sec + ns (max. precision))");
560 * Get a current timestamp.
563 vfs_timestamp(struct timespec *tsp)
567 switch (timestamp_precision) {
569 tsp->tv_sec = time_second;
577 TIMEVAL_TO_TIMESPEC(&tv, tsp);
587 * Set vnode attributes to VNOVAL
590 vattr_null(struct vattr *vap)
594 vap->va_size = VNOVAL;
595 vap->va_bytes = VNOVAL;
596 vap->va_mode = VNOVAL;
597 vap->va_nlink = VNOVAL;
598 vap->va_uid = VNOVAL;
599 vap->va_gid = VNOVAL;
600 vap->va_fsid = VNOVAL;
601 vap->va_fileid = VNOVAL;
602 vap->va_blocksize = VNOVAL;
603 vap->va_rdev = VNOVAL;
604 vap->va_atime.tv_sec = VNOVAL;
605 vap->va_atime.tv_nsec = VNOVAL;
606 vap->va_mtime.tv_sec = VNOVAL;
607 vap->va_mtime.tv_nsec = VNOVAL;
608 vap->va_ctime.tv_sec = VNOVAL;
609 vap->va_ctime.tv_nsec = VNOVAL;
610 vap->va_birthtime.tv_sec = VNOVAL;
611 vap->va_birthtime.tv_nsec = VNOVAL;
612 vap->va_flags = VNOVAL;
613 vap->va_gen = VNOVAL;
618 * This routine is called when we have too many vnodes. It attempts
619 * to free <count> vnodes and will potentially free vnodes that still
620 * have VM backing store (VM backing store is typically the cause
621 * of a vnode blowout so we want to do this). Therefore, this operation
622 * is not considered cheap.
624 * A number of conditions may prevent a vnode from being reclaimed.
625 * the buffer cache may have references on the vnode, a directory
626 * vnode may still have references due to the namei cache representing
627 * underlying files, or the vnode may be in active use. It is not
628 * desireable to reuse such vnodes. These conditions may cause the
629 * number of vnodes to reach some minimum value regardless of what
630 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
633 vlrureclaim(struct mount *mp)
642 * Calculate the trigger point, don't allow user
643 * screwups to blow us up. This prevents us from
644 * recycling vnodes with lots of resident pages. We
645 * aren't trying to free memory, we are trying to
648 usevnodes = desiredvnodes;
651 trigger = cnt.v_page_count * 2 / usevnodes;
653 vn_start_write(NULL, &mp, V_WAIT);
655 count = mp->mnt_nvnodelistsize / 10 + 1;
657 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
658 while (vp != NULL && vp->v_type == VMARKER)
659 vp = TAILQ_NEXT(vp, v_nmntvnodes);
662 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
663 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
668 * If it's been deconstructed already, it's still
669 * referenced, or it exceeds the trigger, skip it.
671 if (vp->v_usecount ||
672 (!vlru_allow_cache_src &&
673 !LIST_EMPTY(&(vp)->v_cache_src)) ||
674 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
675 vp->v_object->resident_page_count > trigger)) {
681 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
683 goto next_iter_mntunlocked;
687 * v_usecount may have been bumped after VOP_LOCK() dropped
688 * the vnode interlock and before it was locked again.
690 * It is not necessary to recheck VI_DOOMED because it can
691 * only be set by another thread that holds both the vnode
692 * lock and vnode interlock. If another thread has the
693 * vnode lock before we get to VOP_LOCK() and obtains the
694 * vnode interlock after VOP_LOCK() drops the vnode
695 * interlock, the other thread will be unable to drop the
696 * vnode lock before our VOP_LOCK() call fails.
698 if (vp->v_usecount ||
699 (!vlru_allow_cache_src &&
700 !LIST_EMPTY(&(vp)->v_cache_src)) ||
701 (vp->v_object != NULL &&
702 vp->v_object->resident_page_count > trigger)) {
703 VOP_UNLOCK(vp, LK_INTERLOCK);
704 goto next_iter_mntunlocked;
706 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
707 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
712 next_iter_mntunlocked:
713 if ((count % 256) != 0)
717 if ((count % 256) != 0)
726 vn_finished_write(mp);
731 * Attempt to keep the free list at wantfreevnodes length.
734 vnlru_free(int count)
739 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
740 for (; count > 0; count--) {
741 vp = TAILQ_FIRST(&vnode_free_list);
743 * The list can be modified while the free_list_mtx
744 * has been dropped and vp could be NULL here.
748 VNASSERT(vp->v_op != NULL, vp,
749 ("vnlru_free: vnode already reclaimed."));
750 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
752 * Don't recycle if we can't get the interlock.
754 if (!VI_TRYLOCK(vp)) {
755 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
758 VNASSERT(VCANRECYCLE(vp), vp,
759 ("vp inconsistent on freelist"));
761 vp->v_iflag &= ~VI_FREE;
763 mtx_unlock(&vnode_free_list_mtx);
765 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
767 VFS_UNLOCK_GIANT(vfslocked);
769 * If the recycled succeeded this vdrop will actually free
770 * the vnode. If not it will simply place it back on
774 mtx_lock(&vnode_free_list_mtx);
778 * Attempt to recycle vnodes in a context that is always safe to block.
779 * Calling vlrurecycle() from the bowels of filesystem code has some
780 * interesting deadlock problems.
782 static struct proc *vnlruproc;
783 static int vnlruproc_sig;
788 struct mount *mp, *nmp;
790 struct proc *p = vnlruproc;
792 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
796 kproc_suspend_check(p);
797 mtx_lock(&vnode_free_list_mtx);
798 if (freevnodes > wantfreevnodes)
799 vnlru_free(freevnodes - wantfreevnodes);
800 if (numvnodes <= desiredvnodes * 9 / 10) {
802 wakeup(&vnlruproc_sig);
803 msleep(vnlruproc, &vnode_free_list_mtx,
804 PVFS|PDROP, "vlruwt", hz);
807 mtx_unlock(&vnode_free_list_mtx);
809 mtx_lock(&mountlist_mtx);
810 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
811 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
812 nmp = TAILQ_NEXT(mp, mnt_list);
815 vfslocked = VFS_LOCK_GIANT(mp);
816 done += vlrureclaim(mp);
817 VFS_UNLOCK_GIANT(vfslocked);
818 mtx_lock(&mountlist_mtx);
819 nmp = TAILQ_NEXT(mp, mnt_list);
822 mtx_unlock(&mountlist_mtx);
825 /* These messages are temporary debugging aids */
826 if (vnlru_nowhere < 5)
827 printf("vnlru process getting nowhere..\n");
828 else if (vnlru_nowhere == 5)
829 printf("vnlru process messages stopped.\n");
832 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
838 static struct kproc_desc vnlru_kp = {
843 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
847 * Routines having to do with the management of the vnode table.
851 vdestroy(struct vnode *vp)
855 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
856 mtx_lock(&vnode_free_list_mtx);
858 mtx_unlock(&vnode_free_list_mtx);
860 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
861 ("cleaned vnode still on the free list."));
862 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
863 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
864 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
865 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
866 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
867 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
868 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
869 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
870 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
871 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
872 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
873 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
876 mac_vnode_destroy(vp);
878 if (vp->v_pollinfo != NULL)
879 destroy_vpollinfo(vp->v_pollinfo);
881 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
884 lockdestroy(vp->v_vnlock);
885 mtx_destroy(&vp->v_interlock);
886 mtx_destroy(BO_MTX(bo));
887 uma_zfree(vnode_zone, vp);
891 * Try to recycle a freed vnode. We abort if anyone picks up a reference
892 * before we actually vgone(). This function must be called with the vnode
893 * held to prevent the vnode from being returned to the free list midway
897 vtryrecycle(struct vnode *vp)
901 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
902 VNASSERT(vp->v_holdcnt, vp,
903 ("vtryrecycle: Recycling vp %p without a reference.", vp));
905 * This vnode may found and locked via some other list, if so we
906 * can't recycle it yet.
908 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
910 "%s: impossible to recycle, vp %p lock is already held",
912 return (EWOULDBLOCK);
915 * Don't recycle if its filesystem is being suspended.
917 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
920 "%s: impossible to recycle, cannot start the write for %p",
925 * If we got this far, we need to acquire the interlock and see if
926 * anyone picked up this vnode from another list. If not, we will
927 * mark it with DOOMED via vgonel() so that anyone who does find it
931 if (vp->v_usecount) {
932 VOP_UNLOCK(vp, LK_INTERLOCK);
933 vn_finished_write(vnmp);
935 "%s: impossible to recycle, %p is already referenced",
939 if ((vp->v_iflag & VI_DOOMED) == 0)
941 VOP_UNLOCK(vp, LK_INTERLOCK);
942 vn_finished_write(vnmp);
947 * Return the next vnode from the free list.
950 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
953 struct vnode *vp = NULL;
956 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
957 mtx_lock(&vnode_free_list_mtx);
959 * Lend our context to reclaim vnodes if they've exceeded the max.
961 if (freevnodes > wantfreevnodes)
964 * Wait for available vnodes.
966 if (numvnodes > desiredvnodes) {
967 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
969 * File system is beeing suspended, we cannot risk a
970 * deadlock here, so allocate new vnode anyway.
972 if (freevnodes > wantfreevnodes)
973 vnlru_free(freevnodes - wantfreevnodes);
976 if (vnlruproc_sig == 0) {
977 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
980 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
982 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
983 if (numvnodes > desiredvnodes) {
984 mtx_unlock(&vnode_free_list_mtx);
991 mtx_unlock(&vnode_free_list_mtx);
992 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
996 vp->v_vnlock = &vp->v_lock;
997 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
999 * By default, don't allow shared locks unless filesystems
1002 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
1004 * Initialize bufobj.
1007 bo->__bo_vnode = vp;
1008 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
1009 bo->bo_ops = &buf_ops_bio;
1010 bo->bo_private = vp;
1011 TAILQ_INIT(&bo->bo_clean.bv_hd);
1012 TAILQ_INIT(&bo->bo_dirty.bv_hd);
1014 * Initialize namecache.
1016 LIST_INIT(&vp->v_cache_src);
1017 TAILQ_INIT(&vp->v_cache_dst);
1019 * Finalize various vnode identity bits.
1024 v_incr_usecount(vp);
1028 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1029 mac_vnode_associate_singlelabel(mp, vp);
1030 else if (mp == NULL && vops != &dead_vnodeops)
1031 printf("NULL mp in getnewvnode()\n");
1034 bo->bo_bsize = mp->mnt_stat.f_iosize;
1035 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1036 vp->v_vflag |= VV_NOKNOTE;
1044 * Delete from old mount point vnode list, if on one.
1047 delmntque(struct vnode *vp)
1056 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1057 ("bad mount point vnode list size"));
1058 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1059 mp->mnt_nvnodelistsize--;
1065 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1069 vp->v_op = &dead_vnodeops;
1070 /* XXX non mp-safe fs may still call insmntque with vnode
1072 if (!VOP_ISLOCKED(vp))
1073 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1079 * Insert into list of vnodes for the new mount point, if available.
1082 insmntque1(struct vnode *vp, struct mount *mp,
1083 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1087 KASSERT(vp->v_mount == NULL,
1088 ("insmntque: vnode already on per mount vnode list"));
1089 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1090 #ifdef DEBUG_VFS_LOCKS
1091 if (!VFS_NEEDSGIANT(mp))
1092 ASSERT_VOP_ELOCKED(vp,
1093 "insmntque: mp-safe fs and non-locked vp");
1096 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1097 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1098 mp->mnt_nvnodelistsize == 0)) {
1099 locked = VOP_ISLOCKED(vp);
1100 if (!locked || (locked == LK_EXCLUSIVE &&
1101 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1110 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1111 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1112 ("neg mount point vnode list size"));
1113 mp->mnt_nvnodelistsize++;
1119 insmntque(struct vnode *vp, struct mount *mp)
1122 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1126 * Flush out and invalidate all buffers associated with a bufobj
1127 * Called with the underlying object locked.
1130 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1135 if (flags & V_SAVE) {
1136 error = bufobj_wwait(bo, slpflag, slptimeo);
1141 if (bo->bo_dirty.bv_cnt > 0) {
1143 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1146 * XXX We could save a lock/unlock if this was only
1147 * enabled under INVARIANTS
1150 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1151 panic("vinvalbuf: dirty bufs");
1155 * If you alter this loop please notice that interlock is dropped and
1156 * reacquired in flushbuflist. Special care is needed to ensure that
1157 * no race conditions occur from this.
1160 error = flushbuflist(&bo->bo_clean,
1161 flags, bo, slpflag, slptimeo);
1163 error = flushbuflist(&bo->bo_dirty,
1164 flags, bo, slpflag, slptimeo);
1165 if (error != 0 && error != EAGAIN) {
1169 } while (error != 0);
1172 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1173 * have write I/O in-progress but if there is a VM object then the
1174 * VM object can also have read-I/O in-progress.
1177 bufobj_wwait(bo, 0, 0);
1179 if (bo->bo_object != NULL) {
1180 VM_OBJECT_LOCK(bo->bo_object);
1181 vm_object_pip_wait(bo->bo_object, "bovlbx");
1182 VM_OBJECT_UNLOCK(bo->bo_object);
1185 } while (bo->bo_numoutput > 0);
1189 * Destroy the copy in the VM cache, too.
1191 if (bo->bo_object != NULL && (flags & (V_ALT | V_NORMAL)) == 0) {
1192 VM_OBJECT_LOCK(bo->bo_object);
1193 vm_object_page_remove(bo->bo_object, 0, 0,
1194 (flags & V_SAVE) ? TRUE : FALSE);
1195 VM_OBJECT_UNLOCK(bo->bo_object);
1200 if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1201 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1202 panic("vinvalbuf: flush failed");
1209 * Flush out and invalidate all buffers associated with a vnode.
1210 * Called with the underlying object locked.
1213 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1216 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1217 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1218 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1222 * Flush out buffers on the specified list.
1226 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1229 struct buf *bp, *nbp;
1234 ASSERT_BO_LOCKED(bo);
1237 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1238 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1239 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1245 lblkno = nbp->b_lblkno;
1246 xflags = nbp->b_xflags &
1247 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1250 error = BUF_TIMELOCK(bp,
1251 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1252 "flushbuf", slpflag, slptimeo);
1255 return (error != ENOLCK ? error : EAGAIN);
1257 KASSERT(bp->b_bufobj == bo,
1258 ("bp %p wrong b_bufobj %p should be %p",
1259 bp, bp->b_bufobj, bo));
1260 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1266 * XXX Since there are no node locks for NFS, I
1267 * believe there is a slight chance that a delayed
1268 * write will occur while sleeping just above, so
1271 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1276 bp->b_flags |= B_ASYNC;
1279 return (EAGAIN); /* XXX: why not loop ? */
1284 bp->b_flags |= (B_INVAL | B_RELBUF);
1285 bp->b_flags &= ~B_ASYNC;
1289 (nbp->b_bufobj != bo ||
1290 nbp->b_lblkno != lblkno ||
1292 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1293 break; /* nbp invalid */
1299 * Truncate a file's buffer and pages to a specified length. This
1300 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1304 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1305 off_t length, int blksize)
1307 struct buf *bp, *nbp;
1312 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1313 vp, cred, blksize, (uintmax_t)length);
1316 * Round up to the *next* lbn.
1318 trunclbn = (length + blksize - 1) / blksize;
1320 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1327 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1328 if (bp->b_lblkno < trunclbn)
1331 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1332 BO_MTX(bo)) == ENOLCK)
1338 bp->b_flags |= (B_INVAL | B_RELBUF);
1339 bp->b_flags &= ~B_ASYNC;
1345 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1346 (nbp->b_vp != vp) ||
1347 (nbp->b_flags & B_DELWRI))) {
1353 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1354 if (bp->b_lblkno < trunclbn)
1357 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1358 BO_MTX(bo)) == ENOLCK)
1363 bp->b_flags |= (B_INVAL | B_RELBUF);
1364 bp->b_flags &= ~B_ASYNC;
1370 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1371 (nbp->b_vp != vp) ||
1372 (nbp->b_flags & B_DELWRI) == 0)) {
1381 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1382 if (bp->b_lblkno > 0)
1385 * Since we hold the vnode lock this should only
1386 * fail if we're racing with the buf daemon.
1389 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1390 BO_MTX(bo)) == ENOLCK) {
1393 VNASSERT((bp->b_flags & B_DELWRI), vp,
1394 ("buf(%p) on dirty queue without DELWRI", bp));
1405 bufobj_wwait(bo, 0, 0);
1407 vnode_pager_setsize(vp, length);
1413 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1416 * NOTE: We have to deal with the special case of a background bitmap
1417 * buffer, a situation where two buffers will have the same logical
1418 * block offset. We want (1) only the foreground buffer to be accessed
1419 * in a lookup and (2) must differentiate between the foreground and
1420 * background buffer in the splay tree algorithm because the splay
1421 * tree cannot normally handle multiple entities with the same 'index'.
1422 * We accomplish this by adding differentiating flags to the splay tree's
1427 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1430 struct buf *lefttreemax, *righttreemin, *y;
1434 lefttreemax = righttreemin = &dummy;
1436 if (lblkno < root->b_lblkno ||
1437 (lblkno == root->b_lblkno &&
1438 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1439 if ((y = root->b_left) == NULL)
1441 if (lblkno < y->b_lblkno) {
1443 root->b_left = y->b_right;
1446 if ((y = root->b_left) == NULL)
1449 /* Link into the new root's right tree. */
1450 righttreemin->b_left = root;
1451 righttreemin = root;
1452 } else if (lblkno > root->b_lblkno ||
1453 (lblkno == root->b_lblkno &&
1454 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1455 if ((y = root->b_right) == NULL)
1457 if (lblkno > y->b_lblkno) {
1459 root->b_right = y->b_left;
1462 if ((y = root->b_right) == NULL)
1465 /* Link into the new root's left tree. */
1466 lefttreemax->b_right = root;
1473 /* Assemble the new root. */
1474 lefttreemax->b_right = root->b_left;
1475 righttreemin->b_left = root->b_right;
1476 root->b_left = dummy.b_right;
1477 root->b_right = dummy.b_left;
1482 buf_vlist_remove(struct buf *bp)
1487 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1488 ASSERT_BO_LOCKED(bp->b_bufobj);
1489 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1490 (BX_VNDIRTY|BX_VNCLEAN),
1491 ("buf_vlist_remove: Buf %p is on two lists", bp));
1492 if (bp->b_xflags & BX_VNDIRTY)
1493 bv = &bp->b_bufobj->bo_dirty;
1495 bv = &bp->b_bufobj->bo_clean;
1496 if (bp != bv->bv_root) {
1497 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1498 KASSERT(root == bp, ("splay lookup failed in remove"));
1500 if (bp->b_left == NULL) {
1503 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1504 root->b_right = bp->b_right;
1507 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1509 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1513 * Add the buffer to the sorted clean or dirty block list using a
1514 * splay tree algorithm.
1516 * NOTE: xflags is passed as a constant, optimizing this inline function!
1519 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1524 ASSERT_BO_LOCKED(bo);
1525 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1526 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1527 bp->b_xflags |= xflags;
1528 if (xflags & BX_VNDIRTY)
1533 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1537 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1538 } else if (bp->b_lblkno < root->b_lblkno ||
1539 (bp->b_lblkno == root->b_lblkno &&
1540 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1541 bp->b_left = root->b_left;
1543 root->b_left = NULL;
1544 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1546 bp->b_right = root->b_right;
1548 root->b_right = NULL;
1549 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1556 * Lookup a buffer using the splay tree. Note that we specifically avoid
1557 * shadow buffers used in background bitmap writes.
1559 * This code isn't quite efficient as it could be because we are maintaining
1560 * two sorted lists and do not know which list the block resides in.
1562 * During a "make buildworld" the desired buffer is found at one of
1563 * the roots more than 60% of the time. Thus, checking both roots
1564 * before performing either splay eliminates unnecessary splays on the
1565 * first tree splayed.
1568 gbincore(struct bufobj *bo, daddr_t lblkno)
1572 ASSERT_BO_LOCKED(bo);
1573 if ((bp = bo->bo_clean.bv_root) != NULL &&
1574 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1576 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1577 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1579 if ((bp = bo->bo_clean.bv_root) != NULL) {
1580 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1581 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1584 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1585 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1586 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1593 * Associate a buffer with a vnode.
1596 bgetvp(struct vnode *vp, struct buf *bp)
1601 ASSERT_BO_LOCKED(bo);
1602 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1604 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1605 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1606 ("bgetvp: bp already attached! %p", bp));
1609 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1610 bp->b_flags |= B_NEEDSGIANT;
1614 * Insert onto list for new vnode.
1616 buf_vlist_add(bp, bo, BX_VNCLEAN);
1620 * Disassociate a buffer from a vnode.
1623 brelvp(struct buf *bp)
1628 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1629 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1632 * Delete from old vnode list, if on one.
1634 vp = bp->b_vp; /* XXX */
1637 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1638 buf_vlist_remove(bp);
1640 panic("brelvp: Buffer %p not on queue.", bp);
1641 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1642 bo->bo_flag &= ~BO_ONWORKLST;
1643 mtx_lock(&sync_mtx);
1644 LIST_REMOVE(bo, bo_synclist);
1645 syncer_worklist_len--;
1646 mtx_unlock(&sync_mtx);
1648 bp->b_flags &= ~B_NEEDSGIANT;
1650 bp->b_bufobj = NULL;
1656 * Add an item to the syncer work queue.
1659 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1663 ASSERT_BO_LOCKED(bo);
1665 mtx_lock(&sync_mtx);
1666 if (bo->bo_flag & BO_ONWORKLST)
1667 LIST_REMOVE(bo, bo_synclist);
1669 bo->bo_flag |= BO_ONWORKLST;
1670 syncer_worklist_len++;
1673 if (delay > syncer_maxdelay - 2)
1674 delay = syncer_maxdelay - 2;
1675 slot = (syncer_delayno + delay) & syncer_mask;
1677 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1679 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1681 mtx_unlock(&sync_mtx);
1685 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1689 mtx_lock(&sync_mtx);
1690 len = syncer_worklist_len - sync_vnode_count;
1691 mtx_unlock(&sync_mtx);
1692 error = SYSCTL_OUT(req, &len, sizeof(len));
1696 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1697 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1699 static struct proc *updateproc;
1700 static void sched_sync(void);
1701 static struct kproc_desc up_kp = {
1706 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1709 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1714 *bo = LIST_FIRST(slp);
1717 vp = (*bo)->__bo_vnode; /* XXX */
1718 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1721 * We use vhold in case the vnode does not
1722 * successfully sync. vhold prevents the vnode from
1723 * going away when we unlock the sync_mtx so that
1724 * we can acquire the vnode interlock.
1727 mtx_unlock(&sync_mtx);
1729 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1731 mtx_lock(&sync_mtx);
1732 return (*bo == LIST_FIRST(slp));
1734 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1735 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1737 vn_finished_write(mp);
1739 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1741 * Put us back on the worklist. The worklist
1742 * routine will remove us from our current
1743 * position and then add us back in at a later
1746 vn_syncer_add_to_worklist(*bo, syncdelay);
1750 mtx_lock(&sync_mtx);
1755 * System filesystem synchronizer daemon.
1760 struct synclist *gnext, *next;
1761 struct synclist *gslp, *slp;
1764 struct thread *td = curthread;
1766 int net_worklist_len;
1767 int syncer_final_iter;
1772 syncer_final_iter = 0;
1774 syncer_state = SYNCER_RUNNING;
1775 starttime = time_uptime;
1776 td->td_pflags |= TDP_NORUNNINGBUF;
1778 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1781 mtx_lock(&sync_mtx);
1783 if (syncer_state == SYNCER_FINAL_DELAY &&
1784 syncer_final_iter == 0) {
1785 mtx_unlock(&sync_mtx);
1786 kproc_suspend_check(td->td_proc);
1787 mtx_lock(&sync_mtx);
1789 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1790 if (syncer_state != SYNCER_RUNNING &&
1791 starttime != time_uptime) {
1793 printf("\nSyncing disks, vnodes remaining...");
1796 printf("%d ", net_worklist_len);
1798 starttime = time_uptime;
1801 * Push files whose dirty time has expired. Be careful
1802 * of interrupt race on slp queue.
1804 * Skip over empty worklist slots when shutting down.
1807 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1808 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1809 syncer_delayno += 1;
1810 if (syncer_delayno == syncer_maxdelay)
1812 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1813 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1815 * If the worklist has wrapped since the
1816 * it was emptied of all but syncer vnodes,
1817 * switch to the FINAL_DELAY state and run
1818 * for one more second.
1820 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1821 net_worklist_len == 0 &&
1822 last_work_seen == syncer_delayno) {
1823 syncer_state = SYNCER_FINAL_DELAY;
1824 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1826 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1827 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1830 * Keep track of the last time there was anything
1831 * on the worklist other than syncer vnodes.
1832 * Return to the SHUTTING_DOWN state if any
1835 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1836 last_work_seen = syncer_delayno;
1837 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1838 syncer_state = SYNCER_SHUTTING_DOWN;
1839 while (!LIST_EMPTY(slp)) {
1840 error = sync_vnode(slp, &bo, td);
1842 LIST_REMOVE(bo, bo_synclist);
1843 LIST_INSERT_HEAD(next, bo, bo_synclist);
1847 if (!LIST_EMPTY(gslp)) {
1848 mtx_unlock(&sync_mtx);
1850 mtx_lock(&sync_mtx);
1851 while (!LIST_EMPTY(gslp)) {
1852 error = sync_vnode(gslp, &bo, td);
1854 LIST_REMOVE(bo, bo_synclist);
1855 LIST_INSERT_HEAD(gnext, bo,
1862 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1863 syncer_final_iter--;
1865 * The variable rushjob allows the kernel to speed up the
1866 * processing of the filesystem syncer process. A rushjob
1867 * value of N tells the filesystem syncer to process the next
1868 * N seconds worth of work on its queue ASAP. Currently rushjob
1869 * is used by the soft update code to speed up the filesystem
1870 * syncer process when the incore state is getting so far
1871 * ahead of the disk that the kernel memory pool is being
1872 * threatened with exhaustion.
1879 * Just sleep for a short period of time between
1880 * iterations when shutting down to allow some I/O
1883 * If it has taken us less than a second to process the
1884 * current work, then wait. Otherwise start right over
1885 * again. We can still lose time if any single round
1886 * takes more than two seconds, but it does not really
1887 * matter as we are just trying to generally pace the
1888 * filesystem activity.
1890 if (syncer_state != SYNCER_RUNNING ||
1891 time_uptime == starttime) {
1893 sched_prio(td, PPAUSE);
1896 if (syncer_state != SYNCER_RUNNING)
1897 cv_timedwait(&sync_wakeup, &sync_mtx,
1898 hz / SYNCER_SHUTDOWN_SPEEDUP);
1899 else if (time_uptime == starttime)
1900 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1905 * Request the syncer daemon to speed up its work.
1906 * We never push it to speed up more than half of its
1907 * normal turn time, otherwise it could take over the cpu.
1910 speedup_syncer(void)
1914 mtx_lock(&sync_mtx);
1915 if (rushjob < syncdelay / 2) {
1917 stat_rush_requests += 1;
1920 mtx_unlock(&sync_mtx);
1921 cv_broadcast(&sync_wakeup);
1926 * Tell the syncer to speed up its work and run though its work
1927 * list several times, then tell it to shut down.
1930 syncer_shutdown(void *arg, int howto)
1933 if (howto & RB_NOSYNC)
1935 mtx_lock(&sync_mtx);
1936 syncer_state = SYNCER_SHUTTING_DOWN;
1938 mtx_unlock(&sync_mtx);
1939 cv_broadcast(&sync_wakeup);
1940 kproc_shutdown(arg, howto);
1944 * Reassign a buffer from one vnode to another.
1945 * Used to assign file specific control information
1946 * (indirect blocks) to the vnode to which they belong.
1949 reassignbuf(struct buf *bp)
1962 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
1963 bp, bp->b_vp, bp->b_flags);
1965 * B_PAGING flagged buffers cannot be reassigned because their vp
1966 * is not fully linked in.
1968 if (bp->b_flags & B_PAGING)
1969 panic("cannot reassign paging buffer");
1972 * Delete from old vnode list, if on one.
1975 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1976 buf_vlist_remove(bp);
1978 panic("reassignbuf: Buffer %p not on queue.", bp);
1980 * If dirty, put on list of dirty buffers; otherwise insert onto list
1983 if (bp->b_flags & B_DELWRI) {
1984 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
1985 switch (vp->v_type) {
1995 vn_syncer_add_to_worklist(bo, delay);
1997 buf_vlist_add(bp, bo, BX_VNDIRTY);
1999 buf_vlist_add(bp, bo, BX_VNCLEAN);
2001 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
2002 mtx_lock(&sync_mtx);
2003 LIST_REMOVE(bo, bo_synclist);
2004 syncer_worklist_len--;
2005 mtx_unlock(&sync_mtx);
2006 bo->bo_flag &= ~BO_ONWORKLST;
2011 bp = TAILQ_FIRST(&bv->bv_hd);
2012 KASSERT(bp == NULL || bp->b_bufobj == bo,
2013 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2014 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2015 KASSERT(bp == NULL || bp->b_bufobj == bo,
2016 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2018 bp = TAILQ_FIRST(&bv->bv_hd);
2019 KASSERT(bp == NULL || bp->b_bufobj == bo,
2020 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2021 bp = TAILQ_LAST(&bv->bv_hd, buflists);
2022 KASSERT(bp == NULL || bp->b_bufobj == bo,
2023 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
2029 * Increment the use and hold counts on the vnode, taking care to reference
2030 * the driver's usecount if this is a chardev. The vholdl() will remove
2031 * the vnode from the free list if it is presently free. Requires the
2032 * vnode interlock and returns with it held.
2035 v_incr_usecount(struct vnode *vp)
2038 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2040 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2042 vp->v_rdev->si_usecount++;
2049 * Turn a holdcnt into a use+holdcnt such that only one call to
2050 * v_decr_usecount is needed.
2053 v_upgrade_usecount(struct vnode *vp)
2056 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2058 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2060 vp->v_rdev->si_usecount++;
2066 * Decrement the vnode use and hold count along with the driver's usecount
2067 * if this is a chardev. The vdropl() below releases the vnode interlock
2068 * as it may free the vnode.
2071 v_decr_usecount(struct vnode *vp)
2074 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2075 VNASSERT(vp->v_usecount > 0, vp,
2076 ("v_decr_usecount: negative usecount"));
2077 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2079 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2081 vp->v_rdev->si_usecount--;
2088 * Decrement only the use count and driver use count. This is intended to
2089 * be paired with a follow on vdropl() to release the remaining hold count.
2090 * In this way we may vgone() a vnode with a 0 usecount without risk of
2091 * having it end up on a free list because the hold count is kept above 0.
2094 v_decr_useonly(struct vnode *vp)
2097 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2098 VNASSERT(vp->v_usecount > 0, vp,
2099 ("v_decr_useonly: negative usecount"));
2100 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2102 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2104 vp->v_rdev->si_usecount--;
2110 * Grab a particular vnode from the free list, increment its
2111 * reference count and lock it. VI_DOOMED is set if the vnode
2112 * is being destroyed. Only callers who specify LK_RETRY will
2113 * see doomed vnodes. If inactive processing was delayed in
2114 * vput try to do it here.
2117 vget(struct vnode *vp, int flags, struct thread *td)
2122 VFS_ASSERT_GIANT(vp->v_mount);
2123 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2124 ("vget: invalid lock operation"));
2125 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2127 if ((flags & LK_INTERLOCK) == 0)
2130 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2132 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2136 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2137 panic("vget: vn_lock failed to return ENOENT\n");
2139 /* Upgrade our holdcnt to a usecount. */
2140 v_upgrade_usecount(vp);
2142 * We don't guarantee that any particular close will
2143 * trigger inactive processing so just make a best effort
2144 * here at preventing a reference to a removed file. If
2145 * we don't succeed no harm is done.
2147 if (vp->v_iflag & VI_OWEINACT) {
2148 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2149 (flags & LK_NOWAIT) == 0)
2151 vp->v_iflag &= ~VI_OWEINACT;
2158 * Increase the reference count of a vnode.
2161 vref(struct vnode *vp)
2164 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2166 v_incr_usecount(vp);
2171 * Return reference count of a vnode.
2173 * The results of this call are only guaranteed when some mechanism other
2174 * than the VI lock is used to stop other processes from gaining references
2175 * to the vnode. This may be the case if the caller holds the only reference.
2176 * This is also useful when stale data is acceptable as race conditions may
2177 * be accounted for by some other means.
2180 vrefcnt(struct vnode *vp)
2185 usecnt = vp->v_usecount;
2191 #define VPUTX_VRELE 1
2192 #define VPUTX_VPUT 2
2193 #define VPUTX_VUNREF 3
2196 vputx(struct vnode *vp, int func)
2200 KASSERT(vp != NULL, ("vputx: null vp"));
2201 if (func == VPUTX_VUNREF)
2202 ASSERT_VOP_LOCKED(vp, "vunref");
2203 else if (func == VPUTX_VPUT)
2204 ASSERT_VOP_LOCKED(vp, "vput");
2206 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2207 VFS_ASSERT_GIANT(vp->v_mount);
2208 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2211 /* Skip this v_writecount check if we're going to panic below. */
2212 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2213 ("vputx: missed vn_close"));
2216 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2217 vp->v_usecount == 1)) {
2218 if (func == VPUTX_VPUT)
2220 v_decr_usecount(vp);
2224 if (vp->v_usecount != 1) {
2225 vprint("vputx: negative ref count", vp);
2226 panic("vputx: negative ref cnt");
2228 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2230 * We want to hold the vnode until the inactive finishes to
2231 * prevent vgone() races. We drop the use count here and the
2232 * hold count below when we're done.
2236 * We must call VOP_INACTIVE with the node locked. Mark
2237 * as VI_DOINGINACT to avoid recursion.
2239 vp->v_iflag |= VI_OWEINACT;
2242 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2246 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2247 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK |
2253 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
2257 if (vp->v_usecount > 0)
2258 vp->v_iflag &= ~VI_OWEINACT;
2260 if (vp->v_iflag & VI_OWEINACT)
2261 vinactive(vp, curthread);
2262 if (func != VPUTX_VUNREF)
2269 * Vnode put/release.
2270 * If count drops to zero, call inactive routine and return to freelist.
2273 vrele(struct vnode *vp)
2276 vputx(vp, VPUTX_VRELE);
2280 * Release an already locked vnode. This give the same effects as
2281 * unlock+vrele(), but takes less time and avoids releasing and
2282 * re-aquiring the lock (as vrele() acquires the lock internally.)
2285 vput(struct vnode *vp)
2288 vputx(vp, VPUTX_VPUT);
2292 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2295 vunref(struct vnode *vp)
2298 vputx(vp, VPUTX_VUNREF);
2302 * Somebody doesn't want the vnode recycled.
2305 vhold(struct vnode *vp)
2314 vholdl(struct vnode *vp)
2317 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2319 if (VSHOULDBUSY(vp))
2324 * Note that there is one less who cares about this vnode. vdrop() is the
2325 * opposite of vhold().
2328 vdrop(struct vnode *vp)
2336 * Drop the hold count of the vnode. If this is the last reference to
2337 * the vnode we will free it if it has been vgone'd otherwise it is
2338 * placed on the free list.
2341 vdropl(struct vnode *vp)
2344 ASSERT_VI_LOCKED(vp, "vdropl");
2345 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2346 if (vp->v_holdcnt <= 0)
2347 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2349 if (vp->v_holdcnt == 0) {
2350 if (vp->v_iflag & VI_DOOMED) {
2351 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__,
2362 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2363 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2364 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2365 * failed lock upgrade.
2368 vinactive(struct vnode *vp, struct thread *td)
2371 ASSERT_VOP_ELOCKED(vp, "vinactive");
2372 ASSERT_VI_LOCKED(vp, "vinactive");
2373 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2374 ("vinactive: recursed on VI_DOINGINACT"));
2375 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2376 vp->v_iflag |= VI_DOINGINACT;
2377 vp->v_iflag &= ~VI_OWEINACT;
2379 VOP_INACTIVE(vp, td);
2381 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2382 ("vinactive: lost VI_DOINGINACT"));
2383 vp->v_iflag &= ~VI_DOINGINACT;
2387 * Remove any vnodes in the vnode table belonging to mount point mp.
2389 * If FORCECLOSE is not specified, there should not be any active ones,
2390 * return error if any are found (nb: this is a user error, not a
2391 * system error). If FORCECLOSE is specified, detach any active vnodes
2394 * If WRITECLOSE is set, only flush out regular file vnodes open for
2397 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2399 * `rootrefs' specifies the base reference count for the root vnode
2400 * of this filesystem. The root vnode is considered busy if its
2401 * v_usecount exceeds this value. On a successful return, vflush(, td)
2402 * will call vrele() on the root vnode exactly rootrefs times.
2403 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2407 static int busyprt = 0; /* print out busy vnodes */
2408 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "Print out busy vnodes");
2412 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td)
2414 struct vnode *vp, *mvp, *rootvp = NULL;
2416 int busy = 0, error;
2418 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2421 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2422 ("vflush: bad args"));
2424 * Get the filesystem root vnode. We can vput() it
2425 * immediately, since with rootrefs > 0, it won't go away.
2427 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2428 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2437 MNT_VNODE_FOREACH(vp, mp, mvp) {
2442 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2446 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2450 * Skip over a vnodes marked VV_SYSTEM.
2452 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2459 * If WRITECLOSE is set, flush out unlinked but still open
2460 * files (even if open only for reading) and regular file
2461 * vnodes open for writing.
2463 if (flags & WRITECLOSE) {
2464 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2467 if ((vp->v_type == VNON ||
2468 (error == 0 && vattr.va_nlink > 0)) &&
2469 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2478 * With v_usecount == 0, all we need to do is clear out the
2479 * vnode data structures and we are done.
2481 * If FORCECLOSE is set, forcibly close the vnode.
2483 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2484 VNASSERT(vp->v_usecount == 0 ||
2485 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2486 ("device VNODE %p is FORCECLOSED", vp));
2492 vprint("vflush: busy vnode", vp);
2500 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2502 * If just the root vnode is busy, and if its refcount
2503 * is equal to `rootrefs', then go ahead and kill it.
2506 KASSERT(busy > 0, ("vflush: not busy"));
2507 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2508 ("vflush: usecount %d < rootrefs %d",
2509 rootvp->v_usecount, rootrefs));
2510 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2511 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2513 VOP_UNLOCK(rootvp, 0);
2519 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2523 for (; rootrefs > 0; rootrefs--)
2529 * Recycle an unused vnode to the front of the free list.
2532 vrecycle(struct vnode *vp, struct thread *td)
2536 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2537 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2540 if (vp->v_usecount == 0) {
2549 * Eliminate all activity associated with a vnode
2550 * in preparation for reuse.
2553 vgone(struct vnode *vp)
2561 * vgone, with the vp interlock held.
2564 vgonel(struct vnode *vp)
2571 ASSERT_VOP_ELOCKED(vp, "vgonel");
2572 ASSERT_VI_LOCKED(vp, "vgonel");
2573 VNASSERT(vp->v_holdcnt, vp,
2574 ("vgonel: vp %p has no reference.", vp));
2575 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2579 * Don't vgonel if we're already doomed.
2581 if (vp->v_iflag & VI_DOOMED)
2583 vp->v_iflag |= VI_DOOMED;
2585 * Check to see if the vnode is in use. If so, we have to call
2586 * VOP_CLOSE() and VOP_INACTIVE().
2588 active = vp->v_usecount;
2589 oweinact = (vp->v_iflag & VI_OWEINACT);
2592 * Clean out any buffers associated with the vnode.
2593 * If the flush fails, just toss the buffers.
2596 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2597 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2598 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2599 vinvalbuf(vp, 0, 0, 0);
2602 * If purging an active vnode, it must be closed and
2603 * deactivated before being reclaimed.
2606 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2607 if (oweinact || active) {
2609 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2614 * Reclaim the vnode.
2616 if (VOP_RECLAIM(vp, td))
2617 panic("vgone: cannot reclaim");
2619 vn_finished_secondary_write(mp);
2620 VNASSERT(vp->v_object == NULL, vp,
2621 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2623 * Clear the advisory locks and wake up waiting threads.
2625 lf_purgelocks(vp, &(vp->v_lockf));
2627 * Delete from old mount point vnode list.
2632 * Done with purge, reset to the standard lock and invalidate
2636 vp->v_vnlock = &vp->v_lock;
2637 vp->v_op = &dead_vnodeops;
2643 * Calculate the total number of references to a special device.
2646 vcount(struct vnode *vp)
2651 count = vp->v_rdev->si_usecount;
2657 * Same as above, but using the struct cdev *as argument
2660 count_dev(struct cdev *dev)
2665 count = dev->si_usecount;
2671 * Print out a description of a vnode.
2673 static char *typename[] =
2674 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2678 vn_printf(struct vnode *vp, const char *fmt, ...)
2681 char buf[256], buf2[16];
2687 printf("%p: ", (void *)vp);
2688 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2689 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2690 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2693 if (vp->v_vflag & VV_ROOT)
2694 strlcat(buf, "|VV_ROOT", sizeof(buf));
2695 if (vp->v_vflag & VV_ISTTY)
2696 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2697 if (vp->v_vflag & VV_NOSYNC)
2698 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2699 if (vp->v_vflag & VV_CACHEDLABEL)
2700 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2701 if (vp->v_vflag & VV_TEXT)
2702 strlcat(buf, "|VV_TEXT", sizeof(buf));
2703 if (vp->v_vflag & VV_COPYONWRITE)
2704 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2705 if (vp->v_vflag & VV_SYSTEM)
2706 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2707 if (vp->v_vflag & VV_PROCDEP)
2708 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2709 if (vp->v_vflag & VV_NOKNOTE)
2710 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2711 if (vp->v_vflag & VV_DELETED)
2712 strlcat(buf, "|VV_DELETED", sizeof(buf));
2713 if (vp->v_vflag & VV_MD)
2714 strlcat(buf, "|VV_MD", sizeof(buf));
2715 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC |
2716 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2717 VV_NOKNOTE | VV_DELETED | VV_MD);
2719 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2720 strlcat(buf, buf2, sizeof(buf));
2722 if (vp->v_iflag & VI_MOUNT)
2723 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2724 if (vp->v_iflag & VI_AGE)
2725 strlcat(buf, "|VI_AGE", sizeof(buf));
2726 if (vp->v_iflag & VI_DOOMED)
2727 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2728 if (vp->v_iflag & VI_FREE)
2729 strlcat(buf, "|VI_FREE", sizeof(buf));
2730 if (vp->v_iflag & VI_DOINGINACT)
2731 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2732 if (vp->v_iflag & VI_OWEINACT)
2733 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2734 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2735 VI_DOINGINACT | VI_OWEINACT);
2737 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2738 strlcat(buf, buf2, sizeof(buf));
2740 printf(" flags (%s)\n", buf + 1);
2741 if (mtx_owned(VI_MTX(vp)))
2742 printf(" VI_LOCKed");
2743 if (vp->v_object != NULL)
2744 printf(" v_object %p ref %d pages %d\n",
2745 vp->v_object, vp->v_object->ref_count,
2746 vp->v_object->resident_page_count);
2748 lockmgr_printinfo(vp->v_vnlock);
2749 if (vp->v_data != NULL)
2755 * List all of the locked vnodes in the system.
2756 * Called when debugging the kernel.
2758 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2760 struct mount *mp, *nmp;
2764 * Note: because this is DDB, we can't obey the locking semantics
2765 * for these structures, which means we could catch an inconsistent
2766 * state and dereference a nasty pointer. Not much to be done
2769 db_printf("Locked vnodes\n");
2770 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2771 nmp = TAILQ_NEXT(mp, mnt_list);
2772 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2773 if (vp->v_type != VMARKER &&
2777 nmp = TAILQ_NEXT(mp, mnt_list);
2782 * Show details about the given vnode.
2784 DB_SHOW_COMMAND(vnode, db_show_vnode)
2790 vp = (struct vnode *)addr;
2791 vn_printf(vp, "vnode ");
2795 * Show details about the given mount point.
2797 DB_SHOW_COMMAND(mount, db_show_mount)
2807 /* No address given, print short info about all mount points. */
2808 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2809 db_printf("%p %s on %s (%s)\n", mp,
2810 mp->mnt_stat.f_mntfromname,
2811 mp->mnt_stat.f_mntonname,
2812 mp->mnt_stat.f_fstypename);
2816 db_printf("\nMore info: show mount <addr>\n");
2820 mp = (struct mount *)addr;
2821 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
2822 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
2825 flags = mp->mnt_flag;
2826 #define MNT_FLAG(flag) do { \
2827 if (flags & (flag)) { \
2828 if (buf[0] != '\0') \
2829 strlcat(buf, ", ", sizeof(buf)); \
2830 strlcat(buf, (#flag) + 4, sizeof(buf)); \
2834 MNT_FLAG(MNT_RDONLY);
2835 MNT_FLAG(MNT_SYNCHRONOUS);
2836 MNT_FLAG(MNT_NOEXEC);
2837 MNT_FLAG(MNT_NOSUID);
2838 MNT_FLAG(MNT_UNION);
2839 MNT_FLAG(MNT_ASYNC);
2840 MNT_FLAG(MNT_SUIDDIR);
2841 MNT_FLAG(MNT_SOFTDEP);
2842 MNT_FLAG(MNT_NOSYMFOLLOW);
2843 MNT_FLAG(MNT_GJOURNAL);
2844 MNT_FLAG(MNT_MULTILABEL);
2846 MNT_FLAG(MNT_NOATIME);
2847 MNT_FLAG(MNT_NOCLUSTERR);
2848 MNT_FLAG(MNT_NOCLUSTERW);
2849 MNT_FLAG(MNT_NFS4ACLS);
2850 MNT_FLAG(MNT_EXRDONLY);
2851 MNT_FLAG(MNT_EXPORTED);
2852 MNT_FLAG(MNT_DEFEXPORTED);
2853 MNT_FLAG(MNT_EXPORTANON);
2854 MNT_FLAG(MNT_EXKERB);
2855 MNT_FLAG(MNT_EXPUBLIC);
2856 MNT_FLAG(MNT_LOCAL);
2857 MNT_FLAG(MNT_QUOTA);
2858 MNT_FLAG(MNT_ROOTFS);
2860 MNT_FLAG(MNT_IGNORE);
2861 MNT_FLAG(MNT_UPDATE);
2862 MNT_FLAG(MNT_DELEXPORT);
2863 MNT_FLAG(MNT_RELOAD);
2864 MNT_FLAG(MNT_FORCE);
2865 MNT_FLAG(MNT_SNAPSHOT);
2866 MNT_FLAG(MNT_BYFSID);
2870 strlcat(buf, ", ", sizeof(buf));
2871 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2874 db_printf(" mnt_flag = %s\n", buf);
2877 flags = mp->mnt_kern_flag;
2878 #define MNT_KERN_FLAG(flag) do { \
2879 if (flags & (flag)) { \
2880 if (buf[0] != '\0') \
2881 strlcat(buf, ", ", sizeof(buf)); \
2882 strlcat(buf, (#flag) + 5, sizeof(buf)); \
2886 MNT_KERN_FLAG(MNTK_UNMOUNTF);
2887 MNT_KERN_FLAG(MNTK_ASYNC);
2888 MNT_KERN_FLAG(MNTK_SOFTDEP);
2889 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
2890 MNT_KERN_FLAG(MNTK_UNMOUNT);
2891 MNT_KERN_FLAG(MNTK_MWAIT);
2892 MNT_KERN_FLAG(MNTK_SUSPEND);
2893 MNT_KERN_FLAG(MNTK_SUSPEND2);
2894 MNT_KERN_FLAG(MNTK_SUSPENDED);
2895 MNT_KERN_FLAG(MNTK_MPSAFE);
2896 MNT_KERN_FLAG(MNTK_NOKNOTE);
2897 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
2898 #undef MNT_KERN_FLAG
2901 strlcat(buf, ", ", sizeof(buf));
2902 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2905 db_printf(" mnt_kern_flag = %s\n", buf);
2907 db_printf(" mnt_opt = ");
2908 opt = TAILQ_FIRST(mp->mnt_opt);
2910 db_printf("%s", opt->name);
2911 opt = TAILQ_NEXT(opt, link);
2912 while (opt != NULL) {
2913 db_printf(", %s", opt->name);
2914 opt = TAILQ_NEXT(opt, link);
2920 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
2921 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
2922 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
2923 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
2924 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
2925 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
2926 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
2927 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
2928 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
2929 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
2930 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
2931 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
2933 db_printf(" mnt_cred = { uid=%u ruid=%u",
2934 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
2935 if (jailed(mp->mnt_cred))
2936 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
2938 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
2939 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
2940 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
2941 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
2942 db_printf(" mnt_noasync = %u\n", mp->mnt_noasync);
2943 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
2944 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
2945 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
2946 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
2947 db_printf(" mnt_secondary_accwrites = %d\n",
2948 mp->mnt_secondary_accwrites);
2949 db_printf(" mnt_gjprovider = %s\n",
2950 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
2953 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2954 if (vp->v_type != VMARKER) {
2955 vn_printf(vp, "vnode ");
2964 * Fill in a struct xvfsconf based on a struct vfsconf.
2967 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
2970 strcpy(xvfsp->vfc_name, vfsp->vfc_name);
2971 xvfsp->vfc_typenum = vfsp->vfc_typenum;
2972 xvfsp->vfc_refcount = vfsp->vfc_refcount;
2973 xvfsp->vfc_flags = vfsp->vfc_flags;
2975 * These are unused in userland, we keep them
2976 * to not break binary compatibility.
2978 xvfsp->vfc_vfsops = NULL;
2979 xvfsp->vfc_next = NULL;
2983 * Top level filesystem related information gathering.
2986 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
2988 struct vfsconf *vfsp;
2989 struct xvfsconf xvfsp;
2993 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
2994 bzero(&xvfsp, sizeof(xvfsp));
2995 vfsconf2x(vfsp, &xvfsp);
2996 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp);
3003 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
3004 "S,xvfsconf", "List of all configured filesystems");
3006 #ifndef BURN_BRIDGES
3007 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
3010 vfs_sysctl(SYSCTL_HANDLER_ARGS)
3012 int *name = (int *)arg1 - 1; /* XXX */
3013 u_int namelen = arg2 + 1; /* XXX */
3014 struct vfsconf *vfsp;
3015 struct xvfsconf xvfsp;
3017 printf("WARNING: userland calling deprecated sysctl, "
3018 "please rebuild world\n");
3020 #if 1 || defined(COMPAT_PRELITE2)
3021 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
3023 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
3027 case VFS_MAXTYPENUM:
3030 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3033 return (ENOTDIR); /* overloaded */
3034 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
3035 if (vfsp->vfc_typenum == name[2])
3038 return (EOPNOTSUPP);
3039 bzero(&xvfsp, sizeof(xvfsp));
3040 vfsconf2x(vfsp, &xvfsp);
3041 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3043 return (EOPNOTSUPP);
3046 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
3047 vfs_sysctl, "Generic filesystem");
3049 #if 1 || defined(COMPAT_PRELITE2)
3052 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3055 struct vfsconf *vfsp;
3056 struct ovfsconf ovfs;
3058 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3059 bzero(&ovfs, sizeof(ovfs));
3060 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3061 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3062 ovfs.vfc_index = vfsp->vfc_typenum;
3063 ovfs.vfc_refcount = vfsp->vfc_refcount;
3064 ovfs.vfc_flags = vfsp->vfc_flags;
3065 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3072 #endif /* 1 || COMPAT_PRELITE2 */
3073 #endif /* !BURN_BRIDGES */
3075 #define KINFO_VNODESLOP 10
3078 * Dump vnode list (via sysctl).
3082 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3090 * Stale numvnodes access is not fatal here.
3093 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3095 /* Make an estimate */
3096 return (SYSCTL_OUT(req, 0, len));
3098 error = sysctl_wire_old_buffer(req, 0);
3101 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3103 mtx_lock(&mountlist_mtx);
3104 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3105 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3108 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3112 xvn[n].xv_size = sizeof *xvn;
3113 xvn[n].xv_vnode = vp;
3114 xvn[n].xv_id = 0; /* XXX compat */
3115 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3117 XV_COPY(writecount);
3123 xvn[n].xv_flag = vp->v_vflag;
3125 switch (vp->v_type) {
3132 if (vp->v_rdev == NULL) {
3136 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3139 xvn[n].xv_socket = vp->v_socket;
3142 xvn[n].xv_fifo = vp->v_fifoinfo;
3147 /* shouldn't happen? */
3155 mtx_lock(&mountlist_mtx);
3160 mtx_unlock(&mountlist_mtx);
3162 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3167 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3168 0, 0, sysctl_vnode, "S,xvnode", "");
3172 * Unmount all filesystems. The list is traversed in reverse order
3173 * of mounting to avoid dependencies.
3176 vfs_unmountall(void)
3182 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
3183 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3187 * Since this only runs when rebooting, it is not interlocked.
3189 while(!TAILQ_EMPTY(&mountlist)) {
3190 mp = TAILQ_LAST(&mountlist, mntlist);
3191 error = dounmount(mp, MNT_FORCE, td);
3193 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3195 * XXX: Due to the way in which we mount the root
3196 * file system off of devfs, devfs will generate a
3197 * "busy" warning when we try to unmount it before
3198 * the root. Don't print a warning as a result in
3199 * order to avoid false positive errors that may
3200 * cause needless upset.
3202 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3203 printf("unmount of %s failed (",
3204 mp->mnt_stat.f_mntonname);
3208 printf("%d)\n", error);
3211 /* The unmount has removed mp from the mountlist */
3217 * perform msync on all vnodes under a mount point
3218 * the mount point must be locked.
3221 vfs_msync(struct mount *mp, int flags)
3223 struct vnode *vp, *mvp;
3224 struct vm_object *obj;
3226 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3228 MNT_VNODE_FOREACH(vp, mp, mvp) {
3231 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3232 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3235 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3237 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3245 VM_OBJECT_LOCK(obj);
3246 vm_object_page_clean(obj, 0, 0,
3248 OBJPC_SYNC : OBJPC_NOSYNC);
3249 VM_OBJECT_UNLOCK(obj);
3261 * Mark a vnode as free, putting it up for recycling.
3264 vfree(struct vnode *vp)
3267 ASSERT_VI_LOCKED(vp, "vfree");
3268 mtx_lock(&vnode_free_list_mtx);
3269 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
3270 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
3271 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
3272 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
3273 ("vfree: Freeing doomed vnode"));
3274 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3275 if (vp->v_iflag & VI_AGE) {
3276 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
3278 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
3281 vp->v_iflag &= ~VI_AGE;
3282 vp->v_iflag |= VI_FREE;
3283 mtx_unlock(&vnode_free_list_mtx);
3287 * Opposite of vfree() - mark a vnode as in use.
3290 vbusy(struct vnode *vp)
3292 ASSERT_VI_LOCKED(vp, "vbusy");
3293 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
3294 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
3295 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3297 mtx_lock(&vnode_free_list_mtx);
3298 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3300 vp->v_iflag &= ~(VI_FREE|VI_AGE);
3301 mtx_unlock(&vnode_free_list_mtx);
3305 destroy_vpollinfo(struct vpollinfo *vi)
3307 knlist_destroy(&vi->vpi_selinfo.si_note);
3308 mtx_destroy(&vi->vpi_lock);
3309 uma_zfree(vnodepoll_zone, vi);
3313 * Initalize per-vnode helper structure to hold poll-related state.
3316 v_addpollinfo(struct vnode *vp)
3318 struct vpollinfo *vi;
3320 if (vp->v_pollinfo != NULL)
3322 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3323 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3324 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3325 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3327 if (vp->v_pollinfo != NULL) {
3329 destroy_vpollinfo(vi);
3332 vp->v_pollinfo = vi;
3337 * Record a process's interest in events which might happen to
3338 * a vnode. Because poll uses the historic select-style interface
3339 * internally, this routine serves as both the ``check for any
3340 * pending events'' and the ``record my interest in future events''
3341 * functions. (These are done together, while the lock is held,
3342 * to avoid race conditions.)
3345 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3349 mtx_lock(&vp->v_pollinfo->vpi_lock);
3350 if (vp->v_pollinfo->vpi_revents & events) {
3352 * This leaves events we are not interested
3353 * in available for the other process which
3354 * which presumably had requested them
3355 * (otherwise they would never have been
3358 events &= vp->v_pollinfo->vpi_revents;
3359 vp->v_pollinfo->vpi_revents &= ~events;
3361 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3364 vp->v_pollinfo->vpi_events |= events;
3365 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3366 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3371 * Routine to create and manage a filesystem syncer vnode.
3373 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3374 static int sync_fsync(struct vop_fsync_args *);
3375 static int sync_inactive(struct vop_inactive_args *);
3376 static int sync_reclaim(struct vop_reclaim_args *);
3378 static struct vop_vector sync_vnodeops = {
3379 .vop_bypass = VOP_EOPNOTSUPP,
3380 .vop_close = sync_close, /* close */
3381 .vop_fsync = sync_fsync, /* fsync */
3382 .vop_inactive = sync_inactive, /* inactive */
3383 .vop_reclaim = sync_reclaim, /* reclaim */
3384 .vop_lock1 = vop_stdlock, /* lock */
3385 .vop_unlock = vop_stdunlock, /* unlock */
3386 .vop_islocked = vop_stdislocked, /* islocked */
3390 * Create a new filesystem syncer vnode for the specified mount point.
3393 vfs_allocate_syncvnode(struct mount *mp)
3397 static long start, incr, next;
3400 /* Allocate a new vnode */
3401 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) {
3402 mp->mnt_syncer = NULL;
3406 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3407 vp->v_vflag |= VV_FORCEINSMQ;
3408 error = insmntque(vp, mp);
3410 panic("vfs_allocate_syncvnode: insmntque failed");
3411 vp->v_vflag &= ~VV_FORCEINSMQ;
3414 * Place the vnode onto the syncer worklist. We attempt to
3415 * scatter them about on the list so that they will go off
3416 * at evenly distributed times even if all the filesystems
3417 * are mounted at once.
3420 if (next == 0 || next > syncer_maxdelay) {
3424 start = syncer_maxdelay / 2;
3425 incr = syncer_maxdelay;
3431 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3432 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3433 mtx_lock(&sync_mtx);
3435 mtx_unlock(&sync_mtx);
3437 mp->mnt_syncer = vp;
3442 * Do a lazy sync of the filesystem.
3445 sync_fsync(struct vop_fsync_args *ap)
3447 struct vnode *syncvp = ap->a_vp;
3448 struct mount *mp = syncvp->v_mount;
3453 * We only need to do something if this is a lazy evaluation.
3455 if (ap->a_waitfor != MNT_LAZY)
3459 * Move ourselves to the back of the sync list.
3461 bo = &syncvp->v_bufobj;
3463 vn_syncer_add_to_worklist(bo, syncdelay);
3467 * Walk the list of vnodes pushing all that are dirty and
3468 * not already on the sync list.
3470 mtx_lock(&mountlist_mtx);
3471 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
3472 mtx_unlock(&mountlist_mtx);
3475 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3481 mp->mnt_kern_flag &= ~MNTK_ASYNC;
3483 vfs_msync(mp, MNT_NOWAIT);
3484 error = VFS_SYNC(mp, MNT_LAZY);
3487 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
3488 mp->mnt_kern_flag |= MNTK_ASYNC;
3490 vn_finished_write(mp);
3496 * The syncer vnode is no referenced.
3499 sync_inactive(struct vop_inactive_args *ap)
3507 * The syncer vnode is no longer needed and is being decommissioned.
3509 * Modifications to the worklist must be protected by sync_mtx.
3512 sync_reclaim(struct vop_reclaim_args *ap)
3514 struct vnode *vp = ap->a_vp;
3519 vp->v_mount->mnt_syncer = NULL;
3520 if (bo->bo_flag & BO_ONWORKLST) {
3521 mtx_lock(&sync_mtx);
3522 LIST_REMOVE(bo, bo_synclist);
3523 syncer_worklist_len--;
3525 mtx_unlock(&sync_mtx);
3526 bo->bo_flag &= ~BO_ONWORKLST;
3534 * Check if vnode represents a disk device
3537 vn_isdisk(struct vnode *vp, int *errp)
3543 if (vp->v_type != VCHR)
3545 else if (vp->v_rdev == NULL)
3547 else if (vp->v_rdev->si_devsw == NULL)
3549 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3554 return (error == 0);
3558 * Common filesystem object access control check routine. Accepts a
3559 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3560 * and optional call-by-reference privused argument allowing vaccess()
3561 * to indicate to the caller whether privilege was used to satisfy the
3562 * request (obsoleted). Returns 0 on success, or an errno on failure.
3564 * The ifdef'd CAPABILITIES version is here for reference, but is not
3568 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3569 accmode_t accmode, struct ucred *cred, int *privused)
3571 accmode_t dac_granted;
3572 accmode_t priv_granted;
3574 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3575 ("invalid bit in accmode"));
3578 * Look for a normal, non-privileged way to access the file/directory
3579 * as requested. If it exists, go with that.
3582 if (privused != NULL)
3587 /* Check the owner. */
3588 if (cred->cr_uid == file_uid) {
3589 dac_granted |= VADMIN;
3590 if (file_mode & S_IXUSR)
3591 dac_granted |= VEXEC;
3592 if (file_mode & S_IRUSR)
3593 dac_granted |= VREAD;
3594 if (file_mode & S_IWUSR)
3595 dac_granted |= (VWRITE | VAPPEND);
3597 if ((accmode & dac_granted) == accmode)
3603 /* Otherwise, check the groups (first match) */
3604 if (groupmember(file_gid, cred)) {
3605 if (file_mode & S_IXGRP)
3606 dac_granted |= VEXEC;
3607 if (file_mode & S_IRGRP)
3608 dac_granted |= VREAD;
3609 if (file_mode & S_IWGRP)
3610 dac_granted |= (VWRITE | VAPPEND);
3612 if ((accmode & dac_granted) == accmode)
3618 /* Otherwise, check everyone else. */
3619 if (file_mode & S_IXOTH)
3620 dac_granted |= VEXEC;
3621 if (file_mode & S_IROTH)
3622 dac_granted |= VREAD;
3623 if (file_mode & S_IWOTH)
3624 dac_granted |= (VWRITE | VAPPEND);
3625 if ((accmode & dac_granted) == accmode)
3630 * Build a privilege mask to determine if the set of privileges
3631 * satisfies the requirements when combined with the granted mask
3632 * from above. For each privilege, if the privilege is required,
3633 * bitwise or the request type onto the priv_granted mask.
3639 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3640 * requests, instead of PRIV_VFS_EXEC.
3642 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3643 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3644 priv_granted |= VEXEC;
3646 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3647 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3648 priv_granted |= VEXEC;
3651 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3652 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3653 priv_granted |= VREAD;
3655 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3656 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3657 priv_granted |= (VWRITE | VAPPEND);
3659 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3660 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3661 priv_granted |= VADMIN;
3663 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3664 /* XXX audit: privilege used */
3665 if (privused != NULL)
3670 return ((accmode & VADMIN) ? EPERM : EACCES);
3674 * Credential check based on process requesting service, and per-attribute
3678 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3679 struct thread *td, accmode_t accmode)
3683 * Kernel-invoked always succeeds.
3689 * Do not allow privileged processes in jail to directly manipulate
3690 * system attributes.
3692 switch (attrnamespace) {
3693 case EXTATTR_NAMESPACE_SYSTEM:
3694 /* Potentially should be: return (EPERM); */
3695 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3696 case EXTATTR_NAMESPACE_USER:
3697 return (VOP_ACCESS(vp, accmode, cred, td));
3703 #ifdef DEBUG_VFS_LOCKS
3705 * This only exists to supress warnings from unlocked specfs accesses. It is
3706 * no longer ok to have an unlocked VFS.
3708 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3709 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3711 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3712 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0,
3713 "Drop into debugger on lock violation");
3715 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3716 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex,
3717 0, "Check for interlock across VOPs");
3719 int vfs_badlock_print = 1; /* Print lock violations. */
3720 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print,
3721 0, "Print lock violations");
3724 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3725 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW,
3726 &vfs_badlock_backtrace, 0, "Print backtrace at lock violations");
3730 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3734 if (vfs_badlock_backtrace)
3737 if (vfs_badlock_print)
3738 printf("%s: %p %s\n", str, (void *)vp, msg);
3739 if (vfs_badlock_ddb)
3740 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3744 assert_vi_locked(struct vnode *vp, const char *str)
3747 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3748 vfs_badlock("interlock is not locked but should be", str, vp);
3752 assert_vi_unlocked(struct vnode *vp, const char *str)
3755 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3756 vfs_badlock("interlock is locked but should not be", str, vp);
3760 assert_vop_locked(struct vnode *vp, const char *str)
3763 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0)
3764 vfs_badlock("is not locked but should be", str, vp);
3768 assert_vop_unlocked(struct vnode *vp, const char *str)
3771 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
3772 vfs_badlock("is locked but should not be", str, vp);
3776 assert_vop_elocked(struct vnode *vp, const char *str)
3779 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
3780 vfs_badlock("is not exclusive locked but should be", str, vp);
3785 assert_vop_elocked_other(struct vnode *vp, const char *str)
3788 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
3789 vfs_badlock("is not exclusive locked by another thread",
3794 assert_vop_slocked(struct vnode *vp, const char *str)
3797 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
3798 vfs_badlock("is not locked shared but should be", str, vp);
3801 #endif /* DEBUG_VFS_LOCKS */
3804 vop_rename_fail(struct vop_rename_args *ap)
3807 if (ap->a_tvp != NULL)
3809 if (ap->a_tdvp == ap->a_tvp)
3818 vop_rename_pre(void *ap)
3820 struct vop_rename_args *a = ap;
3822 #ifdef DEBUG_VFS_LOCKS
3824 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3825 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3826 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3827 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3829 /* Check the source (from). */
3830 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
3831 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
3832 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3833 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
3834 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
3836 /* Check the target. */
3838 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3839 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3841 if (a->a_tdvp != a->a_fdvp)
3843 if (a->a_tvp != a->a_fvp)
3851 vop_strategy_pre(void *ap)
3853 #ifdef DEBUG_VFS_LOCKS
3854 struct vop_strategy_args *a;
3861 * Cluster ops lock their component buffers but not the IO container.
3863 if ((bp->b_flags & B_CLUSTER) != 0)
3866 if (!BUF_ISLOCKED(bp)) {
3867 if (vfs_badlock_print)
3869 "VOP_STRATEGY: bp is not locked but should be\n");
3870 if (vfs_badlock_ddb)
3871 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3877 vop_lookup_pre(void *ap)
3879 #ifdef DEBUG_VFS_LOCKS
3880 struct vop_lookup_args *a;
3885 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3886 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3891 vop_lookup_post(void *ap, int rc)
3893 #ifdef DEBUG_VFS_LOCKS
3894 struct vop_lookup_args *a;
3902 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3903 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3906 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3911 vop_lock_pre(void *ap)
3913 #ifdef DEBUG_VFS_LOCKS
3914 struct vop_lock1_args *a = ap;
3916 if ((a->a_flags & LK_INTERLOCK) == 0)
3917 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3919 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3924 vop_lock_post(void *ap, int rc)
3926 #ifdef DEBUG_VFS_LOCKS
3927 struct vop_lock1_args *a = ap;
3929 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3931 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3936 vop_unlock_pre(void *ap)
3938 #ifdef DEBUG_VFS_LOCKS
3939 struct vop_unlock_args *a = ap;
3941 if (a->a_flags & LK_INTERLOCK)
3942 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3943 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3948 vop_unlock_post(void *ap, int rc)
3950 #ifdef DEBUG_VFS_LOCKS
3951 struct vop_unlock_args *a = ap;
3953 if (a->a_flags & LK_INTERLOCK)
3954 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
3959 vop_create_post(void *ap, int rc)
3961 struct vop_create_args *a = ap;
3964 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3968 vop_link_post(void *ap, int rc)
3970 struct vop_link_args *a = ap;
3973 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
3974 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
3979 vop_mkdir_post(void *ap, int rc)
3981 struct vop_mkdir_args *a = ap;
3984 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3988 vop_mknod_post(void *ap, int rc)
3990 struct vop_mknod_args *a = ap;
3993 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3997 vop_remove_post(void *ap, int rc)
3999 struct vop_remove_args *a = ap;
4002 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4003 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4008 vop_rename_post(void *ap, int rc)
4010 struct vop_rename_args *a = ap;
4013 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
4014 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
4015 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
4017 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
4019 if (a->a_tdvp != a->a_fdvp)
4021 if (a->a_tvp != a->a_fvp)
4029 vop_rmdir_post(void *ap, int rc)
4031 struct vop_rmdir_args *a = ap;
4034 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
4035 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
4040 vop_setattr_post(void *ap, int rc)
4042 struct vop_setattr_args *a = ap;
4045 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4049 vop_symlink_post(void *ap, int rc)
4051 struct vop_symlink_args *a = ap;
4054 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4057 static struct knlist fs_knlist;
4060 vfs_event_init(void *arg)
4062 knlist_init_mtx(&fs_knlist, NULL);
4064 /* XXX - correct order? */
4065 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4068 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
4071 KNOTE_UNLOCKED(&fs_knlist, event);
4074 static int filt_fsattach(struct knote *kn);
4075 static void filt_fsdetach(struct knote *kn);
4076 static int filt_fsevent(struct knote *kn, long hint);
4078 struct filterops fs_filtops =
4079 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
4082 filt_fsattach(struct knote *kn)
4085 kn->kn_flags |= EV_CLEAR;
4086 knlist_add(&fs_knlist, kn, 0);
4091 filt_fsdetach(struct knote *kn)
4094 knlist_remove(&fs_knlist, kn, 0);
4098 filt_fsevent(struct knote *kn, long hint)
4101 kn->kn_fflags |= hint;
4102 return (kn->kn_fflags != 0);
4106 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4112 error = SYSCTL_IN(req, &vc, sizeof(vc));
4115 if (vc.vc_vers != VFS_CTL_VERS1)
4117 mp = vfs_getvfs(&vc.vc_fsid);
4120 /* ensure that a specific sysctl goes to the right filesystem. */
4121 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4122 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4126 VCTLTOREQ(&vc, req);
4127 error = VFS_SYSCTL(mp, vc.vc_op, req);
4132 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, NULL, 0, sysctl_vfs_ctl, "",
4136 * Function to initialize a va_filerev field sensibly.
4137 * XXX: Wouldn't a random number make a lot more sense ??
4140 init_va_filerev(void)
4145 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4148 static int filt_vfsread(struct knote *kn, long hint);
4149 static int filt_vfswrite(struct knote *kn, long hint);
4150 static int filt_vfsvnode(struct knote *kn, long hint);
4151 static void filt_vfsdetach(struct knote *kn);
4152 static struct filterops vfsread_filtops =
4153 { 1, NULL, filt_vfsdetach, filt_vfsread };
4154 static struct filterops vfswrite_filtops =
4155 { 1, NULL, filt_vfsdetach, filt_vfswrite };
4156 static struct filterops vfsvnode_filtops =
4157 { 1, NULL, filt_vfsdetach, filt_vfsvnode };
4160 vfs_knllock(void *arg)
4162 struct vnode *vp = arg;
4164 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4168 vfs_knlunlock(void *arg)
4170 struct vnode *vp = arg;
4176 vfs_knl_assert_locked(void *arg)
4178 #ifdef DEBUG_VFS_LOCKS
4179 struct vnode *vp = arg;
4181 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4186 vfs_knl_assert_unlocked(void *arg)
4188 #ifdef DEBUG_VFS_LOCKS
4189 struct vnode *vp = arg;
4191 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4196 vfs_kqfilter(struct vop_kqfilter_args *ap)
4198 struct vnode *vp = ap->a_vp;
4199 struct knote *kn = ap->a_kn;
4202 switch (kn->kn_filter) {
4204 kn->kn_fop = &vfsread_filtops;
4207 kn->kn_fop = &vfswrite_filtops;
4210 kn->kn_fop = &vfsvnode_filtops;
4216 kn->kn_hook = (caddr_t)vp;
4219 if (vp->v_pollinfo == NULL)
4221 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4222 knlist_add(knl, kn, 0);
4228 * Detach knote from vnode
4231 filt_vfsdetach(struct knote *kn)
4233 struct vnode *vp = (struct vnode *)kn->kn_hook;
4235 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4236 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4241 filt_vfsread(struct knote *kn, long hint)
4243 struct vnode *vp = (struct vnode *)kn->kn_hook;
4248 * filesystem is gone, so set the EOF flag and schedule
4249 * the knote for deletion.
4251 if (hint == NOTE_REVOKE) {
4253 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4258 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4262 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4263 res = (kn->kn_data != 0);
4270 filt_vfswrite(struct knote *kn, long hint)
4272 struct vnode *vp = (struct vnode *)kn->kn_hook;
4277 * filesystem is gone, so set the EOF flag and schedule
4278 * the knote for deletion.
4280 if (hint == NOTE_REVOKE)
4281 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4289 filt_vfsvnode(struct knote *kn, long hint)
4291 struct vnode *vp = (struct vnode *)kn->kn_hook;
4295 if (kn->kn_sfflags & hint)
4296 kn->kn_fflags |= hint;
4297 if (hint == NOTE_REVOKE) {
4298 kn->kn_flags |= EV_EOF;
4302 res = (kn->kn_fflags != 0);
4308 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4312 if (dp->d_reclen > ap->a_uio->uio_resid)
4313 return (ENAMETOOLONG);
4314 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4316 if (ap->a_ncookies != NULL) {
4317 if (ap->a_cookies != NULL)
4318 free(ap->a_cookies, M_TEMP);
4319 ap->a_cookies = NULL;
4320 *ap->a_ncookies = 0;
4324 if (ap->a_ncookies == NULL)
4327 KASSERT(ap->a_cookies,
4328 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4330 *ap->a_cookies = realloc(*ap->a_cookies,
4331 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4332 (*ap->a_cookies)[*ap->a_ncookies] = off;
4337 * Mark for update the access time of the file if the filesystem
4338 * supports VOP_MARKATIME. This functionality is used by execve and
4339 * mmap, so we want to avoid the I/O implied by directly setting
4340 * va_atime for the sake of efficiency.
4343 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4348 VFS_ASSERT_GIANT(mp);
4349 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4350 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4351 (void)VOP_MARKATIME(vp);
4355 * The purpose of this routine is to remove granularity from accmode_t,
4356 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4357 * VADMIN and VAPPEND.
4359 * If it returns 0, the caller is supposed to continue with the usual
4360 * access checks using 'accmode' as modified by this routine. If it
4361 * returns nonzero value, the caller is supposed to return that value
4364 * Note that after this routine runs, accmode may be zero.
4367 vfs_unixify_accmode(accmode_t *accmode)
4370 * There is no way to specify explicit "deny" rule using
4371 * file mode or POSIX.1e ACLs.
4373 if (*accmode & VEXPLICIT_DENY) {
4379 * None of these can be translated into usual access bits.
4380 * Also, the common case for NFSv4 ACLs is to not contain
4381 * either of these bits. Caller should check for VWRITE
4382 * on the containing directory instead.
4384 if (*accmode & (VDELETE_CHILD | VDELETE))
4387 if (*accmode & VADMIN_PERMS) {
4388 *accmode &= ~VADMIN_PERMS;
4393 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4394 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4396 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);