flock-sysid-20030303
[openafs.git] / src / afs / VNOPS / afs_vnop_flock.c
index 7042b49..89f97b4 100644 (file)
  */
 
 #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" /* statistics */
-#include "../afs/afs_cbqueue.h"
-#include "../afs/nfsclient.h"
-#include "../afs/afs_osidnlc.h"
-
-#if    defined(AFS_HPUX102_ENV)
-#define AFS_FLOCK      k_flock
-#else
-#if    defined(AFS_SUN56_ENV) || defined(AFS_LINUX24_ENV)
-#define AFS_FLOCK       flock64
-#else
-#define AFS_FLOCK      flock
-#endif /* AFS_SUN65_ENV */
-#endif /* AFS_HPUX102_ENV */
+#include "afs/sysincludes.h"   /* Standard vendor system headers */
+#include "afsincludes.h"       /* Afs-based standard headers */
+#include "afs/afs_stats.h" /* statistics */
+#include "afs/afs_cbqueue.h"
+#include "afs/nfsclient.h"
+#include "afs/afs_osidnlc.h"
 
+/* Static prototypes */
+static int HandleGetLock(register struct vcache *avc,
+        register struct AFS_FLOCK *af, register struct vrequest *areq, int clid);
 static int GetFlockCount(struct vcache *avc, struct vrequest *areq);
+static int lockIdcmp2(struct AFS_FLOCK *flock1, struct vcache *vp, 
+        register struct SimpleLocks *alp, int onlymine, int clid);
+static void DoLockWarning(void);
 
