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