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