1 /* $NetBSD: umap_vnops.c,v 1.21 2001/12/06 04:29:23 chs 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.6 (Berkeley) 5/22/95 39 */ 40 41 /* 42 * Umap Layer 43 */ 44 45 #include <sys/cdefs.h> 46 __KERNEL_RCSID(0, "$NetBSD: umap_vnops.c,v 1.21 2001/12/06 04:29:23 chs Exp $"); 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/time.h> 51 #include <sys/vnode.h> 52 #include <sys/mount.h> 53 #include <sys/namei.h> 54 #include <sys/malloc.h> 55 #include <sys/buf.h> 56 #include <miscfs/umapfs/umap.h> 57 #include <miscfs/genfs/genfs.h> 58 #include <miscfs/genfs/layer_extern.h> 59 60 int umap_lookup __P((void *)); 61 int umap_getattr __P((void *)); 62 int umap_print __P((void *)); 63 int umap_rename __P((void *)); 64 65 /* 66 * Global vfs data structures 67 */ 68 /* 69 * XXX - strategy, bwrite are hand coded currently. They should 70 * go away with a merged buffer/block cache. 71 * 72 */ 73 int (**umap_vnodeop_p) __P((void *)); 74 const struct vnodeopv_entry_desc umap_vnodeop_entries[] = { 75 { &vop_default_desc, umap_bypass }, 76 77 { &vop_lookup_desc, umap_lookup }, 78 { &vop_getattr_desc, umap_getattr }, 79 { &vop_print_desc, umap_print }, 80 { &vop_rename_desc, umap_rename }, 81 82 { &vop_lock_desc, layer_lock }, 83 { &vop_unlock_desc, layer_unlock }, 84 { &vop_islocked_desc, layer_islocked }, 85 { &vop_fsync_desc, layer_fsync }, 86 { &vop_inactive_desc, layer_inactive }, 87 { &vop_reclaim_desc, layer_reclaim }, 88 { &vop_open_desc, layer_open }, 89 { &vop_setattr_desc, layer_setattr }, 90 { &vop_access_desc, layer_access }, 91 92 { &vop_strategy_desc, layer_strategy }, 93 { &vop_bwrite_desc, layer_bwrite }, 94 { &vop_bmap_desc, layer_bmap }, 95 { &vop_putpages_desc, layer_putpages }, 96 97 { NULL, NULL } 98 }; 99 const struct vnodeopv_desc umapfs_vnodeop_opv_desc = 100 { &umap_vnodeop_p, umap_vnodeop_entries }; 101 102 /* 103 * This is the 08-June-1999 bypass routine. 104 * See layer_vnops.c:layer_bypass for more details. 105 */ 106 int 107 umap_bypass(v) 108 void *v; 109 { 110 struct vop_generic_args /* { 111 struct vnodeop_desc *a_desc; 112 <other random data follows, presumably> 113 } */ *ap = v; 114 struct ucred **credpp = 0, *credp = 0; 115 struct ucred *savecredp = 0, *savecompcredp = 0; 116 struct ucred *compcredp = 0; 117 struct vnode **this_vp_p; 118 int error, error1; 119 int (**our_vnodeop_p) __P((void *)); 120 struct vnode *old_vps[VDESC_MAX_VPS], *vp0; 121 struct vnode **vps_p[VDESC_MAX_VPS]; 122 struct vnode ***vppp; 123 struct vnodeop_desc *descp = ap->a_desc; 124 int reles, i, flags; 125 struct componentname **compnamepp = 0; 126 127 #ifdef SAFETY 128 /* 129 * We require at least one vp. 130 */ 131 if (descp->vdesc_vp_offsets == NULL || 132 descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET) 133 panic ("umap_bypass: no vp's in map.\n"); 134 #endif 135 vps_p[0] = VOPARG_OFFSETTO(struct vnode**,descp->vdesc_vp_offsets[0], 136 ap); 137 vp0 = *vps_p[0]; 138 flags = MOUNTTOUMAPMOUNT(vp0->v_mount)->umapm_flags; 139 our_vnodeop_p = vp0->v_op; 140 141 if (flags & LAYERFS_MBYPASSDEBUG) 142 printf("umap_bypass: %s\n", descp->vdesc_name); 143 144 /* 145 * Map the vnodes going in. 146 * Later, we'll invoke the operation based on 147 * the first mapped vnode's operation vector. 148 */ 149 reles = descp->vdesc_flags; 150 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 151 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 152 break; /* bail out at end of list */ 153 vps_p[i] = this_vp_p = 154 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i], ap); 155 156 /* 157 * We're not guaranteed that any but the first vnode 158 * are of our type. Check for and don't map any 159 * that aren't. (Must map first vp or vclean fails.) 160 */ 161 162 if (i && ((*this_vp_p)==NULL || 163 (*this_vp_p)->v_op != our_vnodeop_p)) { 164 old_vps[i] = NULL; 165 } else { 166 old_vps[i] = *this_vp_p; 167 *(vps_p[i]) = UMAPVPTOLOWERVP(*this_vp_p); 168 if (reles & 1) 169 VREF(*this_vp_p); 170 } 171 172 } 173 174 /* 175 * Fix the credentials. (That's the purpose of this layer.) 176 */ 177 178 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) { 179 180 credpp = VOPARG_OFFSETTO(struct ucred**, 181 descp->vdesc_cred_offset, ap); 182 183 /* Save old values */ 184 185 savecredp = *credpp; 186 if (savecredp != NOCRED) 187 *credpp = crdup(savecredp); 188 credp = *credpp; 189 190 if ((flags & LAYERFS_MBYPASSDEBUG) && credp->cr_uid != 0) 191 printf("umap_bypass: user was %d, group %d\n", 192 credp->cr_uid, credp->cr_gid); 193 194 /* Map all ids in the credential structure. */ 195 196 umap_mapids(vp0->v_mount, credp); 197 198 if ((flags & LAYERFS_MBYPASSDEBUG) && credp->cr_uid != 0) 199 printf("umap_bypass: user now %d, group %d\n", 200 credp->cr_uid, credp->cr_gid); 201 } 202 203 /* BSD often keeps a credential in the componentname structure 204 * for speed. If there is one, it better get mapped, too. 205 */ 206 207 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) { 208 209 compnamepp = VOPARG_OFFSETTO(struct componentname**, 210 descp->vdesc_componentname_offset, ap); 211 212 savecompcredp = (*compnamepp)->cn_cred; 213 if (savecompcredp != NOCRED) 214 (*compnamepp)->cn_cred = crdup(savecompcredp); 215 compcredp = (*compnamepp)->cn_cred; 216 217 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0) 218 printf("umap_bypass: component credit user was %d, group %d\n", 219 compcredp->cr_uid, compcredp->cr_gid); 220 221 /* Map all ids in the credential structure. */ 222 223 umap_mapids(vp0->v_mount, compcredp); 224 225 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0) 226 printf("umap_bypass: component credit user now %d, group %d\n", 227 compcredp->cr_uid, compcredp->cr_gid); 228 } 229 230 /* 231 * Call the operation on the lower layer 232 * with the modified argument structure. 233 */ 234 error = VCALL(*(vps_p[0]), descp->vdesc_offset, ap); 235 236 /* 237 * Maintain the illusion of call-by-value 238 * by restoring vnodes in the argument structure 239 * to their original value. 240 */ 241 reles = descp->vdesc_flags; 242 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 243 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 244 break; /* bail out at end of list */ 245 if (old_vps[i]) { 246 *(vps_p[i]) = old_vps[i]; 247 if (reles & VDESC_VP0_WILLUNLOCK) 248 LAYERFS_UPPERUNLOCK(*(vps_p[i]), 0, error1); 249 if (reles & VDESC_VP0_WILLRELE) 250 vrele(*(vps_p[i])); 251 }; 252 }; 253 254 /* 255 * Map the possible out-going vpp 256 * (Assumes that the lower layer always returns 257 * a VREF'ed vpp unless it gets an error.) 258 */ 259 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && 260 !(descp->vdesc_flags & VDESC_NOMAP_VPP) && 261 !error) { 262 if (descp->vdesc_flags & VDESC_VPP_WILLRELE) 263 goto out; 264 vppp = VOPARG_OFFSETTO(struct vnode***, 265 descp->vdesc_vpp_offset, ap); 266 error = layer_node_create(old_vps[0]->v_mount, **vppp, *vppp); 267 }; 268 269 out: 270 /* 271 * Free duplicate cred structure and restore old one. 272 */ 273 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) { 274 if ((flags & LAYERFS_MBYPASSDEBUG) && credp && 275 credp->cr_uid != 0) 276 printf("umap_bypass: returning-user was %d\n", 277 credp->cr_uid); 278 279 if (savecredp != NOCRED) { 280 crfree(credp); 281 *credpp = savecredp; 282 if ((flags & LAYERFS_MBYPASSDEBUG) && credpp && 283 (*credpp)->cr_uid != 0) 284 printf("umap_bypass: returning-user now %d\n\n", 285 savecredp->cr_uid); 286 } 287 } 288 289 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) { 290 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp && 291 compcredp->cr_uid != 0) 292 printf("umap_bypass: returning-component-user was %d\n", 293 compcredp->cr_uid); 294 295 if (savecompcredp != NOCRED) { 296 crfree(compcredp); 297 (*compnamepp)->cn_cred = savecompcredp; 298 if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp && 299 savecompcredp->cr_uid != 0) 300 printf("umap_bypass: returning-component-user now %d\n", 301 savecompcredp->cr_uid); 302 } 303 } 304 305 return (error); 306 } 307 308 /* 309 * This is based on the 08-June-1999 bypass routine. 310 * See layer_vnops.c:layer_bypass for more details. 311 */ 312 int 313 umap_lookup(v) 314 void *v; 315 { 316 struct vop_lookup_args /* { 317 struct vnodeop_desc *a_desc; 318 struct vnode * a_dvp; 319 struct vnode ** a_vpp; 320 struct componentname * a_cnp; 321 } */ *ap = v; 322 struct componentname *cnp = ap->a_cnp; 323 struct ucred *savecompcredp = NULL; 324 struct ucred *compcredp = NULL; 325 struct vnode *dvp, *vp, *ldvp; 326 struct mount *mp; 327 int error; 328 int i, flags, cnf = cnp->cn_flags; 329 330 dvp = ap->a_dvp; 331 mp = dvp->v_mount; 332 333 if ((cnf & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 334 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 335 return (EROFS); 336 337 flags = MOUNTTOUMAPMOUNT(mp)->umapm_flags; 338 ldvp = UMAPVPTOLOWERVP(dvp); 339 340 if (flags & LAYERFS_MBYPASSDEBUG) 341 printf("umap_lookup\n"); 342 343 /* 344 * Fix the credentials. (That's the purpose of this layer.) 345 * 346 * BSD often keeps a credential in the componentname structure 347 * for speed. If there is one, it better get mapped, too. 348 */ 349 350 if ((savecompcredp = cnp->cn_cred)) { 351 compcredp = crdup(savecompcredp); 352 cnp->cn_cred = compcredp; 353 354 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0) 355 printf("umap_lookup: component credit user was %d, group %d\n", 356 compcredp->cr_uid, compcredp->cr_gid); 357 358 /* Map all ids in the credential structure. */ 359 umap_mapids(mp, compcredp); 360 } 361 362 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0) 363 printf("umap_lookup: component credit user now %d, group %d\n", 364 compcredp->cr_uid, compcredp->cr_gid); 365 366 ap->a_dvp = ldvp; 367 error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap); 368 vp = *ap->a_vpp; 369 370 if (error == EJUSTRETURN && (cnf & ISLASTCN) && 371 (dvp->v_mount->mnt_flag & MNT_RDONLY) && 372 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) 373 error = EROFS; 374 375 /* Do locking fixup as appropriate. See layer_lookup() for info */ 376 if ((cnp->cn_flags & PDIRUNLOCK)) { 377 LAYERFS_UPPERUNLOCK(dvp, 0, i); 378 } 379 if (ldvp == vp) { 380 *ap->a_vpp = dvp; 381 VREF(dvp); 382 vrele(vp); 383 } else if (vp != NULL) { 384 error = layer_node_create(mp, vp, ap->a_vpp); 385 } 386 387 /* 388 * Free duplicate cred structure and restore old one. 389 */ 390 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp && 391 compcredp->cr_uid != 0) 392 printf("umap_lookup: returning-component-user was %d\n", 393 compcredp->cr_uid); 394 395 if (savecompcredp != NOCRED) { 396 crfree(compcredp); 397 cnp->cn_cred = savecompcredp; 398 if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp && 399 savecompcredp->cr_uid != 0) 400 printf("umap_lookup: returning-component-user now %d\n", 401 savecompcredp->cr_uid); 402 } 403 404 return (error); 405 } 406 407 /* 408 * We handle getattr to change the fsid. 409 */ 410 int 411 umap_getattr(v) 412 void *v; 413 { 414 struct vop_getattr_args /* { 415 struct vnode *a_vp; 416 struct vattr *a_vap; 417 struct ucred *a_cred; 418 struct proc *a_p; 419 } */ *ap = v; 420 uid_t uid; 421 gid_t gid; 422 int error, tmpid, nentries, gnentries, flags; 423 u_long (*mapdata)[2]; 424 u_long (*gmapdata)[2]; 425 struct vnode **vp1p; 426 const struct vnodeop_desc *descp = ap->a_desc; 427 428 if ((error = umap_bypass(ap)) != 0) 429 return (error); 430 /* Requires that arguments be restored. */ 431 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0]; 432 433 flags = MOUNTTOUMAPMOUNT(ap->a_vp->v_mount)->umapm_flags; 434 /* 435 * Umap needs to map the uid and gid returned by a stat 436 * into the proper values for this site. This involves 437 * finding the returned uid in the mapping information, 438 * translating it into the uid on the other end, 439 * and filling in the proper field in the vattr 440 * structure pointed to by ap->a_vap. The group 441 * is easier, since currently all groups will be 442 * translate to the NULLGROUP. 443 */ 444 445 /* Find entry in map */ 446 447 uid = ap->a_vap->va_uid; 448 gid = ap->a_vap->va_gid; 449 if ((flags & LAYERFS_MBYPASSDEBUG)) 450 printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid, 451 gid); 452 453 vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap); 454 nentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries; 455 mapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata); 456 gnentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries; 457 gmapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata); 458 459 /* Reverse map the uid for the vnode. Since it's a reverse 460 map, we can't use umap_mapids() to do it. */ 461 462 tmpid = umap_reverse_findid(uid, mapdata, nentries); 463 464 if (tmpid != -1) { 465 ap->a_vap->va_uid = (uid_t) tmpid; 466 if ((flags & LAYERFS_MBYPASSDEBUG)) 467 printf("umap_getattr: original uid = %d\n", uid); 468 } else 469 ap->a_vap->va_uid = (uid_t) NOBODY; 470 471 /* Reverse map the gid for the vnode. */ 472 473 tmpid = umap_reverse_findid(gid, gmapdata, gnentries); 474 475 if (tmpid != -1) { 476 ap->a_vap->va_gid = (gid_t) tmpid; 477 if ((flags & LAYERFS_MBYPASSDEBUG)) 478 printf("umap_getattr: original gid = %d\n", gid); 479 } else 480 ap->a_vap->va_gid = (gid_t) NULLGROUP; 481 482 return (0); 483 } 484 485 int 486 umap_print(v) 487 void *v; 488 { 489 struct vop_print_args /* { 490 struct vnode *a_vp; 491 } */ *ap = v; 492 struct vnode *vp = ap->a_vp; 493 printf("\ttag VT_UMAPFS, vp=%p, lowervp=%p\n", vp, 494 UMAPVPTOLOWERVP(vp)); 495 return (0); 496 } 497 498 int 499 umap_rename(v) 500 void *v; 501 { 502 struct vop_rename_args /* { 503 struct vnode *a_fdvp; 504 struct vnode *a_fvp; 505 struct componentname *a_fcnp; 506 struct vnode *a_tdvp; 507 struct vnode *a_tvp; 508 struct componentname *a_tcnp; 509 } */ *ap = v; 510 int error, flags; 511 struct componentname *compnamep; 512 struct ucred *compcredp, *savecompcredp; 513 struct vnode *vp; 514 515 /* 516 * Rename is irregular, having two componentname structures. 517 * We need to map the cre in the second structure, 518 * and then bypass takes care of the rest. 519 */ 520 521 vp = ap->a_fdvp; 522 flags = MOUNTTOUMAPMOUNT(vp->v_mount)->umapm_flags; 523 compnamep = ap->a_tcnp; 524 compcredp = compnamep->cn_cred; 525 526 savecompcredp = compcredp; 527 compcredp = compnamep->cn_cred = crdup(savecompcredp); 528 529 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0) 530 printf("umap_rename: rename component credit user was %d, group %d\n", 531 compcredp->cr_uid, compcredp->cr_gid); 532 533 /* Map all ids in the credential structure. */ 534 535 umap_mapids(vp->v_mount, compcredp); 536 537 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp->cr_uid != 0) 538 printf("umap_rename: rename component credit user now %d, group %d\n", 539 compcredp->cr_uid, compcredp->cr_gid); 540 541 error = umap_bypass(ap); 542 543 /* Restore the additional mapped componentname cred structure. */ 544 545 crfree(compcredp); 546 compnamep->cn_cred = savecompcredp; 547 548 return error; 549 } 550