154947Sheideman /*
2*69446Smckusick * Copyright (c) 1992, 1993, 1995
363248Sbostic * The Regents of the University of California. All rights reserved.
454947Sheideman *
554947Sheideman * This code is derived from software donated to Berkeley by
654947Sheideman * Jan-Simon Pendry.
754947Sheideman *
854947Sheideman * %sccs.include.redist.c%
954947Sheideman *
10*69446Smckusick * @(#)umap_subr.c 8.9 (Berkeley) 05/14/95
1154947Sheideman *
12*69446Smckusick * From: $Id: lofs_subr.c, v 1.11 1992/05/30 10:05:43 jsp Exp jsp $
1354947Sheideman */
1454947Sheideman
1554947Sheideman #include <sys/param.h>
1654947Sheideman #include <sys/systm.h>
17*69446Smckusick #include <sys/proc.h>
1854947Sheideman #include <sys/time.h>
1954947Sheideman #include <sys/types.h>
2054947Sheideman #include <sys/vnode.h>
2154947Sheideman #include <sys/mount.h>
2254947Sheideman #include <sys/namei.h>
2354947Sheideman #include <sys/malloc.h>
2455031Smckusick #include <miscfs/umapfs/umap.h>
2554947Sheideman
2654947Sheideman #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */
2754947Sheideman #define NUMAPNODECACHE 16
2854947Sheideman
2954947Sheideman /*
3054947Sheideman * Null layer cache:
3154947Sheideman * Each cache entry holds a reference to the target vnode
3254947Sheideman * along with a pointer to the alias vnode. When an
3354947Sheideman * entry is added the target vnode is VREF'd. When the
3454947Sheideman * alias is removed the target vnode is vrele'd.
3554947Sheideman */
3654947Sheideman
3767717Smckusick #define UMAP_NHASH(vp) \
3867717Smckusick (&umap_node_hashtbl[(((u_long)vp)>>LOG2_SIZEVNODE) & umap_node_hash])
3967717Smckusick LIST_HEAD(umap_node_hashhead, umap_node) *umap_node_hashtbl;
4067717Smckusick u_long umap_node_hash;
4154947Sheideman
4254947Sheideman /*
4354947Sheideman * Initialise cache headers
4454947Sheideman */
4568623Smckusick umapfs_init(vfsp)
4668623Smckusick struct vfsconf *vfsp;
4754947Sheideman {
4867717Smckusick
4954947Sheideman #ifdef UMAPFS_DIAGNOSTIC
5054947Sheideman printf("umapfs_init\n"); /* printed during system boot */
5154947Sheideman #endif
5267717Smckusick umap_node_hashtbl = hashinit(NUMAPNODECACHE, M_CACHE, &umap_node_hash);
5354947Sheideman }
5454947Sheideman
5554947Sheideman /*
5665491Spendry * umap_findid is called by various routines in umap_vnodeops.c to
5765491Spendry * find a user or group id in a map.
5865491Spendry */
5965491Spendry static u_long
umap_findid(id,map,nentries)6065491Spendry umap_findid(id, map, nentries)
6165491Spendry u_long id;
6265491Spendry u_long map[][2];
6365491Spendry int nentries;
6465491Spendry {
6565491Spendry int i;
6665491Spendry
6765491Spendry /* Find uid entry in map */
6865491Spendry i = 0;
6965491Spendry while ((i<nentries) && ((map[i][0]) != id))
7065491Spendry i++;
7165491Spendry
7265491Spendry if (i < nentries)
7365491Spendry return (map[i][1]);
7465491Spendry else
7565491Spendry return (-1);
7665491Spendry
7765491Spendry }
7865491Spendry
7965491Spendry /*
8065491Spendry * umap_reverse_findid is called by umap_getattr() in umap_vnodeops.c to
8165491Spendry * find a user or group id in a map, in reverse.
8265491Spendry */
8365491Spendry u_long
umap_reverse_findid(id,map,nentries)8465491Spendry umap_reverse_findid(id, map, nentries)
8565491Spendry u_long id;
8665491Spendry u_long map[][2];
8765491Spendry int nentries;
8865491Spendry {
8965491Spendry int i;
9065491Spendry
9165491Spendry /* Find uid entry in map */
9265491Spendry i = 0;
9365491Spendry while ((i<nentries) && ((map[i][1]) != id))
9465491Spendry i++;
9565491Spendry
9665491Spendry if (i < nentries)
9765491Spendry return (map[i][0]);
9865491Spendry else
9965491Spendry return (-1);
10065491Spendry
10165491Spendry }
10265491Spendry
10365491Spendry /*
10454947Sheideman * Return alias for target vnode if already exists, else 0.
10554947Sheideman */
10654958Sheideman static struct vnode *
umap_node_find(mp,targetvp)10754947Sheideman umap_node_find(mp, targetvp)
10854947Sheideman struct mount *mp;
10954947Sheideman struct vnode *targetvp;
11054947Sheideman {
111*69446Smckusick struct proc *p = curproc; /* XXX */
11267717Smckusick struct umap_node_hashhead *hd;
11354947Sheideman struct umap_node *a;
11454958Sheideman struct vnode *vp;
11554947Sheideman
11654947Sheideman #ifdef UMAPFS_DIAGNOSTIC
11754947Sheideman printf("umap_node_find(mp = %x, target = %x)\n", mp, targetvp);
11854947Sheideman #endif
11954947Sheideman
12054947Sheideman /*
12154947Sheideman * Find hash base, and then search the (two-way) linked
12254947Sheideman * list looking for a umap_node structure which is referencing
12354947Sheideman * the target vnode. If found, the increment the umap_node
12454947Sheideman * reference count (but NOT the target vnode's VREF counter).
12554947Sheideman */
12667717Smckusick hd = UMAP_NHASH(targetvp);
12767717Smckusick loop:
12867717Smckusick for (a = hd->lh_first; a != 0; a = a->umap_hash.le_next) {
12955031Smckusick if (a->umap_lowervp == targetvp &&
13055031Smckusick a->umap_vnode->v_mount == mp) {
13154958Sheideman vp = UMAPTOV(a);
13254958Sheideman /*
13354958Sheideman * We need vget for the VXLOCK
13454958Sheideman * stuff, but we don't want to lock
13554958Sheideman * the lower node.
13654958Sheideman */
137*69446Smckusick if (vget(vp, 0, p)) {
13865491Spendry #ifdef UMAPFS_DIAGNOSTIC
13965491Spendry printf ("umap_node_find: vget failed.\n");
14065491Spendry #endif
14154958Sheideman goto loop;
14255031Smckusick }
14354958Sheideman return (vp);
14454947Sheideman }
14554947Sheideman }
14654947Sheideman
14754947Sheideman #ifdef UMAPFS_DIAGNOSTIC
14854947Sheideman printf("umap_node_find(%x, %x): NOT found\n", mp, targetvp);
14954947Sheideman #endif
15054947Sheideman
15154947Sheideman return (0);
15254947Sheideman }
15354947Sheideman
15454947Sheideman /*
15554958Sheideman * Make a new umap_node node.
15654958Sheideman * Vp is the alias vnode, lofsvp is the target vnode.
15754958Sheideman * Maintain a reference to (targetvp).
15854958Sheideman */
15954958Sheideman static int
umap_node_alloc(mp,lowervp,vpp)16054958Sheideman umap_node_alloc(mp, lowervp, vpp)
16154958Sheideman struct mount *mp;
16254958Sheideman struct vnode *lowervp;
16354958Sheideman struct vnode **vpp;
16454958Sheideman {
16567717Smckusick struct umap_node_hashhead *hd;
16654958Sheideman struct umap_node *xp;
16754958Sheideman struct vnode *othervp, *vp;
16854958Sheideman int error;
16954958Sheideman
17065423Spendry if (error = getnewvnode(VT_UMAP, mp, umap_vnodeop_p, vpp))
17165423Spendry return (error);
17254958Sheideman vp = *vpp;
17354958Sheideman
17455031Smckusick MALLOC(xp, struct umap_node *, sizeof(struct umap_node),
17555031Smckusick M_TEMP, M_WAITOK);
17654958Sheideman vp->v_type = lowervp->v_type;
17754958Sheideman xp->umap_vnode = vp;
17854958Sheideman vp->v_data = xp;
17954958Sheideman xp->umap_lowervp = lowervp;
18054958Sheideman /*
18154958Sheideman * Before we insert our new node onto the hash chains,
18254958Sheideman * check to see if someone else has beaten us to it.
18354958Sheideman * (We could have slept in MALLOC.)
18454958Sheideman */
18554958Sheideman if (othervp = umap_node_find(lowervp)) {
18654958Sheideman FREE(xp, M_TEMP);
18754958Sheideman vp->v_type = VBAD; /* node is discarded */
18854958Sheideman vp->v_usecount = 0; /* XXX */
18954958Sheideman *vpp = othervp;
19055031Smckusick return (0);
19155031Smckusick }
19254958Sheideman VREF(lowervp); /* Extra VREF will be vrele'd in umap_node_create */
19367717Smckusick hd = UMAP_NHASH(lowervp);
19467717Smckusick LIST_INSERT_HEAD(hd, xp, umap_hash);
19555031Smckusick return (0);
19654958Sheideman }
19754958Sheideman
19854958Sheideman
19954958Sheideman /*
20054947Sheideman * Try to find an existing umap_node vnode refering
20154947Sheideman * to it, otherwise make a new umap_node vnode which
20254947Sheideman * contains a reference to the target vnode.
20354947Sheideman */
20454947Sheideman int
umap_node_create(mp,targetvp,newvpp)20554947Sheideman umap_node_create(mp, targetvp, newvpp)
20654947Sheideman struct mount *mp;
20754947Sheideman struct vnode *targetvp;
20854947Sheideman struct vnode **newvpp;
20954947Sheideman {
21054947Sheideman struct vnode *aliasvp;
21154947Sheideman
21254958Sheideman if (aliasvp = umap_node_find(mp, targetvp)) {
21354947Sheideman /*
21454947Sheideman * Take another reference to the alias vnode
21554947Sheideman */
21654947Sheideman #ifdef UMAPFS_DIAGNOSTIC
21754947Sheideman vprint("umap_node_create: exists", ap->umap_vnode);
21854947Sheideman #endif
21954958Sheideman /* VREF(aliasvp); */
22054947Sheideman } else {
22154947Sheideman int error;
22254947Sheideman
22354947Sheideman /*
22454947Sheideman * Get new vnode.
22554947Sheideman */
22654947Sheideman #ifdef UMAPFS_DIAGNOSTIC
22754947Sheideman printf("umap_node_create: create new alias vnode\n");
22854947Sheideman #endif
22954947Sheideman /*
23054947Sheideman * Make new vnode reference the umap_node.
23154947Sheideman */
23254958Sheideman if (error = umap_node_alloc(mp, targetvp, &aliasvp))
23355031Smckusick return (error);
23454947Sheideman
23554947Sheideman /*
23654947Sheideman * aliasvp is already VREF'd by getnewvnode()
23754947Sheideman */
23854947Sheideman }
23954947Sheideman
24054947Sheideman vrele(targetvp);
24154947Sheideman
24254947Sheideman #ifdef UMAPFS_DIAGNOSTIC
24354947Sheideman vprint("umap_node_create: alias", aliasvp);
24454947Sheideman vprint("umap_node_create: target", targetvp);
24554947Sheideman #endif
24654947Sheideman
24754947Sheideman *newvpp = aliasvp;
24854947Sheideman return (0);
24954947Sheideman }
25055031Smckusick
25154947Sheideman #ifdef UMAPFS_DIAGNOSTIC
25254947Sheideman int umap_checkvp_barrier = 1;
25354947Sheideman struct vnode *
umap_checkvp(vp,fil,lno)25454947Sheideman umap_checkvp(vp, fil, lno)
25554947Sheideman struct vnode *vp;
25654947Sheideman char *fil;
25754947Sheideman int lno;
25854947Sheideman {
25954947Sheideman struct umap_node *a = VTOUMAP(vp);
26054947Sheideman #if 0
26154947Sheideman /*
26254947Sheideman * Can't do this check because vop_reclaim runs
26354947Sheideman * with funny vop vector.
26454947Sheideman */
26554947Sheideman if (vp->v_op != umap_vnodeop_p) {
26654947Sheideman printf ("umap_checkvp: on non-umap-node\n");
26754947Sheideman while (umap_checkvp_barrier) /*WAIT*/ ;
26854947Sheideman panic("umap_checkvp");
26955031Smckusick }
27054947Sheideman #endif
27154947Sheideman if (a->umap_lowervp == NULL) {
27254947Sheideman /* Should never happen */
27354947Sheideman int i; u_long *p;
27454947Sheideman printf("vp = %x, ZERO ptr\n", vp);
27554947Sheideman for (p = (u_long *) a, i = 0; i < 8; i++)
27654947Sheideman printf(" %x", p[i]);
27754947Sheideman printf("\n");
27854947Sheideman /* wait for debugger */
27954947Sheideman while (umap_checkvp_barrier) /*WAIT*/ ;
28054947Sheideman panic("umap_checkvp");
28154947Sheideman }
28254947Sheideman if (a->umap_lowervp->v_usecount < 1) {
28354947Sheideman int i; u_long *p;
28454947Sheideman printf("vp = %x, unref'ed lowervp\n", vp);
28554947Sheideman for (p = (u_long *) a, i = 0; i < 8; i++)
28654947Sheideman printf(" %x", p[i]);
28754947Sheideman printf("\n");
28854947Sheideman /* wait for debugger */
28954947Sheideman while (umap_checkvp_barrier) /*WAIT*/ ;
29054947Sheideman panic ("umap with unref'ed lowervp");
29155031Smckusick }
29254947Sheideman #if 0
29354947Sheideman printf("umap %x/%d -> %x/%d [%s, %d]\n",
29454947Sheideman a->umap_vnode, a->umap_vnode->v_usecount,
29554947Sheideman a->umap_lowervp, a->umap_lowervp->v_usecount,
29654947Sheideman fil, lno);
29754947Sheideman #endif
29855031Smckusick return (a->umap_lowervp);
29954947Sheideman }
30054947Sheideman #endif
30154947Sheideman
30254947Sheideman /* umap_mapids maps all of the ids in a credential, both user and group. */
30354947Sheideman
30465491Spendry void
umap_mapids(v_mount,credp)30565491Spendry umap_mapids(v_mount, credp)
30654958Sheideman struct mount *v_mount;
30754947Sheideman struct ucred *credp;
30854947Sheideman {
30965916Smckusick int i, unentries, gnentries;
31065916Smckusick u_long *groupmap, *usermap;
31165491Spendry uid_t uid;
31265491Spendry gid_t gid;
31354947Sheideman
31454958Sheideman unentries = MOUNTTOUMAPMOUNT(v_mount)->info_nentries;
31554958Sheideman usermap = &(MOUNTTOUMAPMOUNT(v_mount)->info_mapdata[0][0]);
31654958Sheideman gnentries = MOUNTTOUMAPMOUNT(v_mount)->info_gnentries;
31754958Sheideman groupmap = &(MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata[0][0]);
31854958Sheideman
31954947Sheideman /* Find uid entry in map */
32054947Sheideman
32165491Spendry uid = (uid_t) umap_findid(credp->cr_uid, usermap, unentries);
32254947Sheideman
32365491Spendry if (uid != -1)
32465491Spendry credp->cr_uid = uid;
32565491Spendry else
32665491Spendry credp->cr_uid = (uid_t) NOBODY;
32754947Sheideman
32865491Spendry #ifdef notdef
32965491Spendry /* cr_gid is the same as cr_groups[0] in 4BSD */
33065491Spendry
33154947Sheideman /* Find gid entry in map */
33254947Sheideman
33365491Spendry gid = (gid_t) umap_findid(credp->cr_gid, groupmap, gnentries);
33454947Sheideman
33565491Spendry if (gid != -1)
33665491Spendry credp->cr_gid = gid;
33765491Spendry else
33865491Spendry credp->cr_gid = NULLGROUP;
33965491Spendry #endif
34054947Sheideman
34154947Sheideman /* Now we must map each of the set of groups in the cr_groups
34254947Sheideman structure. */
34354947Sheideman
34454947Sheideman i = 0;
34565491Spendry while (credp->cr_groups[i] != 0) {
34665491Spendry gid = (gid_t) umap_findid(credp->cr_groups[i],
34765491Spendry groupmap, gnentries);
34865491Spendry
34965491Spendry if (gid != -1)
35065491Spendry credp->cr_groups[i++] = gid;
35154947Sheideman else
35265491Spendry credp->cr_groups[i++] = NULLGROUP;
35354947Sheideman }
35454947Sheideman }
355