Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vfsops.c revision 1.68
      1 /*	$NetBSD: kernfs_vfsops.c,v 1.68 2005/08/30 20:08:01 xtraeme 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. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  *
     34  *	@(#)kernfs_vfsops.c	8.10 (Berkeley) 5/14/95
     35  */
     36 
     37 /*
     38  * Kernel params Filesystem
     39  */
     40 
     41 #include <sys/cdefs.h>
     42 __KERNEL_RCSID(0, "$NetBSD: kernfs_vfsops.c,v 1.68 2005/08/30 20:08:01 xtraeme Exp $");
     43 
     44 #ifdef _KERNEL_OPT
     45 #include "opt_compat_netbsd.h"
     46 #endif
     47 
     48 #include <sys/param.h>
     49 #include <sys/systm.h>
     50 #include <sys/sysctl.h>
     51 #include <sys/conf.h>
     52 #include <sys/proc.h>
     53 #include <sys/vnode.h>
     54 #include <sys/mount.h>
     55 #include <sys/namei.h>
     56 #include <sys/dirent.h>
     57 #include <sys/malloc.h>
     58 #include <sys/syslog.h>
     59 
     60 #include <miscfs/specfs/specdev.h>
     61 #include <miscfs/kernfs/kernfs.h>
     62 
     63 MALLOC_DEFINE(M_KERNFSMNT, "kernfs mount", "kernfs mount structures");
     64 
     65 dev_t rrootdev = NODEV;
     66 
     67 void	kernfs_init(void);
     68 void	kernfs_reinit(void);
     69 void	kernfs_done(void);
     70 void	kernfs_get_rrootdev(void);
     71 int	kernfs_mount(struct mount *, const char *, void *,
     72 	    struct nameidata *, struct proc *);
     73 int	kernfs_start(struct mount *, int, struct proc *);
     74 int	kernfs_unmount(struct mount *, int, struct proc *);
     75 int	kernfs_statvfs(struct mount *, struct statvfs *, struct proc *);
     76 int	kernfs_quotactl(struct mount *, int, uid_t, void *,
     77 			     struct proc *);
     78 int	kernfs_sync(struct mount *, int, struct ucred *, struct proc *);
     79 int	kernfs_vget(struct mount *, ino_t, struct vnode **);
     80 int	kernfs_fhtovp(struct mount *, struct fid *, struct vnode **);
     81 int	kernfs_checkexp(struct mount *, struct mbuf *, int *,
     82 			   struct ucred **);
     83 int	kernfs_vptofh(struct vnode *, struct fid *);
     84 
     85 void
     86 kernfs_init()
     87 {
     88 #ifdef _LKM
     89 	malloc_type_attach(M_KERNFSMNT);
     90 #endif
     91 	kernfs_hashinit();
     92 }
     93 
     94 void
     95 kernfs_reinit()
     96 {
     97 	kernfs_hashreinit();
     98 }
     99 
    100 void
    101 kernfs_done()
    102 {
    103 #ifdef _LKM
    104 	malloc_type_detach(M_KERNFSMNT);
    105 #endif
    106 	kernfs_hashdone();
    107 }
    108 
    109 void
    110 kernfs_get_rrootdev()
    111 {
    112 	static int tried = 0;
    113 
    114 	if (tried) {
    115 		/* Already did it once. */
    116 		return;
    117 	}
    118 	tried = 1;
    119 
    120 	if (rootdev == NODEV)
    121 		return;
    122 	rrootdev = devsw_blk2chr(rootdev);
    123 	if (rrootdev != NODEV)
    124 		return;
    125 	rrootdev = NODEV;
    126 	printf("kernfs_get_rrootdev: no raw root device\n");
    127 }
    128 
    129 /*
    130  * Mount the Kernel params filesystem
    131  */
    132 int
    133 kernfs_mount(mp, path, data, ndp, p)
    134 	struct mount *mp;
    135 	const char *path;
    136 	void *data;
    137 	struct nameidata *ndp;
    138 	struct proc *p;
    139 {
    140 	int error = 0;
    141 	struct kernfs_mount *fmp;
    142 
    143 	if (UIO_MX & (UIO_MX - 1)) {
    144 		log(LOG_ERR, "kernfs: invalid directory entry size");
    145 		return (EINVAL);
    146 	}
    147 
    148 	if (mp->mnt_flag & MNT_GETARGS)
    149 		return 0;
    150 	/*
    151 	 * Update is a no-op
    152 	 */
    153 	if (mp->mnt_flag & MNT_UPDATE)
    154 		return (EOPNOTSUPP);
    155 
    156 	MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
    157 	    M_KERNFSMNT, M_WAITOK);
    158 	memset(fmp, 0, sizeof(*fmp));
    159 	TAILQ_INIT(&fmp->nodelist);
    160 
    161 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    162 	mp->mnt_flag |= MNT_LOCAL;
    163 	mp->mnt_data = fmp;
    164 	vfs_getnewfsid(mp);
    165 
    166 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "kernfs",
    167 	    UIO_SYSSPACE, mp, p)) != 0) {
    168 		free(fmp, M_KERNFSMNT);
    169 		return error;
    170 	}
    171 
    172 	kernfs_get_rrootdev();
    173 	return 0;
    174 }
    175 
    176 int
    177 kernfs_start(mp, flags, p)
    178 	struct mount *mp;
    179 	int flags;
    180 	struct proc *p;
    181 {
    182 
    183 	return (0);
    184 }
    185 
    186 int
    187 kernfs_unmount(mp, mntflags, p)
    188 	struct mount *mp;
    189 	int mntflags;
    190 	struct proc *p;
    191 {
    192 	int error;
    193 	int flags = 0;
    194 
    195 	if (mntflags & MNT_FORCE)
    196 		flags |= FORCECLOSE;
    197 
    198 	if ((error = vflush(mp, 0, flags)) != 0)
    199 		return (error);
    200 
    201 	/*
    202 	 * Finally, throw away the kernfs_mount structure
    203 	 */
    204 	free(mp->mnt_data, M_KERNFSMNT);
    205 	mp->mnt_data = NULL;
    206 	return (0);
    207 }
    208 
    209 int
    210 kernfs_root(mp, vpp)
    211 	struct mount *mp;
    212 	struct vnode **vpp;
    213 {
    214 
    215 	/* setup "." */
    216 	return (kernfs_allocvp(mp, vpp, KFSkern, &kern_targets[0], 0));
    217 }
    218 
    219 int
    220 kernfs_quotactl(mp, cmd, uid, arg, p)
    221 	struct mount *mp;
    222 	int cmd;
    223 	uid_t uid;
    224 	void *arg;
    225 	struct proc *p;
    226 {
    227 
    228 	return (EOPNOTSUPP);
    229 }
    230 
    231 int
    232 kernfs_statvfs(mp, sbp, p)
    233 	struct mount *mp;
    234 	struct statvfs *sbp;
    235 	struct proc *p;
    236 {
    237 
    238 	sbp->f_bsize = DEV_BSIZE;
    239 	sbp->f_frsize = DEV_BSIZE;
    240 	sbp->f_iosize = DEV_BSIZE;
    241 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    242 	sbp->f_bfree = 0;
    243 	sbp->f_bavail = 0;
    244 	sbp->f_bresvd = 0;
    245 	sbp->f_files = 1024;	/* XXX lie */
    246 	sbp->f_ffree = 128;	/* XXX lie */
    247 	sbp->f_favail = 128;	/* XXX lie */
    248 	sbp->f_fresvd = 0;
    249 	copy_statvfs_info(sbp, mp);
    250 	return (0);
    251 }
    252 
    253 /*ARGSUSED*/
    254 int
    255 kernfs_sync(mp, waitfor, uc, p)
    256 	struct mount *mp;
    257 	int waitfor;
    258 	struct ucred *uc;
    259 	struct proc *p;
    260 {
    261 
    262 	return (0);
    263 }
    264 
    265 /*
    266  * Kernfs flat namespace lookup.
    267  * Currently unsupported.
    268  */
    269 int
    270 kernfs_vget(mp, ino, vpp)
    271 	struct mount *mp;
    272 	ino_t ino;
    273 	struct vnode **vpp;
    274 {
    275 
    276 	return (EOPNOTSUPP);
    277 }
    278 
    279 /*ARGSUSED*/
    280 int
    281 kernfs_fhtovp(mp, fhp, vpp)
    282 	struct mount *mp;
    283 	struct fid *fhp;
    284 	struct vnode **vpp;
    285 {
    286 
    287 	return (EOPNOTSUPP);
    288 }
    289 
    290 /*ARGSUSED*/
    291 int
    292 kernfs_checkexp(mp, mb, what, anon)
    293 	struct mount *mp;
    294 	struct mbuf *mb;
    295 	int *what;
    296 	struct ucred **anon;
    297 {
    298 
    299 	return (EOPNOTSUPP);
    300 }
    301 
    302 /*ARGSUSED*/
    303 int
    304 kernfs_vptofh(vp, fhp)
    305 	struct vnode *vp;
    306 	struct fid *fhp;
    307 {
    308 
    309 	return (EOPNOTSUPP);
    310 }
    311 
    312 SYSCTL_SETUP(sysctl_vfs_kernfs_setup, "sysctl vfs.kern subtree setup")
    313 {
    314 
    315 	sysctl_createv(clog, 0, NULL, NULL,
    316 		       CTLFLAG_PERMANENT,
    317 		       CTLTYPE_NODE, "vfs", NULL,
    318 		       NULL, 0, NULL, 0,
    319 		       CTL_VFS, CTL_EOL);
    320 	sysctl_createv(clog, 0, NULL, NULL,
    321 		       CTLFLAG_PERMANENT,
    322 		       CTLTYPE_NODE, "kernfs",
    323 		       SYSCTL_DESCR("/kern file system"),
    324 		       NULL, 0, NULL, 0,
    325 		       CTL_VFS, 11, CTL_EOL);
    326 	/*
    327 	 * XXX the "11" above could be dynamic, thereby eliminating one
    328 	 * more instance of the "number to vfs" mapping problem, but
    329 	 * "11" is the order as taken from sys/mount.h
    330 	 */
    331 }
    332 
    333 extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc;
    334 
    335 const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = {
    336 	&kernfs_vnodeop_opv_desc,
    337 	NULL,
    338 };
    339 
    340 struct vfsops kernfs_vfsops = {
    341 	MOUNT_KERNFS,
    342 	kernfs_mount,
    343 	kernfs_start,
    344 	kernfs_unmount,
    345 	kernfs_root,
    346 	kernfs_quotactl,
    347 	kernfs_statvfs,
    348 	kernfs_sync,
    349 	kernfs_vget,
    350 	kernfs_fhtovp,
    351 	kernfs_vptofh,
    352 	kernfs_init,
    353 	kernfs_reinit,
    354 	kernfs_done,
    355 	NULL,
    356 	NULL,				/* vfs_mountroot */
    357 	kernfs_checkexp,
    358 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    359 	vfs_stdextattrctl,
    360 	kernfs_vnodeopv_descs,
    361 };
    362 VFS_ATTACH(kernfs_vfsops);
    363