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