Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files. =================================================================== RCS file: /ftp/cvs/cvsroot/src/sys/miscfs/kernfs/kernfs_vnops.c,v rcsdiff: /ftp/cvs/cvsroot/src/sys/miscfs/kernfs/kernfs_vnops.c,v: warning: Unknown phrases like `commitid ...;' are present. retrieving revision 1.43 retrieving revision 1.90 diff -u -p -r1.43 -r1.90 --- src/sys/miscfs/kernfs/kernfs_vnops.c 1996/03/16 23:52:47 1.43 +++ src/sys/miscfs/kernfs/kernfs_vnops.c 2003/09/08 06:51:54 1.90 @@ -1,4 +1,4 @@ -/* $NetBSD: kernfs_vnops.c,v 1.43 1996/03/16 23:52:47 christos Exp $ */ +/* $NetBSD: kernfs_vnops.c,v 1.90 2003/09/08 06:51:54 itojun Exp $ */ /* * Copyright (c) 1992, 1993 @@ -15,11 +15,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -35,18 +31,24 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * @(#)kernfs_vnops.c 8.9 (Berkeley) 6/15/94 + * @(#)kernfs_vnops.c 8.15 (Berkeley) 5/21/95 */ /* * Kernel parameter filesystem (/kern) */ +#include +__KERNEL_RCSID(0, "$NetBSD: kernfs_vnops.c,v 1.90 2003/09/08 06:51:54 itojun Exp $"); + +#ifdef _KERNEL_OPT +#include "opt_ipsec.h" +#endif + #include #include #include #include -#include #include #include #include @@ -58,241 +60,350 @@ #include #include #include + +#include #include +#ifdef IPSEC +#include +#include +#include +#include +#include +#endif + +#include + #define KSTRING 256 /* Largest I/O available via this filesystem */ #define UIO_MX 32 #define READ_MODE (S_IRUSR|S_IRGRP|S_IROTH) #define WRITE_MODE (S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH) #define DIR_MODE (S_IRUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) +#define UDIR_MODE (S_IRUSR|S_IXUSR) -struct kern_target { - u_char kt_type; - u_char kt_namlen; - char *kt_name; - void *kt_data; -#define KTT_NULL 1 -#define KTT_TIME 5 -#define KTT_INT 17 -#define KTT_STRING 31 -#define KTT_HOSTNAME 47 -#define KTT_AVENRUN 53 -#define KTT_DEVICE 71 -#define KTT_MSGBUF 89 - u_char kt_tag; - u_char kt_vtype; - mode_t kt_mode; -} kern_targets[] = { -/* NOTE: The name must be less than UIO_MX-16 chars in length */ #define N(s) sizeof(s)-1, s +const struct kern_target kern_targets[] = { +/* NOTE: The name must be less than UIO_MX-16 chars in length */ /* name data tag type ro/rw */ - { DT_DIR, N("."), 0, KTT_NULL, VDIR, DIR_MODE }, - { DT_DIR, N(".."), 0, KTT_NULL, VDIR, DIR_MODE }, - { DT_REG, N("boottime"), &boottime.tv_sec, KTT_INT, VREG, READ_MODE }, - { DT_REG, N("copyright"), copyright, KTT_STRING, VREG, READ_MODE }, - { DT_REG, N("hostname"), 0, KTT_HOSTNAME, VREG, WRITE_MODE }, - { DT_REG, N("hz"), &hz, KTT_INT, VREG, READ_MODE }, - { DT_REG, N("loadavg"), 0, KTT_AVENRUN, VREG, READ_MODE }, - { DT_REG, N("msgbuf"), 0, KTT_MSGBUF, VREG, READ_MODE }, - { DT_REG, N("pagesize"), &cnt.v_page_size, KTT_INT, VREG, READ_MODE }, - { DT_REG, N("physmem"), &physmem, KTT_INT, VREG, READ_MODE }, + { DT_DIR, N("."), 0, Pkern, VDIR, DIR_MODE }, + { DT_DIR, N(".."), 0, Proot, VDIR, DIR_MODE }, + { DT_REG, N("boottime"), &boottime.tv_sec, Pint, VREG, READ_MODE }, + /* XXX cast away const */ + { DT_REG, N("copyright"), (void *)copyright, + Pstring, VREG, READ_MODE }, + { DT_REG, N("hostname"), 0, Phostname, VREG, WRITE_MODE }, + { DT_REG, N("hz"), &hz, Pint, VREG, READ_MODE }, +#ifdef IPSEC + { DT_DIR, N("ipsecsa"), 0, Pipsecsadir, VDIR, UDIR_MODE }, + { DT_DIR, N("ipsecsp"), 0, Pipsecspdir, VDIR, UDIR_MODE }, +#endif + { DT_REG, N("loadavg"), 0, Pavenrun, VREG, READ_MODE }, + { DT_REG, N("msgbuf"), 0, Pmsgbuf, VREG, READ_MODE }, + { DT_REG, N("pagesize"), &uvmexp.pagesize, Pint, VREG, READ_MODE }, + { DT_REG, N("physmem"), &physmem, Pint, VREG, READ_MODE }, #if 0 - { DT_DIR, N("root"), 0, KTT_NULL, VDIR, DIR_MODE }, + { DT_DIR, N("root"), 0, Pnull, VDIR, DIR_MODE }, #endif - { DT_BLK, N("rootdev"), &rootdev, KTT_DEVICE, VBLK, READ_MODE }, - { DT_CHR, N("rrootdev"), &rrootdev, KTT_DEVICE, VCHR, READ_MODE }, - { DT_REG, N("time"), 0, KTT_TIME, VREG, READ_MODE }, - { DT_REG, N("version"), version, KTT_STRING, VREG, READ_MODE }, -#undef N + { DT_BLK, N("rootdev"), &rootdev, Pdevice, VBLK, READ_MODE }, + { DT_CHR, N("rrootdev"), &rrootdev, Pdevice, VCHR, READ_MODE }, + { DT_REG, N("time"), 0, Ptime, VREG, READ_MODE }, + /* XXX cast away const */ + { DT_REG, N("version"), (void *)version, + Pstring, VREG, READ_MODE }, +}; +#ifdef IPSEC +const struct kern_target ipsecsa_targets[] = { +/* NOTE: The name must be less than UIO_MX-16 chars in length */ + /* name data tag type ro/rw */ + { DT_DIR, N("."), 0, Pipsecsadir, VDIR, DIR_MODE }, + { DT_DIR, N(".."), 0, Pkern, VDIR, DIR_MODE }, +}; +const struct kern_target ipsecsp_targets[] = { +/* NOTE: The name must be less than UIO_MX-16 chars in length */ + /* name data tag type ro/rw */ + { DT_DIR, N("."), 0, Pipsecspdir, VDIR, DIR_MODE }, + { DT_DIR, N(".."), 0, Pkern, VDIR, DIR_MODE }, }; -static int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]); +#endif +#undef N +int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]); +#ifdef IPSEC +int nipsecsa_targets = sizeof(ipsecsa_targets) / sizeof(ipsecsa_targets[0]); +int nipsecsp_targets = sizeof(ipsecsp_targets) / sizeof(ipsecsp_targets[0]); +#endif -int kernfs_badop __P((void *)); -int kernfs_enotsupp __P((void *)); int kernfs_lookup __P((void *)); -#define kernfs_create kernfs_enotsupp -#define kernfs_mknod kernfs_enotsupp +#define kernfs_create genfs_eopnotsupp +#define kernfs_mknod genfs_eopnotsupp int kernfs_open __P((void *)); -#define kernfs_close nullop +int kernfs_close __P((void *)); int kernfs_access __P((void *)); int kernfs_getattr __P((void *)); int kernfs_setattr __P((void *)); int kernfs_read __P((void *)); int kernfs_write __P((void *)); -#define kernfs_ioctl kernfs_enotsupp -#define kernfs_select kernfs_enotsupp -#define kernfs_mmap kernfs_enotsupp -#define kernfs_fsync nullop -#define kernfs_seek nullop -#define kernfs_remove kernfs_enotsupp +#define kernfs_fcntl genfs_fcntl +#define kernfs_ioctl genfs_enoioctl +#define kernfs_poll genfs_poll +#define kernfs_revoke genfs_revoke +#define kernfs_fsync genfs_nullop +#define kernfs_seek genfs_nullop +#define kernfs_remove genfs_eopnotsupp int kernfs_link __P((void *)); -#define kernfs_rename kernfs_enotsupp -#define kernfs_mkdir kernfs_enotsupp -#define kernfs_rmdir kernfs_enotsupp +#define kernfs_rename genfs_eopnotsupp +#define kernfs_mkdir genfs_eopnotsupp +#define kernfs_rmdir genfs_eopnotsupp int kernfs_symlink __P((void *)); int kernfs_readdir __P((void *)); -#define kernfs_readlink kernfs_enotsupp -int kernfs_abortop __P((void *)); +#define kernfs_readlink genfs_eopnotsupp +#define kernfs_abortop genfs_abortop int kernfs_inactive __P((void *)); int kernfs_reclaim __P((void *)); -#define kernfs_lock nullop -#define kernfs_unlock nullop -#define kernfs_bmap kernfs_badop -#define kernfs_strategy kernfs_badop +#define kernfs_lock genfs_lock +#define kernfs_unlock genfs_unlock +#define kernfs_bmap genfs_badop +#define kernfs_strategy genfs_badop int kernfs_print __P((void *)); -#define kernfs_islocked nullop +#define kernfs_islocked genfs_islocked int kernfs_pathconf __P((void *)); -#define kernfs_advlock kernfs_enotsupp -#define kernfs_blkatoff kernfs_enotsupp -#define kernfs_valloc kernfs_enotsupp -int kernfs_vfree __P((void *)); -#define kernfs_truncate kernfs_enotsupp -#define kernfs_update kernfs_enotsupp -#define kernfs_bwrite kernfs_enotsupp +#define kernfs_advlock genfs_einval +#define kernfs_blkatoff genfs_eopnotsupp +#define kernfs_valloc genfs_eopnotsupp +#define kernfs_vfree genfs_nullop +#define kernfs_truncate genfs_eopnotsupp +#define kernfs_update genfs_nullop +#define kernfs_bwrite genfs_eopnotsupp +#define kernfs_putpages genfs_putpages -int kernfs_xread __P((struct kern_target *, int, char **, int)); -int kernfs_xwrite __P((struct kern_target *, char *, int)); +static int kernfs_xread __P((struct kernfs_node *, int, char **, size_t, size_t *)); +static int kernfs_xwrite __P((const struct kernfs_node *, char *, size_t)); int (**kernfs_vnodeop_p) __P((void *)); -struct vnodeopv_entry_desc kernfs_vnodeop_entries[] = { +const struct vnodeopv_entry_desc kernfs_vnodeop_entries[] = { { &vop_default_desc, vn_default_error }, - { &vop_lookup_desc, kernfs_lookup }, /* lookup */ - { &vop_create_desc, kernfs_create }, /* create */ - { &vop_mknod_desc, kernfs_mknod }, /* mknod */ - { &vop_open_desc, kernfs_open }, /* open */ - { &vop_close_desc, kernfs_close }, /* close */ - { &vop_access_desc, kernfs_access }, /* access */ - { &vop_getattr_desc, kernfs_getattr }, /* getattr */ - { &vop_setattr_desc, kernfs_setattr }, /* setattr */ - { &vop_read_desc, kernfs_read }, /* read */ - { &vop_write_desc, kernfs_write }, /* write */ - { &vop_ioctl_desc, kernfs_ioctl }, /* ioctl */ - { &vop_select_desc, kernfs_select }, /* select */ - { &vop_mmap_desc, kernfs_mmap }, /* mmap */ - { &vop_fsync_desc, kernfs_fsync }, /* fsync */ - { &vop_seek_desc, kernfs_seek }, /* seek */ - { &vop_remove_desc, kernfs_remove }, /* remove */ - { &vop_link_desc, kernfs_link }, /* link */ - { &vop_rename_desc, kernfs_rename }, /* rename */ - { &vop_mkdir_desc, kernfs_mkdir }, /* mkdir */ - { &vop_rmdir_desc, kernfs_rmdir }, /* rmdir */ - { &vop_symlink_desc, kernfs_symlink }, /* symlink */ - { &vop_readdir_desc, kernfs_readdir }, /* readdir */ - { &vop_readlink_desc, kernfs_readlink },/* readlink */ - { &vop_abortop_desc, kernfs_abortop }, /* abortop */ - { &vop_inactive_desc, kernfs_inactive },/* inactive */ - { &vop_reclaim_desc, kernfs_reclaim }, /* reclaim */ - { &vop_lock_desc, kernfs_lock }, /* lock */ - { &vop_unlock_desc, kernfs_unlock }, /* unlock */ - { &vop_bmap_desc, kernfs_bmap }, /* bmap */ - { &vop_strategy_desc, kernfs_strategy },/* strategy */ - { &vop_print_desc, kernfs_print }, /* print */ - { &vop_islocked_desc, kernfs_islocked },/* islocked */ - { &vop_pathconf_desc, kernfs_pathconf },/* pathconf */ - { &vop_advlock_desc, kernfs_advlock }, /* advlock */ - { &vop_blkatoff_desc, kernfs_blkatoff },/* blkatoff */ - { &vop_valloc_desc, kernfs_valloc }, /* valloc */ - { &vop_vfree_desc, kernfs_vfree }, /* vfree */ - { &vop_truncate_desc, kernfs_truncate },/* truncate */ - { &vop_update_desc, kernfs_update }, /* update */ - { &vop_bwrite_desc, kernfs_bwrite }, /* bwrite */ - { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL } + { &vop_lookup_desc, kernfs_lookup }, /* lookup */ + { &vop_create_desc, kernfs_create }, /* create */ + { &vop_mknod_desc, kernfs_mknod }, /* mknod */ + { &vop_open_desc, kernfs_open }, /* open */ + { &vop_close_desc, kernfs_close }, /* close */ + { &vop_access_desc, kernfs_access }, /* access */ + { &vop_getattr_desc, kernfs_getattr }, /* getattr */ + { &vop_setattr_desc, kernfs_setattr }, /* setattr */ + { &vop_read_desc, kernfs_read }, /* read */ + { &vop_write_desc, kernfs_write }, /* write */ + { &vop_fcntl_desc, kernfs_fcntl }, /* fcntl */ + { &vop_ioctl_desc, kernfs_ioctl }, /* ioctl */ + { &vop_poll_desc, kernfs_poll }, /* poll */ + { &vop_revoke_desc, kernfs_revoke }, /* revoke */ + { &vop_fsync_desc, kernfs_fsync }, /* fsync */ + { &vop_seek_desc, kernfs_seek }, /* seek */ + { &vop_remove_desc, kernfs_remove }, /* remove */ + { &vop_link_desc, kernfs_link }, /* link */ + { &vop_rename_desc, kernfs_rename }, /* rename */ + { &vop_mkdir_desc, kernfs_mkdir }, /* mkdir */ + { &vop_rmdir_desc, kernfs_rmdir }, /* rmdir */ + { &vop_symlink_desc, kernfs_symlink }, /* symlink */ + { &vop_readdir_desc, kernfs_readdir }, /* readdir */ + { &vop_readlink_desc, kernfs_readlink }, /* readlink */ + { &vop_abortop_desc, kernfs_abortop }, /* abortop */ + { &vop_inactive_desc, kernfs_inactive }, /* inactive */ + { &vop_reclaim_desc, kernfs_reclaim }, /* reclaim */ + { &vop_lock_desc, kernfs_lock }, /* lock */ + { &vop_unlock_desc, kernfs_unlock }, /* unlock */ + { &vop_bmap_desc, kernfs_bmap }, /* bmap */ + { &vop_strategy_desc, kernfs_strategy }, /* strategy */ + { &vop_print_desc, kernfs_print }, /* print */ + { &vop_islocked_desc, kernfs_islocked }, /* islocked */ + { &vop_pathconf_desc, kernfs_pathconf }, /* pathconf */ + { &vop_advlock_desc, kernfs_advlock }, /* advlock */ + { &vop_blkatoff_desc, kernfs_blkatoff }, /* blkatoff */ + { &vop_valloc_desc, kernfs_valloc }, /* valloc */ + { &vop_vfree_desc, kernfs_vfree }, /* vfree */ + { &vop_truncate_desc, kernfs_truncate }, /* truncate */ + { &vop_update_desc, kernfs_update }, /* update */ + { &vop_bwrite_desc, kernfs_bwrite }, /* bwrite */ + { &vop_putpages_desc, kernfs_putpages }, /* putpages */ + { NULL, NULL } }; -struct vnodeopv_desc kernfs_vnodeop_opv_desc = +const struct vnodeopv_desc kernfs_vnodeop_opv_desc = { &kernfs_vnodeop_p, kernfs_vnodeop_entries }; -int -kernfs_xread(kt, off, bufp, len) - struct kern_target *kt; +static int +kernfs_xread(kfs, off, bufp, len, wrlen) + struct kernfs_node *kfs; int off; char **bufp; - int len; + size_t len; + size_t *wrlen; { + const struct kern_target *kt; +#ifdef IPSEC + struct mbuf *m; +#endif + + kt = kfs->kfs_kt; - switch (kt->kt_tag) { - case KTT_TIME: { + switch (kfs->kfs_type) { + case Ptime: { struct timeval tv; microtime(&tv); - sprintf(*bufp, "%ld %ld\n", tv.tv_sec, tv.tv_usec); + snprintf(*bufp, len, "%ld %ld\n", tv.tv_sec, tv.tv_usec); break; } - case KTT_INT: { + case Pint: { int *ip = kt->kt_data; - sprintf(*bufp, "%d\n", *ip); + snprintf(*bufp, len, "%d\n", *ip); break; } - case KTT_STRING: { + case Pstring: { char *cp = kt->kt_data; *bufp = cp; break; } - case KTT_MSGBUF: { - extern struct msgbuf *msgbufp; + case Pmsgbuf: { long n; - if (off >= MSG_BSIZE) + /* + * deal with cases where the message buffer has + * become corrupted. + */ + if (!msgbufenabled || msgbufp->msg_magic != MSG_MAGIC) { + msgbufenabled = 0; + return (ENXIO); + } + + /* + * Note that reads of /kern/msgbuf won't necessarily yield + * consistent results, if the message buffer is modified + * while the read is in progress. The worst that can happen + * is that incorrect data will be read. There's no way + * that this can crash the system unless the values in the + * message buffer header are corrupted, but that'll cause + * the system to die anyway. + */ + if (off >= msgbufp->msg_bufs) { + *wrlen = 0; return (0); + } n = msgbufp->msg_bufx + off; - if (n >= MSG_BSIZE) - n -= MSG_BSIZE; - len = min(MSG_BSIZE - n, MSG_BSIZE - off); + if (n >= msgbufp->msg_bufs) + n -= msgbufp->msg_bufs; + len = min(msgbufp->msg_bufs - n, msgbufp->msg_bufs - off); *bufp = msgbufp->msg_bufc + n; - return (len); + *wrlen = len; + return (0); } - case KTT_HOSTNAME: { + case Phostname: { char *cp = hostname; int xlen = hostnamelen; - if (xlen >= (len-2)) + if (xlen >= (len - 2)) return (EINVAL); - bcopy(cp, *bufp, xlen); + memcpy(*bufp, cp, xlen); (*bufp)[xlen] = '\n'; (*bufp)[xlen+1] = '\0'; + len = strlen(*bufp); break; } - case KTT_AVENRUN: + case Pavenrun: averunnable.fscale = FSCALE; - sprintf(*bufp, "%d %d %d %ld\n", + snprintf(*bufp, len, "%d %d %d %ld\n", averunnable.ldavg[0], averunnable.ldavg[1], averunnable.ldavg[2], averunnable.fscale); break; +#ifdef IPSEC + case Pipsecsa: + /* + * Note that SA configuration could be changed during the + * read operation, resulting in garbled output. + */ + m = key_setdumpsa_spi(htonl(kfs->kfs_value)); + if (!m) + return (ENOBUFS); + if (off >= m->m_pkthdr.len) { + *wrlen = 0; + m_freem(m); + return (0); + } + if (len > m->m_pkthdr.len - off) + len = m->m_pkthdr.len - off; + m_copydata(m, off, len, *bufp); + *wrlen = len; + m_freem(m); + return (0); + + case Pipsecsp: + /* + * Note that SP configuration could be changed during the + * read operation, resulting in garbled output. + */ + if (!kfs->kfs_v) { + struct secpolicy *sp; + + sp = key_getspbyid(kfs->kfs_value); + if (sp) + kfs->kfs_v = sp; + else + return (ENOENT); + } + m = key_setdumpsp((struct secpolicy *)kfs->kfs_v, + SADB_X_SPDGET, 0, 0); + if (!m) + return (ENOBUFS); + if (off >= m->m_pkthdr.len) { + *wrlen = 0; + m_freem(m); + return (0); + } + if (len > m->m_pkthdr.len - off) + len = m->m_pkthdr.len - off; + m_copydata(m, off, len, *bufp); + *wrlen = len; + m_freem(m); + return (0); +#endif + default: + *wrlen = 0; return (0); } len = strlen(*bufp); if (len <= off) - return (0); - *bufp += off; - return (len - off); + *wrlen = 0; + else { + *bufp += off; + *wrlen = len - off; + } + return (0); } -int -kernfs_xwrite(kt, buf, len) - struct kern_target *kt; +static int +kernfs_xwrite(kfs, buf, len) + const struct kernfs_node *kfs; char *buf; - int len; + size_t len; { - switch (kt->kt_tag) { - case KTT_HOSTNAME: + switch (kfs->kfs_type) { + case Phostname: if (buf[len-1] == '\n') --len; - bcopy(buf, hostname, len); + memcpy(hostname, buf, len); hostname[len] = '\0'; - hostnamelen = len; + hostnamelen = (size_t) len; return (0); default: @@ -317,18 +428,17 @@ kernfs_lookup(v) struct componentname *cnp = ap->a_cnp; struct vnode **vpp = ap->a_vpp; struct vnode *dvp = ap->a_dvp; - char *pname = cnp->cn_nameptr; - struct kern_target *kt; - struct vnode *fvp; - int error, i; - -#ifdef KERNFS_DIAGNOSTIC - printf("kernfs_lookup(%x)\n", ap); - printf("kernfs_lookup(dp = %x, vpp = %x, cnp = %x)\n", dvp, vpp, ap->a_cnp); - printf("kernfs_lookup(%s)\n", pname); + const char *pname = cnp->cn_nameptr; + const struct kernfs_node *kfs; + const struct kern_target *kt; + int error, i, wantpunlock; +#ifdef IPSEC + char *ep; + u_int32_t id; #endif *vpp = NULLVP; + cnp->cn_flags &= ~PDIRUNLOCK; if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) return (EROFS); @@ -336,68 +446,161 @@ kernfs_lookup(v) if (cnp->cn_namelen == 1 && *pname == '.') { *vpp = dvp; VREF(dvp); - /*VOP_LOCK(dvp);*/ return (0); } -#if 0 - if (cnp->cn_namelen == 4 && bcmp(pname, "root", 4) == 0) { - *vpp = rootdir; - VREF(rootdir); - VOP_LOCK(rootdir); - return (0); - } -#endif + wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN)); + kfs = VTOKERN(dvp); + switch (kfs->kfs_type) { + case Pkern: + /* + * Shouldn't get here with .. in the root node. + */ + if (cnp->cn_flags & ISDOTDOT) + return (EIO); + + for (i = 0; i < nkern_targets; i++) { + kt = &kern_targets[i]; + if (cnp->cn_namelen == kt->kt_namlen && + memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0) + goto found; + } + break; - for (kt = kern_targets, i = 0; i < nkern_targets; kt++, i++) { - if (cnp->cn_namelen == kt->kt_namlen && - bcmp(kt->kt_name, pname, cnp->cn_namelen) == 0) - goto found; - } + found: + error = kernfs_allocvp(dvp->v_mount, vpp, kt->kt_tag, kt, 0); + if ((error == 0) && wantpunlock) { + VOP_UNLOCK(dvp, 0); + cnp->cn_flags |= PDIRUNLOCK; + } + return (error); -#ifdef KERNFS_DIAGNOSTIC - printf("kernfs_lookup: i = %d, failed", i); -#endif +#ifdef IPSEC + case Pipsecsadir: + for (i = 0; i < nipsecsa_targets; i++) { + kt = &ipsecsa_targets[i]; + if (cnp->cn_namelen == kt->kt_namlen && + memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0) { + error = kernfs_allocvp(dvp->v_mount, vpp, + kt->kt_tag, kt, 0); + if ((error == 0) && wantpunlock) { + VOP_UNLOCK(dvp, 0); + cnp->cn_flags |= PDIRUNLOCK; + } + return (error); + } + } - return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS); + ep = NULL; + id = strtoul(pname, &ep, 10); + if (!ep || *ep || ep == pname) + break; -found: - if (kt->kt_tag == KTT_DEVICE) { - dev_t *dp = kt->kt_data; - loop: - if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp)) - return (ENOENT); - *vpp = fvp; - if (vget(fvp, 1)) - goto loop; - return (0); + error = kernfs_allocvp(dvp->v_mount, vpp, Pipsecsa, NULL, id); + if ((error == 0) && wantpunlock) { + VOP_UNLOCK(dvp, 0); + cnp->cn_flags |= PDIRUNLOCK; + } + return (error); + + case Pipsecspdir: + for (i = 0; i < nipsecsp_targets; i++) { + kt = &ipsecsp_targets[i]; + if (cnp->cn_namelen == kt->kt_namlen && + memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0) { + error = kernfs_allocvp(dvp->v_mount, vpp, + kt->kt_tag, kt, 0); + if ((error == 0) && wantpunlock) { + VOP_UNLOCK(dvp, 0); + cnp->cn_flags |= PDIRUNLOCK; + } + return (error); + } + } + + ep = NULL; + id = strtoul(pname, &ep, 10); + if (!ep || *ep || ep == pname) + break; + + error = kernfs_allocvp(dvp->v_mount, vpp, Pipsecsp, NULL, id); + if ((error == 0) && wantpunlock) { + VOP_UNLOCK(dvp, 0); + cnp->cn_flags |= PDIRUNLOCK; + } + return (error); +#endif + + default: + return (ENOTDIR); } -#ifdef KERNFS_DIAGNOSTIC - printf("kernfs_lookup: allocate new vnode\n"); + return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS); +} + +int +kernfs_open(v) + void *v; +{ + struct vop_open_args /* { + struct vnode *a_vp; + int a_mode; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap = v; + struct kernfs_node *kfs = VTOKERN(ap->a_vp); +#ifdef IPSEC + struct mbuf *m; + struct secpolicy *sp; #endif - error = getnewvnode(VT_KERNFS, dvp->v_mount, kernfs_vnodeop_p, &fvp); - if (error) - return (error); - MALLOC(fvp->v_data, void *, sizeof(struct kernfs_node), M_TEMP, - M_WAITOK); - VTOKERN(fvp)->kf_kt = kt; - fvp->v_type = kt->kt_vtype; - *vpp = fvp; + switch (kfs->kfs_type) { +#ifdef IPSEC + case Pipsecsa: + m = key_setdumpsa_spi(htonl(kfs->kfs_value)); + if (m) { + m_freem(m); + return (0); + } else + return (ENOENT); -#ifdef KERNFS_DIAGNOSTIC - printf("kernfs_lookup: newvp = %x\n", fvp); + case Pipsecsp: + sp = key_getspbyid(kfs->kfs_value); + if (sp) { + kfs->kfs_v = sp; + return (0); + } else + return (ENOENT); #endif - return (0); + + default: + return (0); + } } -/*ARGSUSED*/ int -kernfs_open(v) +kernfs_close(v) void *v; { - /* Only need to check access permissions. */ + struct vop_close_args /* { + struct vnode *a_vp; + int a_fflag; + struct ucred *a_cred; + struct proc *a_p; + } */ *ap = v; + struct kernfs_node *kfs = VTOKERN(ap->a_vp); + + switch (kfs->kfs_type) { +#ifdef IPSEC + case Pipsecsp: + key_freesp((struct secpolicy *)kfs->kfs_v); + break; +#endif + + default: + break; + } + return (0); } @@ -411,11 +614,14 @@ kernfs_access(v) struct ucred *a_cred; struct proc *a_p; } */ *ap = v; - struct vnode *vp = ap->a_vp; - mode_t fmode = - (vp->v_flag & VROOT) ? DIR_MODE : VTOKERN(vp)->kf_kt->kt_mode; + struct vattr va; + int error; - return (vaccess(fmode, (uid_t)0, (gid_t)0, ap->a_mode, ap->a_cred)); + if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0) + return (error); + + return (vaccess(va.va_type, va.va_mode, va.va_uid, va.va_gid, + ap->a_mode, ap->a_cred)); } int @@ -428,57 +634,80 @@ kernfs_getattr(v) struct ucred *a_cred; struct proc *a_p; } */ *ap = v; - struct vnode *vp = ap->a_vp; + struct kernfs_node *kfs = VTOKERN(ap->a_vp); struct vattr *vap = ap->a_vap; - struct timeval tv; int error = 0; char strbuf[KSTRING], *buf; + size_t nread, total; - bzero((caddr_t) vap, sizeof(*vap)); - vattr_null(vap); + VATTR_NULL(vap); + vap->va_type = ap->a_vp->v_type; vap->va_uid = 0; vap->va_gid = 0; - vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0]; + vap->va_mode = kfs->kfs_mode; + vap->va_fileid = kfs->kfs_fileno; + vap->va_flags = 0; vap->va_size = 0; vap->va_blocksize = DEV_BSIZE; - microtime(&tv); - TIMEVAL_TO_TIMESPEC(&tv, &vap->va_atime); - vap->va_mtime = vap->va_atime; - vap->va_ctime = vap->va_ctime; + /* + * Make all times be current TOD. Avoid microtime(9), it's slow. + * We don't guard the read from time(9) with splclock(9) since we + * don't actually need to be THAT sure the access is atomic. + */ + TIMEVAL_TO_TIMESPEC(&time, &vap->va_ctime); + vap->va_atime = vap->va_mtime = vap->va_ctime; vap->va_gen = 0; vap->va_flags = 0; vap->va_rdev = 0; vap->va_bytes = 0; - if (vp->v_flag & VROOT) { -#ifdef KERNFS_DIAGNOSTIC - printf("kernfs_getattr: stat rootdir\n"); -#endif - vap->va_type = VDIR; - vap->va_mode = DIR_MODE; - vap->va_nlink = 2; - vap->va_fileid = 2; - vap->va_size = DEV_BSIZE; - } else { - struct kern_target *kt = VTOKERN(vp)->kf_kt; - int nbytes, total; -#ifdef KERNFS_DIAGNOSTIC - printf("kernfs_getattr: stat target %s\n", kt->kt_name); + switch (kfs->kfs_type) { + case Pkern: + vap->va_nlink = 4; + vap->va_bytes = vap->va_size = DEV_BSIZE; + break; + + case Proot: + vap->va_nlink = 1; + vap->va_bytes = vap->va_size = DEV_BSIZE; + break; + + case Pnull: + case Ptime: + case Pint: + case Pstring: + case Phostname: + case Pavenrun: + case Pdevice: + case Pmsgbuf: +#ifdef IPSEC + case Pipsecsa: + case Pipsecsp: #endif - vap->va_type = kt->kt_vtype; - vap->va_mode = kt->kt_mode; vap->va_nlink = 1; - vap->va_fileid = 3 + (kt - kern_targets); total = 0; - while (buf = strbuf, - nbytes = kernfs_xread(kt, total, &buf, sizeof(strbuf))) - total += nbytes; - vap->va_size = total; - } + do { + buf = strbuf; + error = kernfs_xread(kfs, total, &buf, + sizeof(strbuf), &nread); + total += nread; + } while (error == 0 && nread != 0); + vap->va_bytes = vap->va_size = total; + break; -#ifdef KERNFS_DIAGNOSTIC - printf("kernfs_getattr: return error %d\n", error); +#ifdef IPSEC + case Pipsecsadir: + case Pipsecspdir: + vap->va_nlink = 2; + vap->va_bytes = vap->va_size = DEV_BSIZE; + break; #endif + + default: + error = EINVAL; + break; + } + return (error); } @@ -487,6 +716,7 @@ int kernfs_setattr(v) void *v; { + /* * Silently ignore attribute changes. * This allows for open with truncate to have no @@ -506,34 +736,21 @@ kernfs_read(v) int a_ioflag; struct ucred *a_cred; } */ *ap = v; - struct vnode *vp = ap->a_vp; struct uio *uio = ap->a_uio; - struct kern_target *kt; + struct kernfs_node *kfs = VTOKERN(ap->a_vp); char strbuf[KSTRING], *buf; - int off, len; + off_t off; + size_t len; int error; - if (vp->v_type == VDIR) + if (ap->a_vp->v_type == VDIR) return (EOPNOTSUPP); - kt = VTOKERN(vp)->kf_kt; - -#ifdef KERNFS_DIAGNOSTIC - printf("kern_read %s\n", kt->kt_name); -#endif - off = uio->uio_offset; -#if 0 - while (buf = strbuf, -#else - if (buf = strbuf, -#endif - len = kernfs_xread(kt, off, &buf, sizeof(strbuf))) { - if ((error = uiomove(buf, len, uio)) != 0) - return (error); - off += len; - } - return (0); + buf = strbuf; + if ((error = kernfs_xread(kfs, off, &buf, sizeof(strbuf), &len)) == 0) + error = uiomove(buf, len, uio); + return (error); } int @@ -546,17 +763,11 @@ kernfs_write(v) int a_ioflag; struct ucred *a_cred; } */ *ap = v; - struct vnode *vp = ap->a_vp; + struct kernfs_node *kfs = VTOKERN(ap->a_vp); struct uio *uio = ap->a_uio; - struct kern_target *kt; int error, xlen; char strbuf[KSTRING]; - if (vp->v_type == VDIR) - return (EOPNOTSUPP); - - kt = VTOKERN(vp)->kf_kt; - if (uio->uio_offset != 0) return (EINVAL); @@ -569,7 +780,7 @@ kernfs_write(v) strbuf[xlen] = '\0'; xlen = strlen(strbuf); - return (kernfs_xwrite(kt, strbuf, xlen)); + return (kernfs_xwrite(kfs, strbuf, xlen)); } int @@ -581,19 +792,21 @@ kernfs_readdir(v) struct uio *a_uio; struct ucred *a_cred; int *a_eofflag; - u_long *a_cookies; - int a_ncookies; + off_t **a_cookies; + int a_*ncookies; } */ *ap = v; struct uio *uio = ap->a_uio; struct dirent d; - struct kern_target *kt; - int i; + struct kernfs_node *kfs = VTOKERN(ap->a_vp); + const struct kern_target *kt; + off_t i; int error; - u_long *cookies = ap->a_cookies; - int ncookies = ap->a_ncookies; - - if (ap->a_vp->v_type != VDIR) - return (ENOTDIR); + off_t *cookies = NULL; + int ncookies = 0, n; +#ifdef IPSEC + struct secasvar *sav, *sav2; + struct secpolicy *sp; +#endif if (uio->uio_resid < UIO_MX) return (EINVAL); @@ -602,32 +815,208 @@ kernfs_readdir(v) error = 0; i = uio->uio_offset; - bzero((caddr_t)&d, UIO_MX); + memset(&d, 0, sizeof(d)); d.d_reclen = UIO_MX; + ncookies = uio->uio_resid / UIO_MX; - for (kt = &kern_targets[i]; - uio->uio_resid >= UIO_MX && i < nkern_targets; kt++, i++) { -#ifdef KERNFS_DIAGNOSTIC - printf("kernfs_readdir: i = %d\n", i); -#endif + switch (kfs->kfs_type) { + case Pkern: + if (i >= nkern_targets) + return (0); - if (kt->kt_tag == KTT_DEVICE) { - dev_t *dp = kt->kt_data; - struct vnode *fvp; + if (ap->a_ncookies) { + ncookies = min(ncookies, (nkern_targets - i)); + cookies = malloc(ncookies * sizeof(off_t), M_TEMP, + M_WAITOK); + *ap->a_cookies = cookies; + } - if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp)) + n = 0; + for (; i < nkern_targets && uio->uio_resid >= UIO_MX; i++) { + kt = &kern_targets[i]; + d.d_namlen = kt->kt_namlen; + if (i < 2) + d.d_fileno = KERNFS_FILENO(&kern_targets[0], + kern_targets[0].kt_tag, 0); + else + d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0); + memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1); + d.d_type = kt->kt_type; + if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0) + break; + if (cookies) + *cookies++ = i + 1; + n++; + } + ncookies = n; + break; + + case Proot: + if (i >= 2) + return 0; + + if (ap->a_ncookies) { + ncookies = min(ncookies, (2 - i)); + cookies = malloc(ncookies * sizeof(off_t), M_TEMP, + M_WAITOK); + *ap->a_cookies = cookies; + } + + n = 0; + for (; i < 2 && uio->uio_resid >= UIO_MX; i++) { + kt = &kern_targets[i]; + d.d_namlen = kt->kt_namlen; + d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0); + memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1); + d.d_type = kt->kt_type; + if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0) + break; + if (cookies) + *cookies++ = i + 1; + n++; + } + ncookies = n; + break; + +#ifdef IPSEC + case Pipsecsadir: + /* count SA in the system */ + n = 0; + TAILQ_FOREACH(sav, &satailq, tailq) { + for (sav2 = TAILQ_FIRST(&satailq); + sav2 != sav; + sav2 = TAILQ_NEXT(sav2, tailq)) { + if (sav->spi == sav2->spi) { + /* multiple SA with same SPI */ + break; + } + } + if (sav == sav2 || sav->spi != sav2->spi) + n++; + } + + if (i >= nipsecsa_targets + n) + return (0); + + if (ap->a_ncookies) { + ncookies = min(ncookies, (n - i)); + cookies = malloc(ncookies * sizeof(off_t), M_TEMP, + M_WAITOK); + *ap->a_cookies = cookies; + } + + n = 0; + for (; i < nipsecsa_targets && uio->uio_resid >= UIO_MX; i++) { + kt = &ipsecsa_targets[i]; + d.d_namlen = kt->kt_namlen; + d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0); + memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1); + d.d_type = kt->kt_type; + if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0) + break; + if (cookies) + *cookies++ = i + 1; + n++; + } + if (error) { + ncookies = n; + break; + } + + TAILQ_FOREACH(sav, &satailq, tailq) { + for (sav2 = TAILQ_FIRST(&satailq); + sav2 != sav; + sav2 = TAILQ_NEXT(sav2, tailq)) { + if (sav->spi == sav2->spi) { + /* multiple SA with same SPI */ + break; + } + } + if (sav != sav2 && sav->spi == sav2->spi) continue; + if (uio->uio_resid < UIO_MX) + break; + d.d_fileno = KERNFS_FILENO(kfs->kfs_kt, kfs->kfs_type, + kfs->kfs_cookie); + d.d_namlen = snprintf(d.d_name, sizeof(d.d_name), + "%u", ntohl(sav->spi)); + d.d_type = DT_REG; + if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0) + break; + if (cookies) + *cookies++ = i + 1; + n++; + i++; } + ncookies = n; + break; + + case Pipsecspdir: + /* count SP in the system */ + n = 0; + TAILQ_FOREACH(sp, &sptailq, tailq) + n++; + + if (i >= 2 + n) + return (0); - d.d_fileno = i + 3; - d.d_namlen = kt->kt_namlen; - bcopy(kt->kt_name, d.d_name, kt->kt_namlen + 1); - d.d_type = kt->kt_type; + if (ap->a_ncookies) { + ncookies = min(ncookies, (n - i)); + cookies = malloc(ncookies * sizeof(off_t), M_TEMP, + M_WAITOK); + *ap->a_cookies = cookies; + } - if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0) + n = 0; + for (; i < nipsecsp_targets && uio->uio_resid >= UIO_MX; i++) { + kt = &ipsecsp_targets[i]; + d.d_namlen = kt->kt_namlen; + d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0); + memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1); + d.d_type = kt->kt_type; + if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0) + break; + if (cookies) + *cookies++ = i + 1; + n++; + } + if (error) { + ncookies = n; break; - if (ncookies-- > 0) - *cookies++ = i + 1; + } + + TAILQ_FOREACH(sp, &sptailq, tailq) { + if (uio->uio_resid < UIO_MX) + break; + d.d_fileno = KERNFS_FILENO(kfs->kfs_kt, kfs->kfs_type, + kfs->kfs_cookie); + d.d_namlen = snprintf(d.d_name, sizeof(d.d_name), + "%u", sp->id); + d.d_type = DT_REG; + if ((error = uiomove((caddr_t)&d, UIO_MX, uio)) != 0) + break; + if (cookies) + *cookies++ = i + 1; + n++; + i++; + } + ncookies = n; + break; +#endif + + default: + error = ENOTDIR; + break; + } + + if (ap->a_ncookies) { + if (error) { + if (cookies) + free(*ap->a_cookies, M_TEMP); + *ap->a_ncookies = 0; + *ap->a_cookies = NULL; + } else + *ap->a_ncookies = ncookies; } uio->uio_offset = i; @@ -640,17 +1029,40 @@ kernfs_inactive(v) { struct vop_inactive_args /* { struct vnode *a_vp; + struct proc *a_p; } */ *ap = v; struct vnode *vp = ap->a_vp; + const struct kernfs_node *kfs = VTOKERN(ap->a_vp); +#ifdef IPSEC + struct mbuf *m; + struct secpolicy *sp; +#endif -#ifdef KERNFS_DIAGNOSTIC - printf("kernfs_inactive(%x)\n", vp); + VOP_UNLOCK(vp, 0); + switch (kfs->kfs_type) { +#ifdef IPSEC + case Pipsecsa: + m = key_setdumpsa_spi(htonl(kfs->kfs_value)); + if (m) + m_freem(m); + else { + /* should never happen as we hold a refcnt */ + vgone(vp); + } + break; + case Pipsecsp: + sp = key_getspbyid(kfs->kfs_value); + if (sp) + key_freesp(sp); + else { + /* should never happen as we hold a refcnt */ + vgone(vp); + } + break; #endif - /* - * Clear out the v_type field to avoid - * nasty things happening in vgone(). - */ - vp->v_type = VNON; + default: + break; + } return (0); } @@ -661,16 +1073,8 @@ kernfs_reclaim(v) struct vop_reclaim_args /* { struct vnode *a_vp; } */ *ap = v; - struct vnode *vp = ap->a_vp; -#ifdef KERNFS_DIAGNOSTIC - printf("kernfs_reclaim(%x)\n", vp); -#endif - if (vp->v_data) { - FREE(vp->v_data, M_TEMP); - vp->v_data = 0; - } - return (0); + return (kernfs_freevp(ap->a_vp)); } /* @@ -705,6 +1109,9 @@ kernfs_pathconf(v) case _PC_VDISABLE: *ap->a_retval = _POSIX_VDISABLE; return (0); + case _PC_SYNC_IO: + *ap->a_retval = 1; + return (0); default: return (EINVAL); } @@ -724,15 +1131,6 @@ kernfs_print(v) return (0); } -/*ARGSUSED*/ -int -kernfs_vfree(v) - void *v; -{ - - return (0); -} - int kernfs_link(v) void *v; @@ -764,42 +1162,3 @@ kernfs_symlink(v) vput(ap->a_dvp); return (EROFS); } - -int -kernfs_abortop(v) - void *v; -{ - struct vop_abortop_args /* { - struct vnode *a_dvp; - struct componentname *a_cnp; - } */ *ap = v; - - if ((ap->a_cnp->cn_flags & (HASBUF | SAVESTART)) == HASBUF) - FREE(ap->a_cnp->cn_pnbuf, M_NAMEI); - return (0); -} - -/* - * /dev/fd vnode unsupported operation - */ -/*ARGSUSED*/ -int -kernfs_enotsupp(v) - void *v; -{ - - return (EOPNOTSUPP); -} - -/* - * /dev/fd "should never get here" operation - */ -/*ARGSUSED*/ -int -kernfs_badop(v) - void *v; -{ - - panic("kernfs: bad op"); - return 0; -}