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