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