Home | History | Annotate | Line # | Download | only in umapfs
umap_vfsops.c revision 1.51
      1 /*	$NetBSD: umap_vfsops.c,v 1.51 2005/02/26 22:59:00 perry 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.51 2005/02/26 22:59:00 perry 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 <miscfs/umapfs/umap.h>
     56 #include <miscfs/genfs/layer_extern.h>
     57 
     58 int	umapfs_mount __P((struct mount *, const char *, void *,
     59 			  struct nameidata *, struct proc *));
     60 int	umapfs_unmount __P((struct mount *, int, struct proc *));
     61 
     62 /*
     63  * Mount umap layer
     64  */
     65 int
     66 umapfs_mount(mp, path, data, ndp, p)
     67 	struct mount *mp;
     68 	const char *path;
     69 	void *data;
     70 	struct nameidata *ndp;
     71 	struct proc *p;
     72 {
     73 	struct umap_args args;
     74 	struct vnode *lowerrootvp, *vp;
     75 	struct umap_mount *amp;
     76 	int error;
     77 #ifdef UMAPFS_DIAGNOSTIC
     78 	int i;
     79 #endif
     80 	if (mp->mnt_flag & MNT_GETARGS) {
     81 		amp = MOUNTTOUMAPMOUNT(mp);
     82 		if (amp == NULL)
     83 			return EIO;
     84 		args.la.target = NULL;
     85 		vfs_showexport(mp, &args.la.export, &amp->umapm_export);
     86 		args.nentries = amp->info_nentries;
     87 		args.gnentries = amp->info_gnentries;
     88 		return copyout(&args, data, sizeof(args));
     89 	}
     90 
     91 	/* only for root */
     92 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
     93 		return error;
     94 
     95 #ifdef UMAPFS_DIAGNOSTIC
     96 	printf("umapfs_mount(mp = %p)\n", mp);
     97 #endif
     98 
     99 	/*
    100 	 * Get argument
    101 	 */
    102 	error = copyin(data, &args, sizeof(struct umap_args));
    103 	if (error)
    104 		return (error);
    105 
    106 	/*
    107 	 * Update only does export updating.
    108 	 */
    109 	if (mp->mnt_flag & MNT_UPDATE) {
    110 		amp = MOUNTTOUMAPMOUNT(mp);
    111 		if (args.umap_target == 0)
    112 			return (vfs_export(mp, &amp->umapm_export,
    113 					&args.umap_export));
    114 		else
    115 			return (EOPNOTSUPP);
    116 	}
    117 
    118 	/*
    119 	 * Find lower node
    120 	 */
    121 	NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF,
    122 		UIO_USERSPACE, args.umap_target, p);
    123 	if ((error = namei(ndp)) != 0)
    124 		return (error);
    125 
    126 	/*
    127 	 * Sanity check on lower vnode
    128 	 */
    129 	lowerrootvp = ndp->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 	mp->mnt_leaf = lowerrootvp->v_mount->mnt_leaf;
    149 	amp->umapm_vfs = lowerrootvp->v_mount;
    150 	if (amp->umapm_vfs->mnt_flag & MNT_LOCAL)
    151 		mp->mnt_flag |= MNT_LOCAL;
    152 
    153 	/*
    154 	 * Now copy in the number of entries and maps for umap mapping.
    155 	 */
    156 	if (args.nentries > MAPFILEENTRIES || args.gnentries > GMAPFILEENTRIES) {
    157 		vput(lowerrootvp);
    158 		return (error);
    159 	}
    160 
    161 	amp->info_nentries = args.nentries;
    162 	amp->info_gnentries = args.gnentries;
    163 	error = copyin(args.mapdata, amp->info_mapdata,
    164 	    2*sizeof(u_long)*args.nentries);
    165 	if (error) {
    166 		vput(lowerrootvp);
    167 		return (error);
    168 	}
    169 
    170 #ifdef UMAPFS_DIAGNOSTIC
    171 	printf("umap_mount:nentries %d\n",args.nentries);
    172 	for (i = 0; i < args.nentries; i++)
    173 		printf("   %ld maps to %ld\n", amp->info_mapdata[i][0],
    174 	 	    amp->info_mapdata[i][1]);
    175 #endif
    176 
    177 	error = copyin(args.gmapdata, amp->info_gmapdata,
    178 	    2*sizeof(u_long)*args.gnentries);
    179 	if (error) {
    180 		vput(lowerrootvp);
    181 		return (error);
    182 	}
    183 
    184 #ifdef UMAPFS_DIAGNOSTIC
    185 	printf("umap_mount:gnentries %d\n",args.gnentries);
    186 	for (i = 0; i < args.gnentries; i++)
    187 		printf("\tgroup %ld maps to %ld\n",
    188 		    amp->info_gmapdata[i][0],
    189 	 	    amp->info_gmapdata[i][1]);
    190 #endif
    191 
    192 	/*
    193 	 * Make sure the mount point's sufficiently initialized
    194 	 * that the node create call will work.
    195 	 */
    196 	vfs_getnewfsid(mp);
    197 	amp->umapm_size = sizeof(struct umap_node);
    198 	amp->umapm_tag = VT_UMAP;
    199 	amp->umapm_bypass = umap_bypass;
    200 	amp->umapm_alloc = layer_node_alloc;	/* the default alloc is fine */
    201 	amp->umapm_vnodeop_p = umap_vnodeop_p;
    202 	simple_lock_init(&amp->umapm_hashlock);
    203 	amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, HASH_LIST, M_CACHE,
    204 	    M_WAITOK, &amp->umapm_node_hash);
    205 
    206 
    207 	/*
    208 	 * fix up umap node for root vnode.
    209 	 */
    210 	error = layer_node_create(mp, lowerrootvp, &vp);
    211 	/*
    212 	 * Make sure the node alias worked
    213 	 */
    214 	if (error) {
    215 		vput(lowerrootvp);
    216 		free(amp, M_UFSMNT);	/* XXX */
    217 		return (error);
    218 	}
    219 	/*
    220 	 * Unlock the node (either the lower or the alias)
    221 	 */
    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 	vp->v_flag |= VROOT;
    229 	amp->umapm_rootvp = vp;
    230 
    231 	error = set_statvfs_info(path, UIO_USERSPACE, args.umap_target,
    232 	    UIO_USERSPACE, mp, p);
    233 #ifdef UMAPFS_DIAGNOSTIC
    234 	printf("umapfs_mount: lower %s, alias at %s\n",
    235 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    236 #endif
    237 	return error;
    238 }
    239 
    240 /*
    241  * Free reference to umap layer
    242  */
    243 int
    244 umapfs_unmount(mp, mntflags, p)
    245 	struct mount *mp;
    246 	int mntflags;
    247 	struct proc *p;
    248 {
    249 	struct vnode *rootvp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
    250 	int error;
    251 	int flags = 0;
    252 
    253 #ifdef UMAPFS_DIAGNOSTIC
    254 	printf("umapfs_unmount(mp = %p)\n", mp);
    255 #endif
    256 
    257 	if (mntflags & MNT_FORCE)
    258 		flags |= FORCECLOSE;
    259 
    260 	/*
    261 	 * Clear out buffer cache.  I don't think we
    262 	 * ever get anything cached at this level at the
    263 	 * moment, but who knows...
    264 	 */
    265 #ifdef notyet
    266 	mntflushbuf(mp, 0);
    267 	if (mntinvalbuf(mp, 1))
    268 		return (EBUSY);
    269 #endif
    270 	if (rootvp->v_usecount > 1)
    271 		return (EBUSY);
    272 	if ((error = vflush(mp, rootvp, flags)) != 0)
    273 		return (error);
    274 
    275 #ifdef UMAPFS_DIAGNOSTIC
    276 	vprint("alias root of lower", rootvp);
    277 #endif
    278 	/*
    279 	 * Release reference on underlying root vnode
    280 	 */
    281 	vrele(rootvp);
    282 	/*
    283 	 * And blow it away for future re-use
    284 	 */
    285 	vgone(rootvp);
    286 	/*
    287 	 * Finally, throw away the umap_mount structure
    288 	 */
    289 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    290 	mp->mnt_data = 0;
    291 	return (0);
    292 }
    293 
    294 SYSCTL_SETUP(sysctl_vfs_umap_setup, "sysctl vfs.umap subtree setup")
    295 {
    296 
    297 	sysctl_createv(clog, 0, NULL, NULL,
    298 		       CTLFLAG_PERMANENT,
    299 		       CTLTYPE_NODE, "vfs", NULL,
    300 		       NULL, 0, NULL, 0,
    301 		       CTL_VFS, CTL_EOL);
    302 	sysctl_createv(clog, 0, NULL, NULL,
    303 		       CTLFLAG_PERMANENT,
    304 		       CTLTYPE_NODE, "umap",
    305 		       SYSCTL_DESCR("UID/GID remapping file system"),
    306 		       NULL, 0, NULL, 0,
    307 		       CTL_VFS, 10, CTL_EOL);
    308 	/*
    309 	 * XXX the "10" above could be dynamic, thereby eliminating
    310 	 * one more instance of the "number to vfs" mapping problem,
    311 	 * but "10" is the order as taken from sys/mount.h
    312 	 */
    313 }
    314 
    315 extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
    316 
    317 const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
    318 	&umapfs_vnodeop_opv_desc,
    319 	NULL,
    320 };
    321 
    322 struct vfsops umapfs_vfsops = {
    323 	MOUNT_UMAP,
    324 	umapfs_mount,
    325 	layerfs_start,
    326 	umapfs_unmount,
    327 	layerfs_root,
    328 	layerfs_quotactl,
    329 	layerfs_statvfs,
    330 	layerfs_sync,
    331 	layerfs_vget,
    332 	layerfs_fhtovp,
    333 	layerfs_vptofh,
    334 	layerfs_init,
    335 	NULL,
    336 	layerfs_done,
    337 	NULL,
    338 	NULL,				/* vfs_mountroot */
    339 	layerfs_checkexp,
    340 	layerfs_snapshot,
    341 	vfs_stdextattrctl,
    342 	umapfs_vnodeopv_descs,
    343 };
    344