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