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