openbsd-20021028
[openafs.git] / src / afs / afs_pioctl.c
index e9b108f..5710722 100644 (file)
@@ -8,15 +8,18 @@
  */
 
 #include <afsconfig.h>
-#include "../afs/param.h"
+#include "afs/param.h"
 
 RCSID("$Header$");
 
-#include "../afs/sysincludes.h"        /* Standard vendor system headers */
-#include "../afs/afsincludes.h"        /* Afs-based standard headers */
-#include "../afs/afs_stats.h"   /* afs statistics */
-#include "../afs/vice.h"
-#include "../rx/rx_globals.h"
+#include "afs/sysincludes.h"   /* Standard vendor system headers */
+#ifdef AFS_OBSD_ENV
+#include "h/syscallargs.h"
+#endif
+#include "afsincludes.h"       /* Afs-based standard headers */
+#include "afs/afs_stats.h"   /* afs statistics */
+#include "afs/vice.h"
+#include "rx/rx_globals.h"
 
 struct VenusFid afs_rootFid;
 afs_int32 afs_waitForever=0;
@@ -301,6 +304,9 @@ int HandleIoctl(register struct vcache *avc, register afs_int32 acom, struct afs
        default:
 
         code = EINVAL;
+#ifdef AFS_AIX51_ENV
+        code = ENOSYS;
+#endif
         break;
        }
        return code;            /* so far, none implemented */
