]>
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 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1992, 1993
28 * The Regents of the University of California. All rights reserved.
30 * This code is derived from software donated to Berkeley by
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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.
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
61 * @(#)null_subr.c 8.7 (Berkeley) 5/14/95
63 * null_subr.c 8.4 (Berkeley) 1/21/94
66 #include <sys/param.h>
67 #include <sys/systm.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>
76 #include <miscfs/nullfs/null.h>
78 #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */
79 #define NNULLNODECACHE 16
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.
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
;
95 * Initialise cache headers
100 #ifdef NULLFS_DIAGNOSTIC
101 printf("nullfs_init\n"); /* printed during system boot */
103 null_node_hashtbl
= hashinit(NNULLNODECACHE
, M_CACHE
, &null_node_hash
);
107 * Return a VREF'ed alias for lower vnode if already exists, else 0.
109 static struct vnode
*
110 null_node_find(mp
, lowervp
)
112 struct vnode
*lowervp
;
114 struct proc
*p
= curproc
; /* XXX */
115 struct null_node_hashhead
*hd
;
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).
125 hd
= NULL_NHASH(lowervp
);
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
) {
131 * We need vget for the VXLOCK
132 * stuff, but we don't want to lock
135 if (vget(vp
, 0, p
)) {
136 printf ("null_node_find: vget failed.\n");
148 * Make a new null_node node.
149 * Vp is the alias vnode, lofsvp is the lower vnode.
150 * Maintain a reference to (lowervp).
153 null_node_alloc(mp
, lowervp
, vpp
)
155 struct vnode
*lowervp
;
158 struct null_node_hashhead
*hd
;
159 struct null_node
*xp
;
160 struct vnode
*othervp
, *vp
;
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
)) {
170 vp
->v_type
= lowervp
->v_type
;
173 xp
->null_lowervp
= lowervp
;
175 * Before we insert our new node onto the hash chains,
176 * check to see if someone else has beaten us to it.
178 if (othervp
= null_node_find(lowervp
)) {
180 vp
->v_type
= VBAD
; /* node is discarded */
181 vp
->v_usecount
= 0; /* XXX */
182 vp
->v_data
= 0; /* prevent access to freed data */
186 if (vp
->v_type
== VREG
)
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
);
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.
201 null_node_create(mp
, lowervp
, newvpp
)
203 struct vnode
*lowervp
;
204 struct vnode
**newvpp
;
206 struct vnode
*aliasvp
;
208 if (aliasvp
= null_node_find(mp
, lowervp
)) {
210 * null_node_find has taken another reference
211 * to the alias vnode.
213 #ifdef NULLFS_DIAGNOSTIC
214 vprint("null_node_create: exists", NULLTOV(ap
));
216 /* VREF(aliasvp); --- done in null_node_find */
223 #ifdef NULLFS_DIAGNOSTIC
224 printf("null_node_create: create new alias vnode\n");
228 * Make new vnode reference the null_node.
230 if (error
= null_node_alloc(mp
, lowervp
, &aliasvp
))
234 * aliasvp is already VREF'd by getnewvnode()
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.");
249 #ifdef NULLFS_DIAGNOSTIC
250 vprint("null_node_create: alias", aliasvp
);
251 vprint("null_node_create: lower", lowervp
);
257 #ifdef NULLFS_DIAGNOSTIC
259 null_checkvp(vp
, fil
, lno
)
264 struct null_node
*a
= VTONULL(vp
);
267 * Can't do this check because vop_reclaim runs
268 * with a funny vop vector.
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");
276 if (a
->null_lowervp
== NULL
) {
277 /* Should never happen */
279 printf("vp = %x, ZERO ptr\n", vp
);
280 for (p
= (u_long
*) a
, i
= 0; i
< 8; i
++)
283 /* wait for debugger */
284 while (null_checkvp_barrier
) /*WAIT*/ ;
285 panic("null_checkvp");
287 if (a
->null_lowervp
->v_usecount
< 1) {
289 printf("vp = %x, unref'ed lowervp\n", vp
);
290 for (p
= (u_long
*) a
, i
= 0; i
< 8; i
++)
293 /* wait for debugger */
294 while (null_checkvp_barrier
) /*WAIT*/ ;
295 panic ("null with unref'ed lowervp");
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
,
303 return a
->null_lowervp
;