1 /* $NetBSD: umap_vnops.c,v 1.43 2006/12/09 16:11:52 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. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)umap_vnops.c 8.6 (Berkeley) 5/22/95 35 */ 36 37 /* 38 * Umap Layer 39 */ 40 41 #include <sys/cdefs.h> 42 __KERNEL_RCSID(0, "$NetBSD: umap_vnops.c,v 1.43 2006/12/09 16:11:52 chs Exp $"); 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/time.h> 47 #include <sys/vnode.h> 48 #include <sys/mount.h> 49 #include <sys/namei.h> 50 #include <sys/malloc.h> 51 #include <sys/buf.h> 52 #include <sys/kauth.h> 53 54 #include <miscfs/umapfs/umap.h> 55 #include <miscfs/genfs/genfs.h> 56 #include <miscfs/genfs/layer_extern.h> 57 58 int umap_lookup(void *); 59 int umap_getattr(void *); 60 int umap_print(void *); 61 int umap_rename(void *); 62 63 /* 64 * Global vfs data structures 65 */ 66 /* 67 * XXX - strategy, bwrite are hand coded currently. They should 68 * go away with a merged buffer/block cache. 69 * 70 */ 71 int (**umap_vnodeop_p)(void *); 72 const struct vnodeopv_entry_desc umap_vnodeop_entries[] = { 73 { &vop_default_desc, umap_bypass }, 74 75 { &vop_lookup_desc, umap_lookup }, 76 { &vop_getattr_desc, umap_getattr }, 77 { &vop_print_desc, umap_print }, 78 { &vop_rename_desc, umap_rename }, 79 80 { &vop_lock_desc, layer_lock }, 81 { &vop_unlock_desc, layer_unlock }, 82 { &vop_islocked_desc, layer_islocked }, 83 { &vop_fsync_desc, layer_fsync }, 84 { &vop_inactive_desc, layer_inactive }, 85 { &vop_reclaim_desc, layer_reclaim }, 86 { &vop_open_desc, layer_open }, 87 { &vop_setattr_desc, layer_setattr }, 88 { &vop_access_desc, layer_access }, 89 { &vop_remove_desc, layer_remove }, 90 { &vop_rmdir_desc, layer_rmdir }, 91 92 { &vop_bwrite_desc, layer_bwrite }, 93 { &vop_bmap_desc, layer_bmap }, 94 { &vop_getpages_desc, layer_getpages }, 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 int (**our_vnodeop_p)(void *); 115 kauth_cred_t *credpp = NULL, credp = 0; 116 kauth_cred_t savecredp = 0, savecompcredp = 0; 117 kauth_cred_t compcredp = 0; 118 struct vnode **this_vp_p; 119 int error, error1; 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("%s: no vp's in map.\n", __func__); 134 #endif 135 136 vps_p[0] = 137 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap); 138 vp0 = *vps_p[0]; 139 flags = MOUNTTOUMAPMOUNT(vp0->v_mount)->umapm_flags; 140 our_vnodeop_p = vp0->v_op; 141 142 if (flags & LAYERFS_MBYPASSDEBUG) 143 printf("%s: %s\n", __func__, descp->vdesc_name); 144 145 /* 146 * Map the vnodes going in. 147 * Later, we'll invoke the operation based on 148 * the first mapped vnode's operation vector. 149 */ 150 reles = descp->vdesc_flags; 151 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 152 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 153 break; /* bail out at end of list */ 154 vps_p[i] = this_vp_p = 155 VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i], 156 ap); 157 /* 158 * We're not guaranteed that any but the first vnode 159 * are of our type. Check for and don't map any 160 * that aren't. (We must always map first vp or vclean fails.) 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 /* 169 * XXX - Several operations have the side effect 170 * of vrele'ing their vp's. We must account for 171 * that. (This should go away in the future.) 172 */ 173 if (reles & VDESC_VP0_WILLRELE) 174 VREF(*this_vp_p); 175 } 176 177 } 178 179 /* 180 * Fix the credentials. (That's the purpose of this layer.) 181 */ 182 183 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) { 184 185 credpp = VOPARG_OFFSETTO(kauth_cred_t*, 186 descp->vdesc_cred_offset, ap); 187 188 /* Save old values */ 189 190 savecredp = *credpp; 191 if (savecredp != NOCRED) 192 *credpp = kauth_cred_dup(savecredp); 193 credp = *credpp; 194 195 if ((flags & LAYERFS_MBYPASSDEBUG) && 196 kauth_cred_geteuid(credp) != 0) 197 printf("umap_bypass: user was %d, group %d\n", 198 kauth_cred_geteuid(credp), kauth_cred_getegid(credp)); 199 200 /* Map all ids in the credential structure. */ 201 202 umap_mapids(vp0->v_mount, credp); 203 204 if ((flags & LAYERFS_MBYPASSDEBUG) && 205 kauth_cred_geteuid(credp) != 0) 206 printf("umap_bypass: user now %d, group %d\n", 207 kauth_cred_geteuid(credp), kauth_cred_getegid(credp)); 208 } 209 210 /* BSD often keeps a credential in the componentname structure 211 * for speed. If there is one, it better get mapped, too. 212 */ 213 214 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) { 215 216 compnamepp = VOPARG_OFFSETTO(struct componentname**, 217 descp->vdesc_componentname_offset, ap); 218 219 savecompcredp = (*compnamepp)->cn_cred; 220 if (savecompcredp != NOCRED) 221 (*compnamepp)->cn_cred = kauth_cred_dup(savecompcredp); 222 compcredp = (*compnamepp)->cn_cred; 223 224 if ((flags & LAYERFS_MBYPASSDEBUG) && 225 kauth_cred_geteuid(compcredp) != 0) 226 printf("umap_bypass: component credit user was %d, group %d\n", 227 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 228 229 /* Map all ids in the credential structure. */ 230 231 umap_mapids(vp0->v_mount, compcredp); 232 233 if ((flags & LAYERFS_MBYPASSDEBUG) && 234 kauth_cred_geteuid(compcredp) != 0) 235 printf("umap_bypass: component credit user now %d, group %d\n", 236 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 237 } 238 239 /* 240 * Call the operation on the lower layer 241 * with the modified argument structure. 242 */ 243 error = VCALL(*vps_p[0], descp->vdesc_offset, ap); 244 245 /* 246 * Maintain the illusion of call-by-value 247 * by restoring vnodes in the argument structure 248 * to their original value. 249 */ 250 reles = descp->vdesc_flags; 251 for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) { 252 if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET) 253 break; /* bail out at end of list */ 254 if (old_vps[i]) { 255 *(vps_p[i]) = old_vps[i]; 256 if (reles & VDESC_VP0_WILLUNLOCK) 257 LAYERFS_UPPERUNLOCK(*(vps_p[i]), 0, error1); 258 if (reles & VDESC_VP0_WILLRELE) 259 vrele(*(vps_p[i])); 260 } 261 } 262 263 /* 264 * Map the possible out-going vpp 265 * (Assumes that the lower layer always returns 266 * a VREF'ed vpp unless it gets an error.) 267 */ 268 if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && 269 !(descp->vdesc_flags & VDESC_NOMAP_VPP) && 270 !error) { 271 /* 272 * XXX - even though some ops have vpp returned vp's, 273 * several ops actually vrele this before returning. 274 * We must avoid these ops. 275 * (This should go away when these ops are regularized.) 276 */ 277 if (descp->vdesc_flags & VDESC_VPP_WILLRELE) 278 goto out; 279 vppp = VOPARG_OFFSETTO(struct vnode***, 280 descp->vdesc_vpp_offset, ap); 281 /* 282 * Only vop_lookup, vop_create, vop_makedir, vop_bmap, 283 * vop_mknod, and vop_symlink return vpp's. vop_bmap 284 * doesn't call bypass as the lower vpp is fine (we're just 285 * going to do i/o on it). vop_lookup doesn't call bypass 286 * as a lookup on "." would generate a locking error. 287 * So all the calls which get us here have a locked vpp. :-) 288 */ 289 error = layer_node_create(old_vps[0]->v_mount, **vppp, *vppp); 290 if (error) { 291 vput(**vppp); 292 **vppp = NULL; 293 } 294 } 295 296 out: 297 /* 298 * Free duplicate cred structure and restore old one. 299 */ 300 if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) { 301 if ((flags & LAYERFS_MBYPASSDEBUG) && credp && 302 kauth_cred_geteuid(credp) != 0) 303 printf("umap_bypass: returning-user was %d\n", 304 kauth_cred_geteuid(credp)); 305 306 if (savecredp != NOCRED && credpp) { 307 kauth_cred_free(credp); 308 *credpp = savecredp; 309 if ((flags & LAYERFS_MBYPASSDEBUG) && credpp && 310 kauth_cred_geteuid(*credpp) != 0) 311 printf("umap_bypass: returning-user now %d\n\n", 312 kauth_cred_geteuid(savecredp)); 313 } 314 } 315 316 if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) { 317 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp && 318 kauth_cred_geteuid(compcredp) != 0) 319 printf("umap_bypass: returning-component-user was %d\n", 320 kauth_cred_geteuid(compcredp)); 321 322 if (savecompcredp != NOCRED) { 323 kauth_cred_free(compcredp); 324 (*compnamepp)->cn_cred = savecompcredp; 325 if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp && 326 kauth_cred_geteuid(savecompcredp) != 0) 327 printf("umap_bypass: returning-component-user now %d\n", 328 kauth_cred_geteuid(savecompcredp)); 329 } 330 } 331 332 return (error); 333 } 334 335 /* 336 * This is based on the 08-June-1999 bypass routine. 337 * See layer_vnops.c:layer_bypass for more details. 338 */ 339 int 340 umap_lookup(v) 341 void *v; 342 { 343 struct vop_lookup_args /* { 344 struct vnodeop_desc *a_desc; 345 struct vnode * a_dvp; 346 struct vnode ** a_vpp; 347 struct componentname * a_cnp; 348 } */ *ap = v; 349 struct componentname *cnp = ap->a_cnp; 350 kauth_cred_t savecompcredp = NULL; 351 kauth_cred_t compcredp = NULL; 352 struct vnode *dvp, *vp, *ldvp; 353 struct mount *mp; 354 int error; 355 int flags, cnf = cnp->cn_flags; 356 357 dvp = ap->a_dvp; 358 mp = dvp->v_mount; 359 360 if ((cnf & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) && 361 (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) 362 return (EROFS); 363 364 flags = MOUNTTOUMAPMOUNT(mp)->umapm_flags; 365 ldvp = UMAPVPTOLOWERVP(dvp); 366 367 if (flags & LAYERFS_MBYPASSDEBUG) 368 printf("umap_lookup\n"); 369 370 /* 371 * Fix the credentials. (That's the purpose of this layer.) 372 * 373 * BSD often keeps a credential in the componentname structure 374 * for speed. If there is one, it better get mapped, too. 375 */ 376 377 if ((savecompcredp = cnp->cn_cred)) { 378 compcredp = kauth_cred_dup(savecompcredp); 379 cnp->cn_cred = compcredp; 380 381 if ((flags & LAYERFS_MBYPASSDEBUG) && 382 kauth_cred_geteuid(compcredp) != 0) 383 printf("umap_lookup: component credit user was %d, group %d\n", 384 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 385 386 /* Map all ids in the credential structure. */ 387 umap_mapids(mp, compcredp); 388 } 389 390 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp && 391 kauth_cred_geteuid(compcredp) != 0) 392 printf("umap_lookup: component credit user now %d, group %d\n", 393 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 394 395 ap->a_dvp = ldvp; 396 error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap); 397 vp = *ap->a_vpp; 398 *ap->a_vpp = NULL; 399 400 if (error == EJUSTRETURN && (cnf & ISLASTCN) && 401 (dvp->v_mount->mnt_flag & MNT_RDONLY) && 402 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) 403 error = EROFS; 404 405 /* Do locking fixup as appropriate. See layer_lookup() for info */ 406 if (ldvp == vp) { 407 *ap->a_vpp = dvp; 408 VREF(dvp); 409 vrele(vp); 410 } else if (vp != NULL) { 411 error = layer_node_create(mp, vp, ap->a_vpp); 412 if (error) { 413 vput(vp); 414 } 415 } 416 417 /* 418 * Free duplicate cred structure and restore old one. 419 */ 420 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp && 421 kauth_cred_geteuid(compcredp) != 0) 422 printf("umap_lookup: returning-component-user was %d\n", 423 kauth_cred_geteuid(compcredp)); 424 425 if (savecompcredp != NOCRED) { 426 if (compcredp) 427 kauth_cred_free(compcredp); 428 cnp->cn_cred = savecompcredp; 429 if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp && 430 kauth_cred_geteuid(savecompcredp) != 0) 431 printf("umap_lookup: returning-component-user now %d\n", 432 kauth_cred_geteuid(savecompcredp)); 433 } 434 435 return (error); 436 } 437 438 /* 439 * We handle getattr to change the fsid. 440 */ 441 int 442 umap_getattr(v) 443 void *v; 444 { 445 struct vop_getattr_args /* { 446 struct vnode *a_vp; 447 struct vattr *a_vap; 448 kauth_cred_t a_cred; 449 struct lwp *a_l; 450 } */ *ap = v; 451 uid_t uid; 452 gid_t gid; 453 int error, tmpid, nentries, gnentries, flags; 454 u_long (*mapdata)[2]; 455 u_long (*gmapdata)[2]; 456 struct vnode **vp1p; 457 const struct vnodeop_desc *descp = ap->a_desc; 458 459 if ((error = umap_bypass(ap)) != 0) 460 return (error); 461 /* Requires that arguments be restored. */ 462 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 463 464 flags = MOUNTTOUMAPMOUNT(ap->a_vp->v_mount)->umapm_flags; 465 /* 466 * Umap needs to map the uid and gid returned by a stat 467 * into the proper values for this site. This involves 468 * finding the returned uid in the mapping information, 469 * translating it into the uid on the other end, 470 * and filling in the proper field in the vattr 471 * structure pointed to by ap->a_vap. The group 472 * is easier, since currently all groups will be 473 * translate to the NULLGROUP. 474 */ 475 476 /* Find entry in map */ 477 478 uid = ap->a_vap->va_uid; 479 gid = ap->a_vap->va_gid; 480 if ((flags & LAYERFS_MBYPASSDEBUG)) 481 printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid, 482 gid); 483 484 vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap); 485 nentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries; 486 mapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata); 487 gnentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries; 488 gmapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata); 489 490 /* Reverse map the uid for the vnode. Since it's a reverse 491 map, we can't use umap_mapids() to do it. */ 492 493 tmpid = umap_reverse_findid(uid, mapdata, nentries); 494 495 if (tmpid != -1) { 496 ap->a_vap->va_uid = (uid_t) tmpid; 497 if ((flags & LAYERFS_MBYPASSDEBUG)) 498 printf("umap_getattr: original uid = %d\n", uid); 499 } else 500 ap->a_vap->va_uid = (uid_t) NOBODY; 501 502 /* Reverse map the gid for the vnode. */ 503 504 tmpid = umap_reverse_findid(gid, gmapdata, gnentries); 505 506 if (tmpid != -1) { 507 ap->a_vap->va_gid = (gid_t) tmpid; 508 if ((flags & LAYERFS_MBYPASSDEBUG)) 509 printf("umap_getattr: original gid = %d\n", gid); 510 } else 511 ap->a_vap->va_gid = (gid_t) NULLGROUP; 512 513 return (0); 514 } 515 516 int 517 umap_print(v) 518 void *v; 519 { 520 struct vop_print_args /* { 521 struct vnode *a_vp; 522 } */ *ap = v; 523 struct vnode *vp = ap->a_vp; 524 printf("\ttag VT_UMAPFS, vp=%p, lowervp=%p\n", vp, 525 UMAPVPTOLOWERVP(vp)); 526 return (0); 527 } 528 529 int 530 umap_rename(v) 531 void *v; 532 { 533 struct vop_rename_args /* { 534 struct vnode *a_fdvp; 535 struct vnode *a_fvp; 536 struct componentname *a_fcnp; 537 struct vnode *a_tdvp; 538 struct vnode *a_tvp; 539 struct componentname *a_tcnp; 540 } */ *ap = v; 541 int error, flags; 542 struct componentname *compnamep; 543 kauth_cred_t compcredp, savecompcredp; 544 struct vnode *vp; 545 struct vnode *tvp; 546 547 /* 548 * Rename is irregular, having two componentname structures. 549 * We need to map the cre in the second structure, 550 * and then bypass takes care of the rest. 551 */ 552 553 vp = ap->a_fdvp; 554 flags = MOUNTTOUMAPMOUNT(vp->v_mount)->umapm_flags; 555 compnamep = ap->a_tcnp; 556 compcredp = compnamep->cn_cred; 557 558 savecompcredp = compcredp; 559 compcredp = compnamep->cn_cred = kauth_cred_dup(savecompcredp); 560 561 if ((flags & LAYERFS_MBYPASSDEBUG) && 562 kauth_cred_geteuid(compcredp) != 0) 563 printf("umap_rename: rename component credit user was %d, group %d\n", 564 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 565 566 /* Map all ids in the credential structure. */ 567 568 umap_mapids(vp->v_mount, compcredp); 569 570 if ((flags & LAYERFS_MBYPASSDEBUG) && 571 kauth_cred_geteuid(compcredp) != 0) 572 printf("umap_rename: rename component credit user now %d, group %d\n", 573 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 574 575 tvp = ap->a_tvp; 576 if (tvp) { 577 if (tvp->v_mount != vp->v_mount) 578 tvp = NULL; 579 else 580 vref(tvp); 581 } 582 error = umap_bypass(ap); 583 if (tvp) { 584 if (error == 0) 585 VTOLAYER(tvp)->layer_flags |= LAYERFS_REMOVED; 586 vrele(tvp); 587 } 588 589 /* Restore the additional mapped componentname cred structure. */ 590 591 kauth_cred_free(compcredp); 592 compnamep->cn_cred = savecompcredp; 593 594 return error; 595 } 596