Home | History | Annotate | Line # | Download | only in kernfs
kernfs_vnops.c revision 1.166.6.1
      1  1.166.6.1   thorpej /*	$NetBSD: kernfs_vnops.c,v 1.166.6.1 2021/08/01 22:42:40 thorpej Exp $	*/
      2       1.27       cgd 
      3        1.1       cgd /*
      4       1.23   mycroft  * Copyright (c) 1992, 1993
      5       1.23   mycroft  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7       1.17       cgd  * This code is derived from software donated to Berkeley by
      8        1.1       cgd  * Jan-Simon Pendry.
      9        1.1       cgd  *
     10        1.2       cgd  * Redistribution and use in source and binary forms, with or without
     11        1.2       cgd  * modification, are permitted provided that the following conditions
     12        1.2       cgd  * are met:
     13        1.2       cgd  * 1. Redistributions of source code must retain the above copyright
     14        1.2       cgd  *    notice, this list of conditions and the following disclaimer.
     15        1.2       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.2       cgd  *    notice, this list of conditions and the following disclaimer in the
     17        1.2       cgd  *    documentation and/or other materials provided with the distribution.
     18       1.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.166.6.1   thorpej __KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.166.6.1 2021/08/01 22:42:40 thorpej Exp $");
     43       1.55       mrg 
     44       1.14   mycroft #include <sys/param.h>
     45       1.14   mycroft #include <sys/systm.h>
     46       1.14   mycroft #include <sys/kernel.h>
     47       1.23   mycroft #include <sys/vmmeter.h>
     48       1.14   mycroft #include <sys/time.h>
     49       1.14   mycroft #include <sys/proc.h>
     50       1.23   mycroft #include <sys/vnode.h>
     51       1.23   mycroft #include <sys/malloc.h>
     52       1.14   mycroft #include <sys/file.h>
     53       1.14   mycroft #include <sys/stat.h>
     54       1.14   mycroft #include <sys/mount.h>
     55       1.14   mycroft #include <sys/namei.h>
     56       1.14   mycroft #include <sys/buf.h>
     57       1.23   mycroft #include <sys/dirent.h>
     58       1.28   mycroft #include <sys/msgbuf.h>
     59       1.44   mycroft 
     60       1.44   mycroft #include <miscfs/genfs/genfs.h>
     61       1.17       cgd #include <miscfs/kernfs/kernfs.h>
     62      1.151  christos #include <miscfs/specfs/specdev.h>
     63       1.63       mrg 
     64       1.54       mrg #include <uvm/uvm_extern.h>
     65       1.54       mrg 
     66       1.17       cgd #define KSTRING	256		/* Largest I/O available via this filesystem */
     67       1.17       cgd #define	UIO_MX 32
     68        1.1       cgd 
     69       1.23   mycroft #define	READ_MODE	(S_IRUSR|S_IRGRP|S_IROTH)
     70       1.23   mycroft #define	WRITE_MODE	(S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH)
     71      1.101        cl #define	UREAD_MODE	(S_IRUSR)
     72      1.102        cl #define	DIR_MODE	(S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
     73      1.102        cl #define	UDIR_MODE	(S_IRUSR|S_IXUSR)
     74       1.23   mycroft 
     75       1.90    itojun #define N(s) sizeof(s)-1, s
     76       1.75  jdolecek const struct kern_target kern_targets[] = {
     77        1.1       cgd /* NOTE: The name must be less than UIO_MX-16 chars in length */
     78       1.23   mycroft      /*        name            data          tag           type  ro/rw */
     79       1.98     darcy      { DT_DIR, N("."),         0,            KFSkern,        VDIR, DIR_MODE   },
     80       1.98     darcy      { DT_DIR, N(".."),        0,            KFSroot,        VDIR, DIR_MODE   },
     81      1.162   thorpej      { DT_REG, N("boottime"),  0,            KFSboottime,    VREG, READ_MODE  },
     82      1.109  christos 			/* XXXUNCONST */
     83      1.109  christos      { DT_REG, N("copyright"), __UNCONST(copyright),
     84       1.98     darcy      					     KFSstring,      VREG, READ_MODE  },
     85       1.98     darcy      { DT_REG, N("hostname"),  0,            KFShostname,    VREG, WRITE_MODE },
     86       1.98     darcy      { DT_REG, N("hz"),        &hz,          KFSint,         VREG, READ_MODE  },
     87       1.98     darcy      { DT_REG, N("loadavg"),   0,            KFSavenrun,     VREG, READ_MODE  },
     88       1.98     darcy      { DT_REG, N("msgbuf"),    0,	     KFSmsgbuf,      VREG, READ_MODE  },
     89       1.98     darcy      { DT_REG, N("pagesize"),  &uvmexp.pagesize, KFSint,     VREG, READ_MODE  },
     90       1.98     darcy      { DT_REG, N("physmem"),   &physmem,     KFSint,         VREG, READ_MODE  },
     91       1.17       cgd #if 0
     92       1.98     darcy      { DT_DIR, N("root"),      0,            KFSnull,        VDIR, DIR_MODE   },
     93       1.17       cgd #endif
     94  1.166.6.1   thorpej      { DT_BLK, N("rootdev"),   &rootdev,     KFSdevice,      VBLK, UREAD_MODE  },
     95  1.166.6.1   thorpej      { DT_CHR, N("rrootdev"),  &rrootdev,    KFSdevice,      VCHR, UREAD_MODE  },
     96       1.98     darcy      { DT_REG, N("time"),      0,            KFStime,        VREG, READ_MODE  },
     97      1.109  christos 			/* XXXUNCONST */
     98      1.109  christos      { DT_REG, N("version"),   __UNCONST(version),
     99       1.98     darcy      					     KFSstring,      VREG, READ_MODE  },
    100       1.90    itojun };
    101      1.102        cl const struct kern_target subdir_targets[] = {
    102      1.102        cl /* NOTE: The name must be less than UIO_MX-16 chars in length */
    103      1.102        cl      /*        name            data          tag           type  ro/rw */
    104      1.102        cl      { DT_DIR, N("."),         0,            KFSsubdir,      VDIR, DIR_MODE   },
    105      1.102        cl      { DT_DIR, N(".."),        0,            KFSkern,        VDIR, DIR_MODE   },
    106      1.102        cl };
    107       1.23   mycroft #undef N
    108      1.102        cl SIMPLEQ_HEAD(,dyn_kern_target) dyn_kern_targets =
    109      1.102        cl 	SIMPLEQ_HEAD_INITIALIZER(dyn_kern_targets);
    110       1.90    itojun int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);
    111      1.102        cl const int static_nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);
    112      1.102        cl int nkern_dirs = 2;
    113       1.90    itojun 
    114      1.102        cl int kernfs_try_fileop(kfstype, kfsfileop, void *, int);
    115      1.124    bouyer int kernfs_try_xread(kfstype, const struct kernfs_node *, char **,
    116      1.123    bouyer     size_t, int);
    117      1.102        cl int kernfs_try_xwrite(kfstype, const struct kernfs_node *, char *,
    118      1.102        cl     size_t, int);
    119      1.102        cl 
    120      1.123    bouyer static int kernfs_default_xread(void *v);
    121      1.102        cl static int kernfs_default_xwrite(void *v);
    122      1.102        cl static int kernfs_default_fileop_getattr(void *);
    123      1.102        cl 
    124      1.102        cl /* must include all fileop's */
    125      1.102        cl const struct kernfs_fileop kernfs_default_fileops[] = {
    126      1.123    bouyer   { .kf_fileop = KERNFS_XREAD },
    127      1.102        cl   { .kf_fileop = KERNFS_XWRITE },
    128      1.102        cl   { .kf_fileop = KERNFS_FILEOP_OPEN },
    129      1.102        cl   { .kf_fileop = KERNFS_FILEOP_GETATTR,
    130      1.125  christos     .kf_vop = kernfs_default_fileop_getattr },
    131      1.102        cl   { .kf_fileop = KERNFS_FILEOP_IOCTL },
    132      1.102        cl   { .kf_fileop = KERNFS_FILEOP_CLOSE },
    133      1.125  christos   { .kf_fileop = KERNFS_FILEOP_READ,
    134      1.125  christos     .kf_vop = kernfs_default_xread },
    135      1.125  christos   { .kf_fileop = KERNFS_FILEOP_WRITE,
    136      1.125  christos     .kf_vop = kernfs_default_xwrite },
    137      1.102        cl };
    138        1.1       cgd 
    139      1.110   xtraeme int	kernfs_lookup(void *);
    140      1.110   xtraeme int	kernfs_open(void *);
    141      1.110   xtraeme int	kernfs_close(void *);
    142      1.110   xtraeme int	kernfs_access(void *);
    143      1.110   xtraeme int	kernfs_getattr(void *);
    144      1.110   xtraeme int	kernfs_setattr(void *);
    145      1.110   xtraeme int	kernfs_read(void *);
    146      1.110   xtraeme int	kernfs_write(void *);
    147      1.110   xtraeme int	kernfs_ioctl(void *);
    148      1.110   xtraeme int	kernfs_link(void *);
    149      1.110   xtraeme int	kernfs_symlink(void *);
    150      1.110   xtraeme int	kernfs_readdir(void *);
    151      1.110   xtraeme int	kernfs_inactive(void *);
    152      1.110   xtraeme int	kernfs_reclaim(void *);
    153      1.110   xtraeme int	kernfs_print(void *);
    154      1.110   xtraeme int	kernfs_pathconf(void *);
    155      1.161   hannken int	kernfs_getpages(void *);
    156       1.41  christos 
    157      1.110   xtraeme static int	kernfs_xread(struct kernfs_node *, int, char **,
    158      1.110   xtraeme 				size_t, size_t *);
    159      1.110   xtraeme static int	kernfs_xwrite(const struct kernfs_node *, char *, size_t);
    160       1.41  christos 
    161      1.110   xtraeme int (**kernfs_vnodeop_p)(void *);
    162       1.71  jdolecek const struct vnodeopv_entry_desc kernfs_vnodeop_entries[] = {
    163       1.41  christos 	{ &vop_default_desc, vn_default_error },
    164  1.166.6.1   thorpej 	{ &vop_parsepath_desc, genfs_parsepath },	/* parsepath */
    165       1.44   mycroft 	{ &vop_lookup_desc, kernfs_lookup },		/* lookup */
    166  1.166.6.1   thorpej 	{ &vop_create_desc, genfs_eopnotsupp },		/* create */
    167  1.166.6.1   thorpej 	{ &vop_mknod_desc, genfs_eopnotsupp },		/* mknod */
    168       1.44   mycroft 	{ &vop_open_desc, kernfs_open },		/* open */
    169       1.44   mycroft 	{ &vop_close_desc, kernfs_close },		/* close */
    170       1.44   mycroft 	{ &vop_access_desc, kernfs_access },		/* access */
    171      1.165  christos 	{ &vop_accessx_desc, genfs_accessx },		/* accessx */
    172       1.44   mycroft 	{ &vop_getattr_desc, kernfs_getattr },		/* getattr */
    173       1.44   mycroft 	{ &vop_setattr_desc, kernfs_setattr },		/* setattr */
    174       1.44   mycroft 	{ &vop_read_desc, kernfs_read },		/* read */
    175       1.44   mycroft 	{ &vop_write_desc, kernfs_write },		/* write */
    176      1.154  dholland 	{ &vop_fallocate_desc, genfs_eopnotsupp },	/* fallocate */
    177      1.154  dholland 	{ &vop_fdiscard_desc, genfs_eopnotsupp },	/* fdiscard */
    178  1.166.6.1   thorpej 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    179       1.44   mycroft 	{ &vop_ioctl_desc, kernfs_ioctl },		/* ioctl */
    180  1.166.6.1   thorpej 	{ &vop_poll_desc, genfs_poll },			/* poll */
    181  1.166.6.1   thorpej 	{ &vop_kqfilter_desc, genfs_kqfilter },		/* kqfilter */
    182  1.166.6.1   thorpej 	{ &vop_revoke_desc, genfs_revoke },		/* revoke */
    183  1.166.6.1   thorpej 	{ &vop_fsync_desc, genfs_nullop },		/* fsync */
    184  1.166.6.1   thorpej 	{ &vop_seek_desc, genfs_nullop },		/* seek */
    185  1.166.6.1   thorpej 	{ &vop_remove_desc, genfs_eopnotsupp },		/* remove */
    186       1.44   mycroft 	{ &vop_link_desc, kernfs_link },		/* link */
    187  1.166.6.1   thorpej 	{ &vop_rename_desc, genfs_eopnotsupp },		/* rename */
    188  1.166.6.1   thorpej 	{ &vop_mkdir_desc, genfs_eopnotsupp },		/* mkdir */
    189  1.166.6.1   thorpej 	{ &vop_rmdir_desc, genfs_eopnotsupp },		/* rmdir */
    190       1.44   mycroft 	{ &vop_symlink_desc, kernfs_symlink },		/* symlink */
    191       1.44   mycroft 	{ &vop_readdir_desc, kernfs_readdir },		/* readdir */
    192  1.166.6.1   thorpej 	{ &vop_readlink_desc, genfs_eopnotsupp },	/* readlink */
    193  1.166.6.1   thorpej 	{ &vop_abortop_desc, genfs_abortop },		/* abortop */
    194       1.44   mycroft 	{ &vop_inactive_desc, kernfs_inactive },	/* inactive */
    195       1.44   mycroft 	{ &vop_reclaim_desc, kernfs_reclaim },		/* reclaim */
    196  1.166.6.1   thorpej 	{ &vop_lock_desc, genfs_lock },			/* lock */
    197  1.166.6.1   thorpej 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
    198  1.166.6.1   thorpej 	{ &vop_bmap_desc, genfs_badop },		/* bmap */
    199  1.166.6.1   thorpej 	{ &vop_strategy_desc, genfs_eopnotsupp },	/* strategy */
    200       1.44   mycroft 	{ &vop_print_desc, kernfs_print },		/* print */
    201  1.166.6.1   thorpej 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
    202       1.44   mycroft 	{ &vop_pathconf_desc, kernfs_pathconf },	/* pathconf */
    203  1.166.6.1   thorpej 	{ &vop_advlock_desc, genfs_einval },		/* advlock */
    204  1.166.6.1   thorpej 	{ &vop_bwrite_desc, genfs_eopnotsupp },		/* bwrite */
    205      1.161   hannken 	{ &vop_getpages_desc, kernfs_getpages },	/* getpages */
    206  1.166.6.1   thorpej 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
    207       1.76       chs 	{ NULL, NULL }
    208       1.41  christos };
    209       1.71  jdolecek const struct vnodeopv_desc kernfs_vnodeop_opv_desc =
    210       1.41  christos 	{ &kernfs_vnodeop_p, kernfs_vnodeop_entries };
    211       1.41  christos 
    212      1.163  riastrad int (**kernfs_specop_p)(void *);
    213      1.163  riastrad const struct vnodeopv_entry_desc kernfs_specop_entries[] = {
    214      1.163  riastrad 	{ &vop_default_desc, vn_default_error },
    215  1.166.6.1   thorpej 	GENFS_SPECOP_ENTRIES,
    216      1.163  riastrad 	{ &vop_close_desc, spec_close },		/* close */
    217      1.163  riastrad 	{ &vop_access_desc, kernfs_access },		/* access */
    218      1.165  christos 	{ &vop_accessx_desc, genfs_accessx },		/* accessx */
    219      1.163  riastrad 	{ &vop_getattr_desc, kernfs_getattr },		/* getattr */
    220      1.163  riastrad 	{ &vop_setattr_desc, kernfs_setattr },		/* setattr */
    221      1.163  riastrad 	{ &vop_read_desc, spec_read },			/* read */
    222      1.163  riastrad 	{ &vop_write_desc, spec_write },		/* write */
    223  1.166.6.1   thorpej 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
    224      1.163  riastrad 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
    225      1.163  riastrad 	{ &vop_inactive_desc, kernfs_inactive },	/* inactive */
    226      1.163  riastrad 	{ &vop_reclaim_desc, kernfs_reclaim },		/* reclaim */
    227  1.166.6.1   thorpej 	{ &vop_lock_desc, genfs_lock },			/* lock */
    228  1.166.6.1   thorpej 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
    229      1.163  riastrad 	{ &vop_print_desc, kernfs_print },		/* print */
    230  1.166.6.1   thorpej 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
    231  1.166.6.1   thorpej 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
    232      1.163  riastrad 	{ NULL, NULL }
    233      1.163  riastrad };
    234      1.163  riastrad const struct vnodeopv_desc kernfs_specop_opv_desc =
    235      1.163  riastrad 	{ &kernfs_specop_p, kernfs_specop_entries };
    236      1.163  riastrad 
    237      1.116     perry static inline int
    238      1.102        cl kernfs_fileop_compare(struct kernfs_fileop *a, struct kernfs_fileop *b)
    239      1.102        cl {
    240      1.102        cl 	if (a->kf_type < b->kf_type)
    241      1.102        cl 		return -1;
    242      1.102        cl 	if (a->kf_type > b->kf_type)
    243      1.102        cl 		return 1;
    244      1.102        cl 	if (a->kf_fileop < b->kf_fileop)
    245      1.102        cl 		return -1;
    246      1.102        cl 	if (a->kf_fileop > b->kf_fileop)
    247      1.102        cl 		return 1;
    248      1.102        cl 	return (0);
    249      1.102        cl }
    250      1.102        cl 
    251      1.102        cl SPLAY_HEAD(kfsfileoptree, kernfs_fileop) kfsfileoptree =
    252      1.102        cl 	SPLAY_INITIALIZER(kfsfileoptree);
    253      1.102        cl SPLAY_PROTOTYPE(kfsfileoptree, kernfs_fileop, kf_node, kernfs_fileop_compare);
    254      1.102        cl SPLAY_GENERATE(kfsfileoptree, kernfs_fileop, kf_node, kernfs_fileop_compare);
    255      1.102        cl 
    256      1.102        cl kfstype
    257      1.102        cl kernfs_alloctype(int nkf, const struct kernfs_fileop *kf)
    258      1.102        cl {
    259      1.102        cl 	static u_char nextfreetype = KFSlasttype;
    260      1.102        cl 	struct kernfs_fileop *dkf, *fkf, skf;
    261      1.102        cl 	int i;
    262      1.102        cl 
    263      1.102        cl 	/* XXX need to keep track of dkf's memory if we support
    264      1.102        cl            deallocating types */
    265      1.102        cl 	dkf = malloc(sizeof(kernfs_default_fileops), M_TEMP, M_WAITOK);
    266      1.102        cl 	memcpy(dkf, kernfs_default_fileops, sizeof(kernfs_default_fileops));
    267      1.102        cl 
    268      1.102        cl 	for (i = 0; i < sizeof(kernfs_default_fileops) /
    269      1.102        cl 		     sizeof(kernfs_default_fileops[0]); i++) {
    270      1.102        cl 		dkf[i].kf_type = nextfreetype;
    271      1.102        cl 		SPLAY_INSERT(kfsfileoptree, &kfsfileoptree, &dkf[i]);
    272      1.102        cl 	}
    273      1.102        cl 
    274      1.102        cl 	for (i = 0; i < nkf; i++) {
    275      1.102        cl 		skf.kf_type = nextfreetype;
    276      1.102        cl 		skf.kf_fileop = kf[i].kf_fileop;
    277      1.102        cl 		if ((fkf = SPLAY_FIND(kfsfileoptree, &kfsfileoptree, &skf)))
    278      1.125  christos 			fkf->kf_vop = kf[i].kf_vop;
    279      1.102        cl 	}
    280      1.102        cl 
    281      1.102        cl 	return nextfreetype++;
    282      1.102        cl }
    283      1.102        cl 
    284      1.102        cl int
    285      1.102        cl kernfs_try_fileop(kfstype type, kfsfileop fileop, void *v, int error)
    286      1.102        cl {
    287      1.102        cl 	struct kernfs_fileop *kf, skf;
    288      1.102        cl 
    289      1.102        cl 	skf.kf_type = type;
    290      1.102        cl 	skf.kf_fileop = fileop;
    291      1.102        cl 	if ((kf = SPLAY_FIND(kfsfileoptree, &kfsfileoptree, &skf)))
    292      1.102        cl 		if (kf->kf_vop)
    293      1.102        cl 			return kf->kf_vop(v);
    294      1.102        cl 	return error;
    295      1.102        cl }
    296      1.102        cl 
    297      1.102        cl int
    298      1.124    bouyer kernfs_try_xread(kfstype type, const struct kernfs_node *kfs, char **bfp,
    299      1.124    bouyer     size_t len, int error)
    300      1.124    bouyer {
    301      1.124    bouyer 	struct kernfs_fileop *kf, skf;
    302      1.124    bouyer 
    303      1.124    bouyer 	skf.kf_type = type;
    304      1.124    bouyer 	skf.kf_fileop = KERNFS_XREAD;
    305      1.124    bouyer 	if ((kf = SPLAY_FIND(kfsfileoptree, &kfsfileoptree, &skf)))
    306      1.124    bouyer 		if (kf->kf_xread)
    307      1.124    bouyer 			return kf->kf_xread(kfs, bfp, len);
    308      1.124    bouyer 	return error;
    309      1.124    bouyer }
    310      1.124    bouyer 
    311      1.124    bouyer int
    312      1.109  christos kernfs_try_xwrite(kfstype type, const struct kernfs_node *kfs, char *bf,
    313      1.102        cl     size_t len, 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 = KERNFS_XWRITE;
    319      1.102        cl 	if ((kf = SPLAY_FIND(kfsfileoptree, &kfsfileoptree, &skf)))
    320      1.102        cl 		if (kf->kf_xwrite)
    321      1.109  christos 			return kf->kf_xwrite(kfs, bf, len);
    322      1.102        cl 	return error;
    323      1.102        cl }
    324      1.102        cl 
    325      1.102        cl int
    326      1.102        cl kernfs_addentry(kernfs_parentdir_t *pkt, kernfs_entry_t *dkt)
    327      1.102        cl {
    328      1.102        cl 	struct kernfs_subdir *ks, *parent;
    329      1.102        cl 
    330      1.102        cl 	if (pkt == NULL) {
    331      1.102        cl 		SIMPLEQ_INSERT_TAIL(&dyn_kern_targets, dkt, dkt_queue);
    332      1.102        cl 		nkern_targets++;
    333      1.102        cl 		if (dkt->dkt_kt.kt_vtype == VDIR)
    334      1.102        cl 			nkern_dirs++;
    335      1.102        cl 	} else {
    336      1.102        cl 		parent = (struct kernfs_subdir *)pkt->kt_data;
    337      1.102        cl 		SIMPLEQ_INSERT_TAIL(&parent->ks_entries, dkt, dkt_queue);
    338      1.102        cl 		parent->ks_nentries++;
    339      1.102        cl 		if (dkt->dkt_kt.kt_vtype == VDIR)
    340      1.102        cl 			parent->ks_dirs++;
    341      1.102        cl 	}
    342      1.102        cl 	if (dkt->dkt_kt.kt_vtype == VDIR && dkt->dkt_kt.kt_data == NULL) {
    343      1.102        cl 		ks = malloc(sizeof(struct kernfs_subdir),
    344      1.102        cl 		    M_TEMP, M_WAITOK);
    345      1.102        cl 		SIMPLEQ_INIT(&ks->ks_entries);
    346      1.102        cl 		ks->ks_nentries = 2; /* . and .. */
    347      1.102        cl 		ks->ks_dirs = 2;
    348      1.102        cl 		ks->ks_parent = pkt ? pkt : &kern_targets[0];
    349      1.102        cl 		dkt->dkt_kt.kt_data = ks;
    350      1.102        cl 	}
    351      1.102        cl 	return 0;
    352      1.102        cl }
    353      1.102        cl 
    354       1.82  jdolecek static int
    355      1.136       dsl kernfs_xread(struct kernfs_node *kfs, int off, char **bufp, size_t len, size_t *wrlen)
    356        1.1       cgd {
    357       1.90    itojun 	const struct kern_target *kt;
    358      1.124    bouyer 	int err;
    359       1.90    itojun 
    360       1.90    itojun 	kt = kfs->kfs_kt;
    361        1.1       cgd 
    362       1.90    itojun 	switch (kfs->kfs_type) {
    363       1.98     darcy 	case KFStime: {
    364        1.1       cgd 		struct timeval tv;
    365       1.28   mycroft 
    366        1.1       cgd 		microtime(&tv);
    367      1.135  christos 		snprintf(*bufp, len, "%lld %ld\n", (long long)tv.tv_sec,
    368      1.135  christos 		    (long)tv.tv_usec);
    369        1.1       cgd 		break;
    370        1.1       cgd 	}
    371        1.1       cgd 
    372      1.162   thorpej 	case KFSboottime: {
    373      1.162   thorpej 		struct timeval tv;
    374      1.162   thorpej 
    375      1.162   thorpej 		/*
    376      1.162   thorpej 		 * Historically, /kern/boottime only contained seconds.
    377      1.162   thorpej 		 */
    378      1.162   thorpej 		getmicroboottime(&tv);
    379      1.162   thorpej 		snprintf(*bufp, len, "%lld\n", (long long)tv.tv_sec);
    380      1.162   thorpej 		break;
    381      1.162   thorpej 	}
    382      1.162   thorpej 
    383       1.98     darcy 	case KFSint: {
    384        1.1       cgd 		int *ip = kt->kt_data;
    385       1.28   mycroft 
    386       1.90    itojun 		snprintf(*bufp, len, "%d\n", *ip);
    387        1.1       cgd 		break;
    388        1.1       cgd 	}
    389        1.1       cgd 
    390       1.98     darcy 	case KFSstring: {
    391        1.1       cgd 		char *cp = kt->kt_data;
    392        1.1       cgd 
    393       1.28   mycroft 		*bufp = cp;
    394       1.28   mycroft 		break;
    395       1.28   mycroft 	}
    396        1.1       cgd 
    397       1.98     darcy 	case KFSmsgbuf: {
    398       1.28   mycroft 		long n;
    399       1.28   mycroft 
    400       1.52       leo 		/*
    401       1.52       leo 		 * deal with cases where the message buffer has
    402       1.52       leo 		 * become corrupted.
    403       1.52       leo 		 */
    404      1.159  christos 		if (!logenabled(msgbufp)) {
    405       1.52       leo 			msgbufenabled = 0;
    406       1.52       leo 			return (ENXIO);
    407       1.52       leo 		}
    408       1.52       leo 
    409       1.52       leo 		/*
    410       1.52       leo 		 * Note that reads of /kern/msgbuf won't necessarily yield
    411       1.52       leo 		 * consistent results, if the message buffer is modified
    412       1.52       leo 		 * while the read is in progress.  The worst that can happen
    413       1.52       leo 		 * is that incorrect data will be read.  There's no way
    414       1.52       leo 		 * that this can crash the system unless the values in the
    415       1.52       leo 		 * message buffer header are corrupted, but that'll cause
    416       1.52       leo 		 * the system to die anyway.
    417       1.52       leo 		 */
    418       1.82  jdolecek 		if (off >= msgbufp->msg_bufs) {
    419       1.82  jdolecek 			*wrlen = 0;
    420       1.28   mycroft 			return (0);
    421       1.82  jdolecek 		}
    422       1.28   mycroft 		n = msgbufp->msg_bufx + off;
    423       1.52       leo 		if (n >= msgbufp->msg_bufs)
    424       1.52       leo 			n -= msgbufp->msg_bufs;
    425      1.160  riastrad 		len = uimin(msgbufp->msg_bufs - n, msgbufp->msg_bufs - off);
    426       1.28   mycroft 		*bufp = msgbufp->msg_bufc + n;
    427       1.82  jdolecek 		*wrlen = len;
    428       1.82  jdolecek 		return (0);
    429        1.1       cgd 	}
    430        1.1       cgd 
    431       1.98     darcy 	case KFShostname: {
    432        1.1       cgd 		char *cp = hostname;
    433      1.127      jmmv 		size_t xlen = hostnamelen;
    434        1.1       cgd 
    435       1.90    itojun 		if (xlen >= (len - 2))
    436        1.1       cgd 			return (EINVAL);
    437        1.1       cgd 
    438       1.60     perry 		memcpy(*bufp, cp, xlen);
    439       1.28   mycroft 		(*bufp)[xlen] = '\n';
    440       1.28   mycroft 		(*bufp)[xlen+1] = '\0';
    441        1.1       cgd 		break;
    442        1.1       cgd 	}
    443        1.1       cgd 
    444       1.98     darcy 	case KFSavenrun:
    445       1.31   mycroft 		averunnable.fscale = FSCALE;
    446       1.90    itojun 		snprintf(*bufp, len, "%d %d %d %ld\n",
    447       1.23   mycroft 		    averunnable.ldavg[0], averunnable.ldavg[1],
    448       1.23   mycroft 		    averunnable.ldavg[2], averunnable.fscale);
    449        1.1       cgd 		break;
    450        1.1       cgd 
    451        1.1       cgd 	default:
    452      1.124    bouyer 		err = kernfs_try_xread(kfs->kfs_type, kfs, bufp, len,
    453      1.124    bouyer 		    EOPNOTSUPP);
    454      1.124    bouyer 		if (err)
    455      1.124    bouyer 			return err;
    456        1.1       cgd 	}
    457        1.1       cgd 
    458       1.28   mycroft 	len = strlen(*bufp);
    459       1.28   mycroft 	if (len <= off)
    460       1.82  jdolecek 		*wrlen = 0;
    461       1.82  jdolecek 	else {
    462       1.82  jdolecek 		*bufp += off;
    463       1.82  jdolecek 		*wrlen = len - off;
    464       1.82  jdolecek 	}
    465       1.82  jdolecek 	return (0);
    466        1.1       cgd }
    467        1.1       cgd 
    468       1.82  jdolecek static int
    469      1.136       dsl kernfs_xwrite(const struct kernfs_node *kfs, char *bf, size_t len)
    470        1.1       cgd {
    471       1.23   mycroft 
    472       1.90    itojun 	switch (kfs->kfs_type) {
    473       1.98     darcy 	case KFShostname:
    474      1.109  christos 		if (bf[len-1] == '\n')
    475        1.1       cgd 			--len;
    476      1.109  christos 		memcpy(hostname, bf, len);
    477       1.17       cgd 		hostname[len] = '\0';
    478       1.82  jdolecek 		hostnamelen = (size_t) len;
    479        1.1       cgd 		return (0);
    480        1.1       cgd 
    481        1.1       cgd 	default:
    482      1.109  christos 		return kernfs_try_xwrite(kfs->kfs_type, kfs, bf, len, EIO);
    483        1.1       cgd 	}
    484        1.1       cgd }
    485        1.1       cgd 
    486       1.17       cgd 
    487       1.17       cgd /*
    488        1.1       cgd  * vp is the current namei directory
    489        1.1       cgd  * ndp is the name to locate in that directory...
    490        1.1       cgd  */
    491       1.41  christos int
    492      1.136       dsl kernfs_lookup(void *v)
    493       1.41  christos {
    494      1.150   hannken 	struct vop_lookup_v2_args /* {
    495       1.23   mycroft 		struct vnode * a_dvp;
    496       1.23   mycroft 		struct vnode ** a_vpp;
    497       1.23   mycroft 		struct componentname * a_cnp;
    498       1.41  christos 	} */ *ap = v;
    499       1.23   mycroft 	struct componentname *cnp = ap->a_cnp;
    500       1.23   mycroft 	struct vnode **vpp = ap->a_vpp;
    501       1.23   mycroft 	struct vnode *dvp = ap->a_dvp;
    502       1.48       cgd 	const char *pname = cnp->cn_nameptr;
    503       1.90    itojun 	const struct kernfs_node *kfs;
    504       1.75  jdolecek 	const struct kern_target *kt;
    505      1.102        cl 	const struct dyn_kern_target *dkt;
    506      1.102        cl 	const struct kernfs_subdir *ks;
    507      1.129       chs 	int error, i;
    508       1.23   mycroft 
    509       1.35   mycroft 	*vpp = NULLVP;
    510       1.35   mycroft 
    511       1.35   mycroft 	if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
    512       1.35   mycroft 		return (EROFS);
    513       1.35   mycroft 
    514       1.23   mycroft 	if (cnp->cn_namelen == 1 && *pname == '.') {
    515       1.23   mycroft 		*vpp = dvp;
    516      1.139     pooka 		vref(dvp);
    517        1.1       cgd 		return (0);
    518        1.1       cgd 	}
    519       1.13       cgd 
    520       1.90    itojun 	kfs = VTOKERN(dvp);
    521       1.90    itojun 	switch (kfs->kfs_type) {
    522       1.98     darcy 	case KFSkern:
    523       1.90    itojun 		/*
    524       1.90    itojun 		 * Shouldn't get here with .. in the root node.
    525       1.90    itojun 		 */
    526       1.90    itojun 		if (cnp->cn_flags & ISDOTDOT)
    527       1.90    itojun 			return (EIO);
    528       1.64  wrstuden 
    529      1.102        cl 		for (i = 0; i < static_nkern_targets; i++) {
    530       1.90    itojun 			kt = &kern_targets[i];
    531       1.90    itojun 			if (cnp->cn_namelen == kt->kt_namlen &&
    532       1.90    itojun 			    memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
    533       1.90    itojun 				goto found;
    534       1.90    itojun 		}
    535      1.102        cl 		SIMPLEQ_FOREACH(dkt, &dyn_kern_targets, dkt_queue) {
    536      1.102        cl 			if (cnp->cn_namelen == dkt->dkt_kt.kt_namlen &&
    537      1.102        cl 			    memcmp(dkt->dkt_kt.kt_name, pname, cnp->cn_namelen) == 0) {
    538      1.102        cl 				kt = &dkt->dkt_kt;
    539      1.102        cl 				goto found;
    540      1.102        cl 			}
    541      1.102        cl 		}
    542       1.90    itojun 		break;
    543       1.25   mycroft 
    544       1.90    itojun 	found:
    545      1.153   hannken 		error = vcache_get(dvp->v_mount, &kt, sizeof(kt), vpp);
    546      1.153   hannken 		return error;
    547       1.64  wrstuden 
    548      1.102        cl 	case KFSsubdir:
    549      1.102        cl 		ks = (struct kernfs_subdir *)kfs->kfs_kt->kt_data;
    550      1.102        cl 		if (cnp->cn_flags & ISDOTDOT) {
    551      1.102        cl 			kt = ks->ks_parent;
    552      1.102        cl 			goto found;
    553      1.102        cl 		}
    554      1.102        cl 
    555      1.102        cl 		SIMPLEQ_FOREACH(dkt, &ks->ks_entries, dkt_queue) {
    556      1.102        cl 			if (cnp->cn_namelen == dkt->dkt_kt.kt_namlen &&
    557      1.102        cl 			    memcmp(dkt->dkt_kt.kt_name, pname, cnp->cn_namelen) == 0) {
    558      1.102        cl 				kt = &dkt->dkt_kt;
    559      1.102        cl 				goto found;
    560      1.102        cl 			}
    561      1.102        cl 		}
    562      1.102        cl 		break;
    563      1.102        cl 
    564       1.90    itojun 	default:
    565       1.90    itojun 		return (ENOTDIR);
    566       1.90    itojun 	}
    567       1.90    itojun 
    568       1.90    itojun 	return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
    569       1.90    itojun }
    570       1.90    itojun 
    571       1.90    itojun int
    572      1.136       dsl kernfs_open(void *v)
    573       1.90    itojun {
    574       1.90    itojun 	struct vop_open_args /* {
    575       1.90    itojun 		struct vnode *a_vp;
    576       1.90    itojun 		int a_mode;
    577      1.120      elad 		kauth_cred_t a_cred;
    578       1.90    itojun 	} */ *ap = v;
    579       1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    580       1.90    itojun 
    581      1.146  drochner 	return kernfs_try_fileop(kfs->kfs_type, KERNFS_FILEOP_OPEN, v, 0);
    582       1.90    itojun }
    583        1.1       cgd 
    584       1.90    itojun int
    585      1.136       dsl kernfs_close(void *v)
    586       1.90    itojun {
    587       1.90    itojun 	struct vop_close_args /* {
    588       1.90    itojun 		struct vnode *a_vp;
    589       1.90    itojun 		int a_fflag;
    590      1.120      elad 		kauth_cred_t a_cred;
    591       1.90    itojun 	} */ *ap = v;
    592       1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    593       1.90    itojun 
    594      1.146  drochner 	return kernfs_try_fileop(kfs->kfs_type, KERNFS_FILEOP_CLOSE, v, 0);
    595        1.1       cgd }
    596        1.1       cgd 
    597       1.28   mycroft int
    598      1.136       dsl kernfs_access(void *v)
    599       1.41  christos {
    600       1.23   mycroft 	struct vop_access_args /* {
    601       1.23   mycroft 		struct vnode *a_vp;
    602      1.165  christos 		accmode_t a_accmode;
    603      1.120      elad 		kauth_cred_t a_cred;
    604       1.41  christos 	} */ *ap = v;
    605       1.90    itojun 	struct vattr va;
    606       1.90    itojun 	int error;
    607       1.17       cgd 
    608      1.133     pooka 	if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred)) != 0)
    609       1.90    itojun 		return (error);
    610       1.49   mycroft 
    611      1.145      elad 	return kauth_authorize_vnode(ap->a_cred,
    612      1.165  christos 	    KAUTH_ACCESS_ACTION(ap->a_accmode, ap->a_vp->v_type, va.va_mode),
    613      1.165  christos 	    ap->a_vp, NULL, genfs_can_access(ap->a_vp, ap->a_cred,
    614      1.165  christos 	    va.va_uid, va.va_gid, va.va_mode, NULL, ap->a_accmode));
    615       1.23   mycroft }
    616       1.23   mycroft 
    617      1.102        cl static int
    618      1.136       dsl kernfs_default_fileop_getattr(void *v)
    619      1.102        cl {
    620      1.102        cl 	struct vop_getattr_args /* {
    621      1.102        cl 		struct vnode *a_vp;
    622      1.102        cl 		struct vattr *a_vap;
    623      1.120      elad 		kauth_cred_t a_cred;
    624      1.102        cl 	} */ *ap = v;
    625      1.102        cl 	struct vattr *vap = ap->a_vap;
    626      1.102        cl 
    627      1.102        cl 	vap->va_nlink = 1;
    628      1.102        cl 	vap->va_bytes = vap->va_size = 0;
    629      1.102        cl 
    630      1.102        cl 	return 0;
    631      1.102        cl }
    632      1.102        cl 
    633       1.41  christos int
    634      1.136       dsl kernfs_getattr(void *v)
    635       1.41  christos {
    636       1.23   mycroft 	struct vop_getattr_args /* {
    637       1.23   mycroft 		struct vnode *a_vp;
    638       1.23   mycroft 		struct vattr *a_vap;
    639      1.120      elad 		kauth_cred_t a_cred;
    640       1.41  christos 	} */ *ap = v;
    641       1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    642      1.102        cl 	struct kernfs_subdir *ks;
    643       1.23   mycroft 	struct vattr *vap = ap->a_vap;
    644        1.1       cgd 	int error = 0;
    645      1.109  christos 	char strbuf[KSTRING], *bf;
    646       1.90    itojun 	size_t nread, total;
    647        1.1       cgd 
    648      1.139     pooka 	vattr_null(vap);
    649       1.90    itojun 	vap->va_type = ap->a_vp->v_type;
    650       1.17       cgd 	vap->va_uid = 0;
    651       1.17       cgd 	vap->va_gid = 0;
    652       1.90    itojun 	vap->va_mode = kfs->kfs_mode;
    653       1.90    itojun 	vap->va_fileid = kfs->kfs_fileno;
    654       1.90    itojun 	vap->va_flags = 0;
    655       1.23   mycroft 	vap->va_size = 0;
    656        1.1       cgd 	vap->va_blocksize = DEV_BSIZE;
    657      1.121    kardel 	/* Make all times be current TOD, except for the "boottime" node. */
    658      1.132      elad 	if (kfs->kfs_kt->kt_namlen == 8 &&
    659       1.92       dan 	    !memcmp(kfs->kfs_kt->kt_name, "boottime", 8)) {
    660      1.162   thorpej 		getnanoboottime(&vap->va_ctime);
    661       1.92       dan 	} else {
    662      1.121    kardel 		getnanotime(&vap->va_ctime);
    663       1.92       dan 	}
    664       1.81     lukem 	vap->va_atime = vap->va_mtime = vap->va_ctime;
    665        1.1       cgd 	vap->va_gen = 0;
    666        1.1       cgd 	vap->va_flags = 0;
    667        1.1       cgd 	vap->va_rdev = 0;
    668        1.1       cgd 	vap->va_bytes = 0;
    669        1.1       cgd 
    670       1.90    itojun 	switch (kfs->kfs_type) {
    671       1.98     darcy 	case KFSkern:
    672      1.102        cl 		vap->va_nlink = nkern_dirs;
    673       1.90    itojun 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    674       1.90    itojun 		break;
    675       1.90    itojun 
    676      1.151  christos 	case KFSdevice:
    677      1.151  christos 		vap->va_nlink = 1;
    678      1.151  christos 		vap->va_rdev = ap->a_vp->v_rdev;
    679      1.151  christos 		break;
    680      1.151  christos 
    681       1.98     darcy 	case KFSroot:
    682       1.90    itojun 		vap->va_nlink = 1;
    683       1.90    itojun 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    684       1.90    itojun 		break;
    685       1.90    itojun 
    686      1.102        cl 	case KFSsubdir:
    687      1.102        cl 		ks = (struct kernfs_subdir *)kfs->kfs_kt->kt_data;
    688      1.102        cl 		vap->va_nlink = ks->ks_dirs;
    689      1.102        cl 		vap->va_bytes = vap->va_size = DEV_BSIZE;
    690      1.102        cl 		break;
    691      1.102        cl 
    692       1.98     darcy 	case KFSnull:
    693       1.98     darcy 	case KFStime:
    694      1.162   thorpej 	case KFSboottime:
    695       1.98     darcy 	case KFSint:
    696       1.98     darcy 	case KFSstring:
    697       1.98     darcy 	case KFShostname:
    698       1.98     darcy 	case KFSavenrun:
    699       1.98     darcy 	case KFSmsgbuf:
    700        1.1       cgd 		vap->va_nlink = 1;
    701       1.84  jdolecek 		total = 0;
    702       1.84  jdolecek 		do {
    703      1.109  christos 			bf = strbuf;
    704      1.109  christos 			error = kernfs_xread(kfs, total, &bf,
    705       1.90    itojun 			    sizeof(strbuf), &nread);
    706       1.84  jdolecek 			total += nread;
    707       1.84  jdolecek 		} while (error == 0 && nread != 0);
    708       1.90    itojun 		vap->va_bytes = vap->va_size = total;
    709       1.90    itojun 		break;
    710       1.90    itojun 
    711       1.90    itojun 	default:
    712      1.102        cl 		error = kernfs_try_fileop(kfs->kfs_type,
    713      1.102        cl 		    KERNFS_FILEOP_GETATTR, v, EINVAL);
    714       1.90    itojun 		break;
    715        1.1       cgd 	}
    716        1.1       cgd 
    717        1.1       cgd 	return (error);
    718        1.1       cgd }
    719        1.1       cgd 
    720       1.41  christos /*ARGSUSED*/
    721       1.41  christos int
    722      1.128  christos kernfs_setattr(void *v)
    723        1.1       cgd {
    724       1.90    itojun 
    725        1.1       cgd 	/*
    726       1.17       cgd 	 * Silently ignore attribute changes.
    727       1.17       cgd 	 * This allows for open with truncate to have no
    728       1.17       cgd 	 * effect until some data is written.  I want to
    729       1.17       cgd 	 * do it this way because all writes are atomic.
    730        1.1       cgd 	 */
    731       1.17       cgd 	return (0);
    732        1.1       cgd }
    733        1.1       cgd 
    734       1.28   mycroft int
    735      1.136       dsl kernfs_default_xread(void *v)
    736       1.41  christos {
    737       1.23   mycroft 	struct vop_read_args /* {
    738       1.23   mycroft 		struct vnode *a_vp;
    739       1.23   mycroft 		struct uio *a_uio;
    740       1.23   mycroft 		int  a_ioflag;
    741      1.120      elad 		kauth_cred_t a_cred;
    742       1.41  christos 	} */ *ap = v;
    743       1.23   mycroft 	struct uio *uio = ap->a_uio;
    744       1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    745      1.109  christos 	char strbuf[KSTRING], *bf;
    746      1.114  christos 	int off;
    747       1.82  jdolecek 	size_t len;
    748       1.28   mycroft 	int error;
    749       1.23   mycroft 
    750       1.90    itojun 	if (ap->a_vp->v_type == VDIR)
    751      1.144     njoly 		return EISDIR;
    752       1.23   mycroft 
    753      1.114  christos 	off = (int)uio->uio_offset;
    754      1.112  christos 	/* Don't allow negative offsets */
    755      1.114  christos 	if (off < 0)
    756      1.112  christos 		return EINVAL;
    757      1.112  christos 
    758      1.109  christos 	bf = strbuf;
    759      1.109  christos 	if ((error = kernfs_xread(kfs, off, &bf, sizeof(strbuf), &len)) == 0)
    760      1.109  christos 		error = uiomove(bf, len, uio);
    761       1.82  jdolecek 	return (error);
    762        1.1       cgd }
    763        1.1       cgd 
    764      1.123    bouyer int
    765      1.136       dsl kernfs_read(void *v)
    766      1.123    bouyer {
    767      1.123    bouyer 	struct vop_read_args /* {
    768      1.123    bouyer 		struct vnode *a_vp;
    769      1.123    bouyer 		struct uio *a_uio;
    770      1.123    bouyer 		int  a_ioflag;
    771      1.123    bouyer 		struct ucred *a_cred;
    772      1.123    bouyer 	} */ *ap = v;
    773      1.123    bouyer 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    774      1.123    bouyer 
    775      1.124    bouyer 	if (kfs->kfs_type < KFSlasttype) {
    776      1.124    bouyer 		/* use default function */
    777      1.124    bouyer 		return kernfs_default_xread(v);
    778      1.124    bouyer 	}
    779      1.124    bouyer 	return kernfs_try_fileop(kfs->kfs_type, KERNFS_FILEOP_READ, v,
    780      1.124    bouyer 	   EOPNOTSUPP);
    781      1.123    bouyer }
    782      1.123    bouyer 
    783      1.102        cl static int
    784      1.136       dsl kernfs_default_xwrite(void *v)
    785       1.41  christos {
    786       1.23   mycroft 	struct vop_write_args /* {
    787       1.23   mycroft 		struct vnode *a_vp;
    788       1.23   mycroft 		struct uio *a_uio;
    789       1.23   mycroft 		int  a_ioflag;
    790      1.120      elad 		kauth_cred_t a_cred;
    791       1.41  christos 	} */ *ap = v;
    792       1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    793       1.23   mycroft 	struct uio *uio = ap->a_uio;
    794      1.127      jmmv 	int error;
    795      1.127      jmmv 	size_t xlen;
    796        1.1       cgd 	char strbuf[KSTRING];
    797       1.23   mycroft 
    798        1.1       cgd 	if (uio->uio_offset != 0)
    799        1.1       cgd 		return (EINVAL);
    800        1.1       cgd 
    801      1.160  riastrad 	xlen = uimin(uio->uio_resid, KSTRING-1);
    802       1.41  christos 	if ((error = uiomove(strbuf, xlen, uio)) != 0)
    803        1.1       cgd 		return (error);
    804        1.1       cgd 
    805        1.1       cgd 	if (uio->uio_resid != 0)
    806        1.1       cgd 		return (EIO);
    807        1.1       cgd 
    808        1.1       cgd 	strbuf[xlen] = '\0';
    809       1.17       cgd 	xlen = strlen(strbuf);
    810       1.90    itojun 	return (kernfs_xwrite(kfs, strbuf, xlen));
    811        1.1       cgd }
    812        1.1       cgd 
    813      1.102        cl int
    814      1.136       dsl kernfs_write(void *v)
    815      1.102        cl {
    816      1.102        cl 	struct vop_write_args /* {
    817      1.102        cl 		struct vnode *a_vp;
    818      1.102        cl 		struct uio *a_uio;
    819      1.102        cl 		int  a_ioflag;
    820      1.120      elad 		kauth_cred_t a_cred;
    821      1.102        cl 	} */ *ap = v;
    822      1.102        cl 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    823      1.102        cl 
    824      1.124    bouyer 	if (kfs->kfs_type < KFSlasttype) {
    825      1.124    bouyer 		/* use default function */
    826      1.124    bouyer 		return kernfs_default_xwrite(v);
    827      1.124    bouyer 	}
    828      1.124    bouyer 	return kernfs_try_fileop(kfs->kfs_type, KERNFS_FILEOP_WRITE, v,
    829      1.124    bouyer 	    EOPNOTSUPP);
    830      1.102        cl }
    831      1.102        cl 
    832      1.102        cl int
    833      1.136       dsl kernfs_ioctl(void *v)
    834      1.102        cl {
    835      1.102        cl 	struct vop_ioctl_args /* {
    836      1.102        cl 		const struct vnodeop_desc *a_desc;
    837      1.102        cl 		struct vnode *a_vp;
    838      1.102        cl 		u_long a_command;
    839      1.102        cl 		void *a_data;
    840      1.102        cl 		int a_fflag;
    841      1.120      elad 		kauth_cred_t a_cred;
    842      1.102        cl 	} */ *ap = v;
    843      1.102        cl 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    844      1.102        cl 
    845      1.102        cl 	return kernfs_try_fileop(kfs->kfs_type, KERNFS_FILEOP_IOCTL, v,
    846      1.102        cl 	    EPASSTHROUGH);
    847      1.102        cl }
    848      1.102        cl 
    849      1.101        cl static int
    850      1.101        cl kernfs_setdirentfileno_kt(struct dirent *d, const struct kern_target *kt,
    851      1.152   hannken     struct vop_readdir_args *ap)
    852      1.101        cl {
    853      1.101        cl 	struct kernfs_node *kfs;
    854      1.101        cl 	struct vnode *vp;
    855      1.101        cl 	int error;
    856      1.101        cl 
    857      1.153   hannken 	if ((error = vcache_get(ap->a_vp->v_mount, &kt, sizeof(kt), &vp)) != 0)
    858      1.101        cl 		return error;
    859      1.151  christos 	kfs = VTOKERN(vp);
    860      1.151  christos 	d->d_fileno = kfs->kfs_fileno;
    861      1.153   hannken 	vrele(vp);
    862      1.101        cl 	return 0;
    863      1.101        cl }
    864      1.101        cl 
    865      1.101        cl static int
    866      1.101        cl kernfs_setdirentfileno(struct dirent *d, off_t entry,
    867      1.101        cl     struct kernfs_node *thisdir_kfs, const struct kern_target *parent_kt,
    868      1.101        cl     const struct kern_target *kt, struct vop_readdir_args *ap)
    869      1.101        cl {
    870      1.101        cl 	const struct kern_target *ikt;
    871      1.101        cl 	int error;
    872      1.101        cl 
    873      1.101        cl 	switch (entry) {
    874      1.101        cl 	case 0:
    875      1.101        cl 		d->d_fileno = thisdir_kfs->kfs_fileno;
    876      1.101        cl 		return 0;
    877      1.101        cl 	case 1:
    878      1.101        cl 		ikt = parent_kt;
    879      1.101        cl 		break;
    880      1.101        cl 	default:
    881      1.101        cl 		ikt = kt;
    882      1.101        cl 		break;
    883      1.101        cl 	}
    884      1.101        cl 	if (ikt != thisdir_kfs->kfs_kt) {
    885      1.152   hannken 		if ((error = kernfs_setdirentfileno_kt(d, ikt, ap)) != 0)
    886      1.101        cl 			return error;
    887      1.101        cl 	} else
    888      1.101        cl 		d->d_fileno = thisdir_kfs->kfs_fileno;
    889      1.101        cl 	return 0;
    890      1.101        cl }
    891      1.101        cl 
    892       1.41  christos int
    893      1.136       dsl kernfs_readdir(void *v)
    894       1.41  christos {
    895       1.23   mycroft 	struct vop_readdir_args /* {
    896       1.23   mycroft 		struct vnode *a_vp;
    897       1.23   mycroft 		struct uio *a_uio;
    898      1.120      elad 		kauth_cred_t a_cred;
    899       1.26   mycroft 		int *a_eofflag;
    900       1.57      fvdl 		off_t **a_cookies;
    901       1.57      fvdl 		int a_*ncookies;
    902       1.41  christos 	} */ *ap = v;
    903       1.23   mycroft 	struct uio *uio = ap->a_uio;
    904       1.37   mycroft 	struct dirent d;
    905       1.90    itojun 	struct kernfs_node *kfs = VTOKERN(ap->a_vp);
    906       1.75  jdolecek 	const struct kern_target *kt;
    907      1.102        cl 	const struct dyn_kern_target *dkt = NULL;
    908      1.102        cl 	const struct kernfs_subdir *ks;
    909      1.102        cl 	off_t i, j;
    910        1.1       cgd 	int error;
    911       1.57      fvdl 	off_t *cookies = NULL;
    912       1.90    itojun 	int ncookies = 0, n;
    913       1.23   mycroft 
    914       1.37   mycroft 	if (uio->uio_resid < UIO_MX)
    915       1.37   mycroft 		return (EINVAL);
    916       1.38   mycroft 	if (uio->uio_offset < 0)
    917       1.37   mycroft 		return (EINVAL);
    918       1.26   mycroft 
    919        1.1       cgd 	error = 0;
    920       1.38   mycroft 	i = uio->uio_offset;
    921       1.90    itojun 	memset(&d, 0, sizeof(d));
    922       1.90    itojun 	d.d_reclen = UIO_MX;
    923       1.90    itojun 	ncookies = uio->uio_resid / UIO_MX;
    924       1.90    itojun 
    925       1.90    itojun 	switch (kfs->kfs_type) {
    926       1.98     darcy 	case KFSkern:
    927       1.90    itojun 		if (i >= nkern_targets)
    928       1.90    itojun 			return (0);
    929       1.66  sommerfe 
    930       1.90    itojun 		if (ap->a_ncookies) {
    931      1.160  riastrad 			ncookies = uimin(ncookies, (nkern_targets - i));
    932       1.90    itojun 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
    933       1.90    itojun 			    M_WAITOK);
    934       1.90    itojun 			*ap->a_cookies = cookies;
    935       1.90    itojun 		}
    936       1.90    itojun 
    937       1.90    itojun 		n = 0;
    938       1.90    itojun 		for (; i < nkern_targets && uio->uio_resid >= UIO_MX; i++) {
    939      1.102        cl 			if (i < static_nkern_targets)
    940      1.102        cl 				kt = &kern_targets[i];
    941      1.102        cl 			else {
    942      1.102        cl 				if (dkt == NULL) {
    943      1.102        cl 					dkt = SIMPLEQ_FIRST(&dyn_kern_targets);
    944      1.102        cl 					for (j = static_nkern_targets; j < i &&
    945      1.102        cl 						     dkt != NULL; j++)
    946      1.102        cl 						dkt = SIMPLEQ_NEXT(dkt, dkt_queue);
    947      1.102        cl 					if (j != i)
    948      1.102        cl 						break;
    949      1.102        cl 				} else {
    950      1.102        cl 					dkt = SIMPLEQ_NEXT(dkt, dkt_queue);
    951      1.102        cl 				}
    952      1.119  christos 				if (dkt == NULL)
    953      1.119  christos 					break;
    954      1.102        cl 				kt = &dkt->dkt_kt;
    955      1.102        cl 			}
    956      1.141     pooka 			if (kt->kt_tag == KFSmsgbuf) {
    957      1.159  christos 				if (!logenabled(msgbufp)) {
    958      1.141     pooka 					continue;
    959      1.141     pooka 				}
    960      1.141     pooka 			}
    961       1.90    itojun 			d.d_namlen = kt->kt_namlen;
    962      1.101        cl 			if ((error = kernfs_setdirentfileno(&d, i, kfs,
    963      1.101        cl 			    &kern_targets[0], kt, ap)) != 0)
    964      1.101        cl 				break;
    965       1.90    itojun 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
    966       1.90    itojun 			d.d_type = kt->kt_type;
    967       1.99       jrf 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
    968       1.90    itojun 				break;
    969       1.90    itojun 			if (cookies)
    970       1.90    itojun 				*cookies++ = i + 1;
    971       1.90    itojun 			n++;
    972       1.90    itojun 		}
    973       1.90    itojun 		ncookies = n;
    974       1.90    itojun 		break;
    975       1.90    itojun 
    976       1.98     darcy 	case KFSroot:
    977       1.90    itojun 		if (i >= 2)
    978       1.90    itojun 			return 0;
    979       1.90    itojun 
    980       1.90    itojun 		if (ap->a_ncookies) {
    981      1.160  riastrad 			ncookies = uimin(ncookies, (2 - i));
    982       1.90    itojun 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
    983       1.90    itojun 			    M_WAITOK);
    984       1.90    itojun 			*ap->a_cookies = cookies;
    985       1.90    itojun 		}
    986       1.90    itojun 
    987       1.90    itojun 		n = 0;
    988       1.90    itojun 		for (; i < 2 && uio->uio_resid >= UIO_MX; i++) {
    989       1.90    itojun 			kt = &kern_targets[i];
    990       1.90    itojun 			d.d_namlen = kt->kt_namlen;
    991       1.90    itojun 			d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
    992       1.90    itojun 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
    993       1.90    itojun 			d.d_type = kt->kt_type;
    994       1.99       jrf 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
    995       1.90    itojun 				break;
    996       1.90    itojun 			if (cookies)
    997       1.90    itojun 				*cookies++ = i + 1;
    998       1.90    itojun 			n++;
    999       1.90    itojun 		}
   1000       1.90    itojun 		ncookies = n;
   1001       1.90    itojun 		break;
   1002       1.90    itojun 
   1003      1.102        cl 	case KFSsubdir:
   1004      1.102        cl 		ks = (struct kernfs_subdir *)kfs->kfs_kt->kt_data;
   1005      1.102        cl 		if (i >= ks->ks_nentries)
   1006      1.102        cl 			return (0);
   1007      1.102        cl 
   1008      1.102        cl 		if (ap->a_ncookies) {
   1009      1.160  riastrad 			ncookies = uimin(ncookies, (ks->ks_nentries - i));
   1010      1.102        cl 			cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
   1011      1.102        cl 			    M_WAITOK);
   1012      1.102        cl 			*ap->a_cookies = cookies;
   1013      1.102        cl 		}
   1014      1.102        cl 
   1015      1.102        cl 		dkt = SIMPLEQ_FIRST(&ks->ks_entries);
   1016      1.102        cl 		for (j = 0; j < i && dkt != NULL; j++)
   1017      1.102        cl 			dkt = SIMPLEQ_NEXT(dkt, dkt_queue);
   1018      1.102        cl 		n = 0;
   1019      1.102        cl 		for (; i < ks->ks_nentries && uio->uio_resid >= UIO_MX; i++) {
   1020      1.102        cl 			if (i < 2)
   1021      1.102        cl 				kt = &subdir_targets[i];
   1022      1.102        cl 			else {
   1023      1.102        cl 				/* check if ks_nentries lied to us */
   1024      1.102        cl 				if (dkt == NULL)
   1025      1.102        cl 					break;
   1026      1.102        cl 				kt = &dkt->dkt_kt;
   1027      1.102        cl 				dkt = SIMPLEQ_NEXT(dkt, dkt_queue);
   1028      1.102        cl 			}
   1029      1.102        cl 			d.d_namlen = kt->kt_namlen;
   1030      1.102        cl 			if ((error = kernfs_setdirentfileno(&d, i, kfs,
   1031      1.102        cl 			    ks->ks_parent, kt, ap)) != 0)
   1032      1.102        cl 				break;
   1033      1.102        cl 			memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
   1034      1.102        cl 			d.d_type = kt->kt_type;
   1035      1.103       jrf 			if ((error = uiomove(&d, UIO_MX, uio)) != 0)
   1036      1.102        cl 				break;
   1037      1.102        cl 			if (cookies)
   1038      1.102        cl 				*cookies++ = i + 1;
   1039      1.102        cl 			n++;
   1040      1.102        cl 		}
   1041      1.102        cl 		ncookies = n;
   1042      1.102        cl 		break;
   1043      1.102        cl 
   1044       1.90    itojun 	default:
   1045       1.90    itojun 		error = ENOTDIR;
   1046       1.90    itojun 		break;
   1047       1.57      fvdl 	}
   1048       1.57      fvdl 
   1049       1.57      fvdl 	if (ap->a_ncookies) {
   1050       1.57      fvdl 		if (error) {
   1051       1.90    itojun 			if (cookies)
   1052       1.90    itojun 				free(*ap->a_cookies, M_TEMP);
   1053       1.57      fvdl 			*ap->a_ncookies = 0;
   1054       1.57      fvdl 			*ap->a_cookies = NULL;
   1055       1.57      fvdl 		} else
   1056       1.57      fvdl 			*ap->a_ncookies = ncookies;
   1057        1.1       cgd 	}
   1058        1.1       cgd 
   1059       1.38   mycroft 	uio->uio_offset = i;
   1060        1.1       cgd 	return (error);
   1061        1.1       cgd }
   1062        1.1       cgd 
   1063       1.41  christos int
   1064      1.136       dsl kernfs_inactive(void *v)
   1065       1.41  christos {
   1066      1.157  riastrad 	struct vop_inactive_v2_args /* {
   1067       1.23   mycroft 		struct vnode *a_vp;
   1068      1.134        ad 		bool *a_recycle;
   1069       1.41  christos 	} */ *ap = v;
   1070       1.23   mycroft 
   1071      1.134        ad 	*ap->a_recycle = false;
   1072      1.157  riastrad 
   1073       1.23   mycroft 	return (0);
   1074       1.23   mycroft }
   1075       1.23   mycroft 
   1076       1.41  christos int
   1077      1.136       dsl kernfs_reclaim(void *v)
   1078       1.41  christos {
   1079      1.158  riastrad 	struct vop_reclaim_v2_args /* {
   1080       1.23   mycroft 		struct vnode *a_vp;
   1081       1.41  christos 	} */ *ap = v;
   1082      1.153   hannken 	struct vnode *vp = ap->a_vp;
   1083      1.153   hannken 	struct kernfs_node *kfs = VTOKERN(vp);
   1084      1.153   hannken 
   1085      1.158  riastrad 	VOP_UNLOCK(vp);
   1086      1.158  riastrad 
   1087      1.153   hannken 	vp->v_data = NULL;
   1088      1.153   hannken 	mutex_enter(&kfs_lock);
   1089      1.153   hannken 	TAILQ_REMOVE(&VFSTOKERNFS(vp->v_mount)->nodelist, kfs, kfs_list);
   1090      1.153   hannken 	mutex_exit(&kfs_lock);
   1091      1.153   hannken 	kmem_free(kfs, sizeof(struct kernfs_node));
   1092       1.23   mycroft 
   1093      1.153   hannken 	return 0;
   1094        1.1       cgd }
   1095        1.1       cgd 
   1096        1.1       cgd /*
   1097       1.23   mycroft  * Return POSIX pathconf information applicable to special devices.
   1098       1.23   mycroft  */
   1099       1.41  christos int
   1100      1.136       dsl kernfs_pathconf(void *v)
   1101       1.41  christos {
   1102       1.23   mycroft 	struct vop_pathconf_args /* {
   1103       1.23   mycroft 		struct vnode *a_vp;
   1104       1.23   mycroft 		int a_name;
   1105       1.29       cgd 		register_t *a_retval;
   1106       1.41  christos 	} */ *ap = v;
   1107       1.23   mycroft 
   1108       1.23   mycroft 	switch (ap->a_name) {
   1109       1.23   mycroft 	case _PC_LINK_MAX:
   1110       1.23   mycroft 		*ap->a_retval = LINK_MAX;
   1111       1.23   mycroft 		return (0);
   1112       1.23   mycroft 	case _PC_MAX_CANON:
   1113       1.23   mycroft 		*ap->a_retval = MAX_CANON;
   1114       1.23   mycroft 		return (0);
   1115       1.23   mycroft 	case _PC_MAX_INPUT:
   1116       1.23   mycroft 		*ap->a_retval = MAX_INPUT;
   1117       1.23   mycroft 		return (0);
   1118       1.23   mycroft 	case _PC_PIPE_BUF:
   1119       1.23   mycroft 		*ap->a_retval = PIPE_BUF;
   1120       1.23   mycroft 		return (0);
   1121       1.23   mycroft 	case _PC_CHOWN_RESTRICTED:
   1122       1.23   mycroft 		*ap->a_retval = 1;
   1123       1.23   mycroft 		return (0);
   1124       1.23   mycroft 	case _PC_VDISABLE:
   1125       1.23   mycroft 		*ap->a_retval = _POSIX_VDISABLE;
   1126       1.59    kleink 		return (0);
   1127       1.59    kleink 	case _PC_SYNC_IO:
   1128       1.59    kleink 		*ap->a_retval = 1;
   1129       1.23   mycroft 		return (0);
   1130       1.23   mycroft 	default:
   1131      1.166  christos 		return genfs_pathconf(ap);
   1132       1.23   mycroft 	}
   1133       1.23   mycroft 	/* NOTREACHED */
   1134       1.23   mycroft }
   1135       1.23   mycroft 
   1136       1.23   mycroft /*
   1137       1.23   mycroft  * Print out the contents of a /dev/fd vnode.
   1138        1.1       cgd  */
   1139        1.1       cgd /* ARGSUSED */
   1140       1.41  christos int
   1141      1.128  christos kernfs_print(void *v)
   1142       1.23   mycroft {
   1143       1.23   mycroft 
   1144       1.47  christos 	printf("tag VT_KERNFS, kernfs vnode\n");
   1145       1.23   mycroft 	return (0);
   1146       1.23   mycroft }
   1147       1.23   mycroft 
   1148       1.40   mycroft int
   1149      1.136       dsl kernfs_link(void *v)
   1150       1.41  christos {
   1151      1.155  riastrad 	struct vop_link_v2_args /* {
   1152       1.40   mycroft 		struct vnode *a_dvp;
   1153      1.107     perry 		struct vnode *a_vp;
   1154       1.40   mycroft 		struct componentname *a_cnp;
   1155       1.41  christos 	} */ *ap = v;
   1156      1.107     perry 
   1157       1.40   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1158       1.40   mycroft 	return (EROFS);
   1159       1.40   mycroft }
   1160       1.40   mycroft 
   1161       1.40   mycroft int
   1162      1.136       dsl kernfs_symlink(void *v)
   1163       1.41  christos {
   1164      1.149   hannken 	struct vop_symlink_v3_args /* {
   1165       1.40   mycroft 		struct vnode *a_dvp;
   1166       1.40   mycroft 		struct vnode **a_vpp;
   1167       1.40   mycroft 		struct componentname *a_cnp;
   1168       1.40   mycroft 		struct vattr *a_vap;
   1169       1.40   mycroft 		char *a_target;
   1170       1.41  christos 	} */ *ap = v;
   1171      1.107     perry 
   1172       1.40   mycroft 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
   1173       1.40   mycroft 	return (EROFS);
   1174        1.1       cgd }
   1175      1.161   hannken 
   1176      1.161   hannken int
   1177      1.161   hannken kernfs_getpages(void *v)
   1178      1.161   hannken {
   1179      1.161   hannken 	struct vop_getpages_args /* {
   1180      1.161   hannken 		struct vnode *a_vp;
   1181      1.161   hannken 		voff_t a_offset;
   1182      1.161   hannken 		struct vm_page **a_m;
   1183      1.161   hannken 		int *a_count;
   1184      1.161   hannken 		int a_centeridx;
   1185      1.161   hannken 		vm_prot_t a_access_type;
   1186      1.161   hannken 		int a_advice;
   1187      1.161   hannken 		int a_flags;
   1188      1.161   hannken 	} */ *ap = v;
   1189      1.161   hannken 
   1190      1.161   hannken 	if ((ap->a_flags & PGO_LOCKED) == 0)
   1191      1.164        ad 		rw_exit(ap->a_vp->v_uobj.vmobjlock);
   1192      1.161   hannken 
   1193      1.161   hannken 	return (EFAULT);
   1194      1.161   hannken }
   1195