-void lockIdSet(flock, slp, clid)
-   int clid;  /* non-zero on SGI, OSF, SunOS, Darwin, xBSD *//* XXX ptr type */
-    struct SimpleLocks *slp;
-    struct AFS_FLOCK *flock;
+/* int clid;  * non-zero on SGI, OSF, SunOS, Darwin, xBSD ** XXX ptr type */
+void lockIdSet(struct AFS_FLOCK *flock, struct SimpleLocks *slp, int clid)
 {
 #if    defined(AFS_SUN5_ENV)
     register proc_t *procp = ttoproc(curthread);    
 #else
-#if !defined(AFS_AIX41_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_SGI65_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
+#if !defined(AFS_AIX41_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_SGI65_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
 #ifdef AFS_SGI_ENV
     struct proc *procp = OSI_GET_CURRENT_PROCP();
 #else
@@ -84,7 +78,7 @@ void lockIdSet(flock, slp, clid)
 #endif
         slp->pid = clid;
 #else
-#if    defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
+#if    defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
        slp->pid = clid;
 #else
 #if defined(AFS_LINUX20_ENV) || defined(AFS_HPUX_ENV)
@@ -123,7 +117,7 @@ void lockIdSet(flock, slp, clid)
 #endif
         flock->l_pid = clid;
 #else
-#if    defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
+#if    defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
        flock->l_pid = clid;
 #else
 #if defined(AFS_LINUX20_ENV) || defined(AFS_HPUX_ENV)
@@ -146,19 +140,17 @@ void lockIdSet(flock, slp, clid)
  * to p_ppid?  Especially in the context of the lower loop, where
  * the repeated comparison doesn't make much sense...
  */
-static int lockIdcmp2(flock1, vp, alp, onlymine, clid)
-    struct AFS_FLOCK *flock1;
-    struct vcache *vp;
-    register struct SimpleLocks *alp;
-    int onlymine;  /* don't match any locks which are held by my */
-                  /* parent */
-     int clid; /* Only Irix 6.5 for now. */
+/* onlymine - don't match any locks which are held by my parent */
+/* clid - only irix 6.5 */
+
+static int lockIdcmp2(struct AFS_FLOCK *flock1, struct vcache *vp, 
+       register struct SimpleLocks *alp, int onlymine, int clid)
 {
     register struct SimpleLocks *slp;
 #if    defined(AFS_SUN5_ENV)
     register proc_t *procp = ttoproc(curthread);    
 #else
-#if !defined(AFS_AIX41_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_SGI65_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
+#if !defined(AFS_AIX41_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_SGI65_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
 #ifdef AFS_SGI64_ENV
     struct proc *procp = curprocp;
 #else /* AFS_SGI64_ENV */
@@ -166,7 +158,6 @@ static int lockIdcmp2(flock1, vp, alp, onlymine, clid)
 #endif /* AFS_SGI64_ENV */
 #endif
 #endif
-    int code = 0;
 
     if (alp) {
 #if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
@@ -178,7 +169,7 @@ static int lockIdcmp2(flock1, vp, alp, onlymine, clid)
 #if defined(AFS_AIX41_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_HPUX_ENV)
          (!onlymine && (flock1->l_pid == getppid()))
 #else
-#if defined(AFS_SGI65_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
+#if defined(AFS_SGI65_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
           /* XXX check this. used to be *only* irix for some reason. */
          (!onlymine && (flock1->l_pid == clid))
 #else
@@ -192,7 +183,7 @@ static int lockIdcmp2(flock1, vp, alp, onlymine, clid)
     }
 
     for (slp = vp->slocks; slp; slp = slp->next) {
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
        if (flock1->l_sysid != slp->sysid) {
          continue;
        }
@@ -219,12 +210,10 @@ static int lockIdcmp2(flock1, vp, alp, onlymine, clid)
     file, I guess we'll permit it.  however, we don't want simple,
     innocent closes by children to unlock files in the parent process.
 */
-HandleFlock(avc, acom, areq, clid, onlymine)
-    pid_t clid;        /* non-zero on SGI, SunOS, OSF1 only */
-    register struct vcache *avc;
-    struct vrequest *areq;
-    int onlymine; 
-    int acom; {
+/* clid - nonzero on sgi sunos osf1 only */
+int HandleFlock(register struct vcache *avc, int acom, 
+       struct vrequest *areq, pid_t clid, int onlymine)
+{
     struct conn *tc;
     struct SimpleLocks *slp, *tlp, **slpp;
     afs_int32 code;
@@ -235,7 +224,7 @@ HandleFlock(avc, acom, areq, clid, onlymine)
 
     AFS_STATCNT(HandleFlock);
     code = 0;          /* default when we don't make any network calls */
-    lockIdSet(&flock, (struct SimpleLocks *)0, clid);
+    lockIdSet(&flock, NULL, clid);
 
 #if defined(AFS_SGI_ENV)
     osi_Assert(valusema(&avc->vc_rwlock) <= 0);
@@ -253,69 +242,65 @@ HandleFlock(avc, acom, areq, clid, onlymine)
         */
        if ((avc->flockCount < 0) && (getpid() != avc->ownslock)) {
 #ifdef AFS_AIX41_ENV
-         if (onlymine || (getppid() != avc->ownslock)) {
+           if (onlymine || (getppid() != avc->ownslock)) {
 #else
-         if (onlymine || (u.u_procp->p_ppid != avc->ownslock)) {
+           if (onlymine || (u.u_procp->p_ppid != avc->ownslock)) {
 #endif
-           ReleaseWriteLock(&avc->lock);
-           return 0;
-         }
+               ReleaseWriteLock(&avc->lock);
+               return 0;
+           }
        }
 #endif
-       if (lockIdcmp2(&flock, avc, (struct SimpleLocks *)0, onlymine, clid)) {
-         ReleaseWriteLock(&avc->lock);             
-         return 0;
+       if (lockIdcmp2(&flock, avc, NULL, onlymine, clid)) {
+           ReleaseWriteLock(&avc->lock);           
+           return 0;
        } 
 #ifdef AFS_AIX_ENV
        avc->ownslock = 0; 
 #endif
        if (avc->flockCount == 0) {         
-         ReleaseWriteLock(&avc->lock);
-         return 0              /*ENOTTY*/;
-         /* no lock held */
+           ReleaseWriteLock(&avc->lock);
+           return 0            /*ENOTTY*/;
+           /* no lock held */
        }       
        /* unlock the lock */ 
        if (avc->flockCount > 0) {
-         slpp = &avc->slocks; 
-         for (slp = *slpp; slp;) {
-           if (!lockIdcmp2(&flock, avc, slp, onlymine, clid)) {
-             avc->flockCount--;
-             tlp = *slpp = slp->next;
-             osi_FreeSmallSpace(slp);
-             slp = tlp;
-           } else {
-             slpp = &slp->next;
-             slp = *slpp;
+           slpp = &avc->slocks; 
+           for (slp = *slpp; slp;) {
+               if (!lockIdcmp2(&flock, avc, slp, onlymine, clid)) {
+                   avc->flockCount--;
+                   tlp = *slpp = slp->next;
+                   osi_FreeSmallSpace(slp);
+                   slp = tlp;
+               } else {
+                   slpp = &slp->next;
+                   slp = *slpp;
+               }
            }
-         }
        }
        else if (avc->flockCount == -1) {
-         afs_StoreAllSegments(avc, areq, AFS_ASYNC); /* fsync file early */
-         avc->flockCount = 0; 
-         /* And remove the (only) exclusive lock entry from the list... */
-         osi_FreeSmallSpace(avc->slocks);
-         avc->slocks = 0;
+           afs_StoreAllSegments(avc, areq, AFS_ASYNC); /* fsync file early */
+           avc->flockCount = 0; 
+           /* And remove the (only) exclusive lock entry from the list... */
+           osi_FreeSmallSpace(avc->slocks);
+           avc->slocks = 0;
        }
        if (avc->flockCount == 0) {
            do {
                tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
                if (tc) {
-                  XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RELEASELOCK);
-#ifdef RX_ENABLE_LOCKS
-                  AFS_GUNLOCK();
-#endif /* RX_ENABLE_LOCKS */
-                  code = RXAFS_ReleaseLock(tc->id, (struct AFSFid *)
-                                           &avc->fid.Fid, &tsync);
-#ifdef RX_ENABLE_LOCKS
-                  AFS_GLOCK();
-#endif /* RX_ENABLE_LOCKS */
-                  XSTATS_END_TIME;
+                   XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RELEASELOCK);
+                   RX_AFS_GUNLOCK();
+                   code = RXAFS_ReleaseLock(tc->id, (struct AFSFid *)
+                                            &avc->fid.Fid, &tsync);
+                   RX_AFS_GLOCK();
+                   XSTATS_END_TIME;
                }
                else code = -1;
            } while
-             (afs_Analyze(tc, code, &avc->fid, areq, 
-                          AFS_STATS_FS_RPCIDX_RELEASELOCK,
-                           SHARED_LOCK, (struct cell *)0));
+               (afs_Analyze(tc, code, &avc->fid, areq, 
+                            AFS_STATS_FS_RPCIDX_RELEASELOCK,
+                            SHARED_LOCK, NULL));
        }
     }
     else {
@@ -359,25 +344,21 @@ HandleFlock(avc, acom, areq, clid, onlymine)
                        tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
                        if (tc) {
                          XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RELEASELOCK);
-#ifdef RX_ENABLE_LOCKS
-                         AFS_GUNLOCK();
-#endif /* RX_ENABLE_LOCKS */
+                         RX_AFS_GUNLOCK();
                          code = RXAFS_ReleaseLock(tc->id,
                                                   (struct AFSFid *) &avc->fid.Fid,
                                                   &tsync);
-#ifdef RX_ENABLE_LOCKS
-                         AFS_GLOCK();
-#endif /* RX_ENABLE_LOCKS */
+                         RX_AFS_GLOCK();
                          XSTATS_END_TIME;
                        }
                        else code = -1;
                    } while
                      (afs_Analyze(tc, code, &avc->fid, areq,
                                   AFS_STATS_FS_RPCIDX_RELEASELOCK,
-                                  SHARED_LOCK, (struct cell *)0));
+                                  SHARED_LOCK, NULL));
                }
            } else if (avc->flockCount == -1 && (acom & LOCK_EX)) {
-               if (lockIdcmp2(&flock, avc, (struct SimpleLocks *)0, 1, clid)) {
+               if (lockIdcmp2(&flock, avc, NULL, 1, clid)) {
                    code = EWOULDBLOCK;
                } else
                    code = 0;
@@ -394,21 +375,17 @@ HandleFlock(avc, acom, areq, clid, onlymine)
                        tc = afs_Conn(&avc->fid, areq, SHARED_LOCK);
                        if (tc) {
                          XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SETLOCK);
-#ifdef RX_ENABLE_LOCKS
-                         AFS_GUNLOCK();
-#endif /* RX_ENABLE_LOCKS */
+                         RX_AFS_GUNLOCK();
                          code = RXAFS_SetLock(tc->id, (struct AFSFid *)
                                              &avc->fid.Fid, lockType, &tsync);
-#ifdef RX_ENABLE_LOCKS
-                         AFS_GLOCK();
-#endif /* RX_ENABLE_LOCKS */
+                         RX_AFS_GLOCK();
                          XSTATS_END_TIME;
                        }
                        else code = -1;
                    } while
                        (afs_Analyze(tc, code, &avc->fid, areq,
                                     AFS_STATS_FS_RPCIDX_SETLOCK,
-                                    SHARED_LOCK, (struct cell *)0));
+                                    SHARED_LOCK, NULL));
                }
                else code = 0;  /* otherwise, pretend things worked */
            }
