vfs_export.c revision 33205
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.132 1998/02/09 06:09:35 eivind 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
106static TAILQ_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;
121static struct 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		/*
923		 * XXX
924		 * When the object is locked with shared lock, VOP_ISLOCKED()
925		 * returns true.
926		 */
927		if (VOP_ISLOCKED(vp)) {
928			simple_unlock(&vp->v_interlock);
929			vfs_object_create(vp, curproc, curproc->p_ucred, 1);
930		} else
931			vfs_object_create(vp, curproc, curproc->p_ucred, 0);
932		simple_lock(&vp->v_interlock);
933	}
934	if (flags & LK_TYPE_MASK) {
935		if (error = vn_lock(vp, flags | LK_INTERLOCK, p))
936			vrele(vp);
937		return (error);
938	}
939	simple_unlock(&vp->v_interlock);
940	return (0);
941}
942
943void
944vref(struct vnode *vp)
945{
946	simple_lock(&vp->v_interlock);
947	vp->v_usecount++;
948	simple_unlock(&vp->v_interlock);
949}
950
951/*
952 * Vnode put/release.
953 * If count drops to zero, call inactive routine and return to freelist.
954 */
955void
956vrele(vp)
957	struct vnode *vp;
958{
959	struct proc *p = curproc;	/* XXX */
960
961#ifdef DIAGNOSTIC
962	if (vp == NULL)
963		panic("vrele: null vp");
964#endif
965	simple_lock(&vp->v_interlock);
966
967	if (vp->v_usecount > 1) {
968
969		vp->v_usecount--;
970		simple_unlock(&vp->v_interlock);
971
972		return;
973	}
974
975	if (vp->v_usecount == 1) {
976
977		vp->v_usecount--;
978
979		if (VSHOULDFREE(vp))
980			vfree(vp);
981	/*
982	 * If we are doing a vput, the node is already locked, and we must
983	 * call VOP_INACTIVE with the node locked.  So, in the case of
984	 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
985	 */
986		if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) {
987			VOP_INACTIVE(vp, p);
988		}
989
990	} else {
991#ifdef DIAGNOSTIC
992		vprint("vrele: negative ref count", vp);
993		simple_unlock(&vp->v_interlock);
994#endif
995		panic("vrele: negative ref cnt");
996	}
997}
998
999void
1000vput(vp)
1001	struct vnode *vp;
1002{
1003	struct proc *p = curproc;	/* XXX */
1004
1005#ifdef DIAGNOSTIC
1006	if (vp == NULL)
1007		panic("vput: null vp");
1008#endif
1009
1010	simple_lock(&vp->v_interlock);
1011
1012	if (vp->v_usecount > 1) {
1013
1014		vp->v_usecount--;
1015		VOP_UNLOCK(vp, LK_INTERLOCK, p);
1016		return;
1017
1018	}
1019
1020	if (vp->v_usecount == 1) {
1021
1022		vp->v_usecount--;
1023		if (VSHOULDFREE(vp))
1024			vfree(vp);
1025	/*
1026	 * If we are doing a vput, the node is already locked, and we must
1027	 * call VOP_INACTIVE with the node locked.  So, in the case of
1028	 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE.
1029	 */
1030		simple_unlock(&vp->v_interlock);
1031		VOP_INACTIVE(vp, p);
1032
1033	} else {
1034#ifdef DIAGNOSTIC
1035		vprint("vput: negative ref count", vp);
1036#endif
1037		panic("vput: negative ref cnt");
1038	}
1039}
1040
1041/*
1042 * Somebody doesn't want the vnode recycled.
1043 */
1044void
1045vhold(vp)
1046	register struct vnode *vp;
1047{
1048
1049	simple_lock(&vp->v_interlock);
1050	vp->v_holdcnt++;
1051	if (VSHOULDBUSY(vp))
1052		vbusy(vp);
1053	simple_unlock(&vp->v_interlock);
1054}
1055
1056/*
1057 * One less who cares about this vnode.
1058 */
1059void
1060vdrop(vp)
1061	register struct vnode *vp;
1062{
1063
1064	simple_lock(&vp->v_interlock);
1065	if (vp->v_holdcnt <= 0)
1066		panic("holdrele: holdcnt");
1067	vp->v_holdcnt--;
1068	if (VSHOULDFREE(vp))
1069		vfree(vp);
1070	simple_unlock(&vp->v_interlock);
1071}
1072
1073/*
1074 * Remove any vnodes in the vnode table belonging to mount point mp.
1075 *
1076 * If MNT_NOFORCE is specified, there should not be any active ones,
1077 * return error if any are found (nb: this is a user error, not a
1078 * system error). If MNT_FORCE is specified, detach any active vnodes
1079 * that are found.
1080 */
1081#ifdef DIAGNOSTIC
1082static int busyprt = 0;		/* print out busy vnodes */
1083SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, "");
1084#endif
1085
1086int
1087vflush(mp, skipvp, flags)
1088	struct mount *mp;
1089	struct vnode *skipvp;
1090	int flags;
1091{
1092	struct proc *p = curproc;	/* XXX */
1093	struct vnode *vp, *nvp;
1094	int busy = 0;
1095
1096	simple_lock(&mntvnode_slock);
1097loop:
1098	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
1099		/*
1100		 * Make sure this vnode wasn't reclaimed in getnewvnode().
1101		 * Start over if it has (it won't be on the list anymore).
1102		 */
1103		if (vp->v_mount != mp)
1104			goto loop;
1105		nvp = vp->v_mntvnodes.le_next;
1106		/*
1107		 * Skip over a selected vnode.
1108		 */
1109		if (vp == skipvp)
1110			continue;
1111
1112		simple_lock(&vp->v_interlock);
1113		/*
1114		 * Skip over a vnodes marked VSYSTEM.
1115		 */
1116		if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) {
1117			simple_unlock(&vp->v_interlock);
1118			continue;
1119		}
1120		/*
1121		 * If WRITECLOSE is set, only flush out regular file vnodes
1122		 * open for writing.
1123		 */
1124		if ((flags & WRITECLOSE) &&
1125		    (vp->v_writecount == 0 || vp->v_type != VREG)) {
1126			simple_unlock(&vp->v_interlock);
1127			continue;
1128		}
1129
1130		/*
1131		 * With v_usecount == 0, all we need to do is clear out the
1132		 * vnode data structures and we are done.
1133		 */
1134		if (vp->v_usecount == 0) {
1135			simple_unlock(&mntvnode_slock);
1136			vgonel(vp, p);
1137			simple_lock(&mntvnode_slock);
1138			continue;
1139		}
1140
1141		/*
1142		 * If FORCECLOSE is set, forcibly close the vnode. For block
1143		 * or character devices, revert to an anonymous device. For
1144		 * all other files, just kill them.
1145		 */
1146		if (flags & FORCECLOSE) {
1147			simple_unlock(&mntvnode_slock);
1148			if (vp->v_type != VBLK && vp->v_type != VCHR) {
1149				vgonel(vp, p);
1150			} else {
1151				vclean(vp, 0, p);
1152				vp->v_op = spec_vnodeop_p;
1153				insmntque(vp, (struct mount *) 0);
1154			}
1155			simple_lock(&mntvnode_slock);
1156			continue;
1157		}
1158#ifdef DIAGNOSTIC
1159		if (busyprt)
1160			vprint("vflush: busy vnode", vp);
1161#endif
1162		simple_unlock(&vp->v_interlock);
1163		busy++;
1164	}
1165	simple_unlock(&mntvnode_slock);
1166	if (busy)
1167		return (EBUSY);
1168	return (0);
1169}
1170
1171/*
1172 * Disassociate the underlying file system from a vnode.
1173 */
1174static void
1175vclean(vp, flags, p)
1176	struct vnode *vp;
1177	int flags;
1178	struct proc *p;
1179{
1180	int active;
1181	vm_object_t obj;
1182
1183	/*
1184	 * Check to see if the vnode is in use. If so we have to reference it
1185	 * before we clean it out so that its count cannot fall to zero and
1186	 * generate a race against ourselves to recycle it.
1187	 */
1188	if ((active = vp->v_usecount))
1189		vp->v_usecount++;
1190
1191	/*
1192	 * Prevent the vnode from being recycled or brought into use while we
1193	 * clean it out.
1194	 */
1195	if (vp->v_flag & VXLOCK)
1196		panic("vclean: deadlock");
1197	vp->v_flag |= VXLOCK;
1198	/*
1199	 * Even if the count is zero, the VOP_INACTIVE routine may still
1200	 * have the object locked while it cleans it out. The VOP_LOCK
1201	 * ensures that the VOP_INACTIVE routine is done with its work.
1202	 * For active vnodes, it ensures that no other activity can
1203	 * occur while the underlying object is being cleaned out.
1204	 */
1205	VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p);
1206
1207	/*
1208	 * Clean out any buffers associated with the vnode.
1209	 */
1210	vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0);
1211	if (obj = vp->v_object) {
1212		if (obj->ref_count == 0) {
1213			/*
1214			 * This is a normal way of shutting down the object/vnode
1215			 * association.
1216			 */
1217			vm_object_terminate(obj);
1218		} else {
1219			/*
1220			 * Woe to the process that tries to page now :-).
1221			 */
1222			vm_pager_deallocate(obj);
1223		}
1224	}
1225
1226	/*
1227	 * If purging an active vnode, it must be closed and
1228	 * deactivated before being reclaimed. Note that the
1229	 * VOP_INACTIVE will unlock the vnode.
1230	 */
1231	if (active) {
1232		if (flags & DOCLOSE)
1233			VOP_CLOSE(vp, IO_NDELAY, NOCRED, p);
1234		VOP_INACTIVE(vp, p);
1235	} else {
1236		/*
1237		 * Any other processes trying to obtain this lock must first
1238		 * wait for VXLOCK to clear, then call the new lock operation.
1239		 */
1240		VOP_UNLOCK(vp, 0, p);
1241	}
1242	/*
1243	 * Reclaim the vnode.
1244	 */
1245	if (VOP_RECLAIM(vp, p))
1246		panic("vclean: cannot reclaim");
1247	if (active)
1248		vrele(vp);
1249	cache_purge(vp);
1250	if (vp->v_vnlock) {
1251#if 0 /* This is the only place we have LK_DRAINED in the entire kernel ??? */
1252#ifdef DIAGNOSTIC
1253		if ((vp->v_vnlock->lk_flags & LK_DRAINED) == 0)
1254			vprint("vclean: lock not drained", vp);
1255#endif
1256#endif
1257		FREE(vp->v_vnlock, M_VNODE);
1258		vp->v_vnlock = NULL;
1259	}
1260
1261	/*
1262	 * Done with purge, notify sleepers of the grim news.
1263	 */
1264	vp->v_op = dead_vnodeop_p;
1265	vn_pollgone(vp);
1266	vp->v_tag = VT_NON;
1267	vp->v_flag &= ~VXLOCK;
1268	if (vp->v_flag & VXWANT) {
1269		vp->v_flag &= ~VXWANT;
1270		wakeup((caddr_t) vp);
1271	}
1272}
1273
1274/*
1275 * Eliminate all activity associated with the requested vnode
1276 * and with all vnodes aliased to the requested vnode.
1277 */
1278int
1279vop_revoke(ap)
1280	struct vop_revoke_args /* {
1281		struct vnode *a_vp;
1282		int a_flags;
1283	} */ *ap;
1284{
1285	struct vnode *vp, *vq;
1286	struct proc *p = curproc;	/* XXX */
1287
1288#ifdef DIAGNOSTIC
1289	if ((ap->a_flags & REVOKEALL) == 0)
1290		panic("vop_revoke");
1291#endif
1292
1293	vp = ap->a_vp;
1294	simple_lock(&vp->v_interlock);
1295
1296	if (vp->v_flag & VALIASED) {
1297		/*
1298		 * If a vgone (or vclean) is already in progress,
1299		 * wait until it is done and return.
1300		 */
1301		if (vp->v_flag & VXLOCK) {
1302			vp->v_flag |= VXWANT;
1303			simple_unlock(&vp->v_interlock);
1304			tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0);
1305			return (0);
1306		}
1307		/*
1308		 * Ensure that vp will not be vgone'd while we
1309		 * are eliminating its aliases.
1310		 */
1311		vp->v_flag |= VXLOCK;
1312		simple_unlock(&vp->v_interlock);
1313		while (vp->v_flag & VALIASED) {
1314			simple_lock(&spechash_slock);
1315			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1316				if (vq->v_rdev != vp->v_rdev ||
1317				    vq->v_type != vp->v_type || vp == vq)
1318					continue;
1319				simple_unlock(&spechash_slock);
1320				vgone(vq);
1321				break;
1322			}
1323			if (vq == NULLVP) {
1324				simple_unlock(&spechash_slock);
1325			}
1326		}
1327		/*
1328		 * Remove the lock so that vgone below will
1329		 * really eliminate the vnode after which time
1330		 * vgone will awaken any sleepers.
1331		 */
1332		simple_lock(&vp->v_interlock);
1333		vp->v_flag &= ~VXLOCK;
1334		if (vp->v_flag & VXWANT) {
1335			vp->v_flag &= ~VXWANT;
1336			wakeup(vp);
1337		}
1338	}
1339	vgonel(vp, p);
1340	return (0);
1341}
1342
1343/*
1344 * Recycle an unused vnode to the front of the free list.
1345 * Release the passed interlock if the vnode will be recycled.
1346 */
1347int
1348vrecycle(vp, inter_lkp, p)
1349	struct vnode *vp;
1350	struct simplelock *inter_lkp;
1351	struct proc *p;
1352{
1353
1354	simple_lock(&vp->v_interlock);
1355	if (vp->v_usecount == 0) {
1356		if (inter_lkp) {
1357			simple_unlock(inter_lkp);
1358		}
1359		vgonel(vp, p);
1360		return (1);
1361	}
1362	simple_unlock(&vp->v_interlock);
1363	return (0);
1364}
1365
1366/*
1367 * Eliminate all activity associated with a vnode
1368 * in preparation for reuse.
1369 */
1370void
1371vgone(vp)
1372	register struct vnode *vp;
1373{
1374	struct proc *p = curproc;	/* XXX */
1375
1376	simple_lock(&vp->v_interlock);
1377	vgonel(vp, p);
1378}
1379
1380/*
1381 * vgone, with the vp interlock held.
1382 */
1383static void
1384vgonel(vp, p)
1385	struct vnode *vp;
1386	struct proc *p;
1387{
1388	int s;
1389	struct vnode *vq;
1390	struct vnode *vx;
1391
1392	/*
1393	 * If a vgone (or vclean) is already in progress,
1394	 * wait until it is done and return.
1395	 */
1396	if (vp->v_flag & VXLOCK) {
1397		vp->v_flag |= VXWANT;
1398		simple_unlock(&vp->v_interlock);
1399		tsleep((caddr_t)vp, PINOD, "vgone", 0);
1400		return;
1401	}
1402
1403	/*
1404	 * Clean out the filesystem specific data.
1405	 */
1406	vclean(vp, DOCLOSE, p);
1407	simple_lock(&vp->v_interlock);
1408
1409	/*
1410	 * Delete from old mount point vnode list, if on one.
1411	 */
1412	if (vp->v_mount != NULL)
1413		insmntque(vp, (struct mount *)0);
1414	/*
1415	 * If special device, remove it from special device alias list
1416	 * if it is on one.
1417	 */
1418	if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_specinfo != 0) {
1419		simple_lock(&spechash_slock);
1420		if (*vp->v_hashchain == vp) {
1421			*vp->v_hashchain = vp->v_specnext;
1422		} else {
1423			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1424				if (vq->v_specnext != vp)
1425					continue;
1426				vq->v_specnext = vp->v_specnext;
1427				break;
1428			}
1429			if (vq == NULL)
1430				panic("missing bdev");
1431		}
1432		if (vp->v_flag & VALIASED) {
1433			vx = NULL;
1434			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1435				if (vq->v_rdev != vp->v_rdev ||
1436				    vq->v_type != vp->v_type)
1437					continue;
1438				if (vx)
1439					break;
1440				vx = vq;
1441			}
1442			if (vx == NULL)
1443				panic("missing alias");
1444			if (vq == NULL)
1445				vx->v_flag &= ~VALIASED;
1446			vp->v_flag &= ~VALIASED;
1447		}
1448		simple_unlock(&spechash_slock);
1449		FREE(vp->v_specinfo, M_VNODE);
1450		vp->v_specinfo = NULL;
1451	}
1452
1453	/*
1454	 * If it is on the freelist and not already at the head,
1455	 * move it to the head of the list. The test of the back
1456	 * pointer and the reference count of zero is because
1457	 * it will be removed from the free list by getnewvnode,
1458	 * but will not have its reference count incremented until
1459	 * after calling vgone. If the reference count were
1460	 * incremented first, vgone would (incorrectly) try to
1461	 * close the previous instance of the underlying object.
1462	 */
1463	if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) {
1464		s = splbio();
1465		simple_lock(&vnode_free_list_slock);
1466		if (vp->v_flag & VFREE) {
1467			TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
1468		} else if (vp->v_flag & VTBFREE) {
1469			TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
1470			vp->v_flag &= ~VTBFREE;
1471			freevnodes++;
1472		} else
1473			freevnodes++;
1474		vp->v_flag |= VFREE;
1475		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
1476		simple_unlock(&vnode_free_list_slock);
1477		splx(s);
1478	}
1479
1480	vp->v_type = VBAD;
1481	simple_unlock(&vp->v_interlock);
1482}
1483
1484/*
1485 * Lookup a vnode by device number.
1486 */
1487int
1488vfinddev(dev, type, vpp)
1489	dev_t dev;
1490	enum vtype type;
1491	struct vnode **vpp;
1492{
1493	register struct vnode *vp;
1494	int rc = 0;
1495
1496	simple_lock(&spechash_slock);
1497	for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
1498		if (dev != vp->v_rdev || type != vp->v_type)
1499			continue;
1500		*vpp = vp;
1501		rc = 1;
1502		break;
1503	}
1504	simple_unlock(&spechash_slock);
1505	return (rc);
1506}
1507
1508/*
1509 * Calculate the total number of references to a special device.
1510 */
1511int
1512vcount(vp)
1513	register struct vnode *vp;
1514{
1515	struct vnode *vq, *vnext;
1516	int count;
1517
1518loop:
1519	if ((vp->v_flag & VALIASED) == 0)
1520		return (vp->v_usecount);
1521	simple_lock(&spechash_slock);
1522	for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) {
1523		vnext = vq->v_specnext;
1524		if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
1525			continue;
1526		/*
1527		 * Alias, but not in use, so flush it out.
1528		 */
1529		if (vq->v_usecount == 0 && vq != vp) {
1530			simple_unlock(&spechash_slock);
1531			vgone(vq);
1532			goto loop;
1533		}
1534		count += vq->v_usecount;
1535	}
1536	simple_unlock(&spechash_slock);
1537	return (count);
1538}
1539/*
1540 * Print out a description of a vnode.
1541 */
1542static char *typename[] =
1543{"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1544
1545void
1546vprint(label, vp)
1547	char *label;
1548	register struct vnode *vp;
1549{
1550	char buf[64];
1551
1552	if (label != NULL)
1553		printf("%s: %x: ", label, vp);
1554	else
1555		printf("%x: ", vp);
1556	printf("type %s, usecount %d, writecount %d, refcount %ld,",
1557	    typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1558	    vp->v_holdcnt);
1559	buf[0] = '\0';
1560	if (vp->v_flag & VROOT)
1561		strcat(buf, "|VROOT");
1562	if (vp->v_flag & VTEXT)
1563		strcat(buf, "|VTEXT");
1564	if (vp->v_flag & VSYSTEM)
1565		strcat(buf, "|VSYSTEM");
1566	if (vp->v_flag & VXLOCK)
1567		strcat(buf, "|VXLOCK");
1568	if (vp->v_flag & VXWANT)
1569		strcat(buf, "|VXWANT");
1570	if (vp->v_flag & VBWAIT)
1571		strcat(buf, "|VBWAIT");
1572	if (vp->v_flag & VALIASED)
1573		strcat(buf, "|VALIASED");
1574	if (vp->v_flag & VDOOMED)
1575		strcat(buf, "|VDOOMED");
1576	if (vp->v_flag & VFREE)
1577		strcat(buf, "|VFREE");
1578	if (vp->v_flag & VOBJBUF)
1579		strcat(buf, "|VOBJBUF");
1580	if (buf[0] != '\0')
1581		printf(" flags (%s)", &buf[1]);
1582	if (vp->v_data == NULL) {
1583		printf("\n");
1584	} else {
1585		printf("\n\t");
1586		VOP_PRINT(vp);
1587	}
1588}
1589
1590#ifdef DDB
1591/*
1592 * List all of the locked vnodes in the system.
1593 * Called when debugging the kernel.
1594 */
1595static void
1596printlockedvnodes()
1597{
1598	struct proc *p = curproc;	/* XXX */
1599	struct mount *mp, *nmp;
1600	struct vnode *vp;
1601
1602	printf("Locked vnodes\n");
1603	simple_lock(&mountlist_slock);
1604	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
1605		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
1606			nmp = mp->mnt_list.cqe_next;
1607			continue;
1608		}
1609		for (vp = mp->mnt_vnodelist.lh_first;
1610		     vp != NULL;
1611		     vp = vp->v_mntvnodes.le_next) {
1612			if (VOP_ISLOCKED(vp))
1613				vprint((char *)0, vp);
1614		}
1615		simple_lock(&mountlist_slock);
1616		nmp = mp->mnt_list.cqe_next;
1617		vfs_unbusy(mp, p);
1618	}
1619	simple_unlock(&mountlist_slock);
1620}
1621#endif
1622
1623/*
1624 * Top level filesystem related information gathering.
1625 */
1626static int	sysctl_ovfs_conf __P(SYSCTL_HANDLER_ARGS);
1627
1628static int
1629vfs_sysctl SYSCTL_HANDLER_ARGS
1630{
1631	int *name = (int *)arg1 - 1;	/* XXX */
1632	u_int namelen = arg2 + 1;	/* XXX */
1633	struct vfsconf *vfsp;
1634
1635#ifndef NO_COMPAT_PRELITE2
1636	/* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1637	if (namelen == 1)
1638		return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1639#endif
1640
1641#ifdef notyet
1642	/* all sysctl names at this level are at least name and field */
1643	if (namelen < 2)
1644		return (ENOTDIR);		/* overloaded */
1645	if (name[0] != VFS_GENERIC) {
1646		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1647			if (vfsp->vfc_typenum == name[0])
1648				break;
1649		if (vfsp == NULL)
1650			return (EOPNOTSUPP);
1651		return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1652		    oldp, oldlenp, newp, newlen, p));
1653	}
1654#endif
1655	switch (name[1]) {
1656	case VFS_MAXTYPENUM:
1657		if (namelen != 2)
1658			return (ENOTDIR);
1659		return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
1660	case VFS_CONF:
1661		if (namelen != 3)
1662			return (ENOTDIR);	/* overloaded */
1663		for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1664			if (vfsp->vfc_typenum == name[2])
1665				break;
1666		if (vfsp == NULL)
1667			return (EOPNOTSUPP);
1668		return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1669	}
1670	return (EOPNOTSUPP);
1671}
1672
1673SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1674	"Generic filesystem");
1675
1676#ifndef NO_COMPAT_PRELITE2
1677
1678static int
1679sysctl_ovfs_conf SYSCTL_HANDLER_ARGS
1680{
1681	int error;
1682	struct vfsconf *vfsp;
1683	struct ovfsconf ovfs;
1684
1685	for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
1686		ovfs.vfc_vfsops = vfsp->vfc_vfsops;	/* XXX used as flag */
1687		strcpy(ovfs.vfc_name, vfsp->vfc_name);
1688		ovfs.vfc_index = vfsp->vfc_typenum;
1689		ovfs.vfc_refcount = vfsp->vfc_refcount;
1690		ovfs.vfc_flags = vfsp->vfc_flags;
1691		error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1692		if (error)
1693			return error;
1694	}
1695	return 0;
1696}
1697
1698#endif /* !NO_COMPAT_PRELITE2 */
1699
1700static volatile int kinfo_vdebug = 1;
1701
1702#if 0
1703#define KINFO_VNODESLOP	10
1704/*
1705 * Dump vnode list (via sysctl).
1706 * Copyout address of vnode followed by vnode.
1707 */
1708/* ARGSUSED */
1709static int
1710sysctl_vnode SYSCTL_HANDLER_ARGS
1711{
1712	struct proc *p = curproc;	/* XXX */
1713	struct mount *mp, *nmp;
1714	struct vnode *nvp, *vp;
1715	int error;
1716
1717#define VPTRSZ	sizeof (struct vnode *)
1718#define VNODESZ	sizeof (struct vnode)
1719
1720	req->lock = 0;
1721	if (!req->oldptr) /* Make an estimate */
1722		return (SYSCTL_OUT(req, 0,
1723			(numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ)));
1724
1725	simple_lock(&mountlist_slock);
1726	for (mp = mountlist.cqh_first; mp != (void *)&mountlist; mp = nmp) {
1727		if (vfs_busy(mp, LK_NOWAIT, &mountlist_slock, p)) {
1728			nmp = mp->mnt_list.cqe_next;
1729			continue;
1730		}
1731again:
1732		simple_lock(&mntvnode_slock);
1733		for (vp = mp->mnt_vnodelist.lh_first;
1734		     vp != NULL;
1735		     vp = nvp) {
1736			/*
1737			 * Check that the vp is still associated with
1738			 * this filesystem.  RACE: could have been
1739			 * recycled onto the same filesystem.
1740			 */
1741			if (vp->v_mount != mp) {
1742				simple_unlock(&mntvnode_slock);
1743				if (kinfo_vdebug)
1744					printf("kinfo: vp changed\n");
1745				goto again;
1746			}
1747			nvp = vp->v_mntvnodes.le_next;
1748			simple_unlock(&mntvnode_slock);
1749			if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) ||
1750			    (error = SYSCTL_OUT(req, vp, VNODESZ)))
1751				return (error);
1752			simple_lock(&mntvnode_slock);
1753		}
1754		simple_unlock(&mntvnode_slock);
1755		simple_lock(&mountlist_slock);
1756		nmp = mp->mnt_list.cqe_next;
1757		vfs_unbusy(mp, p);
1758	}
1759	simple_unlock(&mountlist_slock);
1760
1761	return (0);
1762}
1763#endif
1764
1765/*
1766 * XXX
1767 * Exporting the vnode list on large systems causes them to crash.
1768 * Exporting the vnode list on medium systems causes sysctl to coredump.
1769 */
1770#if 0
1771SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD,
1772	0, 0, sysctl_vnode, "S,vnode", "");
1773#endif
1774
1775/*
1776 * Check to see if a filesystem is mounted on a block device.
1777 */
1778int
1779vfs_mountedon(vp)
1780	struct vnode *vp;
1781{
1782	struct vnode *vq;
1783	int error = 0;
1784
1785	if (vp->v_specflags & SI_MOUNTEDON)
1786		return (EBUSY);
1787	if (vp->v_flag & VALIASED) {
1788		simple_lock(&spechash_slock);
1789		for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1790			if (vq->v_rdev != vp->v_rdev ||
1791			    vq->v_type != vp->v_type)
1792				continue;
1793			if (vq->v_specflags & SI_MOUNTEDON) {
1794				error = EBUSY;
1795				break;
1796			}
1797		}
1798		simple_unlock(&spechash_slock);
1799	}
1800	return (error);
1801}
1802
1803/*
1804 * Unmount all filesystems. The list is traversed in reverse order
1805 * of mounting to avoid dependencies.
1806 */
1807void
1808vfs_unmountall()
1809{
1810	struct mount *mp, *nmp;
1811	struct proc *p = initproc;	/* XXX XXX should this be proc0? */
1812	int error;
1813
1814	/*
1815	 * Since this only runs when rebooting, it is not interlocked.
1816	 */
1817	for (mp = mountlist.cqh_last; mp != (void *)&mountlist; mp = nmp) {
1818		nmp = mp->mnt_list.cqe_prev;
1819		error = dounmount(mp, MNT_FORCE, p);
1820		if (error) {
1821			printf("unmount of %s failed (",
1822			    mp->mnt_stat.f_mntonname);
1823			if (error == EBUSY)
1824				printf("BUSY)\n");
1825			else
1826				printf("%d)\n", error);
1827		}
1828	}
1829}
1830
1831/*
1832 * Build hash lists of net addresses and hang them off the mount point.
1833 * Called by ufs_mount() to set up the lists of export addresses.
1834 */
1835static int
1836vfs_hang_addrlist(mp, nep, argp)
1837	struct mount *mp;
1838	struct netexport *nep;
1839	struct export_args *argp;
1840{
1841	register struct netcred *np;
1842	register struct radix_node_head *rnh;
1843	register int i;
1844	struct radix_node *rn;
1845	struct sockaddr *saddr, *smask = 0;
1846	struct domain *dom;
1847	int error;
1848
1849	if (argp->ex_addrlen == 0) {
1850		if (mp->mnt_flag & MNT_DEFEXPORTED)
1851			return (EPERM);
1852		np = &nep->ne_defexported;
1853		np->netc_exflags = argp->ex_flags;
1854		np->netc_anon = argp->ex_anon;
1855		np->netc_anon.cr_ref = 1;
1856		mp->mnt_flag |= MNT_DEFEXPORTED;
1857		return (0);
1858	}
1859	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1860	np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
1861	bzero((caddr_t) np, i);
1862	saddr = (struct sockaddr *) (np + 1);
1863	if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1864		goto out;
1865	if (saddr->sa_len > argp->ex_addrlen)
1866		saddr->sa_len = argp->ex_addrlen;
1867	if (argp->ex_masklen) {
1868		smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen);
1869		error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen);
1870		if (error)
1871			goto out;
1872		if (smask->sa_len > argp->ex_masklen)
1873			smask->sa_len = argp->ex_masklen;
1874	}
1875	i = saddr->sa_family;
1876	if ((rnh = nep->ne_rtable[i]) == 0) {
1877		/*
1878		 * Seems silly to initialize every AF when most are not used,
1879		 * do so on demand here
1880		 */
1881		for (dom = domains; dom; dom = dom->dom_next)
1882			if (dom->dom_family == i && dom->dom_rtattach) {
1883				dom->dom_rtattach((void **) &nep->ne_rtable[i],
1884				    dom->dom_rtoffset);
1885				break;
1886			}
1887		if ((rnh = nep->ne_rtable[i]) == 0) {
1888			error = ENOBUFS;
1889			goto out;
1890		}
1891	}
1892	rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh,
1893	    np->netc_rnodes);
1894	if (rn == 0 || np != (struct netcred *) rn) {	/* already exists */
1895		error = EPERM;
1896		goto out;
1897	}
1898	np->netc_exflags = argp->ex_flags;
1899	np->netc_anon = argp->ex_anon;
1900	np->netc_anon.cr_ref = 1;
1901	return (0);
1902out:
1903	free(np, M_NETADDR);
1904	return (error);
1905}
1906
1907/* ARGSUSED */
1908static int
1909vfs_free_netcred(rn, w)
1910	struct radix_node *rn;
1911	void *w;
1912{
1913	register struct radix_node_head *rnh = (struct radix_node_head *) w;
1914
1915	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1916	free((caddr_t) rn, M_NETADDR);
1917	return (0);
1918}
1919
1920/*
1921 * Free the net address hash lists that are hanging off the mount points.
1922 */
1923static void
1924vfs_free_addrlist(nep)
1925	struct netexport *nep;
1926{
1927	register int i;
1928	register struct radix_node_head *rnh;
1929
1930	for (i = 0; i <= AF_MAX; i++)
1931		if ((rnh = nep->ne_rtable[i])) {
1932			(*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1933			    (caddr_t) rnh);
1934			free((caddr_t) rnh, M_RTABLE);
1935			nep->ne_rtable[i] = 0;
1936		}
1937}
1938
1939int
1940vfs_export(mp, nep, argp)
1941	struct mount *mp;
1942	struct netexport *nep;
1943	struct export_args *argp;
1944{
1945	int error;
1946
1947	if (argp->ex_flags & MNT_DELEXPORT) {
1948		if (mp->mnt_flag & MNT_EXPUBLIC) {
1949			vfs_setpublicfs(NULL, NULL, NULL);
1950			mp->mnt_flag &= ~MNT_EXPUBLIC;
1951		}
1952		vfs_free_addrlist(nep);
1953		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1954	}
1955	if (argp->ex_flags & MNT_EXPORTED) {
1956		if (argp->ex_flags & MNT_EXPUBLIC) {
1957			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1958				return (error);
1959			mp->mnt_flag |= MNT_EXPUBLIC;
1960		}
1961		if ((error = vfs_hang_addrlist(mp, nep, argp)))
1962			return (error);
1963		mp->mnt_flag |= MNT_EXPORTED;
1964	}
1965	return (0);
1966}
1967
1968
1969/*
1970 * Set the publicly exported filesystem (WebNFS). Currently, only
1971 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1972 */
1973int
1974vfs_setpublicfs(mp, nep, argp)
1975	struct mount *mp;
1976	struct netexport *nep;
1977	struct export_args *argp;
1978{
1979	int error;
1980	struct vnode *rvp;
1981	char *cp;
1982
1983	/*
1984	 * mp == NULL -> invalidate the current info, the FS is
1985	 * no longer exported. May be called from either vfs_export
1986	 * or unmount, so check if it hasn't already been done.
1987	 */
1988	if (mp == NULL) {
1989		if (nfs_pub.np_valid) {
1990			nfs_pub.np_valid = 0;
1991			if (nfs_pub.np_index != NULL) {
1992				FREE(nfs_pub.np_index, M_TEMP);
1993				nfs_pub.np_index = NULL;
1994			}
1995		}
1996		return (0);
1997	}
1998
1999	/*
2000	 * Only one allowed at a time.
2001	 */
2002	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2003		return (EBUSY);
2004
2005	/*
2006	 * Get real filehandle for root of exported FS.
2007	 */
2008	bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2009	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2010
2011	if ((error = VFS_ROOT(mp, &rvp)))
2012		return (error);
2013
2014	if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2015		return (error);
2016
2017	vput(rvp);
2018
2019	/*
2020	 * If an indexfile was specified, pull it in.
2021	 */
2022	if (argp->ex_indexfile != NULL) {
2023		MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP,
2024		    M_WAITOK);
2025		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2026		    MAXNAMLEN, (size_t *)0);
2027		if (!error) {
2028			/*
2029			 * Check for illegal filenames.
2030			 */
2031			for (cp = nfs_pub.np_index; *cp; cp++) {
2032				if (*cp == '/') {
2033					error = EINVAL;
2034					break;
2035				}
2036			}
2037		}
2038		if (error) {
2039			FREE(nfs_pub.np_index, M_TEMP);
2040			return (error);
2041		}
2042	}
2043
2044	nfs_pub.np_mount = mp;
2045	nfs_pub.np_valid = 1;
2046	return (0);
2047}
2048
2049struct netcred *
2050vfs_export_lookup(mp, nep, nam)
2051	register struct mount *mp;
2052	struct netexport *nep;
2053	struct sockaddr *nam;
2054{
2055	register struct netcred *np;
2056	register struct radix_node_head *rnh;
2057	struct sockaddr *saddr;
2058
2059	np = NULL;
2060	if (mp->mnt_flag & MNT_EXPORTED) {
2061		/*
2062		 * Lookup in the export list first.
2063		 */
2064		if (nam != NULL) {
2065			saddr = nam;
2066			rnh = nep->ne_rtable[saddr->sa_family];
2067			if (rnh != NULL) {
2068				np = (struct netcred *)
2069					(*rnh->rnh_matchaddr)((caddr_t)saddr,
2070							      rnh);
2071				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2072					np = NULL;
2073			}
2074		}
2075		/*
2076		 * If no address match, use the default if it exists.
2077		 */
2078		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2079			np = &nep->ne_defexported;
2080	}
2081	return (np);
2082}
2083
2084/*
2085 * perform msync on all vnodes under a mount point
2086 * the mount point must be locked.
2087 */
2088void
2089vfs_msync(struct mount *mp, int flags) {
2090	struct vnode *vp, *nvp;
2091	int anyio, tries;
2092
2093	tries = 5;
2094loop:
2095	anyio = 0;
2096	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
2097
2098		nvp = vp->v_mntvnodes.le_next;
2099
2100		if (vp->v_mount != mp) {
2101			goto loop;
2102		}
2103
2104		if ((vp->v_flag & VXLOCK) ||
2105			(VOP_ISLOCKED(vp) && (flags != MNT_WAIT))) {
2106			continue;
2107		}
2108
2109		simple_lock(&vp->v_interlock);
2110		if (vp->v_object &&
2111		   (vp->v_object->flags & OBJ_MIGHTBEDIRTY)) {
2112			if (!vget(vp,
2113				LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) {
2114				if (vp->v_object) {
2115					vm_object_page_clean(vp->v_object, 0, 0, TRUE);
2116					anyio = 1;
2117				}
2118				vput(vp);
2119			}
2120		} else {
2121			simple_unlock(&vp->v_interlock);
2122		}
2123	}
2124	if (anyio && (--tries > 0))
2125		goto loop;
2126}
2127
2128/*
2129 * Create the VM object needed for VMIO and mmap support.  This
2130 * is done for all VREG files in the system.  Some filesystems might
2131 * afford the additional metadata buffering capability of the
2132 * VMIO code by making the device node be VMIO mode also.
2133 *
2134 * If !waslocked, must be called with interlock.
2135 */
2136int
2137vfs_object_create(vp, p, cred, waslocked)
2138	struct vnode *vp;
2139	struct proc *p;
2140	struct ucred *cred;
2141	int waslocked;
2142{
2143	struct vattr vat;
2144	vm_object_t object;
2145	int error = 0;
2146
2147	if ((vp->v_type != VREG) && (vp->v_type != VBLK)) {
2148		if (!waslocked)
2149			simple_unlock(&vp->v_interlock);
2150		return 0;
2151	}
2152
2153	if (!waslocked)
2154		vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK | LK_RETRY, p);
2155
2156retry:
2157	if ((object = vp->v_object) == NULL) {
2158		if (vp->v_type == VREG) {
2159			if ((error = VOP_GETATTR(vp, &vat, cred, p)) != 0)
2160				goto retn;
2161			object = vnode_pager_alloc(vp,
2162				OFF_TO_IDX(round_page(vat.va_size)), 0, 0);
2163		} else if (major(vp->v_rdev) < nblkdev) {
2164			/*
2165			 * This simply allocates the biggest object possible
2166			 * for a VBLK vnode.  This should be fixed, but doesn't
2167			 * cause any problems (yet).
2168			 */
2169			object = vnode_pager_alloc(vp, INT_MAX, 0, 0);
2170		}
2171		object->ref_count--;
2172		vp->v_usecount--;
2173	} else {
2174		if (object->flags & OBJ_DEAD) {
2175			VOP_UNLOCK(vp, 0, p);
2176			tsleep(object, PVM, "vodead", 0);
2177			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
2178			goto retry;
2179		}
2180	}
2181
2182	if (vp->v_object) {
2183		vp->v_flag |= VOBJBUF;
2184	}
2185
2186retn:
2187	if (!waslocked) {
2188		simple_lock(&vp->v_interlock);
2189		VOP_UNLOCK(vp, LK_INTERLOCK, p);
2190	}
2191
2192	return error;
2193}
2194
2195static void
2196vfree(vp)
2197	struct vnode *vp;
2198{
2199	int s;
2200
2201	s = splbio();
2202	simple_lock(&vnode_free_list_slock);
2203	if (vp->v_flag & VTBFREE) {
2204		TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2205		vp->v_flag &= ~VTBFREE;
2206	}
2207	if (vp->v_flag & VAGE) {
2208		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
2209	} else {
2210		TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
2211	}
2212	freevnodes++;
2213	simple_unlock(&vnode_free_list_slock);
2214	vp->v_flag &= ~VAGE;
2215	vp->v_flag |= VFREE;
2216	splx(s);
2217}
2218
2219void
2220vbusy(vp)
2221	struct vnode *vp;
2222{
2223	int s;
2224
2225	s = splbio();
2226	simple_lock(&vnode_free_list_slock);
2227	if (vp->v_flag & VTBFREE) {
2228		TAILQ_REMOVE(&vnode_tobefree_list, vp, v_freelist);
2229		vp->v_flag &= ~VTBFREE;
2230	} else {
2231		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
2232		freevnodes--;
2233	}
2234	simple_unlock(&vnode_free_list_slock);
2235	vp->v_flag &= ~VFREE;
2236	splx(s);
2237}
2238
2239/*
2240 * Record a process's interest in events which might happen to
2241 * a vnode.  Because poll uses the historic select-style interface
2242 * internally, this routine serves as both the ``check for any
2243 * pending events'' and the ``record my interest in future events''
2244 * functions.  (These are done together, while the lock is held,
2245 * to avoid race conditions.)
2246 */
2247int
2248vn_pollrecord(vp, p, events)
2249	struct vnode *vp;
2250	struct proc *p;
2251	short events;
2252{
2253	simple_lock(&vp->v_pollinfo.vpi_lock);
2254	if (vp->v_pollinfo.vpi_revents & events) {
2255		/*
2256		 * This leaves events we are not interested
2257		 * in available for the other process which
2258		 * which presumably had requested them
2259		 * (otherwise they would never have been
2260		 * recorded).
2261		 */
2262		events &= vp->v_pollinfo.vpi_revents;
2263		vp->v_pollinfo.vpi_revents &= ~events;
2264
2265		simple_unlock(&vp->v_pollinfo.vpi_lock);
2266		return events;
2267	}
2268	vp->v_pollinfo.vpi_events |= events;
2269	selrecord(p, &vp->v_pollinfo.vpi_selinfo);
2270	simple_unlock(&vp->v_pollinfo.vpi_lock);
2271	return 0;
2272}
2273
2274/*
2275 * Note the occurrence of an event.  If the VN_POLLEVENT macro is used,
2276 * it is possible for us to miss an event due to race conditions, but
2277 * that condition is expected to be rare, so for the moment it is the
2278 * preferred interface.
2279 */
2280void
2281vn_pollevent(vp, events)
2282	struct vnode *vp;
2283	short events;
2284{
2285	simple_lock(&vp->v_pollinfo.vpi_lock);
2286	if (vp->v_pollinfo.vpi_events & events) {
2287		/*
2288		 * We clear vpi_events so that we don't
2289		 * call selwakeup() twice if two events are
2290		 * posted before the polling process(es) is
2291		 * awakened.  This also ensures that we take at
2292		 * most one selwakeup() if the polling process
2293		 * is no longer interested.  However, it does
2294		 * mean that only one event can be noticed at
2295		 * a time.  (Perhaps we should only clear those
2296		 * event bits which we note?) XXX
2297		 */
2298		vp->v_pollinfo.vpi_events = 0;	/* &= ~events ??? */
2299		vp->v_pollinfo.vpi_revents |= events;
2300		selwakeup(&vp->v_pollinfo.vpi_selinfo);
2301	}
2302	simple_unlock(&vp->v_pollinfo.vpi_lock);
2303}
2304
2305/*
2306 * Wake up anyone polling on vp because it is being revoked.
2307 * This depends on dead_poll() returning POLLHUP for correct
2308 * behavior.
2309 */
2310void
2311vn_pollgone(vp)
2312	struct vnode *vp;
2313{
2314	simple_lock(&vp->v_pollinfo.vpi_lock);
2315	if (vp->v_pollinfo.vpi_events) {
2316		vp->v_pollinfo.vpi_events = 0;
2317		selwakeup(&vp->v_pollinfo.vpi_selinfo);
2318	}
2319	simple_unlock(&vp->v_pollinfo.vpi_lock);
2320}
2321