1*cfe3f2c3Shannken /* $NetBSD: umap_vfsops.c,v 1.104 2022/11/04 11:20:40 hannken 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 *
34cde1d475Smycroft * from: @(#)null_vfsops.c 1.5 (Berkeley) 7/10/92
35e5bc90f4Sfvdl * @(#)umap_vfsops.c 8.8 (Berkeley) 5/14/95
36cde1d475Smycroft */
37cde1d475Smycroft
38cde1d475Smycroft /*
39cde1d475Smycroft * Umap Layer
40cde1d475Smycroft * (See mount_umap(8) for a description of this layer.)
41cde1d475Smycroft */
42cde1d475Smycroft
43e4b00f43Slukem #include <sys/cdefs.h>
44*cfe3f2c3Shannken __KERNEL_RCSID(0, "$NetBSD: umap_vfsops.c,v 1.104 2022/11/04 11:20:40 hannken Exp $");
45e4b00f43Slukem
46cde1d475Smycroft #include <sys/param.h>
47cde1d475Smycroft #include <sys/systm.h>
4813f8d2ceSatatat #include <sys/sysctl.h>
49e5bc90f4Sfvdl #include <sys/proc.h>
50cde1d475Smycroft #include <sys/time.h>
51cde1d475Smycroft #include <sys/vnode.h>
52cde1d475Smycroft #include <sys/mount.h>
53cde1d475Smycroft #include <sys/namei.h>
54473107eaSperseant #include <sys/syslog.h>
55fc9422c9Selad #include <sys/kauth.h>
56a1221b6dSrumble #include <sys/module.h>
57fc9422c9Selad
58cde1d475Smycroft #include <miscfs/umapfs/umap.h>
599866514dSwrstuden #include <miscfs/genfs/layer_extern.h>
60cde1d475Smycroft
61024c6fe9Spooka MODULE(MODULE_CLASS_VFS, umap, "layerfs");
62a1221b6dSrumble
638d1f8992Spooka VFS_PROTOS(umapfs);
64631ccba6Schristos
65cde1d475Smycroft /*
66cde1d475Smycroft * Mount umap layer
67cde1d475Smycroft */
68cde1d475Smycroft int
umapfs_mount(struct mount * mp,const char * path,void * data,size_t * data_len)69454af1c0Sdsl umapfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
70cde1d475Smycroft {
7161e8303eSpooka struct lwp *l = curlwp;
728f6ed30dSdholland struct pathbuf *pb;
738d1f8992Spooka struct nameidata nd;
742721ab6cSdsl struct umap_args *args = data;
75cde1d475Smycroft struct vnode *lowerrootvp, *vp;
76cde1d475Smycroft struct umap_mount *amp;
77cde1d475Smycroft int error;
78247156f4Sperseant #ifdef UMAPFS_DIAGNOSTIC
79247156f4Sperseant int i;
80247156f4Sperseant #endif
81473107eaSperseant fsid_t tfsid;
8295e1ffb1Schristos
8323f76b6dSmaxv if (args == NULL)
8423f76b6dSmaxv return EINVAL;
85473107eaSperseant if (*data_len < sizeof *args) {
86473107eaSperseant #ifdef UMAPFS_DIAGNOSTIC
87473107eaSperseant printf("mount_umap: data len %d < args %d\n",
88473107eaSperseant (int)*data_len, (int)(sizeof *args));
89473107eaSperseant #endif
902721ab6cSdsl return EINVAL;
91473107eaSperseant }
922721ab6cSdsl
936868d0a7Schristos if (mp->mnt_flag & MNT_GETARGS) {
946868d0a7Schristos amp = MOUNTTOUMAPMOUNT(mp);
956868d0a7Schristos if (amp == NULL)
966868d0a7Schristos return EIO;
972721ab6cSdsl args->la.target = NULL;
982721ab6cSdsl args->nentries = amp->info_nentries;
992721ab6cSdsl args->gnentries = amp->info_gnentries;
1002721ab6cSdsl *data_len = sizeof *args;
1012721ab6cSdsl return 0;
1026868d0a7Schristos }
103cde1d475Smycroft
1040a2d2af9Sbouyer /* only for root */
1050c9d8d15Selad error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
1060c9d8d15Selad KAUTH_REQ_SYSTEM_MOUNT_UMAP, NULL, NULL, NULL);
1070c9d8d15Selad if (error)
1080a2d2af9Sbouyer return error;
1090a2d2af9Sbouyer
110cde1d475Smycroft #ifdef UMAPFS_DIAGNOSTIC
1114fb8bf72Schristos printf("umapfs_mount(mp = %p)\n", mp);
112cde1d475Smycroft #endif
113cde1d475Smycroft
114cde1d475Smycroft /*
1152a3e5eebSjmmv * Update is not supported
1169866514dSwrstuden */
1172a3e5eebSjmmv if (mp->mnt_flag & MNT_UPDATE)
1182a3e5eebSjmmv return EOPNOTSUPP;
1199866514dSwrstuden
1209866514dSwrstuden /*
121cde1d475Smycroft * Find lower node
122cde1d475Smycroft */
1238f6ed30dSdholland error = pathbuf_copyin(args->umap_target, &pb);
1248f6ed30dSdholland if (error) {
1258f6ed30dSdholland return error;
1268f6ed30dSdholland }
1278f6ed30dSdholland NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, pb);
1288f6ed30dSdholland if ((error = namei(&nd)) != 0) {
1298f6ed30dSdholland pathbuf_destroy(pb);
1308f6ed30dSdholland return error;
1318f6ed30dSdholland }
132cde1d475Smycroft
133cde1d475Smycroft /*
134cde1d475Smycroft * Sanity check on lower vnode
135cde1d475Smycroft */
1368d1f8992Spooka lowerrootvp = nd.ni_vp;
1378f6ed30dSdholland pathbuf_destroy(pb);
138cde1d475Smycroft #ifdef UMAPFS_DIAGNOSTIC
1394fb8bf72Schristos printf("vp = %p, check for VDIR...\n", lowerrootvp);
140cde1d475Smycroft #endif
141cde1d475Smycroft
142cde1d475Smycroft if (lowerrootvp->v_type != VDIR) {
143cde1d475Smycroft vput(lowerrootvp);
144cde1d475Smycroft return (EINVAL);
145cde1d475Smycroft }
146cde1d475Smycroft
147cde1d475Smycroft #ifdef UMAPFS_DIAGNOSTIC
1484fb8bf72Schristos printf("mp = %p\n", mp);
149cde1d475Smycroft #endif
150cde1d475Smycroft
151f1d428afSrmind amp = kmem_zalloc(sizeof(struct umap_mount), KM_SLEEP);
152178d83d5Ssoren mp->mnt_data = amp;
153cde1d475Smycroft
154cde1d475Smycroft /*
155cde1d475Smycroft * Now copy in the number of entries and maps for umap mapping.
156cde1d475Smycroft */
15783fce8c3Smaxv if (args->nentries < 0 || args->nentries > MAPFILEENTRIES ||
15883fce8c3Smaxv args->gnentries < 0 || args->gnentries > GMAPFILEENTRIES) {
159332bb489Sjdolecek vput(lowerrootvp);
16083fce8c3Smaxv return (EINVAL);
161332bb489Sjdolecek }
162332bb489Sjdolecek
1632721ab6cSdsl amp->info_nentries = args->nentries;
1642721ab6cSdsl amp->info_gnentries = args->gnentries;
1652721ab6cSdsl error = copyin(args->mapdata, amp->info_mapdata,
1662721ab6cSdsl 2*sizeof(u_long)*args->nentries);
1679866514dSwrstuden if (error) {
1689866514dSwrstuden vput(lowerrootvp);
169cde1d475Smycroft return (error);
1709866514dSwrstuden }
171cde1d475Smycroft
172247156f4Sperseant #ifdef UMAPFS_DIAGNOSTIC
1732721ab6cSdsl printf("umap_mount:nentries %d\n",args->nentries);
1742721ab6cSdsl for (i = 0; i < args->nentries; i++)
175247156f4Sperseant printf(" %ld maps to %ld\n", amp->info_mapdata[i][0],
176cde1d475Smycroft amp->info_mapdata[i][1]);
177cde1d475Smycroft #endif
178cde1d475Smycroft
1792721ab6cSdsl error = copyin(args->gmapdata, amp->info_gmapdata,
1802721ab6cSdsl 2*sizeof(u_long)*args->gnentries);
1819866514dSwrstuden if (error) {
1829866514dSwrstuden vput(lowerrootvp);
183cde1d475Smycroft return (error);
1849866514dSwrstuden }
185cde1d475Smycroft
186247156f4Sperseant #ifdef UMAPFS_DIAGNOSTIC
1872721ab6cSdsl printf("umap_mount:gnentries %d\n",args->gnentries);
1882721ab6cSdsl for (i = 0; i < args->gnentries; i++)
189247156f4Sperseant printf("\tgroup %ld maps to %ld\n",
190cde1d475Smycroft amp->info_gmapdata[i][0],
191cde1d475Smycroft amp->info_gmapdata[i][1]);
192cde1d475Smycroft #endif
193cde1d475Smycroft
1949866514dSwrstuden /*
1959866514dSwrstuden * Make sure the mount point's sufficiently initialized
1969866514dSwrstuden * that the node create call will work.
1979866514dSwrstuden */
198473107eaSperseant tfsid.__fsid_val[0] = (int32_t)args->fsid;
199473107eaSperseant tfsid.__fsid_val[1] = makefstype(MOUNT_UMAP);
200473107eaSperseant if (tfsid.__fsid_val[0] == 0) {
201473107eaSperseant log(LOG_WARNING, "umapfs: fsid given as 0, ignoring\n");
2026c734cd2Sassar vfs_getnewfsid(mp);
203473107eaSperseant } else if (vfs_getvfs(&tfsid)) {
204473107eaSperseant log(LOG_WARNING, "umapfs: fsid %x already mounted\n",
205473107eaSperseant tfsid.__fsid_val[0]);
206473107eaSperseant vfs_getnewfsid(mp);
207473107eaSperseant } else {
208473107eaSperseant mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
209473107eaSperseant mp->mnt_stat.f_fsidx.__fsid_val[1] = tfsid.__fsid_val[1];
210473107eaSperseant mp->mnt_stat.f_fsid = tfsid.__fsid_val[0];
211473107eaSperseant }
212473107eaSperseant log(LOG_DEBUG, "umapfs: using fsid %x/%x\n",
213473107eaSperseant mp->mnt_stat.f_fsidx.__fsid_val[0],
214473107eaSperseant mp->mnt_stat.f_fsidx.__fsid_val[1]);
215*cfe3f2c3Shannken error = vfs_set_lowermount(mp, lowerrootvp->v_mount);
216*cfe3f2c3Shannken if (error) {
217*cfe3f2c3Shannken vput(lowerrootvp);
218*cfe3f2c3Shannken kmem_free(amp, sizeof(struct umap_mount));
219*cfe3f2c3Shannken return error;
220*cfe3f2c3Shannken }
2212df7877aShannken
2229866514dSwrstuden amp->umapm_size = sizeof(struct umap_node);
2239866514dSwrstuden amp->umapm_tag = VT_UMAP;
2249866514dSwrstuden amp->umapm_bypass = umap_bypass;
2259866514dSwrstuden amp->umapm_vnodeop_p = umap_vnodeop_p;
226cde1d475Smycroft
227cde1d475Smycroft /*
2289866514dSwrstuden * fix up umap node for root vnode.
229cde1d475Smycroft */
23086536158Shannken VOP_UNLOCK(lowerrootvp);
2319866514dSwrstuden error = layer_node_create(mp, lowerrootvp, &vp);
232cde1d475Smycroft /*
233cde1d475Smycroft * Make sure the node alias worked
234cde1d475Smycroft */
235cde1d475Smycroft if (error) {
23686536158Shannken vrele(lowerrootvp);
237f1d428afSrmind kmem_free(amp, sizeof(struct umap_mount));
238f1d428afSrmind return error;
239cde1d475Smycroft }
240cde1d475Smycroft
241cde1d475Smycroft /*
242cde1d475Smycroft * Keep a held reference to the root vnode.
243cde1d475Smycroft * It is vrele'd in umapfs_unmount.
244cde1d475Smycroft */
24586536158Shannken vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
24686536158Shannken vp->v_vflag |= VV_ROOT;
2479866514dSwrstuden amp->umapm_rootvp = vp;
24886536158Shannken VOP_UNLOCK(vp);
249cde1d475Smycroft
2502721ab6cSdsl error = set_statvfs_info(path, UIO_USERSPACE, args->umap_target,
251e24b0872Spooka UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
252dd67c605Shannken if (error)
253dd67c605Shannken return error;
254228d72edShannken
255228d72edShannken if (mp->mnt_lower->mnt_flag & MNT_LOCAL)
256228d72edShannken mp->mnt_flag |= MNT_LOCAL;
257cde1d475Smycroft #ifdef UMAPFS_DIAGNOSTIC
25892a808f1Schristos printf("umapfs_mount: lower %s, alias at %s\n",
259cde1d475Smycroft mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
260cde1d475Smycroft #endif
261dd67c605Shannken return 0;
262cde1d475Smycroft }
263cde1d475Smycroft
264cde1d475Smycroft /*
265cde1d475Smycroft * Free reference to umap layer
266cde1d475Smycroft */
267cde1d475Smycroft int
umapfs_unmount(struct mount * mp,int mntflags)26861e8303eSpooka umapfs_unmount(struct mount *mp, int mntflags)
269cde1d475Smycroft {
2703fa279a5Sad struct umap_mount *amp = MOUNTTOUMAPMOUNT(mp);
2713fa279a5Sad struct vnode *rtvp = amp->umapm_rootvp;
272cde1d475Smycroft int error;
273cde1d475Smycroft int flags = 0;
274cde1d475Smycroft
275cde1d475Smycroft #ifdef UMAPFS_DIAGNOSTIC
2764fb8bf72Schristos printf("umapfs_unmount(mp = %p)\n", mp);
277cde1d475Smycroft #endif
278cde1d475Smycroft
279e5bc90f4Sfvdl if (mntflags & MNT_FORCE)
280cde1d475Smycroft flags |= FORCECLOSE;
281cde1d475Smycroft
28223bf8800Sad if (vrefcnt(rtvp) > 1 && (mntflags & MNT_FORCE) == 0)
283cde1d475Smycroft return (EBUSY);
284c107ef9eSchristos if ((error = vflush(mp, rtvp, flags)) != 0)
285cde1d475Smycroft return (error);
286cde1d475Smycroft
287cde1d475Smycroft #ifdef UMAPFS_DIAGNOSTIC
288c107ef9eSchristos vprint("alias root of lower", rtvp);
289cde1d475Smycroft #endif
290cde1d475Smycroft /*
2914a780c9aSad * Blow it away for future re-use
292cde1d475Smycroft */
293c107ef9eSchristos vgone(rtvp);
294cde1d475Smycroft /*
295cde1d475Smycroft * Finally, throw away the umap_mount structure
296cde1d475Smycroft */
297f1d428afSrmind kmem_free(amp, sizeof(struct umap_mount));
2982fd51303Ssimonb mp->mnt_data = NULL;
299f1d428afSrmind return 0;
300cde1d475Smycroft }
301cde1d475Smycroft
302d9466585Sjdolecek extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
303b5bf2ed6Sthorpej
304d9466585Sjdolecek const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
305b5bf2ed6Sthorpej &umapfs_vnodeop_opv_desc,
306b5bf2ed6Sthorpej NULL,
307b5bf2ed6Sthorpej };
308b5bf2ed6Sthorpej
309c9efd056Sthorpej struct vfsops umapfs_vfsops = {
3106d285189Shannken .vfs_name = MOUNT_UMAP,
3116d285189Shannken .vfs_min_mount_data = sizeof (struct umap_args),
3126d285189Shannken .vfs_mount = umapfs_mount,
3136d285189Shannken .vfs_start = layerfs_start,
3146d285189Shannken .vfs_unmount = umapfs_unmount,
3156d285189Shannken .vfs_root = layerfs_root,
3166d285189Shannken .vfs_quotactl = layerfs_quotactl,
3176d285189Shannken .vfs_statvfs = layerfs_statvfs,
3186d285189Shannken .vfs_sync = layerfs_sync,
31955de4c18Shannken .vfs_loadvnode = layerfs_loadvnode,
3206d285189Shannken .vfs_vget = layerfs_vget,
3216d285189Shannken .vfs_fhtovp = layerfs_fhtovp,
3226d285189Shannken .vfs_vptofh = layerfs_vptofh,
3236d285189Shannken .vfs_init = layerfs_init,
3246d285189Shannken .vfs_done = layerfs_done,
3256d285189Shannken .vfs_snapshot = layerfs_snapshot,
3266d285189Shannken .vfs_extattrctl = vfs_stdextattrctl,
327326db3aaShannken .vfs_suspendctl = layerfs_suspendctl,
3286d285189Shannken .vfs_renamelock_enter = layerfs_renamelock_enter,
3296d285189Shannken .vfs_renamelock_exit = layerfs_renamelock_exit,
3306d285189Shannken .vfs_fsync = (void *)eopnotsupp,
3316d285189Shannken .vfs_opv_descs = umapfs_vnodeopv_descs
332cde1d475Smycroft };
333a1221b6dSrumble
3349120d451Spgoyette SYSCTL_SETUP(umapfs_sysctl_setup, "umapfs sysctl")
335a1221b6dSrumble {
336a1221b6dSrumble
3379120d451Spgoyette sysctl_createv(clog, 0, NULL, NULL,
33828f5ebd8Srumble CTLFLAG_PERMANENT,
33928f5ebd8Srumble CTLTYPE_NODE, "umap",
34028f5ebd8Srumble SYSCTL_DESCR("UID/GID remapping file system"),
34128f5ebd8Srumble NULL, 0, NULL, 0,
34228f5ebd8Srumble CTL_VFS, 10, CTL_EOL);
34328f5ebd8Srumble /*
34428f5ebd8Srumble * XXX the "10" above could be dynamic, thereby eliminating
34528f5ebd8Srumble * one more instance of the "number to vfs" mapping problem,
34628f5ebd8Srumble * but "10" is the order as taken from sys/mount.h
34728f5ebd8Srumble */
3489120d451Spgoyette }
3499120d451Spgoyette
3509120d451Spgoyette static int
umap_modcmd(modcmd_t cmd,void * arg)3519120d451Spgoyette umap_modcmd(modcmd_t cmd, void *arg)
3529120d451Spgoyette {
3539120d451Spgoyette int error;
3549120d451Spgoyette
3559120d451Spgoyette switch (cmd) {
3569120d451Spgoyette case MODULE_CMD_INIT:
3579120d451Spgoyette error = vfs_attach(&umapfs_vfsops);
3589120d451Spgoyette if (error != 0)
3599120d451Spgoyette break;
36028f5ebd8Srumble break;
361a1221b6dSrumble case MODULE_CMD_FINI:
36228f5ebd8Srumble error = vfs_detach(&umapfs_vfsops);
36328f5ebd8Srumble if (error != 0)
36428f5ebd8Srumble break;
36528f5ebd8Srumble break;
366a1221b6dSrumble default:
36728f5ebd8Srumble error = ENOTTY;
36828f5ebd8Srumble break;
369a1221b6dSrumble }
37028f5ebd8Srumble
37128f5ebd8Srumble return (error);
372a1221b6dSrumble }
373