1 /* 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software donated to Berkeley by 6 * the UCLA Ficus project. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * from: @(#)umap_vnops.c 8.3 (Berkeley) 1/5/94 37 * $Id: umap_vnops.c,v 1.1 1994/06/08 11:33:54 mycroft Exp $ 38 */ 39 40 /* 41 * Umap Layer 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/time.h> 47 #include <sys/types.h> 48 #include <sys/vnode.h> 49 #include <sys/mount.h> 50 #include <sys/namei.h> 51 #include <sys/malloc.h> 52 #include <sys/buf.h> 53 #include <miscfs/umapfs/umap.h> 54 55 56 int umap_bug_bypass = 0; /* for debugging: enables bypass printf'ing */ 57 58 /* 59 * This is the 10-Apr-92 bypass routine. 60 * See null_vnops.c:null_bypass for more details. 61 */ 62 int 63 umap_bypass(ap) 64 struct vop_generic_args /* { 65 struct vnodeop_desc *a_desc; 66 <other random data follows, presumably> 67 } */ *ap; 68 { 69 extern int (**umap_vnodeop_p)(); /* not extern, really "forward" */ 70 struct ucred **credpp = 0, *credp = 0; 71 struct ucred *savecredp, *savecompcredp = 0; 72 struct ucred *compcredp = 0; 73 struct vnode **this_vp_p; 74 int error; 75 struct vnode *old_vps[VDESC_MAX_VPS]; 76 struct vnode *vp1 = 0; 77 struct vnode **vps_p[VDESC_MAX_VPS]; 78 struct vnode ***vppp; 79 struct vnodeop_desc *descp = ap->a_desc; 80 int reles, i; 81 struct componentname **compnamepp = 0; 82 83 if (umap_bug_bypass) 84 printf ("umap_bypass: %s\n", descp->vdesc_name); 85 86 #ifdef SAFETY 87 /* 88 * We require at least one vp. 89 */ 90 if (descp->vdesc_vp_offsets == NULL || 91 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET) 92 panic ("umap_bypass: no vp's in map.\n"); 93 #endif 94 95 /* 96 * Map the vnodes going in. 97 * Later, we'll invoke the operation based on 98 * the first mapped vnode's operation vector. 99 */ 100 reles = descp->vdesc_flags; 101 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 102 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 103 break; /* bail out at end of list */ 104 vps_p[i] = this_vp_p = 105 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i], ap); 106 107 if (i == 0) { 108 vp1 = *vps_p[0]; 109 } 110 111 /* 112 * We're not guaranteed that any but the first vnode 113 * are of our type. Check for and don't map any 114 * that aren't. (Must map first vp or vclean fails.) 115 */ 116 117 if (i && (*this_vp_p)->v_op != umap_vnodeop_p) { 118 old_vps[i] = NULL; 119 } else { 120 old_vps[i] = *this_vp_p; 121 *(vps_p[i]) = UMAPVPTOLOWERVP(*this_vp_p); 122 if (reles & 1) 123 VREF(*this_vp_p); 124 } 125 126 } 127 128 /* 129 * Fix the credentials. (That's the purpose of this layer.) 130 */ 131 132 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) { 133 134 credpp = VOPARG_OFFSETTO(struct ucred**, 135 descp->vdesc_cred_offset, ap); 136 137 /* Save old values */ 138 139 savecredp = *credpp; 140 if (savecredp != NOCRED) 141 *credpp = crdup(savecredp); 142 credp = *credpp; 143 144 if (umap_bug_bypass && credp->cr_uid != 0) 145 printf("umap_bypass: user was %d, group %d\n", 146 credp->cr_uid, credp->cr_gid); 147 148 /* Map all ids in the credential structure. */ 149 150 umap_mapids(vp1->v_mount, credp); 151 152 if (umap_bug_bypass && credp->cr_uid != 0) 153 printf("umap_bypass: user now %d, group %d\n", 154 credp->cr_uid, credp->cr_gid); 155 } 156 157 /* BSD often keeps a credential in the componentname structure 158 * for speed. If there is one, it better get mapped, too. 159 */ 160 161 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) { 162 163 compnamepp = VOPARG_OFFSETTO(struct componentname**, 164 descp->vdesc_componentname_offset, ap); 165 166 savecompcredp = (*compnamepp)->cn_cred; 167 if (savecompcredp != NOCRED) 168 (*compnamepp)->cn_cred = crdup(savecompcredp); 169 compcredp = (*compnamepp)->cn_cred; 170 171 if (umap_bug_bypass && compcredp->cr_uid != 0) 172 printf("umap_bypass: component credit user was %d, group %d\n", 173 compcredp->cr_uid, compcredp->cr_gid); 174 175 /* Map all ids in the credential structure. */ 176 177 umap_mapids(vp1->v_mount, compcredp); 178 179 if (umap_bug_bypass && compcredp->cr_uid != 0) 180 printf("umap_bypass: component credit user now %d, group %d\n", 181 compcredp->cr_uid, compcredp->cr_gid); 182 } 183 184 /* 185 * Call the operation on the lower layer 186 * with the modified argument structure. 187 */ 188 error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap); 189 190 /* 191 * Maintain the illusion of call-by-value 192 * by restoring vnodes in the argument structure 193 * to their original value. 194 */ 195 reles = descp->vdesc_flags; 196 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 197 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 198 break; /* bail out at end of list */ 199 if (old_vps[i]) { 200 *(vps_p[i]) = old_vps[i]; 201 if (reles & 1) 202 vrele(*(vps_p[i])); 203 }; 204 }; 205 206 /* 207 * Map the possible out-going vpp 208 * (Assumes that the lower layer always returns 209 * a VREF'ed vpp unless it gets an error.) 210 */ 211 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && 212 !(descp->vdesc_flags & VDESC_NOMAP_VPP) && 213 !error) { 214 if (descp->vdesc_flags & VDESC_VPP_WILLRELE) 215 goto out; 216 vppp = VOPARG_OFFSETTO(struct vnode***, 217 descp->vdesc_vpp_offset, ap); 218 error = umap_node_create(old_vps[0]->v_mount, **vppp, *vppp); 219 }; 220 221 out: 222 /* 223 * Free duplicate cred structure and restore old one. 224 */ 225 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) { 226 if (umap_bug_bypass && credp && credp->cr_uid != 0) 227 printf("umap_bypass: returning-user was %d\n", 228 credp->cr_uid); 229 230 if (savecredp != NOCRED) { 231 crfree(credp); 232 *credpp = savecredp; 233 if (umap_bug_bypass && credpp && (*credpp)->cr_uid != 0) 234 printf("umap_bypass: returning-user now %d\n\n", 235 savecredp->cr_uid); 236 } 237 } 238 239 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) { 240 if (umap_bug_bypass && compcredp && compcredp->cr_uid != 0) 241 printf("umap_bypass: returning-component-user was %d\n", 242 compcredp->cr_uid); 243 244 if (savecompcredp != NOCRED) { 245 crfree(compcredp); 246 (*compnamepp)->cn_cred = savecompcredp; 247 if (umap_bug_bypass && credpp && (*credpp)->cr_uid != 0) 248 printf("umap_bypass: returning-component-user now %d\n", 249 savecompcredp->cr_uid); 250 } 251 } 252 253 return (error); 254 } 255 256 257 /* 258 * We handle getattr to change the fsid. 259 */ 260 int 261 umap_getattr(ap) 262 struct vop_getattr_args /* { 263 struct vnode *a_vp; 264 struct vattr *a_vap; 265 struct ucred *a_cred; 266 struct proc *a_p; 267 } */ *ap; 268 { 269 uid_t uid; 270 gid_t gid; 271 int error, tmpid, nentries, gnentries; 272 uid_t (*mapdata)[2]; 273 gid_t (*gmapdata)[2]; 274 struct vnode **vp1p; 275 struct vnodeop_desc *descp = ap->a_desc; 276 277 if (error = umap_bypass(ap)) 278 return (error); 279 /* Requires that arguments be restored. */ 280 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 281 282 /* 283 * Umap needs to map the uid and gid returned by a stat 284 * into the proper values for this site. This involves 285 * finding the returned uid in the mapping information, 286 * translating it into the uid on the other end, 287 * and filling in the proper field in the vattr 288 * structure pointed to by ap->a_vap. The group 289 * is easier, since currently all groups will be 290 * translate to the NULLGROUP. 291 */ 292 293 /* Find entry in map */ 294 295 uid = ap->a_vap->va_uid; 296 gid = ap->a_vap->va_gid; 297 if (umap_bug_bypass) 298 printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid, 299 gid); 300 301 vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap); 302 nentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries; 303 mapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata); 304 gnentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries; 305 gmapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata); 306 307 /* Reverse map the uid for the vnode. Since it's a reverse 308 map, we can't use umap_mapids() to do it. */ 309 310 tmpid = umap_reverse_findid(uid, mapdata, nentries); 311 312 if (tmpid != -1) { 313 ap->a_vap->va_uid = (uid_t) tmpid; 314 if (umap_bug_bypass) 315 printf("umap_getattr: original uid = %d\n", uid); 316 } else 317 ap->a_vap->va_uid = (uid_t) NOBODY; 318 319 /* Reverse map the gid for the vnode. */ 320 321 tmpid = umap_reverse_findid(gid, gmapdata, gnentries); 322 323 if (tmpid != -1) { 324 ap->a_vap->va_gid = (gid_t) tmpid; 325 if (umap_bug_bypass) 326 printf("umap_getattr: original gid = %d\n", gid); 327 } else 328 ap->a_vap->va_gid = (gid_t) NULLGROUP; 329 330 return (0); 331 } 332 333 int 334 umap_inactive(ap) 335 struct vop_inactive_args /* { 336 struct vnode *a_vp; 337 } */ *ap; 338 { 339 /* 340 * Do nothing (and _don't_ bypass). 341 * Wait to vrele lowervp until reclaim, 342 * so that until then our umap_node is in the 343 * cache and reusable. 344 * 345 */ 346 return (0); 347 } 348 349 int 350 umap_reclaim(ap) 351 struct vop_reclaim_args /* { 352 struct vnode *a_vp; 353 } */ *ap; 354 { 355 struct vnode *vp = ap->a_vp; 356 struct umap_node *xp = VTOUMAP(vp); 357 struct vnode *lowervp = xp->umap_lowervp; 358 359 /* After this assignment, this node will not be re-used. */ 360 xp->umap_lowervp = NULL; 361 remque(xp); 362 FREE(vp->v_data, M_TEMP); 363 vp->v_data = NULL; 364 vrele(lowervp); 365 return (0); 366 } 367 368 int 369 umap_strategy(ap) 370 struct vop_strategy_args /* { 371 struct buf *a_bp; 372 } */ *ap; 373 { 374 struct buf *bp = ap->a_bp; 375 int error; 376 struct vnode *savedvp; 377 378 savedvp = bp->b_vp; 379 bp->b_vp = UMAPVPTOLOWERVP(bp->b_vp); 380 381 error = VOP_STRATEGY(ap->a_bp); 382 383 bp->b_vp = savedvp; 384 385 return (error); 386 } 387 388 int 389 umap_bwrite(ap) 390 struct vop_bwrite_args /* { 391 struct buf *a_bp; 392 } */ *ap; 393 { 394 struct buf *bp = ap->a_bp; 395 int error; 396 struct vnode *savedvp; 397 398 savedvp = bp->b_vp; 399 bp->b_vp = UMAPVPTOLOWERVP(bp->b_vp); 400 401 error = VOP_BWRITE(ap->a_bp); 402 403 bp->b_vp = savedvp; 404 405 return (error); 406 } 407 408 409 int 410 umap_print(ap) 411 struct vop_print_args /* { 412 struct vnode *a_vp; 413 } */ *ap; 414 { 415 struct vnode *vp = ap->a_vp; 416 printf("\ttag VT_UMAPFS, vp=%x, lowervp=%x\n", vp, UMAPVPTOLOWERVP(vp)); 417 return (0); 418 } 419 420 int 421 umap_rename(ap) 422 struct vop_rename_args /* { 423 struct vnode *a_fdvp; 424 struct vnode *a_fvp; 425 struct componentname *a_fcnp; 426 struct vnode *a_tdvp; 427 struct vnode *a_tvp; 428 struct componentname *a_tcnp; 429 } */ *ap; 430 { 431 int error; 432 struct componentname *compnamep; 433 struct ucred *compcredp, *savecompcredp; 434 struct vnode *vp; 435 436 /* 437 * Rename is irregular, having two componentname structures. 438 * We need to map the cre in the second structure, 439 * and then bypass takes care of the rest. 440 */ 441 442 vp = ap->a_fdvp; 443 compnamep = ap->a_tcnp; 444 compcredp = compnamep->cn_cred; 445 446 savecompcredp = compcredp; 447 compcredp = compnamep->cn_cred = crdup(savecompcredp); 448 449 if (umap_bug_bypass && compcredp->cr_uid != 0) 450 printf("umap_rename: rename component credit user was %d, group %d\n", 451 compcredp->cr_uid, compcredp->cr_gid); 452 453 /* Map all ids in the credential structure. */ 454 455 umap_mapids(vp->v_mount, compcredp); 456 457 if (umap_bug_bypass && compcredp->cr_uid != 0) 458 printf("umap_rename: rename component credit user now %d, group %d\n", 459 compcredp->cr_uid, compcredp->cr_gid); 460 461 error = umap_bypass(ap); 462 463 /* Restore the additional mapped componentname cred structure. */ 464 465 crfree(compcredp); 466 compnamep->cn_cred = savecompcredp; 467 468 return error; 469 } 470 471 /* 472 * Global vfs data structures 473 */ 474 /* 475 * XXX - strategy, bwrite are hand coded currently. They should 476 * go away with a merged buffer/block cache. 477 * 478 */ 479 int (**umap_vnodeop_p)(); 480 struct vnodeopv_entry_desc umap_vnodeop_entries[] = { 481 { &vop_default_desc, umap_bypass }, 482 483 { &vop_getattr_desc, umap_getattr }, 484 { &vop_inactive_desc, umap_inactive }, 485 { &vop_reclaim_desc, umap_reclaim }, 486 { &vop_print_desc, umap_print }, 487 { &vop_rename_desc, umap_rename }, 488 489 { &vop_strategy_desc, umap_strategy }, 490 { &vop_bwrite_desc, umap_bwrite }, 491 492 { (struct vnodeop_desc*) NULL, (int(*)()) NULL } 493 }; 494 struct vnodeopv_desc umap_vnodeop_opv_desc = 495 { &umap_vnodeop_p, umap_vnodeop_entries }; 496