cast-pointer-20060126
[openafs.git] / src / afs / afs_vcache.c
index a94e5e5..c338957 100644 (file)
@@ -47,7 +47,7 @@ RCSID
 #include "afs/afs_cbqueue.h"
 #include "afs/afs_osidnlc.h"
 
-#ifdef AFS_OSF_ENV
+#if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
 afs_int32 afs_maxvcount = 0;   /* max number of vcache entries */
 afs_int32 afs_vcount = 0;      /* number of vcache in use now */
 #endif /* AFS_OSF_ENV */
@@ -62,13 +62,18 @@ char *makesname();
 
 /* Exported variables */
 afs_rwlock_t afs_xvcache;      /*Lock: alloc new stat cache entries */
+afs_rwlock_t afs_xvreclaim;    /*Lock: entries reclaimed, not on free list */
 afs_lock_t afs_xvcb;           /*Lock: fids on which there are callbacks */
-struct vcache *freeVCList;     /*Free list for stat cache entries */
-struct vcache *Initial_freeVCList;     /*Initial list for above */
+#if !defined(AFS_LINUX22_ENV)
+static struct vcache *freeVCList;      /*Free list for stat cache entries */
+struct vcache *ReclaimedVCList;        /*Reclaimed list for stat entries */
+static struct vcache *Initial_freeVCList;      /*Initial list for above */
+#endif
 struct afs_q VLRU;             /*vcache LRU */
 afs_int32 vcachegen = 0;
 unsigned int afs_paniconwarn = 0;
 struct vcache *afs_vhashT[VCSIZE];
+struct afs_q afs_vhashTV[VCSIZE];
 static struct afs_cbr *afs_cbrHashT[CBRSIZE];
 afs_int32 afs_bulkStatsLost;
 int afs_norefpanic = 0;
@@ -129,8 +134,8 @@ int
 afs_FlushVCache(struct vcache *avc, int *slept)
 {                              /*afs_FlushVCache */
 
-    register afs_int32 i, code;
-    register struct vcache **uvc, *wvc;
+    afs_int32 i, code;
+    struct vcache **uvc, *wvc;
 
     *slept = 0;
     AFS_STATCNT(afs_FlushVCache);
@@ -150,9 +155,11 @@ afs_FlushVCache(struct vcache *avc, int *slept)
        code = EBUSY;
        goto bad;
     }
+#if !defined(AFS_LINUX22_ENV)
     if (avc->nextfree || !avc->vlruq.prev || !avc->vlruq.next) {       /* qv afs.h */
        refpanic("LRU vs. Free inconsistency");
     }
+#endif
     avc->states |= CVFlushed;
     /* pull the entry out of the lruq and put it on the free list */
     QRemove(&avc->vlruq);
@@ -174,8 +181,10 @@ afs_FlushVCache(struct vcache *avc, int *slept)
            break;
        }
     }
-    if (!wvc)
-       osi_Panic("flushvcache");       /* not in correct hash bucket */
+
+    /* remove entry from the volume hash table */
+    QRemove(&avc->vhashq);
+
     if (avc->mvid)
        osi_FreeSmallSpace(avc->mvid);
     avc->mvid = (struct VenusFid *)0;
@@ -183,14 +192,23 @@ afs_FlushVCache(struct vcache *avc, int *slept)
        afs_osi_Free(avc->linkData, strlen(avc->linkData) + 1);
        avc->linkData = NULL;
     }
-#if defined(AFS_XBSD_ENV)
+#if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
     /* OK, there are no internal vrefCounts, so there shouldn't
      * be any more refs here. */
     if (avc->v) {
+#ifdef AFS_DARWIN80_ENV
+       vnode_clearfsnode(AFSTOV(avc));
+        vnode_removefsref(AFSTOV(avc));
+#else
        avc->v->v_data = NULL;  /* remove from vnode */
-       avc->v = NULL;          /* also drop the ptr to vnode */
+#endif
+       AFSTOV(avc) = NULL;             /* also drop the ptr to vnode */
     }
 #endif
+#ifdef AFS_SUN510_ENV
+    /* As we use private vnodes, cleanup is up to us */
+    vn_reinit(AFSTOV(avc));
+#endif
     afs_FreeAllAxs(&(avc->Access));
 
     /* we can't really give back callbacks on RO files, since the
@@ -204,7 +222,6 @@ afs_FlushVCache(struct vcache *avc, int *slept)
     afs_DequeueCallback(avc);  /* remove it from queued callbacks list */
     avc->states &= ~(CStatd | CUnique);
     ReleaseWriteLock(&afs_xcbhash);
-    afs_symhint_inval(avc);
     if ((avc->states & CForeign) || (avc->fid.Fid.Vnode & 1))
        osi_dnlc_purgedp(avc);  /* if it (could be) a directory */
     else
@@ -220,32 +237,36 @@ afs_FlushVCache(struct vcache *avc, int *slept)
     else
        afs_evenZaps++;
 
-#if    !defined(AFS_OSF_ENV)
+#if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
     /* put the entry in the free list */
     avc->nextfree = freeVCList;
     freeVCList = avc;
     if (avc->vlruq.prev || avc->vlruq.next) {
        refpanic("LRU vs. Free inconsistency");
     }
+    avc->states |= CVFlushed;
 #else
     /* This should put it back on the vnode free list since usecount is 1 */
     afs_vcount--;
     vSetType(avc, VREG);
-    if (VREFCOUNT(avc) > 0) {
+    if (VREFCOUNT_GT(avc,0)) {
+#if defined(AFS_OSF_ENV)
        VN_UNLOCK(AFSTOV(avc));
+#endif
        AFS_RELE(AFSTOV(avc));
     } else {
        if (afs_norefpanic) {
            printf("flush vc refcnt < 1");
            afs_norefpanic++;
+#if defined(AFS_OSF_ENV)
            (void)vgone(avc, VX_NOSLEEP, NULL);
            AFS_GLOCK();
            VN_UNLOCK(AFSTOV(avc));
+#endif
        } else
            osi_Panic("flush vc refcnt < 1");
     }
 #endif /* AFS_OSF_ENV */
-    avc->states |= CVFlushed;
     return 0;
 
   bad:
@@ -557,6 +578,38 @@ afs_RemoveVCB(struct VenusFid *afid)
     MReleaseWriteLock(&afs_xvcb);
 }
 
+void 
+afs_FlushReclaimedVcaches(void)
+{
+#if !defined(AFS_LINUX22_ENV)
+    struct vcache *tvc;
+    int code, fv_slept;
+    struct vcache *tmpReclaimedVCList = NULL;  
+
+    ObtainWriteLock(&afs_xvreclaim, 76);
+    while (ReclaimedVCList) {
+       tvc = ReclaimedVCList;  /* take from free list */
+       ReclaimedVCList = tvc->nextfree;
+       tvc->nextfree = NULL;
+       code = afs_FlushVCache(tvc, &fv_slept);
+       if (code) {
+           /* Ok, so, if we got code != 0, uh, wtf do we do? */
+           /* Probably, build a temporary list and then put all back when we
+              get to the end of the list */
+           /* This is actually really crappy, but we need to not leak these.
+              We probably need a way to be smarter about this. */
+           tvc->nextfree = tmpReclaimedVCList;
+           tmpReclaimedVCList = tvc;
+           printf("Reclaim list flush %lx failed: %d\n", (unsigned long) tvc, code);
+       }
+    }
+    if (tmpReclaimedVCList) 
+       ReclaimedVCList = tmpReclaimedVCList;
+
+    ReleaseWriteLock(&afs_xvreclaim);
+#endif
+}
+
 /*
  * afs_NewVCache
  *
@@ -576,14 +629,11 @@ struct vcache *
 afs_NewVCache(struct VenusFid *afid, struct server *serverp)
 {
     struct vcache *tvc;
-    afs_int32 i;
+    afs_int32 i, j;
     afs_int32 anumber = VCACHE_FREE;
 #ifdef AFS_AIX_ENV
     struct gnode *gnodepnt;
 #endif
-#ifdef AFS_MACH_ENV
-    struct vm_info *vm_info_ptr;
-#endif /* AFS_MACH_ENV */
 #ifdef AFS_OSF_ENV
     struct vcache *nvc;
 #endif /* AFS_OSF_ENV */
