Home | History | Annotate | Line # | Download | only in nullfs
null_vfsops.c revision 1.1.1.2
      1 /*
      2  * Copyright (c) 1992, 1993, 1995
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * This code is derived from software donated to Berkeley by
      6  * Jan-Simon Pendry.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)null_vfsops.c	8.7 (Berkeley) 5/14/95
     37  *
     38  * @(#)lofs_vfsops.c	1.2 (Berkeley) 6/18/92
     39  * $Id: null_vfsops.c,v 1.1.1.2 1998/03/01 02:13:14 fvdl Exp $
     40  */
     41 
     42 /*
     43  * Null Layer
     44  * (See null_vnops.c for a description of what this does.)
     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/nullfs/null.h>
     57 
     58 /*
     59  * Mount null layer
     60  */
     61 int
     62 nullfs_mount(mp, path, data, ndp, p)
     63 	struct mount *mp;
     64 	char *path;
     65 	caddr_t data;
     66 	struct nameidata *ndp;
     67 	struct proc *p;
     68 {
     69 	int error = 0;
     70 	struct null_args args;
     71 	struct vnode *lowerrootvp, *vp;
     72 	struct vnode *nullm_rootvp;
     73 	struct null_mount *xmp;
     74 	u_int size;
     75 
     76 #ifdef NULLFS_DIAGNOSTIC
     77 	printf("nullfs_mount(mp = %x)\n", mp);
     78 #endif
     79 
     80 	/*
     81 	 * Update is a no-op
     82 	 */
     83 	if (mp->mnt_flag & MNT_UPDATE) {
     84 		return (EOPNOTSUPP);
     85 		/* return VFS_MOUNT(MOUNTTONULLMOUNT(mp)->nullm_vfs, path, data, ndp, p);*/
     86 	}
     87 
     88 	/*
     89 	 * Get argument
     90 	 */
     91 	if (error = copyin(data, (caddr_t)&args, sizeof(struct null_args)))
     92 		return (error);
     93 
     94 	/*
     95 	 * Find lower node
     96 	 */
     97 	NDINIT(ndp, LOOKUP, FOLLOW|WANTPARENT|LOCKLEAF,
     98 		UIO_USERSPACE, args.target, p);
     99 	if (error = namei(ndp))
    100 		return (error);
    101 
    102 	/*
    103 	 * Sanity check on lower vnode
    104 	 */
    105 	lowerrootvp = ndp->ni_vp;
    106 
    107 	vrele(ndp->ni_dvp);
    108 	ndp->ni_dvp = NULL;
    109 
    110 	xmp = (struct null_mount *) malloc(sizeof(struct null_mount),
    111 				M_UFSMNT, M_WAITOK);	/* XXX */
    112 
    113 	/*
    114 	 * Save reference to underlying FS
    115 	 */
    116 	xmp->nullm_vfs = lowerrootvp->v_mount;
    117 
    118 	/*
    119 	 * Save reference.  Each mount also holds
    120 	 * a reference on the root vnode.
    121 	 */
    122 	error = null_node_create(mp, lowerrootvp, &vp);
    123 	/*
    124 	 * Unlock the node (either the lower or the alias)
    125 	 */
    126 	VOP_UNLOCK(vp, 0, p);
    127 	/*
    128 	 * Make sure the node alias worked
    129 	 */
    130 	if (error) {
    131 		vrele(lowerrootvp);
    132 		free(xmp, M_UFSMNT);	/* XXX */
    133 		return (error);
    134 	}
    135 
    136 	/*
    137 	 * Keep a held reference to the root vnode.
    138 	 * It is vrele'd in nullfs_unmount.
    139 	 */
    140 	nullm_rootvp = vp;
    141 	nullm_rootvp->v_flag |= VROOT;
    142 	xmp->nullm_rootvp = nullm_rootvp;
    143 	if (NULLVPTOLOWERVP(nullm_rootvp)->v_mount->mnt_flag & MNT_LOCAL)
    144 		mp->mnt_flag |= MNT_LOCAL;
    145 	mp->mnt_data = (qaddr_t) xmp;
    146 	vfs_getnewfsid(mp);
    147 
    148 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
    149 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
    150 	(void) copyinstr(args.target, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
    151 	    &size);
    152 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
    153 #ifdef NULLFS_DIAGNOSTIC
    154 	printf("nullfs_mount: lower %s, alias at %s\n",
    155 		mp->mnt_stat.f_mntfromname, mp->mnt_stat.f_mntonname);
    156 #endif
    157 	return (0);
    158 }
    159 
    160 /*
    161  * VFS start.  Nothing needed here - the start routine
    162  * on the underlying filesystem will have been called
    163  * when that filesystem was mounted.
    164  */
    165 int
    166 nullfs_start(mp, flags, p)
    167 	struct mount *mp;
    168 	int flags;
    169 	struct proc *p;
    170 {
    171 	return (0);
    172 	/* return VFS_START(MOUNTTONULLMOUNT(mp)->nullm_vfs, flags, p); */
    173 }
    174 
    175 /*
    176  * Free reference to null layer
    177  */
    178 int
    179 nullfs_unmount(mp, mntflags, p)
    180 	struct mount *mp;
    181 	int mntflags;
    182 	struct proc *p;
    183 {
    184 	struct vnode *nullm_rootvp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
    185 	int error;
    186 	int flags = 0;
    187 
    188 #ifdef NULLFS_DIAGNOSTIC
    189 	printf("nullfs_unmount(mp = %x)\n", mp);
    190 #endif
    191 
    192 	if (mntflags & MNT_FORCE)
    193 		flags |= FORCECLOSE;
    194 
    195 	/*
    196 	 * Clear out buffer cache.  I don't think we
    197 	 * ever get anything cached at this level at the
    198 	 * moment, but who knows...
    199 	 */
    200 #if 0
    201 	mntflushbuf(mp, 0);
    202 	if (mntinvalbuf(mp, 1))
    203 		return (EBUSY);
    204 #endif
    205 	if (nullm_rootvp->v_usecount > 1)
    206 		return (EBUSY);
    207 	if (error = vflush(mp, nullm_rootvp, flags))
    208 		return (error);
    209 
    210 #ifdef NULLFS_DIAGNOSTIC
    211 	vprint("alias root of lower", nullm_rootvp);
    212 #endif
    213 	/*
    214 	 * Release reference on underlying root vnode
    215 	 */
    216 	vrele(nullm_rootvp);
    217 	/*
    218 	 * And blow it away for future re-use
    219 	 */
    220 	vgone(nullm_rootvp);
    221 	/*
    222 	 * Finally, throw away the null_mount structure
    223 	 */
    224 	free(mp->mnt_data, M_UFSMNT);	/* XXX */
    225 	mp->mnt_data = 0;
    226 	return 0;
    227 }
    228 
    229 int
    230 nullfs_root(mp, vpp)
    231 	struct mount *mp;
    232 	struct vnode **vpp;
    233 {
    234 	struct proc *p = curproc;	/* XXX */
    235 	struct vnode *vp;
    236 
    237 #ifdef NULLFS_DIAGNOSTIC
    238 	printf("nullfs_root(mp = %x, vp = %x->%x)\n", mp,
    239 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
    240 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
    241 			);
    242 #endif
    243 
    244 	/*
    245 	 * Return locked reference to root.
    246 	 */
    247 	vp = MOUNTTONULLMOUNT(mp)->nullm_rootvp;
    248 	VREF(vp);
    249 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
    250 	*vpp = vp;
    251 	return 0;
    252 }
    253 
    254 int
    255 nullfs_quotactl(mp, cmd, uid, arg, p)
    256 	struct mount *mp;
    257 	int cmd;
    258 	uid_t uid;
    259 	caddr_t arg;
    260 	struct proc *p;
    261 {
    262 	return VFS_QUOTACTL(MOUNTTONULLMOUNT(mp)->nullm_vfs, cmd, uid, arg, p);
    263 }
    264 
    265 int
    266 nullfs_statfs(mp, sbp, p)
    267 	struct mount *mp;
    268 	struct statfs *sbp;
    269 	struct proc *p;
    270 {
    271 	int error;
    272 	struct statfs mstat;
    273 
    274 #ifdef NULLFS_DIAGNOSTIC
    275 	printf("nullfs_statfs(mp = %x, vp = %x->%x)\n", mp,
    276 			MOUNTTONULLMOUNT(mp)->nullm_rootvp,
    277 			NULLVPTOLOWERVP(MOUNTTONULLMOUNT(mp)->nullm_rootvp)
    278 			);
    279 #endif
    280 
    281 	bzero(&mstat, sizeof(mstat));
    282 
    283 	error = VFS_STATFS(MOUNTTONULLMOUNT(mp)->nullm_vfs, &mstat, p);
    284 	if (error)
    285 		return (error);
    286 
    287 	/* now copy across the "interesting" information and fake the rest */
    288 	sbp->f_type = mstat.f_type;
    289 	sbp->f_flags = mstat.f_flags;
    290 	sbp->f_bsize = mstat.f_bsize;
    291 	sbp->f_iosize = mstat.f_iosize;
    292 	sbp->f_blocks = mstat.f_blocks;
    293 	sbp->f_bfree = mstat.f_bfree;
    294 	sbp->f_bavail = mstat.f_bavail;
    295 	sbp->f_files = mstat.f_files;
    296 	sbp->f_ffree = mstat.f_ffree;
    297 	if (sbp != &mp->mnt_stat) {
    298 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
    299 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
    300 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
    301 	}
    302 	return (0);
    303 }
    304 
    305 int
    306 nullfs_sync(mp, waitfor, cred, p)
    307 	struct mount *mp;
    308 	int waitfor;
    309 	struct ucred *cred;
    310 	struct proc *p;
    311 {
    312 	/*
    313 	 * XXX - Assumes no data cached at null layer.
    314 	 */
    315 	return (0);
    316 }
    317 
    318 int
    319 nullfs_vget(mp, ino, vpp)
    320 	struct mount *mp;
    321 	ino_t ino;
    322 	struct vnode **vpp;
    323 {
    324 
    325 	return VFS_VGET(MOUNTTONULLMOUNT(mp)->nullm_vfs, ino, vpp);
    326 }
    327 
    328 int
    329 nullfs_fhtovp(mp, fidp, nam, vpp, exflagsp, credanonp)
    330 	struct mount *mp;
    331 	struct fid *fidp;
    332 	struct mbuf *nam;
    333 	struct vnode **vpp;
    334 	int *exflagsp;
    335 	struct ucred**credanonp;
    336 {
    337 
    338 	return VFS_FHTOVP(MOUNTTONULLMOUNT(mp)->nullm_vfs, fidp, nam, vpp, exflagsp,credanonp);
    339 }
    340 
    341 int
    342 nullfs_vptofh(vp, fhp)
    343 	struct vnode *vp;
    344 	struct fid *fhp;
    345 {
    346 	return VFS_VPTOFH(NULLVPTOLOWERVP(vp), fhp);
    347 }
    348 
    349 int nullfs_init __P((struct vfsconf *));
    350 
    351 #define nullfs_sysctl ((int (*) __P((int *, u_int, void *, size_t *, void *, \
    352 	    size_t, struct proc *)))eopnotsupp)
    353 
    354 struct vfsops null_vfsops = {
    355 	nullfs_mount,
    356 	nullfs_start,
    357 	nullfs_unmount,
    358 	nullfs_root,
    359 	nullfs_quotactl,
    360 	nullfs_statfs,
    361 	nullfs_sync,
    362 	nullfs_vget,
    363 	nullfs_fhtovp,
    364 	nullfs_vptofh,
    365 	nullfs_init,
    366 	nullfs_sysctl,
    367 };
    368