]>
git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/nullfs/null_subr.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1992, 1993
26 * The Regents of the University of California. All rights reserved.
28 * This code is derived from software donated to Berkeley by
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
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.
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
59 * @(#)null_subr.c 8.7 (Berkeley) 5/14/95
61 * null_subr.c 8.4 (Berkeley) 1/21/94
64 #include <sys/param.h>
65 #include <sys/systm.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>
74 #include <miscfs/nullfs/null.h>
76 #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */
77 #define NNULLNODECACHE 16
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.
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
;
93 * Initialise cache headers
98 #ifdef NULLFS_DIAGNOSTIC
99 printf("nullfs_init\n"); /* printed during system boot */
101 null_node_hashtbl
= hashinit(NNULLNODECACHE
, M_CACHE
, &null_node_hash
);
105 * Return a vnode_get'ed alias for lower vnode if already exists, else 0.
107 static struct vnode
*
108 null_node_find(mp
, lowervp
)
110 struct vnode
*lowervp
;
112 struct proc
*p
= curproc
; /* XXX */
113 struct null_node_hashhead
*hd
;
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).
123 hd
= NULL_NHASH(lowervp
);
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
) {
130 printf ("null_node_find: vget failed.\n");
142 * Make a new null_node node.
143 * Vp is the alias vnode, lofsvp is the lower vnode.
144 * Maintain a reference to (lowervp).
147 null_node_alloc(mp
, lowervp
, vpp
)
149 struct vnode
*lowervp
;
152 struct null_node_hashhead
*hd
;
153 struct null_node
*xp
;
154 struct vnode
*othervp
, *vp
;
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
)) {
164 vp
->v_type
= lowervp
->v_type
;
167 xp
->null_lowervp
= lowervp
;
169 * Before we insert our new node onto the hash chains,
170 * check to see if someone else has beaten us to it.
172 if (othervp
= null_node_find(lowervp
)) {
174 vp
->v_type
= VBAD
; /* node is discarded */
175 vp
->v_usecount
= 0; /* XXX */
176 vp
->v_data
= 0; /* prevent access to freed data */
180 if (vp
->v_type
== VREG
)
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
);
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.
195 null_node_create(mp
, lowervp
, newvpp
)
197 struct vnode
*lowervp
;
198 struct vnode
**newvpp
;
200 struct vnode
*aliasvp
;
202 if (aliasvp
= null_node_find(mp
, lowervp
)) {
204 * null_node_find has taken another reference
205 * to the alias vnode.
207 #ifdef NULLFS_DIAGNOSTIC
208 vprint("null_node_create: exists", NULLTOV(ap
));
210 /* vnode_get(aliasvp); --- done in null_node_find */
217 #ifdef NULLFS_DIAGNOSTIC
218 printf("null_node_create: create new alias vnode\n");
222 * Make new vnode reference the null_node.
224 if (error
= null_node_alloc(mp
, lowervp
, &aliasvp
))
228 * aliasvp is already vnode_get'd by getnewvnode()
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.");
243 #ifdef NULLFS_DIAGNOSTIC
244 vprint("null_node_create: alias", aliasvp
);
245 vprint("null_node_create: lower", lowervp
);
251 #ifdef NULLFS_DIAGNOSTIC
253 null_checkvp(vp
, fil
, lno
)
258 struct null_node
*a
= VTONULL(vp
);
261 * Can't do this check because vnop_reclaim runs
262 * with a funny vop vector.
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");
270 if (a
->null_lowervp
== NULL
) {
271 /* Should never happen */
273 printf("vp = %x, ZERO ptr\n", vp
);
274 for (p
= (u_long
*) a
, i
= 0; i
< 8; i
++)
277 /* wait for debugger */
278 while (null_checkvp_barrier
) /*WAIT*/ ;
279 panic("null_checkvp");
281 if (a
->null_lowervp
->v_usecount
< 1) {
283 printf("vp = %x, unref'ed lowervp\n", vp
);
284 for (p
= (u_long
*) a
, i
= 0; i
< 8; i
++)
287 /* wait for debugger */
288 while (null_checkvp_barrier
) /*WAIT*/ ;
289 panic ("null with unref'ed lowervp");
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
,
297 return a
->null_lowervp
;