@@ -591,9 +641,12 @@ afs_NewVCache(struct VenusFid *afid, struct server *serverp)
     int code, fv_slept;
 
     AFS_STATCNT(afs_NewVCache);
-#ifdef AFS_OSF_ENV
-#ifdef AFS_OSF30_ENV
-    if (afs_vcount >= afs_maxvcount) {
+
+    afs_FlushReclaimedVcaches();
+
+#if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
+#if defined(AFS_OSF30_ENV) || defined(AFS_LINUX22_ENV)
+    if (afs_vcount >= afs_maxvcount)
 #else
     /*
      * If we are using > 33 % of the total system vnodes for AFS vcache
@@ -602,9 +655,9 @@ afs_NewVCache(struct VenusFid *afid, struct server *serverp)
      * our usage is > afs_maxvcount, set elsewhere to 0.5*nvnode,
      * we _must_ free some -- no choice).
      */
-    if (((3 * afs_vcount) > nvnode) || (afs_vcount >= afs_maxvcount)) {
+    if (((3 * afs_vcount) > nvnode) || (afs_vcount >= afs_maxvcount))
 #endif
-       struct afs_q *tq, *uq;
+    {
        int i;
        char *panicstr;
 
@@ -612,16 +665,62 @@ afs_NewVCache(struct VenusFid *afid, struct server *serverp)
        for (tq = VLRU.prev; tq != &VLRU && anumber > 0; tq = uq) {
            tvc = QTOV(tq);
            uq = QPrev(tq);
-           if (tvc->states & CVFlushed)
+           if (tvc->states & CVFlushed) {
                refpanic("CVFlushed on VLRU");
-           else if (i++ > afs_maxvcount)
+           } else if (i++ > afs_maxvcount) {
                refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
-           else if (QNext(uq) != tq)
+           } else if (QNext(uq) != tq) {
                refpanic("VLRU inconsistent");
-           else if (VREFCOUNT(tvc) < 1)
+           } else if (!VREFCOUNT_GT(tvc,0)) {
                refpanic("refcnt 0 on VLRU");
+           }
+
+#if defined(AFS_LINUX22_ENV)
+           if (tvc != afs_globalVp && VREFCOUNT(tvc) > 1 && tvc->opens == 0) {
+                struct dentry *dentry;
+                struct list_head *cur, *head;
+                AFS_FAST_HOLD(tvc);
+                AFS_GUNLOCK();
+#if defined(AFS_LINUX24_ENV)
+                spin_lock(&dcache_lock);
+#endif
+               head = &(AFSTOV(tvc))->i_dentry;
+
+restart:
+                cur = head;
+                while ((cur = cur->next) != head) {
+                    dentry = list_entry(cur, struct dentry, d_alias);
+
+                   if (d_unhashed(dentry))
+                       continue;
+
+                   dget_locked(dentry);
 
-           if (VREFCOUNT(tvc) == 1 && tvc->opens == 0
+#if defined(AFS_LINUX24_ENV)
+                   spin_unlock(&dcache_lock);
+#endif
+                   if (d_invalidate(dentry) == -EBUSY) {
+                       dput(dentry);
+                       /* perhaps lock and try to continue? (use cur as head?) */
+                       goto inuse;
+                   }
+                   dput(dentry);
+#if defined(AFS_LINUX24_ENV)
+                   spin_lock(&dcache_lock);
+#endif
+                   goto restart;
+               }                   
+#if defined(AFS_LINUX24_ENV)
+               spin_unlock(&dcache_lock);
+#endif
+           inuse:
+               AFS_GLOCK();
+               AFS_FAST_RELE(tvc);
+           }
+#endif
+
+           if (VREFCOUNT_GT(tvc,0) && !VREFCOUNT_GT(tvc,1) &&
+               tvc->opens == 0
                && (tvc->states & CUnlinkedDel) == 0) {
                code = afs_FlushVCache(tvc, &fv_slept);
                if (code == 0) {
@@ -637,16 +736,33 @@ afs_NewVCache(struct VenusFid *afid, struct server *serverp)
                break;
        }
        if (anumber == VCACHE_FREE) {
-           printf("NewVCache: warning none freed, using %d of %d\n",
+           printf("afs_NewVCache: warning none freed, using %d of %d\n",
                   afs_vcount, afs_maxvcount);
            if (afs_vcount >= afs_maxvcount) {
-               osi_Panic("NewVCache - none freed");
-               /* XXX instead of panicing, should do afs_maxvcount++
-                * and magic up another one */
+               printf("afs_NewVCache - none freed\n");
+               return NULL;
            }
        }
     }
 
+#if defined(AFS_LINUX22_ENV)
+{
+    struct inode *ip;
+
+    AFS_GUNLOCK();
+    ip = new_inode(afs_globalVFS);
+    if (!ip)
+       osi_Panic("afs_NewVCache: no more inodes");
+    AFS_GLOCK();
+#if defined(STRUCT_SUPER_HAS_ALLOC_INODE)
+    tvc = VTOAFS(ip);
+#else
+    tvc = afs_osi_Alloc(sizeof(struct vcache));
+    ip->u.generic_ip = tvc;
+    tvc->v = ip;
+#endif
+}
+#else
     AFS_GUNLOCK();
     if (getnewvnode(MOUNT_AFS, &Afs_vnodeops, &nvc)) {
        /* What should we do ???? */
@@ -656,6 +772,7 @@ afs_NewVCache(struct VenusFid *afid, struct server *serverp)
 
     tvc = nvc;
     tvc->nextfree = NULL;
+#endif
     afs_vcount++;
 #else /* AFS_OSF_ENV */
     /* pull out a free cache entry */
@@ -671,84 +788,56 @@ afs_NewVCache(struct VenusFid *afid, struct server *serverp)
                refpanic("Increase -stat parameter of afsd(VLRU cycle?)");
            } else if (QNext(uq) != tq) {
                refpanic("VLRU inconsistent");
-           }
-#ifdef AFS_DARWIN_ENV
-           if ((VREFCOUNT(tvc) < DARWIN_REFBASE) ||
-               (VREFCOUNT(tvc) < 1 + DARWIN_REFBASE &&
-                UBCINFOEXISTS(&tvc->v))) {
-               VREFCOUNT_SET(tvc,
-                             DARWIN_REFBASE +
-                             (UBCINFOEXISTS(&tvc->v) ? 1 : 0));
-           }
-           if (tvc->opens == 0 && ((tvc->states & CUnlinkedDel) == 0)
-               && VREFCOUNT(tvc) == DARWIN_REFBASE + 1
-               && UBCINFOEXISTS(&tvc->v)) {
-               osi_VM_TryReclaim(tvc, &fv_slept);
-               if (fv_slept) {
-                   uq = VLRU.prev;
-                   i = 0;
-                   continue;   /* start over - may have raced. */
+           } else if (tvc->states & CVInit) {
+               continue;
+            }
+
+           if (!VREFCOUNT_GT(tvc,0)
+#if defined(AFS_DARWIN_ENV) && !defined(UKERNEL) && !defined(AFS_DARWIN80_ENV)
+               || ((VREFCOUNT(tvc) == 1) && 
+                   (UBCINFOEXISTS(AFSTOV(tvc))))
+#endif
+               && tvc->opens == 0 && (tvc->states & CUnlinkedDel) == 0) {
+#if defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
+#ifdef AFS_DARWIN80_ENV
+               vnode_t tvp = AFSTOV(tvc);
+               /* VREFCOUNT_GT only sees usecounts, not iocounts */
+               /* so this may fail to actually recycle the vnode now */
+               /* must call vnode_get to avoid races. */
+               if (vnode_get(tvp) == 0) {
+                   fv_slept=1;
+                   /* must release lock, since vnode_put will immediately
+                      reclaim if there are no other users */
+                   ReleaseWriteLock(&afs_xvcache);
+                   AFS_GUNLOCK();
+                   vnode_recycle(tvp);
+                   vnode_put(tvp);
+                   AFS_GLOCK();
+                   ObtainWriteLock(&afs_xvcache, 336);
                }
-           }
-#elif defined(AFS_LINUX22_ENV)
-           if (tvc != afs_globalVp && VREFCOUNT(tvc) && tvc->opens == 0) {
-                struct dentry *dentry;
-                struct list_head *cur, *head = &(AFSTOI(tvc))->i_dentry;
-                AFS_FAST_HOLD(tvc);
-                AFS_GUNLOCK();
-
-restart:
-#if defined(AFS_LINUX24_ENV)
-                spin_lock(&dcache_lock);
-#endif
-                cur = head;
-                while ((cur = cur->next) != head) {
-                    dentry = list_entry(cur, struct dentry, d_alias);
-
-                   if (d_unhashed(dentry))
-                       continue;
-
-                   dget_locked(dentry);
-
-#if defined(AFS_LINUX24_ENV)
-                   spin_unlock(&dcache_lock);
-#endif
-                   if (d_invalidate(dentry) == -EBUSY) {
-                       dput(dentry);
-                       /* perhaps lock and try to continue? (use cur as head?) */
-                       goto inuse;
-                   }
-                   dput(dentry);
-                   goto restart;
-               }                   
-#if defined(AFS_LINUX24_ENV)
-               spin_unlock(&dcache_lock);
-#endif
-           inuse:
-               AFS_GLOCK();
-               AFS_FAST_RELE(tvc);
-           }
-#endif
-
-           if (VREFCOUNT(tvc) ==
-#ifdef AFS_DARWIN_ENV
-               DARWIN_REFBASE
+               /* we can't use the vnode_recycle return value to figure
+                * this out, since the iocount we have to hold makes it
+                * always "fail" */
+               if (AFSTOV(tvc) == tvp)
+                   code = EBUSY;
+               else
+                   code = 0;
 #else
-               0
+                /*
+                 * vgone() reclaims the vnode, which calls afs_FlushVCache(),
+                 * then it puts the vnode on the free list.
+                 * If we don't do this we end up with a cleaned vnode that's
+                 * not on the free list.
+                 * XXX assume FreeBSD is the same for now.
+                 */
+                AFS_GUNLOCK();
+                vgone(AFSTOV(tvc));
+                fv_slept = 0;
+                code = 0;
+                AFS_GLOCK();
 #endif
-               && tvc->opens == 0 && (tvc->states & CUnlinkedDel) == 0) {
-#if defined(AFS_XBSD_ENV)
-               /*
-                * vgone() reclaims the vnode, which calls afs_FlushVCache(),
-                * then it puts the vnode on the free list.
-                * If we don't do this we end up with a cleaned vnode that's
-                * not on the free list.
-                * XXX assume FreeBSD is the same for now.
-                */
-               vgone(AFSTOV(tvc));
-               code = fv_slept = 0;
 #else
-               code = afs_FlushVCache(tvc, &fv_slept);
+                code = afs_FlushVCache(tvc, &fv_slept);
 #endif
                if (code == 0) {
                    anumber--;
@@ -767,17 +856,12 @@ restart:
        /* none free, making one is better than a panic */
        afs_stats_cmperf.vcacheXAllocs++;       /* count in case we have a leak */
        tvc = (struct vcache *)afs_osi_Alloc(sizeof(struct vcache));
+#if defined(AFS_DARWIN_ENV) && !defined(UKERNEL)
+       tvc->v = NULL; /* important to clean this, or use memset 0 */
+#endif
 #ifdef KERNEL_HAVE_PIN
        pin((char *)tvc, sizeof(struct vcache));        /* XXX */
 #endif
-#ifdef AFS_MACH_ENV
-       /* In case it still comes here we need to fill this */
-       tvc->v.v_vm_info = VM_INFO_NULL;
-       vm_info_init(tvc->v.v_vm_info);
-       /* perhaps we should also do close_flush on non-NeXT mach systems;
-        * who knows; we don't currently have the sources.
-        */
-#endif /* AFS_MACH_ENV */
 #if defined(AFS_SGI_ENV)
        {
            char name[METER_NAMSZ];
@@ -803,16 +887,12 @@ restart:
     }
 #endif /* AFS_OSF_ENV */
 
-#ifdef AFS_MACH_ENV
-    vm_info_ptr = tvc->v.v_vm_info;
-#endif /* AFS_MACH_ENV */
-
-#if defined(AFS_XBSD_ENV)
+#if defined(AFS_XBSD_ENV) || defined(AFS_DARWIN_ENV)
     if (tvc->v)
        panic("afs_NewVCache(): free vcache with vnode attached");
 #endif
 
-#if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV)
+#if !defined(AFS_SGI_ENV) && !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
     memset((char *)tvc, 0, sizeof(struct vcache));
 #else
     tvc->uncred = 0;
@@ -823,28 +903,87 @@ restart:
     RWLOCK_INIT(&tvc->vlock, "vcache vlock");
 #endif /* defined(AFS_SUN5_ENV) */
 
-#ifdef AFS_MACH_ENV
-    tvc->v.v_vm_info = vm_info_ptr;
-    tvc->v.v_vm_info->pager = MEMORY_OBJECT_NULL;
-#endif /* AFS_MACH_ENV */
+    tvc->parentVnode = 0;
+    tvc->mvid = NULL;
+    tvc->linkData = NULL;
+    tvc->cbExpires = 0;
+    tvc->opens = 0;
+    tvc->execsOrWriters = 0;
+    tvc->flockCount = 0;
+    tvc->anyAccess = 0;
+    tvc->states = CVInit;
+    tvc->last_looker = 0;
+    tvc->fid = *afid;
+    tvc->asynchrony = -1;
+    tvc->vc_error = 0;
+#ifdef AFS_TEXT_ENV
+    tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
+#endif
+    hzero(tvc->mapDV);
+    tvc->truncPos = AFS_NOTRUNC;        /* don't truncate until we need to */
+    hzero(tvc->m.DataVersion);  /* in case we copy it into flushDV */
+    tvc->Access = NULL;
+    tvc->callback = serverp;    /* to minimize chance that clear
+                                * request is lost */
+
+    i = VCHash(afid);
+    j = VCHashV(afid);
+
+    tvc->hnext = afs_vhashT[i];
+    afs_vhashT[i] = tvc;
+    QAdd(&afs_vhashTV[j], &tvc->vhashq);
+    
+    if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
+        refpanic("NewVCache VLRU inconsistent");
+    }
+    QAdd(&VLRU, &tvc->vlruq);   /* put in lruq */
+    if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
+        refpanic("NewVCache VLRU inconsistent2");
+    }
+    if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
+        refpanic("NewVCache VLRU inconsistent3");
+    }
+    if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
+        refpanic("NewVCache VLRU inconsistent4");
+    }
+    vcachegen++;
+    /* it should now be safe to drop the xvcache lock */
 #ifdef AFS_OBSD_ENV
+    ReleaseWriteLock(&afs_xvcache);
     AFS_GUNLOCK();
     afs_nbsd_getnewvnode(tvc); /* includes one refcount */
     AFS_GLOCK();
+    ObtainWriteLock(&afs_xvcache,337);
+    lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
+#endif
+#ifdef AFS_DARWIN_ENV
+    ReleaseWriteLock(&afs_xvcache);
+    AFS_GUNLOCK();
+    afs_darwin_getnewvnode(tvc);       /* includes one refcount */
+    AFS_GLOCK();
+    ObtainWriteLock(&afs_xvcache,338);
+#ifdef AFS_DARWIN80_ENV
+    LOCKINIT(tvc->rwlock);
+#else
     lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
 #endif
+#endif
 #ifdef AFS_FBSD_ENV
     {
        struct vnode *vp;
 
+       ReleaseWriteLock(&afs_xvcache);
        AFS_GUNLOCK();
-#ifdef AFS_FBSD50_ENV
+#if defined(AFS_FBSD60_ENV)
+       if (getnewvnode(MOUNT_AFS, afs_globalVFS, &afs_vnodeops, &vp))
+#elif defined(AFS_FBSD50_ENV)
        if (getnewvnode(MOUNT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
 #else
        if (getnewvnode(VT_AFS, afs_globalVFS, afs_vnodeop_p, &vp))
 #endif
            panic("afs getnewvnode");   /* can't happen */
        AFS_GLOCK();
+       ObtainWriteLock(&afs_xvcache,339);
        if (tvc->v != NULL) {
            /* I'd like to know if this ever happens...
             * We don't drop global for the rest of this function,
@@ -863,106 +1002,12 @@ restart:
        lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
     }
 #endif
-    tvc->parentVnode = 0;
-    tvc->mvid = NULL;
-    tvc->linkData = NULL;
-    tvc->cbExpires = 0;
-    tvc->opens = 0;
-    tvc->execsOrWriters = 0;
-    tvc->flockCount = 0;
-    tvc->anyAccess = 0;
-    tvc->states = 0;
-    tvc->last_looker = 0;
-    tvc->fid = *afid;
-    tvc->asynchrony = -1;
-    tvc->vc_error = 0;
-    afs_symhint_inval(tvc);
-#ifdef AFS_TEXT_ENV
-    tvc->flushDV.low = tvc->flushDV.high = AFS_MAXDV;
-#endif
-    hzero(tvc->mapDV);
-    tvc->truncPos = AFS_NOTRUNC;       /* don't truncate until we need to */
-    hzero(tvc->m.DataVersion); /* in case we copy it into flushDV */
-#if defined(AFS_LINUX22_ENV)
-    {
-       struct inode *ip = AFSTOI(tvc);
-       struct address_space *mapping = &ip->i_data;
-
-#if defined(AFS_LINUX26_ENV)
-       inode_init_once(ip);
-#else
-       sema_init(&ip->i_sem, 1);
-       INIT_LIST_HEAD(&ip->i_hash);
-       INIT_LIST_HEAD(&ip->i_dentry);
-#if defined(AFS_LINUX24_ENV)
-       sema_init(&ip->i_zombie, 1);
-       init_waitqueue_head(&ip->i_wait);
-       spin_lock_init(&ip->i_data.i_shared_lock);
-#ifdef STRUCT_ADDRESS_SPACE_HAS_PAGE_LOCK
-       spin_lock_init(&ip->i_data.page_lock);
-#endif
-       INIT_LIST_HEAD(&ip->i_data.clean_pages);
-       INIT_LIST_HEAD(&ip->i_data.dirty_pages);
-       INIT_LIST_HEAD(&ip->i_data.locked_pages);
-       INIT_LIST_HEAD(&ip->i_dirty_buffers);
-#ifdef STRUCT_INODE_HAS_I_DIRTY_DATA_BUFFERS
-       INIT_LIST_HEAD(&ip->i_dirty_data_buffers);
-#endif
-#ifdef STRUCT_INODE_HAS_I_DEVICES
-       INIT_LIST_HEAD(&ip->i_devices);
-#endif
-#ifdef STRUCT_INODE_HAS_I_TRUNCATE_SEM
-       init_rwsem(&ip->i_truncate_sem);
-#endif
-#ifdef STRUCT_INODE_HAS_I_ALLOC_SEM
-       init_rwsem(&ip->i_alloc_sem);
-#endif
-
-#else /* AFS_LINUX22_ENV */
-       sema_init(&ip->i_atomic_write, 1);
-       init_waitqueue(&ip->i_wait);
-#endif
-#endif
-
-#if defined(AFS_LINUX24_ENV)
-       mapping->host = ip;
-       ip->i_mapping = mapping;
-#ifdef STRUCT_ADDRESS_SPACE_HAS_GFP_MASK
-       ip->i_data.gfp_mask = GFP_HIGHUSER;
-#endif
-#if defined(AFS_LINUX26_ENV)
-       mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
-       {
-           extern struct backing_dev_info afs_backing_dev_info;
-
-           mapping->backing_dev_info = &afs_backing_dev_info;
-       }
-#endif
-#endif
-
-#if !defined(AFS_LINUX26_ENV)
-       if (afs_globalVFS)
-           ip->i_dev = afs_globalVFS->s_dev;
-#else
-#ifdef STRUCT_INODE_HAS_I_SECURITY
-       ip->i_security = NULL;
-       if (security_inode_alloc(ip))
-           panic("Cannot allocate inode security");
-#endif
-#endif
-       ip->i_sb = afs_globalVFS;
-       put_inode_on_dummy_list(ip);
-#ifdef STRUCT_INODE_HAS_I_SB_LIST
-       list_add(&ip->i_sb_list, &ip->i_sb->s_inodes);
-#endif
-    }
-#endif
 
-#ifdef AFS_OSF_ENV
+#if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
     /* Hold it for the LRU (should make count 2) */
     VN_HOLD(AFSTOV(tvc));
 #else /* AFS_OSF_ENV */
-#if !defined(AFS_XBSD_ENV)
+#if !(defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV))
     VREFCOUNT_SET(tvc, 1);     /* us */
 #endif /* AFS_XBSD_ENV */
 #endif /* AFS_OSF_ENV */
@@ -971,7 +1016,7 @@ restart:
     tvc->vmh = tvc->segid = NULL;
     tvc->credp = NULL;
 #endif
-#if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV) || defined(AFS_SUN5_ENV)
+#ifdef AFS_BOZONLOCK_ENV
 #if    defined(AFS_SUN5_ENV)
     rw_init(&tvc->rwlock, "vcache rwlock", RW_DEFAULT, NULL);
 
@@ -993,9 +1038,6 @@ restart:
     afs_BozonInit(&tvc->pvnLock, tvc);
 #endif
 
-    tvc->Access = NULL;
-    tvc->callback = serverp;   /* to minimize chance that clear
-                                * request is lost */
     /* initialize vnode data, note vrefCount is v.v_count */
 #ifdef AFS_AIX_ENV
     /* Don't forget to free the gnode space */
@@ -1034,17 +1076,6 @@ restart:
 #else
     SetAfsVnode(AFSTOV(tvc));
 #endif /* AFS_SGI64_ENV */
-#ifdef AFS_DARWIN_ENV
-    tvc->v.v_ubcinfo = UBC_INFO_NULL;
-    lockinit(&tvc->rwlock, PINOD, "vcache rwlock", 0, 0);
-    cache_purge(AFSTOV(tvc));
-    tvc->v.v_data = tvc;
-    tvc->v.v_tag = VT_AFS;
-    /* VLISTNONE(&tvc->v); */
-    tvc->v.v_freelist.tqe_next = 0;
-    tvc->v.v_freelist.tqe_prev = (struct vnode **)0xdeadb;
-    tvc->vrefCount += DARWIN_REFBASE;
-#endif
     /*
      * The proper value for mvstat (for root fids) is setup by the caller.
      */
@@ -1053,7 +1084,9 @@ restart:
        tvc->mvstat = 2;
     if (afs_globalVFS == 0)
        osi_Panic("afs globalvfs");
+#if !defined(AFS_LINUX22_ENV)
     vSetVfsp(tvc, afs_globalVFS);
+#endif
     vSetType(tvc, VREG);
 #ifdef AFS_AIX_ENV
     tvc->v.v_vfsnext = afs_globalVFS->vfs_vnodes;      /* link off vfs */
@@ -1064,9 +1097,6 @@ restart:
     tvc->v.v_next = gnodepnt->gn_vnode;        /*Single vnode per gnode for us! */
     gnodepnt->gn_vnode = &tvc->v;
 #endif
-#ifdef AFS_DEC_ENV
-    tvc->v.g_dev = ((struct mount *)afs_globalVFS->vfs_data)->m_dev;
-#endif
 #if    defined(AFS_DUX40_ENV)
     insmntque(tvc, afs_globalVFS, &afs_ubcops);
 #else
@@ -1094,29 +1124,12 @@ restart:
     vn_initlist((struct vnlist *)&tvc->v);
     tvc->lastr = 0;
 #endif /* AFS_SGI_ENV */
-    tvc->h1.dchint = 0;
+    tvc->dchint = NULL;
     osi_dnlc_purgedp(tvc);     /* this may be overkill */
-    memset((char *)&(tvc->quick), 0, sizeof(struct vtodc));
     memset((char *)&(tvc->callsort), 0, sizeof(struct afs_q));
     tvc->slocks = NULL;
-    i = VCHash(afid);
-
-    tvc->hnext = afs_vhashT[i];
-    afs_vhashT[i] = tvc;
-    if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
-       refpanic("NewVCache VLRU inconsistent");
-    }
-    QAdd(&VLRU, &tvc->vlruq);  /* put in lruq */
-    if ((VLRU.next->prev != &VLRU) || (VLRU.prev->next != &VLRU)) {
-       refpanic("NewVCache VLRU inconsistent2");
-    }
-    if (tvc->vlruq.next->prev != &(tvc->vlruq)) {
-       refpanic("NewVCache VLRU inconsistent3");
-    }
-    if (tvc->vlruq.prev->next != &(tvc->vlruq)) {
-       refpanic("NewVCache VLRU inconsistent4");
-    }
-    vcachegen++;
+    tvc->states &=~ CVInit;
+    afs_osi_Wakeup(&tvc->states);
 
     return tvc;
 
@@ -1149,6 +1162,12 @@ afs_FlushActiveVcaches(register afs_int32 doflocks)
     ObtainReadLock(&afs_xvcache);
     for (i = 0; i < VCSIZE; i++) {
        for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
+            if (tvc->states & CVInit) continue;
+#ifdef AFS_DARWIN80_ENV
+            if (tvc->states & CDeadVnode &&
+                (tvc->states & (CCore|CUnlinkedDel) ||
+                 tvc->flockCount)) panic("Dead vnode has core/unlinkedel/flock");
+#endif
            if (doflocks && tvc->flockCount != 0) {
                /* if this entry has an flock, send a keep-alive call out */
                osi_vnhold(tvc, 0);
@@ -1175,8 +1194,13 @@ afs_FlushActiveVcaches(register afs_int32 doflocks)
                          AFS_STATS_FS_RPCIDX_EXTENDLOCK, SHARED_LOCK, NULL));
 
                ReleaseWriteLock(&tvc->lock);
-               ObtainReadLock(&afs_xvcache);
-               AFS_FAST_RELE(tvc);
+#ifdef AFS_DARWIN80_ENV
+                AFS_FAST_RELE(tvc);
+                ObtainReadLock(&afs_xvcache);
+#else
+                ObtainReadLock(&afs_xvcache);
+                AFS_FAST_RELE(tvc);
+#endif
            }
            didCore = 0;
            if ((tvc->states & CCore) || (tvc->states & CUnlinkedDel)) {
@@ -1187,14 +1211,14 @@ afs_FlushActiveVcaches(register afs_int32 doflocks)
                 */
                osi_vnhold(tvc, 0);
                ReleaseReadLock(&afs_xvcache);
-#if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
+#ifdef AFS_BOZONLOCK_ENV
                afs_BozonLock(&tvc->pvnLock, tvc);
 #endif
 #if defined(AFS_SGI_ENV)
                /*
                 * That's because if we come in via the CUnlinkedDel bit state path we'll be have 0 refcnt
                 */
-               osi_Assert(VREFCOUNT(tvc) > 0);
+               osi_Assert(VREFCOUNT_GT(tvc,0));
                AFS_RWLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
 #endif
                ObtainWriteLock(&tvc->lock, 52);
@@ -1209,7 +1233,7 @@ afs_FlushActiveVcaches(register afs_int32 doflocks)
                               tvc->execsOrWriters);
                    code = afs_StoreOnLastReference(tvc, &ureq);
                    ReleaseWriteLock(&tvc->lock);
-#if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
+#ifdef AFS_BOZONLOCK_ENV
                    afs_BozonUnlock(&tvc->pvnLock, tvc);
 #endif
                    hzero(tvc->flushDV);
@@ -1224,7 +1248,7 @@ afs_FlushActiveVcaches(register afs_int32 doflocks)
                     * Ignore errors
                     */
                    ReleaseWriteLock(&tvc->lock);
-#if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
+#ifdef AFS_BOZONLOCK_ENV
                    afs_BozonUnlock(&tvc->pvnLock, tvc);
 #endif
 #if defined(AFS_SGI_ENV)
@@ -1237,39 +1261,38 @@ afs_FlushActiveVcaches(register afs_int32 doflocks)
                } else {
                    /* lost (or won, perhaps) the race condition */
                    ReleaseWriteLock(&tvc->lock);
-#if defined(AFS_SUN_ENV) || defined(AFS_ALPHA_ENV)
+#ifdef AFS_BOZONLOCK_ENV
                    afs_BozonUnlock(&tvc->pvnLock, tvc);
 #endif
                }
 #if defined(AFS_SGI_ENV)
                AFS_RWUNLOCK((vnode_t *) tvc, VRWLOCK_WRITE);
 #endif
-               ObtainReadLock(&afs_xvcache);
+#ifdef AFS_DARWIN80_ENV
                AFS_FAST_RELE(tvc);
                if (didCore) {
-#ifdef AFS_GFS_ENV
-                   VREFCOUNT_DEC(tvc);
+                   AFS_RELE(AFSTOV(tvc));
+                   /* Matches write code setting CCore flag */
+                   crfree(cred);
+               }
+               ObtainReadLock(&afs_xvcache);
 #else
+               ObtainReadLock(&afs_xvcache);
+               AFS_FAST_RELE(tvc);
+               if (didCore) {
                    AFS_RELE(AFSTOV(tvc));
-#endif
                    /* Matches write code setting CCore flag */
                    crfree(cred);
                }
-           }
-#ifdef AFS_DARWIN_ENV
-           if (VREFCOUNT(tvc) == 1 + DARWIN_REFBASE
-               && UBCINFOEXISTS(&tvc->v)) {
-               if (tvc->opens)
-                   panic("flushactive open, hasubc, but refcnt 1");
-               osi_VM_TryReclaim(tvc, 0);
-           }
 #endif
+           }
        }
     }
     ReleaseReadLock(&afs_xvcache);
 }
 
 
+
 /*
  * afs_VerifyVCache
  *
@@ -1602,13 +1625,6 @@ afs_ProcessFS(register struct vcache *avc,
        else                    /* not found, add a new one if possible */
            afs_AddAxs(avc->Access, areq->uid, astat->CallerAccess);
     }
