vfs_export.c revision 37101
1/*
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.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
25 *
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
36 * SUCH DAMAGE.
37 *
38 *	@(#)vfs_subr.c	8.31 (Berkeley) 5/26/95
39 * $Id: vfs_subr.c,v 1.156 1998/06/10 22:02:14 julian Exp $
40 */
41
42/*
43 * External virtual filesystem routines
44 */
45#include "opt_ddb.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/kernel.h>
50#include <sys/proc.h>
51#include <sys/malloc.h>
52#include <sys/mount.h>
53#include <sys/socket.h>
54#include <sys/vnode.h>
55#include <sys/stat.h>
56#include <sys/buf.h>
57#include <sys/domain.h>
58#include <sys/dirent.h>
59#include <sys/vmmeter.h>
60
61#include <machine/limits.h>
62
63#include <vm/vm.h>
64#include <vm/vm_object.h>
65#include <vm/vm_extern.h>
66#include <vm/pmap.h>
67#include <vm/vm_map.h>
68#include <vm/vm_pager.h>
69#include <vm/vnode_pager.h>
70#include <vm/vm_zone.h>
71#include <sys/sysctl.h>
72
73#include <miscfs/specfs/specdev.h>
74
75static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
76
77static void	insmntque __P((struct vnode *vp, struct mount *mp));
78#ifdef DDB
79static void	printlockedvnodes __P((void));
80#endif
81static void	vclean __P((struct vnode *vp, int flags, struct proc *p));
82static void	vfree __P((struct vnode *));
83static void	vgonel __P((struct vnode *vp, struct proc *p));
84static unsigned long	numvnodes;
85SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
86
87enum vtype iftovt_tab[16] = {
88	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
89	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
90};
91int vttoif_tab[9] = {
92	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
93	S_IFSOCK, S_IFIFO, S_IFMT,
94};
95
96/*
97 * Insq/Remq for the vnode usage lists.
98 */
99#define	bufinsvn(bp, dp)	LIST_INSERT_HEAD(dp, bp, b_vnbufs)
100#define	bufremvn(bp) {							\
101	LIST_REMOVE(bp, b_vnbufs);					\
102	(bp)->b_vnbufs.le_next = NOLIST;				\
103}
104
105static TAILQ_HEAD(freelst, vnode) vnode_free_list;	/* vnode free list */
106struct tobefreelist vnode_tobefree_list;	/* vnode free list */
107
108static u_long wantfreevnodes = 25;
109SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, "");
110static u_long freevnodes = 0;
111SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, "");
112
113int vfs_ioopt = 0;
114#ifdef ENABLE_VFS_IOOPT
115SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, "");
116#endif
117
118struct mntlist mountlist;	/* mounted filesystem list */
119struct simplelock mountlist_slock;
120static struct simplelock mntid_slock;
121struct simplelock mntvnode_slock;
122static struct simplelock vnode_free_list_slock;
123static struct simplelock spechash_slock;
124struct nfs_public nfs_pub;	/* publicly exported FS */
125static vm_zone_t vnode_zone;
126
127/*
128 * The workitem queue.
129 */
130#define SYNCER_MAXDELAY		32
131int syncer_maxdelay =		SYNCER_MAXDELAY;	/* maximum delay time */
132time_t syncdelay =		30;
133int rushjob;				/* number of slots to run ASAP */
134
135static int syncer_delayno = 0;
136static long syncer_mask;
137LIST_HEAD(synclist, vnode);
138static struct synclist *syncer_workitem_pending;
139
140int desiredvnodes;
141SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, &desiredvnodes, 0, "");
142
143static void	vfs_free_addrlist __P((struct netexport *nep));
144static int	vfs_free_netcred __P((struct radix_node *rn, void *w));
145static int	vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep,
146				       struct export_args *argp));
147
148/*
149 * Initialize the vnode management data structures.
150 */
151void
152vntblinit()
153{
154
155	desiredvnodes = maxproc + cnt.v_page_count / 4;
156	simple_lock_init(&mntvnode_slock);
157	simple_lock_init(&mntid_slock);
158	simple_lock_init(&spechash_slock);
159	TAILQ_INIT(&vnode_free_list);
160	TAILQ_INIT(&vnode_tobefree_list);
161	simple_lock_init(&vnode_free_list_slock);
162	CIRCLEQ_INIT(&mountlist);
163	vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5);
164	/*
165	 * Initialize the filesystem syncer.
166	 */
167	syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE,
168		&syncer_mask);
169	syncer_maxdelay = syncer_mask + 1;
170}
171
172/*
173 * Mark a mount point as busy. Used to synchronize access and to delay
174 * unmounting. Interlock is not released on failure.
175 */
176int
177vfs_busy(mp, flags, interlkp, p)
178	struct mount *mp;
179	int flags;
180	struct simplelock *interlkp;
181	struct proc *p;
182{
183	int lkflags;
184
185	if (mp->mnt_kern_flag & MNTK_UNMOUNT) {
186		if (flags & LK_NOWAIT)
187			return (ENOENT);
188		mp->mnt_kern_flag |= MNTK_MWAIT;
189		if (interlkp) {
190			simple_unlock(interlkp);
191		}
192		/*
193		 * Since all busy locks are shared except the exclusive
194		 * lock granted when unmounting, the only place that a
195		 * wakeup needs to be done is at the release of the
196		 * exclusive lock at the end of dounmount.
197		 */
198		tsleep((caddr_t)mp, PVFS, "vfs_busy", 0);
199		if (interlkp) {
200			simple_lock(interlkp);
201		}
202		return (ENOENT);
203	}
204	lkflags = LK_SHARED | LK_NOPAUSE;
205	if (interlkp)
206		lkflags |= LK_INTERLOCK;
207	if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p))
208		panic("vfs_busy: unexpected lock failure");
209	return (0);
210}
211
212/*
213 * Free a busy filesystem.
214 */
215void
216vfs_unbusy(mp, p)
217	struct mount *mp;
218	struct proc *p;
219{
220
221	lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p);
222}
223
224/*
225 * Lookup a filesystem type, and if found allocate and initialize
226 * a mount structure for it.
227 *
228 * Devname is usually updated by mount(8) after booting.
229 */
230int
231vfs_rootmountalloc(fstypename, devname, mpp)
232	char *fstypename;
233	char *devname;
234	struct mount **mpp;
235{
236	struct proc *p = curproc;	/* XXX */
237	struct vfsconf *vfsp;
238	struct mount *mp;
239
240	if (fstypename == NULL)
241		return (ENODEV);
242	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
243		if (!strcmp(vfsp->vfc_name, fstypename))
244			break;
245	if (vfsp == NULL)
246		return (ENODEV);
247	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
248	bzero((char *)mp, (u_long)sizeof(struct mount));
249	lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE);
250	(void)vfs_busy(mp, LK_NOWAIT, 0, p);
251	LIST_INIT(&mp->mnt_vnodelist);
252	mp->mnt_vfc = vfsp;
253	mp->mnt_op = vfsp->vfc_vfsops;
254	mp->mnt_flag = MNT_RDONLY;
255	mp->mnt_vnodecovered = NULLVP;
256	vfsp->vfc_refcount++;
257	mp->mnt_stat.f_type = vfsp->vfc_typenum;
258	mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
259	strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
260	mp->mnt_stat.f_mntonname[0] = '/';
261	mp->mnt_stat.f_mntonname[1] = 0;
262	(void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0);
263	*mpp = mp;
264	return (0);
265}
266
267/*
268 * Find an appropriate filesystem to use for the root. If a filesystem
269 * has not been preselected, walk through the list of known filesystems
270 * trying those that have mountroot routines, and try them until one
271 * works or we have tried them all.
272 */
273#ifdef notdef	/* XXX JH */
274int
275lite2_vfs_mountroot()
276{
277	struct vfsconf *vfsp;
278	extern int (*lite2_mountroot) __P((void));
279	int error;
280
281	if (lite2_mountroot != NULL)
282		return ((*lite2_mountroot)());
283	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
284		if (vfsp->vfc_mountroot == NULL)
285			continue;
286		if ((error = (*vfsp->vfc_mountroot)()) == 0)
287			return (0);
288		printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error);
289	}
290	return (ENODEV);
291}
292#endif
293
294/*
295 * Lookup a mount point by filesystem identifier.
296 */
297struct mount *
298vfs_getvfs(fsid)
299	fsid_t *fsid;
300{
301	register struct mount *mp;
302
303	simple_lock(&mountlist_slock);
304	for (mp = mountlist.cqh_first; mp != (void *)&mountlist;
305	    mp = mp->mnt_list.cqe_next) {
306		if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
307		    mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) {
308			simple_unlock(&mountlist_slock);
309			return (mp);
310	    }
311	}
312	simple_unlock(&mountlist_slock);
313	return ((struct mount *) 0);
314}
315
316/*
317 * Get a new unique fsid
318 */
319void
320vfs_getnewfsid(mp)
321	struct mount *mp;
322{
323	static u_short xxxfs_mntid;
324
325	fsid_t tfsid;
326	int mtype;
327
328	simple_lock(&mntid_slock);
329	mtype = mp->mnt_vfc->vfc_typenum;
330	mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0);
331	mp->mnt_stat.f_fsid.val[1] = mtype;
332	if (xxxfs_mntid == 0)
333		++xxxfs_mntid;
334	tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid);
335	tfsid.val[1] = mtype;
336	if (mountlist.cqh_first != (void *)&mountlist) {
337		while (vfs_getvfs(&tfsid)) {
338			tfsid.val[0]++;
339			xxxfs_mntid++;
340		}
341	}
342	mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
343	simple_unlock(&mntid_slock);
344}
345
346/*
347 * Set vnode attributes to VNOVAL
348 */
349void
350vattr_null(vap)
351	register struct vattr *vap;
352{
353
354	vap->va_type = VNON;
355	vap->va_size = VNOVAL;
356	vap->va_bytes = VNOVAL;
357	vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid =
358	    vap->va_fsid = vap->va_fileid =
359	    vap->va_blocksize = vap->va_rdev =
360	    vap->va_atime.tv_sec = vap->va_atime.tv_nsec =
361	    vap->va_mtime.tv_sec = vap->va_mtime.tv_nsec =
362	    vap->va_ctime.tv_sec = vap->va_ctime.tv_nsec =
363	    vap->va_flags = vap->va_gen = VNOVAL;
364	vap->va_vaflags = 0;
365}
366
367/*
368 * Routines having to do with the management of the vnode table.
369 */
370extern vop_t **dead_vnodeop_p;
371
372/*
373 * Return the next vnode from the free list.
374 */
375int
376getnewvnode(tag, mp, vops, vpp)
377	enum vtagtype tag;
378	struct mount *mp;
379	vop_t **vops;
380	struct vnode **vpp;
381{
382	int s;
383	struct proc *p = curproc;	/* XXX */
384	struct vnode *vp, *tvp, *nvp;
385	vm_object_t object;
386	TAILQ_HEAD(freelst, vnode) vnode_tmp_list;
387
388	/*
389	 * We take the least recently used vnode from the freelist
390	 * if we can get it and it has no cached pages, and no
391	 * namecache entries are relative to it.
392	 * Otherwise we allocate a new vnode
393	 */
394
395	s = splbio();
396	simple_lock(&vnode_free_list_slock);
397	TAILQ_INIT(&vnode_tmp_list);
398
399	for (vp = TAILQ_FIRST(&vnode_tobefree_list); vp; vp = nvp) {
400		nvp = TAILQ_NEXT(vp, v_freelist);
401		TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
402		if (vp->v_flag & VAGE) {
403			TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
404		} else {
405			TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
406		}
407		vp->v_flag &= ~(VTBFREE|VAGE);
408		vp->v_flag |= VFREE;
409		if (vp->v_usecount)
410			panic("tobe free vnode isn't");
411		freevnodes++;
412	}
413
414	if (wantfreevnodes && freevnodes < wantfreevnodes) {
415		vp = NULL;
416	} else if (!wantfreevnodes && freevnodes <= desiredvnodes) {
417		/*
418		 * XXX: this is only here to be backwards compatible
419		 */
420		vp = NULL;
421	} else {
422		for (vp = TAILQ_FIRST(&vnode_free_list); vp; vp = nvp) {
423
424			nvp = TAILQ_NEXT(vp, v_freelist);
425
426			if (!simple_lock_try(&vp->v_interlock))
427				continue;
428			if (vp->v_usecount)
429				panic("free vnode isn't");
430
431			object = vp->v_object;
432			if (object && (object->resident_page_count || object->ref_count)) {
433				printf("object inconsistant state: RPC: %d, RC: %d\n",
434					object->resident_page_count, object->ref_count);
435				/* Don't recycle if it's caching some pages */
436				TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
437				TAILQ_INSERT_TAIL(&vnode_tmp_list, vp, v_freelist);
438				continue;
439			} else if (LIST_FIRST(&vp->v_cache_src)) {
440				/* Don't recycle if active in the namecache */
441				simple_unlock(&vp->v_interlock);
442				continue;
443			} else {
444				break;
445			}
446		}
447	}
448
449	for (tvp = TAILQ_FIRST(&vnode_tmp_list); tvp; tvp = nvp) {
450		nvp = TAILQ_NEXT(tvp, v_freelist);
451		TAILQ_REMOVE(&vnode_tmp_list, tvp, v_freelist);
452		TAILQ_INSERT_TAIL(&vnode_free_list, tvp, v_freelist);
453		simple_unlock(&tvp->v_interlock);
454	}
455
456	if (vp) {
457		vp->v_flag |= VDOOMED;
458		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
459		freevnodes--;
460		simple_unlock(&vnode_free_list_slock);
461		cache_purge(vp);
462		vp->v_lease = NULL;
463		if (vp->v_type != VBAD) {
464			vgonel(vp, p);
465		} else {
466			simple_unlock(&vp->v_interlock);
467		}
468
469#ifdef DIAGNOSTIC
470		{
471			int s;
472
473			if (vp->v_data)
474				panic("cleaned vnode isn't");
475			s = splbio();
476			if (vp->v_numoutput)
477				panic("Clean vnode has pending I/O's");
478			splx(s);
479		}
480#endif
481		vp->v_flag = 0;
482		vp->v_lastr = 0;
483		vp->v_lastw = 0;
484		vp->v_lasta = 0;
485		vp->v_cstart = 0;
486		vp->v_clen = 0;
487		vp->v_socket = 0;
488		vp->v_writecount = 0;	/* XXX */
489		vp->v_maxio = 0;
490	} else {
491		simple_unlock(&vnode_free_list_slock);
492		vp = (struct vnode *) zalloc(vnode_zone);
493		bzero((char *) vp, sizeof *vp);
494		simple_lock_init(&vp->v_interlock);
495		vp->v_dd = vp;
496		cache_purge(vp);
497		LIST_INIT(&vp->v_cache_src);
498		TAILQ_INIT(&vp->v_cache_dst);
499		numvnodes++;
500	}
501
502	vp->v_type = VNON;
503	vp->v_tag = tag;
504	vp->v_op = vops;
505	insmntque(vp, mp);
506	*vpp = vp;
507	vp->v_usecount = 1;
508	vp->v_data = 0;
509	splx(s);
510
511	vfs_object_create(vp, p, p->p_ucred, TRUE);
512	return (0);
513}
514
515/*
516 * Move a vnode from one mount queue to another.
517 */
518static void
519insmntque(vp, mp)
520	register struct vnode *vp;
521	register struct mount *mp;
522{
523
524	simple_lock(&mntvnode_slock);
525	/*
526	 * Delete from old mount point vnode list, if on one.
527	 */
528	if (vp->v_mount != NULL)
529		LIST_REMOVE(vp, v_mntvnodes);
530	/*
531	 * Insert into list of vnodes for the new mount point, if available.
532	 */
533	if ((vp->v_mount = mp) == NULL) {
534		simple_unlock(&mntvnode_slock);
535		return;
536	}
537	LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
538	simple_unlock(&mntvnode_slock);
539}
540
541/*
542 * Update outstanding I/O count and do wakeup if requested.
543 */
544void
545vwakeup(bp)
546	register struct buf *bp;
547{
548	register struct vnode *vp;
549
550	bp->b_flags &= ~B_WRITEINPROG;
551	if ((vp = bp->b_vp)) {
552		vp->v_numoutput--;
553		if (vp->v_numoutput < 0)
554			panic("vwakeup: neg numoutput");
555		if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) {
556			vp->v_flag &= ~VBWAIT;
557			wakeup((caddr_t) &vp->v_numoutput);
558		}
559	}
560}
561
562/*
563 * Flush out and invalidate all buffers associated with a vnode.
564 * Called with the underlying object locked.
565 */
566int
567vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
568	register struct vnode *vp;
569	int flags;
570	struct ucred *cred;
571	struct proc *p;
572	int slpflag, slptimeo;
573{
574	register struct buf *bp;
575	struct buf *nbp, *blist;
576	int s, error;
577	vm_object_t object;
578
579	if (flags & V_SAVE) {
580		s = splbio();
581		while (vp->v_numoutput) {
582			vp->v_flag |= VBWAIT;
583			tsleep((caddr_t)&vp->v_numoutput,
584				slpflag | (PRIBIO + 1),
585				"vinvlbuf", slptimeo);
586		}
587		if (vp->v_dirtyblkhd.lh_first != NULL) {
588			splx(s);
589			if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0)
590				return (error);
591			s = splbio();
592			if (vp->v_numoutput > 0 ||
593			    vp->v_dirtyblkhd.lh_first != NULL)
594				panic("vinvalbuf: dirty bufs");
595		}
596		splx(s);
597  	}
598	s = splbio();
599	for (;;) {
600		if ((blist = vp->v_cleanblkhd.lh_first) && (flags & V_SAVEMETA))
601			while (blist && blist->b_lblkno < 0)
602				blist = blist->b_vnbufs.le_next;
603		if (!blist && (blist = vp->v_dirtyblkhd.lh_first) &&
604		    (flags & V_SAVEMETA))
605			while (blist && blist->b_lblkno < 0)
606				blist = blist->b_vnbufs.le_next;
607		if (!blist)
608			break;
609
610		for (bp = blist; bp; bp = nbp) {
611			nbp = bp->b_vnbufs.le_next;
612			if ((flags & V_SAVEMETA) && bp->b_lblkno < 0)
613				continue;
614			if (bp->b_flags & B_BUSY) {
615				bp->b_flags |= B_WANTED;
616				error = tsleep((caddr_t) bp,
617				    slpflag | (PRIBIO + 4), "vinvalbuf",
618				    slptimeo);
619				if (error) {
620					splx(s);
621					return (error);
622				}
623				break;
624			}
625			/*
626			 * XXX Since there are no node locks for NFS, I
627			 * believe there is a slight chance that a delayed
628			 * write will occur while sleeping just above, so
629			 * check for it.  Note that vfs_bio_awrite expects
630			 * buffers to reside on a queue, while VOP_BWRITE and
631			 * brelse do not.
632			 */
633			if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
634				(flags & V_SAVE)) {
635
636				if (bp->b_vp == vp) {
637					if (bp->b_flags & B_CLUSTEROK) {
638						vfs_bio_awrite(bp);
639					} else {
640						bremfree(bp);
641						bp->b_flags |= (B_BUSY | B_ASYNC);
642						VOP_BWRITE(bp);
643					}
644				} else {
645					bremfree(bp);
646					bp->b_flags |= B_BUSY;
647					(void) VOP_BWRITE(bp);
648				}
649				break;
650			}
651			bremfree(bp);
652			bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF | B_BUSY);
653			bp->b_flags &= ~B_ASYNC;
654			brelse(bp);
655		}
656	}
657
658	while (vp->v_numoutput > 0) {
659		vp->v_flag |= VBWAIT;
660		tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0);
661	}
662
663	splx(s);
664
665	/*
666	 * Destroy the copy in the VM cache, too.
667	 */
668	simple_lock(&vp->v_interlock);
669	object = vp->v_object;
670	if (object != NULL) {
671		if (flags & V_SAVEMETA)
672			vm_object_page_remove(object, 0, object->size,
673				(flags & V_SAVE) ? TRUE : FALSE);
674		else
675			vm_object_page_remove(object, 0, 0,
676				(flags & V_SAVE) ? TRUE : FALSE);
677	}
678	simple_unlock(&vp->v_interlock);
679
680	if (!(flags & V_SAVEMETA) &&
681	    (vp->v_dirtyblkhd.lh_first || vp->v_cleanblkhd.lh_first))
682		panic("vinvalbuf: flush failed");
683	return (0);
684}
685
686/*
687 * Truncate a file's buffer and pages to a specified length.  This
688 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
689 * sync activity.
690 */
691int
692vtruncbuf(vp, cred, p, length, blksize)
693	register struct vnode *vp;
694	struct ucred *cred;
695	struct proc *p;
696	off_t length;
697	int blksize;
698{
699	register struct buf *bp;
700	struct buf *nbp, *blist;
701	int s, error, anyfreed;
702	vm_object_t object;
703	int trunclbn;
704
705	/*
706	 * Round up to the *next* lbn.
707	 */
708	trunclbn = (length + blksize - 1) / blksize;
709
710	s = splbio();
711restart:
712	anyfreed = 1;
713	for (;anyfreed;) {
714		anyfreed = 0;
715		for ( bp = LIST_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) {
716
717			nbp = LIST_NEXT(bp, b_vnbufs);
718
719			if (bp->b_lblkno >= trunclbn) {
720				if (bp->b_flags & B_BUSY) {
721					bp->b_flags |= B_WANTED;
722					tsleep(bp, PRIBIO + 4, "vtrb1", 0);
723					goto restart;
724				} else {
725					bremfree(bp);
726					bp->b_flags |= (B_BUSY | B_INVAL | B_RELBUF);
727					bp->b_flags &= ~B_ASYNC;
728					brelse(bp);
729					anyfreed = 1;
730				}
731				if (nbp &&
732					((LIST_NEXT(nbp, b_vnbufs) == NOLIST) ||
733					 (nbp->b_vp != vp) ||
734					 (nbp->b_flags & B_DELWRI))) {
735					goto restart;
736				}
737			}
738		}
739
740		for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
741
742			nbp = LIST_NEXT(bp, b_vnbufs);
743
744			if (bp->b_lblkno >= trunclbn) {
745				if (bp->b_flags & B_BUSY) {
746					bp->b_flags |= B_WANTED;
747					tsleep(bp, PRIBIO + 4, "vtrb2", 0);
748					goto restart;
749				} else {
750					bremfree(bp);
751					bp->b_flags |= (B_BUSY | B_INVAL | B_RELBUF);
752					bp->b_flags &= ~B_ASYNC;
753					brelse(bp);
754					anyfreed = 1;
755				}
756				if (nbp &&
757					((LIST_NEXT(nbp, b_vnbufs) == NOLIST) ||
758					 (nbp->b_vp != vp) ||
759					 (nbp->b_flags & B_DELWRI) == 0)) {
760					goto restart;
761				}
762			}
763		}
764	}
765
766	if (length > 0) {
767restartsync:
768		for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
769
770			nbp = LIST_NEXT(bp, b_vnbufs);
771
772			if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) {
773				if (bp->b_flags & B_BUSY) {
774					bp->b_flags |= B_WANTED;
775					tsleep(bp, PRIBIO, "vtrb3", 0);
776				} else {
777					bremfree(bp);
778					bp->b_flags |= B_BUSY;
779					if (bp->b_vp == vp) {
780						bp->b_flags |= B_ASYNC;
781					} else {
782						bp->b_flags &= ~B_ASYNC;
783					}
784					VOP_BWRITE(bp);
785				}
786				goto restartsync;
787			}
788
789		}
790	}
791
792	while (vp->v_numoutput > 0) {
793		vp->v_flag |= VBWAIT;
794		tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0);
795	}
796
797	splx(s);
798
799	vnode_pager_setsize(vp, length);
800
801	return (0);
802}
803
804/*
805 * Associate a buffer with a vnode.
806 */
807void
808bgetvp(vp, bp)
809	register struct vnode *vp;
810	register struct buf *bp;
811{
812	int s;
813
814#if defined(DIAGNOSTIC)
815	if (bp->b_vp)
816		panic("bgetvp: not free");
817#endif
818	vhold(vp);
819	bp->b_vp = vp;
820	if (vp->v_type == VBLK || vp->v_type == VCHR)
821		bp->b_dev = vp->v_rdev;
822	else
823		bp->b_dev = NODEV;
824	/*
825	 * Insert onto list for new vnode.
826	 */
827	s = splbio();
828	bufinsvn(bp, &vp->v_cleanblkhd);
829	splx(s);
830}
831
832/*
833 * Disassociate a buffer from a vnode.
834 */
835void
836brelvp(bp)
837	register struct buf *bp;
838{
839	struct vnode *vp;
840	int s;
841
842#if defined(DIAGNOSTIC)
843	if (bp->b_vp == (struct vnode *) 0)
844		panic("brelvp: NULL");
845#endif
846
847	/*
848	 * Delete from old vnode list, if on one.
849	 */
850	vp = bp->b_vp;
851	s = splbio();
852	if (bp->b_vnbufs.le_next != NOLIST)
853		bufremvn(bp);
854	if ((vp->v_flag & VONWORKLST) && (LIST_FIRST(&vp->v_dirtyblkhd) == NULL)) {
855		vp->v_flag &= ~VONWORKLST;
856		LIST_REMOVE(vp, v_synclist);
857	}
858	splx(s);
859	bp->b_vp = (struct vnode *) 0;
860	vdrop(vp);
861}
862
863/*
864 * The workitem queue.
865 *
866 * It is useful to delay writes of file data and filesystem metadata
867 * for tens of seconds so that quickly created and deleted files need
868 * not waste disk bandwidth being created and removed. To realize this,
869 * we append vnodes to a "workitem" queue. When running with a soft
870 * updates implementation, most pending metadata dependencies should
871 * not wait for more than a few seconds. Thus, mounted on block devices
872 * are delayed only about a half the time that file data is delayed.
873 * Similarly, directory updates are more critical, so are only delayed
874 * about a third the time that file data is delayed. Thus, there are
875 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of
876 * one each second (driven off the filesystem syner process). The
877 * syncer_delayno variable indicates the next queue that is to be processed.
878 * Items that need to be processed soon are placed in this queue:
879 *
880 *	syncer_workitem_pending[syncer_delayno]
881 *
882 * A delay of fifteen seconds is done by placing the request fifteen
883 * entries later in the queue:
884 *
885 *	syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask]
886 *
887 */
888
889/*
890 * Add an item to the syncer work queue.
891 */
892void
893vn_syncer_add_to_worklist(vp, delay)
894	struct vnode *vp;
895	int delay;
896{
897	int s, slot;
898
899	s = splbio();
900
901	if (vp->v_flag & VONWORKLST) {
902		LIST_REMOVE(vp, v_synclist);
903	}
904
905	if (delay > syncer_maxdelay - 2)
906		delay = syncer_maxdelay - 2;
907	slot = (syncer_delayno + delay) & syncer_mask;
908
909	LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist);
910	vp->v_flag |= VONWORKLST;
911	splx(s);
912}
913
914static void sched_sync __P((void));
915static struct	proc *updateproc;
916static struct kproc_desc up_kp = {
917	"syncer",
918	sched_sync,
919	&updateproc
920};
921SYSINIT_KT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp)
922
923/*
924 * System filesystem synchronizer daemon.
925 */
926void
927sched_sync(void)
928{
929	struct synclist *slp;
930	struct vnode *vp;
931	long starttime;
932	int s;
933	struct proc *p = updateproc;
934
935	for (;;) {
936		starttime = time_second;
937
938		/*
939		 * Push files whose dirty time has expired.
940		 */
941		s = splbio();
942		slp = &syncer_workitem_pending[syncer_delayno];
943		syncer_delayno += 1;
944		if (syncer_delayno == syncer_maxdelay)
945			syncer_delayno = 0;
946		splx(s);
947
948		while ((vp = LIST_FIRST(slp)) != NULL) {
949			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
950			(void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p);
951			VOP_UNLOCK(vp, 0, p);
952			if (LIST_FIRST(slp) == vp) {
953				if (LIST_FIRST(&vp->v_dirtyblkhd) == NULL &&
954				    vp->v_type != VBLK)
955					panic("sched_sync: fsync failed");
956				/*
957				 * Move ourselves to the back of the sync list.
958				 */
959				LIST_REMOVE(vp, v_synclist);
960				vn_syncer_add_to_worklist(vp, syncdelay);
961			}
962		}
963
964		/*
965		 * Do soft update processing.
966		 */
967		if (bioops.io_sync)
968			(*bioops.io_sync)(NULL);
969
970		/*
971		 * The variable rushjob allows the kernel to speed up the
972		 * processing of the filesystem syncer process. A rushjob
973		 * value of N tells the filesystem syncer to process the next
974		 * N seconds worth of work on its queue ASAP. Currently rushjob
975		 * is used by the soft update code to speed up the filesystem
976		 * syncer process when the incore state is getting so far
977		 * ahead of the disk that the kernel memory pool is being
978		 * threatened with exhaustion.
979		 */
980		if (rushjob > 0) {
981			rushjob -= 1;
982			continue;
983		}
984		/*
985		 * If it has taken us less than a second to process the
986		 * current work, then wait. Otherwise start right over
987		 * again. We can still lose time if any single round
988		 * takes more than two seconds, but it does not really
989		 * matter as we are just trying to generally pace the
990		 * filesystem activity.
991		 */
992		if (time_second == starttime)
993			tsleep(&lbolt, PPAUSE, "syncer", 0);
994	}
995}
996
997/*
998 * Associate a p-buffer with a vnode.
999 */
1000void
1001pbgetvp(vp, bp)
1002	register struct vnode *vp;
1003	register struct buf *bp;
1004{
1005#if defined(DIAGNOSTIC)
1006	if (bp->b_vp)
1007		panic("pbgetvp: not free");
1008#endif
1009	bp->b_vp = vp;
1010	if (vp->v_type == VBLK || vp->v_type == VCHR)
1011		bp->b_dev = vp->v_rdev;
1012	else
1013		bp->b_dev = NODEV;
1014}
1015
1016/*
1017 * Disassociate a p-buffer from a vnode.
1018 */
1019void
1020pbrelvp(bp)
1021	register struct buf *bp;
1022{
1023
1024#if defined(DIAGNOSTIC)
1025	if (bp->b_vp == (struct vnode *) 0)
1026		panic("pbrelvp: NULL");
1027#endif
1028
1029	bp->b_vp = (struct vnode *) 0;
1030}
1031
1032/*
1033 * Reassign a buffer from one vnode to another.
1034 * Used to assign file specific control information
1035 * (indirect blocks) to the vnode to which they belong.
1036 */
1037void
1038reassignbuf(bp, newvp)
1039	register struct buf *bp;
1040	register struct vnode *newvp;
1041{
1042	struct buflists *listheadp;
1043	int delay;
1044	int s;
1045
1046	if (newvp == NULL) {
1047		printf("reassignbuf: NULL");
1048		return;
1049	}
1050
1051	s = splbio();
1052	/*
1053	 * Delete from old vnode list, if on one.
1054	 */
1055	if (bp->b_vnbufs.le_next != NOLIST) {
1056		bufremvn(bp);
1057		vdrop(bp->b_vp);
1058	}
1059	/*
1060	 * If dirty, put on list of dirty buffers; otherwise insert onto list
1061	 * of clean buffers.
1062	 */
1063	if (bp->b_flags & B_DELWRI) {
1064		struct buf *tbp;
1065
1066		listheadp = &newvp->v_dirtyblkhd;
1067		if ((newvp->v_flag & VONWORKLST) == 0) {
1068			switch (newvp->v_type) {
1069			case VDIR:
1070				delay = syncdelay / 3;
1071				break;
1072			case VBLK:
1073				if (newvp->v_specmountpoint != NULL) {
1074					delay = syncdelay / 2;
1075					break;
1076				}
1077				/* fall through */
1078			default:
1079				delay = syncdelay;
1080			}
1081			vn_syncer_add_to_worklist(newvp, delay);
1082		}
1083		tbp = listheadp->lh_first;
1084		if (!tbp || (tbp->b_lblkno > bp->b_lblkno)) {
1085			bufinsvn(bp, listheadp);
1086		} else {
1087			while (tbp->b_vnbufs.le_next &&
1088			    (tbp->b_vnbufs.le_next->b_lblkno < bp->b_lblkno)) {
1089				tbp = tbp->b_vnbufs.le_next;
1090			}
1091			LIST_INSERT_AFTER(tbp, bp, b_vnbufs);
1092		}
1093	} else {
1094		bufinsvn(bp, &newvp->v_cleanblkhd);
1095		if ((newvp->v_flag & VONWORKLST) &&
1096			LIST_FIRST(&newvp->v_dirtyblkhd) == NULL) {
1097			newvp->v_flag &= ~VONWORKLST;
1098			LIST_REMOVE(newvp, v_synclist);
1099		}
1100	}
1101	bp->b_vp = newvp;
1102	vhold(bp->b_vp);
1103	splx(s);
1104}
1105
1106#ifndef SLICE
1107/*
1108 * Create a vnode for a block device.
1109 * Used for mounting the root file system.
1110 */
1111int
1112bdevvp(dev, vpp)
1113	dev_t dev;
1114	struct vnode **vpp;
1115{
1116	register struct vnode *vp;
1117	struct vnode *nvp;
1118	int error;
1119
1120	if (dev == NODEV)
1121		return (0);
1122	error = getnewvnode(VT_NON, (struct mount *) 0, spec_vnodeop_p, &nvp);
1123	if (error) {
1124		*vpp = 0;
1125		return (error);
1126	}
1127	vp = nvp;
1128	vp->v_type = VBLK;
1129	if ((nvp = checkalias(vp, dev, (struct mount *) 0))) {
1130		vput(vp);
1131		vp = nvp;
1132	}
1133	*vpp = vp;
1134	return (0);
1135}
1136#endif	/* !SLICE */
1137
1138/*
1139 * Check to see if the new vnode represents a special device
1140 * for which we already have a vnode (either because of
1141 * bdevvp() or because of a different vnode representing
1142 * the same block device). If such an alias exists, deallocate
1143 * the existing contents and return the aliased vnode. The
1144 * caller is responsible for filling it with its new contents.
1145 */
1146struct vnode *
1147checkalias(nvp, nvp_rdev, mp)
1148	register struct vnode *nvp;
1149	dev_t nvp_rdev;
1150	struct mount *mp;
1151{
1152	struct proc *p = curproc;	/* XXX */
1153	struct vnode *vp;
1154	struct vnode **vpp;
1155
1156	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1157		return (NULLVP);
1158
1159	vpp = &speclisth[SPECHASH(nvp_rdev)];
1160loop:
1161	simple_lock(&spechash_slock);
1162	for (vp = *vpp; vp; vp = vp->v_specnext) {
1163		if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type)
1164			continue;
1165		/*
1166		 * Alias, but not in use, so flush it out.
1167		 * Only alias active device nodes.
1168		 * Not sure why we don't re-use this like we do below.
1169		 */
1170		simple_lock(&vp->v_interlock);
1171		if (vp->v_usecount == 0) {
1172			simple_unlock(&spechash_slock);
1173			vgonel(vp, p);
1174			goto loop;
1175		}
1176		if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p)) {
1177			/*
1178			 * It dissappeared, and we may have slept.
1179			 * Restart from the beginning
1180			 */
1181			simple_unlock(&spechash_slock);
1182			goto loop;
1183		}
1184		break;
1185	}
1186	/*
1187	 * It would be a lot clearer what is going on here if
1188	 * this had been expressed as:
1189	 * if ( vp && (vp->v_tag == VT_NULL))
1190	 * and the clauses had been swapped.
1191	 */
1192	if (vp == NULL || vp->v_tag != VT_NON) {
1193		/*
1194		 * Put the new vnode into the hash chain.
1195		 * and if there was an alias, connect them.
1196		 */
1197		MALLOC(nvp->v_specinfo, struct specinfo *,
1198		    sizeof(struct specinfo), M_VNODE, M_WAITOK);
1199		nvp->v_rdev = nvp_rdev;
1200		nvp->v_hashchain = vpp;
1201		nvp->v_specnext = *vpp;
1202		nvp->v_specmountpoint = NULL;
1203		simple_unlock(&spechash_slock);
1204		*vpp = nvp;
1205		if (vp != NULLVP) {
1206			nvp->v_flag |= VALIASED;
1207			vp->v_flag |= VALIASED;
1208			vput(vp);
1209		}
1210		return (NULLVP);
1211	}
1212	/*
1213	 * if ( vp && (vp->v_tag == VT_NULL))
1214	 * We have a vnode alias, but it is a trashed.
1215	 * Make it look like it's newley allocated. (by getnewvnode())
1216	 * The caller should use this instead.
1217	 */
1218	simple_unlock(&spechash_slock);
1219	VOP_UNLOCK(vp, 0, p);
1220	simple_lock(&vp->v_interlock);
1221	vclean(vp, 0, p);
1222	vp->v_op = nvp->v_op;
1223	vp->v_tag = nvp->v_tag;
1224	nvp->v_type = VNON;
1225	insmntque(vp, mp);
1226	return (vp);
1227}
1228
1229/*
1230 * Grab a particular vnode from the free list, increment its
1231 * reference count and lock it. The vnode lock bit is set the
1232 * vnode is being eliminated in vgone. The process is awakened
1233 * when the transition is completed, and an error returned to
1234 * indicate that the vnode is no longer usable (possibly having
1235 * been changed to a new file system type).
1236 */
1237int
1238vget(vp, flags, p)
1239	register struct vnode *vp;
1240	int flags;
1241	struct proc *p;
1242{
1243	int error;
1244
1245	/*
1246	 * If the vnode is in the process of being cleaned out for
1247	 * another use, we wait for the cleaning to finish and then
1248	 * return failure. Cleaning is determined by checking that
1249	 * the VXLOCK flag is set.
1250	 */
1251	if ((flags & LK_INTERLOCK) == 0) {
1252		simple_lock(&vp->v_interlock);
1253	}
1254	if (vp->v_flag & VXLOCK) {
1255		vp->v_flag |= VXWANT;
1256		simple_unlock(&vp->v_interlock);
1257		tsleep((caddr_t)vp, PINOD, "vget", 0);
1258		return (ENOENT);
1259	}
1260
1261	vp->v_usecount++;
1262
1263	if (VSHOULDBUSY(vp))
1264		vbusy(vp);
1265	if (flags & LK_TYPE_MASK) {
1266		if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) {
1267			/*
1268			 * must expand vrele here because we do not want
1269			 * to call VOP_INACTIVE if the reference count
1270			 * drops back to zero since it was never really
1271			 * active. We must remove it from the free list
1272			 * before sleeping so that multiple processes do
1273			 * not try to recycle it.
1274			 */
1275			simple_lock(&vp->v_interlock);
1276			vp->v_usecount--;
1277			if (VSHOULDFREE(vp))
1278				vfree(vp);
1279			simple_unlock(&vp->v_interlock);
1280		}
1281		return (error);
1282	}
1283	simple_unlock(&vp->v_interlock);
1284	return (0);
1285}
1286
1287void
1288vref(struct vnode *vp)
1289{
1290	simple_lock(&vp->v_interlock);
1291	vp->v_usecount++;
1292	simple_unlock(&vp->v_interlock);
1293}
1294
1295/*
1296 * Vnode put/release.
1297 * If count drops to zero, call inactive routine and return to freelist.
1298 */
1299void
1300vrele(vp)
1301	struct vnode *vp;
1302{
1303	struct proc *p = curproc;	/* XXX */
1304
1305#ifdef DIAGNOSTIC
1306	if (vp == NULL)
1307		panic("vrele: null vp");
1308#endif
1309	simple_lock(&vp->v_interlock);
1310
1311	if (vp->v_usecount > 1) {
1312
1313		vp->v_usecount--;
1314		simple_unlock(&vp->v_interlock);
1315
1316		return;
1317	}
1318
1319	if (vp->v_usecount == 1) {
1320
1321		vp->v_usecount--;
1322
1323		if (VSHOULDFREE(vp))
1324			vfree(vp);
1325	/*
1326	 * If we are doing a vput, the node is already locked, and we must
1327	 * call VOP_INACTIVE with the node locked.  So, in the case of
1328	 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1329	 */
1330		if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) {
1331			VOP_INACTIVE(vp, p);
1332		}
1333
1334	} else {
1335#ifdef DIAGNOSTIC
1336		vprint("vrele: negative ref count", vp);
1337		simple_unlock(&vp->v_interlock);
1338#endif
1339		panic("vrele: negative ref cnt");
1340	}
1341}
1342
1343void
1344vput(vp)
1345	struct vnode *vp;
1346{
1347	struct proc *p = curproc;	/* XXX */
1348
1349#ifdef DIAGNOSTIC
1350	if (vp == NULL)
1351		panic("vput: null vp");
1352#endif
1353
1354	simple_lock(&vp->v_interlock);
1355
1356	if (vp->v_usecount > 1) {
1357
1358		vp->v_usecount--;
1359		VOP_UNLOCK(vp, LK_INTERLOCK, p);
1360		return;
1361
1362	}
1363
1364	if (vp->v_usecount == 1) {
1365
1366		vp->v_usecount--;
1367		if (VSHOULDFREE(vp))
1368			vfree(vp);
1369	/*
1370	 * If we are doing a vput, the node is already locked, and we must
1371	 * call VOP_INACTIVE with the node locked.  So, in the case of
1372	 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1373	 */
1374		simple_unlock(&vp->v_interlock);
1375		VOP_INACTIVE(vp, p);
1376
1377	} else {
1378#ifdef DIAGNOSTIC
1379		vprint("vput: negative ref count", vp);
1380#endif
1381		panic("vput: negative ref cnt");
1382	}
1383}
1384
1385/*
1386 * Somebody doesn't want the vnode recycled.
1387 */
1388void
1389vhold(vp)
1390	register struct vnode *vp;
1391{
1392	int s;
1393
1394  	s = splbio();
1395	vp->v_holdcnt++;
1396	if (VSHOULDBUSY(vp))
1397		vbusy(vp);
1398	splx(s);
1399}
1400
1401/*
1402 * One less who cares about this vnode.
1403 */
1404void
1405vdrop(vp)
1406	register struct vnode *vp;
1407{
1408	int s;
1409
1410	s = splbio();
1411	if (vp->v_holdcnt <= 0)
1412		panic("vdrop: holdcnt");
1413	vp->v_holdcnt--;
1414	if (VSHOULDFREE(vp))
1415		vfree(vp);
1416	splx(s);
1417}
1418
1419/*
1420 * Remove any vnodes in the vnode table belonging to mount point mp.
1421 *
1422 * If MNT_NOFORCE is specified, there should not be any active ones,
1423 * return error if any are found (nb: this is a user error, not a
1424 * system error). If MNT_FORCE is specified, detach any active vnodes
1425 * that are found.
1426 */
1427#ifdef DIAGNOSTIC
1428static int busyprt = 0;		/* print out busy vnodes */
1429SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1430#endif
1431
1432int
1433vflush(mp, skipvp, flags)
1434	struct mount *mp;
1435	struct vnode *skipvp;
1436	int flags;
1437{
1438	struct proc *p = curproc;	/* XXX */
1439	struct vnode *vp, *nvp;
1440	int busy = 0;
1441
1442	simple_lock(&mntvnode_slock);
1443loop:
1444	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
1445		/*
1446		 * Make sure this vnode wasn't reclaimed in getnewvnode().
1447		 * Start over if it has (it won't be on the list anymore).
1448		 */
1449		if (vp->v_mount != mp)
1450			goto loop;
1451		nvp = vp->v_mntvnodes.le_next;
1452		/*
1453		 * Skip over a selected vnode.
1454		 */
1455		if (vp == skipvp)
1456			continue;
1457
1458		simple_lock(&vp->v_interlock);
1459		/*
1460		 * Skip over a vnodes marked VSYSTEM.
1461		 */
1462		if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1463			simple_unlock(&vp->v_interlock);
1464			continue;
1465		}
1466		/*
1467		 * If WRITECLOSE is set, only flush out regular file vnodes
1468		 * open for writing.
1469		 */
1470		if ((flags & WRITECLOSE) &&
1471		    (vp->v_writecount == 0 || vp->v_type != VREG)) {
1472			simple_unlock(&vp->v_interlock);
1473			continue;
1474		}
1475
1476		/*
1477		 * With v_usecount == 0, all we need to do is clear out the
1478		 * vnode data structures and we are done.
1479		 */
1480		if (vp->v_usecount == 0) {
1481			simple_unlock(&mntvnode_slock);
1482			vgonel(vp, p);
1483			simple_lock(&mntvnode_slock);
1484			continue;
1485		}
1486
1487		/*
1488		 * If FORCECLOSE is set, forcibly close the vnode. For block
1489		 * or character devices, revert to an anonymous device. For
1490		 * all other files, just kill them.
1491		 */
1492		if (flags & FORCECLOSE) {
1493			simple_unlock(&mntvnode_slock);
1494			if (vp->v_type != VBLK && vp->v_type != VCHR) {
1495				vgonel(vp, p);
1496			} else {
1497				vclean(vp, 0, p);
1498				vp->v_op = spec_vnodeop_p;
1499				insmntque(vp, (struct mount *) 0);
1500			}
1501			simple_lock(&mntvnode_slock);
1502			continue;
1503		}
1504#ifdef DIAGNOSTIC
1505		if (busyprt)
1506			vprint("vflush: busy vnode", vp);
1507#endif
1508		simple_unlock(&vp->v_interlock);
1509		busy++;
1510	}
1511	simple_unlock(&mntvnode_slock);
1512	if (busy)
1513		return (EBUSY);
1514	return (0);
1515}
1516
1517/*
1518 * Disassociate the underlying file system from a vnode.
1519 */
1520static void
1521vclean(vp, flags, p)
1522	struct vnode *vp;
1523	int flags;
1524	struct proc *p;
1525{
1526	int active;
1527	vm_object_t obj;
1528
1529	/*
1530	 * Check to see if the vnode is in use. If so we have to reference it
1531	 * before we clean it out so that its count cannot fall to zero and
1532	 * generate a race against ourselves to recycle it.
1533	 */
1534	if ((active = vp->v_usecount))
1535		vp->v_usecount++;
1536
1537	/*
1538	 * Prevent the vnode from being recycled or brought into use while we
1539	 * clean it out.
1540	 */
1541	if (vp->v_flag & VXLOCK)
1542		panic("vclean: deadlock");
1543	vp->v_flag |= VXLOCK;
1544	/*
1545	 * Even if the count is zero, the VOP_INACTIVE routine may still
1546	 * have the object locked while it cleans it out. The VOP_LOCK
1547	 * ensures that the VOP_INACTIVE routine is done with its work.
1548	 * For active vnodes, it ensures that no other activity can
1549	 * occur while the underlying object is being cleaned out.
1550	 */
1551	VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1552
1553	/*
1554	 * Clean out any buffers associated with the vnode.
1555	 */
1556	vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1557	if (obj = vp->v_object) {
1558		if (obj->ref_count == 0) {
1559			/*
1560			 * This is a normal way of shutting down the object/vnode
1561			 * association.
1562			 */
1563			vm_object_terminate(obj);
1564		} else {
1565			/*
1566			 * Woe to the process that tries to page now :-).
1567			 */
1568			vm_pager_deallocate(obj);
1569		}
1570	}
1571
1572	/*
1573	 * If purging an active vnode, it must be closed and
1574	 * deactivated before being reclaimed. Note that the
1575	 * VOP_INACTIVE will unlock the vnode.
1576	 */
1577	if (active) {
1578		if (flags & DOCLOSE)
1579			VOP_CLOSE(vp, IO_NDELAY, NOCRED, p);
1580		VOP_INACTIVE(vp, p);
1581	} else {
1582		/*
1583		 * Any other processes trying to obtain this lock must first
1584		 * wait for VXLOCK to clear, then call the new lock operation.
1585		 */
1586		VOP_UNLOCK(vp, 0, p);
1587	}
1588	/*
1589	 * Reclaim the vnode.
1590	 */
1591	if (VOP_RECLAIM(vp, p))
1592		panic("vclean: cannot reclaim");
1593
1594	if (active)
1595		vrele(vp);
1596
1597	cache_purge(vp);
1598	if (vp->v_vnlock) {
1599#if 0 /* This is the only place we have LK_DRAINED in the entire kernel ??? */
1600#ifdef DIAGNOSTIC
1601		if ((vp->v_vnlock->lk_flags & LK_DRAINED) == 0)
1602			vprint("vclean: lock not drained", vp);
1603#endif
1604#endif
1605		FREE(vp->v_vnlock, M_VNODE);
1606		vp->v_vnlock = NULL;
1607	}
1608
1609	if (VSHOULDFREE(vp))
1610		vfree(vp);
1611
1612	/*
1613	 * Done with purge, notify sleepers of the grim news.
1614	 */
1615	vp->v_op = dead_vnodeop_p;
1616	vn_pollgone(vp);
1617	vp->v_tag = VT_NON;
1618	vp->v_flag &= ~VXLOCK;
1619	if (vp->v_flag & VXWANT) {
1620		vp->v_flag &= ~VXWANT;
1621		wakeup((caddr_t) vp);
1622	}
1623}
1624
1625/*
1626 * Eliminate all activity associated with the requested vnode
1627 * and with all vnodes aliased to the requested vnode.
1628 */
1629int
1630vop_revoke(ap)
1631	struct vop_revoke_args /* {
1632		struct vnode *a_vp;
1633		int a_flags;
1634	} */ *ap;
1635{
1636	struct vnode *vp, *vq;
1637	struct proc *p = curproc;	/* XXX */
1638
1639#ifdef DIAGNOSTIC
1640	if ((ap->a_flags & REVOKEALL) == 0)
1641		panic("vop_revoke");
1642#endif
1643
1644	vp = ap->a_vp;
1645	simple_lock(&vp->v_interlock);
1646
1647	if (vp->v_flag & VALIASED) {
1648		/*
1649		 * If a vgone (or vclean) is already in progress,
1650		 * wait until it is done and return.
1651		 */
1652		if (vp->v_flag & VXLOCK) {
1653			vp->v_flag |= VXWANT;
1654			simple_unlock(&vp->v_interlock);
1655			tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1656			return (0);
1657		}
1658		/*
1659		 * Ensure that vp will not be vgone'd while we
1660		 * are eliminating its aliases.
1661		 */
1662		vp->v_flag |= VXLOCK;
1663		simple_unlock(&vp->v_interlock);
1664		while (vp->v_flag & VALIASED) {
1665			simple_lock(&spechash_slock);
1666			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1667				if (vq->v_rdev != vp->v_rdev ||
1668				    vq->v_type != vp->v_type || vp == vq)
1669					continue;
1670				simple_unlock(&spechash_slock);
1671				vgone(vq);
1672				break;
1673			}
1674			if (vq == NULLVP) {
1675				simple_unlock(&spechash_slock);
1676			}
1677		}
1678		/*
1679		 * Remove the lock so that vgone below will
1680		 * really eliminate the vnode after which time
1681		 * vgone will awaken any sleepers.
1682		 */
1683		simple_lock(&vp->v_interlock);
1684		vp->v_flag &= ~VXLOCK;
1685		if (vp->v_flag & VXWANT) {
1686			vp->v_flag &= ~VXWANT;
1687			wakeup(vp);
1688		}
1689	}
1690	vgonel(vp, p);
1691	return (0);
1692}
1693
1694/*
1695 * Recycle an unused vnode to the front of the free list.
1696 * Release the passed interlock if the vnode will be recycled.
1697 */
1698int
1699vrecycle(vp, inter_lkp, p)
1700	struct vnode *vp;
1701	struct simplelock *inter_lkp;
1702	struct proc *p;
1703{
1704
1705	simple_lock(&vp->v_interlock);
1706	if (vp->v_usecount == 0) {
1707		if (inter_lkp) {
1708			simple_unlock(inter_lkp);
1709		}
1710		vgonel(vp, p);
1711		return (1);
1712	}
1713	simple_unlock(&vp->v_interlock);
1714	return (0);
1715}
1716
1717/*
1718 * Eliminate all activity associated with a vnode
1719 * in preparation for reuse.
1720 */
1721void
1722vgone(vp)
1723	register struct vnode *vp;
1724{
1725	struct proc *p = curproc;	/* XXX */
1726
1727	simple_lock(&vp->v_interlock);
1728	vgonel(vp, p);
1729}
1730
1731/*
1732 * vgone, with the vp interlock held.
1733 */
1734static void
1735vgonel(vp, p)
1736	struct vnode *vp;
1737	struct proc *p;
1738{
1739	int s;
1740	struct vnode *vq;
1741	struct vnode *vx;
1742
1743	/*
1744	 * If a vgone (or vclean) is already in progress,
1745	 * wait until it is done and return.
1746	 */
1747	if (vp->v_flag & VXLOCK) {
1748		vp->v_flag |= VXWANT;
1749		simple_unlock(&vp->v_interlock);
1750		tsleep((caddr_t)vp, PINOD, "vgone", 0);
1751		return;
1752	}
1753
1754	/*
1755	 * Clean out the filesystem specific data.
1756	 */
1757	vclean(vp, DOCLOSE, p);
1758	simple_lock(&vp->v_interlock);
1759
1760	/*
1761	 * Delete from old mount point vnode list, if on one.
1762	 */
1763	if (vp->v_mount != NULL)
1764		insmntque(vp, (struct mount *)0);
1765	/*
1766	 * If special device, remove it from special device alias list
1767	 * if it is on one.
1768	 */
1769	if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) {
1770		simple_lock(&spechash_slock);
1771		if (*vp->v_hashchain == vp) {
1772			*vp->v_hashchain = vp->v_specnext;
1773		} else {
1774			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1775				if (vq->v_specnext != vp)
1776					continue;
1777				vq->v_specnext = vp->v_specnext;
1778				break;
1779			}
1780			if (vq == NULL)
1781				panic("missing bdev");
1782		}
1783		if (vp->v_flag & VALIASED) {
1784			vx = NULL;
1785			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1786				if (vq->v_rdev != vp->v_rdev ||
1787				    vq->v_type != vp->v_type)
1788					continue;
1789				if (vx)
1790					break;
1791				vx = vq;
1792			}
1793			if (vx == NULL)
1794				panic("missing alias");
1795			if (vq == NULL)
1796				vx->v_flag &= ~VALIASED;
1797			vp->v_flag &= ~VALIASED;
1798		}
1799		simple_unlock(&spechash_slock);
1800		FREE(vp->v_specinfo, M_VNODE);
1801		vp->v_specinfo = NULL;
1802	}
1803
1804	/*
1805	 * If it is on the freelist and not already at the head,
1806	 * move it to the head of the list. The test of the back
1807	 * pointer and the reference count of zero is because
1808	 * it will be removed from the free list by getnewvnode,
1809	 * but will not have its reference count incremented until
1810	 * after calling vgone. If the reference count were
1811	 * incremented first, vgone would (incorrectly) try to
1812	 * close the previous instance of the underlying object.
1813	 */
1814	if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
1815		s = splbio();
1816		simple_lock(&vnode_free_list_slock);
1817		if (vp->v_flag & VFREE) {
1818			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1819		} else if (vp->v_flag & VTBFREE) {
1820			TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
1821			vp->v_flag &= ~VTBFREE;
1822			freevnodes++;
1823		} else
1824			freevnodes++;
1825		vp->v_flag |= VFREE;
1826		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1827		simple_unlock(&vnode_free_list_slock);
1828		splx(s);
1829	}
1830
1831	vp->v_type = VBAD;
1832	simple_unlock(&vp->v_interlock);
1833}
1834
1835/*
1836 * Lookup a vnode by device number.
1837 */
1838int
1839vfinddev(dev, type, vpp)
1840	dev_t dev;
1841	enum vtype type;
1842	struct vnode **vpp;
1843{
1844	register struct vnode *vp;
1845	int rc = 0;
1846
1847	simple_lock(&spechash_slock);
1848	for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
1849		if (dev != vp->v_rdev || type != vp->v_type)
1850			continue;
1851		*vpp = vp;
1852		rc = 1;
1853		break;
1854	}
1855	simple_unlock(&spechash_slock);
1856	return (rc);
1857}
1858
1859/*
1860 * Calculate the total number of references to a special device.
1861 */
1862int
1863vcount(vp)
1864	register struct vnode *vp;
1865{
1866	struct vnode *vq, *vnext;
1867	int count;
1868
1869loop:
1870	if ((vp->v_flag & VALIASED) == 0)
1871		return (vp->v_usecount);
1872	simple_lock(&spechash_slock);
1873	for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) {
1874		vnext = vq->v_specnext;
1875		if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
1876			continue;
1877		/*
1878		 * Alias, but not in use, so flush it out.
1879		 */
1880		if (vq->v_usecount == 0 && vq != vp) {
1881			simple_unlock(&spechash_slock);
1882			vgone(vq);
1883			goto loop;
1884		}
1885		count += vq->v_usecount;
1886	}
1887	simple_unlock(&spechash_slock);
1888	return (count);
1889}
1890/*
1891 * Print out a description of a vnode.
1892 */
1893static char *typename[] =
1894{"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1895
1896void
1897vprint(label, vp)
1898	char *label;
1899	register struct vnode *vp;
1900{
1901	char buf[64];
1902
1903	if (label != NULL)
1904		printf("%s: %x: ", label, vp);
1905	else
1906		printf("%x: ", vp);
1907	printf("type %s, usecount %d, writecount %d, refcount %ld,",
1908	    typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1909	    vp->v_holdcnt);
1910	buf[0] = '\0';
1911	if (vp->v_flag & VROOT)
1912		strcat(buf, "|VROOT");
1913	if (vp->v_flag & VTEXT)
1914		strcat(buf, "|VTEXT");
1915	if (vp->v_flag & VSYSTEM)
1916		strcat(buf, "|VSYSTEM");
1917	if (vp->v_flag & VXLOCK)
1918		strcat(buf, "|VXLOCK");
1919	if (vp->v_flag & VXWANT)
1920		strcat(buf, "|VXWANT");
1921	if (vp->v_flag & VBWAIT)
1922		strcat(buf, "|VBWAIT");
1923	if (vp->v_flag & VALIASED)
1924		strcat(buf, "|VALIASED");
1925	if (vp->v_flag & VDOOMED)
1926		strcat(buf, "|VDOOMED");
1927	if (vp->v_flag & VFREE)
1928		strcat(buf, "|VFREE");
1929	if (vp->v_flag & VOBJBUF)
1930		strcat(buf, "|VOBJBUF");
1931	if (buf[0] != '\0')
1932		printf(" flags (%s)", &buf[1]);
1933	if (vp->v_data == NULL) {
1934		printf("\n");
1935	} else {
1936		printf("\n\t");
1937		VOP_PRINT(vp);
1938	}
1939}
1940
1941#ifdef DDB
1942/*
1943 * List all of the locked vnodes in the system.
1944 * Called when debugging the kernel.
1945 */
1946static void
1947printlockedvnodes()
1948{
1949	struct proc *p = curproc;	/* XXX */
1950	struct mount *mp, *nmp;
1951	struct vnode *vp;
1952
1953	printf("Locked vnodes\n");
1954	simple_lock(&mountlist_slock);
1955	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
1956		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
1957			nmp = mp->mnt_list.cqe_next;
1958			continue;
1959		}
1960		for (vp = mp->mnt_vnodelist.lh_first;
1961		     vp != NULL;
1962		     vp = vp->v_mntvnodes.le_next) {
1963			if (VOP_ISLOCKED(vp))
1964				vprint((char *)0, vp);
1965		}
1966		simple_lock(&mountlist_slock);
1967		nmp = mp->mnt_list.cqe_next;
1968		vfs_unbusy(mp, p);
1969	}
1970	simple_unlock(&mountlist_slock);
1971}
1972#endif
1973
1974/*
1975 * Top level filesystem related information gathering.
1976 */
1977static int	sysctl_ovfs_conf __P(SYSCTL_HANDLER_ARGS);
1978
1979static int
1980vfs_sysctl SYSCTL_HANDLER_ARGS
1981{
1982	int *name = (int *)arg1 - 1;	/* XXX */
1983	u_int namelen = arg2 + 1;	/* XXX */
1984	struct vfsconf *vfsp;
1985
1986#ifndef NO_COMPAT_PRELITE2
1987	/* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1988	if (namelen == 1)
1989		return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1990#endif
1991
1992#ifdef notyet
1993	/* all sysctl names at this level are at least name and field */
1994	if (namelen < 2)
1995		return (ENOTDIR);		/* overloaded */
1996	if (name[0] != VFS_GENERIC) {
1997		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1998			if (vfsp->vfc_typenum == name[0])
1999				break;
2000		if (vfsp == NULL)
2001			return (EOPNOTSUPP);
2002		return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
2003		    oldp, oldlenp, newp, newlen, p));
2004	}
2005#endif
2006	switch (name[1]) {
2007	case VFS_MAXTYPENUM:
2008		if (namelen != 2)
2009			return (ENOTDIR);
2010		return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
2011	case VFS_CONF:
2012		if (namelen != 3)
2013			return (ENOTDIR);	/* overloaded */
2014		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
2015			if (vfsp->vfc_typenum == name[2])
2016				break;
2017		if (vfsp == NULL)
2018			return (EOPNOTSUPP);
2019		return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
2020	}
2021	return (EOPNOTSUPP);
2022}
2023
2024SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
2025	"Generic filesystem");
2026
2027#ifndef NO_COMPAT_PRELITE2
2028
2029static int
2030sysctl_ovfs_conf SYSCTL_HANDLER_ARGS
2031{
2032	int error;
2033	struct vfsconf *vfsp;
2034	struct ovfsconf ovfs;
2035
2036	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
2037		ovfs.vfc_vfsops = vfsp->vfc_vfsops;	/* XXX used as flag */
2038		strcpy(ovfs.vfc_name, vfsp->vfc_name);
2039		ovfs.vfc_index = vfsp->vfc_typenum;
2040		ovfs.vfc_refcount = vfsp->vfc_refcount;
2041		ovfs.vfc_flags = vfsp->vfc_flags;
2042		error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
2043		if (error)
2044			return error;
2045	}
2046	return 0;
2047}
2048
2049#endif /* !NO_COMPAT_PRELITE2 */
2050
2051static volatile int kinfo_vdebug = 1;
2052
2053#if 0
2054#define KINFO_VNODESLOP	10
2055/*
2056 * Dump vnode list (via sysctl).
2057 * Copyout address of vnode followed by vnode.
2058 */
2059/* ARGSUSED */
2060static int
2061sysctl_vnode SYSCTL_HANDLER_ARGS
2062{
2063	struct proc *p = curproc;	/* XXX */
2064	struct mount *mp, *nmp;
2065	struct vnode *nvp, *vp;
2066	int error;
2067
2068#define VPTRSZ	sizeof (struct vnode *)
2069#define VNODESZ	sizeof (struct vnode)
2070
2071	req->lock = 0;
2072	if (!req->oldptr) /* Make an estimate */
2073		return (SYSCTL_OUT(req, 0,
2074			(numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
2075
2076	simple_lock(&mountlist_slock);
2077	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
2078		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
2079			nmp = mp->mnt_list.cqe_next;
2080			continue;
2081		}
2082again:
2083		simple_lock(&mntvnode_slock);
2084		for (vp = mp->mnt_vnodelist.lh_first;
2085		     vp != NULL;
2086		     vp = nvp) {
2087			/*
2088			 * Check that the vp is still associated with
2089			 * this filesystem.  RACE: could have been
2090			 * recycled onto the same filesystem.
2091			 */
2092			if (vp->v_mount != mp) {
2093				simple_unlock(&mntvnode_slock);
2094				if (kinfo_vdebug)
2095					printf("kinfo: vp changed\n");
2096				goto again;
2097			}
2098			nvp = vp->v_mntvnodes.le_next;
2099			simple_unlock(&mntvnode_slock);
2100			if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
2101			    (error = SYSCTL_OUT(req, vp, VNODESZ)))
2102				return (error);
2103			simple_lock(&mntvnode_slock);
2104		}
2105		simple_unlock(&mntvnode_slock);
2106		simple_lock(&mountlist_slock);
2107		nmp = mp->mnt_list.cqe_next;
2108		vfs_unbusy(mp, p);
2109	}
2110	simple_unlock(&mountlist_slock);
2111
2112	return (0);
2113}
2114#endif
2115
2116/*
2117 * XXX
2118 * Exporting the vnode list on large systems causes them to crash.
2119 * Exporting the vnode list on medium systems causes sysctl to coredump.
2120 */
2121#if 0
2122SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
2123	0, 0, sysctl_vnode, "S,vnode", "");
2124#endif
2125
2126/*
2127 * Check to see if a filesystem is mounted on a block device.
2128 */
2129int
2130vfs_mountedon(vp)
2131	struct vnode *vp;
2132{
2133	struct vnode *vq;
2134	int error = 0;
2135
2136	if (vp->v_specmountpoint != NULL)
2137		return (EBUSY);
2138	if (vp->v_flag & VALIASED) {
2139		simple_lock(&spechash_slock);
2140		for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
2141			if (vq->v_rdev != vp->v_rdev ||
2142			    vq->v_type != vp->v_type)
2143				continue;
2144			if (vq->v_specmountpoint != NULL) {
2145				error = EBUSY;
2146				break;
2147			}
2148		}
2149		simple_unlock(&spechash_slock);
2150	}
2151	return (error);
2152}
2153
2154/*
2155 * Unmount all filesystems. The list is traversed in reverse order
2156 * of mounting to avoid dependencies.
2157 */
2158void
2159vfs_unmountall()
2160{
2161	struct mount *mp, *nmp;
2162	struct proc *p;
2163	int error;
2164
2165	if (curproc != NULL)
2166		p = curproc;
2167	else
2168		p = initproc;	/* XXX XXX should this be proc0? */
2169	/*
2170	 * Since this only runs when rebooting, it is not interlocked.
2171	 */
2172	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
2173		nmp = mp->mnt_list.cqe_prev;
2174		error = dounmount(mp, MNT_FORCE, p);
2175		if (error) {
2176			printf("unmount of %s failed (",
2177			    mp->mnt_stat.f_mntonname);
2178			if (error == EBUSY)
2179				printf("BUSY)\n");
2180			else
2181				printf("%d)\n", error);
2182		}
2183	}
2184}
2185
2186/*
2187 * Build hash lists of net addresses and hang them off the mount point.
2188 * Called by ufs_mount() to set up the lists of export addresses.
2189 */
2190static int
2191vfs_hang_addrlist(mp, nep, argp)
2192	struct mount *mp;
2193	struct netexport *nep;
2194	struct export_args *argp;
2195{
2196	register struct netcred *np;
2197	register struct radix_node_head *rnh;
2198	register int i;
2199	struct radix_node *rn;
2200	struct sockaddr *saddr, *smask = 0;
2201	struct domain *dom;
2202	int error;
2203
2204	if (argp->ex_addrlen == 0) {
2205		if (mp->mnt_flag & MNT_DEFEXPORTED)
2206			return (EPERM);
2207		np = &nep->ne_defexported;
2208		np->netc_exflags = argp->ex_flags;
2209		np->netc_anon = argp->ex_anon;
2210		np->netc_anon.cr_ref = 1;
2211		mp->mnt_flag |= MNT_DEFEXPORTED;
2212		return (0);
2213	}
2214	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2215	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
2216	bzero((caddr_t) np, i);
2217	saddr = (struct sockaddr *) (np + 1);
2218	if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2219		goto out;
2220	if (saddr->sa_len > argp->ex_addrlen)
2221		saddr->sa_len = argp->ex_addrlen;
2222	if (argp->ex_masklen) {
2223		smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
2224		error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
2225		if (error)
2226			goto out;
2227		if (smask->sa_len > argp->ex_masklen)
2228			smask->sa_len = argp->ex_masklen;
2229	}
2230	i = saddr->sa_family;
2231	if ((rnh = nep->ne_rtable[i]) == 0) {
2232		/*
2233		 * Seems silly to initialize every AF when most are not used,
2234		 * do so on demand here
2235		 */
2236		for (dom = domains; dom; dom = dom->dom_next)
2237			if (dom->dom_family == i && dom->dom_rtattach) {
2238				dom->dom_rtattach((void **) &nep->ne_rtable[i],
2239				    dom->dom_rtoffset);
2240				break;
2241			}
2242		if ((rnh = nep->ne_rtable[i]) == 0) {
2243			error = ENOBUFS;
2244			goto out;
2245		}
2246	}
2247	rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
2248	    np->netc_rnodes);
2249	if (rn == 0 || np != (struct netcred *) rn) {	/* already exists */
2250		error = EPERM;
2251		goto out;
2252	}
2253	np->netc_exflags = argp->ex_flags;
2254	np->netc_anon = argp->ex_anon;
2255	np->netc_anon.cr_ref = 1;
2256	return (0);
2257out:
2258	free(np, M_NETADDR);
2259	return (error);
2260}
2261
2262/* ARGSUSED */
2263static int
2264vfs_free_netcred(rn, w)
2265	struct radix_node *rn;
2266	void *w;
2267{
2268	register struct radix_node_head *rnh = (struct radix_node_head *) w;
2269
2270	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2271	free((caddr_t) rn, M_NETADDR);
2272	return (0);
2273}
2274
2275/*
2276 * Free the net address hash lists that are hanging off the mount points.
2277 */
2278static void
2279vfs_free_addrlist(nep)
2280	struct netexport *nep;
2281{
2282	register int i;
2283	register struct radix_node_head *rnh;
2284
2285	for (i = 0; i <= AF_MAX; i++)
2286		if ((rnh = nep->ne_rtable[i])) {
2287			(*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2288			    (caddr_t) rnh);
2289			free((caddr_t) rnh, M_RTABLE);
2290			nep->ne_rtable[i] = 0;
2291		}
2292}
2293
2294int
2295vfs_export(mp, nep, argp)
2296	struct mount *mp;
2297	struct netexport *nep;
2298	struct export_args *argp;
2299{
2300	int error;
2301
2302	if (argp->ex_flags & MNT_DELEXPORT) {
2303		if (mp->mnt_flag & MNT_EXPUBLIC) {
2304			vfs_setpublicfs(NULL, NULL, NULL);
2305			mp->mnt_flag &= ~MNT_EXPUBLIC;
2306		}
2307		vfs_free_addrlist(nep);
2308		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2309	}
2310	if (argp->ex_flags & MNT_EXPORTED) {
2311		if (argp->ex_flags & MNT_EXPUBLIC) {
2312			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2313				return (error);
2314			mp->mnt_flag |= MNT_EXPUBLIC;
2315		}
2316		if ((error = vfs_hang_addrlist(mp, nep, argp)))
2317			return (error);
2318		mp->mnt_flag |= MNT_EXPORTED;
2319	}
2320	return (0);
2321}
2322
2323
2324/*
2325 * Set the publicly exported filesystem (WebNFS). Currently, only
2326 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2327 */
2328int
2329vfs_setpublicfs(mp, nep, argp)
2330	struct mount *mp;
2331	struct netexport *nep;
2332	struct export_args *argp;
2333{
2334	int error;
2335	struct vnode *rvp;
2336	char *cp;
2337
2338	/*
2339	 * mp == NULL -> invalidate the current info, the FS is
2340	 * no longer exported. May be called from either vfs_export
2341	 * or unmount, so check if it hasn't already been done.
2342	 */
2343	if (mp == NULL) {
2344		if (nfs_pub.np_valid) {
2345			nfs_pub.np_valid = 0;
2346			if (nfs_pub.np_index != NULL) {
2347				FREE(nfs_pub.np_index, M_TEMP);
2348				nfs_pub.np_index = NULL;
2349			}
2350		}
2351		return (0);
2352	}
2353
2354	/*
2355	 * Only one allowed at a time.
2356	 */
2357	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2358		return (EBUSY);
2359
2360	/*
2361	 * Get real filehandle for root of exported FS.
2362	 */
2363	bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2364	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2365
2366	if ((error = VFS_ROOT(mp, &rvp)))
2367		return (error);
2368
2369	if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2370		return (error);
2371
2372	vput(rvp);
2373
2374	/*
2375	 * If an indexfile was specified, pull it in.
2376	 */
2377	if (argp->ex_indexfile != NULL) {
2378		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2379		    M_WAITOK);
2380		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2381		    MAXNAMLEN, (size_t *)0);
2382		if (!error) {
2383			/*
2384			 * Check for illegal filenames.
2385			 */
2386			for (cp = nfs_pub.np_index; *cp; cp++) {
2387				if (*cp == '/') {
2388					error = EINVAL;
2389					break;
2390				}
2391			}
2392		}
2393		if (error) {
2394			FREE(nfs_pub.np_index, M_TEMP);
2395			return (error);
2396		}
2397	}
2398
2399	nfs_pub.np_mount = mp;
2400	nfs_pub.np_valid = 1;
2401	return (0);
2402}
2403
2404struct netcred *
2405vfs_export_lookup(mp, nep, nam)
2406	register struct mount *mp;
2407	struct netexport *nep;
2408	struct sockaddr *nam;
2409{
2410	register struct netcred *np;
2411	register struct radix_node_head *rnh;
2412	struct sockaddr *saddr;
2413
2414	np = NULL;
2415	if (mp->mnt_flag & MNT_EXPORTED) {
2416		/*
2417		 * Lookup in the export list first.
2418		 */
2419		if (nam != NULL) {
2420			saddr = nam;
2421			rnh = nep->ne_rtable[saddr->sa_family];
2422			if (rnh != NULL) {
2423				np = (struct netcred *)
2424					(*rnh->rnh_matchaddr)((caddr_t)saddr,
2425							      rnh);
2426				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2427					np = NULL;
2428			}
2429		}
2430		/*
2431		 * If no address match, use the default if it exists.
2432		 */
2433		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2434			np = &nep->ne_defexported;
2435	}
2436	return (np);
2437}
2438
2439/*
2440 * perform msync on all vnodes under a mount point
2441 * the mount point must be locked.
2442 */
2443void
2444vfs_msync(struct mount *mp, int flags) {
2445	struct vnode *vp, *nvp;
2446	struct vm_object *obj;
2447	int anyio, tries;
2448
2449	tries = 5;
2450loop:
2451	anyio = 0;
2452	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
2453
2454		nvp = vp->v_mntvnodes.le_next;
2455
2456		if (vp->v_mount != mp) {
2457			goto loop;
2458		}
2459
2460		if (vp->v_flag & VXLOCK)	/* XXX: what if MNT_WAIT? */
2461			continue;
2462
2463		if (flags != MNT_WAIT) {
2464			obj = vp->v_object;
2465			if (obj == NULL || (obj->flags & OBJ_MIGHTBEDIRTY) == 0)
2466				continue;
2467			if (VOP_ISLOCKED(vp))
2468				continue;
2469		}
2470
2471		simple_lock(&vp->v_interlock);
2472		if (vp->v_object &&
2473		   (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
2474			if (!vget(vp,
2475				LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
2476				if (vp->v_object) {
2477					vm_object_page_clean(vp->v_object, 0, 0, TRUE);
2478					anyio = 1;
2479				}
2480				vput(vp);
2481			}
2482		} else {
2483			simple_unlock(&vp->v_interlock);
2484		}
2485	}
2486	if (anyio && (--tries > 0))
2487		goto loop;
2488}
2489
2490/*
2491 * Create the VM object needed for VMIO and mmap support.  This
2492 * is done for all VREG files in the system.  Some filesystems might
2493 * afford the additional metadata buffering capability of the
2494 * VMIO code by making the device node be VMIO mode also.
2495 *
2496 * If !waslocked, must be called with interlock.
2497 */
2498int
2499vfs_object_create(vp, p, cred, waslocked)
2500	struct vnode *vp;
2501	struct proc *p;
2502	struct ucred *cred;
2503	int waslocked;
2504{
2505	struct vattr vat;
2506	vm_object_t object;
2507	int error = 0;
2508
2509	if ((vp->v_type != VREG) && (vp->v_type != VBLK)) {
2510		if (!waslocked)
2511			simple_unlock(&vp->v_interlock);
2512		return 0;
2513	}
2514
2515	if (!waslocked)
2516		vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY, p);
2517
2518retry:
2519	if ((object = vp->v_object) == NULL) {
2520		if (vp->v_type == VREG) {
2521			if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
2522				goto retn;
2523			object = vnode_pager_alloc(vp,
2524				OFF_TO_IDX(round_page(vat.va_size)), 0, 0);
2525		} else if (major(vp->v_rdev) < nblkdev) {
2526			/*
2527			 * This simply allocates the biggest object possible
2528			 * for a VBLK vnode.  This should be fixed, but doesn't
2529			 * cause any problems (yet).
2530			 */
2531			object = vnode_pager_alloc(vp, INT_MAX, 0, 0);
2532		}
2533		object->ref_count--;
2534		vp->v_usecount--;
2535	} else {
2536		if (object->flags & OBJ_DEAD) {
2537			VOP_UNLOCK(vp, 0, p);
2538			tsleep(object, PVM, "vodead", 0);
2539			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2540			goto retry;
2541		}
2542	}
2543
2544	if (vp->v_object) {
2545		vp->v_flag |= VOBJBUF;
2546	}
2547
2548retn:
2549	if (!waslocked) {
2550		simple_lock(&vp->v_interlock);
2551		VOP_UNLOCK(vp, LK_INTERLOCK, p);
2552	}
2553
2554	return error;
2555}
2556
2557static void
2558vfree(vp)
2559	struct vnode *vp;
2560{
2561	int s;
2562
2563	s = splbio();
2564	simple_lock(&vnode_free_list_slock);
2565	if (vp->v_flag & VTBFREE) {
2566		TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2567		vp->v_flag &= ~VTBFREE;
2568	}
2569	if (vp->v_flag & VAGE) {
2570		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2571	} else {
2572		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2573	}
2574	freevnodes++;
2575	simple_unlock(&vnode_free_list_slock);
2576	vp->v_flag &= ~VAGE;
2577	vp->v_flag |= VFREE;
2578	splx(s);
2579}
2580
2581void
2582vbusy(vp)
2583	struct vnode *vp;
2584{
2585	int s;
2586
2587	s = splbio();
2588	simple_lock(&vnode_free_list_slock);
2589	if (vp->v_flag & VTBFREE) {
2590		TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2591		vp->v_flag &= ~VTBFREE;
2592	} else {
2593		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2594		freevnodes--;
2595	}
2596	simple_unlock(&vnode_free_list_slock);
2597	vp->v_flag &= ~(VFREE|VAGE);
2598	splx(s);
2599}
2600
2601/*
2602 * Record a process's interest in events which might happen to
2603 * a vnode.  Because poll uses the historic select-style interface
2604 * internally, this routine serves as both the ``check for any
2605 * pending events'' and the ``record my interest in future events''
2606 * functions.  (These are done together, while the lock is held,
2607 * to avoid race conditions.)
2608 */
2609int
2610vn_pollrecord(vp, p, events)
2611	struct vnode *vp;
2612	struct proc *p;
2613	short events;
2614{
2615	simple_lock(&vp->v_pollinfo.vpi_lock);
2616	if (vp->v_pollinfo.vpi_revents & events) {
2617		/*
2618		 * This leaves events we are not interested
2619		 * in available for the other process which
2620		 * which presumably had requested them
2621		 * (otherwise they would never have been
2622		 * recorded).
2623		 */
2624		events &= vp->v_pollinfo.vpi_revents;
2625		vp->v_pollinfo.vpi_revents &= ~events;
2626
2627		simple_unlock(&vp->v_pollinfo.vpi_lock);
2628		return events;
2629	}
2630	vp->v_pollinfo.vpi_events |= events;
2631	selrecord(p, &vp->v_pollinfo.vpi_selinfo);
2632	simple_unlock(&vp->v_pollinfo.vpi_lock);
2633	return 0;
2634}
2635
2636/*
2637 * Note the occurrence of an event.  If the VN_POLLEVENT macro is used,
2638 * it is possible for us to miss an event due to race conditions, but
2639 * that condition is expected to be rare, so for the moment it is the
2640 * preferred interface.
2641 */
2642void
2643vn_pollevent(vp, events)
2644	struct vnode *vp;
2645	short events;
2646{
2647	simple_lock(&vp->v_pollinfo.vpi_lock);
2648	if (vp->v_pollinfo.vpi_events & events) {
2649		/*
2650		 * We clear vpi_events so that we don't
2651		 * call selwakeup() twice if two events are
2652		 * posted before the polling process(es) is
2653		 * awakened.  This also ensures that we take at
2654		 * most one selwakeup() if the polling process
2655		 * is no longer interested.  However, it does
2656		 * mean that only one event can be noticed at
2657		 * a time.  (Perhaps we should only clear those
2658		 * event bits which we note?) XXX
2659		 */
2660		vp->v_pollinfo.vpi_events = 0;	/* &= ~events ??? */
2661		vp->v_pollinfo.vpi_revents |= events;
2662		selwakeup(&vp->v_pollinfo.vpi_selinfo);
2663	}
2664	simple_unlock(&vp->v_pollinfo.vpi_lock);
2665}
2666
2667/*
2668 * Wake up anyone polling on vp because it is being revoked.
2669 * This depends on dead_poll() returning POLLHUP for correct
2670 * behavior.
2671 */
2672void
2673vn_pollgone(vp)
2674	struct vnode *vp;
2675{
2676	simple_lock(&vp->v_pollinfo.vpi_lock);
2677	if (vp->v_pollinfo.vpi_events) {
2678		vp->v_pollinfo.vpi_events = 0;
2679		selwakeup(&vp->v_pollinfo.vpi_selinfo);
2680	}
2681	simple_unlock(&vp->v_pollinfo.vpi_lock);
2682}
2683
2684
2685
2686/*
2687 * Routine to create and manage a filesystem syncer vnode.
2688 */
2689#define sync_close ((int (*) __P((struct  vop_close_args *)))nullop)
2690int	sync_fsync __P((struct  vop_fsync_args *));
2691int	sync_inactive __P((struct  vop_inactive_args *));
2692int	sync_reclaim  __P((struct  vop_reclaim_args *));
2693#define sync_lock ((int (*) __P((struct  vop_lock_args *)))vop_nolock)
2694#define sync_unlock ((int (*) __P((struct  vop_unlock_args *)))vop_nounlock)
2695int	sync_print __P((struct vop_print_args *));
2696#define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked)
2697
2698vop_t **sync_vnodeop_p;
2699struct vnodeopv_entry_desc sync_vnodeop_entries[] = {
2700	{ &vop_default_desc,	(vop_t *) vop_eopnotsupp },
2701	{ &vop_close_desc,	(vop_t *) sync_close },		/* close */
2702	{ &vop_fsync_desc,	(vop_t *) sync_fsync },		/* fsync */
2703	{ &vop_inactive_desc,	(vop_t *) sync_inactive },	/* inactive */
2704	{ &vop_reclaim_desc,	(vop_t *) sync_reclaim },	/* reclaim */
2705	{ &vop_lock_desc,	(vop_t *) sync_lock },		/* lock */
2706	{ &vop_unlock_desc,	(vop_t *) sync_unlock },	/* unlock */
2707	{ &vop_print_desc,	(vop_t *) sync_print },		/* print */
2708	{ &vop_islocked_desc,	(vop_t *) sync_islocked },	/* islocked */
2709	{ NULL, NULL }
2710};
2711struct vnodeopv_desc sync_vnodeop_opv_desc =
2712	{ &sync_vnodeop_p, sync_vnodeop_entries };
2713
2714VNODEOP_SET(sync_vnodeop_opv_desc);
2715
2716/*
2717 * Create a new filesystem syncer vnode for the specified mount point.
2718 */
2719int
2720vfs_allocate_syncvnode(mp)
2721	struct mount *mp;
2722{
2723	struct vnode *vp;
2724	static long start, incr, next;
2725	int error;
2726
2727	/* Allocate a new vnode */
2728	if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) {
2729		mp->mnt_syncer = NULL;
2730		return (error);
2731	}
2732	vp->v_type = VNON;
2733	/*
2734	 * Place the vnode onto the syncer worklist. We attempt to
2735	 * scatter them about on the list so that they will go off
2736	 * at evenly distributed times even if all the filesystems
2737	 * are mounted at once.
2738	 */
2739	next += incr;
2740	if (next == 0 || next > syncer_maxdelay) {
2741		start /= 2;
2742		incr /= 2;
2743		if (start == 0) {
2744			start = syncer_maxdelay / 2;
2745			incr = syncer_maxdelay;
2746		}
2747		next = start;
2748	}
2749	vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0);
2750	mp->mnt_syncer = vp;
2751	return (0);
2752}
2753
2754/*
2755 * Do a lazy sync of the filesystem.
2756 */
2757int
2758sync_fsync(ap)
2759	struct vop_fsync_args /* {
2760		struct vnode *a_vp;
2761		struct ucred *a_cred;
2762		int a_waitfor;
2763		struct proc *a_p;
2764	} */ *ap;
2765{
2766	struct vnode *syncvp = ap->a_vp;
2767	struct mount *mp = syncvp->v_mount;
2768	struct proc *p = ap->a_p;
2769	int asyncflag;
2770
2771	/*
2772	 * We only need to do something if this is a lazy evaluation.
2773	 */
2774	if (ap->a_waitfor != MNT_LAZY)
2775		return (0);
2776
2777	/*
2778	 * Move ourselves to the back of the sync list.
2779	 */
2780	vn_syncer_add_to_worklist(syncvp, syncdelay);
2781
2782	/*
2783	 * Walk the list of vnodes pushing all that are dirty and
2784	 * not already on the sync list.
2785	 */
2786	simple_lock(&mountlist_slock);
2787	if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_slock, p) != 0) {
2788		simple_unlock(&mountlist_slock);
2789		return (0);
2790	}
2791	asyncflag = mp->mnt_flag & MNT_ASYNC;
2792	mp->mnt_flag &= ~MNT_ASYNC;
2793	vfs_msync(mp, MNT_NOWAIT);
2794	VFS_SYNC(mp, MNT_LAZY, ap->a_cred, p);
2795	if (asyncflag)
2796		mp->mnt_flag |= MNT_ASYNC;
2797	vfs_unbusy(mp, p);
2798	return (0);
2799}
2800
2801/*
2802 * The syncer vnode is no referenced.
2803 */
2804int
2805sync_inactive(ap)
2806	struct vop_inactive_args /* {
2807		struct vnode *a_vp;
2808		struct proc *a_p;
2809	} */ *ap;
2810{
2811
2812	vgone(ap->a_vp);
2813	return (0);
2814}
2815
2816/*
2817 * The syncer vnode is no longer needed and is being decommissioned.
2818 */
2819int
2820sync_reclaim(ap)
2821	struct vop_reclaim_args /* {
2822		struct vnode *a_vp;
2823	} */ *ap;
2824{
2825	struct vnode *vp = ap->a_vp;
2826
2827	vp->v_mount->mnt_syncer = NULL;
2828	if (vp->v_flag & VONWORKLST) {
2829		LIST_REMOVE(vp, v_synclist);
2830		vp->v_flag &= ~VONWORKLST;
2831	}
2832
2833	return (0);
2834}
2835
2836/*
2837 * Print out a syncer vnode.
2838 */
2839int
2840sync_print(ap)
2841	struct vop_print_args /* {
2842		struct vnode *a_vp;
2843	} */ *ap;
2844{
2845	struct vnode *vp = ap->a_vp;
2846
2847	printf("syncer vnode");
2848	if (vp->v_vnlock != NULL)
2849		lockmgr_printinfo(vp->v_vnlock);
2850	printf("\n");
2851	return (0);
2852}
2853