Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vnops.c revision 1.43
      1  1.43  christos /*	$NetBSD: kernfs_vnops.c,v 1.43 1996/03/16 23:52:47 christos Exp $	*/
      2  1.27       cgd 
      3   1.1       cgd /*
      4  1.23   mycroft  * Copyright (c) 1992, 1993
      5  1.23   mycroft  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7  1.17       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.2       cgd  * 3. All advertising materials mentioning features or use of this software
     19   1.2       cgd  *    must display the following acknowledgement:
     20  1.17       cgd  *	This product includes software developed by the University of
     21  1.17       cgd  *	California, Berkeley and its contributors.
     22   1.2       cgd  * 4. Neither the name of the University nor the names of its contributors
     23   1.2       cgd  *    may be used to endorse or promote products derived from this software
     24   1.2       cgd  *    without specific prior written permission.
     25   1.1       cgd  *
     26   1.2       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.2       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.2       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.2       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.2       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.2       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.2       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.2       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.2       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.2       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.2       cgd  * SUCH DAMAGE.
     37   1.1       cgd  *
     38  1.27       cgd  *	@(#)kernfs_vnops.c	8.9 (Berkeley) 6/15/94
     39   1.1       cgd  */
     40   1.1       cgd 
     41   1.1       cgd /*
     42  1.23   mycroft  * Kernel parameter filesystem (/kern)
     43   1.1       cgd  */
     44   1.1       cgd 
     45  1.14   mycroft #include <sys/param.h>
     46  1.14   mycroft #include <sys/systm.h>
     47  1.14   mycroft #include <sys/kernel.h>
     48  1.23   mycroft #include <sys/vmmeter.h>
     49  1.14   mycroft #include <sys/types.h>
     50  1.14   mycroft #include <sys/time.h>
     51  1.14   mycroft #include <sys/proc.h>
     52  1.23   mycroft #include <sys/vnode.h>
     53  1.23   mycroft #include <sys/malloc.h>
     54  1.14   mycroft #include <sys/file.h>
     55  1.14   mycroft #include <sys/stat.h>
     56  1.14   mycroft #include <sys/mount.h>
     57  1.14   mycroft #include <sys/namei.h>
     58  1.14   mycroft #include <sys/buf.h>
     59  1.23   mycroft #include <sys/dirent.h>
     60  1.28   mycroft #include <sys/msgbuf.h>
     61  1.17       cgd #include <miscfs/kernfs/kernfs.h>
     62   1.1       cgd 
     63  1.17       cgd #define KSTRING	256		/* Largest I/O available via this filesystem */
     64  1.17       cgd #define	UIO_MX 32
     65   1.1       cgd 
     66  1.23   mycroft #define	READ_MODE	(S_IRUSR|S_IRGRP|S_IROTH)
     67  1.23   mycroft #define	WRITE_MODE	(S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)
     68  1.23   mycroft #define DIR_MODE	(S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
     69  1.23   mycroft 
     70  1.17       cgd struct kern_target {
     71  1.23   mycroft 	u_char kt_type;
     72  1.23   mycroft 	u_char kt_namlen;
     73  1.17       cgd 	char *kt_name;
     74  1.17       cgd 	void *kt_data;
     75  1.23   mycroft #define	KTT_NULL	 1
     76  1.23   mycroft #define	KTT_TIME	 5
     77  1.23   mycroft #define KTT_INT		17
     78  1.23   mycroft #define	KTT_STRING	31
     79  1.23   mycroft #define KTT_HOSTNAME	47
     80  1.23   mycroft #define KTT_AVENRUN	53
     81  1.23   mycroft #define KTT_DEVICE	71
     82  1.28   mycroft #define	KTT_MSGBUF	89
     83  1.23   mycroft 	u_char kt_tag;
     84  1.23   mycroft 	u_char kt_vtype;
     85  1.23   mycroft 	mode_t kt_mode;
     86  1.17       cgd } kern_targets[] = {
     87   1.1       cgd /* NOTE: The name must be less than UIO_MX-16 chars in length */
     88  1.23   mycroft #define N(s) sizeof(s)-1, s
     89  1.23   mycroft      /*        name            data          tag           type  ro/rw */
     90  1.23   mycroft      { DT_DIR, N("."),         0,            KTT_NULL,     VDIR, DIR_MODE   },
     91  1.23   mycroft      { DT_DIR, N(".."),        0,            KTT_NULL,     VDIR, DIR_MODE   },
     92  1.23   mycroft      { DT_REG, N("boottime"),  &boottime.tv_sec, KTT_INT,  VREG, READ_MODE  },
     93  1.23   mycroft      { DT_REG, N("copyright"), copyright,    KTT_STRING,   VREG, READ_MODE  },
     94  1.23   mycroft      { DT_REG, N("hostname"),  0,            KTT_HOSTNAME, VREG, WRITE_MODE },
     95  1.23   mycroft      { DT_REG, N("hz"),        &hz,          KTT_INT,      VREG, READ_MODE  },
     96  1.23   mycroft      { DT_REG, N("loadavg"),   0,            KTT_AVENRUN,  VREG, READ_MODE  },
     97  1.28   mycroft      { DT_REG, N("msgbuf"),    0,	     KTT_MSGBUF,   VREG, READ_MODE  },
     98  1.23   mycroft      { DT_REG, N("pagesize"),  &cnt.v_page_size, KTT_INT,  VREG, READ_MODE  },
     99  1.23   mycroft      { DT_REG, N("physmem"),   &physmem,     KTT_INT,      VREG, READ_MODE  },
    100  1.17       cgd #if 0
    101  1.23   mycroft      { DT_DIR, N("root"),      0,            KTT_NULL,     VDIR, DIR_MODE   },
    102  1.17       cgd #endif
    103  1.23   mycroft      { DT_BLK, N("rootdev"),   &rootdev,     KTT_DEVICE,   VBLK, READ_MODE  },
    104  1.23   mycroft      { DT_CHR, N("rrootdev"),  &rrootdev,    KTT_DEVICE,   VCHR, READ_MODE  },
    105  1.23   mycroft      { DT_REG, N("time"),      0,            KTT_TIME,     VREG, READ_MODE  },
    106  1.23   mycroft      { DT_REG, N("version"),   version,      KTT_STRING,   VREG, READ_MODE  },
    107  1.23   mycroft #undef N
    108   1.1       cgd };
    109  1.17       cgd static int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);
    110   1.1       cgd 
    111  1.41  christos int	kernfs_badop	__P((void *));
    112  1.42   mycroft int	kernfs_enotsupp __P((void *));
    113  1.42   mycroft 
    114  1.41  christos int	kernfs_lookup	__P((void *));
    115  1.41  christos #define	kernfs_create	kernfs_enotsupp
    116  1.41  christos #define	kernfs_mknod	kernfs_enotsupp
    117  1.41  christos int	kernfs_open	__P((void *));
    118  1.41  christos #define	kernfs_close	nullop
    119  1.41  christos int	kernfs_access	__P((void *));
    120  1.41  christos int	kernfs_getattr	__P((void *));
    121  1.41  christos int	kernfs_setattr	__P((void *));
    122  1.41  christos int	kernfs_read	__P((void *));
    123  1.41  christos int	kernfs_write	__P((void *));
    124  1.41  christos #define	kernfs_ioctl	kernfs_enotsupp
    125  1.41  christos #define	kernfs_select	kernfs_enotsupp
    126  1.41  christos #define	kernfs_mmap	kernfs_enotsupp
    127  1.41  christos #define	kernfs_fsync	nullop
    128  1.41  christos #define	kernfs_seek	nullop
    129  1.41  christos #define	kernfs_remove	kernfs_enotsupp
    130  1.41  christos int	kernfs_link	__P((void *));
    131  1.41  christos #define	kernfs_rename	kernfs_enotsupp
    132  1.41  christos #define	kernfs_mkdir	kernfs_enotsupp
    133  1.41  christos #define	kernfs_rmdir	kernfs_enotsupp
    134  1.41  christos int	kernfs_symlink	__P((void *));
    135  1.41  christos int	kernfs_readdir	__P((void *));
    136  1.41  christos #define	kernfs_readlink	kernfs_enotsupp
    137  1.41  christos int	kernfs_abortop	__P((void *));
    138  1.41  christos int	kernfs_inactive	__P((void *));
    139  1.41  christos int	kernfs_reclaim	__P((void *));
    140  1.41  christos #define	kernfs_lock	nullop
    141  1.41  christos #define	kernfs_unlock	nullop
    142  1.41  christos #define	kernfs_bmap	kernfs_badop
    143  1.41  christos #define	kernfs_strategy	kernfs_badop
    144  1.41  christos int	kernfs_print	__P((void *));
    145  1.41  christos #define	kernfs_islocked	nullop
    146  1.41  christos int	kernfs_pathconf	__P((void *));
    147  1.41  christos #define	kernfs_advlock	kernfs_enotsupp
    148  1.41  christos #define	kernfs_blkatoff	kernfs_enotsupp
    149  1.41  christos #define	kernfs_valloc	kernfs_enotsupp
    150  1.41  christos int	kernfs_vfree	__P((void *));
    151  1.41  christos #define	kernfs_truncate	kernfs_enotsupp
    152  1.41  christos #define	kernfs_update	kernfs_enotsupp
    153  1.41  christos #define	kernfs_bwrite	kernfs_enotsupp
    154  1.41  christos 
    155  1.41  christos int	kernfs_xread __P((struct kern_target *, int, char **, int));
    156  1.41  christos int	kernfs_xwrite __P((struct kern_target *, char *, int));
    157  1.41  christos 
    158  1.41  christos int (**kernfs_vnodeop_p) __P((void *));
    159  1.41  christos struct vnodeopv_entry_desc kernfs_vnodeop_entries[] = {
    160  1.41  christos 	{ &vop_default_desc, vn_default_error },
    161  1.41  christos 	{ &vop_lookup_desc, kernfs_lookup },	/* lookup */
    162  1.41  christos 	{ &vop_create_desc, kernfs_create },	/* create */
    163  1.41  christos 	{ &vop_mknod_desc, kernfs_mknod },	/* mknod */
    164  1.41  christos 	{ &vop_open_desc, kernfs_open },	/* open */
    165  1.41  christos 	{ &vop_close_desc, kernfs_close },	/* close */
    166  1.41  christos 	{ &vop_access_desc, kernfs_access },	/* access */
    167  1.41  christos 	{ &vop_getattr_desc, kernfs_getattr },	/* getattr */
    168  1.41  christos 	{ &vop_setattr_desc, kernfs_setattr },	/* setattr */
    169  1.41  christos 	{ &vop_read_desc, kernfs_read },	/* read */
    170  1.41  christos 	{ &vop_write_desc, kernfs_write },	/* write */
    171  1.41  christos 	{ &vop_ioctl_desc, kernfs_ioctl },	/* ioctl */
    172  1.41  christos 	{ &vop_select_desc, kernfs_select },	/* select */
    173  1.41  christos 	{ &vop_mmap_desc, kernfs_mmap },	/* mmap */
    174  1.41  christos 	{ &vop_fsync_desc, kernfs_fsync },	/* fsync */
    175  1.41  christos 	{ &vop_seek_desc, kernfs_seek },	/* seek */
    176  1.41  christos 	{ &vop_remove_desc, kernfs_remove },	/* remove */
    177  1.41  christos 	{ &vop_link_desc, kernfs_link },	/* link */
    178  1.41  christos 	{ &vop_rename_desc, kernfs_rename },	/* rename */
    179  1.41  christos 	{ &vop_mkdir_desc, kernfs_mkdir },	/* mkdir */
    180  1.41  christos 	{ &vop_rmdir_desc, kernfs_rmdir },	/* rmdir */
    181  1.41  christos 	{ &vop_symlink_desc, kernfs_symlink },	/* symlink */
    182  1.41  christos 	{ &vop_readdir_desc, kernfs_readdir },	/* readdir */
    183  1.41  christos 	{ &vop_readlink_desc, kernfs_readlink },/* readlink */
    184  1.41  christos 	{ &vop_abortop_desc, kernfs_abortop },	/* abortop */
    185  1.41  christos 	{ &vop_inactive_desc, kernfs_inactive },/* inactive */
    186  1.41  christos 	{ &vop_reclaim_desc, kernfs_reclaim },	/* reclaim */
    187  1.41  christos 	{ &vop_lock_desc, kernfs_lock },	/* lock */
    188  1.41  christos 	{ &vop_unlock_desc, kernfs_unlock },	/* unlock */
    189  1.41  christos 	{ &vop_bmap_desc, kernfs_bmap },	/* bmap */
    190  1.41  christos 	{ &vop_strategy_desc, kernfs_strategy },/* strategy */
    191  1.41  christos 	{ &vop_print_desc, kernfs_print },	/* print */
    192  1.41  christos 	{ &vop_islocked_desc, kernfs_islocked },/* islocked */
    193  1.41  christos 	{ &vop_pathconf_desc, kernfs_pathconf },/* pathconf */
    194  1.41  christos 	{ &vop_advlock_desc, kernfs_advlock },	/* advlock */
    195  1.41  christos 	{ &vop_blkatoff_desc, kernfs_blkatoff },/* blkatoff */
    196  1.41  christos 	{ &vop_valloc_desc, kernfs_valloc },	/* valloc */
    197  1.41  christos 	{ &vop_vfree_desc, kernfs_vfree },	/* vfree */
    198  1.41  christos 	{ &vop_truncate_desc, kernfs_truncate },/* truncate */
    199  1.41  christos 	{ &vop_update_desc, kernfs_update },	/* update */
    200  1.41  christos 	{ &vop_bwrite_desc, kernfs_bwrite },	/* bwrite */
    201  1.41  christos 	{ (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
    202  1.41  christos };
    203  1.41  christos struct vnodeopv_desc kernfs_vnodeop_opv_desc =
    204  1.41  christos 	{ &kernfs_vnodeop_p, kernfs_vnodeop_entries };
    205  1.41  christos 
    206  1.28   mycroft int
    207  1.28   mycroft kernfs_xread(kt, off, bufp, len)
    208  1.17       cgd 	struct kern_target *kt;
    209  1.28   mycroft 	int off;
    210  1.28   mycroft 	char **bufp;
    211   1.1       cgd 	int len;
    212   1.1       cgd {
    213   1.1       cgd 
    214   1.1       cgd 	switch (kt->kt_tag) {
    215   1.1       cgd 	case KTT_TIME: {
    216   1.1       cgd 		struct timeval tv;
    217  1.28   mycroft 
    218   1.1       cgd 		microtime(&tv);
    219  1.43  christos 		sprintf(*bufp, "%ld %ld\n", tv.tv_sec, tv.tv_usec);
    220   1.1       cgd 		break;
    221   1.1       cgd 	}
    222   1.1       cgd 
    223   1.1       cgd 	case KTT_INT: {
    224   1.1       cgd 		int *ip = kt->kt_data;
    225  1.28   mycroft 
    226  1.28   mycroft 		sprintf(*bufp, "%d\n", *ip);
    227   1.1       cgd 		break;
    228   1.1       cgd 	}
    229   1.1       cgd 
    230   1.1       cgd 	case KTT_STRING: {
    231   1.1       cgd 		char *cp = kt->kt_data;
    232   1.1       cgd 
    233  1.28   mycroft 		*bufp = cp;
    234  1.28   mycroft 		break;
    235  1.28   mycroft 	}
    236   1.1       cgd 
    237  1.28   mycroft 	case KTT_MSGBUF: {
    238  1.28   mycroft 		extern struct msgbuf *msgbufp;
    239  1.28   mycroft 		long n;
    240  1.28   mycroft 
    241  1.28   mycroft 		if (off >= MSG_BSIZE)
    242  1.28   mycroft 			return (0);
    243  1.28   mycroft 		n = msgbufp->msg_bufx + off;
    244  1.28   mycroft 		if (n >= MSG_BSIZE)
    245  1.28   mycroft 			n -= MSG_BSIZE;
    246  1.28   mycroft 		len = min(MSG_BSIZE - n, MSG_BSIZE - off);
    247  1.28   mycroft 		*bufp = msgbufp->msg_bufc + n;
    248  1.28   mycroft 		return (len);
    249   1.1       cgd 	}
    250   1.1       cgd 
    251   1.1       cgd 	case KTT_HOSTNAME: {
    252   1.1       cgd 		char *cp = hostname;
    253   1.1       cgd 		int xlen = hostnamelen;
    254   1.1       cgd 
    255  1.17       cgd 		if (xlen >= (len-2))
    256   1.1       cgd 			return (EINVAL);
    257   1.1       cgd 
    258  1.28   mycroft 		bcopy(cp, *bufp, xlen);
    259  1.28   mycroft 		(*bufp)[xlen] = '\n';
    260  1.28   mycroft 		(*bufp)[xlen+1] = '\0';
    261   1.1       cgd 		break;
    262   1.1       cgd 	}
    263   1.1       cgd 
    264   1.1       cgd 	case KTT_AVENRUN:
    265  1.31   mycroft 		averunnable.fscale = FSCALE;
    266  1.43  christos 		sprintf(*bufp, "%d %d %d %ld\n",
    267  1.23   mycroft 		    averunnable.ldavg[0], averunnable.ldavg[1],
    268  1.23   mycroft 		    averunnable.ldavg[2], averunnable.fscale);
    269   1.1       cgd 		break;
    270   1.1       cgd 
    271   1.1       cgd 	default:
    272  1.28   mycroft 		return (0);
    273   1.1       cgd 	}
    274   1.1       cgd 
    275  1.28   mycroft 	len = strlen(*bufp);
    276  1.28   mycroft 	if (len <= off)
    277  1.28   mycroft 		return (0);
    278  1.28   mycroft 	*bufp += off;
    279  1.28   mycroft 	return (len - off);
    280   1.1       cgd }
    281   1.1       cgd 
    282  1.28   mycroft int
    283   1.1       cgd kernfs_xwrite(kt, buf, len)
    284  1.17       cgd 	struct kern_target *kt;
    285   1.1       cgd 	char *buf;
    286   1.1       cgd 	int len;
    287   1.1       cgd {
    288  1.23   mycroft 
    289   1.1       cgd 	switch (kt->kt_tag) {
    290  1.23   mycroft 	case KTT_HOSTNAME:
    291   1.1       cgd 		if (buf[len-1] == '\n')
    292   1.1       cgd 			--len;
    293   1.1       cgd 		bcopy(buf, hostname, len);
    294  1.17       cgd 		hostname[len] = '\0';
    295   1.6       cgd 		hostnamelen = len;
    296   1.1       cgd 		return (0);
    297   1.1       cgd 
    298   1.1       cgd 	default:
    299   1.1       cgd 		return (EIO);
    300   1.1       cgd 	}
    301   1.1       cgd }
    302   1.1       cgd 
    303  1.17       cgd 
    304  1.17       cgd /*
    305   1.1       cgd  * vp is the current namei directory
    306   1.1       cgd  * ndp is the name to locate in that directory...
    307   1.1       cgd  */
    308  1.41  christos int
    309  1.41  christos kernfs_lookup(v)
    310  1.41  christos 	void *v;
    311  1.41  christos {
    312  1.23   mycroft 	struct vop_lookup_args /* {
    313  1.23   mycroft 		struct vnode * a_dvp;
    314  1.23   mycroft 		struct vnode ** a_vpp;
    315  1.23   mycroft 		struct componentname * a_cnp;
    316  1.41  christos 	} */ *ap = v;
    317  1.23   mycroft 	struct componentname *cnp = ap->a_cnp;
    318  1.23   mycroft 	struct vnode **vpp = ap->a_vpp;
    319  1.23   mycroft 	struct vnode *dvp = ap->a_dvp;
    320  1.23   mycroft 	char *pname = cnp->cn_nameptr;
    321  1.23   mycroft 	struct kern_target *kt;
    322   1.1       cgd 	struct vnode *fvp;
    323  1.23   mycroft 	int error, i;
    324   1.1       cgd 
    325   1.1       cgd #ifdef KERNFS_DIAGNOSTIC
    326  1.23   mycroft 	printf("kernfs_lookup(%x)\n", ap);
    327  1.23   mycroft 	printf("kernfs_lookup(dp = %x, vpp = %x, cnp = %x)\n", dvp, vpp, ap->a_cnp);
    328   1.1       cgd 	printf("kernfs_lookup(%s)\n", pname);
    329   1.1       cgd #endif
    330  1.23   mycroft 
    331  1.35   mycroft 	*vpp = NULLVP;
    332  1.35   mycroft 
    333  1.35   mycroft 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    334  1.35   mycroft 		return (EROFS);
    335  1.35   mycroft 
    336  1.23   mycroft 	if (cnp->cn_namelen == 1 && *pname == '.') {
    337  1.23   mycroft 		*vpp = dvp;
    338   1.1       cgd 		VREF(dvp);
    339   1.1       cgd 		/*VOP_LOCK(dvp);*/
    340   1.1       cgd 		return (0);
    341   1.1       cgd 	}
    342  1.13       cgd 
    343  1.17       cgd #if 0
    344  1.23   mycroft 	if (cnp->cn_namelen == 4 && bcmp(pname, "root", 4) == 0) {
    345  1.23   mycroft 		*vpp = rootdir;
    346  1.17       cgd 		VREF(rootdir);
    347   1.1       cgd 		VOP_LOCK(rootdir);
    348   1.1       cgd 		return (0);
    349   1.1       cgd 	}
    350  1.13       cgd #endif
    351  1.25   mycroft 
    352  1.35   mycroft 	for (kt = kern_targets, i = 0; i < nkern_targets; kt++, i++) {
    353  1.26   mycroft 		if (cnp->cn_namelen == kt->kt_namlen &&
    354  1.35   mycroft 		    bcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
    355  1.35   mycroft 			goto found;
    356   1.1       cgd 	}
    357   1.1       cgd 
    358   1.1       cgd #ifdef KERNFS_DIAGNOSTIC
    359  1.35   mycroft 	printf("kernfs_lookup: i = %d, failed", i);
    360   1.1       cgd #endif
    361   1.1       cgd 
    362  1.35   mycroft 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
    363  1.23   mycroft 
    364  1.35   mycroft found:
    365  1.23   mycroft 	if (kt->kt_tag == KTT_DEVICE) {
    366  1.23   mycroft 		dev_t *dp = kt->kt_data;
    367  1.24   mycroft 	loop:
    368  1.23   mycroft 		if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp))
    369  1.24   mycroft 			return (ENOENT);
    370  1.23   mycroft 		*vpp = fvp;
    371  1.24   mycroft 		if (vget(fvp, 1))
    372  1.24   mycroft 			goto loop;
    373  1.23   mycroft 		return (0);
    374  1.23   mycroft 	}
    375   1.1       cgd 
    376   1.1       cgd #ifdef KERNFS_DIAGNOSTIC
    377   1.1       cgd 	printf("kernfs_lookup: allocate new vnode\n");
    378   1.1       cgd #endif
    379  1.41  christos 	error = getnewvnode(VT_KERNFS, dvp->v_mount, kernfs_vnodeop_p, &fvp);
    380  1.41  christos 	if (error)
    381  1.23   mycroft 		return (error);
    382  1.23   mycroft 
    383  1.23   mycroft 	MALLOC(fvp->v_data, void *, sizeof(struct kernfs_node), M_TEMP,
    384  1.23   mycroft 	    M_WAITOK);
    385  1.23   mycroft 	VTOKERN(fvp)->kf_kt = kt;
    386  1.23   mycroft 	fvp->v_type = kt->kt_vtype;
    387  1.23   mycroft 	*vpp = fvp;
    388  1.23   mycroft 
    389   1.1       cgd #ifdef KERNFS_DIAGNOSTIC
    390   1.1       cgd 	printf("kernfs_lookup: newvp = %x\n", fvp);
    391   1.1       cgd #endif
    392   1.1       cgd 	return (0);
    393   1.1       cgd }
    394   1.1       cgd 
    395  1.41  christos /*ARGSUSED*/
    396  1.41  christos int
    397  1.41  christos kernfs_open(v)
    398  1.41  christos 	void *v;
    399   1.1       cgd {
    400  1.23   mycroft 	/* Only need to check access permissions. */
    401  1.23   mycroft 	return (0);
    402  1.23   mycroft }
    403   1.1       cgd 
    404  1.28   mycroft int
    405  1.41  christos kernfs_access(v)
    406  1.41  christos 	void *v;
    407  1.41  christos {
    408  1.23   mycroft 	struct vop_access_args /* {
    409  1.23   mycroft 		struct vnode *a_vp;
    410  1.34   mycroft 		int a_mode;
    411  1.23   mycroft 		struct ucred *a_cred;
    412  1.23   mycroft 		struct proc *a_p;
    413  1.41  christos 	} */ *ap = v;
    414  1.26   mycroft 	struct vnode *vp = ap->a_vp;
    415  1.23   mycroft 	mode_t fmode =
    416  1.23   mycroft 	    (vp->v_flag & VROOT) ? DIR_MODE : VTOKERN(vp)->kf_kt->kt_mode;
    417  1.17       cgd 
    418  1.33        ws 	return (vaccess(fmode, (uid_t)0, (gid_t)0, ap->a_mode, ap->a_cred));
    419  1.23   mycroft }
    420  1.23   mycroft 
    421  1.41  christos int
    422  1.41  christos kernfs_getattr(v)
    423  1.41  christos 	void *v;
    424  1.41  christos {
    425  1.23   mycroft 	struct vop_getattr_args /* {
    426  1.23   mycroft 		struct vnode *a_vp;
    427  1.23   mycroft 		struct vattr *a_vap;
    428  1.23   mycroft 		struct ucred *a_cred;
    429  1.23   mycroft 		struct proc *a_p;
    430  1.41  christos 	} */ *ap = v;
    431  1.23   mycroft 	struct vnode *vp = ap->a_vp;
    432  1.23   mycroft 	struct vattr *vap = ap->a_vap;
    433  1.36       cgd 	struct timeval tv;
    434   1.1       cgd 	int error = 0;
    435  1.28   mycroft 	char strbuf[KSTRING], *buf;
    436   1.1       cgd 
    437   1.1       cgd 	bzero((caddr_t) vap, sizeof(*vap));
    438   1.1       cgd 	vattr_null(vap);
    439  1.17       cgd 	vap->va_uid = 0;
    440  1.17       cgd 	vap->va_gid = 0;
    441   1.1       cgd 	vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
    442  1.23   mycroft 	vap->va_size = 0;
    443   1.1       cgd 	vap->va_blocksize = DEV_BSIZE;
    444  1.36       cgd 	microtime(&tv);
    445  1.36       cgd 	TIMEVAL_TO_TIMESPEC(&tv, &vap->va_atime);
    446   1.1       cgd 	vap->va_mtime = vap->va_atime;
    447   1.1       cgd 	vap->va_ctime = vap->va_ctime;
    448   1.1       cgd 	vap->va_gen = 0;
    449   1.1       cgd 	vap->va_flags = 0;
    450   1.1       cgd 	vap->va_rdev = 0;
    451   1.1       cgd 	vap->va_bytes = 0;
    452   1.1       cgd 
    453   1.1       cgd 	if (vp->v_flag & VROOT) {
    454   1.1       cgd #ifdef KERNFS_DIAGNOSTIC
    455   1.1       cgd 		printf("kernfs_getattr: stat rootdir\n");
    456   1.1       cgd #endif
    457  1.17       cgd 		vap->va_type = VDIR;
    458  1.23   mycroft 		vap->va_mode = DIR_MODE;
    459   1.1       cgd 		vap->va_nlink = 2;
    460   1.1       cgd 		vap->va_fileid = 2;
    461   1.1       cgd 		vap->va_size = DEV_BSIZE;
    462   1.1       cgd 	} else {
    463  1.23   mycroft 		struct kern_target *kt = VTOKERN(vp)->kf_kt;
    464  1.28   mycroft 		int nbytes, total;
    465   1.1       cgd #ifdef KERNFS_DIAGNOSTIC
    466   1.1       cgd 		printf("kernfs_getattr: stat target %s\n", kt->kt_name);
    467   1.1       cgd #endif
    468  1.17       cgd 		vap->va_type = kt->kt_vtype;
    469  1.23   mycroft 		vap->va_mode = kt->kt_mode;
    470   1.1       cgd 		vap->va_nlink = 1;
    471  1.39   mycroft 		vap->va_fileid = 3 + (kt - kern_targets);
    472  1.28   mycroft 		total = 0;
    473  1.28   mycroft 		while (buf = strbuf,
    474  1.28   mycroft 		       nbytes = kernfs_xread(kt, total, &buf, sizeof(strbuf)))
    475  1.28   mycroft 			total += nbytes;
    476  1.28   mycroft 		vap->va_size = total;
    477   1.1       cgd 	}
    478   1.1       cgd 
    479   1.1       cgd #ifdef KERNFS_DIAGNOSTIC
    480   1.1       cgd 	printf("kernfs_getattr: return error %d\n", error);
    481   1.1       cgd #endif
    482   1.1       cgd 	return (error);
    483   1.1       cgd }
    484   1.1       cgd 
    485  1.41  christos /*ARGSUSED*/
    486  1.41  christos int
    487  1.41  christos kernfs_setattr(v)
    488  1.41  christos 	void *v;
    489   1.1       cgd {
    490   1.1       cgd 	/*
    491  1.17       cgd 	 * Silently ignore attribute changes.
    492  1.17       cgd 	 * This allows for open with truncate to have no
    493  1.17       cgd 	 * effect until some data is written.  I want to
    494  1.17       cgd 	 * do it this way because all writes are atomic.
    495   1.1       cgd 	 */
    496  1.17       cgd 	return (0);
    497   1.1       cgd }
    498   1.1       cgd 
    499  1.28   mycroft int
    500  1.41  christos kernfs_read(v)
    501  1.41  christos 	void *v;
    502  1.41  christos {
    503  1.23   mycroft 	struct vop_read_args /* {
    504  1.23   mycroft 		struct vnode *a_vp;
    505  1.23   mycroft 		struct uio *a_uio;
    506  1.23   mycroft 		int  a_ioflag;
    507  1.23   mycroft 		struct ucred *a_cred;
    508  1.41  christos 	} */ *ap = v;
    509  1.23   mycroft 	struct vnode *vp = ap->a_vp;
    510  1.23   mycroft 	struct uio *uio = ap->a_uio;
    511  1.23   mycroft 	struct kern_target *kt;
    512  1.28   mycroft 	char strbuf[KSTRING], *buf;
    513  1.28   mycroft 	int off, len;
    514  1.28   mycroft 	int error;
    515  1.23   mycroft 
    516  1.23   mycroft 	if (vp->v_type == VDIR)
    517  1.23   mycroft 		return (EOPNOTSUPP);
    518  1.23   mycroft 
    519  1.23   mycroft 	kt = VTOKERN(vp)->kf_kt;
    520  1.23   mycroft 
    521   1.1       cgd #ifdef KERNFS_DIAGNOSTIC
    522   1.1       cgd 	printf("kern_read %s\n", kt->kt_name);
    523   1.1       cgd #endif
    524  1.18       cgd 
    525  1.28   mycroft 	off = uio->uio_offset;
    526  1.28   mycroft #if 0
    527  1.28   mycroft 	while (buf = strbuf,
    528  1.28   mycroft #else
    529  1.28   mycroft 	if (buf = strbuf,
    530  1.28   mycroft #endif
    531  1.28   mycroft 	    len = kernfs_xread(kt, off, &buf, sizeof(strbuf))) {
    532  1.41  christos 		if ((error = uiomove(buf, len, uio)) != 0)
    533  1.28   mycroft 			return (error);
    534  1.28   mycroft 		off += len;
    535  1.28   mycroft 	}
    536  1.28   mycroft 	return (0);
    537   1.1       cgd }
    538   1.1       cgd 
    539  1.28   mycroft int
    540  1.41  christos kernfs_write(v)
    541  1.41  christos 	void *v;
    542  1.41  christos {
    543  1.23   mycroft 	struct vop_write_args /* {
    544  1.23   mycroft 		struct vnode *a_vp;
    545  1.23   mycroft 		struct uio *a_uio;
    546  1.23   mycroft 		int  a_ioflag;
    547  1.23   mycroft 		struct ucred *a_cred;
    548  1.41  christos 	} */ *ap = v;
    549  1.23   mycroft 	struct vnode *vp = ap->a_vp;
    550  1.23   mycroft 	struct uio *uio = ap->a_uio;
    551  1.23   mycroft 	struct kern_target *kt;
    552  1.23   mycroft 	int error, xlen;
    553   1.1       cgd 	char strbuf[KSTRING];
    554  1.23   mycroft 
    555  1.23   mycroft 	if (vp->v_type == VDIR)
    556  1.23   mycroft 		return (EOPNOTSUPP);
    557  1.23   mycroft 
    558  1.23   mycroft 	kt = VTOKERN(vp)->kf_kt;
    559   1.1       cgd 
    560   1.1       cgd 	if (uio->uio_offset != 0)
    561   1.1       cgd 		return (EINVAL);
    562   1.1       cgd 
    563   1.1       cgd 	xlen = min(uio->uio_resid, KSTRING-1);
    564  1.41  christos 	if ((error = uiomove(strbuf, xlen, uio)) != 0)
    565   1.1       cgd 		return (error);
    566   1.1       cgd 
    567   1.1       cgd 	if (uio->uio_resid != 0)
    568   1.1       cgd 		return (EIO);
    569   1.1       cgd 
    570   1.1       cgd 	strbuf[xlen] = '\0';
    571  1.17       cgd 	xlen = strlen(strbuf);
    572   1.1       cgd 	return (kernfs_xwrite(kt, strbuf, xlen));
    573   1.1       cgd }
    574   1.1       cgd 
    575  1.41  christos int
    576  1.41  christos kernfs_readdir(v)
    577  1.41  christos 	void *v;
    578  1.41  christos {
    579  1.23   mycroft 	struct vop_readdir_args /* {
    580  1.23   mycroft 		struct vnode *a_vp;
    581  1.23   mycroft 		struct uio *a_uio;
    582  1.23   mycroft 		struct ucred *a_cred;
    583  1.26   mycroft 		int *a_eofflag;
    584  1.26   mycroft 		u_long *a_cookies;
    585  1.26   mycroft 		int a_ncookies;
    586  1.41  christos 	} */ *ap = v;
    587  1.23   mycroft 	struct uio *uio = ap->a_uio;
    588  1.37   mycroft 	struct dirent d;
    589  1.26   mycroft 	struct kern_target *kt;
    590   1.1       cgd 	int i;
    591   1.1       cgd 	int error;
    592  1.37   mycroft 	u_long *cookies = ap->a_cookies;
    593  1.37   mycroft 	int ncookies = ap->a_ncookies;
    594   1.1       cgd 
    595  1.23   mycroft 	if (ap->a_vp->v_type != VDIR)
    596  1.23   mycroft 		return (ENOTDIR);
    597  1.23   mycroft 
    598  1.37   mycroft 	if (uio->uio_resid < UIO_MX)
    599  1.37   mycroft 		return (EINVAL);
    600  1.38   mycroft 	if (uio->uio_offset < 0)
    601  1.37   mycroft 		return (EINVAL);
    602  1.26   mycroft 
    603   1.1       cgd 	error = 0;
    604  1.38   mycroft 	i = uio->uio_offset;
    605  1.37   mycroft 	bzero((caddr_t)&d, UIO_MX);
    606  1.37   mycroft 	d.d_reclen = UIO_MX;
    607  1.37   mycroft 
    608  1.23   mycroft 	for (kt = &kern_targets[i];
    609  1.23   mycroft 	     uio->uio_resid >= UIO_MX && i < nkern_targets; kt++, i++) {
    610   1.1       cgd #ifdef KERNFS_DIAGNOSTIC
    611   1.1       cgd 		printf("kernfs_readdir: i = %d\n", i);
    612   1.1       cgd #endif
    613  1.26   mycroft 
    614  1.23   mycroft 		if (kt->kt_tag == KTT_DEVICE) {
    615  1.26   mycroft 			dev_t *dp = kt->kt_data;
    616  1.23   mycroft 			struct vnode *fvp;
    617  1.26   mycroft 
    618  1.23   mycroft 			if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp))
    619  1.23   mycroft 				continue;
    620  1.23   mycroft 		}
    621  1.26   mycroft 
    622  1.37   mycroft 		d.d_fileno = i + 3;
    623  1.37   mycroft 		d.d_namlen = kt->kt_namlen;
    624  1.37   mycroft 		bcopy(kt->kt_name, d.d_name, kt->kt_namlen + 1);
    625  1.37   mycroft 		d.d_type = kt->kt_type;
    626  1.26   mycroft 
    627  1.41  christos 		if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    628   1.1       cgd 			break;
    629  1.37   mycroft 		if (ncookies-- > 0)
    630  1.38   mycroft 			*cookies++ = i + 1;
    631   1.1       cgd 	}
    632   1.1       cgd 
    633  1.38   mycroft 	uio->uio_offset = i;
    634   1.1       cgd 	return (error);
    635   1.1       cgd }
    636   1.1       cgd 
    637  1.41  christos int
    638  1.41  christos kernfs_inactive(v)
    639  1.41  christos 	void *v;
    640  1.41  christos {
    641  1.23   mycroft 	struct vop_inactive_args /* {
    642  1.23   mycroft 		struct vnode *a_vp;
    643  1.41  christos 	} */ *ap = v;
    644  1.23   mycroft 	struct vnode *vp = ap->a_vp;
    645  1.23   mycroft 
    646  1.23   mycroft #ifdef KERNFS_DIAGNOSTIC
    647  1.23   mycroft 	printf("kernfs_inactive(%x)\n", vp);
    648  1.23   mycroft #endif
    649   1.1       cgd 	/*
    650   1.1       cgd 	 * Clear out the v_type field to avoid
    651   1.1       cgd 	 * nasty things happening in vgone().
    652   1.1       cgd 	 */
    653   1.1       cgd 	vp->v_type = VNON;
    654  1.23   mycroft 	return (0);
    655  1.23   mycroft }
    656  1.23   mycroft 
    657  1.41  christos int
    658  1.41  christos kernfs_reclaim(v)
    659  1.41  christos 	void *v;
    660  1.41  christos {
    661  1.23   mycroft 	struct vop_reclaim_args /* {
    662  1.23   mycroft 		struct vnode *a_vp;
    663  1.41  christos 	} */ *ap = v;
    664  1.23   mycroft 	struct vnode *vp = ap->a_vp;
    665  1.23   mycroft 
    666   1.1       cgd #ifdef KERNFS_DIAGNOSTIC
    667  1.23   mycroft 	printf("kernfs_reclaim(%x)\n", vp);
    668   1.1       cgd #endif
    669  1.23   mycroft 	if (vp->v_data) {
    670  1.23   mycroft 		FREE(vp->v_data, M_TEMP);
    671  1.23   mycroft 		vp->v_data = 0;
    672  1.23   mycroft 	}
    673   1.1       cgd 	return (0);
    674   1.1       cgd }
    675   1.1       cgd 
    676   1.1       cgd /*
    677  1.23   mycroft  * Return POSIX pathconf information applicable to special devices.
    678  1.23   mycroft  */
    679  1.41  christos int
    680  1.41  christos kernfs_pathconf(v)
    681  1.41  christos 	void *v;
    682  1.41  christos {
    683  1.23   mycroft 	struct vop_pathconf_args /* {
    684  1.23   mycroft 		struct vnode *a_vp;
    685  1.23   mycroft 		int a_name;
    686  1.29       cgd 		register_t *a_retval;
    687  1.41  christos 	} */ *ap = v;
    688  1.23   mycroft 
    689  1.23   mycroft 	switch (ap->a_name) {
    690  1.23   mycroft 	case _PC_LINK_MAX:
    691  1.23   mycroft 		*ap->a_retval = LINK_MAX;
    692  1.23   mycroft 		return (0);
    693  1.23   mycroft 	case _PC_MAX_CANON:
    694  1.23   mycroft 		*ap->a_retval = MAX_CANON;
    695  1.23   mycroft 		return (0);
    696  1.23   mycroft 	case _PC_MAX_INPUT:
    697  1.23   mycroft 		*ap->a_retval = MAX_INPUT;
    698  1.23   mycroft 		return (0);
    699  1.23   mycroft 	case _PC_PIPE_BUF:
    700  1.23   mycroft 		*ap->a_retval = PIPE_BUF;
    701  1.23   mycroft 		return (0);
    702  1.23   mycroft 	case _PC_CHOWN_RESTRICTED:
    703  1.23   mycroft 		*ap->a_retval = 1;
    704  1.23   mycroft 		return (0);
    705  1.23   mycroft 	case _PC_VDISABLE:
    706  1.23   mycroft 		*ap->a_retval = _POSIX_VDISABLE;
    707  1.23   mycroft 		return (0);
    708  1.23   mycroft 	default:
    709  1.23   mycroft 		return (EINVAL);
    710  1.23   mycroft 	}
    711  1.23   mycroft 	/* NOTREACHED */
    712  1.23   mycroft }
    713  1.23   mycroft 
    714  1.23   mycroft /*
    715  1.23   mycroft  * Print out the contents of a /dev/fd vnode.
    716   1.1       cgd  */
    717   1.1       cgd /* ARGSUSED */
    718  1.41  christos int
    719  1.41  christos kernfs_print(v)
    720  1.41  christos 	void *v;
    721  1.23   mycroft {
    722  1.23   mycroft 
    723  1.23   mycroft 	printf("tag VT_KERNFS, kernfs vnode\n");
    724  1.23   mycroft 	return (0);
    725  1.23   mycroft }
    726  1.23   mycroft 
    727  1.41  christos /*ARGSUSED*/
    728  1.41  christos int
    729  1.41  christos kernfs_vfree(v)
    730  1.41  christos 	void *v;
    731   1.1       cgd {
    732  1.23   mycroft 
    733  1.23   mycroft 	return (0);
    734   1.1       cgd }
    735   1.1       cgd 
    736  1.40   mycroft int
    737  1.41  christos kernfs_link(v)
    738  1.41  christos 	void *v;
    739  1.41  christos {
    740  1.40   mycroft 	struct vop_link_args /* {
    741  1.40   mycroft 		struct vnode *a_dvp;
    742  1.40   mycroft 		struct vnode *a_vp;
    743  1.40   mycroft 		struct componentname *a_cnp;
    744  1.41  christos 	} */ *ap = v;
    745  1.40   mycroft 
    746  1.40   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    747  1.40   mycroft 	vput(ap->a_dvp);
    748  1.40   mycroft 	return (EROFS);
    749  1.40   mycroft }
    750  1.40   mycroft 
    751  1.40   mycroft int
    752  1.41  christos kernfs_symlink(v)
    753  1.41  christos 	void *v;
    754  1.41  christos {
    755  1.40   mycroft 	struct vop_symlink_args /* {
    756  1.40   mycroft 		struct vnode *a_dvp;
    757  1.40   mycroft 		struct vnode **a_vpp;
    758  1.40   mycroft 		struct componentname *a_cnp;
    759  1.40   mycroft 		struct vattr *a_vap;
    760  1.40   mycroft 		char *a_target;
    761  1.41  christos 	} */ *ap = v;
    762  1.40   mycroft 
    763  1.40   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
    764  1.40   mycroft 	vput(ap->a_dvp);
    765  1.40   mycroft 	return (EROFS);
    766  1.40   mycroft }
    767  1.40   mycroft 
    768  1.40   mycroft int
    769  1.41  christos kernfs_abortop(v)
    770  1.41  christos 	void *v;
    771  1.41  christos {
    772  1.40   mycroft 	struct vop_abortop_args /* {
    773  1.40   mycroft 		struct vnode *a_dvp;
    774  1.40   mycroft 		struct componentname *a_cnp;
    775  1.41  christos 	} */ *ap = v;
    776  1.40   mycroft 
    777  1.40   mycroft 	if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF)
    778  1.40   mycroft 		FREE(ap->a_cnp->cn_pnbuf, M_NAMEI);
    779  1.40   mycroft 	return (0);
    780  1.40   mycroft }
    781  1.40   mycroft 
    782   1.1       cgd /*
    783  1.23   mycroft  * /dev/fd vnode unsupported operation
    784   1.1       cgd  */
    785  1.41  christos /*ARGSUSED*/
    786  1.41  christos int
    787  1.41  christos kernfs_enotsupp(v)
    788  1.41  christos 	void *v;
    789   1.1       cgd {
    790  1.23   mycroft 
    791   1.1       cgd 	return (EOPNOTSUPP);
    792   1.1       cgd }
    793   1.1       cgd 
    794   1.1       cgd /*
    795  1.23   mycroft  * /dev/fd "should never get here" operation
    796   1.1       cgd  */
    797  1.41  christos /*ARGSUSED*/
    798  1.41  christos int
    799  1.41  christos kernfs_badop(v)
    800  1.41  christos 	void *v;
    801   1.1       cgd {
    802  1.23   mycroft 
    803   1.1       cgd 	panic("kernfs: bad op");
    804  1.41  christos 	return 0;
    805   1.1       cgd }
    806