|
|
1.1 ! root 1: /* ! 2: * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. ! 3: * ! 4: * @APPLE_LICENSE_HEADER_START@ ! 5: * ! 6: * The contents of this file constitute Original Code as defined in and ! 7: * are subject to the Apple Public Source License Version 1.1 (the ! 8: * "License"). You may not use this file except in compliance with the ! 9: * License. Please obtain a copy of the License at ! 10: * http://www.apple.com/publicsource and read it before using this file. ! 11: * ! 12: * This Original Code and all software distributed under the License are ! 13: * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER ! 14: * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, ! 15: * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, ! 16: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the ! 17: * License for the specific language governing rights and limitations ! 18: * under the License. ! 19: * ! 20: * @APPLE_LICENSE_HEADER_END@ ! 21: */ ! 22: /* $NetBSD: umap_subr.c,v 1.4 1994/09/20 06:43:02 cgd Exp $ */ ! 23: ! 24: /* ! 25: * Copyright (c) 1992, 1993 ! 26: * The Regents of the University of California. All rights reserved. ! 27: * ! 28: * This code is derived from software donated to Berkeley by ! 29: * Jan-Simon Pendry. ! 30: * ! 31: * Redistribution and use in source and binary forms, with or without ! 32: * modification, are permitted provided that the following conditions ! 33: * are met: ! 34: * 1. Redistributions of source code must retain the above copyright ! 35: * notice, this list of conditions and the following disclaimer. ! 36: * 2. Redistributions in binary form must reproduce the above copyright ! 37: * notice, this list of conditions and the following disclaimer in the ! 38: * documentation and/or other materials provided with the distribution. ! 39: * 3. All advertising materials mentioning features or use of this software ! 40: * must display the following acknowledgement: ! 41: * This product includes software developed by the University of ! 42: * California, Berkeley and its contributors. ! 43: * 4. Neither the name of the University nor the names of its contributors ! 44: * may be used to endorse or promote products derived from this software ! 45: * without specific prior written permission. ! 46: * ! 47: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ! 48: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ! 49: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ! 50: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE ! 51: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ! 52: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ! 53: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ! 54: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ! 55: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ! 56: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ! 57: * SUCH DAMAGE. ! 58: * ! 59: * from: Id: lofs_subr.c, v 1.11 1992/05/30 10:05:43 jsp Exp ! 60: * @(#)umap_subr.c 8.6 (Berkeley) 1/26/94 ! 61: */ ! 62: ! 63: #include <sys/param.h> ! 64: #include <sys/systm.h> ! 65: #include <sys/time.h> ! 66: #include <sys/types.h> ! 67: #include <sys/vnode.h> ! 68: #include <sys/mount.h> ! 69: #include <sys/namei.h> ! 70: #include <sys/malloc.h> ! 71: #include <miscfs/specfs/specdev.h> ! 72: #include <miscfs/umapfs/umap.h> ! 73: ! 74: #define LOG2_SIZEVNODE 7 /* log2(sizeof struct vnode) */ ! 75: #define NUMAPNODECACHE 16 ! 76: ! 77: /* ! 78: * Null layer cache: ! 79: * Each cache entry holds a reference to the target vnode ! 80: * along with a pointer to the alias vnode. When an ! 81: * entry is added the target vnode is VREF'd. When the ! 82: * alias is removed the target vnode is vrele'd. ! 83: */ ! 84: ! 85: #define UMAP_NHASH(vp) \ ! 86: (&umap_node_hashtbl[(((u_long)vp)>>LOG2_SIZEVNODE) & umap_node_hash]) ! 87: LIST_HEAD(umap_node_hashhead, umap_node) *umap_node_hashtbl; ! 88: u_long umap_node_hash; ! 89: ! 90: /* ! 91: * Initialise cache headers ! 92: */ ! 93: umapfs_init() ! 94: { ! 95: ! 96: #ifdef UMAPFS_DIAGNOSTIC ! 97: printf("umapfs_init\n"); /* printed during system boot */ ! 98: #endif ! 99: umap_node_hashtbl = hashinit(NUMAPNODECACHE, M_CACHE, &umap_node_hash); ! 100: } ! 101: ! 102: /* ! 103: * umap_findid is called by various routines in umap_vnodeops.c to ! 104: * find a user or group id in a map. ! 105: */ ! 106: static u_long ! 107: umap_findid(id, map, nentries) ! 108: u_long id; ! 109: u_long map[][2]; ! 110: int nentries; ! 111: { ! 112: int i; ! 113: ! 114: /* Find uid entry in map */ ! 115: i = 0; ! 116: while ((i<nentries) && ((map[i][0]) != id)) ! 117: i++; ! 118: ! 119: if (i < nentries) ! 120: return (map[i][1]); ! 121: else ! 122: return (-1); ! 123: ! 124: } ! 125: ! 126: /* ! 127: * umap_reverse_findid is called by umap_getattr() in umap_vnodeops.c to ! 128: * find a user or group id in a map, in reverse. ! 129: */ ! 130: u_long ! 131: umap_reverse_findid(id, map, nentries) ! 132: u_long id; ! 133: u_long map[][2]; ! 134: int nentries; ! 135: { ! 136: int i; ! 137: ! 138: /* Find uid entry in map */ ! 139: i = 0; ! 140: while ((i<nentries) && ((map[i][1]) != id)) ! 141: i++; ! 142: ! 143: if (i < nentries) ! 144: return (map[i][0]); ! 145: else ! 146: return (-1); ! 147: ! 148: } ! 149: ! 150: /* ! 151: * Return alias for target vnode if already exists, else 0. ! 152: */ ! 153: static struct vnode * ! 154: umap_node_find(mp, targetvp) ! 155: struct mount *mp; ! 156: struct vnode *targetvp; ! 157: { ! 158: struct umap_node_hashhead *hd; ! 159: struct umap_node *a; ! 160: struct vnode *vp; ! 161: ! 162: #ifdef UMAPFS_DIAGNOSTIC ! 163: printf("umap_node_find(mp = %x, target = %x)\n", mp, targetvp); ! 164: #endif ! 165: ! 166: /* ! 167: * Find hash base, and then search the (two-way) linked ! 168: * list looking for a umap_node structure which is referencing ! 169: * the target vnode. If found, the increment the umap_node ! 170: * reference count (but NOT the target vnode's VREF counter). ! 171: */ ! 172: hd = UMAP_NHASH(targetvp); ! 173: loop: ! 174: for (a = hd->lh_first; a != 0; a = a->umap_hash.le_next) { ! 175: if (a->umap_lowervp == targetvp && ! 176: a->umap_vnode->v_mount == mp) { ! 177: vp = UMAPTOV(a); ! 178: /* ! 179: * We need vget for the VXLOCK ! 180: * stuff, but we don't want to lock ! 181: * the lower node. ! 182: */ ! 183: if (vget(vp, 0)) { ! 184: #ifdef UMAPFS_DIAGNOSTIC ! 185: printf ("umap_node_find: vget failed.\n"); ! 186: #endif ! 187: goto loop; ! 188: } ! 189: return (vp); ! 190: } ! 191: } ! 192: ! 193: #ifdef UMAPFS_DIAGNOSTIC ! 194: printf("umap_node_find(%x, %x): NOT found\n", mp, targetvp); ! 195: #endif ! 196: ! 197: return (0); ! 198: } ! 199: ! 200: /* ! 201: * Make a new umap_node node. ! 202: * Vp is the alias vnode, lowervp is the target vnode. ! 203: * Maintain a reference to lowervp. ! 204: */ ! 205: static int ! 206: umap_node_alloc(mp, lowervp, vpp) ! 207: struct mount *mp; ! 208: struct vnode *lowervp; ! 209: struct vnode **vpp; ! 210: { ! 211: struct umap_node_hashhead *hd; ! 212: struct umap_node *xp; ! 213: struct vnode *vp, *nvp; ! 214: int error; ! 215: extern int (**dead_vnodeop_p)(); ! 216: struct specinfo *sp = (struct specinfo *)0; ! 217: ! 218: if (lowervp->v_type == VBLK || lowervp->v_type == VCHR) ! 219: MALLOC_ZONE(sp, struct specinfo *, sizeof(struct specinfo), ! 220: M_VNODE, M_WAITOK); ! 221: ! 222: MALLOC(xp, struct umap_node *, sizeof(struct umap_node), M_TEMP, ! 223: M_WAITOK); ! 224: if (error = getnewvnode(VT_UMAP, mp, umap_vnodeop_p, &vp)) { ! 225: FREE(xp, M_TEMP); ! 226: if (sp) ! 227: FREE_ZONE(sp, sizeof (struct specinfo), M_VNODE); ! 228: return (error); ! 229: } ! 230: vp->v_type = lowervp->v_type; ! 231: ! 232: if (vp->v_type == VBLK || vp->v_type == VCHR) { ! 233: vp->v_specinfo = sp; ! 234: vp->v_rdev = lowervp->v_rdev; ! 235: } ! 236: ! 237: vp->v_data = xp; ! 238: xp->umap_vnode = vp; ! 239: xp->umap_lowervp = lowervp; ! 240: /* ! 241: * Before we insert our new node onto the hash chains, ! 242: * check to see if someone else has beaten us to it. ! 243: */ ! 244: if (nvp = umap_node_find(lowervp)) { ! 245: *vpp = nvp; ! 246: ! 247: /* free the substructures we've allocated. */ ! 248: FREE(xp, M_TEMP); ! 249: if (sp) { ! 250: vp->v_specinfo = (struct specinfo *)0; ! 251: FREE_ZONE(sp, sizeof (struct specinfo), M_VNODE); ! 252: } ! 253: ! 254: vp->v_type = VBAD; /* node is discarded */ ! 255: vp->v_op = dead_vnodeop_p; /* so ops will still work */ ! 256: vrele(vp); /* get rid of it. */ ! 257: return (0); ! 258: } ! 259: ! 260: /* ! 261: * XXX if it's a device node, it needs to be checkalias()ed. ! 262: * however, for locking reasons, that's just not possible. ! 263: * so we have to do most of the dirty work inline. Note that ! 264: * this is a limited case; we know that there's going to be ! 265: * an alias, and we know that that alias will be a "real" ! 266: * device node, i.e. not tagged VT_NON. ! 267: */ ! 268: if (vp->v_type == VBLK || vp->v_type == VCHR) { ! 269: struct vnode *cvp, **cvpp; ! 270: ! 271: cvpp = &speclisth[SPECHASH(vp->v_rdev)]; ! 272: loop: ! 273: for (cvp = *cvpp; cvp; cvp = cvp->v_specnext) { ! 274: if (vp->v_rdev != cvp->v_rdev || ! 275: vp->v_type != cvp->v_type) ! 276: continue; ! 277: ! 278: /* ! 279: * Alias, but not in use, so flush it out. ! 280: */ ! 281: if (cvp->v_usecount == 0) { ! 282: vgone(cvp); ! 283: goto loop; ! 284: } ! 285: if (vget(cvp, 0)) /* can't lock; will die! */ ! 286: goto loop; ! 287: break; ! 288: } ! 289: ! 290: vp->v_hashchain = cvpp; ! 291: vp->v_specnext = *cvpp; ! 292: vp->v_specflags = 0; ! 293: *cvpp = vp; ! 294: #if DIAGNOSTIC ! 295: if (cvp == NULLVP) ! 296: panic("umap_node_alloc: no alias for device"); ! 297: #endif ! 298: vp->v_flag |= VALIASED; ! 299: cvp->v_flag |= VALIASED; ! 300: vrele(cvp); ! 301: } ! 302: /* XXX end of transmogrified checkalias() */ ! 303: ! 304: *vpp = vp; ! 305: VREF(lowervp); /* Extra VREF will be vrele'd in umap_node_create */ ! 306: hd = UMAP_NHASH(lowervp); ! 307: LIST_INSERT_HEAD(hd, xp, umap_hash); ! 308: return (0); ! 309: } ! 310: ! 311: ! 312: /* ! 313: * Try to find an existing umap_node vnode refering ! 314: * to it, otherwise make a new umap_node vnode which ! 315: * contains a reference to the target vnode. ! 316: */ ! 317: int ! 318: umap_node_create(mp, targetvp, newvpp) ! 319: struct mount *mp; ! 320: struct vnode *targetvp; ! 321: struct vnode **newvpp; ! 322: { ! 323: struct vnode *aliasvp; ! 324: ! 325: if (aliasvp = umap_node_find(mp, targetvp)) { ! 326: /* ! 327: * Take another reference to the alias vnode ! 328: */ ! 329: #ifdef UMAPFS_DIAGNOSTIC ! 330: vprint("umap_node_create: exists", ap->umap_vnode); ! 331: #endif ! 332: /* VREF(aliasvp); */ ! 333: } else { ! 334: int error; ! 335: ! 336: /* ! 337: * Get new vnode. ! 338: */ ! 339: #ifdef UMAPFS_DIAGNOSTIC ! 340: printf("umap_node_create: create new alias vnode\n"); ! 341: #endif ! 342: /* ! 343: * Make new vnode reference the umap_node. ! 344: */ ! 345: if (error = umap_node_alloc(mp, targetvp, &aliasvp)) ! 346: return (error); ! 347: ! 348: /* ! 349: * aliasvp is already VREF'd by getnewvnode() ! 350: */ ! 351: } ! 352: ! 353: vrele(targetvp); ! 354: ! 355: #ifdef UMAPFS_DIAGNOSTIC ! 356: vprint("umap_node_create: alias", aliasvp); ! 357: vprint("umap_node_create: target", targetvp); ! 358: #endif ! 359: ! 360: *newvpp = aliasvp; ! 361: return (0); ! 362: } ! 363: ! 364: #ifdef UMAPFS_DIAGNOSTIC ! 365: int umap_checkvp_barrier = 1; ! 366: struct vnode * ! 367: umap_checkvp(vp, fil, lno) ! 368: struct vnode *vp; ! 369: char *fil; ! 370: int lno; ! 371: { ! 372: struct umap_node *a = VTOUMAP(vp); ! 373: #if 0 ! 374: /* ! 375: * Can't do this check because vop_reclaim runs ! 376: * with funny vop vector. ! 377: */ ! 378: if (vp->v_op != umap_vnodeop_p) { ! 379: printf ("umap_checkvp: on non-umap-node\n"); ! 380: while (umap_checkvp_barrier) /*WAIT*/ ; ! 381: panic("umap_checkvp"); ! 382: } ! 383: #endif ! 384: if (a->umap_lowervp == NULL) { ! 385: /* Should never happen */ ! 386: int i; u_long *p; ! 387: printf("vp = %x, ZERO ptr\n", vp); ! 388: for (p = (u_long *) a, i = 0; i < 8; i++) ! 389: printf(" %x", p[i]); ! 390: printf("\n"); ! 391: /* wait for debugger */ ! 392: while (umap_checkvp_barrier) /*WAIT*/ ; ! 393: panic("umap_checkvp"); ! 394: } ! 395: if (a->umap_lowervp->v_usecount < 1) { ! 396: int i; u_long *p; ! 397: printf("vp = %x, unref'ed lowervp\n", vp); ! 398: for (p = (u_long *) a, i = 0; i < 8; i++) ! 399: printf(" %x", p[i]); ! 400: printf("\n"); ! 401: /* wait for debugger */ ! 402: while (umap_checkvp_barrier) /*WAIT*/ ; ! 403: panic ("umap with unref'ed lowervp"); ! 404: } ! 405: #if 0 ! 406: printf("umap %x/%d -> %x/%d [%s, %d]\n", ! 407: a->umap_vnode, a->umap_vnode->v_usecount, ! 408: a->umap_lowervp, a->umap_lowervp->v_usecount, ! 409: fil, lno); ! 410: #endif ! 411: return (a->umap_lowervp); ! 412: } ! 413: #endif ! 414: ! 415: /* umap_mapids maps all of the ids in a credential, both user and group. */ ! 416: ! 417: void ! 418: umap_mapids(v_mount, credp) ! 419: struct mount *v_mount; ! 420: struct ucred *credp; ! 421: { ! 422: int i, unentries, gnentries; ! 423: uid_t uid, *usermap; ! 424: gid_t gid, *groupmap; ! 425: ! 426: unentries = MOUNTTOUMAPMOUNT(v_mount)->info_nentries; ! 427: usermap = &(MOUNTTOUMAPMOUNT(v_mount)->info_mapdata[0][0]); ! 428: gnentries = MOUNTTOUMAPMOUNT(v_mount)->info_gnentries; ! 429: groupmap = &(MOUNTTOUMAPMOUNT(v_mount)->info_gmapdata[0][0]); ! 430: ! 431: /* Find uid entry in map */ ! 432: ! 433: uid = (uid_t) umap_findid(credp->cr_uid, usermap, unentries); ! 434: ! 435: if (uid != -1) ! 436: credp->cr_uid = uid; ! 437: else ! 438: credp->cr_uid = (uid_t) NOBODY; ! 439: ! 440: #ifdef notdef ! 441: /* cr_gid is the same as cr_groups[0] in 4BSD */ ! 442: ! 443: /* Find gid entry in map */ ! 444: ! 445: gid = (gid_t) umap_findid(credp->cr_gid, groupmap, gnentries); ! 446: ! 447: if (gid != -1) ! 448: credp->cr_gid = gid; ! 449: else ! 450: credp->cr_gid = NULLGROUP; ! 451: #endif ! 452: ! 453: /* Now we must map each of the set of groups in the cr_groups ! 454: structure. */ ! 455: ! 456: i = 0; ! 457: while (credp->cr_groups[i] != 0) { ! 458: gid = (gid_t) umap_findid(credp->cr_groups[i], ! 459: groupmap, gnentries); ! 460: ! 461: if (gid != -1) ! 462: credp->cr_groups[i++] = gid; ! 463: else ! 464: credp->cr_groups[i++] = NULLGROUP; ! 465: } ! 466: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.