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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
43 * External virtual filesystem routines
47 #include <sys/param.h>
48 #include <sys/systm.h>
51 #include <sys/dirent.h>
52 #include <sys/domain.h>
53 #include <sys/eventhandler.h>
54 #include <sys/fcntl.h>
55 #include <sys/kernel.h>
56 #include <sys/kthread.h>
57 #include <sys/malloc.h>
58 #include <sys/mount.h>
59 #include <sys/namei.h>
61 #include <sys/reboot.h>
62 #include <sys/socket.h>
64 #include <sys/sysctl.h>
65 #include <sys/vmmeter.h>
66 #include <sys/vnode.h>
68 #include <machine/limits.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_extern.h>
74 #include <vm/vm_map.h>
75 #include <vm/vm_page.h>
76 #include <vm/vm_pager.h>
77 #include <vm/vnode_pager.h>
78 #include <vm/vm_zone.h>
80 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
82 static void insmntque __P((struct vnode *vp, struct mount *mp));
83 static void vclean __P((struct vnode *vp, int flags, struct proc *p));
84 static void vfree __P((struct vnode *));
85 static void vgonel __P((struct vnode *vp, struct proc *p));
86 static unsigned long numvnodes;
87 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
89 enum vtype iftovt_tab[16] = {
90 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
91 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
94 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
95 S_IFSOCK, S_IFIFO, S_IFMT,
98 static TAILQ_HEAD(freelst, vnode) vnode_free_list; /* vnode free list */
99 struct tobefreelist vnode_tobefree_list; /* vnode free list */
101 static u_long wantfreevnodes = 25;
102 SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
103 static u_long freevnodes = 0;
104 SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
106 static int reassignbufcalls;
107 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, "");
108 static int reassignbufloops;
109 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW, &reassignbufloops, 0, "");
110 static int reassignbufsortgood;
111 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW, &reassignbufsortgood, 0, "");
112 static int reassignbufsortbad;
113 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW, &reassignbufsortbad, 0, "");
114 static int reassignbufmethod = 1;
115 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW, &reassignbufmethod, 0, "");
117 #ifdef ENABLE_VFS_IOOPT
119 SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
122 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); /* mounted fs */
123 struct simplelock mountlist_slock;
124 struct simplelock mntvnode_slock;
125 int nfs_mount_type = -1;
126 #ifndef NULL_SIMPLELOCKS
127 static struct simplelock mntid_slock;
128 static struct simplelock vnode_free_list_slock;
129 static struct simplelock spechash_slock;
131 struct nfs_public nfs_pub; /* publicly exported FS */
132 static vm_zone_t vnode_zone;
135 * The workitem queue.
137 #define SYNCER_MAXDELAY 32
138 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
139 time_t syncdelay = 30; /* max time to delay syncing data */
140 time_t filedelay = 30; /* time to delay syncing files */
141 SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, "");
142 time_t dirdelay = 29; /* time to delay syncing directories */
143 SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, "");
144 time_t metadelay = 28; /* time to delay syncing metadata */
145 SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, "");
146 static int rushjob; /* number of slots to run ASAP */
147 static int stat_rush_requests; /* number of times I/O speeded up */
148 SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, "");
150 static int syncer_delayno = 0;
151 static long syncer_mask;
152 LIST_HEAD(synclist, vnode);
153 static struct synclist *syncer_workitem_pending;
156 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
157 &desiredvnodes, 0, "Maximum number of vnodes");
159 static void vfs_free_addrlist __P((struct netexport *nep));
160 static int vfs_free_netcred __P((struct radix_node *rn, void *w));
161 static int vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep,
162 struct export_args *argp));
165 * Initialize the vnode management data structures.
171 desiredvnodes = maxproc + cnt.v_page_count / 4;
172 simple_lock_init(&mntvnode_slock);
173 simple_lock_init(&mntid_slock);
174 simple_lock_init(&spechash_slock);
175 TAILQ_INIT(&vnode_free_list);
176 TAILQ_INIT(&vnode_tobefree_list);
177 simple_lock_init(&vnode_free_list_slock);
178 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
180 * Initialize the filesystem syncer.
182 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
184 syncer_maxdelay = syncer_mask + 1;
188 * Mark a mount point as busy. Used to synchronize access and to delay
189 * unmounting. Interlock is not released on failure.
192 vfs_busy(mp, flags, interlkp, p)
195 struct simplelock *interlkp;
200 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
201 if (flags & LK_NOWAIT)
203 mp->mnt_kern_flag |= MNTK_MWAIT;
205 simple_unlock(interlkp);
208 * Since all busy locks are shared except the exclusive
209 * lock granted when unmounting, the only place that a
210 * wakeup needs to be done is at the release of the
211 * exclusive lock at the end of dounmount.
213 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
215 simple_lock(interlkp);
219 lkflags = LK_SHARED | LK_NOPAUSE;
221 lkflags |= LK_INTERLOCK;
222 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p))
223 panic("vfs_busy: unexpected lock failure");
228 * Free a busy filesystem.
236 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p);
240 * Lookup a filesystem type, and if found allocate and initialize
241 * a mount structure for it.
243 * Devname is usually updated by mount(8) after booting.
246 vfs_rootmountalloc(fstypename, devname, mpp)
251 struct proc *p = curproc; /* XXX */
252 struct vfsconf *vfsp;
255 if (fstypename == NULL)
257 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
258 if (!strcmp(vfsp->vfc_name, fstypename))
262 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
263 bzero((char *)mp, (u_long)sizeof(struct mount));
264 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
265 (void)vfs_busy(mp, LK_NOWAIT, 0, p);
266 LIST_INIT(&mp->mnt_vnodelist);
268 mp->mnt_op = vfsp->vfc_vfsops;
269 mp->mnt_flag = MNT_RDONLY;
270 mp->mnt_vnodecovered = NULLVP;
271 vfsp->vfc_refcount++;
272 mp->mnt_iosize_max = DFLTPHYS;
273 mp->mnt_stat.f_type = vfsp->vfc_typenum;
274 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
275 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
276 mp->mnt_stat.f_mntonname[0] = '/';
277 mp->mnt_stat.f_mntonname[1] = 0;
278 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
284 * Find an appropriate filesystem to use for the root. If a filesystem
285 * has not been preselected, walk through the list of known filesystems
286 * trying those that have mountroot routines, and try them until one
287 * works or we have tried them all.
289 #ifdef notdef /* XXX JH */
291 lite2_vfs_mountroot()
293 struct vfsconf *vfsp;
294 extern int (*lite2_mountroot) __P((void));
297 if (lite2_mountroot != NULL)
298 return ((*lite2_mountroot)());
299 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
300 if (vfsp->vfc_mountroot == NULL)
302 if ((error = (*vfsp->vfc_mountroot)()) == 0)
304 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
311 * Lookup a mount point by filesystem identifier.
317 register struct mount *mp;
319 simple_lock(&mountlist_slock);
320 TAILQ_FOREACH(mp, &mountlist, mnt_list) {
321 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
322 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
323 simple_unlock(&mountlist_slock);
327 simple_unlock(&mountlist_slock);
328 return ((struct mount *) 0);
332 * Get a new unique fsid
334 * Keep in mind that several mounts may be running in parallel,
335 * so always increment mntid_base even if lower numbers are available.
338 static u_short mntid_base;
347 simple_lock(&mntid_slock);
349 mtype = mp->mnt_vfc->vfc_typenum;
351 tfsid.val[0] = makeudev(255, mtype + (mntid_base << 16));
352 tfsid.val[1] = mtype;
354 if (vfs_getvfs(&tfsid) == NULL)
358 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
359 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1];
361 simple_unlock(&mntid_slock);
365 * Knob to control the precision of file timestamps:
367 * 0 = seconds only; nanoseconds zeroed.
368 * 1 = seconds and nanoseconds, accurate within 1/HZ.
369 * 2 = seconds and nanoseconds, truncated to microseconds.
370 * >=3 = seconds and nanoseconds, maximum precision.
372 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
374 static int timestamp_precision = TSP_SEC;
375 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
376 ×tamp_precision, 0, "");
379 * Get a current timestamp.
383 struct timespec *tsp;
387 switch (timestamp_precision) {
389 tsp->tv_sec = time_second;
397 TIMEVAL_TO_TIMESPEC(&tv, tsp);
407 * Set vnode attributes to VNOVAL
411 register struct vattr *vap;
415 vap->va_size = VNOVAL;
416 vap->va_bytes = VNOVAL;
417 vap->va_mode = VNOVAL;
418 vap->va_nlink = VNOVAL;
419 vap->va_uid = VNOVAL;
420 vap->va_gid = VNOVAL;
421 vap->va_fsid = VNOVAL;
422 vap->va_fileid = VNOVAL;
423 vap->va_blocksize = VNOVAL;
424 vap->va_rdev = VNOVAL;
425 vap->va_atime.tv_sec = VNOVAL;
426 vap->va_atime.tv_nsec = VNOVAL;
427 vap->va_mtime.tv_sec = VNOVAL;
428 vap->va_mtime.tv_nsec = VNOVAL;
429 vap->va_ctime.tv_sec = VNOVAL;
430 vap->va_ctime.tv_nsec = VNOVAL;
431 vap->va_flags = VNOVAL;
432 vap->va_gen = VNOVAL;
437 * Routines having to do with the management of the vnode table.
439 extern vop_t **dead_vnodeop_p;
442 * Return the next vnode from the free list.
445 getnewvnode(tag, mp, vops, vpp)
452 struct proc *p = curproc; /* XXX */
453 struct vnode *vp, *tvp, *nvp;
455 TAILQ_HEAD(freelst, vnode) vnode_tmp_list;
458 * We take the least recently used vnode from the freelist
459 * if we can get it and it has no cached pages, and no
460 * namecache entries are relative to it.
461 * Otherwise we allocate a new vnode
465 simple_lock(&vnode_free_list_slock);
466 TAILQ_INIT(&vnode_tmp_list);
468 for (vp = TAILQ_FIRST(&vnode_tobefree_list); vp; vp = nvp) {
469 nvp = TAILQ_NEXT(vp, v_freelist);
470 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
471 if (vp->v_flag & VAGE) {
472 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
474 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
476 vp->v_flag &= ~(VTBFREE|VAGE);
479 panic("tobe free vnode isn't");
483 if (wantfreevnodes && freevnodes < wantfreevnodes) {
485 } else if (!wantfreevnodes && freevnodes <= desiredvnodes) {
487 * XXX: this is only here to be backwards compatible
491 for (vp = TAILQ_FIRST(&vnode_free_list); vp; vp = nvp) {
492 nvp = TAILQ_NEXT(vp, v_freelist);
493 if (!simple_lock_try(&vp->v_interlock))
496 panic("free vnode isn't");
498 object = vp->v_object;
499 if (object && (object->resident_page_count || object->ref_count)) {
500 printf("object inconsistant state: RPC: %d, RC: %d\n",
501 object->resident_page_count, object->ref_count);
502 /* Don't recycle if it's caching some pages */
503 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
504 TAILQ_INSERT_TAIL(&vnode_tmp_list, vp, v_freelist);
506 } else if (LIST_FIRST(&vp->v_cache_src)) {
507 /* Don't recycle if active in the namecache */
508 simple_unlock(&vp->v_interlock);
516 for (tvp = TAILQ_FIRST(&vnode_tmp_list); tvp; tvp = nvp) {
517 nvp = TAILQ_NEXT(tvp, v_freelist);
518 TAILQ_REMOVE(&vnode_tmp_list, tvp, v_freelist);
519 TAILQ_INSERT_TAIL(&vnode_free_list, tvp, v_freelist);
520 simple_unlock(&tvp->v_interlock);
524 vp->v_flag |= VDOOMED;
525 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
527 simple_unlock(&vnode_free_list_slock);
530 if (vp->v_type != VBAD) {
533 simple_unlock(&vp->v_interlock);
541 panic("cleaned vnode isn't");
544 panic("Clean vnode has pending I/O's");
554 vp->v_writecount = 0; /* XXX */
556 simple_unlock(&vnode_free_list_slock);
557 vp = (struct vnode *) zalloc(vnode_zone);
558 bzero((char *) vp, sizeof *vp);
559 simple_lock_init(&vp->v_interlock);
562 LIST_INIT(&vp->v_cache_src);
563 TAILQ_INIT(&vp->v_cache_dst);
567 TAILQ_INIT(&vp->v_cleanblkhd);
568 TAILQ_INIT(&vp->v_dirtyblkhd);
578 vfs_object_create(vp, p, p->p_ucred);
583 * Move a vnode from one mount queue to another.
587 register struct vnode *vp;
588 register struct mount *mp;
591 simple_lock(&mntvnode_slock);
593 * Delete from old mount point vnode list, if on one.
595 if (vp->v_mount != NULL)
596 LIST_REMOVE(vp, v_mntvnodes);
598 * Insert into list of vnodes for the new mount point, if available.
600 if ((vp->v_mount = mp) == NULL) {
601 simple_unlock(&mntvnode_slock);
604 LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
605 simple_unlock(&mntvnode_slock);
609 * Update outstanding I/O count and do wakeup if requested.
613 register struct buf *bp;
615 register struct vnode *vp;
617 bp->b_flags &= ~B_WRITEINPROG;
618 if ((vp = bp->b_vp)) {
620 if (vp->v_numoutput < 0)
621 panic("vwakeup: neg numoutput");
622 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
623 vp->v_flag &= ~VBWAIT;
624 wakeup((caddr_t) &vp->v_numoutput);
630 * Flush out and invalidate all buffers associated with a vnode.
631 * Called with the underlying object locked.
634 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
635 register struct vnode *vp;
639 int slpflag, slptimeo;
641 register struct buf *bp;
642 struct buf *nbp, *blist;
646 if (flags & V_SAVE) {
648 while (vp->v_numoutput) {
649 vp->v_flag |= VBWAIT;
650 error = tsleep((caddr_t)&vp->v_numoutput,
651 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
657 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
659 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0)
662 if (vp->v_numoutput > 0 ||
663 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
664 panic("vinvalbuf: dirty bufs");
670 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
672 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
676 for (bp = blist; bp; bp = nbp) {
677 nbp = TAILQ_NEXT(bp, b_vnbufs);
678 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
679 error = BUF_TIMELOCK(bp,
680 LK_EXCLUSIVE | LK_SLEEPFAIL,
681 "vinvalbuf", slpflag, slptimeo);
688 * XXX Since there are no node locks for NFS, I
689 * believe there is a slight chance that a delayed
690 * write will occur while sleeping just above, so
691 * check for it. Note that vfs_bio_awrite expects
692 * buffers to reside on a queue, while VOP_BWRITE and
695 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
698 if (bp->b_vp == vp) {
699 if (bp->b_flags & B_CLUSTEROK) {
704 bp->b_flags |= B_ASYNC;
705 VOP_BWRITE(bp->b_vp, bp);
709 (void) VOP_BWRITE(bp->b_vp, bp);
714 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
715 bp->b_flags &= ~B_ASYNC;
720 while (vp->v_numoutput > 0) {
721 vp->v_flag |= VBWAIT;
722 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
728 * Destroy the copy in the VM cache, too.
730 simple_lock(&vp->v_interlock);
731 object = vp->v_object;
732 if (object != NULL) {
733 vm_object_page_remove(object, 0, 0,
734 (flags & V_SAVE) ? TRUE : FALSE);
736 simple_unlock(&vp->v_interlock);
738 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
739 panic("vinvalbuf: flush failed");
744 * Truncate a file's buffer and pages to a specified length. This
745 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
749 vtruncbuf(vp, cred, p, length, blksize)
750 register struct vnode *vp;
756 register struct buf *bp;
762 * Round up to the *next* lbn.
764 trunclbn = (length + blksize - 1) / blksize;
771 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
772 nbp = TAILQ_NEXT(bp, b_vnbufs);
773 if (bp->b_lblkno >= trunclbn) {
774 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
775 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
779 bp->b_flags |= (B_INVAL | B_RELBUF);
780 bp->b_flags &= ~B_ASYNC;
785 (((nbp->b_xflags & BX_VNCLEAN) == 0) ||
787 (nbp->b_flags & B_DELWRI))) {
793 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
794 nbp = TAILQ_NEXT(bp, b_vnbufs);
795 if (bp->b_lblkno >= trunclbn) {
796 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
797 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
801 bp->b_flags |= (B_INVAL | B_RELBUF);
802 bp->b_flags &= ~B_ASYNC;
807 (((nbp->b_xflags & BX_VNDIRTY) == 0) ||
809 (nbp->b_flags & B_DELWRI) == 0)) {
818 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
819 nbp = TAILQ_NEXT(bp, b_vnbufs);
820 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
821 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
822 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL);
826 if (bp->b_vp == vp) {
827 bp->b_flags |= B_ASYNC;
829 bp->b_flags &= ~B_ASYNC;
831 VOP_BWRITE(bp->b_vp, bp);
839 while (vp->v_numoutput > 0) {
840 vp->v_flag |= VBWAIT;
841 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
846 vnode_pager_setsize(vp, length);
852 * Associate a buffer with a vnode.
856 register struct vnode *vp;
857 register struct buf *bp;
861 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
865 bp->b_dev = vn_todev(vp);
867 * Insert onto list for new vnode.
870 bp->b_xflags |= BX_VNCLEAN;
871 bp->b_xflags &= ~BX_VNDIRTY;
872 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
877 * Disassociate a buffer from a vnode.
881 register struct buf *bp;
884 struct buflists *listheadp;
887 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
890 * Delete from old vnode list, if on one.
894 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
895 if (bp->b_xflags & BX_VNDIRTY)
896 listheadp = &vp->v_dirtyblkhd;
898 listheadp = &vp->v_cleanblkhd;
899 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
900 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
902 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
903 vp->v_flag &= ~VONWORKLST;
904 LIST_REMOVE(vp, v_synclist);
907 bp->b_vp = (struct vnode *) 0;
912 * The workitem queue.
914 * It is useful to delay writes of file data and filesystem metadata
915 * for tens of seconds so that quickly created and deleted files need
916 * not waste disk bandwidth being created and removed. To realize this,
917 * we append vnodes to a "workitem" queue. When running with a soft
918 * updates implementation, most pending metadata dependencies should
919 * not wait for more than a few seconds. Thus, mounted on block devices
920 * are delayed only about a half the time that file data is delayed.
921 * Similarly, directory updates are more critical, so are only delayed
922 * about a third the time that file data is delayed. Thus, there are
923 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
924 * one each second (driven off the filesystem syncer process). The
925 * syncer_delayno variable indicates the next queue that is to be processed.
926 * Items that need to be processed soon are placed in this queue:
928 * syncer_workitem_pending[syncer_delayno]
930 * A delay of fifteen seconds is done by placing the request fifteen
931 * entries later in the queue:
933 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
938 * Add an item to the syncer work queue.
941 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
947 if (vp->v_flag & VONWORKLST) {
948 LIST_REMOVE(vp, v_synclist);
951 if (delay > syncer_maxdelay - 2)
952 delay = syncer_maxdelay - 2;
953 slot = (syncer_delayno + delay) & syncer_mask;
955 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
956 vp->v_flag |= VONWORKLST;
960 struct proc *updateproc;
961 static void sched_sync __P((void));
962 static struct kproc_desc up_kp = {
967 SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
970 * System filesystem synchronizer daemon.
975 struct synclist *slp;
979 struct proc *p = updateproc;
981 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, p,
984 p->p_flag |= P_BUFEXHAUST;
987 kproc_suspend_loop(p);
989 starttime = time_second;
992 * Push files whose dirty time has expired. Be careful
993 * of interrupt race on slp queue.
996 slp = &syncer_workitem_pending[syncer_delayno];
998 if (syncer_delayno == syncer_maxdelay)
1002 while ((vp = LIST_FIRST(slp)) != NULL) {
1003 if (VOP_ISLOCKED(vp, NULL) == 0) {
1004 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
1005 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p);
1006 VOP_UNLOCK(vp, 0, p);
1009 if (LIST_FIRST(slp) == vp) {
1011 * Note: v_tag VT_VFS vps can remain on the
1012 * worklist too with no dirty blocks, but
1013 * since sync_fsync() moves it to a different
1016 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
1018 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
1020 * Put us back on the worklist. The worklist
1021 * routine will remove us from our current
1022 * position and then add us back in at a later
1025 vn_syncer_add_to_worklist(vp, syncdelay);
1031 * Do soft update processing.
1034 (*bioops.io_sync)(NULL);
1037 * The variable rushjob allows the kernel to speed up the
1038 * processing of the filesystem syncer process. A rushjob
1039 * value of N tells the filesystem syncer to process the next
1040 * N seconds worth of work on its queue ASAP. Currently rushjob
1041 * is used by the soft update code to speed up the filesystem
1042 * syncer process when the incore state is getting so far
1043 * ahead of the disk that the kernel memory pool is being
1044 * threatened with exhaustion.
1051 * If it has taken us less than a second to process the
1052 * current work, then wait. Otherwise start right over
1053 * again. We can still lose time if any single round
1054 * takes more than two seconds, but it does not really
1055 * matter as we are just trying to generally pace the
1056 * filesystem activity.
1058 if (time_second == starttime)
1059 tsleep(&lbolt, PPAUSE, "syncer", 0);
1064 * Request the syncer daemon to speed up its work.
1065 * We never push it to speed up more than half of its
1066 * normal turn time, otherwise it could take over the cpu.
1074 if (updateproc->p_wchan == &lbolt)
1075 setrunnable(updateproc);
1077 if (rushjob < syncdelay / 2) {
1079 stat_rush_requests += 1;
1086 * Associate a p-buffer with a vnode.
1088 * Also sets B_PAGING flag to indicate that vnode is not fully associated
1089 * with the buffer. i.e. the bp has not been linked into the vnode or
1094 register struct vnode *vp;
1095 register struct buf *bp;
1098 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1101 bp->b_flags |= B_PAGING;
1102 bp->b_dev = vn_todev(vp);
1106 * Disassociate a p-buffer from a vnode.
1110 register struct buf *bp;
1113 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1115 #if !defined(MAX_PERF)
1117 if (bp->b_vnbufs.tqe_next != NULL) {
1119 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1125 bp->b_vp = (struct vnode *) 0;
1126 bp->b_flags &= ~B_PAGING;
1130 pbreassignbuf(bp, newvp)
1132 struct vnode *newvp;
1134 #if !defined(MAX_PERF)
1135 if ((bp->b_flags & B_PAGING) == 0) {
1137 "pbreassignbuf() on non phys bp %p",
1146 * Reassign a buffer from one vnode to another.
1147 * Used to assign file specific control information
1148 * (indirect blocks) to the vnode to which they belong.
1151 reassignbuf(bp, newvp)
1152 register struct buf *bp;
1153 register struct vnode *newvp;
1155 struct buflists *listheadp;
1159 if (newvp == NULL) {
1160 printf("reassignbuf: NULL");
1165 #if !defined(MAX_PERF)
1167 * B_PAGING flagged buffers cannot be reassigned because their vp
1168 * is not fully linked in.
1170 if (bp->b_flags & B_PAGING)
1171 panic("cannot reassign paging buffer");
1176 * Delete from old vnode list, if on one.
1178 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
1179 if (bp->b_xflags & BX_VNDIRTY)
1180 listheadp = &bp->b_vp->v_dirtyblkhd;
1182 listheadp = &bp->b_vp->v_cleanblkhd;
1183 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1184 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
1185 if (bp->b_vp != newvp) {
1187 bp->b_vp = NULL; /* for clarification */
1191 * If dirty, put on list of dirty buffers; otherwise insert onto list
1194 if (bp->b_flags & B_DELWRI) {
1197 listheadp = &newvp->v_dirtyblkhd;
1198 if ((newvp->v_flag & VONWORKLST) == 0) {
1199 switch (newvp->v_type) {
1205 if (newvp->v_specmountpoint != NULL) {
1213 vn_syncer_add_to_worklist(newvp, delay);
1215 bp->b_xflags |= BX_VNDIRTY;
1216 tbp = TAILQ_FIRST(listheadp);
1218 bp->b_lblkno == 0 ||
1219 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) ||
1220 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) {
1221 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1222 ++reassignbufsortgood;
1223 } else if (bp->b_lblkno < 0) {
1224 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1225 ++reassignbufsortgood;
1226 } else if (reassignbufmethod == 1) {
1228 * New sorting algorithm, only handle sequential case,
1229 * otherwise append to end (but before metadata)
1231 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL &&
1232 (tbp->b_xflags & BX_VNDIRTY)) {
1234 * Found the best place to insert the buffer
1236 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1237 ++reassignbufsortgood;
1240 * Missed, append to end, but before meta-data.
1241 * We know that the head buffer in the list is
1242 * not meta-data due to prior conditionals.
1244 * Indirect effects: NFS second stage write
1245 * tends to wind up here, giving maximum
1246 * distance between the unstable write and the
1249 tbp = TAILQ_LAST(listheadp, buflists);
1250 while (tbp && tbp->b_lblkno < 0)
1251 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs);
1252 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1253 ++reassignbufsortbad;
1257 * Old sorting algorithm, scan queue and insert
1260 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1261 (ttbp->b_lblkno < bp->b_lblkno)) {
1265 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1268 bp->b_xflags |= BX_VNCLEAN;
1269 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1270 if ((newvp->v_flag & VONWORKLST) &&
1271 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1272 newvp->v_flag &= ~VONWORKLST;
1273 LIST_REMOVE(newvp, v_synclist);
1276 if (bp->b_vp != newvp) {
1284 * Create a vnode for a block device.
1285 * Used for mounting the root file system.
1292 register struct vnode *vp;
1300 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1313 * Add vnode to the alias list hung off the dev_t.
1315 * The reason for this gunk is that multiple vnodes can reference
1316 * the same physical device, so checking vp->v_usecount to see
1317 * how many users there are is inadequate; the v_usecount for
1318 * the vnodes need to be accumulated. vcount() does that.
1321 addaliasu(nvp, nvp_rdev)
1326 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1327 panic("addaliasu on non-special vnode");
1328 addalias(nvp, udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0));
1337 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1338 panic("addalias on non-special vnode");
1341 simple_lock(&spechash_slock);
1342 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext);
1343 simple_unlock(&spechash_slock);
1347 * Grab a particular vnode from the free list, increment its
1348 * reference count and lock it. The vnode lock bit is set if the
1349 * vnode is being eliminated in vgone. The process is awakened
1350 * when the transition is completed, and an error returned to
1351 * indicate that the vnode is no longer usable (possibly having
1352 * been changed to a new file system type).
1356 register struct vnode *vp;
1363 * If the vnode is in the process of being cleaned out for
1364 * another use, we wait for the cleaning to finish and then
1365 * return failure. Cleaning is determined by checking that
1366 * the VXLOCK flag is set.
1368 if ((flags & LK_INTERLOCK) == 0) {
1369 simple_lock(&vp->v_interlock);
1371 if (vp->v_flag & VXLOCK) {
1372 vp->v_flag |= VXWANT;
1373 simple_unlock(&vp->v_interlock);
1374 tsleep((caddr_t)vp, PINOD, "vget", 0);
1380 if (VSHOULDBUSY(vp))
1382 if (flags & LK_TYPE_MASK) {
1383 if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) {
1385 * must expand vrele here because we do not want
1386 * to call VOP_INACTIVE if the reference count
1387 * drops back to zero since it was never really
1388 * active. We must remove it from the free list
1389 * before sleeping so that multiple processes do
1390 * not try to recycle it.
1392 simple_lock(&vp->v_interlock);
1394 if (VSHOULDFREE(vp))
1396 simple_unlock(&vp->v_interlock);
1400 simple_unlock(&vp->v_interlock);
1405 vref(struct vnode *vp)
1407 simple_lock(&vp->v_interlock);
1409 simple_unlock(&vp->v_interlock);
1413 * Vnode put/release.
1414 * If count drops to zero, call inactive routine and return to freelist.
1420 struct proc *p = curproc; /* XXX */
1422 KASSERT(vp != NULL, ("vrele: null vp"));
1424 simple_lock(&vp->v_interlock);
1426 if (vp->v_usecount > 1) {
1429 simple_unlock(&vp->v_interlock);
1434 if (vp->v_usecount == 1) {
1437 if (VSHOULDFREE(vp))
1440 * If we are doing a vput, the node is already locked, and we must
1441 * call VOP_INACTIVE with the node locked. So, in the case of
1442 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1444 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) {
1445 VOP_INACTIVE(vp, p);
1450 vprint("vrele: negative ref count", vp);
1451 simple_unlock(&vp->v_interlock);
1453 panic("vrele: negative ref cnt");
1461 struct proc *p = curproc; /* XXX */
1463 KASSERT(vp != NULL, ("vput: null vp"));
1465 simple_lock(&vp->v_interlock);
1467 if (vp->v_usecount > 1) {
1470 VOP_UNLOCK(vp, LK_INTERLOCK, p);
1475 if (vp->v_usecount == 1) {
1478 if (VSHOULDFREE(vp))
1481 * If we are doing a vput, the node is already locked, and we must
1482 * call VOP_INACTIVE with the node locked. So, in the case of
1483 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1485 simple_unlock(&vp->v_interlock);
1486 VOP_INACTIVE(vp, p);
1490 vprint("vput: negative ref count", vp);
1492 panic("vput: negative ref cnt");
1497 * Somebody doesn't want the vnode recycled.
1501 register struct vnode *vp;
1507 if (VSHOULDBUSY(vp))
1513 * One less who cares about this vnode.
1517 register struct vnode *vp;
1522 if (vp->v_holdcnt <= 0)
1523 panic("vdrop: holdcnt");
1525 if (VSHOULDFREE(vp))
1531 * Remove any vnodes in the vnode table belonging to mount point mp.
1533 * If MNT_NOFORCE is specified, there should not be any active ones,
1534 * return error if any are found (nb: this is a user error, not a
1535 * system error). If MNT_FORCE is specified, detach any active vnodes
1539 static int busyprt = 0; /* print out busy vnodes */
1540 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1544 vflush(mp, skipvp, flags)
1546 struct vnode *skipvp;
1549 struct proc *p = curproc; /* XXX */
1550 struct vnode *vp, *nvp;
1553 simple_lock(&mntvnode_slock);
1555 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) {
1557 * Make sure this vnode wasn't reclaimed in getnewvnode().
1558 * Start over if it has (it won't be on the list anymore).
1560 if (vp->v_mount != mp)
1562 nvp = LIST_NEXT(vp, v_mntvnodes);
1564 * Skip over a selected vnode.
1569 simple_lock(&vp->v_interlock);
1571 * Skip over a vnodes marked VSYSTEM.
1573 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1574 simple_unlock(&vp->v_interlock);
1578 * If WRITECLOSE is set, only flush out regular file vnodes
1581 if ((flags & WRITECLOSE) &&
1582 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1583 simple_unlock(&vp->v_interlock);
1588 * With v_usecount == 0, all we need to do is clear out the
1589 * vnode data structures and we are done.
1591 if (vp->v_usecount == 0) {
1592 simple_unlock(&mntvnode_slock);
1594 simple_lock(&mntvnode_slock);
1599 * If FORCECLOSE is set, forcibly close the vnode. For block
1600 * or character devices, revert to an anonymous device. For
1601 * all other files, just kill them.
1603 if (flags & FORCECLOSE) {
1604 simple_unlock(&mntvnode_slock);
1605 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1609 vp->v_op = spec_vnodeop_p;
1610 insmntque(vp, (struct mount *) 0);
1612 simple_lock(&mntvnode_slock);
1617 vprint("vflush: busy vnode", vp);
1619 simple_unlock(&vp->v_interlock);
1622 simple_unlock(&mntvnode_slock);
1629 * Disassociate the underlying file system from a vnode.
1632 vclean(vp, flags, p)
1641 * Check to see if the vnode is in use. If so we have to reference it
1642 * before we clean it out so that its count cannot fall to zero and
1643 * generate a race against ourselves to recycle it.
1645 if ((active = vp->v_usecount))
1649 * Prevent the vnode from being recycled or brought into use while we
1652 if (vp->v_flag & VXLOCK)
1653 panic("vclean: deadlock");
1654 vp->v_flag |= VXLOCK;
1656 * Even if the count is zero, the VOP_INACTIVE routine may still
1657 * have the object locked while it cleans it out. The VOP_LOCK
1658 * ensures that the VOP_INACTIVE routine is done with its work.
1659 * For active vnodes, it ensures that no other activity can
1660 * occur while the underlying object is being cleaned out.
1662 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1665 * Clean out any buffers associated with the vnode.
1667 vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1668 if ((obj = vp->v_object) != NULL) {
1669 if (obj->ref_count == 0) {
1671 * vclean() may be called twice. The first time removes the
1672 * primary reference to the object, the second time goes
1673 * one further and is a special-case to terminate the object.
1675 vm_object_terminate(obj);
1678 * Woe to the process that tries to page now :-).
1680 vm_pager_deallocate(obj);
1685 * If purging an active vnode, it must be closed and
1686 * deactivated before being reclaimed. Note that the
1687 * VOP_INACTIVE will unlock the vnode.
1690 if (flags & DOCLOSE)
1691 VOP_CLOSE(vp, FNONBLOCK, NOCRED, p);
1692 VOP_INACTIVE(vp, p);
1695 * Any other processes trying to obtain this lock must first
1696 * wait for VXLOCK to clear, then call the new lock operation.
1698 VOP_UNLOCK(vp, 0, p);
1701 * Reclaim the vnode.
1703 if (VOP_RECLAIM(vp, p))
1704 panic("vclean: cannot reclaim");
1711 FREE(vp->v_vnlock, M_VNODE);
1712 vp->v_vnlock = NULL;
1715 if (VSHOULDFREE(vp))
1719 * Done with purge, notify sleepers of the grim news.
1721 vp->v_op = dead_vnodeop_p;
1724 vp->v_flag &= ~VXLOCK;
1725 if (vp->v_flag & VXWANT) {
1726 vp->v_flag &= ~VXWANT;
1727 wakeup((caddr_t) vp);
1732 * Eliminate all activity associated with the requested vnode
1733 * and with all vnodes aliased to the requested vnode.
1737 struct vop_revoke_args /* {
1742 struct vnode *vp, *vq;
1745 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1749 * If a vgone (or vclean) is already in progress,
1750 * wait until it is done and return.
1752 if (vp->v_flag & VXLOCK) {
1753 vp->v_flag |= VXWANT;
1754 simple_unlock(&vp->v_interlock);
1755 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1760 simple_lock(&spechash_slock);
1761 vq = SLIST_FIRST(&dev->si_hlist);
1762 simple_unlock(&spechash_slock);
1771 * Recycle an unused vnode to the front of the free list.
1772 * Release the passed interlock if the vnode will be recycled.
1775 vrecycle(vp, inter_lkp, p)
1777 struct simplelock *inter_lkp;
1781 simple_lock(&vp->v_interlock);
1782 if (vp->v_usecount == 0) {
1784 simple_unlock(inter_lkp);
1789 simple_unlock(&vp->v_interlock);
1794 * Eliminate all activity associated with a vnode
1795 * in preparation for reuse.
1799 register struct vnode *vp;
1801 struct proc *p = curproc; /* XXX */
1803 simple_lock(&vp->v_interlock);
1808 * vgone, with the vp interlock held.
1818 * If a vgone (or vclean) is already in progress,
1819 * wait until it is done and return.
1821 if (vp->v_flag & VXLOCK) {
1822 vp->v_flag |= VXWANT;
1823 simple_unlock(&vp->v_interlock);
1824 tsleep((caddr_t)vp, PINOD, "vgone", 0);
1829 * Clean out the filesystem specific data.
1831 vclean(vp, DOCLOSE, p);
1832 simple_lock(&vp->v_interlock);
1835 * Delete from old mount point vnode list, if on one.
1837 if (vp->v_mount != NULL)
1838 insmntque(vp, (struct mount *)0);
1840 * If special device, remove it from special device alias list
1843 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1844 simple_lock(&spechash_slock);
1845 SLIST_REMOVE(&vp->v_hashchain, vp, vnode, v_specnext);
1846 freedev(vp->v_rdev);
1847 simple_unlock(&spechash_slock);
1852 * If it is on the freelist and not already at the head,
1853 * move it to the head of the list. The test of the back
1854 * pointer and the reference count of zero is because
1855 * it will be removed from the free list by getnewvnode,
1856 * but will not have its reference count incremented until
1857 * after calling vgone. If the reference count were
1858 * incremented first, vgone would (incorrectly) try to
1859 * close the previous instance of the underlying object.
1861 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
1863 simple_lock(&vnode_free_list_slock);
1864 if (vp->v_flag & VFREE) {
1865 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1866 } else if (vp->v_flag & VTBFREE) {
1867 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
1868 vp->v_flag &= ~VTBFREE;
1872 vp->v_flag |= VFREE;
1873 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1874 simple_unlock(&vnode_free_list_slock);
1879 simple_unlock(&vp->v_interlock);
1883 * Lookup a vnode by device number.
1886 vfinddev(dev, type, vpp)
1893 simple_lock(&spechash_slock);
1894 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1895 if (type == vp->v_type) {
1897 simple_unlock(&spechash_slock);
1901 simple_unlock(&spechash_slock);
1906 * Calculate the total number of references to a special device.
1916 simple_lock(&spechash_slock);
1917 SLIST_FOREACH(vq, &vp->v_hashchain, v_specnext)
1918 count += vq->v_usecount;
1919 simple_unlock(&spechash_slock);
1924 * Print out a description of a vnode.
1926 static char *typename[] =
1927 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1937 printf("%s: %p: ", label, (void *)vp);
1939 printf("%p: ", (void *)vp);
1940 printf("type %s, usecount %d, writecount %d, refcount %d,",
1941 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1944 if (vp->v_flag & VROOT)
1945 strcat(buf, "|VROOT");
1946 if (vp->v_flag & VTEXT)
1947 strcat(buf, "|VTEXT");
1948 if (vp->v_flag & VSYSTEM)
1949 strcat(buf, "|VSYSTEM");
1950 if (vp->v_flag & VXLOCK)
1951 strcat(buf, "|VXLOCK");
1952 if (vp->v_flag & VXWANT)
1953 strcat(buf, "|VXWANT");
1954 if (vp->v_flag & VBWAIT)
1955 strcat(buf, "|VBWAIT");
1956 if (vp->v_flag & VDOOMED)
1957 strcat(buf, "|VDOOMED");
1958 if (vp->v_flag & VFREE)
1959 strcat(buf, "|VFREE");
1960 if (vp->v_flag & VOBJBUF)
1961 strcat(buf, "|VOBJBUF");
1963 printf(" flags (%s)", &buf[1]);
1964 if (vp->v_data == NULL) {
1973 #include <ddb/ddb.h>
1975 * List all of the locked vnodes in the system.
1976 * Called when debugging the kernel.
1978 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1980 struct proc *p = curproc; /* XXX */
1981 struct mount *mp, *nmp;
1984 printf("Locked vnodes\n");
1985 simple_lock(&mountlist_slock);
1986 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
1987 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
1988 nmp = TAILQ_NEXT(mp, mnt_list);
1991 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) {
1992 if (VOP_ISLOCKED(vp, NULL))
1993 vprint((char *)0, vp);
1995 simple_lock(&mountlist_slock);
1996 nmp = TAILQ_NEXT(mp, mnt_list);
1999 simple_unlock(&mountlist_slock);
2004 * Top level filesystem related information gathering.
2006 static int sysctl_ovfs_conf __P(SYSCTL_HANDLER_ARGS);
2009 vfs_sysctl SYSCTL_HANDLER_ARGS
2011 int *name = (int *)arg1 - 1; /* XXX */
2012 u_int namelen = arg2 + 1; /* XXX */
2013 struct vfsconf *vfsp;
2015 #if 1 || defined(COMPAT_PRELITE2)
2016 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2018 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2022 /* all sysctl names at this level are at least name and field */
2024 return (ENOTDIR); /* overloaded */
2025 if (name[0] != VFS_GENERIC) {
2026 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2027 if (vfsp->vfc_typenum == name[0])
2030 return (EOPNOTSUPP);
2031 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2032 oldp, oldlenp, newp, newlen, p));
2036 case VFS_MAXTYPENUM:
2039 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2042 return (ENOTDIR); /* overloaded */
2043 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2044 if (vfsp->vfc_typenum == name[2])
2047 return (EOPNOTSUPP);
2048 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2050 return (EOPNOTSUPP);
2053 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2054 "Generic filesystem");
2056 #if 1 || defined(COMPAT_PRELITE2)
2059 sysctl_ovfs_conf SYSCTL_HANDLER_ARGS
2062 struct vfsconf *vfsp;
2063 struct ovfsconf ovfs;
2065 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2066 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2067 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2068 ovfs.vfc_index = vfsp->vfc_typenum;
2069 ovfs.vfc_refcount = vfsp->vfc_refcount;
2070 ovfs.vfc_flags = vfsp->vfc_flags;
2071 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2078 #endif /* 1 || COMPAT_PRELITE2 */
2081 #define KINFO_VNODESLOP 10
2083 * Dump vnode list (via sysctl).
2084 * Copyout address of vnode followed by vnode.
2088 sysctl_vnode SYSCTL_HANDLER_ARGS
2090 struct proc *p = curproc; /* XXX */
2091 struct mount *mp, *nmp;
2092 struct vnode *nvp, *vp;
2095 #define VPTRSZ sizeof (struct vnode *)
2096 #define VNODESZ sizeof (struct vnode)
2099 if (!req->oldptr) /* Make an estimate */
2100 return (SYSCTL_OUT(req, 0,
2101 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2103 simple_lock(&mountlist_slock);
2104 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) {
2105 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2106 nmp = TAILQ_NEXT(mp, mnt_list);
2110 simple_lock(&mntvnode_slock);
2111 for (vp = LIST_FIRST(&mp->mnt_vnodelist);
2115 * Check that the vp is still associated with
2116 * this filesystem. RACE: could have been
2117 * recycled onto the same filesystem.
2119 if (vp->v_mount != mp) {
2120 simple_unlock(&mntvnode_slock);
2123 nvp = LIST_NEXT(vp, v_mntvnodes);
2124 simple_unlock(&mntvnode_slock);
2125 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2126 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2128 simple_lock(&mntvnode_slock);
2130 simple_unlock(&mntvnode_slock);
2131 simple_lock(&mountlist_slock);
2132 nmp = TAILQ_NEXT(mp, mnt_list);
2135 simple_unlock(&mountlist_slock);
2143 * Exporting the vnode list on large systems causes them to crash.
2144 * Exporting the vnode list on medium systems causes sysctl to coredump.
2147 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2148 0, 0, sysctl_vnode, "S,vnode", "");
2152 * Check to see if a filesystem is mounted on a block device.
2159 if (vp->v_specmountpoint != NULL)
2165 * Unmount all filesystems. The list is traversed in reverse order
2166 * of mounting to avoid dependencies.
2175 if (curproc != NULL)
2178 p = initproc; /* XXX XXX should this be proc0? */
2180 * Since this only runs when rebooting, it is not interlocked.
2182 while(!TAILQ_EMPTY(&mountlist)) {
2183 mp = TAILQ_LAST(&mountlist, mntlist);
2184 error = dounmount(mp, MNT_FORCE, p);
2186 TAILQ_REMOVE(&mountlist, mp, mnt_list);
2187 printf("unmount of %s failed (",
2188 mp->mnt_stat.f_mntonname);
2192 printf("%d)\n", error);
2194 /* The unmount has removed mp from the mountlist */
2200 * Build hash lists of net addresses and hang them off the mount point.
2201 * Called by ufs_mount() to set up the lists of export addresses.
2204 vfs_hang_addrlist(mp, nep, argp)
2206 struct netexport *nep;
2207 struct export_args *argp;
2209 register struct netcred *np;
2210 register struct radix_node_head *rnh;
2212 struct radix_node *rn;
2213 struct sockaddr *saddr, *smask = 0;
2217 if (argp->ex_addrlen == 0) {
2218 if (mp->mnt_flag & MNT_DEFEXPORTED)
2220 np = &nep->ne_defexported;
2221 np->netc_exflags = argp->ex_flags;
2222 np->netc_anon = argp->ex_anon;
2223 np->netc_anon.cr_ref = 1;
2224 mp->mnt_flag |= MNT_DEFEXPORTED;
2227 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2228 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2229 bzero((caddr_t) np, i);
2230 saddr = (struct sockaddr *) (np + 1);
2231 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2233 if (saddr->sa_len > argp->ex_addrlen)
2234 saddr->sa_len = argp->ex_addrlen;
2235 if (argp->ex_masklen) {
2236 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2237 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2240 if (smask->sa_len > argp->ex_masklen)
2241 smask->sa_len = argp->ex_masklen;
2243 i = saddr->sa_family;
2244 if ((rnh = nep->ne_rtable[i]) == 0) {
2246 * Seems silly to initialize every AF when most are not used,
2247 * do so on demand here
2249 for (dom = domains; dom; dom = dom->dom_next)
2250 if (dom->dom_family == i && dom->dom_rtattach) {
2251 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2255 if ((rnh = nep->ne_rtable[i]) == 0) {
2260 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2262 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2266 np->netc_exflags = argp->ex_flags;
2267 np->netc_anon = argp->ex_anon;
2268 np->netc_anon.cr_ref = 1;
2271 free(np, M_NETADDR);
2277 vfs_free_netcred(rn, w)
2278 struct radix_node *rn;
2281 register struct radix_node_head *rnh = (struct radix_node_head *) w;
2283 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2284 free((caddr_t) rn, M_NETADDR);
2289 * Free the net address hash lists that are hanging off the mount points.
2292 vfs_free_addrlist(nep)
2293 struct netexport *nep;
2296 register struct radix_node_head *rnh;
2298 for (i = 0; i <= AF_MAX; i++)
2299 if ((rnh = nep->ne_rtable[i])) {
2300 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2302 free((caddr_t) rnh, M_RTABLE);
2303 nep->ne_rtable[i] = 0;
2308 vfs_export(mp, nep, argp)
2310 struct netexport *nep;
2311 struct export_args *argp;
2315 if (argp->ex_flags & MNT_DELEXPORT) {
2316 if (mp->mnt_flag & MNT_EXPUBLIC) {
2317 vfs_setpublicfs(NULL, NULL, NULL);
2318 mp->mnt_flag &= ~MNT_EXPUBLIC;
2320 vfs_free_addrlist(nep);
2321 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2323 if (argp->ex_flags & MNT_EXPORTED) {
2324 if (argp->ex_flags & MNT_EXPUBLIC) {
2325 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2327 mp->mnt_flag |= MNT_EXPUBLIC;
2329 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2331 mp->mnt_flag |= MNT_EXPORTED;
2338 * Set the publicly exported filesystem (WebNFS). Currently, only
2339 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2342 vfs_setpublicfs(mp, nep, argp)
2344 struct netexport *nep;
2345 struct export_args *argp;
2352 * mp == NULL -> invalidate the current info, the FS is
2353 * no longer exported. May be called from either vfs_export
2354 * or unmount, so check if it hasn't already been done.
2357 if (nfs_pub.np_valid) {
2358 nfs_pub.np_valid = 0;
2359 if (nfs_pub.np_index != NULL) {
2360 FREE(nfs_pub.np_index, M_TEMP);
2361 nfs_pub.np_index = NULL;
2368 * Only one allowed at a time.
2370 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2374 * Get real filehandle for root of exported FS.
2376 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2377 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2379 if ((error = VFS_ROOT(mp, &rvp)))
2382 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2388 * If an indexfile was specified, pull it in.
2390 if (argp->ex_indexfile != NULL) {
2391 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2393 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2394 MAXNAMLEN, (size_t *)0);
2397 * Check for illegal filenames.
2399 for (cp = nfs_pub.np_index; *cp; cp++) {
2407 FREE(nfs_pub.np_index, M_TEMP);
2412 nfs_pub.np_mount = mp;
2413 nfs_pub.np_valid = 1;
2418 vfs_export_lookup(mp, nep, nam)
2419 register struct mount *mp;
2420 struct netexport *nep;
2421 struct sockaddr *nam;
2423 register struct netcred *np;
2424 register struct radix_node_head *rnh;
2425 struct sockaddr *saddr;
2428 if (mp->mnt_flag & MNT_EXPORTED) {
2430 * Lookup in the export list first.
2434 rnh = nep->ne_rtable[saddr->sa_family];
2436 np = (struct netcred *)
2437 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2439 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2444 * If no address match, use the default if it exists.
2446 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2447 np = &nep->ne_defexported;
2453 * perform msync on all vnodes under a mount point
2454 * the mount point must be locked.
2457 vfs_msync(struct mount *mp, int flags) {
2458 struct vnode *vp, *nvp;
2459 struct vm_object *obj;
2465 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) {
2467 nvp = LIST_NEXT(vp, v_mntvnodes);
2469 if (vp->v_mount != mp) {
2473 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2476 if (flags != MNT_WAIT) {
2478 if (obj == NULL || (obj->flags & OBJ_MIGHTBEDIRTY) == 0)
2480 if (VOP_ISLOCKED(vp, NULL))
2484 simple_lock(&vp->v_interlock);
2486 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
2488 LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
2490 vm_object_page_clean(vp->v_object, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2496 simple_unlock(&vp->v_interlock);
2499 if (anyio && (--tries > 0))
2504 * Create the VM object needed for VMIO and mmap support. This
2505 * is done for all VREG files in the system. Some filesystems might
2506 * afford the additional metadata buffering capability of the
2507 * VMIO code by making the device node be VMIO mode also.
2509 * vp must be locked when vfs_object_create is called.
2512 vfs_object_create(vp, p, cred)
2521 if (!vn_isdisk(vp) && vn_canvmio(vp) == FALSE)
2525 if ((object = vp->v_object) == NULL) {
2526 if (vp->v_type == VREG || vp->v_type == VDIR) {
2527 if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
2529 object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
2530 } else if (devsw(vp->v_rdev) != NULL) {
2532 * This simply allocates the biggest object possible
2533 * for a disk vnode. This should be fixed, but doesn't
2534 * cause any problems (yet).
2536 object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0);
2541 * Dereference the reference we just created. This assumes
2542 * that the object is associated with the vp.
2544 object->ref_count--;
2547 if (object->flags & OBJ_DEAD) {
2548 VOP_UNLOCK(vp, 0, p);
2549 tsleep(object, PVM, "vodead", 0);
2550 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2555 KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object"));
2556 vp->v_flag |= VOBJBUF;
2569 simple_lock(&vnode_free_list_slock);
2570 if (vp->v_flag & VTBFREE) {
2571 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2572 vp->v_flag &= ~VTBFREE;
2574 if (vp->v_flag & VAGE) {
2575 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2577 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2580 simple_unlock(&vnode_free_list_slock);
2581 vp->v_flag &= ~VAGE;
2582 vp->v_flag |= VFREE;
2593 simple_lock(&vnode_free_list_slock);
2594 if (vp->v_flag & VTBFREE) {
2595 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2596 vp->v_flag &= ~VTBFREE;
2598 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2601 simple_unlock(&vnode_free_list_slock);
2602 vp->v_flag &= ~(VFREE|VAGE);
2607 * Record a process's interest in events which might happen to
2608 * a vnode. Because poll uses the historic select-style interface
2609 * internally, this routine serves as both the ``check for any
2610 * pending events'' and the ``record my interest in future events''
2611 * functions. (These are done together, while the lock is held,
2612 * to avoid race conditions.)
2615 vn_pollrecord(vp, p, events)
2620 simple_lock(&vp->v_pollinfo.vpi_lock);
2621 if (vp->v_pollinfo.vpi_revents & events) {
2623 * This leaves events we are not interested
2624 * in available for the other process which
2625 * which presumably had requested them
2626 * (otherwise they would never have been
2629 events &= vp->v_pollinfo.vpi_revents;
2630 vp->v_pollinfo.vpi_revents &= ~events;
2632 simple_unlock(&vp->v_pollinfo.vpi_lock);
2635 vp->v_pollinfo.vpi_events |= events;
2636 selrecord(p, &vp->v_pollinfo.vpi_selinfo);
2637 simple_unlock(&vp->v_pollinfo.vpi_lock);
2642 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2643 * it is possible for us to miss an event due to race conditions, but
2644 * that condition is expected to be rare, so for the moment it is the
2645 * preferred interface.
2648 vn_pollevent(vp, events)
2652 simple_lock(&vp->v_pollinfo.vpi_lock);
2653 if (vp->v_pollinfo.vpi_events & events) {
2655 * We clear vpi_events so that we don't
2656 * call selwakeup() twice if two events are
2657 * posted before the polling process(es) is
2658 * awakened. This also ensures that we take at
2659 * most one selwakeup() if the polling process
2660 * is no longer interested. However, it does
2661 * mean that only one event can be noticed at
2662 * a time. (Perhaps we should only clear those
2663 * event bits which we note?) XXX
2665 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2666 vp->v_pollinfo.vpi_revents |= events;
2667 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2669 simple_unlock(&vp->v_pollinfo.vpi_lock);
2673 * Wake up anyone polling on vp because it is being revoked.
2674 * This depends on dead_poll() returning POLLHUP for correct
2681 simple_lock(&vp->v_pollinfo.vpi_lock);
2682 if (vp->v_pollinfo.vpi_events) {
2683 vp->v_pollinfo.vpi_events = 0;
2684 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2686 simple_unlock(&vp->v_pollinfo.vpi_lock);
2692 * Routine to create and manage a filesystem syncer vnode.
2694 #define sync_close ((int (*) __P((struct vop_close_args *)))nullop)
2695 static int sync_fsync __P((struct vop_fsync_args *));
2696 static int sync_inactive __P((struct vop_inactive_args *));
2697 static int sync_reclaim __P((struct vop_reclaim_args *));
2698 #define sync_lock ((int (*) __P((struct vop_lock_args *)))vop_nolock)
2699 #define sync_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock)
2700 static int sync_print __P((struct vop_print_args *));
2701 #define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2703 static vop_t **sync_vnodeop_p;
2704 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2705 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2706 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2707 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2708 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2709 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2710 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2711 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2712 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2713 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2716 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2717 { &sync_vnodeop_p, sync_vnodeop_entries };
2719 VNODEOP_SET(sync_vnodeop_opv_desc);
2722 * Create a new filesystem syncer vnode for the specified mount point.
2725 vfs_allocate_syncvnode(mp)
2729 static long start, incr, next;
2732 /* Allocate a new vnode */
2733 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2734 mp->mnt_syncer = NULL;
2739 * Place the vnode onto the syncer worklist. We attempt to
2740 * scatter them about on the list so that they will go off
2741 * at evenly distributed times even if all the filesystems
2742 * are mounted at once.
2745 if (next == 0 || next > syncer_maxdelay) {
2749 start = syncer_maxdelay / 2;
2750 incr = syncer_maxdelay;
2754 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2755 mp->mnt_syncer = vp;
2760 * Do a lazy sync of the filesystem.
2764 struct vop_fsync_args /* {
2766 struct ucred *a_cred;
2771 struct vnode *syncvp = ap->a_vp;
2772 struct mount *mp = syncvp->v_mount;
2773 struct proc *p = ap->a_p;
2777 * We only need to do something if this is a lazy evaluation.
2779 if (ap->a_waitfor != MNT_LAZY)
2783 * Move ourselves to the back of the sync list.
2785 vn_syncer_add_to_worklist(syncvp, syncdelay);
2788 * Walk the list of vnodes pushing all that are dirty and
2789 * not already on the sync list.
2791 simple_lock(&mountlist_slock);
2792 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, p) != 0) {
2793 simple_unlock(&mountlist_slock);
2796 asyncflag = mp->mnt_flag & MNT_ASYNC;
2797 mp->mnt_flag &= ~MNT_ASYNC;
2798 vfs_msync(mp, MNT_NOWAIT);
2799 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, p);
2801 mp->mnt_flag |= MNT_ASYNC;
2807 * The syncer vnode is no referenced.
2811 struct vop_inactive_args /* {
2822 * The syncer vnode is no longer needed and is being decommissioned.
2824 * Modifications to the worklist must be protected at splbio().
2828 struct vop_reclaim_args /* {
2832 struct vnode *vp = ap->a_vp;
2836 vp->v_mount->mnt_syncer = NULL;
2837 if (vp->v_flag & VONWORKLST) {
2838 LIST_REMOVE(vp, v_synclist);
2839 vp->v_flag &= ~VONWORKLST;
2847 * Print out a syncer vnode.
2851 struct vop_print_args /* {
2855 struct vnode *vp = ap->a_vp;
2857 printf("syncer vnode");
2858 if (vp->v_vnlock != NULL)
2859 lockmgr_printinfo(vp->v_vnlock);
2865 * extract the dev_t from a VBLK or VCHR
2871 if (vp->v_type != VBLK && vp->v_type != VCHR)
2873 return (vp->v_rdev);
2877 * Check if vnode represents a disk device
2883 if (vp->v_type != VBLK && vp->v_type != VCHR)
2885 if (!devsw(vp->v_rdev))
2887 if (!(devsw(vp->v_rdev)->d_flags & D_DISK))
2894 struct nameidata *ndp;
2897 if (!(flags & NDF_NO_FREE_PNBUF) &&
2898 (ndp->ni_cnd.cn_flags & HASBUF)) {
2899 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
2900 ndp->ni_cnd.cn_flags &= ~HASBUF;
2902 if (!(flags & NDF_NO_DVP_UNLOCK) &&
2903 (ndp->ni_cnd.cn_flags & LOCKPARENT) &&
2904 ndp->ni_dvp != ndp->ni_vp)
2905 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_proc);
2906 if (!(flags & NDF_NO_DVP_RELE) &&
2907 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) {
2911 if (!(flags & NDF_NO_VP_UNLOCK) &&
2912 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp)
2913 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_proc);
2914 if (!(flags & NDF_NO_VP_RELE) &&
2919 if (!(flags & NDF_NO_STARTDIR_RELE) &&
2920 (ndp->ni_cnd.cn_flags & SAVESTART)) {
2921 vrele(ndp->ni_startdir);
2922 ndp->ni_startdir = NULL;