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);
803 EVENTHANDLER_INVOKE(vfs_lowvnodes, desiredvnodes / 10);
805 /* These messages are temporary debugging aids */
806 if (vnlru_nowhere < 5)
807 printf("vnlru process getting nowhere..\n");
808 else if (vnlru_nowhere == 5)
809 printf("vnlru process messages stopped.\n");
812 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
818 static struct kproc_desc vnlru_kp = {
823 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start,
827 * Routines having to do with the management of the vnode table.
831 vdestroy(struct vnode *vp)
835 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
836 mtx_lock(&vnode_free_list_mtx);
838 mtx_unlock(&vnode_free_list_mtx);
840 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
841 ("cleaned vnode still on the free list."));
842 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
843 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
844 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
845 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
846 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
847 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
848 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
849 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
850 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
851 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
852 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
853 VNASSERT(vp->v_cache_dd == NULL, vp, ("vp has namecache for .."));
856 mac_vnode_destroy(vp);
858 if (vp->v_pollinfo != NULL)
859 destroy_vpollinfo(vp->v_pollinfo);
861 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
864 lockdestroy(vp->v_vnlock);
865 mtx_destroy(&vp->v_interlock);
866 mtx_destroy(BO_MTX(bo));
867 uma_zfree(vnode_zone, vp);
871 * Try to recycle a freed vnode. We abort if anyone picks up a reference
872 * before we actually vgone(). This function must be called with the vnode
873 * held to prevent the vnode from being returned to the free list midway
877 vtryrecycle(struct vnode *vp)
881 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
882 VNASSERT(vp->v_holdcnt, vp,
883 ("vtryrecycle: Recycling vp %p without a reference.", vp));
885 * This vnode may found and locked via some other list, if so we
886 * can't recycle it yet.
888 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
890 "%s: impossible to recycle, vp %p lock is already held",
892 return (EWOULDBLOCK);
895 * Don't recycle if its filesystem is being suspended.
897 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
900 "%s: impossible to recycle, cannot start the write for %p",
905 * If we got this far, we need to acquire the interlock and see if
906 * anyone picked up this vnode from another list. If not, we will
907 * mark it with DOOMED via vgonel() so that anyone who does find it
911 if (vp->v_usecount) {
912 VOP_UNLOCK(vp, LK_INTERLOCK);
913 vn_finished_write(vnmp);
915 "%s: impossible to recycle, %p is already referenced",
919 if ((vp->v_iflag & VI_DOOMED) == 0)
921 VOP_UNLOCK(vp, LK_INTERLOCK);
922 vn_finished_write(vnmp);
927 * Return the next vnode from the free list.
930 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
933 struct vnode *vp = NULL;
936 CTR3(KTR_VFS, "%s: mp %p with tag %s", __func__, mp, tag);
937 mtx_lock(&vnode_free_list_mtx);
939 * Lend our context to reclaim vnodes if they've exceeded the max.
941 if (freevnodes > wantfreevnodes)
944 * Wait for available vnodes.
946 if (numvnodes > desiredvnodes) {
947 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
949 * File system is beeing suspended, we cannot risk a
950 * deadlock here, so allocate new vnode anyway.
952 if (freevnodes > wantfreevnodes)
953 vnlru_free(freevnodes - wantfreevnodes);
956 if (vnlruproc_sig == 0) {
957 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
960 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
962 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
963 if (numvnodes > desiredvnodes) {
964 mtx_unlock(&vnode_free_list_mtx);
971 mtx_unlock(&vnode_free_list_mtx);
972 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
976 vp->v_vnlock = &vp->v_lock;
977 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
979 * By default, don't allow shared locks unless filesystems
982 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
988 mtx_init(BO_MTX(bo), "bufobj interlock", NULL, MTX_DEF);
989 bo->bo_ops = &buf_ops_bio;
991 TAILQ_INIT(&bo->bo_clean.bv_hd);
992 TAILQ_INIT(&bo->bo_dirty.bv_hd);
994 * Initialize namecache.
996 LIST_INIT(&vp->v_cache_src);
997 TAILQ_INIT(&vp->v_cache_dst);
999 * Finalize various vnode identity bits.
1004 v_incr_usecount(vp);
1008 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
1009 mac_vnode_associate_singlelabel(mp, vp);
1010 else if (mp == NULL && vops != &dead_vnodeops)
1011 printf("NULL mp in getnewvnode()\n");
1014 bo->bo_bsize = mp->mnt_stat.f_iosize;
1015 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
1016 vp->v_vflag |= VV_NOKNOTE;
1024 * Delete from old mount point vnode list, if on one.
1027 delmntque(struct vnode *vp)
1036 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
1037 ("bad mount point vnode list size"));
1038 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1039 mp->mnt_nvnodelistsize--;
1045 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1049 vp->v_op = &dead_vnodeops;
1050 /* XXX non mp-safe fs may still call insmntque with vnode
1052 if (!VOP_ISLOCKED(vp))
1053 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1059 * Insert into list of vnodes for the new mount point, if available.
1062 insmntque1(struct vnode *vp, struct mount *mp,
1063 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1067 KASSERT(vp->v_mount == NULL,
1068 ("insmntque: vnode already on per mount vnode list"));
1069 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1070 #ifdef DEBUG_VFS_LOCKS
1071 if (!VFS_NEEDSGIANT(mp))
1072 ASSERT_VOP_ELOCKED(vp,
1073 "insmntque: mp-safe fs and non-locked vp");
1076 if ((mp->mnt_kern_flag & MNTK_NOINSMNTQ) != 0 &&
1077 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0 ||
1078 mp->mnt_nvnodelistsize == 0)) {
1079 locked = VOP_ISLOCKED(vp);
1080 if (!locked || (locked == LK_EXCLUSIVE &&
1081 (vp->v_vflag & VV_FORCEINSMQ) == 0)) {
1090 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1091 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1092 ("neg mount point vnode list size"));
1093 mp->mnt_nvnodelistsize++;
1099 insmntque(struct vnode *vp, struct mount *mp)
1102 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1106 * Flush out and invalidate all buffers associated with a bufobj
1107 * Called with the underlying object locked.
1110 bufobj_invalbuf(struct bufobj *bo, int flags, int slpflag, int slptimeo)
1115 if (flags & V_SAVE) {
1116 error = bufobj_wwait(bo, slpflag, slptimeo);
1121 if (bo->bo_dirty.bv_cnt > 0) {
1123 if ((error = BO_SYNC(bo, MNT_WAIT)) != 0)
1126 * XXX We could save a lock/unlock if this was only
1127 * enabled under INVARIANTS
1130 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1131 panic("vinvalbuf: dirty bufs");
1135 * If you alter this loop please notice that interlock is dropped and
1136 * reacquired in flushbuflist. Special care is needed to ensure that
1137 * no race conditions occur from this.
1140 error = flushbuflist(&bo->bo_clean,
1141 flags, bo, slpflag, slptimeo);
1143 error = flushbuflist(&bo->bo_dirty,
1144 flags, bo, slpflag, slptimeo);
1145 if (error != 0 && error != EAGAIN) {
1149 } while (error != 0);
1152 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1153 * have write I/O in-progress but if there is a VM object then the
1154 * VM object can also have read-I/O in-progress.
1157 bufobj_wwait(bo, 0, 0);
1159 if (bo->bo_object != NULL) {
1160 VM_OBJECT_LOCK(bo->bo_object);
1161 vm_object_pip_wait(bo->bo_object, "bovlbx");
1162 VM_OBJECT_UNLOCK(bo->bo_object);
1165 } while (bo->bo_numoutput > 0);
1169 * Destroy the copy in the VM cache, too.
1171 if (bo->bo_object != NULL && (flags & (V_ALT | V_NORMAL)) == 0) {
1172 VM_OBJECT_LOCK(bo->bo_object);
1173 vm_object_page_remove(bo->bo_object, 0, 0,
1174 (flags & V_SAVE) ? TRUE : FALSE);
1175 VM_OBJECT_UNLOCK(bo->bo_object);
1180 if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1181 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1182 panic("vinvalbuf: flush failed");
1189 * Flush out and invalidate all buffers associated with a vnode.
1190 * Called with the underlying object locked.
1193 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
1196 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
1197 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1198 return (bufobj_invalbuf(&vp->v_bufobj, flags, slpflag, slptimeo));
1202 * Flush out buffers on the specified list.
1206 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1209 struct buf *bp, *nbp;
1214 ASSERT_BO_LOCKED(bo);
1217 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1218 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1219 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1225 lblkno = nbp->b_lblkno;
1226 xflags = nbp->b_xflags &
1227 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1230 error = BUF_TIMELOCK(bp,
1231 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1232 "flushbuf", slpflag, slptimeo);
1235 return (error != ENOLCK ? error : EAGAIN);
1237 KASSERT(bp->b_bufobj == bo,
1238 ("bp %p wrong b_bufobj %p should be %p",
1239 bp, bp->b_bufobj, bo));
1240 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1246 * XXX Since there are no node locks for NFS, I
1247 * believe there is a slight chance that a delayed
1248 * write will occur while sleeping just above, so
1251 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1254 bp->b_flags |= B_ASYNC;
1257 return (EAGAIN); /* XXX: why not loop ? */
1260 bp->b_flags |= (B_INVAL | B_RELBUF);
1261 bp->b_flags &= ~B_ASYNC;
1265 (nbp->b_bufobj != bo ||
1266 nbp->b_lblkno != lblkno ||
1268 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1269 break; /* nbp invalid */
1275 * Truncate a file's buffer and pages to a specified length. This
1276 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1280 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1281 off_t length, int blksize)
1283 struct buf *bp, *nbp;
1288 CTR5(KTR_VFS, "%s: vp %p with cred %p and block %d:%ju", __func__,
1289 vp, cred, blksize, (uintmax_t)length);
1292 * Round up to the *next* lbn.
1294 trunclbn = (length + blksize - 1) / blksize;
1296 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1303 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1304 if (bp->b_lblkno < trunclbn)
1307 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1308 BO_MTX(bo)) == ENOLCK)
1312 bp->b_flags |= (B_INVAL | B_RELBUF);
1313 bp->b_flags &= ~B_ASYNC;
1318 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1319 (nbp->b_vp != vp) ||
1320 (nbp->b_flags & B_DELWRI))) {
1326 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1327 if (bp->b_lblkno < trunclbn)
1330 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1331 BO_MTX(bo)) == ENOLCK)
1334 bp->b_flags |= (B_INVAL | B_RELBUF);
1335 bp->b_flags &= ~B_ASYNC;
1339 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1340 (nbp->b_vp != vp) ||
1341 (nbp->b_flags & B_DELWRI) == 0)) {
1350 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1351 if (bp->b_lblkno > 0)
1354 * Since we hold the vnode lock this should only
1355 * fail if we're racing with the buf daemon.
1358 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1359 BO_MTX(bo)) == ENOLCK) {
1362 VNASSERT((bp->b_flags & B_DELWRI), vp,
1363 ("buf(%p) on dirty queue without DELWRI", bp));
1372 bufobj_wwait(bo, 0, 0);
1374 vnode_pager_setsize(vp, length);
1380 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1383 * NOTE: We have to deal with the special case of a background bitmap
1384 * buffer, a situation where two buffers will have the same logical
1385 * block offset. We want (1) only the foreground buffer to be accessed
1386 * in a lookup and (2) must differentiate between the foreground and
1387 * background buffer in the splay tree algorithm because the splay
1388 * tree cannot normally handle multiple entities with the same 'index'.
1389 * We accomplish this by adding differentiating flags to the splay tree's
1394 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1397 struct buf *lefttreemax, *righttreemin, *y;
1401 lefttreemax = righttreemin = &dummy;
1403 if (lblkno < root->b_lblkno ||
1404 (lblkno == root->b_lblkno &&
1405 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1406 if ((y = root->b_left) == NULL)
1408 if (lblkno < y->b_lblkno) {
1410 root->b_left = y->b_right;
1413 if ((y = root->b_left) == NULL)
1416 /* Link into the new root's right tree. */
1417 righttreemin->b_left = root;
1418 righttreemin = root;
1419 } else if (lblkno > root->b_lblkno ||
1420 (lblkno == root->b_lblkno &&
1421 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1422 if ((y = root->b_right) == NULL)
1424 if (lblkno > y->b_lblkno) {
1426 root->b_right = y->b_left;
1429 if ((y = root->b_right) == NULL)
1432 /* Link into the new root's left tree. */
1433 lefttreemax->b_right = root;
1440 /* Assemble the new root. */
1441 lefttreemax->b_right = root->b_left;
1442 righttreemin->b_left = root->b_right;
1443 root->b_left = dummy.b_right;
1444 root->b_right = dummy.b_left;
1449 buf_vlist_remove(struct buf *bp)
1454 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1455 ASSERT_BO_LOCKED(bp->b_bufobj);
1456 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1457 (BX_VNDIRTY|BX_VNCLEAN),
1458 ("buf_vlist_remove: Buf %p is on two lists", bp));
1459 if (bp->b_xflags & BX_VNDIRTY)
1460 bv = &bp->b_bufobj->bo_dirty;
1462 bv = &bp->b_bufobj->bo_clean;
1463 if (bp != bv->bv_root) {
1464 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1465 KASSERT(root == bp, ("splay lookup failed in remove"));
1467 if (bp->b_left == NULL) {
1470 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1471 root->b_right = bp->b_right;
1474 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1476 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1480 * Add the buffer to the sorted clean or dirty block list using a
1481 * splay tree algorithm.
1483 * NOTE: xflags is passed as a constant, optimizing this inline function!
1486 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1491 ASSERT_BO_LOCKED(bo);
1492 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1493 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1494 bp->b_xflags |= xflags;
1495 if (xflags & BX_VNDIRTY)
1500 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1504 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1505 } else if (bp->b_lblkno < root->b_lblkno ||
1506 (bp->b_lblkno == root->b_lblkno &&
1507 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1508 bp->b_left = root->b_left;
1510 root->b_left = NULL;
1511 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1513 bp->b_right = root->b_right;
1515 root->b_right = NULL;
1516 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1523 * Lookup a buffer using the splay tree. Note that we specifically avoid
1524 * shadow buffers used in background bitmap writes.
1526 * This code isn't quite efficient as it could be because we are maintaining
1527 * two sorted lists and do not know which list the block resides in.
1529 * During a "make buildworld" the desired buffer is found at one of
1530 * the roots more than 60% of the time. Thus, checking both roots
1531 * before performing either splay eliminates unnecessary splays on the
1532 * first tree splayed.
1535 gbincore(struct bufobj *bo, daddr_t lblkno)
1539 ASSERT_BO_LOCKED(bo);
1540 if ((bp = bo->bo_clean.bv_root) != NULL &&
1541 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1543 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1544 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1546 if ((bp = bo->bo_clean.bv_root) != NULL) {
1547 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1548 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1551 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1552 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1553 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1560 * Associate a buffer with a vnode.
1563 bgetvp(struct vnode *vp, struct buf *bp)
1568 ASSERT_BO_LOCKED(bo);
1569 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1571 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1572 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1573 ("bgetvp: bp already attached! %p", bp));
1576 if (VFS_NEEDSGIANT(vp->v_mount) || bo->bo_flag & BO_NEEDSGIANT)
1577 bp->b_flags |= B_NEEDSGIANT;
1581 * Insert onto list for new vnode.
1583 buf_vlist_add(bp, bo, BX_VNCLEAN);
1587 * Disassociate a buffer from a vnode.
1590 brelvp(struct buf *bp)
1595 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1596 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1599 * Delete from old vnode list, if on one.
1601 vp = bp->b_vp; /* XXX */
1604 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1605 buf_vlist_remove(bp);
1607 panic("brelvp: Buffer %p not on queue.", bp);
1608 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1609 bo->bo_flag &= ~BO_ONWORKLST;
1610 mtx_lock(&sync_mtx);
1611 LIST_REMOVE(bo, bo_synclist);
1612 syncer_worklist_len--;
1613 mtx_unlock(&sync_mtx);
1615 bp->b_flags &= ~B_NEEDSGIANT;
1617 bp->b_bufobj = NULL;
1623 * Add an item to the syncer work queue.
1626 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1630 ASSERT_BO_LOCKED(bo);
1632 mtx_lock(&sync_mtx);
1633 if (bo->bo_flag & BO_ONWORKLST)
1634 LIST_REMOVE(bo, bo_synclist);
1636 bo->bo_flag |= BO_ONWORKLST;
1637 syncer_worklist_len++;
1640 if (delay > syncer_maxdelay - 2)
1641 delay = syncer_maxdelay - 2;
1642 slot = (syncer_delayno + delay) & syncer_mask;
1644 queue = VFS_NEEDSGIANT(bo->__bo_vnode->v_mount) ? WI_GIANTQ :
1646 LIST_INSERT_HEAD(&syncer_workitem_pending[queue][slot], bo,
1648 mtx_unlock(&sync_mtx);
1652 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1656 mtx_lock(&sync_mtx);
1657 len = syncer_worklist_len - sync_vnode_count;
1658 mtx_unlock(&sync_mtx);
1659 error = SYSCTL_OUT(req, &len, sizeof(len));
1663 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1664 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1666 static struct proc *updateproc;
1667 static void sched_sync(void);
1668 static struct kproc_desc up_kp = {
1673 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp);
1676 sync_vnode(struct synclist *slp, struct bufobj **bo, struct thread *td)
1681 *bo = LIST_FIRST(slp);
1684 vp = (*bo)->__bo_vnode; /* XXX */
1685 if (VOP_ISLOCKED(vp) != 0 || VI_TRYLOCK(vp) == 0)
1688 * We use vhold in case the vnode does not
1689 * successfully sync. vhold prevents the vnode from
1690 * going away when we unlock the sync_mtx so that
1691 * we can acquire the vnode interlock.
1694 mtx_unlock(&sync_mtx);
1696 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1698 mtx_lock(&sync_mtx);
1699 return (*bo == LIST_FIRST(slp));
1701 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1702 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1704 vn_finished_write(mp);
1706 if (((*bo)->bo_flag & BO_ONWORKLST) != 0) {
1708 * Put us back on the worklist. The worklist
1709 * routine will remove us from our current
1710 * position and then add us back in at a later
1713 vn_syncer_add_to_worklist(*bo, syncdelay);
1717 mtx_lock(&sync_mtx);
1722 * System filesystem synchronizer daemon.
1727 struct synclist *gnext, *next;
1728 struct synclist *gslp, *slp;
1731 struct thread *td = curthread;
1733 int net_worklist_len;
1734 int syncer_final_iter;
1739 syncer_final_iter = 0;
1741 syncer_state = SYNCER_RUNNING;
1742 starttime = time_uptime;
1743 td->td_pflags |= TDP_NORUNNINGBUF;
1745 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1748 mtx_lock(&sync_mtx);
1750 if (syncer_state == SYNCER_FINAL_DELAY &&
1751 syncer_final_iter == 0) {
1752 mtx_unlock(&sync_mtx);
1753 kproc_suspend_check(td->td_proc);
1754 mtx_lock(&sync_mtx);
1756 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1757 if (syncer_state != SYNCER_RUNNING &&
1758 starttime != time_uptime) {
1760 printf("\nSyncing disks, vnodes remaining...");
1763 printf("%d ", net_worklist_len);
1765 starttime = time_uptime;
1768 * Push files whose dirty time has expired. Be careful
1769 * of interrupt race on slp queue.
1771 * Skip over empty worklist slots when shutting down.
1774 slp = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1775 gslp = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1776 syncer_delayno += 1;
1777 if (syncer_delayno == syncer_maxdelay)
1779 next = &syncer_workitem_pending[WI_MPSAFEQ][syncer_delayno];
1780 gnext = &syncer_workitem_pending[WI_GIANTQ][syncer_delayno];
1782 * If the worklist has wrapped since the
1783 * it was emptied of all but syncer vnodes,
1784 * switch to the FINAL_DELAY state and run
1785 * for one more second.
1787 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1788 net_worklist_len == 0 &&
1789 last_work_seen == syncer_delayno) {
1790 syncer_state = SYNCER_FINAL_DELAY;
1791 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1793 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1794 LIST_EMPTY(gslp) && syncer_worklist_len > 0);
1797 * Keep track of the last time there was anything
1798 * on the worklist other than syncer vnodes.
1799 * Return to the SHUTTING_DOWN state if any
1802 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1803 last_work_seen = syncer_delayno;
1804 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1805 syncer_state = SYNCER_SHUTTING_DOWN;
1806 while (!LIST_EMPTY(slp)) {
1807 error = sync_vnode(slp, &bo, td);
1809 LIST_REMOVE(bo, bo_synclist);
1810 LIST_INSERT_HEAD(next, bo, bo_synclist);
1814 if (!LIST_EMPTY(gslp)) {
1815 mtx_unlock(&sync_mtx);
1817 mtx_lock(&sync_mtx);
1818 while (!LIST_EMPTY(gslp)) {
1819 error = sync_vnode(gslp, &bo, td);
1821 LIST_REMOVE(bo, bo_synclist);
1822 LIST_INSERT_HEAD(gnext, bo,
1829 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1830 syncer_final_iter--;
1832 * The variable rushjob allows the kernel to speed up the
1833 * processing of the filesystem syncer process. A rushjob
1834 * value of N tells the filesystem syncer to process the next
1835 * N seconds worth of work on its queue ASAP. Currently rushjob
1836 * is used by the soft update code to speed up the filesystem
1837 * syncer process when the incore state is getting so far
1838 * ahead of the disk that the kernel memory pool is being
1839 * threatened with exhaustion.
1846 * Just sleep for a short period of time between
1847 * iterations when shutting down to allow some I/O
1850 * If it has taken us less than a second to process the
1851 * current work, then wait. Otherwise start right over
1852 * again. We can still lose time if any single round
1853 * takes more than two seconds, but it does not really
1854 * matter as we are just trying to generally pace the
1855 * filesystem activity.
1857 if (syncer_state != SYNCER_RUNNING)
1858 cv_timedwait(&sync_wakeup, &sync_mtx,
1859 hz / SYNCER_SHUTDOWN_SPEEDUP);
1860 else if (time_uptime == starttime)
1861 cv_timedwait(&sync_wakeup, &sync_mtx, hz);
1866 * Request the syncer daemon to speed up its work.
1867 * We never push it to speed up more than half of its
1868 * normal turn time, otherwise it could take over the cpu.
1871 speedup_syncer(void)
1875 mtx_lock(&sync_mtx);
1876 if (rushjob < syncdelay / 2) {
1878 stat_rush_requests += 1;
1881 mtx_unlock(&sync_mtx);
1882 cv_broadcast(&sync_wakeup);
1887 * Tell the syncer to speed up its work and run though its work
1888 * list several times, then tell it to shut down.
1891 syncer_shutdown(void *arg, int howto)
1894 if (howto & RB_NOSYNC)
1896 mtx_lock(&sync_mtx);
1897 syncer_state = SYNCER_SHUTTING_DOWN;
1899 mtx_unlock(&sync_mtx);
1900 cv_broadcast(&sync_wakeup);
1901 kproc_shutdown(arg, howto);
1905 * Reassign a buffer from one vnode to another.
1906 * Used to assign file specific control information
1907 * (indirect blocks) to the vnode to which they belong.
1910 reassignbuf(struct buf *bp)
1923 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
1924 bp, bp->b_vp, bp->b_flags);
1926 * B_PAGING flagged buffers cannot be reassigned because their vp
1927 * is not fully linked in.
1929 if (bp->b_flags & B_PAGING)
1930 panic("cannot reassign paging buffer");
1933 * Delete from old vnode list, if on one.
1936 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1937 buf_vlist_remove(bp);
1939 panic("reassignbuf: Buffer %p not on queue.", bp);
1941 * If dirty, put on list of dirty buffers; otherwise insert onto list
1944 if (bp->b_flags & B_DELWRI) {
1945 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
1946 switch (vp->v_type) {
1956 vn_syncer_add_to_worklist(bo, delay);
1958 buf_vlist_add(bp, bo, BX_VNDIRTY);
1960 buf_vlist_add(bp, bo, BX_VNCLEAN);
1962 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1963 mtx_lock(&sync_mtx);
1964 LIST_REMOVE(bo, bo_synclist);
1965 syncer_worklist_len--;
1966 mtx_unlock(&sync_mtx);
1967 bo->bo_flag &= ~BO_ONWORKLST;
1972 bp = TAILQ_FIRST(&bv->bv_hd);
1973 KASSERT(bp == NULL || bp->b_bufobj == bo,
1974 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1975 bp = TAILQ_LAST(&bv->bv_hd, buflists);
1976 KASSERT(bp == NULL || bp->b_bufobj == bo,
1977 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1979 bp = TAILQ_FIRST(&bv->bv_hd);
1980 KASSERT(bp == NULL || bp->b_bufobj == bo,
1981 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1982 bp = TAILQ_LAST(&bv->bv_hd, buflists);
1983 KASSERT(bp == NULL || bp->b_bufobj == bo,
1984 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1990 * Increment the use and hold counts on the vnode, taking care to reference
1991 * the driver's usecount if this is a chardev. The vholdl() will remove
1992 * the vnode from the free list if it is presently free. Requires the
1993 * vnode interlock and returns with it held.
1996 v_incr_usecount(struct vnode *vp)
1999 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2001 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2003 vp->v_rdev->si_usecount++;
2010 * Turn a holdcnt into a use+holdcnt such that only one call to
2011 * v_decr_usecount is needed.
2014 v_upgrade_usecount(struct vnode *vp)
2017 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2019 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2021 vp->v_rdev->si_usecount++;
2027 * Decrement the vnode use and hold count along with the driver's usecount
2028 * if this is a chardev. The vdropl() below releases the vnode interlock
2029 * as it may free the vnode.
2032 v_decr_usecount(struct vnode *vp)
2035 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2036 VNASSERT(vp->v_usecount > 0, vp,
2037 ("v_decr_usecount: negative usecount"));
2038 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2040 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2042 vp->v_rdev->si_usecount--;
2049 * Decrement only the use count and driver use count. This is intended to
2050 * be paired with a follow on vdropl() to release the remaining hold count.
2051 * In this way we may vgone() a vnode with a 0 usecount without risk of
2052 * having it end up on a free list because the hold count is kept above 0.
2055 v_decr_useonly(struct vnode *vp)
2058 ASSERT_VI_LOCKED(vp, __FUNCTION__);
2059 VNASSERT(vp->v_usecount > 0, vp,
2060 ("v_decr_useonly: negative usecount"));
2061 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2063 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
2065 vp->v_rdev->si_usecount--;
2071 * Grab a particular vnode from the free list, increment its
2072 * reference count and lock it. VI_DOOMED is set if the vnode
2073 * is being destroyed. Only callers who specify LK_RETRY will
2074 * see doomed vnodes. If inactive processing was delayed in
2075 * vput try to do it here.
2078 vget(struct vnode *vp, int flags, struct thread *td)
2083 VFS_ASSERT_GIANT(vp->v_mount);
2084 VNASSERT((flags & LK_TYPE_MASK) != 0, vp,
2085 ("vget: invalid lock operation"));
2086 CTR3(KTR_VFS, "%s: vp %p with flags %d", __func__, vp, flags);
2088 if ((flags & LK_INTERLOCK) == 0)
2091 if ((error = vn_lock(vp, flags | LK_INTERLOCK)) != 0) {
2093 CTR2(KTR_VFS, "%s: impossible to lock vnode %p", __func__,
2097 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2098 panic("vget: vn_lock failed to return ENOENT\n");
2100 /* Upgrade our holdcnt to a usecount. */
2101 v_upgrade_usecount(vp);
2103 * We don't guarantee that any particular close will
2104 * trigger inactive processing so just make a best effort
2105 * here at preventing a reference to a removed file. If
2106 * we don't succeed no harm is done.
2108 if (vp->v_iflag & VI_OWEINACT) {
2109 if (VOP_ISLOCKED(vp) == LK_EXCLUSIVE &&
2110 (flags & LK_NOWAIT) == 0)
2112 vp->v_iflag &= ~VI_OWEINACT;
2119 * Increase the reference count of a vnode.
2122 vref(struct vnode *vp)
2125 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2127 v_incr_usecount(vp);
2132 * Return reference count of a vnode.
2134 * The results of this call are only guaranteed when some mechanism other
2135 * than the VI lock is used to stop other processes from gaining references
2136 * to the vnode. This may be the case if the caller holds the only reference.
2137 * This is also useful when stale data is acceptable as race conditions may
2138 * be accounted for by some other means.
2141 vrefcnt(struct vnode *vp)
2146 usecnt = vp->v_usecount;
2154 * Vnode put/release.
2155 * If count drops to zero, call inactive routine and return to freelist.
2158 vrele(struct vnode *vp)
2160 struct thread *td = curthread; /* XXX */
2162 KASSERT(vp != NULL, ("vrele: null vp"));
2163 VFS_ASSERT_GIANT(vp->v_mount);
2167 /* Skip this v_writecount check if we're going to panic below. */
2168 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2169 ("vrele: missed vn_close"));
2170 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2172 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2173 vp->v_usecount == 1)) {
2174 v_decr_usecount(vp);
2177 if (vp->v_usecount != 1) {
2179 vprint("vrele: negative ref count", vp);
2182 panic("vrele: negative ref cnt");
2184 CTR2(KTR_VFS, "%s: return vnode %p to the freelist", __func__, vp);
2186 * We want to hold the vnode until the inactive finishes to
2187 * prevent vgone() races. We drop the use count here and the
2188 * hold count below when we're done.
2192 * We must call VOP_INACTIVE with the node locked. Mark
2193 * as VI_DOINGINACT to avoid recursion.
2195 vp->v_iflag |= VI_OWEINACT;
2196 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK) == 0) {
2198 if (vp->v_usecount > 0)
2199 vp->v_iflag &= ~VI_OWEINACT;
2200 if (vp->v_iflag & VI_OWEINACT)
2205 if (vp->v_usecount > 0)
2206 vp->v_iflag &= ~VI_OWEINACT;
2212 * Release an already locked vnode. This give the same effects as
2213 * unlock+vrele(), but takes less time and avoids releasing and
2214 * re-aquiring the lock (as vrele() acquires the lock internally.)
2217 vput(struct vnode *vp)
2219 struct thread *td = curthread; /* XXX */
2222 KASSERT(vp != NULL, ("vput: null vp"));
2223 ASSERT_VOP_LOCKED(vp, "vput");
2224 VFS_ASSERT_GIANT(vp->v_mount);
2225 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2227 /* Skip this v_writecount check if we're going to panic below. */
2228 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2229 ("vput: missed vn_close"));
2232 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2233 vp->v_usecount == 1)) {
2235 v_decr_usecount(vp);
2239 if (vp->v_usecount != 1) {
2241 vprint("vput: negative ref count", vp);
2243 panic("vput: negative ref cnt");
2245 CTR2(KTR_VFS, "%s: return to freelist the vnode %p", __func__, vp);
2247 * We want to hold the vnode until the inactive finishes to
2248 * prevent vgone() races. We drop the use count here and the
2249 * hold count below when we're done.
2252 vp->v_iflag |= VI_OWEINACT;
2253 if (VOP_ISLOCKED(vp) != LK_EXCLUSIVE) {
2254 error = VOP_LOCK(vp, LK_UPGRADE|LK_INTERLOCK|LK_NOWAIT);
2257 if (vp->v_usecount > 0)
2258 vp->v_iflag &= ~VI_OWEINACT;
2262 if (vp->v_usecount > 0)
2263 vp->v_iflag &= ~VI_OWEINACT;
2264 if (vp->v_iflag & VI_OWEINACT)
2272 * Somebody doesn't want the vnode recycled.
2275 vhold(struct vnode *vp)
2284 vholdl(struct vnode *vp)
2287 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2289 if (VSHOULDBUSY(vp))
2294 * Note that there is one less who cares about this vnode. vdrop() is the
2295 * opposite of vhold().
2298 vdrop(struct vnode *vp)
2306 * Drop the hold count of the vnode. If this is the last reference to
2307 * the vnode we will free it if it has been vgone'd otherwise it is
2308 * placed on the free list.
2311 vdropl(struct vnode *vp)
2314 ASSERT_VI_LOCKED(vp, "vdropl");
2315 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2316 if (vp->v_holdcnt <= 0)
2317 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2319 if (vp->v_holdcnt == 0) {
2320 if (vp->v_iflag & VI_DOOMED) {
2321 CTR2(KTR_VFS, "%s: destroying the vnode %p", __func__,
2332 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2333 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2334 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2335 * failed lock upgrade.
2338 vinactive(struct vnode *vp, struct thread *td)
2341 ASSERT_VOP_ELOCKED(vp, "vinactive");
2342 ASSERT_VI_LOCKED(vp, "vinactive");
2343 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2344 ("vinactive: recursed on VI_DOINGINACT"));
2345 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2346 vp->v_iflag |= VI_DOINGINACT;
2347 vp->v_iflag &= ~VI_OWEINACT;
2349 VOP_INACTIVE(vp, td);
2351 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2352 ("vinactive: lost VI_DOINGINACT"));
2353 vp->v_iflag &= ~VI_DOINGINACT;
2357 * Remove any vnodes in the vnode table belonging to mount point mp.
2359 * If FORCECLOSE is not specified, there should not be any active ones,
2360 * return error if any are found (nb: this is a user error, not a
2361 * system error). If FORCECLOSE is specified, detach any active vnodes
2364 * If WRITECLOSE is set, only flush out regular file vnodes open for
2367 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2369 * `rootrefs' specifies the base reference count for the root vnode
2370 * of this filesystem. The root vnode is considered busy if its
2371 * v_usecount exceeds this value. On a successful return, vflush(, td)
2372 * will call vrele() on the root vnode exactly rootrefs times.
2373 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2377 static int busyprt = 0; /* print out busy vnodes */
2378 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
2382 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td)
2384 struct vnode *vp, *mvp, *rootvp = NULL;
2386 int busy = 0, error;
2388 CTR4(KTR_VFS, "%s: mp %p with rootrefs %d and flags %d", __func__, mp,
2391 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2392 ("vflush: bad args"));
2394 * Get the filesystem root vnode. We can vput() it
2395 * immediately, since with rootrefs > 0, it won't go away.
2397 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp)) != 0) {
2398 CTR2(KTR_VFS, "%s: vfs_root lookup failed with %d",
2407 MNT_VNODE_FOREACH(vp, mp, mvp) {
2412 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE);
2416 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2420 * Skip over a vnodes marked VV_SYSTEM.
2422 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2429 * If WRITECLOSE is set, flush out unlinked but still open
2430 * files (even if open only for reading) and regular file
2431 * vnodes open for writing.
2433 if (flags & WRITECLOSE) {
2434 error = VOP_GETATTR(vp, &vattr, td->td_ucred);
2437 if ((vp->v_type == VNON ||
2438 (error == 0 && vattr.va_nlink > 0)) &&
2439 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2448 * With v_usecount == 0, all we need to do is clear out the
2449 * vnode data structures and we are done.
2451 * If FORCECLOSE is set, forcibly close the vnode.
2453 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2454 VNASSERT(vp->v_usecount == 0 ||
2455 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2456 ("device VNODE %p is FORCECLOSED", vp));
2462 vprint("vflush: busy vnode", vp);
2470 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2472 * If just the root vnode is busy, and if its refcount
2473 * is equal to `rootrefs', then go ahead and kill it.
2476 KASSERT(busy > 0, ("vflush: not busy"));
2477 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2478 ("vflush: usecount %d < rootrefs %d",
2479 rootvp->v_usecount, rootrefs));
2480 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2481 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK);
2483 VOP_UNLOCK(rootvp, 0);
2489 CTR2(KTR_VFS, "%s: failing as %d vnodes are busy", __func__,
2493 for (; rootrefs > 0; rootrefs--)
2499 * Recycle an unused vnode to the front of the free list.
2502 vrecycle(struct vnode *vp, struct thread *td)
2506 ASSERT_VOP_ELOCKED(vp, "vrecycle");
2507 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2510 if (vp->v_usecount == 0) {
2519 * Eliminate all activity associated with a vnode
2520 * in preparation for reuse.
2523 vgone(struct vnode *vp)
2531 * vgone, with the vp interlock held.
2534 vgonel(struct vnode *vp)
2541 ASSERT_VOP_ELOCKED(vp, "vgonel");
2542 ASSERT_VI_LOCKED(vp, "vgonel");
2543 VNASSERT(vp->v_holdcnt, vp,
2544 ("vgonel: vp %p has no reference.", vp));
2545 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
2549 * Don't vgonel if we're already doomed.
2551 if (vp->v_iflag & VI_DOOMED)
2553 vp->v_iflag |= VI_DOOMED;
2555 * Check to see if the vnode is in use. If so, we have to call
2556 * VOP_CLOSE() and VOP_INACTIVE().
2558 active = vp->v_usecount;
2559 oweinact = (vp->v_iflag & VI_OWEINACT);
2562 * Clean out any buffers associated with the vnode.
2563 * If the flush fails, just toss the buffers.
2566 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2567 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2568 if (vinvalbuf(vp, V_SAVE, 0, 0) != 0)
2569 vinvalbuf(vp, 0, 0, 0);
2572 * If purging an active vnode, it must be closed and
2573 * deactivated before being reclaimed.
2576 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2577 if (oweinact || active) {
2579 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2584 * Reclaim the vnode.
2586 if (VOP_RECLAIM(vp, td))
2587 panic("vgone: cannot reclaim");
2589 vn_finished_secondary_write(mp);
2590 VNASSERT(vp->v_object == NULL, vp,
2591 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2593 * Clear the advisory locks and wake up waiting threads.
2595 lf_purgelocks(vp, &(vp->v_lockf));
2597 * Delete from old mount point vnode list.
2602 * Done with purge, reset to the standard lock and invalidate
2606 vp->v_vnlock = &vp->v_lock;
2607 vp->v_op = &dead_vnodeops;
2613 * Calculate the total number of references to a special device.
2616 vcount(struct vnode *vp)
2621 count = vp->v_rdev->si_usecount;
2627 * Same as above, but using the struct cdev *as argument
2630 count_dev(struct cdev *dev)
2635 count = dev->si_usecount;
2641 * Print out a description of a vnode.
2643 static char *typename[] =
2644 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2648 vn_printf(struct vnode *vp, const char *fmt, ...)
2651 char buf[256], buf2[16];
2657 printf("%p: ", (void *)vp);
2658 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2659 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2660 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2663 if (vp->v_vflag & VV_ROOT)
2664 strlcat(buf, "|VV_ROOT", sizeof(buf));
2665 if (vp->v_vflag & VV_ISTTY)
2666 strlcat(buf, "|VV_ISTTY", sizeof(buf));
2667 if (vp->v_vflag & VV_NOSYNC)
2668 strlcat(buf, "|VV_NOSYNC", sizeof(buf));
2669 if (vp->v_vflag & VV_CACHEDLABEL)
2670 strlcat(buf, "|VV_CACHEDLABEL", sizeof(buf));
2671 if (vp->v_vflag & VV_TEXT)
2672 strlcat(buf, "|VV_TEXT", sizeof(buf));
2673 if (vp->v_vflag & VV_COPYONWRITE)
2674 strlcat(buf, "|VV_COPYONWRITE", sizeof(buf));
2675 if (vp->v_vflag & VV_SYSTEM)
2676 strlcat(buf, "|VV_SYSTEM", sizeof(buf));
2677 if (vp->v_vflag & VV_PROCDEP)
2678 strlcat(buf, "|VV_PROCDEP", sizeof(buf));
2679 if (vp->v_vflag & VV_NOKNOTE)
2680 strlcat(buf, "|VV_NOKNOTE", sizeof(buf));
2681 if (vp->v_vflag & VV_DELETED)
2682 strlcat(buf, "|VV_DELETED", sizeof(buf));
2683 if (vp->v_vflag & VV_MD)
2684 strlcat(buf, "|VV_MD", sizeof(buf));
2685 flags = vp->v_vflag & ~(VV_ROOT | VV_ISTTY | VV_NOSYNC |
2686 VV_CACHEDLABEL | VV_TEXT | VV_COPYONWRITE | VV_SYSTEM | VV_PROCDEP |
2687 VV_NOKNOTE | VV_DELETED | VV_MD);
2689 snprintf(buf2, sizeof(buf2), "|VV(0x%lx)", flags);
2690 strlcat(buf, buf2, sizeof(buf));
2692 if (vp->v_iflag & VI_MOUNT)
2693 strlcat(buf, "|VI_MOUNT", sizeof(buf));
2694 if (vp->v_iflag & VI_AGE)
2695 strlcat(buf, "|VI_AGE", sizeof(buf));
2696 if (vp->v_iflag & VI_DOOMED)
2697 strlcat(buf, "|VI_DOOMED", sizeof(buf));
2698 if (vp->v_iflag & VI_FREE)
2699 strlcat(buf, "|VI_FREE", sizeof(buf));
2700 if (vp->v_iflag & VI_DOINGINACT)
2701 strlcat(buf, "|VI_DOINGINACT", sizeof(buf));
2702 if (vp->v_iflag & VI_OWEINACT)
2703 strlcat(buf, "|VI_OWEINACT", sizeof(buf));
2704 flags = vp->v_iflag & ~(VI_MOUNT | VI_AGE | VI_DOOMED | VI_FREE |
2705 VI_DOINGINACT | VI_OWEINACT);
2707 snprintf(buf2, sizeof(buf2), "|VI(0x%lx)", flags);
2708 strlcat(buf, buf2, sizeof(buf));
2710 printf(" flags (%s)\n", buf + 1);
2711 if (mtx_owned(VI_MTX(vp)))
2712 printf(" VI_LOCKed");
2713 if (vp->v_object != NULL)
2714 printf(" v_object %p ref %d pages %d\n",
2715 vp->v_object, vp->v_object->ref_count,
2716 vp->v_object->resident_page_count);
2718 lockmgr_printinfo(vp->v_vnlock);
2719 if (vp->v_data != NULL)
2725 * List all of the locked vnodes in the system.
2726 * Called when debugging the kernel.
2728 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2730 struct mount *mp, *nmp;
2734 * Note: because this is DDB, we can't obey the locking semantics
2735 * for these structures, which means we could catch an inconsistent
2736 * state and dereference a nasty pointer. Not much to be done
2739 db_printf("Locked vnodes\n");
2740 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2741 nmp = TAILQ_NEXT(mp, mnt_list);
2742 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2743 if (vp->v_type != VMARKER &&
2747 nmp = TAILQ_NEXT(mp, mnt_list);
2752 * Show details about the given vnode.
2754 DB_SHOW_COMMAND(vnode, db_show_vnode)
2760 vp = (struct vnode *)addr;
2761 vn_printf(vp, "vnode ");
2765 * Show details about the given mount point.
2767 DB_SHOW_COMMAND(mount, db_show_mount)
2777 /* No address given, print short info about all mount points. */
2778 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2779 db_printf("%p %s on %s (%s)\n", mp,
2780 mp->mnt_stat.f_mntfromname,
2781 mp->mnt_stat.f_mntonname,
2782 mp->mnt_stat.f_fstypename);
2786 db_printf("\nMore info: show mount <addr>\n");
2790 mp = (struct mount *)addr;
2791 db_printf("%p %s on %s (%s)\n", mp, mp->mnt_stat.f_mntfromname,
2792 mp->mnt_stat.f_mntonname, mp->mnt_stat.f_fstypename);
2795 flags = mp->mnt_flag;
2796 #define MNT_FLAG(flag) do { \
2797 if (flags & (flag)) { \
2798 if (buf[0] != '\0') \
2799 strlcat(buf, ", ", sizeof(buf)); \
2800 strlcat(buf, (#flag) + 4, sizeof(buf)); \
2804 MNT_FLAG(MNT_RDONLY);
2805 MNT_FLAG(MNT_SYNCHRONOUS);
2806 MNT_FLAG(MNT_NOEXEC);
2807 MNT_FLAG(MNT_NOSUID);
2808 MNT_FLAG(MNT_UNION);
2809 MNT_FLAG(MNT_ASYNC);
2810 MNT_FLAG(MNT_SUIDDIR);
2811 MNT_FLAG(MNT_SOFTDEP);
2812 MNT_FLAG(MNT_NOSYMFOLLOW);
2813 MNT_FLAG(MNT_GJOURNAL);
2814 MNT_FLAG(MNT_MULTILABEL);
2816 MNT_FLAG(MNT_NOATIME);
2817 MNT_FLAG(MNT_NOCLUSTERR);
2818 MNT_FLAG(MNT_NOCLUSTERW);
2819 MNT_FLAG(MNT_EXRDONLY);
2820 MNT_FLAG(MNT_EXPORTED);
2821 MNT_FLAG(MNT_DEFEXPORTED);
2822 MNT_FLAG(MNT_EXPORTANON);
2823 MNT_FLAG(MNT_EXKERB);
2824 MNT_FLAG(MNT_EXPUBLIC);
2825 MNT_FLAG(MNT_LOCAL);
2826 MNT_FLAG(MNT_QUOTA);
2827 MNT_FLAG(MNT_ROOTFS);
2829 MNT_FLAG(MNT_IGNORE);
2830 MNT_FLAG(MNT_UPDATE);
2831 MNT_FLAG(MNT_DELEXPORT);
2832 MNT_FLAG(MNT_RELOAD);
2833 MNT_FLAG(MNT_FORCE);
2834 MNT_FLAG(MNT_SNAPSHOT);
2835 MNT_FLAG(MNT_BYFSID);
2839 strlcat(buf, ", ", sizeof(buf));
2840 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2843 db_printf(" mnt_flag = %s\n", buf);
2846 flags = mp->mnt_kern_flag;
2847 #define MNT_KERN_FLAG(flag) do { \
2848 if (flags & (flag)) { \
2849 if (buf[0] != '\0') \
2850 strlcat(buf, ", ", sizeof(buf)); \
2851 strlcat(buf, (#flag) + 5, sizeof(buf)); \
2855 MNT_KERN_FLAG(MNTK_UNMOUNTF);
2856 MNT_KERN_FLAG(MNTK_ASYNC);
2857 MNT_KERN_FLAG(MNTK_SOFTDEP);
2858 MNT_KERN_FLAG(MNTK_NOINSMNTQ);
2859 MNT_KERN_FLAG(MNTK_UNMOUNT);
2860 MNT_KERN_FLAG(MNTK_MWAIT);
2861 MNT_KERN_FLAG(MNTK_SUSPEND);
2862 MNT_KERN_FLAG(MNTK_SUSPEND2);
2863 MNT_KERN_FLAG(MNTK_SUSPENDED);
2864 MNT_KERN_FLAG(MNTK_MPSAFE);
2865 MNT_KERN_FLAG(MNTK_NOKNOTE);
2866 MNT_KERN_FLAG(MNTK_LOOKUP_SHARED);
2867 #undef MNT_KERN_FLAG
2870 strlcat(buf, ", ", sizeof(buf));
2871 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
2874 db_printf(" mnt_kern_flag = %s\n", buf);
2876 db_printf(" mnt_opt = ");
2877 opt = TAILQ_FIRST(mp->mnt_opt);
2879 db_printf("%s", opt->name);
2880 opt = TAILQ_NEXT(opt, link);
2881 while (opt != NULL) {
2882 db_printf(", %s", opt->name);
2883 opt = TAILQ_NEXT(opt, link);
2889 db_printf(" mnt_stat = { version=%u type=%u flags=0x%016jx "
2890 "bsize=%ju iosize=%ju blocks=%ju bfree=%ju bavail=%jd files=%ju "
2891 "ffree=%jd syncwrites=%ju asyncwrites=%ju syncreads=%ju "
2892 "asyncreads=%ju namemax=%u owner=%u fsid=[%d, %d] }\n",
2893 (u_int)sp->f_version, (u_int)sp->f_type, (uintmax_t)sp->f_flags,
2894 (uintmax_t)sp->f_bsize, (uintmax_t)sp->f_iosize,
2895 (uintmax_t)sp->f_blocks, (uintmax_t)sp->f_bfree,
2896 (intmax_t)sp->f_bavail, (uintmax_t)sp->f_files,
2897 (intmax_t)sp->f_ffree, (uintmax_t)sp->f_syncwrites,
2898 (uintmax_t)sp->f_asyncwrites, (uintmax_t)sp->f_syncreads,
2899 (uintmax_t)sp->f_asyncreads, (u_int)sp->f_namemax,
2900 (u_int)sp->f_owner, (int)sp->f_fsid.val[0], (int)sp->f_fsid.val[1]);
2902 db_printf(" mnt_cred = { uid=%u ruid=%u",
2903 (u_int)mp->mnt_cred->cr_uid, (u_int)mp->mnt_cred->cr_ruid);
2904 if (jailed(mp->mnt_cred))
2905 db_printf(", jail=%d", mp->mnt_cred->cr_prison->pr_id);
2907 db_printf(" mnt_ref = %d\n", mp->mnt_ref);
2908 db_printf(" mnt_gen = %d\n", mp->mnt_gen);
2909 db_printf(" mnt_nvnodelistsize = %d\n", mp->mnt_nvnodelistsize);
2910 db_printf(" mnt_writeopcount = %d\n", mp->mnt_writeopcount);
2911 db_printf(" mnt_noasync = %u\n", mp->mnt_noasync);
2912 db_printf(" mnt_maxsymlinklen = %d\n", mp->mnt_maxsymlinklen);
2913 db_printf(" mnt_iosize_max = %d\n", mp->mnt_iosize_max);
2914 db_printf(" mnt_hashseed = %u\n", mp->mnt_hashseed);
2915 db_printf(" mnt_secondary_writes = %d\n", mp->mnt_secondary_writes);
2916 db_printf(" mnt_secondary_accwrites = %d\n",
2917 mp->mnt_secondary_accwrites);
2918 db_printf(" mnt_gjprovider = %s\n",
2919 mp->mnt_gjprovider != NULL ? mp->mnt_gjprovider : "NULL");
2922 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2923 if (vp->v_type != VMARKER) {
2924 vn_printf(vp, "vnode ");
2933 * Fill in a struct xvfsconf based on a struct vfsconf.
2936 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
2939 strcpy(xvfsp->vfc_name, vfsp->vfc_name);
2940 xvfsp->vfc_typenum = vfsp->vfc_typenum;
2941 xvfsp->vfc_refcount = vfsp->vfc_refcount;
2942 xvfsp->vfc_flags = vfsp->vfc_flags;
2944 * These are unused in userland, we keep them
2945 * to not break binary compatibility.
2947 xvfsp->vfc_vfsops = NULL;
2948 xvfsp->vfc_next = NULL;
2952 * Top level filesystem related information gathering.
2955 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
2957 struct vfsconf *vfsp;
2958 struct xvfsconf xvfsp;
2962 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
2963 bzero(&xvfsp, sizeof(xvfsp));
2964 vfsconf2x(vfsp, &xvfsp);
2965 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp);
2972 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
2973 "S,xvfsconf", "List of all configured filesystems");
2975 #ifndef BURN_BRIDGES
2976 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
2979 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2981 int *name = (int *)arg1 - 1; /* XXX */
2982 u_int namelen = arg2 + 1; /* XXX */
2983 struct vfsconf *vfsp;
2984 struct xvfsconf xvfsp;
2986 printf("WARNING: userland calling deprecated sysctl, "
2987 "please rebuild world\n");
2989 #if 1 || defined(COMPAT_PRELITE2)
2990 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2992 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2996 case VFS_MAXTYPENUM:
2999 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
3002 return (ENOTDIR); /* overloaded */
3003 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
3004 if (vfsp->vfc_typenum == name[2])
3007 return (EOPNOTSUPP);
3008 bzero(&xvfsp, sizeof(xvfsp));
3009 vfsconf2x(vfsp, &xvfsp);
3010 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
3012 return (EOPNOTSUPP);
3015 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
3016 vfs_sysctl, "Generic filesystem");
3018 #if 1 || defined(COMPAT_PRELITE2)
3021 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
3024 struct vfsconf *vfsp;
3025 struct ovfsconf ovfs;
3027 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
3028 bzero(&ovfs, sizeof(ovfs));
3029 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
3030 strcpy(ovfs.vfc_name, vfsp->vfc_name);
3031 ovfs.vfc_index = vfsp->vfc_typenum;
3032 ovfs.vfc_refcount = vfsp->vfc_refcount;
3033 ovfs.vfc_flags = vfsp->vfc_flags;
3034 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
3041 #endif /* 1 || COMPAT_PRELITE2 */
3042 #endif /* !BURN_BRIDGES */
3044 #define KINFO_VNODESLOP 10
3047 * Dump vnode list (via sysctl).
3051 sysctl_vnode(SYSCTL_HANDLER_ARGS)
3059 * Stale numvnodes access is not fatal here.
3062 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
3064 /* Make an estimate */
3065 return (SYSCTL_OUT(req, 0, len));
3067 error = sysctl_wire_old_buffer(req, 0);
3070 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
3072 mtx_lock(&mountlist_mtx);
3073 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
3074 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK))
3077 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
3081 xvn[n].xv_size = sizeof *xvn;
3082 xvn[n].xv_vnode = vp;
3083 xvn[n].xv_id = 0; /* XXX compat */
3084 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
3086 XV_COPY(writecount);
3092 xvn[n].xv_flag = vp->v_vflag;
3094 switch (vp->v_type) {
3101 if (vp->v_rdev == NULL) {
3105 xvn[n].xv_dev = dev2udev(vp->v_rdev);
3108 xvn[n].xv_socket = vp->v_socket;
3111 xvn[n].xv_fifo = vp->v_fifoinfo;
3116 /* shouldn't happen? */
3124 mtx_lock(&mountlist_mtx);
3129 mtx_unlock(&mountlist_mtx);
3131 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
3136 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
3137 0, 0, sysctl_vnode, "S,xvnode", "");
3141 * Unmount all filesystems. The list is traversed in reverse order
3142 * of mounting to avoid dependencies.
3145 vfs_unmountall(void)
3151 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
3152 CTR1(KTR_VFS, "%s: unmounting all filesystems", __func__);
3156 * Since this only runs when rebooting, it is not interlocked.
3158 while(!TAILQ_EMPTY(&mountlist)) {
3159 mp = TAILQ_LAST(&mountlist, mntlist);
3160 error = dounmount(mp, MNT_FORCE, td);
3162 TAILQ_REMOVE(&mountlist, mp, mnt_list);
3164 * XXX: Due to the way in which we mount the root
3165 * file system off of devfs, devfs will generate a
3166 * "busy" warning when we try to unmount it before
3167 * the root. Don't print a warning as a result in
3168 * order to avoid false positive errors that may
3169 * cause needless upset.
3171 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
3172 printf("unmount of %s failed (",
3173 mp->mnt_stat.f_mntonname);
3177 printf("%d)\n", error);
3180 /* The unmount has removed mp from the mountlist */
3186 * perform msync on all vnodes under a mount point
3187 * the mount point must be locked.
3190 vfs_msync(struct mount *mp, int flags)
3192 struct vnode *vp, *mvp;
3193 struct vm_object *obj;
3195 CTR2(KTR_VFS, "%s: mp %p", __func__, mp);
3197 MNT_VNODE_FOREACH(vp, mp, mvp) {
3200 if (obj != NULL && (obj->flags & OBJ_MIGHTBEDIRTY) != 0 &&
3201 (flags == MNT_WAIT || VOP_ISLOCKED(vp) == 0)) {
3204 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
3206 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
3214 VM_OBJECT_LOCK(obj);
3215 vm_object_page_clean(obj, 0, 0,
3217 OBJPC_SYNC : OBJPC_NOSYNC);
3218 VM_OBJECT_UNLOCK(obj);
3230 * Mark a vnode as free, putting it up for recycling.
3233 vfree(struct vnode *vp)
3236 ASSERT_VI_LOCKED(vp, "vfree");
3237 mtx_lock(&vnode_free_list_mtx);
3238 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
3239 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
3240 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
3241 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
3242 ("vfree: Freeing doomed vnode"));
3243 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3244 if (vp->v_iflag & VI_AGE) {
3245 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
3247 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
3250 vp->v_iflag &= ~VI_AGE;
3251 vp->v_iflag |= VI_FREE;
3252 mtx_unlock(&vnode_free_list_mtx);
3256 * Opposite of vfree() - mark a vnode as in use.
3259 vbusy(struct vnode *vp)
3261 ASSERT_VI_LOCKED(vp, "vbusy");
3262 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
3263 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
3264 CTR2(KTR_VFS, "%s: vp %p", __func__, vp);
3266 mtx_lock(&vnode_free_list_mtx);
3267 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
3269 vp->v_iflag &= ~(VI_FREE|VI_AGE);
3270 mtx_unlock(&vnode_free_list_mtx);
3274 destroy_vpollinfo(struct vpollinfo *vi)
3276 knlist_destroy(&vi->vpi_selinfo.si_note);
3277 mtx_destroy(&vi->vpi_lock);
3278 uma_zfree(vnodepoll_zone, vi);
3282 * Initalize per-vnode helper structure to hold poll-related state.
3285 v_addpollinfo(struct vnode *vp)
3287 struct vpollinfo *vi;
3289 if (vp->v_pollinfo != NULL)
3291 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
3292 mtx_init(&vi->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
3293 knlist_init(&vi->vpi_selinfo.si_note, vp, vfs_knllock,
3294 vfs_knlunlock, vfs_knl_assert_locked, vfs_knl_assert_unlocked);
3296 if (vp->v_pollinfo != NULL) {
3298 destroy_vpollinfo(vi);
3301 vp->v_pollinfo = vi;
3306 * Record a process's interest in events which might happen to
3307 * a vnode. Because poll uses the historic select-style interface
3308 * internally, this routine serves as both the ``check for any
3309 * pending events'' and the ``record my interest in future events''
3310 * functions. (These are done together, while the lock is held,
3311 * to avoid race conditions.)
3314 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3318 mtx_lock(&vp->v_pollinfo->vpi_lock);
3319 if (vp->v_pollinfo->vpi_revents & events) {
3321 * This leaves events we are not interested
3322 * in available for the other process which
3323 * which presumably had requested them
3324 * (otherwise they would never have been
3327 events &= vp->v_pollinfo->vpi_revents;
3328 vp->v_pollinfo->vpi_revents &= ~events;
3330 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3333 vp->v_pollinfo->vpi_events |= events;
3334 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3335 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3340 * Routine to create and manage a filesystem syncer vnode.
3342 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3343 static int sync_fsync(struct vop_fsync_args *);
3344 static int sync_inactive(struct vop_inactive_args *);
3345 static int sync_reclaim(struct vop_reclaim_args *);
3347 static struct vop_vector sync_vnodeops = {
3348 .vop_bypass = VOP_EOPNOTSUPP,
3349 .vop_close = sync_close, /* close */
3350 .vop_fsync = sync_fsync, /* fsync */
3351 .vop_inactive = sync_inactive, /* inactive */
3352 .vop_reclaim = sync_reclaim, /* reclaim */
3353 .vop_lock1 = vop_stdlock, /* lock */
3354 .vop_unlock = vop_stdunlock, /* unlock */
3355 .vop_islocked = vop_stdislocked, /* islocked */
3359 * Create a new filesystem syncer vnode for the specified mount point.
3362 vfs_allocate_syncvnode(struct mount *mp)
3366 static long start, incr, next;
3369 /* Allocate a new vnode */
3370 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) {
3371 mp->mnt_syncer = NULL;
3375 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
3376 vp->v_vflag |= VV_FORCEINSMQ;
3377 error = insmntque(vp, mp);
3379 panic("vfs_allocate_syncvnode: insmntque failed");
3380 vp->v_vflag &= ~VV_FORCEINSMQ;
3383 * Place the vnode onto the syncer worklist. We attempt to
3384 * scatter them about on the list so that they will go off
3385 * at evenly distributed times even if all the filesystems
3386 * are mounted at once.
3389 if (next == 0 || next > syncer_maxdelay) {
3393 start = syncer_maxdelay / 2;
3394 incr = syncer_maxdelay;
3400 vn_syncer_add_to_worklist(bo, syncdelay > 0 ? next % syncdelay : 0);
3401 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3402 mtx_lock(&sync_mtx);
3404 mtx_unlock(&sync_mtx);
3406 mp->mnt_syncer = vp;
3411 * Do a lazy sync of the filesystem.
3414 sync_fsync(struct vop_fsync_args *ap)
3416 struct vnode *syncvp = ap->a_vp;
3417 struct mount *mp = syncvp->v_mount;
3422 * We only need to do something if this is a lazy evaluation.
3424 if (ap->a_waitfor != MNT_LAZY)
3428 * Move ourselves to the back of the sync list.
3430 bo = &syncvp->v_bufobj;
3432 vn_syncer_add_to_worklist(bo, syncdelay);
3436 * Walk the list of vnodes pushing all that are dirty and
3437 * not already on the sync list.
3439 mtx_lock(&mountlist_mtx);
3440 if (vfs_busy(mp, MBF_NOWAIT | MBF_MNTLSTLOCK) != 0) {
3441 mtx_unlock(&mountlist_mtx);
3444 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3450 mp->mnt_kern_flag &= ~MNTK_ASYNC;
3452 vfs_msync(mp, MNT_NOWAIT);
3453 error = VFS_SYNC(mp, MNT_LAZY);
3456 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
3457 mp->mnt_kern_flag |= MNTK_ASYNC;
3459 vn_finished_write(mp);
3465 * The syncer vnode is no referenced.
3468 sync_inactive(struct vop_inactive_args *ap)
3476 * The syncer vnode is no longer needed and is being decommissioned.
3478 * Modifications to the worklist must be protected by sync_mtx.
3481 sync_reclaim(struct vop_reclaim_args *ap)
3483 struct vnode *vp = ap->a_vp;
3488 vp->v_mount->mnt_syncer = NULL;
3489 if (bo->bo_flag & BO_ONWORKLST) {
3490 mtx_lock(&sync_mtx);
3491 LIST_REMOVE(bo, bo_synclist);
3492 syncer_worklist_len--;
3494 mtx_unlock(&sync_mtx);
3495 bo->bo_flag &= ~BO_ONWORKLST;
3503 * Check if vnode represents a disk device
3506 vn_isdisk(struct vnode *vp, int *errp)
3512 if (vp->v_type != VCHR)
3514 else if (vp->v_rdev == NULL)
3516 else if (vp->v_rdev->si_devsw == NULL)
3518 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3523 return (error == 0);
3527 * Common filesystem object access control check routine. Accepts a
3528 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3529 * and optional call-by-reference privused argument allowing vaccess()
3530 * to indicate to the caller whether privilege was used to satisfy the
3531 * request (obsoleted). Returns 0 on success, or an errno on failure.
3533 * The ifdef'd CAPABILITIES version is here for reference, but is not
3537 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3538 accmode_t accmode, struct ucred *cred, int *privused)
3540 accmode_t dac_granted;
3541 accmode_t priv_granted;
3544 * Look for a normal, non-privileged way to access the file/directory
3545 * as requested. If it exists, go with that.
3548 if (privused != NULL)
3553 /* Check the owner. */
3554 if (cred->cr_uid == file_uid) {
3555 dac_granted |= VADMIN;
3556 if (file_mode & S_IXUSR)
3557 dac_granted |= VEXEC;
3558 if (file_mode & S_IRUSR)
3559 dac_granted |= VREAD;
3560 if (file_mode & S_IWUSR)
3561 dac_granted |= (VWRITE | VAPPEND);
3563 if ((accmode & dac_granted) == accmode)
3569 /* Otherwise, check the groups (first match) */
3570 if (groupmember(file_gid, cred)) {
3571 if (file_mode & S_IXGRP)
3572 dac_granted |= VEXEC;
3573 if (file_mode & S_IRGRP)
3574 dac_granted |= VREAD;
3575 if (file_mode & S_IWGRP)
3576 dac_granted |= (VWRITE | VAPPEND);
3578 if ((accmode & dac_granted) == accmode)
3584 /* Otherwise, check everyone else. */
3585 if (file_mode & S_IXOTH)
3586 dac_granted |= VEXEC;
3587 if (file_mode & S_IROTH)
3588 dac_granted |= VREAD;
3589 if (file_mode & S_IWOTH)
3590 dac_granted |= (VWRITE | VAPPEND);
3591 if ((accmode & dac_granted) == accmode)
3596 * Build a privilege mask to determine if the set of privileges
3597 * satisfies the requirements when combined with the granted mask
3598 * from above. For each privilege, if the privilege is required,
3599 * bitwise or the request type onto the priv_granted mask.
3605 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3606 * requests, instead of PRIV_VFS_EXEC.
3608 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3609 !priv_check_cred(cred, PRIV_VFS_LOOKUP, 0))
3610 priv_granted |= VEXEC;
3612 if ((accmode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3613 !priv_check_cred(cred, PRIV_VFS_EXEC, 0))
3614 priv_granted |= VEXEC;
3617 if ((accmode & VREAD) && ((dac_granted & VREAD) == 0) &&
3618 !priv_check_cred(cred, PRIV_VFS_READ, 0))
3619 priv_granted |= VREAD;
3621 if ((accmode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3622 !priv_check_cred(cred, PRIV_VFS_WRITE, 0))
3623 priv_granted |= (VWRITE | VAPPEND);
3625 if ((accmode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3626 !priv_check_cred(cred, PRIV_VFS_ADMIN, 0))
3627 priv_granted |= VADMIN;
3629 if ((accmode & (priv_granted | dac_granted)) == accmode) {
3630 /* XXX audit: privilege used */
3631 if (privused != NULL)
3636 return ((accmode & VADMIN) ? EPERM : EACCES);
3640 * Credential check based on process requesting service, and per-attribute
3644 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3645 struct thread *td, accmode_t accmode)
3649 * Kernel-invoked always succeeds.
3655 * Do not allow privileged processes in jail to directly manipulate
3656 * system attributes.
3658 switch (attrnamespace) {
3659 case EXTATTR_NAMESPACE_SYSTEM:
3660 /* Potentially should be: return (EPERM); */
3661 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3662 case EXTATTR_NAMESPACE_USER:
3663 return (VOP_ACCESS(vp, accmode, cred, td));
3669 #ifdef DEBUG_VFS_LOCKS
3671 * This only exists to supress warnings from unlocked specfs accesses. It is
3672 * no longer ok to have an unlocked VFS.
3674 #define IGNORE_LOCK(vp) (panicstr != NULL || (vp) == NULL || \
3675 (vp)->v_type == VCHR || (vp)->v_type == VBAD)
3677 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3678 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, "");
3680 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3681 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 0, "");
3683 int vfs_badlock_print = 1; /* Print lock violations. */
3684 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 0, "");
3687 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3688 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, &vfs_badlock_backtrace, 0, "");
3692 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3696 if (vfs_badlock_backtrace)
3699 if (vfs_badlock_print)
3700 printf("%s: %p %s\n", str, (void *)vp, msg);
3701 if (vfs_badlock_ddb)
3702 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3706 assert_vi_locked(struct vnode *vp, const char *str)
3709 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3710 vfs_badlock("interlock is not locked but should be", str, vp);
3714 assert_vi_unlocked(struct vnode *vp, const char *str)
3717 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3718 vfs_badlock("interlock is locked but should not be", str, vp);
3722 assert_vop_locked(struct vnode *vp, const char *str)
3725 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == 0)
3726 vfs_badlock("is not locked but should be", str, vp);
3730 assert_vop_unlocked(struct vnode *vp, const char *str)
3733 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) == LK_EXCLUSIVE)
3734 vfs_badlock("is locked but should not be", str, vp);
3738 assert_vop_elocked(struct vnode *vp, const char *str)
3741 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLUSIVE)
3742 vfs_badlock("is not exclusive locked but should be", str, vp);
3747 assert_vop_elocked_other(struct vnode *vp, const char *str)
3750 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_EXCLOTHER)
3751 vfs_badlock("is not exclusive locked by another thread",
3756 assert_vop_slocked(struct vnode *vp, const char *str)
3759 if (!IGNORE_LOCK(vp) && VOP_ISLOCKED(vp) != LK_SHARED)
3760 vfs_badlock("is not locked shared but should be", str, vp);
3763 #endif /* DEBUG_VFS_LOCKS */
3766 vop_rename_pre(void *ap)
3768 struct vop_rename_args *a = ap;
3770 #ifdef DEBUG_VFS_LOCKS
3772 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3773 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3774 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3775 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3777 /* Check the source (from). */
3778 if (a->a_tdvp != a->a_fdvp && a->a_tvp != a->a_fdvp)
3779 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3780 if (a->a_tvp != a->a_fvp)
3781 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
3783 /* Check the target. */
3785 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3786 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3788 if (a->a_tdvp != a->a_fdvp)
3790 if (a->a_tvp != a->a_fvp)
3798 vop_strategy_pre(void *ap)
3800 #ifdef DEBUG_VFS_LOCKS
3801 struct vop_strategy_args *a;
3808 * Cluster ops lock their component buffers but not the IO container.
3810 if ((bp->b_flags & B_CLUSTER) != 0)
3813 if (!BUF_ISLOCKED(bp)) {
3814 if (vfs_badlock_print)
3816 "VOP_STRATEGY: bp is not locked but should be\n");
3817 if (vfs_badlock_ddb)
3818 kdb_enter(KDB_WHY_VFSLOCK, "lock violation");
3824 vop_lookup_pre(void *ap)
3826 #ifdef DEBUG_VFS_LOCKS
3827 struct vop_lookup_args *a;
3832 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3833 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3838 vop_lookup_post(void *ap, int rc)
3840 #ifdef DEBUG_VFS_LOCKS
3841 struct vop_lookup_args *a;
3849 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3850 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3853 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3858 vop_lock_pre(void *ap)
3860 #ifdef DEBUG_VFS_LOCKS
3861 struct vop_lock1_args *a = ap;
3863 if ((a->a_flags & LK_INTERLOCK) == 0)
3864 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3866 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3871 vop_lock_post(void *ap, int rc)
3873 #ifdef DEBUG_VFS_LOCKS
3874 struct vop_lock1_args *a = ap;
3876 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3878 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3883 vop_unlock_pre(void *ap)
3885 #ifdef DEBUG_VFS_LOCKS
3886 struct vop_unlock_args *a = ap;
3888 if (a->a_flags & LK_INTERLOCK)
3889 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3890 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3895 vop_unlock_post(void *ap, int rc)
3897 #ifdef DEBUG_VFS_LOCKS
3898 struct vop_unlock_args *a = ap;
3900 if (a->a_flags & LK_INTERLOCK)
3901 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
3906 vop_create_post(void *ap, int rc)
3908 struct vop_create_args *a = ap;
3911 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3915 vop_link_post(void *ap, int rc)
3917 struct vop_link_args *a = ap;
3920 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
3921 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
3926 vop_mkdir_post(void *ap, int rc)
3928 struct vop_mkdir_args *a = ap;
3931 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3935 vop_mknod_post(void *ap, int rc)
3937 struct vop_mknod_args *a = ap;
3940 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3944 vop_remove_post(void *ap, int rc)
3946 struct vop_remove_args *a = ap;
3949 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3950 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3955 vop_rename_post(void *ap, int rc)
3957 struct vop_rename_args *a = ap;
3960 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
3961 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
3962 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
3964 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
3966 if (a->a_tdvp != a->a_fdvp)
3968 if (a->a_tvp != a->a_fvp)
3976 vop_rmdir_post(void *ap, int rc)
3978 struct vop_rmdir_args *a = ap;
3981 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3982 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3987 vop_setattr_post(void *ap, int rc)
3989 struct vop_setattr_args *a = ap;
3992 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
3996 vop_symlink_post(void *ap, int rc)
3998 struct vop_symlink_args *a = ap;
4001 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
4004 static struct knlist fs_knlist;
4007 vfs_event_init(void *arg)
4009 knlist_init_mtx(&fs_knlist, NULL);
4011 /* XXX - correct order? */
4012 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
4015 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
4018 KNOTE_UNLOCKED(&fs_knlist, event);
4021 static int filt_fsattach(struct knote *kn);
4022 static void filt_fsdetach(struct knote *kn);
4023 static int filt_fsevent(struct knote *kn, long hint);
4025 struct filterops fs_filtops =
4026 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
4029 filt_fsattach(struct knote *kn)
4032 kn->kn_flags |= EV_CLEAR;
4033 knlist_add(&fs_knlist, kn, 0);
4038 filt_fsdetach(struct knote *kn)
4041 knlist_remove(&fs_knlist, kn, 0);
4045 filt_fsevent(struct knote *kn, long hint)
4048 kn->kn_fflags |= hint;
4049 return (kn->kn_fflags != 0);
4053 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
4059 error = SYSCTL_IN(req, &vc, sizeof(vc));
4062 if (vc.vc_vers != VFS_CTL_VERS1)
4064 mp = vfs_getvfs(&vc.vc_fsid);
4067 /* ensure that a specific sysctl goes to the right filesystem. */
4068 if (strcmp(vc.vc_fstypename, "*") != 0 &&
4069 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
4073 VCTLTOREQ(&vc, req);
4074 error = VFS_SYSCTL(mp, vc.vc_op, req);
4079 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, NULL, 0, sysctl_vfs_ctl, "",
4083 * Function to initialize a va_filerev field sensibly.
4084 * XXX: Wouldn't a random number make a lot more sense ??
4087 init_va_filerev(void)
4092 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
4095 static int filt_vfsread(struct knote *kn, long hint);
4096 static int filt_vfswrite(struct knote *kn, long hint);
4097 static int filt_vfsvnode(struct knote *kn, long hint);
4098 static void filt_vfsdetach(struct knote *kn);
4099 static struct filterops vfsread_filtops =
4100 { 1, NULL, filt_vfsdetach, filt_vfsread };
4101 static struct filterops vfswrite_filtops =
4102 { 1, NULL, filt_vfsdetach, filt_vfswrite };
4103 static struct filterops vfsvnode_filtops =
4104 { 1, NULL, filt_vfsdetach, filt_vfsvnode };
4107 vfs_knllock(void *arg)
4109 struct vnode *vp = arg;
4111 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
4115 vfs_knlunlock(void *arg)
4117 struct vnode *vp = arg;
4123 vfs_knl_assert_locked(void *arg)
4125 #ifdef DEBUG_VFS_LOCKS
4126 struct vnode *vp = arg;
4128 ASSERT_VOP_LOCKED(vp, "vfs_knl_assert_locked");
4133 vfs_knl_assert_unlocked(void *arg)
4135 #ifdef DEBUG_VFS_LOCKS
4136 struct vnode *vp = arg;
4138 ASSERT_VOP_UNLOCKED(vp, "vfs_knl_assert_unlocked");
4143 vfs_kqfilter(struct vop_kqfilter_args *ap)
4145 struct vnode *vp = ap->a_vp;
4146 struct knote *kn = ap->a_kn;
4149 switch (kn->kn_filter) {
4151 kn->kn_fop = &vfsread_filtops;
4154 kn->kn_fop = &vfswrite_filtops;
4157 kn->kn_fop = &vfsvnode_filtops;
4163 kn->kn_hook = (caddr_t)vp;
4166 if (vp->v_pollinfo == NULL)
4168 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
4169 knlist_add(knl, kn, 0);
4175 * Detach knote from vnode
4178 filt_vfsdetach(struct knote *kn)
4180 struct vnode *vp = (struct vnode *)kn->kn_hook;
4182 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
4183 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
4188 filt_vfsread(struct knote *kn, long hint)
4190 struct vnode *vp = (struct vnode *)kn->kn_hook;
4195 * filesystem is gone, so set the EOF flag and schedule
4196 * the knote for deletion.
4198 if (hint == NOTE_REVOKE) {
4200 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4205 if (VOP_GETATTR(vp, &va, curthread->td_ucred))
4209 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
4210 res = (kn->kn_data != 0);
4217 filt_vfswrite(struct knote *kn, long hint)
4219 struct vnode *vp = (struct vnode *)kn->kn_hook;
4224 * filesystem is gone, so set the EOF flag and schedule
4225 * the knote for deletion.
4227 if (hint == NOTE_REVOKE)
4228 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
4236 filt_vfsvnode(struct knote *kn, long hint)
4238 struct vnode *vp = (struct vnode *)kn->kn_hook;
4242 if (kn->kn_sfflags & hint)
4243 kn->kn_fflags |= hint;
4244 if (hint == NOTE_REVOKE) {
4245 kn->kn_flags |= EV_EOF;
4249 res = (kn->kn_fflags != 0);
4255 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
4259 if (dp->d_reclen > ap->a_uio->uio_resid)
4260 return (ENAMETOOLONG);
4261 error = uiomove(dp, dp->d_reclen, ap->a_uio);
4263 if (ap->a_ncookies != NULL) {
4264 if (ap->a_cookies != NULL)
4265 free(ap->a_cookies, M_TEMP);
4266 ap->a_cookies = NULL;
4267 *ap->a_ncookies = 0;
4271 if (ap->a_ncookies == NULL)
4274 KASSERT(ap->a_cookies,
4275 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
4277 *ap->a_cookies = realloc(*ap->a_cookies,
4278 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
4279 (*ap->a_cookies)[*ap->a_ncookies] = off;
4284 * Mark for update the access time of the file if the filesystem
4285 * supports VOP_MARKATIME. This functionality is used by execve and
4286 * mmap, so we want to avoid the I/O implied by directly setting
4287 * va_atime for the sake of efficiency.
4290 vfs_mark_atime(struct vnode *vp, struct ucred *cred)
4295 VFS_ASSERT_GIANT(mp);
4296 ASSERT_VOP_LOCKED(vp, "vfs_mark_atime");
4297 if (mp != NULL && (mp->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0)
4298 (void)VOP_MARKATIME(vp);
4302 * The purpose of this routine is to remove granularity from accmode_t,
4303 * reducing it into standard unix access bits - VEXEC, VREAD, VWRITE,
4304 * VADMIN and VAPPEND.
4306 * If it returns 0, the caller is supposed to continue with the usual
4307 * access checks using 'accmode' as modified by this routine. If it
4308 * returns nonzero value, the caller is supposed to return that value
4311 * Note that after this routine runs, accmode may be zero.
4314 vfs_unixify_accmode(accmode_t *accmode)
4317 * There is no way to specify explicit "deny" rule using
4318 * file mode or POSIX.1e ACLs.
4320 if (*accmode & VEXPLICIT_DENY) {
4326 * None of these can be translated into usual access bits.
4327 * Also, the common case for NFSv4 ACLs is to not contain
4328 * either of these bits. Caller should check for VWRITE
4329 * on the containing directory instead.
4331 if (*accmode & (VDELETE_CHILD | VDELETE))
4334 if (*accmode & VADMIN_PERMS) {
4335 *accmode &= ~VADMIN_PERMS;
4340 * There is no way to deny VREAD_ATTRIBUTES, VREAD_ACL
4341 * or VSYNCHRONIZE using file mode or POSIX.1e ACL.
4343 *accmode &= ~(VSTAT_PERMS | VSYNCHRONIZE);