Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vfsops.c revision 1.83.10.3
      1  1.83.10.3      yamt /*	$NetBSD: kernfs_vfsops.c,v 1.83.10.3 2010/03/11 15:04:23 yamt 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.83.10.3      yamt __KERNEL_RCSID(0, "$NetBSD: kernfs_vfsops.c,v 1.83.10.3 2010/03/11 15:04:23 yamt 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.83.10.1      yamt #include <sys/module.h>
     61       1.15   mycroft 
     62       1.83  dholland #include <miscfs/genfs/genfs.h>
     63       1.15   mycroft #include <miscfs/specfs/specdev.h>
     64        1.9   mycroft #include <miscfs/kernfs/kernfs.h>
     65        1.1       cgd 
     66  1.83.10.1      yamt MODULE(MODULE_CLASS_VFS, kernfs, NULL);
     67  1.83.10.1      yamt 
     68       1.77     pooka MALLOC_JUSTDEFINE(M_KERNFSMNT, "kernfs mount", "kernfs mount structures");
     69       1.47   thorpej 
     70       1.15   mycroft dev_t rrootdev = NODEV;
     71       1.15   mycroft 
     72       1.81     pooka VFS_PROTOS(kernfs);
     73       1.81     pooka 
     74       1.68   xtraeme void	kernfs_get_rrootdev(void);
     75       1.25  christos 
     76  1.83.10.2      yamt static struct sysctllog *kernfs_sysctl_log;
     77  1.83.10.2      yamt 
     78       1.25  christos void
     79  1.83.10.2      yamt kernfs_init(void)
     80        1.1       cgd {
     81       1.77     pooka 
     82       1.49  christos 	malloc_type_attach(M_KERNFSMNT);
     83       1.54    itojun 	kernfs_hashinit();
     84       1.54    itojun }
     85       1.54    itojun 
     86       1.54    itojun void
     87  1.83.10.2      yamt kernfs_reinit(void)
     88       1.54    itojun {
     89       1.54    itojun 	kernfs_hashreinit();
     90       1.10       cgd }
     91       1.10       cgd 
     92       1.11       cgd void
     93  1.83.10.2      yamt kernfs_done(void)
     94       1.36  jdolecek {
     95       1.77     pooka 
     96       1.77     pooka 	kernfs_hashdone();
     97       1.49  christos 	malloc_type_detach(M_KERNFSMNT);
     98       1.36  jdolecek }
     99       1.36  jdolecek 
    100       1.36  jdolecek void
    101  1.83.10.2      yamt kernfs_get_rrootdev(void)
    102       1.10       cgd {
    103       1.16   mycroft 	static int tried = 0;
    104       1.10       cgd 
    105       1.16   mycroft 	if (tried) {
    106       1.15   mycroft 		/* Already did it once. */
    107       1.15   mycroft 		return;
    108       1.15   mycroft 	}
    109       1.16   mycroft 	tried = 1;
    110       1.16   mycroft 
    111       1.16   mycroft 	if (rootdev == NODEV)
    112       1.16   mycroft 		return;
    113       1.45   gehenna 	rrootdev = devsw_blk2chr(rootdev);
    114       1.54    itojun 	if (rrootdev != NODEV)
    115       1.45   gehenna 		return;
    116       1.16   mycroft 	rrootdev = NODEV;
    117       1.28  christos 	printf("kernfs_get_rrootdev: no raw root device\n");
    118        1.1       cgd }
    119        1.1       cgd 
    120        1.1       cgd /*
    121       1.15   mycroft  * Mount the Kernel params filesystem
    122        1.1       cgd  */
    123       1.25  christos int
    124       1.82     pooka kernfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    125        1.1       cgd {
    126       1.82     pooka 	struct lwp *l = curlwp;
    127        1.1       cgd 	int error = 0;
    128        1.1       cgd 	struct kernfs_mount *fmp;
    129        1.1       cgd 
    130       1.54    itojun 	if (UIO_MX & (UIO_MX - 1)) {
    131       1.54    itojun 		log(LOG_ERR, "kernfs: invalid directory entry size");
    132       1.54    itojun 		return (EINVAL);
    133       1.54    itojun 	}
    134        1.1       cgd 
    135       1.78       dsl 	if (mp->mnt_flag & MNT_GETARGS) {
    136       1.78       dsl 		*data_len = 0;
    137       1.46  christos 		return 0;
    138       1.78       dsl 	}
    139        1.1       cgd 	/*
    140        1.1       cgd 	 * Update is a no-op
    141        1.1       cgd 	 */
    142        1.1       cgd 	if (mp->mnt_flag & MNT_UPDATE)
    143        1.1       cgd 		return (EOPNOTSUPP);
    144        1.1       cgd 
    145  1.83.10.2      yamt 	fmp = malloc(sizeof(struct kernfs_mount), M_KERNFSMNT, M_WAITOK|M_ZERO);
    146       1.54    itojun 	TAILQ_INIT(&fmp->nodelist);
    147       1.54    itojun 
    148       1.65  jdolecek 	mp->mnt_stat.f_namemax = MAXNAMLEN;
    149       1.65  jdolecek 	mp->mnt_flag |= MNT_LOCAL;
    150       1.54    itojun 	mp->mnt_data = fmp;
    151       1.37     assar 	vfs_getnewfsid(mp);
    152        1.1       cgd 
    153       1.64      tron 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "kernfs",
    154       1.79     pooka 	    UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) {
    155       1.64      tron 		free(fmp, M_KERNFSMNT);
    156       1.64      tron 		return error;
    157       1.64      tron 	}
    158       1.10       cgd 
    159       1.15   mycroft 	kernfs_get_rrootdev();
    160       1.64      tron 	return 0;
    161        1.1       cgd }
    162        1.1       cgd 
    163       1.25  christos int
    164       1.82     pooka kernfs_start(struct mount *mp, int flags)
    165        1.1       cgd {
    166       1.15   mycroft 
    167        1.1       cgd 	return (0);
    168        1.1       cgd }
    169        1.1       cgd 
    170       1.25  christos int
    171       1.82     pooka kernfs_unmount(struct mount *mp, int mntflags)
    172        1.1       cgd {
    173        1.1       cgd 	int error;
    174        1.1       cgd 	int flags = 0;
    175        1.1       cgd 
    176       1.54    itojun 	if (mntflags & MNT_FORCE)
    177        1.1       cgd 		flags |= FORCECLOSE;
    178        1.1       cgd 
    179       1.54    itojun 	if ((error = vflush(mp, 0, flags)) != 0)
    180        1.1       cgd 		return (error);
    181        1.1       cgd 
    182        1.1       cgd 	/*
    183        1.1       cgd 	 * Finally, throw away the kernfs_mount structure
    184        1.1       cgd 	 */
    185       1.47   thorpej 	free(mp->mnt_data, M_KERNFSMNT);
    186       1.64      tron 	mp->mnt_data = NULL;
    187       1.15   mycroft 	return (0);
    188        1.1       cgd }
    189        1.1       cgd 
    190       1.25  christos int
    191  1.83.10.2      yamt kernfs_root(struct mount *mp, struct vnode **vpp)
    192        1.1       cgd {
    193        1.1       cgd 
    194       1.54    itojun 	/* setup "." */
    195       1.56     darcy 	return (kernfs_allocvp(mp, vpp, KFSkern, &kern_targets[0], 0));
    196        1.1       cgd }
    197        1.1       cgd 
    198       1.25  christos /*ARGSUSED*/
    199       1.25  christos int
    200       1.74  christos kernfs_sync(struct mount *mp, int waitfor,
    201       1.82     pooka     kauth_cred_t uc)
    202        1.1       cgd {
    203       1.15   mycroft 
    204        1.1       cgd 	return (0);
    205        1.1       cgd }
    206        1.1       cgd 
    207       1.15   mycroft /*
    208       1.15   mycroft  * Kernfs flat namespace lookup.
    209       1.15   mycroft  * Currently unsupported.
    210       1.15   mycroft  */
    211       1.25  christos int
    212       1.74  christos kernfs_vget(struct mount *mp, ino_t ino,
    213       1.74  christos     struct vnode **vpp)
    214       1.15   mycroft {
    215       1.15   mycroft 
    216       1.15   mycroft 	return (EOPNOTSUPP);
    217       1.15   mycroft }
    218       1.15   mycroft 
    219       1.38  jdolecek extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc;
    220       1.31   thorpej 
    221       1.38  jdolecek const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = {
    222       1.31   thorpej 	&kernfs_vnodeop_opv_desc,
    223       1.31   thorpej 	NULL,
    224       1.31   thorpej };
    225       1.31   thorpej 
    226        1.1       cgd struct vfsops kernfs_vfsops = {
    227       1.12       cgd 	MOUNT_KERNFS,
    228       1.78       dsl 	0,
    229        1.1       cgd 	kernfs_mount,
    230        1.1       cgd 	kernfs_start,
    231        1.1       cgd 	kernfs_unmount,
    232        1.1       cgd 	kernfs_root,
    233       1.82     pooka 	(void *)eopnotsupp,		/* vfs_quotactl */
    234  1.83.10.3      yamt 	genfs_statvfs,
    235        1.1       cgd 	kernfs_sync,
    236       1.15   mycroft 	kernfs_vget,
    237       1.75       chs 	(void *)eopnotsupp,		/* vfs_fhtovp */
    238       1.75       chs 	(void *)eopnotsupp,		/* vfs_vptofh */
    239        1.1       cgd 	kernfs_init,
    240       1.54    itojun 	kernfs_reinit,
    241       1.36  jdolecek 	kernfs_done,
    242       1.31   thorpej 	NULL,				/* vfs_mountroot */
    243       1.63   hannken 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
    244       1.66   thorpej 	vfs_stdextattrctl,
    245       1.80     pooka 	(void *)eopnotsupp,		/* vfs_suspendctl */
    246       1.83  dholland 	genfs_renamelock_enter,
    247       1.83  dholland 	genfs_renamelock_exit,
    248  1.83.10.1      yamt 	(void *)eopnotsupp,
    249       1.31   thorpej 	kernfs_vnodeopv_descs,
    250       1.72  christos 	0,
    251       1.72  christos 	{ NULL, NULL },
    252        1.1       cgd };
    253  1.83.10.1      yamt 
    254  1.83.10.1      yamt static int
    255  1.83.10.1      yamt kernfs_modcmd(modcmd_t cmd, void *arg)
    256  1.83.10.1      yamt {
    257  1.83.10.2      yamt 	int error;
    258  1.83.10.1      yamt 
    259  1.83.10.1      yamt 	switch (cmd) {
    260  1.83.10.1      yamt 	case MODULE_CMD_INIT:
    261  1.83.10.2      yamt 		error = vfs_attach(&kernfs_vfsops);
    262  1.83.10.2      yamt 		if (error != 0)
    263  1.83.10.2      yamt 			break;
    264  1.83.10.2      yamt 		sysctl_createv(&kernfs_sysctl_log, 0, NULL, NULL,
    265  1.83.10.2      yamt 			       CTLFLAG_PERMANENT,
    266  1.83.10.2      yamt 			       CTLTYPE_NODE, "vfs", NULL,
    267  1.83.10.2      yamt 			       NULL, 0, NULL, 0,
    268  1.83.10.2      yamt 			       CTL_VFS, CTL_EOL);
    269  1.83.10.2      yamt 		sysctl_createv(&kernfs_sysctl_log, 0, NULL, NULL,
    270  1.83.10.2      yamt 			       CTLFLAG_PERMANENT,
    271  1.83.10.2      yamt 			       CTLTYPE_NODE, "kernfs",
    272  1.83.10.2      yamt 			       SYSCTL_DESCR("/kern file system"),
    273  1.83.10.2      yamt 			       NULL, 0, NULL, 0,
    274  1.83.10.2      yamt 			       CTL_VFS, 11, CTL_EOL);
    275  1.83.10.2      yamt 		/*
    276  1.83.10.2      yamt 		 * XXX the "11" above could be dynamic, thereby eliminating one
    277  1.83.10.2      yamt 		 * more instance of the "number to vfs" mapping problem, but
    278  1.83.10.2      yamt 		 * "11" is the order as taken from sys/mount.h
    279  1.83.10.2      yamt 		 */
    280  1.83.10.2      yamt 		break;
    281  1.83.10.1      yamt 	case MODULE_CMD_FINI:
    282  1.83.10.2      yamt 		error = vfs_detach(&kernfs_vfsops);
    283  1.83.10.2      yamt 		if (error != 0)
    284  1.83.10.2      yamt 			break;
    285  1.83.10.2      yamt 		sysctl_teardown(&kernfs_sysctl_log);
    286  1.83.10.2      yamt 		break;
    287  1.83.10.1      yamt 	default:
    288  1.83.10.2      yamt 		error = ENOTTY;
    289  1.83.10.2      yamt 		break;
    290  1.83.10.1      yamt 	}
    291  1.83.10.2      yamt 
    292  1.83.10.2      yamt 	return (error);
    293  1.83.10.1      yamt }
    294