xref: /freebsd/sys/fs/nullfs/null_subr.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
1 /*
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software donated to Berkeley by
6  * Jan-Simon Pendry.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)null_subr.c	8.4 (Berkeley) 1/21/94
37  *
38  * $Id: lofs_subr.c,v 1.11 1992/05/30 10:05:43 jsp Exp jsp $
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/time.h>
44 #include <sys/types.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/namei.h>
48 #include <sys/malloc.h>
49 #include <miscfs/nullfs/null.h>
50 
51 #define LOG2_SIZEVNODE 7		/* log2(sizeof struct vnode) */
52 #define	NNULLNODECACHE 16
53 #define	NULL_NHASH(vp) ((((u_long)vp)>>LOG2_SIZEVNODE) & (NNULLNODECACHE-1))
54 
55 /*
56  * Null layer cache:
57  * Each cache entry holds a reference to the lower vnode
58  * along with a pointer to the alias vnode.  When an
59  * entry is added the lower vnode is VREF'd.  When the
60  * alias is removed the lower vnode is vrele'd.
61  */
62 
63 /*
64  * Cache head
65  */
66 struct null_node_cache {
67 	struct null_node	*ac_forw;
68 	struct null_node	*ac_back;
69 };
70 
71 static struct null_node_cache null_node_cache[NNULLNODECACHE];
72 
73 /*
74  * Initialise cache headers
75  */
76 int
77 nullfs_init()
78 {
79 	struct null_node_cache *ac;
80 #ifdef NULLFS_DIAGNOSTIC
81 	printf("nullfs_init\n");		/* printed during system boot */
82 #endif
83 
84 	for (ac = null_node_cache; ac < null_node_cache + NNULLNODECACHE; ac++)
85 		ac->ac_forw = ac->ac_back = (struct null_node *) ac;
86 	return (0);
87 }
88 
89 /*
90  * Compute hash list for given lower vnode
91  */
92 static struct null_node_cache *
93 null_node_hash(lowervp)
94 struct vnode *lowervp;
95 {
96 
97 	return (&null_node_cache[NULL_NHASH(lowervp)]);
98 }
99 
100 /*
101  * Return a VREF'ed alias for lower vnode if already exists, else 0.
102  */
103 static struct vnode *
104 null_node_find(mp, lowervp)
105 	struct mount *mp;
106 	struct vnode *lowervp;
107 {
108 	struct null_node_cache *hd;
109 	struct null_node *a;
110 	struct vnode *vp;
111 
112 	/*
113 	 * Find hash base, and then search the (two-way) linked
114 	 * list looking for a null_node structure which is referencing
115 	 * the lower vnode.  If found, the increment the null_node
116 	 * reference count (but NOT the lower vnode's VREF counter).
117 	 */
118 	hd = null_node_hash(lowervp);
119 loop:
120 	for (a = hd->ac_forw; a != (struct null_node *) hd; a = a->null_forw) {
121 		if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
122 			vp = NULLTOV(a);
123 			/*
124 			 * We need vget for the VXLOCK
125 			 * stuff, but we don't want to lock
126 			 * the lower node.
127 			 */
128 			if (vget(vp, 0)) {
129 				printf ("null_node_find: vget failed.\n");
130 				goto loop;
131 			};
132 			return (vp);
133 		}
134 	}
135 
136 	return NULL;
137 }
138 
139 
140 /*
141  * Make a new null_node node.
142  * Vp is the alias vnode, lofsvp is the lower vnode.
143  * Maintain a reference to (lowervp).
144  */
145 static int
146 null_node_alloc(mp, lowervp, vpp)
147 	struct mount *mp;
148 	struct vnode *lowervp;
149 	struct vnode **vpp;
150 {
151 	struct null_node_cache *hd;
152 	struct null_node *xp;
153 	struct vnode *othervp, *vp;
154 	int error;
155 
156 	if (error = getnewvnode(VT_NULL, mp, null_vnodeop_p, vpp))
157 		return (error);
158 	vp = *vpp;
159 
160 	MALLOC(xp, struct null_node *, sizeof(struct null_node), M_TEMP, M_WAITOK);
161 	vp->v_type = lowervp->v_type;
162 	xp->null_vnode = vp;
163 	vp->v_data = xp;
164 	xp->null_lowervp = lowervp;
165 	/*
166 	 * Before we insert our new node onto the hash chains,
167 	 * check to see if someone else has beaten us to it.
168 	 * (We could have slept in MALLOC.)
169 	 */
170 	if (othervp = null_node_find(lowervp)) {
171 		FREE(xp, M_TEMP);
172 		vp->v_type = VBAD;	/* node is discarded */
173 		vp->v_usecount = 0;	/* XXX */
174 		*vpp = othervp;
175 		return 0;
176 	};
177 	VREF(lowervp);   /* Extra VREF will be vrele'd in null_node_create */
178 	hd = null_node_hash(lowervp);
179 	insque(xp, hd);
180 	return 0;
181 }
182 
183 
184 /*
185  * Try to find an existing null_node vnode refering
186  * to it, otherwise make a new null_node vnode which
187  * contains a reference to the lower vnode.
188  */
189 int
190 null_node_create(mp, lowervp, newvpp)
191 	struct mount *mp;
192 	struct vnode *lowervp;
193 	struct vnode **newvpp;
194 {
195 	struct vnode *aliasvp;
196 
197 	if (aliasvp = null_node_find(mp, lowervp)) {
198 		/*
199 		 * null_node_find has taken another reference
200 		 * to the alias vnode.
201 		 */
202 #ifdef NULLFS_DIAGNOSTIC
203 		vprint("null_node_create: exists", NULLTOV(ap));
204 #endif
205 		/* VREF(aliasvp); --- done in null_node_find */
206 	} else {
207 		int error;
208 
209 		/*
210 		 * Get new vnode.
211 		 */
212 #ifdef NULLFS_DIAGNOSTIC
213 		printf("null_node_create: create new alias vnode\n");
214 #endif
215 
216 		/*
217 		 * Make new vnode reference the null_node.
218 		 */
219 		if (error = null_node_alloc(mp, lowervp, &aliasvp))
220 			return error;
221 
222 		/*
223 		 * aliasvp is already VREF'd by getnewvnode()
224 		 */
225 	}
226 
227 	vrele(lowervp);
228 
229 #ifdef DIAGNOSTIC
230 	if (lowervp->v_usecount < 1) {
231 		/* Should never happen... */
232 		vprint ("null_node_create: alias ");
233 		vprint ("null_node_create: lower ");
234 		printf ("null_node_create: lower has 0 usecount.\n");
235 		panic ("null_node_create: lower has 0 usecount.");
236 	};
237 #endif
238 
239 #ifdef NULLFS_DIAGNOSTIC
240 	vprint("null_node_create: alias", aliasvp);
241 	vprint("null_node_create: lower", lowervp);
242 #endif
243 
244 	*newvpp = aliasvp;
245 	return (0);
246 }
247 #ifdef NULLFS_DIAGNOSTIC
248 struct vnode *
249 null_checkvp(vp, fil, lno)
250 	struct vnode *vp;
251 	char *fil;
252 	int lno;
253 {
254 	struct null_node *a = VTONULL(vp);
255 #ifdef notyet
256 	/*
257 	 * Can't do this check because vop_reclaim runs
258 	 * with a funny vop vector.
259 	 */
260 	if (vp->v_op != null_vnodeop_p) {
261 		printf ("null_checkvp: on non-null-node\n");
262 		while (null_checkvp_barrier) /*WAIT*/ ;
263 		panic("null_checkvp");
264 	};
265 #endif
266 	if (a->null_lowervp == NULL) {
267 		/* Should never happen */
268 		int i; u_long *p;
269 		printf("vp = %x, ZERO ptr\n", vp);
270 		for (p = (u_long *) a, i = 0; i < 8; i++)
271 			printf(" %x", p[i]);
272 		printf("\n");
273 		/* wait for debugger */
274 		while (null_checkvp_barrier) /*WAIT*/ ;
275 		panic("null_checkvp");
276 	}
277 	if (a->null_lowervp->v_usecount < 1) {
278 		int i; u_long *p;
279 		printf("vp = %x, unref'ed lowervp\n", vp);
280 		for (p = (u_long *) a, i = 0; i < 8; i++)
281 			printf(" %x", p[i]);
282 		printf("\n");
283 		/* wait for debugger */
284 		while (null_checkvp_barrier) /*WAIT*/ ;
285 		panic ("null with unref'ed lowervp");
286 	};
287 #ifdef notyet
288 	printf("null %x/%d -> %x/%d [%s, %d]\n",
289 	        NULLTOV(a), NULLTOV(a)->v_usecount,
290 		a->null_lowervp, a->null_lowervp->v_usecount,
291 		fil, lno);
292 #endif
293 	return a->null_lowervp;
294 }
295 #endif
296