xref: /netbsd-src/sys/miscfs/genfs/layer_subr.c (revision f14316bcbc544b96a93e884bc5c2b15fd60e22ae)
1 /*	$NetBSD: layer_subr.c,v 1.36 2014/05/25 13:51:25 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.36 2014/05/25 13:51:25 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/genfs/layer.h>
85 #include <miscfs/genfs/layer_extern.h>
86 
87 #ifdef LAYERFS_DIAGNOSTIC
88 int layerfs_debug = 1;
89 #endif
90 
91 /*
92  * layer cache:
93  * Each cache entry holds a reference to the lower vnode
94  * along with a pointer to the alias vnode.  When an
95  * entry is added the lower vnode is VREF'd.  When the
96  * alias is removed the lower vnode is vrele'd.
97  */
98 
99 void
100 layerfs_init(void)
101 {
102 	/* Nothing. */
103 }
104 
105 void
106 layerfs_done(void)
107 {
108 	/* Nothing. */
109 }
110 
111 /*
112  * layer_node_create: try to find an existing layerfs vnode refering to it,
113  * otherwise make a new vnode which contains a reference to the lower vnode.
114  */
115 int
116 layer_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **nvpp)
117 {
118 	int error;
119 	struct vnode *aliasvp;
120 
121 	error = vcache_get(mp, &lowervp, sizeof(lowervp), &aliasvp);
122 	if (error)
123 		return error;
124 
125 	/*
126 	 * Now that we acquired a reference on the upper vnode, release one
127 	 * on the lower node.  The existence of the layer_node retains one
128 	 * reference to the lower node.
129 	 */
130 	vrele(lowervp);
131 	KASSERT(lowervp->v_usecount > 0);
132 
133 #ifdef LAYERFS_DIAGNOSTIC
134 	if (layerfs_debug)
135 		vprint("layer_node_create: alias", aliasvp);
136 #endif
137 	*nvpp = aliasvp;
138 	return 0;
139 }
140 
141 #ifdef LAYERFS_DIAGNOSTIC
142 struct vnode *
143 layer_checkvp(struct vnode *vp, const char *fil, int lno)
144 {
145 	struct layer_node *a = VTOLAYER(vp);
146 #ifdef notyet
147 	/*
148 	 * Can't do this check because vop_reclaim runs
149 	 * with a funny vop vector.
150 	 *
151 	 * WRS - no it doesnt...
152 	 */
153 	if (vp->v_op != layer_vnodeop_p) {
154 		printf ("layer_checkvp: on non-layer-node\n");
155 #ifdef notyet
156 		while (layer_checkvp_barrier) /*WAIT*/ ;
157 #endif
158 		panic("layer_checkvp");
159 	};
160 #endif
161 	if (a->layer_lowervp == NULL) {
162 		/* Should never happen */
163 		int i; u_long *p;
164 		printf("vp = %p, ZERO ptr\n", vp);
165 		for (p = (u_long *) a, i = 0; i < 8; i++)
166 			printf(" %lx", p[i]);
167 		printf("\n");
168 		/* wait for debugger */
169 		panic("layer_checkvp");
170 	}
171 	if (a->layer_lowervp->v_usecount < 1) {
172 		int i; u_long *p;
173 		printf("vp = %p, unref'ed lowervp\n", vp);
174 		for (p = (u_long *) a, i = 0; i < 8; i++)
175 			printf(" %lx", p[i]);
176 		printf("\n");
177 		/* wait for debugger */
178 		panic ("layer with unref'ed lowervp");
179 	};
180 #ifdef notnow
181 	printf("layer %p/%d -> %p/%d [%s, %d]\n",
182 	        LAYERTOV(a), LAYERTOV(a)->v_usecount,
183 		a->layer_lowervp, a->layer_lowervp->v_usecount,
184 		fil, lno);
185 #endif
186 	return a->layer_lowervp;
187 }
188 #endif
189