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