xref: /netbsd-src/sys/miscfs/genfs/layer_subr.c (revision 19ef5b5b0bcb90f63509df6e78769de1b57c2758)
1 /*	$NetBSD: layer_subr.c,v 1.35 2014/02/10 11:23:14 hannken Exp $	*/
2 
3 /*
4  * Copyright (c) 1999 National Aeronautics & Space Administration
5  * All rights reserved.
6  *
7  * This software was written by William Studenmund of the
8  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
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. Neither the name of the National Aeronautics & Space Administration
19  *    nor the names of its contributors may be used to endorse or promote
20  *    products derived from this software without specific prior written
21  *    permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NATIONAL AERONAUTICS & SPACE ADMINISTRATION
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ADMINISTRATION OR CONTRIB-
27  * UTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1992, 1993
38  *	The Regents of the University of California.  All rights reserved.
39  *
40  * This code is derived from software donated to Berkeley by
41  * Jan-Simon Pendry.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. Neither the name of the University nor the names of its contributors
52  *    may be used to endorse or promote products derived from this software
53  *    without specific prior written permission.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65  * SUCH DAMAGE.
66  *
67  *	from: Id: lofs_subr.c,v 1.11 1992/05/30 10:05:43 jsp Exp
68  *	@(#)null_subr.c	8.7 (Berkeley) 5/14/95
69  */
70 
71 #include <sys/cdefs.h>
72 __KERNEL_RCSID(0, "$NetBSD: layer_subr.c,v 1.35 2014/02/10 11:23:14 hannken Exp $");
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/proc.h>
77 #include <sys/time.h>
78 #include <sys/vnode.h>
79 #include <sys/mount.h>
80 #include <sys/namei.h>
81 #include <sys/kmem.h>
82 #include <sys/malloc.h>
83 
84 #include <miscfs/specfs/specdev.h>
85 #include <miscfs/genfs/layer.h>
86 #include <miscfs/genfs/layer_extern.h>
87 
88 #ifdef LAYERFS_DIAGNOSTIC
89 int layerfs_debug = 1;
90 #endif
91 
92 /*
93  * layer cache:
94  * Each cache entry holds a reference to the lower vnode
95  * along with a pointer to the alias vnode.  When an
96  * entry is added the lower vnode is VREF'd.  When the
97  * alias is removed the lower vnode is vrele'd.
98  */
99 
100 void
101 layerfs_init(void)
102 {
103 	/* Nothing. */
104 }
105 
106 void
107 layerfs_done(void)
108 {
109 	/* Nothing. */
110 }
111 
112 /*
113  * layer_node_find: find and return alias for lower vnode or NULL.
114  *
115  * => Return alias vnode referenced. if already exists.
116  * => The layermp's hashlock must be held on entry, we will unlock on success.
117  */
118 struct vnode *
119 layer_node_find(struct mount *mp, struct vnode *lowervp)
120 {
121 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
122 	struct layer_node_hashhead *hd;
123 	struct layer_node *a;
124 	struct vnode *vp;
125 	int error;
126 
127 	/*
128 	 * Find hash bucket and search the (two-way) linked list looking
129 	 * for a layerfs node structure which is referencing the lower vnode.
130 	 * If found, the increment the layer_node reference count, but NOT
131 	 * the lower vnode's reference counter.
132 	 */
133 	KASSERT(mutex_owned(&lmp->layerm_hashlock));
134 	hd = LAYER_NHASH(lmp, lowervp);
135 loop:
136 	LIST_FOREACH(a, hd, layer_hash) {
137 		if (a->layer_lowervp != lowervp) {
138 			continue;
139 		}
140 		vp = LAYERTOV(a);
141 		if (vp->v_mount != mp) {
142 			continue;
143 		}
144 		mutex_enter(vp->v_interlock);
145 		mutex_exit(&lmp->layerm_hashlock);
146 		error = vget(vp, 0);
147 		if (error) {
148 			mutex_enter(&lmp->layerm_hashlock);
149 			goto loop;
150 		}
151 		return vp;
152 	}
153 	return NULL;
154 }
155 
156 /*
157  * layer_node_alloc: make a new layerfs vnode.
158  *
159  * => vp is the alias vnode, lowervp is the lower vnode.
160  * => We will hold a reference to lowervp.
161  */
162 int
163 layer_node_alloc(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
164 {
165 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
166 	struct layer_node_hashhead *hd;
167 	struct layer_node *xp;
168 	struct vnode *vp, *nvp;
169 	int error;
170 
171 	/* Get a new vnode and share its interlock with underlying vnode. */
172 	error = getnewvnode(lmp->layerm_tag, mp, lmp->layerm_vnodeop_p,
173 	    lowervp->v_interlock, &vp);
174 	if (error) {
175 		return error;
176 	}
177 	vp->v_type = lowervp->v_type;
178 	mutex_enter(vp->v_interlock);
179 	vp->v_iflag |= VI_LAYER;
180 	mutex_exit(vp->v_interlock);
181 
182 	xp = kmem_alloc(lmp->layerm_size, KM_SLEEP);
183 	if (xp == NULL) {
184 		ungetnewvnode(vp);
185 		return ENOMEM;
186 	}
187 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
188 		spec_node_init(vp, lowervp->v_rdev);
189 	}
190 
191 	/*
192 	 * Before inserting the node into the hash, check if other thread
193 	 * did not race with us.  If so - return that node, destroy ours.
194 	 */
195 	mutex_enter(&lmp->layerm_hashlock);
196 	if ((nvp = layer_node_find(mp, lowervp)) != NULL) {
197 		ungetnewvnode(vp);
198 		kmem_free(xp, lmp->layerm_size);
199 		*vpp = nvp;
200 		return 0;
201 	}
202 
203 	vp->v_data = xp;
204 	vp->v_vflag = (vp->v_vflag & ~VV_MPSAFE) |
205 	    (lowervp->v_vflag & VV_MPSAFE);
206 	xp->layer_vnode = vp;
207 	xp->layer_lowervp = lowervp;
208 	xp->layer_flags = 0;
209 
210 	/*
211 	 * Insert the new node into the hash.
212 	 * Add a reference to the lower node.
213 	 */
214 	vref(lowervp);
215 	hd = LAYER_NHASH(lmp, lowervp);
216 	LIST_INSERT_HEAD(hd, xp, layer_hash);
217 	uvm_vnp_setsize(vp, 0);
218 	mutex_exit(&lmp->layerm_hashlock);
219 
220 	*vpp = vp;
221 	return 0;
222 }
223 
224 /*
225  * layer_node_create: try to find an existing layerfs vnode refering to it,
226  * otherwise make a new vnode which contains a reference to the lower vnode.
227  *
228  * => Caller should lock the lower node.
229  */
230 int
231 layer_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **nvpp)
232 {
233 	struct vnode *aliasvp;
234 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
235 
236 	mutex_enter(&lmp->layerm_hashlock);
237 	aliasvp = layer_node_find(mp, lowervp);
238 	if (aliasvp != NULL) {
239 		/*
240 		 * Note: layer_node_find() has taken another reference to
241 		 * the alias vnode and moved the lock holding to aliasvp.
242 		 */
243 #ifdef LAYERFS_DIAGNOSTIC
244 		if (layerfs_debug)
245 			vprint("layer_node_create: exists", aliasvp);
246 #endif
247 	} else {
248 		int error;
249 
250 		mutex_exit(&lmp->layerm_hashlock);
251 		/*
252 		 * Get a new vnode.  Make it to reference the layer_node.
253 		 * Note: aliasvp will be return with the reference held.
254 		 */
255 		error = (lmp->layerm_alloc)(mp, lowervp, &aliasvp);
256 		if (error)
257 			return error;
258 #ifdef LAYERFS_DIAGNOSTIC
259 		if (layerfs_debug)
260 			printf("layer_node_create: create new alias vnode\n");
261 #endif
262 	}
263 
264 	/*
265 	 * Now that we acquired a reference on the upper vnode, release one
266 	 * on the lower node.  The existence of the layer_node retains one
267 	 * reference to the lower node.
268 	 */
269 	vrele(lowervp);
270 	KASSERT(lowervp->v_usecount > 0);
271 
272 #ifdef LAYERFS_DIAGNOSTIC
273 	if (layerfs_debug)
274 		vprint("layer_node_create: alias", aliasvp);
275 #endif
276 	*nvpp = aliasvp;
277 	return 0;
278 }
279 
280 #ifdef LAYERFS_DIAGNOSTIC
281 struct vnode *
282 layer_checkvp(struct vnode *vp, const char *fil, int lno)
283 {
284 	struct layer_node *a = VTOLAYER(vp);
285 #ifdef notyet
286 	/*
287 	 * Can't do this check because vop_reclaim runs
288 	 * with a funny vop vector.
289 	 *
290 	 * WRS - no it doesnt...
291 	 */
292 	if (vp->v_op != layer_vnodeop_p) {
293 		printf ("layer_checkvp: on non-layer-node\n");
294 #ifdef notyet
295 		while (layer_checkvp_barrier) /*WAIT*/ ;
296 #endif
297 		panic("layer_checkvp");
298 	};
299 #endif
300 	if (a->layer_lowervp == NULL) {
301 		/* Should never happen */
302 		int i; u_long *p;
303 		printf("vp = %p, ZERO ptr\n", vp);
304 		for (p = (u_long *) a, i = 0; i < 8; i++)
305 			printf(" %lx", p[i]);
306 		printf("\n");
307 		/* wait for debugger */
308 		panic("layer_checkvp");
309 	}
310 	if (a->layer_lowervp->v_usecount < 1) {
311 		int i; u_long *p;
312 		printf("vp = %p, unref'ed lowervp\n", vp);
313 		for (p = (u_long *) a, i = 0; i < 8; i++)
314 			printf(" %lx", p[i]);
315 		printf("\n");
316 		/* wait for debugger */
317 		panic ("layer with unref'ed lowervp");
318 	};
319 #ifdef notnow
320 	printf("layer %p/%d -> %p/%d [%s, %d]\n",
321 	        LAYERTOV(a), LAYERTOV(a)->v_usecount,
322 		a->layer_lowervp, a->layer_lowervp->v_usecount,
323 		fil, lno);
324 #endif
325 	return a->layer_lowervp;
326 }
327 #endif
328