1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software donated to Berkeley by
6 * Jan-Simon Pendry.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)null_subr.c 8.7 (Berkeley) 5/14/95
33 *
34 * $FreeBSD$
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/malloc.h>
43 #include <sys/mount.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46
47 #include <fs/nullfs/null.h>
48
49 #define LOG2_SIZEVNODE 8 /* log2(sizeof struct vnode) */
50 #define NNULLNODECACHE 16
51
52 /*
53 * Null layer cache:
54 * Each cache entry holds a reference to the lower vnode
55 * along with a pointer to the alias vnode. When an
56 * entry is added the lower vnode is VREF'd. When the
57 * alias is removed the lower vnode is vrele'd.
58 */
59
60 #define NULL_NHASH(vp) \
61 (&null_node_hashtbl[(((uintptr_t)vp)>>LOG2_SIZEVNODE) & null_node_hash])
62
63 static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl;
64 static u_long null_node_hash;
65 struct mtx null_hashmtx;
66
67 static MALLOC_DEFINE(M_NULLFSHASH, "nullfs_hash", "NULLFS hash table");
68 MALLOC_DEFINE(M_NULLFSNODE, "nullfs_node", "NULLFS vnode private part");
69
70 static struct vnode * null_hashget(struct mount *, struct vnode *);
71 static struct vnode * null_hashins(struct mount *, struct null_node *);
72
73 /*
74 * Initialise cache headers
75 */
76 int
77 nullfs_init(vfsp)
78 struct vfsconf *vfsp;
79 {
80
81 NULLFSDEBUG("nullfs_init\n"); /* printed during system boot */
82 null_node_hashtbl = hashinit(NNULLNODECACHE, M_NULLFSHASH, &null_node_hash);
83 mtx_init(&null_hashmtx, "nullhs", NULL, MTX_DEF);
84 return (0);
85 }
86
87 int
88 nullfs_uninit(vfsp)
89 struct vfsconf *vfsp;
90 {
91
92 mtx_destroy(&null_hashmtx);
93 free(null_node_hashtbl, M_NULLFSHASH);
94 return (0);
95 }
96
97 /*
98 * Return a VREF'ed alias for lower vnode if already exists, else 0.
99 * Lower vnode should be locked on entry and will be left locked on exit.
100 */
101 static struct vnode *
102 null_hashget(mp, lowervp)
103 struct mount *mp;
104 struct vnode *lowervp;
105 {
106 struct thread *td = curthread; /* XXX */
107 struct null_node_hashhead *hd;
108 struct null_node *a;
109 struct vnode *vp;
110 int error;
111
112 ASSERT_VOP_LOCKED(lowervp, "null_hashget");
113
114 /*
115 * Find hash base, and then search the (two-way) linked
116 * list looking for a null_node structure which is referencing
117 * the lower vnode. If found, the increment the null_node
118 * reference count (but NOT the lower vnode's VREF counter).
119 */
120 hd = NULL_NHASH(lowervp);
121 mtx_lock(&null_hashmtx);
122 LIST_FOREACH(a, hd, null_hash) {
123 if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) {
124 vp = NULLTOV(a);
125 VI_LOCK(vp);
126 mtx_unlock(&null_hashmtx);
127 /*
128 * We need to clear the OWEINACT flag here as this
129 * may lead vget() to try to lock our vnode which
130 * is already locked via lowervp.
131 */
132 vp->v_iflag &= ~VI_OWEINACT;
133 error = vget(vp, LK_INTERLOCK, td);
134 /*
135 * Since we have the lower node locked the nullfs
136 * node can not be in the process of recycling. If
137 * it had been recycled before we grabed the lower
138 * lock it would not have been found on the hash.
139 */
140 if (error)
141 panic("null_hashget: vget error %d", error);
142 return (vp);
143 }
144 }
145 mtx_unlock(&null_hashmtx);
146 return (NULLVP);
147 }
148
149 /*
150 * Act like null_hashget, but add passed null_node to hash if no existing
151 * node found.
152 */
153 static struct vnode *
154 null_hashins(mp, xp)
155 struct mount *mp;
156 struct null_node *xp;
157 {
158 struct thread *td = curthread; /* XXX */
159 struct null_node_hashhead *hd;
160 struct null_node *oxp;
161 struct vnode *ovp;
162 int error;
163
164 hd = NULL_NHASH(xp->null_lowervp);
165 mtx_lock(&null_hashmtx);
166 LIST_FOREACH(oxp, hd, null_hash) {
167 if (oxp->null_lowervp == xp->null_lowervp &&
168 NULLTOV(oxp)->v_mount == mp) {
169 /*
170 * See null_hashget for a description of this
171 * operation.
172 */
173 ovp = NULLTOV(oxp);
174 VI_LOCK(ovp);
175 mtx_unlock(&null_hashmtx);
176 ovp->v_iflag &= ~VI_OWEINACT;
177 error = vget(ovp, LK_INTERLOCK, td);
178 if (error)
179 panic("null_hashins: vget error %d", error);
180 return (ovp);
181 }
182 }
183 LIST_INSERT_HEAD(hd, xp, null_hash);
184 mtx_unlock(&null_hashmtx);
185 return (NULLVP);
186 }
187
188 static void
189 null_insmntque_dtr(struct vnode *vp, void *xp)
190 {
191 vp->v_data = NULL;
192 vp->v_vnlock = &vp->v_lock;
193 FREE(xp, M_NULLFSNODE);
194 vp->v_op = &dead_vnodeops;
195 (void) vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
196 vgone(vp);
197 vput(vp);
198 }
199
200 /*
201 * Make a new or get existing nullfs node.
202 * Vp is the alias vnode, lowervp is the lower vnode.
203 *
204 * The lowervp assumed to be locked and having "spare" reference. This routine
205 * vrele lowervp if nullfs node was taken from hash. Otherwise it "transfers"
206 * the caller's "spare" reference to created nullfs vnode.
207 */
208 int
209 null_nodeget(mp, lowervp, vpp)
210 struct mount *mp;
211 struct vnode *lowervp;
212 struct vnode **vpp;
213 {
214 struct null_node *xp;
215 struct vnode *vp;
216 int error;
217
218 /* Lookup the hash firstly */
219 *vpp = null_hashget(mp, lowervp);
220 if (*vpp != NULL) {
221 vrele(lowervp);
222 return (0);
223 }
224
225 /*
226 * We do not serialize vnode creation, instead we will check for
227 * duplicates later, when adding new vnode to hash.
228 *
229 * Note that duplicate can only appear in hash if the lowervp is
230 * locked LK_SHARED.
231 */
232
233 /*
234 * Do the MALLOC before the getnewvnode since doing so afterward
235 * might cause a bogus v_data pointer to get dereferenced
236 * elsewhere if MALLOC should block.
237 */
238 MALLOC(xp, struct null_node *, sizeof(struct null_node),
239 M_NULLFSNODE, M_WAITOK);
240
241 error = getnewvnode("null", mp, &null_vnodeops, &vp);
242 if (error) {
243 FREE(xp, M_NULLFSNODE);
244 return (error);
245 }
246
247 xp->null_vnode = vp;
248 xp->null_lowervp = lowervp;
249 vp->v_type = lowervp->v_type;
250 vp->v_data = xp;
251 vp->v_vnlock = lowervp->v_vnlock;
252 if (vp->v_vnlock == NULL)
253 panic("null_nodeget: Passed a NULL vnlock.\n");
254 error = insmntque1(vp, mp, null_insmntque_dtr, xp);
255 if (error != 0)
256 return (error);
257 /*
258 * Atomically insert our new node into the hash or vget existing
259 * if someone else has beaten us to it.
260 */
261 *vpp = null_hashins(mp, xp);
262 if (*vpp != NULL) {
263 vrele(lowervp);
264 vp->v_vnlock = &vp->v_lock;
265 xp->null_lowervp = NULL;
266 vrele(vp);
267 return (0);
268 }
269 *vpp = vp;
270
271 return (0);
272 }
273
274 /*
275 * Remove node from hash.
276 */
277 void
278 null_hashrem(xp)
279 struct null_node *xp;
280 {
281
282 mtx_lock(&null_hashmtx);
283 LIST_REMOVE(xp, null_hash);
284 mtx_unlock(&null_hashmtx);
285 }
286
287 #ifdef DIAGNOSTIC
288
289 #ifdef KDB
290 #define null_checkvp_barrier 1
291 #else
292 #define null_checkvp_barrier 0
293 #endif
294
295 struct vnode *
296 null_checkvp(vp, fil, lno)
297 struct vnode *vp;
298 char *fil;
299 int lno;
300 {
301 int interlock = 0;
302 struct null_node *a = VTONULL(vp);
303 #ifdef notyet
304 /*
305 * Can't do this check because vop_reclaim runs
306 * with a funny vop vector.
307 */
308 if (vp->v_op != null_vnodeop_p) {
309 printf ("null_checkvp: on non-null-node\n");
310 while (null_checkvp_barrier) /*WAIT*/ ;
311 panic("null_checkvp");
312 };
313 #endif
314 if (a->null_lowervp == NULLVP) {
315 /* Should never happen */
316 int i; u_long *p;
317 printf("vp = %p, ZERO ptr\n", (void *)vp);
318 for (p = (u_long *) a, i = 0; i < 8; i++)
319 printf(" %lx", p[i]);
320 printf("\n");
321 /* wait for debugger */
322 while (null_checkvp_barrier) /*WAIT*/ ;
323 panic("null_checkvp");
324 }
325 if (mtx_owned(VI_MTX(vp)) != 0) {
326 VI_UNLOCK(vp);
327 interlock = 1;
328 }
329 if (vrefcnt(a->null_lowervp) < 1) {
330 int i; u_long *p;
331 printf("vp = %p, unref'ed lowervp\n", (void *)vp);
332 for (p = (u_long *) a, i = 0; i < 8; i++)
333 printf(" %lx", p[i]);
334 printf("\n");
335 /* wait for debugger */
336 while (null_checkvp_barrier) /*WAIT*/ ;
337 panic ("null with unref'ed lowervp");
338 };
339 if (interlock != 0)
340 VI_LOCK(vp);
341 #ifdef notyet
342 printf("null %x/%d -> %x/%d [%s, %d]\n",
343 NULLTOV(a), vrefcnt(NULLTOV(a)),
344 a->null_lowervp, vrefcnt(a->null_lowervp),
345 fil, lno);
346 #endif
347 return a->null_lowervp;
348 }
349 #endif
Cache object: 9092225b889e5a42372d0c3fbf5d724a
|