Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vfsops.c revision 1.74
      1  1.74  christos /*	$NetBSD: kernfs_vfsops.c,v 1.74 2006/11/16 01:33:38 christos Exp $	*/
      2  1.18       cgd 
      3   1.1       cgd /*
      4  1.32      fvdl  * Copyright (c) 1992, 1993, 1995
      5  1.15   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7  1.11       cgd  * This code is derived from software donated to Berkeley by
      8   1.1       cgd  * Jan-Simon Pendry.
      9   1.1       cgd  *
     10   1.2       cgd  * Redistribution and use in source and binary forms, with or without
     11   1.2       cgd  * modification, are permitted provided that the following conditions
     12   1.2       cgd  * are met:
     13   1.2       cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.2       cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.2       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.2       cgd  *    notice, this list of conditions and the following disclaimer in the
     17   1.2       cgd  *    documentation and/or other materials provided with the distribution.
     18  1.53       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.2       cgd  *    may be used to endorse or promote products derived from this software
     20   1.2       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.2       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.2       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.2       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.2       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.2       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.2       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.2       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.2       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.2       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.2       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.2       cgd  * SUCH DAMAGE.
     33   1.1       cgd  *
     34  1.32      fvdl  *	@(#)kernfs_vfsops.c	8.10 (Berkeley) 5/14/95
     35   1.1       cgd  */
     36   1.1       cgd 
     37   1.1       cgd /*
     38   1.1       cgd  * Kernel params Filesystem
     39   1.1       cgd  */
     40  1.42     lukem 
     41  1.42     lukem #include <sys/cdefs.h>
     42  1.74  christos __KERNEL_RCSID(0, "$NetBSD: kernfs_vfsops.c,v 1.74 2006/11/16 01:33:38 christos Exp $");
     43  1.33  jonathan 
     44  1.54    itojun #ifdef _KERNEL_OPT
     45  1.33  jonathan #include "opt_compat_netbsd.h"
     46  1.33  jonathan #endif
     47   1.1       cgd 
     48   1.9   mycroft #include <sys/param.h>
     49   1.9   mycroft #include <sys/systm.h>
     50  1.57    atatat #include <sys/sysctl.h>
     51  1.15   mycroft #include <sys/conf.h>
     52   1.9   mycroft #include <sys/proc.h>
     53   1.9   mycroft #include <sys/vnode.h>
     54   1.9   mycroft #include <sys/mount.h>
     55   1.9   mycroft #include <sys/namei.h>
     56  1.60  christos #include <sys/dirent.h>
     57   1.9   mycroft #include <sys/malloc.h>
     58  1.54    itojun #include <sys/syslog.h>
     59  1.71      elad #include <sys/kauth.h>
     60  1.15   mycroft 
     61  1.15   mycroft #include <miscfs/specfs/specdev.h>
     62   1.9   mycroft #include <miscfs/kernfs/kernfs.h>
     63   1.1       cgd 
     64  1.47   thorpej MALLOC_DEFINE(M_KERNFSMNT, "kernfs mount", "kernfs mount structures");
     65  1.47   thorpej 
     66  1.15   mycroft dev_t rrootdev = NODEV;
     67  1.15   mycroft 
     68  1.68   xtraeme void	kernfs_init(void);
     69  1.68   xtraeme void	kernfs_reinit(void);
     70  1.68   xtraeme void	kernfs_done(void);
     71  1.68   xtraeme void	kernfs_get_rrootdev(void);
     72  1.68   xtraeme int	kernfs_mount(struct mount *, const char *, void *,
     73  1.70  christos 	    struct nameidata *, struct lwp *);
     74  1.70  christos int	kernfs_start(struct mount *, int, struct lwp *);
     75  1.70  christos int	kernfs_unmount(struct mount *, int, struct lwp *);
     76  1.70  christos int	kernfs_statvfs(struct mount *, struct statvfs *, struct lwp *);
     77  1.68   xtraeme int	kernfs_quotactl(struct mount *, int, uid_t, void *,
     78  1.70  christos 			     struct lwp *);
     79  1.71      elad int	kernfs_sync(struct mount *, int, kauth_cred_t, struct lwp *);
     80  1.68   xtraeme int	kernfs_vget(struct mount *, ino_t, struct vnode **);
     81  1.25  christos 
     82  1.25  christos void
     83   1.1       cgd kernfs_init()
     84   1.1       cgd {
     85  1.49  christos #ifdef _LKM
     86  1.49  christos 	malloc_type_attach(M_KERNFSMNT);
     87  1.49  christos #endif
     88  1.54    itojun 	kernfs_hashinit();
     89  1.54    itojun }
     90  1.54    itojun 
     91  1.54    itojun void
     92  1.54    itojun kernfs_reinit()
     93  1.54    itojun {
     94  1.54    itojun 	kernfs_hashreinit();
     95  1.10       cgd }
     96  1.10       cgd 
     97  1.11       cgd void
     98  1.36  jdolecek kernfs_done()
     99  1.36  jdolecek {
    100  1.49  christos #ifdef _LKM
    101  1.49  christos 	malloc_type_detach(M_KERNFSMNT);
    102  1.49  christos #endif
    103  1.54    itojun 	kernfs_hashdone();
    104  1.36  jdolecek }
    105  1.36  jdolecek 
    106  1.36  jdolecek void
    107  1.15   mycroft kernfs_get_rrootdev()
    108  1.10       cgd {
    109  1.16   mycroft 	static int tried = 0;
    110  1.10       cgd 
    111  1.16   mycroft 	if (tried) {
    112  1.15   mycroft 		/* Already did it once. */
    113  1.15   mycroft 		return;
    114  1.15   mycroft 	}
    115  1.16   mycroft 	tried = 1;
    116  1.16   mycroft 
    117  1.16   mycroft 	if (rootdev == NODEV)
    118  1.16   mycroft 		return;
    119  1.45   gehenna 	rrootdev = devsw_blk2chr(rootdev);
    120  1.54    itojun 	if (rrootdev != NODEV)
    121  1.45   gehenna 		return;
    122  1.16   mycroft 	rrootdev = NODEV;
    123  1.28  christos 	printf("kernfs_get_rrootdev: no raw root device\n");
    124   1.1       cgd }
    125   1.1       cgd 
    126   1.1       cgd /*
    127  1.15   mycroft  * Mount the Kernel params filesystem
    128   1.1       cgd  */
    129  1.25  christos int
    130  1.74  christos kernfs_mount(struct mount *mp, const char *path, void *data,
    131  1.74  christos     struct nameidata *ndp, struct lwp *l)
    132   1.1       cgd {
    133   1.1       cgd 	int error = 0;
    134   1.1       cgd 	struct kernfs_mount *fmp;
    135   1.1       cgd 
    136  1.54    itojun 	if (UIO_MX & (UIO_MX - 1)) {
    137  1.54    itojun 		log(LOG_ERR, "kernfs: invalid directory entry size");
    138  1.54    itojun 		return (EINVAL);
    139  1.54    itojun 	}
    140   1.1       cgd 
    141  1.46  christos 	if (mp->mnt_flag & MNT_GETARGS)
    142  1.46  christos 		return 0;
    143   1.1       cgd 	/*
    144   1.1       cgd 	 * Update is a no-op
    145   1.1       cgd 	 */
    146   1.1       cgd 	if (mp->mnt_flag & MNT_UPDATE)
    147   1.1       cgd 		return (EOPNOTSUPP);
    148   1.1       cgd 
    149  1.15   mycroft 	MALLOC(fmp, struct kernfs_mount *, sizeof(struct kernfs_mount),
    150  1.47   thorpej 	    M_KERNFSMNT, M_WAITOK);
    151  1.54    itojun 	memset(fmp, 0, sizeof(*fmp));
    152  1.54    itojun 	TAILQ_INIT(&fmp->nodelist);
    153  1.54    itojun 
    154  1.65  jdolecek 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    155  1.65  jdolecek 	mp->mnt_flag |= MNT_LOCAL;
    156  1.54    itojun 	mp->mnt_data = fmp;
    157  1.37     assar 	vfs_getnewfsid(mp);
    158   1.1       cgd 
    159  1.64      tron 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "kernfs",
    160  1.70  christos 	    UIO_SYSSPACE, mp, l)) != 0) {
    161  1.64      tron 		free(fmp, M_KERNFSMNT);
    162  1.64      tron 		return error;
    163  1.64      tron 	}
    164  1.10       cgd 
    165  1.15   mycroft 	kernfs_get_rrootdev();
    166  1.64      tron 	return 0;
    167   1.1       cgd }
    168   1.1       cgd 
    169  1.25  christos int
    170  1.74  christos kernfs_start(struct mount *mp, int flags,
    171  1.74  christos     struct lwp *l)
    172   1.1       cgd {
    173  1.15   mycroft 
    174   1.1       cgd 	return (0);
    175   1.1       cgd }
    176   1.1       cgd 
    177  1.25  christos int
    178  1.74  christos kernfs_unmount(struct mount *mp, int mntflags, struct lwp *l)
    179   1.1       cgd {
    180   1.1       cgd 	int error;
    181   1.1       cgd 	int flags = 0;
    182   1.1       cgd 
    183  1.54    itojun 	if (mntflags & MNT_FORCE)
    184   1.1       cgd 		flags |= FORCECLOSE;
    185   1.1       cgd 
    186  1.54    itojun 	if ((error = vflush(mp, 0, flags)) != 0)
    187   1.1       cgd 		return (error);
    188   1.1       cgd 
    189   1.1       cgd 	/*
    190   1.1       cgd 	 * Finally, throw away the kernfs_mount structure
    191   1.1       cgd 	 */
    192  1.47   thorpej 	free(mp->mnt_data, M_KERNFSMNT);
    193  1.64      tron 	mp->mnt_data = NULL;
    194  1.15   mycroft 	return (0);
    195   1.1       cgd }
    196   1.1       cgd 
    197  1.25  christos int
    198  1.51   thorpej kernfs_root(mp, vpp)
    199   1.1       cgd 	struct mount *mp;
    200   1.1       cgd 	struct vnode **vpp;
    201   1.1       cgd {
    202   1.1       cgd 
    203  1.54    itojun 	/* setup "." */
    204  1.56     darcy 	return (kernfs_allocvp(mp, vpp, KFSkern, &kern_targets[0], 0));
    205   1.1       cgd }
    206   1.1       cgd 
    207  1.25  christos int
    208  1.74  christos kernfs_quotactl(struct mount *mp, int cmd, uid_t uid,
    209  1.74  christos     void *arg, struct lwp *l)
    210   1.1       cgd {
    211  1.15   mycroft 
    212   1.1       cgd 	return (EOPNOTSUPP);
    213   1.1       cgd }
    214   1.1       cgd 
    215  1.25  christos int
    216  1.74  christos kernfs_statvfs(struct mount *mp, struct statvfs *sbp, struct lwp *l)
    217   1.1       cgd {
    218   1.1       cgd 
    219   1.1       cgd 	sbp->f_bsize = DEV_BSIZE;
    220  1.59  christos 	sbp->f_frsize = DEV_BSIZE;
    221  1.13       cgd 	sbp->f_iosize = DEV_BSIZE;
    222   1.1       cgd 	sbp->f_blocks = 2;		/* 1K to keep df happy */
    223   1.1       cgd 	sbp->f_bfree = 0;
    224   1.1       cgd 	sbp->f_bavail = 0;
    225  1.59  christos 	sbp->f_bresvd = 0;
    226  1.54    itojun 	sbp->f_files = 1024;	/* XXX lie */
    227  1.54    itojun 	sbp->f_ffree = 128;	/* XXX lie */
    228  1.59  christos 	sbp->f_favail = 128;	/* XXX lie */
    229  1.59  christos 	sbp->f_fresvd = 0;
    230  1.59  christos 	copy_statvfs_info(sbp, mp);
    231   1.1       cgd 	return (0);
    232   1.1       cgd }
    233   1.1       cgd 
    234  1.25  christos /*ARGSUSED*/
    235  1.25  christos int
    236  1.74  christos kernfs_sync(struct mount *mp, int waitfor,
    237  1.74  christos     kauth_cred_t uc, struct lwp *l)
    238   1.1       cgd {
    239  1.15   mycroft 
    240   1.1       cgd 	return (0);
    241   1.1       cgd }
    242   1.1       cgd 
    243  1.15   mycroft /*
    244  1.15   mycroft  * Kernfs flat namespace lookup.
    245  1.15   mycroft  * Currently unsupported.
    246  1.15   mycroft  */
    247  1.25  christos int
    248  1.74  christos kernfs_vget(struct mount *mp, ino_t ino,
    249  1.74  christos     struct vnode **vpp)
    250  1.15   mycroft {
    251  1.15   mycroft 
    252  1.15   mycroft 	return (EOPNOTSUPP);
    253  1.15   mycroft }
    254  1.15   mycroft 
    255  1.58    atatat SYSCTL_SETUP(sysctl_vfs_kernfs_setup, "sysctl vfs.kern subtree setup")
    256  1.32      fvdl {
    257  1.57    atatat 
    258  1.58    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    259  1.58    atatat 		       CTLFLAG_PERMANENT,
    260  1.57    atatat 		       CTLTYPE_NODE, "vfs", NULL,
    261  1.57    atatat 		       NULL, 0, NULL, 0,
    262  1.57    atatat 		       CTL_VFS, CTL_EOL);
    263  1.58    atatat 	sysctl_createv(clog, 0, NULL, NULL,
    264  1.58    atatat 		       CTLFLAG_PERMANENT,
    265  1.62    atatat 		       CTLTYPE_NODE, "kernfs",
    266  1.62    atatat 		       SYSCTL_DESCR("/kern file system"),
    267  1.57    atatat 		       NULL, 0, NULL, 0,
    268  1.57    atatat 		       CTL_VFS, 11, CTL_EOL);
    269  1.57    atatat 	/*
    270  1.57    atatat 	 * XXX the "11" above could be dynamic, thereby eliminating one
    271  1.57    atatat 	 * more instance of the "number to vfs" mapping problem, but
    272  1.57    atatat 	 * "11" is the order as taken from sys/mount.h
    273  1.57    atatat 	 */
    274  1.32      fvdl }
    275  1.32      fvdl 
    276  1.38  jdolecek extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc;
    277  1.31   thorpej 
    278  1.38  jdolecek const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = {
    279  1.31   thorpej 	&kernfs_vnodeop_opv_desc,
    280  1.31   thorpej 	NULL,
    281  1.31   thorpej };
    282  1.31   thorpej 
    283   1.1       cgd struct vfsops kernfs_vfsops = {
    284  1.12       cgd 	MOUNT_KERNFS,
    285   1.1       cgd 	kernfs_mount,
    286   1.1       cgd 	kernfs_start,
    287   1.1       cgd 	kernfs_unmount,
    288   1.1       cgd 	kernfs_root,
    289   1.1       cgd 	kernfs_quotactl,
    290  1.59  christos 	kernfs_statvfs,
    291   1.1       cgd 	kernfs_sync,
    292  1.15   mycroft 	kernfs_vget,
    293  1.69      jmmv 	NULL,				/* vfs_fhtovp */
    294  1.69      jmmv 	NULL,				/* vfs_vptofh */
    295   1.1       cgd 	kernfs_init,
    296  1.54    itojun 	kernfs_reinit,
    297  1.36  jdolecek 	kernfs_done,
    298  1.31   thorpej 	NULL,				/* vfs_mountroot */
    299  1.63   hannken 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    300  1.66   thorpej 	vfs_stdextattrctl,
    301  1.31   thorpej 	kernfs_vnodeopv_descs,
    302  1.72  christos 	0,
    303  1.72  christos 	{ NULL, NULL },
    304   1.1       cgd };
    305  1.67   thorpej VFS_ATTACH(kernfs_vfsops);
    306