1 /* $NetBSD: umap_vnops.c,v 1.39 2006/05/14 21:32:21 elad 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.39 2006/05/14 21:32:21 elad 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 i, 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) && kauth_cred_geteuid(compcredp) != 0) 382 printf("umap_lookup: component credit user was %d, group %d\n", 383 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 384 385 /* Map all ids in the credential structure. */ 386 umap_mapids(mp, compcredp); 387 } 388 389 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp && kauth_cred_geteuid(compcredp) != 0) 390 printf("umap_lookup: component credit user now %d, group %d\n", 391 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 392 393 ap->a_dvp = ldvp; 394 error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap); 395 vp = *ap->a_vpp; 396 *ap->a_vpp = NULL; 397 398 if (error == EJUSTRETURN && (cnf & ISLASTCN) && 399 (dvp->v_mount->mnt_flag & MNT_RDONLY) && 400 (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) 401 error = EROFS; 402 403 /* Do locking fixup as appropriate. See layer_lookup() for info */ 404 if ((cnp->cn_flags & PDIRUNLOCK)) { 405 LAYERFS_UPPERUNLOCK(dvp, 0, i); 406 } 407 if (ldvp == vp) { 408 *ap->a_vpp = dvp; 409 VREF(dvp); 410 vrele(vp); 411 } else if (vp != NULL) { 412 error = layer_node_create(mp, vp, ap->a_vpp); 413 if (error) { 414 vput(vp); 415 if (cnp->cn_flags & PDIRUNLOCK) { 416 if (vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY) == 0) 417 cnp->cn_flags &= ~PDIRUNLOCK; 418 } 419 } 420 } 421 422 /* 423 * Free duplicate cred structure and restore old one. 424 */ 425 if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp && 426 kauth_cred_geteuid(compcredp) != 0) 427 printf("umap_lookup: returning-component-user was %d\n", 428 kauth_cred_geteuid(compcredp)); 429 430 if (savecompcredp != NOCRED) { 431 if (compcredp) 432 kauth_cred_free(compcredp); 433 cnp->cn_cred = savecompcredp; 434 if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp && 435 kauth_cred_geteuid(savecompcredp) != 0) 436 printf("umap_lookup: returning-component-user now %d\n", 437 kauth_cred_geteuid(savecompcredp)); 438 } 439 440 return (error); 441 } 442 443 /* 444 * We handle getattr to change the fsid. 445 */ 446 int 447 umap_getattr(v) 448 void *v; 449 { 450 struct vop_getattr_args /* { 451 struct vnode *a_vp; 452 struct vattr *a_vap; 453 kauth_cred_t a_cred; 454 struct lwp *a_l; 455 } */ *ap = v; 456 uid_t uid; 457 gid_t gid; 458 int error, tmpid, nentries, gnentries, flags; 459 u_long (*mapdata)[2]; 460 u_long (*gmapdata)[2]; 461 struct vnode **vp1p; 462 const struct vnodeop_desc *descp = ap->a_desc; 463 464 if ((error = umap_bypass(ap)) != 0) 465 return (error); 466 /* Requires that arguments be restored. */ 467 ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0]; 468 469 flags = MOUNTTOUMAPMOUNT(ap->a_vp->v_mount)->umapm_flags; 470 /* 471 * Umap needs to map the uid and gid returned by a stat 472 * into the proper values for this site. This involves 473 * finding the returned uid in the mapping information, 474 * translating it into the uid on the other end, 475 * and filling in the proper field in the vattr 476 * structure pointed to by ap->a_vap. The group 477 * is easier, since currently all groups will be 478 * translate to the NULLGROUP. 479 */ 480 481 /* Find entry in map */ 482 483 uid = ap->a_vap->va_uid; 484 gid = ap->a_vap->va_gid; 485 if ((flags & LAYERFS_MBYPASSDEBUG)) 486 printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid, 487 gid); 488 489 vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap); 490 nentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries; 491 mapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata); 492 gnentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries; 493 gmapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata); 494 495 /* Reverse map the uid for the vnode. Since it's a reverse 496 map, we can't use umap_mapids() to do it. */ 497 498 tmpid = umap_reverse_findid(uid, mapdata, nentries); 499 500 if (tmpid != -1) { 501 ap->a_vap->va_uid = (uid_t) tmpid; 502 if ((flags & LAYERFS_MBYPASSDEBUG)) 503 printf("umap_getattr: original uid = %d\n", uid); 504 } else 505 ap->a_vap->va_uid = (uid_t) NOBODY; 506 507 /* Reverse map the gid for the vnode. */ 508 509 tmpid = umap_reverse_findid(gid, gmapdata, gnentries); 510 511 if (tmpid != -1) { 512 ap->a_vap->va_gid = (gid_t) tmpid; 513 if ((flags & LAYERFS_MBYPASSDEBUG)) 514 printf("umap_getattr: original gid = %d\n", gid); 515 } else 516 ap->a_vap->va_gid = (gid_t) NULLGROUP; 517 518 return (0); 519 } 520 521 int 522 umap_print(v) 523 void *v; 524 { 525 struct vop_print_args /* { 526 struct vnode *a_vp; 527 } */ *ap = v; 528 struct vnode *vp = ap->a_vp; 529 printf("\ttag VT_UMAPFS, vp=%p, lowervp=%p\n", vp, 530 UMAPVPTOLOWERVP(vp)); 531 return (0); 532 } 533 534 int 535 umap_rename(v) 536 void *v; 537 { 538 struct vop_rename_args /* { 539 struct vnode *a_fdvp; 540 struct vnode *a_fvp; 541 struct componentname *a_fcnp; 542 struct vnode *a_tdvp; 543 struct vnode *a_tvp; 544 struct componentname *a_tcnp; 545 } */ *ap = v; 546 int error, flags; 547 struct componentname *compnamep; 548 kauth_cred_t compcredp, savecompcredp; 549 struct vnode *vp; 550 struct vnode *tvp; 551 552 /* 553 * Rename is irregular, having two componentname structures. 554 * We need to map the cre in the second structure, 555 * and then bypass takes care of the rest. 556 */ 557 558 vp = ap->a_fdvp; 559 flags = MOUNTTOUMAPMOUNT(vp->v_mount)->umapm_flags; 560 compnamep = ap->a_tcnp; 561 compcredp = compnamep->cn_cred; 562 563 savecompcredp = compcredp; 564 compcredp = compnamep->cn_cred = kauth_cred_dup(savecompcredp); 565 566 if ((flags & LAYERFS_MBYPASSDEBUG) && 567 kauth_cred_geteuid(compcredp) != 0) 568 printf("umap_rename: rename component credit user was %d, group %d\n", 569 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 570 571 /* Map all ids in the credential structure. */ 572 573 umap_mapids(vp->v_mount, compcredp); 574 575 if ((flags & LAYERFS_MBYPASSDEBUG) && 576 kauth_cred_geteuid(compcredp) != 0) 577 printf("umap_rename: rename component credit user now %d, group %d\n", 578 kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp)); 579 580 tvp = ap->a_tvp; 581 if (tvp) { 582 if (tvp->v_mount != vp->v_mount) 583 tvp = NULL; 584 else 585 vref(tvp); 586 } 587 error = umap_bypass(ap); 588 if (tvp) { 589 if (error == 0) 590 VTOLAYER(tvp)->layer_flags |= LAYERFS_REMOVED; 591 vrele(tvp); 592 } 593 594 /* Restore the additional mapped componentname cred structure. */ 595 596 kauth_cred_free(compcredp); 597 compnamep->cn_cred = savecompcredp; 598 599 return error; 600 } 601