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