xref: /freebsd/sys/kern/vfs_export.c (revision df8bae1de4b67ccf57f4afebd4e2bf258c38910d)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
19df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
20df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
21df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
22df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
23df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
24df8bae1dSRodney W. Grimes  *    without specific prior written permission.
25df8bae1dSRodney W. Grimes  *
26df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *	@(#)vfs_subr.c	8.13 (Berkeley) 4/18/94
39df8bae1dSRodney W. Grimes  */
40df8bae1dSRodney W. Grimes 
41df8bae1dSRodney W. Grimes /*
42df8bae1dSRodney W. Grimes  * External virtual filesystem routines
43df8bae1dSRodney W. Grimes  */
44df8bae1dSRodney W. Grimes 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
47df8bae1dSRodney W. Grimes #include <sys/proc.h>
48df8bae1dSRodney W. Grimes #include <sys/mount.h>
49df8bae1dSRodney W. Grimes #include <sys/time.h>
50df8bae1dSRodney W. Grimes #include <sys/vnode.h>
51df8bae1dSRodney W. Grimes #include <sys/stat.h>
52df8bae1dSRodney W. Grimes #include <sys/namei.h>
53df8bae1dSRodney W. Grimes #include <sys/ucred.h>
54df8bae1dSRodney W. Grimes #include <sys/buf.h>
55df8bae1dSRodney W. Grimes #include <sys/errno.h>
56df8bae1dSRodney W. Grimes #include <sys/malloc.h>
57df8bae1dSRodney W. Grimes #include <sys/domain.h>
58df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <vm/vm.h>
61df8bae1dSRodney W. Grimes #include <sys/sysctl.h>
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes #include <miscfs/specfs/specdev.h>
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes enum vtype iftovt_tab[16] = {
66df8bae1dSRodney W. Grimes 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
67df8bae1dSRodney W. Grimes 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
68df8bae1dSRodney W. Grimes };
69df8bae1dSRodney W. Grimes int	vttoif_tab[9] = {
70df8bae1dSRodney W. Grimes 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
71df8bae1dSRodney W. Grimes 	S_IFSOCK, S_IFIFO, S_IFMT,
72df8bae1dSRodney W. Grimes };
73df8bae1dSRodney W. Grimes 
74df8bae1dSRodney W. Grimes /*
75df8bae1dSRodney W. Grimes  * Insq/Remq for the vnode usage lists.
76df8bae1dSRodney W. Grimes  */
77df8bae1dSRodney W. Grimes #define	bufinsvn(bp, dp)	LIST_INSERT_HEAD(dp, bp, b_vnbufs)
78df8bae1dSRodney W. Grimes #define	bufremvn(bp) {  \
79df8bae1dSRodney W. Grimes 	LIST_REMOVE(bp, b_vnbufs); \
80df8bae1dSRodney W. Grimes 	(bp)->b_vnbufs.le_next = NOLIST; \
81df8bae1dSRodney W. Grimes }
82df8bae1dSRodney W. Grimes 
83df8bae1dSRodney W. Grimes TAILQ_HEAD(freelst, vnode) vnode_free_list;	/* vnode free list */
84df8bae1dSRodney W. Grimes struct mntlist mountlist;			/* mounted filesystem list */
85df8bae1dSRodney W. Grimes 
86df8bae1dSRodney W. Grimes /*
87df8bae1dSRodney W. Grimes  * Initialize the vnode management data structures.
88df8bae1dSRodney W. Grimes  */
89df8bae1dSRodney W. Grimes vntblinit()
90df8bae1dSRodney W. Grimes {
91df8bae1dSRodney W. Grimes 
92df8bae1dSRodney W. Grimes 	TAILQ_INIT(&vnode_free_list);
93df8bae1dSRodney W. Grimes 	TAILQ_INIT(&mountlist);
94df8bae1dSRodney W. Grimes }
95df8bae1dSRodney W. Grimes 
96df8bae1dSRodney W. Grimes /*
97df8bae1dSRodney W. Grimes  * Lock a filesystem.
98df8bae1dSRodney W. Grimes  * Used to prevent access to it while mounting and unmounting.
99df8bae1dSRodney W. Grimes  */
100df8bae1dSRodney W. Grimes vfs_lock(mp)
101df8bae1dSRodney W. Grimes 	register struct mount *mp;
102df8bae1dSRodney W. Grimes {
103df8bae1dSRodney W. Grimes 
104df8bae1dSRodney W. Grimes 	while(mp->mnt_flag & MNT_MLOCK) {
105df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_MWAIT;
106df8bae1dSRodney W. Grimes 		sleep((caddr_t)mp, PVFS);
107df8bae1dSRodney W. Grimes 	}
108df8bae1dSRodney W. Grimes 	mp->mnt_flag |= MNT_MLOCK;
109df8bae1dSRodney W. Grimes 	return (0);
110df8bae1dSRodney W. Grimes }
111df8bae1dSRodney W. Grimes 
112df8bae1dSRodney W. Grimes /*
113df8bae1dSRodney W. Grimes  * Unlock a locked filesystem.
114df8bae1dSRodney W. Grimes  * Panic if filesystem is not locked.
115df8bae1dSRodney W. Grimes  */
116df8bae1dSRodney W. Grimes void
117df8bae1dSRodney W. Grimes vfs_unlock(mp)
118df8bae1dSRodney W. Grimes 	register struct mount *mp;
119df8bae1dSRodney W. Grimes {
120df8bae1dSRodney W. Grimes 
121df8bae1dSRodney W. Grimes 	if ((mp->mnt_flag & MNT_MLOCK) == 0)
122df8bae1dSRodney W. Grimes 		panic("vfs_unlock: not locked");
123df8bae1dSRodney W. Grimes 	mp->mnt_flag &= ~MNT_MLOCK;
124df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_MWAIT) {
125df8bae1dSRodney W. Grimes 		mp->mnt_flag &= ~MNT_MWAIT;
126df8bae1dSRodney W. Grimes 		wakeup((caddr_t)mp);
127df8bae1dSRodney W. Grimes 	}
128df8bae1dSRodney W. Grimes }
129df8bae1dSRodney W. Grimes 
130df8bae1dSRodney W. Grimes /*
131df8bae1dSRodney W. Grimes  * Mark a mount point as busy.
132df8bae1dSRodney W. Grimes  * Used to synchronize access and to delay unmounting.
133df8bae1dSRodney W. Grimes  */
134df8bae1dSRodney W. Grimes vfs_busy(mp)
135df8bae1dSRodney W. Grimes 	register struct mount *mp;
136df8bae1dSRodney W. Grimes {
137df8bae1dSRodney W. Grimes 
138df8bae1dSRodney W. Grimes 	while(mp->mnt_flag & MNT_MPBUSY) {
139df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_MPWANT;
140df8bae1dSRodney W. Grimes 		sleep((caddr_t)&mp->mnt_flag, PVFS);
141df8bae1dSRodney W. Grimes 	}
142df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_UNMOUNT)
143df8bae1dSRodney W. Grimes 		return (1);
144df8bae1dSRodney W. Grimes 	mp->mnt_flag |= MNT_MPBUSY;
145df8bae1dSRodney W. Grimes 	return (0);
146df8bae1dSRodney W. Grimes }
147df8bae1dSRodney W. Grimes 
148df8bae1dSRodney W. Grimes /*
149df8bae1dSRodney W. Grimes  * Free a busy filesystem.
150df8bae1dSRodney W. Grimes  * Panic if filesystem is not busy.
151df8bae1dSRodney W. Grimes  */
152df8bae1dSRodney W. Grimes vfs_unbusy(mp)
153df8bae1dSRodney W. Grimes 	register struct mount *mp;
154df8bae1dSRodney W. Grimes {
155df8bae1dSRodney W. Grimes 
156df8bae1dSRodney W. Grimes 	if ((mp->mnt_flag & MNT_MPBUSY) == 0)
157df8bae1dSRodney W. Grimes 		panic("vfs_unbusy: not busy");
158df8bae1dSRodney W. Grimes 	mp->mnt_flag &= ~MNT_MPBUSY;
159df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_MPWANT) {
160df8bae1dSRodney W. Grimes 		mp->mnt_flag &= ~MNT_MPWANT;
161df8bae1dSRodney W. Grimes 		wakeup((caddr_t)&mp->mnt_flag);
162df8bae1dSRodney W. Grimes 	}
163df8bae1dSRodney W. Grimes }
164df8bae1dSRodney W. Grimes 
165df8bae1dSRodney W. Grimes /*
166df8bae1dSRodney W. Grimes  * Lookup a mount point by filesystem identifier.
167df8bae1dSRodney W. Grimes  */
168df8bae1dSRodney W. Grimes struct mount *
169df8bae1dSRodney W. Grimes getvfs(fsid)
170df8bae1dSRodney W. Grimes 	fsid_t *fsid;
171df8bae1dSRodney W. Grimes {
172df8bae1dSRodney W. Grimes 	register struct mount *mp;
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes 	for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) {
175df8bae1dSRodney W. Grimes 		if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] &&
176df8bae1dSRodney W. Grimes 		    mp->mnt_stat.f_fsid.val[1] == fsid->val[1])
177df8bae1dSRodney W. Grimes 			return (mp);
178df8bae1dSRodney W. Grimes 	}
179df8bae1dSRodney W. Grimes 	return ((struct mount *)0);
180df8bae1dSRodney W. Grimes }
181df8bae1dSRodney W. Grimes 
182df8bae1dSRodney W. Grimes /*
183df8bae1dSRodney W. Grimes  * Get a new unique fsid
184df8bae1dSRodney W. Grimes  */
185df8bae1dSRodney W. Grimes void
186df8bae1dSRodney W. Grimes getnewfsid(mp, mtype)
187df8bae1dSRodney W. Grimes 	struct mount *mp;
188df8bae1dSRodney W. Grimes 	int mtype;
189df8bae1dSRodney W. Grimes {
190df8bae1dSRodney W. Grimes static u_short xxxfs_mntid;
191df8bae1dSRodney W. Grimes 
192df8bae1dSRodney W. Grimes 	fsid_t tfsid;
193df8bae1dSRodney W. Grimes 
194df8bae1dSRodney W. Grimes 	mp->mnt_stat.f_fsid.val[0] = makedev(nblkdev + mtype, 0);
195df8bae1dSRodney W. Grimes 	mp->mnt_stat.f_fsid.val[1] = mtype;
196df8bae1dSRodney W. Grimes 	if (xxxfs_mntid == 0)
197df8bae1dSRodney W. Grimes 		++xxxfs_mntid;
198df8bae1dSRodney W. Grimes 	tfsid.val[0] = makedev(nblkdev + mtype, xxxfs_mntid);
199df8bae1dSRodney W. Grimes 	tfsid.val[1] = mtype;
200df8bae1dSRodney W. Grimes 	if (mountlist.tqh_first != NULL) {
201df8bae1dSRodney W. Grimes 		while (getvfs(&tfsid)) {
202df8bae1dSRodney W. Grimes 			tfsid.val[0]++;
203df8bae1dSRodney W. Grimes 			xxxfs_mntid++;
204df8bae1dSRodney W. Grimes 		}
205df8bae1dSRodney W. Grimes 	}
206df8bae1dSRodney W. Grimes 	mp->mnt_stat.f_fsid.val[0] = tfsid.val[0];
207df8bae1dSRodney W. Grimes }
208df8bae1dSRodney W. Grimes 
209df8bae1dSRodney W. Grimes /*
210df8bae1dSRodney W. Grimes  * Set vnode attributes to VNOVAL
211df8bae1dSRodney W. Grimes  */
212df8bae1dSRodney W. Grimes void vattr_null(vap)
213df8bae1dSRodney W. Grimes 	register struct vattr *vap;
214df8bae1dSRodney W. Grimes {
215df8bae1dSRodney W. Grimes 
216df8bae1dSRodney W. Grimes 	vap->va_type = VNON;
217df8bae1dSRodney W. Grimes 	vap->va_size = vap->va_bytes = VNOVAL;
218df8bae1dSRodney W. Grimes 	vap->va_mode = vap->va_nlink = vap->va_uid = vap->va_gid =
219df8bae1dSRodney W. Grimes 		vap->va_fsid = vap->va_fileid =
220df8bae1dSRodney W. Grimes 		vap->va_blocksize = vap->va_rdev =
221df8bae1dSRodney W. Grimes 		vap->va_atime.ts_sec = vap->va_atime.ts_nsec =
222df8bae1dSRodney W. Grimes 		vap->va_mtime.ts_sec = vap->va_mtime.ts_nsec =
223df8bae1dSRodney W. Grimes 		vap->va_ctime.ts_sec = vap->va_ctime.ts_nsec =
224df8bae1dSRodney W. Grimes 		vap->va_flags = vap->va_gen = VNOVAL;
225df8bae1dSRodney W. Grimes 	vap->va_vaflags = 0;
226df8bae1dSRodney W. Grimes }
227df8bae1dSRodney W. Grimes 
228df8bae1dSRodney W. Grimes /*
229df8bae1dSRodney W. Grimes  * Routines having to do with the management of the vnode table.
230df8bae1dSRodney W. Grimes  */
231df8bae1dSRodney W. Grimes extern int (**dead_vnodeop_p)();
232df8bae1dSRodney W. Grimes extern void vclean();
233df8bae1dSRodney W. Grimes long numvnodes;
234df8bae1dSRodney W. Grimes extern struct vattr va_null;
235df8bae1dSRodney W. Grimes 
236df8bae1dSRodney W. Grimes /*
237df8bae1dSRodney W. Grimes  * Return the next vnode from the free list.
238df8bae1dSRodney W. Grimes  */
239df8bae1dSRodney W. Grimes getnewvnode(tag, mp, vops, vpp)
240df8bae1dSRodney W. Grimes 	enum vtagtype tag;
241df8bae1dSRodney W. Grimes 	struct mount *mp;
242df8bae1dSRodney W. Grimes 	int (**vops)();
243df8bae1dSRodney W. Grimes 	struct vnode **vpp;
244df8bae1dSRodney W. Grimes {
245df8bae1dSRodney W. Grimes 	register struct vnode *vp;
246df8bae1dSRodney W. Grimes 	int s;
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes 	if ((vnode_free_list.tqh_first == NULL &&
249df8bae1dSRodney W. Grimes 	     numvnodes < 2 * desiredvnodes) ||
250df8bae1dSRodney W. Grimes 	    numvnodes < desiredvnodes) {
251df8bae1dSRodney W. Grimes 		vp = (struct vnode *)malloc((u_long)sizeof *vp,
252df8bae1dSRodney W. Grimes 		    M_VNODE, M_WAITOK);
253df8bae1dSRodney W. Grimes 		bzero((char *)vp, sizeof *vp);
254df8bae1dSRodney W. Grimes 		numvnodes++;
255df8bae1dSRodney W. Grimes 	} else {
256df8bae1dSRodney W. Grimes 		if ((vp = vnode_free_list.tqh_first) == NULL) {
257df8bae1dSRodney W. Grimes 			tablefull("vnode");
258df8bae1dSRodney W. Grimes 			*vpp = 0;
259df8bae1dSRodney W. Grimes 			return (ENFILE);
260df8bae1dSRodney W. Grimes 		}
261df8bae1dSRodney W. Grimes 		if (vp->v_usecount)
262df8bae1dSRodney W. Grimes 			panic("free vnode isn't");
263df8bae1dSRodney W. Grimes 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
264df8bae1dSRodney W. Grimes 		/* see comment on why 0xdeadb is set at end of vgone (below) */
265df8bae1dSRodney W. Grimes 		vp->v_freelist.tqe_prev = (struct vnode **)0xdeadb;
266df8bae1dSRodney W. Grimes 		vp->v_lease = NULL;
267df8bae1dSRodney W. Grimes 		if (vp->v_type != VBAD)
268df8bae1dSRodney W. Grimes 			vgone(vp);
269df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
270df8bae1dSRodney W. Grimes 		if (vp->v_data)
271df8bae1dSRodney W. Grimes 			panic("cleaned vnode isn't");
272df8bae1dSRodney W. Grimes 		s = splbio();
273df8bae1dSRodney W. Grimes 		if (vp->v_numoutput)
274df8bae1dSRodney W. Grimes 			panic("Clean vnode has pending I/O's");
275df8bae1dSRodney W. Grimes 		splx(s);
276df8bae1dSRodney W. Grimes #endif
277df8bae1dSRodney W. Grimes 		vp->v_flag = 0;
278df8bae1dSRodney W. Grimes 		vp->v_lastr = 0;
279df8bae1dSRodney W. Grimes 		vp->v_ralen = 0;
280df8bae1dSRodney W. Grimes 		vp->v_maxra = 0;
281df8bae1dSRodney W. Grimes 		vp->v_lastw = 0;
282df8bae1dSRodney W. Grimes 		vp->v_lasta = 0;
283df8bae1dSRodney W. Grimes 		vp->v_cstart = 0;
284df8bae1dSRodney W. Grimes 		vp->v_clen = 0;
285df8bae1dSRodney W. Grimes 		vp->v_socket = 0;
286df8bae1dSRodney W. Grimes 	}
287df8bae1dSRodney W. Grimes 	vp->v_type = VNON;
288df8bae1dSRodney W. Grimes 	cache_purge(vp);
289df8bae1dSRodney W. Grimes 	vp->v_tag = tag;
290df8bae1dSRodney W. Grimes 	vp->v_op = vops;
291df8bae1dSRodney W. Grimes 	insmntque(vp, mp);
292df8bae1dSRodney W. Grimes 	*vpp = vp;
293df8bae1dSRodney W. Grimes 	vp->v_usecount = 1;
294df8bae1dSRodney W. Grimes 	vp->v_data = 0;
295df8bae1dSRodney W. Grimes 	return (0);
296df8bae1dSRodney W. Grimes }
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes /*
299df8bae1dSRodney W. Grimes  * Move a vnode from one mount queue to another.
300df8bae1dSRodney W. Grimes  */
301df8bae1dSRodney W. Grimes insmntque(vp, mp)
302df8bae1dSRodney W. Grimes 	register struct vnode *vp;
303df8bae1dSRodney W. Grimes 	register struct mount *mp;
304df8bae1dSRodney W. Grimes {
305df8bae1dSRodney W. Grimes 
306df8bae1dSRodney W. Grimes 	/*
307df8bae1dSRodney W. Grimes 	 * Delete from old mount point vnode list, if on one.
308df8bae1dSRodney W. Grimes 	 */
309df8bae1dSRodney W. Grimes 	if (vp->v_mount != NULL)
310df8bae1dSRodney W. Grimes 		LIST_REMOVE(vp, v_mntvnodes);
311df8bae1dSRodney W. Grimes 	/*
312df8bae1dSRodney W. Grimes 	 * Insert into list of vnodes for the new mount point, if available.
313df8bae1dSRodney W. Grimes 	 */
314df8bae1dSRodney W. Grimes 	if ((vp->v_mount = mp) == NULL)
315df8bae1dSRodney W. Grimes 		return;
316df8bae1dSRodney W. Grimes 	LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes);
317df8bae1dSRodney W. Grimes }
318df8bae1dSRodney W. Grimes 
319df8bae1dSRodney W. Grimes /*
320df8bae1dSRodney W. Grimes  * Update outstanding I/O count and do wakeup if requested.
321df8bae1dSRodney W. Grimes  */
322df8bae1dSRodney W. Grimes vwakeup(bp)
323df8bae1dSRodney W. Grimes 	register struct buf *bp;
324df8bae1dSRodney W. Grimes {
325df8bae1dSRodney W. Grimes 	register struct vnode *vp;
326df8bae1dSRodney W. Grimes 
327df8bae1dSRodney W. Grimes 	bp->b_flags &= ~B_WRITEINPROG;
328df8bae1dSRodney W. Grimes 	if (vp = bp->b_vp) {
329df8bae1dSRodney W. Grimes 		vp->v_numoutput--;
330df8bae1dSRodney W. Grimes 		if (vp->v_numoutput < 0)
331df8bae1dSRodney W. Grimes 			panic("vwakeup: neg numoutput");
332df8bae1dSRodney W. Grimes 		if ((vp->v_flag & VBWAIT) && vp->v_numoutput <= 0) {
333df8bae1dSRodney W. Grimes 			if (vp->v_numoutput < 0)
334df8bae1dSRodney W. Grimes 				panic("vwakeup: neg numoutput");
335df8bae1dSRodney W. Grimes 			vp->v_flag &= ~VBWAIT;
336df8bae1dSRodney W. Grimes 			wakeup((caddr_t)&vp->v_numoutput);
337df8bae1dSRodney W. Grimes 		}
338df8bae1dSRodney W. Grimes 	}
339df8bae1dSRodney W. Grimes }
340df8bae1dSRodney W. Grimes 
341df8bae1dSRodney W. Grimes /*
342df8bae1dSRodney W. Grimes  * Flush out and invalidate all buffers associated with a vnode.
343df8bae1dSRodney W. Grimes  * Called with the underlying object locked.
344df8bae1dSRodney W. Grimes  */
345df8bae1dSRodney W. Grimes int
346df8bae1dSRodney W. Grimes vinvalbuf(vp, flags, cred, p, slpflag, slptimeo)
347df8bae1dSRodney W. Grimes 	register struct vnode *vp;
348df8bae1dSRodney W. Grimes 	int flags;
349df8bae1dSRodney W. Grimes 	struct ucred *cred;
350df8bae1dSRodney W. Grimes 	struct proc *p;
351df8bae1dSRodney W. Grimes 	int slpflag, slptimeo;
352df8bae1dSRodney W. Grimes {
353df8bae1dSRodney W. Grimes 	register struct buf *bp;
354df8bae1dSRodney W. Grimes 	struct buf *nbp, *blist;
355df8bae1dSRodney W. Grimes 	int s, error;
356df8bae1dSRodney W. Grimes 
357df8bae1dSRodney W. Grimes 	if (flags & V_SAVE) {
358df8bae1dSRodney W. Grimes 		if (error = VOP_FSYNC(vp, cred, MNT_WAIT, p))
359df8bae1dSRodney W. Grimes 			return (error);
360df8bae1dSRodney W. Grimes 		if (vp->v_dirtyblkhd.lh_first != NULL)
361df8bae1dSRodney W. Grimes 			panic("vinvalbuf: dirty bufs");
362df8bae1dSRodney W. Grimes 	}
363df8bae1dSRodney W. Grimes 	for (;;) {
364df8bae1dSRodney W. Grimes 		if ((blist = vp->v_cleanblkhd.lh_first) && flags & V_SAVEMETA)
365df8bae1dSRodney W. Grimes 			while (blist && blist->b_lblkno < 0)
366df8bae1dSRodney W. Grimes 				blist = blist->b_vnbufs.le_next;
367df8bae1dSRodney W. Grimes 		if (!blist && (blist = vp->v_dirtyblkhd.lh_first) &&
368df8bae1dSRodney W. Grimes 		    (flags & V_SAVEMETA))
369df8bae1dSRodney W. Grimes 			while (blist && blist->b_lblkno < 0)
370df8bae1dSRodney W. Grimes 				blist = blist->b_vnbufs.le_next;
371df8bae1dSRodney W. Grimes 		if (!blist)
372df8bae1dSRodney W. Grimes 			break;
373df8bae1dSRodney W. Grimes 
374df8bae1dSRodney W. Grimes 		for (bp = blist; bp; bp = nbp) {
375df8bae1dSRodney W. Grimes 			nbp = bp->b_vnbufs.le_next;
376df8bae1dSRodney W. Grimes 			if (flags & V_SAVEMETA && bp->b_lblkno < 0)
377df8bae1dSRodney W. Grimes 				continue;
378df8bae1dSRodney W. Grimes 			s = splbio();
379df8bae1dSRodney W. Grimes 			if (bp->b_flags & B_BUSY) {
380df8bae1dSRodney W. Grimes 				bp->b_flags |= B_WANTED;
381df8bae1dSRodney W. Grimes 				error = tsleep((caddr_t)bp,
382df8bae1dSRodney W. Grimes 					slpflag | (PRIBIO + 1), "vinvalbuf",
383df8bae1dSRodney W. Grimes 					slptimeo);
384df8bae1dSRodney W. Grimes 				splx(s);
385df8bae1dSRodney W. Grimes 				if (error)
386df8bae1dSRodney W. Grimes 					return (error);
387df8bae1dSRodney W. Grimes 				break;
388df8bae1dSRodney W. Grimes 			}
389df8bae1dSRodney W. Grimes 			bremfree(bp);
390df8bae1dSRodney W. Grimes 			bp->b_flags |= B_BUSY;
391df8bae1dSRodney W. Grimes 			splx(s);
392df8bae1dSRodney W. Grimes 			/*
393df8bae1dSRodney W. Grimes 			 * XXX Since there are no node locks for NFS, I believe
394df8bae1dSRodney W. Grimes 			 * there is a slight chance that a delayed write will
395df8bae1dSRodney W. Grimes 			 * occur while sleeping just above, so check for it.
396df8bae1dSRodney W. Grimes 			 */
397df8bae1dSRodney W. Grimes 			if ((bp->b_flags & B_DELWRI) && (flags & V_SAVE)) {
398df8bae1dSRodney W. Grimes 				(void) VOP_BWRITE(bp);
399df8bae1dSRodney W. Grimes 				break;
400df8bae1dSRodney W. Grimes 			}
401df8bae1dSRodney W. Grimes 			bp->b_flags |= B_INVAL;
402df8bae1dSRodney W. Grimes 			brelse(bp);
403df8bae1dSRodney W. Grimes 		}
404df8bae1dSRodney W. Grimes 	}
405df8bae1dSRodney W. Grimes 	if (!(flags & V_SAVEMETA) &&
406df8bae1dSRodney W. Grimes 	    (vp->v_dirtyblkhd.lh_first || vp->v_cleanblkhd.lh_first))
407df8bae1dSRodney W. Grimes 		panic("vinvalbuf: flush failed");
408df8bae1dSRodney W. Grimes 	return (0);
409df8bae1dSRodney W. Grimes }
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes /*
412df8bae1dSRodney W. Grimes  * Associate a buffer with a vnode.
413df8bae1dSRodney W. Grimes  */
414df8bae1dSRodney W. Grimes bgetvp(vp, bp)
415df8bae1dSRodney W. Grimes 	register struct vnode *vp;
416df8bae1dSRodney W. Grimes 	register struct buf *bp;
417df8bae1dSRodney W. Grimes {
418df8bae1dSRodney W. Grimes 
419df8bae1dSRodney W. Grimes 	if (bp->b_vp)
420df8bae1dSRodney W. Grimes 		panic("bgetvp: not free");
421df8bae1dSRodney W. Grimes 	VHOLD(vp);
422df8bae1dSRodney W. Grimes 	bp->b_vp = vp;
423df8bae1dSRodney W. Grimes 	if (vp->v_type == VBLK || vp->v_type == VCHR)
424df8bae1dSRodney W. Grimes 		bp->b_dev = vp->v_rdev;
425df8bae1dSRodney W. Grimes 	else
426df8bae1dSRodney W. Grimes 		bp->b_dev = NODEV;
427df8bae1dSRodney W. Grimes 	/*
428df8bae1dSRodney W. Grimes 	 * Insert onto list for new vnode.
429df8bae1dSRodney W. Grimes 	 */
430df8bae1dSRodney W. Grimes 	bufinsvn(bp, &vp->v_cleanblkhd);
431df8bae1dSRodney W. Grimes }
432df8bae1dSRodney W. Grimes 
433df8bae1dSRodney W. Grimes /*
434df8bae1dSRodney W. Grimes  * Disassociate a buffer from a vnode.
435df8bae1dSRodney W. Grimes  */
436df8bae1dSRodney W. Grimes brelvp(bp)
437df8bae1dSRodney W. Grimes 	register struct buf *bp;
438df8bae1dSRodney W. Grimes {
439df8bae1dSRodney W. Grimes 	struct vnode *vp;
440df8bae1dSRodney W. Grimes 
441df8bae1dSRodney W. Grimes 	if (bp->b_vp == (struct vnode *) 0)
442df8bae1dSRodney W. Grimes 		panic("brelvp: NULL");
443df8bae1dSRodney W. Grimes 	/*
444df8bae1dSRodney W. Grimes 	 * Delete from old vnode list, if on one.
445df8bae1dSRodney W. Grimes 	 */
446df8bae1dSRodney W. Grimes 	if (bp->b_vnbufs.le_next != NOLIST)
447df8bae1dSRodney W. Grimes 		bufremvn(bp);
448df8bae1dSRodney W. Grimes 	vp = bp->b_vp;
449df8bae1dSRodney W. Grimes 	bp->b_vp = (struct vnode *) 0;
450df8bae1dSRodney W. Grimes 	HOLDRELE(vp);
451df8bae1dSRodney W. Grimes }
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes /*
454df8bae1dSRodney W. Grimes  * Reassign a buffer from one vnode to another.
455df8bae1dSRodney W. Grimes  * Used to assign file specific control information
456df8bae1dSRodney W. Grimes  * (indirect blocks) to the vnode to which they belong.
457df8bae1dSRodney W. Grimes  */
458df8bae1dSRodney W. Grimes reassignbuf(bp, newvp)
459df8bae1dSRodney W. Grimes 	register struct buf *bp;
460df8bae1dSRodney W. Grimes 	register struct vnode *newvp;
461df8bae1dSRodney W. Grimes {
462df8bae1dSRodney W. Grimes 	register struct buflists *listheadp;
463df8bae1dSRodney W. Grimes 
464df8bae1dSRodney W. Grimes 	if (newvp == NULL) {
465df8bae1dSRodney W. Grimes 		printf("reassignbuf: NULL");
466df8bae1dSRodney W. Grimes 		return;
467df8bae1dSRodney W. Grimes 	}
468df8bae1dSRodney W. Grimes 	/*
469df8bae1dSRodney W. Grimes 	 * Delete from old vnode list, if on one.
470df8bae1dSRodney W. Grimes 	 */
471df8bae1dSRodney W. Grimes 	if (bp->b_vnbufs.le_next != NOLIST)
472df8bae1dSRodney W. Grimes 		bufremvn(bp);
473df8bae1dSRodney W. Grimes 	/*
474df8bae1dSRodney W. Grimes 	 * If dirty, put on list of dirty buffers;
475df8bae1dSRodney W. Grimes 	 * otherwise insert onto list of clean buffers.
476df8bae1dSRodney W. Grimes 	 */
477df8bae1dSRodney W. Grimes 	if (bp->b_flags & B_DELWRI)
478df8bae1dSRodney W. Grimes 		listheadp = &newvp->v_dirtyblkhd;
479df8bae1dSRodney W. Grimes 	else
480df8bae1dSRodney W. Grimes 		listheadp = &newvp->v_cleanblkhd;
481df8bae1dSRodney W. Grimes 	bufinsvn(bp, listheadp);
482df8bae1dSRodney W. Grimes }
483df8bae1dSRodney W. Grimes 
484df8bae1dSRodney W. Grimes /*
485df8bae1dSRodney W. Grimes  * Create a vnode for a block device.
486df8bae1dSRodney W. Grimes  * Used for root filesystem, argdev, and swap areas.
487df8bae1dSRodney W. Grimes  * Also used for memory file system special devices.
488df8bae1dSRodney W. Grimes  */
489df8bae1dSRodney W. Grimes bdevvp(dev, vpp)
490df8bae1dSRodney W. Grimes 	dev_t dev;
491df8bae1dSRodney W. Grimes 	struct vnode **vpp;
492df8bae1dSRodney W. Grimes {
493df8bae1dSRodney W. Grimes 	register struct vnode *vp;
494df8bae1dSRodney W. Grimes 	struct vnode *nvp;
495df8bae1dSRodney W. Grimes 	int error;
496df8bae1dSRodney W. Grimes 
497df8bae1dSRodney W. Grimes 	if (dev == NODEV)
498df8bae1dSRodney W. Grimes 		return (0);
499df8bae1dSRodney W. Grimes 	error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp);
500df8bae1dSRodney W. Grimes 	if (error) {
501df8bae1dSRodney W. Grimes 		*vpp = 0;
502df8bae1dSRodney W. Grimes 		return (error);
503df8bae1dSRodney W. Grimes 	}
504df8bae1dSRodney W. Grimes 	vp = nvp;
505df8bae1dSRodney W. Grimes 	vp->v_type = VBLK;
506df8bae1dSRodney W. Grimes 	if (nvp = checkalias(vp, dev, (struct mount *)0)) {
507df8bae1dSRodney W. Grimes 		vput(vp);
508df8bae1dSRodney W. Grimes 		vp = nvp;
509df8bae1dSRodney W. Grimes 	}
510df8bae1dSRodney W. Grimes 	*vpp = vp;
511df8bae1dSRodney W. Grimes 	return (0);
512df8bae1dSRodney W. Grimes }
513df8bae1dSRodney W. Grimes 
514df8bae1dSRodney W. Grimes /*
515df8bae1dSRodney W. Grimes  * Check to see if the new vnode represents a special device
516df8bae1dSRodney W. Grimes  * for which we already have a vnode (either because of
517df8bae1dSRodney W. Grimes  * bdevvp() or because of a different vnode representing
518df8bae1dSRodney W. Grimes  * the same block device). If such an alias exists, deallocate
519df8bae1dSRodney W. Grimes  * the existing contents and return the aliased vnode. The
520df8bae1dSRodney W. Grimes  * caller is responsible for filling it with its new contents.
521df8bae1dSRodney W. Grimes  */
522df8bae1dSRodney W. Grimes struct vnode *
523df8bae1dSRodney W. Grimes checkalias(nvp, nvp_rdev, mp)
524df8bae1dSRodney W. Grimes 	register struct vnode *nvp;
525df8bae1dSRodney W. Grimes 	dev_t nvp_rdev;
526df8bae1dSRodney W. Grimes 	struct mount *mp;
527df8bae1dSRodney W. Grimes {
528df8bae1dSRodney W. Grimes 	register struct vnode *vp;
529df8bae1dSRodney W. Grimes 	struct vnode **vpp;
530df8bae1dSRodney W. Grimes 
531df8bae1dSRodney W. Grimes 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
532df8bae1dSRodney W. Grimes 		return (NULLVP);
533df8bae1dSRodney W. Grimes 
534df8bae1dSRodney W. Grimes 	vpp = &speclisth[SPECHASH(nvp_rdev)];
535df8bae1dSRodney W. Grimes loop:
536df8bae1dSRodney W. Grimes 	for (vp = *vpp; vp; vp = vp->v_specnext) {
537df8bae1dSRodney W. Grimes 		if (nvp_rdev != vp->v_rdev || nvp->v_type != vp->v_type)
538df8bae1dSRodney W. Grimes 			continue;
539df8bae1dSRodney W. Grimes 		/*
540df8bae1dSRodney W. Grimes 		 * Alias, but not in use, so flush it out.
541df8bae1dSRodney W. Grimes 		 */
542df8bae1dSRodney W. Grimes 		if (vp->v_usecount == 0) {
543df8bae1dSRodney W. Grimes 			vgone(vp);
544df8bae1dSRodney W. Grimes 			goto loop;
545df8bae1dSRodney W. Grimes 		}
546df8bae1dSRodney W. Grimes 		if (vget(vp, 1))
547df8bae1dSRodney W. Grimes 			goto loop;
548df8bae1dSRodney W. Grimes 		break;
549df8bae1dSRodney W. Grimes 	}
550df8bae1dSRodney W. Grimes 	if (vp == NULL || vp->v_tag != VT_NON) {
551df8bae1dSRodney W. Grimes 		MALLOC(nvp->v_specinfo, struct specinfo *,
552df8bae1dSRodney W. Grimes 			sizeof(struct specinfo), M_VNODE, M_WAITOK);
553df8bae1dSRodney W. Grimes 		nvp->v_rdev = nvp_rdev;
554df8bae1dSRodney W. Grimes 		nvp->v_hashchain = vpp;
555df8bae1dSRodney W. Grimes 		nvp->v_specnext = *vpp;
556df8bae1dSRodney W. Grimes 		nvp->v_specflags = 0;
557df8bae1dSRodney W. Grimes 		*vpp = nvp;
558df8bae1dSRodney W. Grimes 		if (vp != NULL) {
559df8bae1dSRodney W. Grimes 			nvp->v_flag |= VALIASED;
560df8bae1dSRodney W. Grimes 			vp->v_flag |= VALIASED;
561df8bae1dSRodney W. Grimes 			vput(vp);
562df8bae1dSRodney W. Grimes 		}
563df8bae1dSRodney W. Grimes 		return (NULLVP);
564df8bae1dSRodney W. Grimes 	}
565df8bae1dSRodney W. Grimes 	VOP_UNLOCK(vp);
566df8bae1dSRodney W. Grimes 	vclean(vp, 0);
567df8bae1dSRodney W. Grimes 	vp->v_op = nvp->v_op;
568df8bae1dSRodney W. Grimes 	vp->v_tag = nvp->v_tag;
569df8bae1dSRodney W. Grimes 	nvp->v_type = VNON;
570df8bae1dSRodney W. Grimes 	insmntque(vp, mp);
571df8bae1dSRodney W. Grimes 	return (vp);
572df8bae1dSRodney W. Grimes }
573df8bae1dSRodney W. Grimes 
574df8bae1dSRodney W. Grimes /*
575df8bae1dSRodney W. Grimes  * Grab a particular vnode from the free list, increment its
576df8bae1dSRodney W. Grimes  * reference count and lock it. The vnode lock bit is set the
577df8bae1dSRodney W. Grimes  * vnode is being eliminated in vgone. The process is awakened
578df8bae1dSRodney W. Grimes  * when the transition is completed, and an error returned to
579df8bae1dSRodney W. Grimes  * indicate that the vnode is no longer usable (possibly having
580df8bae1dSRodney W. Grimes  * been changed to a new file system type).
581df8bae1dSRodney W. Grimes  */
582df8bae1dSRodney W. Grimes vget(vp, lockflag)
583df8bae1dSRodney W. Grimes 	register struct vnode *vp;
584df8bae1dSRodney W. Grimes 	int lockflag;
585df8bae1dSRodney W. Grimes {
586df8bae1dSRodney W. Grimes 
587df8bae1dSRodney W. Grimes 	/*
588df8bae1dSRodney W. Grimes 	 * If the vnode is in the process of being cleaned out for
589df8bae1dSRodney W. Grimes 	 * another use, we wait for the cleaning to finish and then
590df8bae1dSRodney W. Grimes 	 * return failure. Cleaning is determined either by checking
591df8bae1dSRodney W. Grimes 	 * that the VXLOCK flag is set, or that the use count is
592df8bae1dSRodney W. Grimes 	 * zero with the back pointer set to show that it has been
593df8bae1dSRodney W. Grimes 	 * removed from the free list by getnewvnode. The VXLOCK
594df8bae1dSRodney W. Grimes 	 * flag may not have been set yet because vclean is blocked in
595df8bae1dSRodney W. Grimes 	 * the VOP_LOCK call waiting for the VOP_INACTIVE to complete.
596df8bae1dSRodney W. Grimes 	 */
597df8bae1dSRodney W. Grimes 	if ((vp->v_flag & VXLOCK) ||
598df8bae1dSRodney W. Grimes 	    (vp->v_usecount == 0 &&
599df8bae1dSRodney W. Grimes 	     vp->v_freelist.tqe_prev == (struct vnode **)0xdeadb)) {
600df8bae1dSRodney W. Grimes 		vp->v_flag |= VXWANT;
601df8bae1dSRodney W. Grimes 		sleep((caddr_t)vp, PINOD);
602df8bae1dSRodney W. Grimes 		return (1);
603df8bae1dSRodney W. Grimes 	}
604df8bae1dSRodney W. Grimes 	if (vp->v_usecount == 0)
605df8bae1dSRodney W. Grimes 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
606df8bae1dSRodney W. Grimes 	vp->v_usecount++;
607df8bae1dSRodney W. Grimes 	if (lockflag)
608df8bae1dSRodney W. Grimes 		VOP_LOCK(vp);
609df8bae1dSRodney W. Grimes 	return (0);
610df8bae1dSRodney W. Grimes }
611df8bae1dSRodney W. Grimes 
612df8bae1dSRodney W. Grimes /*
613df8bae1dSRodney W. Grimes  * Vnode reference, just increment the count
614df8bae1dSRodney W. Grimes  */
615df8bae1dSRodney W. Grimes void vref(vp)
616df8bae1dSRodney W. Grimes 	struct vnode *vp;
617df8bae1dSRodney W. Grimes {
618df8bae1dSRodney W. Grimes 
619df8bae1dSRodney W. Grimes 	if (vp->v_usecount <= 0)
620df8bae1dSRodney W. Grimes 		panic("vref used where vget required");
621df8bae1dSRodney W. Grimes 	vp->v_usecount++;
622df8bae1dSRodney W. Grimes }
623df8bae1dSRodney W. Grimes 
624df8bae1dSRodney W. Grimes /*
625df8bae1dSRodney W. Grimes  * vput(), just unlock and vrele()
626df8bae1dSRodney W. Grimes  */
627df8bae1dSRodney W. Grimes void vput(vp)
628df8bae1dSRodney W. Grimes 	register struct vnode *vp;
629df8bae1dSRodney W. Grimes {
630df8bae1dSRodney W. Grimes 
631df8bae1dSRodney W. Grimes 	VOP_UNLOCK(vp);
632df8bae1dSRodney W. Grimes 	vrele(vp);
633df8bae1dSRodney W. Grimes }
634df8bae1dSRodney W. Grimes 
635df8bae1dSRodney W. Grimes /*
636df8bae1dSRodney W. Grimes  * Vnode release.
637df8bae1dSRodney W. Grimes  * If count drops to zero, call inactive routine and return to freelist.
638df8bae1dSRodney W. Grimes  */
639df8bae1dSRodney W. Grimes void vrele(vp)
640df8bae1dSRodney W. Grimes 	register struct vnode *vp;
641df8bae1dSRodney W. Grimes {
642df8bae1dSRodney W. Grimes 
643df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
644df8bae1dSRodney W. Grimes 	if (vp == NULL)
645df8bae1dSRodney W. Grimes 		panic("vrele: null vp");
646df8bae1dSRodney W. Grimes #endif
647df8bae1dSRodney W. Grimes 	vp->v_usecount--;
648df8bae1dSRodney W. Grimes 	if (vp->v_usecount > 0)
649df8bae1dSRodney W. Grimes 		return;
650df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
651df8bae1dSRodney W. Grimes 	if (vp->v_usecount != 0 || vp->v_writecount != 0) {
652df8bae1dSRodney W. Grimes 		vprint("vrele: bad ref count", vp);
653df8bae1dSRodney W. Grimes 		panic("vrele: ref cnt");
654df8bae1dSRodney W. Grimes 	}
655df8bae1dSRodney W. Grimes #endif
656df8bae1dSRodney W. Grimes 	/*
657df8bae1dSRodney W. Grimes 	 * insert at tail of LRU list
658df8bae1dSRodney W. Grimes 	 */
659df8bae1dSRodney W. Grimes 	TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist);
660df8bae1dSRodney W. Grimes 	VOP_INACTIVE(vp);
661df8bae1dSRodney W. Grimes }
662df8bae1dSRodney W. Grimes 
663df8bae1dSRodney W. Grimes /*
664df8bae1dSRodney W. Grimes  * Page or buffer structure gets a reference.
665df8bae1dSRodney W. Grimes  */
666df8bae1dSRodney W. Grimes void vhold(vp)
667df8bae1dSRodney W. Grimes 	register struct vnode *vp;
668df8bae1dSRodney W. Grimes {
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 	vp->v_holdcnt++;
671df8bae1dSRodney W. Grimes }
672df8bae1dSRodney W. Grimes 
673df8bae1dSRodney W. Grimes /*
674df8bae1dSRodney W. Grimes  * Page or buffer structure frees a reference.
675df8bae1dSRodney W. Grimes  */
676df8bae1dSRodney W. Grimes void holdrele(vp)
677df8bae1dSRodney W. Grimes 	register struct vnode *vp;
678df8bae1dSRodney W. Grimes {
679df8bae1dSRodney W. Grimes 
680df8bae1dSRodney W. Grimes 	if (vp->v_holdcnt <= 0)
681df8bae1dSRodney W. Grimes 		panic("holdrele: holdcnt");
682df8bae1dSRodney W. Grimes 	vp->v_holdcnt--;
683df8bae1dSRodney W. Grimes }
684df8bae1dSRodney W. Grimes 
685df8bae1dSRodney W. Grimes /*
686df8bae1dSRodney W. Grimes  * Remove any vnodes in the vnode table belonging to mount point mp.
687df8bae1dSRodney W. Grimes  *
688df8bae1dSRodney W. Grimes  * If MNT_NOFORCE is specified, there should not be any active ones,
689df8bae1dSRodney W. Grimes  * return error if any are found (nb: this is a user error, not a
690df8bae1dSRodney W. Grimes  * system error). If MNT_FORCE is specified, detach any active vnodes
691df8bae1dSRodney W. Grimes  * that are found.
692df8bae1dSRodney W. Grimes  */
693df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
694df8bae1dSRodney W. Grimes int busyprt = 0;	/* print out busy vnodes */
695df8bae1dSRodney W. Grimes struct ctldebug debug1 = { "busyprt", &busyprt };
696df8bae1dSRodney W. Grimes #endif
697df8bae1dSRodney W. Grimes 
698df8bae1dSRodney W. Grimes vflush(mp, skipvp, flags)
699df8bae1dSRodney W. Grimes 	struct mount *mp;
700df8bae1dSRodney W. Grimes 	struct vnode *skipvp;
701df8bae1dSRodney W. Grimes 	int flags;
702df8bae1dSRodney W. Grimes {
703df8bae1dSRodney W. Grimes 	register struct vnode *vp, *nvp;
704df8bae1dSRodney W. Grimes 	int busy = 0;
705df8bae1dSRodney W. Grimes 
706df8bae1dSRodney W. Grimes 	if ((mp->mnt_flag & MNT_MPBUSY) == 0)
707df8bae1dSRodney W. Grimes 		panic("vflush: not busy");
708df8bae1dSRodney W. Grimes loop:
709df8bae1dSRodney W. Grimes 	for (vp = mp->mnt_vnodelist.lh_first; vp; vp = nvp) {
710df8bae1dSRodney W. Grimes 		if (vp->v_mount != mp)
711df8bae1dSRodney W. Grimes 			goto loop;
712df8bae1dSRodney W. Grimes 		nvp = vp->v_mntvnodes.le_next;
713df8bae1dSRodney W. Grimes 		/*
714df8bae1dSRodney W. Grimes 		 * Skip over a selected vnode.
715df8bae1dSRodney W. Grimes 		 */
716df8bae1dSRodney W. Grimes 		if (vp == skipvp)
717df8bae1dSRodney W. Grimes 			continue;
718df8bae1dSRodney W. Grimes 		/*
719df8bae1dSRodney W. Grimes 		 * Skip over a vnodes marked VSYSTEM.
720df8bae1dSRodney W. Grimes 		 */
721df8bae1dSRodney W. Grimes 		if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM))
722df8bae1dSRodney W. Grimes 			continue;
723df8bae1dSRodney W. Grimes 		/*
724df8bae1dSRodney W. Grimes 		 * If WRITECLOSE is set, only flush out regular file
725df8bae1dSRodney W. Grimes 		 * vnodes open for writing.
726df8bae1dSRodney W. Grimes 		 */
727df8bae1dSRodney W. Grimes 		if ((flags & WRITECLOSE) &&
728df8bae1dSRodney W. Grimes 		    (vp->v_writecount == 0 || vp->v_type != VREG))
729df8bae1dSRodney W. Grimes 			continue;
730df8bae1dSRodney W. Grimes 		/*
731df8bae1dSRodney W. Grimes 		 * With v_usecount == 0, all we need to do is clear
732df8bae1dSRodney W. Grimes 		 * out the vnode data structures and we are done.
733df8bae1dSRodney W. Grimes 		 */
734df8bae1dSRodney W. Grimes 		if (vp->v_usecount == 0) {
735df8bae1dSRodney W. Grimes 			vgone(vp);
736df8bae1dSRodney W. Grimes 			continue;
737df8bae1dSRodney W. Grimes 		}
738df8bae1dSRodney W. Grimes 		/*
739df8bae1dSRodney W. Grimes 		 * If FORCECLOSE is set, forcibly close the vnode.
740df8bae1dSRodney W. Grimes 		 * For block or character devices, revert to an
741df8bae1dSRodney W. Grimes 		 * anonymous device. For all other files, just kill them.
742df8bae1dSRodney W. Grimes 		 */
743df8bae1dSRodney W. Grimes 		if (flags & FORCECLOSE) {
744df8bae1dSRodney W. Grimes 			if (vp->v_type != VBLK && vp->v_type != VCHR) {
745df8bae1dSRodney W. Grimes 				vgone(vp);
746df8bae1dSRodney W. Grimes 			} else {
747df8bae1dSRodney W. Grimes 				vclean(vp, 0);
748df8bae1dSRodney W. Grimes 				vp->v_op = spec_vnodeop_p;
749df8bae1dSRodney W. Grimes 				insmntque(vp, (struct mount *)0);
750df8bae1dSRodney W. Grimes 			}
751df8bae1dSRodney W. Grimes 			continue;
752df8bae1dSRodney W. Grimes 		}
753df8bae1dSRodney W. Grimes #ifdef DIAGNOSTIC
754df8bae1dSRodney W. Grimes 		if (busyprt)
755df8bae1dSRodney W. Grimes 			vprint("vflush: busy vnode", vp);
756df8bae1dSRodney W. Grimes #endif
757df8bae1dSRodney W. Grimes 		busy++;
758df8bae1dSRodney W. Grimes 	}
759df8bae1dSRodney W. Grimes 	if (busy)
760df8bae1dSRodney W. Grimes 		return (EBUSY);
761df8bae1dSRodney W. Grimes 	return (0);
762df8bae1dSRodney W. Grimes }
763df8bae1dSRodney W. Grimes 
764df8bae1dSRodney W. Grimes /*
765df8bae1dSRodney W. Grimes  * Disassociate the underlying file system from a vnode.
766df8bae1dSRodney W. Grimes  */
767df8bae1dSRodney W. Grimes void
768df8bae1dSRodney W. Grimes vclean(vp, flags)
769df8bae1dSRodney W. Grimes 	register struct vnode *vp;
770df8bae1dSRodney W. Grimes 	int flags;
771df8bae1dSRodney W. Grimes {
772df8bae1dSRodney W. Grimes 	int active;
773df8bae1dSRodney W. Grimes 
774df8bae1dSRodney W. Grimes 	/*
775df8bae1dSRodney W. Grimes 	 * Check to see if the vnode is in use.
776df8bae1dSRodney W. Grimes 	 * If so we have to reference it before we clean it out
777df8bae1dSRodney W. Grimes 	 * so that its count cannot fall to zero and generate a
778df8bae1dSRodney W. Grimes 	 * race against ourselves to recycle it.
779df8bae1dSRodney W. Grimes 	 */
780df8bae1dSRodney W. Grimes 	if (active = vp->v_usecount)
781df8bae1dSRodney W. Grimes 		VREF(vp);
782df8bae1dSRodney W. Grimes 	/*
783df8bae1dSRodney W. Grimes 	 * Even if the count is zero, the VOP_INACTIVE routine may still
784df8bae1dSRodney W. Grimes 	 * have the object locked while it cleans it out. The VOP_LOCK
785df8bae1dSRodney W. Grimes 	 * ensures that the VOP_INACTIVE routine is done with its work.
786df8bae1dSRodney W. Grimes 	 * For active vnodes, it ensures that no other activity can
787df8bae1dSRodney W. Grimes 	 * occur while the underlying object is being cleaned out.
788df8bae1dSRodney W. Grimes 	 */
789df8bae1dSRodney W. Grimes 	VOP_LOCK(vp);
790df8bae1dSRodney W. Grimes 	/*
791df8bae1dSRodney W. Grimes 	 * Prevent the vnode from being recycled or
792df8bae1dSRodney W. Grimes 	 * brought into use while we clean it out.
793df8bae1dSRodney W. Grimes 	 */
794df8bae1dSRodney W. Grimes 	if (vp->v_flag & VXLOCK)
795df8bae1dSRodney W. Grimes 		panic("vclean: deadlock");
796df8bae1dSRodney W. Grimes 	vp->v_flag |= VXLOCK;
797df8bae1dSRodney W. Grimes 	/*
798df8bae1dSRodney W. Grimes 	 * Clean out any buffers associated with the vnode.
799df8bae1dSRodney W. Grimes 	 */
800df8bae1dSRodney W. Grimes 	if (flags & DOCLOSE)
801df8bae1dSRodney W. Grimes 		vinvalbuf(vp, V_SAVE, NOCRED, NULL, 0, 0);
802df8bae1dSRodney W. Grimes 	/*
803df8bae1dSRodney W. Grimes 	 * Any other processes trying to obtain this lock must first
804df8bae1dSRodney W. Grimes 	 * wait for VXLOCK to clear, then call the new lock operation.
805df8bae1dSRodney W. Grimes 	 */
806df8bae1dSRodney W. Grimes 	VOP_UNLOCK(vp);
807df8bae1dSRodney W. Grimes 	/*
808df8bae1dSRodney W. Grimes 	 * If purging an active vnode, it must be closed and
809df8bae1dSRodney W. Grimes 	 * deactivated before being reclaimed.
810df8bae1dSRodney W. Grimes 	 */
811df8bae1dSRodney W. Grimes 	if (active) {
812df8bae1dSRodney W. Grimes 		if (flags & DOCLOSE)
813df8bae1dSRodney W. Grimes 			VOP_CLOSE(vp, IO_NDELAY, NOCRED, NULL);
814df8bae1dSRodney W. Grimes 		VOP_INACTIVE(vp);
815df8bae1dSRodney W. Grimes 	}
816df8bae1dSRodney W. Grimes 	/*
817df8bae1dSRodney W. Grimes 	 * Reclaim the vnode.
818df8bae1dSRodney W. Grimes 	 */
819df8bae1dSRodney W. Grimes 	if (VOP_RECLAIM(vp))
820df8bae1dSRodney W. Grimes 		panic("vclean: cannot reclaim");
821df8bae1dSRodney W. Grimes 	if (active)
822df8bae1dSRodney W. Grimes 		vrele(vp);
823df8bae1dSRodney W. Grimes 
824df8bae1dSRodney W. Grimes 	/*
825df8bae1dSRodney W. Grimes 	 * Done with purge, notify sleepers of the grim news.
826df8bae1dSRodney W. Grimes 	 */
827df8bae1dSRodney W. Grimes 	vp->v_op = dead_vnodeop_p;
828df8bae1dSRodney W. Grimes 	vp->v_tag = VT_NON;
829df8bae1dSRodney W. Grimes 	vp->v_flag &= ~VXLOCK;
830df8bae1dSRodney W. Grimes 	if (vp->v_flag & VXWANT) {
831df8bae1dSRodney W. Grimes 		vp->v_flag &= ~VXWANT;
832df8bae1dSRodney W. Grimes 		wakeup((caddr_t)vp);
833df8bae1dSRodney W. Grimes 	}
834df8bae1dSRodney W. Grimes }
835df8bae1dSRodney W. Grimes 
836df8bae1dSRodney W. Grimes /*
837df8bae1dSRodney W. Grimes  * Eliminate all activity associated with  the requested vnode
838df8bae1dSRodney W. Grimes  * and with all vnodes aliased to the requested vnode.
839df8bae1dSRodney W. Grimes  */
840df8bae1dSRodney W. Grimes void vgoneall(vp)
841df8bae1dSRodney W. Grimes 	register struct vnode *vp;
842df8bae1dSRodney W. Grimes {
843df8bae1dSRodney W. Grimes 	register struct vnode *vq;
844df8bae1dSRodney W. Grimes 
845df8bae1dSRodney W. Grimes 	if (vp->v_flag & VALIASED) {
846df8bae1dSRodney W. Grimes 		/*
847df8bae1dSRodney W. Grimes 		 * If a vgone (or vclean) is already in progress,
848df8bae1dSRodney W. Grimes 		 * wait until it is done and return.
849df8bae1dSRodney W. Grimes 		 */
850df8bae1dSRodney W. Grimes 		if (vp->v_flag & VXLOCK) {
851df8bae1dSRodney W. Grimes 			vp->v_flag |= VXWANT;
852df8bae1dSRodney W. Grimes 			sleep((caddr_t)vp, PINOD);
853df8bae1dSRodney W. Grimes 			return;
854df8bae1dSRodney W. Grimes 		}
855df8bae1dSRodney W. Grimes 		/*
856df8bae1dSRodney W. Grimes 		 * Ensure that vp will not be vgone'd while we
857df8bae1dSRodney W. Grimes 		 * are eliminating its aliases.
858df8bae1dSRodney W. Grimes 		 */
859df8bae1dSRodney W. Grimes 		vp->v_flag |= VXLOCK;
860df8bae1dSRodney W. Grimes 		while (vp->v_flag & VALIASED) {
861df8bae1dSRodney W. Grimes 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
862df8bae1dSRodney W. Grimes 				if (vq->v_rdev != vp->v_rdev ||
863df8bae1dSRodney W. Grimes 				    vq->v_type != vp->v_type || vp == vq)
864df8bae1dSRodney W. Grimes 					continue;
865df8bae1dSRodney W. Grimes 				vgone(vq);
866df8bae1dSRodney W. Grimes 				break;
867df8bae1dSRodney W. Grimes 			}
868df8bae1dSRodney W. Grimes 		}
869df8bae1dSRodney W. Grimes 		/*
870df8bae1dSRodney W. Grimes 		 * Remove the lock so that vgone below will
871df8bae1dSRodney W. Grimes 		 * really eliminate the vnode after which time
872df8bae1dSRodney W. Grimes 		 * vgone will awaken any sleepers.
873df8bae1dSRodney W. Grimes 		 */
874df8bae1dSRodney W. Grimes 		vp->v_flag &= ~VXLOCK;
875df8bae1dSRodney W. Grimes 	}
876df8bae1dSRodney W. Grimes 	vgone(vp);
877df8bae1dSRodney W. Grimes }
878df8bae1dSRodney W. Grimes 
879df8bae1dSRodney W. Grimes /*
880df8bae1dSRodney W. Grimes  * Eliminate all activity associated with a vnode
881df8bae1dSRodney W. Grimes  * in preparation for reuse.
882df8bae1dSRodney W. Grimes  */
883df8bae1dSRodney W. Grimes void vgone(vp)
884df8bae1dSRodney W. Grimes 	register struct vnode *vp;
885df8bae1dSRodney W. Grimes {
886df8bae1dSRodney W. Grimes 	register struct vnode *vq;
887df8bae1dSRodney W. Grimes 	struct vnode *vx;
888df8bae1dSRodney W. Grimes 
889df8bae1dSRodney W. Grimes 	/*
890df8bae1dSRodney W. Grimes 	 * If a vgone (or vclean) is already in progress,
891df8bae1dSRodney W. Grimes 	 * wait until it is done and return.
892df8bae1dSRodney W. Grimes 	 */
893df8bae1dSRodney W. Grimes 	if (vp->v_flag & VXLOCK) {
894df8bae1dSRodney W. Grimes 		vp->v_flag |= VXWANT;
895df8bae1dSRodney W. Grimes 		sleep((caddr_t)vp, PINOD);
896df8bae1dSRodney W. Grimes 		return;
897df8bae1dSRodney W. Grimes 	}
898df8bae1dSRodney W. Grimes 	/*
899df8bae1dSRodney W. Grimes 	 * Clean out the filesystem specific data.
900df8bae1dSRodney W. Grimes 	 */
901df8bae1dSRodney W. Grimes 	vclean(vp, DOCLOSE);
902df8bae1dSRodney W. Grimes 	/*
903df8bae1dSRodney W. Grimes 	 * Delete from old mount point vnode list, if on one.
904df8bae1dSRodney W. Grimes 	 */
905df8bae1dSRodney W. Grimes 	if (vp->v_mount != NULL) {
906df8bae1dSRodney W. Grimes 		LIST_REMOVE(vp, v_mntvnodes);
907df8bae1dSRodney W. Grimes 		vp->v_mount = NULL;
908df8bae1dSRodney W. Grimes 	}
909df8bae1dSRodney W. Grimes 	/*
910df8bae1dSRodney W. Grimes 	 * If special device, remove it from special device alias list.
911df8bae1dSRodney W. Grimes 	 */
912df8bae1dSRodney W. Grimes 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
913df8bae1dSRodney W. Grimes 		if (*vp->v_hashchain == vp) {
914df8bae1dSRodney W. Grimes 			*vp->v_hashchain = vp->v_specnext;
915df8bae1dSRodney W. Grimes 		} else {
916df8bae1dSRodney W. Grimes 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
917df8bae1dSRodney W. Grimes 				if (vq->v_specnext != vp)
918df8bae1dSRodney W. Grimes 					continue;
919df8bae1dSRodney W. Grimes 				vq->v_specnext = vp->v_specnext;
920df8bae1dSRodney W. Grimes 				break;
921df8bae1dSRodney W. Grimes 			}
922df8bae1dSRodney W. Grimes 			if (vq == NULL)
923df8bae1dSRodney W. Grimes 				panic("missing bdev");
924df8bae1dSRodney W. Grimes 		}
925df8bae1dSRodney W. Grimes 		if (vp->v_flag & VALIASED) {
926df8bae1dSRodney W. Grimes 			vx = NULL;
927df8bae1dSRodney W. Grimes 			for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
928df8bae1dSRodney W. Grimes 				if (vq->v_rdev != vp->v_rdev ||
929df8bae1dSRodney W. Grimes 				    vq->v_type != vp->v_type)
930df8bae1dSRodney W. Grimes 					continue;
931df8bae1dSRodney W. Grimes 				if (vx)
932df8bae1dSRodney W. Grimes 					break;
933df8bae1dSRodney W. Grimes 				vx = vq;
934df8bae1dSRodney W. Grimes 			}
935df8bae1dSRodney W. Grimes 			if (vx == NULL)
936df8bae1dSRodney W. Grimes 				panic("missing alias");
937df8bae1dSRodney W. Grimes 			if (vq == NULL)
938df8bae1dSRodney W. Grimes 				vx->v_flag &= ~VALIASED;
939df8bae1dSRodney W. Grimes 			vp->v_flag &= ~VALIASED;
940df8bae1dSRodney W. Grimes 		}
941df8bae1dSRodney W. Grimes 		FREE(vp->v_specinfo, M_VNODE);
942df8bae1dSRodney W. Grimes 		vp->v_specinfo = NULL;
943df8bae1dSRodney W. Grimes 	}
944df8bae1dSRodney W. Grimes 	/*
945df8bae1dSRodney W. Grimes 	 * If it is on the freelist and not already at the head,
946df8bae1dSRodney W. Grimes 	 * move it to the head of the list. The test of the back
947df8bae1dSRodney W. Grimes 	 * pointer and the reference count of zero is because
948df8bae1dSRodney W. Grimes 	 * it will be removed from the free list by getnewvnode,
949df8bae1dSRodney W. Grimes 	 * but will not have its reference count incremented until
950df8bae1dSRodney W. Grimes 	 * after calling vgone. If the reference count were
951df8bae1dSRodney W. Grimes 	 * incremented first, vgone would (incorrectly) try to
952df8bae1dSRodney W. Grimes 	 * close the previous instance of the underlying object.
953df8bae1dSRodney W. Grimes 	 * So, the back pointer is explicitly set to `0xdeadb' in
954df8bae1dSRodney W. Grimes 	 * getnewvnode after removing it from the freelist to ensure
955df8bae1dSRodney W. Grimes 	 * that we do not try to move it here.
956df8bae1dSRodney W. Grimes 	 */
957df8bae1dSRodney W. Grimes 	if (vp->v_usecount == 0 &&
958df8bae1dSRodney W. Grimes 	    vp->v_freelist.tqe_prev != (struct vnode **)0xdeadb &&
959df8bae1dSRodney W. Grimes 	    vnode_free_list.tqh_first != vp) {
960df8bae1dSRodney W. Grimes 		TAILQ_REMOVE(&vnode_free_list, vp, v_freelist);
961df8bae1dSRodney W. Grimes 		TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist);
962df8bae1dSRodney W. Grimes 	}
963df8bae1dSRodney W. Grimes 	vp->v_type = VBAD;
964df8bae1dSRodney W. Grimes }
965df8bae1dSRodney W. Grimes 
966df8bae1dSRodney W. Grimes /*
967df8bae1dSRodney W. Grimes  * Lookup a vnode by device number.
968df8bae1dSRodney W. Grimes  */
969df8bae1dSRodney W. Grimes vfinddev(dev, type, vpp)
970df8bae1dSRodney W. Grimes 	dev_t dev;
971df8bae1dSRodney W. Grimes 	enum vtype type;
972df8bae1dSRodney W. Grimes 	struct vnode **vpp;
973df8bae1dSRodney W. Grimes {
974df8bae1dSRodney W. Grimes 	register struct vnode *vp;
975df8bae1dSRodney W. Grimes 
976df8bae1dSRodney W. Grimes 	for (vp = speclisth[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
977df8bae1dSRodney W. Grimes 		if (dev != vp->v_rdev || type != vp->v_type)
978df8bae1dSRodney W. Grimes 			continue;
979df8bae1dSRodney W. Grimes 		*vpp = vp;
980df8bae1dSRodney W. Grimes 		return (1);
981df8bae1dSRodney W. Grimes 	}
982df8bae1dSRodney W. Grimes 	return (0);
983df8bae1dSRodney W. Grimes }
984df8bae1dSRodney W. Grimes 
985df8bae1dSRodney W. Grimes /*
986df8bae1dSRodney W. Grimes  * Calculate the total number of references to a special device.
987df8bae1dSRodney W. Grimes  */
988df8bae1dSRodney W. Grimes vcount(vp)
989df8bae1dSRodney W. Grimes 	register struct vnode *vp;
990df8bae1dSRodney W. Grimes {
991df8bae1dSRodney W. Grimes 	register struct vnode *vq, *vnext;
992df8bae1dSRodney W. Grimes 	int count;
993df8bae1dSRodney W. Grimes 
994df8bae1dSRodney W. Grimes loop:
995df8bae1dSRodney W. Grimes 	if ((vp->v_flag & VALIASED) == 0)
996df8bae1dSRodney W. Grimes 		return (vp->v_usecount);
997df8bae1dSRodney W. Grimes 	for (count = 0, vq = *vp->v_hashchain; vq; vq = vnext) {
998df8bae1dSRodney W. Grimes 		vnext = vq->v_specnext;
999df8bae1dSRodney W. Grimes 		if (vq->v_rdev != vp->v_rdev || vq->v_type != vp->v_type)
1000df8bae1dSRodney W. Grimes 			continue;
1001df8bae1dSRodney W. Grimes 		/*
1002df8bae1dSRodney W. Grimes 		 * Alias, but not in use, so flush it out.
1003df8bae1dSRodney W. Grimes 		 */
1004df8bae1dSRodney W. Grimes 		if (vq->v_usecount == 0 && vq != vp) {
1005df8bae1dSRodney W. Grimes 			vgone(vq);
1006df8bae1dSRodney W. Grimes 			goto loop;
1007df8bae1dSRodney W. Grimes 		}
1008df8bae1dSRodney W. Grimes 		count += vq->v_usecount;
1009df8bae1dSRodney W. Grimes 	}
1010df8bae1dSRodney W. Grimes 	return (count);
1011df8bae1dSRodney W. Grimes }
1012df8bae1dSRodney W. Grimes 
1013df8bae1dSRodney W. Grimes /*
1014df8bae1dSRodney W. Grimes  * Print out a description of a vnode.
1015df8bae1dSRodney W. Grimes  */
1016df8bae1dSRodney W. Grimes static char *typename[] =
1017df8bae1dSRodney W. Grimes    { "VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD" };
1018df8bae1dSRodney W. Grimes 
1019df8bae1dSRodney W. Grimes vprint(label, vp)
1020df8bae1dSRodney W. Grimes 	char *label;
1021df8bae1dSRodney W. Grimes 	register struct vnode *vp;
1022df8bae1dSRodney W. Grimes {
1023df8bae1dSRodney W. Grimes 	char buf[64];
1024df8bae1dSRodney W. Grimes 
1025df8bae1dSRodney W. Grimes 	if (label != NULL)
1026df8bae1dSRodney W. Grimes 		printf("%s: ", label);
1027df8bae1dSRodney W. Grimes 	printf("type %s, usecount %d, writecount %d, refcount %d,",
1028df8bae1dSRodney W. Grimes 		typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1029df8bae1dSRodney W. Grimes 		vp->v_holdcnt);
1030df8bae1dSRodney W. Grimes 	buf[0] = '\0';
1031df8bae1dSRodney W. Grimes 	if (vp->v_flag & VROOT)
1032df8bae1dSRodney W. Grimes 		strcat(buf, "|VROOT");
1033df8bae1dSRodney W. Grimes 	if (vp->v_flag & VTEXT)
1034df8bae1dSRodney W. Grimes 		strcat(buf, "|VTEXT");
1035df8bae1dSRodney W. Grimes 	if (vp->v_flag & VSYSTEM)
1036df8bae1dSRodney W. Grimes 		strcat(buf, "|VSYSTEM");
1037df8bae1dSRodney W. Grimes 	if (vp->v_flag & VXLOCK)
1038df8bae1dSRodney W. Grimes 		strcat(buf, "|VXLOCK");
1039df8bae1dSRodney W. Grimes 	if (vp->v_flag & VXWANT)
1040df8bae1dSRodney W. Grimes 		strcat(buf, "|VXWANT");
1041df8bae1dSRodney W. Grimes 	if (vp->v_flag & VBWAIT)
1042df8bae1dSRodney W. Grimes 		strcat(buf, "|VBWAIT");
1043df8bae1dSRodney W. Grimes 	if (vp->v_flag & VALIASED)
1044df8bae1dSRodney W. Grimes 		strcat(buf, "|VALIASED");
1045df8bae1dSRodney W. Grimes 	if (buf[0] != '\0')
1046df8bae1dSRodney W. Grimes 		printf(" flags (%s)", &buf[1]);
1047df8bae1dSRodney W. Grimes 	if (vp->v_data == NULL) {
1048df8bae1dSRodney W. Grimes 		printf("\n");
1049df8bae1dSRodney W. Grimes 	} else {
1050df8bae1dSRodney W. Grimes 		printf("\n\t");
1051df8bae1dSRodney W. Grimes 		VOP_PRINT(vp);
1052df8bae1dSRodney W. Grimes 	}
1053df8bae1dSRodney W. Grimes }
1054df8bae1dSRodney W. Grimes 
1055df8bae1dSRodney W. Grimes #ifdef DEBUG
1056df8bae1dSRodney W. Grimes /*
1057df8bae1dSRodney W. Grimes  * List all of the locked vnodes in the system.
1058df8bae1dSRodney W. Grimes  * Called when debugging the kernel.
1059df8bae1dSRodney W. Grimes  */
1060df8bae1dSRodney W. Grimes printlockedvnodes()
1061df8bae1dSRodney W. Grimes {
1062df8bae1dSRodney W. Grimes 	register struct mount *mp;
1063df8bae1dSRodney W. Grimes 	register struct vnode *vp;
1064df8bae1dSRodney W. Grimes 
1065df8bae1dSRodney W. Grimes 	printf("Locked vnodes\n");
1066df8bae1dSRodney W. Grimes 	for (mp = mountlist.tqh_first; mp != NULL; mp = mp->mnt_list.tqe_next) {
1067df8bae1dSRodney W. Grimes 		for (vp = mp->mnt_vnodelist.lh_first;
1068df8bae1dSRodney W. Grimes 		     vp != NULL;
1069df8bae1dSRodney W. Grimes 		     vp = vp->v_mntvnodes.le_next)
1070df8bae1dSRodney W. Grimes 			if (VOP_ISLOCKED(vp))
1071df8bae1dSRodney W. Grimes 				vprint((char *)0, vp);
1072df8bae1dSRodney W. Grimes 	}
1073df8bae1dSRodney W. Grimes }
1074df8bae1dSRodney W. Grimes #endif
1075df8bae1dSRodney W. Grimes 
1076df8bae1dSRodney W. Grimes int kinfo_vdebug = 1;
1077df8bae1dSRodney W. Grimes int kinfo_vgetfailed;
1078df8bae1dSRodney W. Grimes #define KINFO_VNODESLOP	10
1079df8bae1dSRodney W. Grimes /*
1080df8bae1dSRodney W. Grimes  * Dump vnode list (via sysctl).
1081df8bae1dSRodney W. Grimes  * Copyout address of vnode followed by vnode.
1082df8bae1dSRodney W. Grimes  */
1083df8bae1dSRodney W. Grimes /* ARGSUSED */
1084df8bae1dSRodney W. Grimes sysctl_vnode(where, sizep)
1085df8bae1dSRodney W. Grimes 	char *where;
1086df8bae1dSRodney W. Grimes 	size_t *sizep;
1087df8bae1dSRodney W. Grimes {
1088df8bae1dSRodney W. Grimes 	register struct mount *mp, *nmp;
1089df8bae1dSRodney W. Grimes 	struct vnode *vp;
1090df8bae1dSRodney W. Grimes 	register char *bp = where, *savebp;
1091df8bae1dSRodney W. Grimes 	char *ewhere;
1092df8bae1dSRodney W. Grimes 	int error;
1093df8bae1dSRodney W. Grimes 
1094df8bae1dSRodney W. Grimes #define VPTRSZ	sizeof (struct vnode *)
1095df8bae1dSRodney W. Grimes #define VNODESZ	sizeof (struct vnode)
1096df8bae1dSRodney W. Grimes 	if (where == NULL) {
1097df8bae1dSRodney W. Grimes 		*sizep = (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ);
1098df8bae1dSRodney W. Grimes 		return (0);
1099df8bae1dSRodney W. Grimes 	}
1100df8bae1dSRodney W. Grimes 	ewhere = where + *sizep;
1101df8bae1dSRodney W. Grimes 
1102df8bae1dSRodney W. Grimes 	for (mp = mountlist.tqh_first; mp != NULL; mp = nmp) {
1103df8bae1dSRodney W. Grimes 		nmp = mp->mnt_list.tqe_next;
1104df8bae1dSRodney W. Grimes 		if (vfs_busy(mp))
1105df8bae1dSRodney W. Grimes 			continue;
1106df8bae1dSRodney W. Grimes 		savebp = bp;
1107df8bae1dSRodney W. Grimes again:
1108df8bae1dSRodney W. Grimes 		for (vp = mp->mnt_vnodelist.lh_first;
1109df8bae1dSRodney W. Grimes 		     vp != NULL;
1110df8bae1dSRodney W. Grimes 		     vp = vp->v_mntvnodes.le_next) {
1111df8bae1dSRodney W. Grimes 			/*
1112df8bae1dSRodney W. Grimes 			 * Check that the vp is still associated with
1113df8bae1dSRodney W. Grimes 			 * this filesystem.  RACE: could have been
1114df8bae1dSRodney W. Grimes 			 * recycled onto the same filesystem.
1115df8bae1dSRodney W. Grimes 			 */
1116df8bae1dSRodney W. Grimes 			if (vp->v_mount != mp) {
1117df8bae1dSRodney W. Grimes 				if (kinfo_vdebug)
1118df8bae1dSRodney W. Grimes 					printf("kinfo: vp changed\n");
1119df8bae1dSRodney W. Grimes 				bp = savebp;
1120df8bae1dSRodney W. Grimes 				goto again;
1121df8bae1dSRodney W. Grimes 			}
1122df8bae1dSRodney W. Grimes 			if (bp + VPTRSZ + VNODESZ > ewhere) {
1123df8bae1dSRodney W. Grimes 				*sizep = bp - where;
1124df8bae1dSRodney W. Grimes 				return (ENOMEM);
1125df8bae1dSRodney W. Grimes 			}
1126df8bae1dSRodney W. Grimes 			if ((error = copyout((caddr_t)&vp, bp, VPTRSZ)) ||
1127df8bae1dSRodney W. Grimes 			   (error = copyout((caddr_t)vp, bp + VPTRSZ, VNODESZ)))
1128df8bae1dSRodney W. Grimes 				return (error);
1129df8bae1dSRodney W. Grimes 			bp += VPTRSZ + VNODESZ;
1130df8bae1dSRodney W. Grimes 		}
1131df8bae1dSRodney W. Grimes 		vfs_unbusy(mp);
1132df8bae1dSRodney W. Grimes 	}
1133df8bae1dSRodney W. Grimes 
1134df8bae1dSRodney W. Grimes 	*sizep = bp - where;
1135df8bae1dSRodney W. Grimes 	return (0);
1136df8bae1dSRodney W. Grimes }
1137df8bae1dSRodney W. Grimes 
1138df8bae1dSRodney W. Grimes /*
1139df8bae1dSRodney W. Grimes  * Check to see if a filesystem is mounted on a block device.
1140df8bae1dSRodney W. Grimes  */
1141df8bae1dSRodney W. Grimes int
1142df8bae1dSRodney W. Grimes vfs_mountedon(vp)
1143df8bae1dSRodney W. Grimes 	register struct vnode *vp;
1144df8bae1dSRodney W. Grimes {
1145df8bae1dSRodney W. Grimes 	register struct vnode *vq;
1146df8bae1dSRodney W. Grimes 
1147df8bae1dSRodney W. Grimes 	if (vp->v_specflags & SI_MOUNTEDON)
1148df8bae1dSRodney W. Grimes 		return (EBUSY);
1149df8bae1dSRodney W. Grimes 	if (vp->v_flag & VALIASED) {
1150df8bae1dSRodney W. Grimes 		for (vq = *vp->v_hashchain; vq; vq = vq->v_specnext) {
1151df8bae1dSRodney W. Grimes 			if (vq->v_rdev != vp->v_rdev ||
1152df8bae1dSRodney W. Grimes 			    vq->v_type != vp->v_type)
1153df8bae1dSRodney W. Grimes 				continue;
1154df8bae1dSRodney W. Grimes 			if (vq->v_specflags & SI_MOUNTEDON)
1155df8bae1dSRodney W. Grimes 				return (EBUSY);
1156df8bae1dSRodney W. Grimes 		}
1157df8bae1dSRodney W. Grimes 	}
1158df8bae1dSRodney W. Grimes 	return (0);
1159df8bae1dSRodney W. Grimes }
1160df8bae1dSRodney W. Grimes 
1161df8bae1dSRodney W. Grimes /*
1162df8bae1dSRodney W. Grimes  * Build hash lists of net addresses and hang them off the mount point.
1163df8bae1dSRodney W. Grimes  * Called by ufs_mount() to set up the lists of export addresses.
1164df8bae1dSRodney W. Grimes  */
1165df8bae1dSRodney W. Grimes static int
1166df8bae1dSRodney W. Grimes vfs_hang_addrlist(mp, nep, argp)
1167df8bae1dSRodney W. Grimes 	struct mount *mp;
1168df8bae1dSRodney W. Grimes 	struct netexport *nep;
1169df8bae1dSRodney W. Grimes 	struct export_args *argp;
1170df8bae1dSRodney W. Grimes {
1171df8bae1dSRodney W. Grimes 	register struct netcred *np;
1172df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
1173df8bae1dSRodney W. Grimes 	register int i;
1174df8bae1dSRodney W. Grimes 	struct radix_node *rn;
1175df8bae1dSRodney W. Grimes 	struct sockaddr *saddr, *smask = 0;
1176df8bae1dSRodney W. Grimes 	struct domain *dom;
1177df8bae1dSRodney W. Grimes 	int error;
1178df8bae1dSRodney W. Grimes 
1179df8bae1dSRodney W. Grimes 	if (argp->ex_addrlen == 0) {
1180df8bae1dSRodney W. Grimes 		if (mp->mnt_flag & MNT_DEFEXPORTED)
1181df8bae1dSRodney W. Grimes 			return (EPERM);
1182df8bae1dSRodney W. Grimes 		np = &nep->ne_defexported;
1183df8bae1dSRodney W. Grimes 		np->netc_exflags = argp->ex_flags;
1184df8bae1dSRodney W. Grimes 		np->netc_anon = argp->ex_anon;
1185df8bae1dSRodney W. Grimes 		np->netc_anon.cr_ref = 1;
1186df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_DEFEXPORTED;
1187df8bae1dSRodney W. Grimes 		return (0);
1188df8bae1dSRodney W. Grimes 	}
1189df8bae1dSRodney W. Grimes 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1190df8bae1dSRodney W. Grimes 	np = (struct netcred *)malloc(i, M_NETADDR, M_WAITOK);
1191df8bae1dSRodney W. Grimes 	bzero((caddr_t)np, i);
1192df8bae1dSRodney W. Grimes 	saddr = (struct sockaddr *)(np + 1);
1193df8bae1dSRodney W. Grimes 	if (error = copyin(argp->ex_addr, (caddr_t)saddr, argp->ex_addrlen))
1194df8bae1dSRodney W. Grimes 		goto out;
1195df8bae1dSRodney W. Grimes 	if (saddr->sa_len > argp->ex_addrlen)
1196df8bae1dSRodney W. Grimes 		saddr->sa_len = argp->ex_addrlen;
1197df8bae1dSRodney W. Grimes 	if (argp->ex_masklen) {
1198df8bae1dSRodney W. Grimes 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1199df8bae1dSRodney W. Grimes 		error = copyin(argp->ex_addr, (caddr_t)smask, argp->ex_masklen);
1200df8bae1dSRodney W. Grimes 		if (error)
1201df8bae1dSRodney W. Grimes 			goto out;
1202df8bae1dSRodney W. Grimes 		if (smask->sa_len > argp->ex_masklen)
1203df8bae1dSRodney W. Grimes 			smask->sa_len = argp->ex_masklen;
1204df8bae1dSRodney W. Grimes 	}
1205df8bae1dSRodney W. Grimes 	i = saddr->sa_family;
1206df8bae1dSRodney W. Grimes 	if ((rnh = nep->ne_rtable[i]) == 0) {
1207df8bae1dSRodney W. Grimes 		/*
1208df8bae1dSRodney W. Grimes 		 * Seems silly to initialize every AF when most are not
1209df8bae1dSRodney W. Grimes 		 * used, do so on demand here
1210df8bae1dSRodney W. Grimes 		 */
1211df8bae1dSRodney W. Grimes 		for (dom = domains; dom; dom = dom->dom_next)
1212df8bae1dSRodney W. Grimes 			if (dom->dom_family == i && dom->dom_rtattach) {
1213df8bae1dSRodney W. Grimes 				dom->dom_rtattach((void **)&nep->ne_rtable[i],
1214df8bae1dSRodney W. Grimes 					dom->dom_rtoffset);
1215df8bae1dSRodney W. Grimes 				break;
1216df8bae1dSRodney W. Grimes 			}
1217df8bae1dSRodney W. Grimes 		if ((rnh = nep->ne_rtable[i]) == 0) {
1218df8bae1dSRodney W. Grimes 			error = ENOBUFS;
1219df8bae1dSRodney W. Grimes 			goto out;
1220df8bae1dSRodney W. Grimes 		}
1221df8bae1dSRodney W. Grimes 	}
1222df8bae1dSRodney W. Grimes 	rn = (*rnh->rnh_addaddr)((caddr_t)saddr, (caddr_t)smask, rnh,
1223df8bae1dSRodney W. Grimes 		np->netc_rnodes);
1224df8bae1dSRodney W. Grimes 	if (rn == 0 || np != (struct netcred *)rn) { /* already exists */
1225df8bae1dSRodney W. Grimes 		error = EPERM;
1226df8bae1dSRodney W. Grimes 		goto out;
1227df8bae1dSRodney W. Grimes 	}
1228df8bae1dSRodney W. Grimes 	np->netc_exflags = argp->ex_flags;
1229df8bae1dSRodney W. Grimes 	np->netc_anon = argp->ex_anon;
1230df8bae1dSRodney W. Grimes 	np->netc_anon.cr_ref = 1;
1231df8bae1dSRodney W. Grimes 	return (0);
1232df8bae1dSRodney W. Grimes out:
1233df8bae1dSRodney W. Grimes 	free(np, M_NETADDR);
1234df8bae1dSRodney W. Grimes 	return (error);
1235df8bae1dSRodney W. Grimes }
1236df8bae1dSRodney W. Grimes 
1237df8bae1dSRodney W. Grimes /* ARGSUSED */
1238df8bae1dSRodney W. Grimes static int
1239df8bae1dSRodney W. Grimes vfs_free_netcred(rn, w)
1240df8bae1dSRodney W. Grimes 	struct radix_node *rn;
1241df8bae1dSRodney W. Grimes 	caddr_t w;
1242df8bae1dSRodney W. Grimes {
1243df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh = (struct radix_node_head *)w;
1244df8bae1dSRodney W. Grimes 
1245df8bae1dSRodney W. Grimes 	(*rnh->rnh_deladdr)(rn->rn_key, rn->rn_mask, rnh);
1246df8bae1dSRodney W. Grimes 	free((caddr_t)rn, M_NETADDR);
1247df8bae1dSRodney W. Grimes 	return (0);
1248df8bae1dSRodney W. Grimes }
1249df8bae1dSRodney W. Grimes 
1250df8bae1dSRodney W. Grimes /*
1251df8bae1dSRodney W. Grimes  * Free the net address hash lists that are hanging off the mount points.
1252df8bae1dSRodney W. Grimes  */
1253df8bae1dSRodney W. Grimes static void
1254df8bae1dSRodney W. Grimes vfs_free_addrlist(nep)
1255df8bae1dSRodney W. Grimes 	struct netexport *nep;
1256df8bae1dSRodney W. Grimes {
1257df8bae1dSRodney W. Grimes 	register int i;
1258df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
1259df8bae1dSRodney W. Grimes 
1260df8bae1dSRodney W. Grimes 	for (i = 0; i <= AF_MAX; i++)
1261df8bae1dSRodney W. Grimes 		if (rnh = nep->ne_rtable[i]) {
1262df8bae1dSRodney W. Grimes 			(*rnh->rnh_walktree)(rnh, vfs_free_netcred,
1263df8bae1dSRodney W. Grimes 			    (caddr_t)rnh);
1264df8bae1dSRodney W. Grimes 			free((caddr_t)rnh, M_RTABLE);
1265df8bae1dSRodney W. Grimes 			nep->ne_rtable[i] = 0;
1266df8bae1dSRodney W. Grimes 		}
1267df8bae1dSRodney W. Grimes }
1268df8bae1dSRodney W. Grimes 
1269df8bae1dSRodney W. Grimes int
1270df8bae1dSRodney W. Grimes vfs_export(mp, nep, argp)
1271df8bae1dSRodney W. Grimes 	struct mount *mp;
1272df8bae1dSRodney W. Grimes 	struct netexport *nep;
1273df8bae1dSRodney W. Grimes 	struct export_args *argp;
1274df8bae1dSRodney W. Grimes {
1275df8bae1dSRodney W. Grimes 	int error;
1276df8bae1dSRodney W. Grimes 
1277df8bae1dSRodney W. Grimes 	if (argp->ex_flags & MNT_DELEXPORT) {
1278df8bae1dSRodney W. Grimes 		vfs_free_addrlist(nep);
1279df8bae1dSRodney W. Grimes 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1280df8bae1dSRodney W. Grimes 	}
1281df8bae1dSRodney W. Grimes 	if (argp->ex_flags & MNT_EXPORTED) {
1282df8bae1dSRodney W. Grimes 		if (error = vfs_hang_addrlist(mp, nep, argp))
1283df8bae1dSRodney W. Grimes 			return (error);
1284df8bae1dSRodney W. Grimes 		mp->mnt_flag |= MNT_EXPORTED;
1285df8bae1dSRodney W. Grimes 	}
1286df8bae1dSRodney W. Grimes 	return (0);
1287df8bae1dSRodney W. Grimes }
1288df8bae1dSRodney W. Grimes 
1289df8bae1dSRodney W. Grimes struct netcred *
1290df8bae1dSRodney W. Grimes vfs_export_lookup(mp, nep, nam)
1291df8bae1dSRodney W. Grimes 	register struct mount *mp;
1292df8bae1dSRodney W. Grimes 	struct netexport *nep;
1293df8bae1dSRodney W. Grimes 	struct mbuf *nam;
1294df8bae1dSRodney W. Grimes {
1295df8bae1dSRodney W. Grimes 	register struct netcred *np;
1296df8bae1dSRodney W. Grimes 	register struct radix_node_head *rnh;
1297df8bae1dSRodney W. Grimes 	struct sockaddr *saddr;
1298df8bae1dSRodney W. Grimes 
1299df8bae1dSRodney W. Grimes 	np = NULL;
1300df8bae1dSRodney W. Grimes 	if (mp->mnt_flag & MNT_EXPORTED) {
1301df8bae1dSRodney W. Grimes 		/*
1302df8bae1dSRodney W. Grimes 		 * Lookup in the export list first.
1303df8bae1dSRodney W. Grimes 		 */
1304df8bae1dSRodney W. Grimes 		if (nam != NULL) {
1305df8bae1dSRodney W. Grimes 			saddr = mtod(nam, struct sockaddr *);
1306df8bae1dSRodney W. Grimes 			rnh = nep->ne_rtable[saddr->sa_family];
1307df8bae1dSRodney W. Grimes 			if (rnh != NULL) {
1308df8bae1dSRodney W. Grimes 				np = (struct netcred *)
1309df8bae1dSRodney W. Grimes 					(*rnh->rnh_matchaddr)((caddr_t)saddr,
1310df8bae1dSRodney W. Grimes 							      rnh);
1311df8bae1dSRodney W. Grimes 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1312df8bae1dSRodney W. Grimes 					np = NULL;
1313df8bae1dSRodney W. Grimes 			}
1314df8bae1dSRodney W. Grimes 		}
1315df8bae1dSRodney W. Grimes 		/*
1316df8bae1dSRodney W. Grimes 		 * If no address match, use the default if it exists.
1317df8bae1dSRodney W. Grimes 		 */
1318df8bae1dSRodney W. Grimes 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1319df8bae1dSRodney W. Grimes 			np = &nep->ne_defexported;
1320df8bae1dSRodney W. Grimes 	}
1321df8bae1dSRodney W. Grimes 	return (np);
1322df8bae1dSRodney W. Grimes }
1323