1*982ae832Sthorpej /* $NetBSD: umap_vnops.c,v 1.62 2021/10/20 03:08:18 thorpej Exp $ */
2cf92afd6Scgd
3cde1d475Smycroft /*
4cde1d475Smycroft * Copyright (c) 1992, 1993
5cde1d475Smycroft * The Regents of the University of California. All rights reserved.
6cde1d475Smycroft *
7cde1d475Smycroft * This code is derived from software donated to Berkeley by
8cde1d475Smycroft * the UCLA Ficus project.
9cde1d475Smycroft *
10cde1d475Smycroft * Redistribution and use in source and binary forms, with or without
11cde1d475Smycroft * modification, are permitted provided that the following conditions
12cde1d475Smycroft * are met:
13cde1d475Smycroft * 1. Redistributions of source code must retain the above copyright
14cde1d475Smycroft * notice, this list of conditions and the following disclaimer.
15cde1d475Smycroft * 2. Redistributions in binary form must reproduce the above copyright
16cde1d475Smycroft * notice, this list of conditions and the following disclaimer in the
17cde1d475Smycroft * documentation and/or other materials provided with the distribution.
18aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
19cde1d475Smycroft * may be used to endorse or promote products derived from this software
20cde1d475Smycroft * without specific prior written permission.
21cde1d475Smycroft *
22cde1d475Smycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23cde1d475Smycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24cde1d475Smycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25cde1d475Smycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26cde1d475Smycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27cde1d475Smycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28cde1d475Smycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29cde1d475Smycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30cde1d475Smycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31cde1d475Smycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32cde1d475Smycroft * SUCH DAMAGE.
33cde1d475Smycroft *
34e5bc90f4Sfvdl * @(#)umap_vnops.c 8.6 (Berkeley) 5/22/95
35cde1d475Smycroft */
36cde1d475Smycroft
37cde1d475Smycroft /*
38cde1d475Smycroft * Umap Layer
39cde1d475Smycroft */
40cde1d475Smycroft
41e4b00f43Slukem #include <sys/cdefs.h>
42*982ae832Sthorpej __KERNEL_RCSID(0, "$NetBSD: umap_vnops.c,v 1.62 2021/10/20 03:08:18 thorpej Exp $");
43e4b00f43Slukem
44cde1d475Smycroft #include <sys/param.h>
45cde1d475Smycroft #include <sys/systm.h>
46cde1d475Smycroft #include <sys/time.h>
47cde1d475Smycroft #include <sys/vnode.h>
48cde1d475Smycroft #include <sys/mount.h>
49cde1d475Smycroft #include <sys/namei.h>
50cde1d475Smycroft #include <sys/buf.h>
51fc9422c9Selad #include <sys/kauth.h>
52fc9422c9Selad
53cde1d475Smycroft #include <miscfs/umapfs/umap.h>
54e5bc90f4Sfvdl #include <miscfs/genfs/genfs.h>
559866514dSwrstuden #include <miscfs/genfs/layer_extern.h>
56cde1d475Smycroft
57767dc27aSplunky /*
58767dc27aSplunky * Note: If the LAYERFS_MBYPASSDEBUG flag is set, it is possible
59767dc27aSplunky * that the debug printing will bomb out, because kauth routines
60767dc27aSplunky * do not handle NOCRED or FSCRED like other credentials and end
61767dc27aSplunky * up dereferencing an inappropriate pointer.
62767dc27aSplunky *
63767dc27aSplunky * That should be fixed in kauth rather than here.
64767dc27aSplunky */
65767dc27aSplunky
66af97f2e8Sxtraeme int umap_lookup(void *);
67af97f2e8Sxtraeme int umap_getattr(void *);
68af97f2e8Sxtraeme int umap_print(void *);
69af97f2e8Sxtraeme int umap_rename(void *);
70e5bc90f4Sfvdl
71631ccba6Schristos /*
72631ccba6Schristos * Global vfs data structures
73631ccba6Schristos */
74631ccba6Schristos /*
75631ccba6Schristos * XXX - strategy, bwrite are hand coded currently. They should
76631ccba6Schristos * go away with a merged buffer/block cache.
77631ccba6Schristos *
78631ccba6Schristos */
79af97f2e8Sxtraeme int (**umap_vnodeop_p)(void *);
80d9466585Sjdolecek const struct vnodeopv_entry_desc umap_vnodeop_entries[] = {
81631ccba6Schristos { &vop_default_desc, umap_bypass },
82631ccba6Schristos
839866514dSwrstuden { &vop_lookup_desc, umap_lookup },
84631ccba6Schristos { &vop_getattr_desc, umap_getattr },
85631ccba6Schristos { &vop_print_desc, umap_print },
86631ccba6Schristos { &vop_rename_desc, umap_rename },
87631ccba6Schristos
889866514dSwrstuden { &vop_fsync_desc, layer_fsync },
899866514dSwrstuden { &vop_inactive_desc, layer_inactive },
909866514dSwrstuden { &vop_reclaim_desc, layer_reclaim },
919866514dSwrstuden { &vop_open_desc, layer_open },
926412ef2bShannken { &vop_close_desc, layer_close },
939866514dSwrstuden { &vop_setattr_desc, layer_setattr },
949866514dSwrstuden { &vop_access_desc, layer_access },
959aa2a9c3Schristos { &vop_accessx_desc, genfs_accessx },
966753c745Swrstuden { &vop_remove_desc, layer_remove },
97b89d0815Shannken { &vop_revoke_desc, layer_revoke },
987ca8e916Shannken { &vop_rmdir_desc, layer_rmdir },
999866514dSwrstuden
1009866514dSwrstuden { &vop_bmap_desc, layer_bmap },
1012776bd06Schs { &vop_getpages_desc, layer_getpages },
1024d146714Schs { &vop_putpages_desc, layer_putpages },
103631ccba6Schristos
1044d146714Schs { NULL, NULL }
105631ccba6Schristos };
106d9466585Sjdolecek const struct vnodeopv_desc umapfs_vnodeop_opv_desc =
107631ccba6Schristos { &umap_vnodeop_p, umap_vnodeop_entries };
108631ccba6Schristos
109cde1d475Smycroft /*
1109866514dSwrstuden * This is the 08-June-1999 bypass routine.
1119866514dSwrstuden * See layer_vnops.c:layer_bypass for more details.
112cde1d475Smycroft */
113cde1d475Smycroft int
umap_bypass(void * v)11482357f6dSdsl umap_bypass(void *v)
115631ccba6Schristos {
116cde1d475Smycroft struct vop_generic_args /* {
117cde1d475Smycroft struct vnodeop_desc *a_desc;
118cde1d475Smycroft <other random data follows, presumably>
119631ccba6Schristos } */ *ap = v;
120af97f2e8Sxtraeme int (**our_vnodeop_p)(void *);
121fc9422c9Selad kauth_cred_t *credpp = NULL, credp = 0;
122fc9422c9Selad kauth_cred_t savecredp = 0, savecompcredp = 0;
123fc9422c9Selad kauth_cred_t compcredp = 0;
124cde1d475Smycroft struct vnode **this_vp_p;
12562bfdd2bShannken int error;
1269866514dSwrstuden struct vnode *old_vps[VDESC_MAX_VPS], *vp0;
127cde1d475Smycroft struct vnode **vps_p[VDESC_MAX_VPS];
128cde1d475Smycroft struct vnode ***vppp;
129cde1d475Smycroft struct vnodeop_desc *descp = ap->a_desc;
1309866514dSwrstuden int reles, i, flags;
131cde1d475Smycroft struct componentname **compnamepp = 0;
132cde1d475Smycroft
133cea3e862Splunky #ifdef DIAGNOSTIC
134cde1d475Smycroft /*
135cde1d475Smycroft * We require at least one vp.
136cde1d475Smycroft */
137cde1d475Smycroft if (descp->vdesc_vp_offsets == NULL ||
138cde1d475Smycroft descp->vdesc_vp_offsets[0] == VDESC_NO_OFFSET)
13900611676Syamt panic("%s: no vp's in map.\n", __func__);
140cde1d475Smycroft #endif
14100611676Syamt
14200611676Syamt vps_p[0] =
14300611676Syamt VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
1449866514dSwrstuden vp0 = *vps_p[0];
1459866514dSwrstuden flags = MOUNTTOUMAPMOUNT(vp0->v_mount)->umapm_flags;
1469866514dSwrstuden our_vnodeop_p = vp0->v_op;
1479866514dSwrstuden
1489866514dSwrstuden if (flags & LAYERFS_MBYPASSDEBUG)
14900611676Syamt printf("%s: %s\n", __func__, descp->vdesc_name);
150cde1d475Smycroft
151cde1d475Smycroft /*
152cde1d475Smycroft * Map the vnodes going in.
153cde1d475Smycroft * Later, we'll invoke the operation based on
154cde1d475Smycroft * the first mapped vnode's operation vector.
155cde1d475Smycroft */
156cde1d475Smycroft reles = descp->vdesc_flags;
157cde1d475Smycroft for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
158cde1d475Smycroft if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
159cde1d475Smycroft break; /* bail out at end of list */
160cde1d475Smycroft vps_p[i] = this_vp_p =
16100611676Syamt VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[i],
16200611676Syamt ap);
163cde1d475Smycroft /*
164cde1d475Smycroft * We're not guaranteed that any but the first vnode
165cde1d475Smycroft * are of our type. Check for and don't map any
16600611676Syamt * that aren't. (We must always map first vp or vclean fails.)
167cde1d475Smycroft */
16800611676Syamt if (i && (*this_vp_p == NULL ||
1699866514dSwrstuden (*this_vp_p)->v_op != our_vnodeop_p)) {
170cde1d475Smycroft old_vps[i] = NULL;
171cde1d475Smycroft } else {
172cde1d475Smycroft old_vps[i] = *this_vp_p;
173cde1d475Smycroft *(vps_p[i]) = UMAPVPTOLOWERVP(*this_vp_p);
17400611676Syamt /*
17500611676Syamt * XXX - Several operations have the side effect
17600611676Syamt * of vrele'ing their vp's. We must account for
17700611676Syamt * that. (This should go away in the future.)
17800611676Syamt */
17900611676Syamt if (reles & VDESC_VP0_WILLRELE)
180c3183f32Spooka vref(*this_vp_p);
181cde1d475Smycroft }
182cde1d475Smycroft
183cde1d475Smycroft }
184cde1d475Smycroft
185cde1d475Smycroft /*
186cde1d475Smycroft * Fix the credentials. (That's the purpose of this layer.)
187cde1d475Smycroft */
188cde1d475Smycroft
189cde1d475Smycroft if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
190cde1d475Smycroft
191fc9422c9Selad credpp = VOPARG_OFFSETTO(kauth_cred_t*,
192cde1d475Smycroft descp->vdesc_cred_offset, ap);
193cde1d475Smycroft
194cde1d475Smycroft /* Save old values */
195cde1d475Smycroft
196cde1d475Smycroft savecredp = *credpp;
197821f05b0Splunky if (savecredp != NOCRED && savecredp != FSCRED)
198fc9422c9Selad *credpp = kauth_cred_dup(savecredp);
199cde1d475Smycroft credp = *credpp;
200cde1d475Smycroft
201fc9422c9Selad if ((flags & LAYERFS_MBYPASSDEBUG) &&
20259e67acdSelad kauth_cred_geteuid(credp) != 0)
20392a808f1Schristos printf("umap_bypass: user was %d, group %d\n",
204fc9422c9Selad kauth_cred_geteuid(credp), kauth_cred_getegid(credp));
205cde1d475Smycroft
206cde1d475Smycroft /* Map all ids in the credential structure. */
207cde1d475Smycroft
2089866514dSwrstuden umap_mapids(vp0->v_mount, credp);
209cde1d475Smycroft
210fc9422c9Selad if ((flags & LAYERFS_MBYPASSDEBUG) &&
21159e67acdSelad kauth_cred_geteuid(credp) != 0)
21292a808f1Schristos printf("umap_bypass: user now %d, group %d\n",
213fc9422c9Selad kauth_cred_geteuid(credp), kauth_cred_getegid(credp));
214cde1d475Smycroft }
215cde1d475Smycroft
216cde1d475Smycroft /* BSD often keeps a credential in the componentname structure
217cde1d475Smycroft * for speed. If there is one, it better get mapped, too.
218cde1d475Smycroft */
219cde1d475Smycroft
220cde1d475Smycroft if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
221cde1d475Smycroft
222cde1d475Smycroft compnamepp = VOPARG_OFFSETTO(struct componentname**,
223cde1d475Smycroft descp->vdesc_componentname_offset, ap);
224cde1d475Smycroft
225cde1d475Smycroft savecompcredp = (*compnamepp)->cn_cred;
226821f05b0Splunky if (savecompcredp != NOCRED && savecompcredp != FSCRED)
227fc9422c9Selad (*compnamepp)->cn_cred = kauth_cred_dup(savecompcredp);
228cde1d475Smycroft compcredp = (*compnamepp)->cn_cred;
229cde1d475Smycroft
230fc9422c9Selad if ((flags & LAYERFS_MBYPASSDEBUG) &&
23159e67acdSelad kauth_cred_geteuid(compcredp) != 0)
23292a808f1Schristos printf("umap_bypass: component credit user was %d, group %d\n",
233fc9422c9Selad kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp));
234cde1d475Smycroft
235cde1d475Smycroft /* Map all ids in the credential structure. */
236cde1d475Smycroft
2379866514dSwrstuden umap_mapids(vp0->v_mount, compcredp);
238cde1d475Smycroft
239fc9422c9Selad if ((flags & LAYERFS_MBYPASSDEBUG) &&
24059e67acdSelad kauth_cred_geteuid(compcredp) != 0)
24192a808f1Schristos printf("umap_bypass: component credit user now %d, group %d\n",
242fc9422c9Selad kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp));
243cde1d475Smycroft }
244cde1d475Smycroft
245cde1d475Smycroft /*
246cde1d475Smycroft * Call the operation on the lower layer
247cde1d475Smycroft * with the modified argument structure.
248cde1d475Smycroft */
24900611676Syamt error = VCALL(*vps_p[0], descp->vdesc_offset, ap);
250cde1d475Smycroft
251cde1d475Smycroft /*
252cde1d475Smycroft * Maintain the illusion of call-by-value
253cde1d475Smycroft * by restoring vnodes in the argument structure
254cde1d475Smycroft * to their original value.
255cde1d475Smycroft */
256cde1d475Smycroft reles = descp->vdesc_flags;
257cde1d475Smycroft for (i = 0; i < VDESC_MAX_VPS; reles >>= 1, i++) {
258cde1d475Smycroft if (descp->vdesc_vp_offsets[i] == VDESC_NO_OFFSET)
259cde1d475Smycroft break; /* bail out at end of list */
260cde1d475Smycroft if (old_vps[i]) {
261cde1d475Smycroft *(vps_p[i]) = old_vps[i];
2629866514dSwrstuden if (reles & VDESC_VP0_WILLRELE)
263cde1d475Smycroft vrele(*(vps_p[i]));
26400611676Syamt }
26500611676Syamt }
266cde1d475Smycroft
267cde1d475Smycroft /*
268cde1d475Smycroft * Map the possible out-going vpp
269cde1d475Smycroft * (Assumes that the lower layer always returns
270cde1d475Smycroft * a VREF'ed vpp unless it gets an error.)
271cde1d475Smycroft */
272c71a09f0Srmind if (descp->vdesc_vpp_offset != VDESC_NO_OFFSET && !error) {
273cde1d475Smycroft vppp = VOPARG_OFFSETTO(struct vnode***,
274cde1d475Smycroft descp->vdesc_vpp_offset, ap);
27500611676Syamt /*
276c95ccd22Shannken * Only vop_lookup, vop_create, vop_makedir, vop_mknod
277c95ccd22Shannken * and vop_symlink return vpp's. vop_lookup doesn't call bypass
27800611676Syamt * as a lookup on "." would generate a locking error.
279c95ccd22Shannken * So all the calls which get us here have a unlocked vpp. :-)
28000611676Syamt */
2819866514dSwrstuden error = layer_node_create(old_vps[0]->v_mount, **vppp, *vppp);
2822fa619c2Syamt if (error) {
283c95ccd22Shannken vrele(**vppp);
2842fa619c2Syamt **vppp = NULL;
2852fa619c2Syamt }
2862fa619c2Syamt }
287cde1d475Smycroft
288cde1d475Smycroft /*
289cde1d475Smycroft * Free duplicate cred structure and restore old one.
290cde1d475Smycroft */
291cde1d475Smycroft if (descp->vdesc_cred_offset != VDESC_NO_OFFSET) {
2929866514dSwrstuden if ((flags & LAYERFS_MBYPASSDEBUG) && credp &&
293fc9422c9Selad kauth_cred_geteuid(credp) != 0)
29492a808f1Schristos printf("umap_bypass: returning-user was %d\n",
295fc9422c9Selad kauth_cred_geteuid(credp));
296cde1d475Smycroft
297821f05b0Splunky if (savecredp != NOCRED && savecredp != FSCRED && credpp) {
298fc9422c9Selad kauth_cred_free(credp);
299cde1d475Smycroft *credpp = savecredp;
3009866514dSwrstuden if ((flags & LAYERFS_MBYPASSDEBUG) && credpp &&
30159e67acdSelad kauth_cred_geteuid(*credpp) != 0)
30292a808f1Schristos printf("umap_bypass: returning-user now %d\n\n",
303fc9422c9Selad kauth_cred_geteuid(savecredp));
304cde1d475Smycroft }
305cde1d475Smycroft }
306cde1d475Smycroft
307cde1d475Smycroft if (descp->vdesc_componentname_offset != VDESC_NO_OFFSET) {
3089866514dSwrstuden if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp &&
30959e67acdSelad kauth_cred_geteuid(compcredp) != 0)
31092a808f1Schristos printf("umap_bypass: returning-component-user was %d\n",
311fc9422c9Selad kauth_cred_geteuid(compcredp));
312cde1d475Smycroft
313821f05b0Splunky if (savecompcredp != NOCRED && savecompcredp != FSCRED) {
314fc9422c9Selad kauth_cred_free(compcredp);
315cde1d475Smycroft (*compnamepp)->cn_cred = savecompcredp;
3169866514dSwrstuden if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp &&
31759e67acdSelad kauth_cred_geteuid(savecompcredp) != 0)
31892a808f1Schristos printf("umap_bypass: returning-component-user now %d\n",
319fc9422c9Selad kauth_cred_geteuid(savecompcredp));
320cde1d475Smycroft }
321cde1d475Smycroft }
322cde1d475Smycroft
323cde1d475Smycroft return (error);
324cde1d475Smycroft }
325cde1d475Smycroft
326e5bc90f4Sfvdl /*
3279866514dSwrstuden * This is based on the 08-June-1999 bypass routine.
3289866514dSwrstuden * See layer_vnops.c:layer_bypass for more details.
329e5bc90f4Sfvdl */
330e5bc90f4Sfvdl int
umap_lookup(void * v)33182357f6dSdsl umap_lookup(void *v)
332e5bc90f4Sfvdl {
33397834f7bShannken struct vop_lookup_v2_args /* {
3349866514dSwrstuden struct vnodeop_desc *a_desc;
3359866514dSwrstuden struct vnode * a_dvp;
3369866514dSwrstuden struct vnode ** a_vpp;
3379866514dSwrstuden struct componentname * a_cnp;
338e5bc90f4Sfvdl } */ *ap = v;
3399866514dSwrstuden struct componentname *cnp = ap->a_cnp;
340fc9422c9Selad kauth_cred_t savecompcredp = NULL;
341fc9422c9Selad kauth_cred_t compcredp = NULL;
3429866514dSwrstuden struct vnode *dvp, *vp, *ldvp;
3439866514dSwrstuden struct mount *mp;
3449866514dSwrstuden int error;
345c398ae97Schs int flags, cnf = cnp->cn_flags;
346e5bc90f4Sfvdl
3479866514dSwrstuden dvp = ap->a_dvp;
3489866514dSwrstuden mp = dvp->v_mount;
3499866514dSwrstuden
3509866514dSwrstuden if ((cnf & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
3519866514dSwrstuden (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME))
3529866514dSwrstuden return (EROFS);
3539866514dSwrstuden
3549866514dSwrstuden flags = MOUNTTOUMAPMOUNT(mp)->umapm_flags;
3559866514dSwrstuden ldvp = UMAPVPTOLOWERVP(dvp);
3569866514dSwrstuden
3579866514dSwrstuden if (flags & LAYERFS_MBYPASSDEBUG)
3589866514dSwrstuden printf("umap_lookup\n");
3599866514dSwrstuden
3609866514dSwrstuden /*
3619866514dSwrstuden * Fix the credentials. (That's the purpose of this layer.)
3629866514dSwrstuden *
3639866514dSwrstuden * BSD often keeps a credential in the componentname structure
3649866514dSwrstuden * for speed. If there is one, it better get mapped, too.
3659866514dSwrstuden */
3669866514dSwrstuden
3679866514dSwrstuden if ((savecompcredp = cnp->cn_cred)) {
368fc9422c9Selad compcredp = kauth_cred_dup(savecompcredp);
3699866514dSwrstuden cnp->cn_cred = compcredp;
3709866514dSwrstuden
3715f7169ccSelad if ((flags & LAYERFS_MBYPASSDEBUG) &&
37259e67acdSelad kauth_cred_geteuid(compcredp) != 0)
3739866514dSwrstuden printf("umap_lookup: component credit user was %d, group %d\n",
374fc9422c9Selad kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp));
3759866514dSwrstuden
3769866514dSwrstuden /* Map all ids in the credential structure. */
3779866514dSwrstuden umap_mapids(mp, compcredp);
3789866514dSwrstuden }
3799866514dSwrstuden
3805f7169ccSelad if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp &&
38159e67acdSelad kauth_cred_geteuid(compcredp) != 0)
3829866514dSwrstuden printf("umap_lookup: component credit user now %d, group %d\n",
383fc9422c9Selad kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp));
3849866514dSwrstuden
3859866514dSwrstuden ap->a_dvp = ldvp;
3869866514dSwrstuden error = VCALL(ldvp, ap->a_desc->vdesc_offset, ap);
387e4a256cfSwrstuden vp = *ap->a_vpp;
388c9817d1aSyamt *ap->a_vpp = NULL;
3899866514dSwrstuden
3909866514dSwrstuden if (error == EJUSTRETURN && (cnf & ISLASTCN) &&
3919866514dSwrstuden (dvp->v_mount->mnt_flag & MNT_RDONLY) &&
3929866514dSwrstuden (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME))
3939866514dSwrstuden error = EROFS;
3949866514dSwrstuden
3959866514dSwrstuden /* Do locking fixup as appropriate. See layer_lookup() for info */
3969866514dSwrstuden if (ldvp == vp) {
3979866514dSwrstuden *ap->a_vpp = dvp;
398c3183f32Spooka vref(dvp);
3999866514dSwrstuden vrele(vp);
4009866514dSwrstuden } else if (vp != NULL) {
4019866514dSwrstuden error = layer_node_create(mp, vp, ap->a_vpp);
4022fa619c2Syamt if (error) {
40397834f7bShannken vrele(vp);
4042fa619c2Syamt }
405e5bc90f4Sfvdl }
406e5bc90f4Sfvdl
407e5bc90f4Sfvdl /*
4089866514dSwrstuden * Free duplicate cred structure and restore old one.
409e5bc90f4Sfvdl */
4109866514dSwrstuden if ((flags & LAYERFS_MBYPASSDEBUG) && compcredp &&
41159e67acdSelad kauth_cred_geteuid(compcredp) != 0)
4129866514dSwrstuden printf("umap_lookup: returning-component-user was %d\n",
413fc9422c9Selad kauth_cred_geteuid(compcredp));
414e5bc90f4Sfvdl
415821f05b0Splunky if (savecompcredp != NOCRED && savecompcredp != FSCRED) {
4169ae6310dSchristos if (compcredp)
417fc9422c9Selad kauth_cred_free(compcredp);
4189866514dSwrstuden cnp->cn_cred = savecompcredp;
4199866514dSwrstuden if ((flags & LAYERFS_MBYPASSDEBUG) && savecompcredp &&
42059e67acdSelad kauth_cred_geteuid(savecompcredp) != 0)
4219866514dSwrstuden printf("umap_lookup: returning-component-user now %d\n",
422fc9422c9Selad kauth_cred_geteuid(savecompcredp));
423e5bc90f4Sfvdl }
424cde1d475Smycroft
4259866514dSwrstuden return (error);
42636dc99adSsommerfe }
42736dc99adSsommerfe
42836dc99adSsommerfe /*
429cde1d475Smycroft * We handle getattr to change the fsid.
430cde1d475Smycroft */
431cde1d475Smycroft int
umap_getattr(void * v)43282357f6dSdsl umap_getattr(void *v)
433631ccba6Schristos {
434cde1d475Smycroft struct vop_getattr_args /* {
435cde1d475Smycroft struct vnode *a_vp;
436cde1d475Smycroft struct vattr *a_vap;
437fc9422c9Selad kauth_cred_t a_cred;
43895e1ffb1Schristos struct lwp *a_l;
439631ccba6Schristos } */ *ap = v;
440cde1d475Smycroft uid_t uid;
441cde1d475Smycroft gid_t gid;
4429866514dSwrstuden int error, tmpid, nentries, gnentries, flags;
443df052e72Scgd u_long (*mapdata)[2];
444df052e72Scgd u_long (*gmapdata)[2];
445cde1d475Smycroft struct vnode **vp1p;
44634c8ae80Sjdolecek const struct vnodeop_desc *descp = ap->a_desc;
447cde1d475Smycroft
448631ccba6Schristos if ((error = umap_bypass(ap)) != 0)
449cde1d475Smycroft return (error);
450cde1d475Smycroft /* Requires that arguments be restored. */
4516bd1d6d4Schristos ap->a_vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsidx.__fsid_val[0];
452cde1d475Smycroft
4539866514dSwrstuden flags = MOUNTTOUMAPMOUNT(ap->a_vp->v_mount)->umapm_flags;
454cde1d475Smycroft /*
455cde1d475Smycroft * Umap needs to map the uid and gid returned by a stat
456cde1d475Smycroft * into the proper values for this site. This involves
457cde1d475Smycroft * finding the returned uid in the mapping information,
458cde1d475Smycroft * translating it into the uid on the other end,
459cde1d475Smycroft * and filling in the proper field in the vattr
460cde1d475Smycroft * structure pointed to by ap->a_vap. The group
461cde1d475Smycroft * is easier, since currently all groups will be
462cde1d475Smycroft * translate to the NULLGROUP.
463cde1d475Smycroft */
464cde1d475Smycroft
465cde1d475Smycroft /* Find entry in map */
466cde1d475Smycroft
467cde1d475Smycroft uid = ap->a_vap->va_uid;
468cde1d475Smycroft gid = ap->a_vap->va_gid;
4699866514dSwrstuden if ((flags & LAYERFS_MBYPASSDEBUG))
47092a808f1Schristos printf("umap_getattr: mapped uid = %d, mapped gid = %d\n", uid,
471cde1d475Smycroft gid);
472cde1d475Smycroft
473cde1d475Smycroft vp1p = VOPARG_OFFSETTO(struct vnode**, descp->vdesc_vp_offsets[0], ap);
474cde1d475Smycroft nentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_nentries;
475cde1d475Smycroft mapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_mapdata);
476cde1d475Smycroft gnentries = MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gnentries;
477cde1d475Smycroft gmapdata = (MOUNTTOUMAPMOUNT((*vp1p)->v_mount)->info_gmapdata);
478cde1d475Smycroft
479cde1d475Smycroft /* Reverse map the uid for the vnode. Since it's a reverse
480cde1d475Smycroft map, we can't use umap_mapids() to do it. */
481cde1d475Smycroft
482cde1d475Smycroft tmpid = umap_reverse_findid(uid, mapdata, nentries);
483cde1d475Smycroft
484cde1d475Smycroft if (tmpid != -1) {
485cde1d475Smycroft ap->a_vap->va_uid = (uid_t) tmpid;
4869866514dSwrstuden if ((flags & LAYERFS_MBYPASSDEBUG))
48792a808f1Schristos printf("umap_getattr: original uid = %d\n", uid);
488cde1d475Smycroft } else
489cde1d475Smycroft ap->a_vap->va_uid = (uid_t) NOBODY;
490cde1d475Smycroft
491cde1d475Smycroft /* Reverse map the gid for the vnode. */
492cde1d475Smycroft
493cde1d475Smycroft tmpid = umap_reverse_findid(gid, gmapdata, gnentries);
494cde1d475Smycroft
495cde1d475Smycroft if (tmpid != -1) {
496cde1d475Smycroft ap->a_vap->va_gid = (gid_t) tmpid;
4979866514dSwrstuden if ((flags & LAYERFS_MBYPASSDEBUG))
49892a808f1Schristos printf("umap_getattr: original gid = %d\n", gid);
499cde1d475Smycroft } else
500cde1d475Smycroft ap->a_vap->va_gid = (gid_t) NULLGROUP;
501cde1d475Smycroft
502cde1d475Smycroft return (0);
503cde1d475Smycroft }
504cde1d475Smycroft
505cde1d475Smycroft int
umap_print(void * v)50682357f6dSdsl umap_print(void *v)
507631ccba6Schristos {
508cde1d475Smycroft struct vop_print_args /* {
509cde1d475Smycroft struct vnode *a_vp;
510631ccba6Schristos } */ *ap = v;
511cde1d475Smycroft struct vnode *vp = ap->a_vp;
51292a808f1Schristos printf("\ttag VT_UMAPFS, vp=%p, lowervp=%p\n", vp,
513f7f3f429Scgd UMAPVPTOLOWERVP(vp));
514cde1d475Smycroft return (0);
515cde1d475Smycroft }
516cde1d475Smycroft
517cde1d475Smycroft int
umap_rename(void * v)51882357f6dSdsl umap_rename(void *v)
519631ccba6Schristos {
520cde1d475Smycroft struct vop_rename_args /* {
521cde1d475Smycroft struct vnode *a_fdvp;
522cde1d475Smycroft struct vnode *a_fvp;
523cde1d475Smycroft struct componentname *a_fcnp;
524cde1d475Smycroft struct vnode *a_tdvp;
525cde1d475Smycroft struct vnode *a_tvp;
526cde1d475Smycroft struct componentname *a_tcnp;
527631ccba6Schristos } */ *ap = v;
5289866514dSwrstuden int error, flags;
529cde1d475Smycroft struct componentname *compnamep;
530fc9422c9Selad kauth_cred_t compcredp, savecompcredp;
531cde1d475Smycroft struct vnode *vp;
532656e74e2Syamt struct vnode *tvp;
533cde1d475Smycroft
534cde1d475Smycroft /*
535cde1d475Smycroft * Rename is irregular, having two componentname structures.
536cde1d475Smycroft * We need to map the cre in the second structure,
537cde1d475Smycroft * and then bypass takes care of the rest.
538cde1d475Smycroft */
539cde1d475Smycroft
540cde1d475Smycroft vp = ap->a_fdvp;
5419866514dSwrstuden flags = MOUNTTOUMAPMOUNT(vp->v_mount)->umapm_flags;
542cde1d475Smycroft compnamep = ap->a_tcnp;
543cde1d475Smycroft compcredp = compnamep->cn_cred;
544cde1d475Smycroft
545cde1d475Smycroft savecompcredp = compcredp;
546fc9422c9Selad compcredp = compnamep->cn_cred = kauth_cred_dup(savecompcredp);
547cde1d475Smycroft
548fc9422c9Selad if ((flags & LAYERFS_MBYPASSDEBUG) &&
54959e67acdSelad kauth_cred_geteuid(compcredp) != 0)
55092a808f1Schristos printf("umap_rename: rename component credit user was %d, group %d\n",
551fc9422c9Selad kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp));
552cde1d475Smycroft
553cde1d475Smycroft /* Map all ids in the credential structure. */
554cde1d475Smycroft
555cde1d475Smycroft umap_mapids(vp->v_mount, compcredp);
556cde1d475Smycroft
557fc9422c9Selad if ((flags & LAYERFS_MBYPASSDEBUG) &&
55859e67acdSelad kauth_cred_geteuid(compcredp) != 0)
55992a808f1Schristos printf("umap_rename: rename component credit user now %d, group %d\n",
560fc9422c9Selad kauth_cred_geteuid(compcredp), kauth_cred_getegid(compcredp));
561cde1d475Smycroft
562656e74e2Syamt tvp = ap->a_tvp;
563656e74e2Syamt if (tvp) {
564656e74e2Syamt if (tvp->v_mount != vp->v_mount)
565656e74e2Syamt tvp = NULL;
566656e74e2Syamt else
567656e74e2Syamt vref(tvp);
568656e74e2Syamt }
569cde1d475Smycroft error = umap_bypass(ap);
570656e74e2Syamt if (tvp) {
571656e74e2Syamt if (error == 0)
572656e74e2Syamt VTOLAYER(tvp)->layer_flags |= LAYERFS_REMOVED;
573656e74e2Syamt vrele(tvp);
574656e74e2Syamt }
575cde1d475Smycroft
576cde1d475Smycroft /* Restore the additional mapped componentname cred structure. */
577cde1d475Smycroft
578fc9422c9Selad kauth_cred_free(compcredp);
579cde1d475Smycroft compnamep->cn_cred = savecompcredp;
580cde1d475Smycroft
581cde1d475Smycroft return error;
582cde1d475Smycroft }
583