Home | History | Annotate | Line # | Download | only in umapfs
umap_vfsops.c revision 1.20
      1 /*	$NetBSD: umap_vfsops.c,v 1.20 1999/02/26 23:44:46 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 
     58 int	umapfs_mount __P((struct mount *, const char *, void *,
     59 			  struct nameidata *, struct proc *));
     60 int	umapfs_start __P((struct mount *, int, struct proc *));
     61 int	umapfs_unmount __P((struct mount *, int, struct proc *));
     62 int	umapfs_root __P((struct mount *, struct vnode **));
     63 int	umapfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
     64 			     struct proc *));
     65 int	umapfs_statfs __P((struct mount *, struct statfs *, struct proc *));
     66 int	umapfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
     67 int	umapfs_vget __P((struct mount *, ino_t, struct vnode **));
     68 int	umapfs_fhtovp __P((struct mount *, struct fid *, struct vnode **));
     69 int	umapfs_checkexp __P((struct mount *, struct mbuf *, int *,
     70 			   struct ucred **));
     71 int	umapfs_vptofh __P((struct vnode *, struct fid *));
     72 int	umapfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
     73 			   struct proc *));
     74 
     75 /*
     76  * Mount umap layer
     77  */
     78 int
     79 umapfs_mount(mp, path, data, ndp, p)
     80 	struct mount *mp;
     81 	const char *path;
     82 	void *data;
     83 	struct nameidata *ndp;
     84 	struct proc *p;
     85 {
     86 	struct umap_args args;
     87 	struct vnode *lowerrootvp, *vp;
     88 	struct vnode *umapm_rootvp;
     89 	struct umap_mount *amp;
     90 	size_t size;
     91 	int error;
     92 
     93 #ifdef UMAPFS_DIAGNOSTIC
     94 	printf("umapfs_mount(mp = %p)\n", mp);
     95 #endif
     96 
     97 	/*
     98 	 * Update is a no-op
     99 	 */
    100 	if (mp->mnt_flag & MNT_UPDATE) {
    101 		return (EOPNOTSUPP);
    102 		/* return (VFS_MOUNT(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, path, data, ndp, p));*/
    103 	}
    104 
    105 	/*
    106 	 * Get argument
    107 	 */
    108 	error = copyin(data, (caddr_t)&args, sizeof(struct umap_args));
    109 	if (error)
    110 		return (error);
    111 
    112 	/*
    113 	 * Find lower node
    114 	 */
    115 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
    116 		UIO_USERSPACE, args.target, p);
    117 	if ((error = namei(ndp)) != 0)
    118 		return (error);
    119 
    120 	/*
    121 	 * Sanity check on lower vnode
    122 	 */
    123 	lowerrootvp = ndp->ni_vp;
    124 #ifdef UMAPFS_DIAGNOSTIC
    125 	printf("vp = %p, check for VDIR...\n", lowerrootvp);
    126 #endif
    127 	vrele(ndp->ni_dvp);
    128 	ndp->ni_dvp = 0;
    129 
    130 	if (lowerrootvp->v_type != VDIR) {
    131 		vput(lowerrootvp);
    132 		return (EINVAL);
    133 	}
    134 
    135 #ifdef UMAPFS_DIAGNOSTIC
    136 	printf("mp = %p\n", mp);
    137 #endif
    138 
    139 	amp = (struct umap_mount *) malloc(sizeof(struct umap_mount),
    140 				M_UFSMNT, M_WAITOK);	/* XXX */
    141 
    142 	/*
    143 	 * Save reference to underlying FS
    144 	 */
    145 	amp->umapm_vfs = lowerrootvp->v_mount;
    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 		return (error);
    156 
    157 #ifdef UMAP_DIAGNOSTIC
    158 	printf("umap_mount:nentries %d\n",args.nentries);
    159 	for (i = 0; i < args.nentries; i++)
    160 		printf("   %d maps to %d\n", amp->info_mapdata[i][0],
    161 	 	    amp->info_mapdata[i][1]);
    162 #endif
    163 
    164 	error = copyin(args.gmapdata, (caddr_t)amp->info_gmapdata,
    165 	    2*sizeof(u_long)*args.gnentries);
    166 	if (error)
    167 		return (error);
    168 
    169 #ifdef UMAP_DIAGNOSTIC
    170 	printf("umap_mount:gnentries %d\n",args.gnentries);
    171 	for (i = 0; i < args.gnentries; i++)
    172 		printf("\tgroup %d maps to %d\n",
    173 		    amp->info_gmapdata[i][0],
    174 	 	    amp->info_gmapdata[i][1]);
    175 #endif
    176 
    177 
    178 	/*
    179 	 * Save reference.  Each mount also holds
    180 	 * a reference on the root vnode.
    181 	 */
    182 	error = umap_node_create(mp, lowerrootvp, &vp);
    183 	/*
    184 	 * Unlock the node (either the lower or the alias)
    185 	 */
    186 	VOP_UNLOCK(vp, 0);
    187 	/*
    188 	 * Make sure the node alias worked
    189 	 */
    190 	if (error) {
    191 		vrele(lowerrootvp);
    192 		free(amp, M_UFSMNT);	/* XXX */
    193 		return (error);
    194 	}
    195 
    196 	/*
    197 	 * Keep a held reference to the root vnode.
    198 	 * It is vrele'd in umapfs_unmount.
    199 	 */
    200 	umapm_rootvp = vp;
    201 	umapm_rootvp->v_flag |= VROOT;
    202 	amp->umapm_rootvp = umapm_rootvp;
    203 	if (UMAPVPTOLOWERVP(umapm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
    204 		mp->mnt_flag |= MNT_LOCAL;
    205 	mp->mnt_data = (qaddr_t) amp;
    206 	vfs_getnewfsid(mp, MOUNT_UMAP);
    207 
    208 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
    209 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
    210 	(void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    211 	    &size);
    212 	memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
    213 #ifdef UMAPFS_DIAGNOSTIC
    214 	printf("umapfs_mount: lower %s, alias at %s\n",
    215 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    216 #endif
    217 	return (0);
    218 }
    219 
    220 /*
    221  * VFS start.  Nothing needed here - the start routine
    222  * on the underlying filesystem will have been called
    223  * when that filesystem was mounted.
    224  */
    225 int
    226 umapfs_start(mp, flags, p)
    227 	struct mount *mp;
    228 	int flags;
    229 	struct proc *p;
    230 {
    231 
    232 	return (0);
    233 	/* return (VFS_START(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, flags, p)); */
    234 }
    235 
    236 /*
    237  * Free reference to umap layer
    238  */
    239 int
    240 umapfs_unmount(mp, mntflags, p)
    241 	struct mount *mp;
    242 	int mntflags;
    243 	struct proc *p;
    244 {
    245 	struct vnode *umapm_rootvp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
    246 	int error;
    247 	int flags = 0;
    248 
    249 #ifdef UMAPFS_DIAGNOSTIC
    250 	printf("umapfs_unmount(mp = %p)\n", mp);
    251 #endif
    252 
    253 	if (mntflags & MNT_FORCE)
    254 		flags |= FORCECLOSE;
    255 
    256 	/*
    257 	 * Clear out buffer cache.  I don't think we
    258 	 * ever get anything cached at this level at the
    259 	 * moment, but who knows...
    260 	 */
    261 #ifdef notyet
    262 	mntflushbuf(mp, 0);
    263 	if (mntinvalbuf(mp, 1))
    264 		return (EBUSY);
    265 #endif
    266 	if (umapm_rootvp->v_usecount > 1)
    267 		return (EBUSY);
    268 	if ((error = vflush(mp, umapm_rootvp, flags)) != 0)
    269 		return (error);
    270 
    271 #ifdef UMAPFS_DIAGNOSTIC
    272 	vprint("alias root of lower", umapm_rootvp);
    273 #endif
    274 	/*
    275 	 * Release reference on underlying root vnode
    276 	 */
    277 	vrele(umapm_rootvp);
    278 	/*
    279 	 * And blow it away for future re-use
    280 	 */
    281 	vgone(umapm_rootvp);
    282 	/*
    283 	 * Finally, throw away the umap_mount structure
    284 	 */
    285 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    286 	mp->mnt_data = 0;
    287 	return (0);
    288 }
    289 
    290 int
    291 umapfs_root(mp, vpp)
    292 	struct mount *mp;
    293 	struct vnode **vpp;
    294 {
    295 	struct vnode *vp;
    296 
    297 #ifdef UMAPFS_DIAGNOSTIC
    298 	printf("umapfs_root(mp = %p, vp = %p->%p)\n", mp,
    299 	    MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
    300 	    UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp));
    301 #endif
    302 
    303 	/*
    304 	 * Return locked reference to root.
    305 	 */
    306 	vp = MOUNTTOUMAPMOUNT(mp)->umapm_rootvp;
    307 	VREF(vp);
    308 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    309 	*vpp = vp;
    310 	return (0);
    311 }
    312 
    313 int
    314 umapfs_quotactl(mp, cmd, uid, arg, p)
    315 	struct mount *mp;
    316 	int cmd;
    317 	uid_t uid;
    318 	caddr_t arg;
    319 	struct proc *p;
    320 {
    321 
    322 	return (VFS_QUOTACTL(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, cmd, uid, arg, p));
    323 }
    324 
    325 int
    326 umapfs_statfs(mp, sbp, p)
    327 	struct mount *mp;
    328 	struct statfs *sbp;
    329 	struct proc *p;
    330 {
    331 	int error;
    332 	struct statfs mstat;
    333 
    334 #ifdef UMAPFS_DIAGNOSTIC
    335 	printf("umapfs_statfs(mp = %p, vp = %p->%p)\n", mp,
    336 	    MOUNTTOUMAPMOUNT(mp)->umapm_rootvp,
    337 	    UMAPVPTOLOWERVP(MOUNTTOUMAPMOUNT(mp)->umapm_rootvp));
    338 #endif
    339 
    340 	memset(&mstat, 0, sizeof(mstat));
    341 
    342 	error = VFS_STATFS(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, &mstat, p);
    343 	if (error)
    344 		return (error);
    345 
    346 	/* now copy across the "interesting" information and fake the rest */
    347 	sbp->f_type = mstat.f_type;
    348 	sbp->f_flags = mstat.f_flags;
    349 	sbp->f_bsize = mstat.f_bsize;
    350 	sbp->f_iosize = mstat.f_iosize;
    351 	sbp->f_blocks = mstat.f_blocks;
    352 	sbp->f_bfree = mstat.f_bfree;
    353 	sbp->f_bavail = mstat.f_bavail;
    354 	sbp->f_files = mstat.f_files;
    355 	sbp->f_ffree = mstat.f_ffree;
    356 	if (sbp != &mp->mnt_stat) {
    357 		memcpy(&sbp->f_fsid, &mp->mnt_stat.f_fsid, sizeof(sbp->f_fsid));
    358 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
    359 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
    360 	}
    361 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
    362 	return (0);
    363 }
    364 
    365 int
    366 umapfs_sync(mp, waitfor, cred, p)
    367 	struct mount *mp;
    368 	int waitfor;
    369 	struct ucred *cred;
    370 	struct proc *p;
    371 {
    372 
    373 	/*
    374 	 * XXX - Assumes no data cached at umap layer.
    375 	 */
    376 	return (0);
    377 }
    378 
    379 int
    380 umapfs_vget(mp, ino, vpp)
    381 	struct mount *mp;
    382 	ino_t ino;
    383 	struct vnode **vpp;
    384 {
    385 
    386 	return (VFS_VGET(MOUNTTOUMAPMOUNT(mp)->umapm_vfs, ino, vpp));
    387 }
    388 
    389 int
    390 umapfs_fhtovp(mp, fidp, vpp)
    391 	struct mount *mp;
    392 	struct fid *fidp;
    393 	struct vnode **vpp;
    394 {
    395 
    396 	return (EOPNOTSUPP);
    397 }
    398 
    399 int
    400 umapfs_checkexp(mp, nam, exflagsp, credanonp)
    401 	struct mount *mp;
    402 	struct mbuf *nam;
    403 	int *exflagsp;
    404 	struct ucred**credanonp;
    405 {
    406 
    407 	return (EOPNOTSUPP);
    408 }
    409 
    410 int
    411 umapfs_vptofh(vp, fhp)
    412 	struct vnode *vp;
    413 	struct fid *fhp;
    414 {
    415 
    416 	return (EOPNOTSUPP);
    417 }
    418 
    419 int
    420 umapfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    421 	int *name;
    422 	u_int namelen;
    423 	void *oldp;
    424 	size_t *oldlenp;
    425 	void *newp;
    426 	size_t newlen;
    427 	struct proc *p;
    428 {
    429 	return (EOPNOTSUPP);
    430 }
    431 
    432 extern struct vnodeopv_desc umapfs_vnodeop_opv_desc;
    433 
    434 struct vnodeopv_desc *umapfs_vnodeopv_descs[] = {
    435 	&umapfs_vnodeop_opv_desc,
    436 	NULL,
    437 };
    438 
    439 struct vfsops umapfs_vfsops = {
    440 	MOUNT_UMAP,
    441 	umapfs_mount,
    442 	umapfs_start,
    443 	umapfs_unmount,
    444 	umapfs_root,
    445 	umapfs_quotactl,
    446 	umapfs_statfs,
    447 	umapfs_sync,
    448 	umapfs_vget,
    449 	umapfs_fhtovp,
    450 	umapfs_vptofh,
    451 	umapfs_init,
    452 	umapfs_sysctl,
    453 	NULL,				/* vfs_mountroot */
    454 	umapfs_checkexp,
    455 	umapfs_vnodeopv_descs,
    456 };
    457