Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vnops.c revision 1.98.2.6
      1  1.98.2.6       riz /*	$NetBSD: kernfs_vnops.c,v 1.98.2.6 2005/09/01 17:53:25 riz 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.98.2.6       riz __KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.98.2.6 2005/09/01 17:53:25 riz 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.98.2.1       jdc #define	UREAD_MODE	(S_IRUSR)
     83  1.98.2.2      tron #define	DIR_MODE	(S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
     84  1.98.2.2      tron #define	UDIR_MODE	(S_IRUSR|S_IXUSR)
     85      1.23   mycroft 
     86      1.90    itojun #define N(s) sizeof(s)-1, s
     87      1.75  jdolecek const struct kern_target kern_targets[] = {
     88       1.1       cgd /* NOTE: The name must be less than UIO_MX-16 chars in length */
     89      1.23   mycroft      /*        name            data          tag           type  ro/rw */
     90      1.98     darcy      { DT_DIR, N("."),         0,            KFSkern,        VDIR, DIR_MODE   },
     91      1.98     darcy      { DT_DIR, N(".."),        0,            KFSroot,        VDIR, DIR_MODE   },
     92      1.98     darcy      { DT_REG, N("boottime"),  &boottime.tv_sec, KFSint,     VREG, READ_MODE  },
     93      1.69   thorpej 			/* XXX cast away const */
     94      1.69   thorpej      { DT_REG, N("copyright"), (void *)copyright,
     95      1.98     darcy      					     KFSstring,      VREG, READ_MODE  },
     96      1.98     darcy      { DT_REG, N("hostname"),  0,            KFShostname,    VREG, WRITE_MODE },
     97      1.98     darcy      { DT_REG, N("hz"),        &hz,          KFSint,         VREG, READ_MODE  },
     98      1.90    itojun #ifdef IPSEC
     99      1.98     darcy      { DT_DIR, N("ipsecsa"),   0,	     KFSipsecsadir,  VDIR, UDIR_MODE  },
    100      1.98     darcy      { DT_DIR, N("ipsecsp"),   0,	     KFSipsecspdir,  VDIR, UDIR_MODE  },
    101      1.90    itojun #endif
    102      1.98     darcy      { DT_REG, N("loadavg"),   0,            KFSavenrun,     VREG, READ_MODE  },
    103      1.98     darcy      { DT_REG, N("msgbuf"),    0,	     KFSmsgbuf,      VREG, READ_MODE  },
    104      1.98     darcy      { DT_REG, N("pagesize"),  &uvmexp.pagesize, KFSint,     VREG, READ_MODE  },
    105      1.98     darcy      { DT_REG, N("physmem"),   &physmem,     KFSint,         VREG, READ_MODE  },
    106      1.17       cgd #if 0
    107      1.98     darcy      { DT_DIR, N("root"),      0,            KFSnull,        VDIR, DIR_MODE   },
    108      1.17       cgd #endif
    109      1.98     darcy      { DT_BLK, N("rootdev"),   &rootdev,     KFSdevice,      VBLK, READ_MODE  },
    110      1.98     darcy      { DT_CHR, N("rrootdev"),  &rrootdev,    KFSdevice,      VCHR, READ_MODE  },
    111      1.98     darcy      { DT_REG, N("time"),      0,            KFStime,        VREG, READ_MODE  },
    112      1.69   thorpej 			/* XXX cast away const */
    113      1.69   thorpej      { DT_REG, N("version"),   (void *)version,
    114      1.98     darcy      					     KFSstring,      VREG, READ_MODE  },
    115      1.90    itojun };
    116  1.98.2.2      tron const struct kern_target subdir_targets[] = {
    117  1.98.2.2      tron /* NOTE: The name must be less than UIO_MX-16 chars in length */
    118  1.98.2.2      tron      /*        name            data          tag           type  ro/rw */
    119  1.98.2.2      tron      { DT_DIR, N("."),         0,            KFSsubdir,      VDIR, DIR_MODE   },
    120  1.98.2.2      tron      { DT_DIR, N(".."),        0,            KFSkern,        VDIR, DIR_MODE   },
    121  1.98.2.2      tron };
    122      1.90    itojun #ifdef IPSEC
    123      1.90    itojun const struct kern_target ipsecsa_targets[] = {
    124      1.90    itojun /* NOTE: The name must be less than UIO_MX-16 chars in length */
    125      1.90    itojun      /*        name            data          tag           type  ro/rw */
    126      1.98     darcy      { DT_DIR, N("."),         0,            KFSipsecsadir,  VDIR, DIR_MODE   },
    127      1.98     darcy      { DT_DIR, N(".."),        0,            KFSkern,        VDIR, DIR_MODE   },
    128      1.90    itojun };
    129      1.90    itojun const struct kern_target ipsecsp_targets[] = {
    130      1.90    itojun /* NOTE: The name must be less than UIO_MX-16 chars in length */
    131      1.90    itojun      /*        name            data          tag           type  ro/rw */
    132      1.98     darcy      { DT_DIR, N("."),         0,            KFSipsecspdir,  VDIR, DIR_MODE   },
    133      1.98     darcy      { DT_DIR, N(".."),        0,            KFSkern,        VDIR, DIR_MODE   },
    134      1.90    itojun };
    135  1.98.2.1       jdc const struct kern_target ipsecsa_kt =
    136  1.98.2.1       jdc      { DT_DIR, N(""),          0,            KFSipsecsa,     VREG, UREAD_MODE };
    137  1.98.2.1       jdc const struct kern_target ipsecsp_kt =
    138  1.98.2.1       jdc      { DT_DIR, N(""),          0,            KFSipsecsp,     VREG, UREAD_MODE };
    139      1.90    itojun #endif
    140      1.23   mycroft #undef N
    141  1.98.2.2      tron SIMPLEQ_HEAD(,dyn_kern_target) dyn_kern_targets =
    142  1.98.2.2      tron 	SIMPLEQ_HEAD_INITIALIZER(dyn_kern_targets);
    143      1.90    itojun int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);
    144  1.98.2.2      tron const int static_nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);
    145      1.90    itojun #ifdef IPSEC
    146      1.90    itojun int nipsecsa_targets = sizeof(ipsecsa_targets) / sizeof(ipsecsa_targets[0]);
    147      1.90    itojun int nipsecsp_targets = sizeof(ipsecsp_targets) / sizeof(ipsecsp_targets[0]);
    148  1.98.2.2      tron int nkern_dirs = 4; /* 2 extra subdirs */
    149  1.98.2.2      tron #else
    150  1.98.2.2      tron int nkern_dirs = 2;
    151      1.90    itojun #endif
    152      1.90    itojun 
    153  1.98.2.2      tron int kernfs_try_fileop(kfstype, kfsfileop, void *, int);
    154  1.98.2.2      tron int kernfs_try_xwrite(kfstype, const struct kernfs_node *, char *,
    155  1.98.2.2      tron     size_t, int);
    156  1.98.2.2      tron 
    157  1.98.2.2      tron static int kernfs_default_xwrite(void *v);
    158  1.98.2.2      tron static int kernfs_default_fileop_getattr(void *);
    159  1.98.2.2      tron 
    160  1.98.2.2      tron /* must include all fileop's */
    161  1.98.2.2      tron const struct kernfs_fileop kernfs_default_fileops[] = {
    162  1.98.2.2      tron   { .kf_fileop = KERNFS_XWRITE },
    163  1.98.2.2      tron   { .kf_fileop = KERNFS_FILEOP_OPEN },
    164  1.98.2.2      tron   { .kf_fileop = KERNFS_FILEOP_GETATTR,
    165  1.98.2.3      tron     .kf_genop = {kernfs_default_fileop_getattr} },
    166  1.98.2.2      tron   { .kf_fileop = KERNFS_FILEOP_IOCTL },
    167  1.98.2.2      tron   { .kf_fileop = KERNFS_FILEOP_CLOSE },
    168  1.98.2.3      tron   { .kf_fileop = KERNFS_FILEOP_WRITE, .kf_genop = {kernfs_default_xwrite} },
    169  1.98.2.2      tron };
    170       1.1       cgd 
    171      1.41  christos int	kernfs_lookup	__P((void *));
    172      1.85  jdolecek #define	kernfs_create	genfs_eopnotsupp
    173      1.85  jdolecek #define	kernfs_mknod	genfs_eopnotsupp
    174      1.90    itojun int	kernfs_open	__P((void *));
    175      1.90    itojun int	kernfs_close	__P((void *));
    176      1.41  christos int	kernfs_access	__P((void *));
    177      1.41  christos int	kernfs_getattr	__P((void *));
    178      1.41  christos int	kernfs_setattr	__P((void *));
    179      1.41  christos int	kernfs_read	__P((void *));
    180      1.41  christos int	kernfs_write	__P((void *));
    181      1.65  wrstuden #define	kernfs_fcntl	genfs_fcntl
    182  1.98.2.2      tron int	kernfs_ioctl	__P((void *));
    183      1.45   mycroft #define	kernfs_poll	genfs_poll
    184      1.57      fvdl #define kernfs_revoke	genfs_revoke
    185      1.44   mycroft #define	kernfs_fsync	genfs_nullop
    186      1.44   mycroft #define	kernfs_seek	genfs_nullop
    187      1.85  jdolecek #define	kernfs_remove	genfs_eopnotsupp
    188      1.41  christos int	kernfs_link	__P((void *));
    189      1.85  jdolecek #define	kernfs_rename	genfs_eopnotsupp
    190      1.85  jdolecek #define	kernfs_mkdir	genfs_eopnotsupp
    191      1.85  jdolecek #define	kernfs_rmdir	genfs_eopnotsupp
    192      1.41  christos int	kernfs_symlink	__P((void *));
    193      1.41  christos int	kernfs_readdir	__P((void *));
    194      1.44   mycroft #define	kernfs_readlink	genfs_eopnotsupp
    195      1.44   mycroft #define	kernfs_abortop	genfs_abortop
    196      1.41  christos int	kernfs_inactive	__P((void *));
    197      1.41  christos int	kernfs_reclaim	__P((void *));
    198      1.64  wrstuden #define	kernfs_lock	genfs_lock
    199      1.64  wrstuden #define	kernfs_unlock	genfs_unlock
    200      1.44   mycroft #define	kernfs_bmap	genfs_badop
    201      1.44   mycroft #define	kernfs_strategy	genfs_badop
    202      1.41  christos int	kernfs_print	__P((void *));
    203      1.64  wrstuden #define	kernfs_islocked	genfs_islocked
    204      1.41  christos int	kernfs_pathconf	__P((void *));
    205      1.62    kleink #define	kernfs_advlock	genfs_einval
    206      1.44   mycroft #define	kernfs_blkatoff	genfs_eopnotsupp
    207      1.44   mycroft #define	kernfs_valloc	genfs_eopnotsupp
    208      1.44   mycroft #define	kernfs_vfree	genfs_nullop
    209      1.44   mycroft #define	kernfs_truncate	genfs_eopnotsupp
    210      1.44   mycroft #define	kernfs_update	genfs_nullop
    211      1.44   mycroft #define	kernfs_bwrite	genfs_eopnotsupp
    212      1.79       chs #define	kernfs_putpages	genfs_putpages
    213      1.41  christos 
    214      1.90    itojun static int	kernfs_xread __P((struct kernfs_node *, int, char **, size_t, size_t *));
    215      1.90    itojun static int	kernfs_xwrite __P((const struct kernfs_node *, char *, size_t));
    216      1.41  christos 
    217      1.41  christos int (**kernfs_vnodeop_p) __P((void *));
    218      1.71  jdolecek const struct vnodeopv_entry_desc kernfs_vnodeop_entries[] = {
    219      1.41  christos 	{ &vop_default_desc, vn_default_error },
    220      1.44   mycroft 	{ &vop_lookup_desc, kernfs_lookup },		/* lookup */
    221      1.44   mycroft 	{ &vop_create_desc, kernfs_create },		/* create */
    222      1.44   mycroft 	{ &vop_mknod_desc, kernfs_mknod },		/* mknod */
    223      1.44   mycroft 	{ &vop_open_desc, kernfs_open },		/* open */
    224      1.44   mycroft 	{ &vop_close_desc, kernfs_close },		/* close */
    225      1.44   mycroft 	{ &vop_access_desc, kernfs_access },		/* access */
    226      1.44   mycroft 	{ &vop_getattr_desc, kernfs_getattr },		/* getattr */
    227      1.44   mycroft 	{ &vop_setattr_desc, kernfs_setattr },		/* setattr */
    228      1.44   mycroft 	{ &vop_read_desc, kernfs_read },		/* read */
    229      1.44   mycroft 	{ &vop_write_desc, kernfs_write },		/* write */
    230      1.65  wrstuden 	{ &vop_fcntl_desc, kernfs_fcntl },		/* fcntl */
    231      1.44   mycroft 	{ &vop_ioctl_desc, kernfs_ioctl },		/* ioctl */
    232      1.45   mycroft 	{ &vop_poll_desc, kernfs_poll },		/* poll */
    233      1.57      fvdl 	{ &vop_revoke_desc, kernfs_revoke },		/* revoke */
    234      1.44   mycroft 	{ &vop_fsync_desc, kernfs_fsync },		/* fsync */
    235      1.44   mycroft 	{ &vop_seek_desc, kernfs_seek },		/* seek */
    236      1.44   mycroft 	{ &vop_remove_desc, kernfs_remove },		/* remove */
    237      1.44   mycroft 	{ &vop_link_desc, kernfs_link },		/* link */
    238      1.44   mycroft 	{ &vop_rename_desc, kernfs_rename },		/* rename */
    239      1.44   mycroft 	{ &vop_mkdir_desc, kernfs_mkdir },		/* mkdir */
    240      1.44   mycroft 	{ &vop_rmdir_desc, kernfs_rmdir },		/* rmdir */
    241      1.44   mycroft 	{ &vop_symlink_desc, kernfs_symlink },		/* symlink */
    242      1.44   mycroft 	{ &vop_readdir_desc, kernfs_readdir },		/* readdir */
    243      1.44   mycroft 	{ &vop_readlink_desc, kernfs_readlink },	/* readlink */
    244      1.44   mycroft 	{ &vop_abortop_desc, kernfs_abortop },		/* abortop */
    245      1.44   mycroft 	{ &vop_inactive_desc, kernfs_inactive },	/* inactive */
    246      1.44   mycroft 	{ &vop_reclaim_desc, kernfs_reclaim },		/* reclaim */
    247      1.44   mycroft 	{ &vop_lock_desc, kernfs_lock },		/* lock */
    248      1.44   mycroft 	{ &vop_unlock_desc, kernfs_unlock },		/* unlock */
    249      1.44   mycroft 	{ &vop_bmap_desc, kernfs_bmap },		/* bmap */
    250      1.44   mycroft 	{ &vop_strategy_desc, kernfs_strategy },	/* strategy */
    251      1.44   mycroft 	{ &vop_print_desc, kernfs_print },		/* print */
    252      1.44   mycroft 	{ &vop_islocked_desc, kernfs_islocked },	/* islocked */
    253      1.44   mycroft 	{ &vop_pathconf_desc, kernfs_pathconf },	/* pathconf */
    254      1.44   mycroft 	{ &vop_advlock_desc, kernfs_advlock },		/* advlock */
    255      1.44   mycroft 	{ &vop_blkatoff_desc, kernfs_blkatoff },	/* blkatoff */
    256      1.44   mycroft 	{ &vop_valloc_desc, kernfs_valloc },		/* valloc */
    257      1.44   mycroft 	{ &vop_vfree_desc, kernfs_vfree },		/* vfree */
    258      1.44   mycroft 	{ &vop_truncate_desc, kernfs_truncate },	/* truncate */
    259      1.44   mycroft 	{ &vop_update_desc, kernfs_update },		/* update */
    260      1.44   mycroft 	{ &vop_bwrite_desc, kernfs_bwrite },		/* bwrite */
    261      1.79       chs 	{ &vop_putpages_desc, kernfs_putpages },	/* putpages */
    262      1.76       chs 	{ NULL, NULL }
    263      1.41  christos };
    264      1.71  jdolecek const struct vnodeopv_desc kernfs_vnodeop_opv_desc =
    265      1.41  christos 	{ &kernfs_vnodeop_p, kernfs_vnodeop_entries };
    266      1.41  christos 
    267  1.98.2.2      tron static __inline int
    268  1.98.2.2      tron kernfs_fileop_compare(struct kernfs_fileop *a, struct kernfs_fileop *b)
    269  1.98.2.2      tron {
    270  1.98.2.2      tron 	if (a->kf_type < b->kf_type)
    271  1.98.2.2      tron 		return -1;
    272  1.98.2.2      tron 	if (a->kf_type > b->kf_type)
    273  1.98.2.2      tron 		return 1;
    274  1.98.2.2      tron 	if (a->kf_fileop < b->kf_fileop)
    275  1.98.2.2      tron 		return -1;
    276  1.98.2.2      tron 	if (a->kf_fileop > b->kf_fileop)
    277  1.98.2.2      tron 		return 1;
    278  1.98.2.2      tron 	return (0);
    279  1.98.2.2      tron }
    280  1.98.2.2      tron 
    281  1.98.2.2      tron SPLAY_HEAD(kfsfileoptree, kernfs_fileop) kfsfileoptree =
    282  1.98.2.2      tron 	SPLAY_INITIALIZER(kfsfileoptree);
    283  1.98.2.2      tron SPLAY_PROTOTYPE(kfsfileoptree, kernfs_fileop, kf_node, kernfs_fileop_compare);
    284  1.98.2.2      tron SPLAY_GENERATE(kfsfileoptree, kernfs_fileop, kf_node, kernfs_fileop_compare);
    285  1.98.2.2      tron 
    286  1.98.2.2      tron kfstype
    287  1.98.2.2      tron kernfs_alloctype(int nkf, const struct kernfs_fileop *kf)
    288  1.98.2.2      tron {
    289  1.98.2.2      tron 	static u_char nextfreetype = KFSlasttype;
    290  1.98.2.2      tron 	struct kernfs_fileop *dkf, *fkf, skf;
    291  1.98.2.2      tron 	int i;
    292  1.98.2.2      tron 
    293  1.98.2.2      tron 	/* XXX need to keep track of dkf's memory if we support
    294  1.98.2.2      tron            deallocating types */
    295  1.98.2.2      tron 	dkf = malloc(sizeof(kernfs_default_fileops), M_TEMP, M_WAITOK);
    296  1.98.2.2      tron 	memcpy(dkf, kernfs_default_fileops, sizeof(kernfs_default_fileops));
    297  1.98.2.2      tron 
    298  1.98.2.2      tron 	for (i = 0; i < sizeof(kernfs_default_fileops) /
    299  1.98.2.2      tron 		     sizeof(kernfs_default_fileops[0]); i++) {
    300  1.98.2.2      tron 		dkf[i].kf_type = nextfreetype;
    301  1.98.2.2      tron 		SPLAY_INSERT(kfsfileoptree, &kfsfileoptree, &dkf[i]);
    302  1.98.2.2      tron 	}
    303  1.98.2.2      tron 
    304  1.98.2.2      tron 	for (i = 0; i < nkf; i++) {
    305  1.98.2.2      tron 		skf.kf_type = nextfreetype;
    306  1.98.2.2      tron 		skf.kf_fileop = kf[i].kf_fileop;
    307  1.98.2.2      tron 		if ((fkf = SPLAY_FIND(kfsfileoptree, &kfsfileoptree, &skf)))
    308  1.98.2.2      tron 			fkf->kf_genop = kf[i].kf_genop;
    309  1.98.2.2      tron 	}
    310  1.98.2.2      tron 
    311  1.98.2.2      tron 	return nextfreetype++;
    312  1.98.2.2      tron }
    313  1.98.2.2      tron 
    314  1.98.2.2      tron int
    315  1.98.2.2      tron kernfs_try_fileop(kfstype type, kfsfileop fileop, void *v, int error)
    316  1.98.2.2      tron {
    317  1.98.2.2      tron 	struct kernfs_fileop *kf, skf;
    318  1.98.2.2      tron 
    319  1.98.2.2      tron 	skf.kf_type = type;
    320  1.98.2.2      tron 	skf.kf_fileop = fileop;
    321  1.98.2.2      tron 	if ((kf = SPLAY_FIND(kfsfileoptree, &kfsfileoptree, &skf)))
    322  1.98.2.2      tron 		if (kf->kf_vop)
    323  1.98.2.2      tron 			return kf->kf_vop(v);
    324  1.98.2.2      tron 	return error;
    325  1.98.2.2      tron }
    326  1.98.2.2      tron 
    327  1.98.2.2      tron int
    328  1.98.2.2      tron kernfs_try_xwrite(kfstype type, const struct kernfs_node *kfs, char *buf,
    329  1.98.2.2      tron     size_t len, int error)
    330  1.98.2.2      tron {
    331  1.98.2.2      tron 	struct kernfs_fileop *kf, skf;
    332  1.98.2.2      tron 
    333  1.98.2.2      tron 	skf.kf_type = type;
    334  1.98.2.2      tron 	skf.kf_fileop = KERNFS_XWRITE;
    335  1.98.2.2      tron 	if ((kf = SPLAY_FIND(kfsfileoptree, &kfsfileoptree, &skf)))
    336  1.98.2.2      tron 		if (kf->kf_xwrite)
    337  1.98.2.2      tron 			return kf->kf_xwrite(kfs, buf, len);
    338  1.98.2.2      tron 	return error;
    339  1.98.2.2      tron }
    340  1.98.2.2      tron 
    341  1.98.2.2      tron int
    342  1.98.2.2      tron kernfs_addentry(kernfs_parentdir_t *pkt, kernfs_entry_t *dkt)
    343  1.98.2.2      tron {
    344  1.98.2.2      tron 	struct kernfs_subdir *ks, *parent;
    345  1.98.2.2      tron 
    346  1.98.2.2      tron 	if (pkt == NULL) {
    347  1.98.2.2      tron 		SIMPLEQ_INSERT_TAIL(&dyn_kern_targets, dkt, dkt_queue);
    348  1.98.2.2      tron 		nkern_targets++;
    349  1.98.2.2      tron 		if (dkt->dkt_kt.kt_vtype == VDIR)
    350  1.98.2.2      tron 			nkern_dirs++;
    351  1.98.2.2      tron 	} else {
    352  1.98.2.2      tron 		parent = (struct kernfs_subdir *)pkt->kt_data;
    353  1.98.2.2      tron 		SIMPLEQ_INSERT_TAIL(&parent->ks_entries, dkt, dkt_queue);
    354  1.98.2.2      tron 		parent->ks_nentries++;
    355  1.98.2.2      tron 		if (dkt->dkt_kt.kt_vtype == VDIR)
    356  1.98.2.2      tron 			parent->ks_dirs++;
    357  1.98.2.2      tron 	}
    358  1.98.2.2      tron 	if (dkt->dkt_kt.kt_vtype == VDIR && dkt->dkt_kt.kt_data == NULL) {
    359  1.98.2.2      tron 		ks = malloc(sizeof(struct kernfs_subdir),
    360  1.98.2.2      tron 		    M_TEMP, M_WAITOK);
    361  1.98.2.2      tron 		SIMPLEQ_INIT(&ks->ks_entries);
    362  1.98.2.2      tron 		ks->ks_nentries = 2; /* . and .. */
    363  1.98.2.2      tron 		ks->ks_dirs = 2;
    364  1.98.2.2      tron 		ks->ks_parent = pkt ? pkt : &kern_targets[0];
    365  1.98.2.2      tron 		dkt->dkt_kt.kt_data = ks;
    366  1.98.2.2      tron 	}
    367  1.98.2.2      tron 	return 0;
    368  1.98.2.2      tron }
    369  1.98.2.2      tron 
    370      1.82  jdolecek static int
    371      1.90    itojun kernfs_xread(kfs, off, bufp, len, wrlen)
    372      1.90    itojun 	struct kernfs_node *kfs;
    373      1.28   mycroft 	int off;
    374      1.28   mycroft 	char **bufp;
    375      1.82  jdolecek 	size_t len;
    376      1.82  jdolecek 	size_t *wrlen;
    377       1.1       cgd {
    378      1.90    itojun 	const struct kern_target *kt;
    379      1.90    itojun #ifdef IPSEC
    380      1.90    itojun 	struct mbuf *m;
    381      1.90    itojun #endif
    382      1.90    itojun 
    383      1.90    itojun 	kt = kfs->kfs_kt;
    384       1.1       cgd 
    385      1.90    itojun 	switch (kfs->kfs_type) {
    386      1.98     darcy 	case KFStime: {
    387       1.1       cgd 		struct timeval tv;
    388      1.28   mycroft 
    389       1.1       cgd 		microtime(&tv);
    390      1.90    itojun 		snprintf(*bufp, len, "%ld %ld\n", tv.tv_sec, tv.tv_usec);
    391       1.1       cgd 		break;
    392       1.1       cgd 	}
    393       1.1       cgd 
    394      1.98     darcy 	case KFSint: {
    395       1.1       cgd 		int *ip = kt->kt_data;
    396      1.28   mycroft 
    397      1.90    itojun 		snprintf(*bufp, len, "%d\n", *ip);
    398       1.1       cgd 		break;
    399       1.1       cgd 	}
    400       1.1       cgd 
    401      1.98     darcy 	case KFSstring: {
    402       1.1       cgd 		char *cp = kt->kt_data;
    403       1.1       cgd 
    404      1.28   mycroft 		*bufp = cp;
    405      1.28   mycroft 		break;
    406      1.28   mycroft 	}
    407       1.1       cgd 
    408      1.98     darcy 	case KFSmsgbuf: {
    409      1.28   mycroft 		long n;
    410      1.28   mycroft 
    411      1.52       leo 		/*
    412      1.52       leo 		 * deal with cases where the message buffer has
    413      1.52       leo 		 * become corrupted.
    414      1.52       leo 		 */
    415      1.52       leo 		if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) {
    416      1.52       leo 			msgbufenabled = 0;
    417      1.52       leo 			return (ENXIO);
    418      1.52       leo 		}
    419      1.52       leo 
    420      1.52       leo 		/*
    421      1.52       leo 		 * Note that reads of /kern/msgbuf won't necessarily yield
    422      1.52       leo 		 * consistent results, if the message buffer is modified
    423      1.52       leo 		 * while the read is in progress.  The worst that can happen
    424      1.52       leo 		 * is that incorrect data will be read.  There's no way
    425      1.52       leo 		 * that this can crash the system unless the values in the
    426      1.52       leo 		 * message buffer header are corrupted, but that'll cause
    427      1.52       leo 		 * the system to die anyway.
    428      1.52       leo 		 */
    429      1.82  jdolecek 		if (off >= msgbufp->msg_bufs) {
    430      1.82  jdolecek 			*wrlen = 0;
    431      1.28   mycroft 			return (0);
    432      1.82  jdolecek 		}
    433      1.28   mycroft 		n = msgbufp->msg_bufx + off;
    434      1.52       leo 		if (n >= msgbufp->msg_bufs)
    435      1.52       leo 			n -= msgbufp->msg_bufs;
    436      1.52       leo 		len = min(msgbufp->msg_bufs - n, msgbufp->msg_bufs - off);
    437      1.28   mycroft 		*bufp = msgbufp->msg_bufc + n;
    438      1.82  jdolecek 		*wrlen = len;
    439      1.82  jdolecek 		return (0);
    440       1.1       cgd 	}
    441       1.1       cgd 
    442      1.98     darcy 	case KFShostname: {
    443       1.1       cgd 		char *cp = hostname;
    444       1.1       cgd 		int xlen = hostnamelen;
    445       1.1       cgd 
    446      1.90    itojun 		if (xlen >= (len - 2))
    447       1.1       cgd 			return (EINVAL);
    448       1.1       cgd 
    449      1.60     perry 		memcpy(*bufp, cp, xlen);
    450      1.28   mycroft 		(*bufp)[xlen] = '\n';
    451      1.28   mycroft 		(*bufp)[xlen+1] = '\0';
    452      1.90    itojun 		len = strlen(*bufp);
    453       1.1       cgd 		break;
    454       1.1       cgd 	}
    455       1.1       cgd 
    456      1.98     darcy 	case KFSavenrun:
    457      1.31   mycroft 		averunnable.fscale = FSCALE;
    458      1.90    itojun 		snprintf(*bufp, len, "%d %d %d %ld\n",
    459      1.23   mycroft 		    averunnable.ldavg[0], averunnable.ldavg[1],
    460      1.23   mycroft 		    averunnable.ldavg[2], averunnable.fscale);
    461       1.1       cgd 		break;
    462       1.1       cgd 
    463      1.90    itojun #ifdef IPSEC
    464      1.98     darcy 	case KFSipsecsa:
    465      1.90    itojun 		/*
    466      1.90    itojun 		 * Note that SA configuration could be changed during the
    467      1.90    itojun 		 * read operation, resulting in garbled output.
    468      1.90    itojun 		 */
    469      1.90    itojun 		m = key_setdumpsa_spi(htonl(kfs->kfs_value));
    470      1.90    itojun 		if (!m)
    471      1.90    itojun 			return (ENOBUFS);
    472      1.90    itojun 		if (off >= m->m_pkthdr.len) {
    473      1.90    itojun 			*wrlen = 0;
    474      1.90    itojun 			m_freem(m);
    475      1.90    itojun 			return (0);
    476      1.90    itojun 		}
    477      1.90    itojun 		if (len > m->m_pkthdr.len - off)
    478      1.90    itojun 			len = m->m_pkthdr.len - off;
    479      1.90    itojun 		m_copydata(m, off, len, *bufp);
    480      1.90    itojun 		*wrlen = len;
    481      1.90    itojun 		m_freem(m);
    482      1.90    itojun 		return (0);
    483      1.90    itojun 
    484      1.98     darcy 	case KFSipsecsp:
    485      1.90    itojun 		/*
    486      1.90    itojun 		 * Note that SP configuration could be changed during the
    487      1.90    itojun 		 * read operation, resulting in garbled output.
    488      1.90    itojun 		 */
    489      1.90    itojun 		if (!kfs->kfs_v) {
    490      1.90    itojun 			struct secpolicy *sp;
    491      1.90    itojun 
    492      1.90    itojun 			sp = key_getspbyid(kfs->kfs_value);
    493      1.90    itojun 			if (sp)
    494      1.90    itojun 				kfs->kfs_v = sp;
    495      1.90    itojun 			else
    496      1.90    itojun 				return (ENOENT);
    497      1.90    itojun 		}
    498      1.90    itojun 		m = key_setdumpsp((struct secpolicy *)kfs->kfs_v,
    499      1.90    itojun 		    SADB_X_SPDGET, 0, 0);
    500      1.90    itojun 		if (!m)
    501      1.90    itojun 			return (ENOBUFS);
    502      1.90    itojun 		if (off >= m->m_pkthdr.len) {
    503      1.90    itojun 			*wrlen = 0;
    504      1.90    itojun 			m_freem(m);
    505      1.90    itojun 			return (0);
    506      1.90    itojun 		}
    507      1.90    itojun 		if (len > m->m_pkthdr.len - off)
    508      1.90    itojun 			len = m->m_pkthdr.len - off;
    509      1.90    itojun 		m_copydata(m, off, len, *bufp);
    510      1.90    itojun 		*wrlen = len;
    511      1.90    itojun 		m_freem(m);
    512      1.90    itojun 		return (0);
    513      1.90    itojun #endif
    514      1.90    itojun 
    515       1.1       cgd 	default:
    516      1.82  jdolecek 		*wrlen = 0;
    517      1.28   mycroft 		return (0);
    518       1.1       cgd 	}
    519       1.1       cgd 
    520      1.28   mycroft 	len = strlen(*bufp);
    521      1.28   mycroft 	if (len <= off)
    522      1.82  jdolecek 		*wrlen = 0;
    523      1.82  jdolecek 	else {
    524      1.82  jdolecek 		*bufp += off;
    525      1.82  jdolecek 		*wrlen = len - off;
    526      1.82  jdolecek 	}
    527      1.82  jdolecek 	return (0);
    528       1.1       cgd }
    529       1.1       cgd 
    530      1.82  jdolecek static int
    531      1.90    itojun kernfs_xwrite(kfs, buf, len)
    532      1.90    itojun 	const struct kernfs_node *kfs;
    533       1.1       cgd 	char *buf;
    534      1.82  jdolecek 	size_t len;
    535       1.1       cgd {
    536      1.23   mycroft 
    537      1.90    itojun 	switch (kfs->kfs_type) {
    538      1.98     darcy 	case KFShostname:
    539       1.1       cgd 		if (buf[len-1] == '\n')
    540       1.1       cgd 			--len;
    541      1.60     perry 		memcpy(hostname, buf, len);
    542      1.17       cgd 		hostname[len] = '\0';
    543      1.82  jdolecek 		hostnamelen = (size_t) len;
    544       1.1       cgd 		return (0);
    545       1.1       cgd 
    546       1.1       cgd 	default:
    547  1.98.2.2      tron 		return kernfs_try_xwrite(kfs->kfs_type, kfs, buf, len, EIO);
    548       1.1       cgd 	}
    549       1.1       cgd }
    550       1.1       cgd 
    551      1.17       cgd 
    552      1.17       cgd /*
    553       1.1       cgd  * vp is the current namei directory
    554       1.1       cgd  * ndp is the name to locate in that directory...
    555       1.1       cgd  */
    556      1.41  christos int
    557      1.41  christos kernfs_lookup(v)
    558      1.41  christos 	void *v;
    559      1.41  christos {
    560      1.23   mycroft 	struct vop_lookup_args /* {
    561      1.23   mycroft 		struct vnode * a_dvp;
    562      1.23   mycroft 		struct vnode ** a_vpp;
    563      1.23   mycroft 		struct componentname * a_cnp;
    564      1.41  christos 	} */ *ap = v;
    565      1.23   mycroft 	struct componentname *cnp = ap->a_cnp;
    566      1.23   mycroft 	struct vnode **vpp = ap->a_vpp;
    567      1.23   mycroft 	struct vnode *dvp = ap->a_dvp;
    568      1.48       cgd 	const char *pname = cnp->cn_nameptr;
    569      1.90    itojun 	const struct kernfs_node *kfs;
    570      1.75  jdolecek 	const struct kern_target *kt;
    571  1.98.2.2      tron 	const struct dyn_kern_target *dkt;
    572  1.98.2.2      tron 	const struct kernfs_subdir *ks;
    573      1.64  wrstuden 	int error, i, wantpunlock;
    574      1.90    itojun #ifdef IPSEC
    575      1.90    itojun 	char *ep;
    576      1.90    itojun 	u_int32_t id;
    577       1.1       cgd #endif
    578      1.23   mycroft 
    579      1.35   mycroft 	*vpp = NULLVP;
    580      1.64  wrstuden 	cnp->cn_flags &= ~PDIRUNLOCK;
    581      1.35   mycroft 
    582      1.35   mycroft 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    583      1.35   mycroft 		return (EROFS);
    584      1.35   mycroft 
    585      1.23   mycroft 	if (cnp->cn_namelen == 1 && *pname == '.') {
    586      1.23   mycroft 		*vpp = dvp;
    587       1.1       cgd 		VREF(dvp);
    588       1.1       cgd 		return (0);
    589       1.1       cgd 	}
    590      1.13       cgd 
    591      1.90    itojun 	wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
    592      1.90    itojun 	kfs = VTOKERN(dvp);
    593      1.90    itojun 	switch (kfs->kfs_type) {
    594      1.98     darcy 	case KFSkern:
    595      1.90    itojun 		/*
    596      1.90    itojun 		 * Shouldn't get here with .. in the root node.
    597      1.90    itojun 		 */
    598      1.90    itojun 		if (cnp->cn_flags & ISDOTDOT)
    599      1.90    itojun 			return (EIO);
    600      1.64  wrstuden 
    601  1.98.2.2      tron 		for (i = 0; i < static_nkern_targets; i++) {
    602      1.90    itojun 			kt = &kern_targets[i];
    603      1.90    itojun 			if (cnp->cn_namelen == kt->kt_namlen &&
    604      1.90    itojun 			    memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
    605      1.90    itojun 				goto found;
    606      1.90    itojun 		}
    607  1.98.2.2      tron 		SIMPLEQ_FOREACH(dkt, &dyn_kern_targets, dkt_queue) {
    608  1.98.2.2      tron 			if (cnp->cn_namelen == dkt->dkt_kt.kt_namlen &&
    609  1.98.2.2      tron 			    memcmp(dkt->dkt_kt.kt_name, pname, cnp->cn_namelen) == 0) {
    610  1.98.2.2      tron 				kt = &dkt->dkt_kt;
    611  1.98.2.2      tron 				goto found;
    612  1.98.2.2      tron 			}
    613  1.98.2.2      tron 		}
    614      1.90    itojun 		break;
    615      1.25   mycroft 
    616      1.90    itojun 	found:
    617      1.90    itojun 		error = kernfs_allocvp(dvp->v_mount, vpp, kt->kt_tag, kt, 0);
    618      1.90    itojun 		if ((error == 0) && wantpunlock) {
    619      1.90    itojun 			VOP_UNLOCK(dvp, 0);
    620      1.90    itojun 			cnp->cn_flags |= PDIRUNLOCK;
    621      1.90    itojun 		}
    622      1.90    itojun 		return (error);
    623      1.64  wrstuden 
    624  1.98.2.2      tron 	case KFSsubdir:
    625  1.98.2.2      tron 		ks = (struct kernfs_subdir *)kfs->kfs_kt->kt_data;
    626  1.98.2.2      tron 		if (cnp->cn_flags & ISDOTDOT) {
    627  1.98.2.2      tron 			kt = ks->ks_parent;
    628  1.98.2.2      tron 			goto found;
    629  1.98.2.2      tron 		}
    630  1.98.2.2      tron 
    631  1.98.2.2      tron 		SIMPLEQ_FOREACH(dkt, &ks->ks_entries, dkt_queue) {
    632  1.98.2.2      tron 			if (cnp->cn_namelen == dkt->dkt_kt.kt_namlen &&
    633  1.98.2.2      tron 			    memcmp(dkt->dkt_kt.kt_name, pname, cnp->cn_namelen) == 0) {
    634  1.98.2.2      tron 				kt = &dkt->dkt_kt;
    635  1.98.2.2      tron 				goto found;
    636  1.98.2.2      tron 			}
    637  1.98.2.2      tron 		}
    638  1.98.2.2      tron 		break;
    639  1.98.2.2      tron 
    640      1.90    itojun #ifdef IPSEC
    641      1.98     darcy 	case KFSipsecsadir:
    642  1.98.2.1       jdc 		if (cnp->cn_flags & ISDOTDOT) {
    643  1.98.2.1       jdc 			kt = &kern_targets[0];
    644  1.98.2.1       jdc 			goto found;
    645  1.98.2.1       jdc 		}
    646  1.98.2.1       jdc 
    647  1.98.2.1       jdc 		for (i = 2; i < nipsecsa_targets; i++) {
    648      1.90    itojun 			kt = &ipsecsa_targets[i];
    649      1.90    itojun 			if (cnp->cn_namelen == kt->kt_namlen &&
    650  1.98.2.1       jdc 			    memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
    651  1.98.2.1       jdc 				goto found;
    652      1.90    itojun 		}
    653       1.1       cgd 
    654      1.90    itojun 		ep = NULL;
    655      1.90    itojun 		id = strtoul(pname, &ep, 10);
    656      1.90    itojun 		if (!ep || *ep || ep == pname)
    657      1.90    itojun 			break;
    658       1.1       cgd 
    659  1.98.2.1       jdc 		error = kernfs_allocvp(dvp->v_mount, vpp, KFSipsecsa, &ipsecsa_kt, id);
    660      1.90    itojun 		if ((error == 0) && wantpunlock) {
    661      1.90    itojun 			VOP_UNLOCK(dvp, 0);
    662      1.90    itojun 			cnp->cn_flags |= PDIRUNLOCK;
    663      1.90    itojun 		}
    664      1.90    itojun 		return (error);
    665      1.23   mycroft 
    666      1.98     darcy 	case KFSipsecspdir:
    667  1.98.2.1       jdc 		if (cnp->cn_flags & ISDOTDOT) {
    668  1.98.2.1       jdc 			kt = &kern_targets[0];
    669  1.98.2.1       jdc 			goto found;
    670  1.98.2.1       jdc 		}
    671  1.98.2.1       jdc 
    672  1.98.2.1       jdc 		for (i = 2; i < nipsecsp_targets; i++) {
    673      1.90    itojun 			kt = &ipsecsp_targets[i];
    674      1.90    itojun 			if (cnp->cn_namelen == kt->kt_namlen &&
    675  1.98.2.1       jdc 			    memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
    676  1.98.2.1       jdc 				goto found;
    677      1.57      fvdl 		}
    678      1.90    itojun 
    679      1.90    itojun 		ep = NULL;
    680      1.90    itojun 		id = strtoul(pname, &ep, 10);
    681      1.90    itojun 		if (!ep || *ep || ep == pname)
    682      1.90    itojun 			break;
    683      1.90    itojun 
    684  1.98.2.1       jdc 		error = kernfs_allocvp(dvp->v_mount, vpp, KFSipsecsp, &ipsecsp_kt, id);
    685      1.90    itojun 		if ((error == 0) && wantpunlock) {
    686      1.64  wrstuden 			VOP_UNLOCK(dvp, 0);
    687      1.64  wrstuden 			cnp->cn_flags |= PDIRUNLOCK;
    688      1.64  wrstuden 		}
    689      1.90    itojun 		return (error);
    690      1.90    itojun #endif
    691      1.90    itojun 
    692      1.90    itojun 	default:
    693      1.90    itojun 		return (ENOTDIR);
    694      1.90    itojun 	}
    695      1.90    itojun 
    696      1.90    itojun 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
    697      1.90    itojun }
    698      1.90    itojun 
    699      1.90    itojun int
    700      1.90    itojun kernfs_open(v)
    701      1.90    itojun 	void *v;
    702      1.90    itojun {
    703      1.90    itojun 	struct vop_open_args /* {
    704      1.90    itojun 		struct vnode *a_vp;
    705      1.90    itojun 		int a_mode;
    706      1.90    itojun 		struct ucred *a_cred;
    707      1.90    itojun 		struct proc *a_p;
    708      1.90    itojun 	} */ *ap = v;
    709      1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    710      1.90    itojun #ifdef IPSEC
    711      1.90    itojun 	struct mbuf *m;
    712      1.90    itojun 	struct secpolicy *sp;
    713      1.90    itojun #endif
    714      1.90    itojun 
    715      1.90    itojun 	switch (kfs->kfs_type) {
    716      1.90    itojun #ifdef IPSEC
    717      1.98     darcy 	case KFSipsecsa:
    718      1.90    itojun 		m = key_setdumpsa_spi(htonl(kfs->kfs_value));
    719      1.90    itojun 		if (m) {
    720      1.90    itojun 			m_freem(m);
    721      1.90    itojun 			return (0);
    722      1.90    itojun 		} else
    723      1.90    itojun 			return (ENOENT);
    724      1.90    itojun 
    725      1.98     darcy 	case KFSipsecsp:
    726      1.90    itojun 		sp = key_getspbyid(kfs->kfs_value);
    727      1.90    itojun 		if (sp) {
    728      1.90    itojun 			kfs->kfs_v = sp;
    729      1.90    itojun 			return (0);
    730      1.90    itojun 		} else
    731      1.90    itojun 			return (ENOENT);
    732      1.90    itojun #endif
    733      1.90    itojun 
    734      1.90    itojun 	default:
    735  1.98.2.2      tron 		return kernfs_try_fileop(kfs->kfs_type, KERNFS_FILEOP_OPEN,
    736  1.98.2.2      tron 		    v, 0);
    737      1.23   mycroft 	}
    738      1.90    itojun }
    739       1.1       cgd 
    740      1.90    itojun int
    741      1.90    itojun kernfs_close(v)
    742      1.90    itojun 	void *v;
    743      1.90    itojun {
    744      1.90    itojun 	struct vop_close_args /* {
    745      1.90    itojun 		struct vnode *a_vp;
    746      1.90    itojun 		int a_fflag;
    747      1.90    itojun 		struct ucred *a_cred;
    748      1.90    itojun 		struct proc *a_p;
    749      1.90    itojun 	} */ *ap = v;
    750      1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    751      1.90    itojun 
    752      1.90    itojun 	switch (kfs->kfs_type) {
    753      1.90    itojun #ifdef IPSEC
    754      1.98     darcy 	case KFSipsecsp:
    755      1.90    itojun 		key_freesp((struct secpolicy *)kfs->kfs_v);
    756      1.90    itojun 		break;
    757       1.1       cgd #endif
    758      1.90    itojun 
    759      1.90    itojun 	default:
    760  1.98.2.2      tron 		return kernfs_try_fileop(kfs->kfs_type, KERNFS_FILEOP_CLOSE,
    761  1.98.2.2      tron 		    v, 0);
    762      1.57      fvdl 	}
    763      1.23   mycroft 
    764       1.1       cgd 	return (0);
    765       1.1       cgd }
    766       1.1       cgd 
    767      1.28   mycroft int
    768      1.41  christos kernfs_access(v)
    769      1.41  christos 	void *v;
    770      1.41  christos {
    771      1.23   mycroft 	struct vop_access_args /* {
    772      1.23   mycroft 		struct vnode *a_vp;
    773      1.34   mycroft 		int a_mode;
    774      1.23   mycroft 		struct ucred *a_cred;
    775      1.23   mycroft 		struct proc *a_p;
    776      1.41  christos 	} */ *ap = v;
    777      1.90    itojun 	struct vattr va;
    778      1.90    itojun 	int error;
    779      1.17       cgd 
    780      1.90    itojun 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
    781      1.90    itojun 		return (error);
    782      1.49   mycroft 
    783      1.90    itojun 	return (vaccess(va.va_type, va.va_mode, va.va_uid, va.va_gid,
    784      1.90    itojun 	    ap->a_mode, ap->a_cred));
    785      1.23   mycroft }
    786      1.23   mycroft 
    787  1.98.2.2      tron static int
    788  1.98.2.2      tron kernfs_default_fileop_getattr(v)
    789  1.98.2.2      tron 	void *v;
    790  1.98.2.2      tron {
    791  1.98.2.2      tron 	struct vop_getattr_args /* {
    792  1.98.2.2      tron 		struct vnode *a_vp;
    793  1.98.2.2      tron 		struct vattr *a_vap;
    794  1.98.2.2      tron 		struct ucred *a_cred;
    795  1.98.2.2      tron 		struct proc *a_p;
    796  1.98.2.2      tron 	} */ *ap = v;
    797  1.98.2.2      tron 	struct vattr *vap = ap->a_vap;
    798  1.98.2.2      tron 
    799  1.98.2.2      tron 	vap->va_nlink = 1;
    800  1.98.2.2      tron 	vap->va_bytes = vap->va_size = 0;
    801  1.98.2.2      tron 
    802  1.98.2.2      tron 	return 0;
    803  1.98.2.2      tron }
    804  1.98.2.2      tron 
    805      1.41  christos int
    806      1.41  christos kernfs_getattr(v)
    807      1.41  christos 	void *v;
    808      1.41  christos {
    809      1.23   mycroft 	struct vop_getattr_args /* {
    810      1.23   mycroft 		struct vnode *a_vp;
    811      1.23   mycroft 		struct vattr *a_vap;
    812      1.23   mycroft 		struct ucred *a_cred;
    813      1.23   mycroft 		struct proc *a_p;
    814      1.41  christos 	} */ *ap = v;
    815      1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    816  1.98.2.2      tron 	struct kernfs_subdir *ks;
    817      1.23   mycroft 	struct vattr *vap = ap->a_vap;
    818       1.1       cgd 	int error = 0;
    819      1.28   mycroft 	char strbuf[KSTRING], *buf;
    820      1.90    itojun 	size_t nread, total;
    821       1.1       cgd 
    822      1.90    itojun 	VATTR_NULL(vap);
    823      1.90    itojun 	vap->va_type = ap->a_vp->v_type;
    824      1.17       cgd 	vap->va_uid = 0;
    825      1.17       cgd 	vap->va_gid = 0;
    826      1.90    itojun 	vap->va_mode = kfs->kfs_mode;
    827      1.90    itojun 	vap->va_fileid = kfs->kfs_fileno;
    828      1.90    itojun 	vap->va_flags = 0;
    829      1.23   mycroft 	vap->va_size = 0;
    830       1.1       cgd 	vap->va_blocksize = DEV_BSIZE;
    831      1.81     lukem 	/*
    832      1.92       dan 	 * Make all times be current TOD, except for the "boottime" node.
    833      1.93    simonb 	 * Avoid microtime(9), it's slow.
    834      1.83    simonb 	 * We don't guard the read from time(9) with splclock(9) since we
    835      1.83    simonb 	 * don't actually need to be THAT sure the access is atomic.
    836      1.81     lukem 	 */
    837      1.96    itojun 	if (kfs->kfs_kt && kfs->kfs_kt->kt_namlen == 8 &&
    838      1.92       dan 	    !memcmp(kfs->kfs_kt->kt_name, "boottime", 8)) {
    839      1.92       dan 		TIMEVAL_TO_TIMESPEC(&boottime, &vap->va_ctime);
    840      1.92       dan 	} else {
    841      1.92       dan 		TIMEVAL_TO_TIMESPEC(&time, &vap->va_ctime);
    842      1.92       dan 	}
    843      1.81     lukem 	vap->va_atime = vap->va_mtime = vap->va_ctime;
    844       1.1       cgd 	vap->va_gen = 0;
    845       1.1       cgd 	vap->va_flags = 0;
    846       1.1       cgd 	vap->va_rdev = 0;
    847       1.1       cgd 	vap->va_bytes = 0;
    848       1.1       cgd 
    849      1.90    itojun 	switch (kfs->kfs_type) {
    850      1.98     darcy 	case KFSkern:
    851  1.98.2.2      tron 		vap->va_nlink = nkern_dirs;
    852      1.90    itojun 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    853      1.90    itojun 		break;
    854      1.90    itojun 
    855      1.98     darcy 	case KFSroot:
    856      1.90    itojun 		vap->va_nlink = 1;
    857      1.90    itojun 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    858      1.90    itojun 		break;
    859      1.90    itojun 
    860  1.98.2.2      tron 	case KFSsubdir:
    861  1.98.2.2      tron 		ks = (struct kernfs_subdir *)kfs->kfs_kt->kt_data;
    862  1.98.2.2      tron 		vap->va_nlink = ks->ks_dirs;
    863  1.98.2.2      tron 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    864  1.98.2.2      tron 		break;
    865  1.98.2.2      tron 
    866      1.98     darcy 	case KFSnull:
    867      1.98     darcy 	case KFStime:
    868      1.98     darcy 	case KFSint:
    869      1.98     darcy 	case KFSstring:
    870      1.98     darcy 	case KFShostname:
    871      1.98     darcy 	case KFSavenrun:
    872      1.98     darcy 	case KFSdevice:
    873      1.98     darcy 	case KFSmsgbuf:
    874      1.90    itojun #ifdef IPSEC
    875      1.98     darcy 	case KFSipsecsa:
    876      1.98     darcy 	case KFSipsecsp:
    877       1.1       cgd #endif
    878       1.1       cgd 		vap->va_nlink = 1;
    879      1.84  jdolecek 		total = 0;
    880      1.84  jdolecek 		do {
    881      1.84  jdolecek 			buf = strbuf;
    882      1.90    itojun 			error = kernfs_xread(kfs, total, &buf,
    883      1.90    itojun 			    sizeof(strbuf), &nread);
    884      1.84  jdolecek 			total += nread;
    885      1.84  jdolecek 		} while (error == 0 && nread != 0);
    886      1.90    itojun 		vap->va_bytes = vap->va_size = total;
    887      1.90    itojun 		break;
    888      1.90    itojun 
    889      1.90    itojun #ifdef IPSEC
    890      1.98     darcy 	case KFSipsecsadir:
    891      1.98     darcy 	case KFSipsecspdir:
    892      1.90    itojun 		vap->va_nlink = 2;
    893      1.90    itojun 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    894      1.90    itojun 		break;
    895      1.90    itojun #endif
    896      1.90    itojun 
    897      1.90    itojun 	default:
    898  1.98.2.2      tron 		error = kernfs_try_fileop(kfs->kfs_type,
    899  1.98.2.2      tron 		    KERNFS_FILEOP_GETATTR, v, EINVAL);
    900      1.90    itojun 		break;
    901       1.1       cgd 	}
    902       1.1       cgd 
    903       1.1       cgd 	return (error);
    904       1.1       cgd }
    905       1.1       cgd 
    906      1.41  christos /*ARGSUSED*/
    907      1.41  christos int
    908      1.41  christos kernfs_setattr(v)
    909      1.41  christos 	void *v;
    910       1.1       cgd {
    911      1.90    itojun 
    912       1.1       cgd 	/*
    913      1.17       cgd 	 * Silently ignore attribute changes.
    914      1.17       cgd 	 * This allows for open with truncate to have no
    915      1.17       cgd 	 * effect until some data is written.  I want to
    916      1.17       cgd 	 * do it this way because all writes are atomic.
    917       1.1       cgd 	 */
    918      1.17       cgd 	return (0);
    919       1.1       cgd }
    920       1.1       cgd 
    921      1.28   mycroft int
    922      1.41  christos kernfs_read(v)
    923      1.41  christos 	void *v;
    924      1.41  christos {
    925      1.23   mycroft 	struct vop_read_args /* {
    926      1.23   mycroft 		struct vnode *a_vp;
    927      1.23   mycroft 		struct uio *a_uio;
    928      1.23   mycroft 		int  a_ioflag;
    929      1.23   mycroft 		struct ucred *a_cred;
    930      1.41  christos 	} */ *ap = v;
    931      1.23   mycroft 	struct uio *uio = ap->a_uio;
    932      1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    933      1.28   mycroft 	char strbuf[KSTRING], *buf;
    934      1.82  jdolecek 	off_t off;
    935      1.82  jdolecek 	size_t len;
    936      1.28   mycroft 	int error;
    937      1.23   mycroft 
    938      1.90    itojun 	if (ap->a_vp->v_type == VDIR)
    939      1.23   mycroft 		return (EOPNOTSUPP);
    940      1.23   mycroft 
    941  1.98.2.6       riz 	/* Don't allow negative offsets */
    942  1.98.2.6       riz 	if (uio->uio_offset < 0)
    943  1.98.2.6       riz 		return EINVAL;
    944  1.98.2.6       riz 
    945      1.28   mycroft 	off = uio->uio_offset;
    946      1.82  jdolecek 	buf = strbuf;
    947      1.90    itojun 	if ((error = kernfs_xread(kfs, off, &buf, sizeof(strbuf), &len)) == 0)
    948      1.82  jdolecek 		error = uiomove(buf, len, uio);
    949      1.82  jdolecek 	return (error);
    950       1.1       cgd }
    951       1.1       cgd 
    952  1.98.2.2      tron static int
    953  1.98.2.2      tron kernfs_default_xwrite(v)
    954      1.41  christos 	void *v;
    955      1.41  christos {
    956      1.23   mycroft 	struct vop_write_args /* {
    957      1.23   mycroft 		struct vnode *a_vp;
    958      1.23   mycroft 		struct uio *a_uio;
    959      1.23   mycroft 		int  a_ioflag;
    960      1.23   mycroft 		struct ucred *a_cred;
    961      1.41  christos 	} */ *ap = v;
    962      1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    963      1.23   mycroft 	struct uio *uio = ap->a_uio;
    964      1.23   mycroft 	int error, xlen;
    965       1.1       cgd 	char strbuf[KSTRING];
    966      1.23   mycroft 
    967       1.1       cgd 	if (uio->uio_offset != 0)
    968       1.1       cgd 		return (EINVAL);
    969       1.1       cgd 
    970       1.1       cgd 	xlen = min(uio->uio_resid, KSTRING-1);
    971      1.41  christos 	if ((error = uiomove(strbuf, xlen, uio)) != 0)
    972       1.1       cgd 		return (error);
    973       1.1       cgd 
    974       1.1       cgd 	if (uio->uio_resid != 0)
    975       1.1       cgd 		return (EIO);
    976       1.1       cgd 
    977       1.1       cgd 	strbuf[xlen] = '\0';
    978      1.17       cgd 	xlen = strlen(strbuf);
    979      1.90    itojun 	return (kernfs_xwrite(kfs, strbuf, xlen));
    980       1.1       cgd }
    981       1.1       cgd 
    982  1.98.2.2      tron int
    983  1.98.2.2      tron kernfs_write(v)
    984  1.98.2.2      tron 	void *v;
    985  1.98.2.2      tron {
    986  1.98.2.2      tron 	struct vop_write_args /* {
    987  1.98.2.2      tron 		struct vnode *a_vp;
    988  1.98.2.2      tron 		struct uio *a_uio;
    989  1.98.2.2      tron 		int  a_ioflag;
    990  1.98.2.2      tron 		struct ucred *a_cred;
    991  1.98.2.2      tron 	} */ *ap = v;
    992  1.98.2.2      tron 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    993  1.98.2.2      tron 
    994  1.98.2.2      tron 	return kernfs_try_fileop(kfs->kfs_type, KERNFS_FILEOP_WRITE, v, 0);
    995  1.98.2.2      tron }
    996  1.98.2.2      tron 
    997  1.98.2.2      tron int
    998  1.98.2.2      tron kernfs_ioctl(v)
    999  1.98.2.2      tron 	void *v;
   1000  1.98.2.2      tron {
   1001  1.98.2.2      tron 	struct vop_ioctl_args /* {
   1002  1.98.2.2      tron 		const struct vnodeop_desc *a_desc;
   1003  1.98.2.2      tron 		struct vnode *a_vp;
   1004  1.98.2.2      tron 		u_long a_command;
   1005  1.98.2.2      tron 		void *a_data;
   1006  1.98.2.2      tron 		int a_fflag;
   1007  1.98.2.2      tron 		struct ucred *a_cred;
   1008  1.98.2.2      tron 		struct proc *a_p;
   1009  1.98.2.2      tron 	} */ *ap = v;
   1010  1.98.2.2      tron 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
   1011  1.98.2.2      tron 
   1012  1.98.2.2      tron 	return kernfs_try_fileop(kfs->kfs_type, KERNFS_FILEOP_IOCTL, v,
   1013  1.98.2.2      tron 	    EPASSTHROUGH);
   1014  1.98.2.2      tron }
   1015  1.98.2.2      tron 
   1016  1.98.2.1       jdc static int
   1017  1.98.2.1       jdc kernfs_setdirentfileno_kt(struct dirent *d, const struct kern_target *kt,
   1018  1.98.2.1       jdc     u_int32_t value, struct vop_readdir_args *ap)
   1019  1.98.2.1       jdc {
   1020  1.98.2.1       jdc 	struct kernfs_node *kfs;
   1021  1.98.2.1       jdc 	struct vnode *vp;
   1022  1.98.2.1       jdc 	int error;
   1023  1.98.2.1       jdc 
   1024  1.98.2.1       jdc 	if ((error = kernfs_allocvp(ap->a_vp->v_mount, &vp, kt->kt_tag, kt,
   1025  1.98.2.1       jdc 	    value)) != 0)
   1026  1.98.2.1       jdc 		return error;
   1027  1.98.2.1       jdc 	if (kt->kt_tag == KFSdevice) {
   1028  1.98.2.1       jdc 		struct vattr va;
   1029  1.98.2.1       jdc 		if ((error = VOP_GETATTR(vp, &va, ap->a_cred,
   1030  1.98.2.1       jdc 		    ap->a_uio->uio_segflg == UIO_USERSPACE ?
   1031  1.98.2.1       jdc 		    ap->a_uio->uio_procp : &proc0)) != 0)
   1032  1.98.2.1       jdc 			return (error);
   1033  1.98.2.1       jdc 		d->d_fileno = va.va_fileid;
   1034  1.98.2.1       jdc 	} else {
   1035  1.98.2.1       jdc 		kfs = VTOKERN(vp);
   1036  1.98.2.1       jdc 		d->d_fileno = kfs->kfs_fileno;
   1037  1.98.2.1       jdc 	}
   1038  1.98.2.1       jdc 	vput(vp);
   1039  1.98.2.1       jdc 	return 0;
   1040  1.98.2.1       jdc }
   1041  1.98.2.1       jdc 
   1042  1.98.2.1       jdc static int
   1043  1.98.2.1       jdc kernfs_setdirentfileno(struct dirent *d, off_t entry,
   1044  1.98.2.1       jdc     struct kernfs_node *thisdir_kfs, const struct kern_target *parent_kt,
   1045  1.98.2.1       jdc     const struct kern_target *kt, struct vop_readdir_args *ap)
   1046  1.98.2.1       jdc {
   1047  1.98.2.1       jdc 	const struct kern_target *ikt;
   1048  1.98.2.1       jdc 	int error;
   1049  1.98.2.1       jdc 
   1050  1.98.2.1       jdc 	switch (entry) {
   1051  1.98.2.1       jdc 	case 0:
   1052  1.98.2.1       jdc 		d->d_fileno = thisdir_kfs->kfs_fileno;
   1053  1.98.2.1       jdc 		return 0;
   1054  1.98.2.1       jdc 	case 1:
   1055  1.98.2.1       jdc 		ikt = parent_kt;
   1056  1.98.2.1       jdc 		break;
   1057  1.98.2.1       jdc 	default:
   1058  1.98.2.1       jdc 		ikt = kt;
   1059  1.98.2.1       jdc 		break;
   1060  1.98.2.1       jdc 	}
   1061  1.98.2.1       jdc 	if (ikt != thisdir_kfs->kfs_kt) {
   1062  1.98.2.1       jdc 		if ((error = kernfs_setdirentfileno_kt(d, ikt, 0, ap)) != 0)
   1063  1.98.2.1       jdc 			return error;
   1064  1.98.2.1       jdc 	} else
   1065  1.98.2.1       jdc 		d->d_fileno = thisdir_kfs->kfs_fileno;
   1066  1.98.2.1       jdc 	return 0;
   1067  1.98.2.1       jdc }
   1068  1.98.2.1       jdc 
   1069      1.41  christos int
   1070      1.41  christos kernfs_readdir(v)
   1071      1.41  christos 	void *v;
   1072      1.41  christos {
   1073      1.23   mycroft 	struct vop_readdir_args /* {
   1074      1.23   mycroft 		struct vnode *a_vp;
   1075      1.23   mycroft 		struct uio *a_uio;
   1076      1.23   mycroft 		struct ucred *a_cred;
   1077      1.26   mycroft 		int *a_eofflag;
   1078      1.57      fvdl 		off_t **a_cookies;
   1079      1.57      fvdl 		int a_*ncookies;
   1080      1.41  christos 	} */ *ap = v;
   1081      1.23   mycroft 	struct uio *uio = ap->a_uio;
   1082      1.37   mycroft 	struct dirent d;
   1083      1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
   1084      1.75  jdolecek 	const struct kern_target *kt;
   1085  1.98.2.2      tron 	const struct dyn_kern_target *dkt = NULL;
   1086  1.98.2.2      tron 	const struct kernfs_subdir *ks;
   1087  1.98.2.2      tron 	off_t i, j;
   1088       1.1       cgd 	int error;
   1089      1.57      fvdl 	off_t *cookies = NULL;
   1090      1.90    itojun 	int ncookies = 0, n;
   1091      1.90    itojun #ifdef IPSEC
   1092      1.90    itojun 	struct secasvar *sav, *sav2;
   1093      1.90    itojun 	struct secpolicy *sp;
   1094      1.90    itojun #endif
   1095      1.23   mycroft 
   1096      1.37   mycroft 	if (uio->uio_resid < UIO_MX)
   1097      1.37   mycroft 		return (EINVAL);
   1098      1.38   mycroft 	if (uio->uio_offset < 0)
   1099      1.37   mycroft 		return (EINVAL);
   1100      1.26   mycroft 
   1101       1.1       cgd 	error = 0;
   1102      1.38   mycroft 	i = uio->uio_offset;
   1103      1.90    itojun 	memset(&d, 0, sizeof(d));
   1104      1.90    itojun 	d.d_reclen = UIO_MX;
   1105      1.90    itojun 	ncookies = uio->uio_resid / UIO_MX;
   1106      1.90    itojun 
   1107      1.90    itojun 	switch (kfs->kfs_type) {
   1108      1.98     darcy 	case KFSkern:
   1109      1.90    itojun 		if (i >= nkern_targets)
   1110      1.90    itojun 			return (0);
   1111      1.66  sommerfe 
   1112      1.90    itojun 		if (ap->a_ncookies) {
   1113      1.90    itojun 			ncookies = min(ncookies, (nkern_targets - i));
   1114      1.90    itojun 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
   1115      1.90    itojun 			    M_WAITOK);
   1116      1.90    itojun 			*ap->a_cookies = cookies;
   1117      1.90    itojun 		}
   1118      1.90    itojun 
   1119      1.90    itojun 		n = 0;
   1120      1.90    itojun 		for (; i < nkern_targets && uio->uio_resid >= UIO_MX; i++) {
   1121  1.98.2.2      tron 			if (i < static_nkern_targets)
   1122  1.98.2.2      tron 				kt = &kern_targets[i];
   1123  1.98.2.2      tron 			else {
   1124  1.98.2.2      tron 				if (dkt == NULL) {
   1125  1.98.2.2      tron 					dkt = SIMPLEQ_FIRST(&dyn_kern_targets);
   1126  1.98.2.2      tron 					for (j = static_nkern_targets; j < i &&
   1127  1.98.2.2      tron 						     dkt != NULL; j++)
   1128  1.98.2.2      tron 						dkt = SIMPLEQ_NEXT(dkt, dkt_queue);
   1129  1.98.2.2      tron 					if (j != i)
   1130  1.98.2.2      tron 						break;
   1131  1.98.2.2      tron 				} else {
   1132  1.98.2.2      tron 					dkt = SIMPLEQ_NEXT(dkt, dkt_queue);
   1133  1.98.2.2      tron 					if (dkt == NULL)
   1134  1.98.2.2      tron 						break;
   1135  1.98.2.2      tron 				}
   1136  1.98.2.2      tron 				kt = &dkt->dkt_kt;
   1137  1.98.2.2      tron 			}
   1138      1.98     darcy 			if (kt->kt_tag == KFSdevice) {
   1139      1.94    itojun 				dev_t *dp = kt->kt_data;
   1140      1.94    itojun 				struct vnode *fvp;
   1141      1.94    itojun 
   1142      1.94    itojun 				if (*dp == NODEV ||
   1143      1.94    itojun 				    !vfinddev(*dp, kt->kt_vtype, &fvp))
   1144      1.94    itojun 					continue;
   1145      1.94    itojun 			}
   1146      1.90    itojun 			d.d_namlen = kt->kt_namlen;
   1147  1.98.2.1       jdc 			if ((error = kernfs_setdirentfileno(&d, i, kfs,
   1148  1.98.2.1       jdc 			    &kern_targets[0], kt, ap)) != 0)
   1149  1.98.2.1       jdc 				break;
   1150      1.90    itojun 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
   1151      1.90    itojun 			d.d_type = kt->kt_type;
   1152      1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1153      1.90    itojun 				break;
   1154      1.90    itojun 			if (cookies)
   1155      1.90    itojun 				*cookies++ = i + 1;
   1156      1.90    itojun 			n++;
   1157      1.90    itojun 		}
   1158      1.90    itojun 		ncookies = n;
   1159      1.90    itojun 		break;
   1160      1.90    itojun 
   1161      1.98     darcy 	case KFSroot:
   1162      1.90    itojun 		if (i >= 2)
   1163      1.90    itojun 			return 0;
   1164      1.90    itojun 
   1165      1.90    itojun 		if (ap->a_ncookies) {
   1166      1.90    itojun 			ncookies = min(ncookies, (2 - i));
   1167      1.90    itojun 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
   1168      1.90    itojun 			    M_WAITOK);
   1169      1.90    itojun 			*ap->a_cookies = cookies;
   1170      1.90    itojun 		}
   1171      1.90    itojun 
   1172      1.90    itojun 		n = 0;
   1173      1.90    itojun 		for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
   1174      1.90    itojun 			kt = &kern_targets[i];
   1175      1.90    itojun 			d.d_namlen = kt->kt_namlen;
   1176      1.90    itojun 			d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
   1177      1.90    itojun 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
   1178      1.90    itojun 			d.d_type = kt->kt_type;
   1179      1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1180      1.90    itojun 				break;
   1181      1.90    itojun 			if (cookies)
   1182      1.90    itojun 				*cookies++ = i + 1;
   1183      1.90    itojun 			n++;
   1184      1.90    itojun 		}
   1185      1.90    itojun 		ncookies = n;
   1186      1.90    itojun 		break;
   1187      1.90    itojun 
   1188  1.98.2.2      tron 	case KFSsubdir:
   1189  1.98.2.2      tron 		ks = (struct kernfs_subdir *)kfs->kfs_kt->kt_data;
   1190  1.98.2.2      tron 		if (i >= ks->ks_nentries)
   1191  1.98.2.2      tron 			return (0);
   1192  1.98.2.2      tron 
   1193  1.98.2.2      tron 		if (ap->a_ncookies) {
   1194  1.98.2.2      tron 			ncookies = min(ncookies, (ks->ks_nentries - i));
   1195  1.98.2.2      tron 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
   1196  1.98.2.2      tron 			    M_WAITOK);
   1197  1.98.2.2      tron 			*ap->a_cookies = cookies;
   1198  1.98.2.2      tron 		}
   1199  1.98.2.2      tron 
   1200  1.98.2.2      tron 		dkt = SIMPLEQ_FIRST(&ks->ks_entries);
   1201  1.98.2.2      tron 		for (j = 0; j < i && dkt != NULL; j++)
   1202  1.98.2.2      tron 			dkt = SIMPLEQ_NEXT(dkt, dkt_queue);
   1203  1.98.2.2      tron 		n = 0;
   1204  1.98.2.2      tron 		for (; i < ks->ks_nentries && uio->uio_resid >= UIO_MX; i++) {
   1205  1.98.2.2      tron 			if (i < 2)
   1206  1.98.2.2      tron 				kt = &subdir_targets[i];
   1207  1.98.2.2      tron 			else {
   1208  1.98.2.2      tron 				/* check if ks_nentries lied to us */
   1209  1.98.2.2      tron 				if (dkt == NULL)
   1210  1.98.2.2      tron 					break;
   1211  1.98.2.2      tron 				kt = &dkt->dkt_kt;
   1212  1.98.2.2      tron 				dkt = SIMPLEQ_NEXT(dkt, dkt_queue);
   1213  1.98.2.2      tron 			}
   1214  1.98.2.2      tron 			if (kt->kt_tag == KFSdevice) {
   1215  1.98.2.2      tron 				dev_t *dp = kt->kt_data;
   1216  1.98.2.2      tron 				struct vnode *fvp;
   1217  1.98.2.2      tron 
   1218  1.98.2.2      tron 				if (*dp == NODEV ||
   1219  1.98.2.2      tron 				    !vfinddev(*dp, kt->kt_vtype, &fvp))
   1220  1.98.2.2      tron 					continue;
   1221  1.98.2.2      tron 			}
   1222  1.98.2.2      tron 			d.d_namlen = kt->kt_namlen;
   1223  1.98.2.2      tron 			if ((error = kernfs_setdirentfileno(&d, i, kfs,
   1224  1.98.2.2      tron 			    ks->ks_parent, kt, ap)) != 0)
   1225  1.98.2.2      tron 				break;
   1226  1.98.2.2      tron 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
   1227  1.98.2.2      tron 			d.d_type = kt->kt_type;
   1228  1.98.2.2      tron 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1229  1.98.2.2      tron 				break;
   1230  1.98.2.2      tron 			if (cookies)
   1231  1.98.2.2      tron 				*cookies++ = i + 1;
   1232  1.98.2.2      tron 			n++;
   1233  1.98.2.2      tron 		}
   1234  1.98.2.2      tron 		ncookies = n;
   1235  1.98.2.2      tron 		break;
   1236  1.98.2.2      tron 
   1237      1.90    itojun #ifdef IPSEC
   1238      1.98     darcy 	case KFSipsecsadir:
   1239      1.90    itojun 		/* count SA in the system */
   1240      1.90    itojun 		n = 0;
   1241      1.90    itojun 		TAILQ_FOREACH(sav, &satailq, tailq) {
   1242      1.90    itojun 			for (sav2 = TAILQ_FIRST(&satailq);
   1243      1.90    itojun 			    sav2 != sav;
   1244      1.90    itojun 			    sav2 = TAILQ_NEXT(sav2, tailq)) {
   1245      1.90    itojun 				if (sav->spi == sav2->spi) {
   1246      1.90    itojun 					/* multiple SA with same SPI */
   1247      1.90    itojun 					break;
   1248      1.90    itojun 				}
   1249      1.90    itojun 			}
   1250      1.90    itojun 			if (sav == sav2 || sav->spi != sav2->spi)
   1251      1.90    itojun 				n++;
   1252      1.90    itojun 		}
   1253      1.37   mycroft 
   1254      1.90    itojun 		if (i >= nipsecsa_targets + n)
   1255      1.90    itojun 			return (0);
   1256      1.57      fvdl 
   1257      1.90    itojun 		if (ap->a_ncookies) {
   1258      1.90    itojun 			ncookies = min(ncookies, (n - i));
   1259      1.90    itojun 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
   1260      1.90    itojun 			    M_WAITOK);
   1261      1.90    itojun 			*ap->a_cookies = cookies;
   1262      1.90    itojun 		}
   1263      1.26   mycroft 
   1264      1.90    itojun 		n = 0;
   1265      1.90    itojun 		for (; i < nipsecsa_targets && uio->uio_resid >= UIO_MX; i++) {
   1266      1.90    itojun 			kt = &ipsecsa_targets[i];
   1267      1.90    itojun 			d.d_namlen = kt->kt_namlen;
   1268  1.98.2.1       jdc 			if ((error = kernfs_setdirentfileno(&d, i, kfs,
   1269  1.98.2.1       jdc 			    &kern_targets[0], kt, ap)) != 0)
   1270  1.98.2.1       jdc 				break;
   1271      1.90    itojun 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
   1272      1.90    itojun 			d.d_type = kt->kt_type;
   1273      1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1274      1.90    itojun 				break;
   1275      1.90    itojun 			if (cookies)
   1276      1.90    itojun 				*cookies++ = i + 1;
   1277      1.90    itojun 			n++;
   1278      1.90    itojun 		}
   1279      1.90    itojun 		if (error) {
   1280      1.90    itojun 			ncookies = n;
   1281      1.90    itojun 			break;
   1282      1.90    itojun 		}
   1283      1.26   mycroft 
   1284      1.90    itojun 		TAILQ_FOREACH(sav, &satailq, tailq) {
   1285      1.90    itojun 			for (sav2 = TAILQ_FIRST(&satailq);
   1286      1.90    itojun 			    sav2 != sav;
   1287      1.90    itojun 			    sav2 = TAILQ_NEXT(sav2, tailq)) {
   1288      1.90    itojun 				if (sav->spi == sav2->spi) {
   1289      1.90    itojun 					/* multiple SA with same SPI */
   1290      1.90    itojun 					break;
   1291      1.90    itojun 				}
   1292      1.90    itojun 			}
   1293      1.90    itojun 			if (sav != sav2 && sav->spi == sav2->spi)
   1294      1.23   mycroft 				continue;
   1295      1.90    itojun 			if (uio->uio_resid < UIO_MX)
   1296      1.90    itojun 				break;
   1297  1.98.2.1       jdc 			if ((error = kernfs_setdirentfileno_kt(&d, &ipsecsa_kt,
   1298  1.98.2.1       jdc 			    sav->spi, ap)) != 0)
   1299  1.98.2.1       jdc 				break;
   1300      1.90    itojun 			d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
   1301      1.90    itojun 			    "%u", ntohl(sav->spi));
   1302      1.90    itojun 			d.d_type = DT_REG;
   1303      1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1304      1.90    itojun 				break;
   1305      1.90    itojun 			if (cookies)
   1306      1.90    itojun 				*cookies++ = i + 1;
   1307      1.90    itojun 			n++;
   1308      1.90    itojun 			i++;
   1309      1.23   mycroft 		}
   1310      1.90    itojun 		ncookies = n;
   1311      1.90    itojun 		break;
   1312      1.90    itojun 
   1313      1.98     darcy 	case KFSipsecspdir:
   1314      1.90    itojun 		/* count SP in the system */
   1315      1.90    itojun 		n = 0;
   1316      1.90    itojun 		TAILQ_FOREACH(sp, &sptailq, tailq)
   1317      1.90    itojun 			n++;
   1318      1.26   mycroft 
   1319  1.98.2.2      tron 		if (i >= nipsecsp_targets + n)
   1320      1.90    itojun 			return (0);
   1321      1.90    itojun 
   1322      1.90    itojun 		if (ap->a_ncookies) {
   1323      1.90    itojun 			ncookies = min(ncookies, (n - i));
   1324      1.90    itojun 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
   1325      1.90    itojun 			    M_WAITOK);
   1326      1.90    itojun 			*ap->a_cookies = cookies;
   1327      1.90    itojun 		}
   1328      1.26   mycroft 
   1329      1.90    itojun 		n = 0;
   1330      1.90    itojun 		for (; i < nipsecsp_targets && uio->uio_resid >= UIO_MX; i++) {
   1331      1.90    itojun 			kt = &ipsecsp_targets[i];
   1332      1.90    itojun 			d.d_namlen = kt->kt_namlen;
   1333  1.98.2.1       jdc 			if ((error = kernfs_setdirentfileno(&d, i, kfs,
   1334  1.98.2.1       jdc 			    &kern_targets[0], kt, ap)) != 0)
   1335  1.98.2.1       jdc 				break;
   1336      1.90    itojun 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
   1337      1.90    itojun 			d.d_type = kt->kt_type;
   1338      1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1339      1.90    itojun 				break;
   1340      1.90    itojun 			if (cookies)
   1341      1.90    itojun 				*cookies++ = i + 1;
   1342      1.90    itojun 			n++;
   1343      1.90    itojun 		}
   1344      1.90    itojun 		if (error) {
   1345      1.90    itojun 			ncookies = n;
   1346       1.1       cgd 			break;
   1347      1.57      fvdl 		}
   1348      1.90    itojun 
   1349      1.90    itojun 		TAILQ_FOREACH(sp, &sptailq, tailq) {
   1350      1.90    itojun 			if (uio->uio_resid < UIO_MX)
   1351      1.90    itojun 				break;
   1352  1.98.2.1       jdc 			if ((error = kernfs_setdirentfileno_kt(&d, &ipsecsp_kt,
   1353  1.98.2.1       jdc 			    sp->id, ap)) != 0)
   1354  1.98.2.1       jdc 				break;
   1355      1.90    itojun 			d.d_namlen = snprintf(d.d_name, sizeof(d.d_name),
   1356      1.90    itojun 			    "%u", sp->id);
   1357      1.90    itojun 			d.d_type = DT_REG;
   1358      1.90    itojun 			if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0)
   1359      1.90    itojun 				break;
   1360      1.90    itojun 			if (cookies)
   1361      1.90    itojun 				*cookies++ = i + 1;
   1362      1.90    itojun 			n++;
   1363      1.90    itojun 			i++;
   1364      1.90    itojun 		}
   1365      1.90    itojun 		ncookies = n;
   1366      1.90    itojun 		break;
   1367      1.90    itojun #endif
   1368      1.90    itojun 
   1369      1.90    itojun 	default:
   1370      1.90    itojun 		error = ENOTDIR;
   1371      1.90    itojun 		break;
   1372      1.57      fvdl 	}
   1373      1.57      fvdl 
   1374      1.57      fvdl 	if (ap->a_ncookies) {
   1375      1.57      fvdl 		if (error) {
   1376      1.90    itojun 			if (cookies)
   1377      1.90    itojun 				free(*ap->a_cookies, M_TEMP);
   1378      1.57      fvdl 			*ap->a_ncookies = 0;
   1379      1.57      fvdl 			*ap->a_cookies = NULL;
   1380      1.57      fvdl 		} else
   1381      1.57      fvdl 			*ap->a_ncookies = ncookies;
   1382       1.1       cgd 	}
   1383       1.1       cgd 
   1384      1.38   mycroft 	uio->uio_offset = i;
   1385       1.1       cgd 	return (error);
   1386       1.1       cgd }
   1387       1.1       cgd 
   1388      1.41  christos int
   1389      1.41  christos kernfs_inactive(v)
   1390      1.41  christos 	void *v;
   1391      1.41  christos {
   1392      1.23   mycroft 	struct vop_inactive_args /* {
   1393      1.23   mycroft 		struct vnode *a_vp;
   1394      1.57      fvdl 		struct proc *a_p;
   1395      1.41  christos 	} */ *ap = v;
   1396      1.23   mycroft 	struct vnode *vp = ap->a_vp;
   1397      1.90    itojun 	const struct kernfs_node *kfs = VTOKERN(ap->a_vp);
   1398      1.90    itojun #ifdef IPSEC
   1399      1.90    itojun 	struct mbuf *m;
   1400      1.90    itojun 	struct secpolicy *sp;
   1401      1.90    itojun #endif
   1402      1.23   mycroft 
   1403      1.90    itojun 	VOP_UNLOCK(vp, 0);
   1404      1.90    itojun 	switch (kfs->kfs_type) {
   1405      1.90    itojun #ifdef IPSEC
   1406      1.98     darcy 	case KFSipsecsa:
   1407      1.90    itojun 		m = key_setdumpsa_spi(htonl(kfs->kfs_value));
   1408      1.90    itojun 		if (m)
   1409      1.90    itojun 			m_freem(m);
   1410      1.91    itojun 		else
   1411      1.90    itojun 			vgone(vp);
   1412      1.90    itojun 		break;
   1413      1.98     darcy 	case KFSipsecsp:
   1414      1.90    itojun 		sp = key_getspbyid(kfs->kfs_value);
   1415      1.90    itojun 		if (sp)
   1416      1.90    itojun 			key_freesp(sp);
   1417      1.90    itojun 		else {
   1418      1.90    itojun 			/* should never happen as we hold a refcnt */
   1419      1.90    itojun 			vgone(vp);
   1420      1.90    itojun 		}
   1421      1.90    itojun 		break;
   1422      1.23   mycroft #endif
   1423      1.90    itojun 	default:
   1424      1.90    itojun 		break;
   1425      1.90    itojun 	}
   1426      1.23   mycroft 	return (0);
   1427      1.23   mycroft }
   1428      1.23   mycroft 
   1429      1.41  christos int
   1430      1.41  christos kernfs_reclaim(v)
   1431      1.41  christos 	void *v;
   1432      1.41  christos {
   1433      1.23   mycroft 	struct vop_reclaim_args /* {
   1434      1.23   mycroft 		struct vnode *a_vp;
   1435      1.41  christos 	} */ *ap = v;
   1436      1.23   mycroft 
   1437      1.90    itojun 	return (kernfs_freevp(ap->a_vp));
   1438       1.1       cgd }
   1439       1.1       cgd 
   1440       1.1       cgd /*
   1441      1.23   mycroft  * Return POSIX pathconf information applicable to special devices.
   1442      1.23   mycroft  */
   1443      1.41  christos int
   1444      1.41  christos kernfs_pathconf(v)
   1445      1.41  christos 	void *v;
   1446      1.41  christos {
   1447      1.23   mycroft 	struct vop_pathconf_args /* {
   1448      1.23   mycroft 		struct vnode *a_vp;
   1449      1.23   mycroft 		int a_name;
   1450      1.29       cgd 		register_t *a_retval;
   1451      1.41  christos 	} */ *ap = v;
   1452      1.23   mycroft 
   1453      1.23   mycroft 	switch (ap->a_name) {
   1454      1.23   mycroft 	case _PC_LINK_MAX:
   1455      1.23   mycroft 		*ap->a_retval = LINK_MAX;
   1456      1.23   mycroft 		return (0);
   1457      1.23   mycroft 	case _PC_MAX_CANON:
   1458      1.23   mycroft 		*ap->a_retval = MAX_CANON;
   1459      1.23   mycroft 		return (0);
   1460      1.23   mycroft 	case _PC_MAX_INPUT:
   1461      1.23   mycroft 		*ap->a_retval = MAX_INPUT;
   1462      1.23   mycroft 		return (0);
   1463      1.23   mycroft 	case _PC_PIPE_BUF:
   1464      1.23   mycroft 		*ap->a_retval = PIPE_BUF;
   1465      1.23   mycroft 		return (0);
   1466      1.23   mycroft 	case _PC_CHOWN_RESTRICTED:
   1467      1.23   mycroft 		*ap->a_retval = 1;
   1468      1.23   mycroft 		return (0);
   1469      1.23   mycroft 	case _PC_VDISABLE:
   1470      1.23   mycroft 		*ap->a_retval = _POSIX_VDISABLE;
   1471      1.59    kleink 		return (0);
   1472      1.59    kleink 	case _PC_SYNC_IO:
   1473      1.59    kleink 		*ap->a_retval = 1;
   1474      1.23   mycroft 		return (0);
   1475      1.23   mycroft 	default:
   1476      1.23   mycroft 		return (EINVAL);
   1477      1.23   mycroft 	}
   1478      1.23   mycroft 	/* NOTREACHED */
   1479      1.23   mycroft }
   1480      1.23   mycroft 
   1481      1.23   mycroft /*
   1482      1.23   mycroft  * Print out the contents of a /dev/fd vnode.
   1483       1.1       cgd  */
   1484       1.1       cgd /* ARGSUSED */
   1485      1.41  christos int
   1486      1.41  christos kernfs_print(v)
   1487      1.41  christos 	void *v;
   1488      1.23   mycroft {
   1489      1.23   mycroft 
   1490      1.47  christos 	printf("tag VT_KERNFS, kernfs vnode\n");
   1491      1.23   mycroft 	return (0);
   1492      1.23   mycroft }
   1493      1.23   mycroft 
   1494      1.40   mycroft int
   1495      1.41  christos kernfs_link(v)
   1496      1.41  christos 	void *v;
   1497      1.41  christos {
   1498      1.40   mycroft 	struct vop_link_args /* {
   1499      1.40   mycroft 		struct vnode *a_dvp;
   1500      1.40   mycroft 		struct vnode *a_vp;
   1501      1.40   mycroft 		struct componentname *a_cnp;
   1502      1.41  christos 	} */ *ap = v;
   1503      1.40   mycroft 
   1504      1.40   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1505      1.40   mycroft 	vput(ap->a_dvp);
   1506      1.40   mycroft 	return (EROFS);
   1507      1.40   mycroft }
   1508      1.40   mycroft 
   1509      1.40   mycroft int
   1510      1.41  christos kernfs_symlink(v)
   1511      1.41  christos 	void *v;
   1512      1.41  christos {
   1513      1.40   mycroft 	struct vop_symlink_args /* {
   1514      1.40   mycroft 		struct vnode *a_dvp;
   1515      1.40   mycroft 		struct vnode **a_vpp;
   1516      1.40   mycroft 		struct componentname *a_cnp;
   1517      1.40   mycroft 		struct vattr *a_vap;
   1518      1.40   mycroft 		char *a_target;
   1519      1.41  christos 	} */ *ap = v;
   1520      1.40   mycroft 
   1521      1.40   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1522      1.40   mycroft 	vput(ap->a_dvp);
   1523      1.40   mycroft 	return (EROFS);
   1524       1.1       cgd }
   1525