Home | History | Annotate | Line # | Download | only in umapfs
      1 /*	$NetBSD: umap_vfsops.c,v 1.105 2025/02/16 18:38:58 joe 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  *	from: @(#)null_vfsops.c       1.5 (Berkeley) 7/10/92
     35  *	@(#)umap_vfsops.c	8.8 (Berkeley) 5/14/95
     36  */
     37 
     38 /*
     39  * Umap Layer
     40  * (See mount_umap(8) for a description of this layer.)
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: umap_vfsops.c,v 1.105 2025/02/16 18:38:58 joe Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/sysctl.h>
     49 #include <sys/proc.h>
     50 #include <sys/time.h>
     51 #include <sys/vnode.h>
     52 #include <sys/mount.h>
     53 #include <sys/namei.h>
     54 #include <sys/syslog.h>
     55 #include <sys/kauth.h>
     56 #include <sys/module.h>
     57 
     58 #include <miscfs/umapfs/umap.h>
     59 #include <miscfs/genfs/layer_extern.h>
     60 
     61 MODULE(MODULE_CLASS_VFS, umap, "layerfs");
     62 
     63 VFS_PROTOS(umapfs);
     64 
     65 /*
     66  * Mount umap layer
     67  */
     68 int
     69 umapfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
     70 {
     71 	struct lwp *l = curlwp;
     72 	struct pathbuf *pb;
     73 	struct nameidata nd;
     74 	struct umap_args *args = data;
     75 	struct vnode *lowerrootvp, *vp;
     76 	struct umap_mount *amp;
     77 	int error;
     78 #ifdef UMAPFS_DIAGNOSTIC
     79 	int i;
     80 #endif
     81 	fsid_t tfsid;
     82 
     83 	if (args == NULL)
     84 		return EINVAL;
     85 	if (*data_len < sizeof *args) {
     86 #ifdef UMAPFS_DIAGNOSTIC
     87 		printf("mount_umap: data len %d < args %d\n",
     88 			(int)*data_len, (int)(sizeof *args));
     89 #endif
     90 		return EINVAL;
     91 	}
     92 
     93 	if (mp->mnt_flag & MNT_GETARGS) {
     94 		amp = MOUNTTOUMAPMOUNT(mp);
     95 		if (amp == NULL)
     96 			return EIO;
     97 		args->la.target = NULL;
     98 		args->nentries = amp->info_nentries;
     99 		args->gnentries = amp->info_gnentries;
    100 		*data_len = sizeof *args;
    101 		return 0;
    102 	}
    103 
    104 	/* only for root */
    105 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
    106 	    KAUTH_REQ_SYSTEM_MOUNT_UMAP, NULL, NULL, NULL);
    107 	if (error)
    108 		return error;
    109 
    110 #ifdef UMAPFS_DIAGNOSTIC
    111 	printf("umapfs_mount(mp = %p)\n", mp);
    112 #endif
    113 
    114 	/*
    115 	 * Update is not supported
    116 	 */
    117 	if (mp->mnt_flag & MNT_UPDATE)
    118 		return EOPNOTSUPP;
    119 
    120 	/*
    121 	 * Find lower node
    122 	 */
    123 	error = pathbuf_copyin(args->umap_target, &pb);
    124 	if (error) {
    125 		return error;
    126 	}
    127 	NDINIT(&nd, LOOKUP, FOLLOW|LOCKLEAF, pb);
    128 	if ((error = namei(&nd)) != 0) {
    129 		pathbuf_destroy(pb);
    130 		return error;
    131 	}
    132 
    133 	/*
    134 	 * Sanity check on lower vnode
    135 	 */
    136 	lowerrootvp = nd.ni_vp;
    137 	pathbuf_destroy(pb);
    138 #ifdef UMAPFS_DIAGNOSTIC
    139 	printf("vp = %p, check for VDIR...\n", lowerrootvp);
    140 #endif
    141 
    142 	if (lowerrootvp->v_type != VDIR) {
    143 		vput(lowerrootvp);
    144 		return (EINVAL);
    145 	}
    146 
    147 #ifdef UMAPFS_DIAGNOSTIC
    148 	printf("mp = %p\n", mp);
    149 #endif
    150 
    151 	amp = kmem_zalloc(sizeof(struct umap_mount), KM_SLEEP);
    152 	mp->mnt_data = amp;
    153 
    154 	/*
    155 	 * Now copy in the number of entries and maps for umap mapping.
    156 	 */
    157 	if (args->nentries < 0 || args->nentries > MAPFILEENTRIES ||
    158 	    args->gnentries < 0 || args->gnentries > GMAPFILEENTRIES) {
    159 		vput(lowerrootvp);
    160 		return (EINVAL);
    161 	}
    162 
    163 	amp->info_nentries = args->nentries;
    164 	amp->info_gnentries = args->gnentries;
    165 	error = copyin(args->mapdata, amp->info_mapdata,
    166 	    2*sizeof(u_long)*args->nentries);
    167 	if (error) {
    168 		vput(lowerrootvp);
    169 		return (error);
    170 	}
    171 
    172 #ifdef UMAPFS_DIAGNOSTIC
    173 	printf("umap_mount:nentries %d\n",args->nentries);
    174 	for (i = 0; i < args->nentries; i++)
    175 		printf("   %ld maps to %ld\n", amp->info_mapdata[i][0],
    176 	 	    amp->info_mapdata[i][1]);
    177 #endif
    178 
    179 	error = copyin(args->gmapdata, amp->info_gmapdata,
    180 	    2*sizeof(u_long)*args->gnentries);
    181 	if (error) {
    182 		vput(lowerrootvp);
    183 		return (error);
    184 	}
    185 
    186 #ifdef UMAPFS_DIAGNOSTIC
    187 	printf("umap_mount:gnentries %d\n",args->gnentries);
    188 	for (i = 0; i < args->gnentries; i++)
    189 		printf("\tgroup %ld maps to %ld\n",
    190 		    amp->info_gmapdata[i][0],
    191 	 	    amp->info_gmapdata[i][1]);
    192 #endif
    193 
    194 	/*
    195 	 * Make sure the mount point's sufficiently initialized
    196 	 * that the node create call will work.
    197 	 */
    198 	tfsid.__fsid_val[0] = (int32_t)args->fsid;
    199 	tfsid.__fsid_val[1] = makefstype(MOUNT_UMAP);
    200 	if (tfsid.__fsid_val[0] == 0) {
    201 		log(LOG_WARNING, "umapfs: fsid given as 0, ignoring\n");
    202 		vfs_getnewfsid(mp);
    203 	} else if (vfs_getvfs(&tfsid)) {
    204 		log(LOG_WARNING, "umapfs: fsid %x already mounted\n",
    205 			tfsid.__fsid_val[0]);
    206 		vfs_getnewfsid(mp);
    207 	} else {
    208        		mp->mnt_stat.f_fsidx.__fsid_val[0] = tfsid.__fsid_val[0];
    209        		mp->mnt_stat.f_fsidx.__fsid_val[1] = tfsid.__fsid_val[1];
    210 		mp->mnt_stat.f_fsid = tfsid.__fsid_val[0];
    211 	}
    212 	log(LOG_DEBUG, "umapfs: using fsid %x/%x\n",
    213 		mp->mnt_stat.f_fsidx.__fsid_val[0],
    214 		mp->mnt_stat.f_fsidx.__fsid_val[1]);
    215 	error = vfs_set_lowermount(mp, lowerrootvp->v_mount);
    216 	if (error) {
    217 		vput(lowerrootvp);
    218 		kmem_free(amp, sizeof(struct umap_mount));
    219 		return error;
    220 	}
    221 
    222 	amp->umapm_size = sizeof(struct umap_node);
    223 	amp->umapm_tag = VT_UMAP;
    224 	amp->umapm_bypass = umap_bypass;
    225 	amp->umapm_vnodeop_p = umap_vnodeop_p;
    226 
    227 	/*
    228 	 * fix up umap node for root vnode.
    229 	 */
    230 	VOP_UNLOCK(lowerrootvp);
    231 	error = layer_node_create(mp, lowerrootvp, &vp);
    232 	/*
    233 	 * Make sure the node alias worked
    234 	 */
    235 	if (error) {
    236 		vrele(lowerrootvp);
    237 		kmem_free(amp, sizeof(struct umap_mount));
    238 		return error;
    239 	}
    240 
    241 	/*
    242 	 * Keep a held reference to the root vnode.
    243 	 * It is vrele'd in umapfs_unmount.
    244 	 */
    245 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    246 	vp->v_vflag |= VV_ROOT;
    247 	amp->umapm_rootvp = vp;
    248 	VOP_UNLOCK(vp);
    249 
    250 	error = set_statvfs_info(path, UIO_USERSPACE, args->umap_target,
    251 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    252 	if (error)
    253 		return error;
    254 
    255 	if (mp->mnt_lower->mnt_flag & MNT_LOCAL)
    256 		mp->mnt_flag |= MNT_LOCAL;
    257 #ifdef UMAPFS_DIAGNOSTIC
    258 	printf("umapfs_mount: lower %s, alias at %s\n",
    259 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    260 #endif
    261 	return 0;
    262 }
    263 
    264 /*
    265  * Free reference to umap layer
    266  */
    267 int
    268 umapfs_unmount(struct mount *mp, int mntflags)
    269 {
    270 	struct umap_mount *amp = MOUNTTOUMAPMOUNT(mp);
    271 	struct vnode *rtvp = amp->umapm_rootvp;
    272 	int error;
    273 	int flags = 0;
    274 
    275 #ifdef UMAPFS_DIAGNOSTIC
    276 	printf("umapfs_unmount(mp = %p)\n", mp);
    277 #endif
    278 
    279 	if (mntflags & MNT_FORCE)
    280 		flags |= FORCECLOSE;
    281 
    282 	if (vrefcnt(rtvp) > 1 && (mntflags & MNT_FORCE) == 0)
    283 		return (EBUSY);
    284 	if ((error = vflush(mp, rtvp, flags)) != 0)
    285 		return (error);
    286 
    287 #ifdef UMAPFS_DIAGNOSTIC
    288 	vprint("alias root of lower", rtvp);
    289 #endif
    290 	/*
    291 	 * Blow it away for future re-use
    292 	 */
    293 	vgone(rtvp);
    294 	/*
    295 	 * Finally, throw away the umap_mount structure
    296 	 */
    297 	kmem_free(amp, sizeof(struct umap_mount));
    298 	mp->mnt_data = NULL;
    299 	return 0;
    300 }
    301 
    302 extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
    303 
    304 const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
    305 	&umapfs_vnodeop_opv_desc,
    306 	NULL,
    307 };
    308 
    309 struct vfsops umapfs_vfsops = {
    310 	.vfs_name = MOUNT_UMAP,
    311 	.vfs_min_mount_data = sizeof (struct umap_args),
    312 	.vfs_mount = umapfs_mount,
    313 	.vfs_start = layerfs_start,
    314 	.vfs_unmount = umapfs_unmount,
    315 	.vfs_root = layerfs_root,
    316 	.vfs_quotactl = layerfs_quotactl,
    317 	.vfs_statvfs = layerfs_statvfs,
    318 	.vfs_sync = layerfs_sync,
    319 	.vfs_loadvnode = layerfs_loadvnode,
    320 	.vfs_vget = layerfs_vget,
    321 	.vfs_fhtovp = layerfs_fhtovp,
    322 	.vfs_vptofh = layerfs_vptofh,
    323 	.vfs_init = layerfs_init,
    324 	.vfs_done = layerfs_done,
    325 	.vfs_snapshot = layerfs_snapshot,
    326 	.vfs_extattrctl = vfs_stdextattrctl,
    327 	.vfs_suspendctl = layerfs_suspendctl,
    328 	.vfs_renamelock_enter = layerfs_renamelock_enter,
    329 	.vfs_renamelock_exit = layerfs_renamelock_exit,
    330 	.vfs_fsync = (void *)eopnotsupp,
    331 	.vfs_opv_descs = umapfs_vnodeopv_descs
    332 };
    333 
    334 SYSCTL_SETUP(umapfs_sysctl_setup, "umapfs sysctl")
    335 {
    336 
    337 	sysctl_createv(clog, 0, NULL, NULL,
    338 		       CTLFLAG_PERMANENT,
    339 		       CTLTYPE_NODE, "umap",
    340 		       SYSCTL_DESCR("UID/GID remapping file system"),
    341 		       NULL, 0, NULL, 0,
    342 		       CTL_VFS, 10, CTL_EOL);
    343 	/*
    344 	 * XXX the "10" above could be dynamic, thereby eliminating
    345 	 * one more instance of the "number to vfs" mapping problem,
    346 	 * but "10" is the order as taken from sys/mount.h
    347 	 */
    348 }
    349 
    350 static int
    351 umap_modcmd(modcmd_t cmd, void *arg)
    352 {
    353 	int error;
    354 
    355 	switch (cmd) {
    356 	case MODULE_CMD_INIT:
    357 		error = vfs_attach(&umapfs_vfsops);
    358 		break;
    359 	case MODULE_CMD_FINI:
    360 		error = vfs_detach(&umapfs_vfsops);
    361 		break;
    362 	default:
    363 		error = ENOTTY;
    364 		break;
    365 	}
    366 
    367 	return (error);
    368 }
    369