-#ifdef AFS_LINUX22_ENV
-    vcache2inode(avc);         /* Set the inode attr cache */
-#endif
-#ifdef AFS_DARWIN_ENV
-    osi_VM_Setup(avc, 1);
-#endif
-
 }                              /*afs_ProcessFS */
 
 
@@ -1706,7 +1722,7 @@ afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
 
     ObtainSharedLock(&afs_xvcache, 5);
 
-    tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU);
+    tvc = afs_FindVCache(afid, &retry, DO_STATS | DO_VLRU | IS_SLOCK);
     if (tvc && retry) {
 #if    defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
        ReleaseSharedLock(&afs_xvcache);
@@ -1718,6 +1734,7 @@ afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
     if (tvc) {
        if (cached)
            *cached = 1;
+       osi_Assert((tvc->states & CVInit) == 0);
        if (tvc->states & CStatd) {
            ReleaseSharedLock(&afs_xvcache);
            return tvc;
@@ -1730,6 +1747,12 @@ afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
        newvcache = 1;
 
        ConvertWToSLock(&afs_xvcache);
+       if (!tvc)
+       {
+               ReleaseSharedLock(&afs_xvcache);
+               return NULL;
+       }
+
        afs_stats_cmperf.vcacheMisses++;
     }
 
@@ -1738,13 +1761,7 @@ afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
     ObtainWriteLock(&tvc->lock, 54);
 
     if (tvc->states & CStatd) {
-#ifdef AFS_LINUX22_ENV
-       vcache2inode(tvc);
-#endif
        ReleaseWriteLock(&tvc->lock);
-#ifdef AFS_DARWIN_ENV
-       osi_VM_Setup(tvc, 0);
-#endif
        return tvc;
     }
 #if defined(AFS_OSF_ENV)
@@ -1753,12 +1770,11 @@ afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
        return tvc;
     }
 #endif /* AFS_OSF_ENV */
-#ifdef AFS_OBSD_ENV
-    VOP_LOCK(AFSTOV(tvc), LK_EXCLUSIVE | LK_RETRY, curproc);
-    uvm_vnp_uncache(AFSTOV(tvc));
-    VOP_UNLOCK(AFSTOV(tvc), 0, curproc);
-#endif
-#ifdef AFS_FBSD_ENV
+#ifdef AFS_DARWIN80_ENV
+/* Darwin 8.0 only has bufs in nfs, so we shouldn't have to worry about them.
+   What about ubc? */
+#else
+#if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
     /*
      * XXX - I really don't like this.  Should try to understand better.
      * It seems that sometimes, when we get called, we already hold the
@@ -1771,27 +1787,55 @@ afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
      * to vinvalbuf; otherwise, we leave it alone.
      */
     {
-       struct vnode *vp;
+       struct vnode *vp = AFSTOV(tvc);
        int iheldthelock;
 
-       vp = AFSTOV(tvc);
-#ifdef AFS_FBSD50_ENV
+#if defined(AFS_DARWIN_ENV)
+       iheldthelock = VOP_ISLOCKED(vp);
+       if (!iheldthelock)
+           vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, current_proc());
+       /* this is messy. we can call fsync which will try to reobtain this */
+       if (VTOAFS(vp) == tvc) 
+         ReleaseWriteLock(&tvc->lock);
+       if (UBCINFOEXISTS(vp)) {
+         vinvalbuf(vp, V_SAVE, &afs_osi_cred, current_proc(), PINOD, 0);
+       }
+       if (VTOAFS(vp) == tvc) 
+         ObtainWriteLock(&tvc->lock, 954);
+       if (!iheldthelock)
+           VOP_UNLOCK(vp, LK_EXCLUSIVE, current_proc());
+#elif defined(AFS_FBSD60_ENV)
+       iheldthelock = VOP_ISLOCKED(vp, curthread);
+       if (!iheldthelock)
+           vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
+       vinvalbuf(vp, V_SAVE, curthread, PINOD, 0);
+       if (!iheldthelock)
+           VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
+#elif defined(AFS_FBSD50_ENV)
        iheldthelock = VOP_ISLOCKED(vp, curthread);
        if (!iheldthelock)
            vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curthread);
        vinvalbuf(vp, V_SAVE, osi_curcred(), curthread, PINOD, 0);
        if (!iheldthelock)
            VOP_UNLOCK(vp, LK_EXCLUSIVE, curthread);
