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