]>
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 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1992, 1993
25 * The Regents of the University of California. All rights reserved.
27 * This code is derived from software donated to Berkeley by
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
33 * 1. Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * 2. Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in the
37 * documentation and/or other materials provided with the distribution.
38 * 3. All advertising materials mentioning features or use of this software
39 * must display the following acknowledgement:
40 * This product includes software developed by the University of
41 * California, Berkeley and its contributors.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * @(#)null_subr.c 8.7 (Berkeley) 5/14/95
60 * null_subr.c 8.4 (Berkeley) 1/21/94
63 #include <sys/param.h>
64 #include <sys/systm.h>
67 #include <sys/types.h>
68 #include <sys/vnode.h>
69 #include <sys/mount_internal.h>
70 #include <sys/namei.h>
71 #include <sys/malloc.h>
73 #include <miscfs/nullfs/null.h>
75 #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */
76 #define NNULLNODECACHE 16
80 * Each cache entry holds a reference to the lower vnode
81 * along with a pointer to the alias vnode. When an
82 * entry is added the lower vnode is vnode_get'd. When the
83 * alias is removed the lower vnode is vnode_put'd.
86 #define NULL_NHASH(vp) \
87 (&null_node_hashtbl[(((u_long)vp)>>LOG2_SIZEVNODE) & null_node_hash])
88 LIST_HEAD(null_node_hashhead
, null_node
) *null_node_hashtbl
;
89 u_long null_node_hash
;
92 * Initialise cache headers
97 #ifdef NULLFS_DIAGNOSTIC
98 printf("nullfs_init\n"); /* printed during system boot */
100 null_node_hashtbl
= hashinit(NNULLNODECACHE
, M_CACHE
, &null_node_hash
);
104 * Return a vnode_get'ed alias for lower vnode if already exists, else 0.
106 static struct vnode
*
107 null_node_find(mp
, lowervp
)
109 struct vnode
*lowervp
;
111 struct proc
*p
= curproc
; /* XXX */
112 struct null_node_hashhead
*hd
;
117 * Find hash base, and then search the (two-way) linked
118 * list looking for a null_node structure which is referencing
119 * the lower vnode. If found, the increment the null_node
120 * reference count (but NOT the lower vnode's vnode_get counter).
122 hd
= NULL_NHASH(lowervp
);
124 for (a
= hd
->lh_first
; a
!= 0; a
= a
->null_hash
.le_next
) {
125 if (a
->null_lowervp
== lowervp
&& NULLTOV(a
)->v_mount
== mp
) {
129 printf ("null_node_find: vget failed.\n");
141 * Make a new null_node node.
142 * Vp is the alias vnode, lofsvp is the lower vnode.
143 * Maintain a reference to (lowervp).
146 null_node_alloc(mp
, lowervp
, vpp
)
148 struct vnode
*lowervp
;
151 struct null_node_hashhead
*hd
;
152 struct null_node
*xp
;
153 struct vnode
*othervp
, *vp
;
156 MALLOC(xp
, struct null_node
*, sizeof(struct null_node
), M_TEMP
, M_WAITOK
);
157 if (error
= getnewvnode(VT_NULL
, mp
, null_vnodeop_p
, vpp
)) {
163 vp
->v_type
= lowervp
->v_type
;
166 xp
->null_lowervp
= lowervp
;
168 * Before we insert our new node onto the hash chains,
169 * check to see if someone else has beaten us to it.
171 if (othervp
= null_node_find(lowervp
)) {
173 vp
->v_type
= VBAD
; /* node is discarded */
174 vp
->v_usecount
= 0; /* XXX */
175 vp
->v_data
= 0; /* prevent access to freed data */
179 if (vp
->v_type
== VREG
)
181 vnode_get(lowervp
); /* Extra vnode_get will be vnode_put'd in null_node_create */
182 hd
= NULL_NHASH(lowervp
);
183 LIST_INSERT_HEAD(hd
, xp
, null_hash
);
189 * Try to find an existing null_node vnode refering
190 * to it, otherwise make a new null_node vnode which
191 * contains a reference to the lower vnode.
194 null_node_create(mp
, lowervp
, newvpp
)
196 struct vnode
*lowervp
;
197 struct vnode
**newvpp
;
199 struct vnode
*aliasvp
;
201 if (aliasvp
= null_node_find(mp
, lowervp
)) {
203 * null_node_find has taken another reference
204 * to the alias vnode.
206 #ifdef NULLFS_DIAGNOSTIC
207 vprint("null_node_create: exists", NULLTOV(ap
));
209 /* vnode_get(aliasvp); --- done in null_node_find */
216 #ifdef NULLFS_DIAGNOSTIC
217 printf("null_node_create: create new alias vnode\n");
221 * Make new vnode reference the null_node.
223 if (error
= null_node_alloc(mp
, lowervp
, &aliasvp
))
227 * aliasvp is already vnode_get'd by getnewvnode()
234 if (lowervp
->v_usecount
< 1) {
235 /* Should never happen... */
236 vprint ("null_node_create: alias ", aliasvp
);
237 vprint ("null_node_create: lower ", lowervp
);
238 panic ("null_node_create: lower has 0 usecount.");
242 #ifdef NULLFS_DIAGNOSTIC
243 vprint("null_node_create: alias", aliasvp
);
244 vprint("null_node_create: lower", lowervp
);
250 #ifdef NULLFS_DIAGNOSTIC
252 null_checkvp(vp
, fil
, lno
)
257 struct null_node
*a
= VTONULL(vp
);
260 * Can't do this check because vnop_reclaim runs
261 * with a funny vop vector.
263 if (vp
->v_op
!= null_vnodeop_p
) {
264 printf ("null_checkvp: on non-null-node\n");
265 while (null_checkvp_barrier
) /*WAIT*/ ;
266 panic("null_checkvp");
269 if (a
->null_lowervp
== NULL
) {
270 /* Should never happen */
272 printf("vp = %x, ZERO ptr\n", vp
);
273 for (p
= (u_long
*) a
, i
= 0; i
< 8; i
++)
276 /* wait for debugger */
277 while (null_checkvp_barrier
) /*WAIT*/ ;
278 panic("null_checkvp");
280 if (a
->null_lowervp
->v_usecount
< 1) {
282 printf("vp = %x, unref'ed lowervp\n", vp
);
283 for (p
= (u_long
*) a
, i
= 0; i
< 8; i
++)
286 /* wait for debugger */
287 while (null_checkvp_barrier
) /*WAIT*/ ;
288 panic ("null with unref'ed lowervp");
291 printf("null %x/%d -> %x/%d [%s, %d]\n",
292 NULLTOV(a
), NULLTOV(a
)->v_usecount
,
293 a
->null_lowervp
, a
->null_lowervp
->v_usecount
,
296 return a
->null_lowervp
;