-#else
+#elif defined(AFS_FBSD40_ENV)
        iheldthelock = VOP_ISLOCKED(vp, curproc);
        if (!iheldthelock)
            vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
        vinvalbuf(vp, V_SAVE, osi_curcred(), curproc, PINOD, 0);
        if (!iheldthelock)
            VOP_UNLOCK(vp, LK_EXCLUSIVE, curproc);
+#elif defined(AFS_OBSD_ENV)
+       iheldthelock = VOP_ISLOCKED(vp, curproc);
+       if (!iheldthelock)
+           VOP_LOCK(vp, LK_EXCLUSIVE | LK_RETRY, curproc);
+       uvm_vnp_uncache(vp);
+       if (!iheldthelock)
+           VOP_UNLOCK(vp, 0, curproc);
 #endif
     }
 #endif
+#endif
 
     ObtainWriteLock(&afs_xcbhash, 464);
     tvc->states &= ~CUnique;
@@ -1842,9 +1886,7 @@ afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
     if (code) {
        ReleaseWriteLock(&tvc->lock);
 
-       ObtainReadLock(&afs_xvcache);
-       AFS_FAST_RELE(tvc);
-       ReleaseReadLock(&afs_xvcache);
+       afs_PutVCache(tvc);
        return NULL;
     }
 
