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