Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vfsops.c revision 1.35
      1 /*	$NetBSD: kernfs_vfsops.c,v 1.35 1999/02/26 23:44:45 wrstuden Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1992, 1993, 1995
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software donated to Berkeley by
      8  * Jan-Simon Pendry.
      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  *	@(#)kernfs_vfsops.c	8.10 (Berkeley) 5/14/95
     39  */
     40 
     41 /*
     42  * Kernel params Filesystem
     43  */
     44 
     45 #if defined(_KERNEL) && !defined(_LKM)
     46 #include "opt_compat_netbsd.h"
     47 #endif
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/conf.h>
     52 #include <sys/types.h>
     53 #include <sys/proc.h>
     54 #include <sys/vnode.h>
     55 #include <sys/mount.h>
     56 #include <sys/namei.h>
     57 #include <sys/malloc.h>
     58 
     59 #include <miscfs/specfs/specdev.h>
     60 #include <miscfs/kernfs/kernfs.h>
     61 
     62 dev_t rrootdev = NODEV;
     63 
     64 void	kernfs_init __P((void));
     65 void	kernfs_get_rrootdev __P((void));
     66 int	kernfs_mount __P((struct mount *, const char *, void *,
     67 	    struct nameidata *, struct proc *));
     68 int	kernfs_start __P((struct mount *, int, struct proc *));
     69 int	kernfs_unmount __P((struct mount *, int, struct proc *));
     70 int	kernfs_root __P((struct mount *, struct vnode **));
     71 int	kernfs_statfs __P((struct mount *, struct statfs *, struct proc *));
     72 int	kernfs_quotactl __P((struct mount *, int, uid_t, caddr_t,
     73 			     struct proc *));
     74 int	kernfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
     75 int	kernfs_vget __P((struct mount *, ino_t, struct vnode **));
     76 int	kernfs_fhtovp __P((struct mount *, struct fid *, struct vnode **));
     77 int	kernfs_checkexp __P((struct mount *, struct mbuf *, int *,
     78 			   struct ucred **));
     79 int	kernfs_vptofh __P((struct vnode *, struct fid *));
     80 int	kernfs_sysctl __P((int *, u_int, void *, size_t *, void *, size_t,
     81 			   struct proc *));
     82 
     83 void
     84 kernfs_init()
     85 {
     86 }
     87 
     88 void
     89 kernfs_get_rrootdev()
     90 {
     91 	static int tried = 0;
     92 	int cmaj;
     93 
     94 	if (tried) {
     95 		/* Already did it once. */
     96 		return;
     97 	}
     98 	tried = 1;
     99 
    100 	if (rootdev == NODEV)
    101 		return;
    102 	for (cmaj = 0; cmaj < nchrdev; cmaj++) {
    103 		rrootdev = makedev(cmaj, minor(rootdev));
    104 		if (chrtoblk(rrootdev) == rootdev)
    105 			return;
    106 	}
    107 	rrootdev = NODEV;
    108 	printf("kernfs_get_rrootdev: no raw root device\n");
    109 }
    110 
    111 /*
    112  * Mount the Kernel params filesystem
    113  */
    114 int
    115 kernfs_mount(mp, path, data, ndp, p)
    116 	struct mount *mp;
    117 	const char *path;
    118 	void *data;
    119 	struct nameidata *ndp;
    120 	struct proc *p;
    121 {
    122 	int error = 0;
    123 	size_t size;
    124 	struct kernfs_mount *fmp;
    125 	struct vnode *rvp;
    126 
    127 #ifdef KERNFS_DIAGNOSTIC
    128 	printf("kernfs_mount(mp = %p)\n", mp);
    129 #endif
    130 
    131 	/*
    132 	 * Update is a no-op
    133 	 */
    134 	if (mp->mnt_flag & MNT_UPDATE)
    135 		return (EOPNOTSUPP);
    136 
    137 	error = getnewvnode(VT_KERNFS, mp, kernfs_vnodeop_p, &rvp);
    138 	if (error)
    139 		return (error);
    140 
    141 	MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
    142 	    M_MISCFSMNT, M_WAITOK);
    143 	rvp->v_type = VDIR;
    144 	rvp->v_flag |= VROOT;
    145 #ifdef KERNFS_DIAGNOSTIC
    146 	printf("kernfs_mount: root vp = %p\n", rvp);
    147 #endif
    148 	fmp->kf_root = rvp;
    149 	mp->mnt_flag |= MNT_LOCAL;
    150 	mp->mnt_data = (qaddr_t)fmp;
    151 	vfs_getnewfsid(mp, MOUNT_KERNFS);
    152 
    153 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
    154 	memset(mp->mnt_stat.f_mntonname + size, 0, MNAMELEN - size);
    155 	memset(mp->mnt_stat.f_mntfromname, 0, MNAMELEN);
    156 	memcpy(mp->mnt_stat.f_mntfromname, "kernfs", sizeof("kernfs"));
    157 #ifdef KERNFS_DIAGNOSTIC
    158 	printf("kernfs_mount: at %s\n", mp->mnt_stat.f_mntonname);
    159 #endif
    160 
    161 	kernfs_get_rrootdev();
    162 	return (0);
    163 }
    164 
    165 int
    166 kernfs_start(mp, flags, p)
    167 	struct mount *mp;
    168 	int flags;
    169 	struct proc *p;
    170 {
    171 
    172 	return (0);
    173 }
    174 
    175 int
    176 kernfs_unmount(mp, mntflags, p)
    177 	struct mount *mp;
    178 	int mntflags;
    179 	struct proc *p;
    180 {
    181 	int error;
    182 	int flags = 0;
    183 	struct vnode *rootvp = VFSTOKERNFS(mp)->kf_root;
    184 
    185 #ifdef KERNFS_DIAGNOSTIC
    186 	printf("kernfs_unmount(mp = %p)\n", mp);
    187 #endif
    188 
    189 	 if (mntflags & MNT_FORCE)
    190 		flags |= FORCECLOSE;
    191 
    192 	/*
    193 	 * Clear out buffer cache.  I don't think we
    194 	 * ever get anything cached at this level at the
    195 	 * moment, but who knows...
    196 	 */
    197 	if (rootvp->v_usecount > 1)
    198 		return (EBUSY);
    199 #ifdef KERNFS_DIAGNOSTIC
    200 	printf("kernfs_unmount: calling vflush\n");
    201 #endif
    202 	if ((error = vflush(mp, rootvp, flags)) != 0)
    203 		return (error);
    204 
    205 #ifdef KERNFS_DIAGNOSTIC
    206 	vprint("kernfs root", rootvp);
    207 #endif
    208 	/*
    209 	 * Clean out the old root vnode for reuse.
    210 	 */
    211 	vrele(rootvp);
    212 	vgone(rootvp);
    213 	/*
    214 	 * Finally, throw away the kernfs_mount structure
    215 	 */
    216 	free(mp->mnt_data, M_MISCFSMNT);
    217 	mp->mnt_data = 0;
    218 	return (0);
    219 }
    220 
    221 int
    222 kernfs_root(mp, vpp)
    223 	struct mount *mp;
    224 	struct vnode **vpp;
    225 {
    226 	struct vnode *vp;
    227 
    228 #ifdef KERNFS_DIAGNOSTIC
    229 	printf("kernfs_root(mp = %p)\n", mp);
    230 #endif
    231 
    232 	/*
    233 	 * Return locked reference to root.
    234 	 */
    235 	vp = VFSTOKERNFS(mp)->kf_root;
    236 	VREF(vp);
    237 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    238 	*vpp = vp;
    239 	return (0);
    240 }
    241 
    242 int
    243 kernfs_quotactl(mp, cmd, uid, arg, p)
    244 	struct mount *mp;
    245 	int cmd;
    246 	uid_t uid;
    247 	caddr_t arg;
    248 	struct proc *p;
    249 {
    250 
    251 	return (EOPNOTSUPP);
    252 }
    253 
    254 int
    255 kernfs_statfs(mp, sbp, p)
    256 	struct mount *mp;
    257 	struct statfs *sbp;
    258 	struct proc *p;
    259 {
    260 
    261 #ifdef KERNFS_DIAGNOSTIC
    262 	printf("kernfs_statfs(mp = %p)\n", mp);
    263 #endif
    264 
    265 	sbp->f_bsize = DEV_BSIZE;
    266 	sbp->f_iosize = DEV_BSIZE;
    267 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    268 	sbp->f_bfree = 0;
    269 	sbp->f_bavail = 0;
    270 	sbp->f_files = 0;
    271 	sbp->f_ffree = 0;
    272 #ifdef COMPAT_09
    273 	sbp->f_type = 7;
    274 #else
    275 	sbp->f_type = 0;
    276 #endif
    277 	if (sbp != &mp->mnt_stat) {
    278 		memcpy(&sbp->f_fsid, &mp->mnt_stat.f_fsid, sizeof(sbp->f_fsid));
    279 		memcpy(sbp->f_mntonname, mp->mnt_stat.f_mntonname, MNAMELEN);
    280 		memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
    281 	}
    282 	strncpy(sbp->f_fstypename, mp->mnt_op->vfs_name, MFSNAMELEN);
    283 	return (0);
    284 }
    285 
    286 /*ARGSUSED*/
    287 int
    288 kernfs_sync(mp, waitfor, uc, p)
    289 	struct mount *mp;
    290 	int waitfor;
    291 	struct ucred *uc;
    292 	struct proc *p;
    293 {
    294 
    295 	return (0);
    296 }
    297 
    298 /*
    299  * Kernfs flat namespace lookup.
    300  * Currently unsupported.
    301  */
    302 int
    303 kernfs_vget(mp, ino, vpp)
    304 	struct mount *mp;
    305 	ino_t ino;
    306 	struct vnode **vpp;
    307 {
    308 
    309 	return (EOPNOTSUPP);
    310 }
    311 
    312 /*ARGSUSED*/
    313 int
    314 kernfs_fhtovp(mp, fhp, vpp)
    315 	struct mount *mp;
    316 	struct fid *fhp;
    317 	struct vnode **vpp;
    318 {
    319 
    320 	return (EOPNOTSUPP);
    321 }
    322 
    323 /*ARGSUSED*/
    324 int
    325 kernfs_checkexp(mp, mb, what, anon)
    326 	struct mount *mp;
    327 	struct mbuf *mb;
    328 	int *what;
    329 	struct ucred **anon;
    330 {
    331 
    332 	return (EOPNOTSUPP);
    333 }
    334 
    335 /*ARGSUSED*/
    336 int
    337 kernfs_vptofh(vp, fhp)
    338 	struct vnode *vp;
    339 	struct fid *fhp;
    340 {
    341 
    342 	return (EOPNOTSUPP);
    343 }
    344 
    345 int
    346 kernfs_sysctl(name, namelen, oldp, oldlenp, newp, newlen, p)
    347 	int *name;
    348 	u_int namelen;
    349 	void *oldp;
    350 	size_t *oldlenp;
    351 	void *newp;
    352 	size_t newlen;
    353 	struct proc *p;
    354 {
    355 	return (EOPNOTSUPP);
    356 }
    357 
    358 extern struct vnodeopv_desc kernfs_vnodeop_opv_desc;
    359 
    360 struct vnodeopv_desc *kernfs_vnodeopv_descs[] = {
    361 	&kernfs_vnodeop_opv_desc,
    362 	NULL,
    363 };
    364 
    365 struct vfsops kernfs_vfsops = {
    366 	MOUNT_KERNFS,
    367 	kernfs_mount,
    368 	kernfs_start,
    369 	kernfs_unmount,
    370 	kernfs_root,
    371 	kernfs_quotactl,
    372 	kernfs_statfs,
    373 	kernfs_sync,
    374 	kernfs_vget,
    375 	kernfs_fhtovp,
    376 	kernfs_vptofh,
    377 	kernfs_init,
    378 	kernfs_sysctl,
    379 	NULL,				/* vfs_mountroot */
    380 	kernfs_checkexp,
    381 	kernfs_vnodeopv_descs,
    382 };
    383