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/event.h>
53 #include <sys/eventhandler.h>
54 #include <sys/extattr.h>
56 #include <sys/fcntl.h>
58 #include <sys/kernel.h>
59 #include <sys/kthread.h>
61 #include <sys/malloc.h>
62 #include <sys/mount.h>
63 #include <sys/namei.h>
64 #include <sys/reboot.h>
65 #include <sys/sleepqueue.h>
67 #include <sys/sysctl.h>
68 #include <sys/syslog.h>
69 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
72 #include <machine/stdarg.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_extern.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_kern.h>
83 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
85 static void delmntque(struct vnode *vp);
86 static void insmntque(struct vnode *vp, struct mount *mp);
87 static int flushbuflist(struct bufv *bufv, int flags, struct bufobj *bo,
88 int slpflag, int slptimeo);
89 static void syncer_shutdown(void *arg, int howto);
90 static int vtryrecycle(struct vnode *vp);
91 static void vbusy(struct vnode *vp);
92 static void vdropl(struct vnode *vp);
93 static void vinactive(struct vnode *, struct thread *);
94 static void v_incr_usecount(struct vnode *);
95 static void v_decr_usecount(struct vnode *);
96 static void v_decr_useonly(struct vnode *);
97 static void vfree(struct vnode *);
98 static void vnlru_free(int);
99 static void vdestroy(struct vnode *);
100 static void vgonel(struct vnode *);
101 static void vfs_knllock(void *arg);
102 static void vfs_knlunlock(void *arg);
103 static int vfs_knllocked(void *arg);
107 * Enable Giant pushdown based on whether or not the vm is mpsafe in this
108 * build. Without mpsafevm the buffer cache can not run Giant free.
110 #if defined(__alpha__) || defined(__amd64__) || defined(__i386__)
115 TUNABLE_INT("debug.mpsafevfs", &mpsafe_vfs);
116 SYSCTL_INT(_debug, OID_AUTO, mpsafevfs, CTLFLAG_RD, &mpsafe_vfs, 0,
120 * Number of vnodes in existence. Increased whenever getnewvnode()
121 * allocates a new vnode, never decreased.
123 static unsigned long numvnodes;
125 SYSCTL_LONG(_vfs, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
128 * Conversion tables for conversion from vnode types to inode formats
131 enum vtype iftovt_tab[16] = {
132 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
133 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
135 int vttoif_tab[9] = {
136 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
137 S_IFSOCK, S_IFIFO, S_IFMT,
141 * List of vnodes that are ready for recycling.
143 static TAILQ_HEAD(freelst, vnode) vnode_free_list;
146 * Free vnode target. Free vnodes may simply be files which have been stat'd
147 * but not read. This is somewhat common, and a small cache of such files
148 * should be kept to avoid recreation costs.
150 static u_long wantfreevnodes;
151 SYSCTL_LONG(_vfs, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
152 /* Number of vnodes in the free list. */
153 static u_long freevnodes;
154 SYSCTL_LONG(_vfs, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
157 * Various variables used for debugging the new implementation of
159 * XXX these are probably of (very) limited utility now.
161 static int reassignbufcalls;
162 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
165 * Cache for the mount type id assigned to NFS. This is used for
166 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c.
168 int nfs_mount_type = -1;
170 /* To keep more than one thread at a time from running vfs_getnewfsid */
171 static struct mtx mntid_mtx;
174 * Lock for any access to the following:
179 static struct mtx vnode_free_list_mtx;
181 /* Publicly exported FS */
182 struct nfs_public nfs_pub;
184 /* Zone for allocation of new vnodes - used exclusively by getnewvnode() */
185 static uma_zone_t vnode_zone;
186 static uma_zone_t vnodepoll_zone;
188 /* Set to 1 to print out reclaim of active vnodes */
192 * The workitem queue.
194 * It is useful to delay writes of file data and filesystem metadata
195 * for tens of seconds so that quickly created and deleted files need
196 * not waste disk bandwidth being created and removed. To realize this,
197 * we append vnodes to a "workitem" queue. When running with a soft
198 * updates implementation, most pending metadata dependencies should
199 * not wait for more than a few seconds. Thus, mounted on block devices
200 * are delayed only about a half the time that file data is delayed.
201 * Similarly, directory updates are more critical, so are only delayed
202 * about a third the time that file data is delayed. Thus, there are
203 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
204 * one each second (driven off the filesystem syncer process). The
205 * syncer_delayno variable indicates the next queue that is to be processed.
206 * Items that need to be processed soon are placed in this queue:
208 * syncer_workitem_pending[syncer_delayno]
210 * A delay of fifteen seconds is done by placing the request fifteen
211 * entries later in the queue:
213 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
216 static int syncer_delayno;
217 static long syncer_mask;
218 LIST_HEAD(synclist, bufobj);
219 static struct synclist *syncer_workitem_pending;
221 * The sync_mtx protects:
226 * syncer_workitem_pending
227 * syncer_worklist_len
230 static struct mtx sync_mtx;
232 #define SYNCER_MAXDELAY 32
233 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
234 static int syncdelay = 30; /* max time to delay syncing data */
235 static int filedelay = 30; /* time to delay syncing files */
236 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
237 static int dirdelay = 29; /* time to delay syncing directories */
238 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
239 static int metadelay = 28; /* time to delay syncing metadata */
240 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
241 static int rushjob; /* number of slots to run ASAP */
242 static int stat_rush_requests; /* number of times I/O speeded up */
243 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
246 * When shutting down the syncer, run it at four times normal speed.
248 #define SYNCER_SHUTDOWN_SPEEDUP 4
249 static int sync_vnode_count;
250 static int syncer_worklist_len;
251 static enum { SYNCER_RUNNING, SYNCER_SHUTTING_DOWN, SYNCER_FINAL_DELAY }
255 * Number of vnodes we want to exist at any one time. This is mostly used
256 * to size hash tables in vnode-related code. It is normally not used in
257 * getnewvnode(), as wantfreevnodes is normally nonzero.)
259 * XXX desiredvnodes is historical cruft and should not exist.
262 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
263 &desiredvnodes, 0, "Maximum number of vnodes");
264 SYSCTL_INT(_kern, OID_AUTO, minvnodes, CTLFLAG_RW,
265 &wantfreevnodes, 0, "Minimum number of vnodes (legacy)");
266 static int vnlru_nowhere;
267 SYSCTL_INT(_debug, OID_AUTO, vnlru_nowhere, CTLFLAG_RW,
268 &vnlru_nowhere, 0, "Number of times the vnlru process ran without success");
270 /* Hook for calling soft updates. */
271 int (*softdep_process_worklist_hook)(struct mount *);
274 * Macros to control when a vnode is freed and recycled. All require
275 * the vnode interlock.
277 #define VCANRECYCLE(vp) (((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
278 #define VSHOULDFREE(vp) (!((vp)->v_iflag & VI_FREE) && !(vp)->v_holdcnt)
279 #define VSHOULDBUSY(vp) (((vp)->v_iflag & VI_FREE) && (vp)->v_holdcnt)
283 * Initialize the vnode management data structures.
285 #ifndef MAXVNODES_MAX
286 #define MAXVNODES_MAX 100000
289 vntblinit(void *dummy __unused)
293 * Desiredvnodes is a function of the physical memory size and
294 * the kernel's heap size. Specifically, desiredvnodes scales
295 * in proportion to the physical memory size until two fifths
296 * of the kernel's heap size is consumed by vnodes and vm
299 desiredvnodes = min(maxproc + cnt.v_page_count / 4, 2 * vm_kmem_size /
300 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
301 if (desiredvnodes > MAXVNODES_MAX) {
303 printf("Reducing kern.maxvnodes %d -> %d\n",
304 desiredvnodes, MAXVNODES_MAX);
305 desiredvnodes = MAXVNODES_MAX;
307 wantfreevnodes = desiredvnodes / 4;
308 mtx_init(&mntid_mtx, "mntid", NULL, MTX_DEF);
309 TAILQ_INIT(&vnode_free_list);
310 mtx_init(&vnode_free_list_mtx, "vnode_free_list", NULL, MTX_DEF);
311 vnode_zone = uma_zcreate("VNODE", sizeof (struct vnode), NULL, NULL,
312 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
313 vnodepoll_zone = uma_zcreate("VNODEPOLL", sizeof (struct vpollinfo),
314 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
316 * Initialize the filesystem syncer.
318 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
320 syncer_maxdelay = syncer_mask + 1;
321 mtx_init(&sync_mtx, "Syncer mtx", NULL, MTX_DEF);
323 SYSINIT(vfs, SI_SUB_VFS, SI_ORDER_FIRST, vntblinit, NULL)
327 * Mark a mount point as busy. Used to synchronize access and to delay
328 * unmounting. Interlock is not released on failure.
331 vfs_busy(mp, flags, interlkp, td)
334 struct mtx *interlkp;
340 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
341 if (flags & LK_NOWAIT) {
346 mtx_unlock(interlkp);
347 mp->mnt_kern_flag |= MNTK_MWAIT;
349 * Since all busy locks are shared except the exclusive
350 * lock granted when unmounting, the only place that a
351 * wakeup needs to be done is at the release of the
352 * exclusive lock at the end of dounmount.
354 msleep(mp, MNT_MTX(mp), PVFS|PDROP, "vfs_busy", 0);
360 mtx_unlock(interlkp);
361 lkflags = LK_SHARED | LK_INTERLOCK;
362 if (lockmgr(&mp->mnt_lock, lkflags, MNT_MTX(mp), td))
363 panic("vfs_busy: unexpected lock failure");
368 * Free a busy filesystem.
376 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td);
380 * Lookup a mount point by filesystem identifier.
388 mtx_lock(&mountlist_mtx);
389 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
390 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
391 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
392 mtx_unlock(&mountlist_mtx);
396 mtx_unlock(&mountlist_mtx);
397 return ((struct mount *) 0);
401 * Check if a user can access priveledged mount options.
404 vfs_suser(struct mount *mp, struct thread *td)
408 if ((mp->mnt_flag & MNT_USER) == 0 ||
409 mp->mnt_cred->cr_uid != td->td_ucred->cr_uid) {
410 if ((error = suser(td)) != 0)
417 * Get a new unique fsid. Try to make its val[0] unique, since this value
418 * will be used to create fake device numbers for stat(). Also try (but
419 * not so hard) make its val[0] unique mod 2^16, since some emulators only
420 * support 16-bit device numbers. We end up with unique val[0]'s for the
421 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls.
423 * Keep in mind that several mounts may be running in parallel. Starting
424 * the search one past where the previous search terminated is both a
425 * micro-optimization and a defense against returning the same fsid to
432 static u_int16_t mntid_base;
436 mtx_lock(&mntid_mtx);
437 mtype = mp->mnt_vfc->vfc_typenum;
438 tfsid.val[1] = mtype;
439 mtype = (mtype & 0xFF) << 24;
441 tfsid.val[0] = makedev(255,
442 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF));
444 if (vfs_getvfs(&tfsid) == NULL)
447 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
448 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
449 mtx_unlock(&mntid_mtx);
453 * Knob to control the precision of file timestamps:
455 * 0 = seconds only; nanoseconds zeroed.
456 * 1 = seconds and nanoseconds, accurate within 1/HZ.
457 * 2 = seconds and nanoseconds, truncated to microseconds.
458 * >=3 = seconds and nanoseconds, maximum precision.
460 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
462 static int timestamp_precision = TSP_SEC;
463 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
464 ×tamp_precision, 0, "");
467 * Get a current timestamp.
471 struct timespec *tsp;
475 switch (timestamp_precision) {
477 tsp->tv_sec = time_second;
485 TIMEVAL_TO_TIMESPEC(&tv, tsp);
495 * Set vnode attributes to VNOVAL
503 vap->va_size = VNOVAL;
504 vap->va_bytes = VNOVAL;
505 vap->va_mode = VNOVAL;
506 vap->va_nlink = VNOVAL;
507 vap->va_uid = VNOVAL;
508 vap->va_gid = VNOVAL;
509 vap->va_fsid = VNOVAL;
510 vap->va_fileid = VNOVAL;
511 vap->va_blocksize = VNOVAL;
512 vap->va_rdev = VNOVAL;
513 vap->va_atime.tv_sec = VNOVAL;
514 vap->va_atime.tv_nsec = VNOVAL;
515 vap->va_mtime.tv_sec = VNOVAL;
516 vap->va_mtime.tv_nsec = VNOVAL;
517 vap->va_ctime.tv_sec = VNOVAL;
518 vap->va_ctime.tv_nsec = VNOVAL;
519 vap->va_birthtime.tv_sec = VNOVAL;
520 vap->va_birthtime.tv_nsec = VNOVAL;
521 vap->va_flags = VNOVAL;
522 vap->va_gen = VNOVAL;
527 * This routine is called when we have too many vnodes. It attempts
528 * to free <count> vnodes and will potentially free vnodes that still
529 * have VM backing store (VM backing store is typically the cause
530 * of a vnode blowout so we want to do this). Therefore, this operation
531 * is not considered cheap.
533 * A number of conditions may prevent a vnode from being reclaimed.
534 * the buffer cache may have references on the vnode, a directory
535 * vnode may still have references due to the namei cache representing
536 * underlying files, or the vnode may be in active use. It is not
537 * desireable to reuse such vnodes. These conditions may cause the
538 * number of vnodes to reach some minimum value regardless of what
539 * you set kern.maxvnodes to. Do not set kern.maxvnodes too low.
542 vlrureclaim(struct mount *mp)
552 * Calculate the trigger point, don't allow user
553 * screwups to blow us up. This prevents us from
554 * recycling vnodes with lots of resident pages. We
555 * aren't trying to free memory, we are trying to
558 usevnodes = desiredvnodes;
561 trigger = cnt.v_page_count * 2 / usevnodes;
564 vn_start_write(NULL, &mp, V_WAIT);
566 count = mp->mnt_nvnodelistsize / 10 + 1;
567 while (count && (vp = TAILQ_FIRST(&mp->mnt_nvnodelist)) != NULL) {
568 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
569 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
574 * If it's been deconstructed already, it's still
575 * referenced, or it exceeds the trigger, skip it.
577 if ((vp->v_iflag & VI_DOOMED) != 0 || vp->v_usecount ||
578 !LIST_EMPTY(&(vp)->v_cache_src) || (vp->v_object != NULL &&
579 vp->v_object->resident_page_count > trigger)) {
585 if (VOP_LOCK(vp, LK_INTERLOCK|LK_EXCLUSIVE, td)) {
592 VOP_UNLOCK(vp, 0, td);
598 vn_finished_write(mp);
603 * Attempt to keep the free list at wantfreevnodes length.
606 vnlru_free(int count)
610 mtx_assert(&vnode_free_list_mtx, MA_OWNED);
611 for (; count > 0; count--) {
612 vp = TAILQ_FIRST(&vnode_free_list);
614 * The list can be modified while the free_list_mtx
615 * has been dropped and vp could be NULL here.
619 VNASSERT(vp->v_op != NULL, vp,
620 ("vnlru_free: vnode already reclaimed."));
621 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
623 * Don't recycle if we can't get the interlock.
625 if (!VI_TRYLOCK(vp)) {
626 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
629 VNASSERT(VCANRECYCLE(vp), vp,
630 ("vp inconsistent on freelist"));
632 vp->v_iflag &= ~VI_FREE;
634 mtx_unlock(&vnode_free_list_mtx);
638 * If the recycled succeeded this vdrop will actually free
639 * the vnode. If not it will simply place it back on
643 mtx_lock(&vnode_free_list_mtx);
647 * Attempt to recycle vnodes in a context that is always safe to block.
648 * Calling vlrurecycle() from the bowels of filesystem code has some
649 * interesting deadlock problems.
651 static struct proc *vnlruproc;
652 static int vnlruproc_sig;
657 struct mount *mp, *nmp;
659 struct proc *p = vnlruproc;
660 struct thread *td = FIRST_THREAD_IN_PROC(p);
664 EVENTHANDLER_REGISTER(shutdown_pre_sync, kproc_shutdown, p,
668 kthread_suspend_check(p);
669 mtx_lock(&vnode_free_list_mtx);
670 if (freevnodes > wantfreevnodes)
671 vnlru_free(freevnodes - wantfreevnodes);
672 if (numvnodes <= desiredvnodes * 9 / 10) {
674 wakeup(&vnlruproc_sig);
675 msleep(vnlruproc, &vnode_free_list_mtx,
676 PVFS|PDROP, "vlruwt", hz);
679 mtx_unlock(&vnode_free_list_mtx);
681 mtx_lock(&mountlist_mtx);
682 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
684 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td)) {
685 nmp = TAILQ_NEXT(mp, mnt_list);
688 if (!VFS_NEEDSGIANT(mp)) {
693 done += vlrureclaim(mp);
696 mtx_lock(&mountlist_mtx);
697 nmp = TAILQ_NEXT(mp, mnt_list);
700 mtx_unlock(&mountlist_mtx);
703 /* These messages are temporary debugging aids */
704 if (vnlru_nowhere < 5)
705 printf("vnlru process getting nowhere..\n");
706 else if (vnlru_nowhere == 5)
707 printf("vnlru process messages stopped.\n");
710 tsleep(vnlruproc, PPAUSE, "vlrup", hz * 3);
716 static struct kproc_desc vnlru_kp = {
721 SYSINIT(vnlru, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &vnlru_kp)
724 * Routines having to do with the management of the vnode table.
728 vdestroy(struct vnode *vp)
732 CTR1(KTR_VFS, "vdestroy vp %p", vp);
733 mtx_lock(&vnode_free_list_mtx);
735 mtx_unlock(&vnode_free_list_mtx);
737 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp,
738 ("cleaned vnode still on the free list."));
739 VNASSERT(vp->v_data == NULL, vp, ("cleaned vnode isn't"));
740 VNASSERT(vp->v_holdcnt == 0, vp, ("Non-zero hold count"));
741 VNASSERT(vp->v_usecount == 0, vp, ("Non-zero use count"));
742 VNASSERT(vp->v_writecount == 0, vp, ("Non-zero write count"));
743 VNASSERT(bo->bo_numoutput == 0, vp, ("Clean vnode has pending I/O's"));
744 VNASSERT(bo->bo_clean.bv_cnt == 0, vp, ("cleanbufcnt not 0"));
745 VNASSERT(bo->bo_clean.bv_root == NULL, vp, ("cleanblkroot not NULL"));
746 VNASSERT(bo->bo_dirty.bv_cnt == 0, vp, ("dirtybufcnt not 0"));
747 VNASSERT(bo->bo_dirty.bv_root == NULL, vp, ("dirtyblkroot not NULL"));
748 VNASSERT(TAILQ_EMPTY(&vp->v_cache_dst), vp, ("vp has namecache dst"));
749 VNASSERT(LIST_EMPTY(&vp->v_cache_src), vp, ("vp has namecache src"));
752 mac_destroy_vnode(vp);
754 if (vp->v_pollinfo != NULL) {
755 knlist_destroy(&vp->v_pollinfo->vpi_selinfo.si_note);
756 mtx_destroy(&vp->v_pollinfo->vpi_lock);
757 uma_zfree(vnodepoll_zone, vp->v_pollinfo);
760 /* XXX Elsewhere we can detect an already freed vnode via NULL v_op. */
763 lockdestroy(vp->v_vnlock);
764 mtx_destroy(&vp->v_interlock);
765 uma_zfree(vnode_zone, vp);
769 * Try to recycle a freed vnode. We abort if anyone picks up a reference
770 * before we actually vgone(). This function must be called with the vnode
771 * held to prevent the vnode from being returned to the free list midway
775 vtryrecycle(struct vnode *vp)
777 struct thread *td = curthread;
780 CTR1(KTR_VFS, "vtryrecycle: trying vp %p", vp);
781 VNASSERT(vp->v_holdcnt, vp,
782 ("vtryrecycle: Recycling vp %p without a reference.", vp));
784 * This vnode may found and locked via some other list, if so we
785 * can't recycle it yet.
787 if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT, td) != 0)
788 return (EWOULDBLOCK);
790 * Don't recycle if its filesystem is being suspended.
792 if (vn_start_write(vp, &vnmp, V_NOWAIT) != 0) {
793 VOP_UNLOCK(vp, 0, td);
797 * If we got this far, we need to acquire the interlock and see if
798 * anyone picked up this vnode from another list. If not, we will
799 * mark it with DOOMED via vgonel() so that anyone who does find it
803 if (vp->v_usecount) {
804 VOP_UNLOCK(vp, LK_INTERLOCK, td);
805 vn_finished_write(vnmp);
808 if ((vp->v_iflag & VI_DOOMED) == 0)
810 VOP_UNLOCK(vp, LK_INTERLOCK, td);
811 vn_finished_write(vnmp);
812 CTR1(KTR_VFS, "vtryrecycle: recycled vp %p", vp);
817 * Return the next vnode from the free list.
820 getnewvnode(tag, mp, vops, vpp)
823 struct vop_vector *vops;
826 struct vnode *vp = NULL;
829 mtx_lock(&vnode_free_list_mtx);
831 * Lend our context to reclaim vnodes if they've exceeded the max.
833 if (freevnodes > wantfreevnodes)
836 * Wait for available vnodes.
838 if (numvnodes > desiredvnodes) {
839 if (vnlruproc_sig == 0) {
840 vnlruproc_sig = 1; /* avoid unnecessary wakeups */
843 msleep(&vnlruproc_sig, &vnode_free_list_mtx, PVFS,
845 #if 0 /* XXX Not all VFS_VGET/ffs_vget callers check returns. */
846 if (numvnodes > desiredvnodes) {
847 mtx_unlock(&vnode_free_list_mtx);
853 mtx_unlock(&vnode_free_list_mtx);
854 vp = (struct vnode *) uma_zalloc(vnode_zone, M_WAITOK|M_ZERO);
858 vp->v_vnlock = &vp->v_lock;
859 mtx_init(&vp->v_interlock, "vnode interlock", NULL, MTX_DEF);
861 * By default, don't allow shared locks unless filesystems
864 lockinit(vp->v_vnlock, PVFS, tag, VLKTIMEOUT, LK_NOSHARE);
870 bo->bo_mtx = &vp->v_interlock;
871 bo->bo_ops = &buf_ops_bio;
873 TAILQ_INIT(&bo->bo_clean.bv_hd);
874 TAILQ_INIT(&bo->bo_dirty.bv_hd);
876 * Initialize namecache.
878 LIST_INIT(&vp->v_cache_src);
879 TAILQ_INIT(&vp->v_cache_dst);
881 * Finalize various vnode identity bits.
890 if (mp != NULL && (mp->mnt_flag & MNT_MULTILABEL) == 0)
891 mac_associate_vnode_singlelabel(mp, vp);
893 printf("NULL mp in getnewvnode()\n");
898 bo->bo_bsize = mp->mnt_stat.f_iosize;
899 if ((mp->mnt_kern_flag & MNTK_NOKNOTE) != 0)
900 vp->v_vflag |= VV_NOKNOTE;
903 CTR2(KTR_VFS, "getnewvnode: mp %p vp %p", mp, vp);
909 * Delete from old mount point vnode list, if on one.
912 delmntque(struct vnode *vp)
916 if (vp->v_mount == NULL)
921 VNASSERT(mp->mnt_nvnodelistsize > 0, vp,
922 ("bad mount point vnode list size"));
923 TAILQ_REMOVE(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
924 mp->mnt_nvnodelistsize--;
929 * Insert into list of vnodes for the new mount point, if available.
932 insmntque(struct vnode *vp, struct mount *mp)
936 VNASSERT(mp != NULL, vp, ("Don't call insmntque(foo, NULL)"));
937 MNT_ILOCK(vp->v_mount);
938 TAILQ_INSERT_TAIL(&mp->mnt_nvnodelist, vp, v_nmntvnodes);
939 mp->mnt_nvnodelistsize++;
940 MNT_IUNLOCK(vp->v_mount);
944 * Flush out and invalidate all buffers associated with a bufobj
945 * Called with the underlying object locked.
948 bufobj_invalbuf(struct bufobj *bo, int flags, struct thread *td, int slpflag, int slptimeo)
953 if (flags & V_SAVE) {
954 error = bufobj_wwait(bo, slpflag, slptimeo);
959 if (bo->bo_dirty.bv_cnt > 0) {
961 if ((error = BO_SYNC(bo, MNT_WAIT, td)) != 0)
964 * XXX We could save a lock/unlock if this was only
965 * enabled under INVARIANTS
968 if (bo->bo_numoutput > 0 || bo->bo_dirty.bv_cnt > 0)
969 panic("vinvalbuf: dirty bufs");
973 * If you alter this loop please notice that interlock is dropped and
974 * reacquired in flushbuflist. Special care is needed to ensure that
975 * no race conditions occur from this.
978 error = flushbuflist(&bo->bo_clean,
979 flags, bo, slpflag, slptimeo);
981 error = flushbuflist(&bo->bo_dirty,
982 flags, bo, slpflag, slptimeo);
983 if (error != 0 && error != EAGAIN) {
987 } while (error != 0);
990 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
991 * have write I/O in-progress but if there is a VM object then the
992 * VM object can also have read-I/O in-progress.
995 bufobj_wwait(bo, 0, 0);
997 if (bo->bo_object != NULL) {
998 VM_OBJECT_LOCK(bo->bo_object);
999 vm_object_pip_wait(bo->bo_object, "bovlbx");
1000 VM_OBJECT_UNLOCK(bo->bo_object);
1003 } while (bo->bo_numoutput > 0);
1007 * Destroy the copy in the VM cache, too.
1009 if (bo->bo_object != NULL) {
1010 VM_OBJECT_LOCK(bo->bo_object);
1011 vm_object_page_remove(bo->bo_object, 0, 0,
1012 (flags & V_SAVE) ? TRUE : FALSE);
1013 VM_OBJECT_UNLOCK(bo->bo_object);
1018 if ((flags & (V_ALT | V_NORMAL)) == 0 &&
1019 (bo->bo_dirty.bv_cnt > 0 || bo->bo_clean.bv_cnt > 0))
1020 panic("vinvalbuf: flush failed");
1027 * Flush out and invalidate all buffers associated with a vnode.
1028 * Called with the underlying object locked.
1031 vinvalbuf(struct vnode *vp, int flags, struct thread *td, int slpflag, int slptimeo)
1034 CTR2(KTR_VFS, "vinvalbuf vp %p flags %d", vp, flags);
1035 ASSERT_VOP_LOCKED(vp, "vinvalbuf");
1036 return (bufobj_invalbuf(&vp->v_bufobj, flags, td, slpflag, slptimeo));
1040 * Flush out buffers on the specified list.
1044 flushbuflist(bufv, flags, bo, slpflag, slptimeo)
1048 int slpflag, slptimeo;
1050 struct buf *bp, *nbp;
1053 ASSERT_BO_LOCKED(bo);
1056 TAILQ_FOREACH_SAFE(bp, &bufv->bv_hd, b_bobufs, nbp) {
1057 if (((flags & V_NORMAL) && (bp->b_xflags & BX_ALTDATA)) ||
1058 ((flags & V_ALT) && (bp->b_xflags & BX_ALTDATA) == 0)) {
1062 error = BUF_TIMELOCK(bp,
1063 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK, BO_MTX(bo),
1064 "flushbuf", slpflag, slptimeo);
1067 return (error != ENOLCK ? error : EAGAIN);
1069 KASSERT(bp->b_bufobj == bo,
1070 ("bp %p wrong b_bufobj %p should be %p",
1071 bp, bp->b_bufobj, bo));
1072 if (bp->b_bufobj != bo) { /* XXX: necessary ? */
1078 * XXX Since there are no node locks for NFS, I
1079 * believe there is a slight chance that a delayed
1080 * write will occur while sleeping just above, so
1083 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
1086 bp->b_flags |= B_ASYNC;
1089 return (EAGAIN); /* XXX: why not loop ? */
1092 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
1093 bp->b_flags &= ~B_ASYNC;
1101 * Truncate a file's buffer and pages to a specified length. This
1102 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
1106 vtruncbuf(struct vnode *vp, struct ucred *cred, struct thread *td, off_t length, int blksize)
1108 struct buf *bp, *nbp;
1113 CTR2(KTR_VFS, "vtruncbuf vp %p length %jd", vp, length);
1115 * Round up to the *next* lbn.
1117 trunclbn = (length + blksize - 1) / blksize;
1119 ASSERT_VOP_LOCKED(vp, "vtruncbuf");
1126 TAILQ_FOREACH_SAFE(bp, &bo->bo_clean.bv_hd, b_bobufs, nbp) {
1127 if (bp->b_lblkno < trunclbn)
1130 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1131 VI_MTX(vp)) == ENOLCK)
1135 bp->b_flags |= (B_INVAL | B_RELBUF);
1136 bp->b_flags &= ~B_ASYNC;
1141 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
1142 (nbp->b_vp != vp) ||
1143 (nbp->b_flags & B_DELWRI))) {
1149 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1150 if (bp->b_lblkno < trunclbn)
1153 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1154 VI_MTX(vp)) == ENOLCK)
1157 bp->b_flags |= (B_INVAL | B_RELBUF);
1158 bp->b_flags &= ~B_ASYNC;
1162 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
1163 (nbp->b_vp != vp) ||
1164 (nbp->b_flags & B_DELWRI) == 0)) {
1173 TAILQ_FOREACH_SAFE(bp, &bo->bo_dirty.bv_hd, b_bobufs, nbp) {
1174 if (bp->b_lblkno > 0)
1177 * Since we hold the vnode lock this should only
1178 * fail if we're racing with the buf daemon.
1181 LK_EXCLUSIVE | LK_SLEEPFAIL | LK_INTERLOCK,
1182 VI_MTX(vp)) == ENOLCK) {
1185 VNASSERT((bp->b_flags & B_DELWRI), vp,
1186 ("buf(%p) on dirty queue without DELWRI", bp));
1195 bufobj_wwait(bo, 0, 0);
1197 vnode_pager_setsize(vp, length);
1203 * buf_splay() - splay tree core for the clean/dirty list of buffers in
1206 * NOTE: We have to deal with the special case of a background bitmap
1207 * buffer, a situation where two buffers will have the same logical
1208 * block offset. We want (1) only the foreground buffer to be accessed
1209 * in a lookup and (2) must differentiate between the foreground and
1210 * background buffer in the splay tree algorithm because the splay
1211 * tree cannot normally handle multiple entities with the same 'index'.
1212 * We accomplish this by adding differentiating flags to the splay tree's
1217 buf_splay(daddr_t lblkno, b_xflags_t xflags, struct buf *root)
1220 struct buf *lefttreemax, *righttreemin, *y;
1224 lefttreemax = righttreemin = &dummy;
1226 if (lblkno < root->b_lblkno ||
1227 (lblkno == root->b_lblkno &&
1228 (xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1229 if ((y = root->b_left) == NULL)
1231 if (lblkno < y->b_lblkno) {
1233 root->b_left = y->b_right;
1236 if ((y = root->b_left) == NULL)
1239 /* Link into the new root's right tree. */
1240 righttreemin->b_left = root;
1241 righttreemin = root;
1242 } else if (lblkno > root->b_lblkno ||
1243 (lblkno == root->b_lblkno &&
1244 (xflags & BX_BKGRDMARKER) > (root->b_xflags & BX_BKGRDMARKER))) {
1245 if ((y = root->b_right) == NULL)
1247 if (lblkno > y->b_lblkno) {
1249 root->b_right = y->b_left;
1252 if ((y = root->b_right) == NULL)
1255 /* Link into the new root's left tree. */
1256 lefttreemax->b_right = root;
1263 /* Assemble the new root. */
1264 lefttreemax->b_right = root->b_left;
1265 righttreemin->b_left = root->b_right;
1266 root->b_left = dummy.b_right;
1267 root->b_right = dummy.b_left;
1272 buf_vlist_remove(struct buf *bp)
1277 KASSERT(bp->b_bufobj != NULL, ("No b_bufobj %p", bp));
1278 ASSERT_BO_LOCKED(bp->b_bufobj);
1279 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) !=
1280 (BX_VNDIRTY|BX_VNCLEAN),
1281 ("buf_vlist_remove: Buf %p is on two lists", bp));
1282 if (bp->b_xflags & BX_VNDIRTY)
1283 bv = &bp->b_bufobj->bo_dirty;
1285 bv = &bp->b_bufobj->bo_clean;
1286 if (bp != bv->bv_root) {
1287 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1288 KASSERT(root == bp, ("splay lookup failed in remove"));
1290 if (bp->b_left == NULL) {
1293 root = buf_splay(bp->b_lblkno, bp->b_xflags, bp->b_left);
1294 root->b_right = bp->b_right;
1297 TAILQ_REMOVE(&bv->bv_hd, bp, b_bobufs);
1299 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1303 * Add the buffer to the sorted clean or dirty block list using a
1304 * splay tree algorithm.
1306 * NOTE: xflags is passed as a constant, optimizing this inline function!
1309 buf_vlist_add(struct buf *bp, struct bufobj *bo, b_xflags_t xflags)
1314 ASSERT_BO_LOCKED(bo);
1315 KASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0,
1316 ("buf_vlist_add: Buf %p has existing xflags %d", bp, bp->b_xflags));
1317 bp->b_xflags |= xflags;
1318 if (xflags & BX_VNDIRTY)
1323 root = buf_splay(bp->b_lblkno, bp->b_xflags, bv->bv_root);
1327 TAILQ_INSERT_TAIL(&bv->bv_hd, bp, b_bobufs);
1328 } else if (bp->b_lblkno < root->b_lblkno ||
1329 (bp->b_lblkno == root->b_lblkno &&
1330 (bp->b_xflags & BX_BKGRDMARKER) < (root->b_xflags & BX_BKGRDMARKER))) {
1331 bp->b_left = root->b_left;
1333 root->b_left = NULL;
1334 TAILQ_INSERT_BEFORE(root, bp, b_bobufs);
1336 bp->b_right = root->b_right;
1338 root->b_right = NULL;
1339 TAILQ_INSERT_AFTER(&bv->bv_hd, root, bp, b_bobufs);
1346 * Lookup a buffer using the splay tree. Note that we specifically avoid
1347 * shadow buffers used in background bitmap writes.
1349 * This code isn't quite efficient as it could be because we are maintaining
1350 * two sorted lists and do not know which list the block resides in.
1352 * During a "make buildworld" the desired buffer is found at one of
1353 * the roots more than 60% of the time. Thus, checking both roots
1354 * before performing either splay eliminates unnecessary splays on the
1355 * first tree splayed.
1358 gbincore(struct bufobj *bo, daddr_t lblkno)
1362 ASSERT_BO_LOCKED(bo);
1363 if ((bp = bo->bo_clean.bv_root) != NULL &&
1364 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1366 if ((bp = bo->bo_dirty.bv_root) != NULL &&
1367 bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1369 if ((bp = bo->bo_clean.bv_root) != NULL) {
1370 bo->bo_clean.bv_root = bp = buf_splay(lblkno, 0, bp);
1371 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1374 if ((bp = bo->bo_dirty.bv_root) != NULL) {
1375 bo->bo_dirty.bv_root = bp = buf_splay(lblkno, 0, bp);
1376 if (bp->b_lblkno == lblkno && !(bp->b_xflags & BX_BKGRDMARKER))
1383 * Associate a buffer with a vnode.
1386 bgetvp(struct vnode *vp, struct buf *bp)
1389 VNASSERT(bp->b_vp == NULL, bp->b_vp, ("bgetvp: not free"));
1391 CTR3(KTR_BUF, "bgetvp(%p) vp %p flags %X", bp, vp, bp->b_flags);
1392 VNASSERT((bp->b_xflags & (BX_VNDIRTY|BX_VNCLEAN)) == 0, vp,
1393 ("bgetvp: bp already attached! %p", bp));
1395 ASSERT_VI_LOCKED(vp, "bgetvp");
1398 bp->b_bufobj = &vp->v_bufobj;
1400 * Insert onto list for new vnode.
1402 buf_vlist_add(bp, &vp->v_bufobj, BX_VNCLEAN);
1406 * Disassociate a buffer from a vnode.
1409 brelvp(struct buf *bp)
1414 CTR3(KTR_BUF, "brelvp(%p) vp %p flags %X", bp, bp->b_vp, bp->b_flags);
1415 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1418 * Delete from old vnode list, if on one.
1420 vp = bp->b_vp; /* XXX */
1423 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1424 buf_vlist_remove(bp);
1426 panic("brelvp: Buffer %p not on queue.", bp);
1427 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1428 bo->bo_flag &= ~BO_ONWORKLST;
1429 mtx_lock(&sync_mtx);
1430 LIST_REMOVE(bo, bo_synclist);
1431 syncer_worklist_len--;
1432 mtx_unlock(&sync_mtx);
1435 bp->b_bufobj = NULL;
1440 * Add an item to the syncer work queue.
1443 vn_syncer_add_to_worklist(struct bufobj *bo, int delay)
1447 ASSERT_BO_LOCKED(bo);
1449 mtx_lock(&sync_mtx);
1450 if (bo->bo_flag & BO_ONWORKLST)
1451 LIST_REMOVE(bo, bo_synclist);
1453 bo->bo_flag |= BO_ONWORKLST;
1454 syncer_worklist_len++;
1457 if (delay > syncer_maxdelay - 2)
1458 delay = syncer_maxdelay - 2;
1459 slot = (syncer_delayno + delay) & syncer_mask;
1461 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], bo, bo_synclist);
1462 mtx_unlock(&sync_mtx);
1466 sysctl_vfs_worklist_len(SYSCTL_HANDLER_ARGS)
1470 mtx_lock(&sync_mtx);
1471 len = syncer_worklist_len - sync_vnode_count;
1472 mtx_unlock(&sync_mtx);
1473 error = SYSCTL_OUT(req, &len, sizeof(len));
1477 SYSCTL_PROC(_vfs, OID_AUTO, worklist_len, CTLTYPE_INT | CTLFLAG_RD, NULL, 0,
1478 sysctl_vfs_worklist_len, "I", "Syncer thread worklist length");
1480 struct proc *updateproc;
1481 static void sched_sync(void);
1482 static struct kproc_desc up_kp = {
1487 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
1490 sync_vnode(struct bufobj *bo, struct thread *td)
1495 vp = bo->__bo_vnode; /* XXX */
1496 if (VOP_ISLOCKED(vp, NULL) != 0)
1498 if (VI_TRYLOCK(vp) == 0)
1501 * We use vhold in case the vnode does not
1502 * successfully sync. vhold prevents the vnode from
1503 * going away when we unlock the sync_mtx so that
1504 * we can acquire the vnode interlock.
1507 mtx_unlock(&sync_mtx);
1509 if (vn_start_write(vp, &mp, V_NOWAIT) != 0) {
1511 mtx_lock(&sync_mtx);
1514 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
1515 (void) VOP_FSYNC(vp, MNT_LAZY, td);
1516 VOP_UNLOCK(vp, 0, td);
1517 vn_finished_write(mp);
1519 if ((bo->bo_flag & BO_ONWORKLST) != 0) {
1521 * Put us back on the worklist. The worklist
1522 * routine will remove us from our current
1523 * position and then add us back in at a later
1526 vn_syncer_add_to_worklist(bo, syncdelay);
1529 mtx_lock(&sync_mtx);
1534 * System filesystem synchronizer daemon.
1539 struct synclist *next;
1540 struct synclist *slp;
1543 struct thread *td = FIRST_THREAD_IN_PROC(updateproc);
1544 static int dummychan;
1546 int net_worklist_len;
1547 int syncer_final_iter;
1553 syncer_final_iter = 0;
1555 syncer_state = SYNCER_RUNNING;
1556 starttime = time_second;
1558 EVENTHANDLER_REGISTER(shutdown_pre_sync, syncer_shutdown, td->td_proc,
1562 mtx_lock(&sync_mtx);
1563 if (syncer_state == SYNCER_FINAL_DELAY &&
1564 syncer_final_iter == 0) {
1565 mtx_unlock(&sync_mtx);
1566 kthread_suspend_check(td->td_proc);
1567 mtx_lock(&sync_mtx);
1569 net_worklist_len = syncer_worklist_len - sync_vnode_count;
1570 if (syncer_state != SYNCER_RUNNING &&
1571 starttime != time_second) {
1573 printf("\nSyncing disks, vnodes remaining...");
1576 printf("%d ", net_worklist_len);
1578 starttime = time_second;
1581 * Push files whose dirty time has expired. Be careful
1582 * of interrupt race on slp queue.
1584 * Skip over empty worklist slots when shutting down.
1587 slp = &syncer_workitem_pending[syncer_delayno];
1588 syncer_delayno += 1;
1589 if (syncer_delayno == syncer_maxdelay)
1591 next = &syncer_workitem_pending[syncer_delayno];
1593 * If the worklist has wrapped since the
1594 * it was emptied of all but syncer vnodes,
1595 * switch to the FINAL_DELAY state and run
1596 * for one more second.
1598 if (syncer_state == SYNCER_SHUTTING_DOWN &&
1599 net_worklist_len == 0 &&
1600 last_work_seen == syncer_delayno) {
1601 syncer_state = SYNCER_FINAL_DELAY;
1602 syncer_final_iter = SYNCER_SHUTDOWN_SPEEDUP;
1604 } while (syncer_state != SYNCER_RUNNING && LIST_EMPTY(slp) &&
1605 syncer_worklist_len > 0);
1608 * Keep track of the last time there was anything
1609 * on the worklist other than syncer vnodes.
1610 * Return to the SHUTTING_DOWN state if any
1613 if (net_worklist_len > 0 || syncer_state == SYNCER_RUNNING)
1614 last_work_seen = syncer_delayno;
1615 if (net_worklist_len > 0 && syncer_state == SYNCER_FINAL_DELAY)
1616 syncer_state = SYNCER_SHUTTING_DOWN;
1617 while ((bo = LIST_FIRST(slp)) != NULL) {
1618 error = sync_vnode(bo, td);
1620 LIST_REMOVE(bo, bo_synclist);
1621 LIST_INSERT_HEAD(next, bo, bo_synclist);
1625 if (syncer_state == SYNCER_FINAL_DELAY && syncer_final_iter > 0)
1626 syncer_final_iter--;
1627 mtx_unlock(&sync_mtx);
1630 * Do soft update processing.
1632 if (softdep_process_worklist_hook != NULL)
1633 (*softdep_process_worklist_hook)(NULL);
1636 * The variable rushjob allows the kernel to speed up the
1637 * processing of the filesystem syncer process. A rushjob
1638 * value of N tells the filesystem syncer to process the next
1639 * N seconds worth of work on its queue ASAP. Currently rushjob
1640 * is used by the soft update code to speed up the filesystem
1641 * syncer process when the incore state is getting so far
1642 * ahead of the disk that the kernel memory pool is being
1643 * threatened with exhaustion.
1645 mtx_lock(&sync_mtx);
1648 mtx_unlock(&sync_mtx);
1651 mtx_unlock(&sync_mtx);
1653 * Just sleep for a short period if time between
1654 * iterations when shutting down to allow some I/O
1657 * If it has taken us less than a second to process the
1658 * current work, then wait. Otherwise start right over
1659 * again. We can still lose time if any single round
1660 * takes more than two seconds, but it does not really
1661 * matter as we are just trying to generally pace the
1662 * filesystem activity.
1664 if (syncer_state != SYNCER_RUNNING)
1665 tsleep(&dummychan, PPAUSE, "syncfnl",
1666 hz / SYNCER_SHUTDOWN_SPEEDUP);
1667 else if (time_second == starttime)
1668 tsleep(&lbolt, PPAUSE, "syncer", 0);
1673 * Request the syncer daemon to speed up its work.
1674 * We never push it to speed up more than half of its
1675 * normal turn time, otherwise it could take over the cpu.
1683 td = FIRST_THREAD_IN_PROC(updateproc);
1684 sleepq_remove(td, &lbolt);
1685 mtx_lock(&sync_mtx);
1686 if (rushjob < syncdelay / 2) {
1688 stat_rush_requests += 1;
1691 mtx_unlock(&sync_mtx);
1696 * Tell the syncer to speed up its work and run though its work
1697 * list several times, then tell it to shut down.
1700 syncer_shutdown(void *arg, int howto)
1704 if (howto & RB_NOSYNC)
1706 td = FIRST_THREAD_IN_PROC(updateproc);
1707 sleepq_remove(td, &lbolt);
1708 mtx_lock(&sync_mtx);
1709 syncer_state = SYNCER_SHUTTING_DOWN;
1711 mtx_unlock(&sync_mtx);
1712 kproc_shutdown(arg, howto);
1716 * Reassign a buffer from one vnode to another.
1717 * Used to assign file specific control information
1718 * (indirect blocks) to the vnode to which they belong.
1721 reassignbuf(struct buf *bp)
1734 CTR3(KTR_BUF, "reassignbuf(%p) vp %p flags %X",
1735 bp, bp->b_vp, bp->b_flags);
1737 * B_PAGING flagged buffers cannot be reassigned because their vp
1738 * is not fully linked in.
1740 if (bp->b_flags & B_PAGING)
1741 panic("cannot reassign paging buffer");
1744 * Delete from old vnode list, if on one.
1747 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN))
1748 buf_vlist_remove(bp);
1750 panic("reassignbuf: Buffer %p not on queue.", bp);
1752 * If dirty, put on list of dirty buffers; otherwise insert onto list
1755 if (bp->b_flags & B_DELWRI) {
1756 if ((bo->bo_flag & BO_ONWORKLST) == 0) {
1757 switch (vp->v_type) {
1767 vn_syncer_add_to_worklist(bo, delay);
1769 buf_vlist_add(bp, bo, BX_VNDIRTY);
1771 buf_vlist_add(bp, bo, BX_VNCLEAN);
1773 if ((bo->bo_flag & BO_ONWORKLST) && bo->bo_dirty.bv_cnt == 0) {
1774 mtx_lock(&sync_mtx);
1775 LIST_REMOVE(bo, bo_synclist);
1776 syncer_worklist_len--;
1777 mtx_unlock(&sync_mtx);
1778 bo->bo_flag &= ~BO_ONWORKLST;
1783 bp = TAILQ_FIRST(&bv->bv_hd);
1784 KASSERT(bp == NULL || bp->b_bufobj == bo,
1785 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1786 bp = TAILQ_LAST(&bv->bv_hd, buflists);
1787 KASSERT(bp == NULL || bp->b_bufobj == bo,
1788 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1790 bp = TAILQ_FIRST(&bv->bv_hd);
1791 KASSERT(bp == NULL || bp->b_bufobj == bo,
1792 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1793 bp = TAILQ_LAST(&bv->bv_hd, buflists);
1794 KASSERT(bp == NULL || bp->b_bufobj == bo,
1795 ("bp %p wrong b_bufobj %p should be %p", bp, bp->b_bufobj, bo));
1801 * Increment the use and hold counts on the vnode, taking care to reference
1802 * the driver's usecount if this is a chardev. The vholdl() will remove
1803 * the vnode from the free list if it is presently free. Requires the
1804 * vnode interlock and returns with it held.
1807 v_incr_usecount(struct vnode *vp)
1810 CTR3(KTR_VFS, "v_incr_usecount: vp %p holdcnt %d usecount %d\n",
1811 vp, vp->v_holdcnt, vp->v_usecount);
1813 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1815 vp->v_rdev->si_usecount++;
1822 * Decrement the vnode use and hold count along with the driver's usecount
1823 * if this is a chardev. The vdropl() below releases the vnode interlock
1824 * as it may free the vnode.
1827 v_decr_usecount(struct vnode *vp)
1830 CTR3(KTR_VFS, "v_decr_usecount: vp %p holdcnt %d usecount %d\n",
1831 vp, vp->v_holdcnt, vp->v_usecount);
1832 ASSERT_VI_LOCKED(vp, __FUNCTION__);
1833 VNASSERT(vp->v_usecount > 0, vp,
1834 ("v_decr_usecount: negative usecount"));
1836 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1838 vp->v_rdev->si_usecount--;
1845 * Decrement only the use count and driver use count. This is intended to
1846 * be paired with a follow on vdropl() to release the remaining hold count.
1847 * In this way we may vgone() a vnode with a 0 usecount without risk of
1848 * having it end up on a free list because the hold count is kept above 0.
1851 v_decr_useonly(struct vnode *vp)
1854 CTR3(KTR_VFS, "v_decr_useonly: vp %p holdcnt %d usecount %d\n",
1855 vp, vp->v_holdcnt, vp->v_usecount);
1856 ASSERT_VI_LOCKED(vp, __FUNCTION__);
1857 VNASSERT(vp->v_usecount > 0, vp,
1858 ("v_decr_useonly: negative usecount"));
1860 if (vp->v_type == VCHR && vp->v_rdev != NULL) {
1862 vp->v_rdev->si_usecount--;
1868 * Grab a particular vnode from the free list, increment its
1869 * reference count and lock it. The vnode lock bit is set if the
1870 * vnode is being eliminated in vgone. The process is awakened
1871 * when the transition is completed, and an error returned to
1872 * indicate that the vnode is no longer usable (possibly having
1873 * been changed to a new filesystem type).
1888 if ((flags & LK_INTERLOCK) == 0)
1891 * If the inactive call was deferred because vput() was called
1892 * with a shared lock, we have to do it here before another thread
1893 * gets a reference to data that should be dead.
1895 if (vp->v_iflag & VI_OWEINACT) {
1896 if (flags & LK_NOWAIT) {
1900 flags &= ~LK_TYPE_MASK;
1901 flags |= LK_EXCLUSIVE;
1904 v_incr_usecount(vp);
1905 if ((error = vn_lock(vp, flags | LK_INTERLOCK, td)) != 0) {
1908 * must expand vrele here because we do not want
1909 * to call VOP_INACTIVE if the reference count
1910 * drops back to zero since it was never really
1913 v_decr_usecount(vp);
1916 if (vp->v_iflag & VI_DOOMED && (flags & LK_RETRY) == 0)
1917 panic("vget: vn_lock failed to return ENOENT\n");
1920 if (vp->v_iflag & VI_OWEINACT)
1923 if ((oldflags & LK_TYPE_MASK) == 0)
1924 VOP_UNLOCK(vp, 0, td);
1930 * Increase the reference count of a vnode.
1933 vref(struct vnode *vp)
1937 v_incr_usecount(vp);
1942 * Return reference count of a vnode.
1944 * The results of this call are only guaranteed when some mechanism other
1945 * than the VI lock is used to stop other processes from gaining references
1946 * to the vnode. This may be the case if the caller holds the only reference.
1947 * This is also useful when stale data is acceptable as race conditions may
1948 * be accounted for by some other means.
1951 vrefcnt(struct vnode *vp)
1956 usecnt = vp->v_usecount;
1964 * Vnode put/release.
1965 * If count drops to zero, call inactive routine and return to freelist.
1971 struct thread *td = curthread; /* XXX */
1973 KASSERT(vp != NULL, ("vrele: null vp"));
1977 /* Skip this v_writecount check if we're going to panic below. */
1978 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
1979 ("vrele: missed vn_close"));
1981 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
1982 vp->v_usecount == 1)) {
1983 v_decr_usecount(vp);
1986 if (vp->v_usecount != 1) {
1988 vprint("vrele: negative ref count", vp);
1991 panic("vrele: negative ref cnt");
1994 * We want to hold the vnode until the inactive finishes to
1995 * prevent vgone() races. We drop the use count here and the
1996 * hold count below when we're done.
2000 * We must call VOP_INACTIVE with the node locked. Mark
2001 * as VI_DOINGINACT to avoid recursion.
2003 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td) == 0) {
2006 VOP_UNLOCK(vp, 0, td);
2013 * Release an already locked vnode. This give the same effects as
2014 * unlock+vrele(), but takes less time and avoids releasing and
2015 * re-aquiring the lock (as vrele() aquires the lock internally.)
2021 struct thread *td = curthread; /* XXX */
2024 KASSERT(vp != NULL, ("vput: null vp"));
2025 ASSERT_VOP_LOCKED(vp, "vput");
2027 /* Skip this v_writecount check if we're going to panic below. */
2028 VNASSERT(vp->v_writecount < vp->v_usecount || vp->v_usecount < 1, vp,
2029 ("vput: missed vn_close"));
2032 if (vp->v_usecount > 1 || ((vp->v_iflag & VI_DOINGINACT) &&
2033 vp->v_usecount == 1)) {
2034 VOP_UNLOCK(vp, 0, td);
2035 v_decr_usecount(vp);
2039 if (vp->v_usecount != 1) {
2041 vprint("vput: negative ref count", vp);
2043 panic("vput: negative ref cnt");
2046 * We want to hold the vnode until the inactive finishes to
2047 * prevent vgone() races. We drop the use count here and the
2048 * hold count below when we're done.
2051 vp->v_iflag |= VI_OWEINACT;
2052 if (VOP_ISLOCKED(vp, NULL) != LK_EXCLUSIVE) {
2053 error = VOP_LOCK(vp, LK_EXCLUPGRADE|LK_INTERLOCK|LK_NOWAIT, td);
2058 if (vp->v_iflag & VI_OWEINACT)
2060 VOP_UNLOCK(vp, 0, td);
2066 * Somebody doesn't want the vnode recycled.
2069 vhold(struct vnode *vp)
2078 vholdl(struct vnode *vp)
2082 if (VSHOULDBUSY(vp))
2087 * Note that there is one less who cares about this vnode. vdrop() is the
2088 * opposite of vhold().
2091 vdrop(struct vnode *vp)
2099 * Drop the hold count of the vnode. If this is the last reference to
2100 * the vnode we will free it if it has been vgone'd otherwise it is
2101 * placed on the free list.
2104 vdropl(struct vnode *vp)
2107 if (vp->v_holdcnt <= 0)
2108 panic("vdrop: holdcnt %d", vp->v_holdcnt);
2110 if (vp->v_holdcnt == 0) {
2111 if (vp->v_iflag & VI_DOOMED) {
2121 * Call VOP_INACTIVE on the vnode and manage the DOINGINACT and OWEINACT
2122 * flags. DOINGINACT prevents us from recursing in calls to vinactive.
2123 * OWEINACT tracks whether a vnode missed a call to inactive due to a
2124 * failed lock upgrade.
2127 vinactive(struct vnode *vp, struct thread *td)
2130 ASSERT_VOP_LOCKED(vp, "vinactive");
2131 ASSERT_VI_LOCKED(vp, "vinactive");
2132 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2133 ("vinactive: recursed on VI_DOINGINACT"));
2134 vp->v_iflag |= VI_DOINGINACT;
2135 vp->v_iflag &= ~VI_OWEINACT;
2137 VOP_INACTIVE(vp, td);
2139 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2140 ("vinactive: lost VI_DOINGINACT"));
2141 vp->v_iflag &= ~VI_DOINGINACT;
2145 * Remove any vnodes in the vnode table belonging to mount point mp.
2147 * If FORCECLOSE is not specified, there should not be any active ones,
2148 * return error if any are found (nb: this is a user error, not a
2149 * system error). If FORCECLOSE is specified, detach any active vnodes
2152 * If WRITECLOSE is set, only flush out regular file vnodes open for
2155 * SKIPSYSTEM causes any vnodes marked VV_SYSTEM to be skipped.
2157 * `rootrefs' specifies the base reference count for the root vnode
2158 * of this filesystem. The root vnode is considered busy if its
2159 * v_usecount exceeds this value. On a successful return, vflush(, td)
2160 * will call vrele() on the root vnode exactly rootrefs times.
2161 * If the SKIPSYSTEM or WRITECLOSE flags are specified, rootrefs must
2165 static int busyprt = 0; /* print out busy vnodes */
2166 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
2170 vflush(mp, rootrefs, flags, td)
2176 struct vnode *vp, *nvp, *rootvp = NULL;
2178 int busy = 0, error;
2180 CTR1(KTR_VFS, "vflush: mp %p", mp);
2182 KASSERT((flags & (SKIPSYSTEM | WRITECLOSE)) == 0,
2183 ("vflush: bad args"));
2185 * Get the filesystem root vnode. We can vput() it
2186 * immediately, since with rootrefs > 0, it won't go away.
2188 if ((error = VFS_ROOT(mp, LK_EXCLUSIVE, &rootvp, td)) != 0)
2195 MNT_VNODE_FOREACH(vp, mp, nvp) {
2200 error = vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE, td);
2207 * Skip over a vnodes marked VV_SYSTEM.
2209 if ((flags & SKIPSYSTEM) && (vp->v_vflag & VV_SYSTEM)) {
2210 VOP_UNLOCK(vp, 0, td);
2216 * If WRITECLOSE is set, flush out unlinked but still open
2217 * files (even if open only for reading) and regular file
2218 * vnodes open for writing.
2220 if (flags & WRITECLOSE) {
2221 error = VOP_GETATTR(vp, &vattr, td->td_ucred, td);
2224 if ((vp->v_type == VNON ||
2225 (error == 0 && vattr.va_nlink > 0)) &&
2226 (vp->v_writecount == 0 || vp->v_type != VREG)) {
2227 VOP_UNLOCK(vp, 0, td);
2235 * With v_usecount == 0, all we need to do is clear out the
2236 * vnode data structures and we are done.
2238 * If FORCECLOSE is set, forcibly close the vnode.
2240 if (vp->v_usecount == 0 || (flags & FORCECLOSE)) {
2241 VNASSERT(vp->v_usecount == 0 ||
2242 (vp->v_type != VCHR && vp->v_type != VBLK), vp,
2243 ("device VNODE %p is FORCECLOSED", vp));
2249 vprint("vflush: busy vnode", vp);
2252 VOP_UNLOCK(vp, 0, td);
2257 if (rootrefs > 0 && (flags & FORCECLOSE) == 0) {
2259 * If just the root vnode is busy, and if its refcount
2260 * is equal to `rootrefs', then go ahead and kill it.
2263 KASSERT(busy > 0, ("vflush: not busy"));
2264 VNASSERT(rootvp->v_usecount >= rootrefs, rootvp,
2265 ("vflush: usecount %d < rootrefs %d",
2266 rootvp->v_usecount, rootrefs));
2267 if (busy == 1 && rootvp->v_usecount == rootrefs) {
2268 VOP_LOCK(rootvp, LK_EXCLUSIVE|LK_INTERLOCK, td);
2270 VOP_UNLOCK(rootvp, 0, td);
2277 for (; rootrefs > 0; rootrefs--)
2283 * Recycle an unused vnode to the front of the free list.
2286 vrecycle(struct vnode *vp, struct thread *td)
2290 ASSERT_VOP_LOCKED(vp, "vrecycle");
2293 if (vp->v_usecount == 0) {
2302 * Eliminate all activity associated with a vnode
2303 * in preparation for reuse.
2306 vgone(struct vnode *vp)
2314 * vgone, with the vp interlock held.
2317 vgonel(struct vnode *vp)
2323 CTR1(KTR_VFS, "vgonel: vp %p", vp);
2324 ASSERT_VOP_LOCKED(vp, "vgonel");
2325 ASSERT_VI_LOCKED(vp, "vgonel");
2327 /* XXX Need to fix ttyvp before I enable this. */
2328 VNASSERT(vp->v_holdcnt, vp,
2329 ("vgonel: vp %p has no reference.", vp));
2334 * Don't vgonel if we're already doomed.
2336 if (vp->v_iflag & VI_DOOMED) {
2340 vp->v_iflag |= VI_DOOMED;
2342 * Check to see if the vnode is in use. If so, we have to call
2343 * VOP_CLOSE() and VOP_INACTIVE().
2345 active = vp->v_usecount;
2346 oweinact = (vp->v_iflag & VI_OWEINACT);
2349 * Clean out any buffers associated with the vnode.
2350 * If the flush fails, just toss the buffers.
2352 if (!TAILQ_EMPTY(&vp->v_bufobj.bo_dirty.bv_hd))
2353 (void) vn_write_suspend_wait(vp, NULL, V_WAIT);
2354 if (vinvalbuf(vp, V_SAVE, td, 0, 0) != 0)
2355 vinvalbuf(vp, 0, td, 0, 0);
2358 * If purging an active vnode, it must be closed and
2359 * deactivated before being reclaimed.
2362 VOP_CLOSE(vp, FNONBLOCK, NOCRED, td);
2363 if (oweinact || active) {
2365 if ((vp->v_iflag & VI_DOINGINACT) == 0)
2370 * Reclaim the vnode.
2372 if (VOP_RECLAIM(vp, td))
2373 panic("vgone: cannot reclaim");
2374 VNASSERT(vp->v_object == NULL, vp,
2375 ("vop_reclaim left v_object vp=%p, tag=%s", vp, vp->v_tag));
2377 * Delete from old mount point vnode list.
2382 * Done with purge, reset to the standard lock and invalidate
2386 vp->v_vnlock = &vp->v_lock;
2387 vp->v_op = &dead_vnodeops;
2393 * Calculate the total number of references to a special device.
2402 count = vp->v_rdev->si_usecount;
2408 * Same as above, but using the struct cdev *as argument
2417 count = dev->si_usecount;
2423 * Print out a description of a vnode.
2425 static char *typename[] =
2426 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
2429 vn_printf(struct vnode *vp, const char *fmt, ...)
2437 printf("%p: ", (void *)vp);
2438 printf("tag %s, type %s\n", vp->v_tag, typename[vp->v_type]);
2439 printf(" usecount %d, writecount %d, refcount %d mountedhere %p\n",
2440 vp->v_usecount, vp->v_writecount, vp->v_holdcnt, vp->v_mountedhere);
2443 if (vp->v_vflag & VV_ROOT)
2444 strcat(buf, "|VV_ROOT");
2445 if (vp->v_vflag & VV_TEXT)
2446 strcat(buf, "|VV_TEXT");
2447 if (vp->v_vflag & VV_SYSTEM)
2448 strcat(buf, "|VV_SYSTEM");
2449 if (vp->v_iflag & VI_DOOMED)
2450 strcat(buf, "|VI_DOOMED");
2451 if (vp->v_iflag & VI_FREE)
2452 strcat(buf, "|VI_FREE");
2453 printf(" flags (%s)\n", buf + 1);
2454 if (mtx_owned(VI_MTX(vp)))
2455 printf(" VI_LOCKed");
2456 if (vp->v_object != NULL)
2457 printf(" v_object %p ref %d pages %d\n",
2458 vp->v_object, vp->v_object->ref_count,
2459 vp->v_object->resident_page_count);
2461 lockmgr_printinfo(vp->v_vnlock);
2463 if (vp->v_data != NULL)
2468 #include <ddb/ddb.h>
2470 * List all of the locked vnodes in the system.
2471 * Called when debugging the kernel.
2473 DB_SHOW_COMMAND(lockedvnods, lockedvnodes)
2475 struct mount *mp, *nmp;
2479 * Note: because this is DDB, we can't obey the locking semantics
2480 * for these structures, which means we could catch an inconsistent
2481 * state and dereference a nasty pointer. Not much to be done
2484 printf("Locked vnodes\n");
2485 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2486 nmp = TAILQ_NEXT(mp, mnt_list);
2487 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2488 if (VOP_ISLOCKED(vp, NULL))
2491 nmp = TAILQ_NEXT(mp, mnt_list);
2497 * Fill in a struct xvfsconf based on a struct vfsconf.
2500 vfsconf2x(struct vfsconf *vfsp, struct xvfsconf *xvfsp)
2503 strcpy(xvfsp->vfc_name, vfsp->vfc_name);
2504 xvfsp->vfc_typenum = vfsp->vfc_typenum;
2505 xvfsp->vfc_refcount = vfsp->vfc_refcount;
2506 xvfsp->vfc_flags = vfsp->vfc_flags;
2508 * These are unused in userland, we keep them
2509 * to not break binary compatibility.
2511 xvfsp->vfc_vfsops = NULL;
2512 xvfsp->vfc_next = NULL;
2516 * Top level filesystem related information gathering.
2519 sysctl_vfs_conflist(SYSCTL_HANDLER_ARGS)
2521 struct vfsconf *vfsp;
2522 struct xvfsconf xvfsp;
2526 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
2527 bzero(&xvfsp, sizeof(xvfsp));
2528 vfsconf2x(vfsp, &xvfsp);
2529 error = SYSCTL_OUT(req, &xvfsp, sizeof xvfsp);
2536 SYSCTL_PROC(_vfs, OID_AUTO, conflist, CTLFLAG_RD, NULL, 0, sysctl_vfs_conflist,
2537 "S,xvfsconf", "List of all configured filesystems");
2539 #ifndef BURN_BRIDGES
2540 static int sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS);
2543 vfs_sysctl(SYSCTL_HANDLER_ARGS)
2545 int *name = (int *)arg1 - 1; /* XXX */
2546 u_int namelen = arg2 + 1; /* XXX */
2547 struct vfsconf *vfsp;
2548 struct xvfsconf xvfsp;
2550 printf("WARNING: userland calling deprecated sysctl, "
2551 "please rebuild world\n");
2553 #if 1 || defined(COMPAT_PRELITE2)
2554 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2556 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2560 case VFS_MAXTYPENUM:
2563 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2566 return (ENOTDIR); /* overloaded */
2567 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list)
2568 if (vfsp->vfc_typenum == name[2])
2571 return (EOPNOTSUPP);
2572 bzero(&xvfsp, sizeof(xvfsp));
2573 vfsconf2x(vfsp, &xvfsp);
2574 return (SYSCTL_OUT(req, &xvfsp, sizeof(xvfsp)));
2576 return (EOPNOTSUPP);
2579 static SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD | CTLFLAG_SKIP,
2580 vfs_sysctl, "Generic filesystem");
2582 #if 1 || defined(COMPAT_PRELITE2)
2585 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
2588 struct vfsconf *vfsp;
2589 struct ovfsconf ovfs;
2591 TAILQ_FOREACH(vfsp, &vfsconf, vfc_list) {
2592 bzero(&ovfs, sizeof(ovfs));
2593 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2594 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2595 ovfs.vfc_index = vfsp->vfc_typenum;
2596 ovfs.vfc_refcount = vfsp->vfc_refcount;
2597 ovfs.vfc_flags = vfsp->vfc_flags;
2598 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2605 #endif /* 1 || COMPAT_PRELITE2 */
2606 #endif /* !BURN_BRIDGES */
2608 #define KINFO_VNODESLOP 10
2611 * Dump vnode list (via sysctl).
2615 sysctl_vnode(SYSCTL_HANDLER_ARGS)
2618 struct thread *td = req->td;
2624 * Stale numvnodes access is not fatal here.
2627 len = (numvnodes + KINFO_VNODESLOP) * sizeof *xvn;
2629 /* Make an estimate */
2630 return (SYSCTL_OUT(req, 0, len));
2632 error = sysctl_wire_old_buffer(req, 0);
2635 xvn = malloc(len, M_TEMP, M_ZERO | M_WAITOK);
2637 mtx_lock(&mountlist_mtx);
2638 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
2639 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, td))
2642 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
2646 xvn[n].xv_size = sizeof *xvn;
2647 xvn[n].xv_vnode = vp;
2648 xvn[n].xv_id = 0; /* XXX compat */
2649 #define XV_COPY(field) xvn[n].xv_##field = vp->v_##field
2651 XV_COPY(writecount);
2657 xvn[n].xv_flag = vp->v_vflag;
2659 switch (vp->v_type) {
2666 if (vp->v_rdev == NULL) {
2670 xvn[n].xv_dev = dev2udev(vp->v_rdev);
2673 xvn[n].xv_socket = vp->v_socket;
2676 xvn[n].xv_fifo = vp->v_fifoinfo;
2681 /* shouldn't happen? */
2689 mtx_lock(&mountlist_mtx);
2694 mtx_unlock(&mountlist_mtx);
2696 error = SYSCTL_OUT(req, xvn, n * sizeof *xvn);
2701 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2702 0, 0, sysctl_vnode, "S,xvnode", "");
2706 * Unmount all filesystems. The list is traversed in reverse order
2707 * of mounting to avoid dependencies.
2716 KASSERT(curthread != NULL, ("vfs_unmountall: NULL curthread"));
2719 * Since this only runs when rebooting, it is not interlocked.
2721 while(!TAILQ_EMPTY(&mountlist)) {
2722 mp = TAILQ_LAST(&mountlist, mntlist);
2723 error = dounmount(mp, MNT_FORCE, td);
2725 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2726 printf("unmount of %s failed (",
2727 mp->mnt_stat.f_mntonname);
2731 printf("%d)\n", error);
2733 /* The unmount has removed mp from the mountlist */
2739 * perform msync on all vnodes under a mount point
2740 * the mount point must be locked.
2743 vfs_msync(struct mount *mp, int flags)
2745 struct vnode *vp, *nvp;
2746 struct vm_object *obj;
2752 TAILQ_FOREACH_SAFE(vp, &mp->mnt_nvnodelist, v_nmntvnodes, nvp) {
2753 if (vp->v_mount != mp) {
2760 if ((vp->v_iflag & VI_OBJDIRTY) &&
2761 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
2764 LK_EXCLUSIVE | LK_RETRY | LK_INTERLOCK,
2766 if (vp->v_vflag & VV_NOSYNC) { /* unlinked */
2774 VM_OBJECT_LOCK(obj);
2775 vm_object_page_clean(obj, 0, 0,
2777 OBJPC_SYNC : OBJPC_NOSYNC);
2778 VM_OBJECT_UNLOCK(obj);
2783 if (TAILQ_NEXT(vp, v_nmntvnodes) != nvp) {
2795 * Mark a vnode as free, putting it up for recycling.
2798 vfree(struct vnode *vp)
2801 CTR1(KTR_VFS, "vfree vp %p", vp);
2802 ASSERT_VI_LOCKED(vp, "vfree");
2803 mtx_lock(&vnode_free_list_mtx);
2804 VNASSERT(vp->v_op != NULL, vp, ("vfree: vnode already reclaimed."));
2805 VNASSERT((vp->v_iflag & VI_FREE) == 0, vp, ("vnode already free"));
2806 VNASSERT(VSHOULDFREE(vp), vp, ("vfree: freeing when we shouldn't"));
2807 VNASSERT((vp->v_iflag & VI_DOOMED) == 0, vp,
2808 ("vfree: Freeing doomed vnode"));
2809 if (vp->v_iflag & VI_AGE) {
2810 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2812 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2815 vp->v_iflag &= ~VI_AGE;
2816 vp->v_iflag |= VI_FREE;
2817 mtx_unlock(&vnode_free_list_mtx);
2821 * Opposite of vfree() - mark a vnode as in use.
2824 vbusy(struct vnode *vp)
2826 CTR1(KTR_VFS, "vbusy vp %p", vp);
2827 ASSERT_VI_LOCKED(vp, "vbusy");
2828 VNASSERT((vp->v_iflag & VI_FREE) != 0, vp, ("vnode not free"));
2829 VNASSERT(vp->v_op != NULL, vp, ("vbusy: vnode already reclaimed."));
2831 mtx_lock(&vnode_free_list_mtx);
2832 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2834 vp->v_iflag &= ~(VI_FREE|VI_AGE);
2835 mtx_unlock(&vnode_free_list_mtx);
2839 * Initalize per-vnode helper structure to hold poll-related state.
2842 v_addpollinfo(struct vnode *vp)
2844 struct vpollinfo *vi;
2846 vi = uma_zalloc(vnodepoll_zone, M_WAITOK);
2847 if (vp->v_pollinfo != NULL) {
2848 uma_zfree(vnodepoll_zone, vi);
2851 vp->v_pollinfo = vi;
2852 mtx_init(&vp->v_pollinfo->vpi_lock, "vnode pollinfo", NULL, MTX_DEF);
2853 knlist_init(&vp->v_pollinfo->vpi_selinfo.si_note, vp, vfs_knllock,
2854 vfs_knlunlock, vfs_knllocked);
2858 * Record a process's interest in events which might happen to
2859 * a vnode. Because poll uses the historic select-style interface
2860 * internally, this routine serves as both the ``check for any
2861 * pending events'' and the ``record my interest in future events''
2862 * functions. (These are done together, while the lock is held,
2863 * to avoid race conditions.)
2866 vn_pollrecord(vp, td, events)
2872 if (vp->v_pollinfo == NULL)
2874 mtx_lock(&vp->v_pollinfo->vpi_lock);
2875 if (vp->v_pollinfo->vpi_revents & events) {
2877 * This leaves events we are not interested
2878 * in available for the other process which
2879 * which presumably had requested them
2880 * (otherwise they would never have been
2883 events &= vp->v_pollinfo->vpi_revents;
2884 vp->v_pollinfo->vpi_revents &= ~events;
2886 mtx_unlock(&vp->v_pollinfo->vpi_lock);
2889 vp->v_pollinfo->vpi_events |= events;
2890 selrecord(td, &vp->v_pollinfo->vpi_selinfo);
2891 mtx_unlock(&vp->v_pollinfo->vpi_lock);
2896 * Routine to create and manage a filesystem syncer vnode.
2898 #define sync_close ((int (*)(struct vop_close_args *))nullop)
2899 static int sync_fsync(struct vop_fsync_args *);
2900 static int sync_inactive(struct vop_inactive_args *);
2901 static int sync_reclaim(struct vop_reclaim_args *);
2903 static struct vop_vector sync_vnodeops = {
2904 .vop_bypass = VOP_EOPNOTSUPP,
2905 .vop_close = sync_close, /* close */
2906 .vop_fsync = sync_fsync, /* fsync */
2907 .vop_inactive = sync_inactive, /* inactive */
2908 .vop_reclaim = sync_reclaim, /* reclaim */
2909 .vop_lock = vop_stdlock, /* lock */
2910 .vop_unlock = vop_stdunlock, /* unlock */
2911 .vop_islocked = vop_stdislocked, /* islocked */
2915 * Create a new filesystem syncer vnode for the specified mount point.
2918 vfs_allocate_syncvnode(mp)
2922 static long start, incr, next;
2925 /* Allocate a new vnode */
2926 if ((error = getnewvnode("syncer", mp, &sync_vnodeops, &vp)) != 0) {
2927 mp->mnt_syncer = NULL;
2932 * Place the vnode onto the syncer worklist. We attempt to
2933 * scatter them about on the list so that they will go off
2934 * at evenly distributed times even if all the filesystems
2935 * are mounted at once.
2938 if (next == 0 || next > syncer_maxdelay) {
2942 start = syncer_maxdelay / 2;
2943 incr = syncer_maxdelay;
2948 vn_syncer_add_to_worklist(&vp->v_bufobj,
2949 syncdelay > 0 ? next % syncdelay : 0);
2950 /* XXX - vn_syncer_add_to_worklist() also grabs and drops sync_mtx. */
2951 mtx_lock(&sync_mtx);
2953 mtx_unlock(&sync_mtx);
2955 mp->mnt_syncer = vp;
2960 * Do a lazy sync of the filesystem.
2964 struct vop_fsync_args /* {
2966 struct ucred *a_cred;
2968 struct thread *a_td;
2971 struct vnode *syncvp = ap->a_vp;
2972 struct mount *mp = syncvp->v_mount;
2973 struct thread *td = ap->a_td;
2974 int error, asyncflag;
2978 * We only need to do something if this is a lazy evaluation.
2980 if (ap->a_waitfor != MNT_LAZY)
2984 * Move ourselves to the back of the sync list.
2986 bo = &syncvp->v_bufobj;
2988 vn_syncer_add_to_worklist(bo, syncdelay);
2992 * Walk the list of vnodes pushing all that are dirty and
2993 * not already on the sync list.
2995 mtx_lock(&mountlist_mtx);
2996 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, td) != 0) {
2997 mtx_unlock(&mountlist_mtx);
3000 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) {
3004 asyncflag = mp->mnt_flag & MNT_ASYNC;
3005 mp->mnt_flag &= ~MNT_ASYNC;
3006 vfs_msync(mp, MNT_NOWAIT);
3007 error = VFS_SYNC(mp, MNT_LAZY, td);
3009 mp->mnt_flag |= MNT_ASYNC;
3010 vn_finished_write(mp);
3016 * The syncer vnode is no referenced.
3020 struct vop_inactive_args /* {
3022 struct thread *a_td;
3031 * The syncer vnode is no longer needed and is being decommissioned.
3033 * Modifications to the worklist must be protected by sync_mtx.
3037 struct vop_reclaim_args /* {
3041 struct vnode *vp = ap->a_vp;
3046 vp->v_mount->mnt_syncer = NULL;
3047 if (bo->bo_flag & BO_ONWORKLST) {
3048 mtx_lock(&sync_mtx);
3049 LIST_REMOVE(bo, bo_synclist);
3050 syncer_worklist_len--;
3052 mtx_unlock(&sync_mtx);
3053 bo->bo_flag &= ~BO_ONWORKLST;
3061 * Check if vnode represents a disk device
3072 if (vp->v_type != VCHR)
3074 else if (vp->v_rdev == NULL)
3076 else if (vp->v_rdev->si_devsw == NULL)
3078 else if (!(vp->v_rdev->si_devsw->d_flags & D_DISK))
3083 return (error == 0);
3087 * Common filesystem object access control check routine. Accepts a
3088 * vnode's type, "mode", uid and gid, requested access mode, credentials,
3089 * and optional call-by-reference privused argument allowing vaccess()
3090 * to indicate to the caller whether privilege was used to satisfy the
3091 * request (obsoleted). Returns 0 on success, or an errno on failure.
3094 vaccess(type, file_mode, file_uid, file_gid, acc_mode, cred, privused)
3109 * Look for a normal, non-privileged way to access the file/directory
3110 * as requested. If it exists, go with that.
3113 if (privused != NULL)
3118 /* Check the owner. */
3119 if (cred->cr_uid == file_uid) {
3120 dac_granted |= VADMIN;
3121 if (file_mode & S_IXUSR)
3122 dac_granted |= VEXEC;
3123 if (file_mode & S_IRUSR)
3124 dac_granted |= VREAD;
3125 if (file_mode & S_IWUSR)
3126 dac_granted |= (VWRITE | VAPPEND);
3128 if ((acc_mode & dac_granted) == acc_mode)
3134 /* Otherwise, check the groups (first match) */
3135 if (groupmember(file_gid, cred)) {
3136 if (file_mode & S_IXGRP)
3137 dac_granted |= VEXEC;
3138 if (file_mode & S_IRGRP)
3139 dac_granted |= VREAD;
3140 if (file_mode & S_IWGRP)
3141 dac_granted |= (VWRITE | VAPPEND);
3143 if ((acc_mode & dac_granted) == acc_mode)
3149 /* Otherwise, check everyone else. */
3150 if (file_mode & S_IXOTH)
3151 dac_granted |= VEXEC;
3152 if (file_mode & S_IROTH)
3153 dac_granted |= VREAD;
3154 if (file_mode & S_IWOTH)
3155 dac_granted |= (VWRITE | VAPPEND);
3156 if ((acc_mode & dac_granted) == acc_mode)
3160 if (!suser_cred(cred, SUSER_ALLOWJAIL)) {
3161 /* XXX audit: privilege used */
3162 if (privused != NULL)
3169 * Build a capability mask to determine if the set of capabilities
3170 * satisfies the requirements when combined with the granted mask
3172 * For each capability, if the capability is required, bitwise
3173 * or the request type onto the cap_granted mask.
3179 * For directories, use CAP_DAC_READ_SEARCH to satisfy
3180 * VEXEC requests, instead of CAP_DAC_EXECUTE.
3182 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3183 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, SUSER_ALLOWJAIL))
3184 cap_granted |= VEXEC;
3186 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) &&
3187 !cap_check(cred, NULL, CAP_DAC_EXECUTE, SUSER_ALLOWJAIL))
3188 cap_granted |= VEXEC;
3191 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) &&
3192 !cap_check(cred, NULL, CAP_DAC_READ_SEARCH, SUSER_ALLOWJAIL))
3193 cap_granted |= VREAD;
3195 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) &&
3196 !cap_check(cred, NULL, CAP_DAC_WRITE, SUSER_ALLOWJAIL))
3197 cap_granted |= (VWRITE | VAPPEND);
3199 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) &&
3200 !cap_check(cred, NULL, CAP_FOWNER, SUSER_ALLOWJAIL))
3201 cap_granted |= VADMIN;
3203 if ((acc_mode & (cap_granted | dac_granted)) == acc_mode) {
3204 /* XXX audit: privilege used */
3205 if (privused != NULL)
3211 return ((acc_mode & VADMIN) ? EPERM : EACCES);
3215 * Credential check based on process requesting service, and per-attribute
3219 extattr_check_cred(struct vnode *vp, int attrnamespace,
3220 struct ucred *cred, struct thread *td, int access)
3224 * Kernel-invoked always succeeds.
3230 * Do not allow privileged processes in jail to directly
3231 * manipulate system attributes.
3233 * XXX What capability should apply here?
3234 * Probably CAP_SYS_SETFFLAG.
3236 switch (attrnamespace) {
3237 case EXTATTR_NAMESPACE_SYSTEM:
3238 /* Potentially should be: return (EPERM); */
3239 return (suser_cred(cred, 0));
3240 case EXTATTR_NAMESPACE_USER:
3241 return (VOP_ACCESS(vp, access, cred, td));
3247 #ifdef DEBUG_VFS_LOCKS
3249 * This only exists to supress warnings from unlocked specfs accesses. It is
3250 * no longer ok to have an unlocked VFS.
3252 #define IGNORE_LOCK(vp) ((vp)->v_type == VCHR || (vp)->v_type == VBAD)
3254 int vfs_badlock_ddb = 1; /* Drop into debugger on violation. */
3255 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_ddb, CTLFLAG_RW, &vfs_badlock_ddb, 0, "");
3257 int vfs_badlock_mutex = 1; /* Check for interlock across VOPs. */
3258 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_mutex, CTLFLAG_RW, &vfs_badlock_mutex, 0, "");
3260 int vfs_badlock_print = 1; /* Print lock violations. */
3261 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_print, CTLFLAG_RW, &vfs_badlock_print, 0, "");
3264 int vfs_badlock_backtrace = 1; /* Print backtrace at lock violations. */
3265 SYSCTL_INT(_debug, OID_AUTO, vfs_badlock_backtrace, CTLFLAG_RW, &vfs_badlock_backtrace, 0, "");
3269 vfs_badlock(const char *msg, const char *str, struct vnode *vp)
3273 if (vfs_badlock_backtrace)
3276 if (vfs_badlock_print)
3277 printf("%s: %p %s\n", str, (void *)vp, msg);
3278 if (vfs_badlock_ddb)
3279 kdb_enter("lock violation");
3283 assert_vi_locked(struct vnode *vp, const char *str)
3286 if (vfs_badlock_mutex && !mtx_owned(VI_MTX(vp)))
3287 vfs_badlock("interlock is not locked but should be", str, vp);
3291 assert_vi_unlocked(struct vnode *vp, const char *str)
3294 if (vfs_badlock_mutex && mtx_owned(VI_MTX(vp)))
3295 vfs_badlock("interlock is locked but should not be", str, vp);
3299 assert_vop_locked(struct vnode *vp, const char *str)
3302 if (vp && !IGNORE_LOCK(vp) && VOP_ISLOCKED(vp, NULL) == 0)
3303 vfs_badlock("is not locked but should be", str, vp);
3307 assert_vop_unlocked(struct vnode *vp, const char *str)
3310 if (vp && !IGNORE_LOCK(vp) &&
3311 VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE)
3312 vfs_badlock("is locked but should not be", str, vp);
3316 assert_vop_elocked(struct vnode *vp, const char *str)
3319 if (vp && !IGNORE_LOCK(vp) &&
3320 VOP_ISLOCKED(vp, curthread) != LK_EXCLUSIVE)
3321 vfs_badlock("is not exclusive locked but should be", str, vp);
3326 assert_vop_elocked_other(struct vnode *vp, const char *str)
3329 if (vp && !IGNORE_LOCK(vp) &&
3330 VOP_ISLOCKED(vp, curthread) != LK_EXCLOTHER)
3331 vfs_badlock("is not exclusive locked by another thread",
3336 assert_vop_slocked(struct vnode *vp, const char *str)
3339 if (vp && !IGNORE_LOCK(vp) &&
3340 VOP_ISLOCKED(vp, curthread) != LK_SHARED)
3341 vfs_badlock("is not locked shared but should be", str, vp);
3344 #endif /* DEBUG_VFS_LOCKS */
3347 vop_rename_pre(void *ap)
3349 struct vop_rename_args *a = ap;
3351 #ifdef DEBUG_VFS_LOCKS
3353 ASSERT_VI_UNLOCKED(a->a_tvp, "VOP_RENAME");
3354 ASSERT_VI_UNLOCKED(a->a_tdvp, "VOP_RENAME");
3355 ASSERT_VI_UNLOCKED(a->a_fvp, "VOP_RENAME");
3356 ASSERT_VI_UNLOCKED(a->a_fdvp, "VOP_RENAME");
3358 /* Check the source (from). */
3359 if (a->a_tdvp != a->a_fdvp)
3360 ASSERT_VOP_UNLOCKED(a->a_fdvp, "vop_rename: fdvp locked");
3361 if (a->a_tvp != a->a_fvp)
3362 ASSERT_VOP_UNLOCKED(a->a_fvp, "vop_rename: tvp locked");
3364 /* Check the target. */
3366 ASSERT_VOP_LOCKED(a->a_tvp, "vop_rename: tvp not locked");
3367 ASSERT_VOP_LOCKED(a->a_tdvp, "vop_rename: tdvp not locked");
3369 if (a->a_tdvp != a->a_fdvp)
3371 if (a->a_tvp != a->a_fvp)
3379 vop_strategy_pre(void *ap)
3381 #ifdef DEBUG_VFS_LOCKS
3382 struct vop_strategy_args *a;
3389 * Cluster ops lock their component buffers but not the IO container.
3391 if ((bp->b_flags & B_CLUSTER) != 0)
3394 if (BUF_REFCNT(bp) < 1) {
3395 if (vfs_badlock_print)
3397 "VOP_STRATEGY: bp is not locked but should be\n");
3398 if (vfs_badlock_ddb)
3399 kdb_enter("lock violation");
3405 vop_lookup_pre(void *ap)
3407 #ifdef DEBUG_VFS_LOCKS
3408 struct vop_lookup_args *a;
3413 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3414 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3419 vop_lookup_post(void *ap, int rc)
3421 #ifdef DEBUG_VFS_LOCKS
3422 struct vop_lookup_args *a;
3430 ASSERT_VI_UNLOCKED(dvp, "VOP_LOOKUP");
3431 ASSERT_VOP_LOCKED(dvp, "VOP_LOOKUP");
3434 ASSERT_VOP_LOCKED(vp, "VOP_LOOKUP (child)");
3439 vop_lock_pre(void *ap)
3441 #ifdef DEBUG_VFS_LOCKS
3442 struct vop_lock_args *a = ap;
3444 if ((a->a_flags & LK_INTERLOCK) == 0)
3445 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3447 ASSERT_VI_LOCKED(a->a_vp, "VOP_LOCK");
3452 vop_lock_post(void *ap, int rc)
3454 #ifdef DEBUG_VFS_LOCKS
3455 struct vop_lock_args *a = ap;
3457 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_LOCK");
3459 ASSERT_VOP_LOCKED(a->a_vp, "VOP_LOCK");
3464 vop_unlock_pre(void *ap)
3466 #ifdef DEBUG_VFS_LOCKS
3467 struct vop_unlock_args *a = ap;
3469 if (a->a_flags & LK_INTERLOCK)
3470 ASSERT_VI_LOCKED(a->a_vp, "VOP_UNLOCK");
3471 ASSERT_VOP_LOCKED(a->a_vp, "VOP_UNLOCK");
3476 vop_unlock_post(void *ap, int rc)
3478 #ifdef DEBUG_VFS_LOCKS
3479 struct vop_unlock_args *a = ap;
3481 if (a->a_flags & LK_INTERLOCK)
3482 ASSERT_VI_UNLOCKED(a->a_vp, "VOP_UNLOCK");
3487 vop_create_post(void *ap, int rc)
3489 struct vop_create_args *a = ap;
3492 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3496 vop_link_post(void *ap, int rc)
3498 struct vop_link_args *a = ap;
3501 VFS_KNOTE_LOCKED(a->a_vp, NOTE_LINK);
3502 VFS_KNOTE_LOCKED(a->a_tdvp, NOTE_WRITE);
3507 vop_mkdir_post(void *ap, int rc)
3509 struct vop_mkdir_args *a = ap;
3512 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3516 vop_mknod_post(void *ap, int rc)
3518 struct vop_mknod_args *a = ap;
3521 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3525 vop_remove_post(void *ap, int rc)
3527 struct vop_remove_args *a = ap;
3530 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3531 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3536 vop_rename_post(void *ap, int rc)
3538 struct vop_rename_args *a = ap;
3541 VFS_KNOTE_UNLOCKED(a->a_fdvp, NOTE_WRITE);
3542 VFS_KNOTE_UNLOCKED(a->a_tdvp, NOTE_WRITE);
3543 VFS_KNOTE_UNLOCKED(a->a_fvp, NOTE_RENAME);
3545 VFS_KNOTE_UNLOCKED(a->a_tvp, NOTE_DELETE);
3547 if (a->a_tdvp != a->a_fdvp)
3549 if (a->a_tvp != a->a_fvp)
3557 vop_rmdir_post(void *ap, int rc)
3559 struct vop_rmdir_args *a = ap;
3562 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE | NOTE_LINK);
3563 VFS_KNOTE_LOCKED(a->a_vp, NOTE_DELETE);
3568 vop_setattr_post(void *ap, int rc)
3570 struct vop_setattr_args *a = ap;
3573 VFS_KNOTE_LOCKED(a->a_vp, NOTE_ATTRIB);
3577 vop_symlink_post(void *ap, int rc)
3579 struct vop_symlink_args *a = ap;
3582 VFS_KNOTE_LOCKED(a->a_dvp, NOTE_WRITE);
3585 static struct knlist fs_knlist;
3588 vfs_event_init(void *arg)
3590 knlist_init(&fs_knlist, NULL, NULL, NULL, NULL);
3592 /* XXX - correct order? */
3593 SYSINIT(vfs_knlist, SI_SUB_VFS, SI_ORDER_ANY, vfs_event_init, NULL);
3596 vfs_event_signal(fsid_t *fsid, u_int32_t event, intptr_t data __unused)
3599 KNOTE_UNLOCKED(&fs_knlist, event);
3602 static int filt_fsattach(struct knote *kn);
3603 static void filt_fsdetach(struct knote *kn);
3604 static int filt_fsevent(struct knote *kn, long hint);
3606 struct filterops fs_filtops =
3607 { 0, filt_fsattach, filt_fsdetach, filt_fsevent };
3610 filt_fsattach(struct knote *kn)
3613 kn->kn_flags |= EV_CLEAR;
3614 knlist_add(&fs_knlist, kn, 0);
3619 filt_fsdetach(struct knote *kn)
3622 knlist_remove(&fs_knlist, kn, 0);
3626 filt_fsevent(struct knote *kn, long hint)
3629 kn->kn_fflags |= hint;
3630 return (kn->kn_fflags != 0);
3634 sysctl_vfs_ctl(SYSCTL_HANDLER_ARGS)
3640 error = SYSCTL_IN(req, &vc, sizeof(vc));
3643 if (vc.vc_vers != VFS_CTL_VERS1)
3645 mp = vfs_getvfs(&vc.vc_fsid);
3648 /* ensure that a specific sysctl goes to the right filesystem. */
3649 if (strcmp(vc.vc_fstypename, "*") != 0 &&
3650 strcmp(vc.vc_fstypename, mp->mnt_vfc->vfc_name) != 0) {
3653 VCTLTOREQ(&vc, req);
3654 return (VFS_SYSCTL(mp, vc.vc_op, req));
3657 SYSCTL_PROC(_vfs, OID_AUTO, ctl, CTLFLAG_WR,
3658 NULL, 0, sysctl_vfs_ctl, "", "Sysctl by fsid");
3661 * Function to initialize a va_filerev field sensibly.
3662 * XXX: Wouldn't a random number make a lot more sense ??
3665 init_va_filerev(void)
3670 return (((u_quad_t)bt.sec << 32LL) | (bt.frac >> 32LL));
3673 static int filt_vfsread(struct knote *kn, long hint);
3674 static int filt_vfswrite(struct knote *kn, long hint);
3675 static int filt_vfsvnode(struct knote *kn, long hint);
3676 static void filt_vfsdetach(struct knote *kn);
3677 static struct filterops vfsread_filtops =
3678 { 1, NULL, filt_vfsdetach, filt_vfsread };
3679 static struct filterops vfswrite_filtops =
3680 { 1, NULL, filt_vfsdetach, filt_vfswrite };
3681 static struct filterops vfsvnode_filtops =
3682 { 1, NULL, filt_vfsdetach, filt_vfsvnode };
3685 vfs_knllock(void *arg)
3687 struct vnode *vp = arg;
3689 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
3693 vfs_knlunlock(void *arg)
3695 struct vnode *vp = arg;
3697 VOP_UNLOCK(vp, 0, curthread);
3701 vfs_knllocked(void *arg)
3703 struct vnode *vp = arg;
3705 return (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE);
3709 vfs_kqfilter(struct vop_kqfilter_args *ap)
3711 struct vnode *vp = ap->a_vp;
3712 struct knote *kn = ap->a_kn;
3715 switch (kn->kn_filter) {
3717 kn->kn_fop = &vfsread_filtops;
3720 kn->kn_fop = &vfswrite_filtops;
3723 kn->kn_fop = &vfsvnode_filtops;
3729 kn->kn_hook = (caddr_t)vp;
3731 if (vp->v_pollinfo == NULL)
3733 if (vp->v_pollinfo == NULL)
3735 knl = &vp->v_pollinfo->vpi_selinfo.si_note;
3736 knlist_add(knl, kn, 0);
3742 * Detach knote from vnode
3745 filt_vfsdetach(struct knote *kn)
3747 struct vnode *vp = (struct vnode *)kn->kn_hook;
3749 KASSERT(vp->v_pollinfo != NULL, ("Missing v_pollinfo"));
3750 knlist_remove(&vp->v_pollinfo->vpi_selinfo.si_note, kn, 0);
3755 filt_vfsread(struct knote *kn, long hint)
3757 struct vnode *vp = (struct vnode *)kn->kn_hook;
3761 * filesystem is gone, so set the EOF flag and schedule
3762 * the knote for deletion.
3764 if (hint == NOTE_REVOKE) {
3765 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3769 if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread))
3772 kn->kn_data = va.va_size - kn->kn_fp->f_offset;
3773 return (kn->kn_data != 0);
3778 filt_vfswrite(struct knote *kn, long hint)
3781 * filesystem is gone, so set the EOF flag and schedule
3782 * the knote for deletion.
3784 if (hint == NOTE_REVOKE)
3785 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
3792 filt_vfsvnode(struct knote *kn, long hint)
3794 if (kn->kn_sfflags & hint)
3795 kn->kn_fflags |= hint;
3796 if (hint == NOTE_REVOKE) {
3797 kn->kn_flags |= EV_EOF;
3800 return (kn->kn_fflags != 0);