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