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