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