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$");
47 #include <sys/param.h>
48 #include <sys/systm.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/malloc.h>
63 #include <sys/mount.h>
64 #include <sys/namei.h>
66 #include <sys/reboot.h>
67 #include <sys/sleepqueue.h>
69 #include <sys/sysctl.h>
70 #include <sys/syslog.h>
71 #include <sys/vmmeter.h>
72 #include <sys/vnode.h>
74 #include <machine/stdarg.h>
76 #include <security/mac/mac_framework.h>
79 #include <vm/vm_object.h>
80 #include <vm/vm_extern.h>
82 #include <vm/vm_map.h>
83 #include <vm/vm_page.h>
84 #include <vm/vm_kern.h>
91 static MALLOC_DEFINE(M_NETADDR, "subr_export_host", "Export host address structure");
93 static void delmntque(struct vnode *vp);
94 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
95 int slpflag, int slptimeo);
96 static void syncer_shutdown(void *arg, int howto);
97 static int vtryrecycle(struct vnode *vp);
98 static void vbusy(struct vnode *vp);
99 static void vinactive(struct vnode *, struct thread *);
100 static void v_incr_usecount(struct vnode *);
101 static void v_decr_usecount(struct vnode *);
102 static void v_decr_useonly(struct vnode *);
103 static void v_upgrade_usecount(struct vnode *);
104 static void vfree(struct vnode *);
105 static void vnlru_free(int);
106 static void vdestroy(struct vnode *);
107 static void vgonel(struct vnode *);
108 static void vfs_knllock(void *arg);
109 static void vfs_knlunlock(void *arg);
110 static int vfs_knllocked(void *arg);
114 * Enable Giant pushdown based on whether or not the vm is mpsafe in this
115 * build. Without mpsafevm the buffer cache can not run Giant free.
118 TUNABLE_INT("debug.mpsafevfs", &mpsafe_vfs);
119 SYSCTL_INT(_debug, OID_AUTO, mpsafevfs, CTLFLAG_RD, &mpsafe_vfs, 0,
123 * Number of vnodes in existence. Increased whenever getnewvnode()
124 * allocates a new vnode, decreased on vdestroy() called on VI_DOOMed
127 static unsigned long numvnodes;
129 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
132 * Conversion tables for conversion from vnode types to inode formats
135 enum vtype iftovt_tab[16] = {
136 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
137 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
139 int vttoif_tab[10] = {
140 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
141 S_IFSOCK, S_IFIFO, S_IFMT, S_IFMT
145 * List of vnodes that are ready for recycling.
147 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
150 * Free vnode target. Free vnodes may simply be files which have been stat'd
151 * but not read. This is somewhat common, and a small cache of such files
152 * should be kept to avoid recreation costs.
154 static u_long wantfreevnodes;
155 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
156 /* Number of vnodes in the free list. */
157 static u_long freevnodes;
158 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
161 * Various variables used for debugging the new implementation of
163 * XXX these are probably of (very) limited utility now.
165 static int reassignbufcalls;
166 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
169 * Cache for the mount type id assigned to NFS. This is used for
170 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
172 int nfs_mount_type = -1;
174 /* To keep more than one thread at a time from running vfs_getnewfsid */
175 static struct mtx mntid_mtx;
178 * Lock for any access to the following:
183 static struct mtx vnode_free_list_mtx;
185 /* Publicly exported FS */
186 struct nfs_public nfs_pub;
188 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
189 static uma_zone_t vnode_zone;
190 static uma_zone_t vnodepoll_zone;
192 /* Set to 1 to print out reclaim of active vnodes */
196 * The workitem queue.
198 * It is useful to delay writes of file data and filesystem metadata
199 * for tens of seconds so that quickly created and deleted files need
200 * not waste disk bandwidth being created and removed. To realize this,
201 * we append vnodes to a "workitem" queue. When running with a soft
202 * updates implementation, most pending metadata dependencies should
203 * not wait for more than a few seconds. Thus, mounted on block devices
204 * are delayed only about a half the time that file data is delayed.
205 * Similarly, directory updates are more critical, so are only delayed
206 * about a third the time that file data is delayed. Thus, there are
207 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
208 * one each second (driven off the filesystem syncer process). The
209 * syncer_delayno variable indicates the next queue that is to be processed.
210 * Items that need to be processed soon are placed in this queue:
212 * syncer_workitem_pending[syncer_delayno]
214 * A delay of fifteen seconds is done by placing the request fifteen
215 * entries later in the queue:
217 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
220 static int syncer_delayno;
221 static long syncer_mask;
222 LIST_HEAD(synclist, bufobj);
223 static struct synclist *syncer_workitem_pending;
225 * The sync_mtx protects:
230 * syncer_workitem_pending
231 * syncer_worklist_len
234 static struct mtx sync_mtx;
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 + VMCNT_GET(page_count) / 4, 2 *
301 vm_kmem_size / (5 * (sizeof(struct vm_object) +
302 sizeof(struct vnode))));
303 if (desiredvnodes > MAXVNODES_MAX) {
305 printf("Reducing kern.maxvnodes %d -> %d\n",
306 desiredvnodes, MAXVNODES_MAX);
307 desiredvnodes = MAXVNODES_MAX;
309 wantfreevnodes = desiredvnodes / 4;
310 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
311 TAILQ_INIT(&vnode_free_list);
312 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
313 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
314 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
315 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
316 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
318 * Initialize the filesystem syncer.
320 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
322 syncer_maxdelay = syncer_mask + 1;
323 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
325 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL)
329 * Mark a mount point as busy. Used to synchronize access and to delay
330 * unmounting. Interlock is not released on failure.
333 vfs_busy(struct mount *mp, int flags, struct mtx *interlkp,
340 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
341 if (flags & LK_NOWAIT) {
347 mtx_unlock(interlkp);
348 mp->mnt_kern_flag |= MNTK_MWAIT;
350 * Since all busy locks are shared except the exclusive
351 * lock granted when unmounting, the only place that a
352 * wakeup needs to be done is at the release of the
353 * exclusive lock at the end of dounmount.
355 msleep(mp, MNT_MTX(mp), PVFS, "vfs_busy", 0);
363 mtx_unlock(interlkp);
364 lkflags = LK_SHARED | LK_INTERLOCK;
365 if (lockmgr(&mp->mnt_lock, lkflags, MNT_MTX(mp), td))
366 panic("vfs_busy: unexpected lock failure");
371 * Free a busy filesystem.
374 vfs_unbusy(struct mount *mp, struct thread *td)
377 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
382 * Lookup a mount point by filesystem identifier.
385 vfs_getvfs(fsid_t *fsid)
389 mtx_lock(&mountlist_mtx);
390 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
391 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
392 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
394 mtx_unlock(&mountlist_mtx);
398 mtx_unlock(&mountlist_mtx);
399 return ((struct mount *) 0);
403 * Check if a user can access privileged mount options.
406 vfs_suser(struct mount *mp, struct thread *td)
411 * If the thread is jailed, but this is not a jail-friendly file
412 * system, deny immediately.
414 if (jailed(td->td_ucred) && !(mp->mnt_vfc->vfc_flags & VFCF_JAIL))
418 * If the file system was mounted outside a jail and a jailed thread
419 * tries to access it, deny immediately.
421 if (!jailed(mp->mnt_cred) && jailed(td->td_ucred))
425 * If the file system was mounted inside different jail that the jail of
426 * the calling thread, deny immediately.
428 if (jailed(mp->mnt_cred) && jailed(td->td_ucred) &&
429 mp->mnt_cred->cr_prison != td->td_ucred->cr_prison) {
433 if ((mp->mnt_flag & MNT_USER) == 0 ||
434 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
435 if ((error = priv_check(td, PRIV_VFS_MOUNT_OWNER)) != 0)
442 * Get a new unique fsid. Try to make its val[0] unique, since this value
443 * will be used to create fake device numbers for stat(). Also try (but
444 * not so hard) make its val[0] unique mod 2^16, since some emulators only
445 * support 16-bit device numbers. We end up with unique val[0]'s for the
446 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
448 * Keep in mind that several mounts may be running in parallel. Starting
449 * the search one past where the previous search terminated is both a
450 * micro-optimization and a defense against returning the same fsid to
454 vfs_getnewfsid(struct mount *mp)
456 static u_int16_t mntid_base;
461 mtx_lock(&mntid_mtx);
462 mtype = mp->mnt_vfc->vfc_typenum;
463 tfsid.val[1] = mtype;
464 mtype = (mtype & 0xFF) << 24;
466 tfsid.val[0] = makedev(255,
467 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
469 if ((nmp = vfs_getvfs(&tfsid)) == NULL)
473 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
474 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
475 mtx_unlock(&mntid_mtx);
479 * Knob to control the precision of file timestamps:
481 * 0 = seconds only; nanoseconds zeroed.
482 * 1 = seconds and nanoseconds, accurate within 1/HZ.
483 * 2 = seconds and nanoseconds, truncated to microseconds.
484 * >=3 = seconds and nanoseconds, maximum precision.
486 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
488 static int timestamp_precision = TSP_SEC;
489 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
490 ×tamp_precision, 0, "");
493 * Get a current timestamp.
496 vfs_timestamp(struct timespec *tsp)
500 switch (timestamp_precision) {
502 tsp->tv_sec = time_second;
510 TIMEVAL_TO_TIMESPEC(&tv, tsp);
520 * Set vnode attributes to VNOVAL
523 vattr_null(struct vattr *vap)
527 vap->va_size = VNOVAL;
528 vap->va_bytes = VNOVAL;
529 vap->va_mode = VNOVAL;
530 vap->va_nlink = VNOVAL;
531 vap->va_uid = VNOVAL;
532 vap->va_gid = VNOVAL;
533 vap->va_fsid = VNOVAL;
534 vap->va_fileid = VNOVAL;
535 vap->va_blocksize = VNOVAL;
536 vap->va_rdev = VNOVAL;
537 vap->va_atime.tv_sec = VNOVAL;
538 vap->va_atime.tv_nsec = VNOVAL;
539 vap->va_mtime.tv_sec = VNOVAL;
540 vap->va_mtime.tv_nsec = VNOVAL;
541 vap->va_ctime.tv_sec = VNOVAL;
542 vap->va_ctime.tv_nsec = VNOVAL;
543 vap->va_birthtime.tv_sec = VNOVAL;
544 vap->va_birthtime.tv_nsec = VNOVAL;
545 vap->va_flags = VNOVAL;
546 vap->va_gen = VNOVAL;
551 * This routine is called when we have too many vnodes. It attempts
552 * to free <count> vnodes and will potentially free vnodes that still
553 * have VM backing store (VM backing store is typically the cause
554 * of a vnode blowout so we want to do this). Therefore, this operation
555 * is not considered cheap.
557 * A number of conditions may prevent a vnode from being reclaimed.
558 * the buffer cache may have references on the vnode, a directory
559 * vnode may still have references due to the namei cache representing
560 * underlying files, or the vnode may be in active use. It is not
561 * desireable to reuse such vnodes. These conditions may cause the
562 * number of vnodes to reach some minimum value regardless of what
563 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
566 vlrureclaim(struct mount *mp)
576 * Calculate the trigger point, don't allow user
577 * screwups to blow us up. This prevents us from
578 * recycling vnodes with lots of resident pages. We
579 * aren't trying to free memory, we are trying to
582 usevnodes = desiredvnodes;
585 trigger = VMCNT_GET(page_count) * 2 / usevnodes;
588 vn_start_write(NULL, &mp, V_WAIT);
590 count = mp->mnt_nvnodelistsize / 10 + 1;
592 vp = TAILQ_FIRST(&mp->mnt_nvnodelist);
593 while (vp != NULL && vp->v_type == VMARKER)
594 vp = TAILQ_NEXT(vp, v_nmntvnodes);
597 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
598 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
603 * If it's been deconstructed already, it's still
604 * referenced, or it exceeds the trigger, skip it.
606 if (vp->v_usecount || !LIST_EMPTY(&(vp)->v_cache_src) ||
607 (vp->v_iflag & VI_DOOMED) != 0 || (vp->v_object != NULL &&
608 vp->v_object->resident_page_count > trigger)) {
614 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE|LK_NOWAIT, td)) {
616 goto next_iter_mntunlocked;
620 * v_usecount may have been bumped after VOP_LOCK() dropped
621 * the vnode interlock and before it was locked again.
623 * It is not necessary to recheck VI_DOOMED because it can
624 * only be set by another thread that holds both the vnode
625 * lock and vnode interlock. If another thread has the
626 * vnode lock before we get to VOP_LOCK() and obtains the
627 * vnode interlock after VOP_LOCK() drops the vnode
628 * interlock, the other thread will be unable to drop the
629 * vnode lock before our VOP_LOCK() call fails.
631 if (vp->v_usecount || !LIST_EMPTY(&(vp)->v_cache_src) ||
632 (vp->v_object != NULL &&
633 vp->v_object->resident_page_count > trigger)) {
634 VOP_UNLOCK(vp, LK_INTERLOCK, td);
635 goto next_iter_mntunlocked;
637 KASSERT((vp->v_iflag & VI_DOOMED) == 0,
638 ("VI_DOOMED unexpectedly detected in vlrureclaim()"));
640 VOP_UNLOCK(vp, 0, td);
643 next_iter_mntunlocked:
644 if ((count % 256) != 0)
648 if ((count % 256) != 0)
657 vn_finished_write(mp);
662 * Attempt to keep the free list at wantfreevnodes length.
665 vnlru_free(int count)
670 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
671 for (; count > 0; count--) {
672 vp = TAILQ_FIRST(&vnode_free_list);
674 * The list can be modified while the free_list_mtx
675 * has been dropped and vp could be NULL here.
679 VNASSERT(vp->v_op != NULL, vp,
680 ("vnlru_free: vnode already reclaimed."));
681 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
683 * Don't recycle if we can't get the interlock.
685 if (!VI_TRYLOCK(vp)) {
686 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
689 VNASSERT(VCANRECYCLE(vp), vp,
690 ("vp inconsistent on freelist"));
692 vp->v_iflag &= ~VI_FREE;
694 mtx_unlock(&vnode_free_list_mtx);
696 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
698 VFS_UNLOCK_GIANT(vfslocked);
700 * If the recycled succeeded this vdrop will actually free
701 * the vnode. If not it will simply place it back on
705 mtx_lock(&vnode_free_list_mtx);
709 * Attempt to recycle vnodes in a context that is always safe to block.
710 * Calling vlrurecycle() from the bowels of filesystem code has some
711 * interesting deadlock problems.
713 static struct proc *vnlruproc;
714 static int vnlruproc_sig;
719 struct mount *mp, *nmp;
721 struct proc *p = vnlruproc;
722 struct thread *td = FIRST_THREAD_IN_PROC(p);
726 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
730 kthread_suspend_check(p);
731 mtx_lock(&vnode_free_list_mtx);
732 if (freevnodes > wantfreevnodes)
733 vnlru_free(freevnodes - wantfreevnodes);
734 if (numvnodes <= desiredvnodes * 9 / 10) {
736 wakeup(&vnlruproc_sig);
737 msleep(vnlruproc, &vnode_free_list_mtx,
738 PVFS|PDROP, "vlruwt", hz);
741 mtx_unlock(&vnode_free_list_mtx);
743 mtx_lock(&mountlist_mtx);
744 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
746 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
747 nmp = TAILQ_NEXT(mp, mnt_list);
750 if (!VFS_NEEDSGIANT(mp)) {
755 done += vlrureclaim(mp);
758 mtx_lock(&mountlist_mtx);
759 nmp = TAILQ_NEXT(mp, mnt_list);
762 mtx_unlock(&mountlist_mtx);
764 EVENTHANDLER_INVOKE(vfs_lowvnodes, desiredvnodes / 10);
766 /* These messages are temporary debugging aids */
767 if (vnlru_nowhere < 5)
768 printf("vnlru process getting nowhere..\n");
769 else if (vnlru_nowhere == 5)
770 printf("vnlru process messages stopped.\n");
773 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
779 static struct kproc_desc vnlru_kp = {
784 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
787 * Routines having to do with the management of the vnode table.
791 vdestroy(struct vnode *vp)
795 CTR1(KTR_VFS, "vdestroy vp %p", vp);
796 mtx_lock(&vnode_free_list_mtx);
798 mtx_unlock(&vnode_free_list_mtx);
800 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
801 ("cleaned vnode still on the free list."));
802 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
803 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
804 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
805 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
806 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
807 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
808 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
809 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
810 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
811 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
812 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
815 mac_destroy_vnode(vp);
817 if (vp->v_pollinfo != NULL) {
818 knlist_destroy(&vp->v_pollinfo->vpi_selinfo.si_note);
819 mtx_destroy(&vp->v_pollinfo->vpi_lock);
820 uma_zfree(vnodepoll_zone, vp->v_pollinfo);
823 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
826 lockdestroy(vp->v_vnlock);
827 mtx_destroy(&vp->v_interlock);
828 uma_zfree(vnode_zone, vp);
832 * Try to recycle a freed vnode. We abort if anyone picks up a reference
833 * before we actually vgone(). This function must be called with the vnode
834 * held to prevent the vnode from being returned to the free list midway
838 vtryrecycle(struct vnode *vp)
840 struct thread *td = curthread;
843 CTR1(KTR_VFS, "vtryrecycle: trying vp %p", vp);
844 VNASSERT(vp->v_holdcnt, vp,
845 ("vtryrecycle: Recycling vp %p without a reference.", vp));
847 * This vnode may found and locked via some other list, if so we
848 * can't recycle it yet.
850 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT, td) != 0)
851 return (EWOULDBLOCK);
853 * Don't recycle if its filesystem is being suspended.
855 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
856 VOP_UNLOCK(vp, 0, td);
860 * If we got this far, we need to acquire the interlock and see if
861 * anyone picked up this vnode from another list. If not, we will
862 * mark it with DOOMED via vgonel() so that anyone who does find it
866 if (vp->v_usecount) {
867 VOP_UNLOCK(vp, LK_INTERLOCK, td);
868 vn_finished_write(vnmp);
871 if ((vp->v_iflag & VI_DOOMED) == 0)
873 VOP_UNLOCK(vp, LK_INTERLOCK, td);
874 vn_finished_write(vnmp);
875 CTR1(KTR_VFS, "vtryrecycle: recycled vp %p", vp);
880 * Return the next vnode from the free list.
883 getnewvnode(const char *tag, struct mount *mp, struct vop_vector *vops,
886 struct vnode *vp = NULL;
889 mtx_lock(&vnode_free_list_mtx);
891 * Lend our context to reclaim vnodes if they've exceeded the max.
893 if (freevnodes > wantfreevnodes)
896 * Wait for available vnodes.
898 if (numvnodes > desiredvnodes) {
899 if (mp != NULL && (mp->mnt_kern_flag & MNTK_SUSPEND)) {
901 * File system is beeing suspended, we cannot risk a
902 * deadlock here, so allocate new vnode anyway.
904 if (freevnodes > wantfreevnodes)
905 vnlru_free(freevnodes - wantfreevnodes);
908 if (vnlruproc_sig == 0) {
909 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
912 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
914 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
915 if (numvnodes > desiredvnodes) {
916 mtx_unlock(&vnode_free_list_mtx);
923 mtx_unlock(&vnode_free_list_mtx);
924 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
928 vp->v_vnlock = &vp->v_lock;
929 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
931 * By default, don't allow shared locks unless filesystems
934 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
940 bo->bo_mtx = &vp->v_interlock;
941 bo->bo_ops = &buf_ops_bio;
943 TAILQ_INIT(&bo->bo_clean.bv_hd);
944 TAILQ_INIT(&bo->bo_dirty.bv_hd);
946 * Initialize namecache.
948 LIST_INIT(&vp->v_cache_src);
949 TAILQ_INIT(&vp->v_cache_dst);
951 * Finalize various vnode identity bits.
960 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
961 mac_associate_vnode_singlelabel(mp, vp);
963 printf("NULL mp in getnewvnode()\n");
966 bo->bo_bsize = mp->mnt_stat.f_iosize;
967 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
968 vp->v_vflag |= VV_NOKNOTE;
971 CTR2(KTR_VFS, "getnewvnode: mp %p vp %p", mp, vp);
977 * Delete from old mount point vnode list, if on one.
980 delmntque(struct vnode *vp)
989 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
990 ("bad mount point vnode list size"));
991 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
992 mp->mnt_nvnodelistsize--;
998 insmntque_stddtr(struct vnode *vp, void *dtr_arg)
1002 td = curthread; /* XXX ? */
1004 vp->v_op = &dead_vnodeops;
1005 /* XXX non mp-safe fs may still call insmntque with vnode
1007 if (!VOP_ISLOCKED(vp, td))
1008 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1014 * Insert into list of vnodes for the new mount point, if available.
1017 insmntque1(struct vnode *vp, struct mount *mp,
1018 void (*dtr)(struct vnode *, void *), void *dtr_arg)
1021 KASSERT(vp->v_mount == NULL,
1022 ("insmntque: vnode already on per mount vnode list"));
1023 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
1025 if ((mp->mnt_kern_flag & MNTK_UNMOUNT) != 0 &&
1026 mp->mnt_nvnodelistsize == 0) {
1034 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
1035 VNASSERT(mp->mnt_nvnodelistsize >= 0, vp,
1036 ("neg mount point vnode list size"));
1037 mp->mnt_nvnodelistsize++;
1043 insmntque(struct vnode *vp, struct mount *mp)
1046 return (insmntque1(vp, mp, insmntque_stddtr, NULL));
1050 * Flush out and invalidate all buffers associated with a bufobj
1051 * Called with the underlying object locked.
1054 bufobj_invalbuf(struct bufobj *bo, int flags, struct thread *td, int slpflag,
1060 if (flags & V_SAVE) {
1061 error = bufobj_wwait(bo, slpflag, slptimeo);
1066 if (bo->bo_dirty.bv_cnt > 0) {
1068 if ((error = BO_SYNC(bo, MNT_WAIT, td)) != 0)
1071 * XXX We could save a lock/unlock if this was only
1072 * enabled under INVARIANTS
1075 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
1076 panic("vinvalbuf: dirty bufs");
1080 * If you alter this loop please notice that interlock is dropped and
1081 * reacquired in flushbuflist. Special care is needed to ensure that
1082 * no race conditions occur from this.
1085 error = flushbuflist(&bo->bo_clean,
1086 flags, bo, slpflag, slptimeo);
1088 error = flushbuflist(&bo->bo_dirty,
1089 flags, bo, slpflag, slptimeo);
1090 if (error != 0 && error != EAGAIN) {
1094 } while (error != 0);
1097 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
1098 * have write I/O in-progress but if there is a VM object then the
1099 * VM object can also have read-I/O in-progress.
1102 bufobj_wwait(bo, 0, 0);
1104 if (bo->bo_object != NULL) {
1105 VM_OBJECT_LOCK(bo->bo_object);
1106 vm_object_pip_wait(bo->bo_object, "bovlbx");
1107 VM_OBJECT_UNLOCK(bo->bo_object);
1110 } while (bo->bo_numoutput > 0);
1114 * Destroy the copy in the VM cache, too.
1116 if (bo->bo_object != NULL) {
1117 VM_OBJECT_LOCK(bo->bo_object);
1118 vm_object_page_remove(bo->bo_object, 0, 0,
1119 (flags & V_SAVE) ? TRUE : FALSE);
1120 VM_OBJECT_UNLOCK(bo->bo_object);
1125 if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1126 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1127 panic("vinvalbuf: flush failed");
1134 * Flush out and invalidate all buffers associated with a vnode.
1135 * Called with the underlying object locked.
1138 vinvalbuf(struct vnode *vp, int flags, struct thread *td, int slpflag,
1142 CTR2(KTR_VFS, "vinvalbuf vp %p flags %d", vp, flags);
1143 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1144 return (bufobj_invalbuf(&vp->v_bufobj, flags, td, slpflag, slptimeo));
1148 * Flush out buffers on the specified list.
1152 flushbuflist( struct bufv *bufv, int flags, struct bufobj *bo, int slpflag,
1155 struct buf *bp, *nbp;
1160 ASSERT_BO_LOCKED(bo);
1163 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1164 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1165 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1171 lblkno = nbp->b_lblkno;
1172 xflags = nbp->b_xflags &
1173 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN);
1176 error = BUF_TIMELOCK(bp,
1177 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1178 "flushbuf", slpflag, slptimeo);
1181 return (error != ENOLCK ? error : EAGAIN);
1183 KASSERT(bp->b_bufobj == bo,
1184 ("bp %p wrong b_bufobj %p should be %p",
1185 bp, bp->b_bufobj, bo));
1186 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1192 * XXX Since there are no node locks for NFS, I
1193 * believe there is a slight chance that a delayed
1194 * write will occur while sleeping just above, so
1197 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1200 bp->b_flags |= B_ASYNC;
1203 return (EAGAIN); /* XXX: why not loop ? */
1206 bp->b_flags |= (B_INVAL | B_RELBUF);
1207 bp->b_flags &= ~B_ASYNC;
1211 (nbp->b_bufobj != bo ||
1212 nbp->b_lblkno != lblkno ||
1214 (BX_BKGRDMARKER | BX_VNDIRTY | BX_VNCLEAN)) != xflags))
1215 break; /* nbp invalid */
1221 * Truncate a file's buffer and pages to a specified length. This
1222 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1226 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td,
1227 off_t length, int blksize)
1229 struct buf *bp, *nbp;
1234 CTR2(KTR_VFS, "vtruncbuf vp %p length %jd", vp, length);
1236 * Round up to the *next* lbn.
1238 trunclbn = (length + blksize - 1) / blksize;
1240 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1247 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1248 if (bp->b_lblkno < trunclbn)
1251 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1252 VI_MTX(vp)) == ENOLCK)
1256 bp->b_flags |= (B_INVAL | B_RELBUF);
1257 bp->b_flags &= ~B_ASYNC;
1262 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1263 (nbp->b_vp != vp) ||
1264 (nbp->b_flags & B_DELWRI))) {
1270 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1271 if (bp->b_lblkno < trunclbn)
1274 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1275 VI_MTX(vp)) == ENOLCK)
1278 bp->b_flags |= (B_INVAL | B_RELBUF);
1279 bp->b_flags &= ~B_ASYNC;
1283 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1284 (nbp->b_vp != vp) ||
1285 (nbp->b_flags & B_DELWRI) == 0)) {
1294 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1295 if (bp->b_lblkno > 0)
1298 * Since we hold the vnode lock this should only
1299 * fail if we're racing with the buf daemon.
1302 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1303 VI_MTX(vp)) == ENOLCK) {
1306 VNASSERT((bp->b_flags & B_DELWRI), vp,
1307 ("buf(%p) on dirty queue without DELWRI", bp));
1316 bufobj_wwait(bo, 0, 0);
1318 vnode_pager_setsize(vp, length);
1324 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1327 * NOTE: We have to deal with the special case of a background bitmap
1328 * buffer, a situation where two buffers will have the same logical
1329 * block offset. We want (1) only the foreground buffer to be accessed
1330 * in a lookup and (2) must differentiate between the foreground and
1331 * background buffer in the splay tree algorithm because the splay
1332 * tree cannot normally handle multiple entities with the same 'index'.
1333 * We accomplish this by adding differentiating flags to the splay tree's
1338 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1341 struct buf *lefttreemax, *righttreemin, *y;
1345 lefttreemax = righttreemin = &dummy;
1347 if (lblkno < root->b_lblkno ||
1348 (lblkno == root->b_lblkno &&
1349 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1350 if ((y = root->b_left) == NULL)
1352 if (lblkno < y->b_lblkno) {
1354 root->b_left = y->b_right;
1357 if ((y = root->b_left) == NULL)
1360 /* Link into the new root's right tree. */
1361 righttreemin->b_left = root;
1362 righttreemin = root;
1363 } else if (lblkno > root->b_lblkno ||
1364 (lblkno == root->b_lblkno &&
1365 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1366 if ((y = root->b_right) == NULL)
1368 if (lblkno > y->b_lblkno) {
1370 root->b_right = y->b_left;
1373 if ((y = root->b_right) == NULL)
1376 /* Link into the new root's left tree. */
1377 lefttreemax->b_right = root;
1384 /* Assemble the new root. */
1385 lefttreemax->b_right = root->b_left;
1386 righttreemin->b_left = root->b_right;
1387 root->b_left = dummy.b_right;
1388 root->b_right = dummy.b_left;
1393 buf_vlist_remove(struct buf *bp)
1398 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1399 ASSERT_BO_LOCKED(bp->b_bufobj);
1400 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1401 (BX_VNDIRTY|BX_VNCLEAN),
1402 ("buf_vlist_remove: Buf %p is on two lists", bp));
1403 if (bp->b_xflags & BX_VNDIRTY)
1404 bv = &bp->b_bufobj->bo_dirty;
1406 bv = &bp->b_bufobj->bo_clean;
1407 if (bp != bv->bv_root) {
1408 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1409 KASSERT(root == bp, ("splay lookup failed in remove"));
1411 if (bp->b_left == NULL) {
1414 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1415 root->b_right = bp->b_right;
1418 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1420 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1424 * Add the buffer to the sorted clean or dirty block list using a
1425 * splay tree algorithm.
1427 * NOTE: xflags is passed as a constant, optimizing this inline function!
1430 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1435 ASSERT_BO_LOCKED(bo);
1436 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1437 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1438 bp->b_xflags |= xflags;
1439 if (xflags & BX_VNDIRTY)
1444 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1448 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1449 } else if (bp->b_lblkno < root->b_lblkno ||
1450 (bp->b_lblkno == root->b_lblkno &&
1451 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1452 bp->b_left = root->b_left;
1454 root->b_left = NULL;
1455 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1457 bp->b_right = root->b_right;
1459 root->b_right = NULL;
1460 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1467 * Lookup a buffer using the splay tree. Note that we specifically avoid
1468 * shadow buffers used in background bitmap writes.
1470 * This code isn't quite efficient as it could be because we are maintaining
1471 * two sorted lists and do not know which list the block resides in.
1473 * During a "make buildworld" the desired buffer is found at one of
1474 * the roots more than 60% of the time. Thus, checking both roots
1475 * before performing either splay eliminates unnecessary splays on the
1476 * first tree splayed.
1479 gbincore(struct bufobj *bo, daddr_t lblkno)
1483 ASSERT_BO_LOCKED(bo);
1484 if ((bp = bo->bo_clean.bv_root) != NULL &&
1485 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1487 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1488 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1490 if ((bp = bo->bo_clean.bv_root) != NULL) {
1491 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1492 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1495 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1496 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1497 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1504 * Associate a buffer with a vnode.
1507 bgetvp(struct vnode *vp, struct buf *bp)
1510 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1512 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1513 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1514 ("bgetvp: bp already attached! %p", bp));
1516 ASSERT_VI_LOCKED(vp, "bgetvp");
1518 if (VFS_NEEDSGIANT(vp->v_mount) ||
1519 vp->v_bufobj.bo_flag & BO_NEEDSGIANT)
1520 bp->b_flags |= B_NEEDSGIANT;
1522 bp->b_bufobj = &vp->v_bufobj;
1524 * Insert onto list for new vnode.
1526 buf_vlist_add(bp, &vp->v_bufobj, BX_VNCLEAN);
1530 * Disassociate a buffer from a vnode.
1533 brelvp(struct buf *bp)
1538 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1539 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1542 * Delete from old vnode list, if on one.
1544 vp = bp->b_vp; /* XXX */
1547 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1548 buf_vlist_remove(bp);
1550 panic("brelvp: Buffer %p not on queue.", bp);
1551 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1552 bo->bo_flag &= ~BO_ONWORKLST;
1553 mtx_lock(&sync_mtx);
1554 LIST_REMOVE(bo, bo_synclist);
1555 syncer_worklist_len--;
1556 mtx_unlock(&sync_mtx);
1558 bp->b_flags &= ~B_NEEDSGIANT;
1560 bp->b_bufobj = NULL;
1565 * Add an item to the syncer work queue.
1568 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1572 ASSERT_BO_LOCKED(bo);
1574 mtx_lock(&sync_mtx);
1575 if (bo->bo_flag & BO_ONWORKLST)
1576 LIST_REMOVE(bo, bo_synclist);
1578 bo->bo_flag |= BO_ONWORKLST;
1579 syncer_worklist_len++;
1582 if (delay > syncer_maxdelay - 2)
1583 delay = syncer_maxdelay - 2;
1584 slot = (syncer_delayno + delay) & syncer_mask;
1586 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
1587 mtx_unlock(&sync_mtx);
1591 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1595 mtx_lock(&sync_mtx);
1596 len = syncer_worklist_len - sync_vnode_count;
1597 mtx_unlock(&sync_mtx);
1598 error = SYSCTL_OUT(req, &len, sizeof(len));
1602 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1603 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1605 static struct proc *updateproc;
1606 static void sched_sync(void);
1607 static struct kproc_desc up_kp = {
1612 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1615 sync_vnode(struct bufobj *bo, struct thread *td)
1620 vp = bo->__bo_vnode; /* XXX */
1621 if (VOP_ISLOCKED(vp, NULL) != 0)
1623 if (VI_TRYLOCK(vp) == 0)
1626 * We use vhold in case the vnode does not
1627 * successfully sync. vhold prevents the vnode from
1628 * going away when we unlock the sync_mtx so that
1629 * we can acquire the vnode interlock.
1632 mtx_unlock(&sync_mtx);
1634 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1636 mtx_lock(&sync_mtx);
1639 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1640 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1641 VOP_UNLOCK(vp, 0, td);
1642 vn_finished_write(mp);
1644 if ((bo->bo_flag & BO_ONWORKLST) != 0) {
1646 * Put us back on the worklist. The worklist
1647 * routine will remove us from our current
1648 * position and then add us back in at a later
1651 vn_syncer_add_to_worklist(bo, syncdelay);
1654 mtx_lock(&sync_mtx);
1659 * System filesystem synchronizer daemon.
1664 struct synclist *next;
1665 struct synclist *slp;
1668 struct thread *td = FIRST_THREAD_IN_PROC(updateproc);
1669 static int dummychan;
1671 int net_worklist_len;
1672 int syncer_final_iter;
1678 syncer_final_iter = 0;
1680 syncer_state = SYNCER_RUNNING;
1681 starttime = time_uptime;
1682 td->td_pflags |= TDP_NORUNNINGBUF;
1684 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1687 mtx_lock(&sync_mtx);
1689 if (syncer_state == SYNCER_FINAL_DELAY &&
1690 syncer_final_iter == 0) {
1691 mtx_unlock(&sync_mtx);
1692 kthread_suspend_check(td->td_proc);
1693 mtx_lock(&sync_mtx);
1695 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1696 if (syncer_state != SYNCER_RUNNING &&
1697 starttime != time_uptime) {
1699 printf("\nSyncing disks, vnodes remaining...");
1702 printf("%d ", net_worklist_len);
1704 starttime = time_uptime;
1707 * Push files whose dirty time has expired. Be careful
1708 * of interrupt race on slp queue.
1710 * Skip over empty worklist slots when shutting down.
1713 slp = &syncer_workitem_pending[syncer_delayno];
1714 syncer_delayno += 1;
1715 if (syncer_delayno == syncer_maxdelay)
1717 next = &syncer_workitem_pending[syncer_delayno];
1719 * If the worklist has wrapped since the
1720 * it was emptied of all but syncer vnodes,
1721 * switch to the FINAL_DELAY state and run
1722 * for one more second.
1724 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1725 net_worklist_len == 0 &&
1726 last_work_seen == syncer_delayno) {
1727 syncer_state = SYNCER_FINAL_DELAY;
1728 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1730 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1731 syncer_worklist_len > 0);
1734 * Keep track of the last time there was anything
1735 * on the worklist other than syncer vnodes.
1736 * Return to the SHUTTING_DOWN state if any
1739 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1740 last_work_seen = syncer_delayno;
1741 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1742 syncer_state = SYNCER_SHUTTING_DOWN;
1743 while ((bo = LIST_FIRST(slp)) != NULL) {
1744 error = sync_vnode(bo, td);
1746 LIST_REMOVE(bo, bo_synclist);
1747 LIST_INSERT_HEAD(next, bo, bo_synclist);
1751 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1752 syncer_final_iter--;
1754 * The variable rushjob allows the kernel to speed up the
1755 * processing of the filesystem syncer process. A rushjob
1756 * value of N tells the filesystem syncer to process the next
1757 * N seconds worth of work on its queue ASAP. Currently rushjob
1758 * is used by the soft update code to speed up the filesystem
1759 * syncer process when the incore state is getting so far
1760 * ahead of the disk that the kernel memory pool is being
1761 * threatened with exhaustion.
1768 * Just sleep for a short period of time between
1769 * iterations when shutting down to allow some I/O
1772 * If it has taken us less than a second to process the
1773 * current work, then wait. Otherwise start right over
1774 * again. We can still lose time if any single round
1775 * takes more than two seconds, but it does not really
1776 * matter as we are just trying to generally pace the
1777 * filesystem activity.
1779 if (syncer_state != SYNCER_RUNNING)
1780 msleep(&dummychan, &sync_mtx, PPAUSE, "syncfnl",
1781 hz / SYNCER_SHUTDOWN_SPEEDUP);
1782 else if (time_uptime == starttime)
1783 msleep(&lbolt, &sync_mtx, PPAUSE, "syncer", 0);
1788 * Request the syncer daemon to speed up its work.
1789 * We never push it to speed up more than half of its
1790 * normal turn time, otherwise it could take over the cpu.
1793 speedup_syncer(void)
1798 td = FIRST_THREAD_IN_PROC(updateproc);
1799 sleepq_remove(td, &lbolt);
1800 mtx_lock(&sync_mtx);
1801 if (rushjob < syncdelay / 2) {
1803 stat_rush_requests += 1;
1806 mtx_unlock(&sync_mtx);
1811 * Tell the syncer to speed up its work and run though its work
1812 * list several times, then tell it to shut down.
1815 syncer_shutdown(void *arg, int howto)
1819 if (howto & RB_NOSYNC)
1821 td = FIRST_THREAD_IN_PROC(updateproc);
1822 sleepq_remove(td, &lbolt);
1823 mtx_lock(&sync_mtx);
1824 syncer_state = SYNCER_SHUTTING_DOWN;
1826 mtx_unlock(&sync_mtx);
1827 kproc_shutdown(arg, howto);
1831 * Reassign a buffer from one vnode to another.
1832 * Used to assign file specific control information
1833 * (indirect blocks) to the vnode to which they belong.
1836 reassignbuf(struct buf *bp)
1849 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
1850 bp, bp->b_vp, bp->b_flags);
1852 * B_PAGING flagged buffers cannot be reassigned because their vp
1853 * is not fully linked in.
1855 if (bp->b_flags & B_PAGING)
1856 panic("cannot reassign paging buffer");
1859 * Delete from old vnode list, if on one.
1862 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1863 buf_vlist_remove(bp);
1865 panic("reassignbuf: Buffer %p not on queue.", bp);
1867 * If dirty, put on list of dirty buffers; otherwise insert onto list
1870 if (bp->b_flags & B_DELWRI) {
1871 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
1872 switch (vp->v_type) {
1882 vn_syncer_add_to_worklist(bo, delay);
1884 buf_vlist_add(bp, bo, BX_VNDIRTY);
1886 buf_vlist_add(bp, bo, BX_VNCLEAN);
1888 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1889 mtx_lock(&sync_mtx);
1890 LIST_REMOVE(bo, bo_synclist);
1891 syncer_worklist_len--;
1892 mtx_unlock(&sync_mtx);
1893 bo->bo_flag &= ~BO_ONWORKLST;
1898 bp = TAILQ_FIRST(&bv->bv_hd);
1899 KASSERT(bp == NULL || bp->b_bufobj == bo,
1900 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1901 bp = TAILQ_LAST(&bv->bv_hd, buflists);
1902 KASSERT(bp == NULL || bp->b_bufobj == bo,
1903 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1905 bp = TAILQ_FIRST(&bv->bv_hd);
1906 KASSERT(bp == NULL || bp->b_bufobj == bo,
1907 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1908 bp = TAILQ_LAST(&bv->bv_hd, buflists);
1909 KASSERT(bp == NULL || bp->b_bufobj == bo,
1910 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1916 * Increment the use and hold counts on the vnode, taking care to reference
1917 * the driver's usecount if this is a chardev. The vholdl() will remove
1918 * the vnode from the free list if it is presently free. Requires the
1919 * vnode interlock and returns with it held.
1922 v_incr_usecount(struct vnode *vp)
1925 CTR3(KTR_VFS, "v_incr_usecount: vp %p holdcnt %d usecount %d\n",
1926 vp, vp->v_holdcnt, vp->v_usecount);
1928 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1930 vp->v_rdev->si_usecount++;
1937 * Turn a holdcnt into a use+holdcnt such that only one call to
1938 * v_decr_usecount is needed.
1941 v_upgrade_usecount(struct vnode *vp)
1944 CTR3(KTR_VFS, "v_upgrade_usecount: vp %p holdcnt %d usecount %d\n",
1945 vp, vp->v_holdcnt, vp->v_usecount);
1947 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1949 vp->v_rdev->si_usecount++;
1955 * Decrement the vnode use and hold count along with the driver's usecount
1956 * if this is a chardev. The vdropl() below releases the vnode interlock
1957 * as it may free the vnode.
1960 v_decr_usecount(struct vnode *vp)
1963 CTR3(KTR_VFS, "v_decr_usecount: vp %p holdcnt %d usecount %d\n",
1964 vp, vp->v_holdcnt, vp->v_usecount);
1965 ASSERT_VI_LOCKED(vp, __FUNCTION__);
1966 VNASSERT(vp->v_usecount > 0, vp,
1967 ("v_decr_usecount: negative usecount"));
1969 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1971 vp->v_rdev->si_usecount--;
1978 * Decrement only the use count and driver use count. This is intended to
1979 * be paired with a follow on vdropl() to release the remaining hold count.
1980 * In this way we may vgone() a vnode with a 0 usecount without risk of
1981 * having it end up on a free list because the hold count is kept above 0.
1984 v_decr_useonly(struct vnode *vp)
1987 CTR3(KTR_VFS, "v_decr_useonly: vp %p holdcnt %d usecount %d\n",
1988 vp, vp->v_holdcnt, vp->v_usecount);
1989 ASSERT_VI_LOCKED(vp, __FUNCTION__);
1990 VNASSERT(vp->v_usecount > 0, vp,
1991 ("v_decr_useonly: negative usecount"));
1993 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1995 vp->v_rdev->si_usecount--;
2001 * Grab a particular vnode from the free list, increment its
2002 * reference count and lock it. The vnode lock bit is set if the
2003 * vnode is being eliminated in vgone. The process is awakened
2004 * when the transition is completed, and an error returned to
2005 * indicate that the vnode is no longer usable (possibly having
2006 * been changed to a new filesystem type).
2009 vget(struct vnode *vp, int flags, struct thread *td)
2018 VFS_ASSERT_GIANT(vp->v_mount);
2019 if ((flags & LK_INTERLOCK) == 0)
2022 * If the inactive call was deferred because vput() was called
2023 * with a shared lock, we have to do it here before another thread
2024 * gets a reference to data that should be dead.
2026 if (vp->v_iflag & VI_OWEINACT) {
2027 if (flags & LK_NOWAIT) {
2031 flags &= ~LK_TYPE_MASK;
2032 flags |= LK_EXCLUSIVE;
2036 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) {
2041 /* Upgrade our holdcnt to a usecount. */
2042 v_upgrade_usecount(vp);
2043 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
2044 panic("vget: vn_lock failed to return ENOENT\n");
2046 if (vp->v_iflag & VI_OWEINACT)
2049 if ((oldflags & LK_TYPE_MASK) == 0)
2050 VOP_UNLOCK(vp, 0, td);
2057 * Increase the reference count of a vnode.
2060 vref(struct vnode *vp)
2064 v_incr_usecount(vp);
2069 * Return reference count of a vnode.
2071 * The results of this call are only guaranteed when some mechanism other
2072 * than the VI lock is used to stop other processes from gaining references
2073 * to the vnode. This may be the case if the caller holds the only reference.
2074 * This is also useful when stale data is acceptable as race conditions may
2075 * be accounted for by some other means.
2078 vrefcnt(struct vnode *vp)
2083 usecnt = vp->v_usecount;
2091 * Vnode put/release.
2092 * If count drops to zero, call inactive routine and return to freelist.
2095 vrele(struct vnode *vp)
2097 struct thread *td = curthread; /* XXX */
2099 KASSERT(vp != NULL, ("vrele: null vp"));
2100 VFS_ASSERT_GIANT(vp->v_mount);
2104 /* Skip this v_writecount check if we're going to panic below. */
2105 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2106 ("vrele: missed vn_close"));
2108 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2109 vp->v_usecount == 1)) {
2110 v_decr_usecount(vp);
2113 if (vp->v_usecount != 1) {
2115 vprint("vrele: negative ref count", vp);
2118 panic("vrele: negative ref cnt");
2121 * We want to hold the vnode until the inactive finishes to
2122 * prevent vgone() races. We drop the use count here and the
2123 * hold count below when we're done.
2127 * We must call VOP_INACTIVE with the node locked. Mark
2128 * as VI_DOINGINACT to avoid recursion.
2130 vp->v_iflag |= VI_OWEINACT;
2131 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) {
2133 if (vp->v_usecount > 0)
2134 vp->v_iflag &= ~VI_OWEINACT;
2135 if (vp->v_iflag & VI_OWEINACT)
2137 VOP_UNLOCK(vp, 0, td);
2140 if (vp->v_usecount > 0)
2141 vp->v_iflag &= ~VI_OWEINACT;
2147 * Release an already locked vnode. This give the same effects as
2148 * unlock+vrele(), but takes less time and avoids releasing and
2149 * re-aquiring the lock (as vrele() aquires the lock internally.)
2152 vput(struct vnode *vp)
2154 struct thread *td = curthread; /* XXX */
2157 KASSERT(vp != NULL, ("vput: null vp"));
2158 ASSERT_VOP_LOCKED(vp, "vput");
2159 VFS_ASSERT_GIANT(vp->v_mount);
2161 /* Skip this v_writecount check if we're going to panic below. */
2162 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2163 ("vput: missed vn_close"));
2166 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2167 vp->v_usecount == 1)) {
2168 VOP_UNLOCK(vp, 0, td);
2169 v_decr_usecount(vp);
2173 if (vp->v_usecount != 1) {
2175 vprint("vput: negative ref count", vp);
2177 panic("vput: negative ref cnt");
2180 * We want to hold the vnode until the inactive finishes to
2181 * prevent vgone() races. We drop the use count here and the
2182 * hold count below when we're done.
2185 vp->v_iflag |= VI_OWEINACT;
2186 if (VOP_ISLOCKED(vp, NULL) != LK_EXCLUSIVE) {
2187 error = VOP_LOCK(vp, LK_EXCLUPGRADE|LK_INTERLOCK|LK_NOWAIT, td);
2190 if (vp->v_usecount > 0)
2191 vp->v_iflag &= ~VI_OWEINACT;
2195 if (vp->v_usecount > 0)
2196 vp->v_iflag &= ~VI_OWEINACT;
2197 if (vp->v_iflag & VI_OWEINACT)
2199 VOP_UNLOCK(vp, 0, td);
2205 * Somebody doesn't want the vnode recycled.
2208 vhold(struct vnode *vp)
2217 vholdl(struct vnode *vp)
2221 if (VSHOULDBUSY(vp))
2226 * Note that there is one less who cares about this vnode. vdrop() is the
2227 * opposite of vhold().
2230 vdrop(struct vnode *vp)
2238 * Drop the hold count of the vnode. If this is the last reference to
2239 * the vnode we will free it if it has been vgone'd otherwise it is
2240 * placed on the free list.
2243 vdropl(struct vnode *vp)
2246 ASSERT_VI_LOCKED(vp, "vdropl");
2247 if (vp->v_holdcnt <= 0)
2248 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2250 if (vp->v_holdcnt == 0) {
2251 if (vp->v_iflag & VI_DOOMED) {
2261 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2262 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2263 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2264 * failed lock upgrade.
2267 vinactive(struct vnode *vp, struct thread *td)
2270 ASSERT_VOP_LOCKED(vp, "vinactive");
2271 ASSERT_VI_LOCKED(vp, "vinactive");
2272 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2273 ("vinactive: recursed on VI_DOINGINACT"));
2274 vp->v_iflag |= VI_DOINGINACT;
2275 vp->v_iflag &= ~VI_OWEINACT;
2277 VOP_INACTIVE(vp, td);
2279 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2280 ("vinactive: lost VI_DOINGINACT"));
2281 vp->v_iflag &= ~VI_DOINGINACT;
2285 * Remove any vnodes in the vnode table belonging to mount point mp.
2287 * If FORCECLOSE is not specified, there should not be any active ones,
2288 * return error if any are found (nb: this is a user error, not a
2289 * system error). If FORCECLOSE is specified, detach any active vnodes
2292 * If WRITECLOSE is set, only flush out regular file vnodes open for
2295 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2297 * `rootrefs' specifies the base reference count for the root vnode
2298 * of this filesystem. The root vnode is considered busy if its
2299 * v_usecount exceeds this value. On a successful return, vflush(, td)
2300 * will call vrele() on the root vnode exactly rootrefs times.
2301 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2305 static int busyprt = 0; /* print out busy vnodes */
2306 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
2310 vflush( struct mount *mp, int rootrefs, int flags, struct thread *td)
2312 struct vnode *vp, *mvp, *rootvp = NULL;
2314 int busy = 0, error;
2316 CTR1(KTR_VFS, "vflush: mp %p", mp);
2318 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2319 ("vflush: bad args"));
2321 * Get the filesystem root vnode. We can vput() it
2322 * immediately, since with rootrefs > 0, it won't go away.
2324 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp, td)) != 0)
2331 MNT_VNODE_FOREACH(vp, mp, mvp) {
2336 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td);
2340 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2344 * Skip over a vnodes marked VV_SYSTEM.
2346 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2347 VOP_UNLOCK(vp, 0, td);
2353 * If WRITECLOSE is set, flush out unlinked but still open
2354 * files (even if open only for reading) and regular file
2355 * vnodes open for writing.
2357 if (flags & WRITECLOSE) {
2358 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
2361 if ((vp->v_type == VNON ||
2362 (error == 0 && vattr.va_nlink > 0)) &&
2363 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2364 VOP_UNLOCK(vp, 0, td);
2372 * With v_usecount == 0, all we need to do is clear out the
2373 * vnode data structures and we are done.
2375 * If FORCECLOSE is set, forcibly close the vnode.
2377 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2378 VNASSERT(vp->v_usecount == 0 ||
2379 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2380 ("device VNODE %p is FORCECLOSED", vp));
2386 vprint("vflush: busy vnode", vp);
2389 VOP_UNLOCK(vp, 0, td);
2394 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2396 * If just the root vnode is busy, and if its refcount
2397 * is equal to `rootrefs', then go ahead and kill it.
2400 KASSERT(busy > 0, ("vflush: not busy"));
2401 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2402 ("vflush: usecount %d < rootrefs %d",
2403 rootvp->v_usecount, rootrefs));
2404 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2405 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK, td);
2407 VOP_UNLOCK(rootvp, 0, td);
2414 for (; rootrefs > 0; rootrefs--)
2420 * Recycle an unused vnode to the front of the free list.
2423 vrecycle(struct vnode *vp, struct thread *td)
2427 ASSERT_VOP_LOCKED(vp, "vrecycle");
2430 if (vp->v_usecount == 0) {
2439 * Eliminate all activity associated with a vnode
2440 * in preparation for reuse.
2443 vgone(struct vnode *vp)
2451 * vgone, with the vp interlock held.
2454 vgonel(struct vnode *vp)
2461 CTR1(KTR_VFS, "vgonel: vp %p", vp);
2462 ASSERT_VOP_LOCKED(vp, "vgonel");
2463 ASSERT_VI_LOCKED(vp, "vgonel");
2464 VNASSERT(vp->v_holdcnt, vp,
2465 ("vgonel: vp %p has no reference.", vp));
2469 * Don't vgonel if we're already doomed.
2471 if (vp->v_iflag & VI_DOOMED)
2473 vp->v_iflag |= VI_DOOMED;
2475 * Check to see if the vnode is in use. If so, we have to call
2476 * VOP_CLOSE() and VOP_INACTIVE().
2478 active = vp->v_usecount;
2479 oweinact = (vp->v_iflag & VI_OWEINACT);
2482 * Clean out any buffers associated with the vnode.
2483 * If the flush fails, just toss the buffers.
2486 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2487 (void) vn_start_secondary_write(vp, &mp, V_WAIT);
2488 if (vinvalbuf(vp, V_SAVE, td, 0, 0) != 0)
2489 vinvalbuf(vp, 0, td, 0, 0);
2492 * If purging an active vnode, it must be closed and
2493 * deactivated before being reclaimed.
2496 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2497 if (oweinact || active) {
2499 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2504 * Reclaim the vnode.
2506 if (VOP_RECLAIM(vp, td))
2507 panic("vgone: cannot reclaim");
2509 vn_finished_secondary_write(mp);
2510 VNASSERT(vp->v_object == NULL, vp,
2511 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2513 * Delete from old mount point vnode list.
2518 * Done with purge, reset to the standard lock and invalidate
2522 vp->v_vnlock = &vp->v_lock;
2523 vp->v_op = &dead_vnodeops;
2529 * Calculate the total number of references to a special device.
2532 vcount(struct vnode *vp)
2537 count = vp->v_rdev->si_usecount;
2543 * Same as above, but using the struct cdev *as argument
2546 count_dev(struct cdev *dev)
2551 count = dev->si_usecount;
2557 * Print out a description of a vnode.
2559 static char *typename[] =
2560 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD",
2564 vn_printf(struct vnode *vp, const char *fmt, ...)
2572 printf("%p: ", (void *)vp);
2573 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2574 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2575 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2578 if (vp->v_vflag & VV_ROOT)
2579 strcat(buf, "|VV_ROOT");
2580 if (vp->v_vflag & VV_TEXT)
2581 strcat(buf, "|VV_TEXT");
2582 if (vp->v_vflag & VV_SYSTEM)
2583 strcat(buf, "|VV_SYSTEM");
2584 if (vp->v_vflag & VV_DELETED)
2585 strcat(buf, "|VV_DELETED");
2586 if (vp->v_iflag & VI_DOOMED)
2587 strcat(buf, "|VI_DOOMED");
2588 if (vp->v_iflag & VI_FREE)
2589 strcat(buf, "|VI_FREE");
2590 printf(" flags (%s)\n", buf + 1);
2591 if (mtx_owned(VI_MTX(vp)))
2592 printf(" VI_LOCKed");
2593 if (vp->v_object != NULL)
2594 printf(" v_object %p ref %d pages %d\n",
2595 vp->v_object, vp->v_object->ref_count,
2596 vp->v_object->resident_page_count);
2598 lockmgr_printinfo(vp->v_vnlock);
2600 if (vp->v_data != NULL)
2606 * List all of the locked vnodes in the system.
2607 * Called when debugging the kernel.
2609 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2611 struct mount *mp, *nmp;
2615 * Note: because this is DDB, we can't obey the locking semantics
2616 * for these structures, which means we could catch an inconsistent
2617 * state and dereference a nasty pointer. Not much to be done
2620 printf("Locked vnodes\n");
2621 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2622 nmp = TAILQ_NEXT(mp, mnt_list);
2623 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2624 if (vp->v_type != VMARKER && VOP_ISLOCKED(vp, NULL))
2627 nmp = TAILQ_NEXT(mp, mnt_list);
2632 * Show details about the given vnode.
2634 DB_SHOW_COMMAND(vnode, db_show_vnode)
2640 vp = (struct vnode *)addr;
2641 vn_printf(vp, "vnode ");
2646 * Fill in a struct xvfsconf based on a struct vfsconf.
2649 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
2652 strcpy(xvfsp->vfc_name, vfsp->vfc_name);
2653 xvfsp->vfc_typenum = vfsp->vfc_typenum;
2654 xvfsp->vfc_refcount = vfsp->vfc_refcount;
2655 xvfsp->vfc_flags = vfsp->vfc_flags;
2657 * These are unused in userland, we keep them
2658 * to not break binary compatibility.
2660 xvfsp->vfc_vfsops = NULL;
2661 xvfsp->vfc_next = NULL;
2665 * Top level filesystem related information gathering.
2668 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
2670 struct vfsconf *vfsp;
2671 struct xvfsconf xvfsp;
2675 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
2676 bzero(&xvfsp, sizeof(xvfsp));
2677 vfsconf2x(vfsp, &xvfsp);
2678 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp);
2685 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
2686 "S,xvfsconf", "List of all configured filesystems");
2688 #ifndef BURN_BRIDGES
2689 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
2692 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2694 int *name = (int *)arg1 - 1; /* XXX */
2695 u_int namelen = arg2 + 1; /* XXX */
2696 struct vfsconf *vfsp;
2697 struct xvfsconf xvfsp;
2699 printf("WARNING: userland calling deprecated sysctl, "
2700 "please rebuild world\n");
2702 #if 1 || defined(COMPAT_PRELITE2)
2703 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2705 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2709 case VFS_MAXTYPENUM:
2712 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2715 return (ENOTDIR); /* overloaded */
2716 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
2717 if (vfsp->vfc_typenum == name[2])
2720 return (EOPNOTSUPP);
2721 bzero(&xvfsp, sizeof(xvfsp));
2722 vfsconf2x(vfsp, &xvfsp);
2723 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
2725 return (EOPNOTSUPP);
2728 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
2729 vfs_sysctl, "Generic filesystem");
2731 #if 1 || defined(COMPAT_PRELITE2)
2734 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2737 struct vfsconf *vfsp;
2738 struct ovfsconf ovfs;
2740 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
2741 bzero(&ovfs, sizeof(ovfs));
2742 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2743 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2744 ovfs.vfc_index = vfsp->vfc_typenum;
2745 ovfs.vfc_refcount = vfsp->vfc_refcount;
2746 ovfs.vfc_flags = vfsp->vfc_flags;
2747 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2754 #endif /* 1 || COMPAT_PRELITE2 */
2755 #endif /* !BURN_BRIDGES */
2757 #define KINFO_VNODESLOP 10
2760 * Dump vnode list (via sysctl).
2764 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2767 struct thread *td = req->td;
2773 * Stale numvnodes access is not fatal here.
2776 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
2778 /* Make an estimate */
2779 return (SYSCTL_OUT(req, 0, len));
2781 error = sysctl_wire_old_buffer(req, 0);
2784 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
2786 mtx_lock(&mountlist_mtx);
2787 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2788 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td))
2791 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2795 xvn[n].xv_size = sizeof *xvn;
2796 xvn[n].xv_vnode = vp;
2797 xvn[n].xv_id = 0; /* XXX compat */
2798 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
2800 XV_COPY(writecount);
2806 xvn[n].xv_flag = vp->v_vflag;
2808 switch (vp->v_type) {
2815 if (vp->v_rdev == NULL) {
2819 xvn[n].xv_dev = dev2udev(vp->v_rdev);
2822 xvn[n].xv_socket = vp->v_socket;
2825 xvn[n].xv_fifo = vp->v_fifoinfo;
2830 /* shouldn't happen? */
2838 mtx_lock(&mountlist_mtx);
2843 mtx_unlock(&mountlist_mtx);
2845 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
2850 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2851 0, 0, sysctl_vnode, "S,xvnode", "");
2855 * Unmount all filesystems. The list is traversed in reverse order
2856 * of mounting to avoid dependencies.
2859 vfs_unmountall(void)
2865 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
2868 * Since this only runs when rebooting, it is not interlocked.
2870 while(!TAILQ_EMPTY(&mountlist)) {
2871 mp = TAILQ_LAST(&mountlist, mntlist);
2872 error = dounmount(mp, MNT_FORCE, td);
2874 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2876 * XXX: Due to the way in which we mount the root
2877 * file system off of devfs, devfs will generate a
2878 * "busy" warning when we try to unmount it before
2879 * the root. Don't print a warning as a result in
2880 * order to avoid false positive errors that may
2881 * cause needless upset.
2883 if (strcmp(mp->mnt_vfc->vfc_name, "devfs") != 0) {
2884 printf("unmount of %s failed (",
2885 mp->mnt_stat.f_mntonname);
2889 printf("%d)\n", error);
2892 /* The unmount has removed mp from the mountlist */
2898 * perform msync on all vnodes under a mount point
2899 * the mount point must be locked.
2902 vfs_msync(struct mount *mp, int flags)
2904 struct vnode *vp, *mvp;
2905 struct vm_object *obj;
2908 MNT_VNODE_FOREACH(vp, mp, mvp) {
2910 if ((vp->v_iflag & VI_OBJDIRTY) &&
2911 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2914 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
2916 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
2924 VM_OBJECT_LOCK(obj);
2925 vm_object_page_clean(obj, 0, 0,
2927 OBJPC_SYNC : OBJPC_NOSYNC);
2928 VM_OBJECT_UNLOCK(obj);
2940 * Mark a vnode as free, putting it up for recycling.
2943 vfree(struct vnode *vp)
2946 CTR1(KTR_VFS, "vfree vp %p", vp);
2947 ASSERT_VI_LOCKED(vp, "vfree");
2948 mtx_lock(&vnode_free_list_mtx);
2949 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
2950 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
2951 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
2952 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
2953 ("vfree: Freeing doomed vnode"));
2954 if (vp->v_iflag & VI_AGE) {
2955 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2957 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2960 vp->v_iflag &= ~VI_AGE;
2961 vp->v_iflag |= VI_FREE;
2962 mtx_unlock(&vnode_free_list_mtx);
2966 * Opposite of vfree() - mark a vnode as in use.
2969 vbusy(struct vnode *vp)
2971 CTR1(KTR_VFS, "vbusy vp %p", vp);
2972 ASSERT_VI_LOCKED(vp, "vbusy");
2973 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
2974 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
2976 mtx_lock(&vnode_free_list_mtx);
2977 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2979 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2980 mtx_unlock(&vnode_free_list_mtx);
2984 * Initalize per-vnode helper structure to hold poll-related state.
2987 v_addpollinfo(struct vnode *vp)
2989 struct vpollinfo *vi;
2991 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
2992 if (vp->v_pollinfo != NULL) {
2993 uma_zfree(vnodepoll_zone, vi);
2996 vp->v_pollinfo = vi;
2997 mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
2998 knlist_init(&vp->v_pollinfo->vpi_selinfo.si_note, vp, vfs_knllock,
2999 vfs_knlunlock, vfs_knllocked);
3003 * Record a process's interest in events which might happen to
3004 * a vnode. Because poll uses the historic select-style interface
3005 * internally, this routine serves as both the ``check for any
3006 * pending events'' and the ``record my interest in future events''
3007 * functions. (These are done together, while the lock is held,
3008 * to avoid race conditions.)
3011 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
3014 if (vp->v_pollinfo == NULL)
3016 mtx_lock(&vp->v_pollinfo->vpi_lock);
3017 if (vp->v_pollinfo->vpi_revents & events) {
3019 * This leaves events we are not interested
3020 * in available for the other process which
3021 * which presumably had requested them
3022 * (otherwise they would never have been
3025 events &= vp->v_pollinfo->vpi_revents;
3026 vp->v_pollinfo->vpi_revents &= ~events;
3028 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3031 vp->v_pollinfo->vpi_events |= events;
3032 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
3033 mtx_unlock(&vp->v_pollinfo->vpi_lock);
3038 * Routine to create and manage a filesystem syncer vnode.
3040 #define sync_close ((int (*)(struct vop_close_args *))nullop)
3041 static int sync_fsync(struct vop_fsync_args *);
3042 static int sync_inactive(struct vop_inactive_args *);
3043 static int sync_reclaim(struct vop_reclaim_args *);
3045 static struct vop_vector sync_vnodeops = {
3046 .vop_bypass = VOP_EOPNOTSUPP,
3047 .vop_close = sync_close, /* close */
3048 .vop_fsync = sync_fsync, /* fsync */
3049 .vop_inactive = sync_inactive, /* inactive */
3050 .vop_reclaim = sync_reclaim, /* reclaim */
3051 .vop_lock1 = vop_stdlock, /* lock */
3052 .vop_unlock = vop_stdunlock, /* unlock */
3053 .vop_islocked = vop_stdislocked, /* islocked */
3057 * Create a new filesystem syncer vnode for the specified mount point.
3060 vfs_allocate_syncvnode(struct mount *mp)
3063 static long start, incr, next;
3066 /* Allocate a new vnode */
3067 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) {
3068 mp->mnt_syncer = NULL;
3072 error = insmntque(vp, mp);
3074 panic("vfs_allocate_syncvnode: insmntque failed");
3076 * Place the vnode onto the syncer worklist. We attempt to
3077 * scatter them about on the list so that they will go off
3078 * at evenly distributed times even if all the filesystems
3079 * are mounted at once.
3082 if (next == 0 || next > syncer_maxdelay) {
3086 start = syncer_maxdelay / 2;
3087 incr = syncer_maxdelay;
3092 vn_syncer_add_to_worklist(&vp->v_bufobj,
3093 syncdelay > 0 ? next % syncdelay : 0);
3094 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
3095 mtx_lock(&sync_mtx);
3097 mtx_unlock(&sync_mtx);
3099 mp->mnt_syncer = vp;
3104 * Do a lazy sync of the filesystem.
3107 sync_fsync(struct vop_fsync_args *ap)
3109 struct vnode *syncvp = ap->a_vp;
3110 struct mount *mp = syncvp->v_mount;
3111 struct thread *td = ap->a_td;
3116 * We only need to do something if this is a lazy evaluation.
3118 if (ap->a_waitfor != MNT_LAZY)
3122 * Move ourselves to the back of the sync list.
3124 bo = &syncvp->v_bufobj;
3126 vn_syncer_add_to_worklist(bo, syncdelay);
3130 * Walk the list of vnodes pushing all that are dirty and
3131 * not already on the sync list.
3133 mtx_lock(&mountlist_mtx);
3134 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) {
3135 mtx_unlock(&mountlist_mtx);
3138 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3144 mp->mnt_kern_flag &= ~MNTK_ASYNC;
3146 vfs_msync(mp, MNT_NOWAIT);
3147 error = VFS_SYNC(mp, MNT_LAZY, td);
3150 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0)
3151 mp->mnt_kern_flag |= MNTK_ASYNC;
3153 vn_finished_write(mp);
3159 * The syncer vnode is no referenced.
3162 sync_inactive(struct vop_inactive_args *ap)
3170 * The syncer vnode is no longer needed and is being decommissioned.
3172 * Modifications to the worklist must be protected by sync_mtx.
3175 sync_reclaim(struct vop_reclaim_args *ap)
3177 struct vnode *vp = ap->a_vp;
3182 vp->v_mount->mnt_syncer = NULL;
3183 if (bo->bo_flag & BO_ONWORKLST) {
3184 mtx_lock(&sync_mtx);
3185 LIST_REMOVE(bo, bo_synclist);
3186 syncer_worklist_len--;
3188 mtx_unlock(&sync_mtx);
3189 bo->bo_flag &= ~BO_ONWORKLST;
3197 * Check if vnode represents a disk device
3200 vn_isdisk(struct vnode *vp, int *errp)
3206 if (vp->v_type != VCHR)
3208 else if (vp->v_rdev == NULL)
3210 else if (vp->v_rdev->si_devsw == NULL)
3212 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3217 return (error == 0);
3221 * Common filesystem object access control check routine. Accepts a
3222 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3223 * and optional call-by-reference privused argument allowing vaccess()
3224 * to indicate to the caller whether privilege was used to satisfy the
3225 * request (obsoleted). Returns 0 on success, or an errno on failure.
3227 * The ifdef'd CAPABILITIES version is here for reference, but is not
3231 vaccess(enum vtype type, mode_t file_mode, uid_t file_uid, gid_t file_gid,
3232 mode_t acc_mode, struct ucred *cred, int *privused)
3235 mode_t priv_granted;
3238 * Look for a normal, non-privileged way to access the file/directory
3239 * as requested. If it exists, go with that.
3242 if (privused != NULL)
3247 /* Check the owner. */
3248 if (cred->cr_uid == file_uid) {
3249 dac_granted |= VADMIN;
3250 if (file_mode & S_IXUSR)
3251 dac_granted |= VEXEC;
3252 if (file_mode & S_IRUSR)
3253 dac_granted |= VREAD;
3254 if (file_mode & S_IWUSR)
3255 dac_granted |= (VWRITE | VAPPEND);
3257 if ((acc_mode & dac_granted) == acc_mode)
3263 /* Otherwise, check the groups (first match) */
3264 if (groupmember(file_gid, cred)) {
3265 if (file_mode & S_IXGRP)
3266 dac_granted |= VEXEC;
3267 if (file_mode & S_IRGRP)
3268 dac_granted |= VREAD;
3269 if (file_mode & S_IWGRP)
3270 dac_granted |= (VWRITE | VAPPEND);
3272 if ((acc_mode & dac_granted) == acc_mode)
3278 /* Otherwise, check everyone else. */
3279 if (file_mode & S_IXOTH)
3280 dac_granted |= VEXEC;
3281 if (file_mode & S_IROTH)
3282 dac_granted |= VREAD;
3283 if (file_mode & S_IWOTH)
3284 dac_granted |= (VWRITE | VAPPEND);
3285 if ((acc_mode & dac_granted) == acc_mode)
3290 * Build a privilege mask to determine if the set of privileges
3291 * satisfies the requirements when combined with the granted mask
3292 * from above. For each privilege, if the privilege is required,
3293 * bitwise or the request type onto the priv_granted mask.
3299 * For directories, use PRIV_VFS_LOOKUP to satisfy VEXEC
3300 * requests, instead of PRIV_VFS_EXEC.
3302 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3303 !priv_check_cred(cred, PRIV_VFS_LOOKUP, SUSER_ALLOWJAIL))
3304 priv_granted |= VEXEC;
3306 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3307 !priv_check_cred(cred, PRIV_VFS_EXEC, SUSER_ALLOWJAIL))
3308 priv_granted |= VEXEC;
3311 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) &&
3312 !priv_check_cred(cred, PRIV_VFS_READ, SUSER_ALLOWJAIL))
3313 priv_granted |= VREAD;
3315 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3316 !priv_check_cred(cred, PRIV_VFS_WRITE, SUSER_ALLOWJAIL))
3317 priv_granted |= (VWRITE | VAPPEND);
3319 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3320 !priv_check_cred(cred, PRIV_VFS_ADMIN, SUSER_ALLOWJAIL))
3321 priv_granted |= VADMIN;
3323 if ((acc_mode & (priv_granted | dac_granted)) == acc_mode) {
3324 /* XXX audit: privilege used */
3325 if (privused != NULL)
3330 return ((acc_mode & VADMIN) ? EPERM : EACCES);
3334 * Credential check based on process requesting service, and per-attribute
3338 extattr_check_cred(struct vnode *vp, int attrnamespace, struct ucred *cred,
3339 struct thread *td, int access)
3343 * Kernel-invoked always succeeds.
3349 * Do not allow privileged processes in jail to directly manipulate
3350 * system attributes.
3352 switch (attrnamespace) {
3353 case EXTATTR_NAMESPACE_SYSTEM:
3354 /* Potentially should be: return (EPERM); */
3355 return (priv_check_cred(cred, PRIV_VFS_EXTATTR_SYSTEM, 0));
3356 case EXTATTR_NAMESPACE_USER:
3357 return (VOP_ACCESS(vp, access, cred, td));
3363 #ifdef DEBUG_VFS_LOCKS
3365 * This only exists to supress warnings from unlocked specfs accesses. It is
3366 * no longer ok to have an unlocked VFS.
3368 #define IGNORE_LOCK(vp) ((vp)->v_type == VCHR || (vp)->v_type == VBAD)
3370 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3371 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, "");
3373 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3374 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 0, "");
3376 int vfs_badlock_print = 1; /* Print lock violations. */
3377 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 0, "");
3380 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3381 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, &vfs_badlock_backtrace, 0, "");
3385 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3389 if (vfs_badlock_backtrace)
3392 if (vfs_badlock_print)
3393 printf("%s: %p %s\n", str, (void *)vp, msg);
3394 if (vfs_badlock_ddb)
3395 kdb_enter("lock violation");
3399 assert_vi_locked(struct vnode *vp, const char *str)
3402 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3403 vfs_badlock("interlock is not locked but should be", str, vp);
3407 assert_vi_unlocked(struct vnode *vp, const char *str)
3410 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3411 vfs_badlock("interlock is locked but should not be", str, vp);
3415 assert_vop_locked(struct vnode *vp, const char *str)
3418 if (vp && !IGNORE_LOCK(vp) && VOP_ISLOCKED(vp, NULL) == 0)
3419 vfs_badlock("is not locked but should be", str, vp);
3423 assert_vop_unlocked(struct vnode *vp, const char *str)
3426 if (vp && !IGNORE_LOCK(vp) &&
3427 VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE)
3428 vfs_badlock("is locked but should not be", str, vp);
3432 assert_vop_elocked(struct vnode *vp, const char *str)
3435 if (vp && !IGNORE_LOCK(vp) &&
3436 VOP_ISLOCKED(vp, curthread) != LK_EXCLUSIVE)
3437 vfs_badlock("is not exclusive locked but should be", str, vp);
3442 assert_vop_elocked_other(struct vnode *vp, const char *str)
3445 if (vp && !IGNORE_LOCK(vp) &&
3446 VOP_ISLOCKED(vp, curthread) != LK_EXCLOTHER)
3447 vfs_badlock("is not exclusive locked by another thread",
3452 assert_vop_slocked(struct vnode *vp, const char *str)
3455 if (vp && !IGNORE_LOCK(vp) &&
3456 VOP_ISLOCKED(vp, curthread) != LK_SHARED)
3457 vfs_badlock("is not locked shared but should be", str, vp);
3460 #endif /* DEBUG_VFS_LOCKS */
3463 vop_rename_pre(void *ap)
3465 struct vop_rename_args *a = ap;
3467 #ifdef DEBUG_VFS_LOCKS
3469 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3470 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3471 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3472 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3474 /* Check the source (from). */
3475 if (a->a_tdvp != a->a_fdvp && a->a_tvp != a->a_fdvp)
3476 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3477 if (a->a_tvp != a->a_fvp)
3478 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: fvp locked");
3480 /* Check the target. */
3482 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3483 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3485 if (a->a_tdvp != a->a_fdvp)
3487 if (a->a_tvp != a->a_fvp)
3495 vop_strategy_pre(void *ap)
3497 #ifdef DEBUG_VFS_LOCKS
3498 struct vop_strategy_args *a;
3505 * Cluster ops lock their component buffers but not the IO container.
3507 if ((bp->b_flags & B_CLUSTER) != 0)
3510 if (BUF_REFCNT(bp) < 1) {
3511 if (vfs_badlock_print)
3513 "VOP_STRATEGY: bp is not locked but should be\n");
3514 if (vfs_badlock_ddb)
3515 kdb_enter("lock violation");
3521 vop_lookup_pre(void *ap)
3523 #ifdef DEBUG_VFS_LOCKS
3524 struct vop_lookup_args *a;
3529 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3530 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3535 vop_lookup_post(void *ap, int rc)
3537 #ifdef DEBUG_VFS_LOCKS
3538 struct vop_lookup_args *a;
3546 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3547 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3550 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3555 vop_lock_pre(void *ap)
3557 #ifdef DEBUG_VFS_LOCKS
3558 struct vop_lock1_args *a = ap;
3560 if ((a->a_flags & LK_INTERLOCK) == 0)
3561 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3563 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3568 vop_lock_post(void *ap, int rc)
3570 #ifdef DEBUG_VFS_LOCKS
3571 struct vop_lock1_args *a = ap;
3573 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3575 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3580 vop_unlock_pre(void *ap)
3582 #ifdef DEBUG_VFS_LOCKS
3583 struct vop_unlock_args *a = ap;
3585 if (a->a_flags & LK_INTERLOCK)
3586 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3587 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3592 vop_unlock_post(void *ap, int rc)
3594 #ifdef DEBUG_VFS_LOCKS
3595 struct vop_unlock_args *a = ap;
3597 if (a->a_flags & LK_INTERLOCK)
3598 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
3603 vop_create_post(void *ap, int rc)
3605 struct vop_create_args *a = ap;
3608 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3612 vop_link_post(void *ap, int rc)
3614 struct vop_link_args *a = ap;
3617 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
3618 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
3623 vop_mkdir_post(void *ap, int rc)
3625 struct vop_mkdir_args *a = ap;
3628 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3632 vop_mknod_post(void *ap, int rc)
3634 struct vop_mknod_args *a = ap;
3637 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3641 vop_remove_post(void *ap, int rc)
3643 struct vop_remove_args *a = ap;
3646 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3647 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3652 vop_rename_post(void *ap, int rc)
3654 struct vop_rename_args *a = ap;
3657 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
3658 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
3659 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
3661 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
3663 if (a->a_tdvp != a->a_fdvp)
3665 if (a->a_tvp != a->a_fvp)
3673 vop_rmdir_post(void *ap, int rc)
3675 struct vop_rmdir_args *a = ap;
3678 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3679 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3684 vop_setattr_post(void *ap, int rc)
3686 struct vop_setattr_args *a = ap;
3689 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
3693 vop_symlink_post(void *ap, int rc)
3695 struct vop_symlink_args *a = ap;
3698 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3701 static struct knlist fs_knlist;
3704 vfs_event_init(void *arg)
3706 knlist_init(&fs_knlist, NULL, NULL, NULL, NULL);
3708 /* XXX - correct order? */
3709 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
3712 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
3715 KNOTE_UNLOCKED(&fs_knlist, event);
3718 static int filt_fsattach(struct knote *kn);
3719 static void filt_fsdetach(struct knote *kn);
3720 static int filt_fsevent(struct knote *kn, long hint);
3722 struct filterops fs_filtops =
3723 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
3726 filt_fsattach(struct knote *kn)
3729 kn->kn_flags |= EV_CLEAR;
3730 knlist_add(&fs_knlist, kn, 0);
3735 filt_fsdetach(struct knote *kn)
3738 knlist_remove(&fs_knlist, kn, 0);
3742 filt_fsevent(struct knote *kn, long hint)
3745 kn->kn_fflags |= hint;
3746 return (kn->kn_fflags != 0);
3750 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
3756 error = SYSCTL_IN(req, &vc, sizeof(vc));
3759 if (vc.vc_vers != VFS_CTL_VERS1)
3761 mp = vfs_getvfs(&vc.vc_fsid);
3764 /* ensure that a specific sysctl goes to the right filesystem. */
3765 if (strcmp(vc.vc_fstypename, "*") != 0 &&
3766 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
3770 VCTLTOREQ(&vc, req);
3771 error = VFS_SYSCTL(mp, vc.vc_op, req);
3776 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR, NULL, 0, sysctl_vfs_ctl, "",
3780 * Function to initialize a va_filerev field sensibly.
3781 * XXX: Wouldn't a random number make a lot more sense ??
3784 init_va_filerev(void)
3789 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
3792 static int filt_vfsread(struct knote *kn, long hint);
3793 static int filt_vfswrite(struct knote *kn, long hint);
3794 static int filt_vfsvnode(struct knote *kn, long hint);
3795 static void filt_vfsdetach(struct knote *kn);
3796 static struct filterops vfsread_filtops =
3797 { 1, NULL, filt_vfsdetach, filt_vfsread };
3798 static struct filterops vfswrite_filtops =
3799 { 1, NULL, filt_vfsdetach, filt_vfswrite };
3800 static struct filterops vfsvnode_filtops =
3801 { 1, NULL, filt_vfsdetach, filt_vfsvnode };
3804 vfs_knllock(void *arg)
3806 struct vnode *vp = arg;
3808 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
3812 vfs_knlunlock(void *arg)
3814 struct vnode *vp = arg;
3816 VOP_UNLOCK(vp, 0, curthread);
3820 vfs_knllocked(void *arg)
3822 struct vnode *vp = arg;
3824 return (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE);
3828 vfs_kqfilter(struct vop_kqfilter_args *ap)
3830 struct vnode *vp = ap->a_vp;
3831 struct knote *kn = ap->a_kn;
3834 switch (kn->kn_filter) {
3836 kn->kn_fop = &vfsread_filtops;
3839 kn->kn_fop = &vfswrite_filtops;
3842 kn->kn_fop = &vfsvnode_filtops;
3848 kn->kn_hook = (caddr_t)vp;
3850 if (vp->v_pollinfo == NULL)
3852 if (vp->v_pollinfo == NULL)
3854 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
3855 knlist_add(knl, kn, 0);
3861 * Detach knote from vnode
3864 filt_vfsdetach(struct knote *kn)
3866 struct vnode *vp = (struct vnode *)kn->kn_hook;
3868 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
3869 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
3874 filt_vfsread(struct knote *kn, long hint)
3876 struct vnode *vp = (struct vnode *)kn->kn_hook;
3880 * filesystem is gone, so set the EOF flag and schedule
3881 * the knote for deletion.
3883 if (hint == NOTE_REVOKE) {
3884 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3888 if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread))
3891 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
3892 return (kn->kn_data != 0);
3897 filt_vfswrite(struct knote *kn, long hint)
3900 * filesystem is gone, so set the EOF flag and schedule
3901 * the knote for deletion.
3903 if (hint == NOTE_REVOKE)
3904 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3911 filt_vfsvnode(struct knote *kn, long hint)
3913 if (kn->kn_sfflags & hint)
3914 kn->kn_fflags |= hint;
3915 if (hint == NOTE_REVOKE) {
3916 kn->kn_flags |= EV_EOF;
3919 return (kn->kn_fflags != 0);
3923 vfs_read_dirent(struct vop_readdir_args *ap, struct dirent *dp, off_t off)
3927 if (dp->d_reclen > ap->a_uio->uio_resid)
3928 return (ENAMETOOLONG);
3929 error = uiomove(dp, dp->d_reclen, ap->a_uio);
3931 if (ap->a_ncookies != NULL) {
3932 if (ap->a_cookies != NULL)
3933 free(ap->a_cookies, M_TEMP);
3934 ap->a_cookies = NULL;
3935 *ap->a_ncookies = 0;
3939 if (ap->a_ncookies == NULL)
3942 KASSERT(ap->a_cookies,
3943 ("NULL ap->a_cookies value with non-NULL ap->a_ncookies!"));
3945 *ap->a_cookies = realloc(*ap->a_cookies,
3946 (*ap->a_ncookies + 1) * sizeof(u_long), M_TEMP, M_WAITOK | M_ZERO);
3947 (*ap->a_cookies)[*ap->a_ncookies] = off;
3952 * Mark for update the access time of the file if the filesystem
3953 * supports VA_MARK_ATIME. This functionality is used by execve
3954 * and mmap, so we want to avoid the synchronous I/O implied by
3955 * directly setting va_atime for the sake of efficiency.
3958 vfs_mark_atime(struct vnode *vp, struct thread *td)
3960 struct vattr atimeattr;
3962 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
3963 VATTR_NULL(&atimeattr);
3964 atimeattr.va_vaflags |= VA_MARK_ATIME;
3965 (void)VOP_SETATTR(vp, &atimeattr, td->td_ucred, td);