Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vfsops.c revision 1.72
      1 /*	$NetBSD: kernfs_vfsops.c,v 1.72 2006/09/02 06:37:41 christos 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.72 2006/09/02 06:37:41 christos 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 #include <sys/kauth.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(void);
     69 void	kernfs_reinit(void);
     70 void	kernfs_done(void);
     71 void	kernfs_get_rrootdev(void);
     72 int	kernfs_mount(struct mount *, const char *, void *,
     73 	    struct nameidata *, struct lwp *);
     74 int	kernfs_start(struct mount *, int, struct lwp *);
     75 int	kernfs_unmount(struct mount *, int, struct lwp *);
     76 int	kernfs_statvfs(struct mount *, struct statvfs *, struct lwp *);
     77 int	kernfs_quotactl(struct mount *, int, uid_t, void *,
     78 			     struct lwp *);
     79 int	kernfs_sync(struct mount *, int, kauth_cred_t, struct lwp *);
     80 int	kernfs_vget(struct mount *, ino_t, struct vnode **);
     81 
     82 void
     83 kernfs_init()
     84 {
     85 #ifdef _LKM
     86 	malloc_type_attach(M_KERNFSMNT);
     87 #endif
     88 	kernfs_hashinit();
     89 }
     90 
     91 void
     92 kernfs_reinit()
     93 {
     94 	kernfs_hashreinit();
     95 }
     96 
     97 void
     98 kernfs_done()
     99 {
    100 #ifdef _LKM
    101 	malloc_type_detach(M_KERNFSMNT);
    102 #endif
    103 	kernfs_hashdone();
    104 }
    105 
    106 void
    107 kernfs_get_rrootdev()
    108 {
    109 	static int tried = 0;
    110 
    111 	if (tried) {
    112 		/* Already did it once. */
    113 		return;
    114 	}
    115 	tried = 1;
    116 
    117 	if (rootdev == NODEV)
    118 		return;
    119 	rrootdev = devsw_blk2chr(rootdev);
    120 	if (rrootdev != NODEV)
    121 		return;
    122 	rrootdev = NODEV;
    123 	printf("kernfs_get_rrootdev: no raw root device\n");
    124 }
    125 
    126 /*
    127  * Mount the Kernel params filesystem
    128  */
    129 int
    130 kernfs_mount(mp, path, data, ndp, l)
    131 	struct mount *mp;
    132 	const char *path;
    133 	void *data;
    134 	struct nameidata *ndp;
    135 	struct lwp *l;
    136 {
    137 	int error = 0;
    138 	struct kernfs_mount *fmp;
    139 
    140 	if (UIO_MX & (UIO_MX - 1)) {
    141 		log(LOG_ERR, "kernfs: invalid directory entry size");
    142 		return (EINVAL);
    143 	}
    144 
    145 	if (mp->mnt_flag & MNT_GETARGS)
    146 		return 0;
    147 	/*
    148 	 * Update is a no-op
    149 	 */
    150 	if (mp->mnt_flag & MNT_UPDATE)
    151 		return (EOPNOTSUPP);
    152 
    153 	MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
    154 	    M_KERNFSMNT, M_WAITOK);
    155 	memset(fmp, 0, sizeof(*fmp));
    156 	TAILQ_INIT(&fmp->nodelist);
    157 
    158 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    159 	mp->mnt_flag |= MNT_LOCAL;
    160 	mp->mnt_data = fmp;
    161 	vfs_getnewfsid(mp);
    162 
    163 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "kernfs",
    164 	    UIO_SYSSPACE, mp, l)) != 0) {
    165 		free(fmp, M_KERNFSMNT);
    166 		return error;
    167 	}
    168 
    169 	kernfs_get_rrootdev();
    170 	return 0;
    171 }
    172 
    173 int
    174 kernfs_start(mp, flags, l)
    175 	struct mount *mp;
    176 	int flags;
    177 	struct lwp *l;
    178 {
    179 
    180 	return (0);
    181 }
    182 
    183 int
    184 kernfs_unmount(mp, mntflags, l)
    185 	struct mount *mp;
    186 	int mntflags;
    187 	struct lwp *l;
    188 {
    189 	int error;
    190 	int flags = 0;
    191 
    192 	if (mntflags & MNT_FORCE)
    193 		flags |= FORCECLOSE;
    194 
    195 	if ((error = vflush(mp, 0, flags)) != 0)
    196 		return (error);
    197 
    198 	/*
    199 	 * Finally, throw away the kernfs_mount structure
    200 	 */
    201 	free(mp->mnt_data, M_KERNFSMNT);
    202 	mp->mnt_data = NULL;
    203 	return (0);
    204 }
    205 
    206 int
    207 kernfs_root(mp, vpp)
    208 	struct mount *mp;
    209 	struct vnode **vpp;
    210 {
    211 
    212 	/* setup "." */
    213 	return (kernfs_allocvp(mp, vpp, KFSkern, &kern_targets[0], 0));
    214 }
    215 
    216 int
    217 kernfs_quotactl(mp, cmd, uid, arg, l)
    218 	struct mount *mp;
    219 	int cmd;
    220 	uid_t uid;
    221 	void *arg;
    222 	struct lwp *l;
    223 {
    224 
    225 	return (EOPNOTSUPP);
    226 }
    227 
    228 int
    229 kernfs_statvfs(mp, sbp, l)
    230 	struct mount *mp;
    231 	struct statvfs *sbp;
    232 	struct lwp *l;
    233 {
    234 
    235 	sbp->f_bsize = DEV_BSIZE;
    236 	sbp->f_frsize = DEV_BSIZE;
    237 	sbp->f_iosize = DEV_BSIZE;
    238 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    239 	sbp->f_bfree = 0;
    240 	sbp->f_bavail = 0;
    241 	sbp->f_bresvd = 0;
    242 	sbp->f_files = 1024;	/* XXX lie */
    243 	sbp->f_ffree = 128;	/* XXX lie */
    244 	sbp->f_favail = 128;	/* XXX lie */
    245 	sbp->f_fresvd = 0;
    246 	copy_statvfs_info(sbp, mp);
    247 	return (0);
    248 }
    249 
    250 /*ARGSUSED*/
    251 int
    252 kernfs_sync(mp, waitfor, uc, l)
    253 	struct mount *mp;
    254 	int waitfor;
    255 	kauth_cred_t uc;
    256 	struct lwp *l;
    257 {
    258 
    259 	return (0);
    260 }
    261 
    262 /*
    263  * Kernfs flat namespace lookup.
    264  * Currently unsupported.
    265  */
    266 int
    267 kernfs_vget(mp, ino, vpp)
    268 	struct mount *mp;
    269 	ino_t ino;
    270 	struct vnode **vpp;
    271 {
    272 
    273 	return (EOPNOTSUPP);
    274 }
    275 
    276 SYSCTL_SETUP(sysctl_vfs_kernfs_setup, "sysctl vfs.kern subtree setup")
    277 {
    278 
    279 	sysctl_createv(clog, 0, NULL, NULL,
    280 		       CTLFLAG_PERMANENT,
    281 		       CTLTYPE_NODE, "vfs", NULL,
    282 		       NULL, 0, NULL, 0,
    283 		       CTL_VFS, CTL_EOL);
    284 	sysctl_createv(clog, 0, NULL, NULL,
    285 		       CTLFLAG_PERMANENT,
    286 		       CTLTYPE_NODE, "kernfs",
    287 		       SYSCTL_DESCR("/kern file system"),
    288 		       NULL, 0, NULL, 0,
    289 		       CTL_VFS, 11, CTL_EOL);
    290 	/*
    291 	 * XXX the "11" above could be dynamic, thereby eliminating one
    292 	 * more instance of the "number to vfs" mapping problem, but
    293 	 * "11" is the order as taken from sys/mount.h
    294 	 */
    295 }
    296 
    297 extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc;
    298 
    299 const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = {
    300 	&kernfs_vnodeop_opv_desc,
    301 	NULL,
    302 };
    303 
    304 struct vfsops kernfs_vfsops = {
    305 	MOUNT_KERNFS,
    306 	kernfs_mount,
    307 	kernfs_start,
    308 	kernfs_unmount,
    309 	kernfs_root,
    310 	kernfs_quotactl,
    311 	kernfs_statvfs,
    312 	kernfs_sync,
    313 	kernfs_vget,
    314 	NULL,				/* vfs_fhtovp */
    315 	NULL,				/* vfs_vptofh */
    316 	kernfs_init,
    317 	kernfs_reinit,
    318 	kernfs_done,
    319 	NULL,				/* vfs_mountroot */
    320 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    321 	vfs_stdextattrctl,
    322 	kernfs_vnodeopv_descs,
    323 	0,
    324 	{ NULL, NULL },
    325 };
    326 VFS_ATTACH(kernfs_vfsops);
    327