@@ -1901,8 +1943,8 @@ afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
        tvc->states &= ~CUnique;
 
        ReleaseReadLock(&tvc->lock);
+       afs_PutVCache(tvc);
        ObtainReadLock(&afs_xvcache);
-       AFS_FAST_RELE(tvc);
     }
     /* if (tvc) */
     ReleaseReadLock(&afs_xvcache);
@@ -1920,7 +1962,7 @@ afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
 #endif
 
     ObtainSharedLock(&afs_xvcache, 6);
-    tvc = afs_FindVCache(&nfid, &retry, DO_VLRU /* no xstats now */ );
+    tvc = afs_FindVCache(&nfid, &retry, DO_VLRU | IS_SLOCK/* no xstats now */ );
     if (tvc && retry) {
 #if    defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
        ReleaseSharedLock(&afs_xvcache);
@@ -1935,6 +1977,11 @@ afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
        tvc = afs_NewVCache(&nfid, serverp);
        newvcache = 1;
        ConvertWToSLock(&afs_xvcache);
+       if (!tvc)
+       {
+               ReleaseSharedLock(&afs_xvcache);
+               return NULL;
+       }
     }
 
     ReleaseSharedLock(&afs_xvcache);
@@ -1974,9 +2021,7 @@ afs_LookupVCache(struct VenusFid *afid, struct vrequest *areq,
        if (tvp)
            afs_PutVolume(tvp, READ_LOCK);
        ReleaseWriteLock(&tvc->lock);
-       ObtainReadLock(&afs_xvcache);
-       AFS_FAST_RELE(tvc);
-       ReleaseReadLock(&afs_xvcache);
+       afs_PutVCache(tvc);
        return NULL;
     }
 
