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