@@ -424,7 +401,7 @@ HandleFlock(avc, acom, areq, clid, onlymine)
 #endif
 
                    slp->type = LockWrite;
-                   slp->next = (struct SimpleLocks *)0;                    
+                   slp->next = NULL;               
                    avc->slocks = slp;
                    avc->flockCount = -1;
                } else {
@@ -464,8 +441,9 @@ HandleFlock(avc, acom, areq, clid, onlymine)
 
 
 /* warn a user that a lock has been ignored */
-afs_int32 lastWarnTime = 0;
-static void DoLockWarning() {
+afs_int32 lastWarnTime = 0; /* this is used elsewhere */
+static void DoLockWarning(void)
+{
     register afs_int32 now;
     now = osi_Time();
 
@@ -480,34 +458,37 @@ static void DoLockWarning() {
 
 
 #ifdef AFS_OSF_ENV
-afs_lockctl(avc, af, flag, acred, clid, offset)
-struct eflock *af;
-int flag;
-pid_t clid;
-off_t offset;
+afs_lockctl(struct vcache *avc, struct eflock *af, int flag, 
+       struct AFS_UCRED *acred, pid_t clid, off_t offset)
 #else
-#if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
-afs_lockctl(avc, af, acmd, acred, clid)
-pid_t clid;
+#if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
+afs_lockctl(struct vcache *avc, struct AFS_FLOCK *af, int acmd, struct AFS_UCRED *acred, pid_t clid)
 #else
 u_int clid=0;
-afs_lockctl(avc, af, acmd, acred)
+afs_lockctl(struct vcache *avc, struct AFS_FLOCK *af, int acmd, struct AFS_UCRED *acred)
 #endif
-struct AFS_FLOCK *af;
-int acmd;
 #endif
-struct vcache *avc;
-struct AFS_UCRED *acred; {
+{
     struct vrequest treq;
     afs_int32 code;
 #ifdef AFS_OSF_ENV
     int acmd = 0;
 #endif
+    struct afs_fakestat_state fakestate;
 
     AFS_STATCNT(afs_lockctl);
-    if (code = afs_InitReq(&treq, acred)) return code;
+    if ((code = afs_InitReq(&treq, acred))) return code;
+    afs_InitFakeStat(&fakestate);
+    code = afs_EvalFakeStat(&avc, &fakestate, &treq);
+    if (code) {
+       afs_PutFakeStat(&fakestate);
+       return code;
+    }
 #ifdef AFS_OSF_ENV
-    if (flag & VNOFLCK)        return 0;
+    if (flag & VNOFLCK)        {
+       afs_PutFakeStat(&fakestate);
+       return 0;
+    }
     if (flag & CLNFLCK) {
        acmd = LOCK_UN;
     } else if ((flag & GETFLCK) || (flag & RGETFLCK)) {
@@ -521,12 +502,15 @@ struct AFS_UCRED *acred; {
 #else
     if (acmd == F_GETLK) {
 #endif
-       if (af->l_type == F_UNLCK)
+       if (af->l_type == F_UNLCK) {
+           afs_PutFakeStat(&fakestate);
            return 0;
+       }
 #ifndef        AFS_OSF_ENV     /* getlock is a no-op for osf (for now) */
        code = HandleGetLock(avc, af, &treq, clid);
 #endif
        code = afs_CheckCode(code, &treq, 2); /* defeat buggy AIX optimz */
+       afs_PutFakeStat(&fakestate);
        return code;
     }
     else if ((acmd == F_SETLK) || (acmd == F_SETLKW) 
@@ -554,13 +538,17 @@ struct AFS_UCRED *acred; {
           even when they should block */
        if (af->l_whence != 0 || af->l_start != 0 || af->l_len != 0) {
            DoLockWarning();
+           afs_PutFakeStat(&fakestate);
            return 0;
        }
        /* otherwise we can turn this into a whole-file flock */
        if (af->l_type == F_RDLCK) code = LOCK_SH;
        else if (af->l_type == F_WRLCK) code = LOCK_EX;
        else if (af->l_type == F_UNLCK) code = LOCK_UN;
-       else return EINVAL; /* unknown lock type */
+       else {
+           afs_PutFakeStat(&fakestate);
+           return EINVAL; /* unknown lock type */
+       }
        if (((acmd == F_SETLK) 
 #if    (defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV)) && !defined(AFS_SUN58_ENV)
        || (acmd == F_RSETLK) 
@@ -579,8 +567,10 @@ struct AFS_UCRED *acred; {
 #endif
 #endif
        code = afs_CheckCode(code, &treq, 3); /* defeat AIX -O bug */
+       afs_PutFakeStat(&fakestate);
        return code;
     }
+    afs_PutFakeStat(&fakestate);
     return EINVAL;
 }
 
@@ -597,16 +587,13 @@ struct AFS_UCRED *acred; {
  *       PID has the file read locked.
  */
 #ifndef        AFS_OSF_ENV     /* getlock is a no-op for osf (for now) */
-HandleGetLock(avc, af, areq, clid)
-    int clid;           /* not used by some OSes */
-    register struct vcache *avc;
-    register struct vrequest *areq;
-    register struct AFS_FLOCK *af; 
+static int HandleGetLock(register struct vcache *avc, 
+       register struct AFS_FLOCK *af, register struct vrequest *areq, int clid)
 {
     register afs_int32 code;
     struct AFS_FLOCK flock;
 
-    lockIdSet(&flock, (struct SimpleLocks *)0, clid);
+    lockIdSet(&flock, NULL, clid);
 
     ObtainWriteLock(&avc->lock,122);
     if (avc->flockCount == 0) {
@@ -624,7 +611,7 @@ HandleGetLock(avc, af, areq, clid)
            af->l_type = F_WRLCK;
 
        af->l_pid = 0;
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
        af->l_sysid = 0;
 #endif
        goto done;
@@ -637,7 +624,7 @@ HandleGetLock(avc, af, areq, clid)
         * write lock, say it is unlocked.
         */
        if (avc->flockCount > 0 ||      /* only read locks */
-           !lockIdcmp2(&flock, avc, (struct SimpleLocks *)0, 1, clid)) {
+           !lockIdcmp2(&flock, avc, NULL, 1, clid)) {
            af->l_type = F_UNLCK;
            goto unlck_leave;
        }
@@ -646,12 +633,12 @@ HandleGetLock(avc, af, areq, clid)
        af->l_type = F_WRLCK;           /* not us, so lock would block */
        if (avc->slocks) {              /* we know who, so tell */
            af->l_pid = avc->slocks->pid;
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
            af->l_sysid = avc->slocks->sysid;
 #endif
        } else {
            af->l_pid = 0;      /* XXX can't happen?? */
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
            af->l_sysid = 0;
 #endif
        }
@@ -663,16 +650,16 @@ HandleGetLock(avc, af, areq, clid)
      * already, and it is not this process, we fail.
      */
     if (avc->flockCount < 0) {
-       if (lockIdcmp2(&flock, avc, (struct SimpleLocks *)0, 1, clid)) {
+       if (lockIdcmp2(&flock, avc, NULL, 1, clid)) {
            af->l_type = F_WRLCK;
            if (avc->slocks) {
                af->l_pid = avc->slocks->pid;
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
                af->l_sysid = avc->slocks->sysid;
 #endif
            } else {
                af->l_pid = 0;  /* XXX can't happen?? */
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
                af->l_sysid = 0;
 #endif
            }
@@ -688,19 +675,19 @@ HandleGetLock(avc, af, areq, clid)
      * If there is more than one, or it isn't us, we cannot lock.
      */
     if ((avc->flockCount > 1)
-       || lockIdcmp2(&flock, avc, (struct SimpleLocks *)0, 1, clid)) {
+       || lockIdcmp2(&flock, avc, NULL, 1, clid)) {
        struct SimpleLocks *slp;
        
        af->l_type = F_RDLCK;
        af->l_pid = 0;
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
        af->l_sysid = 0;
 #endif
        /* find a pid that isn't our own */
        for (slp = avc->slocks; slp; slp = slp->next) {
-           if (lockIdcmp2(&flock, (struct vcache *)0, slp, 1, clid)) {
+           if (lockIdcmp2(&flock, NULL, slp, 1, clid)) {
                af->l_pid = slp->pid;
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
                af->l_sysid = avc->slocks->sysid;
 #endif
                break;
@@ -714,16 +701,16 @@ HandleGetLock(avc, af, areq, clid)
        * already, and it is not this process, we fail.
        */
        if (avc->flockCount < 0) {
-           if (lockIdcmp2(&flock, avc, (struct SimpleLocks *)0, 1, clid)) {
+           if (lockIdcmp2(&flock, avc, NULL, 1, clid)) {
                af->l_type = F_WRLCK;
                if (avc->slocks) {
                    af->l_pid = avc->slocks->pid;
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
                    af->l_sysid = avc->slocks->sysid;
 #endif
                } else {
                    af->l_pid = 0;  /* XXX can't happen?? */
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
                    af->l_sysid = 0;
 #endif
                }
@@ -739,18 +726,18 @@ HandleGetLock(avc, af, areq, clid)
         * If there is more than one, or it isn't us, we cannot lock.
         */
        if ((avc->flockCount > 1)
-           || lockIdcmp2(&flock, avc, (struct SimpleLocks *)0, 1, clid)) {
+           || lockIdcmp2(&flock, avc, NULL, 1, clid)) {
            struct SimpleLocks *slp;
            af->l_type = F_RDLCK;
            af->l_pid = 0;
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
            af->l_sysid = 0;
 #endif
            /* find a pid that isn't our own */
            for (slp = avc->slocks; slp; slp = slp->next) {
-               if (lockIdcmp2(&flock, (struct vcache *)0, slp, 1, clid)) {
+               if (lockIdcmp2(&flock, NULL, slp, 1, clid)) {
                    af->l_pid = slp->pid;
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
                    af->l_sysid = avc->slocks->sysid;
 #endif
                    break;
@@ -774,7 +761,7 @@ HandleGetLock(avc, af, areq, clid)
        else
            af->l_type = F_WRLCK;
        af->l_pid = 0;
-#if    defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
+#if defined(AFS_HAVE_FLOCK_SYSID)
        af->l_sysid = 0;
 #endif
 
@@ -811,20 +798,16 @@ static int GetFlockCount(struct vcache *avc, struct vrequest *areq)
        tc = afs_Conn(&avc->fid, areq, SHARED_LOCK); 
        if (tc){
           XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
-#ifdef RX_ENABLE_LOCKS
-         AFS_GUNLOCK();
-#endif /* RX_ENABLE_LOCKS */
+         RX_AFS_GUNLOCK();
          code = RXAFS_FetchStatus(tc->id, (struct AFSFid *) &avc->fid.Fid,
                                     &OutStatus, &CallBack, &tsync);
-#ifdef RX_ENABLE_LOCKS
-         AFS_GLOCK();
-#endif /* RX_ENABLE_LOCKS */
+         RX_AFS_GLOCK();
           XSTATS_END_TIME;
        } else code = -1;
     } while
       (afs_Analyze(tc, code, &avc->fid, areq,
                   AFS_STATS_FS_RPCIDX_FETCHSTATUS,
-                  SHARED_LOCK, (struct cell *)0));
+                  SHARED_LOCK, NULL));
 
     if (temp)
        areq->flags &= ~O_NONBLOCK;
@@ -838,18 +821,16 @@ static int GetFlockCount(struct vcache *avc, struct vrequest *areq)
 #endif
 
 
-#if    !defined(AFS_AIX_ENV) && !defined(AFS_HPUX_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV) && !defined(UKERNEL) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_FBSD_ENV)
+#if    !defined(AFS_AIX_ENV) && !defined(AFS_HPUX_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_SGI_ENV) && !defined(UKERNEL) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
 /* Flock not support on System V systems */
 #ifdef AFS_OSF_ENV
 extern struct fileops afs_fileops;
-afs_xflock (p, args, retval) 
-       struct proc *p;
-       void *args;
-       int *retval;
-{
+
+int afs_xflock (struct proc *p, void *args, int *retval) 
 #else /* AFS_OSF_ENV */
-afs_xflock () {
+int afs_xflock (void)
 #endif
+{
     int code = 0;
     struct a {
        int fd;
@@ -859,7 +840,9 @@ afs_xflock () {
     struct vrequest treq;
     struct vcache *tvc;
     int flockDone;
+    struct afs_fakestat_state fakestate;
 
+    afs_InitFakeStat(&fakestate);
     AFS_STATCNT(afs_xflock);
     flockDone = 0;
 #ifdef AFS_OSF_ENV
@@ -869,9 +852,15 @@ afs_xflock () {
     uap = (struct a *)u.u_ap;
     fd = getf(uap->fd);
 #endif
-    if (!fd) return;
+    if (!fd) {
+       afs_PutFakeStat(&fakestate);
+       return;
+    }
 
-    if (flockDone = afs_InitReq(&treq, u.u_cred)) return flockDone;
+    if (flockDone = afs_InitReq(&treq, u.u_cred)) {
+       afs_PutFakeStat(&fakestate);
+       return flockDone;
+    }
     /* first determine whether this is any sort of vnode */
     if (fd->f_type == DTYPE_VNODE) {
        /* good, this is a vnode; next see if it is an AFS vnode */
@@ -885,9 +874,15 @@ afs_xflock () {
            tvc = VTOAFS(afs_gntovn)(tvc);
            if (!tvc) {
                u.u_error = ENOENT;
+               afs_PutFakeStat(&fakestate);
                return;
            }
 #endif
+           code = afs_EvalFakeStat(&tvc, &fakestate, &treq);
+           if (code) {
+               afs_PutFakeStat(&fakestate);
+               return code;
+           }
            if ((fd->f_flag & (FEXLOCK | FSHLOCK)) && !(uap->com & LOCK_UN)) {
                /* First, if fd already has lock, release it for relock path */
 #if defined(AFS_SGI_ENV) || defined(AFS_OSF_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV))
@@ -935,6 +930,7 @@ afs_xflock () {
 #else
     FP_UNREF(fd);
 #endif
+    afs_PutFakeStat(&fakestate);
     return code;
 #else  /* AFS_OSF_ENV */
     if (!flockDone)
@@ -943,6 +939,7 @@ afs_xflock () {
 #else
        flock();
 #endif
+    afs_PutFakeStat(&fakestate);
     return;
 #endif
 }