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