- Fix indentation in VarFind()
[dragonfly.git] / sys / vfs / nullfs / null_subr.c
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  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      @(#)null_subr.c 8.7 (Berkeley) 5/14/95
37  *
38  * $FreeBSD: src/sys/miscfs/nullfs/null_subr.c,v 1.21.2.4 2001/06/26 04:20:09 bp Exp $
39  * $DragonFly: src/sys/vfs/nullfs/Attic/null_subr.c,v 1.17 2004/12/17 00:18:30 dillon Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/proc.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/malloc.h>
49 #include "null.h"
50
51 #define LOG2_SIZEVNODE 7                /* log2(sizeof struct vnode) */
52
53 /*
54  * Null layer cache:
55  * Each cache entry holds a reference to the lower vnode
56  * along with a pointer to the alias vnode.  When an
57  * entry is added the lower vnode is vref'd.  When the
58  * alias is removed the lower vnode is vrele'd.
59  */
60
61 #define NULL_NHASH(vp) \
62         (&null_node_hashtbl[(((uintptr_t)vp)>>LOG2_SIZEVNODE) & null_node_hash])
63
64 static struct null_node **null_node_hashtbl;
65 static u_long null_node_hash;
66 static struct lwkt_token null_ihash_token;
67
68 static MALLOC_DEFINE(M_NULLFSHASH, "NULLFS hash", "NULLFS hash table");
69 MALLOC_DEFINE(M_NULLFSNODE, "NULLFS node", "NULLFS vnode private part");
70
71 static int      null_node_alloc(struct mount *mp, struct vnode *lowervp,
72                                      struct vnode **vpp);
73 static struct vnode *
74                 null_node_find(struct mount *mp, struct vnode *lowervp);
75
76 /*
77  * Initialise cache headers
78  */
79 int
80 nullfs_init(struct vfsconf *vfsp)
81 {
82         NULLFSDEBUG("nullfs_init\n");           /* printed during system boot */
83         null_node_hash = 16;
84         while (null_node_hash < desiredvnodes)
85                 null_node_hash <<= 1;
86         null_node_hashtbl = malloc(sizeof(void *) * null_node_hash,
87                                     M_NULLFSHASH, M_WAITOK|M_ZERO);
88         --null_node_hash;
89         lwkt_token_init(&null_ihash_token);
90         return (0);
91 }
92
93 int
94 nullfs_uninit(struct vfsconf *vfsp)
95 {
96         if (null_node_hashtbl) {
97                 free(null_node_hashtbl, M_NULLFSHASH);
98                 null_node_hashtbl = NULL;
99         }
100         return (0);
101 }
102
103 /*
104  * Return a vref'ed alias for lower vnode if already exists, else 0.
105  * Lower vnode should be locked (but with no additional refs) on entry
106  * and will be unlocked on return if the search was successful, and left
107  * locked if the search was not successful.
108  */
109 static struct vnode *
110 null_node_find(struct mount *mp, struct vnode *lowervp)
111 {
112         struct thread *td = curthread;  /* XXX */
113         struct null_node *np;
114         struct null_node *xp;
115         struct vnode *vp;
116         lwkt_tokref ilock;
117
118         lwkt_gettoken(&ilock, &null_ihash_token);
119 loop:
120         for (np = *NULL_NHASH(lowervp); np; np = np->null_next) {
121                 if (np->null_lowervp == lowervp && NULLTOV(np)->v_mount == mp) {
122                         vp = NULLTOV(np);
123                         if (vget(vp, LK_EXCLUSIVE | LK_CANRECURSE, td)) {
124                                 printf ("null_node_find: vget failed.\n");
125                                 goto loop;
126                         }
127
128                         /*
129                          * vget() might have blocked, we have to check that
130                          * our vnode is still valid.
131                          */
132                         xp = *NULL_NHASH(lowervp);
133                         while (xp) {
134                                 if (xp == np && xp->null_lowervp == lowervp &&
135                                     NULLTOV(xp) == vp &&
136                                     NULLTOV(xp)->v_mount == mp) {
137                                         break;
138                                 }
139                                 xp = xp->null_next;
140                         }
141                         if (xp == NULL) {
142                                 printf ("null_node_find: node race, retry.\n");
143                                 vput(vp);
144                                 goto loop;
145                         }
146                         /*
147                          * SUCCESS!  Returned the locked and referenced vp
148                          * and release the lock on lowervp.
149                          */
150                         VOP_UNLOCK(lowervp, 0, td);
151                         lwkt_reltoken(&ilock);
152                         return (vp);
153                 }
154         }
155
156         /*
157          * Failure, leave lowervp locked on return.
158          */
159         lwkt_reltoken(&ilock);
160         return(NULL);
161 }
162
163 int
164 null_node_add(struct null_node *np)
165 {
166         struct null_node **npp;
167         struct null_node *n2;
168         lwkt_tokref ilock;
169
170         lwkt_gettoken(&ilock, &null_ihash_token);
171         npp = NULL_NHASH(np->null_lowervp);
172         while ((n2 = *npp) != NULL) {
173                 if (n2->null_lowervp == np->null_lowervp &&
174                     n2->null_vnode->v_mount == np->null_vnode->v_mount) {
175                         lwkt_reltoken(&ilock);
176                         return(EBUSY);
177                 }
178                 npp = &n2->null_next;
179         }
180         np->null_next = NULL;
181         *npp = np;
182         lwkt_reltoken(&ilock);
183         return(0);
184 }
185
186 void
187 null_node_rem(struct null_node *np)
188 {
189         struct null_node **npp;
190         struct null_node *n2;
191         lwkt_tokref ilock;
192
193         lwkt_gettoken(&ilock, &null_ihash_token);
194         npp = NULL_NHASH(np->null_lowervp);
195         while ((n2 = *npp) != NULL) {
196                 if (n2 == np)
197                         break;
198                 npp = &n2->null_next;
199         }
200         KKASSERT(np == n2);
201         *npp = np->null_next;
202         np->null_next = NULL;
203         lwkt_reltoken(&ilock);
204 }
205
206 /*
207  * Make a new null_node node.  vp is the null mount vnode, lowervp is the
208  * lower vnode.  Maintain a reference to (lowervp).  lowervp must be
209  * locked on call.
210  */
211 static int
212 null_node_alloc(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
213 {
214         struct null_node *np;
215         struct thread *td;
216         struct vnode *vp;
217         int error;
218
219         td = curthread;
220 retry:
221         /*
222          * If we have already hashed the vp we can just return it.
223          */
224         *vpp = null_node_find(mp, lowervp);
225         if (*vpp)
226                 return 0;
227
228         /*
229          * lowervp is locked but not referenced at this point.
230          */
231         MALLOC(np, struct null_node *, sizeof(struct null_node),
232                M_NULLFSNODE, M_WAITOK);
233
234         error = getnewvnode(VT_NULL, mp, vpp, 0, LK_CANRECURSE);
235         if (error) {
236                 FREE(np, M_NULLFSNODE);
237                 return (error);
238         }
239         vp = *vpp;
240
241         /*
242          * Set up the np/vp relationship and set the lower vnode.
243          *
244          * XXX:
245          * When nullfs encounters sockets or device nodes, it
246          * has a hard time working with the normal vp union, probably
247          * because the device has not yet been opened.  Needs investigation.
248          */
249         vp->v_type = lowervp->v_type;
250         if (vp->v_type == VCHR || vp->v_type == VBLK)
251                 addaliasu(vp, lowervp->v_udev);
252         else
253                 vp->v_un = lowervp->v_un;       /* XXX why this assignment? */
254         np->null_vnode = vp;
255         np->null_lowervp = lowervp;
256
257         /*
258          * Our new vnode is already VX locked (which is effective
259          * LK_THISLAYER, which is what we want).
260          */
261
262         /*
263          * Try to add our new node to the hash table.  If a collision
264          * occurs someone else beat us to it and we need to destroy the
265          * vnode and retry.
266          */
267         if (null_node_add(np) != 0) {
268                 free(np, M_NULLFSNODE);
269                 vput(vp);
270                 goto retry;
271         }
272
273         /*
274          * Finish up.  Link the vnode and null_node together, ref lowervp
275          * for the null node.  lowervp is already locked so the lock state
276          * is already properly synchronized.
277          *
278          * Set the vnode up to reclaim as quickly as possible
279          */
280         vp->v_data = np;
281         vp->v_flag |= VAGE;
282         vref(lowervp);
283         return (0);
284 }
285
286
287 /*
288  * Try to find an existing null_node vnode refering to the given underlying
289  * vnode (which should be locked and referenced). If no vnode found, create
290  * a new null_node vnode which contains a reference to the lower vnode.
291  */
292 int
293 null_node_create(struct mount *mp, struct vnode *lowervp, struct vnode **newvpp)
294 {
295         struct vnode *aliasvp;
296
297         aliasvp = null_node_find(mp, lowervp);
298         if (aliasvp) {
299                 /*
300                  * null_node_find() has unlocked lowervp for us, so we just
301                  * have to get rid of the reference.
302                  */
303                 vrele(lowervp);
304 #ifdef NULLFS_DEBUG
305                 vprint("null_node_create: exists", aliasvp);
306 #endif
307         } else {
308                 int error;
309
310                 /*
311                  * Get new vnode.  Note that lowervp is locked and referenced
312                  * at this point (as it was passed to us).
313                  */
314                 NULLFSDEBUG("null_node_create: create new alias vnode\n");
315
316                 /*
317                  * Make new vnode reference the null_node.
318                  */
319                 error = null_node_alloc(mp, lowervp, &aliasvp);
320                 vrele(lowervp);
321                 if (error)
322                         return error;
323
324                 /*
325                  * aliasvp is already locked and ref'd by getnewvnode()
326                  */
327         }
328
329 #ifdef DIAGNOSTIC
330         if (lowervp->v_usecount < 1) {
331                 /* Should never happen... */
332                 vprint ("null_node_create: alias ", aliasvp);
333                 vprint ("null_node_create: lower ", lowervp);
334                 panic ("null_node_create: lower has 0 usecount.");
335         };
336 #endif
337
338 #ifdef NULLFS_DEBUG
339         vprint("null_node_create: alias", aliasvp);
340         vprint("null_node_create: lower", lowervp);
341 #endif
342
343         *newvpp = aliasvp;
344         return (0);
345 }
346
347 #ifdef DIAGNOSTIC
348 #include "opt_ddb.h"
349
350 #ifdef DDB
351 #define null_checkvp_barrier    1
352 #else
353 #define null_checkvp_barrier    0
354 #endif
355
356 struct vnode *
357 null_checkvp(struct vnode *vp, char *fil, int lno)
358 {
359         struct null_node *a = VTONULL(vp);
360         if (a->null_lowervp == NULLVP) {
361                 /* Should never happen */
362                 int i; u_long *p;
363                 printf("vp = %p, ZERO ptr\n", (void *)vp);
364                 for (p = (u_long *) a, i = 0; i < 8; i++)
365                         printf(" %lx", p[i]);
366                 printf("\n");
367                 /* wait for debugger */
368                 while (null_checkvp_barrier) /*WAIT*/ ;
369                 panic("null_checkvp");
370         }
371         if (a->null_lowervp->v_usecount < 1) {
372                 int i; u_long *p;
373                 printf("vp = %p, unref'ed lowervp\n", (void *)vp);
374                 for (p = (u_long *) a, i = 0; i < 8; i++)
375                         printf(" %lx", p[i]);
376                 printf("\n");
377                 /* wait for debugger */
378                 while (null_checkvp_barrier) /*WAIT*/ ;
379                 panic ("null with unref'ed lowervp");
380         };
381 #ifdef notyet
382         printf("null %x/%d -> %x/%d [%s, %d]\n",
383                 NULLTOV(a), NULLTOV(a)->v_usecount,
384                 a->null_lowervp, a->null_lowervp->v_usecount,
385                 fil, lno);
386 #endif
387         return a->null_lowervp;
388 }
389 #endif