@@ -2033,6 +2078,9 @@ afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
     struct AFSCallBack CallBack;
     struct AFSVolSync tsync;
     int origCBs = 0;
+#ifdef AFS_OSF_ENV
+    int vg;
+#endif
 
     start = osi_Time();
 
@@ -2061,37 +2109,35 @@ afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
        afid->Fid.Unique = tvolp->rootUnique;
     }
 
+ rootvc_loop:
     ObtainSharedLock(&afs_xvcache, 7);
     i = VCHash(afid);
     for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
        if (!FidCmp(&(tvc->fid), afid)) {
+            if (tvc->states & CVInit) {
+               ReleaseSharedLock(&afs_xvcache);
+               afs_osi_Sleep(&tvc->states);
+               goto rootvc_loop;
+            }
 #ifdef AFS_OSF_ENV
            /* Grab this vnode, possibly reactivating from the free list */
            /* for the present (95.05.25) everything on the hash table is
             * definitively NOT in the free list -- at least until afs_reclaim
             * can be safely implemented */
-           int vg;
            AFS_GUNLOCK();
            vg = vget(AFSTOV(tvc));     /* this bumps ref count */
            AFS_GLOCK();
            if (vg)
                continue;
 #endif /* AFS_OSF_ENV */
-#ifdef AFS_DARWIN14_ENV
-           /* It'd really suck if we allowed duplicate vcaches for the 
-            * same fid to happen. Wonder if this will work? */
-           struct vnode *vp = AFSTOV(tvc);
-           if (vp->v_flag & (VXLOCK | VORECLAIM | VTERMINATE)) {
-               printf("precluded FindVCache on %x (%d:%d:%d)\n",
-                      vp, tvc->fid.Fid.Volume, tvc->fid.Fid.Vnode,
-                      tvc->fid.Fid.Unique);
-               simple_lock(&vp->v_interlock);
-               SET(vp->v_flag, VTERMWANT);
-               simple_unlock(&vp->v_interlock);
-               (void)tsleep((caddr_t) & vp->v_ubcinfo, PINOD, "vget1", 0);
-               printf("VTERMWANT ended on %x\n", vp);
-               continue;
-           }
+#ifdef AFS_DARWIN80_ENV
+            if (tvc->states & CDeadVnode) {
+               ReleaseSharedLock(&afs_xvcache);
+               afs_osi_Sleep(&tvc->states);
+               goto rootvc_loop;
+            }
+            if (vnode_get(AFSTOV(tvc)))       /* this bumps ref count */
+                continue;
 #endif
            break;
        }
