Home | History | Annotate | Line # | Download | only in umapfs
umap_vfsops.c revision 1.59
      1 /*	$NetBSD: umap_vfsops.c,v 1.59 2006/09/03 22:28:53 christos 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.59 2006/09/03 22:28:53 christos 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 int	umapfs_mount(struct mount *, const char *, void *,
     61 			  struct nameidata *, struct lwp *);
     62 int	umapfs_unmount(struct mount *, int, struct lwp *);
     63 
     64 /*
     65  * Mount umap layer
     66  */
     67 int
     68 umapfs_mount(mp, path, data, ndp, l)
     69 	struct mount *mp;
     70 	const char *path;
     71 	void *data;
     72 	struct nameidata *ndp;
     73 	struct lwp *l;
     74 {
     75 	struct umap_args args;
     76 	struct vnode *lowerrootvp, *vp;
     77 	struct umap_mount *amp;
     78 	int error;
     79 #ifdef UMAPFS_DIAGNOSTIC
     80 	int i;
     81 #endif
     82 
     83 	if (mp->mnt_flag & MNT_GETARGS) {
     84 		amp = MOUNTTOUMAPMOUNT(mp);
     85 		if (amp == NULL)
     86 			return EIO;
     87 		args.la.target = NULL;
     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 = kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
     95 	    &l->l_acflag)) != 0)
     96 		return error;
     97 
     98 #ifdef UMAPFS_DIAGNOSTIC
     99 	printf("umapfs_mount(mp = %p)\n", mp);
    100 #endif
    101 
    102 	/*
    103 	 * Get argument
    104 	 */
    105 	error = copyin(data, &args, sizeof(struct umap_args));
    106 	if (error)
    107 		return (error);
    108 
    109 	/*
    110 	 * Update is not supported
    111 	 */
    112 	if (mp->mnt_flag & MNT_UPDATE)
    113 		return EOPNOTSUPP;
    114 
    115 	/*
    116 	 * Find lower node
    117 	 */
    118 	NDINIT(ndp, LOOKUP, FOLLOW|LOCKLEAF,
    119 		UIO_USERSPACE, args.umap_target, l);
    120 	if ((error = namei(ndp)) != 0)
    121 		return (error);
    122 
    123 	/*
    124 	 * Sanity check on lower vnode
    125 	 */
    126 	lowerrootvp = ndp->ni_vp;
    127 #ifdef UMAPFS_DIAGNOSTIC
    128 	printf("vp = %p, check for VDIR...\n", lowerrootvp);
    129 #endif
    130 
    131 	if (lowerrootvp->v_type != VDIR) {
    132 		vput(lowerrootvp);
    133 		return (EINVAL);
    134 	}
    135 
    136 #ifdef UMAPFS_DIAGNOSTIC
    137 	printf("mp = %p\n", mp);
    138 #endif
    139 
    140 	amp = (struct umap_mount *) malloc(sizeof(struct umap_mount),
    141 				M_UFSMNT, M_WAITOK);	/* XXX */
    142 	memset(amp, 0, sizeof(struct umap_mount));
    143 
    144 	mp->mnt_data = amp;
    145 	mp->mnt_leaf = lowerrootvp->v_mount->mnt_leaf;
    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 	simple_lock_init(&amp->umapm_hashlock);
    200 	amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, HASH_LIST, M_CACHE,
    201 	    M_WAITOK, &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 		free(amp, M_UFSMNT);	/* XXX */
    214 		return (error);
    215 	}
    216 	/*
    217 	 * Unlock the node (either the lower or the alias)
    218 	 */
    219 	VOP_UNLOCK(vp, 0);
    220 
    221 	/*
    222 	 * Keep a held reference to the root vnode.
    223 	 * It is vrele'd in umapfs_unmount.
    224 	 */
    225 	vp->v_flag |= VROOT;
    226 	amp->umapm_rootvp = vp;
    227 
    228 	error = set_statvfs_info(path, UIO_USERSPACE, args.umap_target,
    229 	    UIO_USERSPACE, mp, l);
    230 #ifdef UMAPFS_DIAGNOSTIC
    231 	printf("umapfs_mount: lower %s, alias at %s\n",
    232 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    233 #endif
    234 	return error;
    235 }
    236 
    237 /*
    238  * Free reference to umap layer
    239  */
    240 int
    241 umapfs_unmount(mp, mntflags, l)
    242 	struct mount *mp;
    243 	int mntflags;
    244 	struct lwp *l;
    245 {
    246 	struct vnode *rtvp = MOUNTTOUMAPMOUNT(mp)->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 	/*
    258 	 * Clear out buffer cache.  I don't think we
    259 	 * ever get anything cached at this level at the
    260 	 * moment, but who knows...
    261 	 */
    262 #ifdef notyet
    263 	mntflushbuf(mp, 0);
    264 	if (mntinvalbuf(mp, 1))
    265 		return (EBUSY);
    266 #endif
    267 	if (rtvp->v_usecount > 1)
    268 		return (EBUSY);
    269 	if ((error = vflush(mp, rtvp, flags)) != 0)
    270 		return (error);
    271 
    272 #ifdef UMAPFS_DIAGNOSTIC
    273 	vprint("alias root of lower", rtvp);
    274 #endif
    275 	/*
    276 	 * Release reference on underlying root vnode
    277 	 */
    278 	vrele(rtvp);
    279 	/*
    280 	 * And blow it away for future re-use
    281 	 */
    282 	vgone(rtvp);
    283 	/*
    284 	 * Finally, throw away the umap_mount structure
    285 	 */
    286 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    287 	mp->mnt_data = 0;
    288 	return (0);
    289 }
    290 
    291 SYSCTL_SETUP(sysctl_vfs_umap_setup, "sysctl vfs.umap subtree setup")
    292 {
    293 
    294 	sysctl_createv(clog, 0, NULL, NULL,
    295 		       CTLFLAG_PERMANENT,
    296 		       CTLTYPE_NODE, "vfs", NULL,
    297 		       NULL, 0, NULL, 0,
    298 		       CTL_VFS, CTL_EOL);
    299 	sysctl_createv(clog, 0, NULL, NULL,
    300 		       CTLFLAG_PERMANENT,
    301 		       CTLTYPE_NODE, "umap",
    302 		       SYSCTL_DESCR("UID/GID remapping file system"),
    303 		       NULL, 0, NULL, 0,
    304 		       CTL_VFS, 10, CTL_EOL);
    305 	/*
    306 	 * XXX the "10" above could be dynamic, thereby eliminating
    307 	 * one more instance of the "number to vfs" mapping problem,
    308 	 * but "10" is the order as taken from sys/mount.h
    309 	 */
    310 }
    311 
    312 extern const struct vnodeopv_desc umapfs_vnodeop_opv_desc;
    313 
    314 const struct vnodeopv_desc * const umapfs_vnodeopv_descs[] = {
    315 	&umapfs_vnodeop_opv_desc,
    316 	NULL,
    317 };
    318 
    319 struct vfsops umapfs_vfsops = {
    320 	MOUNT_UMAP,
    321 	umapfs_mount,
    322 	layerfs_start,
    323 	umapfs_unmount,
    324 	layerfs_root,
    325 	layerfs_quotactl,
    326 	layerfs_statvfs,
    327 	layerfs_sync,
    328 	layerfs_vget,
    329 	layerfs_fhtovp,
    330 	layerfs_vptofh,
    331 	layerfs_init,
    332 	NULL,
    333 	layerfs_done,
    334 	NULL,				/* vfs_mountroot */
    335 	layerfs_snapshot,
    336 	vfs_stdextattrctl,
    337 	umapfs_vnodeopv_descs,
    338 	0,				/* vfs_refcount */
    339 	{ NULL, NULL },
    340 };
    341 VFS_ATTACH(umapfs_vfsops);
    342