Per-CPU VFS Namecache Effectiveness Statistics:
[dragonfly.git] / sys / vfs / umapfs / umap_subr.c
1 /*
2  * Copyright (c) 1992, 1993, 1995
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  *      @(#)umap_subr.c 8.9 (Berkeley) 5/14/95
37  *
38  * $FreeBSD: src/sys/miscfs/umapfs/umap_subr.c,v 1.19 1999/09/04 11:51:41 bde Exp $
39  * $DragonFly: src/sys/vfs/umapfs/Attic/umap_subr.c,v 1.6 2004/03/01 06:33:23 dillon Exp $
40  */
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/proc.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/malloc.h>
48 #include "umap.h"
49
50 #define LOG2_SIZEVNODE 7                /* log2(sizeof struct vnode) */
51 #define NUMAPNODECACHE 16
52
53 /*
54  * Null layer cache:
55  * Each cache entry holds a reference to the target vnode
56  * along with a pointer to the alias vnode.  When an
57  * entry is added the target vnode is VREF'd.  When the
58  * alias is removed the target vnode is vrele'd.
59  */
60
61 #define UMAP_NHASH(vp) \
62         (&umap_node_hashtbl \
63         [((uintptr_t)(void *)(vp) >> LOG2_SIZEVNODE) & umap_node_hash])
64 static LIST_HEAD(umap_node_hashhead, umap_node) *umap_node_hashtbl;
65 static u_long umap_node_hash;
66
67 static u_long   umap_findid (u_long id, u_long map[][2], int nentries);
68 static int      umap_node_alloc (struct mount *mp, struct vnode *lowervp,
69                                      struct vnode **vpp);
70 static struct vnode *
71                 umap_node_find (struct mount *mp, struct vnode *targetvp);
72
73 /*
74  * Initialise cache headers
75  */
76 int
77 umapfs_init(vfsp)
78         struct vfsconf *vfsp;
79 {
80
81 #ifdef DEBUG
82         printf("umapfs_init\n");                /* printed during system boot */
83 #endif
84         umap_node_hashtbl = hashinit(NUMAPNODECACHE, M_CACHE, &umap_node_hash);
85         return (0);
86 }
87
88 /*
89  * umap_findid is called by various routines in umap_vnodeops.c to
90  * find a user or group id in a map.
91  */
92 static u_long
93 umap_findid(id, map, nentries)
94         u_long id;
95         u_long map[][2];
96         int nentries;
97 {
98         int i;
99
100         /* Find uid entry in map */
101         i = 0;
102         while ((i<nentries) && ((map[i][0]) != id))
103                 i++;
104
105         if (i < nentries)
106                 return (map[i][1]);
107         else
108                 return (-1);
109
110 }
111
112 /*
113  * umap_reverse_findid is called by umap_getattr() in umap_vnodeops.c to
114  * find a user or group id in a map, in reverse.
115  */
116 u_long
117 umap_reverse_findid(id, map, nentries)
118         u_long id;
119         u_long map[][2];
120         int nentries;
121 {
122         int i;
123
124         /* Find uid entry in map */
125         i = 0;
126         while ((i<nentries) && ((map[i][1]) != id))
127                 i++;
128
129         if (i < nentries)
130                 return (map[i][0]);
131         else
132                 return (-1);
133
134 }
135
136 /*
137  * Return alias for target vnode if already exists, else 0.
138  */
139 static struct vnode *
140 umap_node_find(mp, targetvp)
141         struct mount *mp;
142         struct vnode *targetvp;
143 {
144         struct thread *td = curthread;          /* XXX */
145         struct umap_node_hashhead *hd;
146         struct umap_node *a;
147         struct vnode *vp;
148
149 #ifdef DEBUG
150         printf("umap_node_find(mp = %p, target = %p)\n",
151             (void *)mp, (void *)targetvp);
152 #endif
153
154         /*
155          * Find hash base, and then search the (two-way) linked
156          * list looking for a umap_node structure which is referencing
157          * the target vnode.  If found, the increment the umap_node
158          * reference count (but NOT the target vnode's VREF counter).
159          */
160         hd = UMAP_NHASH(targetvp);
161 loop:
162         for (a = hd->lh_first; a != 0; a = a->umap_hash.le_next) {
163                 if (a->umap_lowervp == targetvp &&
164                     a->umap_vnode->v_mount == mp) {
165                         vp = UMAPTOV(a);
166                         /*
167                          * We need vget for the VXLOCK
168                          * stuff, but we don't want to lock
169                          * the lower node.
170                          */
171                         if (vget(vp, NULL, 0, td)) {
172 #ifdef DEBUG
173                                 printf ("umap_node_find: vget failed.\n");
174 #endif
175                                 goto loop;
176                         }
177                         return (vp);
178                 }
179         }
180
181 #ifdef DEBUG
182         printf("umap_node_find(%p, %p): NOT found\n",
183             (void *)mp, (void *)targetvp);
184 #endif
185
186         return (0);
187 }
188
189 /*
190  * Make a new umap_node node.
191  * Vp is the alias vnode, lofsvp is the target vnode.
192  * Maintain a reference to (targetvp).
193  */
194 static int
195 umap_node_alloc(mp, lowervp, vpp)
196         struct mount *mp;
197         struct vnode *lowervp;
198         struct vnode **vpp;
199 {
200         struct umap_node_hashhead *hd;
201         struct umap_node *xp;
202         struct vnode *othervp, *vp;
203         int error;
204
205         /* XXX This routine probably needs a node_alloc lock */
206
207         /*
208          * Do the MALLOC before the getnewvnode since doing so afterward
209          * might cause a bogus v_data pointer to get dereferenced
210          * elsewhere if MALLOC should block.
211          */
212         MALLOC(xp, struct umap_node *, sizeof(struct umap_node),
213             M_TEMP, M_WAITOK);
214
215         error = getnewvnode(VT_UMAP, mp, umap_vnodeop_p, vpp);
216         if (error) {
217                 FREE(xp, M_TEMP);
218                 return (error);
219         }
220         vp = *vpp;
221
222         vp->v_type = lowervp->v_type;
223         xp->umap_vnode = vp;
224         vp->v_data = xp;
225         xp->umap_lowervp = lowervp;
226         /*
227          * Before we insert our new node onto the hash chains,
228          * check to see if someone else has beaten us to it.
229          * (We could have slept in MALLOC.)
230          */
231         othervp = umap_node_find(mp, lowervp);
232         if (othervp) {
233                 FREE(xp, M_TEMP);
234                 vp->v_type = VBAD;      /* node is discarded */
235                 vp->v_usecount = 0;     /* XXX */
236                 *vpp = othervp;
237                 return (0);
238         }
239         VREF(lowervp);   /* Extra VREF will be vrele'd in umap_node_create */
240         hd = UMAP_NHASH(lowervp);
241         LIST_INSERT_HEAD(hd, xp, umap_hash);
242         return (0);
243 }
244
245
246 /*
247  * Try to find an existing umap_node vnode refering
248  * to it, otherwise make a new umap_node vnode which
249  * contains a reference to the target vnode.
250  */
251 int
252 umap_node_create(mp, targetvp, newvpp)
253         struct mount *mp;
254         struct vnode *targetvp;
255         struct vnode **newvpp;
256 {
257         struct vnode *aliasvp;
258
259         aliasvp = umap_node_find(mp, targetvp);
260         if (aliasvp) {
261                 /*
262                  * Take another reference to the alias vnode
263                  */
264 #ifdef DEBUG
265                 vprint("umap_node_create: exists", aliasvp);
266 #endif
267                 /* VREF(aliasvp); */
268         } else {
269                 int error;
270
271                 /*
272                  * Get new vnode.
273                  */
274 #ifdef DEBUG
275                 printf("umap_node_create: create new alias vnode\n");
276 #endif
277                 /*
278                  * Make new vnode reference the umap_node.
279                  */
280                 error = umap_node_alloc(mp, targetvp, &aliasvp);
281                 if (error)
282                         return (error);
283
284                 /*
285                  * aliasvp is already VREF'd by getnewvnode()
286                  */
287         }
288
289         vrele(targetvp);
290
291 #ifdef DEBUG
292         vprint("umap_node_create: alias", aliasvp);
293         vprint("umap_node_create: target", targetvp);
294 #endif
295
296         *newvpp = aliasvp;
297         return (0);
298 }
299
300 #ifdef DIAGNOSTIC
301 int umap_checkvp_barrier = 1;
302 struct vnode *
303 umap_checkvp(vp, fil, lno)
304         struct vnode *vp;
305         char *fil;
306         int lno;
307 {
308         struct umap_node *a = VTOUMAP(vp);
309 #if 0
310         /*
311          * Can't do this check because vop_reclaim runs
312          * with funny vop vector.
313          */
314         if (vp->v_op != umap_vnodeop_p) {
315                 printf ("umap_checkvp: on non-umap-node\n");
316                 while (umap_checkvp_barrier) /*WAIT*/ ;
317                 panic("umap_checkvp");
318         }
319 #endif
320         if (a->umap_lowervp == NULL) {
321                 /* Should never happen */
322                 int i; u_long *p;
323                 printf("vp = %p, ZERO ptr\n", (void *)vp);
324                 for (p = (u_long *) a, i = 0; i < 8; i++)
325                         printf(" %p", (void *)p[i]);
326                 printf("\n");
327                 /* wait for debugger */
328                 while (umap_checkvp_barrier) /*WAIT*/ ;
329                 panic("umap_checkvp");
330         }
331         if (a->umap_lowervp->v_usecount < 1) {
332                 int i; u_long *p;
333                 printf("vp = %p, unref'ed lowervp\n", (void *)vp);
334                 for (p = (u_long *) a, i = 0; i < 8; i++)
335                         printf(" %p", (void *)p[i]);
336                 printf("\n");
337                 /* wait for debugger */
338                 while (umap_checkvp_barrier) /*WAIT*/ ;
339                 panic ("umap with unref'ed lowervp");
340         }
341 #if 0
342         printf("umap %x/%d -> %x/%d [%s, %d]\n",
343                 a->umap_vnode, a->umap_vnode->v_usecount,
344                 a->umap_lowervp, a->umap_lowervp->v_usecount,
345                 fil, lno);
346 #endif
347         return (a->umap_lowervp);
348 }
349 #endif /* DIAGNOSTIC */
350
351 /* umap_mapids maps all of the ids in a credential, both user and group. */
352
353 void
354 umap_mapids(v_mount, credp)
355         struct mount *v_mount;
356         struct ucred *credp;
357 {
358         int i;
359         uid_t uid;
360         gid_t gid;
361
362         if (credp == NOCRED)
363                 return;
364
365         /* Find uid entry in map */
366
367         uid = (uid_t) umap_findid(credp->cr_uid,
368                                 MOUNTTOUMAPMOUNT(v_mount)->info_mapdata,
369                                 MOUNTTOUMAPMOUNT(v_mount)->info_nentries);
370
371         if (uid != -1)
372                 credp->cr_uid = uid;
373         else
374                 credp->cr_uid = (uid_t) NOBODY;
375
376 #ifdef notdef
377         /* cr_gid is the same as cr_groups[0] in 4BSD */
378
379         /* Find gid entry in map */
380
381         gid = (gid_t) umap_findid(credp->cr_gid,
382                                 MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata,
383                                 MOUNTTOUMAPMOUNT(v_mount)->info_gnentries);
384
385         if (gid != -1)
386                 credp->cr_gid = gid;
387         else
388                 credp->cr_gid = NULLGROUP;
389 #endif
390
391         /* Now we must map each of the set of groups in the cr_groups
392                 structure. */
393
394         i = 0;
395         while (credp->cr_groups[i] != 0) {
396                 gid = (gid_t) umap_findid(credp->cr_groups[i],
397                                 MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata,
398                                 MOUNTTOUMAPMOUNT(v_mount)->info_gnentries);
399
400                 if (gid != -1)
401                         credp->cr_groups[i++] = gid;
402                 else
403                         credp->cr_groups[i++] = NULLGROUP;
404         }
405 }