Home | History | Annotate | Line # | Download | only in umapfs
umap_vfsops.c revision 1.23.8.1
      1 /*	$NetBSD: umap_vfsops.c,v 1.23.8.1 1999/12/21 23:20:01 wrstuden 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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  *	from: @(#)null_vfsops.c       1.5 (Berkeley) 7/10/92
     39  *	@(#)umap_vfsops.c	8.8 (Berkeley) 5/14/95
     40  */
     41 
     42 /*
     43  * Umap Layer
     44  * (See mount_umap(8) for a description of this layer.)
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/proc.h>
     50 #include <sys/time.h>
     51 #include <sys/types.h>
     52 #include <sys/vnode.h>
     53 #include <sys/mount.h>
     54 #include <sys/namei.h>
     55 #include <sys/malloc.h>
     56 #include <miscfs/umapfs/umap.h>
     57 #include <miscfs/genfs/layer_extern.h>
     58 
     59 int	umapfs_mount __P((struct mount *, const char *, void *,
     60 			  struct nameidata *, struct proc *));
     61 int	umapfs_unmount __P((struct mount *, int, struct proc *));
     62 
     63 /*
     64  * Mount umap layer
     65  */
     66 int
     67 umapfs_mount(mp, path, data, ndp, p)
     68 	struct mount *mp;
     69 	const char *path;
     70 	void *data;
     71 	struct nameidata *ndp;
     72 	struct proc *p;
     73 {
     74 	struct umap_args args;
     75 	struct vnode *lowerrootvp, *vp;
     76 	struct umap_mount *amp;
     77 	size_t size;
     78 	int error;
     79 #ifdef UMAPFS_DIAGNOSTIC
     80 	int i;
     81 #endif
     82 
     83 	/* only for root */
     84 	if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
     85 		return error;
     86 
     87 #ifdef UMAPFS_DIAGNOSTIC
     88 	printf("umapfs_mount(mp = %p)\n", mp);
     89 #endif
     90 
     91 	/*
     92 	 * Get argument
     93 	 */
     94 	error = copyin(data, (caddr_t)&args, sizeof(struct umap_args));
     95 	if (error)
     96 		return (error);
     97 
     98 	/*
     99 	 * Update only does export updating.
    100 	 */
    101 	if (mp->mnt_flag & MNT_UPDATE) {
    102 		amp = MOUNTTOUMAPMOUNT(mp);
    103 		if (args.umap_target == 0)
    104 			return (vfs_export(mp, &amp->umapm_export,
    105 					&args.umap_export));
    106 		else
    107 			return (EOPNOTSUPP);
    108 	}
    109 
    110 	/*
    111 	 * Find lower node
    112 	 */
    113 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
    114 		UIO_USERSPACE, args.umap_target, p);
    115 	if ((error = namei(ndp)) != 0)
    116 		return (error);
    117 
    118 	/*
    119 	 * Sanity check on lower vnode
    120 	 */
    121 	lowerrootvp = ndp->ni_vp;
    122 #ifdef UMAPFS_DIAGNOSTIC
    123 	printf("vp = %p, check for VDIR...\n", lowerrootvp);
    124 #endif
    125 	vrele(ndp->ni_dvp);
    126 	ndp->ni_dvp = 0;
    127 
    128 	if (lowerrootvp->v_type != VDIR) {
    129 		vput(lowerrootvp);
    130 		return (EINVAL);
    131 	}
    132 
    133 #ifdef UMAPFS_DIAGNOSTIC
    134 	printf("mp = %p\n", mp);
    135 #endif
    136 
    137 	amp = (struct umap_mount *) malloc(sizeof(struct umap_mount),
    138 				M_UFSMNT, M_WAITOK);	/* XXX */
    139 	memset((caddr_t)amp, 0, sizeof(struct umap_mount));
    140 
    141 	mp->mnt_data = (qaddr_t) amp;
    142 	amp->umapm_vfs = lowerrootvp->v_mount;
    143 	if (amp->umapm_vfs->mnt_flag & MNT_LOCAL)
    144 		mp->mnt_flag |= MNT_LOCAL;
    145 	mp->mnt_bshift = amp->umapm_vfs->mnt_bshift;
    146 
    147 	/*
    148 	 * Now copy in the number of entries and maps for umap mapping.
    149 	 */
    150 	amp->info_nentries = args.nentries;
    151 	amp->info_gnentries = args.gnentries;
    152 	error = copyin(args.mapdata, (caddr_t)amp->info_mapdata,
    153 	    2*sizeof(u_long)*args.nentries);
    154 	if (error) {
    155 		vput(lowerrootvp);
    156 		return (error);
    157 	}
    158 
    159 #ifdef UMAPFS_DIAGNOSTIC
    160 	printf("umap_mount:nentries %d\n",args.nentries);
    161 	for (i = 0; i < args.nentries; i++)
    162 		printf("   %ld maps to %ld\n", amp->info_mapdata[i][0],
    163 	 	    amp->info_mapdata[i][1]);
    164 #endif
    165 
    166 	error = copyin(args.gmapdata, (caddr_t)amp->info_gmapdata,
    167 	    2*sizeof(u_long)*args.gnentries);
    168 	if (error) {
    169 		vput(lowerrootvp);
    170 		return (error);
    171 	}
    172 
    173 #ifdef UMAPFS_DIAGNOSTIC
    174 	printf("umap_mount:gnentries %d\n",args.gnentries);
    175 	for (i = 0; i < args.gnentries; i++)
    176 		printf("\tgroup %ld maps to %ld\n",
    177 		    amp->info_gmapdata[i][0],
    178 	 	    amp->info_gmapdata[i][1]);
    179 #endif
    180 
    181 	/*
    182 	 * Make sure the mount point's sufficiently initialized
    183 	 * that the node create call will work.
    184 	 */
    185 	vfs_getnewfsid(mp, MOUNT_UMAP);
    186 	amp->umapm_size = sizeof(struct umap_node);
    187 	amp->umapm_tag = VT_UMAP;
    188 	amp->umapm_bypass = umap_bypass;
    189 	amp->umapm_alloc = layer_node_alloc;	/* the default alloc is fine */
    190 	amp->umapm_vnodeop_p = umap_vnodeop_p;
    191 	simple_lock_init(&amp->umapm_hashlock);
    192 	amp->umapm_node_hashtbl = hashinit(NUMAPNODECACHE, M_CACHE, M_WAITOK,
    193 			&amp->umapm_node_hash);
    194 
    195 
    196 	/*
    197 	 * fix up umap node for root vnode.
    198 	 */
    199 	error = layer_node_create(mp, lowerrootvp, &vp);
    200 	/*
    201 	 * Make sure the node alias worked
    202 	 */
    203 	if (error) {
    204 		vput(lowerrootvp);
    205 		free(amp, M_UFSMNT);	/* XXX */
    206 		return (error);
    207 	}
    208 	/*
    209 	 * Unlock the node (either the lower or the alias)
    210 	 */
    211 	VOP_UNLOCK(vp, 0);
    212 
    213 	/*
    214 	 * Keep a held reference to the root vnode.
    215 	 * It is vrele'd in umapfs_unmount.
    216 	 */
    217 	vp->v_flag |= VROOT;
    218 	amp->umapm_rootvp = vp;
    219 
    220 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
    221 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
    222 	(void) copyinstr(args.umap_target, mp->mnt_stat.f_mntfromname,
    223 		MNAMELEN - 1, &size);
    224 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
    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 (0);
    230 }
    231 
    232 /*
    233  * Free reference to umap layer
    234  */
    235 int
    236 umapfs_unmount(mp, mntflags, p)
    237 	struct mount *mp;
    238 	int mntflags;
    239 	struct proc *p;
    240 {
    241 	struct vnode *rootvp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
    242 	int error;
    243 	int flags = 0;
    244 
    245 #ifdef UMAPFS_DIAGNOSTIC
    246 	printf("umapfs_unmount(mp = %p)\n", mp);
    247 #endif
    248 
    249 	if (mntflags & MNT_FORCE)
    250 		flags |= FORCECLOSE;
    251 
    252 	/*
    253 	 * Clear out buffer cache.  I don't think we
    254 	 * ever get anything cached at this level at the
    255 	 * moment, but who knows...
    256 	 */
    257 #ifdef notyet
    258 	mntflushbuf(mp, 0);
    259 	if (mntinvalbuf(mp, 1))
    260 		return (EBUSY);
    261 #endif
    262 	if (rootvp->v_usecount > 1)
    263 		return (EBUSY);
    264 	if ((error = vflush(mp, rootvp, flags)) != 0)
    265 		return (error);
    266 
    267 #ifdef UMAPFS_DIAGNOSTIC
    268 	vprint("alias root of lower", rootvp);
    269 #endif
    270 	/*
    271 	 * Release reference on underlying root vnode
    272 	 */
    273 	vrele(rootvp);
    274 	/*
    275 	 * And blow it away for future re-use
    276 	 */
    277 	vgone(rootvp);
    278 	/*
    279 	 * Finally, throw away the umap_mount structure
    280 	 */
    281 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    282 	mp->mnt_data = 0;
    283 	return (0);
    284 }
    285 
    286 extern struct vnodeopv_desc umapfs_vnodeop_opv_desc;
    287 
    288 struct vnodeopv_desc *umapfs_vnodeopv_descs[] = {
    289 	&umapfs_vnodeop_opv_desc,
    290 	NULL,
    291 };
    292 
    293 struct vfsops umapfs_vfsops = {
    294 	MOUNT_UMAP,
    295 	umapfs_mount,
    296 	layerfs_start,
    297 	umapfs_unmount,
    298 	layerfs_root,
    299 	layerfs_quotactl,
    300 	layerfs_statfs,
    301 	layerfs_sync,
    302 	layerfs_vget,
    303 	layerfs_fhtovp,
    304 	layerfs_vptofh,
    305 	layerfs_init,
    306 	layerfs_sysctl,
    307 	NULL,				/* vfs_mountroot */
    308 	layerfs_checkexp,
    309 	umapfs_vnodeopv_descs,
    310 };
    311