[BACK]Return to kernfs_vnops.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / miscfs / kernfs

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/sys/miscfs/kernfs/kernfs_vnops.c between version 1.16 and 1.99

version 1.16, 1993/12/22 13:14:10 version 1.99, 2004/04/29 16:10:55
Line 1 
Line 1 
   /*      $NetBSD$        */
   
 /*  /*
  * Copyright (c) 1990, 1992 Jan-Simon Pendry   * Copyright (c) 1992, 1993
  * All rights reserved.   *      The Regents of the University of California.  All rights reserved.
  *   *
  * This code is derived from software contributed to Berkeley by   * This code is derived from software donated to Berkeley by
  * Jan-Simon Pendry.   * Jan-Simon Pendry.
  *   *
  * Redistribution and use in source and binary forms, with or without   * Redistribution and use in source and binary forms, with or without
Line 13 
Line 15 
  * 2. Redistributions in binary form must reproduce the above copyright   * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the   *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.   *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software   * 3. Neither the name of the University nor the names of its contributors
  *    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  
  *    may be used to endorse or promote products derived from this software   *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.   *    without specific prior written permission.
  *   *
Line 33 
Line 31 
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.   * SUCH DAMAGE.
  *   *
  *      $Id$   *      @(#)kernfs_vnops.c      8.15 (Berkeley) 5/21/95
  */   */
   
 /*  /*
  * Kernel parameter filesystem   * Kernel parameter filesystem (/kern)
  */   */
   
   #include <sys/cdefs.h>
   __KERNEL_RCSID(0, "$NetBSD$");
   
   #ifdef _KERNEL_OPT
   #include "opt_ipsec.h"
   #endif
   
 #include <sys/param.h>  #include <sys/param.h>
 #include <sys/systm.h>  #include <sys/systm.h>
 #include <sys/kernel.h>  #include <sys/kernel.h>
 #include <sys/types.h>  #include <sys/vmmeter.h>
 #include <sys/time.h>  #include <sys/time.h>
 #include <sys/proc.h>  #include <sys/proc.h>
 #include <sys/file.h>  
 #include <sys/vnode.h>  #include <sys/vnode.h>
   #include <sys/malloc.h>
   #include <sys/file.h>
 #include <sys/stat.h>  #include <sys/stat.h>
 #include <sys/mount.h>  #include <sys/mount.h>
 #include <sys/namei.h>  #include <sys/namei.h>
 #include <sys/buf.h>  #include <sys/buf.h>
   #include <sys/dirent.h>
   #include <sys/msgbuf.h>
   
 #include <ufs/dir.h>            /* For readdir() XXX */  #include <miscfs/genfs/genfs.h>
   
 #include <miscfs/kernfs/kernfs.h>  #include <miscfs/kernfs/kernfs.h>
   
 struct kernfs_target kernfs_targets[] = {  #ifdef IPSEC
   #include <sys/mbuf.h>
   #include <net/route.h>
   #include <netinet/in.h>
   #include <netinet6/ipsec.h>
   #include <netkey/key.h>
   #endif
   
   #include <uvm/uvm_extern.h>
   
   #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)
   
   #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,            KFSkern,        VDIR, DIR_MODE   },
        { DT_DIR, N(".."),        0,            KFSroot,        VDIR, DIR_MODE   },
        { DT_REG, N("boottime"),  &boottime.tv_sec, KFSint,     VREG, READ_MODE  },
                           /* XXX cast away const */
        { DT_REG, N("copyright"), (void *)copyright,
                                                KFSstring,      VREG, READ_MODE  },
        { DT_REG, N("hostname"),  0,            KFShostname,    VREG, WRITE_MODE },
        { DT_REG, N("hz"),        &hz,          KFSint,         VREG, READ_MODE  },
   #ifdef IPSEC
        { DT_DIR, N("ipsecsa"),   0,            KFSipsecsadir,  VDIR, UDIR_MODE  },
        { DT_DIR, N("ipsecsp"),   0,            KFSipsecspdir,  VDIR, UDIR_MODE  },
   #endif
        { DT_REG, N("loadavg"),   0,            KFSavenrun,     VREG, READ_MODE  },
        { DT_REG, N("msgbuf"),    0,            KFSmsgbuf,      VREG, READ_MODE  },
        { DT_REG, N("pagesize"),  &uvmexp.pagesize, KFSint,     VREG, READ_MODE  },
        { DT_REG, N("physmem"),   &physmem,     KFSint,         VREG, READ_MODE  },
   #if 0
        { DT_DIR, N("root"),      0,            KFSnull,        VDIR, DIR_MODE   },
   #endif
        { DT_BLK, N("rootdev"),   &rootdev,     KFSdevice,      VBLK, READ_MODE  },
        { DT_CHR, N("rrootdev"),  &rrootdev,    KFSdevice,      VCHR, READ_MODE  },
        { DT_REG, N("time"),      0,            KFStime,        VREG, READ_MODE  },
                           /* XXX cast away const */
        { DT_REG, N("version"),   (void *)version,
                                                KFSstring,      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,            KFSipsecsadir,  VDIR, DIR_MODE   },
        { DT_DIR, N(".."),        0,            KFSkern,        VDIR, DIR_MODE   },
   };
   const struct kern_target ipsecsp_targets[] = {
 /* NOTE: The name must be less than UIO_MX-16 chars in length */  /* NOTE: The name must be less than UIO_MX-16 chars in length */
 DIR_TARGET(".",         0,              KTT_NULL,       KTM_DIR_PERMS   )       /*        name            data          tag           type  ro/rw */
 DIR_TARGET("..",        0,              KTT_NULL,       KTM_DIR_PERMS   )       { DT_DIR, N("."),         0,            KFSipsecspdir,  VDIR, DIR_MODE   },
 REG_TARGET("copyright", copyright,      KTT_STRING,     KTM_RO_PERMS    )       { DT_DIR, N(".."),        0,            KFSkern,        VDIR, DIR_MODE   },
 REG_TARGET("hostname",  0,              KTT_HOSTNAME,   KTM_RW_PERMS    )  
 REG_TARGET("hz",        &hz,            KTT_INT,        KTM_RO_PERMS    )  
 REG_TARGET("loadavg",   0,              KTT_AVENRUN,    KTM_RO_PERMS    )  
 REG_TARGET("physmem",   &physmem,       KTT_INT,        KTM_RO_PERMS    )  
 #ifdef KERNFS_HAVE_ROOTDIR  
 DIR_TARGET("root",      0,              KTT_NULL,       KTM_DIR_PERMS   )  
 #endif  
 BLK_TARGET("rootdev",   0,              KTT_NULL,       KTM_RO_PERMS    )  
 CHR_TARGET("rrootdev",  0,              KTT_NULL,       KTM_RO_PERMS    )  
 REG_TARGET("time",      0,              KTT_TIME,       KTM_RO_PERMS    )  
 REG_TARGET("version",   version,        KTT_STRING,     KTM_RO_PERMS    )  
 };  };
   #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
   
 static int nkernfs_targets = sizeof(kernfs_targets) / sizeof(kernfs_targets[0]);  
   int     kernfs_lookup   __P((void *));
   #define kernfs_create   genfs_eopnotsupp
   #define kernfs_mknod    genfs_eopnotsupp
   int     kernfs_open     __P((void *));
   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_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   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 genfs_eopnotsupp
   #define kernfs_abortop  genfs_abortop
   int     kernfs_inactive __P((void *));
   int     kernfs_reclaim  __P((void *));
   #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 genfs_islocked
   int     kernfs_pathconf __P((void *));
   #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
   
   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 *));
   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_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 }
   };
   const struct vnodeopv_desc kernfs_vnodeop_opv_desc =
           { &kernfs_vnodeop_p, kernfs_vnodeop_entries };
   
 static int  static int
 kernfs_xread(kt, buf, len, lenp)  kernfs_xread(kfs, off, bufp, len, wrlen)
         struct kernfs_target *kt;          struct kernfs_node *kfs;
         char *buf;          int off;
         int len;          char **bufp;
         int *lenp;          size_t len;
           size_t *wrlen;
 {  {
         int xlen;          const struct kern_target *kt;
   #ifdef IPSEC
           struct mbuf *m;
   #endif
   
           kt = kfs->kfs_kt;
   
         switch (kt->kt_tag) {          switch (kfs->kfs_type) {
         case KTT_TIME: {          case KFStime: {
                 struct timeval tv;                  struct timeval tv;
   
                 microtime(&tv);                  microtime(&tv);
                 sprintf(buf, "%d %d\n", tv.tv_sec, tv.tv_usec);                  snprintf(*bufp, len, "%ld %ld\n", tv.tv_sec, tv.tv_usec);
                 break;                  break;
         }          }
   
         case KTT_INT: {          case KFSint: {
                 int *ip = kt->kt_data;                  int *ip = kt->kt_data;
                 sprintf(buf, "%d\n", *ip);  
                   snprintf(*bufp, len, "%d\n", *ip);
                 break;                  break;
         }          }
   
         case KTT_STRING: {          case KFSstring: {
                 char *cp = kt->kt_data;                  char *cp = kt->kt_data;
                 int xlen = strlen(cp) + 1;  
   
                 if (xlen >= len)                  *bufp = cp;
                         return (EINVAL);  
   
                 bcopy(cp, buf, xlen);  
                 break;                  break;
         }          }
   
         case KTT_HOSTNAME: {          case KFSmsgbuf: {
                   long n;
   
                   /*
                    * 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 >= msgbufp->msg_bufs)
                           n -= msgbufp->msg_bufs;
                   len = min(msgbufp->msg_bufs - n, msgbufp->msg_bufs - off);
                   *bufp = msgbufp->msg_bufc + n;
                   *wrlen = len;
                   return (0);
           }
   
           case KFShostname: {
                 char *cp = hostname;                  char *cp = hostname;
                 int xlen = hostnamelen;                  int xlen = hostnamelen;
   
                 if (xlen + 2 > len)     /* extra space for null and newline */                  if (xlen >= (len - 2))
                         return (EINVAL);                          return (EINVAL);
   
                 bcopy(cp, buf, xlen);   /* safer than sprintf */                  memcpy(*bufp, cp, xlen);
                 buf[xlen] = '\n';                  (*bufp)[xlen] = '\n';
                 buf[xlen+1] = '\0';                  (*bufp)[xlen+1] = '\0';
                   len = strlen(*bufp);
                 break;                  break;
         }          }
   
         case KTT_AVENRUN:          case KFSavenrun:
                 sprintf(buf, "%d %d %d %d\n",                  averunnable.fscale = FSCALE;
                                 averunnable.ldavg[0],                  snprintf(*bufp, len, "%d %d %d %ld\n",
                                 averunnable.ldavg[1],                      averunnable.ldavg[0], averunnable.ldavg[1],
                                 averunnable.ldavg[2],                      averunnable.ldavg[2], averunnable.fscale);
                                 FSCALE);  
                 break;                  break;
   
   #ifdef IPSEC
           case KFSipsecsa:
                   /*
                    * 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 KFSipsecsp:
                   /*
                    * 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:          default:
                 return (EINVAL);                  *wrlen = 0;
                   return (0);
         }          }
   
         *lenp = strlen(buf);          len = strlen(*bufp);
           if (len <= off)
                   *wrlen = 0;
           else {
                   *bufp += off;
                   *wrlen = len - off;
           }
         return (0);          return (0);
 }  }
   
 static int  static int
 kernfs_xwrite(kt, buf, len)  kernfs_xwrite(kfs, buf, len)
         struct kernfs_target *kt;          const struct kernfs_node *kfs;
         char *buf;          char *buf;
         int len;          size_t len;
 {  {
         switch (kt->kt_tag) {  
         case KTT_HOSTNAME: {          switch (kfs->kfs_type) {
           case KFShostname:
                 if (buf[len-1] == '\n')                  if (buf[len-1] == '\n')
                         --len;                          --len;
                 bcopy(buf, hostname, len);                  memcpy(hostname, buf, len);
                 /* kernfs_write set buf[value_passed_as_len] = \0.                  hostname[len] = '\0';
                  * therefore, buf len (hostnamelen) = len.                  hostnamelen = (size_t) len;
                  */  
                 hostnamelen = len;  
                 hostname[hostnamelen] = '\0';   /* null end of string. */  
                 return (0);                  return (0);
         }  
   
         default:          default:
                 return (EIO);                  return (EIO);
         }          }
 }  }
   
   
 /*  /*
  * vp is the current namei directory   * vp is the current namei directory
  * ndp is the name to locate in that directory...   * ndp is the name to locate in that directory...
  */   */
 kernfs_lookup(dvp, ndp, p)  int
         struct vnode *dvp;  kernfs_lookup(v)
         struct nameidata *ndp;          void *v;
         struct proc *p;  {
 {          struct vop_lookup_args /* {
         char *pname = ndp->ni_ptr;                  struct vnode * a_dvp;
         int error = ENOENT;                  struct vnode ** a_vpp;
         int i;                  struct componentname * a_cnp;
         struct vnode *fvp;          } */ *ap = v;
           struct componentname *cnp = ap->a_cnp;
 #ifdef KERNFS_DIAGNOSTIC          struct vnode **vpp = ap->a_vpp;
         printf("kernfs_lookup(%s)\n", pname);          struct vnode *dvp = ap->a_dvp;
 #endif          const char *pname = cnp->cn_nameptr;
         if (ndp->ni_namelen == 1 && *pname == '.') {          const struct kernfs_node *kfs;
                 ndp->ni_dvp = dvp;          const struct kern_target *kt;
                 ndp->ni_vp = dvp;          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);
   
           if (cnp->cn_namelen == 1 && *pname == '.') {
                   *vpp = dvp;
                 VREF(dvp);                  VREF(dvp);
                 /*VOP_LOCK(dvp);*/  
                 return (0);                  return (0);
         }          }
   
 #ifdef KERNFS_HAVE_ROOTDIR          wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
         if (ndp->ni_namelen == 4 && bcmp(pname, "root", 4) == 0) {          kfs = VTOKERN(dvp);
                 ndp->ni_dvp = dvp;          switch (kfs->kfs_type) {
                 ndp->ni_vp = rootdir;          case KFSkern:
                 VREF(rootdir);                  /*
                 VOP_LOCK(rootdir);                   * Shouldn't get here with .. in the root node.
                 return (0);                   */
         }                  if (cnp->cn_flags & ISDOTDOT)
 #endif                          return (EIO);
   
         /*                  for (i = 0; i < nkern_targets; i++) {
          * /kern/rootdev is the root device                          kt = &kern_targets[i];
          */                          if (cnp->cn_namelen == kt->kt_namlen &&
         if (ndp->ni_namelen == 7 && bcmp(pname, "rootdev", 7) == 0) {                              memcmp(kt->kt_name, pname, cnp->cn_namelen) == 0)
                 if (!rootvp) {                                  goto found;
                         error = ENOENT;                  }
                         goto bad;                  break;
                 }  
                 ndp->ni_dvp = dvp;  
                 ndp->ni_vp = rootvp;  
                 VREF(rootvp);  
                 VOP_LOCK(rootvp);  
                 return (0);  
         }  
   
         /*          found:
          * /kern/rrootdev is the raw root device                  error = kernfs_allocvp(dvp->v_mount, vpp, kt->kt_tag, kt, 0);
          */                  if ((error == 0) && wantpunlock) {
         if (ndp->ni_namelen == 8 && bcmp(pname, "rrootdev", 7) == 0) {                          VOP_UNLOCK(dvp, 0);
                 if (!rrootdevvp) {                          cnp->cn_flags |= PDIRUNLOCK;
                         error = ENOENT;                  }
                         goto bad;                  return (error);
                 }  
                 ndp->ni_dvp = dvp;  #ifdef IPSEC
                 ndp->ni_vp = rrootdevvp;          case KFSipsecsadir:
                 VREF(rrootdevvp);                  for (i = 0; i < nipsecsa_targets; i++) {
                 VOP_LOCK(rrootdevvp);                          kt = &ipsecsa_targets[i];
                 return (0);                          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);
                           }
                   }
   
         for (i = 0; i < nkernfs_targets; i++) {                  ep = NULL;
                 struct kernfs_target *kt = &kernfs_targets[i];                  id = strtoul(pname, &ep, 10);
                 if (ndp->ni_namelen == strlen(kt->kt_name) &&                  if (!ep || *ep || ep == pname)
                     bcmp(kt->kt_name, pname, ndp->ni_namelen) == 0) {  
                         error = 0;  
                         break;                          break;
   
                   error = kernfs_allocvp(dvp->v_mount, vpp, KFSipsecsa, NULL, id);
                   if ((error == 0) && wantpunlock) {
                           VOP_UNLOCK(dvp, 0);
                           cnp->cn_flags |= PDIRUNLOCK;
                 }                  }
         }                  return (error);
   
           case KFSipsecspdir:
                   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;
   
 #ifdef KERNFS_DIAGNOSTIC                  error = kernfs_allocvp(dvp->v_mount, vpp, KFSipsecsp, NULL, id);
         printf("kernfs_lookup: i = %d, error = %d\n", i, error);                  if ((error == 0) && wantpunlock) {
                           VOP_UNLOCK(dvp, 0);
                           cnp->cn_flags |= PDIRUNLOCK;
                   }
                   return (error);
 #endif  #endif
   
         if (error)          default:
                 goto bad;                  return (ENOTDIR);
           }
   
 #ifdef KERNFS_DIAGNOSTIC          return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
         printf("kernfs_lookup: allocate new vnode\n");  }
 #endif  
         error = getnewvnode(VT_KERNFS, dvp->v_mount, &kernfs_vnodeops, &fvp);  int
         if (error)  kernfs_open(v)
                 goto bad;          void *v;
         VTOKERN(fvp)->kf_kt = &kernfs_targets[i];  {
         fvp->v_type = VTOKERN(fvp)->kf_kt->kt_vtype;          struct vop_open_args /* {
         ndp->ni_dvp = dvp;                  struct vnode *a_vp;
         ndp->ni_vp = fvp;                  int a_mode;
 #ifdef KERNFS_DIAGNOSTIC                  struct ucred *a_cred;
         printf("kernfs_lookup: newvp = %x\n", fvp);                  struct proc *a_p;
           } */ *ap = v;
           struct kernfs_node *kfs = VTOKERN(ap->a_vp);
   #ifdef IPSEC
           struct mbuf *m;
           struct secpolicy *sp;
 #endif  #endif
         return (0);  
   
 bad:;          switch (kfs->kfs_type) {
         ndp->ni_dvp = dvp;  #ifdef IPSEC
         ndp->ni_vp = NULL;          case KFSipsecsa:
 #ifdef KERNFS_DIAGNOSTIC                  m = key_setdumpsa_spi(htonl(kfs->kfs_value));
         printf("kernfs_lookup: error = %d\n", error);                  if (m) {
                           m_freem(m);
                           return (0);
                   } else
                           return (ENOENT);
   
           case KFSipsecsp:
                   sp = key_getspbyid(kfs->kfs_value);
                   if (sp) {
                           kfs->kfs_v = sp;
                           return (0);
                   } else
                           return (ENOENT);
 #endif  #endif
         return (error);  
           default:
                   return (0);
           }
 }  }
   
 kernfs_open(vp, mode, cred, p)  int
         struct vnode *vp;  kernfs_close(v)
         int mode;          void *v;
         struct ucred *cred;  
         struct proc *p;  
 {  {
         /* if access succeeded, this always does, too */          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 KFSipsecsp:
                   key_freesp((struct secpolicy *)kfs->kfs_v);
                   break;
   #endif
   
           default:
                   break;
           }
   
         return (0);          return (0);
 }  }
   
 /*  int
  * Check mode permission on target pointer. Mode is READ, WRITE or EXEC.  kernfs_access(v)
  * The mode is shifted to select the owner/group/other fields. The          void *v;
  * super user is granted all permissions.  {
  */          struct vop_access_args /* {
 kernfs_access(vp, mode, cred, p)                  struct vnode *a_vp;
         struct vnode *vp;                  int a_mode;
         register int mode;                  struct ucred *a_cred;
         struct ucred *cred;                  struct proc *a_p;
         struct proc *p;          } */ *ap = v;
 {          struct vattr va;
         struct kernfs_target *kt = VTOKERN(vp)->kf_kt;          int error;
         register gid_t *gp;  
         int i, error;          if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
                   return (error);
 #ifdef KERN_DIAGNOSTIC  
         if (!VOP_ISLOCKED(vp)) {          return (vaccess(va.va_type, va.va_mode, va.va_uid, va.va_gid,
                 vprint("kernfs_access: not locked", vp);              ap->a_mode, ap->a_cred));
                 panic("kernfs_access: not locked");  
         }  
 #endif  
         /*  
          * If you're the super-user, you always get access.  
          */  
         if (cred->cr_uid == 0)  
                 return (0);  
         /*  
          * Access check is based on only one of owner, group, public.  
          * If not owner, then check group. If not a member of the  
          * group, then check public access.  
          */  
         if (cred->cr_uid != /* kt->kt_uid XXX */ 0) {  
                 mode >>= 3;  
                 gp = cred->cr_groups;  
                 for (i = 0; i < cred->cr_ngroups; i++, gp++)  
                         if (/* kt->kt_gid XXX */ 0 == *gp)  
                                 goto found;  
                 mode >>= 3;  
 found:  
                 ;  
         }  
         if ((kt->kt_perms & mode) == mode)  
                 return (0);  
         return (EACCES);  
 }  }
   
 kernfs_getattr(vp, vap, cred, p)  int
         struct vnode *vp;  kernfs_getattr(v)
         struct vattr *vap;          void *v;
         struct ucred *cred;  
         struct proc *p;  
 {  {
           struct vop_getattr_args /* {
                   struct vnode *a_vp;
                   struct vattr *a_vap;
                   struct ucred *a_cred;
                   struct proc *a_p;
           } */ *ap = v;
           struct kernfs_node *kfs = VTOKERN(ap->a_vp);
           struct vattr *vap = ap->a_vap;
         int error = 0;          int error = 0;
         char strbuf[KSTRING];          char strbuf[KSTRING], *buf;
         struct kernfs_target *kt = VTOKERN(vp)->kf_kt;          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 = kt->kt_uid;          vap->va_uid = 0;
         vap->va_gid = kt->kt_gid;          vap->va_gid = 0;
         vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];          vap->va_mode = kfs->kfs_mode;
         /* vap->va_qsize = 0; */          vap->va_fileid = kfs->kfs_fileno;
           vap->va_flags = 0;
           vap->va_size = 0;
         vap->va_blocksize = DEV_BSIZE;          vap->va_blocksize = DEV_BSIZE;
         microtime(&vap->va_atime);          /*
         vap->va_mtime = vap->va_atime;           * Make all times be current TOD, except for the "boottime" node.
         vap->va_ctime = vap->va_ctime;           * 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.
            */
           if (kfs->kfs_kt && kfs->kfs_kt->kt_namlen == 8 &&
               !memcmp(kfs->kfs_kt->kt_name, "boottime", 8)) {
                   TIMEVAL_TO_TIMESPEC(&boottime, &vap->va_ctime);
           } else {
                   TIMEVAL_TO_TIMESPEC(&time, &vap->va_ctime);
           }
           vap->va_atime = vap->va_mtime = vap->va_ctime;
         vap->va_gen = 0;          vap->va_gen = 0;
         vap->va_flags = 0;          vap->va_flags = 0;
         vap->va_rdev = 0;          vap->va_rdev = 0;
         /* vap->va_qbytes = 0; */  
         vap->va_bytes = 0;          vap->va_bytes = 0;
         vap->va_type = kt->kt_vtype;  
         vap->va_mode = kt->kt_perms;  
   
         if (vp->v_flag & VROOT) {          switch (kfs->kfs_type) {
 #ifdef KERNFS_DIAGNOSTIC          case KFSkern:
                 printf("kernfs_getattr: stat rootdir\n");  #ifdef IPSEC
 #endif                  vap->va_nlink = 4; /* 2 extra subdirs */
   #else
                 vap->va_nlink = 2;                  vap->va_nlink = 2;
                 vap->va_fileid = 2;  
                 vap->va_size = DEV_BSIZE;  
         } else {  
 #ifdef KERNFS_DIAGNOSTIC  
                 printf("kernfs_getattr: stat target %s\n", kt->kt_name);  
 #endif  #endif
                   vap->va_bytes = vap->va_size = DEV_BSIZE;
                   break;
   
           case KFSroot:
                 vap->va_nlink = 1;                  vap->va_nlink = 1;
                 vap->va_fileid = 3 + (kt - kernfs_targets) / sizeof(*kt);                  vap->va_bytes = vap->va_size = DEV_BSIZE;
                 error = kernfs_xread(kt, strbuf, sizeof(strbuf), &vap->va_size);                  break;
         }  
   
         vp->v_type = vap->va_type;          case KFSnull:
 #ifdef KERNFS_DIAGNOSTIC          case KFStime:
         printf("kernfs_getattr: return error %d\n", error);          case KFSint:
           case KFSstring:
           case KFShostname:
           case KFSavenrun:
           case KFSdevice:
           case KFSmsgbuf:
   #ifdef IPSEC
           case KFSipsecsa:
           case KFSipsecsp:
 #endif  #endif
         return (error);                  vap->va_nlink = 1;
 }                  total = 0;
                   do {
                           buf = strbuf;
 /*                          error = kernfs_xread(kfs, total, &buf,
  * Change the mode on a file.                              sizeof(strbuf), &nread);
  */                          total += nread;
 kernfs_chmod1(vp, mode, p)                  } while (error == 0 && nread != 0);
         register struct vnode *vp;                  vap->va_bytes = vap->va_size = total;
         register int mode;                  break;
         struct proc *p;  
 {  
         register struct ucred *cred = p->p_ucred;  
         register struct kernfs_target *kt = VTOKERN(vp)->kf_kt;  
         int error;  
   
         if ((mode & kt->kt_maxperms) != mode)   /* can't set ro var to rw */  
                 return (EPERM);  
   
         if (cred->cr_uid != kt->kt_uid &&  
             (error = suser(cred, &p->p_acflag)))  
                 return (error);  
         if (cred->cr_uid) {  
                 if (vp->v_type != VDIR && (mode & S_ISVTX))  
                         return (EFTYPE);  
                 if (!groupmember(kt->kt_gid, cred) && (mode & S_ISGID))  
                         return (EPERM);  
         }  
         kt->kt_perms &= ~07777;  
         kt->kt_perms |= mode & 07777;  
 /*      ip->i_flag |= ICHG;*/  
         return (0);  
 }  
   
 /*  
  * Perform chown operation on kernfs_target kt  
  */  
 kernfs_chown1(vp, uid, gid, p)  
         register struct vnode *vp;  
         uid_t uid;  
         gid_t gid;  
         struct proc *p;  
 {  
         register struct kernfs_target *kt = VTOKERN(vp)->kf_kt;  
         register struct ucred *cred = p->p_ucred;  
         uid_t ouid;  
         gid_t ogid;  
         int error = 0;  
   
         if (uid == (u_short)VNOVAL)  #ifdef IPSEC
                 uid = kt->kt_uid;          case KFSipsecsadir:
         if (gid == (u_short)VNOVAL)          case KFSipsecspdir:
                 gid = kt->kt_gid;                  vap->va_nlink = 2;
         /*                  vap->va_bytes = vap->va_size = DEV_BSIZE;
          * If we don't own the file, are trying to change the owner                  break;
          * of the file, or are not a member of the target group,  #endif
          * the caller must be superuser or the call fails.  
          */  
         if ((cred->cr_uid != kt->kt_uid || uid != kt->kt_uid ||  
             !groupmember((gid_t)gid, cred)) &&  
             (error = suser(cred, &p->p_acflag)))  
                 return (error);  
         ouid = kt->kt_uid;  
         ogid = kt->kt_gid;  
   
         kt->kt_uid = uid;          default:
         kt->kt_gid = gid;                  error = EINVAL;
                   break;
           }
   
 /*      if (ouid != uid || ogid != gid)          return (error);
                 ip->i_flag |= ICHG;*/  
         if (ouid != uid && cred->cr_uid != 0)  
                 kt->kt_perms &= ~S_ISUID;  
         if (ogid != gid && cred->cr_uid != 0)  
                 kt->kt_perms &= ~S_ISGID;  
         return (0);  
 }  }
   
 /*  /*ARGSUSED*/
  * Set attribute vnode op. called from several syscalls  int
  */  kernfs_setattr(v)
 kernfs_setattr(vp, vap, cred, p)          void *v;
         struct vnode *vp;  
         struct vattr *vap;  
         struct ucred *cred;  
         struct proc *p;  
 {  {
         int error = 0;  
   
         /*          /*
          * Check for unsetable attributes.           * Silently ignore attribute changes.
          */           * This allows for open with truncate to have no
         if ((vap->va_type != VNON) || (vap->va_nlink != VNOVAL) ||           * effect until some data is written.  I want to
             (vap->va_fsid != VNOVAL) || (vap->va_fileid != VNOVAL) ||           * do it this way because all writes are atomic.
             (vap->va_blocksize != VNOVAL) || (vap->va_rdev != VNOVAL) ||  
             ((int)vap->va_bytes != VNOVAL) || (vap->va_gen != VNOVAL)) {  
                 return (EINVAL);  
         }  
         /*  
          * Go through the fields and update iff not VNOVAL.  
          */           */
         if (vap->va_uid != (u_short)VNOVAL || vap->va_gid != (u_short)VNOVAL)          return (0);
                 if (error = kernfs_chown1(vp, vap->va_uid, vap->va_gid, p))  
                         return (error);  
         if (vap->va_size != VNOVAL) {  
                 if (vp->v_type == VDIR)  
                         return (EISDIR);  
                 /* else just nod and smile... */  
         }  
         if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {  
 /*              if (cred->cr_uid != ip->i_uid &&  
                     (error = suser(cred, &p->p_acflag)))  
                         return (error);  
                 if (vap->va_atime.tv_sec != VNOVAL)  
                         ip->i_flag |= IACC;  
                 if (vap->va_mtime.tv_sec != VNOVAL)  
                         ip->i_flag |= IUPD;  
                 ip->i_flag |= ICHG;  
                 if (error = iupdat(ip, &vap->va_atime, &vap->va_mtime, 1))  
                         return (error);  
 */  
         }  
         if (vap->va_mode != (u_short)VNOVAL)  
                 error = kernfs_chmod1(vp, (int)vap->va_mode, p);  
         if (vap->va_flags != VNOVAL) {  
 /*              if (cred->cr_uid != ip->i_uid &&  
                     (error = suser(cred, &p->p_acflag)))  
                         return (error);  
                 if (cred->cr_uid == 0) {  
                         ip->i_flags = vap->va_flags;  
                 } else {  
                         ip->i_flags &= 0xffff0000;  
                         ip->i_flags |= (vap->va_flags & 0xffff);  
                 }  
                 ip->i_flag |= ICHG;  
 */  
         }  
         return (error);  
 }  }
   
 static int  int
 kernfs_read(vp, uio, ioflag, cred)  kernfs_read(v)
         struct vnode *vp;          void *v;
         struct uio *uio;  
         int ioflag;  
         struct ucred *cred;  
 {  {
         struct kernfs_target *kt = VTOKERN(vp)->kf_kt;          struct vop_read_args /* {
         char strbuf[KSTRING];                  struct vnode *a_vp;
         int off = uio->uio_offset;                  struct uio *a_uio;
         int len = 0;                  int  a_ioflag;
         char *cp = strbuf;                  struct ucred *a_cred;
           } */ *ap = v;
           struct uio *uio = ap->a_uio;
           struct kernfs_node *kfs = VTOKERN(ap->a_vp);
           char strbuf[KSTRING], *buf;
           off_t off;
           size_t len;
         int error;          int error;
 #ifdef KERNFS_DIAGNOSTIC  
         printf("kern_read %s\n", kt->kt_name);  
 #endif  
   
         error = kernfs_xread(kt, strbuf, sizeof(strbuf), &len);          if (ap->a_vp->v_type == VDIR)
         if (error)                  return (EOPNOTSUPP);
                 return (error);  
         cp = strbuf + off;          off = uio->uio_offset;
         len -= off;          buf = strbuf;
         return (uiomove(cp, len, uio));          if ((error = kernfs_xread(kfs, off, &buf, sizeof(strbuf), &len)) == 0)
                   error = uiomove(buf, len, uio);
           return (error);
 }  }
   
 static int  int
 kernfs_write(vp, uio, ioflag, cred)  kernfs_write(v)
         struct vnode *vp;          void *v;
         struct uio *uio;  
         int ioflag;  
         struct ucred *cred;  
 {  {
         struct kernfs_target *kt = VTOKERN(vp)->kf_kt;          struct vop_write_args /* {
                   struct vnode *a_vp;
                   struct uio *a_uio;
                   int  a_ioflag;
                   struct ucred *a_cred;
           } */ *ap = v;
           struct kernfs_node *kfs = VTOKERN(ap->a_vp);
           struct uio *uio = ap->a_uio;
           int error, xlen;
         char strbuf[KSTRING];          char strbuf[KSTRING];
         int len = uio->uio_resid;  
         char *cp = strbuf;  
         int xlen;  
         int error;  
   
         if (uio->uio_offset != 0)          if (uio->uio_offset != 0)
                 return (EINVAL);                  return (EINVAL);
   
         xlen = min(uio->uio_resid, KSTRING-1);          xlen = min(uio->uio_resid, KSTRING-1);
         error = uiomove(strbuf, xlen, uio);          if ((error = uiomove(strbuf, xlen, uio)) != 0)
         if (error)  
                 return (error);                  return (error);
   
         if (uio->uio_resid != 0)          if (uio->uio_resid != 0)
                 return (EIO);                  return (EIO);
   
         strbuf[xlen] = '\0';          strbuf[xlen] = '\0';
         return (kernfs_xwrite(kt, strbuf, xlen));          xlen = strlen(strbuf);
           return (kernfs_xwrite(kfs, strbuf, xlen));
 }  }
   
 kernfs_readdir(vp, uio, cred, eofflagp, cookies, ncookies)  int
         struct vnode *vp;  kernfs_readdir(v)
         struct uio *uio;          void *v;
         struct ucred *cred;  
         int *eofflagp;  
         u_int *cookies;  
         int ncookies;  
 {  {
         struct filedesc *fdp;          struct vop_readdir_args /* {
         int i;                  struct vnode *a_vp;
                   struct uio *a_uio;
                   struct ucred *a_cred;
                   int *a_eofflag;
                   off_t **a_cookies;
                   int a_*ncookies;
           } */ *ap = v;
           struct uio *uio = ap->a_uio;
           struct dirent d;
           struct kernfs_node *kfs = VTOKERN(ap->a_vp);
           const struct kern_target *kt;
           off_t i;
         int error;          int error;
           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);
           if (uio->uio_offset < 0)
                   return (EINVAL);
   
         i = uio->uio_offset / UIO_MX;  
         error = 0;          error = 0;
         while (uio->uio_resid > 0 && (!cookies || ncookies > 0)) {          i = uio->uio_offset;
 #ifdef KERNFS_DIAGNOSTIC          memset(&d, 0, sizeof(d));
                 printf("kernfs_readdir: i = %d\n", i);          d.d_reclen = UIO_MX;
 #endif          ncookies = uio->uio_resid / UIO_MX;
                 if (i >= nkernfs_targets) {  
                         *eofflagp = 1;          switch (kfs->kfs_type) {
                         break;          case KFSkern:
                   if (i >= nkern_targets)
                           return (0);
   
                   if (ap->a_ncookies) {
                           ncookies = min(ncookies, (nkern_targets - i));
                           cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
                               M_WAITOK);
                           *ap->a_cookies = cookies;
                   }
   
                   n = 0;
                   for (; i < nkern_targets && uio->uio_resid >= UIO_MX; i++) {
                           kt = &kern_targets[i];
                           if (kt->kt_tag == KFSdevice) {
                                   dev_t *dp = kt->kt_data;
                                   struct vnode *fvp;
   
                                   if (*dp == NODEV ||
                                       !vfinddev(*dp, kt->kt_vtype, &fvp))
                                           continue;
                           }
                           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(&d, UIO_MX, uio)) != 0)
                                   break;
                           if (cookies)
                                   *cookies++ = i + 1;
                           n++;
                   }
                   ncookies = n;
                   break;
   
           case KFSroot:
                   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(&d, UIO_MX, uio)) != 0)
                                   break;
                           if (cookies)
                                   *cookies++ = i + 1;
                           n++;
                   }
                   ncookies = n;
                   break;
   
   #ifdef IPSEC
           case KFSipsecsadir:
                   /* 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++;
                 }                  }
                 {  
                         struct direct d;                  if (i >= nipsecsa_targets + n)
                         struct direct *dp = &d;                          return (0);
                         struct kernfs_target *kt = &kernfs_targets[i];  
                   if (ap->a_ncookies) {
                         bzero((caddr_t) dp, UIO_MX);                          ncookies = min(ncookies, (n - i));
                           cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
                         dp->d_namlen = strlen(kt->kt_name);                              M_WAITOK);
                         bcopy(kt->kt_name, dp->d_name, dp->d_namlen+1);                          *ap->a_cookies = cookies;
                   }
 #ifdef KERNFS_DIAGNOSTIC  
                         printf("kernfs_readdir: name = %s, len = %d\n",                  n = 0;
                                         dp->d_name, dp->d_namlen);                  for (; i < nipsecsa_targets && uio->uio_resid >= UIO_MX; i++) {
 #endif                          kt = &ipsecsa_targets[i];
                         /*                          d.d_namlen = kt->kt_namlen;
                          * Fill in the remaining fields                          d.d_fileno = KERNFS_FILENO(kt, kt->kt_tag, 0);
                          */                          memcpy(d.d_name, kt->kt_name, kt->kt_namlen + 1);
                         dp->d_reclen = UIO_MX;                          d.d_type = kt->kt_type;
                         dp->d_ino = i + 3;                          if ((error = uiomove(&d, UIO_MX, uio)) != 0)
                         /*  
                          * And ship to userland  
                          */  
                         error = uiomove((caddr_t) dp, UIO_MX, uio);  
                         if (error)  
                                 break;                                  break;
                         if (cookies) {                          if (cookies)
                                 *cookies = (i + 1) * UIO_MX;                                  *cookies++ = i + 1;
                                 ncookies--;                          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(&d, UIO_MX, uio)) != 0)
                                   break;
                           if (cookies)
                                   *cookies++ = i + 1;
                           n++;
                           i++;
                   }
                   ncookies = n;
                   break;
   
           case KFSipsecspdir:
                   /* count SP in the system */
                   n = 0;
                   TAILQ_FOREACH(sp, &sptailq, tailq)
                           n++;
   
                   if (i >= 2 + 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 < 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(&d, UIO_MX, uio)) != 0)
                                   break;
                           if (cookies)
                                   *cookies++ = i + 1;
                           n++;
                   }
                   if (error) {
                           ncookies = n;
                           break;
                 }                  }
                 i++;  
                   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(&d, UIO_MX, uio)) != 0)
                                   break;
                           if (cookies)
                                   *cookies++ = i + 1;
                           n++;
                           i++;
                   }
                   ncookies = n;
                   break;
   #endif
   
           default:
                   error = ENOTDIR;
                   break;
         }          }
   
         uio->uio_offset = i * UIO_MX;          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;
         return (error);          return (error);
 }  }
   
 kernfs_inactive(vp, p)  int
         struct vnode *vp;  kernfs_inactive(v)
         struct proc *p;          void *v;
 {  {
         /*          struct vop_inactive_args /* {
          * Clear out the v_type field to avoid                  struct vnode *a_vp;
          * nasty things happening in vgone().                  struct proc *a_p;
          */          } */ *ap = v;
         vp->v_type = VNON;          struct vnode *vp = ap->a_vp;
 #ifdef KERNFS_DIAGNOSTIC          const struct kernfs_node *kfs = VTOKERN(ap->a_vp);
         printf("kernfs_inactive(%x)\n", vp);  #ifdef IPSEC
           struct mbuf *m;
           struct secpolicy *sp;
   #endif
   
           VOP_UNLOCK(vp, 0);
           switch (kfs->kfs_type) {
   #ifdef IPSEC
           case KFSipsecsa:
                   m = key_setdumpsa_spi(htonl(kfs->kfs_value));
                   if (m)
                           m_freem(m);
                   else
                           vgone(vp);
                   break;
           case KFSipsecsp:
                   sp = key_getspbyid(kfs->kfs_value);
                   if (sp)
                           key_freesp(sp);
                   else {
                           /* should never happen as we hold a refcnt */
                           vgone(vp);
                   }
                   break;
 #endif  #endif
           default:
                   break;
           }
         return (0);          return (0);
 }  }
   
 /*  
  * Print out the contents of a kernfs vnode.  
  */  
 /* ARGSUSED */  
 int  int
 kernfs_print(vp)  kernfs_reclaim(v)
         struct vnode *vp;          void *v;
 {  {
         printf("tag VT_KERNFS, kernfs vnode\n");          struct vop_reclaim_args /* {
         return (0);                  struct vnode *a_vp;
 }          } */ *ap = v;
   
 /*          return (kernfs_freevp(ap->a_vp));
  * kernfs vnode unsupported operation  
  */  
 kernfs_enotsupp()  
 {  
         return (EOPNOTSUPP);  
 }  }
   
 /*  /*
  * kernfs "should never get here" operation   * Return POSIX pathconf information applicable to special devices.
  */   */
 kernfs_badop()  int
   kernfs_pathconf(v)
           void *v;
 {  {
         panic("kernfs: bad op");          struct vop_pathconf_args /* {
                   struct vnode *a_vp;
                   int a_name;
                   register_t *a_retval;
           } */ *ap = v;
   
           switch (ap->a_name) {
           case _PC_LINK_MAX:
                   *ap->a_retval = LINK_MAX;
                   return (0);
           case _PC_MAX_CANON:
                   *ap->a_retval = MAX_CANON;
                   return (0);
           case _PC_MAX_INPUT:
                   *ap->a_retval = MAX_INPUT;
                   return (0);
           case _PC_PIPE_BUF:
                   *ap->a_retval = PIPE_BUF;
                   return (0);
           case _PC_CHOWN_RESTRICTED:
                   *ap->a_retval = 1;
                   return (0);
           case _PC_VDISABLE:
                   *ap->a_retval = _POSIX_VDISABLE;
                   return (0);
           case _PC_SYNC_IO:
                   *ap->a_retval = 1;
                   return (0);
           default:
                   return (EINVAL);
           }
         /* NOTREACHED */          /* NOTREACHED */
 }  }
   
 /*  /*
  * kernfs vnode null operation   * Print out the contents of a /dev/fd vnode.
  */   */
 kernfs_nullop()  /* ARGSUSED */
   int
   kernfs_print(v)
           void *v;
 {  {
   
           printf("tag VT_KERNFS, kernfs vnode\n");
         return (0);          return (0);
 }  }
   
 #define kernfs_create ((int (*) __P(( \  int
                 struct nameidata *ndp, \  kernfs_link(v)
                 struct vattr *vap, \          void *v;
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_mknod ((int (*) __P(( \  
                 struct nameidata *ndp, \  
                 struct vattr *vap, \  
                 struct ucred *cred, \  
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_close ((int (*) __P(( \  
                 struct vnode *vp, \  
                 int fflag, \  
                 struct ucred *cred, \  
                 struct proc *p))) nullop)  
 #define kernfs_ioctl ((int (*) __P(( \  
                 struct vnode *vp, \  
                 int command, \  
                 caddr_t data, \  
                 int fflag, \  
                 struct ucred *cred, \  
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_select ((int (*) __P(( \  
                 struct vnode *vp, \  
                 int which, \  
                 int fflags, \  
                 struct ucred *cred, \  
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_mmap ((int (*) __P(( \  
                 struct vnode *vp, \  
                 int fflags, \  
                 struct ucred *cred, \  
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_fsync ((int (*) __P(( \  
                 struct vnode *vp, \  
                 int fflags, \  
                 struct ucred *cred, \  
                 int waitfor, \  
                 struct proc *p))) nullop)  
 #define kernfs_seek ((int (*) __P(( \  
                 struct vnode *vp, \  
                 off_t oldoff, \  
                 off_t newoff, \  
                 struct ucred *cred))) nullop)  
 #define kernfs_remove ((int (*) __P(( \  
                 struct nameidata *ndp, \  
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_link ((int (*) __P(( \  
                 struct vnode *vp, \  
                 struct nameidata *ndp, \  
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_rename ((int (*) __P(( \  
                 struct nameidata *fndp, \  
                 struct nameidata *tdnp, \  
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_mkdir ((int (*) __P(( \  
                 struct nameidata *ndp, \  
                 struct vattr *vap, \  
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_rmdir ((int (*) __P(( \  
                 struct nameidata *ndp, \  
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_symlink ((int (*) __P(( \  
                 struct nameidata *ndp, \  
                 struct vattr *vap, \  
                 char *target, \  
                 struct proc *p))) kernfs_enotsupp)  
 #define kernfs_readlink ((int (*) __P(( \  
                 struct vnode *vp, \  
                 struct uio *uio, \  
                 struct ucred *cred))) kernfs_enotsupp)  
 #define kernfs_abortop ((int (*) __P(( \  
                 struct nameidata *ndp))) nullop)  
 #ifdef KERNFS_DIAGNOSTIC  
 int kernfs_reclaim(vp)  
 struct vnode *vp;  
 {  {
         printf("kernfs_reclaim(%x)\n", vp);          struct vop_link_args /* {
         return (0);                  struct vnode *a_dvp;
                   struct vnode *a_vp;
                   struct componentname *a_cnp;
           } */ *ap = v;
   
           VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
           vput(ap->a_dvp);
           return (EROFS);
   }
   
   int
   kernfs_symlink(v)
           void *v;
   {
           struct vop_symlink_args /* {
                   struct vnode *a_dvp;
                   struct vnode **a_vpp;
                   struct componentname *a_cnp;
                   struct vattr *a_vap;
                   char *a_target;
           } */ *ap = v;
   
           VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
           vput(ap->a_dvp);
           return (EROFS);
 }  }
 #else  
 #define kernfs_reclaim ((int (*) __P(( \  
                 struct vnode *vp))) nullop)  
 #endif  
 #define kernfs_lock ((int (*) __P(( \  
                 struct vnode *vp))) nullop)  
 #define kernfs_unlock ((int (*) __P(( \  
                 struct vnode *vp))) nullop)  
 #define kernfs_bmap ((int (*) __P(( \  
                 struct vnode *vp, \  
                 daddr_t bn, \  
                 struct vnode **vpp, \  
                 daddr_t *bnp))) kernfs_badop)  
 #define kernfs_strategy ((int (*) __P(( \  
                 struct buf *bp))) kernfs_badop)  
 #define kernfs_islocked ((int (*) __P(( \  
                 struct vnode *vp))) nullop)  
 #define kernfs_advlock ((int (*) __P(( \  
                 struct vnode *vp, \  
                 caddr_t id, \  
                 int op, \  
                 struct flock *fl, \  
                 int flags))) kernfs_enotsupp)  
   
 struct vnodeops kernfs_vnodeops = {  
         kernfs_lookup,  /* lookup */  
         kernfs_create,  /* create */  
         kernfs_mknod,   /* mknod */  
         kernfs_open,    /* open */  
         kernfs_close,   /* close */  
         kernfs_access,  /* access */  
         kernfs_getattr, /* getattr */  
         kernfs_setattr, /* setattr */  
         kernfs_read,    /* read */  
         kernfs_write,   /* write */  
         kernfs_ioctl,   /* ioctl */  
         kernfs_select,  /* select */  
         kernfs_mmap,    /* mmap */  
         kernfs_fsync,   /* fsync */  
         kernfs_seek,    /* seek */  
         kernfs_remove,  /* remove */  
         kernfs_link,    /* link */  
         kernfs_rename,  /* rename */  
         kernfs_mkdir,   /* mkdir */  
         kernfs_rmdir,   /* rmdir */  
         kernfs_symlink, /* symlink */  
         kernfs_readdir, /* readdir */  
         kernfs_readlink,        /* readlink */  
         kernfs_abortop, /* abortop */  
         kernfs_inactive,        /* inactive */  
         kernfs_reclaim, /* reclaim */  
         kernfs_lock,    /* lock */  
         kernfs_unlock,  /* unlock */  
         kernfs_bmap,    /* bmap */  
         kernfs_strategy,        /* strategy */  
         kernfs_print,   /* print */  
         kernfs_islocked,        /* islocked */  
         kernfs_advlock, /* advlock */  
 };  

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.99

CVSweb <webmaster@jp.NetBSD.org>