Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vfsops.c revision 1.8.2.3
      1 /*
      2  * Copyright (c) 1990, 1992 Jan-Simon Pendry
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed 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  *	$Id: kernfs_vfsops.c,v 1.8.2.3 1993/12/03 21:09:25 cgd Exp $
     37  */
     38 
     39 /*
     40  * Kernel params Filesystem
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/time.h>
     46 #include <sys/types.h>
     47 #include <sys/proc.h>
     48 #include <sys/vnode.h>
     49 #include <sys/mount.h>
     50 #include <sys/namei.h>
     51 #include <sys/malloc.h>
     52 #include <sys/conf.h>
     53 #include <miscfs/kernfs/kernfs.h>
     54 
     55 /* bring in the spec vnodeops for cdevvp */
     56 extern struct vnodeops spec_vnodeops;
     57 
     58 struct vnode *rrootdevvp;
     59 
     60 kernfs_init()
     61 {
     62 #ifdef KERNFS_DIAGNOSTIC
     63 	printf("kernfs_init\n");	/* printed during system boot */
     64 #endif
     65 
     66 	/* DO NOTHING */
     67 }
     68 
     69 kernfs_rrootdevvp_init()
     70 {
     71 	int error, bmaj, cmaj;
     72 
     73 	if (rrootdevvp != NULL)		/* then we've already done this */
     74 		return;
     75 
     76 	error = ENXIO;
     77 	bmaj = major(rootdev);
     78 
     79 	/* hunt for the raw root device by looking in cdevsw for a matching
     80 	 * open routine...
     81 	 */
     82 	for (cmaj = 0; cmaj < nchrdev; cmaj++) {
     83 		if (cdevsw[cmaj].d_open == bdevsw[bmaj].d_open) {
     84 			dev_t cdev = makedev(cmaj, minor(rootdev));
     85 			error = cdevvp(cdev, &rrootdevvp);
     86 			if (!error)
     87 				return;
     88 		}
     89 	}
     90 
     91 	/* this isn't fatal... */
     92 	if (error) {
     93 		printf("kernfs: no raw root device\n");
     94 		rrootdevvp = NULL;
     95 	}
     96 }
     97 
     98 /*
     99  * Mount the kernel parameter filesystem
    100  */
    101 kernfs_mount(mp, path, data, ndp, p)
    102 	struct mount *mp;
    103 	char *path;
    104 	caddr_t data;
    105 	struct nameidata *ndp;
    106 	struct proc *p;
    107 {
    108 	int error = 0;
    109 	u_int size;
    110 	struct kernfs_mount *fmp;
    111 	struct vnode *rvp;
    112 
    113 #ifdef KERNFS_DIAGNOSTIC
    114 	printf("kernfs_mount(mp = %x)\n", mp);
    115 #endif
    116 
    117 	/*
    118 	 * Update is a no-op
    119 	 */
    120 	if (mp->mnt_flag & MNT_UPDATE)
    121 		return (EOPNOTSUPP);
    122 
    123 	error = getnewvnode(VT_KERNFS, mp, &kernfs_vnodeops, &rvp);
    124 	if (error)
    125 		return (error);
    126 
    127 	fmp = (struct kernfs_mount *) malloc(sizeof(struct kernfs_mount),
    128 				 M_MISCFSMNT, M_WAITOK);
    129 	rvp->v_type = VDIR;
    130 	rvp->v_flag |= VROOT;
    131 	VTOKERN(rvp)->kf_kt = &kernfs_targets[KERNFS_TARGET_ROOT];
    132 #ifdef KERNFS_DIAGNOSTIC
    133 	printf("kernfs_mount: root vp = %x\n", rvp);
    134 #endif
    135 	fmp->kf_root = rvp;
    136 	mp->mnt_flag |= MNT_LOCAL;
    137 	mp->mnt_data = (qaddr_t) fmp;
    138 	getnewfsid(mp, MOUNT_KERNFS);
    139 
    140 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
    141 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
    142 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
    143 	bcopy("kernfs", mp->mnt_stat.f_mntfromname, sizeof("kernfs"));
    144 #ifdef KERNFS_DIAGNOSTIC
    145 	printf("kernfs_mount: at %s\n", mp->mnt_stat.f_mntonname);
    146 #endif
    147 
    148 	kernfs_rrootdevvp_init();
    149 
    150 	return (0);
    151 }
    152 
    153 kernfs_start(mp, flags, p)
    154 	struct mount *mp;
    155 	int flags;
    156 	struct proc *p;
    157 {
    158 	return (0);
    159 }
    160 
    161 kernfs_unmount(mp, mntflags, p)
    162 	struct mount *mp;
    163 	int mntflags;
    164 	struct proc *p;
    165 {
    166 	int error;
    167 	int flags = 0;
    168 	extern int doforce;
    169 	struct vnode *rootvp = VFSTOKERNFS(mp)->kf_root;
    170 
    171 #ifdef KERNFS_DIAGNOSTIC
    172 	printf("kernfs_unmount(mp = %x)\n", mp);
    173 #endif
    174 
    175 	if (mntflags & MNT_FORCE) {
    176 		/* kernfs can never be rootfs so don't check for it */
    177 		if (!doforce)
    178 			return (EINVAL);
    179 		flags |= FORCECLOSE;
    180 	}
    181 
    182 	/*
    183 	 * Clear out buffer cache.  I don't think we
    184 	 * ever get anything cached at this level at the
    185 	 * moment, but who knows...
    186 	 */
    187 #ifdef KERNFS_DIAGNOSTIC
    188 	printf("kernfs_unmount: calling mntflushbuf\n");
    189 #endif
    190 	mntflushbuf(mp, 0);
    191 #ifdef KERNFS_DIAGNOSTIC
    192 	printf("kernfs_unmount: calling mntinvalbuf\n");
    193 #endif
    194 	if (mntinvalbuf(mp, 1))
    195 		return (EBUSY);
    196 	if (rootvp->v_usecount > 1)
    197 		return (EBUSY);
    198 #ifdef KERNFS_DIAGNOSTIC
    199 	printf("kernfs_unmount: calling vflush\n");
    200 #endif
    201 	if (error = vflush(mp, rootvp, flags))
    202 		return (error);
    203 
    204 #ifdef KERNFS_DIAGNOSTIC
    205 	vprint("kernfs root", rootvp);
    206 #endif
    207 	/*
    208 	 * Release reference on underlying root vnode
    209 	 */
    210 	vrele(rootvp);
    211 	/*
    212 	 * And blow it away for future re-use
    213 	 */
    214 	vgone(rootvp);
    215 	/*
    216 	 * Finally, throw away the kernfs_mount structure
    217 	 */
    218 	free(mp->mnt_data, M_MISCFSMNT);
    219 	mp->mnt_data = 0;
    220 	return 0;
    221 }
    222 
    223 kernfs_root(mp, vpp)
    224 	struct mount *mp;
    225 	struct vnode **vpp;
    226 {
    227 	struct vnode *vp;
    228 	int error;
    229 
    230 #ifdef KERNFS_DIAGNOSTIC
    231 	printf("kernfs_root(mp = %x)\n", mp);
    232 #endif
    233 
    234 	/*
    235 	 * Return locked reference to root.
    236 	 */
    237 	vp = VFSTOKERNFS(mp)->kf_root;
    238 	VREF(vp);
    239 	VOP_LOCK(vp);
    240 	*vpp = vp;
    241 	return (0);
    242 }
    243 
    244 kernfs_quotactl(mp, cmd, uid, arg, p)
    245 	struct mount *mp;
    246 	int cmd;
    247 	uid_t uid;
    248 	caddr_t arg;
    249 	struct proc *p;
    250 {
    251 	return (EOPNOTSUPP);
    252 }
    253 
    254 kernfs_statfs(mp, sbp, p)
    255 	struct mount *mp;
    256 	struct statfs *sbp;
    257 	struct proc *p;
    258 {
    259 	struct filedesc *fdp;
    260 	int lim;
    261 	int i;
    262 	int last;
    263 	int freefd;
    264 
    265 #ifdef KERNFS_DIAGNOSTIC
    266 	printf("kernfs_statfs(mp = %x)\n", mp);
    267 #endif
    268 
    269 	sbp->f_type = MOUNT_KERNFS;
    270 	sbp->f_flags = 0;
    271 	sbp->f_fsize = DEV_BSIZE;
    272 	sbp->f_bsize = DEV_BSIZE;
    273 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    274 	sbp->f_bfree = 0;
    275 	sbp->f_bavail = 0;
    276 	sbp->f_files = 0;		/* Allow for "." */
    277 	sbp->f_ffree = 0;		/* See comments above */
    278 	if (sbp != &mp->mnt_stat) {
    279 		bcopy(&mp->mnt_stat.f_fsid, &sbp->f_fsid, sizeof(sbp->f_fsid));
    280 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
    281 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
    282 	}
    283 	return (0);
    284 }
    285 
    286 kernfs_sync(mp, waitfor)
    287 	struct mount *mp;
    288 	int waitfor;
    289 {
    290 	return (0);
    291 }
    292 
    293 kernfs_fhtovp(mp, fhp, vpp)
    294 	struct mount *mp;
    295 	struct fid *fhp;
    296 	struct vnode **vpp;
    297 {
    298 	return (EOPNOTSUPP);
    299 }
    300 
    301 kernfs_vptofh(vp, fhp)
    302 	struct vnode *vp;
    303 	struct fid *fhp;
    304 {
    305 	return (EOPNOTSUPP);
    306 }
    307 
    308 struct vfsops kernfs_vfsops = {
    309 	kernfs_mount,
    310 	kernfs_start,
    311 	kernfs_unmount,
    312 	kernfs_root,
    313 	kernfs_quotactl,
    314 	kernfs_statfs,
    315 	kernfs_sync,
    316 	kernfs_fhtovp,
    317 	kernfs_vptofh,
    318 	kernfs_init,
    319 };
    320