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