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