@@ -2103,9 +2149,16 @@ afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
        if (tvc)
            AFS_RELE(AFSTOV(tvc));
 #endif
-       tvc = NULL;
        getNewFid = 1;
        ReleaseSharedLock(&afs_xvcache);
+#ifdef AFS_DARWIN80_ENV
+        if (tvc) {
+            AFS_GUNLOCK();
+            vnode_put(AFSTOV(tvc));
+            AFS_GLOCK();
+        }
+#endif
+        tvc = NULL;
        goto newmtpt;
     }
 
@@ -2113,6 +2166,11 @@ afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
        UpgradeSToWLock(&afs_xvcache, 23);
        /* no cache entry, better grab one */
        tvc = afs_NewVCache(afid, NULL);
+       if (!tvc)
+       {
+               ReleaseWriteLock(&afs_xvcache);
+               return NULL;
+       }
        newvcache = 1;
        afs_stats_cmperf.vcacheMisses++;
     } else {
@@ -2206,9 +2264,7 @@ afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
        if ((tvc->states & CForeign) || (tvc->fid.Fid.Vnode & 1))
            osi_dnlc_purgedp(tvc);      /* if it (could be) a directory */
        ReleaseWriteLock(&tvc->lock);
-       ObtainReadLock(&afs_xvcache);
-       AFS_FAST_RELE(tvc);
-       ReleaseReadLock(&afs_xvcache);
+       afs_PutVCache(tvc);
        return NULL;
     }
 
@@ -2262,8 +2318,7 @@ afs_FetchStatus(struct vcache * avc, struct VenusFid * afid,
     XSTATS_DECLS;
     do {
        tc = afs_Conn(afid, areq, SHARED_LOCK);
-       avc->quick.stamp = 0;
-       avc->h1.dchint = NULL;  /* invalidate hints */
+       avc->dchint = NULL;     /* invalidate hints */
        if (tc) {
            avc->callback = tc->srvr->server;
            start = osi_Time();
@@ -2371,7 +2426,7 @@ afs_StuffVcache(register struct VenusFid *afid,
   loop:
     ObtainSharedLock(&afs_xvcache, 8);
 
-    tvc = afs_FindVCache(afid, &retry, DO_VLRU /* no stats */ );
+    tvc = afs_FindVCache(afid, &retry, DO_VLRU| IS_SLOCK /* no stats */ );
     if (tvc && retry) {
 #if    defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
        ReleaseSharedLock(&afs_xvcache);
@@ -2386,6 +2441,11 @@ afs_StuffVcache(register struct VenusFid *afid,
        tvc = afs_NewVCache(afid, NULL);
        newvcache = 1;
        ConvertWToSLock(&afs_xvcache);
+       if (!tvc)
+       {
+               ReleaseSharedLock(&afs_xvcache);
+               return NULL;
+       }
     }
 
     ReleaseSharedLock(&afs_xvcache);
@@ -2488,14 +2548,41 @@ void
 afs_PutVCache(register struct vcache *avc)
 {
     AFS_STATCNT(afs_PutVCache);
+#ifdef AFS_DARWIN80_ENV
+    vnode_put(AFSTOV(avc));
+    AFS_FAST_RELE(avc);
+#else
     /*
      * Can we use a read lock here?
      */
     ObtainReadLock(&afs_xvcache);
     AFS_FAST_RELE(avc);
     ReleaseReadLock(&afs_xvcache);
+#endif
 }                              /*afs_PutVCache */
 
+
+static void findvc_sleep(struct vcache *avc, int flag) {
+    if (flag & IS_SLOCK) {
+           ReleaseSharedLock(&afs_xvcache);
+    } else {
+       if (flag & IS_WLOCK) {
+           ReleaseWriteLock(&afs_xvcache);
+       } else {
+           ReleaseReadLock(&afs_xvcache);
+       }
+    }
+    afs_osi_Sleep(&avc->states);
+    if (flag & IS_SLOCK) {
+           ObtainSharedLock(&afs_xvcache, 341);
+    } else {
+       if (flag & IS_WLOCK) {
+           ObtainWriteLock(&afs_xvcache, 343);
+       } else {
+           ObtainReadLock(&afs_xvcache);
+       }
+    }
+}
 /*
  * afs_FindVCache
  *
@@ -2521,21 +2608,36 @@ afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
 
     register struct vcache *tvc;
     afs_int32 i;
+#if defined( AFS_OSF_ENV)
+    int vg;
+#endif
 
     AFS_STATCNT(afs_FindVCache);
 
+ findloop:
     i = VCHash(afid);
     for (tvc = afs_vhashT[i]; tvc; tvc = tvc->hnext) {
        if (FidMatches(afid, tvc)) {
+            if (tvc->states & CVInit) {
+               findvc_sleep(tvc, flag);
+               goto findloop;
+            }
 #ifdef  AFS_OSF_ENV
            /* Grab this vnode, possibly reactivating from the free list */
-           int vg;
            AFS_GUNLOCK();
            vg = vget(AFSTOV(tvc));
            AFS_GLOCK();
            if (vg)
                continue;
 #endif /* AFS_OSF_ENV */
+#ifdef  AFS_DARWIN80_ENV
+            if (tvc->states & CDeadVnode) {
+                findvc_sleep(tvc, flag);
+               goto findloop;
+            }
+            if (vnode_get(AFSTOV(tvc)))
+                continue;
+#endif
            break;
        }
     }
@@ -2549,6 +2651,16 @@ afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
        if (retry && *retry)
            return 0;
 #endif
+#if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN80_ENV)
+       tvc->states |= CUBCinit;
+       AFS_GUNLOCK();
+       if (UBCINFOMISSING(AFSTOV(tvc)) ||
+           UBCINFORECLAIMED(AFSTOV(tvc))) {
+         ubc_info_init(AFSTOV(tvc));
+       }
+       AFS_GLOCK();
+       tvc->states &= ~CUBCinit;
+#endif
        /*
         * only move to front of vlru if we have proper vcache locking)
         */
@@ -2589,14 +2701,6 @@ afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
        else
            afs_stats_cmperf.vremoteAccesses++;
     }
-#ifdef AFS_LINUX22_ENV
-    if (tvc && (tvc->states & CStatd))
-       vcache2inode(tvc);      /* mainly to reset i_nlink */
-#endif
-#ifdef AFS_DARWIN_ENV
-    if (tvc)
-       osi_VM_Setup(tvc, 0);
-#endif
     return tvc;
 }                              /*afs_FindVCache */
 
@@ -2633,12 +2737,13 @@ afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
     afs_int32 i;
     afs_int32 count = 0;
     struct vcache *found_tvc = NULL;
+#ifdef  AFS_OSF_ENV
+    int vg;
+#endif
 
     AFS_STATCNT(afs_FindVCache);
 
-#if defined(AFS_SGI_ENV) && !defined(AFS_SGI53_ENV)
   loop:
-#endif
 
     ObtainSharedLock(&afs_xvcache, 331);
 
@@ -2649,9 +2754,14 @@ afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
            && (tvc->fid.Fid.Volume == afid->Fid.Volume)
            && ((tvc->fid.Fid.Unique & 0xffffff) == afid->Fid.Unique)
            && (tvc->fid.Cell == afid->Cell)) {
+           if (tvc->states & CVInit) {
+               int lock;
+               ReleaseSharedLock(&afs_xvcache);
+               afs_osi_Sleep(&tvc->states);
+               goto loop;
+            }
 #ifdef  AFS_OSF_ENV
            /* Grab this vnode, possibly reactivating from the free list */
-           int vg;
            AFS_GUNLOCK();
            vg = vget(AFSTOV(tvc));
            AFS_GLOCK();
@@ -2660,6 +2770,17 @@ afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
                continue;
            }
 #endif /* AFS_OSF_ENV */
