The thread/proc pointer argument in the VFS subsystem originally existed
[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.14 2006/05/06 02:43:15 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
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 struct umap_node **umap_node_hashtbl;
64 static u_long umap_node_hash;
65
66 static u_long   umap_findid (u_long id, u_long map[][2], int nentries);
67 static int      umap_node_alloc (struct mount *mp, struct vnode *lowervp,
68                                      struct vnode **vpp);
69 static struct vnode *
70                 umap_node_find (struct mount *mp, struct vnode *targetvp);
71
72 /*
73  * Initialise cache headers
74  */
75 int
76 umapfs_init(struct vfsconf *vfsp)
77 {
78 #ifdef DEBUG
79         printf("umapfs_init\n");                /* printed during system boot */
80 #endif
81         umap_node_hash = 16;
82         while (umap_node_hash < desiredvnodes)
83                 umap_node_hash <<= 1;
84         umap_node_hashtbl = malloc(sizeof(void *) * umap_node_hash, M_CACHE,
85                                    M_WAITOK|M_ZERO);
86         --umap_node_hash;
87         return (0);
88 }
89
90 /*
91  * umap_findid is called by various routines in umap_vnodeops.c to
92  * find a user or group id in a map.
93  */
94 static u_long
95 umap_findid(u_long id, u_long map[][2], 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  * umap_reverse_findid is called by umap_getattr() in umap_vnodeops.c to
112  * find a user or group id in a map, in reverse.
113  */
114 u_long
115 umap_reverse_findid(u_long id, u_long map[][2], int nentries)
116 {
117         int i;
118
119         /* Find uid entry in map */
120         i = 0;
121         while ((i<nentries) && ((map[i][1]) != id))
122                 i++;
123
124         if (i < nentries)
125                 return (map[i][0]);
126         else
127                 return (-1);
128 }
129
130 /*
131  * Return alias for target vnode if already exists, else 0.
132  */
133 static struct vnode *
134 umap_node_find(struct mount *mp, struct vnode *targetvp)
135 {
136         struct thread *td = curthread;          /* XXX */
137         struct umap_node **xpp;
138         struct umap_node *xp;
139         struct vnode *vp;
140
141 #ifdef DEBUG
142         printf("umap_node_find(mp = %p, target = %p)\n",
143             (void *)mp, (void *)targetvp);
144 #endif
145
146         /*
147          * Find hash base, and then search the (two-way) linked
148          * list looking for a umap_node structure which is referencing
149          * the target vnode.  If found, the increment the umap_node
150          * reference count (but NOT the target vnode's vref counter).
151          */
152         xpp = UMAP_NHASH(targetvp);
153 loop:
154         for (xp = *xpp; xp; xp = (void *)xp->umap_next) {
155                 if (xp->umap_lowervp == targetvp &&
156                     xp->umap_vnode->v_mount == mp) {
157                         vp = UMAPTOV(xp);
158                         /*
159                          * We need vget for the VXLOCK
160                          * stuff, but we don't want to lock
161                          * the lower node.
162                          */
163                         if (vget(vp, LK_EXCLUSIVE|LK_SLEEPFAIL)) {
164 #ifdef DEBUG
165                                 printf ("umap_node_find: vget failed.\n");
166 #endif
167                                 goto loop;
168                         }
169                         return (vp);
170                 }
171         }
172
173 #ifdef DEBUG
174         printf("umap_node_find(%p, %p): NOT found\n",
175             (void *)mp, (void *)targetvp);
176 #endif
177
178         return (0);
179 }
180
181 /*
182  * Make a new umap_node node.
183  * Vp is the alias vnode, lofsvp is the target vnode.
184  * Maintain a reference to (targetvp).
185  */
186 static int
187 umap_node_alloc(struct mount *mp, struct vnode *lowervp, struct vnode **vpp)
188 {
189         struct umap_node *xp;
190         struct umap_node **xpp;
191         struct vnode *othervp, *vp;
192         int error;
193
194         /* XXX This routine probably needs a node_alloc lock */
195
196         /*
197          * Do the MALLOC before the getnewvnode since doing so afterward
198          * might cause a bogus v_data pointer to get dereferenced
199          * elsewhere if MALLOC should block.
200          */
201         MALLOC(xp, struct umap_node *, sizeof(struct umap_node),
202             M_TEMP, M_WAITOK);
203
204         error = getnewvnode(VT_UMAP, mp, vpp, 0, 0);
205         if (error) {
206                 FREE(xp, M_TEMP);
207                 return (error);
208         }
209         vp = *vpp;
210
211         vp->v_type = lowervp->v_type;
212         xp->umap_vnode = vp;
213         vp->v_data = xp;
214         xp->umap_lowervp = lowervp;
215         /*
216          * Before we insert our new node onto the hash chains,
217          * check to see if someone else has beaten us to it.
218          * (We could have slept in MALLOC.)
219          */
220         othervp = umap_node_find(mp, lowervp);
221         if (othervp) {
222                 FREE(xp, M_TEMP);
223                 vp->v_type = VBAD;      /* node is discarded */
224                 vx_put(vp);
225                 *vpp = othervp;
226                 return (0);
227         }
228         vref(lowervp);   /* Extra vref will be vrele'd in umap_node_create */
229         xpp = UMAP_NHASH(lowervp);
230         xp->umap_next = (void *)*xpp;
231         *xpp = xp;
232         return (0);
233 }
234
235 void
236 umap_node_delete(struct umap_node *xp)
237 {
238         struct umap_node **xpp;
239
240         xpp = UMAP_NHASH(xp->umap_lowervp);
241         while (*xpp && *xpp != xp)
242                 xpp = &(*xpp)->umap_next;
243         KKASSERT(*xpp);
244         *xpp = (void *)xp->umap_next;
245         xp->umap_next = NULL;
246 }
247
248 /*
249  * Try to find an existing umap_node vnode refering
250  * to it, otherwise make a new umap_node vnode which
251  * contains a reference to the target vnode.
252  */
253 int
254 umap_node_create(struct mount *mp, 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(struct vnode *vp, char *fil, int lno)
304 {
305         struct umap_node *a = VTOUMAP(vp);
306         if (a->umap_lowervp == NULL) {
307                 /* Should never happen */
308                 int i; u_long *p;
309                 printf("vp = %p, ZERO ptr\n", (void *)vp);
310                 for (p = (u_long *) a, i = 0; i < 8; i++)
311                         printf(" %p", (void *)p[i]);
312                 printf("\n");
313                 /* wait for debugger */
314                 while (umap_checkvp_barrier) /*WAIT*/ ;
315                 panic("umap_checkvp");
316         }
317         if (a->umap_lowervp->v_usecount < 1) {
318                 int i; u_long *p;
319                 printf("vp = %p, unref'ed lowervp\n", (void *)vp);
320                 for (p = (u_long *) a, i = 0; i < 8; i++)
321                         printf(" %p", (void *)p[i]);
322                 printf("\n");
323                 /* wait for debugger */
324                 while (umap_checkvp_barrier) /*WAIT*/ ;
325                 panic ("umap with unref'ed lowervp");
326         }
327 #if 0
328         printf("umap %x/%d -> %x/%d [%s, %d]\n",
329                 a->umap_vnode, a->umap_vnode->v_usecount,
330                 a->umap_lowervp, a->umap_lowervp->v_usecount,
331                 fil, lno);
332 #endif
333         return (a->umap_lowervp);
334 }
335 #endif /* DIAGNOSTIC */
336
337 /* umap_mapids maps all of the ids in a credential, both user and group. */
338
339 void
340 umap_mapids(struct mount *v_mount, struct ucred *credp)
341 {
342         int i;
343         uid_t uid;
344         gid_t gid;
345
346         if (credp == NOCRED)
347                 return;
348
349         /* Find uid entry in map */
350
351         uid = (uid_t) umap_findid(credp->cr_uid,
352                                 MOUNTTOUMAPMOUNT(v_mount)->info_mapdata,
353                                 MOUNTTOUMAPMOUNT(v_mount)->info_nentries);
354
355         if (uid != -1)
356                 credp->cr_uid = uid;
357         else
358                 credp->cr_uid = (uid_t) NOBODY;
359
360 #ifdef notdef
361         /* cr_gid is the same as cr_groups[0] in 4BSD */
362
363         /* Find gid entry in map */
364
365         gid = (gid_t) umap_findid(credp->cr_gid,
366                                 MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata,
367                                 MOUNTTOUMAPMOUNT(v_mount)->info_gnentries);
368
369         if (gid != -1)
370                 credp->cr_gid = gid;
371         else
372                 credp->cr_gid = NULLGROUP;
373 #endif
374
375         /* Now we must map each of the set of groups in the cr_groups
376                 structure. */
377
378         i = 0;
379         while (credp->cr_groups[i] != 0) {
380                 gid = (gid_t) umap_findid(credp->cr_groups[i],
381                                 MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata,
382                                 MOUNTTOUMAPMOUNT(v_mount)->info_gnentries);
383
384                 if (gid != -1)
385                         credp->cr_groups[i++] = gid;
386                 else
387                         credp->cr_groups[i++] = NULLGROUP;
388         }
389 }