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