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
39 * $Id: vfs_subr.c,v 1.187 1999/02/19 17:36:58 dillon Exp $
43 * External virtual filesystem routines
47 #include <sys/param.h>
48 #include <sys/systm.h>
50 #include <sys/fcntl.h>
51 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mount.h>
55 #include <sys/socket.h>
56 #include <sys/vnode.h>
59 #include <sys/domain.h>
60 #include <sys/dirent.h>
61 #include <sys/vmmeter.h>
63 #include <machine/limits.h>
66 #include <vm/vm_param.h>
67 #include <vm/vm_prot.h>
68 #include <vm/vm_object.h>
69 #include <vm/vm_extern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_page.h>
73 #include <vm/vm_pager.h>
74 #include <vm/vnode_pager.h>
75 #include <vm/vm_zone.h>
76 #include <sys/sysctl.h>
78 #include <miscfs/specfs/specdev.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, "");
107 #ifdef ENABLE_VFS_IOOPT
108 SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
111 struct mntlist mountlist; /* mounted filesystem list */
112 struct simplelock mountlist_slock;
113 struct simplelock mntvnode_slock;
114 int nfs_mount_type = -1;
115 #ifndef NULL_SIMPLELOCKS
116 static struct simplelock mntid_slock;
117 static struct simplelock vnode_free_list_slock;
118 static struct simplelock spechash_slock;
120 struct nfs_public nfs_pub; /* publicly exported FS */
121 static vm_zone_t vnode_zone;
124 * The workitem queue.
126 #define SYNCER_MAXDELAY 32
127 static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */
128 time_t syncdelay = 30;
129 int rushjob; /* number of slots to run ASAP */
131 static int syncer_delayno = 0;
132 static long syncer_mask;
133 LIST_HEAD(synclist, vnode);
134 static struct synclist *syncer_workitem_pending;
137 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, &desiredvnodes, 0, "");
139 static void vfs_free_addrlist __P((struct netexport *nep));
140 static int vfs_free_netcred __P((struct radix_node *rn, void *w));
141 static int vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep,
142 struct export_args *argp));
145 * Initialize the vnode management data structures.
151 desiredvnodes = maxproc + cnt.v_page_count / 4;
152 simple_lock_init(&mntvnode_slock);
153 simple_lock_init(&mntid_slock);
154 simple_lock_init(&spechash_slock);
155 TAILQ_INIT(&vnode_free_list);
156 TAILQ_INIT(&vnode_tobefree_list);
157 simple_lock_init(&vnode_free_list_slock);
158 CIRCLEQ_INIT(&mountlist);
159 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
161 * Initialize the filesystem syncer.
163 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
165 syncer_maxdelay = syncer_mask + 1;
169 * Mark a mount point as busy. Used to synchronize access and to delay
170 * unmounting. Interlock is not released on failure.
173 vfs_busy(mp, flags, interlkp, p)
176 struct simplelock *interlkp;
181 if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
182 if (flags & LK_NOWAIT)
184 mp->mnt_kern_flag |= MNTK_MWAIT;
186 simple_unlock(interlkp);
189 * Since all busy locks are shared except the exclusive
190 * lock granted when unmounting, the only place that a
191 * wakeup needs to be done is at the release of the
192 * exclusive lock at the end of dounmount.
194 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
196 simple_lock(interlkp);
200 lkflags = LK_SHARED | LK_NOPAUSE;
202 lkflags |= LK_INTERLOCK;
203 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p))
204 panic("vfs_busy: unexpected lock failure");
209 * Free a busy filesystem.
217 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p);
221 * Lookup a filesystem type, and if found allocate and initialize
222 * a mount structure for it.
224 * Devname is usually updated by mount(8) after booting.
227 vfs_rootmountalloc(fstypename, devname, mpp)
232 struct proc *p = curproc; /* XXX */
233 struct vfsconf *vfsp;
236 if (fstypename == NULL)
238 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
239 if (!strcmp(vfsp->vfc_name, fstypename))
243 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
244 bzero((char *)mp, (u_long)sizeof(struct mount));
245 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
246 (void)vfs_busy(mp, LK_NOWAIT, 0, p);
247 LIST_INIT(&mp->mnt_vnodelist);
249 mp->mnt_op = vfsp->vfc_vfsops;
250 mp->mnt_flag = MNT_RDONLY;
251 mp->mnt_vnodecovered = NULLVP;
252 vfsp->vfc_refcount++;
253 mp->mnt_stat.f_type = vfsp->vfc_typenum;
254 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
255 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
256 mp->mnt_stat.f_mntonname[0] = '/';
257 mp->mnt_stat.f_mntonname[1] = 0;
258 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
264 * Find an appropriate filesystem to use for the root. If a filesystem
265 * has not been preselected, walk through the list of known filesystems
266 * trying those that have mountroot routines, and try them until one
267 * works or we have tried them all.
269 #ifdef notdef /* XXX JH */
271 lite2_vfs_mountroot()
273 struct vfsconf *vfsp;
274 extern int (*lite2_mountroot) __P((void));
277 if (lite2_mountroot != NULL)
278 return ((*lite2_mountroot)());
279 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
280 if (vfsp->vfc_mountroot == NULL)
282 if ((error = (*vfsp->vfc_mountroot)()) == 0)
284 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
291 * Lookup a mount point by filesystem identifier.
297 register struct mount *mp;
299 simple_lock(&mountlist_slock);
300 for (mp = mountlist.cqh_first; mp != (void *)&mountlist;
301 mp = mp->mnt_list.cqe_next) {
302 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
303 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
304 simple_unlock(&mountlist_slock);
308 simple_unlock(&mountlist_slock);
309 return ((struct mount *) 0);
313 * Get a new unique fsid
319 static u_short xxxfs_mntid;
324 simple_lock(&mntid_slock);
325 mtype = mp->mnt_vfc->vfc_typenum;
326 mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0);
327 mp->mnt_stat.f_fsid.val[1] = mtype;
328 if (xxxfs_mntid == 0)
330 tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid);
331 tfsid.val[1] = mtype;
332 if (mountlist.cqh_first != (void *)&mountlist) {
333 while (vfs_getvfs(&tfsid)) {
338 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
339 simple_unlock(&mntid_slock);
343 * Set vnode attributes to VNOVAL
347 register struct vattr *vap;
351 vap->va_size = VNOVAL;
352 vap->va_bytes = VNOVAL;
353 vap->va_mode = VNOVAL;
354 vap->va_nlink = VNOVAL;
355 vap->va_uid = VNOVAL;
356 vap->va_gid = VNOVAL;
357 vap->va_fsid = VNOVAL;
358 vap->va_fileid = VNOVAL;
359 vap->va_blocksize = VNOVAL;
360 vap->va_rdev = VNOVAL;
361 vap->va_atime.tv_sec = VNOVAL;
362 vap->va_atime.tv_nsec = VNOVAL;
363 vap->va_mtime.tv_sec = VNOVAL;
364 vap->va_mtime.tv_nsec = VNOVAL;
365 vap->va_ctime.tv_sec = VNOVAL;
366 vap->va_ctime.tv_nsec = VNOVAL;
367 vap->va_flags = VNOVAL;
368 vap->va_gen = VNOVAL;
373 * Routines having to do with the management of the vnode table.
375 extern vop_t **dead_vnodeop_p;
378 * Return the next vnode from the free list.
381 getnewvnode(tag, mp, vops, vpp)
388 struct proc *p = curproc; /* XXX */
389 struct vnode *vp, *tvp, *nvp;
391 TAILQ_HEAD(freelst, vnode) vnode_tmp_list;
394 * We take the least recently used vnode from the freelist
395 * if we can get it and it has no cached pages, and no
396 * namecache entries are relative to it.
397 * Otherwise we allocate a new vnode
401 simple_lock(&vnode_free_list_slock);
402 TAILQ_INIT(&vnode_tmp_list);
404 for (vp = TAILQ_FIRST(&vnode_tobefree_list); vp; vp = nvp) {
405 nvp = TAILQ_NEXT(vp, v_freelist);
406 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
407 if (vp->v_flag & VAGE) {
408 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
410 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
412 vp->v_flag &= ~(VTBFREE|VAGE);
415 panic("tobe free vnode isn't");
419 if (wantfreevnodes && freevnodes < wantfreevnodes) {
421 } else if (!wantfreevnodes && freevnodes <= desiredvnodes) {
423 * XXX: this is only here to be backwards compatible
427 for (vp = TAILQ_FIRST(&vnode_free_list); vp; vp = nvp) {
428 nvp = TAILQ_NEXT(vp, v_freelist);
429 if (!simple_lock_try(&vp->v_interlock))
432 panic("free vnode isn't");
434 object = vp->v_object;
435 if (object && (object->resident_page_count || object->ref_count)) {
436 printf("object inconsistant state: RPC: %d, RC: %d\n",
437 object->resident_page_count, object->ref_count);
438 /* Don't recycle if it's caching some pages */
439 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
440 TAILQ_INSERT_TAIL(&vnode_tmp_list, vp, v_freelist);
442 } else if (LIST_FIRST(&vp->v_cache_src)) {
443 /* Don't recycle if active in the namecache */
444 simple_unlock(&vp->v_interlock);
452 for (tvp = TAILQ_FIRST(&vnode_tmp_list); tvp; tvp = nvp) {
453 nvp = TAILQ_NEXT(tvp, v_freelist);
454 TAILQ_REMOVE(&vnode_tmp_list, tvp, v_freelist);
455 TAILQ_INSERT_TAIL(&vnode_free_list, tvp, v_freelist);
456 simple_unlock(&tvp->v_interlock);
460 vp->v_flag |= VDOOMED;
461 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
463 simple_unlock(&vnode_free_list_slock);
466 if (vp->v_type != VBAD) {
469 simple_unlock(&vp->v_interlock);
477 panic("cleaned vnode isn't");
480 panic("Clean vnode has pending I/O's");
491 vp->v_writecount = 0; /* XXX */
494 simple_unlock(&vnode_free_list_slock);
495 vp = (struct vnode *) zalloc(vnode_zone);
496 bzero((char *) vp, sizeof *vp);
497 simple_lock_init(&vp->v_interlock);
500 LIST_INIT(&vp->v_cache_src);
501 TAILQ_INIT(&vp->v_cache_dst);
505 TAILQ_INIT(&vp->v_cleanblkhd);
506 TAILQ_INIT(&vp->v_dirtyblkhd);
516 vfs_object_create(vp, p, p->p_ucred);
521 * Move a vnode from one mount queue to another.
525 register struct vnode *vp;
526 register struct mount *mp;
529 simple_lock(&mntvnode_slock);
531 * Delete from old mount point vnode list, if on one.
533 if (vp->v_mount != NULL)
534 LIST_REMOVE(vp, v_mntvnodes);
536 * Insert into list of vnodes for the new mount point, if available.
538 if ((vp->v_mount = mp) == NULL) {
539 simple_unlock(&mntvnode_slock);
542 LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
543 simple_unlock(&mntvnode_slock);
547 * Update outstanding I/O count and do wakeup if requested.
551 register struct buf *bp;
553 register struct vnode *vp;
555 bp->b_flags &= ~B_WRITEINPROG;
556 if ((vp = bp->b_vp)) {
558 if (vp->v_numoutput < 0)
559 panic("vwakeup: neg numoutput");
560 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
561 vp->v_flag &= ~VBWAIT;
562 wakeup((caddr_t) &vp->v_numoutput);
568 * Flush out and invalidate all buffers associated with a vnode.
569 * Called with the underlying object locked.
572 vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
573 register struct vnode *vp;
577 int slpflag, slptimeo;
579 register struct buf *bp;
580 struct buf *nbp, *blist;
584 if (flags & V_SAVE) {
586 while (vp->v_numoutput) {
587 vp->v_flag |= VBWAIT;
588 error = tsleep((caddr_t)&vp->v_numoutput,
589 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo);
595 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
597 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0)
600 if (vp->v_numoutput > 0 ||
601 !TAILQ_EMPTY(&vp->v_dirtyblkhd))
602 panic("vinvalbuf: dirty bufs");
608 blist = TAILQ_FIRST(&vp->v_cleanblkhd);
610 blist = TAILQ_FIRST(&vp->v_dirtyblkhd);
614 for (bp = blist; bp; bp = nbp) {
615 nbp = TAILQ_NEXT(bp, b_vnbufs);
616 if (bp->b_flags & B_BUSY) {
617 bp->b_flags |= B_WANTED;
618 error = tsleep((caddr_t) bp,
619 slpflag | (PRIBIO + 4), "vinvalbuf",
628 * XXX Since there are no node locks for NFS, I
629 * believe there is a slight chance that a delayed
630 * write will occur while sleeping just above, so
631 * check for it. Note that vfs_bio_awrite expects
632 * buffers to reside on a queue, while VOP_BWRITE and
635 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
638 if (bp->b_vp == vp) {
639 if (bp->b_flags & B_CLUSTEROK) {
643 bp->b_flags |= (B_BUSY | B_ASYNC);
648 bp->b_flags |= B_BUSY;
649 (void) VOP_BWRITE(bp);
654 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF | B_BUSY);
655 bp->b_flags &= ~B_ASYNC;
660 while (vp->v_numoutput > 0) {
661 vp->v_flag |= VBWAIT;
662 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
668 * Destroy the copy in the VM cache, too.
670 simple_lock(&vp->v_interlock);
671 object = vp->v_object;
672 if (object != NULL) {
673 vm_object_page_remove(object, 0, 0,
674 (flags & V_SAVE) ? TRUE : FALSE);
676 simple_unlock(&vp->v_interlock);
678 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd))
679 panic("vinvalbuf: flush failed");
684 * Truncate a file's buffer and pages to a specified length. This
685 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
689 vtruncbuf(vp, cred, p, length, blksize)
690 register struct vnode *vp;
696 register struct buf *bp;
702 * Round up to the *next* lbn.
704 trunclbn = (length + blksize - 1) / blksize;
711 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
712 nbp = TAILQ_NEXT(bp, b_vnbufs);
713 if (bp->b_lblkno >= trunclbn) {
714 if (bp->b_flags & B_BUSY) {
715 bp->b_flags |= B_WANTED;
716 tsleep(bp, PRIBIO + 4, "vtrb1", 0);
720 bp->b_flags |= (B_BUSY | B_INVAL | B_RELBUF);
721 bp->b_flags &= ~B_ASYNC;
725 if (nbp && (((nbp->b_xflags & B_VNCLEAN) == 0)||
727 (nbp->b_flags & B_DELWRI))) {
733 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
734 nbp = TAILQ_NEXT(bp, b_vnbufs);
735 if (bp->b_lblkno >= trunclbn) {
736 if (bp->b_flags & B_BUSY) {
737 bp->b_flags |= B_WANTED;
738 tsleep(bp, PRIBIO + 4, "vtrb2", 0);
742 bp->b_flags |= (B_BUSY | B_INVAL | B_RELBUF);
743 bp->b_flags &= ~B_ASYNC;
747 if (nbp && (((nbp->b_xflags & B_VNDIRTY) == 0)||
749 (nbp->b_flags & B_DELWRI) == 0)) {
758 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
759 nbp = TAILQ_NEXT(bp, b_vnbufs);
760 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
761 if (bp->b_flags & B_BUSY) {
762 bp->b_flags |= B_WANTED;
763 tsleep(bp, PRIBIO, "vtrb3", 0);
766 bp->b_flags |= B_BUSY;
767 if (bp->b_vp == vp) {
768 bp->b_flags |= B_ASYNC;
770 bp->b_flags &= ~B_ASYNC;
780 while (vp->v_numoutput > 0) {
781 vp->v_flag |= VBWAIT;
782 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
787 vnode_pager_setsize(vp, length);
793 * Associate a buffer with a vnode.
797 register struct vnode *vp;
798 register struct buf *bp;
802 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
806 if (vp->v_type == VBLK || vp->v_type == VCHR)
807 bp->b_dev = vp->v_rdev;
811 * Insert onto list for new vnode.
814 bp->b_xflags |= B_VNCLEAN;
815 bp->b_xflags &= ~B_VNDIRTY;
816 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs);
821 * Disassociate a buffer from a vnode.
825 register struct buf *bp;
828 struct buflists *listheadp;
831 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
834 * Delete from old vnode list, if on one.
838 if (bp->b_xflags & (B_VNDIRTY|B_VNCLEAN)) {
839 if (bp->b_xflags & B_VNDIRTY)
840 listheadp = &vp->v_dirtyblkhd;
842 listheadp = &vp->v_cleanblkhd;
843 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
844 bp->b_xflags &= ~(B_VNDIRTY|B_VNCLEAN);
846 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) {
847 vp->v_flag &= ~VONWORKLST;
848 LIST_REMOVE(vp, v_synclist);
851 bp->b_vp = (struct vnode *) 0;
856 * The workitem queue.
858 * It is useful to delay writes of file data and filesystem metadata
859 * for tens of seconds so that quickly created and deleted files need
860 * not waste disk bandwidth being created and removed. To realize this,
861 * we append vnodes to a "workitem" queue. When running with a soft
862 * updates implementation, most pending metadata dependencies should
863 * not wait for more than a few seconds. Thus, mounted on block devices
864 * are delayed only about a half the time that file data is delayed.
865 * Similarly, directory updates are more critical, so are only delayed
866 * about a third the time that file data is delayed. Thus, there are
867 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
868 * one each second (driven off the filesystem syner process). The
869 * syncer_delayno variable indicates the next queue that is to be processed.
870 * Items that need to be processed soon are placed in this queue:
872 * syncer_workitem_pending[syncer_delayno]
874 * A delay of fifteen seconds is done by placing the request fifteen
875 * entries later in the queue:
877 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
882 * Add an item to the syncer work queue.
885 vn_syncer_add_to_worklist(struct vnode *vp, int delay)
891 if (vp->v_flag & VONWORKLST) {
892 LIST_REMOVE(vp, v_synclist);
895 if (delay > syncer_maxdelay - 2)
896 delay = syncer_maxdelay - 2;
897 slot = (syncer_delayno + delay) & syncer_mask;
899 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
900 vp->v_flag |= VONWORKLST;
904 static struct proc *updateproc;
905 static void sched_sync __P((void));
906 static const struct kproc_desc up_kp = {
911 SYSINIT_KT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
914 * System filesystem synchronizer daemon.
919 struct synclist *slp;
923 struct proc *p = updateproc;
926 starttime = time_second;
929 * Push files whose dirty time has expired. Be careful
930 * of interrupt race on slp queue.
933 slp = &syncer_workitem_pending[syncer_delayno];
935 if (syncer_delayno == syncer_maxdelay)
939 while ((vp = LIST_FIRST(slp)) != NULL) {
940 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
941 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p);
942 VOP_UNLOCK(vp, 0, p);
944 if (LIST_FIRST(slp) == vp) {
945 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) &&
947 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag);
949 * Put us back on the worklist. The worklist
950 * routine will remove us from our current
951 * position and then add us back in at a later
954 vn_syncer_add_to_worklist(vp, syncdelay);
960 * Do soft update processing.
963 (*bioops.io_sync)(NULL);
966 * The variable rushjob allows the kernel to speed up the
967 * processing of the filesystem syncer process. A rushjob
968 * value of N tells the filesystem syncer to process the next
969 * N seconds worth of work on its queue ASAP. Currently rushjob
970 * is used by the soft update code to speed up the filesystem
971 * syncer process when the incore state is getting so far
972 * ahead of the disk that the kernel memory pool is being
973 * threatened with exhaustion.
980 * If it has taken us less than a second to process the
981 * current work, then wait. Otherwise start right over
982 * again. We can still lose time if any single round
983 * takes more than two seconds, but it does not really
984 * matter as we are just trying to generally pace the
985 * filesystem activity.
987 if (time_second == starttime)
988 tsleep(&lbolt, PPAUSE, "syncer", 0);
993 * Associate a p-buffer with a vnode.
995 * Also sets B_PAGING flag to indicate that vnode is not fully associated
996 * with the buffer. i.e. the bp has not been linked into the vnode or
1001 register struct vnode *vp;
1002 register struct buf *bp;
1005 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
1008 bp->b_flags |= B_PAGING;
1009 if (vp->v_type == VBLK || vp->v_type == VCHR)
1010 bp->b_dev = vp->v_rdev;
1016 * Disassociate a p-buffer from a vnode.
1020 register struct buf *bp;
1023 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
1025 #if !defined(MAX_PERF)
1027 if (bp->b_vnbufs.tqe_next != NULL) {
1029 "relpbuf(): b_vp was probably reassignbuf()d %p %x",
1035 bp->b_vp = (struct vnode *) 0;
1036 bp->b_flags &= ~B_PAGING;
1040 pbreassignbuf(bp, newvp)
1042 struct vnode *newvp;
1044 #if !defined(MAX_PERF)
1045 if ((bp->b_flags & B_PAGING) == 0) {
1047 "pbreassignbuf() on non phys bp %p",
1056 * Reassign a buffer from one vnode to another.
1057 * Used to assign file specific control information
1058 * (indirect blocks) to the vnode to which they belong.
1061 reassignbuf(bp, newvp)
1062 register struct buf *bp;
1063 register struct vnode *newvp;
1065 struct buflists *listheadp;
1066 struct vnode *oldvp;
1070 if (newvp == NULL) {
1071 printf("reassignbuf: NULL");
1075 #if !defined(MAX_PERF)
1077 * B_PAGING flagged buffers cannot be reassigned because their vp
1078 * is not fully linked in.
1080 if (bp->b_flags & B_PAGING)
1081 panic("cannot reassign paging buffer");
1086 * Delete from old vnode list, if on one.
1088 if (bp->b_xflags & (B_VNDIRTY|B_VNCLEAN)) {
1090 if (bp->b_xflags & B_VNDIRTY)
1091 listheadp = &oldvp->v_dirtyblkhd;
1093 listheadp = &oldvp->v_cleanblkhd;
1094 TAILQ_REMOVE(listheadp, bp, b_vnbufs);
1095 bp->b_xflags &= ~(B_VNDIRTY|B_VNCLEAN);
1099 * If dirty, put on list of dirty buffers; otherwise insert onto list
1102 if (bp->b_flags & B_DELWRI) {
1105 listheadp = &newvp->v_dirtyblkhd;
1106 if ((newvp->v_flag & VONWORKLST) == 0) {
1107 switch (newvp->v_type) {
1109 delay = syncdelay / 3;
1112 if (newvp->v_specmountpoint != NULL) {
1113 delay = syncdelay / 2;
1120 vn_syncer_add_to_worklist(newvp, delay);
1122 bp->b_xflags |= B_VNDIRTY;
1123 tbp = TAILQ_FIRST(listheadp);
1125 (bp->b_lblkno >= 0 && tbp->b_lblkno > bp->b_lblkno)) {
1126 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs);
1128 if (bp->b_lblkno >= 0) {
1130 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) &&
1131 (ttbp->b_lblkno < bp->b_lblkno)) {
1134 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs);
1136 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs);
1140 bp->b_xflags |= B_VNCLEAN;
1141 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs);
1142 if ((newvp->v_flag & VONWORKLST) &&
1143 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) {
1144 newvp->v_flag &= ~VONWORKLST;
1145 LIST_REMOVE(newvp, v_synclist);
1154 * Create a vnode for a block device.
1155 * Used for mounting the root file system.
1162 register struct vnode *vp;
1166 /* XXX 255 is for mfs. */
1167 if (dev == NODEV || (major(dev) != 255 && (major(dev) >= nblkdev ||
1168 bdevsw[major(dev)] == NULL))) {
1172 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
1179 if ((nvp = checkalias(vp, dev, (struct mount *)0)) != NULL) {
1188 * Check to see if the new vnode represents a special device
1189 * for which we already have a vnode (either because of
1190 * bdevvp() or because of a different vnode representing
1191 * the same block device). If such an alias exists, deallocate
1192 * the existing contents and return the aliased vnode. The
1193 * caller is responsible for filling it with its new contents.
1196 checkalias(nvp, nvp_rdev, mp)
1197 register struct vnode *nvp;
1201 struct proc *p = curproc; /* XXX */
1204 int rmaj = major(nvp_rdev);
1206 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1209 vpp = &speclisth[SPECHASH(nvp_rdev)];
1211 simple_lock(&spechash_slock);
1212 for (vp = *vpp; vp; vp = vp->v_specnext) {
1213 if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type)
1216 * Alias, but not in use, so flush it out.
1217 * Only alias active device nodes.
1218 * Not sure why we don't re-use this like we do below.
1220 simple_lock(&vp->v_interlock);
1221 if (vp->v_usecount == 0) {
1222 simple_unlock(&spechash_slock);
1226 if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) {
1228 * It dissappeared, and we may have slept.
1229 * Restart from the beginning
1231 simple_unlock(&spechash_slock);
1237 * It would be a lot clearer what is going on here if
1238 * this had been expressed as:
1239 * if ( vp && (vp->v_tag == VT_NULL))
1240 * and the clauses had been swapped.
1242 if (vp == NULL || vp->v_tag != VT_NON) {
1243 struct specinfo *sinfo;
1246 * Put the new vnode into the hash chain.
1247 * and if there was an alias, connect them.
1249 MALLOC(sinfo, struct specinfo *,
1250 sizeof(struct specinfo), M_VNODE, M_WAITOK);
1251 bzero(sinfo, sizeof(struct specinfo));
1252 nvp->v_specinfo = sinfo;
1253 sinfo->si_rdev = nvp_rdev;
1254 sinfo->si_hashchain = vpp;
1255 sinfo->si_specnext = *vpp;
1256 sinfo->si_bsize_phys = DEV_BSIZE;
1257 sinfo->si_bsize_best = BLKDEV_IOSIZE;
1258 sinfo->si_bsize_max = MAXBSIZE;
1261 * Ask the device to fix up specinfo. Typically the
1262 * si_bsize_* parameters may need fixing up.
1265 if (nvp->v_type == VBLK && rmaj < nblkdev) {
1266 if (bdevsw[rmaj] && bdevsw[rmaj]->d_parms)
1268 (*bdevsw[rmaj]->d_parms)(nvp_rdev, sinfo, DPARM_GET);
1269 } else if (nvp->v_type == VCHR && rmaj < nchrdev) {
1270 if (cdevsw[rmaj] && cdevsw[rmaj]->d_parms)
1271 (*cdevsw[rmaj]->d_parms)(nvp_rdev, sinfo, DPARM_GET);
1274 simple_unlock(&spechash_slock);
1277 nvp->v_flag |= VALIASED;
1278 vp->v_flag |= VALIASED;
1284 * if ( vp && (vp->v_tag == VT_NULL))
1285 * We have a vnode alias, but it is a trashed.
1286 * Make it look like it's newley allocated. (by getnewvnode())
1287 * The caller should use this instead.
1289 simple_unlock(&spechash_slock);
1290 VOP_UNLOCK(vp, 0, p);
1291 simple_lock(&vp->v_interlock);
1293 vp->v_op = nvp->v_op;
1294 vp->v_tag = nvp->v_tag;
1301 * Grab a particular vnode from the free list, increment its
1302 * reference count and lock it. The vnode lock bit is set the
1303 * vnode is being eliminated in vgone. The process is awakened
1304 * when the transition is completed, and an error returned to
1305 * indicate that the vnode is no longer usable (possibly having
1306 * been changed to a new file system type).
1310 register struct vnode *vp;
1317 * If the vnode is in the process of being cleaned out for
1318 * another use, we wait for the cleaning to finish and then
1319 * return failure. Cleaning is determined by checking that
1320 * the VXLOCK flag is set.
1322 if ((flags & LK_INTERLOCK) == 0) {
1323 simple_lock(&vp->v_interlock);
1325 if (vp->v_flag & VXLOCK) {
1326 vp->v_flag |= VXWANT;
1327 simple_unlock(&vp->v_interlock);
1328 tsleep((caddr_t)vp, PINOD, "vget", 0);
1334 if (VSHOULDBUSY(vp))
1336 if (flags & LK_TYPE_MASK) {
1337 if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) {
1339 * must expand vrele here because we do not want
1340 * to call VOP_INACTIVE if the reference count
1341 * drops back to zero since it was never really
1342 * active. We must remove it from the free list
1343 * before sleeping so that multiple processes do
1344 * not try to recycle it.
1346 simple_lock(&vp->v_interlock);
1348 if (VSHOULDFREE(vp))
1350 simple_unlock(&vp->v_interlock);
1354 simple_unlock(&vp->v_interlock);
1359 vref(struct vnode *vp)
1361 simple_lock(&vp->v_interlock);
1363 simple_unlock(&vp->v_interlock);
1367 * Vnode put/release.
1368 * If count drops to zero, call inactive routine and return to freelist.
1374 struct proc *p = curproc; /* XXX */
1376 KASSERT(vp != NULL, ("vrele: null vp"));
1378 simple_lock(&vp->v_interlock);
1380 if (vp->v_usecount > 1) {
1383 simple_unlock(&vp->v_interlock);
1388 if (vp->v_usecount == 1) {
1391 if (VSHOULDFREE(vp))
1394 * If we are doing a vput, the node is already locked, and we must
1395 * call VOP_INACTIVE with the node locked. So, in the case of
1396 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1398 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) {
1399 VOP_INACTIVE(vp, p);
1404 vprint("vrele: negative ref count", vp);
1405 simple_unlock(&vp->v_interlock);
1407 panic("vrele: negative ref cnt");
1415 struct proc *p = curproc; /* XXX */
1417 KASSERT(vp != NULL, ("vput: null vp"));
1419 simple_lock(&vp->v_interlock);
1421 if (vp->v_usecount > 1) {
1424 VOP_UNLOCK(vp, LK_INTERLOCK, p);
1429 if (vp->v_usecount == 1) {
1432 if (VSHOULDFREE(vp))
1435 * If we are doing a vput, the node is already locked, and we must
1436 * call VOP_INACTIVE with the node locked. So, in the case of
1437 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1439 simple_unlock(&vp->v_interlock);
1440 VOP_INACTIVE(vp, p);
1444 vprint("vput: negative ref count", vp);
1446 panic("vput: negative ref cnt");
1451 * Somebody doesn't want the vnode recycled.
1455 register struct vnode *vp;
1461 if (VSHOULDBUSY(vp))
1467 * One less who cares about this vnode.
1471 register struct vnode *vp;
1476 if (vp->v_holdcnt <= 0)
1477 panic("vdrop: holdcnt");
1479 if (VSHOULDFREE(vp))
1485 * Remove any vnodes in the vnode table belonging to mount point mp.
1487 * If MNT_NOFORCE is specified, there should not be any active ones,
1488 * return error if any are found (nb: this is a user error, not a
1489 * system error). If MNT_FORCE is specified, detach any active vnodes
1493 static int busyprt = 0; /* print out busy vnodes */
1494 SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1498 vflush(mp, skipvp, flags)
1500 struct vnode *skipvp;
1503 struct proc *p = curproc; /* XXX */
1504 struct vnode *vp, *nvp;
1507 simple_lock(&mntvnode_slock);
1509 for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
1511 * Make sure this vnode wasn't reclaimed in getnewvnode().
1512 * Start over if it has (it won't be on the list anymore).
1514 if (vp->v_mount != mp)
1516 nvp = vp->v_mntvnodes.le_next;
1518 * Skip over a selected vnode.
1523 simple_lock(&vp->v_interlock);
1525 * Skip over a vnodes marked VSYSTEM.
1527 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1528 simple_unlock(&vp->v_interlock);
1532 * If WRITECLOSE is set, only flush out regular file vnodes
1535 if ((flags & WRITECLOSE) &&
1536 (vp->v_writecount == 0 || vp->v_type != VREG)) {
1537 simple_unlock(&vp->v_interlock);
1542 * With v_usecount == 0, all we need to do is clear out the
1543 * vnode data structures and we are done.
1545 if (vp->v_usecount == 0) {
1546 simple_unlock(&mntvnode_slock);
1548 simple_lock(&mntvnode_slock);
1553 * If FORCECLOSE is set, forcibly close the vnode. For block
1554 * or character devices, revert to an anonymous device. For
1555 * all other files, just kill them.
1557 if (flags & FORCECLOSE) {
1558 simple_unlock(&mntvnode_slock);
1559 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1563 vp->v_op = spec_vnodeop_p;
1564 insmntque(vp, (struct mount *) 0);
1566 simple_lock(&mntvnode_slock);
1571 vprint("vflush: busy vnode", vp);
1573 simple_unlock(&vp->v_interlock);
1576 simple_unlock(&mntvnode_slock);
1583 * Disassociate the underlying file system from a vnode.
1586 vclean(vp, flags, p)
1595 * Check to see if the vnode is in use. If so we have to reference it
1596 * before we clean it out so that its count cannot fall to zero and
1597 * generate a race against ourselves to recycle it.
1599 if ((active = vp->v_usecount))
1603 * Prevent the vnode from being recycled or brought into use while we
1606 if (vp->v_flag & VXLOCK)
1607 panic("vclean: deadlock");
1608 vp->v_flag |= VXLOCK;
1610 * Even if the count is zero, the VOP_INACTIVE routine may still
1611 * have the object locked while it cleans it out. The VOP_LOCK
1612 * ensures that the VOP_INACTIVE routine is done with its work.
1613 * For active vnodes, it ensures that no other activity can
1614 * occur while the underlying object is being cleaned out.
1616 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1619 * Clean out any buffers associated with the vnode.
1621 vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1622 if ((obj = vp->v_object) != NULL) {
1623 if (obj->ref_count == 0) {
1625 * This is a normal way of shutting down the object/vnode
1628 vm_object_terminate(obj);
1631 * Woe to the process that tries to page now :-).
1633 vm_pager_deallocate(obj);
1638 * If purging an active vnode, it must be closed and
1639 * deactivated before being reclaimed. Note that the
1640 * VOP_INACTIVE will unlock the vnode.
1643 if (flags & DOCLOSE)
1644 VOP_CLOSE(vp, FNONBLOCK, NOCRED, p);
1645 VOP_INACTIVE(vp, p);
1648 * Any other processes trying to obtain this lock must first
1649 * wait for VXLOCK to clear, then call the new lock operation.
1651 VOP_UNLOCK(vp, 0, p);
1654 * Reclaim the vnode.
1656 if (VOP_RECLAIM(vp, p))
1657 panic("vclean: cannot reclaim");
1664 #if 0 /* This is the only place we have LK_DRAINED in the entire kernel ??? */
1666 if ((vp->v_vnlock->lk_flags & LK_DRAINED) == 0)
1667 vprint("vclean: lock not drained", vp);
1670 FREE(vp->v_vnlock, M_VNODE);
1671 vp->v_vnlock = NULL;
1674 if (VSHOULDFREE(vp))
1678 * Done with purge, notify sleepers of the grim news.
1680 vp->v_op = dead_vnodeop_p;
1683 vp->v_flag &= ~VXLOCK;
1684 if (vp->v_flag & VXWANT) {
1685 vp->v_flag &= ~VXWANT;
1686 wakeup((caddr_t) vp);
1691 * Eliminate all activity associated with the requested vnode
1692 * and with all vnodes aliased to the requested vnode.
1696 struct vop_revoke_args /* {
1701 struct vnode *vp, *vq;
1702 struct proc *p = curproc; /* XXX */
1704 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1707 simple_lock(&vp->v_interlock);
1709 if (vp->v_flag & VALIASED) {
1711 * If a vgone (or vclean) is already in progress,
1712 * wait until it is done and return.
1714 if (vp->v_flag & VXLOCK) {
1715 vp->v_flag |= VXWANT;
1716 simple_unlock(&vp->v_interlock);
1717 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1721 * Ensure that vp will not be vgone'd while we
1722 * are eliminating its aliases.
1724 vp->v_flag |= VXLOCK;
1725 simple_unlock(&vp->v_interlock);
1726 while (vp->v_flag & VALIASED) {
1727 simple_lock(&spechash_slock);
1728 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1729 if (vq->v_rdev != vp->v_rdev ||
1730 vq->v_type != vp->v_type || vp == vq)
1732 simple_unlock(&spechash_slock);
1737 simple_unlock(&spechash_slock);
1741 * Remove the lock so that vgone below will
1742 * really eliminate the vnode after which time
1743 * vgone will awaken any sleepers.
1745 simple_lock(&vp->v_interlock);
1746 vp->v_flag &= ~VXLOCK;
1747 if (vp->v_flag & VXWANT) {
1748 vp->v_flag &= ~VXWANT;
1757 * Recycle an unused vnode to the front of the free list.
1758 * Release the passed interlock if the vnode will be recycled.
1761 vrecycle(vp, inter_lkp, p)
1763 struct simplelock *inter_lkp;
1767 simple_lock(&vp->v_interlock);
1768 if (vp->v_usecount == 0) {
1770 simple_unlock(inter_lkp);
1775 simple_unlock(&vp->v_interlock);
1780 * Eliminate all activity associated with a vnode
1781 * in preparation for reuse.
1785 register struct vnode *vp;
1787 struct proc *p = curproc; /* XXX */
1789 simple_lock(&vp->v_interlock);
1794 * vgone, with the vp interlock held.
1806 * If a vgone (or vclean) is already in progress,
1807 * wait until it is done and return.
1809 if (vp->v_flag & VXLOCK) {
1810 vp->v_flag |= VXWANT;
1811 simple_unlock(&vp->v_interlock);
1812 tsleep((caddr_t)vp, PINOD, "vgone", 0);
1817 * Clean out the filesystem specific data.
1819 vclean(vp, DOCLOSE, p);
1820 simple_lock(&vp->v_interlock);
1823 * Delete from old mount point vnode list, if on one.
1825 if (vp->v_mount != NULL)
1826 insmntque(vp, (struct mount *)0);
1828 * If special device, remove it from special device alias list
1831 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) {
1832 simple_lock(&spechash_slock);
1833 if (*vp->v_hashchain == vp) {
1834 *vp->v_hashchain = vp->v_specnext;
1836 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1837 if (vq->v_specnext != vp)
1839 vq->v_specnext = vp->v_specnext;
1843 panic("missing bdev");
1845 if (vp->v_flag & VALIASED) {
1847 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1848 if (vq->v_rdev != vp->v_rdev ||
1849 vq->v_type != vp->v_type)
1856 panic("missing alias");
1858 vx->v_flag &= ~VALIASED;
1859 vp->v_flag &= ~VALIASED;
1861 simple_unlock(&spechash_slock);
1862 FREE(vp->v_specinfo, M_VNODE);
1863 vp->v_specinfo = NULL;
1867 * If it is on the freelist and not already at the head,
1868 * move it to the head of the list. The test of the back
1869 * pointer and the reference count of zero is because
1870 * it will be removed from the free list by getnewvnode,
1871 * but will not have its reference count incremented until
1872 * after calling vgone. If the reference count were
1873 * incremented first, vgone would (incorrectly) try to
1874 * close the previous instance of the underlying object.
1876 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
1878 simple_lock(&vnode_free_list_slock);
1879 if (vp->v_flag & VFREE) {
1880 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1881 } else if (vp->v_flag & VTBFREE) {
1882 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
1883 vp->v_flag &= ~VTBFREE;
1887 vp->v_flag |= VFREE;
1888 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1889 simple_unlock(&vnode_free_list_slock);
1894 simple_unlock(&vp->v_interlock);
1898 * Lookup a vnode by device number.
1901 vfinddev(dev, type, vpp)
1906 register struct vnode *vp;
1909 simple_lock(&spechash_slock);
1910 for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
1911 if (dev != vp->v_rdev || type != vp->v_type)
1917 simple_unlock(&spechash_slock);
1922 * Calculate the total number of references to a special device.
1926 register struct vnode *vp;
1928 struct vnode *vq, *vnext;
1932 if ((vp->v_flag & VALIASED) == 0)
1933 return (vp->v_usecount);
1934 simple_lock(&spechash_slock);
1935 for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) {
1936 vnext = vq->v_specnext;
1937 if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
1940 * Alias, but not in use, so flush it out.
1942 if (vq->v_usecount == 0 && vq != vp) {
1943 simple_unlock(&spechash_slock);
1947 count += vq->v_usecount;
1949 simple_unlock(&spechash_slock);
1953 * Print out a description of a vnode.
1955 static char *typename[] =
1956 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1961 register struct vnode *vp;
1966 printf("%s: %p: ", label, (void *)vp);
1968 printf("%p: ", (void *)vp);
1969 printf("type %s, usecount %d, writecount %d, refcount %d,",
1970 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1973 if (vp->v_flag & VROOT)
1974 strcat(buf, "|VROOT");
1975 if (vp->v_flag & VTEXT)
1976 strcat(buf, "|VTEXT");
1977 if (vp->v_flag & VSYSTEM)
1978 strcat(buf, "|VSYSTEM");
1979 if (vp->v_flag & VXLOCK)
1980 strcat(buf, "|VXLOCK");
1981 if (vp->v_flag & VXWANT)
1982 strcat(buf, "|VXWANT");
1983 if (vp->v_flag & VBWAIT)
1984 strcat(buf, "|VBWAIT");
1985 if (vp->v_flag & VALIASED)
1986 strcat(buf, "|VALIASED");
1987 if (vp->v_flag & VDOOMED)
1988 strcat(buf, "|VDOOMED");
1989 if (vp->v_flag & VFREE)
1990 strcat(buf, "|VFREE");
1991 if (vp->v_flag & VOBJBUF)
1992 strcat(buf, "|VOBJBUF");
1994 printf(" flags (%s)", &buf[1]);
1995 if (vp->v_data == NULL) {
2004 #include <ddb/ddb.h>
2006 * List all of the locked vnodes in the system.
2007 * Called when debugging the kernel.
2009 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
2011 struct proc *p = curproc; /* XXX */
2012 struct mount *mp, *nmp;
2015 printf("Locked vnodes\n");
2016 simple_lock(&mountlist_slock);
2017 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
2018 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2019 nmp = mp->mnt_list.cqe_next;
2022 for (vp = mp->mnt_vnodelist.lh_first;
2024 vp = vp->v_mntvnodes.le_next) {
2025 if (VOP_ISLOCKED(vp))
2026 vprint((char *)0, vp);
2028 simple_lock(&mountlist_slock);
2029 nmp = mp->mnt_list.cqe_next;
2032 simple_unlock(&mountlist_slock);
2037 * Top level filesystem related information gathering.
2039 static int sysctl_ovfs_conf __P(SYSCTL_HANDLER_ARGS);
2042 vfs_sysctl SYSCTL_HANDLER_ARGS
2044 int *name = (int *)arg1 - 1; /* XXX */
2045 u_int namelen = arg2 + 1; /* XXX */
2046 struct vfsconf *vfsp;
2048 #if 1 || defined(COMPAT_PRELITE2)
2049 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
2051 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
2055 /* all sysctl names at this level are at least name and field */
2057 return (ENOTDIR); /* overloaded */
2058 if (name[0] != VFS_GENERIC) {
2059 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2060 if (vfsp->vfc_typenum == name[0])
2063 return (EOPNOTSUPP);
2064 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2065 oldp, oldlenp, newp, newlen, p));
2069 case VFS_MAXTYPENUM:
2072 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2075 return (ENOTDIR); /* overloaded */
2076 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2077 if (vfsp->vfc_typenum == name[2])
2080 return (EOPNOTSUPP);
2081 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2083 return (EOPNOTSUPP);
2086 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2087 "Generic filesystem");
2089 #if 1 || defined(COMPAT_PRELITE2)
2092 sysctl_ovfs_conf SYSCTL_HANDLER_ARGS
2095 struct vfsconf *vfsp;
2096 struct ovfsconf ovfs;
2098 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2099 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
2100 strcpy(ovfs.vfc_name, vfsp->vfc_name);
2101 ovfs.vfc_index = vfsp->vfc_typenum;
2102 ovfs.vfc_refcount = vfsp->vfc_refcount;
2103 ovfs.vfc_flags = vfsp->vfc_flags;
2104 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2111 #endif /* 1 || COMPAT_PRELITE2 */
2114 #define KINFO_VNODESLOP 10
2116 * Dump vnode list (via sysctl).
2117 * Copyout address of vnode followed by vnode.
2121 sysctl_vnode SYSCTL_HANDLER_ARGS
2123 struct proc *p = curproc; /* XXX */
2124 struct mount *mp, *nmp;
2125 struct vnode *nvp, *vp;
2128 #define VPTRSZ sizeof (struct vnode *)
2129 #define VNODESZ sizeof (struct vnode)
2132 if (!req->oldptr) /* Make an estimate */
2133 return (SYSCTL_OUT(req, 0,
2134 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2136 simple_lock(&mountlist_slock);
2137 for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
2138 if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2139 nmp = mp->mnt_list.cqe_next;
2143 simple_lock(&mntvnode_slock);
2144 for (vp = mp->mnt_vnodelist.lh_first;
2148 * Check that the vp is still associated with
2149 * this filesystem. RACE: could have been
2150 * recycled onto the same filesystem.
2152 if (vp->v_mount != mp) {
2153 simple_unlock(&mntvnode_slock);
2156 nvp = vp->v_mntvnodes.le_next;
2157 simple_unlock(&mntvnode_slock);
2158 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2159 (error = SYSCTL_OUT(req, vp, VNODESZ)))
2161 simple_lock(&mntvnode_slock);
2163 simple_unlock(&mntvnode_slock);
2164 simple_lock(&mountlist_slock);
2165 nmp = mp->mnt_list.cqe_next;
2168 simple_unlock(&mountlist_slock);
2176 * Exporting the vnode list on large systems causes them to crash.
2177 * Exporting the vnode list on medium systems causes sysctl to coredump.
2180 SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2181 0, 0, sysctl_vnode, "S,vnode", "");
2185 * Check to see if a filesystem is mounted on a block device.
2194 if (vp->v_specmountpoint != NULL)
2196 if (vp->v_flag & VALIASED) {
2197 simple_lock(&spechash_slock);
2198 for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
2199 if (vq->v_rdev != vp->v_rdev ||
2200 vq->v_type != vp->v_type)
2202 if (vq->v_specmountpoint != NULL) {
2207 simple_unlock(&spechash_slock);
2213 * Unmount all filesystems. The list is traversed in reverse order
2214 * of mounting to avoid dependencies.
2219 struct mount *mp, *nmp;
2223 if (curproc != NULL)
2226 p = initproc; /* XXX XXX should this be proc0? */
2228 * Since this only runs when rebooting, it is not interlocked.
2230 for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
2231 nmp = mp->mnt_list.cqe_prev;
2232 error = dounmount(mp, MNT_FORCE, p);
2234 printf("unmount of %s failed (",
2235 mp->mnt_stat.f_mntonname);
2239 printf("%d)\n", error);
2245 * Build hash lists of net addresses and hang them off the mount point.
2246 * Called by ufs_mount() to set up the lists of export addresses.
2249 vfs_hang_addrlist(mp, nep, argp)
2251 struct netexport *nep;
2252 struct export_args *argp;
2254 register struct netcred *np;
2255 register struct radix_node_head *rnh;
2257 struct radix_node *rn;
2258 struct sockaddr *saddr, *smask = 0;
2262 if (argp->ex_addrlen == 0) {
2263 if (mp->mnt_flag & MNT_DEFEXPORTED)
2265 np = &nep->ne_defexported;
2266 np->netc_exflags = argp->ex_flags;
2267 np->netc_anon = argp->ex_anon;
2268 np->netc_anon.cr_ref = 1;
2269 mp->mnt_flag |= MNT_DEFEXPORTED;
2272 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2273 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2274 bzero((caddr_t) np, i);
2275 saddr = (struct sockaddr *) (np + 1);
2276 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2278 if (saddr->sa_len > argp->ex_addrlen)
2279 saddr->sa_len = argp->ex_addrlen;
2280 if (argp->ex_masklen) {
2281 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2282 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2285 if (smask->sa_len > argp->ex_masklen)
2286 smask->sa_len = argp->ex_masklen;
2288 i = saddr->sa_family;
2289 if ((rnh = nep->ne_rtable[i]) == 0) {
2291 * Seems silly to initialize every AF when most are not used,
2292 * do so on demand here
2294 for (dom = domains; dom; dom = dom->dom_next)
2295 if (dom->dom_family == i && dom->dom_rtattach) {
2296 dom->dom_rtattach((void **) &nep->ne_rtable[i],
2300 if ((rnh = nep->ne_rtable[i]) == 0) {
2305 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2307 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
2311 np->netc_exflags = argp->ex_flags;
2312 np->netc_anon = argp->ex_anon;
2313 np->netc_anon.cr_ref = 1;
2316 free(np, M_NETADDR);
2322 vfs_free_netcred(rn, w)
2323 struct radix_node *rn;
2326 register struct radix_node_head *rnh = (struct radix_node_head *) w;
2328 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2329 free((caddr_t) rn, M_NETADDR);
2334 * Free the net address hash lists that are hanging off the mount points.
2337 vfs_free_addrlist(nep)
2338 struct netexport *nep;
2341 register struct radix_node_head *rnh;
2343 for (i = 0; i <= AF_MAX; i++)
2344 if ((rnh = nep->ne_rtable[i])) {
2345 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2347 free((caddr_t) rnh, M_RTABLE);
2348 nep->ne_rtable[i] = 0;
2353 vfs_export(mp, nep, argp)
2355 struct netexport *nep;
2356 struct export_args *argp;
2360 if (argp->ex_flags & MNT_DELEXPORT) {
2361 if (mp->mnt_flag & MNT_EXPUBLIC) {
2362 vfs_setpublicfs(NULL, NULL, NULL);
2363 mp->mnt_flag &= ~MNT_EXPUBLIC;
2365 vfs_free_addrlist(nep);
2366 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2368 if (argp->ex_flags & MNT_EXPORTED) {
2369 if (argp->ex_flags & MNT_EXPUBLIC) {
2370 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2372 mp->mnt_flag |= MNT_EXPUBLIC;
2374 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2376 mp->mnt_flag |= MNT_EXPORTED;
2383 * Set the publicly exported filesystem (WebNFS). Currently, only
2384 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2387 vfs_setpublicfs(mp, nep, argp)
2389 struct netexport *nep;
2390 struct export_args *argp;
2397 * mp == NULL -> invalidate the current info, the FS is
2398 * no longer exported. May be called from either vfs_export
2399 * or unmount, so check if it hasn't already been done.
2402 if (nfs_pub.np_valid) {
2403 nfs_pub.np_valid = 0;
2404 if (nfs_pub.np_index != NULL) {
2405 FREE(nfs_pub.np_index, M_TEMP);
2406 nfs_pub.np_index = NULL;
2413 * Only one allowed at a time.
2415 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2419 * Get real filehandle for root of exported FS.
2421 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2422 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2424 if ((error = VFS_ROOT(mp, &rvp)))
2427 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2433 * If an indexfile was specified, pull it in.
2435 if (argp->ex_indexfile != NULL) {
2436 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2438 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2439 MAXNAMLEN, (size_t *)0);
2442 * Check for illegal filenames.
2444 for (cp = nfs_pub.np_index; *cp; cp++) {
2452 FREE(nfs_pub.np_index, M_TEMP);
2457 nfs_pub.np_mount = mp;
2458 nfs_pub.np_valid = 1;
2463 vfs_export_lookup(mp, nep, nam)
2464 register struct mount *mp;
2465 struct netexport *nep;
2466 struct sockaddr *nam;
2468 register struct netcred *np;
2469 register struct radix_node_head *rnh;
2470 struct sockaddr *saddr;
2473 if (mp->mnt_flag & MNT_EXPORTED) {
2475 * Lookup in the export list first.
2479 rnh = nep->ne_rtable[saddr->sa_family];
2481 np = (struct netcred *)
2482 (*rnh->rnh_matchaddr)((caddr_t)saddr,
2484 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2489 * If no address match, use the default if it exists.
2491 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2492 np = &nep->ne_defexported;
2498 * perform msync on all vnodes under a mount point
2499 * the mount point must be locked.
2502 vfs_msync(struct mount *mp, int flags) {
2503 struct vnode *vp, *nvp;
2504 struct vm_object *obj;
2510 for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
2512 nvp = vp->v_mntvnodes.le_next;
2514 if (vp->v_mount != mp) {
2518 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */
2521 if (flags != MNT_WAIT) {
2523 if (obj == NULL || (obj->flags & OBJ_MIGHTBEDIRTY) == 0)
2525 if (VOP_ISLOCKED(vp))
2529 simple_lock(&vp->v_interlock);
2531 (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
2533 LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
2535 vm_object_page_clean(vp->v_object, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : 0);
2541 simple_unlock(&vp->v_interlock);
2544 if (anyio && (--tries > 0))
2549 * Create the VM object needed for VMIO and mmap support. This
2550 * is done for all VREG files in the system. Some filesystems might
2551 * afford the additional metadata buffering capability of the
2552 * VMIO code by making the device node be VMIO mode also.
2554 * vp must be locked when vfs_object_create is called.
2557 vfs_object_create(vp, p, cred)
2566 if ((vp->v_type != VREG) && (vp->v_type != VBLK))
2570 if ((object = vp->v_object) == NULL) {
2571 if (vp->v_type == VREG) {
2572 if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
2574 object = vnode_pager_alloc(vp, vat.va_size, 0, 0);
2575 } else if (major(vp->v_rdev) < nblkdev &&
2576 bdevsw[major(vp->v_rdev)] != NULL) {
2578 * This simply allocates the biggest object possible
2579 * for a VBLK vnode. This should be fixed, but doesn't
2580 * cause any problems (yet).
2582 object = vnode_pager_alloc(vp, IDX_TO_OFF(INT_MAX), 0, 0);
2587 * Dereference the reference we just created. This assumes
2588 * that the object is associated with the vp.
2590 object->ref_count--;
2593 if (object->flags & OBJ_DEAD) {
2594 VOP_UNLOCK(vp, 0, p);
2595 tsleep(object, PVM, "vodead", 0);
2596 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2601 KASSERT(vp->v_object != NULL, ("vfs_object_create: NULL object"));
2602 vp->v_flag |= VOBJBUF;
2615 simple_lock(&vnode_free_list_slock);
2616 if (vp->v_flag & VTBFREE) {
2617 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2618 vp->v_flag &= ~VTBFREE;
2620 if (vp->v_flag & VAGE) {
2621 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2623 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2626 simple_unlock(&vnode_free_list_slock);
2627 vp->v_flag &= ~VAGE;
2628 vp->v_flag |= VFREE;
2639 simple_lock(&vnode_free_list_slock);
2640 if (vp->v_flag & VTBFREE) {
2641 TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2642 vp->v_flag &= ~VTBFREE;
2644 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2647 simple_unlock(&vnode_free_list_slock);
2648 vp->v_flag &= ~(VFREE|VAGE);
2653 * Record a process's interest in events which might happen to
2654 * a vnode. Because poll uses the historic select-style interface
2655 * internally, this routine serves as both the ``check for any
2656 * pending events'' and the ``record my interest in future events''
2657 * functions. (These are done together, while the lock is held,
2658 * to avoid race conditions.)
2661 vn_pollrecord(vp, p, events)
2666 simple_lock(&vp->v_pollinfo.vpi_lock);
2667 if (vp->v_pollinfo.vpi_revents & events) {
2669 * This leaves events we are not interested
2670 * in available for the other process which
2671 * which presumably had requested them
2672 * (otherwise they would never have been
2675 events &= vp->v_pollinfo.vpi_revents;
2676 vp->v_pollinfo.vpi_revents &= ~events;
2678 simple_unlock(&vp->v_pollinfo.vpi_lock);
2681 vp->v_pollinfo.vpi_events |= events;
2682 selrecord(p, &vp->v_pollinfo.vpi_selinfo);
2683 simple_unlock(&vp->v_pollinfo.vpi_lock);
2688 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2689 * it is possible for us to miss an event due to race conditions, but
2690 * that condition is expected to be rare, so for the moment it is the
2691 * preferred interface.
2694 vn_pollevent(vp, events)
2698 simple_lock(&vp->v_pollinfo.vpi_lock);
2699 if (vp->v_pollinfo.vpi_events & events) {
2701 * We clear vpi_events so that we don't
2702 * call selwakeup() twice if two events are
2703 * posted before the polling process(es) is
2704 * awakened. This also ensures that we take at
2705 * most one selwakeup() if the polling process
2706 * is no longer interested. However, it does
2707 * mean that only one event can be noticed at
2708 * a time. (Perhaps we should only clear those
2709 * event bits which we note?) XXX
2711 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2712 vp->v_pollinfo.vpi_revents |= events;
2713 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2715 simple_unlock(&vp->v_pollinfo.vpi_lock);
2719 * Wake up anyone polling on vp because it is being revoked.
2720 * This depends on dead_poll() returning POLLHUP for correct
2727 simple_lock(&vp->v_pollinfo.vpi_lock);
2728 if (vp->v_pollinfo.vpi_events) {
2729 vp->v_pollinfo.vpi_events = 0;
2730 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2732 simple_unlock(&vp->v_pollinfo.vpi_lock);
2738 * Routine to create and manage a filesystem syncer vnode.
2740 #define sync_close ((int (*) __P((struct vop_close_args *)))nullop)
2741 static int sync_fsync __P((struct vop_fsync_args *));
2742 static int sync_inactive __P((struct vop_inactive_args *));
2743 static int sync_reclaim __P((struct vop_reclaim_args *));
2744 #define sync_lock ((int (*) __P((struct vop_lock_args *)))vop_nolock)
2745 #define sync_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock)
2746 static int sync_print __P((struct vop_print_args *));
2747 #define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2749 static vop_t **sync_vnodeop_p;
2750 static struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2751 { &vop_default_desc, (vop_t *) vop_eopnotsupp },
2752 { &vop_close_desc, (vop_t *) sync_close }, /* close */
2753 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */
2754 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */
2755 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */
2756 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */
2757 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */
2758 { &vop_print_desc, (vop_t *) sync_print }, /* print */
2759 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */
2762 static struct vnodeopv_desc sync_vnodeop_opv_desc =
2763 { &sync_vnodeop_p, sync_vnodeop_entries };
2765 VNODEOP_SET(sync_vnodeop_opv_desc);
2768 * Create a new filesystem syncer vnode for the specified mount point.
2771 vfs_allocate_syncvnode(mp)
2775 static long start, incr, next;
2778 /* Allocate a new vnode */
2779 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2780 mp->mnt_syncer = NULL;
2785 * Place the vnode onto the syncer worklist. We attempt to
2786 * scatter them about on the list so that they will go off
2787 * at evenly distributed times even if all the filesystems
2788 * are mounted at once.
2791 if (next == 0 || next > syncer_maxdelay) {
2795 start = syncer_maxdelay / 2;
2796 incr = syncer_maxdelay;
2800 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2801 mp->mnt_syncer = vp;
2806 * Do a lazy sync of the filesystem.
2810 struct vop_fsync_args /* {
2812 struct ucred *a_cred;
2817 struct vnode *syncvp = ap->a_vp;
2818 struct mount *mp = syncvp->v_mount;
2819 struct proc *p = ap->a_p;
2823 * We only need to do something if this is a lazy evaluation.
2825 if (ap->a_waitfor != MNT_LAZY)
2829 * Move ourselves to the back of the sync list.
2831 vn_syncer_add_to_worklist(syncvp, syncdelay);
2834 * Walk the list of vnodes pushing all that are dirty and
2835 * not already on the sync list.
2837 simple_lock(&mountlist_slock);
2838 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, p) != 0) {
2839 simple_unlock(&mountlist_slock);
2842 asyncflag = mp->mnt_flag & MNT_ASYNC;
2843 mp->mnt_flag &= ~MNT_ASYNC;
2844 vfs_msync(mp, MNT_NOWAIT);
2845 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, p);
2847 mp->mnt_flag |= MNT_ASYNC;
2853 * The syncer vnode is no referenced.
2857 struct vop_inactive_args /* {
2868 * The syncer vnode is no longer needed and is being decommissioned.
2870 * Modifications to the worklist must be protected at splbio().
2874 struct vop_reclaim_args /* {
2878 struct vnode *vp = ap->a_vp;
2882 vp->v_mount->mnt_syncer = NULL;
2883 if (vp->v_flag & VONWORKLST) {
2884 LIST_REMOVE(vp, v_synclist);
2885 vp->v_flag &= ~VONWORKLST;
2893 * Print out a syncer vnode.
2897 struct vop_print_args /* {
2901 struct vnode *vp = ap->a_vp;
2903 printf("syncer vnode");
2904 if (vp->v_vnlock != NULL)
2905 lockmgr_printinfo(vp->v_vnlock);