xref: /netbsd-src/sys/miscfs/genfs/layer_subr.c (revision 88fcb00c0357f2d7c1774f86a352637bfda96184)
1 /*	$NetBSD: layer_subr.c,v 1.32 2011/06/12 03:35:58 rmind 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.32 2011/06/12 03:35:58 rmind 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 locked and 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.  Return vnode locked.
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 		/*
146 		 * If we find a node being cleaned out, then ignore it and
147 		 * continue.  A thread trying to clean out the extant layer
148 		 * vnode needs to acquire the shared lock (i.e. the lower
149 		 * vnode's lock), which our caller already holds.  To allow
150 		 * the cleaning to succeed the current thread must make
151 		 * progress.  So, for a brief time more than one vnode in a
152 		 * layered file system may refer to a single vnode in the
153 		 * lower file system.
154 		 */
155 		if ((vp->v_iflag & VI_XLOCK) != 0) {
156 			mutex_exit(vp->v_interlock);
157 			continue;
158 		}
159 		mutex_exit(&lmp->layerm_hashlock);
160 		/*
161 		 * We must not let vget() try to lock the layer vp, since
162 		 * the lower vp is already locked and locking the layer vp
163 		 * will involve locking the lower vp.
164 		 */
165 		error = vget(vp, LK_NOWAIT);
166 		if (error) {
167 			kpause("layerfs", false, 1, NULL);
168 			mutex_enter(&lmp->layerm_hashlock);
169 			goto loop;
170 		}
171 		return vp;
172 	}
173 	return NULL;
174 }
175 
176 /*
177  * layer_node_alloc: make a new layerfs vnode.
178  *
179  * => vp is the alias vnode, lowervp is the lower vnode.
180  * => We will hold a reference to lowervp.
181  */
182 int
183 layer_node_alloc(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
184 {
185 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
186 	struct layer_node_hashhead *hd;
187 	struct layer_node *xp;
188 	struct vnode *vp, *nvp;
189 	int error;
190 	extern int (**dead_vnodeop_p)(void *);
191 
192 	/* Get a new vnode and share its interlock with underlying vnode. */
193 	error = getnewvnode(lmp->layerm_tag, mp, lmp->layerm_vnodeop_p,
194 	    lowervp->v_interlock, &vp);
195 	if (error) {
196 		return error;
197 	}
198 	vp->v_type = lowervp->v_type;
199 	mutex_enter(vp->v_interlock);
200 	vp->v_iflag |= VI_LAYER;
201 	mutex_exit(vp->v_interlock);
202 
203 	xp = kmem_alloc(lmp->layerm_size, KM_SLEEP);
204 	if (xp == NULL) {
205 		ungetnewvnode(vp);
206 		return ENOMEM;
207 	}
208 	if (vp->v_type == VBLK || vp->v_type == VCHR) {
209 		spec_node_init(vp, lowervp->v_rdev);
210 	}
211 
212 	vp->v_data = xp;
213 	vp->v_vflag = (vp->v_vflag & ~VV_MPSAFE) |
214 	    (lowervp->v_vflag & VV_MPSAFE);
215 	xp->layer_vnode = vp;
216 	xp->layer_lowervp = lowervp;
217 	xp->layer_flags = 0;
218 
219 	/*
220 	 * Before inserting the node into the hash, check if other thread
221 	 * did not race with us.  If so - return that node, destroy ours.
222 	 */
223 	mutex_enter(&lmp->layerm_hashlock);
224 	if ((nvp = layer_node_find(mp, lowervp)) != NULL) {
225 		/* Free the structures we have created. */
226 		if (vp->v_type == VBLK || vp->v_type == VCHR)
227 			spec_node_destroy(vp);
228 
229 		vp->v_type = VBAD;		/* node is discarded */
230 		vp->v_op = dead_vnodeop_p;	/* so ops will still work */
231 		vrele(vp);			/* get rid of it. */
232 		kmem_free(xp, lmp->layerm_size);
233 		*vpp = nvp;
234 		return 0;
235 	}
236 
237 	/*
238 	 * Insert the new node into the hash.
239 	 * Add a reference to the lower node.
240 	 */
241 	vref(lowervp);
242 	hd = LAYER_NHASH(lmp, lowervp);
243 	LIST_INSERT_HEAD(hd, xp, layer_hash);
244 	uvm_vnp_setsize(vp, 0);
245 	mutex_exit(&lmp->layerm_hashlock);
246 
247 	*vpp = vp;
248 	return 0;
249 }
250 
251 /*
252  * layer_node_create: try to find an existing layerfs vnode refering to it,
253  * otherwise make a new vnode which contains a reference to the lower vnode.
254  *
255  * => Caller should lock the lower node.
256  */
257 int
258 layer_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **nvpp)
259 {
260 	struct vnode *aliasvp;
261 	struct layer_mount *lmp = MOUNTTOLAYERMOUNT(mp);
262 
263 	KASSERT(VOP_ISLOCKED(lowervp));
264 
265 	mutex_enter(&lmp->layerm_hashlock);
266 	aliasvp = layer_node_find(mp, lowervp);
267 	if (aliasvp != NULL) {
268 		/*
269 		 * Note: layer_node_find() has taken another reference to
270 		 * the alias vnode and moved the lock holding to aliasvp.
271 		 */
272 #ifdef LAYERFS_DIAGNOSTIC
273 		if (layerfs_debug)
274 			vprint("layer_node_create: exists", aliasvp);
275 #endif
276 	} else {
277 		int error;
278 
279 		mutex_exit(&lmp->layerm_hashlock);
280 		/*
281 		 * Get a new vnode.  Make it to reference the layer_node.
282 		 * Note: aliasvp will be return with the reference held.
283 		 */
284 		error = (lmp->layerm_alloc)(mp, lowervp, &aliasvp);
285 		if (error)
286 			return error;
287 #ifdef LAYERFS_DIAGNOSTIC
288 		if (layerfs_debug)
289 			printf("layer_node_create: create new alias vnode\n");
290 #endif
291 	}
292 
293 	/*
294 	 * Now that we acquired a reference on the upper vnode, release one
295 	 * on the lower node.  The existence of the layer_node retains one
296 	 * reference to the lower node.
297 	 */
298 	vrele(lowervp);
299 	KASSERT(lowervp->v_usecount > 0);
300 
301 #ifdef LAYERFS_DIAGNOSTIC
302 	if (layerfs_debug)
303 		vprint("layer_node_create: alias", aliasvp);
304 #endif
305 	*nvpp = aliasvp;
306 	return 0;
307 }
308 
309 #ifdef LAYERFS_DIAGNOSTIC
310 struct vnode *
311 layer_checkvp(struct vnode *vp, const char *fil, int lno)
312 {
313 	struct layer_node *a = VTOLAYER(vp);
314 #ifdef notyet
315 	/*
316 	 * Can't do this check because vop_reclaim runs
317 	 * with a funny vop vector.
318 	 *
319 	 * WRS - no it doesnt...
320 	 */
321 	if (vp->v_op != layer_vnodeop_p) {
322 		printf ("layer_checkvp: on non-layer-node\n");
323 #ifdef notyet
324 		while (layer_checkvp_barrier) /*WAIT*/ ;
325 #endif
326 		panic("layer_checkvp");
327 	};
328 #endif
329 	if (a->layer_lowervp == NULL) {
330 		/* Should never happen */
331 		int i; u_long *p;
332 		printf("vp = %p, ZERO ptr\n", vp);
333 		for (p = (u_long *) a, i = 0; i < 8; i++)
334 			printf(" %lx", p[i]);
335 		printf("\n");
336 		/* wait for debugger */
337 		panic("layer_checkvp");
338 	}
339 	if (a->layer_lowervp->v_usecount < 1) {
340 		int i; u_long *p;
341 		printf("vp = %p, unref'ed lowervp\n", vp);
342 		for (p = (u_long *) a, i = 0; i < 8; i++)
343 			printf(" %lx", p[i]);
344 		printf("\n");
345 		/* wait for debugger */
346 		panic ("layer with unref'ed lowervp");
347 	};
348 #ifdef notnow
349 	printf("layer %p/%d -> %p/%d [%s, %d]\n",
350 	        LAYERTOV(a), LAYERTOV(a)->v_usecount,
351 		a->layer_lowervp, a->layer_lowervp->v_usecount,
352 		fil, lno);
353 #endif
354 	return a->layer_lowervp;
355 }
356 #endif
357