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