Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vnops.c revision 1.93
      1  1.93    simonb /*	$NetBSD: kernfs_vnops.c,v 1.93 2003/09/10 03:24:38 simonb 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.89       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.2       cgd  *    may be used to endorse or promote products derived from this software
     20   1.2       cgd  *    without specific prior written permission.
     21   1.1       cgd  *
     22   1.2       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.2       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.2       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.2       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.2       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.2       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.2       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.2       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.2       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.2       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.2       cgd  * SUCH DAMAGE.
     33   1.1       cgd  *
     34  1.57      fvdl  *	@(#)kernfs_vnops.c	8.15 (Berkeley) 5/21/95
     35   1.1       cgd  */
     36   1.1       cgd 
     37   1.1       cgd /*
     38  1.23   mycroft  * Kernel parameter filesystem (/kern)
     39   1.1       cgd  */
     40  1.77     lukem 
     41  1.77     lukem #include <sys/cdefs.h>
     42  1.93    simonb __KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.93 2003/09/10 03:24:38 simonb Exp $");
     43  1.90    itojun 
     44  1.90    itojun #ifdef _KERNEL_OPT
     45  1.90    itojun #include "opt_ipsec.h"
     46  1.90    itojun #endif
     47  1.55       mrg 
     48  1.14   mycroft #include <sys/param.h>
     49  1.14   mycroft #include <sys/systm.h>
     50  1.14   mycroft #include <sys/kernel.h>
     51  1.23   mycroft #include <sys/vmmeter.h>
     52  1.14   mycroft #include <sys/time.h>
     53  1.14   mycroft #include <sys/proc.h>
     54  1.23   mycroft #include <sys/vnode.h>
     55  1.23   mycroft #include <sys/malloc.h>
     56  1.14   mycroft #include <sys/file.h>
     57  1.14   mycroft #include <sys/stat.h>
     58  1.14   mycroft #include <sys/mount.h>
     59  1.14   mycroft #include <sys/namei.h>
     60  1.14   mycroft #include <sys/buf.h>
     61  1.23   mycroft #include <sys/dirent.h>
     62  1.28   mycroft #include <sys/msgbuf.h>
     63  1.44   mycroft 
     64  1.44   mycroft #include <miscfs/genfs/genfs.h>
     65  1.17       cgd #include <miscfs/kernfs/kernfs.h>
     66  1.63       mrg 
     67  1.90    itojun #ifdef IPSEC
     68  1.90    itojun #include <sys/mbuf.h>
     69  1.90    itojun #include <net/route.h>
     70  1.90    itojun #include <netinet/in.h>
     71  1.90    itojun #include <netinet6/ipsec.h>
     72  1.90    itojun #include <netkey/key.h>
     73  1.90    itojun #endif
     74  1.90    itojun 
     75  1.54       mrg #include <uvm/uvm_extern.h>
     76  1.54       mrg 
     77  1.17       cgd #define KSTRING	256		/* Largest I/O available via this filesystem */
     78  1.17       cgd #define	UIO_MX 32
     79   1.1       cgd 
     80  1.23   mycroft #define	READ_MODE	(S_IRUSR|S_IRGRP|S_IROTH)
     81  1.23   mycroft #define	WRITE_MODE	(S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)
     82  1.23   mycroft #define DIR_MODE	(S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
     83  1.90    itojun #define UDIR_MODE	(S_IRUSR|S_IXUSR)
     84  1.23   mycroft 
     85  1.90    itojun #define N(s) sizeof(s)-1, s
     86  1.75  jdolecek const struct kern_target kern_targets[] = {
     87   1.1       cgd /* NOTE: The name must be less than UIO_MX-16 chars in length */
     88  1.23   mycroft      /*        name            data          tag           type  ro/rw */
     89  1.90    itojun      { DT_DIR, N("."),         0,            Pkern,        VDIR, DIR_MODE   },
     90  1.90    itojun      { DT_DIR, N(".."),        0,            Proot,        VDIR, DIR_MODE   },
     91  1.90    itojun      { DT_REG, N("boottime"),  &boottime.tv_sec, Pint,     VREG, READ_MODE  },
     92  1.69   thorpej 			/* XXX cast away const */
     93  1.69   thorpej      { DT_REG, N("copyright"), (void *)copyright,
     94  1.90    itojun      					     Pstring,      VREG, READ_MODE  },
     95  1.90    itojun      { DT_REG, N("hostname"),  0,            Phostname,    VREG, WRITE_MODE },
     96  1.90    itojun      { DT_REG, N("hz"),        &hz,          Pint,         VREG, READ_MODE  },
     97  1.90    itojun #ifdef IPSEC
     98  1.90    itojun      { DT_DIR, N("ipsecsa"),   0,	     Pipsecsadir,  VDIR, UDIR_MODE  },
     99  1.90    itojun      { DT_DIR, N("ipsecsp"),   0,	     Pipsecspdir,  VDIR, UDIR_MODE  },
    100  1.90    itojun #endif
    101  1.90    itojun      { DT_REG, N("loadavg"),   0,            Pavenrun,     VREG, READ_MODE  },
    102  1.90    itojun      { DT_REG, N("msgbuf"),    0,	     Pmsgbuf,      VREG, READ_MODE  },
    103  1.90    itojun      { DT_REG, N("pagesize"),  &uvmexp.pagesize, Pint,     VREG, READ_MODE  },
    104  1.90    itojun      { DT_REG, N("physmem"),   &physmem,     Pint,         VREG, READ_MODE  },
    105  1.17       cgd #if 0
    106  1.90    itojun      { DT_DIR, N("root"),      0,            Pnull,        VDIR, DIR_MODE   },
    107  1.17       cgd #endif
    108  1.90    itojun      { DT_BLK, N("rootdev"),   &rootdev,     Pdevice,      VBLK, READ_MODE  },
    109  1.90    itojun      { DT_CHR, N("rrootdev"),  &rrootdev,    Pdevice,      VCHR, READ_MODE  },
    110  1.90    itojun      { DT_REG, N("time"),      0,            Ptime,        VREG, READ_MODE  },
    111  1.69   thorpej 			/* XXX cast away const */
    112  1.69   thorpej      { DT_REG, N("version"),   (void *)version,
    113  1.90    itojun      					     Pstring,      VREG, READ_MODE  },
    114  1.90    itojun };
    115  1.90    itojun #ifdef IPSEC
    116  1.90    itojun const struct kern_target ipsecsa_targets[] = {
    117  1.90    itojun /* NOTE: The name must be less than UIO_MX-16 chars in length */
    118  1.90    itojun      /*        name            data          tag           type  ro/rw */
    119  1.90    itojun      { DT_DIR, N("."),         0,            Pipsecsadir,  VDIR, DIR_MODE   },
    120  1.90    itojun      { DT_DIR, N(".."),        0,            Pkern,        VDIR, DIR_MODE   },
    121  1.90    itojun };
    122  1.90    itojun const struct kern_target ipsecsp_targets[] = {
    123  1.90    itojun /* NOTE: The name must be less than UIO_MX-16 chars in length */
    124  1.90    itojun      /*        name            data          tag           type  ro/rw */
    125  1.90    itojun      { DT_DIR, N("."),         0,            Pipsecspdir,  VDIR, DIR_MODE   },
    126  1.90    itojun      { DT_DIR, N(".."),        0,            Pkern,        VDIR, DIR_MODE   },
    127  1.90    itojun };
    128  1.90    itojun #endif
    129  1.23   mycroft #undef N
    130  1.90    itojun int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);
    131  1.90    itojun #ifdef IPSEC
    132  1.90    itojun int nipsecsa_targets = sizeof(ipsecsa_targets) / sizeof(ipsecsa_targets[0]);
    133  1.90    itojun int nipsecsp_targets = sizeof(ipsecsp_targets) / sizeof(ipsecsp_targets[0]);
    134  1.90    itojun #endif
    135  1.90    itojun 
    136   1.1       cgd 
    137  1.41  christos int	kernfs_lookup	__P((void *));
    138  1.85  jdolecek #define	kernfs_create	genfs_eopnotsupp
    139  1.85  jdolecek #define	kernfs_mknod	genfs_eopnotsupp
    140  1.90    itojun int	kernfs_open	__P((void *));
    141  1.90    itojun int	kernfs_close	__P((void *));
    142  1.41  christos int	kernfs_access	__P((void *));
    143  1.41  christos int	kernfs_getattr	__P((void *));
    144  1.41  christos int	kernfs_setattr	__P((void *));
    145  1.41  christos int	kernfs_read	__P((void *));
    146  1.41  christos int	kernfs_write	__P((void *));
    147  1.65  wrstuden #define	kernfs_fcntl	genfs_fcntl
    148  1.61  matthias #define	kernfs_ioctl	genfs_enoioctl
    149  1.45   mycroft #define	kernfs_poll	genfs_poll
    150  1.57      fvdl #define kernfs_revoke	genfs_revoke
    151  1.44   mycroft #define	kernfs_fsync	genfs_nullop
    152  1.44   mycroft #define	kernfs_seek	genfs_nullop
    153  1.85  jdolecek #define	kernfs_remove	genfs_eopnotsupp
    154  1.41  christos int	kernfs_link	__P((void *));
    155  1.85  jdolecek #define	kernfs_rename	genfs_eopnotsupp
    156  1.85  jdolecek #define	kernfs_mkdir	genfs_eopnotsupp
    157  1.85  jdolecek #define	kernfs_rmdir	genfs_eopnotsupp
    158  1.41  christos int	kernfs_symlink	__P((void *));
    159  1.41  christos int	kernfs_readdir	__P((void *));
    160  1.44   mycroft #define	kernfs_readlink	genfs_eopnotsupp
    161  1.44   mycroft #define	kernfs_abortop	genfs_abortop
    162  1.41  christos int	kernfs_inactive	__P((void *));
    163  1.41  christos int	kernfs_reclaim	__P((void *));
    164  1.64  wrstuden #define	kernfs_lock	genfs_lock
    165  1.64  wrstuden #define	kernfs_unlock	genfs_unlock
    166  1.44   mycroft #define	kernfs_bmap	genfs_badop
    167  1.44   mycroft #define	kernfs_strategy	genfs_badop
    168  1.41  christos int	kernfs_print	__P((void *));
    169  1.64  wrstuden #define	kernfs_islocked	genfs_islocked
    170  1.41  christos int	kernfs_pathconf	__P((void *));
    171  1.62    kleink #define	kernfs_advlock	genfs_einval
    172  1.44   mycroft #define	kernfs_blkatoff	genfs_eopnotsupp
    173  1.44   mycroft #define	kernfs_valloc	genfs_eopnotsupp
    174  1.44   mycroft #define	kernfs_vfree	genfs_nullop
    175  1.44   mycroft #define	kernfs_truncate	genfs_eopnotsupp
    176  1.44   mycroft #define	kernfs_update	genfs_nullop
    177  1.44   mycroft #define	kernfs_bwrite	genfs_eopnotsupp
    178  1.79       chs #define	kernfs_putpages	genfs_putpages
    179  1.41  christos 
    180  1.90    itojun static int	kernfs_xread __P((struct kernfs_node *, int, char **, size_t, size_t *));
    181  1.90    itojun static int	kernfs_xwrite __P((const struct kernfs_node *, char *, size_t));
    182  1.41  christos 
    183  1.41  christos int (**kernfs_vnodeop_p) __P((void *));
    184  1.71  jdolecek const struct vnodeopv_entry_desc kernfs_vnodeop_entries[] = {
    185  1.41  christos 	{ &vop_default_desc, vn_default_error },
    186  1.44   mycroft 	{ &vop_lookup_desc, kernfs_lookup },		/* lookup */
    187  1.44   mycroft 	{ &vop_create_desc, kernfs_create },		/* create */
    188  1.44   mycroft 	{ &vop_mknod_desc, kernfs_mknod },		/* mknod */
    189  1.44   mycroft 	{ &vop_open_desc, kernfs_open },		/* open */
    190  1.44   mycroft 	{ &vop_close_desc, kernfs_close },		/* close */
    191  1.44   mycroft 	{ &vop_access_desc, kernfs_access },		/* access */
    192  1.44   mycroft 	{ &vop_getattr_desc, kernfs_getattr },		/* getattr */
    193  1.44   mycroft 	{ &vop_setattr_desc, kernfs_setattr },		/* setattr */
    194  1.44   mycroft 	{ &vop_read_desc, kernfs_read },		/* read */
    195  1.44   mycroft 	{ &vop_write_desc, kernfs_write },		/* write */
    196  1.65  wrstuden 	{ &vop_fcntl_desc, kernfs_fcntl },		/* fcntl */
    197  1.44   mycroft 	{ &vop_ioctl_desc, kernfs_ioctl },		/* ioctl */
    198  1.45   mycroft 	{ &vop_poll_desc, kernfs_poll },		/* poll */
    199  1.57      fvdl 	{ &vop_revoke_desc, kernfs_revoke },		/* revoke */
    200  1.44   mycroft 	{ &vop_fsync_desc, kernfs_fsync },		/* fsync */
    201  1.44   mycroft 	{ &vop_seek_desc, kernfs_seek },		/* seek */
    202  1.44   mycroft 	{ &vop_remove_desc, kernfs_remove },		/* remove */
    203  1.44   mycroft 	{ &vop_link_desc, kernfs_link },		/* link */
    204  1.44   mycroft 	{ &vop_rename_desc, kernfs_rename },		/* rename */
    205  1.44   mycroft 	{ &vop_mkdir_desc, kernfs_mkdir },		/* mkdir */
    206  1.44   mycroft 	{ &vop_rmdir_desc, kernfs_rmdir },		/* rmdir */
    207  1.44   mycroft 	{ &vop_symlink_desc, kernfs_symlink },		/* symlink */
    208  1.44   mycroft 	{ &vop_readdir_desc, kernfs_readdir },		/* readdir */
    209  1.44   mycroft 	{ &vop_readlink_desc, kernfs_readlink },	/* readlink */
    210  1.44   mycroft 	{ &vop_abortop_desc, kernfs_abortop },		/* abortop */
    211  1.44   mycroft 	{ &vop_inactive_desc, kernfs_inactive },	/* inactive */
    212  1.44   mycroft 	{ &vop_reclaim_desc, kernfs_reclaim },		/* reclaim */
    213  1.44   mycroft 	{ &vop_lock_desc, kernfs_lock },		/* lock */
    214  1.44   mycroft 	{ &vop_unlock_desc, kernfs_unlock },		/* unlock */
    215  1.44   mycroft 	{ &vop_bmap_desc, kernfs_bmap },		/* bmap */
    216  1.44   mycroft 	{ &vop_strategy_desc, kernfs_strategy },	/* strategy */
    217  1.44   mycroft 	{ &vop_print_desc, kernfs_print },		/* print */
    218  1.44   mycroft 	{ &vop_islocked_desc, kernfs_islocked },	/* islocked */
    219  1.44   mycroft 	{ &vop_pathconf_desc, kernfs_pathconf },	/* pathconf */
    220  1.44   mycroft 	{ &vop_advlock_desc, kernfs_advlock },		/* advlock */
    221  1.44   mycroft 	{ &vop_blkatoff_desc, kernfs_blkatoff },	/* blkatoff */
    222  1.44   mycroft 	{ &vop_valloc_desc, kernfs_valloc },		/* valloc */
    223  1.44   mycroft 	{ &vop_vfree_desc, kernfs_vfree },		/* vfree */
    224  1.44   mycroft 	{ &vop_truncate_desc, kernfs_truncate },	/* truncate */
    225  1.44   mycroft 	{ &vop_update_desc, kernfs_update },		/* update */
    226  1.44   mycroft 	{ &vop_bwrite_desc, kernfs_bwrite },		/* bwrite */
    227  1.79       chs 	{ &vop_putpages_desc, kernfs_putpages },	/* putpages */
    228  1.76       chs 	{ NULL, NULL }
    229  1.41  christos };
    230  1.71  jdolecek const struct vnodeopv_desc kernfs_vnodeop_opv_desc =
    231  1.41  christos 	{ &kernfs_vnodeop_p, kernfs_vnodeop_entries };
    232  1.41  christos 
    233  1.82  jdolecek static int
    234  1.90    itojun kernfs_xread(kfs, off, bufp, len, wrlen)
    235  1.90    itojun 	struct kernfs_node *kfs;
    236  1.28   mycroft 	int off;
    237  1.28   mycroft 	char **bufp;
    238  1.82  jdolecek 	size_t len;
    239  1.82  jdolecek 	size_t *wrlen;
    240   1.1       cgd {
    241  1.90    itojun 	const struct kern_target *kt;
    242  1.90    itojun #ifdef IPSEC
    243  1.90    itojun 	struct mbuf *m;
    244  1.90    itojun #endif
    245  1.90    itojun 
    246  1.90    itojun 	kt = kfs->kfs_kt;
    247   1.1       cgd 
    248  1.90    itojun 	switch (kfs->kfs_type) {
    249  1.90    itojun 	case Ptime: {
    250   1.1       cgd 		struct timeval tv;
    251  1.28   mycroft 
    252   1.1       cgd 		microtime(&tv);
    253  1.90    itojun 		snprintf(*bufp, len, "%ld %ld\n", tv.tv_sec, tv.tv_usec);
    254   1.1       cgd 		break;
    255   1.1       cgd 	}
    256   1.1       cgd 
    257  1.90    itojun 	case Pint: {
    258   1.1       cgd 		int *ip = kt->kt_data;
    259  1.28   mycroft 
    260  1.90    itojun 		snprintf(*bufp, len, "%d\n", *ip);
    261   1.1       cgd 		break;
    262   1.1       cgd 	}
    263   1.1       cgd 
    264  1.90    itojun 	case Pstring: {
    265   1.1       cgd 		char *cp = kt->kt_data;
    266   1.1       cgd 
    267  1.28   mycroft 		*bufp = cp;
    268  1.28   mycroft 		break;
    269  1.28   mycroft 	}
    270   1.1       cgd 
    271  1.90    itojun 	case Pmsgbuf: {
    272  1.28   mycroft 		long n;
    273  1.28   mycroft 
    274  1.52       leo 		/*
    275  1.52       leo 		 * deal with cases where the message buffer has
    276  1.52       leo 		 * become corrupted.
    277  1.52       leo 		 */
    278  1.52       leo 		if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    279  1.52       leo 			msgbufenabled = 0;
    280  1.52       leo 			return (ENXIO);
    281  1.52       leo 		}
    282  1.52       leo 
    283  1.52       leo 		/*
    284  1.52       leo 		 * Note that reads of /kern/msgbuf won't necessarily yield
    285  1.52       leo 		 * consistent results, if the message buffer is modified
    286  1.52       leo 		 * while the read is in progress.  The worst that can happen
    287  1.52       leo 		 * is that incorrect data will be read.  There's no way
    288  1.52       leo 		 * that this can crash the system unless the values in the
    289  1.52       leo 		 * message buffer header are corrupted, but that'll cause
    290  1.52       leo 		 * the system to die anyway.
    291  1.52       leo 		 */
    292  1.82  jdolecek 		if (off >= msgbufp->msg_bufs) {
    293  1.82  jdolecek 			*wrlen = 0;
    294  1.28   mycroft 			return (0);
    295  1.82  jdolecek 		}
    296  1.28   mycroft 		n = msgbufp->msg_bufx + off;
    297  1.52       leo 		if (n >= msgbufp->msg_bufs)
    298  1.52       leo 			n -= msgbufp->msg_bufs;
    299  1.52       leo 		len = min(msgbufp->msg_bufs - n, msgbufp->msg_bufs - off);
    300  1.28   mycroft 		*bufp = msgbufp->msg_bufc + n;
    301  1.82  jdolecek 		*wrlen = len;
    302  1.82  jdolecek 		return (0);
    303   1.1       cgd 	}
    304   1.1       cgd 
    305  1.90    itojun 	case Phostname: {
    306   1.1       cgd 		char *cp = hostname;
    307   1.1       cgd 		int xlen = hostnamelen;
    308   1.1       cgd 
    309  1.90    itojun 		if (xlen >= (len - 2))
    310   1.1       cgd 			return (EINVAL);
    311   1.1       cgd 
    312  1.60     perry 		memcpy(*bufp, cp, xlen);
    313  1.28   mycroft 		(*bufp)[xlen] = '\n';
    314  1.28   mycroft 		(*bufp)[xlen+1] = '\0';
    315  1.90    itojun 		len = strlen(*bufp);
    316   1.1       cgd 		break;
    317   1.1       cgd 	}
    318   1.1       cgd 
    319  1.90    itojun 	case Pavenrun:
    320  1.31   mycroft 		averunnable.fscale = FSCALE;
    321  1.90    itojun 		snprintf(*bufp, len, "%d %d %d %ld\n",
    322  1.23   mycroft 		    averunnable.ldavg[0], averunnable.ldavg[1],
    323  1.23   mycroft 		    averunnable.ldavg[2], averunnable.fscale);
    324   1.1       cgd 		break;
    325   1.1       cgd 
    326  1.90    itojun #ifdef IPSEC
    327  1.90    itojun 	case Pipsecsa:
    328  1.90    itojun 		/*
    329  1.90    itojun 		 * Note that SA configuration could be changed during the
    330  1.90    itojun 		 * read operation, resulting in garbled output.
    331  1.90    itojun 		 */
    332  1.90    itojun 		m = key_setdumpsa_spi(htonl(kfs->kfs_value));
    333  1.90    itojun 		if (!m)
    334  1.90    itojun 			return (ENOBUFS);
    335  1.90    itojun 		if (off >= m->m_pkthdr.len) {
    336  1.90    itojun 			*wrlen = 0;
    337  1.90    itojun 			m_freem(m);
    338  1.90    itojun 			return (0);
    339  1.90    itojun 		}
    340  1.90    itojun 		if (len > m->m_pkthdr.len - off)
    341  1.90    itojun 			len = m->m_pkthdr.len - off;
    342  1.90    itojun 		m_copydata(m, off, len, *bufp);
    343  1.90    itojun 		*wrlen = len;
    344  1.90    itojun 		m_freem(m);
    345  1.90    itojun 		return (0);
    346  1.90    itojun 
    347  1.90    itojun 	case Pipsecsp:
    348  1.90    itojun 		/*
    349  1.90    itojun 		 * Note that SP configuration could be changed during the
    350  1.90    itojun 		 * read operation, resulting in garbled output.
    351  1.90    itojun 		 */
    352  1.90    itojun 		if (!kfs->kfs_v) {
    353  1.90    itojun 			struct secpolicy *sp;
    354  1.90    itojun 
    355  1.90    itojun 			sp = key_getspbyid(kfs->kfs_value);
    356  1.90    itojun 			if (sp)
    357  1.90    itojun 				kfs->kfs_v = sp;
    358  1.90    itojun 			else
    359  1.90    itojun 				return (ENOENT);
    360  1.90    itojun 		}
    361  1.90    itojun 		m = key_setdumpsp((struct secpolicy *)kfs->kfs_v,
    362  1.90    itojun 		    SADB_X_SPDGET, 0, 0);
    363  1.90    itojun 		if (!m)
    364  1.90    itojun 			return (ENOBUFS);
    365  1.90    itojun 		if (off >= m->m_pkthdr.len) {
    366  1.90    itojun 			*wrlen = 0;
    367  1.90    itojun 			m_freem(m);
    368  1.90    itojun 			return (0);
    369  1.90    itojun 		}
    370  1.90    itojun 		if (len > m->m_pkthdr.len - off)
    371  1.90    itojun 			len = m->m_pkthdr.len - off;
    372  1.90    itojun 		m_copydata(m, off, len, *bufp);
    373  1.90    itojun 		*wrlen = len;
    374  1.90    itojun 		m_freem(m);
    375  1.90    itojun 		return (0);
    376  1.90    itojun #endif
    377  1.90    itojun 
    378   1.1       cgd 	default:
    379  1.82  jdolecek 		*wrlen = 0;
    380  1.28   mycroft 		return (0);
    381   1.1       cgd 	}
    382   1.1       cgd 
    383  1.28   mycroft 	len = strlen(*bufp);
    384  1.28   mycroft 	if (len <= off)
    385  1.82  jdolecek 		*wrlen = 0;
    386  1.82  jdolecek 	else {
    387  1.82  jdolecek 		*bufp += off;
    388  1.82  jdolecek 		*wrlen = len - off;
    389  1.82  jdolecek 	}
    390  1.82  jdolecek 	return (0);
    391   1.1       cgd }
    392   1.1       cgd 
    393  1.82  jdolecek static int
    394  1.90    itojun kernfs_xwrite(kfs, buf, len)
    395  1.90    itojun 	const struct kernfs_node *kfs;
    396   1.1       cgd 	char *buf;
    397  1.82  jdolecek 	size_t len;
    398   1.1       cgd {
    399  1.23   mycroft 
    400  1.90    itojun 	switch (kfs->kfs_type) {
    401  1.90    itojun 	case Phostname:
    402   1.1       cgd 		if (buf[len-1] == '\n')
    403   1.1       cgd 			--len;
    404  1.60     perry 		memcpy(hostname, buf, len);
    405  1.17       cgd 		hostname[len] = '\0';
    406  1.82  jdolecek 		hostnamelen = (size_t) len;
    407   1.1       cgd 		return (0);
    408   1.1       cgd 
    409   1.1       cgd 	default:
    410   1.1       cgd 		return (EIO);
    411   1.1       cgd 	}
    412   1.1       cgd }
    413   1.1       cgd 
    414  1.17       cgd 
    415  1.17       cgd /*
    416   1.1       cgd  * vp is the current namei directory
    417   1.1       cgd  * ndp is the name to locate in that directory...
    418   1.1       cgd  */
    419  1.41  christos int
    420  1.41  christos kernfs_lookup(v)
    421  1.41  christos 	void *v;
    422  1.41  christos {
    423  1.23   mycroft 	struct vop_lookup_args /* {
    424  1.23   mycroft 		struct vnode * a_dvp;
    425  1.23   mycroft 		struct vnode ** a_vpp;
    426  1.23   mycroft 		struct componentname * a_cnp;
    427  1.41  christos 	} */ *ap = v;
    428  1.23   mycroft 	struct componentname *cnp = ap->a_cnp;
    429  1.23   mycroft 	struct vnode **vpp = ap->a_vpp;
    430  1.23   mycroft 	struct vnode *dvp = ap->a_dvp;
    431  1.48       cgd 	const char *pname = cnp->cn_nameptr;
    432  1.90    itojun 	const struct kernfs_node *kfs;
    433  1.75  jdolecek 	const struct kern_target *kt;
    434  1.64  wrstuden 	int error, i, wantpunlock;
    435  1.90    itojun #ifdef IPSEC
    436  1.90    itojun 	char *ep;
    437  1.90    itojun 	u_int32_t id;
    438   1.1       cgd #endif
    439  1.23   mycroft 
    440  1.35   mycroft 	*vpp = NULLVP;
    441  1.64  wrstuden 	cnp->cn_flags &= ~PDIRUNLOCK;
    442  1.35   mycroft 
    443  1.35   mycroft 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    444  1.35   mycroft 		return (EROFS);
    445  1.35   mycroft 
    446  1.23   mycroft 	if (cnp->cn_namelen == 1 && *pname == '.') {
    447  1.23   mycroft 		*vpp = dvp;
    448   1.1       cgd 		VREF(dvp);
    449   1.1       cgd 		return (0);
    450   1.1       cgd 	}
    451  1.13       cgd 
    452  1.90    itojun 	wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
    453  1.90    itojun 	kfs = VTOKERN(dvp);
    454  1.90    itojun 	switch (kfs->kfs_type) {
    455  1.90    itojun 	case Pkern:
    456  1.90    itojun 		/*
    457  1.90    itojun 		 * Shouldn't get here with .. in the root node.
    458  1.90    itojun 		 */
    459  1.90    itojun 		if (cnp->cn_flags & ISDOTDOT)
    460  1.90    itojun 			return (EIO);
    461  1.64  wrstuden 
    462  1.90    itojun 		for (i = 0; i < nkern_targets; i++) {
    463  1.90    itojun 			kt = &kern_targets[i];
    464  1.90    itojun 			if (cnp->cn_namelen == kt->kt_namlen &&
    465  1.90    itojun 			    memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
    466  1.90    itojun 				goto found;
    467  1.90    itojun 		}
    468  1.90    itojun 		break;
    469  1.25   mycroft 
    470  1.90    itojun 	found:
    471  1.90    itojun 		error = kernfs_allocvp(dvp->v_mount, vpp, kt->kt_tag, kt, 0);
    472  1.90    itojun 		if ((error == 0) && wantpunlock) {
    473  1.90    itojun 			VOP_UNLOCK(dvp, 0);
    474  1.90    itojun 			cnp->cn_flags |= PDIRUNLOCK;
    475  1.90    itojun 		}
    476  1.90    itojun 		return (error);
    477  1.64  wrstuden 
    478  1.90    itojun #ifdef IPSEC
    479  1.90    itojun 	case Pipsecsadir:
    480  1.90    itojun 		for (i = 0; i < nipsecsa_targets; i++) {
    481  1.90    itojun 			kt = &ipsecsa_targets[i];
    482  1.90    itojun 			if (cnp->cn_namelen == kt->kt_namlen &&
    483  1.90    itojun 			    memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0) {
    484  1.90    itojun 				error = kernfs_allocvp(dvp->v_mount, vpp,
    485  1.90    itojun 				    kt->kt_tag, kt, 0);
    486  1.90    itojun 				if ((error == 0) && wantpunlock) {
    487  1.90    itojun 					VOP_UNLOCK(dvp, 0);
    488  1.90    itojun 					cnp->cn_flags |= PDIRUNLOCK;
    489  1.90    itojun 				}
    490  1.90    itojun 				return (error);
    491  1.90    itojun 			}
    492  1.90    itojun 		}
    493   1.1       cgd 
    494  1.90    itojun 		ep = NULL;
    495  1.90    itojun 		id = strtoul(pname, &ep, 10);
    496  1.90    itojun 		if (!ep || *ep || ep == pname)
    497  1.90    itojun 			break;
    498   1.1       cgd 
    499  1.90    itojun 		error = kernfs_allocvp(dvp->v_mount, vpp, Pipsecsa, NULL, id);
    500  1.90    itojun 		if ((error == 0) && wantpunlock) {
    501  1.90    itojun 			VOP_UNLOCK(dvp, 0);
    502  1.90    itojun 			cnp->cn_flags |= PDIRUNLOCK;
    503  1.90    itojun 		}
    504  1.90    itojun 		return (error);
    505  1.23   mycroft 
    506  1.90    itojun 	case Pipsecspdir:
    507  1.90    itojun 		for (i = 0; i < nipsecsp_targets; i++) {
    508  1.90    itojun 			kt = &ipsecsp_targets[i];
    509  1.90    itojun 			if (cnp->cn_namelen == kt->kt_namlen &&
    510  1.90    itojun 			    memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0) {
    511  1.90    itojun 				error = kernfs_allocvp(dvp->v_mount, vpp,
    512  1.90    itojun 				    kt->kt_tag, kt, 0);
    513  1.90    itojun 				if ((error == 0) && wantpunlock) {
    514  1.90    itojun 					VOP_UNLOCK(dvp, 0);
    515  1.90    itojun 					cnp->cn_flags |= PDIRUNLOCK;
    516  1.90    itojun 				}
    517  1.90    itojun 				return (error);
    518  1.90    itojun 			}
    519  1.57      fvdl 		}
    520  1.90    itojun 
    521  1.90    itojun 		ep = NULL;
    522  1.90    itojun 		id = strtoul(pname, &ep, 10);
    523  1.90    itojun 		if (!ep || *ep || ep == pname)
    524  1.90    itojun 			break;
    525  1.90    itojun 
    526  1.90    itojun 		error = kernfs_allocvp(dvp->v_mount, vpp, Pipsecsp, NULL, id);
    527  1.90    itojun 		if ((error == 0) && wantpunlock) {
    528  1.64  wrstuden 			VOP_UNLOCK(dvp, 0);
    529  1.64  wrstuden 			cnp->cn_flags |= PDIRUNLOCK;
    530  1.64  wrstuden 		}
    531  1.90    itojun 		return (error);
    532  1.90    itojun #endif
    533  1.90    itojun 
    534  1.90    itojun 	default:
    535  1.90    itojun 		return (ENOTDIR);
    536  1.90    itojun 	}
    537  1.90    itojun 
    538  1.90    itojun 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
    539  1.90    itojun }
    540  1.90    itojun 
    541  1.90    itojun int
    542  1.90    itojun kernfs_open(v)
    543  1.90    itojun 	void *v;
    544  1.90    itojun {
    545  1.90    itojun 	struct vop_open_args /* {
    546  1.90    itojun 		struct vnode *a_vp;
    547  1.90    itojun 		int a_mode;
    548  1.90    itojun 		struct ucred *a_cred;
    549  1.90    itojun 		struct proc *a_p;
    550  1.90    itojun 	} */ *ap = v;
    551  1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    552  1.90    itojun #ifdef IPSEC
    553  1.90    itojun 	struct mbuf *m;
    554  1.90    itojun 	struct secpolicy *sp;
    555  1.90    itojun #endif
    556  1.90    itojun 
    557  1.90    itojun 	switch (kfs->kfs_type) {
    558  1.90    itojun #ifdef IPSEC
    559  1.90    itojun 	case Pipsecsa:
    560  1.90    itojun 		m = key_setdumpsa_spi(htonl(kfs->kfs_value));
    561  1.90    itojun 		if (m) {
    562  1.90    itojun 			m_freem(m);
    563  1.90    itojun 			return (0);
    564  1.90    itojun 		} else
    565  1.90    itojun 			return (ENOENT);
    566  1.90    itojun 
    567  1.90    itojun 	case Pipsecsp:
    568  1.90    itojun 		sp = key_getspbyid(kfs->kfs_value);
    569  1.90    itojun 		if (sp) {
    570  1.90    itojun 			kfs->kfs_v = sp;
    571  1.90    itojun 			return (0);
    572  1.90    itojun 		} else
    573  1.90    itojun 			return (ENOENT);
    574  1.90    itojun #endif
    575  1.90    itojun 
    576  1.90    itojun 	default:
    577  1.23   mycroft 		return (0);
    578  1.23   mycroft 	}
    579  1.90    itojun }
    580   1.1       cgd 
    581  1.90    itojun int
    582  1.90    itojun kernfs_close(v)
    583  1.90    itojun 	void *v;
    584  1.90    itojun {
    585  1.90    itojun 	struct vop_close_args /* {
    586  1.90    itojun 		struct vnode *a_vp;
    587  1.90    itojun 		int a_fflag;
    588  1.90    itojun 		struct ucred *a_cred;
    589  1.90    itojun 		struct proc *a_p;
    590  1.90    itojun 	} */ *ap = v;
    591  1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    592  1.90    itojun 
    593  1.90    itojun 	switch (kfs->kfs_type) {
    594  1.90    itojun #ifdef IPSEC
    595  1.90    itojun 	case Pipsecsp:
    596  1.90    itojun 		key_freesp((struct secpolicy *)kfs->kfs_v);
    597  1.90    itojun 		break;
    598   1.1       cgd #endif
    599  1.90    itojun 
    600  1.90    itojun 	default:
    601  1.90    itojun 		break;
    602  1.57      fvdl 	}
    603  1.23   mycroft 
    604   1.1       cgd 	return (0);
    605   1.1       cgd }
    606   1.1       cgd 
    607  1.28   mycroft int
    608  1.41  christos kernfs_access(v)
    609  1.41  christos 	void *v;
    610  1.41  christos {
    611  1.23   mycroft 	struct vop_access_args /* {
    612  1.23   mycroft 		struct vnode *a_vp;
    613  1.34   mycroft 		int a_mode;
    614  1.23   mycroft 		struct ucred *a_cred;
    615  1.23   mycroft 		struct proc *a_p;
    616  1.41  christos 	} */ *ap = v;
    617  1.90    itojun 	struct vattr va;
    618  1.90    itojun 	int error;
    619  1.17       cgd 
    620  1.90    itojun 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
    621  1.90    itojun 		return (error);
    622  1.49   mycroft 
    623  1.90    itojun 	return (vaccess(va.va_type, va.va_mode, va.va_uid, va.va_gid,
    624  1.90    itojun 	    ap->a_mode, ap->a_cred));
    625  1.23   mycroft }
    626  1.23   mycroft 
    627  1.41  christos int
    628  1.41  christos kernfs_getattr(v)
    629  1.41  christos 	void *v;
    630  1.41  christos {
    631  1.23   mycroft 	struct vop_getattr_args /* {
    632  1.23   mycroft 		struct vnode *a_vp;
    633  1.23   mycroft 		struct vattr *a_vap;
    634  1.23   mycroft 		struct ucred *a_cred;
    635  1.23   mycroft 		struct proc *a_p;
    636  1.41  christos 	} */ *ap = v;
    637  1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    638  1.23   mycroft 	struct vattr *vap = ap->a_vap;
    639   1.1       cgd 	int error = 0;
    640  1.28   mycroft 	char strbuf[KSTRING], *buf;
    641  1.90    itojun 	size_t nread, total;
    642   1.1       cgd 
    643  1.90    itojun 	VATTR_NULL(vap);
    644  1.90    itojun 	vap->va_type = ap->a_vp->v_type;
    645  1.17       cgd 	vap->va_uid = 0;
    646  1.17       cgd 	vap->va_gid = 0;
    647  1.90    itojun 	vap->va_mode = kfs->kfs_mode;
    648  1.90    itojun 	vap->va_fileid = kfs->kfs_fileno;
    649  1.90    itojun 	vap->va_flags = 0;
    650  1.23   mycroft 	vap->va_size = 0;
    651   1.1       cgd 	vap->va_blocksize = DEV_BSIZE;
    652  1.81     lukem 	/*
    653  1.92       dan 	 * Make all times be current TOD, except for the "boottime" node.
    654  1.93    simonb 	 * Avoid microtime(9), it's slow.
    655  1.83    simonb 	 * We don't guard the read from time(9) with splclock(9) since we
    656  1.83    simonb 	 * don't actually need to be THAT sure the access is atomic.
    657  1.81     lukem 	 */
    658  1.92       dan 	if (kfs->kfs_kt->kt_namlen == 8 &&
    659  1.92       dan 	    !memcmp(kfs->kfs_kt->kt_name, "boottime", 8)) {
    660  1.92       dan 		TIMEVAL_TO_TIMESPEC(&boottime, &vap->va_ctime);
    661  1.92       dan 	} else {
    662  1.92       dan 		TIMEVAL_TO_TIMESPEC(&time, &vap->va_ctime);
    663  1.92       dan 	}
    664  1.81     lukem 	vap->va_atime = vap->va_mtime = vap->va_ctime;
    665   1.1       cgd 	vap->va_gen = 0;
    666   1.1       cgd 	vap->va_flags = 0;
    667   1.1       cgd 	vap->va_rdev = 0;
    668   1.1       cgd 	vap->va_bytes = 0;
    669   1.1       cgd 
    670  1.90    itojun 	switch (kfs->kfs_type) {
    671  1.90    itojun 	case Pkern:
    672  1.90    itojun 		vap->va_nlink = 4;
    673  1.90    itojun 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    674  1.90    itojun 		break;
    675  1.90    itojun 
    676  1.90    itojun 	case Proot:
    677  1.90    itojun 		vap->va_nlink = 1;
    678  1.90    itojun 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    679  1.90    itojun 		break;
    680  1.90    itojun 
    681  1.90    itojun 	case Pnull:
    682  1.90    itojun 	case Ptime:
    683  1.90    itojun 	case Pint:
    684  1.90    itojun 	case Pstring:
    685  1.90    itojun 	case Phostname:
    686  1.90    itojun 	case Pavenrun:
    687  1.90    itojun 	case Pdevice:
    688  1.90    itojun 	case Pmsgbuf:
    689  1.90    itojun #ifdef IPSEC
    690  1.90    itojun 	case Pipsecsa:
    691  1.90    itojun 	case Pipsecsp:
    692   1.1       cgd #endif
    693   1.1       cgd 		vap->va_nlink = 1;
    694  1.84  jdolecek 		total = 0;
    695  1.84  jdolecek 		do {
    696  1.84  jdolecek 			buf = strbuf;
    697  1.90    itojun 			error = kernfs_xread(kfs, total, &buf,
    698  1.90    itojun 			    sizeof(strbuf), &nread);
    699  1.84  jdolecek 			total += nread;
    700  1.84  jdolecek 		} while (error == 0 && nread != 0);
    701  1.90    itojun 		vap->va_bytes = vap->va_size = total;
    702  1.90    itojun 		break;
    703  1.90    itojun 
    704  1.90    itojun #ifdef IPSEC
    705  1.90    itojun 	case Pipsecsadir:
    706  1.90    itojun 	case Pipsecspdir:
    707  1.90    itojun 		vap->va_nlink = 2;
    708  1.90    itojun 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    709  1.90    itojun 		break;
    710  1.90    itojun #endif
    711  1.90    itojun 
    712  1.90    itojun 	default:
    713  1.90    itojun 		error = EINVAL;
    714  1.90    itojun 		break;
    715   1.1       cgd 	}
    716   1.1       cgd 
    717   1.1       cgd 	return (error);
    718   1.1       cgd }
    719   1.1       cgd 
    720  1.41  christos /*ARGSUSED*/
    721  1.41  christos int
    722  1.41  christos kernfs_setattr(v)
    723  1.41  christos 	void *v;
    724   1.1       cgd {
    725  1.90    itojun 
    726   1.1       cgd 	/*
    727  1.17       cgd 	 * Silently ignore attribute changes.
    728  1.17       cgd 	 * This allows for open with truncate to have no
    729  1.17       cgd 	 * effect until some data is written.  I want to
    730  1.17       cgd 	 * do it this way because all writes are atomic.
    731   1.1       cgd 	 */
    732  1.17       cgd 	return (0);
    733   1.1       cgd }
    734   1.1       cgd 
    735  1.28   mycroft int
    736  1.41  christos kernfs_read(v)
    737  1.41  christos 	void *v;
    738  1.41  christos {
    739  1.23   mycroft 	struct vop_read_args /* {
    740  1.23   mycroft 		struct vnode *a_vp;
    741  1.23   mycroft 		struct uio *a_uio;
    742  1.23   mycroft 		int  a_ioflag;
    743  1.23   mycroft 		struct ucred *a_cred;
    744  1.41  christos 	} */ *ap = v;
    745  1.23   mycroft 	struct uio *uio = ap->a_uio;
    746  1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    747  1.28   mycroft 	char strbuf[KSTRING], *buf;
    748  1.82  jdolecek 	off_t off;
    749  1.82  jdolecek 	size_t len;
    750  1.28   mycroft 	int error;
    751  1.23   mycroft 
    752  1.90    itojun 	if (ap->a_vp->v_type == VDIR)
    753  1.23   mycroft 		return (EOPNOTSUPP);
    754  1.23   mycroft 
    755  1.28   mycroft 	off = uio->uio_offset;
    756  1.82  jdolecek 	buf = strbuf;
    757  1.90    itojun 	if ((error = kernfs_xread(kfs, off, &buf, sizeof(strbuf), &len)) == 0)
    758  1.82  jdolecek 		error = uiomove(buf, len, uio);
    759  1.82  jdolecek 	return (error);
    760   1.1       cgd }
    761   1.1       cgd 
    762  1.28   mycroft int
    763  1.41  christos kernfs_write(v)
    764  1.41  christos 	void *v;
    765  1.41  christos {
    766  1.23   mycroft 	struct vop_write_args /* {
    767  1.23   mycroft 		struct vnode *a_vp;
    768  1.23   mycroft 		struct uio *a_uio;
    769  1.23   mycroft 		int  a_ioflag;
    770  1.23   mycroft 		struct ucred *a_cred;
    771  1.41  christos 	} */ *ap = v;
    772  1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    773  1.23   mycroft 	struct uio *uio = ap->a_uio;
    774  1.23   mycroft 	int error, xlen;
    775   1.1       cgd 	char strbuf[KSTRING];
    776  1.23   mycroft 
    777   1.1       cgd 	if (uio->uio_offset != 0)
    778   1.1       cgd 		return (EINVAL);
    779   1.1       cgd 
    780   1.1       cgd 	xlen = min(uio->uio_resid, KSTRING-1);
    781  1.41  christos 	if ((error = uiomove(strbuf, xlen, uio)) != 0)
    782   1.1       cgd 		return (error);
    783   1.1       cgd 
    784   1.1       cgd 	if (uio->uio_resid != 0)
    785   1.1       cgd 		return (EIO);
    786   1.1       cgd 
    787   1.1       cgd 	strbuf[xlen] = '\0';
    788  1.17       cgd 	xlen = strlen(strbuf);
    789  1.90    itojun 	return (kernfs_xwrite(kfs, strbuf, xlen));
    790   1.1       cgd }
    791   1.1       cgd 
    792  1.41  christos int
    793  1.41  christos kernfs_readdir(v)
    794  1.41  christos 	void *v;
    795  1.41  christos {
    796  1.23   mycroft 	struct vop_readdir_args /* {
    797  1.23   mycroft 		struct vnode *a_vp;
    798  1.23   mycroft 		struct uio *a_uio;
    799  1.23   mycroft 		struct ucred *a_cred;
    800  1.26   mycroft 		int *a_eofflag;
    801  1.57      fvdl 		off_t **a_cookies;
    802  1.57      fvdl 		int a_*ncookies;
    803  1.41  christos 	} */ *ap = v;
    804  1.23   mycroft 	struct uio *uio = ap->a_uio;
    805  1.37   mycroft 	struct dirent d;
    806  1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    807  1.75  jdolecek 	const struct kern_target *kt;
    808  1.67  sommerfe 	off_t i;
    809   1.1       cgd 	int error;
    810  1.57      fvdl 	off_t *cookies = NULL;
    811  1.90    itojun 	int ncookies = 0, n;
    812  1.90    itojun #ifdef IPSEC
    813  1.90    itojun 	struct secasvar *sav, *sav2;
    814  1.90    itojun 	struct secpolicy *sp;
    815  1.90    itojun #endif
    816  1.23   mycroft 
    817  1.37   mycroft 	if (uio->uio_resid < UIO_MX)
    818  1.37   mycroft 		return (EINVAL);
    819  1.38   mycroft 	if (uio->uio_offset < 0)
    820  1.37   mycroft 		return (EINVAL);
    821  1.26   mycroft 
    822   1.1       cgd 	error = 0;
    823  1.38   mycroft 	i = uio->uio_offset;
    824  1.90    itojun 	memset(&d, 0, sizeof(d));
    825  1.90    itojun 	d.d_reclen = UIO_MX;
    826  1.90    itojun 	ncookies = uio->uio_resid / UIO_MX;
    827  1.90    itojun 
    828  1.90    itojun 	switch (kfs->kfs_type) {
    829  1.90    itojun 	case Pkern:
    830  1.90    itojun 		if (i >= nkern_targets)
    831  1.90    itojun 			return (0);
    832  1.66  sommerfe 
    833  1.90    itojun 		if (ap->a_ncookies) {
    834  1.90    itojun 			ncookies = min(ncookies, (nkern_targets - i));
    835  1.90    itojun 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
    836  1.90    itojun 			    M_WAITOK);
    837  1.90    itojun 			*ap->a_cookies = cookies;
    838  1.90    itojun 		}
    839  1.90    itojun 
    840  1.90    itojun 		n = 0;
    841  1.90    itojun 		for (; i < nkern_targets && uio->uio_resid >= UIO_MX; i++) {
    842  1.90    itojun 			kt = &kern_targets[i];
    843  1.90    itojun 			d.d_namlen = kt->kt_namlen;
    844  1.90    itojun 			if (i < 2)
    845  1.90    itojun 				d.d_fileno = KERNFS_FILENO(&kern_targets[0],
    846  1.90    itojun 				    kern_targets[0].kt_tag, 0);
    847  1.90    itojun 			else
    848  1.90    itojun 				d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
    849  1.90    itojun 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
    850  1.90    itojun 			d.d_type = kt->kt_type;
    851  1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    852  1.90    itojun 				break;
    853  1.90    itojun 			if (cookies)
    854  1.90    itojun 				*cookies++ = i + 1;
    855  1.90    itojun 			n++;
    856  1.90    itojun 		}
    857  1.90    itojun 		ncookies = n;
    858  1.90    itojun 		break;
    859  1.90    itojun 
    860  1.90    itojun 	case Proot:
    861  1.90    itojun 		if (i >= 2)
    862  1.90    itojun 			return 0;
    863  1.90    itojun 
    864  1.90    itojun 		if (ap->a_ncookies) {
    865  1.90    itojun 			ncookies = min(ncookies, (2 - i));
    866  1.90    itojun 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
    867  1.90    itojun 			    M_WAITOK);
    868  1.90    itojun 			*ap->a_cookies = cookies;
    869  1.90    itojun 		}
    870  1.90    itojun 
    871  1.90    itojun 		n = 0;
    872  1.90    itojun 		for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
    873  1.90    itojun 			kt = &kern_targets[i];
    874  1.90    itojun 			d.d_namlen = kt->kt_namlen;
    875  1.90    itojun 			d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
    876  1.90    itojun 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
    877  1.90    itojun 			d.d_type = kt->kt_type;
    878  1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    879  1.90    itojun 				break;
    880  1.90    itojun 			if (cookies)
    881  1.90    itojun 				*cookies++ = i + 1;
    882  1.90    itojun 			n++;
    883  1.90    itojun 		}
    884  1.90    itojun 		ncookies = n;
    885  1.90    itojun 		break;
    886  1.90    itojun 
    887  1.90    itojun #ifdef IPSEC
    888  1.90    itojun 	case Pipsecsadir:
    889  1.90    itojun 		/* count SA in the system */
    890  1.90    itojun 		n = 0;
    891  1.90    itojun 		TAILQ_FOREACH(sav, &satailq, tailq) {
    892  1.90    itojun 			for (sav2 = TAILQ_FIRST(&satailq);
    893  1.90    itojun 			    sav2 != sav;
    894  1.90    itojun 			    sav2 = TAILQ_NEXT(sav2, tailq)) {
    895  1.90    itojun 				if (sav->spi == sav2->spi) {
    896  1.90    itojun 					/* multiple SA with same SPI */
    897  1.90    itojun 					break;
    898  1.90    itojun 				}
    899  1.90    itojun 			}
    900  1.90    itojun 			if (sav == sav2 || sav->spi != sav2->spi)
    901  1.90    itojun 				n++;
    902  1.90    itojun 		}
    903  1.37   mycroft 
    904  1.90    itojun 		if (i >= nipsecsa_targets + n)
    905  1.90    itojun 			return (0);
    906  1.57      fvdl 
    907  1.90    itojun 		if (ap->a_ncookies) {
    908  1.90    itojun 			ncookies = min(ncookies, (n - i));
    909  1.90    itojun 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
    910  1.90    itojun 			    M_WAITOK);
    911  1.90    itojun 			*ap->a_cookies = cookies;
    912  1.90    itojun 		}
    913  1.26   mycroft 
    914  1.90    itojun 		n = 0;
    915  1.90    itojun 		for (; i < nipsecsa_targets && uio->uio_resid >= UIO_MX; i++) {
    916  1.90    itojun 			kt = &ipsecsa_targets[i];
    917  1.90    itojun 			d.d_namlen = kt->kt_namlen;
    918  1.90    itojun 			d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
    919  1.90    itojun 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
    920  1.90    itojun 			d.d_type = kt->kt_type;
    921  1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    922  1.90    itojun 				break;
    923  1.90    itojun 			if (cookies)
    924  1.90    itojun 				*cookies++ = i + 1;
    925  1.90    itojun 			n++;
    926  1.90    itojun 		}
    927  1.90    itojun 		if (error) {
    928  1.90    itojun 			ncookies = n;
    929  1.90    itojun 			break;
    930  1.90    itojun 		}
    931  1.26   mycroft 
    932  1.90    itojun 		TAILQ_FOREACH(sav, &satailq, tailq) {
    933  1.90    itojun 			for (sav2 = TAILQ_FIRST(&satailq);
    934  1.90    itojun 			    sav2 != sav;
    935  1.90    itojun 			    sav2 = TAILQ_NEXT(sav2, tailq)) {
    936  1.90    itojun 				if (sav->spi == sav2->spi) {
    937  1.90    itojun 					/* multiple SA with same SPI */
    938  1.90    itojun 					break;
    939  1.90    itojun 				}
    940  1.90    itojun 			}
    941  1.90    itojun 			if (sav != sav2 && sav->spi == sav2->spi)
    942  1.23   mycroft 				continue;
    943  1.90    itojun 			if (uio->uio_resid < UIO_MX)
    944  1.90    itojun 				break;
    945  1.90    itojun 			d.d_fileno = KERNFS_FILENO(kfs->kfs_kt, kfs->kfs_type,
    946  1.90    itojun 			    kfs->kfs_cookie);
    947  1.90    itojun 			d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
    948  1.90    itojun 			    "%u", ntohl(sav->spi));
    949  1.90    itojun 			d.d_type = DT_REG;
    950  1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    951  1.90    itojun 				break;
    952  1.90    itojun 			if (cookies)
    953  1.90    itojun 				*cookies++ = i + 1;
    954  1.90    itojun 			n++;
    955  1.90    itojun 			i++;
    956  1.23   mycroft 		}
    957  1.90    itojun 		ncookies = n;
    958  1.90    itojun 		break;
    959  1.90    itojun 
    960  1.90    itojun 	case Pipsecspdir:
    961  1.90    itojun 		/* count SP in the system */
    962  1.90    itojun 		n = 0;
    963  1.90    itojun 		TAILQ_FOREACH(sp, &sptailq, tailq)
    964  1.90    itojun 			n++;
    965  1.26   mycroft 
    966  1.90    itojun 		if (i >= 2 + n)
    967  1.90    itojun 			return (0);
    968  1.90    itojun 
    969  1.90    itojun 		if (ap->a_ncookies) {
    970  1.90    itojun 			ncookies = min(ncookies, (n - i));
    971  1.90    itojun 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
    972  1.90    itojun 			    M_WAITOK);
    973  1.90    itojun 			*ap->a_cookies = cookies;
    974  1.90    itojun 		}
    975  1.26   mycroft 
    976  1.90    itojun 		n = 0;
    977  1.90    itojun 		for (; i < nipsecsp_targets && uio->uio_resid >= UIO_MX; i++) {
    978  1.90    itojun 			kt = &ipsecsp_targets[i];
    979  1.90    itojun 			d.d_namlen = kt->kt_namlen;
    980  1.90    itojun 			d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
    981  1.90    itojun 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
    982  1.90    itojun 			d.d_type = kt->kt_type;
    983  1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
    984  1.90    itojun 				break;
    985  1.90    itojun 			if (cookies)
    986  1.90    itojun 				*cookies++ = i + 1;
    987  1.90    itojun 			n++;
    988  1.90    itojun 		}
    989  1.90    itojun 		if (error) {
    990  1.90    itojun 			ncookies = n;
    991   1.1       cgd 			break;
    992  1.57      fvdl 		}
    993  1.90    itojun 
    994  1.90    itojun 		TAILQ_FOREACH(sp, &sptailq, tailq) {
    995  1.90    itojun 			if (uio->uio_resid < UIO_MX)
    996  1.90    itojun 				break;
    997  1.90    itojun 			d.d_fileno = KERNFS_FILENO(kfs->kfs_kt, kfs->kfs_type,
    998  1.90    itojun 			    kfs->kfs_cookie);
    999  1.90    itojun 			d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
   1000  1.90    itojun 			    "%u", sp->id);
   1001  1.90    itojun 			d.d_type = DT_REG;
   1002  1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1003  1.90    itojun 				break;
   1004  1.90    itojun 			if (cookies)
   1005  1.90    itojun 				*cookies++ = i + 1;
   1006  1.90    itojun 			n++;
   1007  1.90    itojun 			i++;
   1008  1.90    itojun 		}
   1009  1.90    itojun 		ncookies = n;
   1010  1.90    itojun 		break;
   1011  1.90    itojun #endif
   1012  1.90    itojun 
   1013  1.90    itojun 	default:
   1014  1.90    itojun 		error = ENOTDIR;
   1015  1.90    itojun 		break;
   1016  1.57      fvdl 	}
   1017  1.57      fvdl 
   1018  1.57      fvdl 	if (ap->a_ncookies) {
   1019  1.57      fvdl 		if (error) {
   1020  1.90    itojun 			if (cookies)
   1021  1.90    itojun 				free(*ap->a_cookies, M_TEMP);
   1022  1.57      fvdl 			*ap->a_ncookies = 0;
   1023  1.57      fvdl 			*ap->a_cookies = NULL;
   1024  1.57      fvdl 		} else
   1025  1.57      fvdl 			*ap->a_ncookies = ncookies;
   1026   1.1       cgd 	}
   1027   1.1       cgd 
   1028  1.38   mycroft 	uio->uio_offset = i;
   1029   1.1       cgd 	return (error);
   1030   1.1       cgd }
   1031   1.1       cgd 
   1032  1.41  christos int
   1033  1.41  christos kernfs_inactive(v)
   1034  1.41  christos 	void *v;
   1035  1.41  christos {
   1036  1.23   mycroft 	struct vop_inactive_args /* {
   1037  1.23   mycroft 		struct vnode *a_vp;
   1038  1.57      fvdl 		struct proc *a_p;
   1039  1.41  christos 	} */ *ap = v;
   1040  1.23   mycroft 	struct vnode *vp = ap->a_vp;
   1041  1.90    itojun 	const struct kernfs_node *kfs = VTOKERN(ap->a_vp);
   1042  1.90    itojun #ifdef IPSEC
   1043  1.90    itojun 	struct mbuf *m;
   1044  1.90    itojun 	struct secpolicy *sp;
   1045  1.90    itojun #endif
   1046  1.23   mycroft 
   1047  1.90    itojun 	VOP_UNLOCK(vp, 0);
   1048  1.90    itojun 	switch (kfs->kfs_type) {
   1049  1.90    itojun #ifdef IPSEC
   1050  1.90    itojun 	case Pipsecsa:
   1051  1.90    itojun 		m = key_setdumpsa_spi(htonl(kfs->kfs_value));
   1052  1.90    itojun 		if (m)
   1053  1.90    itojun 			m_freem(m);
   1054  1.91    itojun 		else
   1055  1.90    itojun 			vgone(vp);
   1056  1.90    itojun 		break;
   1057  1.90    itojun 	case Pipsecsp:
   1058  1.90    itojun 		sp = key_getspbyid(kfs->kfs_value);
   1059  1.90    itojun 		if (sp)
   1060  1.90    itojun 			key_freesp(sp);
   1061  1.90    itojun 		else {
   1062  1.90    itojun 			/* should never happen as we hold a refcnt */
   1063  1.90    itojun 			vgone(vp);
   1064  1.90    itojun 		}
   1065  1.90    itojun 		break;
   1066  1.23   mycroft #endif
   1067  1.90    itojun 	default:
   1068  1.90    itojun 		break;
   1069  1.90    itojun 	}
   1070  1.23   mycroft 	return (0);
   1071  1.23   mycroft }
   1072  1.23   mycroft 
   1073  1.41  christos int
   1074  1.41  christos kernfs_reclaim(v)
   1075  1.41  christos 	void *v;
   1076  1.41  christos {
   1077  1.23   mycroft 	struct vop_reclaim_args /* {
   1078  1.23   mycroft 		struct vnode *a_vp;
   1079  1.41  christos 	} */ *ap = v;
   1080  1.23   mycroft 
   1081  1.90    itojun 	return (kernfs_freevp(ap->a_vp));
   1082   1.1       cgd }
   1083   1.1       cgd 
   1084   1.1       cgd /*
   1085  1.23   mycroft  * Return POSIX pathconf information applicable to special devices.
   1086  1.23   mycroft  */
   1087  1.41  christos int
   1088  1.41  christos kernfs_pathconf(v)
   1089  1.41  christos 	void *v;
   1090  1.41  christos {
   1091  1.23   mycroft 	struct vop_pathconf_args /* {
   1092  1.23   mycroft 		struct vnode *a_vp;
   1093  1.23   mycroft 		int a_name;
   1094  1.29       cgd 		register_t *a_retval;
   1095  1.41  christos 	} */ *ap = v;
   1096  1.23   mycroft 
   1097  1.23   mycroft 	switch (ap->a_name) {
   1098  1.23   mycroft 	case _PC_LINK_MAX:
   1099  1.23   mycroft 		*ap->a_retval = LINK_MAX;
   1100  1.23   mycroft 		return (0);
   1101  1.23   mycroft 	case _PC_MAX_CANON:
   1102  1.23   mycroft 		*ap->a_retval = MAX_CANON;
   1103  1.23   mycroft 		return (0);
   1104  1.23   mycroft 	case _PC_MAX_INPUT:
   1105  1.23   mycroft 		*ap->a_retval = MAX_INPUT;
   1106  1.23   mycroft 		return (0);
   1107  1.23   mycroft 	case _PC_PIPE_BUF:
   1108  1.23   mycroft 		*ap->a_retval = PIPE_BUF;
   1109  1.23   mycroft 		return (0);
   1110  1.23   mycroft 	case _PC_CHOWN_RESTRICTED:
   1111  1.23   mycroft 		*ap->a_retval = 1;
   1112  1.23   mycroft 		return (0);
   1113  1.23   mycroft 	case _PC_VDISABLE:
   1114  1.23   mycroft 		*ap->a_retval = _POSIX_VDISABLE;
   1115  1.59    kleink 		return (0);
   1116  1.59    kleink 	case _PC_SYNC_IO:
   1117  1.59    kleink 		*ap->a_retval = 1;
   1118  1.23   mycroft 		return (0);
   1119  1.23   mycroft 	default:
   1120  1.23   mycroft 		return (EINVAL);
   1121  1.23   mycroft 	}
   1122  1.23   mycroft 	/* NOTREACHED */
   1123  1.23   mycroft }
   1124  1.23   mycroft 
   1125  1.23   mycroft /*
   1126  1.23   mycroft  * Print out the contents of a /dev/fd vnode.
   1127   1.1       cgd  */
   1128   1.1       cgd /* ARGSUSED */
   1129  1.41  christos int
   1130  1.41  christos kernfs_print(v)
   1131  1.41  christos 	void *v;
   1132  1.23   mycroft {
   1133  1.23   mycroft 
   1134  1.47  christos 	printf("tag VT_KERNFS, kernfs vnode\n");
   1135  1.23   mycroft 	return (0);
   1136  1.23   mycroft }
   1137  1.23   mycroft 
   1138  1.40   mycroft int
   1139  1.41  christos kernfs_link(v)
   1140  1.41  christos 	void *v;
   1141  1.41  christos {
   1142  1.40   mycroft 	struct vop_link_args /* {
   1143  1.40   mycroft 		struct vnode *a_dvp;
   1144  1.40   mycroft 		struct vnode *a_vp;
   1145  1.40   mycroft 		struct componentname *a_cnp;
   1146  1.41  christos 	} */ *ap = v;
   1147  1.40   mycroft 
   1148  1.40   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1149  1.40   mycroft 	vput(ap->a_dvp);
   1150  1.40   mycroft 	return (EROFS);
   1151  1.40   mycroft }
   1152  1.40   mycroft 
   1153  1.40   mycroft int
   1154  1.41  christos kernfs_symlink(v)
   1155  1.41  christos 	void *v;
   1156  1.41  christos {
   1157  1.40   mycroft 	struct vop_symlink_args /* {
   1158  1.40   mycroft 		struct vnode *a_dvp;
   1159  1.40   mycroft 		struct vnode **a_vpp;
   1160  1.40   mycroft 		struct componentname *a_cnp;
   1161  1.40   mycroft 		struct vattr *a_vap;
   1162  1.40   mycroft 		char *a_target;
   1163  1.41  christos 	} */ *ap = v;
   1164  1.40   mycroft 
   1165  1.40   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1166  1.40   mycroft 	vput(ap->a_dvp);
   1167  1.40   mycroft 	return (EROFS);
   1168   1.1       cgd }
   1169