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