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