@@ -374,16 +380,24 @@ afs_ioctl(OSI_VN_DECL(tvc), int cmd, void * arg, int flag, cred_t *cr, rval_t *r
 #ifndef AFS_HPUX102_ENV
 #if !defined(AFS_SGI_ENV)
 #ifdef AFS_AIX32_ENV
+#ifdef AFS_AIX51_ENV
+kioctl(fdes, com, arg, ext, arg2, arg3)
+     caddr_t arg2, arg3;
+#else
 kioctl(fdes, com, arg, ext)
+#endif
      int fdes, com;
      caddr_t arg, ext;
 {
   struct a {
     int fd, com;
     caddr_t arg, ext;
+#ifdef AFS_AIX51_ENV
+     caddr_t arg2, arg3;
+#endif
   } u_uap, *uap = &u_uap;
 #else
-#ifdef AFS_SUN5_ENV
+#if defined(AFS_SUN5_ENV)
 
 struct afs_ioctl_sys {
     int fd;
@@ -395,8 +409,7 @@ afs_xioctl (uap, rvp)
     struct afs_ioctl_sys *uap;
     rval_t *rvp;
 {
-#else
-#ifdef AFS_OSF_ENV
+#elif defined(AFS_OSF_ENV)
 afs_xioctl (p, args, retval)
         struct proc *p;
         void *args;
@@ -407,20 +420,20 @@ afs_xioctl (p, args, retval)
         u_long com;
         caddr_t arg;
     } *uap = (struct a *)args;
-#else /* AFS_OSF_ENV */
-#if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
+#elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
 struct ioctl_args {
-        int fd;
-        u_long com;
-        caddr_t arg;
+    int fd;
+    u_long com;
+    caddr_t arg;
 };
+
+int
 afs_xioctl(p, uap, retval) 
         struct proc *p; 
         register struct ioctl_args *uap;
         register_t *retval;
 {
-#else
-#ifdef AFS_LINUX22_ENV
+#elif defined(AFS_LINUX22_ENV)
 struct afs_ioctl_sys {
     unsigned int com;
     unsigned long arg;
@@ -431,52 +444,52 @@ asmlinkage int afs_xioctl(struct inode *ip, struct file *fp,
     struct afs_ioctl_sys ua, *uap = &ua;
 #else
 int afs_xioctl (void) 
-    {
-      register struct a {
+{
+    register struct a {
        int fd;
        int com;
        caddr_t arg;
-      } *uap = (struct a *)u.u_ap;
-#endif /* AFS_LINUX22_ENV */
-#endif /* AFS_DARWIN_ENV || AFS_FBSD_ENV */
-#endif /* AFS_OSF_ENV */
+    } *uap = (struct a *)u.u_ap;
 #endif /* AFS_SUN5_ENV */
 #endif
-#ifndef AFS_LINUX22_ENV
-#if    defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV)
-      struct file *fd;
-#else
-      register struct file *fd;
-#endif
+#if defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV)
+    struct file *fd;
+#elif !defined(AFS_LINUX22_ENV)
+    register struct file *fd;
 #endif
-#if defined(AFS_FBSD_ENV)
-      register struct filedesc *fdp;
+#if defined(AFS_XBSD_ENV)
+    register struct filedesc *fdp;
 #endif
-      register struct vcache *tvc;
-      register int ioctlDone = 0, code = 0;
+    register struct vcache *tvc;
+    register int ioctlDone = 0, code = 0;
       
-      AFS_STATCNT(afs_xioctl);
-#if defined(AFS_FBSD_ENV)
-        fdp=p->p_fd;
-        if ((u_int)uap->fd >= fdp->fd_nfiles ||
-            (fd = fdp->fd_ofiles[uap->fd]) == NULL)
-                return EBADF;
-        if ((fd->f_flag & (FREAD | FWRITE)) == 0)
-                return EBADF;
+    AFS_STATCNT(afs_xioctl);
+#if defined(AFS_XBSD_ENV)
+    fdp=p->p_fd;
+    if ((u_int)uap->fd >= fdp->fd_nfiles ||
+       (fd = fdp->fd_ofiles[uap->fd]) == NULL)
+       return EBADF;
+    if ((fd->f_flag & (FREAD | FWRITE)) == 0)
+       return EBADF;
 #else
 #if defined(AFS_DARWIN_ENV)
-        if ((code=fdgetf(p, uap->fd, &fd)))
-           return code;
+    if ((code=fdgetf(p, uap->fd, &fd)))
+       return code;
 #else
 #ifdef AFS_LINUX22_ENV
     ua.com = com;
     ua.arg = arg;
 #else
 #ifdef AFS_AIX32_ENV
-      uap->fd = fdes;
-      uap->com = com;
-      uap->arg = arg;
-      if (setuerror(getf(uap->fd, &fd))) {
+    uap->fd = fdes;
+    uap->com = com;
+    uap->arg = arg;
+#ifdef AFS_AIX51_ENV
+    uap->arg2 = arg2;
+    uap->arg3 = arg3;
+#endif
+
+    if (setuerror(getf(uap->fd, &fd))) {
        return -1;
     }
 #else
@@ -487,184 +500,183 @@ int afs_xioctl (void)
 #else   /* AFS_OSF_ENV */
 #ifdef AFS_SUN5_ENV
 #if defined(AFS_SUN57_ENV)
-       fd = getf(uap->fd);
-        if (!fd) return(EBADF);
+    fd = getf(uap->fd);
+    if (!fd) return(EBADF);
 #elif defined(AFS_SUN54_ENV)
-      fd = GETF(uap->fd);
-      if (!fd) return(EBADF);
+    fd = GETF(uap->fd);
+    if (!fd) return(EBADF);
 #else
-      if (code = getf(uap->fd, &fd)) {
-         return (code);
-      }
+    if (code = getf(uap->fd, &fd)) {
+       return (code);
+    }
 #endif
 #else
-      fd = getf(uap->fd);
-      if (!fd) return;
+    fd = getf(uap->fd);
+    if (!fd) return(EBADF);
 #endif
 #endif
 #endif
 #endif
 #endif
 #endif
-      /* first determine whether this is any sort of vnode */
-#ifdef AFS_LINUX22_ENV
-      tvc = VTOAFS(ip);
-      {
+    /* first determine whether this is any sort of vnode */
+#if defined(AFS_LINUX22_ENV)
+    tvc = VTOAFS(ip);
+    {
 #else
-#ifdef AFS_SUN5_ENV
-      if (fd->f_vnode->v_type == VREG || fd->f_vnode->v_type == VDIR) {
+#ifdef AFS_SUN5_ENV
+    if (fd->f_vnode->v_type == VREG || fd->f_vnode->v_type == VDIR) {
 #else
-      if (fd->f_type == DTYPE_VNODE) {
+    if (fd->f_type == DTYPE_VNODE) {
 #endif
        /* good, this is a vnode; next see if it is an AFS vnode */
 #if    defined(AFS_AIX32_ENV) || defined(AFS_SUN5_ENV)
        tvc = VTOAFS(fd->f_vnode);      /* valid, given a vnode */
+#elif defined(AFS_OBSD_ENV)
+       tvc = IsAfsVnode((struct vnode *) fd->f_data) ?
+           VTOAFS((struct vnode *) fd->f_data) : NULL;
 #else
        tvc = VTOAFS((struct vnode*)fd->f_data);        /* valid, given a vnode */
 #endif
 #endif /* AFS_LINUX22_ENV */
        if (tvc && IsAfsVnode(AFSTOV(tvc))) {
 #ifdef AFS_DEC_ENV
-         tvc = VTOAFS(afs_gntovn((struct gnode *) tvc));
-         if (!tvc) {   /* shouldn't happen with held gnodes */
-           u.u_error = ENOENT;
-           return;
-         }
-#endif
-         /* This is an AFS vnode */
-         if (((uap->com >> 8) & 0xff) == 'V') {
-           register struct afs_ioctl *datap;
-           AFS_GLOCK();
-           datap = (struct afs_ioctl *) osi_AllocSmallSpace(AFS_SMALLOCSIZ);
-           AFS_COPYIN((char *)uap->arg, (caddr_t) datap, sizeof (struct afs_ioctl), code);
-           if (code) {
-             osi_FreeSmallSpace(datap);
-             AFS_GUNLOCK();
-#if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
-              return code;
+           tvc = VTOAFS(afs_gntovn((struct gnode *) tvc));
+           if (!tvc) { /* shouldn't happen with held gnodes */
+               u.u_error = ENOENT;
+               return;
+           }
+#endif
+           /* This is an AFS vnode */
+           if (((uap->com >> 8) & 0xff) == 'V') {
+               register struct afs_ioctl *datap;
+               AFS_GLOCK();
+               datap = (struct afs_ioctl *) osi_AllocSmallSpace(AFS_SMALLOCSIZ);
+               AFS_COPYIN((char *)uap->arg, (caddr_t) datap, sizeof (struct afs_ioctl), code);
+               if (code) {
+                   osi_FreeSmallSpace(datap);
+                   AFS_GUNLOCK();
+#if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
+                   return code;
 #else 
 #if    defined(AFS_SUN5_ENV)
 #ifdef AFS_SUN54_ENV
-             releasef(uap->fd);
+                   releasef(uap->fd);
 #else
-             releasef(fd);
+                   releasef(fd);
 #endif
-             return (EFAULT);
+                   return (EFAULT);
 #else
 #ifdef AFS_OSF_ENV
 #ifdef AFS_OSF30_ENV
-             FP_UNREF_ALWAYS(fd);
+                   FP_UNREF_ALWAYS(fd);
 #else
-              FP_UNREF(fd);
+                   FP_UNREF(fd);
 #endif
-              return code;
+                   return code;
 #else  /* AFS_OSF_ENV */
 #ifdef AFS_AIX41_ENV
-             ufdrele(uap->fd);
+                   ufdrele(uap->fd);
 #endif
 #ifdef AFS_LINUX22_ENV
-             return -code;
+                   return -code;
 #else
-             setuerror(code);
-             return;
+                   setuerror(code);
+                   return;
 #endif
 #endif
 #endif
 #endif
-           }
-           code = HandleIoctl(tvc, uap->com, datap);
-           osi_FreeSmallSpace(datap);
-           AFS_GUNLOCK();
-           ioctlDone = 1;
+               }
+               code = HandleIoctl(tvc, uap->com, datap);
+               osi_FreeSmallSpace(datap);
+               AFS_GUNLOCK();
+               ioctlDone = 1;
 #ifdef AFS_AIX41_ENV
-           ufdrele(uap->fd);
+               ufdrele(uap->fd);
 #endif
 #ifdef AFS_OSF_ENV
 #ifdef AFS_OSF30_ENV
-             FP_UNREF_ALWAYS(fd);
+               FP_UNREF_ALWAYS(fd);
 #else
-              FP_UNREF(fd);
+               FP_UNREF(fd);
 #endif
 #endif
-         }
+           }
 #if defined(AFS_LINUX22_ENV)
-         else 
-           code = EINVAL;
+           else 
+               code = EINVAL;
 #endif
        }
-      }
+    }
 
-      if (!ioctlDone) {
+    if (!ioctlDone) {
 #ifdef AFS_AIX41_ENV
-         ufdrele(uap->fd);
-         code = okioctl(fdes, com, arg, ext);
-         return code;
+       ufdrele(uap->fd);
+#ifdef AFS_AIX51_ENV
+       code = okioctl(fdes, com, arg, ext, arg2, arg3);
 #else
-#ifdef AFS_AIX32_ENV
-         okioctl(fdes, com, arg, ext);
+       code = okioctl(fdes, com, arg, ext);
+#endif
+       return code;
 #else
-#if    defined(AFS_SUN5_ENV)
+#ifdef AFS_AIX32_ENV
+       okioctl(fdes, com, arg, ext);
+#elif defined(AFS_SUN5_ENV)
 #if defined(AFS_SUN57_ENV)
        releasef(uap->fd);
 #elif defined(AFS_SUN54_ENV)
-         RELEASEF(uap->fd);
+       RELEASEF(uap->fd);
 #else
-          releasef(fd);
+       releasef(fd);
 #endif
-          code = ioctl(uap, rvp);
-#else
-#if defined(AFS_FBSD_ENV)
+       code = ioctl(uap, rvp);
+#elif defined(AFS_FBSD_ENV)
         return ioctl(p, uap);
-#else
-#if defined(AFS_DARWIN_ENV) 
+#elif defined(AFS_OBSD_ENV)
+        code = sys_ioctl(p, uap, retval);
+#elif defined(AFS_DARWIN_ENV) 
         return ioctl(p, uap, retval);
-#else
-#ifdef  AFS_OSF_ENV
-         code = ioctl(p, args, retval);
+#elif defined(AFS_OSF_ENV)
+       code = ioctl(p, args, retval);
 #ifdef AFS_OSF30_ENV
-         FP_UNREF_ALWAYS(fd);
+       FP_UNREF_ALWAYS(fd);
 #else
-         FP_UNREF(fd);
-#endif
-         return code;
-#else   /* AFS_OSF_ENV */
-#ifndef AFS_LINUX22_ENV
-          ioctl();
-#endif
-#endif
-#endif
-#endif
+       FP_UNREF(fd);
 #endif
+       return code;
+#elif !defined(AFS_LINUX22_ENV)
+       ioctl();
 #endif
 #endif
-      }
+    }
 #ifdef AFS_SUN5_ENV
-      if (ioctlDone)
+    if (ioctlDone)
 #ifdef AFS_SUN54_ENV
-         releasef(uap->fd);
+       releasef(uap->fd);
 #else
-         releasef(fd);
+       releasef(fd);
 #endif
-      return (code);
+    return (code);
 #else
 #ifdef AFS_LINUX22_ENV
-      return -code;
+    return -code;
 #else
-#if defined(KERNEL_HAVE_SETUERROR)
-      if (!getuerror())
-         setuerror(code);
+#if defined(KERNEL_HAVE_UERROR)
+    if (!getuerror())
+       setuerror(code);
 #if    defined(AFS_AIX32_ENV) && !defined(AFS_AIX41_ENV)
-      return (getuerror() ? -1 : u.u_ioctlrv);
+    return (getuerror() ? -1 : u.u_ioctlrv);
 #else
-      return getuerror() ? -1 : 0;
+    return getuerror() ? -1 : 0;
 #endif
 #endif
 #endif /* AFS_LINUX22_ENV */
 #endif /* AFS_SUN5_ENV */
-#if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
-      return (code);
+#if defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
+    return (code);
 #endif
-    }
+}
 #endif /* AFS_SGI_ENV */
 #endif /* AFS_HPUX102_ENV */
   
@@ -712,7 +724,8 @@ afs_pioctl(p, args, retval)
 }
 
 #else  /* AFS_OSF_ENV */
-#if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
+#if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
+int
 afs_pioctl(p, args, retval)
         struct proc *p;
         void *args;
@@ -740,12 +753,13 @@ afs_pioctl(p, args, retval)
 #define PIOCTL_FREE_CRED()
 #endif
 
+int
 #ifdef AFS_SUN5_ENV
 afs_syscall_pioctl(path, com, cmarg, follow, rvp, credp)
     rval_t *rvp;
     struct AFS_UCRED *credp;
 #else
-#if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
+#if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
 afs_syscall_pioctl(path, com, cmarg, follow, credp)
     struct AFS_UCRED *credp;
 #else
@@ -753,7 +767,7 @@ afs_syscall_pioctl(path, com, cmarg, follow)
 #endif
 #endif
     char *path;
-    unsigned int       com;
+    unsigned int com;
     caddr_t cmarg;
     int        follow;
 {
@@ -768,7 +782,7 @@ afs_syscall_pioctl(path, com, cmarg, follow)
     struct ucred *credp = crref(); /* don't free until done! */
 #endif
 #ifdef AFS_LINUX22_ENV
-    cred_t *credp = crref(); /* don't free until done! */
+    cred_t *credp = crref();   /* don't free until done! */
     struct dentry *dp;
 
 #endif
@@ -777,51 +791,41 @@ afs_syscall_pioctl(path, com, cmarg, follow)
     code = copyin_afs_ioctl(cmarg, &data);
     if (code) {
        PIOCTL_FREE_CRED();
-#if defined(KERNEL_HAVE_SETUERROR)
+#if defined(KERNEL_HAVE_UERROR)
        setuerror(code);
 #endif
        return (code);
-  }
+    }
     if ((com & 0xff) == PSetClientContext) {
 #if defined(AFS_LINUX22_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
-       return EINVAL; /* Not handling these yet. */
-#else
-#if    defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_LINUX22_ENV)
+       return EINVAL;          /* Not handling these yet. */
+#elif  defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_LINUX22_ENV)
        code = HandleClientContext(&data, &com, &foreigncreds, credp);
 #else
-#if    defined(AFS_HPUX101_ENV)
-       code=HandleClientContext(&data, &com, &foreigncreds, p_cred(u.u_procp));
-#else
-#ifdef AFS_SGI_ENV
-       code = HandleClientContext(&data, &com, &foreigncreds, OSI_GET_CURRENT_CRED());
-#else
-       code = HandleClientContext(&data, &com, &foreigncreds, u.u_cred);
-#endif /* AFS_SGI_ENV */
-#endif
+       code = HandleClientContext(&data, &com, &foreigncreds, osi_curcred());
 #endif
-#endif
-      if (code) {
-         if (foreigncreds) {
-             crfree(foreigncreds);
-         }
-         PIOCTL_FREE_CRED();
-#if defined(KERNEL_HAVE_SETUERROR)
-         return (setuerror(code), code);
+       if (code) {
+           if (foreigncreds) {
+               crfree(foreigncreds);
+           }
+           PIOCTL_FREE_CRED();
+#if defined(KERNEL_HAVE_UERROR)
+           return (setuerror(code), code);
 #else
-         return (code);
+           return (code);
 #endif
-      }
+       }
     } 
 #if !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
     if (foreigncreds) {
-      /*
-       * We could have done without temporary setting the u.u_cred below
-       * (foreigncreds could be passed as param the pioctl modules)
-       * but calls such as afs_osi_suser() doesn't allow that since it
-       * references u.u_cred directly.  We could, of course, do something
-       * like afs_osi_suser(cred) which, I think, is better since it
-       * generalizes and supports multi cred environments...
-       */
+       /*
+        * We could have done without temporary setting the u.u_cred below
+        * (foreigncreds could be passed as param the pioctl modules)
+        * but calls such as afs_osi_suser() doesn't allow that since it
+        * references u.u_cred directly.  We could, of course, do something
+        * like afs_osi_suser(cred) which, I think, is better since it
+        * generalizes and supports multi cred environments...
+        */
 #ifdef AFS_SUN5_ENV
        tmpcred = credp;
        credp = foreigncreds;
@@ -838,63 +842,39 @@ afs_syscall_pioctl(path, com, cmarg, follow)
         tmpcred = OSI_GET_CURRENT_CRED();
         OSI_SET_CURRENT_CRED(foreigncreds);
 #else
+#ifdef AFS_OBSD_ENV
+        tmpcred = osi_curcred();
+        osi_curcred() = foreigncreds;
+#else
         tmpcred = u.u_cred;
         u.u_cred = foreigncreds;
 #endif /* AFS_SGI64_ENV */
 #endif /* AFS_HPUX101_ENV */
 #endif
 #endif
+#endif
     }
 #endif
     if ((com & 0xff) == 15) {
-      /* special case prefetch so entire pathname eval occurs in helper process.
-        otherwise, the pioctl call is essentially useless */
+       /* special case prefetch so entire pathname eval occurs in helper process.
+          otherwise, the pioctl call is essentially useless */
 #if    defined(AFS_SUN5_ENV) || defined(AFS_AIX41_ENV) || defined(AFS_LINUX22_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
        code =  Prefetch(path, &data, follow,
                         foreigncreds ? foreigncreds : credp);
 #else
-#if    defined(AFS_HPUX101_ENV)
-       code =  Prefetch(path, &data, follow, p_cred(u.u_procp));
-#else
-#ifdef AFS_SGI_ENV 
-       code =  Prefetch(path, &data, follow, OSI_GET_CURRENT_CRED());
-#else
-       code =  Prefetch(path, &data, follow, u.u_cred);
-#endif /* AFS_SGI64_ENV */
-#endif /* AFS_HPUX101_ENV */
+       code =  Prefetch(path, &data, follow, osi_curcred());
 #endif
-#if !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
-       if (foreigncreds) {
-#ifdef AFS_AIX41_ENV
-           crset(tmpcred);     /* restore original credentials */
-#else
-#if    defined(AFS_HPUX101_ENV)
-       set_p_cred(u.u_procp, tmpcred); /* restore original credentials */
-#else
-#ifndef        AFS_SUN5_ENV
-#ifdef AFS_SGI_ENV
-           OSI_SET_CURRENT_CRED(tmpcred);          /* restore original credentials */
-#else
-           u.u_cred = tmpcred;     /* restore original credentials */
-#endif
-#endif
-#endif /* AFS_HPUX101_ENV */
-           crfree(foreigncreds);
-#endif
-       }
-#endif /* AFS_LINUX22_ENV */
-       PIOCTL_FREE_CRED();
-#if defined(KERNEL_HAVE_SETUERROR)
-       return (setuerror(code), code);
-#else
-       return (code);
+       vp = NULL;
+#if defined(KERNEL_HAVE_UERROR)
+       setuerror(code);
 #endif
+       goto rescred;
     }
     if (path) {
        AFS_GUNLOCK();
 #ifdef AFS_AIX41_ENV
        code = lookupname(path, USR, follow, NULL, &vp,
-                       foreigncreds ? foreigncreds : credp);
+                         foreigncreds ? foreigncreds : credp);
 #else
 #ifdef AFS_LINUX22_ENV
        code = gop_lookupname(path, AFS_UIOUSER, follow,  NULL, &dp);
@@ -906,96 +886,75 @@ afs_syscall_pioctl(path, com, cmarg, follow)
 #endif /* AFS_AIX41_ENV */
        AFS_GLOCK();
        if (code) {
-#if !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
-           if (foreigncreds) {
-#ifdef AFS_AIX41_ENV
-               crset(tmpcred); /* restore original credentials */
-#else
-#if    defined(AFS_HPUX101_ENV)
-       set_p_cred(u.u_procp, tmpcred); /* restore original credentials */
-#else
-#if    !defined(AFS_SUN5_ENV)
-#ifdef AFS_SGI_ENV
-               OSI_SET_CURRENT_CRED(tmpcred);      /* restore original credentials */
-#else
-               u.u_cred = tmpcred;         /* restore original credentials */
-#endif /* AFS_SGI64_ENV */
-#endif
-#endif /* AFS_HPUX101_ENV */
-               crfree(foreigncreds);
-#endif
-           }
-#endif /* AFS_LINUX22_ENV */
-           PIOCTL_FREE_CRED();
-#if defined(KERNEL_HAVE_SETUERROR)
-           return(setuerror(code), code);
-#else
-           return (code);
+           vp = NULL;
+#if defined(KERNEL_HAVE_UERROR)
+           setuerror(code);
 #endif
+           goto rescred;
        }
     }
     else vp = NULL;
-    
+
     /* now make the call if we were passed no file, or were passed an AFS file */
     if (!vp || IsAfsVnode(vp)) {
 #ifdef AFS_DEC_ENV
-      /* Ultrix 4.0: can't get vcache entry unless we've got an AFS gnode.
-       * So, we must test in this part of the code.  Also, must arrange to
-       * GRELE the original gnode pointer when we're done, since in Ultrix 4.0,
-       * we hold gnodes, whose references hold our vcache entries.
-       */
-      if (vp) {
-       gp = vp;        /* remember for "put" */
-       vp = (struct vnode *) afs_gntovn(vp);   /* get vcache from gp */
-      }
-      else gp = NULL;
+       /* Ultrix 4.0: can't get vcache entry unless we've got an AFS gnode.
+        * So, we must test in this part of the code.  Also, must arrange to
+        * GRELE the original gnode pointer when we're done, since in Ultrix 4.0,
+        * we hold gnodes, whose references hold our vcache entries.
+        */
+       if (vp) {
+           gp = vp;            /* remember for "put" */
+           vp = (struct vnode *) afs_gntovn(vp); /* get vcache from gp */
+       }
+       else gp = NULL;
 #endif 
 #ifdef AFS_SUN5_ENV
-      code = afs_HandlePioctl(vp, com, &data, follow, &credp);
+       code = afs_HandlePioctl(vp, com, &data, follow, &credp);
 #else
 #ifdef AFS_AIX41_ENV
-      {
-         struct ucred *cred1, *cred2;
+    {
+       struct ucred *cred1, *cred2;
  
-         if (foreigncreds) {
-             cred1 = cred2 = foreigncreds;
-         } else {
-             cred1 = cred2 = credp;
-         }
-         code = afs_HandlePioctl(vp, com, &data, follow, &cred1);
-         if (cred1 != cred2) { 
-             /* something changed the creds */
-             crset(cred1);
-         }
-      }
+       if (foreigncreds) {
+           cred1 = cred2 = foreigncreds;
+       } else {
+           cred1 = cred2 = credp;
+       }
+       code = afs_HandlePioctl(vp, com, &data, follow, &cred1);
+       if (cred1 != cred2) { 
+           /* something changed the creds */
+           crset(cred1);
+       }
+    }
 #else
 #if    defined(AFS_HPUX101_ENV)
-      {
-         struct ucred *cred = p_cred(u.u_procp);
-         code = afs_HandlePioctl(vp, com, &data, follow, &cred);
-      }
+    {
+       struct ucred *cred = p_cred(u.u_procp);
+       code = afs_HandlePioctl(vp, com, &data, follow, &cred);
+    }
 #else
 #ifdef AFS_SGI_ENV
-      {
-      struct cred *credp;
-      credp = OSI_GET_CURRENT_CRED();
-      code = afs_HandlePioctl(vp, com, &data, follow, &credp);
-      }
+    {
+       struct cred *credp;
+       credp = OSI_GET_CURRENT_CRED();
+       code = afs_HandlePioctl(vp, com, &data, follow, &credp);
+    }
 #else
-#if defined(AFS_LINUX22_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
-      code = afs_HandlePioctl(vp, com, &data, follow, &credp);
+#if defined(AFS_LINUX22_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
+       code = afs_HandlePioctl(vp, com, &data, follow, &credp);
 #else
-      code = afs_HandlePioctl(vp, com, &data, follow, &u.u_cred);
+       code = afs_HandlePioctl(vp, com, &data, follow, &u.u_cred);
 #endif
 #endif /* AFS_SGI_ENV */
 #endif /* AFS_HPUX101_ENV */
 #endif /* AFS_AIX41_ENV */
 #endif /* AFS_SUN5_ENV */
     } else {
-#if defined(KERNEL_HAVE_SETUERROR)
+#if defined(KERNEL_HAVE_UERROR)
        setuerror(EINVAL);
 #else
-       code = EINVAL;  /* not in /afs */
+       code = EINVAL;          /* not in /afs */
 #endif
 #ifdef AFS_DEC_ENV
        if (vp) {
@@ -1005,35 +964,32 @@ afs_syscall_pioctl(path, com, cmarg, follow)
 #endif
     }
 
+ rescred:
 #if !defined(AFS_LINUX22_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
     if (foreigncreds) {
 #ifdef AFS_AIX41_ENV
-       crset(tmpcred);
+       crset(tmpcred); /* restore original credentials */
 #else
 #if    defined(AFS_HPUX101_ENV)
        set_p_cred(u.u_procp, tmpcred); /* restore original credentials */
-#else
-#ifndef        AFS_SUN5_ENV
-#ifdef AFS_SGI_ENV
-       OSI_SET_CURRENT_CRED(tmpcred);      /* restore original credentials */
-#else
-       u.u_cred = tmpcred;         /* restore original credentials */
-#endif /* ASF_SGI64_ENV */
-#endif
+#elif  defined(AFS_SGI_ENV)
+       OSI_SET_CURRENT_CRED(tmpcred); /* restore original credentials */
+#elif  !defined(AFS_SUN5_ENV)
+       osi_curcred() = tmpcred;        /* restore original credentials */
 #endif /* AFS_HPUX101_ENV */
        crfree(foreigncreds);
-#endif
+#endif /* AIX41 */
     }
-#endif /* AFS_LINUX22_ENV */
+#endif /* LINUX, DARWIN, FBSD */
     if (vp) {
 #ifdef AFS_LINUX22_ENV
        dput(dp);
 #else
-       AFS_RELE(vp);   /* put vnode back */
+       AFS_RELE(vp);           /* put vnode back */
 #endif
     }
     PIOCTL_FREE_CRED();
-#if defined(KERNEL_HAVE_SETUERROR)
+#if defined(KERNEL_HAVE_UERROR)
     if (!getuerror())  
        setuerror(code);
     return (getuerror());
@@ -1118,14 +1074,12 @@ int afs_HandlePioctl(struct vcache *avc, afs_int32 acom,
   
 DECL_PIOCTL(PGetFID)
 {
-    register afs_int32 code;
-    
     AFS_STATCNT(PGetFID);
     if (!avc) return EINVAL;
     memcpy(aout, (char *)&avc->fid, sizeof(struct VenusFid));
     *aoutSize = sizeof(struct VenusFid);
     return 0;
-  }
+}
   
 DECL_PIOCTL(PSetAcl)
 {
@@ -1360,6 +1314,7 @@ DECL_PIOCTL(PSetTokens)
     ain += sizeof(afs_int32);
     stp        = ain;  /* remember where the ticket is */
     if (i < 0 || i > 2000) return EINVAL;      /* malloc may fail */
+    if (i > MAXKTCTICKETLEN) return EINVAL;
     stLen = i;
     ain        += i;   /* skip over ticket */
     memcpy((char *)&i, ain, sizeof(afs_int32));
@@ -1392,12 +1347,12 @@ DECL_PIOCTL(PSetTokens)
     i = tcell->cellNum;
     afs_PutCell(tcell, READ_LOCK);
     if (set_parent_pag) {
-       int pag;
-#if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
+       afs_int32 pag;
+#if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
 #if defined(AFS_DARWIN_ENV)
-        struct proc *p=current_proc(); /* XXX */
+        struct proc *p = current_proc(); /* XXX */
 #else
-        struct proc *p=curproc; /* XXX */
+        struct proc *p = curproc; /* XXX */
 #endif
         uprintf("Process %d (%s) tried to change pags in PSetTokens\n",
                 p->p_pid, p->p_comm);
@@ -1954,7 +1909,7 @@ static int Prefetch(char *apath, struct afs_ioctl *adata, int afollow, struct AF
 {
     register char *tp;
     register afs_int32 code;
-#if defined(AFS_SGI61_ENV) || defined(AFS_SUN57_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
+#if defined(AFS_SGI61_ENV) || defined(AFS_SUN57_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
     size_t bufferSize;
 #else
     u_int bufferSize;
@@ -2068,7 +2023,7 @@ DECL_PIOCTL(PGetCacheSize)
 DECL_PIOCTL(PRemoveCallBack)
 {
     register struct conn *tc;
-    register afs_int32 code;
+    register afs_int32 code = 0;
     struct AFSCallBack CallBacks_Array[1];
     struct AFSCBFids theFids;
     struct AFSCBs theCBs;
@@ -2115,7 +2070,6 @@ DECL_PIOCTL(PNewCell)
 {
     /* create a new cell */
     afs_int32 cellHosts[MAXCELLHOSTS], *lp, magic=0;
-    register struct cell *tcell;
     char *newcell=0, *linkedcell=0, *tp= ain;
     register afs_int32 code, linkedstate=0, ls;
     u_short fsport = 0, vlport = 0;
@@ -2432,7 +2386,7 @@ DECL_PIOCTL(PFlushVolumeData)
 #if    defined(AFS_SGI_ENV) || defined(AFS_ALPHA_ENV)  || defined(AFS_SUN5_ENV)  || defined(AFS_HPUX_ENV) || defined(AFS_LINUX20_ENV)
                VN_HOLD(AFSTOV(tvc));
 #else
-#if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
+#if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
                osi_vnhold(tvc, 0);
 #else
                VREFCOUNT_INC(tvc);
@@ -2571,7 +2525,7 @@ DECL_PIOCTL(PSetSysName)
     AFS_STATCNT(PSetSysName);
     if (!afs_globalVFS) {
       /* Afsd is NOT running; disable it */
-#if defined(KERNEL_HAVE_SETUERROR)
+#if defined(KERNEL_HAVE_UERROR)
        return (setuerror(EINVAL), EINVAL);
 #else
        return (EINVAL);
@@ -2697,37 +2651,39 @@ static void *ReSortCells_cb(struct cell *cell, void *arg)
            ReleaseWriteLock(&cell->lock);
        }
     }
+
+    return NULL;
 }
 
 static void ReSortCells(int s, afs_int32 *l, int vlonly)  
 {
-  int i;
-  struct volume *j;
-  register int  k;
-
-  if (vlonly) {
-      afs_int32 *p;
-      p = (afs_int32 *) afs_osi_Alloc(sizeof(afs_int32) * (s+1));
-      p[0] = s;
-      memcpy(p+1, l, s * sizeof(afs_int32));
-      afs_TraverseCells(&ReSortCells_cb, p);
-      afs_osi_Free(p, sizeof(afs_int32) * (s+1));
-      return;
-  }
+    int i;
+    struct volume *j;
+    register int  k;
 
-  ObtainReadLock(&afs_xvolume);
-  for (i= 0; i< NVOLS; i++) {
-      for (j=afs_volumes[i];j;j=j->next) {
-         for (k=0;k<s;k++)
-             if (j->cell == l[k]) {
-                 ObtainWriteLock(&j->lock,233);
-                 afs_SortServers(j->serverHost, MAXHOSTS);
-                 ReleaseWriteLock(&j->lock);
-                 break; 
-             }
-      }
-  }
-  ReleaseReadLock(&afs_xvolume);
+    if (vlonly) {
+       afs_int32 *p;
+       p = (afs_int32 *) afs_osi_Alloc(sizeof(afs_int32) * (s+1));
+       p[0] = s;
+       memcpy(p+1, l, s * sizeof(afs_int32));
+       afs_TraverseCells(&ReSortCells_cb, p);
+       afs_osi_Free(p, sizeof(afs_int32) * (s+1));
+       return;
+    }
+
+    ObtainReadLock(&afs_xvolume);
+    for (i= 0; i< NVOLS; i++) {
+       for (j=afs_volumes[i];j;j=j->next) {
+           for (k=0;k<s;k++)
+               if (j->cell == l[k]) {
+                   ObtainWriteLock(&j->lock,233);
+                   afs_SortServers(j->serverHost, MAXHOSTS);
+                   ReleaseWriteLock(&j->lock);
+                   break; 
+               }
+       }
+    }
+    ReleaseReadLock(&afs_xvolume);
 }
 
 
@@ -2859,7 +2815,6 @@ DECL_PIOCTL(PGetSPrefs)
    int i,j;                   /* counters for hash table traversal */
    struct server *srvr;       /* one of CM's server structs */
    struct srvAddr *sa;
-   afs_uint32 prevh;
    int vlonly;                /* just return vlservers ? */
    int isfs;
    
@@ -3201,8 +3156,13 @@ static int HandleClientContext(struct afs_ioctl *ablob, int *com, struct AFS_UCR
     setuerror(0);      
 #endif
     newcred->cr_gid = RMTUSER_REQ;
+#ifdef AFS_AIX51_ENV
+    newcred->cr_groupset.gs_union.un_groups[0] = g0;
+    newcred->cr_groupset.gs_union.un_groups[1] = g1;
+#else
     newcred->cr_groups[0] = g0;
     newcred->cr_groups[1] = g1;
+#endif
 #ifdef AFS_AIX_ENV
     newcred->cr_ngrps = 2;
 #else
@@ -3477,7 +3437,6 @@ DECL_PIOCTL(PPrefetchFromTape)
     struct VenusFid tfid;
     struct AFSFid *Fid;
     struct vcache *tvc;
-    XSTATS_DECLS;
 
     AFS_STATCNT(PSetAcl);
     if (!avc)