]>
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_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1992, 1993
31 * The Regents of the University of California. All rights reserved.
33 * This code is derived from software donated to Berkeley by
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
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.
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
64 * @(#)null_subr.c 8.7 (Berkeley) 5/14/95
66 * null_subr.c 8.4 (Berkeley) 1/21/94
69 #include <sys/param.h>
70 #include <sys/systm.h>
73 #include <sys/types.h>
74 #include <sys/vnode.h>
75 #include <sys/mount_internal.h>
76 #include <sys/namei.h>
77 #include <sys/malloc.h>
79 #include <miscfs/nullfs/null.h>
81 #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */
82 #define NNULLNODECACHE 16
86 * Each cache entry holds a reference to the lower vnode
87 * along with a pointer to the alias vnode. When an
88 * entry is added the lower vnode is vnode_get'd. When the
89 * alias is removed the lower vnode is vnode_put'd.
92 #define NULL_NHASH(vp) \
93 (&null_node_hashtbl[(((uintptr_t)vp)>>LOG2_SIZEVNODE) & null_node_hash])
94 LIST_HEAD(null_node_hashhead
, null_node
) *null_node_hashtbl
;
95 u_long null_node_hash
;
98 * Initialise cache headers
103 #ifdef NULLFS_DIAGNOSTIC
104 printf("nullfs_init\n"); /* printed during system boot */
106 null_node_hashtbl
= hashinit(NNULLNODECACHE
, M_CACHE
, &null_node_hash
);
110 * Return a vnode_get'ed alias for lower vnode if already exists, else 0.
112 static struct vnode
*
113 null_node_find(mp
, lowervp
)
115 struct vnode
*lowervp
;
117 struct proc
*p
= curproc
; /* XXX */
118 struct null_node_hashhead
*hd
;
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
126 * reference count (but NOT the lower vnode's vnode_get counter).
128 hd
= NULL_NHASH(lowervp
);
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
) {
135 printf ("null_node_find: vget failed.\n");
147 * Make a new null_node node.
148 * Vp is the alias vnode, lofsvp is the lower vnode.
149 * Maintain a reference to (lowervp).
152 null_node_alloc(mp
, lowervp
, vpp
)
154 struct vnode
*lowervp
;
157 struct null_node_hashhead
*hd
;
158 struct null_node
*xp
;
159 struct vnode
*othervp
, *vp
;
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
)) {
169 vp
->v_type
= lowervp
->v_type
;
172 xp
->null_lowervp
= lowervp
;
174 * Before we insert our new node onto the hash chains,
175 * check to see if someone else has beaten us to it.
177 if (othervp
= null_node_find(lowervp
)) {
179 vp
->v_type
= VBAD
; /* node is discarded */
180 vp
->v_usecount
= 0; /* XXX */
181 vp
->v_data
= 0; /* prevent access to freed data */
185 if (vp
->v_type
== VREG
)
187 vnode_get(lowervp
); /* Extra vnode_get will be vnode_put'd in null_node_create */
188 hd
= NULL_NHASH(lowervp
);
189 LIST_INSERT_HEAD(hd
, xp
, null_hash
);
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.
200 null_node_create(mp
, lowervp
, newvpp
)
202 struct vnode
*lowervp
;
203 struct vnode
**newvpp
;
205 struct vnode
*aliasvp
;
207 if (aliasvp
= null_node_find(mp
, lowervp
)) {
209 * null_node_find has taken another reference
210 * to the alias vnode.
212 #ifdef NULLFS_DIAGNOSTIC
213 vprint("null_node_create: exists", NULLTOV(ap
));
215 /* vnode_get(aliasvp); --- done in null_node_find */
222 #ifdef NULLFS_DIAGNOSTIC
223 printf("null_node_create: create new alias vnode\n");
227 * Make new vnode reference the null_node.
229 if (error
= null_node_alloc(mp
, lowervp
, &aliasvp
))
233 * aliasvp is already vnode_get'd by getnewvnode()
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.");
248 #ifdef NULLFS_DIAGNOSTIC
249 vprint("null_node_create: alias", aliasvp
);
250 vprint("null_node_create: lower", lowervp
);
256 #ifdef NULLFS_DIAGNOSTIC
258 null_checkvp(vp
, fil
, lno
)
263 struct null_node
*a
= VTONULL(vp
);
266 * Can't do this check because vnop_reclaim runs
267 * with a funny vop vector.
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");
275 if (a
->null_lowervp
== NULL
) {
276 /* Should never happen */
278 printf("vp = %x, ZERO ptr\n", vp
);
279 for (p
= (uint32_t *) a
, i
= 0; i
< 8; i
++)
282 /* wait for debugger */
283 while (null_checkvp_barrier
) /*WAIT*/ ;
284 panic("null_checkvp");
286 if (a
->null_lowervp
->v_usecount
< 1) {
288 printf("vp = %x, unref'ed lowervp\n", vp
);
289 for (p
= (uint32_t *) a
, i
= 0; i
< 8; i
++)
292 /* wait for debugger */
293 while (null_checkvp_barrier
) /*WAIT*/ ;
294 panic ("null with unref'ed lowervp");
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
,
302 return a
->null_lowervp
;