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