154947Sheideman /* 263248Sbostic * Copyright (c) 1992, 1993 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*67717Smckusick * @(#)umap_subr.c 8.7 (Berkeley) 08/20/94 1154947Sheideman * 1265491Spendry * $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> 1754947Sheideman #include <sys/time.h> 1854947Sheideman #include <sys/types.h> 1954947Sheideman #include <sys/vnode.h> 2054947Sheideman #include <sys/mount.h> 2154947Sheideman #include <sys/namei.h> 2254947Sheideman #include <sys/malloc.h> 2355031Smckusick #include <miscfs/umapfs/umap.h> 2454947Sheideman 2554947Sheideman #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */ 2654947Sheideman #define NUMAPNODECACHE 16 2754947Sheideman 2854947Sheideman /* 2954947Sheideman * Null layer cache: 3054947Sheideman * Each cache entry holds a reference to the target vnode 3154947Sheideman * along with a pointer to the alias vnode. When an 3254947Sheideman * entry is added the target vnode is VREF'd. When the 3354947Sheideman * alias is removed the target vnode is vrele'd. 3454947Sheideman */ 3554947Sheideman 36*67717Smckusick #define UMAP_NHASH(vp) \ 37*67717Smckusick (&umap_node_hashtbl[(((u_long)vp)>>LOG2_SIZEVNODE) & umap_node_hash]) 38*67717Smckusick LIST_HEAD(umap_node_hashhead, umap_node) *umap_node_hashtbl; 39*67717Smckusick u_long umap_node_hash; 4054947Sheideman 4154947Sheideman /* 4254947Sheideman * Initialise cache headers 4354947Sheideman */ 4454947Sheideman umapfs_init() 4554947Sheideman { 46*67717Smckusick 4754947Sheideman #ifdef UMAPFS_DIAGNOSTIC 4854947Sheideman printf("umapfs_init\n"); /* printed during system boot */ 4954947Sheideman #endif 50*67717Smckusick umap_node_hashtbl = hashinit(NUMAPNODECACHE, M_CACHE, &umap_node_hash); 5154947Sheideman } 5254947Sheideman 5354947Sheideman /* 5465491Spendry * umap_findid is called by various routines in umap_vnodeops.c to 5565491Spendry * find a user or group id in a map. 5665491Spendry */ 5765491Spendry static u_long 5865491Spendry umap_findid(id, map, nentries) 5965491Spendry u_long id; 6065491Spendry u_long map[][2]; 6165491Spendry int nentries; 6265491Spendry { 6365491Spendry int i; 6465491Spendry 6565491Spendry /* Find uid entry in map */ 6665491Spendry i = 0; 6765491Spendry while ((i<nentries) && ((map[i][0]) != id)) 6865491Spendry i++; 6965491Spendry 7065491Spendry if (i < nentries) 7165491Spendry return (map[i][1]); 7265491Spendry else 7365491Spendry return (-1); 7465491Spendry 7565491Spendry } 7665491Spendry 7765491Spendry /* 7865491Spendry * umap_reverse_findid is called by umap_getattr() in umap_vnodeops.c to 7965491Spendry * find a user or group id in a map, in reverse. 8065491Spendry */ 8165491Spendry u_long 8265491Spendry umap_reverse_findid(id, map, nentries) 8365491Spendry u_long id; 8465491Spendry u_long map[][2]; 8565491Spendry int nentries; 8665491Spendry { 8765491Spendry int i; 8865491Spendry 8965491Spendry /* Find uid entry in map */ 9065491Spendry i = 0; 9165491Spendry while ((i<nentries) && ((map[i][1]) != id)) 9265491Spendry i++; 9365491Spendry 9465491Spendry if (i < nentries) 9565491Spendry return (map[i][0]); 9665491Spendry else 9765491Spendry return (-1); 9865491Spendry 9965491Spendry } 10065491Spendry 10165491Spendry /* 10254947Sheideman * Return alias for target vnode if already exists, else 0. 10354947Sheideman */ 10454958Sheideman static struct vnode * 10554947Sheideman umap_node_find(mp, targetvp) 10654947Sheideman struct mount *mp; 10754947Sheideman struct vnode *targetvp; 10854947Sheideman { 109*67717Smckusick struct umap_node_hashhead *hd; 11054947Sheideman struct umap_node *a; 11154958Sheideman struct vnode *vp; 11254947Sheideman 11354947Sheideman #ifdef UMAPFS_DIAGNOSTIC 11454947Sheideman printf("umap_node_find(mp = %x, target = %x)\n", mp, targetvp); 11554947Sheideman #endif 11654947Sheideman 11754947Sheideman /* 11854947Sheideman * Find hash base, and then search the (two-way) linked 11954947Sheideman * list looking for a umap_node structure which is referencing 12054947Sheideman * the target vnode. If found, the increment the umap_node 12154947Sheideman * reference count (but NOT the target vnode's VREF counter). 12254947Sheideman */ 123*67717Smckusick hd = UMAP_NHASH(targetvp); 124*67717Smckusick loop: 125*67717Smckusick for (a = hd->lh_first; a != 0; a = a->umap_hash.le_next) { 12655031Smckusick if (a->umap_lowervp == targetvp && 12755031Smckusick a->umap_vnode->v_mount == mp) { 12854958Sheideman vp = UMAPTOV(a); 12954958Sheideman /* 13054958Sheideman * We need vget for the VXLOCK 13154958Sheideman * stuff, but we don't want to lock 13254958Sheideman * the lower node. 13354958Sheideman */ 13465132Smckusick if (vget(vp, 0)) { 13565491Spendry #ifdef UMAPFS_DIAGNOSTIC 13665491Spendry printf ("umap_node_find: vget failed.\n"); 13765491Spendry #endif 13854958Sheideman goto loop; 13955031Smckusick } 14054958Sheideman return (vp); 14154947Sheideman } 14254947Sheideman } 14354947Sheideman 14454947Sheideman #ifdef UMAPFS_DIAGNOSTIC 14554947Sheideman printf("umap_node_find(%x, %x): NOT found\n", mp, targetvp); 14654947Sheideman #endif 14754947Sheideman 14854947Sheideman return (0); 14954947Sheideman } 15054947Sheideman 15154947Sheideman /* 15254958Sheideman * Make a new umap_node node. 15354958Sheideman * Vp is the alias vnode, lofsvp is the target vnode. 15454958Sheideman * Maintain a reference to (targetvp). 15554958Sheideman */ 15654958Sheideman static int 15754958Sheideman umap_node_alloc(mp, lowervp, vpp) 15854958Sheideman struct mount *mp; 15954958Sheideman struct vnode *lowervp; 16054958Sheideman struct vnode **vpp; 16154958Sheideman { 162*67717Smckusick struct umap_node_hashhead *hd; 16354958Sheideman struct umap_node *xp; 16454958Sheideman struct vnode *othervp, *vp; 16554958Sheideman int error; 16654958Sheideman 16765423Spendry if (error = getnewvnode(VT_UMAP, mp, umap_vnodeop_p, vpp)) 16865423Spendry return (error); 16954958Sheideman vp = *vpp; 17054958Sheideman 17155031Smckusick MALLOC(xp, struct umap_node *, sizeof(struct umap_node), 17255031Smckusick M_TEMP, M_WAITOK); 17354958Sheideman vp->v_type = lowervp->v_type; 17454958Sheideman xp->umap_vnode = vp; 17554958Sheideman vp->v_data = xp; 17654958Sheideman xp->umap_lowervp = lowervp; 17754958Sheideman /* 17854958Sheideman * Before we insert our new node onto the hash chains, 17954958Sheideman * check to see if someone else has beaten us to it. 18054958Sheideman * (We could have slept in MALLOC.) 18154958Sheideman */ 18254958Sheideman if (othervp = umap_node_find(lowervp)) { 18354958Sheideman FREE(xp, M_TEMP); 18454958Sheideman vp->v_type = VBAD; /* node is discarded */ 18554958Sheideman vp->v_usecount = 0; /* XXX */ 18654958Sheideman *vpp = othervp; 18755031Smckusick return (0); 18855031Smckusick } 18954958Sheideman VREF(lowervp); /* Extra VREF will be vrele'd in umap_node_create */ 190*67717Smckusick hd = UMAP_NHASH(lowervp); 191*67717Smckusick LIST_INSERT_HEAD(hd, xp, umap_hash); 19255031Smckusick return (0); 19354958Sheideman } 19454958Sheideman 19554958Sheideman 19654958Sheideman /* 19754947Sheideman * Try to find an existing umap_node vnode refering 19854947Sheideman * to it, otherwise make a new umap_node vnode which 19954947Sheideman * contains a reference to the target vnode. 20054947Sheideman */ 20154947Sheideman int 20254947Sheideman umap_node_create(mp, targetvp, newvpp) 20354947Sheideman struct mount *mp; 20454947Sheideman struct vnode *targetvp; 20554947Sheideman struct vnode **newvpp; 20654947Sheideman { 20754947Sheideman struct vnode *aliasvp; 20854947Sheideman 20954958Sheideman if (aliasvp = umap_node_find(mp, targetvp)) { 21054947Sheideman /* 21154947Sheideman * Take another reference to the alias vnode 21254947Sheideman */ 21354947Sheideman #ifdef UMAPFS_DIAGNOSTIC 21454947Sheideman vprint("umap_node_create: exists", ap->umap_vnode); 21554947Sheideman #endif 21654958Sheideman /* VREF(aliasvp); */ 21754947Sheideman } else { 21854947Sheideman int error; 21954947Sheideman 22054947Sheideman /* 22154947Sheideman * Get new vnode. 22254947Sheideman */ 22354947Sheideman #ifdef UMAPFS_DIAGNOSTIC 22454947Sheideman printf("umap_node_create: create new alias vnode\n"); 22554947Sheideman #endif 22654947Sheideman /* 22754947Sheideman * Make new vnode reference the umap_node. 22854947Sheideman */ 22954958Sheideman if (error = umap_node_alloc(mp, targetvp, &aliasvp)) 23055031Smckusick return (error); 23154947Sheideman 23254947Sheideman /* 23354947Sheideman * aliasvp is already VREF'd by getnewvnode() 23454947Sheideman */ 23554947Sheideman } 23654947Sheideman 23754947Sheideman vrele(targetvp); 23854947Sheideman 23954947Sheideman #ifdef UMAPFS_DIAGNOSTIC 24054947Sheideman vprint("umap_node_create: alias", aliasvp); 24154947Sheideman vprint("umap_node_create: target", targetvp); 24254947Sheideman #endif 24354947Sheideman 24454947Sheideman *newvpp = aliasvp; 24554947Sheideman return (0); 24654947Sheideman } 24755031Smckusick 24854947Sheideman #ifdef UMAPFS_DIAGNOSTIC 24954947Sheideman int umap_checkvp_barrier = 1; 25054947Sheideman struct vnode * 25154947Sheideman umap_checkvp(vp, fil, lno) 25254947Sheideman struct vnode *vp; 25354947Sheideman char *fil; 25454947Sheideman int lno; 25554947Sheideman { 25654947Sheideman struct umap_node *a = VTOUMAP(vp); 25754947Sheideman #if 0 25854947Sheideman /* 25954947Sheideman * Can't do this check because vop_reclaim runs 26054947Sheideman * with funny vop vector. 26154947Sheideman */ 26254947Sheideman if (vp->v_op != umap_vnodeop_p) { 26354947Sheideman printf ("umap_checkvp: on non-umap-node\n"); 26454947Sheideman while (umap_checkvp_barrier) /*WAIT*/ ; 26554947Sheideman panic("umap_checkvp"); 26655031Smckusick } 26754947Sheideman #endif 26854947Sheideman if (a->umap_lowervp == NULL) { 26954947Sheideman /* Should never happen */ 27054947Sheideman int i; u_long *p; 27154947Sheideman printf("vp = %x, ZERO ptr\n", vp); 27254947Sheideman for (p = (u_long *) a, i = 0; i < 8; i++) 27354947Sheideman printf(" %x", p[i]); 27454947Sheideman printf("\n"); 27554947Sheideman /* wait for debugger */ 27654947Sheideman while (umap_checkvp_barrier) /*WAIT*/ ; 27754947Sheideman panic("umap_checkvp"); 27854947Sheideman } 27954947Sheideman if (a->umap_lowervp->v_usecount < 1) { 28054947Sheideman int i; u_long *p; 28154947Sheideman printf("vp = %x, unref'ed lowervp\n", vp); 28254947Sheideman for (p = (u_long *) a, i = 0; i < 8; i++) 28354947Sheideman printf(" %x", p[i]); 28454947Sheideman printf("\n"); 28554947Sheideman /* wait for debugger */ 28654947Sheideman while (umap_checkvp_barrier) /*WAIT*/ ; 28754947Sheideman panic ("umap with unref'ed lowervp"); 28855031Smckusick } 28954947Sheideman #if 0 29054947Sheideman printf("umap %x/%d -> %x/%d [%s, %d]\n", 29154947Sheideman a->umap_vnode, a->umap_vnode->v_usecount, 29254947Sheideman a->umap_lowervp, a->umap_lowervp->v_usecount, 29354947Sheideman fil, lno); 29454947Sheideman #endif 29555031Smckusick return (a->umap_lowervp); 29654947Sheideman } 29754947Sheideman #endif 29854947Sheideman 29954947Sheideman /* umap_mapids maps all of the ids in a credential, both user and group. */ 30054947Sheideman 30165491Spendry void 30265491Spendry umap_mapids(v_mount, credp) 30354958Sheideman struct mount *v_mount; 30454947Sheideman struct ucred *credp; 30554947Sheideman { 30665916Smckusick int i, unentries, gnentries; 30765916Smckusick u_long *groupmap, *usermap; 30865491Spendry uid_t uid; 30965491Spendry gid_t gid; 31054947Sheideman 31154958Sheideman unentries = MOUNTTOUMAPMOUNT(v_mount)->info_nentries; 31254958Sheideman usermap = &(MOUNTTOUMAPMOUNT(v_mount)->info_mapdata[0][0]); 31354958Sheideman gnentries = MOUNTTOUMAPMOUNT(v_mount)->info_gnentries; 31454958Sheideman groupmap = &(MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata[0][0]); 31554958Sheideman 31654947Sheideman /* Find uid entry in map */ 31754947Sheideman 31865491Spendry uid = (uid_t) umap_findid(credp->cr_uid, usermap, unentries); 31954947Sheideman 32065491Spendry if (uid != -1) 32165491Spendry credp->cr_uid = uid; 32265491Spendry else 32365491Spendry credp->cr_uid = (uid_t) NOBODY; 32454947Sheideman 32565491Spendry #ifdef notdef 32665491Spendry /* cr_gid is the same as cr_groups[0] in 4BSD */ 32765491Spendry 32854947Sheideman /* Find gid entry in map */ 32954947Sheideman 33065491Spendry gid = (gid_t) umap_findid(credp->cr_gid, groupmap, gnentries); 33154947Sheideman 33265491Spendry if (gid != -1) 33365491Spendry credp->cr_gid = gid; 33465491Spendry else 33565491Spendry credp->cr_gid = NULLGROUP; 33665491Spendry #endif 33754947Sheideman 33854947Sheideman /* Now we must map each of the set of groups in the cr_groups 33954947Sheideman structure. */ 34054947Sheideman 34154947Sheideman i = 0; 34265491Spendry while (credp->cr_groups[i] != 0) { 34365491Spendry gid = (gid_t) umap_findid(credp->cr_groups[i], 34465491Spendry groupmap, gnentries); 34565491Spendry 34665491Spendry if (gid != -1) 34765491Spendry credp->cr_groups[i++] = gid; 34854947Sheideman else 34965491Spendry credp->cr_groups[i++] = NULLGROUP; 35054947Sheideman } 35154947Sheideman } 352