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/sleepqueue.h>
70 #include <sys/sysctl.h>
71 #include <sys/syslog.h>
72 #include <sys/vmmeter.h>
73 #include <sys/vnode.h>
75 #include <machine/stdarg.h>
77 #include <security/mac/mac_framework.h>
80 #include <vm/vm_object.h>
81 #include <vm/vm_extern.h>
83 #include <vm/vm_map.h>
84 #include <vm/vm_page.h>
85 #include <vm/vm_kern.h>
95 static MALLOC_DEFINE(M_NETADDR, "subr_export_host", "Export host address structure");
97 static void delmntque(struct vnode *vp);
98 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
99 int slpflag, int slptimeo);
100 static void syncer_shutdown(void *arg, int howto);
101 static int vtryrecycle(struct vnode *vp);
102 static void vbusy(struct vnode *vp);
103 static void vinactive(struct vnode *, struct thread *);
104 static void v_incr_usecount(struct vnode *);
105 static void v_decr_usecount(struct vnode *);
106 static void v_decr_useonly(struct vnode *);
107 static void v_upgrade_usecount(struct vnode *);
108 static void vfree(struct vnode *);
109 static void vnlru_free(int);
110 static void vgonel(struct vnode *);
111 static void vfs_knllock(void *arg);
112 static void vfs_knlunlock(void *arg);
113 static void vfs_knl_assert_locked(void *arg);
114 static void vfs_knl_assert_unlocked(void *arg);
115 static void destroy_vpollinfo(struct vpollinfo *vi);
118 * Number of vnodes in existence. Increased whenever getnewvnode()
119 * allocates a new vnode, decreased on vdestroy() called on VI_DOOMed
122 static unsigned long numvnodes;
124 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
127 * Conversion tables for conversion from vnode types to inode formats
130 enum vtype iftovt_tab[16] = {
131 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
132 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
134 int vttoif_tab[10] = {
135 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
136 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
140 * List of vnodes that are ready for recycling.
142 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
145 * Free vnode target. Free vnodes may simply be files which have been stat'd
146 * but not read. This is somewhat common, and a small cache of such files
147 * should be kept to avoid recreation costs.
149 static u_long wantfreevnodes;
150 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
151 /* Number of vnodes in the free list. */
152 static u_long freevnodes;
153 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
155 static int vlru_allow_cache_src;
156 SYSCTL_INT(_vfs, OID_AUTO, vlru_allow_cache_src, CTLFLAG_RW,
157 &vlru_allow_cache_src, 0, "Allow vlru to reclaim source vnode");
160 * Various variables used for debugging the new implementation of
162 * XXX these are probably of (very) limited utility now.
164 static int reassignbufcalls;
165 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
168 * Cache for the mount type id assigned to NFS. This is used for
169 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
171 int nfs_mount_type = -1;
173 /* To keep more than one thread at a time from running vfs_getnewfsid */
174 static struct mtx mntid_mtx;
177 * Lock for any access to the following:
182 static struct mtx vnode_free_list_mtx;
184 /* Publicly exported FS */
185 struct nfs_public nfs_pub;
187 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
188 static uma_zone_t vnode_zone;
189 static uma_zone_t vnodepoll_zone;
191 /* Set to 1 to print out reclaim of active vnodes */
195 * The workitem queue.
197 * It is useful to delay writes of file data and filesystem metadata
198 * for tens of seconds so that quickly created and deleted files need
199 * not waste disk bandwidth being created and removed. To realize this,
200 * we append vnodes to a "workitem" queue. When running with a soft
201 * updates implementation, most pending metadata dependencies should
202 * not wait for more than a few seconds. Thus, mounted on block devices
203 * are delayed only about a half the time that file data is delayed.
204 * Similarly, directory updates are more critical, so are only delayed
205 * about a third the time that file data is delayed. Thus, there are
206 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
207 * one each second (driven off the filesystem syncer process). The
208 * syncer_delayno variable indicates the next queue that is to be processed.
209 * Items that need to be processed soon are placed in this queue:
211 * syncer_workitem_pending[syncer_delayno]
213 * A delay of fifteen seconds is done by placing the request fifteen
214 * entries later in the queue:
216 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
219 static int syncer_delayno;
220 static long syncer_mask;
221 LIST_HEAD(synclist, bufobj);
222 static struct synclist *syncer_workitem_pending[2];
224 * The sync_mtx protects:
229 * syncer_workitem_pending
230 * syncer_worklist_len
233 static struct mtx sync_mtx;
234 static struct cv sync_wakeup;
236 #define SYNCER_MAXDELAY 32
237 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
238 static int syncdelay = 30; /* max time to delay syncing data */
239 static int filedelay = 30; /* time to delay syncing files */
240 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
241 static int dirdelay = 29; /* time to delay syncing directories */
242 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
243 static int metadelay = 28; /* time to delay syncing metadata */
244 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
245 static int rushjob; /* number of slots to run ASAP */
246 static int stat_rush_requests; /* number of times I/O speeded up */
247 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
250 * When shutting down the syncer, run it at four times normal speed.
252 #define SYNCER_SHUTDOWN_SPEEDUP 4
253 static int sync_vnode_count;
254 static int syncer_worklist_len;
255 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
259 * Number of vnodes we want to exist at any one time. This is mostly used
260 * to size hash tables in vnode-related code. It is normally not used in
261 * getnewvnode(), as wantfreevnodes is normally nonzero.)
263 * XXX desiredvnodes is historical cruft and should not exist.
266 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
267 &desiredvnodes, 0, "Maximum number of vnodes");
268 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
269 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
270 static int vnlru_nowhere;
271 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
272 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
275 * Macros to control when a vnode is freed and recycled. All require
276 * the vnode interlock.
278 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
279 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
280 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
284 * Initialize the vnode management data structures.
286 #ifndef MAXVNODES_MAX
287 #define MAXVNODES_MAX 100000
290 vntblinit(void *dummy __unused)
294 * Desiredvnodes is a function of the physical memory size and
295 * the kernel's heap size. Specifically, desiredvnodes scales
296 * in proportion to the physical memory size until two fifths
297 * of the kernel's heap size is consumed by vnodes and vm
300 desiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 * vm_kmem_size /
301 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
302 if (desiredvnodes > MAXVNODES_MAX) {
304 printf("Reducing kern.maxvnodes %d -> %d\n",
305 desiredvnodes, MAXVNODES_MAX);
306 desiredvnodes = MAXVNODES_MAX;
308 wantfreevnodes = desiredvnodes / 4;
309 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
310 TAILQ_INIT(&vnode_free_list);
311 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
312 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
313 NULL, NULL, UMA_ALIGN_PTR, 0);
314 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
315 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
317 * Initialize the filesystem syncer.
319 syncer_workitem_pending[WI_MPSAFEQ] = hashinit(syncer_maxdelay, M_VNODE,
321 syncer_workitem_pending[WI_GIANTQ] = hashinit(syncer_maxdelay, M_VNODE,
323 syncer_maxdelay = syncer_mask + 1;
324 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
325 cv_init(&sync_wakeup, "syncer");
327 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL);
331 * Mark a mount point as busy. Used to synchronize access and to delay
332 * unmounting. Eventually, mountlist_mtx is not released on failure.
335 vfs_busy(struct mount *mp, int flags)
338 MPASS((flags & ~MBF_MASK) == 0);
339 CTR3(KTR_VFS, "%s: mp %p with flags %d", __func__, mp, flags);
344 * If mount point is currenly being unmounted, sleep until the
345 * mount point fate is decided. If thread doing the unmounting fails,
346 * it will clear MNTK_UNMOUNT flag before waking us up, indicating
347 * that this mount point has survived the unmount attempt and vfs_busy
348 * should retry. Otherwise the unmounter thread will set MNTK_REFEXPIRE
349 * flag in addition to MNTK_UNMOUNT, indicating that mount point is
350 * about to be really destroyed. vfs_busy needs to release its
351 * reference on the mount point in this case and return with ENOENT,
352 * telling the caller that mount mount it tried to busy is no longer
355 while (mp->mnt_kern_flag & MNTK_UNMOUNT) {
356 if (flags & MBF_NOWAIT || mp->mnt_kern_flag & MNTK_REFEXPIRE) {
359 CTR1(KTR_VFS, "%s: failed busying before sleeping",
363 if (flags & MBF_MNTLSTLOCK)
364 mtx_unlock(&mountlist_mtx);
365 mp->mnt_kern_flag |= MNTK_MWAIT;
366 msleep(mp, MNT_MTX(mp), PVFS, "vfs_busy", 0);
367 if (flags & MBF_MNTLSTLOCK)
368 mtx_lock(&mountlist_mtx);
370 if (flags & MBF_MNTLSTLOCK)
371 mtx_unlock(&mountlist_mtx);
378 * Free a busy filesystem.
381 vfs_unbusy(struct mount *mp)
384 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
387 KASSERT(mp->mnt_lockref > 0, ("negative mnt_lockref"));
389 if (mp->mnt_lockref == 0 && (mp->mnt_kern_flag & MNTK_DRAINING) != 0) {
390 MPASS(mp->mnt_kern_flag & MNTK_UNMOUNT);
391 CTR1(KTR_VFS, "%s: waking up waiters", __func__);
392 mp->mnt_kern_flag &= ~MNTK_DRAINING;
393 wakeup(&mp->mnt_lockref);
399 * Lookup a mount point by filesystem identifier.
402 vfs_getvfs(fsid_t *fsid)
406 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
407 mtx_lock(&mountlist_mtx);
408 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
409 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
410 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
412 mtx_unlock(&mountlist_mtx);
416 mtx_unlock(&mountlist_mtx);
417 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
418 return ((struct mount *) 0);
422 * Lookup a mount point by filesystem identifier, busying it before
426 vfs_busyfs(fsid_t *fsid)
431 CTR2(KTR_VFS, "%s: fsid %p", __func__, fsid);
432 mtx_lock(&mountlist_mtx);
433 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
434 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
435 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
436 error = vfs_busy(mp, MBF_MNTLSTLOCK);
438 mtx_unlock(&mountlist_mtx);
444 CTR2(KTR_VFS, "%s: lookup failed for %p id", __func__, fsid);
445 mtx_unlock(&mountlist_mtx);
446 return ((struct mount *) 0);
450 * Check if a user can access privileged mount options.
453 vfs_suser(struct mount *mp, struct thread *td)
458 * If the thread is jailed, but this is not a jail-friendly file
459 * system, deny immediately.
461 if (!(mp->mnt_vfc->vfc_flags & VFCF_JAIL) && jailed(td->td_ucred))
465 * If the file system was mounted outside the jail of the calling
466 * thread, deny immediately.
468 if (prison_check(td->td_ucred, mp->mnt_cred) != 0)
472 * If file system supports delegated administration, we don't check
473 * for the PRIV_VFS_MOUNT_OWNER privilege - it will be better verified
474 * by the file system itself.
475 * If this is not the user that did original mount, we check for
476 * the PRIV_VFS_MOUNT_OWNER privilege.
478 if (!(mp->mnt_vfc->vfc_flags & VFCF_DELEGADMIN) &&
479 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
480 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
487 * Get a new unique fsid. Try to make its val[0] unique, since this value
488 * will be used to create fake device numbers for stat(). Also try (but
489 * not so hard) make its val[0] unique mod 2^16, since some emulators only
490 * support 16-bit device numbers. We end up with unique val[0]'s for the
491 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
493 * Keep in mind that several mounts may be running in parallel. Starting
494 * the search one past where the previous search terminated is both a
495 * micro-optimization and a defense against returning the same fsid to
499 vfs_getnewfsid(struct mount *mp)
501 static u_int16_t mntid_base;
506 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
507 mtx_lock(&mntid_mtx);
508 mtype = mp->mnt_vfc->vfc_typenum;
509 tfsid.val[1] = mtype;
510 mtype = (mtype & 0xFF) << 24;
512 tfsid.val[0] = makedev(255,
513 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
515 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
519 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
520 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
521 mtx_unlock(&mntid_mtx);
525 * Knob to control the precision of file timestamps:
527 * 0 = seconds only; nanoseconds zeroed.
528 * 1 = seconds and nanoseconds, accurate within 1/HZ.
529 * 2 = seconds and nanoseconds, truncated to microseconds.
530 * >=3 = seconds and nanoseconds, maximum precision.
532 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
534 static int timestamp_precision = TSP_SEC;
535 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
536 ×tamp_precision, 0, "");
539 * Get a current timestamp.
542 vfs_timestamp(struct timespec *tsp)
546 switch (timestamp_precision) {
548 tsp->tv_sec = time_second;
556 TIMEVAL_TO_TIMESPEC(&tv, tsp);
566 * Set vnode attributes to VNOVAL
569 vattr_null(struct vattr *vap)
573 vap->va_size = VNOVAL;
574 vap->va_bytes = VNOVAL;
575 vap->va_mode = VNOVAL;
576 vap->va_nlink = VNOVAL;
577 vap->va_uid = VNOVAL;
578 vap->va_gid = VNOVAL;
579 vap->va_fsid = VNOVAL;
580 vap->va_fileid = VNOVAL;
581 vap->va_blocksize = VNOVAL;
582 vap->va_rdev = VNOVAL;
583 vap->va_atime.tv_sec = VNOVAL;
584 vap->va_atime.tv_nsec = VNOVAL;
585 vap->va_mtime.tv_sec = VNOVAL;
586 vap->va_mtime.tv_nsec = VNOVAL;
587 vap->va_ctime.tv_sec = VNOVAL;
588 vap->va_ctime.tv_nsec = VNOVAL;
589 vap->va_birthtime.tv_sec = VNOVAL;
590 vap->va_birthtime.tv_nsec = VNOVAL;
591 vap->va_flags = VNOVAL;
592 vap->va_gen = VNOVAL;
597 * This routine is called when we have too many vnodes. It attempts
598 * to free <count> vnodes and will potentially free vnodes that still
599 * have VM backing store (VM backing store is typically the cause
600 * of a vnode blowout so we want to do this). Therefore, this operation
601 * is not considered cheap.
603 * A number of conditions may prevent a vnode from being reclaimed.
604 * the buffer cache may have references on the vnode, a directory
605 * vnode may still have references due to the namei cache representing
606 * underlying files, or the vnode may be in active use. It is not
607 * desireable to reuse such vnodes. These conditions may cause the
608 * number of vnodes to reach some minimum value regardless of what
609 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
612 vlrureclaim(struct mount *mp)
621 * Calculate the trigger point, don't allow user
622 * screwups to blow us up. This prevents us from
623 * recycling vnodes with lots of resident pages. We
624 * aren't trying to free memory, we are trying to
627 usevnodes = desiredvnodes;
630 trigger = cnt.v_page_count * 2 / usevnodes;
632 vn_start_write(NULL, &mp, V_WAIT);
634 count = mp->mnt_nvnodelistsize / 10 + 1;
636 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
637 while (vp != NULL && vp->v_type == VMARKER)
638 vp = TAILQ_NEXT(vp, v_nmntvnodes);
641 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
642 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
647 * If it's been deconstructed already, it's still
648 * referenced, or it exceeds the trigger, skip it.
650 if (vp->v_usecount ||
651 (!vlru_allow_cache_src &&
652 !LIST_EMPTY(&(vp)->v_cache_src)) ||
653 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
654 vp->v_object->resident_page_count > trigger)) {
660 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT)) {
662 goto next_iter_mntunlocked;
666 * v_usecount may have been bumped after VOP_LOCK() dropped
667 * the vnode interlock and before it was locked again.
669 * It is not necessary to recheck VI_DOOMED because it can
670 * only be set by another thread that holds both the vnode
671 * lock and vnode interlock. If another thread has the
672 * vnode lock before we get to VOP_LOCK() and obtains the
673 * vnode interlock after VOP_LOCK() drops the vnode
674 * interlock, the other thread will be unable to drop the
675 * vnode lock before our VOP_LOCK() call fails.
677 if (vp->v_usecount ||
678 (!vlru_allow_cache_src &&
679 !LIST_EMPTY(&(vp)->v_cache_src)) ||
680 (vp->v_object != NULL &&
681 vp->v_object->resident_page_count > trigger)) {
682 VOP_UNLOCK(vp, LK_INTERLOCK);
683 goto next_iter_mntunlocked;
685 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
686 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
691 next_iter_mntunlocked:
692 if ((count % 256) != 0)
696 if ((count % 256) != 0)
705 vn_finished_write(mp);
710 * Attempt to keep the free list at wantfreevnodes length.
713 vnlru_free(int count)
718 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
719 for (; count > 0; count--) {
720 vp = TAILQ_FIRST(&vnode_free_list);
722 * The list can be modified while the free_list_mtx
723 * has been dropped and vp could be NULL here.
727 VNASSERT(vp->v_op != NULL, vp,
728 ("vnlru_free: vnode already reclaimed."));
729 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
731 * Don't recycle if we can't get the interlock.
733 if (!VI_TRYLOCK(vp)) {
734 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
737 VNASSERT(VCANRECYCLE(vp), vp,
738 ("vp inconsistent on freelist"));
740 vp->v_iflag &= ~VI_FREE;
742 mtx_unlock(&vnode_free_list_mtx);
744 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
746 VFS_UNLOCK_GIANT(vfslocked);
748 * If the recycled succeeded this vdrop will actually free
749 * the vnode. If not it will simply place it back on
753 mtx_lock(&vnode_free_list_mtx);
757 * Attempt to recycle vnodes in a context that is always safe to block.
758 * Calling vlrurecycle() from the bowels of filesystem code has some
759 * interesting deadlock problems.
761 static struct proc *vnlruproc;
762 static int vnlruproc_sig;
767 struct mount *mp, *nmp;
769 struct proc *p = vnlruproc;
771 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
775 kproc_suspend_check(p);
776 mtx_lock(&vnode_free_list_mtx);
777 if (freevnodes > wantfreevnodes)
778 vnlru_free(freevnodes - wantfreevnodes);
779 if (numvnodes <= desiredvnodes * 9 / 10) {
781 wakeup(&vnlruproc_sig);
782 msleep(vnlruproc, &vnode_free_list_mtx,
783 PVFS|PDROP, "vlruwt", hz);
786 mtx_unlock(&vnode_free_list_mtx);
788 mtx_lock(&mountlist_mtx);
789 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
790 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK)) {
791 nmp = TAILQ_NEXT(mp, mnt_list);
794 vfslocked = VFS_LOCK_GIANT(mp);
795 done += vlrureclaim(mp);
796 VFS_UNLOCK_GIANT(vfslocked);
797 mtx_lock(&mountlist_mtx);
798 nmp = TAILQ_NEXT(mp, mnt_list);
801 mtx_unlock(&mountlist_mtx);
804 /* These messages are temporary debugging aids */
805 if (vnlru_nowhere < 5)
806 printf("vnlru process getting nowhere..\n");
807 else if (vnlru_nowhere == 5)
808 printf("vnlru process messages stopped.\n");
811 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
817 static struct kproc_desc vnlru_kp = {
822 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
826 * Routines having to do with the management of the vnode table.
830 vdestroy(struct vnode *vp)
834 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
835 mtx_lock(&vnode_free_list_mtx);
837 mtx_unlock(&vnode_free_list_mtx);
839 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
840 ("cleaned vnode still on the free list."));
841 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
842 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
843 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
844 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
845 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
846 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
847 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
848 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
849 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
850 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
851 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
852 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
855 mac_vnode_destroy(vp);
857 if (vp->v_pollinfo != NULL)
858 destroy_vpollinfo(vp->v_pollinfo);
860 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
863 lockdestroy(vp->v_vnlock);
864 mtx_destroy(&vp->v_interlock);
865 mtx_destroy(BO_MTX(bo));
866 uma_zfree(vnode_zone, vp);
870 * Try to recycle a freed vnode. We abort if anyone picks up a reference
871 * before we actually vgone(). This function must be called with the vnode
872 * held to prevent the vnode from being returned to the free list midway
876 vtryrecycle(struct vnode *vp)
880 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
881 VNASSERT(vp->v_holdcnt, vp,
882 ("vtryrecycle: Recycling vp %p without a reference.", vp));
884 * This vnode may found and locked via some other list, if so we
885 * can't recycle it yet.
887 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
889 "%s: impossible to recycle, vp %p lock is already held",
891 return (EWOULDBLOCK);
894 * Don't recycle if its filesystem is being suspended.
896 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
899 "%s: impossible to recycle, cannot start the write for %p",
904 * If we got this far, we need to acquire the interlock and see if
905 * anyone picked up this vnode from another list. If not, we will
906 * mark it with DOOMED via vgonel() so that anyone who does find it
910 if (vp->v_usecount) {
911 VOP_UNLOCK(vp, LK_INTERLOCK);
912 vn_finished_write(vnmp);
914 "%s: impossible to recycle, %p is already referenced",
918 if ((vp->v_iflag & VI_DOOMED) == 0)
920 VOP_UNLOCK(vp, LK_INTERLOCK);
921 vn_finished_write(vnmp);
926 * Return the next vnode from the free list.
929 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
932 struct vnode *vp = NULL;
935 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
936 mtx_lock(&vnode_free_list_mtx);
938 * Lend our context to reclaim vnodes if they've exceeded the max.
940 if (freevnodes > wantfreevnodes)
943 * Wait for available vnodes.
945 if (numvnodes > desiredvnodes) {
946 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
948 * File system is beeing suspended, we cannot risk a
949 * deadlock here, so allocate new vnode anyway.
951 if (freevnodes > wantfreevnodes)
952 vnlru_free(freevnodes - wantfreevnodes);
955 if (vnlruproc_sig == 0) {
956 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
959 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
961 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
962 if (numvnodes > desiredvnodes) {
963 mtx_unlock(&vnode_free_list_mtx);
970 mtx_unlock(&vnode_free_list_mtx);
971 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
975 vp->v_vnlock = &vp->v_lock;
976 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
978 * By default, don't allow shared locks unless filesystems
981 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
987 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
988 bo->bo_ops = &buf_ops_bio;
990 TAILQ_INIT(&bo->bo_clean.bv_hd);
991 TAILQ_INIT(&bo->bo_dirty.bv_hd);
993 * Initialize namecache.
995 LIST_INIT(&vp->v_cache_src);
996 TAILQ_INIT(&vp->v_cache_dst);
998 * Finalize various vnode identity bits.
1003 v_incr_usecount(vp);
1007 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1008 mac_vnode_associate_singlelabel(mp, vp);
1009 else if (mp == NULL && vops != &dead_vnodeops)
1010 printf("NULL mp in getnewvnode()\n");
1013 bo->bo_bsize = mp->mnt_stat.f_iosize;
1014 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1015 vp->v_vflag |= VV_NOKNOTE;
1023 * Delete from old mount point vnode list, if on one.
1026 delmntque(struct vnode *vp)
1035 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1036 ("bad mount point vnode list size"));
1037 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1038 mp->mnt_nvnodelistsize--;
1044 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1048 vp->v_op = &dead_vnodeops;
1049 /* XXX non mp-safe fs may still call insmntque with vnode
1051 if (!VOP_ISLOCKED(vp))
1052 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1058 * Insert into list of vnodes for the new mount point, if available.
1061 insmntque1(struct vnode *vp, struct mount *mp,
1062 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1066 KASSERT(vp->v_mount == NULL,
1067 ("insmntque: vnode already on per mount vnode list"));
1068 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1069 #ifdef DEBUG_VFS_LOCKS
1070 if (!VFS_NEEDSGIANT(mp))
1071 ASSERT_VOP_ELOCKED(vp,
1072 "insmntque: mp-safe fs and non-locked vp");
1075 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1076 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1077 mp->mnt_nvnodelistsize == 0)) {
1078 locked = VOP_ISLOCKED(vp);
1079 if (!locked || (locked == LK_EXCLUSIVE &&
1080 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1089 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1090 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1091 ("neg mount point vnode list size"));
1092 mp->mnt_nvnodelistsize++;
1098 insmntque(struct vnode *vp, struct mount *mp)
1101 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1105 * Flush out and invalidate all buffers associated with a bufobj
1106 * Called with the underlying object locked.
1109 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1114 if (flags & V_SAVE) {
1115 error = bufobj_wwait(bo, slpflag, slptimeo);
1120 if (bo->bo_dirty.bv_cnt > 0) {
1122 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1125 * XXX We could save a lock/unlock if this was only
1126 * enabled under INVARIANTS
1129 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1130 panic("vinvalbuf: dirty bufs");
1134 * If you alter this loop please notice that interlock is dropped and
1135 * reacquired in flushbuflist. Special care is needed to ensure that
1136 * no race conditions occur from this.
1139 error = flushbuflist(&bo->bo_clean,
1140 flags, bo, slpflag, slptimeo);
1142 error = flushbuflist(&bo->bo_dirty,
1143 flags, bo, slpflag, slptimeo);
1144 if (error != 0 && error != EAGAIN) {
1148 } while (error != 0);
1151 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1152 * have write I/O in-progress but if there is a VM object then the
1153 * VM object can also have read-I/O in-progress.
1156 bufobj_wwait(bo, 0, 0);
1158 if (bo->bo_object != NULL) {
1159 VM_OBJECT_LOCK(bo->bo_object);
1160 vm_object_pip_wait(bo->bo_object, "bovlbx");
1161 VM_OBJECT_UNLOCK(bo->bo_object);
1164 } while (bo->bo_numoutput > 0);
1168 * Destroy the copy in the VM cache, too.
1170 if (bo->bo_object != NULL && (flags & (V_ALT | V_NORMAL)) == 0) {
1171 VM_OBJECT_LOCK(bo->bo_object);
1172 vm_object_page_remove(bo->bo_object, 0, 0,
1173 (flags & V_SAVE) ? TRUE : FALSE);
1174 VM_OBJECT_UNLOCK(bo->bo_object);
1179 if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1180 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1181 panic("vinvalbuf: flush failed");
1188 * Flush out and invalidate all buffers associated with a vnode.
1189 * Called with the underlying object locked.
1192 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1195 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1196 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1197 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1201 * Flush out buffers on the specified list.
1205 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1208 struct buf *bp, *nbp;
1213 ASSERT_BO_LOCKED(bo);
1216 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1217 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1218 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1224 lblkno = nbp->b_lblkno;
1225 xflags = nbp->b_xflags &
1226 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1229 error = BUF_TIMELOCK(bp,
1230 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1231 "flushbuf", slpflag, slptimeo);
1234 return (error != ENOLCK ? error : EAGAIN);
1236 KASSERT(bp->b_bufobj == bo,
1237 ("bp %p wrong b_bufobj %p should be %p",
1238 bp, bp->b_bufobj, bo));
1239 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1245 * XXX Since there are no node locks for NFS, I
1246 * believe there is a slight chance that a delayed
1247 * write will occur while sleeping just above, so
1250 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1255 bp->b_flags |= B_ASYNC;
1258 return (EAGAIN); /* XXX: why not loop ? */
1263 bp->b_flags |= (B_INVAL | B_RELBUF);
1264 bp->b_flags &= ~B_ASYNC;
1268 (nbp->b_bufobj != bo ||
1269 nbp->b_lblkno != lblkno ||
1271 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1272 break; /* nbp invalid */
1278 * Truncate a file's buffer and pages to a specified length. This
1279 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1283 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1284 off_t length, int blksize)
1286 struct buf *bp, *nbp;
1291 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1292 vp, cred, blksize, (uintmax_t)length);
1295 * Round up to the *next* lbn.
1297 trunclbn = (length + blksize - 1) / blksize;
1299 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1306 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1307 if (bp->b_lblkno < trunclbn)
1310 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1311 BO_MTX(bo)) == ENOLCK)
1317 bp->b_flags |= (B_INVAL | B_RELBUF);
1318 bp->b_flags &= ~B_ASYNC;
1323 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1324 (nbp->b_vp != vp) ||
1325 (nbp->b_flags & B_DELWRI))) {
1331 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1332 if (bp->b_lblkno < trunclbn)
1335 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1336 BO_MTX(bo)) == ENOLCK)
1341 bp->b_flags |= (B_INVAL | B_RELBUF);
1342 bp->b_flags &= ~B_ASYNC;
1346 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1347 (nbp->b_vp != vp) ||
1348 (nbp->b_flags & B_DELWRI) == 0)) {
1357 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1358 if (bp->b_lblkno > 0)
1361 * Since we hold the vnode lock this should only
1362 * fail if we're racing with the buf daemon.
1365 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1366 BO_MTX(bo)) == ENOLCK) {
1369 VNASSERT((bp->b_flags & B_DELWRI), vp,
1370 ("buf(%p) on dirty queue without DELWRI", bp));
1381 bufobj_wwait(bo, 0, 0);
1383 vnode_pager_setsize(vp, length);
1389 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1392 * NOTE: We have to deal with the special case of a background bitmap
1393 * buffer, a situation where two buffers will have the same logical
1394 * block offset. We want (1) only the foreground buffer to be accessed
1395 * in a lookup and (2) must differentiate between the foreground and
1396 * background buffer in the splay tree algorithm because the splay
1397 * tree cannot normally handle multiple entities with the same 'index'.
1398 * We accomplish this by adding differentiating flags to the splay tree's
1403 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1406 struct buf *lefttreemax, *righttreemin, *y;
1410 lefttreemax = righttreemin = &dummy;
1412 if (lblkno < root->b_lblkno ||
1413 (lblkno == root->b_lblkno &&
1414 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1415 if ((y = root->b_left) == NULL)
1417 if (lblkno < y->b_lblkno) {
1419 root->b_left = y->b_right;
1422 if ((y = root->b_left) == NULL)
1425 /* Link into the new root's right tree. */
1426 righttreemin->b_left = root;
1427 righttreemin = root;
1428 } else if (lblkno > root->b_lblkno ||
1429 (lblkno == root->b_lblkno &&
1430 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1431 if ((y = root->b_right) == NULL)
1433 if (lblkno > y->b_lblkno) {
1435 root->b_right = y->b_left;
1438 if ((y = root->b_right) == NULL)
1441 /* Link into the new root's left tree. */
1442 lefttreemax->b_right = root;
1449 /* Assemble the new root. */
1450 lefttreemax->b_right = root->b_left;
1451 righttreemin->b_left = root->b_right;
1452 root->b_left = dummy.b_right;
1453 root->b_right = dummy.b_left;
1458 buf_vlist_remove(struct buf *bp)
1463 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1464 ASSERT_BO_LOCKED(bp->b_bufobj);
1465 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1466 (BX_VNDIRTY|BX_VNCLEAN),
1467 ("buf_vlist_remove: Buf %p is on two lists", bp));
1468 if (bp->b_xflags & BX_VNDIRTY)
1469 bv = &bp->b_bufobj->bo_dirty;
1471 bv = &bp->b_bufobj->bo_clean;
1472 if (bp != bv->bv_root) {
1473 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1474 KASSERT(root == bp, ("splay lookup failed in remove"));
1476 if (bp->b_left == NULL) {
1479 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1480 root->b_right = bp->b_right;
1483 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1485 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1489 * Add the buffer to the sorted clean or dirty block list using a
1490 * splay tree algorithm.
1492 * NOTE: xflags is passed as a constant, optimizing this inline function!
1495 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1500 ASSERT_BO_LOCKED(bo);
1501 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1502 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1503 bp->b_xflags |= xflags;
1504 if (xflags & BX_VNDIRTY)
1509 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1513 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1514 } else if (bp->b_lblkno < root->b_lblkno ||
1515 (bp->b_lblkno == root->b_lblkno &&
1516 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1517 bp->b_left = root->b_left;
1519 root->b_left = NULL;
1520 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1522 bp->b_right = root->b_right;
1524 root->b_right = NULL;
1525 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1532 * Lookup a buffer using the splay tree. Note that we specifically avoid
1533 * shadow buffers used in background bitmap writes.
1535 * This code isn't quite efficient as it could be because we are maintaining
1536 * two sorted lists and do not know which list the block resides in.
1538 * During a "make buildworld" the desired buffer is found at one of
1539 * the roots more than 60% of the time. Thus, checking both roots
1540 * before performing either splay eliminates unnecessary splays on the
1541 * first tree splayed.
1544 gbincore(struct bufobj *bo, daddr_t lblkno)
1548 ASSERT_BO_LOCKED(bo);
1549 if ((bp = bo->bo_clean.bv_root) != NULL &&
1550 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1552 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1553 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1555 if ((bp = bo->bo_clean.bv_root) != NULL) {
1556 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1557 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1560 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1561 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1562 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1569 * Associate a buffer with a vnode.
1572 bgetvp(struct vnode *vp, struct buf *bp)
1577 ASSERT_BO_LOCKED(bo);
1578 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1580 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1581 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1582 ("bgetvp: bp already attached! %p", bp));
1585 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1586 bp->b_flags |= B_NEEDSGIANT;
1590 * Insert onto list for new vnode.
1592 buf_vlist_add(bp, bo, BX_VNCLEAN);
1596 * Disassociate a buffer from a vnode.
1599 brelvp(struct buf *bp)
1604 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1605 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1608 * Delete from old vnode list, if on one.
1610 vp = bp->b_vp; /* XXX */
1613 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1614 buf_vlist_remove(bp);
1616 panic("brelvp: Buffer %p not on queue.", bp);
1617 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1618 bo->bo_flag &= ~BO_ONWORKLST;
1619 mtx_lock(&sync_mtx);
1620 LIST_REMOVE(bo, bo_synclist);
1621 syncer_worklist_len--;
1622 mtx_unlock(&sync_mtx);
1624 bp->b_flags &= ~B_NEEDSGIANT;
1626 bp->b_bufobj = NULL;
1632 * Add an item to the syncer work queue.
1635 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1639 ASSERT_BO_LOCKED(bo);
1641 mtx_lock(&sync_mtx);
1642 if (bo->bo_flag & BO_ONWORKLST)
1643 LIST_REMOVE(bo, bo_synclist);
1645 bo->bo_flag |= BO_ONWORKLST;
1646 syncer_worklist_len++;
1649 if (delay > syncer_maxdelay - 2)
1650 delay = syncer_maxdelay - 2;
1651 slot = (syncer_delayno + delay) & syncer_mask;
1653 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1655 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1657 mtx_unlock(&sync_mtx);
1661 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1665 mtx_lock(&sync_mtx);
1666 len = syncer_worklist_len - sync_vnode_count;
1667 mtx_unlock(&sync_mtx);
1668 error = SYSCTL_OUT(req, &len, sizeof(len));
1672 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1673 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1675 static struct proc *updateproc;
1676 static void sched_sync(void);
1677 static struct kproc_desc up_kp = {
1682 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1685 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1690 *bo = LIST_FIRST(slp);
1693 vp = (*bo)->__bo_vnode; /* XXX */
1694 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1697 * We use vhold in case the vnode does not
1698 * successfully sync. vhold prevents the vnode from
1699 * going away when we unlock the sync_mtx so that
1700 * we can acquire the vnode interlock.
1703 mtx_unlock(&sync_mtx);
1705 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1707 mtx_lock(&sync_mtx);
1708 return (*bo == LIST_FIRST(slp));
1710 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1711 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1713 vn_finished_write(mp);
1715 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1717 * Put us back on the worklist. The worklist
1718 * routine will remove us from our current
1719 * position and then add us back in at a later
1722 vn_syncer_add_to_worklist(*bo, syncdelay);
1726 mtx_lock(&sync_mtx);
1731 * System filesystem synchronizer daemon.
1736 struct synclist *gnext, *next;
1737 struct synclist *gslp, *slp;
1740 struct thread *td = curthread;
1742 int net_worklist_len;
1743 int syncer_final_iter;
1748 syncer_final_iter = 0;
1750 syncer_state = SYNCER_RUNNING;
1751 starttime = time_uptime;
1752 td->td_pflags |= TDP_NORUNNINGBUF;
1754 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1757 mtx_lock(&sync_mtx);
1759 if (syncer_state == SYNCER_FINAL_DELAY &&
1760 syncer_final_iter == 0) {
1761 mtx_unlock(&sync_mtx);
1762 kproc_suspend_check(td->td_proc);
1763 mtx_lock(&sync_mtx);
1765 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1766 if (syncer_state != SYNCER_RUNNING &&
1767 starttime != time_uptime) {
1769 printf("\nSyncing disks, vnodes remaining...");
1772 printf("%d ", net_worklist_len);
1774 starttime = time_uptime;
1777 * Push files whose dirty time has expired. Be careful
1778 * of interrupt race on slp queue.
1780 * Skip over empty worklist slots when shutting down.
1783 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1784 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1785 syncer_delayno += 1;
1786 if (syncer_delayno == syncer_maxdelay)
1788 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1789 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1791 * If the worklist has wrapped since the
1792 * it was emptied of all but syncer vnodes,
1793 * switch to the FINAL_DELAY state and run
1794 * for one more second.
1796 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1797 net_worklist_len == 0 &&
1798 last_work_seen == syncer_delayno) {
1799 syncer_state = SYNCER_FINAL_DELAY;
1800 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1802 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1803 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1806 * Keep track of the last time there was anything
1807 * on the worklist other than syncer vnodes.
1808 * Return to the SHUTTING_DOWN state if any
1811 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1812 last_work_seen = syncer_delayno;
1813 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1814 syncer_state = SYNCER_SHUTTING_DOWN;
1815 while (!LIST_EMPTY(slp)) {
1816 error = sync_vnode(slp, &bo, td);
1818 LIST_REMOVE(bo, bo_synclist);
1819 LIST_INSERT_HEAD(next, bo, bo_synclist);
1823 if (!LIST_EMPTY(gslp)) {
1824 mtx_unlock(&sync_mtx);
1826 mtx_lock(&sync_mtx);
1827 while (!LIST_EMPTY(gslp)) {
1828 error = sync_vnode(gslp, &bo, td);
1830 LIST_REMOVE(bo, bo_synclist);
1831 LIST_INSERT_HEAD(gnext, bo,
1838 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1839 syncer_final_iter--;
1841 * The variable rushjob allows the kernel to speed up the
1842 * processing of the filesystem syncer process. A rushjob
1843 * value of N tells the filesystem syncer to process the next
1844 * N seconds worth of work on its queue ASAP. Currently rushjob
1845 * is used by the soft update code to speed up the filesystem
1846 * syncer process when the incore state is getting so far
1847 * ahead of the disk that the kernel memory pool is being
1848 * threatened with exhaustion.
1855 * Just sleep for a short period of time between
1856 * iterations when shutting down to allow some I/O
1859 * If it has taken us less than a second to process the
1860 * current work, then wait. Otherwise start right over
1861 * again. We can still lose time if any single round
1862 * takes more than two seconds, but it does not really
1863 * matter as we are just trying to generally pace the
1864 * filesystem activity.
1866 if (syncer_state != SYNCER_RUNNING)
1867 cv_timedwait(&sync_wakeup, &sync_mtx,
1868 hz / SYNCER_SHUTDOWN_SPEEDUP);
1869 else if (time_uptime == starttime)
1870 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1875 * Request the syncer daemon to speed up its work.
1876 * We never push it to speed up more than half of its
1877 * normal turn time, otherwise it could take over the cpu.
1880 speedup_syncer(void)
1884 mtx_lock(&sync_mtx);
1885 if (rushjob < syncdelay / 2) {
1887 stat_rush_requests += 1;
1890 mtx_unlock(&sync_mtx);
1891 cv_broadcast(&sync_wakeup);
1896 * Tell the syncer to speed up its work and run though its work
1897 * list several times, then tell it to shut down.
1900 syncer_shutdown(void *arg, int howto)
1903 if (howto & RB_NOSYNC)
1905 mtx_lock(&sync_mtx);
1906 syncer_state = SYNCER_SHUTTING_DOWN;
1908 mtx_unlock(&sync_mtx);
1909 cv_broadcast(&sync_wakeup);
1910 kproc_shutdown(arg, howto);
1914 * Reassign a buffer from one vnode to another.
1915 * Used to assign file specific control information
1916 * (indirect blocks) to the vnode to which they belong.
1919 reassignbuf(struct buf *bp)
1932 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
1933 bp, bp->b_vp, bp->b_flags);
1935 * B_PAGING flagged buffers cannot be reassigned because their vp
1936 * is not fully linked in.
1938 if (bp->b_flags & B_PAGING)
1939 panic("cannot reassign paging buffer");
1942 * Delete from old vnode list, if on one.
1945 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1946 buf_vlist_remove(bp);
1948 panic("reassignbuf: Buffer %p not on queue.", bp);
1950 * If dirty, put on list of dirty buffers; otherwise insert onto list
1953 if (bp->b_flags & B_DELWRI) {
1954 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
1955 switch (vp->v_type) {
1965 vn_syncer_add_to_worklist(bo, delay);
1967 buf_vlist_add(bp, bo, BX_VNDIRTY);
1969 buf_vlist_add(bp, bo, BX_VNCLEAN);
1971 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1972 mtx_lock(&sync_mtx);
1973 LIST_REMOVE(bo, bo_synclist);
1974 syncer_worklist_len--;
1975 mtx_unlock(&sync_mtx);
1976 bo->bo_flag &= ~BO_ONWORKLST;
1981 bp = TAILQ_FIRST(&bv->bv_hd);
1982 KASSERT(bp == NULL || bp->b_bufobj == bo,
1983 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1984 bp = TAILQ_LAST(&bv->bv_hd, buflists);
1985 KASSERT(bp == NULL || bp->b_bufobj == bo,
1986 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1988 bp = TAILQ_FIRST(&bv->bv_hd);
1989 KASSERT(bp == NULL || bp->b_bufobj == bo,
1990 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1991 bp = TAILQ_LAST(&bv->bv_hd, buflists);
1992 KASSERT(bp == NULL || bp->b_bufobj == bo,
1993 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1999 * Increment the use and hold counts on the vnode, taking care to reference
2000 * the driver's usecount if this is a chardev. The vholdl() will remove
2001 * the vnode from the free list if it is presently free. Requires the
2002 * vnode interlock and returns with it held.
2005 v_incr_usecount(struct vnode *vp)
2008 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2010 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2012 vp->v_rdev->si_usecount++;
2019 * Turn a holdcnt into a use+holdcnt such that only one call to
2020 * v_decr_usecount is needed.
2023 v_upgrade_usecount(struct vnode *vp)
2026 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2028 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2030 vp->v_rdev->si_usecount++;
2036 * Decrement the vnode use and hold count along with the driver's usecount
2037 * if this is a chardev. The vdropl() below releases the vnode interlock
2038 * as it may free the vnode.
2041 v_decr_usecount(struct vnode *vp)
2044 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2045 VNASSERT(vp->v_usecount > 0, vp,
2046 ("v_decr_usecount: negative usecount"));
2047 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2049 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2051 vp->v_rdev->si_usecount--;
2058 * Decrement only the use count and driver use count. This is intended to
2059 * be paired with a follow on vdropl() to release the remaining hold count.
2060 * In this way we may vgone() a vnode with a 0 usecount without risk of
2061 * having it end up on a free list because the hold count is kept above 0.
2064 v_decr_useonly(struct vnode *vp)
2067 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2068 VNASSERT(vp->v_usecount > 0, vp,
2069 ("v_decr_useonly: negative usecount"));
2070 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2072 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2074 vp->v_rdev->si_usecount--;
2080 * Grab a particular vnode from the free list, increment its
2081 * reference count and lock it. VI_DOOMED is set if the vnode
2082 * is being destroyed. Only callers who specify LK_RETRY will
2083 * see doomed vnodes. If inactive processing was delayed in
2084 * vput try to do it here.
2087 vget(struct vnode *vp, int flags, struct thread *td)
2092 VFS_ASSERT_GIANT(vp->v_mount);
2093 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2094 ("vget: invalid lock operation"));
2095 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2097 if ((flags & LK_INTERLOCK) == 0)
2100 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2102 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2106 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2107 panic("vget: vn_lock failed to return ENOENT\n");
2109 /* Upgrade our holdcnt to a usecount. */
2110 v_upgrade_usecount(vp);
2112 * We don't guarantee that any particular close will
2113 * trigger inactive processing so just make a best effort
2114 * here at preventing a reference to a removed file. If
2115 * we don't succeed no harm is done.
2117 if (vp->v_iflag & VI_OWEINACT) {
2118 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2119 (flags & LK_NOWAIT) == 0)
2121 vp->v_iflag &= ~VI_OWEINACT;
2128 * Increase the reference count of a vnode.
2131 vref(struct vnode *vp)
2134 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2136 v_incr_usecount(vp);
2141 * Return reference count of a vnode.
2143 * The results of this call are only guaranteed when some mechanism other
2144 * than the VI lock is used to stop other processes from gaining references
2145 * to the vnode. This may be the case if the caller holds the only reference.
2146 * This is also useful when stale data is acceptable as race conditions may
2147 * be accounted for by some other means.
2150 vrefcnt(struct vnode *vp)
2155 usecnt = vp->v_usecount;
2161 #define VPUTX_VRELE 1
2162 #define VPUTX_VPUT 2
2163 #define VPUTX_VUNREF 3
2166 vputx(struct vnode *vp, int func)
2170 KASSERT(vp != NULL, ("vputx: null vp"));
2171 if (func == VPUTX_VUNREF)
2172 ASSERT_VOP_ELOCKED(vp, "vunref");
2173 else if (func == VPUTX_VPUT)
2174 ASSERT_VOP_LOCKED(vp, "vput");
2176 KASSERT(func == VPUTX_VRELE, ("vputx: wrong func"));
2177 VFS_ASSERT_GIANT(vp->v_mount);
2178 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2181 /* Skip this v_writecount check if we're going to panic below. */
2182 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2183 ("vputx: missed vn_close"));
2186 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2187 vp->v_usecount == 1)) {
2188 if (func == VPUTX_VPUT)
2190 v_decr_usecount(vp);
2194 if (vp->v_usecount != 1) {
2196 vprint("vputx: negative ref count", vp);
2198 panic("vputx: negative ref cnt");
2200 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2202 * We want to hold the vnode until the inactive finishes to
2203 * prevent vgone() races. We drop the use count here and the
2204 * hold count below when we're done.
2208 * We must call VOP_INACTIVE with the node locked. Mark
2209 * as VI_DOINGINACT to avoid recursion.
2211 vp->v_iflag |= VI_OWEINACT;
2212 if (func == VPUTX_VRELE) {
2213 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2215 } else if (func == VPUTX_VPUT && VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2216 error = VOP_LOCK(vp, LK_UPGRADE | LK_INTERLOCK | LK_NOWAIT);
2219 if (vp->v_usecount > 0)
2220 vp->v_iflag &= ~VI_OWEINACT;
2222 if (vp->v_iflag & VI_OWEINACT)
2223 vinactive(vp, curthread);
2224 if (func != VPUTX_VUNREF)
2231 * Vnode put/release.
2232 * If count drops to zero, call inactive routine and return to freelist.
2235 vrele(struct vnode *vp)
2238 vputx(vp, VPUTX_VRELE);
2242 * Release an already locked vnode. This give the same effects as
2243 * unlock+vrele(), but takes less time and avoids releasing and
2244 * re-aquiring the lock (as vrele() acquires the lock internally.)
2247 vput(struct vnode *vp)
2250 vputx(vp, VPUTX_VPUT);
2254 * Release an exclusively locked vnode. Do not unlock the vnode lock.
2257 vunref(struct vnode *vp)
2260 vputx(vp, VPUTX_VUNREF);
2264 * Somebody doesn't want the vnode recycled.
2267 vhold(struct vnode *vp)
2276 vholdl(struct vnode *vp)
2279 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2281 if (VSHOULDBUSY(vp))
2286 * Note that there is one less who cares about this vnode. vdrop() is the
2287 * opposite of vhold().
2290 vdrop(struct vnode *vp)
2298 * Drop the hold count of the vnode. If this is the last reference to
2299 * the vnode we will free it if it has been vgone'd otherwise it is
2300 * placed on the free list.
2303 vdropl(struct vnode *vp)
2306 ASSERT_VI_LOCKED(vp, "vdropl");
2307 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2308 if (vp->v_holdcnt <= 0)
2309 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2311 if (vp->v_holdcnt == 0) {
2312 if (vp->v_iflag & VI_DOOMED) {
2313 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__,
2324 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2325 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2326 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2327 * failed lock upgrade.
2330 vinactive(struct vnode *vp, struct thread *td)
2333 ASSERT_VOP_ELOCKED(vp, "vinactive");
2334 ASSERT_VI_LOCKED(vp, "vinactive");
2335 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2336 ("vinactive: recursed on VI_DOINGINACT"));
2337 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2338 vp->v_iflag |= VI_DOINGINACT;
2339 vp->v_iflag &= ~VI_OWEINACT;
2341 VOP_INACTIVE(vp, td);
2343 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2344 ("vinactive: lost VI_DOINGINACT"));
2345 vp->v_iflag &= ~VI_DOINGINACT;
2349 * Remove any vnodes in the vnode table belonging to mount point mp.
2351 * If FORCECLOSE is not specified, there should not be any active ones,
2352 * return error if any are found (nb: this is a user error, not a
2353 * system error). If FORCECLOSE is specified, detach any active vnodes
2356 * If WRITECLOSE is set, only flush out regular file vnodes open for
2359 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2361 * `rootrefs' specifies the base reference count for the root vnode
2362 * of this filesystem. The root vnode is considered busy if its
2363 * v_usecount exceeds this value. On a successful return, vflush(, td)
2364 * will call vrele() on the root vnode exactly rootrefs times.
2365 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2369 static int busyprt = 0; /* print out busy vnodes */
2370 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
2374 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td)
2376 struct vnode *vp, *mvp, *rootvp = NULL;
2378 int busy = 0, error;
2380 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2383 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2384 ("vflush: bad args"));
2386 * Get the filesystem root vnode. We can vput() it
2387 * immediately, since with rootrefs > 0, it won't go away.
2389 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2390 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2399 MNT_VNODE_FOREACH(vp, mp, mvp) {
2404 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2408 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2412 * Skip over a vnodes marked VV_SYSTEM.
2414 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2421 * If WRITECLOSE is set, flush out unlinked but still open
2422 * files (even if open only for reading) and regular file
2423 * vnodes open for writing.
2425 if (flags & WRITECLOSE) {
2426 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2429 if ((vp->v_type == VNON ||
2430 (error == 0 && vattr.va_nlink > 0)) &&
2431 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2440 * With v_usecount == 0, all we need to do is clear out the
2441 * vnode data structures and we are done.
2443 * If FORCECLOSE is set, forcibly close the vnode.
2445 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2446 VNASSERT(vp->v_usecount == 0 ||
2447 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2448 ("device VNODE %p is FORCECLOSED", vp));
2454 vprint("vflush: busy vnode", vp);
2462 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2464 * If just the root vnode is busy, and if its refcount
2465 * is equal to `rootrefs', then go ahead and kill it.
2468 KASSERT(busy > 0, ("vflush: not busy"));
2469 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2470 ("vflush: usecount %d < rootrefs %d",
2471 rootvp->v_usecount, rootrefs));
2472 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2473 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2475 VOP_UNLOCK(rootvp, 0);
2481 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2485 for (; rootrefs > 0; rootrefs--)
2491 * Recycle an unused vnode to the front of the free list.
2494 vrecycle(struct vnode *vp, struct thread *td)
2498 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2499 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2502 if (vp->v_usecount == 0) {
2511 * Eliminate all activity associated with a vnode
2512 * in preparation for reuse.
2515 vgone(struct vnode *vp)
2523 * vgone, with the vp interlock held.
2526 vgonel(struct vnode *vp)
2533 ASSERT_VOP_ELOCKED(vp, "vgonel");
2534 ASSERT_VI_LOCKED(vp, "vgonel");
2535 VNASSERT(vp->v_holdcnt, vp,
2536 ("vgonel: vp %p has no reference.", vp));
2537 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2541 * Don't vgonel if we're already doomed.
2543 if (vp->v_iflag & VI_DOOMED)
2545 vp->v_iflag |= VI_DOOMED;
2547 * Check to see if the vnode is in use. If so, we have to call
2548 * VOP_CLOSE() and VOP_INACTIVE().
2550 active = vp->v_usecount;
2551 oweinact = (vp->v_iflag & VI_OWEINACT);
2554 * Clean out any buffers associated with the vnode.
2555 * If the flush fails, just toss the buffers.
2558 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2559 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2560 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2561 vinvalbuf(vp, 0, 0, 0);
2564 * If purging an active vnode, it must be closed and
2565 * deactivated before being reclaimed.
2568 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2569 if (oweinact || active) {
2571 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2576 * Reclaim the vnode.
2578 if (VOP_RECLAIM(vp, td))
2579 panic("vgone: cannot reclaim");
2581 vn_finished_secondary_write(mp);
2582 VNASSERT(vp->v_object == NULL, vp,
2583 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2585 * Clear the advisory locks and wake up waiting threads.
2587 lf_purgelocks(vp, &(vp->v_lockf));
2589 * Delete from old mount point vnode list.
2594 * Done with purge, reset to the standard lock and invalidate
2598 vp->v_vnlock = &vp->v_lock;
2599 vp->v_op = &dead_vnodeops;
2605 * Calculate the total number of references to a special device.
2608 vcount(struct vnode *vp)
2613 count = vp->v_rdev->si_usecount;
2619 * Same as above, but using the struct cdev *as argument
2622 count_dev(struct cdev *dev)
2627 count = dev->si_usecount;
2633 * Print out a description of a vnode.
2635 static char *typename[] =
2636 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2640 vn_printf(struct vnode *vp, const char *fmt, ...)
2643 char buf[256], buf2[16];
2649 printf("%p: ", (void *)vp);
2650 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2651 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2652 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2655 if (vp->v_vflag & VV_ROOT)
2656 strlcat(buf, "|VV_ROOT", sizeof(buf));
2657 if (vp->v_vflag & VV_ISTTY)
2658 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2659 if (vp->v_vflag & VV_NOSYNC)
2660 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2661 if (vp->v_vflag & VV_CACHEDLABEL)
2662 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2663 if (vp->v_vflag & VV_TEXT)
2664 strlcat(buf, "|VV_TEXT", sizeof(buf));
2665 if (vp->v_vflag & VV_COPYONWRITE)
2666 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2667 if (vp->v_vflag & VV_SYSTEM)
2668 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2669 if (vp->v_vflag & VV_PROCDEP)
2670 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2671 if (vp->v_vflag & VV_NOKNOTE)
2672 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2673 if (vp->v_vflag & VV_DELETED)
2674 strlcat(buf, "|VV_DELETED", sizeof(buf));
2675 if (vp->v_vflag & VV_MD)
2676 strlcat(buf, "|VV_MD", sizeof(buf));
2677 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC |
2678 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2679 VV_NOKNOTE | VV_DELETED | VV_MD);
2681 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2682 strlcat(buf, buf2, sizeof(buf));
2684 if (vp->v_iflag & VI_MOUNT)
2685 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2686 if (vp->v_iflag & VI_AGE)
2687 strlcat(buf, "|VI_AGE", sizeof(buf));
2688 if (vp->v_iflag & VI_DOOMED)
2689 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2690 if (vp->v_iflag & VI_FREE)
2691 strlcat(buf, "|VI_FREE", sizeof(buf));
2692 if (vp->v_iflag & VI_DOINGINACT)
2693 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2694 if (vp->v_iflag & VI_OWEINACT)
2695 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2696 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2697 VI_DOINGINACT | VI_OWEINACT);
2699 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2700 strlcat(buf, buf2, sizeof(buf));
2702 printf(" flags (%s)\n", buf + 1);
2703 if (mtx_owned(VI_MTX(vp)))
2704 printf(" VI_LOCKed");
2705 if (vp->v_object != NULL)
2706 printf(" v_object %p ref %d pages %d\n",
2707 vp->v_object, vp->v_object->ref_count,
2708 vp->v_object->resident_page_count);
2710 lockmgr_printinfo(vp->v_vnlock);
2711 if (vp->v_data != NULL)
2717 * List all of the locked vnodes in the system.
2718 * Called when debugging the kernel.
2720 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2722 struct mount *mp, *nmp;
2726 * Note: because this is DDB, we can't obey the locking semantics
2727 * for these structures, which means we could catch an inconsistent
2728 * state and dereference a nasty pointer. Not much to be done
2731 db_printf("Locked vnodes\n");
2732 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2733 nmp = TAILQ_NEXT(mp, mnt_list);
2734 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2735 if (vp->v_type != VMARKER &&
2739 nmp = TAILQ_NEXT(mp, mnt_list);
2744 * Show details about the given vnode.
2746 DB_SHOW_COMMAND(vnode, db_show_vnode)
2752 vp = (struct vnode *)addr;
2753 vn_printf(vp, "vnode ");
2757 * Show details about the given mount point.
2759 DB_SHOW_COMMAND(mount, db_show_mount)
2769 /* No address given, print short info about all mount points. */
2770 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2771 db_printf("%p %s on %s (%s)\n", mp,
2772 mp->mnt_stat.f_mntfromname,
2773 mp->mnt_stat.f_mntonname,
2774 mp->mnt_stat.f_fstypename);
2778 db_printf("\nMore info: show mount <addr>\n");
2782 mp = (struct mount *)addr;
2783 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
2784 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
2787 flags = mp->mnt_flag;
2788 #define MNT_FLAG(flag) do { \
2789 if (flags & (flag)) { \
2790 if (buf[0] != '\0') \
2791 strlcat(buf, ", ", sizeof(buf)); \
2792 strlcat(buf, (#flag) + 4, sizeof(buf)); \
2796 MNT_FLAG(MNT_RDONLY);
2797 MNT_FLAG(MNT_SYNCHRONOUS);
2798 MNT_FLAG(MNT_NOEXEC);
2799 MNT_FLAG(MNT_NOSUID);
2800 MNT_FLAG(MNT_UNION);
2801 MNT_FLAG(MNT_ASYNC);
2802 MNT_FLAG(MNT_SUIDDIR);
2803 MNT_FLAG(MNT_SOFTDEP);
2804 MNT_FLAG(MNT_NOSYMFOLLOW);
2805 MNT_FLAG(MNT_GJOURNAL);
2806 MNT_FLAG(MNT_MULTILABEL);
2808 MNT_FLAG(MNT_NOATIME);
2809 MNT_FLAG(MNT_NOCLUSTERR);
2810 MNT_FLAG(MNT_NOCLUSTERW);
2811 MNT_FLAG(MNT_NFS4ACLS);
2812 MNT_FLAG(MNT_EXRDONLY);
2813 MNT_FLAG(MNT_EXPORTED);
2814 MNT_FLAG(MNT_DEFEXPORTED);
2815 MNT_FLAG(MNT_EXPORTANON);
2816 MNT_FLAG(MNT_EXKERB);
2817 MNT_FLAG(MNT_EXPUBLIC);
2818 MNT_FLAG(MNT_LOCAL);
2819 MNT_FLAG(MNT_QUOTA);
2820 MNT_FLAG(MNT_ROOTFS);
2822 MNT_FLAG(MNT_IGNORE);
2823 MNT_FLAG(MNT_UPDATE);
2824 MNT_FLAG(MNT_DELEXPORT);
2825 MNT_FLAG(MNT_RELOAD);
2826 MNT_FLAG(MNT_FORCE);
2827 MNT_FLAG(MNT_SNAPSHOT);
2828 MNT_FLAG(MNT_BYFSID);
2832 strlcat(buf, ", ", sizeof(buf));
2833 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2836 db_printf(" mnt_flag = %s\n", buf);
2839 flags = mp->mnt_kern_flag;
2840 #define MNT_KERN_FLAG(flag) do { \
2841 if (flags & (flag)) { \
2842 if (buf[0] != '\0') \
2843 strlcat(buf, ", ", sizeof(buf)); \
2844 strlcat(buf, (#flag) + 5, sizeof(buf)); \
2848 MNT_KERN_FLAG(MNTK_UNMOUNTF);
2849 MNT_KERN_FLAG(MNTK_ASYNC);
2850 MNT_KERN_FLAG(MNTK_SOFTDEP);
2851 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
2852 MNT_KERN_FLAG(MNTK_UNMOUNT);
2853 MNT_KERN_FLAG(MNTK_MWAIT);
2854 MNT_KERN_FLAG(MNTK_SUSPEND);
2855 MNT_KERN_FLAG(MNTK_SUSPEND2);
2856 MNT_KERN_FLAG(MNTK_SUSPENDED);
2857 MNT_KERN_FLAG(MNTK_MPSAFE);
2858 MNT_KERN_FLAG(MNTK_NOKNOTE);
2859 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
2860 #undef MNT_KERN_FLAG
2863 strlcat(buf, ", ", sizeof(buf));
2864 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2867 db_printf(" mnt_kern_flag = %s\n", buf);
2869 db_printf(" mnt_opt = ");
2870 opt = TAILQ_FIRST(mp->mnt_opt);
2872 db_printf("%s", opt->name);
2873 opt = TAILQ_NEXT(opt, link);
2874 while (opt != NULL) {
2875 db_printf(", %s", opt->name);
2876 opt = TAILQ_NEXT(opt, link);
2882 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
2883 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
2884 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
2885 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
2886 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
2887 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
2888 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
2889 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
2890 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
2891 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
2892 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
2893 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
2895 db_printf(" mnt_cred = { uid=%u ruid=%u",
2896 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
2897 if (jailed(mp->mnt_cred))
2898 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
2900 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
2901 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
2902 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
2903 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
2904 db_printf(" mnt_noasync = %u\n", mp->mnt_noasync);
2905 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
2906 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
2907 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
2908 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
2909 db_printf(" mnt_secondary_accwrites = %d\n",
2910 mp->mnt_secondary_accwrites);
2911 db_printf(" mnt_gjprovider = %s\n",
2912 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
2915 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2916 if (vp->v_type != VMARKER) {
2917 vn_printf(vp, "vnode ");
2926 * Fill in a struct xvfsconf based on a struct vfsconf.
2929 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
2932 strcpy(xvfsp->vfc_name, vfsp->vfc_name);
2933 xvfsp->vfc_typenum = vfsp->vfc_typenum;
2934 xvfsp->vfc_refcount = vfsp->vfc_refcount;
2935 xvfsp->vfc_flags = vfsp->vfc_flags;
2937 * These are unused in userland, we keep them
2938 * to not break binary compatibility.
2940 xvfsp->vfc_vfsops = NULL;
2941 xvfsp->vfc_next = NULL;
2945 * Top level filesystem related information gathering.
2948 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
2950 struct vfsconf *vfsp;
2951 struct xvfsconf xvfsp;
2955 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
2956 bzero(&xvfsp, sizeof(xvfsp));
2957 vfsconf2x(vfsp, &xvfsp);
2958 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp);
2965 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
2966 "S,xvfsconf", "List of all configured filesystems");
2968 #ifndef BURN_BRIDGES
2969 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
2972 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2974 int *name = (int *)arg1 - 1; /* XXX */
2975 u_int namelen = arg2 + 1; /* XXX */
2976 struct vfsconf *vfsp;
2977 struct xvfsconf xvfsp;
2979 printf("WARNING: userland calling deprecated sysctl, "
2980 "please rebuild world\n");
2982 #if 1 || defined(COMPAT_PRELITE2)
2983 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2985 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2989 case VFS_MAXTYPENUM:
2992 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2995 return (ENOTDIR); /* overloaded */
2996 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
2997 if (vfsp->vfc_typenum == name[2])
3000 return (EOPNOTSUPP);
3001 bzero(&xvfsp, sizeof(xvfsp));
3002 vfsconf2x(vfsp, &xvfsp);
3003 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3005 return (EOPNOTSUPP);
3008 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
3009 vfs_sysctl, "Generic filesystem");
3011 #if 1 || defined(COMPAT_PRELITE2)
3014 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3017 struct vfsconf *vfsp;
3018 struct ovfsconf ovfs;
3020 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3021 bzero(&ovfs, sizeof(ovfs));
3022 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3023 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3024 ovfs.vfc_index = vfsp->vfc_typenum;
3025 ovfs.vfc_refcount = vfsp->vfc_refcount;
3026 ovfs.vfc_flags = vfsp->vfc_flags;
3027 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3034 #endif /* 1 || COMPAT_PRELITE2 */
3035 #endif /* !BURN_BRIDGES */
3037 #define KINFO_VNODESLOP 10
3040 * Dump vnode list (via sysctl).
3044 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3052 * Stale numvnodes access is not fatal here.
3055 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3057 /* Make an estimate */
3058 return (SYSCTL_OUT(req, 0, len));
3060 error = sysctl_wire_old_buffer(req, 0);
3063 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3065 mtx_lock(&mountlist_mtx);
3066 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3067 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3070 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3074 xvn[n].xv_size = sizeof *xvn;
3075 xvn[n].xv_vnode = vp;
3076 xvn[n].xv_id = 0; /* XXX compat */
3077 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3079 XV_COPY(writecount);
3085 xvn[n].xv_flag = vp->v_vflag;
3087 switch (vp->v_type) {
3094 if (vp->v_rdev == NULL) {
3098 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3101 xvn[n].xv_socket = vp->v_socket;
3104 xvn[n].xv_fifo = vp->v_fifoinfo;
3109 /* shouldn't happen? */
3117 mtx_lock(&mountlist_mtx);
3122 mtx_unlock(&mountlist_mtx);
3124 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3129 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3130 0, 0, sysctl_vnode, "S,xvnode", "");
3134 * Unmount all filesystems. The list is traversed in reverse order
3135 * of mounting to avoid dependencies.
3138 vfs_unmountall(void)
3144 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
3145 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3149 * Since this only runs when rebooting, it is not interlocked.
3151 while(!TAILQ_EMPTY(&mountlist)) {
3152 mp = TAILQ_LAST(&mountlist, mntlist);
3153 error = dounmount(mp, MNT_FORCE, td);
3155 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3157 * XXX: Due to the way in which we mount the root
3158 * file system off of devfs, devfs will generate a
3159 * "busy" warning when we try to unmount it before
3160 * the root. Don't print a warning as a result in
3161 * order to avoid false positive errors that may
3162 * cause needless upset.
3164 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3165 printf("unmount of %s failed (",
3166 mp->mnt_stat.f_mntonname);
3170 printf("%d)\n", error);
3173 /* The unmount has removed mp from the mountlist */
3179 * perform msync on all vnodes under a mount point
3180 * the mount point must be locked.
3183 vfs_msync(struct mount *mp, int flags)
3185 struct vnode *vp, *mvp;
3186 struct vm_object *obj;
3188 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3190 MNT_VNODE_FOREACH(vp, mp, mvp) {
3193 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3194 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3197 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3199 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3207 VM_OBJECT_LOCK(obj);
3208 vm_object_page_clean(obj, 0, 0,
3210 OBJPC_SYNC : OBJPC_NOSYNC);
3211 VM_OBJECT_UNLOCK(obj);
3223 * Mark a vnode as free, putting it up for recycling.
3226 vfree(struct vnode *vp)
3229 ASSERT_VI_LOCKED(vp, "vfree");
3230 mtx_lock(&vnode_free_list_mtx);
3231 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
3232 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
3233 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
3234 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
3235 ("vfree: Freeing doomed vnode"));
3236 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3237 if (vp->v_iflag & VI_AGE) {
3238 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
3240 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
3243 vp->v_iflag &= ~VI_AGE;
3244 vp->v_iflag |= VI_FREE;
3245 mtx_unlock(&vnode_free_list_mtx);
3249 * Opposite of vfree() - mark a vnode as in use.
3252 vbusy(struct vnode *vp)
3254 ASSERT_VI_LOCKED(vp, "vbusy");
3255 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
3256 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
3257 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3259 mtx_lock(&vnode_free_list_mtx);
3260 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3262 vp->v_iflag &= ~(VI_FREE|VI_AGE);
3263 mtx_unlock(&vnode_free_list_mtx);
3267 destroy_vpollinfo(struct vpollinfo *vi)
3269 knlist_destroy(&vi->vpi_selinfo.si_note);
3270 mtx_destroy(&vi->vpi_lock);
3271 uma_zfree(vnodepoll_zone, vi);
3275 * Initalize per-vnode helper structure to hold poll-related state.
3278 v_addpollinfo(struct vnode *vp)
3280 struct vpollinfo *vi;
3282 if (vp->v_pollinfo != NULL)
3284 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3285 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3286 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3287 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3289 if (vp->v_pollinfo != NULL) {
3291 destroy_vpollinfo(vi);
3294 vp->v_pollinfo = vi;
3299 * Record a process's interest in events which might happen to
3300 * a vnode. Because poll uses the historic select-style interface
3301 * internally, this routine serves as both the ``check for any
3302 * pending events'' and the ``record my interest in future events''
3303 * functions. (These are done together, while the lock is held,
3304 * to avoid race conditions.)
3307 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3311 mtx_lock(&vp->v_pollinfo->vpi_lock);
3312 if (vp->v_pollinfo->vpi_revents & events) {
3314 * This leaves events we are not interested
3315 * in available for the other process which
3316 * which presumably had requested them
3317 * (otherwise they would never have been
3320 events &= vp->v_pollinfo->vpi_revents;
3321 vp->v_pollinfo->vpi_revents &= ~events;
3323 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3326 vp->v_pollinfo->vpi_events |= events;
3327 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3328 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3333 * Routine to create and manage a filesystem syncer vnode.
3335 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3336 static int sync_fsync(struct vop_fsync_args *);
3337 static int sync_inactive(struct vop_inactive_args *);
3338 static int sync_reclaim(struct vop_reclaim_args *);
3340 static struct vop_vector sync_vnodeops = {
3341 .vop_bypass = VOP_EOPNOTSUPP,
3342 .vop_close = sync_close, /* close */
3343 .vop_fsync = sync_fsync, /* fsync */
3344 .vop_inactive = sync_inactive, /* inactive */
3345 .vop_reclaim = sync_reclaim, /* reclaim */
3346 .vop_lock1 = vop_stdlock, /* lock */
3347 .vop_unlock = vop_stdunlock, /* unlock */
3348 .vop_islocked = vop_stdislocked, /* islocked */
3352 * Create a new filesystem syncer vnode for the specified mount point.
3355 vfs_allocate_syncvnode(struct mount *mp)
3359 static long start, incr, next;
3362 /* Allocate a new vnode */
3363 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) {
3364 mp->mnt_syncer = NULL;
3368 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3369 vp->v_vflag |= VV_FORCEINSMQ;
3370 error = insmntque(vp, mp);
3372 panic("vfs_allocate_syncvnode: insmntque failed");
3373 vp->v_vflag &= ~VV_FORCEINSMQ;
3376 * Place the vnode onto the syncer worklist. We attempt to
3377 * scatter them about on the list so that they will go off
3378 * at evenly distributed times even if all the filesystems
3379 * are mounted at once.
3382 if (next == 0 || next > syncer_maxdelay) {
3386 start = syncer_maxdelay / 2;
3387 incr = syncer_maxdelay;
3393 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3394 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3395 mtx_lock(&sync_mtx);
3397 mtx_unlock(&sync_mtx);
3399 mp->mnt_syncer = vp;
3404 * Do a lazy sync of the filesystem.
3407 sync_fsync(struct vop_fsync_args *ap)
3409 struct vnode *syncvp = ap->a_vp;
3410 struct mount *mp = syncvp->v_mount;
3415 * We only need to do something if this is a lazy evaluation.
3417 if (ap->a_waitfor != MNT_LAZY)
3421 * Move ourselves to the back of the sync list.
3423 bo = &syncvp->v_bufobj;
3425 vn_syncer_add_to_worklist(bo, syncdelay);
3429 * Walk the list of vnodes pushing all that are dirty and
3430 * not already on the sync list.
3432 mtx_lock(&mountlist_mtx);
3433 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
3434 mtx_unlock(&mountlist_mtx);
3437 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3443 mp->mnt_kern_flag &= ~MNTK_ASYNC;
3445 vfs_msync(mp, MNT_NOWAIT);
3446 error = VFS_SYNC(mp, MNT_LAZY);
3449 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
3450 mp->mnt_kern_flag |= MNTK_ASYNC;
3452 vn_finished_write(mp);
3458 * The syncer vnode is no referenced.
3461 sync_inactive(struct vop_inactive_args *ap)
3469 * The syncer vnode is no longer needed and is being decommissioned.
3471 * Modifications to the worklist must be protected by sync_mtx.
3474 sync_reclaim(struct vop_reclaim_args *ap)
3476 struct vnode *vp = ap->a_vp;
3481 vp->v_mount->mnt_syncer = NULL;
3482 if (bo->bo_flag & BO_ONWORKLST) {
3483 mtx_lock(&sync_mtx);
3484 LIST_REMOVE(bo, bo_synclist);
3485 syncer_worklist_len--;
3487 mtx_unlock(&sync_mtx);
3488 bo->bo_flag &= ~BO_ONWORKLST;
3496 * Check if vnode represents a disk device
3499 vn_isdisk(struct vnode *vp, int *errp)
3505 if (vp->v_type != VCHR)
3507 else if (vp->v_rdev == NULL)
3509 else if (vp->v_rdev->si_devsw == NULL)
3511 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3516 return (error == 0);
3520 * Common filesystem object access control check routine. Accepts a
3521 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3522 * and optional call-by-reference privused argument allowing vaccess()
3523 * to indicate to the caller whether privilege was used to satisfy the
3524 * request (obsoleted). Returns 0 on success, or an errno on failure.
3526 * The ifdef'd CAPABILITIES version is here for reference, but is not
3530 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3531 accmode_t accmode, struct ucred *cred, int *privused)
3533 accmode_t dac_granted;
3534 accmode_t priv_granted;
3536 KASSERT((accmode & ~(VEXEC | VWRITE | VREAD | VADMIN | VAPPEND)) == 0,
3537 ("invalid bit in accmode"));
3540 * Look for a normal, non-privileged way to access the file/directory
3541 * as requested. If it exists, go with that.
3544 if (privused != NULL)
3549 /* Check the owner. */
3550 if (cred->cr_uid == file_uid) {
3551 dac_granted |= VADMIN;
3552 if (file_mode & S_IXUSR)
3553 dac_granted |= VEXEC;
3554 if (file_mode & S_IRUSR)
3555 dac_granted |= VREAD;
3556 if (file_mode & S_IWUSR)
3557 dac_granted |= (VWRITE | VAPPEND);
3559 if ((accmode & dac_granted) == accmode)
3565 /* Otherwise, check the groups (first match) */
3566 if (groupmember(file_gid, cred)) {
3567 if (file_mode & S_IXGRP)
3568 dac_granted |= VEXEC;
3569 if (file_mode & S_IRGRP)
3570 dac_granted |= VREAD;
3571 if (file_mode & S_IWGRP)
3572 dac_granted |= (VWRITE | VAPPEND);
3574 if ((accmode & dac_granted) == accmode)
3580 /* Otherwise, check everyone else. */
3581 if (file_mode & S_IXOTH)
3582 dac_granted |= VEXEC;
3583 if (file_mode & S_IROTH)
3584 dac_granted |= VREAD;
3585 if (file_mode & S_IWOTH)
3586 dac_granted |= (VWRITE | VAPPEND);
3587 if ((accmode & dac_granted) == accmode)
3592 * Build a privilege mask to determine if the set of privileges
3593 * satisfies the requirements when combined with the granted mask
3594 * from above. For each privilege, if the privilege is required,
3595 * bitwise or the request type onto the priv_granted mask.
3601 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3602 * requests, instead of PRIV_VFS_EXEC.
3604 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3605 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3606 priv_granted |= VEXEC;
3608 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3609 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3610 priv_granted |= VEXEC;
3613 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3614 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3615 priv_granted |= VREAD;
3617 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3618 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3619 priv_granted |= (VWRITE | VAPPEND);
3621 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3622 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3623 priv_granted |= VADMIN;
3625 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3626 /* XXX audit: privilege used */
3627 if (privused != NULL)
3632 return ((accmode & VADMIN) ? EPERM : EACCES);
3636 * Credential check based on process requesting service, and per-attribute
3640 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3641 struct thread *td, accmode_t accmode)
3645 * Kernel-invoked always succeeds.
3651 * Do not allow privileged processes in jail to directly manipulate
3652 * system attributes.
3654 switch (attrnamespace) {
3655 case EXTATTR_NAMESPACE_SYSTEM:
3656 /* Potentially should be: return (EPERM); */
3657 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3658 case EXTATTR_NAMESPACE_USER:
3659 return (VOP_ACCESS(vp, accmode, cred, td));
3665 #ifdef DEBUG_VFS_LOCKS
3667 * This only exists to supress warnings from unlocked specfs accesses. It is
3668 * no longer ok to have an unlocked VFS.
3670 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3671 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3673 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3674 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, "");
3676 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3677 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 0, "");
3679 int vfs_badlock_print = 1; /* Print lock violations. */
3680 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 0, "");
3683 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3684 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, &vfs_badlock_backtrace, 0, "");
3688 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3692 if (vfs_badlock_backtrace)
3695 if (vfs_badlock_print)
3696 printf("%s: %p %s\n", str, (void *)vp, msg);
3697 if (vfs_badlock_ddb)
3698 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3702 assert_vi_locked(struct vnode *vp, const char *str)
3705 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3706 vfs_badlock("interlock is not locked but should be", str, vp);
3710 assert_vi_unlocked(struct vnode *vp, const char *str)
3713 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3714 vfs_badlock("interlock is locked but should not be", str, vp);
3718 assert_vop_locked(struct vnode *vp, const char *str)
3721 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0)
3722 vfs_badlock("is not locked but should be", str, vp);
3726 assert_vop_unlocked(struct vnode *vp, const char *str)
3729 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
3730 vfs_badlock("is locked but should not be", str, vp);
3734 assert_vop_elocked(struct vnode *vp, const char *str)
3737 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
3738 vfs_badlock("is not exclusive locked but should be", str, vp);
3743 assert_vop_elocked_other(struct vnode *vp, const char *str)
3746 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
3747 vfs_badlock("is not exclusive locked by another thread",
3752 assert_vop_slocked(struct vnode *vp, const char *str)
3755 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
3756 vfs_badlock("is not locked shared but should be", str, vp);
3759 #endif /* DEBUG_VFS_LOCKS */
3762 vop_rename_fail(struct vop_rename_args *ap)
3765 if (ap->a_tvp != NULL)
3767 if (ap->a_tdvp == ap->a_tvp)
3776 vop_rename_pre(void *ap)
3778 struct vop_rename_args *a = ap;
3780 #ifdef DEBUG_VFS_LOCKS
3782 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3783 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3784 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3785 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3787 /* Check the source (from). */
3788 if (a->a_tdvp->v_vnlock != a->a_fdvp->v_vnlock &&
3789 (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fdvp->v_vnlock))
3790 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3791 if (a->a_tvp == NULL || a->a_tvp->v_vnlock != a->a_fvp->v_vnlock)
3792 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
3794 /* Check the target. */
3796 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3797 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3799 if (a->a_tdvp != a->a_fdvp)
3801 if (a->a_tvp != a->a_fvp)
3809 vop_strategy_pre(void *ap)
3811 #ifdef DEBUG_VFS_LOCKS
3812 struct vop_strategy_args *a;
3819 * Cluster ops lock their component buffers but not the IO container.
3821 if ((bp->b_flags & B_CLUSTER) != 0)
3824 if (!BUF_ISLOCKED(bp)) {
3825 if (vfs_badlock_print)
3827 "VOP_STRATEGY: bp is not locked but should be\n");
3828 if (vfs_badlock_ddb)
3829 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3835 vop_lookup_pre(void *ap)
3837 #ifdef DEBUG_VFS_LOCKS
3838 struct vop_lookup_args *a;
3843 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3844 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3849 vop_lookup_post(void *ap, int rc)
3851 #ifdef DEBUG_VFS_LOCKS
3852 struct vop_lookup_args *a;
3860 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3861 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3864 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3869 vop_lock_pre(void *ap)
3871 #ifdef DEBUG_VFS_LOCKS
3872 struct vop_lock1_args *a = ap;
3874 if ((a->a_flags & LK_INTERLOCK) == 0)
3875 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3877 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3882 vop_lock_post(void *ap, int rc)
3884 #ifdef DEBUG_VFS_LOCKS
3885 struct vop_lock1_args *a = ap;
3887 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3889 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3894 vop_unlock_pre(void *ap)
3896 #ifdef DEBUG_VFS_LOCKS
3897 struct vop_unlock_args *a = ap;
3899 if (a->a_flags & LK_INTERLOCK)
3900 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3901 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3906 vop_unlock_post(void *ap, int rc)
3908 #ifdef DEBUG_VFS_LOCKS
3909 struct vop_unlock_args *a = ap;
3911 if (a->a_flags & LK_INTERLOCK)
3912 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
3917 vop_create_post(void *ap, int rc)
3919 struct vop_create_args *a = ap;
3922 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3926 vop_link_post(void *ap, int rc)
3928 struct vop_link_args *a = ap;
3931 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
3932 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
3937 vop_mkdir_post(void *ap, int rc)
3939 struct vop_mkdir_args *a = ap;
3942 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3946 vop_mknod_post(void *ap, int rc)
3948 struct vop_mknod_args *a = ap;
3951 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3955 vop_remove_post(void *ap, int rc)
3957 struct vop_remove_args *a = ap;
3960 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3961 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3966 vop_rename_post(void *ap, int rc)
3968 struct vop_rename_args *a = ap;
3971 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
3972 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
3973 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
3975 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
3977 if (a->a_tdvp != a->a_fdvp)
3979 if (a->a_tvp != a->a_fvp)
3987 vop_rmdir_post(void *ap, int rc)
3989 struct vop_rmdir_args *a = ap;
3992 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3993 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3998 vop_setattr_post(void *ap, int rc)
4000 struct vop_setattr_args *a = ap;
4003 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
4007 vop_symlink_post(void *ap, int rc)
4009 struct vop_symlink_args *a = ap;
4012 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4015 static struct knlist fs_knlist;
4018 vfs_event_init(void *arg)
4020 knlist_init_mtx(&fs_knlist, NULL);
4022 /* XXX - correct order? */
4023 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4026 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
4029 KNOTE_UNLOCKED(&fs_knlist, event);
4032 static int filt_fsattach(struct knote *kn);
4033 static void filt_fsdetach(struct knote *kn);
4034 static int filt_fsevent(struct knote *kn, long hint);
4036 struct filterops fs_filtops =
4037 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
4040 filt_fsattach(struct knote *kn)
4043 kn->kn_flags |= EV_CLEAR;
4044 knlist_add(&fs_knlist, kn, 0);
4049 filt_fsdetach(struct knote *kn)
4052 knlist_remove(&fs_knlist, kn, 0);
4056 filt_fsevent(struct knote *kn, long hint)
4059 kn->kn_fflags |= hint;
4060 return (kn->kn_fflags != 0);
4064 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4070 error = SYSCTL_IN(req, &vc, sizeof(vc));
4073 if (vc.vc_vers != VFS_CTL_VERS1)
4075 mp = vfs_getvfs(&vc.vc_fsid);
4078 /* ensure that a specific sysctl goes to the right filesystem. */
4079 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4080 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4084 VCTLTOREQ(&vc, req);
4085 error = VFS_SYSCTL(mp, vc.vc_op, req);
4090 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, NULL, 0, sysctl_vfs_ctl, "",
4094 * Function to initialize a va_filerev field sensibly.
4095 * XXX: Wouldn't a random number make a lot more sense ??
4098 init_va_filerev(void)
4103 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4106 static int filt_vfsread(struct knote *kn, long hint);
4107 static int filt_vfswrite(struct knote *kn, long hint);
4108 static int filt_vfsvnode(struct knote *kn, long hint);
4109 static void filt_vfsdetach(struct knote *kn);
4110 static struct filterops vfsread_filtops =
4111 { 1, NULL, filt_vfsdetach, filt_vfsread };
4112 static struct filterops vfswrite_filtops =
4113 { 1, NULL, filt_vfsdetach, filt_vfswrite };
4114 static struct filterops vfsvnode_filtops =
4115 { 1, NULL, filt_vfsdetach, filt_vfsvnode };
4118 vfs_knllock(void *arg)
4120 struct vnode *vp = arg;
4122 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4126 vfs_knlunlock(void *arg)
4128 struct vnode *vp = arg;
4134 vfs_knl_assert_locked(void *arg)
4136 #ifdef DEBUG_VFS_LOCKS
4137 struct vnode *vp = arg;
4139 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4144 vfs_knl_assert_unlocked(void *arg)
4146 #ifdef DEBUG_VFS_LOCKS
4147 struct vnode *vp = arg;
4149 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4154 vfs_kqfilter(struct vop_kqfilter_args *ap)
4156 struct vnode *vp = ap->a_vp;
4157 struct knote *kn = ap->a_kn;
4160 switch (kn->kn_filter) {
4162 kn->kn_fop = &vfsread_filtops;
4165 kn->kn_fop = &vfswrite_filtops;
4168 kn->kn_fop = &vfsvnode_filtops;
4174 kn->kn_hook = (caddr_t)vp;
4177 if (vp->v_pollinfo == NULL)
4179 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4180 knlist_add(knl, kn, 0);
4186 * Detach knote from vnode
4189 filt_vfsdetach(struct knote *kn)
4191 struct vnode *vp = (struct vnode *)kn->kn_hook;
4193 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4194 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4199 filt_vfsread(struct knote *kn, long hint)
4201 struct vnode *vp = (struct vnode *)kn->kn_hook;
4206 * filesystem is gone, so set the EOF flag and schedule
4207 * the knote for deletion.
4209 if (hint == NOTE_REVOKE) {
4211 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4216 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4220 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4221 res = (kn->kn_data != 0);
4228 filt_vfswrite(struct knote *kn, long hint)
4230 struct vnode *vp = (struct vnode *)kn->kn_hook;
4235 * filesystem is gone, so set the EOF flag and schedule
4236 * the knote for deletion.
4238 if (hint == NOTE_REVOKE)
4239 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4247 filt_vfsvnode(struct knote *kn, long hint)
4249 struct vnode *vp = (struct vnode *)kn->kn_hook;
4253 if (kn->kn_sfflags & hint)
4254 kn->kn_fflags |= hint;
4255 if (hint == NOTE_REVOKE) {
4256 kn->kn_flags |= EV_EOF;
4260 res = (kn->kn_fflags != 0);
4266 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4270 if (dp->d_reclen > ap->a_uio->uio_resid)
4271 return (ENAMETOOLONG);
4272 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4274 if (ap->a_ncookies != NULL) {
4275 if (ap->a_cookies != NULL)
4276 free(ap->a_cookies, M_TEMP);
4277 ap->a_cookies = NULL;
4278 *ap->a_ncookies = 0;
4282 if (ap->a_ncookies == NULL)
4285 KASSERT(ap->a_cookies,
4286 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4288 *ap->a_cookies = realloc(*ap->a_cookies,
4289 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4290 (*ap->a_cookies)[*ap->a_ncookies] = off;
4295 * Mark for update the access time of the file if the filesystem
4296 * supports VOP_MARKATIME. This functionality is used by execve and
4297 * mmap, so we want to avoid the I/O implied by directly setting
4298 * va_atime for the sake of efficiency.
4301 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4306 VFS_ASSERT_GIANT(mp);
4307 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4308 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4309 (void)VOP_MARKATIME(vp);
4313 * The purpose of this routine is to remove granularity from accmode_t,
4314 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4315 * VADMIN and VAPPEND.
4317 * If it returns 0, the caller is supposed to continue with the usual
4318 * access checks using 'accmode' as modified by this routine. If it
4319 * returns nonzero value, the caller is supposed to return that value
4322 * Note that after this routine runs, accmode may be zero.
4325 vfs_unixify_accmode(accmode_t *accmode)
4328 * There is no way to specify explicit "deny" rule using
4329 * file mode or POSIX.1e ACLs.
4331 if (*accmode & VEXPLICIT_DENY) {
4337 * None of these can be translated into usual access bits.
4338 * Also, the common case for NFSv4 ACLs is to not contain
4339 * either of these bits. Caller should check for VWRITE
4340 * on the containing directory instead.
4342 if (*accmode & (VDELETE_CHILD | VDELETE))
4345 if (*accmode & VADMIN_PERMS) {
4346 *accmode &= ~VADMIN_PERMS;
4351 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4352 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4354 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);