2 * Copyright (c) 1992, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software donated to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)umap_subr.c 8.9 (Berkeley) 5/14/95
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mount.h>
42 #include <sys/vnode.h>
44 #include <fs/umapfs/umap.h>
46 #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */
47 #define NUMAPNODECACHE 16
51 * Each cache entry holds a reference to the target vnode
52 * along with a pointer to the alias vnode. When an
53 * entry is added the target vnode is VREF'd. When the
54 * alias is removed the target vnode is vrele'd.
57 #define UMAP_NHASH(vp) \
59 [((uintptr_t)(void *)(vp) >> LOG2_SIZEVNODE) & umap_node_hash])
60 static LIST_HEAD(umap_node_hashhead, umap_node) *umap_node_hashtbl;
61 static u_long umap_node_hash;
63 static u_long umap_findid(u_long id, u_long map[][2], int nentries);
64 static int umap_node_alloc(struct mount *mp, struct vnode *lowervp,
67 umap_node_find(struct mount *mp, struct vnode *targetvp);
70 * Initialise cache headers
78 printf("umapfs_init\n"); /* printed during system boot */
80 umap_node_hashtbl = hashinit(NUMAPNODECACHE, M_CACHE, &umap_node_hash);
85 * umap_findid is called by various routines in umap_vnodeops.c to
86 * find a user or group id in a map.
89 umap_findid(id, map, nentries)
96 /* Find uid entry in map */
98 while ((i<nentries) && ((map[i][0]) != id))
109 * umap_reverse_findid is called by umap_getattr() in umap_vnodeops.c to
110 * find a user or group id in a map, in reverse.
113 umap_reverse_findid(id, map, nentries)
120 /* Find uid entry in map */
122 while ((i<nentries) && ((map[i][1]) != id))
133 * Return alias for target vnode if already exists, else 0.
135 static struct vnode *
136 umap_node_find(mp, targetvp)
138 struct vnode *targetvp;
140 struct thread *td = curthread; /* XXX */
141 struct umap_node_hashhead *hd;
146 printf("umap_node_find(mp = %p, target = %p)\n",
147 (void *)mp, (void *)targetvp);
151 * Find hash base, and then search the (two-way) linked
152 * list looking for a umap_node structure which is referencing
153 * the target vnode. If found, the increment the umap_node
154 * reference count (but NOT the target vnode's VREF counter).
156 hd = UMAP_NHASH(targetvp);
158 LIST_FOREACH(a, hd, umap_hash) {
159 if (a->umap_lowervp == targetvp &&
160 a->umap_vnode->v_mount == mp) {
163 * We need vget for the VXLOCK
164 * stuff, but we don't want to lock
167 if (vget(vp, 0, td)) {
169 printf ("umap_node_find: vget failed.\n");
178 printf("umap_node_find(%p, %p): NOT found\n",
179 (void *)mp, (void *)targetvp);
186 * Make a new umap_node node.
187 * Vp is the alias vnode, lofsvp is the target vnode.
188 * Maintain a reference to (targetvp).
191 umap_node_alloc(mp, lowervp, vpp)
193 struct vnode *lowervp;
196 struct umap_node_hashhead *hd;
197 struct umap_node *xp;
198 struct vnode *othervp, *vp;
201 /* XXX This routine probably needs a node_alloc lock */
204 * Do the MALLOC before the getnewvnode since doing so afterward
205 * might cause a bogus v_data pointer to get dereferenced
206 * elsewhere if MALLOC should block.
208 MALLOC(xp, struct umap_node *, sizeof(struct umap_node),
211 error = getnewvnode("umap", mp, umap_vnodeop_p, vpp);
218 vp->v_type = lowervp->v_type;
221 xp->umap_lowervp = lowervp;
223 * Before we insert our new node onto the hash chains,
224 * check to see if someone else has beaten us to it.
225 * (We could have slept in MALLOC.)
227 othervp = umap_node_find(mp, lowervp);
230 vp->v_type = VBAD; /* node is discarded */
231 vp->v_usecount = 0; /* XXX */
235 VREF(lowervp); /* Extra VREF will be vrele'd in umap_node_create */
236 hd = UMAP_NHASH(lowervp);
237 LIST_INSERT_HEAD(hd, xp, umap_hash);
243 * Try to find an existing umap_node vnode refering
244 * to it, otherwise make a new umap_node vnode which
245 * contains a reference to the target vnode.
248 umap_node_create(mp, targetvp, newvpp)
250 struct vnode *targetvp;
251 struct vnode **newvpp;
253 struct vnode *aliasvp;
255 aliasvp = umap_node_find(mp, targetvp);
258 * Take another reference to the alias vnode
261 vprint("umap_node_create: exists", aliasvp);
271 printf("umap_node_create: create new alias vnode\n");
274 * Make new vnode reference the umap_node.
276 error = umap_node_alloc(mp, targetvp, &aliasvp);
281 * aliasvp is already VREF'd by getnewvnode()
288 vprint("umap_node_create: alias", aliasvp);
289 vprint("umap_node_create: target", targetvp);
297 int umap_checkvp_barrier = 1;
299 umap_checkvp(vp, fil, lno)
304 struct umap_node *a = VTOUMAP(vp);
307 * Can't do this check because vop_reclaim runs
308 * with funny vop vector.
310 if (vp->v_op != umap_vnodeop_p) {
311 printf ("umap_checkvp: on non-umap-node\n");
312 while (umap_checkvp_barrier) /*WAIT*/ ;
313 panic("umap_checkvp");
316 if (a->umap_lowervp == NULL) {
317 /* Should never happen */
319 printf("vp = %p, ZERO ptr\n", (void *)vp);
320 for (p = (u_long *) a, i = 0; i < 8; i++)
321 printf(" %p", (void *)p[i]);
323 /* wait for debugger */
324 while (umap_checkvp_barrier) /*WAIT*/ ;
325 panic("umap_checkvp");
327 if (vrefcnt(a->umap_lowervp) < 1) {
329 printf("vp = %p, unref'ed lowervp\n", (void *)vp);
330 for (p = (u_long *) a, i = 0; i < 8; i++)
331 printf(" %p", (void *)p[i]);
333 /* wait for debugger */
334 while (umap_checkvp_barrier) /*WAIT*/ ;
335 panic ("umap with unref'ed lowervp");
338 printf("umap %x/%d -> %x/%d [%s, %d]\n",
339 a->umap_vnode, a->umap_vnode->v_usecount,
340 a->umap_lowervp, a->umap_lowervp->v_usecount,
343 return (a->umap_lowervp);
345 #endif /* DIAGNOSTIC */
347 /* umap_mapids maps all of the ids in a credential, both user and group. */
350 umap_mapids(v_mount, credp)
351 struct mount *v_mount;
361 KASSERT(!crshared(credp), ("remapping a shared cred"));
363 /* Find uid entry in map */
365 uid = (uid_t) umap_findid(credp->cr_uid,
366 MOUNTTOUMAPMOUNT(v_mount)->info_mapdata,
367 MOUNTTOUMAPMOUNT(v_mount)->info_nentries);
372 credp->cr_uid = (uid_t) NOBODY;
375 /* cr_gid is the same as cr_groups[0] in 4BSD */
377 /* Find gid entry in map */
379 gid = (gid_t) umap_findid(credp->cr_gid,
380 MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata,
381 MOUNTTOUMAPMOUNT(v_mount)->info_gnentries);
386 credp->cr_gid = NULLGROUP;
389 /* Now we must map each of the set of groups in the cr_groups
393 while (credp->cr_groups[i] != 0) {
394 gid = (gid_t) umap_findid(credp->cr_groups[i],
395 MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata,
396 MOUNTTOUMAPMOUNT(v_mount)->info_gnentries);
399 credp->cr_groups[i++] = gid;
401 credp->cr_groups[i++] = NULLGROUP;