Home | History | Annotate | Line # | Download | only in umapfs
umap_vfsops.c revision 1.70
      1 /*	$NetBSD: umap_vfsops.c,v 1.70 2007/10/10 20:42:31 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.70 2007/10/10 20:42:31 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, l)
     67 	struct mount *mp;
     68 	const char *path;
     69 	void *data;
     70 	size_t *data_len;
     71 	struct lwp *l;
     72 {
     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, l);
    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 	simple_lock_init(&amp->umapm_hashlock);
    195 	amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, HASH_LIST, M_CACHE,
    196 	    M_WAITOK, &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 		free(amp, M_UFSMNT);	/* XXX */
    209 		return (error);
    210 	}
    211 	/*
    212 	 * Unlock the node (either the lower or the alias)
    213 	 */
    214 	vp->v_vflag |= VV_ROOT;
    215 	VOP_UNLOCK(vp, 0);
    216 
    217 	/*
    218 	 * Keep a held reference to the root vnode.
    219 	 * It is vrele'd in umapfs_unmount.
    220 	 */
    221 	amp->umapm_rootvp = vp;
    222 
    223 	error = set_statvfs_info(path, UIO_USERSPACE, args->umap_target,
    224 	    UIO_USERSPACE, mp->mnt_op->vfs_name, mp, l);
    225 #ifdef UMAPFS_DIAGNOSTIC
    226 	printf("umapfs_mount: lower %s, alias at %s\n",
    227 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    228 #endif
    229 	return error;
    230 }
    231 
    232 /*
    233  * Free reference to umap layer
    234  */
    235 int
    236 umapfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    237 {
    238 	struct vnode *rtvp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
    239 	int error;
    240 	int flags = 0;
    241 
    242 #ifdef UMAPFS_DIAGNOSTIC
    243 	printf("umapfs_unmount(mp = %p)\n", mp);
    244 #endif
    245 
    246 	if (mntflags & MNT_FORCE)
    247 		flags |= FORCECLOSE;
    248 
    249 	if (rtvp->v_usecount > 1 && (mntflags & MNT_FORCE) == 0)
    250 		return (EBUSY);
    251 	if ((error = vflush(mp, rtvp, flags)) != 0)
    252 		return (error);
    253 
    254 #ifdef UMAPFS_DIAGNOSTIC
    255 	vprint("alias root of lower", rtvp);
    256 #endif
    257 	/*
    258 	 * Release reference on underlying root vnode
    259 	 */
    260 	vrele(rtvp);
    261 	/*
    262 	 * And blow it away for future re-use
    263 	 */
    264 	vgone(rtvp);
    265 	/*
    266 	 * Finally, throw away the umap_mount structure
    267 	 */
    268 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    269 	mp->mnt_data = 0;
    270 	return (0);
    271 }
    272 
    273 SYSCTL_SETUP(sysctl_vfs_umap_setup, "sysctl vfs.umap subtree setup")
    274 {
    275 
    276 	sysctl_createv(clog, 0, NULL, NULL,
    277 		       CTLFLAG_PERMANENT,
    278 		       CTLTYPE_NODE, "vfs", NULL,
    279 		       NULL, 0, NULL, 0,
    280 		       CTL_VFS, CTL_EOL);
    281 	sysctl_createv(clog, 0, NULL, NULL,
    282 		       CTLFLAG_PERMANENT,
    283 		       CTLTYPE_NODE, "umap",
    284 		       SYSCTL_DESCR("UID/GID remapping file system"),
    285 		       NULL, 0, NULL, 0,
    286 		       CTL_VFS, 10, CTL_EOL);
    287 	/*
    288 	 * XXX the "10" above could be dynamic, thereby eliminating
    289 	 * one more instance of the "number to vfs" mapping problem,
    290 	 * but "10" is the order as taken from sys/mount.h
    291 	 */
    292 }
    293 
    294 extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
    295 
    296 const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
    297 	&umapfs_vnodeop_opv_desc,
    298 	NULL,
    299 };
    300 
    301 struct vfsops umapfs_vfsops = {
    302 	MOUNT_UMAP,
    303 	sizeof (struct umap_args),
    304 	umapfs_mount,
    305 	layerfs_start,
    306 	umapfs_unmount,
    307 	layerfs_root,
    308 	layerfs_quotactl,
    309 	layerfs_statvfs,
    310 	layerfs_sync,
    311 	layerfs_vget,
    312 	layerfs_fhtovp,
    313 	layerfs_vptofh,
    314 	layerfs_init,
    315 	NULL,
    316 	layerfs_done,
    317 	NULL,				/* vfs_mountroot */
    318 	layerfs_snapshot,
    319 	vfs_stdextattrctl,
    320 	(void *)eopnotsupp,		/* vfs_suspendctl */
    321 	umapfs_vnodeopv_descs,
    322 	0,				/* vfs_refcount */
    323 	{ NULL, NULL },
    324 };
    325 VFS_ATTACH(umapfs_vfsops);
    326