[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.32 and 1.91

version 1.32, 1994/12/14 18:47:42 version 1.91, 2003/09/08 10:54:14
Line 15 
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 35 
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.
  *   *
  *      @(#)kernfs_vnops.c      8.9 (Berkeley) 6/15/94   *      @(#)kernfs_vnops.c      8.15 (Berkeley) 5/21/95
  */   */
   
 /*  /*
  * Kernel parameter filesystem (/kern)   * 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/vmmeter.h>  #include <sys/vmmeter.h>
 #include <sys/types.h>  
 #include <sys/time.h>  #include <sys/time.h>
 #include <sys/proc.h>  #include <sys/proc.h>
 #include <sys/vnode.h>  #include <sys/vnode.h>
Line 58 
Line 60 
 #include <sys/buf.h>  #include <sys/buf.h>
 #include <sys/dirent.h>  #include <sys/dirent.h>
 #include <sys/msgbuf.h>  #include <sys/msgbuf.h>
   
   #include <miscfs/genfs/genfs.h>
 #include <miscfs/kernfs/kernfs.h>  #include <miscfs/kernfs/kernfs.h>
   
   #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 KSTRING 256             /* Largest I/O available via this filesystem */
 #define UIO_MX 32  #define UIO_MX 32
   
 #define READ_MODE       (S_IRUSR|S_IRGRP|S_IROTH)  #define READ_MODE       (S_IRUSR|S_IRGRP|S_IROTH)
 #define WRITE_MODE      (S_IWUSR|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 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  #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 */       /*        name            data          tag           type  ro/rw */
      { DT_DIR, N("."),         0,            KTT_NULL,     VDIR, DIR_MODE   },       { DT_DIR, N("."),         0,            Pkern,        VDIR, DIR_MODE   },
      { DT_DIR, N(".."),        0,            KTT_NULL,     VDIR, DIR_MODE   },       { DT_DIR, N(".."),        0,            Proot,        VDIR, DIR_MODE   },
      { DT_REG, N("boottime"),  &boottime.tv_sec, KTT_INT,  VREG, READ_MODE  },       { DT_REG, N("boottime"),  &boottime.tv_sec, Pint,     VREG, READ_MODE  },
      { DT_REG, N("copyright"), copyright,    KTT_STRING,   VREG, READ_MODE  },                          /* XXX cast away const */
      { DT_REG, N("hostname"),  0,            KTT_HOSTNAME, VREG, WRITE_MODE },       { DT_REG, N("copyright"), (void *)copyright,
      { DT_REG, N("hz"),        &hz,          KTT_INT,      VREG, READ_MODE  },                                               Pstring,      VREG, READ_MODE  },
      { DT_REG, N("loadavg"),   0,            KTT_AVENRUN,  VREG, READ_MODE  },       { DT_REG, N("hostname"),  0,            Phostname,    VREG, WRITE_MODE },
      { DT_REG, N("msgbuf"),    0,            KTT_MSGBUF,   VREG, READ_MODE  },       { DT_REG, N("hz"),        &hz,          Pint,         VREG, READ_MODE  },
      { DT_REG, N("pagesize"),  &cnt.v_page_size, KTT_INT,  VREG, READ_MODE  },  #ifdef IPSEC
      { DT_REG, N("physmem"),   &physmem,     KTT_INT,      VREG, READ_MODE  },       { 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  #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,     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   },
   };
 #endif  #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  #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_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 }
 };  };
 static int nkern_targets = sizeof(kern_targets) / sizeof(kern_targets[0]);  const struct vnodeopv_desc kernfs_vnodeop_opv_desc =
           { &kernfs_vnodeop_p, kernfs_vnodeop_entries };
   
 int  static int
 kernfs_xread(kt, off, bufp, len)  kernfs_xread(kfs, off, bufp, len, wrlen)
         struct kern_target *kt;          struct kernfs_node *kfs;
         int off;          int off;
         char **bufp;          char **bufp;
         int len;          size_t len;
           size_t *wrlen;
 {  {
           const struct kern_target *kt;
   #ifdef IPSEC
           struct mbuf *m;
   #endif
   
         switch (kt->kt_tag) {          kt = kfs->kfs_kt;
         case KTT_TIME: {  
           switch (kfs->kfs_type) {
           case Ptime: {
                 struct timeval tv;                  struct timeval tv;
   
                 microtime(&tv);                  microtime(&tv);
                 sprintf(*bufp, "%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 Pint: {
                 int *ip = kt->kt_data;                  int *ip = kt->kt_data;
   
                 sprintf(*bufp, "%d\n", *ip);                  snprintf(*bufp, len, "%d\n", *ip);
                 break;                  break;
         }          }
   
         case KTT_STRING: {          case Pstring: {
                 char *cp = kt->kt_data;                  char *cp = kt->kt_data;
   
                 *bufp = cp;                  *bufp = cp;
                 break;                  break;
         }          }
   
         case KTT_MSGBUF: {          case Pmsgbuf: {
                 extern struct msgbuf *msgbufp;  
                 long n;                  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);                          return (0);
                   }
                 n = msgbufp->msg_bufx + off;                  n = msgbufp->msg_bufx + off;
                 if (n >= MSG_BSIZE)                  if (n >= msgbufp->msg_bufs)
                         n -= MSG_BSIZE;                          n -= msgbufp->msg_bufs;
                 len = min(MSG_BSIZE - n, MSG_BSIZE - off);                  len = min(msgbufp->msg_bufs - n, msgbufp->msg_bufs - off);
                 *bufp = msgbufp->msg_bufc + n;                  *bufp = msgbufp->msg_bufc + n;
                 return (len);                  *wrlen = len;
                   return (0);
         }          }
   
         case KTT_HOSTNAME: {          case Phostname: {
                 char *cp = hostname;                  char *cp = hostname;
                 int xlen = hostnamelen;                  int xlen = hostnamelen;
   
                 if (xlen >= (len-2))                  if (xlen >= (len - 2))
                         return (EINVAL);                          return (EINVAL);
   
                 bcopy(cp, *bufp, xlen);                  memcpy(*bufp, cp, xlen);
                 (*bufp)[xlen] = '\n';                  (*bufp)[xlen] = '\n';
                 (*bufp)[xlen+1] = '\0';                  (*bufp)[xlen+1] = '\0';
                   len = strlen(*bufp);
                 break;                  break;
         }          }
   
         case KTT_AVENRUN:          case Pavenrun:
                 averunnable.fscale = FSCALE;                  averunnable.fscale = FSCALE;
                 sprintf(*bufp, "%ld %ld %ld %ld\n",                  snprintf(*bufp, len, "%d %d %d %ld\n",
                     averunnable.ldavg[0], averunnable.ldavg[1],                      averunnable.ldavg[0], averunnable.ldavg[1],
                     averunnable.ldavg[2], averunnable.fscale);                      averunnable.ldavg[2], averunnable.fscale);
                 break;                  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:          default:
                   *wrlen = 0;
                 return (0);                  return (0);
         }          }
   
         len = strlen(*bufp);          len = strlen(*bufp);
         if (len <= off)          if (len <= off)
                 return (0);                  *wrlen = 0;
         *bufp += off;          else {
         return (len - off);                  *bufp += off;
                   *wrlen = len - off;
           }
           return (0);
 }  }
   
 int  static int
 kernfs_xwrite(kt, buf, len)  kernfs_xwrite(kfs, buf, len)
         struct kern_target *kt;          const struct kernfs_node *kfs;
         char *buf;          char *buf;
         int len;          size_t len;
 {  {
   
         switch (kt->kt_tag) {          switch (kfs->kfs_type) {
         case KTT_HOSTNAME:          case Phostname:
                 if (buf[len-1] == '\n')                  if (buf[len-1] == '\n')
                         --len;                          --len;
                 bcopy(buf, hostname, len);                  memcpy(hostname, buf, len);
                 hostname[len] = '\0';                  hostname[len] = '\0';
                 hostnamelen = len;                  hostnamelen = (size_t) len;
                 return (0);                  return (0);
   
         default:          default:
Line 210  kernfs_xwrite(kt, buf, len)
Line 416  kernfs_xwrite(kt, buf, len)
  * 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(ap)  int
   kernfs_lookup(v)
           void *v;
   {
         struct vop_lookup_args /* {          struct vop_lookup_args /* {
                 struct vnode * a_dvp;                  struct vnode * a_dvp;
                 struct vnode ** a_vpp;                  struct vnode ** a_vpp;
                 struct componentname * a_cnp;                  struct componentname * a_cnp;
         } */ *ap;          } */ *ap = v;
 {  
         struct componentname *cnp = ap->a_cnp;          struct componentname *cnp = ap->a_cnp;
         struct vnode **vpp = ap->a_vpp;          struct vnode **vpp = ap->a_vpp;
         struct vnode *dvp = ap->a_dvp;          struct vnode *dvp = ap->a_dvp;
         char *pname = cnp->cn_nameptr;          const char *pname = cnp->cn_nameptr;
         struct kern_target *kt;          const struct kernfs_node *kfs;
         struct vnode *fvp;          const struct kern_target *kt;
         int error, i;          int error, i, wantpunlock;
   #ifdef IPSEC
 #ifdef KERNFS_DIAGNOSTIC          char *ep;
         printf("kernfs_lookup(%x)\n", ap);          u_int32_t id;
         printf("kernfs_lookup(dp = %x, vpp = %x, cnp = %x)\n", dvp, vpp, ap->a_cnp);  
         printf("kernfs_lookup(%s)\n", pname);  
 #endif  #endif
   
           *vpp = NULLVP;
           cnp->cn_flags &= ~PDIRUNLOCK;
   
           if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)
                   return (EROFS);
   
         if (cnp->cn_namelen == 1 && *pname == '.') {          if (cnp->cn_namelen == 1 && *pname == '.') {
                 *vpp = dvp;                  *vpp = dvp;
                 VREF(dvp);                  VREF(dvp);
                 /*VOP_LOCK(dvp);*/  
                 return (0);                  return (0);
         }          }
   
 #if 0          wantpunlock = (~cnp->cn_flags & (LOCKPARENT | ISLASTCN));
         if (cnp->cn_namelen == 4 && bcmp(pname, "root", 4) == 0) {          kfs = VTOKERN(dvp);
                 *vpp = rootdir;          switch (kfs->kfs_type) {
                 VREF(rootdir);          case Pkern:
                 VOP_LOCK(rootdir);                  /*
                 return (0);                   * Shouldn't get here with .. in the root node.
         }                   */
 #endif                  if (cnp->cn_flags & ISDOTDOT)
                           return (EIO);
   
         *vpp = NULLVP;                  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 (error = ENOENT, kt = kern_targets, i = 0; i < nkern_targets;          found:
              kt++, i++) {                  error = kernfs_allocvp(dvp->v_mount, vpp, kt->kt_tag, kt, 0);
                 if (cnp->cn_namelen == kt->kt_namlen &&                  if ((error == 0) && wantpunlock) {
                     bcmp(kt->kt_name, pname, cnp->cn_namelen) == 0) {                          VOP_UNLOCK(dvp, 0);
                         error = 0;                          cnp->cn_flags |= PDIRUNLOCK;
                   }
                   return (error);
   
   #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);
                           }
                   }
   
                   ep = NULL;
                   id = strtoul(pname, &ep, 10);
                   if (!ep || *ep || ep == pname)
                         break;                          break;
   
                   error = kernfs_allocvp(dvp->v_mount, vpp, Pipsecsa, NULL, id);
                   if ((error == 0) && wantpunlock) {
                           VOP_UNLOCK(dvp, 0);
                           cnp->cn_flags |= PDIRUNLOCK;
                 }                  }
         }                  return (error);
   
 #ifdef KERNFS_DIAGNOSTIC          case Pipsecspdir:
         printf("kernfs_lookup: i = %d, error = %d\n", i, error);                  for (i = 0; i < nipsecsp_targets; i++) {
 #endif                          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;
   
         if (error)                  error = kernfs_allocvp(dvp->v_mount, vpp, Pipsecsp, NULL, id);
                   if ((error == 0) && wantpunlock) {
                           VOP_UNLOCK(dvp, 0);
                           cnp->cn_flags |= PDIRUNLOCK;
                   }
                 return (error);                  return (error);
   #endif
   
         if (kt->kt_tag == KTT_DEVICE) {          default:
                 dev_t *dp = kt->kt_data;                  return (ENOTDIR);
         loop:  
                 if (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp))  
                         return (ENOENT);  
                 *vpp = fvp;  
                 if (vget(fvp, 1))  
                         goto loop;  
                 return (0);  
         }          }
   
 #ifdef KERNFS_DIAGNOSTIC          return (cnp->cn_nameiop == LOOKUP ? ENOENT : EROFS);
         printf("kernfs_lookup: allocate new vnode\n");  }
   
   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  #endif
         if (error = getnewvnode(VT_KERNFS, dvp->v_mount, kernfs_vnodeop_p,  
             &fvp))  
                 return (error);  
   
         MALLOC(fvp->v_data, void *, sizeof(struct kernfs_node), M_TEMP,          switch (kfs->kfs_type) {
             M_WAITOK);  #ifdef IPSEC
         VTOKERN(fvp)->kf_kt = kt;          case Pipsecsa:
         fvp->v_type = kt->kt_vtype;                  m = key_setdumpsa_spi(htonl(kfs->kfs_value));
         *vpp = fvp;                  if (m) {
                           m_freem(m);
                           return (0);
                   } else
                           return (ENOENT);
   
 #ifdef KERNFS_DIAGNOSTIC          case Pipsecsp:
         printf("kernfs_lookup: newvp = %x\n", fvp);                  sp = key_getspbyid(kfs->kfs_value);
                   if (sp) {
                           kfs->kfs_v = sp;
                           return (0);
                   } else
                           return (ENOENT);
 #endif  #endif
         return (0);  
           default:
                   return (0);
           }
 }  }
   
 kernfs_open(ap)  int
         struct vop_open_args /* {  kernfs_close(v)
           void *v;
   {
           struct vop_close_args /* {
                 struct vnode *a_vp;                  struct vnode *a_vp;
                 int  a_mode;                  int a_fflag;
                 struct ucred *a_cred;                  struct ucred *a_cred;
                 struct proc *a_p;                  struct proc *a_p;
         } */ *ap;          } */ *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;
           }
   
         /* Only need to check access permissions. */  
         return (0);          return (0);
 }  }
   
 int  int
 kernfs_access(ap)  kernfs_access(v)
           void *v;
   {
         struct vop_access_args /* {          struct vop_access_args /* {
                 struct vnode *a_vp;                  struct vnode *a_vp;
                 int  a_mode;                  int a_mode;
                 struct ucred *a_cred;                  struct ucred *a_cred;
                 struct proc *a_p;                  struct proc *a_p;
         } */ *ap;          } */ *ap = v;
 {          struct vattr va;
         struct vnode *vp = ap->a_vp;          int error;
         struct ucred *cred = ap->a_cred;  
         mode_t amode = ap->a_mode;          if ((error = VOP_GETATTR(ap->a_vp, &va, ap->a_cred, ap->a_p)) != 0)
         mode_t fmode =                  return (error);
             (vp->v_flag & VROOT) ? DIR_MODE : VTOKERN(vp)->kf_kt->kt_mode;  
         mode_t mask = 0;          return (vaccess(va.va_type, va.va_mode, va.va_uid, va.va_gid,
         gid_t *gp;              ap->a_mode, ap->a_cred));
         int i;  
   
         /* Some files are simply not modifiable. */  
         if ((amode & VWRITE) && (fmode & (S_IWUSR|S_IWGRP|S_IWOTH)) == 0)  
                 return (EPERM);  
   
         /* Root can do anything else. */  
         if (cred->cr_uid == 0)  
                 return (0);  
   
         /* Check for group 0 (wheel) permissions. */  
         for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++)  
                 if (*gp == 0) {  
                         if (amode & VEXEC)  
                                 mask |= S_IXGRP;  
                         if (amode & VREAD)  
                                 mask |= S_IRGRP;  
                         if (amode & VWRITE)  
                                 mask |= S_IWGRP;  
                         return ((fmode & mask) == mask ?  0 : EACCES);  
                 }  
   
         /* Otherwise, check everyone else. */  
         if (amode & VEXEC)  
                 mask |= S_IXOTH;  
         if (amode & VREAD)  
                 mask |= S_IROTH;  
         if (amode & VWRITE)  
                 mask |= S_IWOTH;  
         return ((fmode & mask) == mask ? 0 : EACCES);  
 }  }
   
 kernfs_getattr(ap)  int
   kernfs_getattr(v)
           void *v;
   {
         struct vop_getattr_args /* {          struct vop_getattr_args /* {
                 struct vnode *a_vp;                  struct vnode *a_vp;
                 struct vattr *a_vap;                  struct vattr *a_vap;
                 struct ucred *a_cred;                  struct ucred *a_cred;
                 struct proc *a_p;                  struct proc *a_p;
         } */ *ap;          } */ *ap = v;
 {          struct kernfs_node *kfs = VTOKERN(ap->a_vp);
         struct vnode *vp = ap->a_vp;  
         struct vattr *vap = ap->a_vap;          struct vattr *vap = ap->a_vap;
         int error = 0;          int error = 0;
         char strbuf[KSTRING], *buf;          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_uid = 0;
         vap->va_gid = 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_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.  Avoid microtime(9), it's slow.
         vap->va_ctime = vap->va_ctime;           * 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_gen = 0;
         vap->va_flags = 0;          vap->va_flags = 0;
         vap->va_rdev = 0;          vap->va_rdev = 0;
         vap->va_bytes = 0;          vap->va_bytes = 0;
   
         if (vp->v_flag & VROOT) {          switch (kfs->kfs_type) {
 #ifdef KERNFS_DIAGNOSTIC          case Pkern:
                 printf("kernfs_getattr: stat rootdir\n");                  vap->va_nlink = 4;
 #endif                  vap->va_bytes = vap->va_size = DEV_BSIZE;
                 vap->va_type = VDIR;                  break;
                 vap->va_mode = DIR_MODE;  
                 vap->va_nlink = 2;          case Proot:
                 vap->va_fileid = 2;                  vap->va_nlink = 1;
                 vap->va_size = DEV_BSIZE;                  vap->va_bytes = vap->va_size = DEV_BSIZE;
         } else {                  break;
                 struct kern_target *kt = VTOKERN(vp)->kf_kt;  
                 int nbytes, total;          case Pnull:
 #ifdef KERNFS_DIAGNOSTIC          case Ptime:
                 printf("kernfs_getattr: stat target %s\n", kt->kt_name);          case Pint:
           case Pstring:
           case Phostname:
           case Pavenrun:
           case Pdevice:
           case Pmsgbuf:
   #ifdef IPSEC
           case Pipsecsa:
           case Pipsecsp:
 #endif  #endif
                 vap->va_type = kt->kt_vtype;  
                 vap->va_mode = kt->kt_mode;  
                 vap->va_nlink = 1;                  vap->va_nlink = 1;
                 vap->va_fileid = 1 + (kt - kern_targets) / sizeof(*kt);  
                 total = 0;                  total = 0;
                 while (buf = strbuf,                  do {
                        nbytes = kernfs_xread(kt, total, &buf, sizeof(strbuf)))                          buf = strbuf;
                         total += nbytes;                          error = kernfs_xread(kfs, total, &buf,
                 vap->va_size = total;                              sizeof(strbuf), &nread);
         }                          total += nread;
                   } while (error == 0 && nread != 0);
                   vap->va_bytes = vap->va_size = total;
                   break;
   
 #ifdef KERNFS_DIAGNOSTIC  #ifdef IPSEC
         printf("kernfs_getattr: return error %d\n", error);          case Pipsecsadir:
           case Pipsecspdir:
                   vap->va_nlink = 2;
                   vap->va_bytes = vap->va_size = DEV_BSIZE;
                   break;
 #endif  #endif
   
           default:
                   error = EINVAL;
                   break;
           }
   
         return (error);          return (error);
 }  }
   
 kernfs_setattr(ap)  /*ARGSUSED*/
         struct vop_setattr_args /* {  int
                 struct vnode *a_vp;  kernfs_setattr(v)
                 struct vattr *a_vap;          void *v;
                 struct ucred *a_cred;  
                 struct proc *a_p;  
         } */ *ap;  
 {  {
   
         /*          /*
Line 435  kernfs_setattr(ap)
Line 727  kernfs_setattr(ap)
 }  }
   
 int  int
 kernfs_read(ap)  kernfs_read(v)
           void *v;
   {
         struct vop_read_args /* {          struct vop_read_args /* {
                 struct vnode *a_vp;                  struct vnode *a_vp;
                 struct uio *a_uio;                  struct uio *a_uio;
                 int  a_ioflag;                  int  a_ioflag;
                 struct ucred *a_cred;                  struct ucred *a_cred;
         } */ *ap;          } */ *ap = v;
 {  
         struct vnode *vp = ap->a_vp;  
         struct uio *uio = ap->a_uio;          struct uio *uio = ap->a_uio;
         struct kern_target *kt;          struct kernfs_node *kfs = VTOKERN(ap->a_vp);
         char strbuf[KSTRING], *buf;          char strbuf[KSTRING], *buf;
         int off, len;          off_t off;
           size_t len;
         int error;          int error;
   
         if (vp->v_type == VDIR)          if (ap->a_vp->v_type == VDIR)
                 return (EOPNOTSUPP);                  return (EOPNOTSUPP);
   
         kt = VTOKERN(vp)->kf_kt;  
   
 #ifdef KERNFS_DIAGNOSTIC  
         printf("kern_read %s\n", kt->kt_name);  
 #endif  
   
         off = uio->uio_offset;          off = uio->uio_offset;
 #if 0          buf = strbuf;
         while (buf = strbuf,          if ((error = kernfs_xread(kfs, off, &buf, sizeof(strbuf), &len)) == 0)
 #else                  error = uiomove(buf, len, uio);
         if (buf = strbuf,          return (error);
 #endif  
             len = kernfs_xread(kt, off, &buf, sizeof(strbuf))) {  
                 if (error = uiomove(buf, len, uio))  
                         return (error);  
                 off += len;  
         }  
         return (0);  
 }  }
   
 int  int
 kernfs_write(ap)  kernfs_write(v)
           void *v;
   {
         struct vop_write_args /* {          struct vop_write_args /* {
                 struct vnode *a_vp;                  struct vnode *a_vp;
                 struct uio *a_uio;                  struct uio *a_uio;
                 int  a_ioflag;                  int  a_ioflag;
                 struct ucred *a_cred;                  struct ucred *a_cred;
         } */ *ap;          } */ *ap = v;
 {          struct kernfs_node *kfs = VTOKERN(ap->a_vp);
         struct vnode *vp = ap->a_vp;  
         struct uio *uio = ap->a_uio;          struct uio *uio = ap->a_uio;
         struct kern_target *kt;  
         int error, xlen;          int error, xlen;
         char strbuf[KSTRING];          char strbuf[KSTRING];
   
         if (vp->v_type == VDIR)  
                 return (EOPNOTSUPP);  
   
         kt = VTOKERN(vp)->kf_kt;  
   
         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);
         if (error = uiomove(strbuf, xlen, uio))          if ((error = uiomove(strbuf, xlen, uio)) != 0)
                 return (error);                  return (error);
   
         if (uio->uio_resid != 0)          if (uio->uio_resid != 0)
Line 505  kernfs_write(ap)
Line 780  kernfs_write(ap)
   
         strbuf[xlen] = '\0';          strbuf[xlen] = '\0';
         xlen = strlen(strbuf);          xlen = strlen(strbuf);
         return (kernfs_xwrite(kt, strbuf, xlen));          return (kernfs_xwrite(kfs, strbuf, xlen));
 }  }
   
 kernfs_readdir(ap)  int
   kernfs_readdir(v)
           void *v;
   {
         struct vop_readdir_args /* {          struct vop_readdir_args /* {
                 struct vnode *a_vp;                  struct vnode *a_vp;
                 struct uio *a_uio;                  struct uio *a_uio;
                 struct ucred *a_cred;                  struct ucred *a_cred;
                 int *a_eofflag;                  int *a_eofflag;
                 u_long *a_cookies;                  off_t **a_cookies;
                 int a_ncookies;                  int a_*ncookies;
         } */ *ap;          } */ *ap = v;
 {  
         struct uio *uio = ap->a_uio;          struct uio *uio = ap->a_uio;
         struct kern_target *kt;  
         struct dirent d;          struct dirent d;
         int i;          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 (ap->a_vp->v_type != VDIR)          if (uio->uio_resid < UIO_MX)
                 return (ENOTDIR);                  return (EINVAL);
           if (uio->uio_offset < 0)
         /*                  return (EINVAL);
          * We don't allow exporting kernfs mounts, and currently local  
          * requests do not need cookies.  
          */  
         if (ap->a_ncookies != NULL)  
                 panic("kernfs_readdir: not hungry");  
   
         i = uio->uio_offset / UIO_MX;  
         error = 0;          error = 0;
         for (kt = &kern_targets[i];          i = uio->uio_offset;
              uio->uio_resid >= UIO_MX && i < nkern_targets; kt++, i++) {          memset(&d, 0, sizeof(d));
                 struct dirent *dp = &d;          d.d_reclen = UIO_MX;
 #ifdef KERNFS_DIAGNOSTIC          ncookies = uio->uio_resid / UIO_MX;
                 printf("kernfs_readdir: i = %d\n", i);  
 #endif          switch (kfs->kfs_type) {
           case Pkern:
                   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];
                           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;
   
                 if (kt->kt_tag == KTT_DEVICE) {  #ifdef IPSEC
                         dev_t *dp = kt->kt_data;          case Pipsecsadir:
                         struct vnode *fvp;                  /* 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 (*dp == NODEV || !vfinddev(*dp, kt->kt_vtype, &fvp))                  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;                                  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;
   
                 bzero((caddr_t)dp, UIO_MX);          case Pipsecspdir:
                 dp->d_namlen = kt->kt_namlen;                  /* count SP in the system */
                 bcopy(kt->kt_name, dp->d_name, kt->kt_namlen+1);                  n = 0;
                   TAILQ_FOREACH(sp, &sptailq, tailq)
 #ifdef KERNFS_DIAGNOSTIC                          n++;
                 printf("kernfs_readdir: name = %s, len = %d\n",  
                                 dp->d_name, dp->d_namlen);                  if (i >= 2 + n)
 #endif                          return (0);
                 /*  
                  * Fill in the remaining fields                  if (ap->a_ncookies) {
                  */                          ncookies = min(ncookies, (n - i));
                 dp->d_reclen = UIO_MX;                          cookies = malloc(ncookies * sizeof(off_t), M_TEMP,
                 dp->d_fileno = i + 3;                              M_WAITOK);
                 dp->d_type = kt->kt_type;                          *ap->a_cookies = cookies;
                 /*                  }
                  * And ship to userland  
                  */                  n = 0;
                 if (error = uiomove((caddr_t)dp, UIO_MX, uio))                  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;                          break;
                   }
   
                   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;
         }          }
   
         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(ap)  int
   kernfs_inactive(v)
           void *v;
   {
         struct vop_inactive_args /* {          struct vop_inactive_args /* {
                 struct vnode *a_vp;                  struct vnode *a_vp;
         } */ *ap;                  struct proc *a_p;
 {          } */ *ap = v;
         struct vnode *vp = ap->a_vp;          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          VOP_UNLOCK(vp, 0);
         printf("kernfs_inactive(%x)\n", vp);          switch (kfs->kfs_type) {
   #ifdef IPSEC
           case Pipsecsa:
                   m = key_setdumpsa_spi(htonl(kfs->kfs_value));
                   if (m)
                           m_freem(m);
                   else
                           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  #endif
         /*          default:
          * Clear out the v_type field to avoid                  break;
          * nasty things happening in vgone().          }
          */  
         vp->v_type = VNON;  
         return (0);          return (0);
 }  }
   
 kernfs_reclaim(ap)  int
   kernfs_reclaim(v)
           void *v;
   {
         struct vop_reclaim_args /* {          struct vop_reclaim_args /* {
                 struct vnode *a_vp;                  struct vnode *a_vp;
         } */ *ap;          } */ *ap = v;
 {  
         struct vnode *vp = ap->a_vp;  
   
 #ifdef KERNFS_DIAGNOSTIC          return (kernfs_freevp(ap->a_vp));
         printf("kernfs_reclaim(%x)\n", vp);  
 #endif  
         if (vp->v_data) {  
                 FREE(vp->v_data, M_TEMP);  
                 vp->v_data = 0;  
         }  
         return (0);  
 }  }
   
 /*  /*
  * Return POSIX pathconf information applicable to special devices.   * Return POSIX pathconf information applicable to special devices.
  */   */
 kernfs_pathconf(ap)  int
   kernfs_pathconf(v)
           void *v;
   {
         struct vop_pathconf_args /* {          struct vop_pathconf_args /* {
                 struct vnode *a_vp;                  struct vnode *a_vp;
                 int a_name;                  int a_name;
                 register_t *a_retval;                  register_t *a_retval;
         } */ *ap;          } */ *ap = v;
 {  
   
         switch (ap->a_name) {          switch (ap->a_name) {
         case _PC_LINK_MAX:          case _PC_LINK_MAX:
Line 642  kernfs_pathconf(ap)
Line 1107  kernfs_pathconf(ap)
         case _PC_VDISABLE:          case _PC_VDISABLE:
                 *ap->a_retval = _POSIX_VDISABLE;                  *ap->a_retval = _POSIX_VDISABLE;
                 return (0);                  return (0);
           case _PC_SYNC_IO:
                   *ap->a_retval = 1;
                   return (0);
         default:          default:
                 return (EINVAL);                  return (EINVAL);
         }          }
Line 652  kernfs_pathconf(ap)
Line 1120  kernfs_pathconf(ap)
  * Print out the contents of a /dev/fd vnode.   * Print out the contents of a /dev/fd vnode.
  */   */
 /* ARGSUSED */  /* ARGSUSED */
 kernfs_print(ap)  int
         struct vop_print_args /* {  kernfs_print(v)
                 struct vnode *a_vp;          void *v;
         } */ *ap;  
 {  {
   
         printf("tag VT_KERNFS, kernfs vnode\n");          printf("tag VT_KERNFS, kernfs vnode\n");
         return (0);          return (0);
 }  }
   
 /*void*/  int
 kernfs_vfree(ap)  kernfs_link(v)
         struct vop_vfree_args /* {          void *v;
                 struct vnode *a_pvp;  
                 ino_t a_ino;  
                 int a_mode;  
         } */ *ap;  
 {  
   
         return (0);  
 }  
   
 /*  
  * /dev/fd vnode unsupported operation  
  */  
 kernfs_enotsupp()  
 {  
   
         return (EOPNOTSUPP);  
 }  
   
 /*  
  * /dev/fd "should never get here" operation  
  */  
 kernfs_badop()  
 {  {
           struct vop_link_args /* {
         panic("kernfs: bad op");                  struct vnode *a_dvp;
         /* NOTREACHED */                  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 vnode null operation  kernfs_symlink(v)
  */          void *v;
 kernfs_nullop()  
 {  {
           struct vop_symlink_args /* {
         return (0);                  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);
 }  }
   
 #define kernfs_create ((int (*) __P((struct  vop_create_args *)))kernfs_enotsupp)  
 #define kernfs_mknod ((int (*) __P((struct  vop_mknod_args *)))kernfs_enotsupp)  
 #define kernfs_close ((int (*) __P((struct  vop_close_args *)))nullop)  
 #define kernfs_ioctl ((int (*) __P((struct  vop_ioctl_args *)))kernfs_enotsupp)  
 #define kernfs_select ((int (*) __P((struct  vop_select_args *)))kernfs_enotsupp)  
 #define kernfs_mmap ((int (*) __P((struct  vop_mmap_args *)))kernfs_enotsupp)  
 #define kernfs_fsync ((int (*) __P((struct  vop_fsync_args *)))nullop)  
 #define kernfs_seek ((int (*) __P((struct  vop_seek_args *)))nullop)  
 #define kernfs_remove ((int (*) __P((struct  vop_remove_args *)))kernfs_enotsupp)  
 #define kernfs_link ((int (*) __P((struct  vop_link_args *)))kernfs_enotsupp)  
 #define kernfs_rename ((int (*) __P((struct  vop_rename_args *)))kernfs_enotsupp)  
 #define kernfs_mkdir ((int (*) __P((struct  vop_mkdir_args *)))kernfs_enotsupp)  
 #define kernfs_rmdir ((int (*) __P((struct  vop_rmdir_args *)))kernfs_enotsupp)  
 #define kernfs_symlink ((int (*) __P((struct vop_symlink_args *)))kernfs_enotsupp)  
 #define kernfs_readlink \  
         ((int (*) __P((struct  vop_readlink_args *)))kernfs_enotsupp)  
 #define kernfs_abortop ((int (*) __P((struct  vop_abortop_args *)))nullop)  
 #define kernfs_lock ((int (*) __P((struct  vop_lock_args *)))nullop)  
 #define kernfs_unlock ((int (*) __P((struct  vop_unlock_args *)))nullop)  
 #define kernfs_bmap ((int (*) __P((struct  vop_bmap_args *)))kernfs_badop)  
 #define kernfs_strategy ((int (*) __P((struct  vop_strategy_args *)))kernfs_badop)  
 #define kernfs_islocked ((int (*) __P((struct  vop_islocked_args *)))nullop)  
 #define kernfs_advlock ((int (*) __P((struct vop_advlock_args *)))kernfs_enotsupp)  
 #define kernfs_blkatoff \  
         ((int (*) __P((struct  vop_blkatoff_args *)))kernfs_enotsupp)  
 #define kernfs_valloc ((int(*) __P(( \  
                 struct vnode *pvp, \  
                 int mode, \  
                 struct ucred *cred, \  
                 struct vnode **vpp))) kernfs_enotsupp)  
 #define kernfs_truncate \  
         ((int (*) __P((struct  vop_truncate_args *)))kernfs_enotsupp)  
 #define kernfs_update ((int (*) __P((struct  vop_update_args *)))kernfs_enotsupp)  
 #define kernfs_bwrite ((int (*) __P((struct  vop_bwrite_args *)))kernfs_enotsupp)  
   
 int (**kernfs_vnodeop_p)();  
 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(*)())NULL }  
 };  
 struct vnodeopv_desc kernfs_vnodeop_opv_desc =  
         { &kernfs_vnodeop_p, kernfs_vnodeop_entries };  

Legend:
Removed from v.1.32  
changed lines
  Added in v.1.91

CVSweb <webmaster@jp.NetBSD.org>