Home | History | Annotate | Line # | Download | only in umapfs
umap_subr.c revision 1.1.1.1
      1      1.1  mycroft /*
      2      1.1  mycroft  * Copyright (c) 1992, 1993
      3      1.1  mycroft  *	The Regents of the University of California.  All rights reserved.
      4      1.1  mycroft  *
      5      1.1  mycroft  * This code is derived from software donated to Berkeley by
      6      1.1  mycroft  * Jan-Simon Pendry.
      7      1.1  mycroft  *
      8      1.1  mycroft  * Redistribution and use in source and binary forms, with or without
      9      1.1  mycroft  * modification, are permitted provided that the following conditions
     10      1.1  mycroft  * are met:
     11      1.1  mycroft  * 1. Redistributions of source code must retain the above copyright
     12      1.1  mycroft  *    notice, this list of conditions and the following disclaimer.
     13      1.1  mycroft  * 2. Redistributions in binary form must reproduce the above copyright
     14      1.1  mycroft  *    notice, this list of conditions and the following disclaimer in the
     15      1.1  mycroft  *    documentation and/or other materials provided with the distribution.
     16      1.1  mycroft  * 3. All advertising materials mentioning features or use of this software
     17      1.1  mycroft  *    must display the following acknowledgement:
     18      1.1  mycroft  *	This product includes software developed by the University of
     19      1.1  mycroft  *	California, Berkeley and its contributors.
     20      1.1  mycroft  * 4. Neither the name of the University nor the names of its contributors
     21      1.1  mycroft  *    may be used to endorse or promote products derived from this software
     22      1.1  mycroft  *    without specific prior written permission.
     23      1.1  mycroft  *
     24      1.1  mycroft  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25      1.1  mycroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26      1.1  mycroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27      1.1  mycroft  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28      1.1  mycroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29      1.1  mycroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30      1.1  mycroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31      1.1  mycroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32      1.1  mycroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33      1.1  mycroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34      1.1  mycroft  * SUCH DAMAGE.
     35      1.1  mycroft  *
     36  1.1.1.1     fvdl  *	@(#)umap_subr.c	8.6 (Berkeley) 1/26/94
     37  1.1.1.1     fvdl  *
     38  1.1.1.1     fvdl  * $Id: umap_subr.c,v 1.1.1.1 1998/03/01 02:10:03 fvdl Exp $
     39      1.1  mycroft  */
     40      1.1  mycroft 
     41      1.1  mycroft #include <sys/param.h>
     42      1.1  mycroft #include <sys/systm.h>
     43      1.1  mycroft #include <sys/time.h>
     44      1.1  mycroft #include <sys/types.h>
     45      1.1  mycroft #include <sys/vnode.h>
     46      1.1  mycroft #include <sys/mount.h>
     47      1.1  mycroft #include <sys/namei.h>
     48      1.1  mycroft #include <sys/malloc.h>
     49      1.1  mycroft #include <miscfs/umapfs/umap.h>
     50      1.1  mycroft 
     51      1.1  mycroft #define LOG2_SIZEVNODE 7		/* log2(sizeof struct vnode) */
     52      1.1  mycroft #define	NUMAPNODECACHE 16
     53      1.1  mycroft #define	UMAP_NHASH(vp) ((((u_long) vp)>>LOG2_SIZEVNODE) & (NUMAPNODECACHE-1))
     54      1.1  mycroft 
     55      1.1  mycroft /*
     56      1.1  mycroft  * Null layer cache:
     57      1.1  mycroft  * Each cache entry holds a reference to the target vnode
     58      1.1  mycroft  * along with a pointer to the alias vnode.  When an
     59      1.1  mycroft  * entry is added the target vnode is VREF'd.  When the
     60      1.1  mycroft  * alias is removed the target vnode is vrele'd.
     61      1.1  mycroft  */
     62      1.1  mycroft 
     63      1.1  mycroft /*
     64      1.1  mycroft  * Cache head
     65      1.1  mycroft  */
     66      1.1  mycroft struct umap_node_cache {
     67      1.1  mycroft 	struct umap_node	*ac_forw;
     68      1.1  mycroft 	struct umap_node	*ac_back;
     69      1.1  mycroft };
     70      1.1  mycroft 
     71      1.1  mycroft static struct umap_node_cache umap_node_cache[NUMAPNODECACHE];
     72      1.1  mycroft 
     73      1.1  mycroft /*
     74      1.1  mycroft  * Initialise cache headers
     75      1.1  mycroft  */
     76      1.1  mycroft umapfs_init()
     77      1.1  mycroft {
     78      1.1  mycroft 	struct umap_node_cache *ac;
     79      1.1  mycroft #ifdef UMAPFS_DIAGNOSTIC
     80      1.1  mycroft 	printf("umapfs_init\n");		/* printed during system boot */
     81      1.1  mycroft #endif
     82      1.1  mycroft 
     83      1.1  mycroft 	for (ac = umap_node_cache; ac < umap_node_cache + NUMAPNODECACHE; ac++)
     84      1.1  mycroft 		ac->ac_forw = ac->ac_back = (struct umap_node *) ac;
     85      1.1  mycroft }
     86      1.1  mycroft 
     87      1.1  mycroft /*
     88      1.1  mycroft  * Compute hash list for given target vnode
     89      1.1  mycroft  */
     90      1.1  mycroft static struct umap_node_cache *
     91      1.1  mycroft umap_node_hash(targetvp)
     92      1.1  mycroft 	struct vnode *targetvp;
     93      1.1  mycroft {
     94      1.1  mycroft 
     95      1.1  mycroft 	return (&umap_node_cache[UMAP_NHASH(targetvp)]);
     96      1.1  mycroft }
     97      1.1  mycroft 
     98      1.1  mycroft /*
     99      1.1  mycroft  * umap_findid is called by various routines in umap_vnodeops.c to
    100      1.1  mycroft  * find a user or group id in a map.
    101      1.1  mycroft  */
    102      1.1  mycroft static u_long
    103      1.1  mycroft umap_findid(id, map, nentries)
    104      1.1  mycroft 	u_long id;
    105      1.1  mycroft 	u_long map[][2];
    106      1.1  mycroft 	int nentries;
    107      1.1  mycroft {
    108      1.1  mycroft 	int i;
    109      1.1  mycroft 
    110      1.1  mycroft 	/* Find uid entry in map */
    111      1.1  mycroft 	i = 0;
    112      1.1  mycroft 	while ((i<nentries) && ((map[i][0]) != id))
    113      1.1  mycroft 		i++;
    114      1.1  mycroft 
    115      1.1  mycroft 	if (i < nentries)
    116      1.1  mycroft 		return (map[i][1]);
    117      1.1  mycroft 	else
    118      1.1  mycroft 		return (-1);
    119      1.1  mycroft 
    120      1.1  mycroft }
    121      1.1  mycroft 
    122      1.1  mycroft /*
    123      1.1  mycroft  * umap_reverse_findid is called by umap_getattr() in umap_vnodeops.c to
    124      1.1  mycroft  * find a user or group id in a map, in reverse.
    125      1.1  mycroft  */
    126      1.1  mycroft u_long
    127      1.1  mycroft umap_reverse_findid(id, map, nentries)
    128      1.1  mycroft 	u_long id;
    129      1.1  mycroft 	u_long map[][2];
    130      1.1  mycroft 	int nentries;
    131      1.1  mycroft {
    132      1.1  mycroft 	int i;
    133      1.1  mycroft 
    134      1.1  mycroft 	/* Find uid entry in map */
    135      1.1  mycroft 	i = 0;
    136      1.1  mycroft 	while ((i<nentries) && ((map[i][1]) != id))
    137      1.1  mycroft 		i++;
    138      1.1  mycroft 
    139      1.1  mycroft 	if (i < nentries)
    140      1.1  mycroft 		return (map[i][0]);
    141      1.1  mycroft 	else
    142      1.1  mycroft 		return (-1);
    143      1.1  mycroft 
    144      1.1  mycroft }
    145      1.1  mycroft 
    146      1.1  mycroft /*
    147      1.1  mycroft  * Return alias for target vnode if already exists, else 0.
    148      1.1  mycroft  */
    149      1.1  mycroft static struct vnode *
    150      1.1  mycroft umap_node_find(mp, targetvp)
    151      1.1  mycroft 	struct mount *mp;
    152      1.1  mycroft 	struct vnode *targetvp;
    153      1.1  mycroft {
    154      1.1  mycroft 	struct umap_node_cache *hd;
    155      1.1  mycroft 	struct umap_node *a;
    156      1.1  mycroft 	struct vnode *vp;
    157      1.1  mycroft 
    158      1.1  mycroft #ifdef UMAPFS_DIAGNOSTIC
    159      1.1  mycroft 	printf("umap_node_find(mp = %x, target = %x)\n", mp, targetvp);
    160      1.1  mycroft #endif
    161      1.1  mycroft 
    162      1.1  mycroft 	/*
    163      1.1  mycroft 	 * Find hash base, and then search the (two-way) linked
    164      1.1  mycroft 	 * list looking for a umap_node structure which is referencing
    165      1.1  mycroft 	 * the target vnode.  If found, the increment the umap_node
    166      1.1  mycroft 	 * reference count (but NOT the target vnode's VREF counter).
    167      1.1  mycroft 	 */
    168      1.1  mycroft 	hd = umap_node_hash(targetvp);
    169      1.1  mycroft 
    170      1.1  mycroft  loop:
    171      1.1  mycroft 	for (a = hd->ac_forw; a != (struct umap_node *) hd; a = a->umap_forw) {
    172      1.1  mycroft 		if (a->umap_lowervp == targetvp &&
    173      1.1  mycroft 		    a->umap_vnode->v_mount == mp) {
    174      1.1  mycroft 			vp = UMAPTOV(a);
    175      1.1  mycroft 			/*
    176      1.1  mycroft 			 * We need vget for the VXLOCK
    177      1.1  mycroft 			 * stuff, but we don't want to lock
    178      1.1  mycroft 			 * the lower node.
    179      1.1  mycroft 			 */
    180      1.1  mycroft 			if (vget(vp, 0)) {
    181      1.1  mycroft #ifdef UMAPFS_DIAGNOSTIC
    182      1.1  mycroft 				printf ("umap_node_find: vget failed.\n");
    183      1.1  mycroft #endif
    184      1.1  mycroft 				goto loop;
    185      1.1  mycroft 			}
    186      1.1  mycroft 			return (vp);
    187      1.1  mycroft 		}
    188      1.1  mycroft 	}
    189      1.1  mycroft 
    190      1.1  mycroft #ifdef UMAPFS_DIAGNOSTIC
    191      1.1  mycroft 	printf("umap_node_find(%x, %x): NOT found\n", mp, targetvp);
    192      1.1  mycroft #endif
    193      1.1  mycroft 
    194      1.1  mycroft 	return (0);
    195      1.1  mycroft }
    196      1.1  mycroft 
    197      1.1  mycroft /*
    198      1.1  mycroft  * Make a new umap_node node.
    199      1.1  mycroft  * Vp is the alias vnode, lofsvp is the target vnode.
    200      1.1  mycroft  * Maintain a reference to (targetvp).
    201      1.1  mycroft  */
    202      1.1  mycroft static int
    203      1.1  mycroft umap_node_alloc(mp, lowervp, vpp)
    204      1.1  mycroft 	struct mount *mp;
    205      1.1  mycroft 	struct vnode *lowervp;
    206      1.1  mycroft 	struct vnode **vpp;
    207      1.1  mycroft {
    208      1.1  mycroft 	struct umap_node_cache *hd;
    209      1.1  mycroft 	struct umap_node *xp;
    210      1.1  mycroft 	struct vnode *othervp, *vp;
    211      1.1  mycroft 	int error;
    212      1.1  mycroft 
    213      1.1  mycroft 	if (error = getnewvnode(VT_UMAP, mp, umap_vnodeop_p, vpp))
    214      1.1  mycroft 		return (error);
    215      1.1  mycroft 	vp = *vpp;
    216      1.1  mycroft 
    217      1.1  mycroft 	MALLOC(xp, struct umap_node *, sizeof(struct umap_node),
    218      1.1  mycroft 	    M_TEMP, M_WAITOK);
    219      1.1  mycroft 	vp->v_type = lowervp->v_type;
    220      1.1  mycroft 	xp->umap_vnode = vp;
    221      1.1  mycroft 	vp->v_data = xp;
    222      1.1  mycroft 	xp->umap_lowervp = lowervp;
    223      1.1  mycroft 	/*
    224      1.1  mycroft 	 * Before we insert our new node onto the hash chains,
    225      1.1  mycroft 	 * check to see if someone else has beaten us to it.
    226      1.1  mycroft 	 * (We could have slept in MALLOC.)
    227      1.1  mycroft 	 */
    228      1.1  mycroft 	if (othervp = umap_node_find(lowervp)) {
    229      1.1  mycroft 		FREE(xp, M_TEMP);
    230      1.1  mycroft 		vp->v_type = VBAD;	/* node is discarded */
    231      1.1  mycroft 		vp->v_usecount = 0;	/* XXX */
    232      1.1  mycroft 		*vpp = othervp;
    233      1.1  mycroft 		return (0);
    234      1.1  mycroft 	}
    235      1.1  mycroft 	VREF(lowervp);   /* Extra VREF will be vrele'd in umap_node_create */
    236      1.1  mycroft 	hd = umap_node_hash(lowervp);
    237      1.1  mycroft 	insque(xp, hd);
    238      1.1  mycroft 	return (0);
    239      1.1  mycroft }
    240      1.1  mycroft 
    241      1.1  mycroft 
    242      1.1  mycroft /*
    243      1.1  mycroft  * Try to find an existing umap_node vnode refering
    244      1.1  mycroft  * to it, otherwise make a new umap_node vnode which
    245      1.1  mycroft  * contains a reference to the target vnode.
    246      1.1  mycroft  */
    247      1.1  mycroft int
    248      1.1  mycroft umap_node_create(mp, targetvp, newvpp)
    249      1.1  mycroft 	struct mount *mp;
    250      1.1  mycroft 	struct vnode *targetvp;
    251      1.1  mycroft 	struct vnode **newvpp;
    252      1.1  mycroft {
    253      1.1  mycroft 	struct vnode *aliasvp;
    254      1.1  mycroft 
    255      1.1  mycroft 	if (aliasvp = umap_node_find(mp, targetvp)) {
    256      1.1  mycroft 		/*
    257      1.1  mycroft 		 * Take another reference to the alias vnode
    258      1.1  mycroft 		 */
    259      1.1  mycroft #ifdef UMAPFS_DIAGNOSTIC
    260      1.1  mycroft 		vprint("umap_node_create: exists", ap->umap_vnode);
    261      1.1  mycroft #endif
    262      1.1  mycroft 		/* VREF(aliasvp); */
    263      1.1  mycroft 	} else {
    264      1.1  mycroft 		int error;
    265      1.1  mycroft 
    266      1.1  mycroft 		/*
    267      1.1  mycroft 		 * Get new vnode.
    268      1.1  mycroft 		 */
    269      1.1  mycroft #ifdef UMAPFS_DIAGNOSTIC
    270      1.1  mycroft 		printf("umap_node_create: create new alias vnode\n");
    271      1.1  mycroft #endif
    272      1.1  mycroft 		/*
    273      1.1  mycroft 		 * Make new vnode reference the umap_node.
    274      1.1  mycroft 		 */
    275      1.1  mycroft 		if (error = umap_node_alloc(mp, targetvp, &aliasvp))
    276      1.1  mycroft 			return (error);
    277      1.1  mycroft 
    278      1.1  mycroft 		/*
    279      1.1  mycroft 		 * aliasvp is already VREF'd by getnewvnode()
    280      1.1  mycroft 		 */
    281      1.1  mycroft 	}
    282      1.1  mycroft 
    283      1.1  mycroft 	vrele(targetvp);
    284      1.1  mycroft 
    285      1.1  mycroft #ifdef UMAPFS_DIAGNOSTIC
    286      1.1  mycroft 	vprint("umap_node_create: alias", aliasvp);
    287      1.1  mycroft 	vprint("umap_node_create: target", targetvp);
    288      1.1  mycroft #endif
    289      1.1  mycroft 
    290      1.1  mycroft 	*newvpp = aliasvp;
    291      1.1  mycroft 	return (0);
    292      1.1  mycroft }
    293      1.1  mycroft 
    294      1.1  mycroft #ifdef UMAPFS_DIAGNOSTIC
    295      1.1  mycroft int umap_checkvp_barrier = 1;
    296      1.1  mycroft struct vnode *
    297      1.1  mycroft umap_checkvp(vp, fil, lno)
    298      1.1  mycroft 	struct vnode *vp;
    299      1.1  mycroft 	char *fil;
    300      1.1  mycroft 	int lno;
    301      1.1  mycroft {
    302      1.1  mycroft 	struct umap_node *a = VTOUMAP(vp);
    303      1.1  mycroft #if 0
    304      1.1  mycroft 	/*
    305      1.1  mycroft 	 * Can't do this check because vop_reclaim runs
    306      1.1  mycroft 	 * with funny vop vector.
    307      1.1  mycroft 	 */
    308      1.1  mycroft 	if (vp->v_op != umap_vnodeop_p) {
    309      1.1  mycroft 		printf ("umap_checkvp: on non-umap-node\n");
    310      1.1  mycroft 		while (umap_checkvp_barrier) /*WAIT*/ ;
    311      1.1  mycroft 		panic("umap_checkvp");
    312      1.1  mycroft 	}
    313      1.1  mycroft #endif
    314      1.1  mycroft 	if (a->umap_lowervp == NULL) {
    315      1.1  mycroft 		/* Should never happen */
    316      1.1  mycroft 		int i; u_long *p;
    317      1.1  mycroft 		printf("vp = %x, ZERO ptr\n", vp);
    318      1.1  mycroft 		for (p = (u_long *) a, i = 0; i < 8; i++)
    319      1.1  mycroft 			printf(" %x", p[i]);
    320      1.1  mycroft 		printf("\n");
    321      1.1  mycroft 		/* wait for debugger */
    322      1.1  mycroft 		while (umap_checkvp_barrier) /*WAIT*/ ;
    323      1.1  mycroft 		panic("umap_checkvp");
    324      1.1  mycroft 	}
    325      1.1  mycroft 	if (a->umap_lowervp->v_usecount < 1) {
    326      1.1  mycroft 		int i; u_long *p;
    327      1.1  mycroft 		printf("vp = %x, unref'ed lowervp\n", vp);
    328      1.1  mycroft 		for (p = (u_long *) a, i = 0; i < 8; i++)
    329      1.1  mycroft 			printf(" %x", p[i]);
    330      1.1  mycroft 		printf("\n");
    331      1.1  mycroft 		/* wait for debugger */
    332      1.1  mycroft 		while (umap_checkvp_barrier) /*WAIT*/ ;
    333      1.1  mycroft 		panic ("umap with unref'ed lowervp");
    334      1.1  mycroft 	}
    335      1.1  mycroft #if 0
    336      1.1  mycroft 	printf("umap %x/%d -> %x/%d [%s, %d]\n",
    337      1.1  mycroft 	        a->umap_vnode, a->umap_vnode->v_usecount,
    338      1.1  mycroft 		a->umap_lowervp, a->umap_lowervp->v_usecount,
    339      1.1  mycroft 		fil, lno);
    340      1.1  mycroft #endif
    341      1.1  mycroft 	return (a->umap_lowervp);
    342      1.1  mycroft }
    343      1.1  mycroft #endif
    344      1.1  mycroft 
    345      1.1  mycroft /* umap_mapids maps all of the ids in a credential, both user and group. */
    346      1.1  mycroft 
    347      1.1  mycroft void
    348      1.1  mycroft umap_mapids(v_mount, credp)
    349      1.1  mycroft 	struct mount *v_mount;
    350      1.1  mycroft 	struct ucred *credp;
    351      1.1  mycroft {
    352      1.1  mycroft 	int i, unentries, gnentries;
    353  1.1.1.1     fvdl 	u_long *groupmap, *usermap;
    354  1.1.1.1     fvdl 	uid_t uid;
    355  1.1.1.1     fvdl 	gid_t gid;
    356      1.1  mycroft 
    357      1.1  mycroft 	unentries =  MOUNTTOUMAPMOUNT(v_mount)->info_nentries;
    358      1.1  mycroft 	usermap =  &(MOUNTTOUMAPMOUNT(v_mount)->info_mapdata[0][0]);
    359      1.1  mycroft 	gnentries =  MOUNTTOUMAPMOUNT(v_mount)->info_gnentries;
    360      1.1  mycroft 	groupmap =  &(MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata[0][0]);
    361      1.1  mycroft 
    362      1.1  mycroft 	/* Find uid entry in map */
    363      1.1  mycroft 
    364      1.1  mycroft 	uid = (uid_t) umap_findid(credp->cr_uid, usermap, unentries);
    365      1.1  mycroft 
    366      1.1  mycroft 	if (uid != -1)
    367      1.1  mycroft 		credp->cr_uid = uid;
    368      1.1  mycroft 	else
    369      1.1  mycroft 		credp->cr_uid = (uid_t) NOBODY;
    370      1.1  mycroft 
    371      1.1  mycroft #ifdef notdef
    372      1.1  mycroft 	/* cr_gid is the same as cr_groups[0] in 4BSD */
    373      1.1  mycroft 
    374      1.1  mycroft 	/* Find gid entry in map */
    375      1.1  mycroft 
    376      1.1  mycroft 	gid = (gid_t) umap_findid(credp->cr_gid, groupmap, gnentries);
    377      1.1  mycroft 
    378      1.1  mycroft 	if (gid != -1)
    379      1.1  mycroft 		credp->cr_gid = gid;
    380      1.1  mycroft 	else
    381      1.1  mycroft 		credp->cr_gid = NULLGROUP;
    382      1.1  mycroft #endif
    383      1.1  mycroft 
    384      1.1  mycroft 	/* Now we must map each of the set of groups in the cr_groups
    385      1.1  mycroft 		structure. */
    386      1.1  mycroft 
    387      1.1  mycroft 	i = 0;
    388      1.1  mycroft 	while (credp->cr_groups[i] != 0) {
    389      1.1  mycroft 		gid = (gid_t) umap_findid(credp->cr_groups[i],
    390      1.1  mycroft 					groupmap, gnentries);
    391      1.1  mycroft 
    392      1.1  mycroft 		if (gid != -1)
    393      1.1  mycroft 			credp->cr_groups[i++] = gid;
    394      1.1  mycroft 		else
    395      1.1  mycroft 			credp->cr_groups[i++] = NULLGROUP;
    396      1.1  mycroft 	}
    397      1.1  mycroft }
    398