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