]>
git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/nullfs/null_subr.c
d0f26022ee5f9bc8807604fc035a69c1a35eb73c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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. 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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1992, 1993
33 * The Regents of the University of California. All rights reserved.
35 * This code is derived from software donated to Berkeley by
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
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.
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
66 * @(#)null_subr.c 8.7 (Berkeley) 5/14/95
68 * null_subr.c 8.4 (Berkeley) 1/21/94
71 #include <sys/param.h>
72 #include <sys/systm.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>
81 #include <miscfs/nullfs/null.h>
83 #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */
84 #define NNULLNODECACHE 16
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.
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
;
100 * Initialise cache headers
105 #ifdef NULLFS_DIAGNOSTIC
106 printf("nullfs_init\n"); /* printed during system boot */
108 null_node_hashtbl
= hashinit(NNULLNODECACHE
, M_CACHE
, &null_node_hash
);
112 * Return a vnode_get'ed alias for lower vnode if already exists, else 0.
114 static struct vnode
*
115 null_node_find(mp
, lowervp
)
117 struct vnode
*lowervp
;
119 struct proc
*p
= curproc
; /* XXX */
120 struct null_node_hashhead
*hd
;
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).
130 hd
= NULL_NHASH(lowervp
);
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
) {
137 printf ("null_node_find: vget failed.\n");
149 * Make a new null_node node.
150 * Vp is the alias vnode, lofsvp is the lower vnode.
151 * Maintain a reference to (lowervp).
154 null_node_alloc(mp
, lowervp
, vpp
)
156 struct vnode
*lowervp
;
159 struct null_node_hashhead
*hd
;
160 struct null_node
*xp
;
161 struct vnode
*othervp
, *vp
;
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
)) {
171 vp
->v_type
= lowervp
->v_type
;
174 xp
->null_lowervp
= lowervp
;
176 * Before we insert our new node onto the hash chains,
177 * check to see if someone else has beaten us to it.
179 if (othervp
= null_node_find(lowervp
)) {
181 vp
->v_type
= VBAD
; /* node is discarded */
182 vp
->v_usecount
= 0; /* XXX */
183 vp
->v_data
= 0; /* prevent access to freed data */
187 if (vp
->v_type
== VREG
)
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
);
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.
202 null_node_create(mp
, lowervp
, newvpp
)
204 struct vnode
*lowervp
;
205 struct vnode
**newvpp
;
207 struct vnode
*aliasvp
;
209 if (aliasvp
= null_node_find(mp
, lowervp
)) {
211 * null_node_find has taken another reference
212 * to the alias vnode.
214 #ifdef NULLFS_DIAGNOSTIC
215 vprint("null_node_create: exists", NULLTOV(ap
));
217 /* vnode_get(aliasvp); --- done in null_node_find */
224 #ifdef NULLFS_DIAGNOSTIC
225 printf("null_node_create: create new alias vnode\n");
229 * Make new vnode reference the null_node.
231 if (error
= null_node_alloc(mp
, lowervp
, &aliasvp
))
235 * aliasvp is already vnode_get'd by getnewvnode()
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.");
250 #ifdef NULLFS_DIAGNOSTIC
251 vprint("null_node_create: alias", aliasvp
);
252 vprint("null_node_create: lower", lowervp
);
258 #ifdef NULLFS_DIAGNOSTIC
260 null_checkvp(vp
, fil
, lno
)
265 struct null_node
*a
= VTONULL(vp
);
268 * Can't do this check because vnop_reclaim runs
269 * with a funny vop vector.
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");
277 if (a
->null_lowervp
== NULL
) {
278 /* Should never happen */
280 printf("vp = %x, ZERO ptr\n", vp
);
281 for (p
= (u_long
*) a
, i
= 0; i
< 8; i
++)
284 /* wait for debugger */
285 while (null_checkvp_barrier
) /*WAIT*/ ;
286 panic("null_checkvp");
288 if (a
->null_lowervp
->v_usecount
< 1) {
290 printf("vp = %x, unref'ed lowervp\n", vp
);
291 for (p
= (u_long
*) a
, i
= 0; i
< 8; i
++)
294 /* wait for debugger */
295 while (null_checkvp_barrier
) /*WAIT*/ ;
296 panic ("null with unref'ed lowervp");
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
,
304 return a
->null_lowervp
;