+#ifdef  AFS_DARWIN80_ENV
+            if (tvc->states & CDeadVnode) {
+               ReleaseSharedLock(&afs_xvcache);
+               afs_osi_Sleep(&tvc->states);
+               goto loop;
+            }
+            if (vnode_get(AFSTOV(tvc))) {
+                /* This vnode no longer exists. */
+                continue;
+            }
+#endif /* AFS_DARWIN80_ENV */
            count++;
            if (found_tvc) {
                /* Duplicates */
@@ -2670,6 +2791,11 @@ afs_NFSFindVCache(struct vcache **avcp, struct VenusFid *afid)
 #endif
                afs_duplicate_nfs_fids++;
                ReleaseSharedLock(&afs_xvcache);
+#ifdef AFS_DARWIN80_ENV
+                /* Drop our reference counts. */
+                vnode_put(AFSTOV(tvc));
+                vnode_put(AFSTOV(found_tvc));
+#endif
                return count;
            }
            found_tvc = tvc;
@@ -2751,9 +2877,11 @@ afs_vcacheInit(int astatSize)
 {
     register struct vcache *tvp;
     int i;
-#if    defined(AFS_OSF_ENV)
+#if defined(AFS_OSF_ENV) || defined(AFS_LINUX22_ENV)
     if (!afs_maxvcount) {
-#if    defined(AFS_OSF30_ENV)
+#if defined(AFS_LINUX22_ENV)
+       afs_maxvcount = astatSize;      /* no particular limit on linux? */
+#elif defined(AFS_OSF30_ENV)
        afs_maxvcount = max_vnodes / 2; /* limit ourselves to half the total */
 #else
        afs_maxvcount = nvnode / 2;     /* limit ourselves to half the total */
@@ -2769,18 +2897,7 @@ afs_vcacheInit(int astatSize)
     RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
     LOCK_INIT(&afs_xvcb, "afs_xvcb");
 
-#if    !defined(AFS_OSF_ENV)
-#ifdef AFS_LINUX26_ENV
-    printf("old style would have needed %d contiguous bytes\n", astatSize *
-          sizeof(struct vcache));
-    Initial_freeVCList = freeVCList = tvp = (struct vcache *)
-       afs_osi_Alloc(sizeof(struct vcache));
-    for (i = 0; i < astatSize; i++) {
-       tvp->nextfree = (struct vcache *) afs_osi_Alloc(sizeof(struct vcache));
-       tvp = tvp->nextfree;
-    }
-    tvp->nextfree = NULL;
-#else
+#if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
     /* Allocate and thread the struct vcache entries */
     tvp = (struct vcache *)afs_osi_Alloc(astatSize * sizeof(struct vcache));
     memset((char *)tvp, 0, sizeof(struct vcache) * astatSize);
@@ -2795,7 +2912,6 @@ afs_vcacheInit(int astatSize)
     pin((char *)tvp, astatSize * sizeof(struct vcache));       /* XXX */
 #endif
 #endif
-#endif
 
 #if defined(AFS_SGI_ENV)
     for (i = 0; i < astatSize; i++) {
@@ -2814,10 +2930,9 @@ afs_vcacheInit(int astatSize)
 #endif /* AFS_SGI62_ENV */
     }
 #endif
-
     QInit(&VLRU);
-
-
+    for(i = 0; i < VCSIZE; ++i)
+       QInit(&afs_vhashTV[i]);
 }
 
 /*
@@ -2871,7 +2986,7 @@ shutdown_vcache(void)
                    vms_delete(tvc->segid);
                    AFS_GLOCK();
                    tvc->segid = tvc->vmh = NULL;
-                   if (VREFCOUNT(tvc))
+                   if (VREFCOUNT_GT(tvc,0))
                        osi_Panic("flushVcache: vm race");
                }
                if (tvc->credp) {
@@ -2905,30 +3020,19 @@ shutdown_vcache(void)
     }
     afs_cbrSpace = 0;
 
-#ifdef AFS_LINUX26_ENV
-    {
-       struct vcache *tvp = Initial_freeVCList;
-       while (tvp) {
-           struct vcache *next = tvp->nextfree;
-           
-           afs_osi_Free(tvp, sizeof(struct vcache));
-           tvp = next;
-       }
-    }
-#else
 #ifdef  KERNEL_HAVE_PIN
     unpin(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
 #endif
-#if    !defined(AFS_OSF_ENV)
+#if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
     afs_osi_Free(Initial_freeVCList, afs_cacheStats * sizeof(struct vcache));
 #endif
-#endif
 
-#if    !defined(AFS_OSF_ENV)
+#if !defined(AFS_OSF_ENV) && !defined(AFS_LINUX22_ENV)
     freeVCList = Initial_freeVCList = 0;
 #endif
     RWLOCK_INIT(&afs_xvcache, "afs_xvcache");
     LOCK_INIT(&afs_xvcb, "afs_xvcb");
     QInit(&VLRU);
-
+    for(i = 0; i < VCSIZE; ++i)
+       QInit(&afs_vhashTV[i]);
 }