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