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