Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vfsops.c revision 1.101
      1  1.101       joe /*	$NetBSD: kernfs_vfsops.c,v 1.101 2025/02/16 18:38:58 joe 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.101       joe __KERNEL_RCSID(0, "$NetBSD: kernfs_vfsops.c,v 1.101 2025/02/16 18:38:58 joe 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.54    itojun #include <sys/syslog.h>
     58   1.71      elad #include <sys/kauth.h>
     59   1.85    rumble #include <sys/module.h>
     60   1.15   mycroft 
     61   1.83  dholland #include <miscfs/genfs/genfs.h>
     62   1.15   mycroft #include <miscfs/specfs/specdev.h>
     63    1.9   mycroft #include <miscfs/kernfs/kernfs.h>
     64    1.1       cgd 
     65   1.85    rumble MODULE(MODULE_CLASS_VFS, kernfs, NULL);
     66   1.85    rumble 
     67   1.15   mycroft dev_t rrootdev = NODEV;
     68   1.95   hannken kmutex_t kfs_lock;
     69   1.15   mycroft 
     70   1.81     pooka VFS_PROTOS(kernfs);
     71   1.81     pooka 
     72   1.68   xtraeme void	kernfs_get_rrootdev(void);
     73   1.25  christos 
     74   1.25  christos void
     75   1.89    cegger kernfs_init(void)
     76    1.1       cgd {
     77   1.77     pooka 
     78   1.95   hannken 	mutex_init(&kfs_lock, MUTEX_DEFAULT, IPL_NONE);
     79   1.54    itojun }
     80   1.54    itojun 
     81   1.54    itojun void
     82   1.89    cegger kernfs_reinit(void)
     83   1.54    itojun {
     84   1.95   hannken 
     85   1.10       cgd }
     86   1.10       cgd 
     87   1.11       cgd void
     88   1.89    cegger kernfs_done(void)
     89   1.36  jdolecek {
     90   1.77     pooka 
     91   1.95   hannken 	mutex_destroy(&kfs_lock);
     92   1.36  jdolecek }
     93   1.36  jdolecek 
     94   1.36  jdolecek void
     95   1.89    cegger kernfs_get_rrootdev(void)
     96   1.10       cgd {
     97   1.16   mycroft 	static int tried = 0;
     98   1.10       cgd 
     99   1.16   mycroft 	if (tried) {
    100   1.15   mycroft 		/* Already did it once. */
    101   1.15   mycroft 		return;
    102   1.15   mycroft 	}
    103   1.16   mycroft 	tried = 1;
    104   1.16   mycroft 
    105   1.16   mycroft 	if (rootdev == NODEV)
    106   1.16   mycroft 		return;
    107   1.45   gehenna 	rrootdev = devsw_blk2chr(rootdev);
    108   1.54    itojun 	if (rrootdev != NODEV)
    109   1.45   gehenna 		return;
    110   1.16   mycroft 	rrootdev = NODEV;
    111   1.28  christos 	printf("kernfs_get_rrootdev: no raw root device\n");
    112    1.1       cgd }
    113    1.1       cgd 
    114    1.1       cgd /*
    115   1.15   mycroft  * Mount the Kernel params filesystem
    116    1.1       cgd  */
    117   1.25  christos int
    118   1.82     pooka kernfs_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
    119    1.1       cgd {
    120   1.82     pooka 	struct lwp *l = curlwp;
    121    1.1       cgd 	int error = 0;
    122    1.1       cgd 	struct kernfs_mount *fmp;
    123    1.1       cgd 
    124   1.54    itojun 	if (UIO_MX & (UIO_MX - 1)) {
    125   1.54    itojun 		log(LOG_ERR, "kernfs: invalid directory entry size");
    126   1.54    itojun 		return (EINVAL);
    127   1.54    itojun 	}
    128    1.1       cgd 
    129   1.78       dsl 	if (mp->mnt_flag & MNT_GETARGS) {
    130   1.78       dsl 		*data_len = 0;
    131   1.46  christos 		return 0;
    132   1.78       dsl 	}
    133    1.1       cgd 	/*
    134    1.1       cgd 	 * Update is a no-op
    135    1.1       cgd 	 */
    136    1.1       cgd 	if (mp->mnt_flag & MNT_UPDATE)
    137    1.1       cgd 		return (EOPNOTSUPP);
    138    1.1       cgd 
    139  1.100  jdolecek 	fmp = kmem_zalloc(sizeof(struct kernfs_mount), KM_SLEEP);
    140   1.54    itojun 	TAILQ_INIT(&fmp->nodelist);
    141   1.54    itojun 
    142   1.91  christos 	mp->mnt_stat.f_namemax = KERNFS_MAXNAMLEN;
    143   1.65  jdolecek 	mp->mnt_flag |= MNT_LOCAL;
    144   1.54    itojun 	mp->mnt_data = fmp;
    145   1.37     assar 	vfs_getnewfsid(mp);
    146    1.1       cgd 
    147   1.64      tron 	if ((error = set_statvfs_info(path, UIO_USERSPACE, "kernfs",
    148   1.79     pooka 	    UIO_SYSSPACE, mp->mnt_op->vfs_name, mp, l)) != 0) {
    149  1.100  jdolecek 		kmem_free(fmp, sizeof(struct kernfs_mount));
    150   1.64      tron 		return error;
    151   1.64      tron 	}
    152   1.10       cgd 
    153   1.15   mycroft 	kernfs_get_rrootdev();
    154   1.64      tron 	return 0;
    155    1.1       cgd }
    156    1.1       cgd 
    157   1.25  christos int
    158   1.82     pooka kernfs_start(struct mount *mp, int flags)
    159    1.1       cgd {
    160   1.15   mycroft 
    161    1.1       cgd 	return (0);
    162    1.1       cgd }
    163    1.1       cgd 
    164   1.25  christos int
    165   1.82     pooka kernfs_unmount(struct mount *mp, int mntflags)
    166    1.1       cgd {
    167    1.1       cgd 	int error;
    168    1.1       cgd 	int flags = 0;
    169    1.1       cgd 
    170   1.54    itojun 	if (mntflags & MNT_FORCE)
    171    1.1       cgd 		flags |= FORCECLOSE;
    172    1.1       cgd 
    173   1.54    itojun 	if ((error = vflush(mp, 0, flags)) != 0)
    174    1.1       cgd 		return (error);
    175    1.1       cgd 
    176    1.1       cgd 	/*
    177    1.1       cgd 	 * Finally, throw away the kernfs_mount structure
    178    1.1       cgd 	 */
    179  1.100  jdolecek 	kmem_free(mp->mnt_data, sizeof(struct kernfs_mount));
    180   1.64      tron 	mp->mnt_data = NULL;
    181   1.15   mycroft 	return (0);
    182    1.1       cgd }
    183    1.1       cgd 
    184   1.25  christos int
    185   1.97        ad kernfs_root(struct mount *mp, int lktype, struct vnode **vpp)
    186    1.1       cgd {
    187   1.95   hannken 	const struct kern_target *root_target = &kern_targets[0];
    188   1.95   hannken 	int error;
    189    1.1       cgd 
    190   1.54    itojun 	/* setup "." */
    191   1.95   hannken 	error = vcache_get(mp, &root_target, sizeof(root_target), vpp);
    192   1.95   hannken 	if (error)
    193   1.95   hannken 		return error;
    194   1.97        ad 	error = vn_lock(*vpp, lktype);
    195   1.95   hannken 	if (error) {
    196   1.95   hannken 		vrele(*vpp);
    197   1.95   hannken 		*vpp = NULL;
    198   1.95   hannken 		return error;
    199   1.95   hannken 	}
    200   1.95   hannken 	return 0;
    201    1.1       cgd }
    202    1.1       cgd 
    203   1.25  christos /*ARGSUSED*/
    204   1.25  christos int
    205   1.74  christos kernfs_sync(struct mount *mp, int waitfor,
    206   1.82     pooka     kauth_cred_t uc)
    207    1.1       cgd {
    208   1.15   mycroft 
    209    1.1       cgd 	return (0);
    210    1.1       cgd }
    211    1.1       cgd 
    212   1.15   mycroft /*
    213   1.15   mycroft  * Kernfs flat namespace lookup.
    214   1.15   mycroft  * Currently unsupported.
    215   1.15   mycroft  */
    216   1.25  christos int
    217   1.97        ad kernfs_vget(struct mount *mp, ino_t ino, int lktype,
    218   1.74  christos     struct vnode **vpp)
    219   1.15   mycroft {
    220   1.15   mycroft 
    221   1.15   mycroft 	return (EOPNOTSUPP);
    222   1.15   mycroft }
    223   1.15   mycroft 
    224   1.95   hannken int
    225   1.95   hannken kernfs_loadvnode(struct mount *mp, struct vnode *vp,
    226   1.95   hannken     const void *key, size_t key_len, const void **new_key)
    227   1.95   hannken {
    228   1.95   hannken 	const struct kern_target *kt;
    229   1.95   hannken 	struct kernfs_node *kfs, *kfsp;
    230   1.95   hannken 	long *cookie;
    231   1.95   hannken 
    232   1.95   hannken 	KASSERT(key_len == sizeof(kt));
    233   1.95   hannken 	memcpy(&kt, key, key_len);
    234   1.95   hannken 
    235   1.95   hannken 	kfs = kmem_zalloc(sizeof(struct kernfs_node), KM_SLEEP);
    236   1.95   hannken 	cookie = &(VFSTOKERNFS(mp)->fileno_cookie);
    237   1.95   hannken 	mutex_enter(&kfs_lock);
    238   1.95   hannken again:
    239   1.95   hannken 	TAILQ_FOREACH(kfsp, &VFSTOKERNFS(mp)->nodelist, kfs_list) {
    240   1.95   hannken 		if (kfsp->kfs_cookie == *cookie) {
    241   1.95   hannken 			(*cookie) ++;
    242   1.95   hannken 			goto again;
    243   1.95   hannken 		}
    244   1.95   hannken 		if (TAILQ_NEXT(kfsp, kfs_list)) {
    245   1.95   hannken 			if (kfsp->kfs_cookie < *cookie &&
    246   1.95   hannken 			    *cookie < TAILQ_NEXT(kfsp, kfs_list)->kfs_cookie)
    247   1.95   hannken 				break;
    248   1.95   hannken 			if (kfsp->kfs_cookie + 1 <
    249   1.95   hannken 			    TAILQ_NEXT(kfsp, kfs_list)->kfs_cookie) {
    250   1.95   hannken 				*cookie = kfsp->kfs_cookie + 1;
    251   1.95   hannken 				break;
    252   1.95   hannken 			}
    253   1.95   hannken 		}
    254   1.95   hannken 	}
    255   1.95   hannken 
    256   1.95   hannken 	kfs->kfs_cookie = *cookie;
    257   1.95   hannken 
    258   1.95   hannken 	if (kfsp)
    259   1.95   hannken 		TAILQ_INSERT_AFTER(&VFSTOKERNFS(mp)->nodelist, kfsp, kfs,
    260   1.95   hannken 		    kfs_list);
    261   1.95   hannken 	else
    262   1.95   hannken 		TAILQ_INSERT_TAIL(&VFSTOKERNFS(mp)->nodelist, kfs, kfs_list);
    263   1.95   hannken 
    264   1.95   hannken 	kfs->kfs_type = kt->kt_tag;
    265   1.95   hannken 	kfs->kfs_vnode = vp;
    266   1.95   hannken 	kfs->kfs_fileno = KERNFS_FILENO(kt, kt->kt_tag, kfs->kfs_cookie);
    267   1.95   hannken 	kfs->kfs_kt = kt;
    268   1.95   hannken 	kfs->kfs_mode = kt->kt_mode;
    269   1.95   hannken 	vp->v_tag = VT_KERNFS;
    270   1.95   hannken 	vp->v_op = kernfs_vnodeop_p;
    271   1.95   hannken 	vp->v_data = kfs;
    272   1.95   hannken 	vp->v_type = kt->kt_vtype;
    273   1.95   hannken 	mutex_exit(&kfs_lock);
    274   1.95   hannken 
    275   1.95   hannken 	if (kt->kt_tag == KFSkern)
    276   1.95   hannken 		vp->v_vflag = VV_ROOT;
    277   1.95   hannken 
    278   1.95   hannken 	if (kt->kt_tag == KFSdevice) {
    279   1.98  riastrad 		vp->v_op = kernfs_specop_p;
    280   1.95   hannken 		spec_node_init(vp, *(dev_t *)kt->kt_data);
    281   1.95   hannken 	}
    282   1.95   hannken 
    283   1.95   hannken 	uvm_vnp_setsize(vp, 0);
    284   1.95   hannken 
    285   1.95   hannken 	*new_key = &kfs->kfs_kt;
    286   1.95   hannken 	return 0;
    287   1.95   hannken }
    288   1.95   hannken 
    289   1.38  jdolecek extern const struct vnodeopv_desc kernfs_vnodeop_opv_desc;
    290   1.98  riastrad extern const struct vnodeopv_desc kernfs_specop_opv_desc;
    291   1.31   thorpej 
    292   1.38  jdolecek const struct vnodeopv_desc * const kernfs_vnodeopv_descs[] = {
    293   1.31   thorpej 	&kernfs_vnodeop_opv_desc,
    294   1.98  riastrad 	&kernfs_specop_opv_desc,
    295   1.31   thorpej 	NULL,
    296   1.31   thorpej };
    297   1.31   thorpej 
    298    1.1       cgd struct vfsops kernfs_vfsops = {
    299   1.93   hannken 	.vfs_name = MOUNT_KERNFS,
    300   1.93   hannken 	.vfs_min_mount_data = 0,
    301   1.93   hannken 	.vfs_mount = kernfs_mount,
    302   1.93   hannken 	.vfs_start = kernfs_start,
    303   1.93   hannken 	.vfs_unmount = kernfs_unmount,
    304   1.93   hannken 	.vfs_root = kernfs_root,
    305   1.93   hannken 	.vfs_quotactl = (void *)eopnotsupp,
    306   1.93   hannken 	.vfs_statvfs = genfs_statvfs,
    307   1.93   hannken 	.vfs_sync = kernfs_sync,
    308   1.93   hannken 	.vfs_vget = kernfs_vget,
    309   1.95   hannken 	.vfs_loadvnode = kernfs_loadvnode,
    310   1.93   hannken 	.vfs_fhtovp = (void *)eopnotsupp,
    311   1.93   hannken 	.vfs_vptofh = (void *)eopnotsupp,
    312   1.93   hannken 	.vfs_init = kernfs_init,
    313   1.93   hannken 	.vfs_reinit = kernfs_reinit,
    314   1.93   hannken 	.vfs_done = kernfs_done,
    315   1.93   hannken 	.vfs_snapshot = (void *)eopnotsupp,
    316   1.93   hannken 	.vfs_extattrctl = vfs_stdextattrctl,
    317   1.96   hannken 	.vfs_suspendctl = genfs_suspendctl,
    318   1.93   hannken 	.vfs_renamelock_enter = genfs_renamelock_enter,
    319   1.93   hannken 	.vfs_renamelock_exit = genfs_renamelock_exit,
    320   1.93   hannken 	.vfs_fsync = (void *)eopnotsupp,
    321   1.93   hannken 	.vfs_opv_descs = kernfs_vnodeopv_descs
    322    1.1       cgd };
    323   1.85    rumble 
    324   1.99  pgoyette SYSCTL_SETUP(kernfs_sysctl_setup, "kernfs sysctl")
    325   1.99  pgoyette {
    326   1.99  pgoyette 
    327   1.99  pgoyette 	sysctl_createv(clog, 0, NULL, NULL,
    328   1.99  pgoyette 		       CTLFLAG_PERMANENT,
    329   1.99  pgoyette 		       CTLTYPE_NODE, "kernfs",
    330   1.99  pgoyette 		       SYSCTL_DESCR("/kern file system"),
    331   1.99  pgoyette 		       NULL, 0, NULL, 0,
    332   1.99  pgoyette 		       CTL_VFS, 11, CTL_EOL);
    333   1.99  pgoyette 	/*
    334   1.99  pgoyette 	 * XXX the "11" above could be dynamic, thereby eliminating one
    335   1.99  pgoyette 	 * more instance of the "number to vfs" mapping problem, but
    336   1.99  pgoyette 	 * "11" is the order as taken from sys/mount.h
    337   1.99  pgoyette 	 */
    338   1.99  pgoyette }
    339   1.99  pgoyette 
    340   1.85    rumble static int
    341   1.85    rumble kernfs_modcmd(modcmd_t cmd, void *arg)
    342   1.85    rumble {
    343   1.86    rumble 	int error;
    344   1.85    rumble 
    345   1.85    rumble 	switch (cmd) {
    346   1.85    rumble 	case MODULE_CMD_INIT:
    347   1.86    rumble 		error = vfs_attach(&kernfs_vfsops);
    348   1.86    rumble 		break;
    349   1.85    rumble 	case MODULE_CMD_FINI:
    350   1.86    rumble 		error = vfs_detach(&kernfs_vfsops);
    351   1.86    rumble 		break;
    352   1.85    rumble 	default:
    353   1.86    rumble 		error = ENOTTY;
    354   1.86    rumble 		break;
    355   1.85    rumble 	}
    356   1.86    rumble 
    357   1.86    rumble 	return (error);
    358   1.85    rumble }
    359