macos104-not-yet-20050511
[openafs.git] / src / afs / afs_vcache.c
index efc7503..70ff491 100644 (file)
@@ -183,7 +183,7 @@ 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) {
@@ -231,7 +231,11 @@ afs_FlushVCache(struct vcache *avc, int *slept)
     /* This should put it back on the vnode free list since usecount is 1 */
     afs_vcount--;
     vSetType(avc, VREG);
+#ifdef AFS_DARWIN80_ENV
+    if (vnode_isinuse(AFSTOV(avc), 0)) {
+#else
     if (VREFCOUNT(avc) > 0) {
+#endif
        VN_UNLOCK(AFSTOV(avc));
        AFS_RELE(AFSTOV(avc));
     } else {
@@ -581,9 +585,6 @@ afs_NewVCache(struct VenusFid *afid, struct server *serverp)
 #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 */
@@ -618,10 +619,19 @@ afs_NewVCache(struct VenusFid *afid, struct server *serverp)
                refpanic("Exceeded pool of AFS vnodes(VLRU cycle?)");
            else if (QNext(uq) != tq)
                refpanic("VLRU inconsistent");
+#ifdef AFS_DARWIN80_ENV
+           else if (!vnode_isinuse(AFSTOV(tvc), 0)) 
+#else
            else if (VREFCOUNT(tvc) < 1)
+#endif
                refpanic("refcnt 0 on VLRU");
 
-           if (VREFCOUNT(tvc) == 1 && tvc->opens == 0
+#ifdef AFS_DARWIN80_ENV
+           if (vnode_isinuse(AFSTOV(tvc), 0) &&  
+#else
+           if (VREFCOUNT(tvc) == 1 &&
+#endif
+               tvc->opens == 0
                && (tvc->states & CUnlinkedDel) == 0) {
                code = afs_FlushVCache(tvc, &fv_slept);
                if (code == 0) {
@@ -667,30 +677,14 @@ afs_NewVCache(struct VenusFid *afid, struct server *serverp)
 
            if (tvc->states & CVFlushed) {
                refpanic("CVFlushed on VLRU");
+#if 0
            } else if (i++ > 2 * afs_cacheStats) {      /* even allowing for a few xallocs... */
                refpanic("Increase -stat parameter of afsd(VLRU cycle?)");
+#endif
            } 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. */
-               }
-           }
-#elif defined(AFS_LINUX22_ENV)
+#if 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;
@@ -730,14 +724,17 @@ restart:
            }
 #endif
 
-           if (VREFCOUNT(tvc) ==
-#ifdef AFS_DARWIN_ENV
-               DARWIN_REFBASE
+#ifdef AFS_DARWIN80_ENV
+           if (!vnode_isinuse(AFSTOV(tvc), 0
 #else
-               0
+           if (((VREFCOUNT(tvc) == 0) 
+#if defined(AFS_DARWIN_ENV) && !defined(UKERNEL) 
+                || ((VREFCOUNT(tvc) == 1) && 
+                    (UBCINFOEXISTS(AFSTOV(tvc))))
+#endif
 #endif
-               && tvc->opens == 0 && (tvc->states & CUnlinkedDel) == 0) {
-#if defined(AFS_XBSD_ENV)
+                ) && tvc->opens == 0 && (tvc->states & CUnlinkedDel) == 0) {
+#if defined (AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
                /*
                 * vgone() reclaims the vnode, which calls afs_FlushVCache(),
                 * then it puts the vnode on the free list.
@@ -745,7 +742,9 @@ restart:
                 * not on the free list.
                 * XXX assume FreeBSD is the same for now.
                 */
+               AFS_GUNLOCK();
                vgone(AFSTOV(tvc));
+               AFS_GLOCK();
                code = fv_slept = 0;
 #else
                code = afs_FlushVCache(tvc, &fv_slept);
@@ -766,18 +765,14 @@ restart:
     if (!freeVCList) {
        /* none free, making one is better than a panic */
        afs_stats_cmperf.vcacheXAllocs++;       /* count in case we have a leak */
+       if (afs_cacheStats == afs_stats_cmperf.vcacheXAllocs) printf("would vlru cycle panic\n");
        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,11 +798,7 @@ 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
@@ -823,22 +814,30 @@ 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 */
 #ifdef AFS_OBSD_ENV
     AFS_GUNLOCK();
     afs_nbsd_getnewvnode(tvc); /* includes one refcount */
     AFS_GLOCK();
     lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
 #endif
+#ifdef AFS_DARWIN_ENV
+    AFS_GUNLOCK();
+    afs_darwin_getnewvnode(tvc);       /* includes one refcount */
+    AFS_GLOCK();
+#ifdef AFS_DARWIN80_ENV
+    LOCKINIT(tvc->rwlock);
+#else
+    lockinit(&tvc->rwlock, PINOD, "vcache", 0, 0);
+#endif
+#endif
 #ifdef AFS_FBSD_ENV
     {
        struct vnode *vp;
 
        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))
@@ -886,7 +885,9 @@ restart:
 #if defined(AFS_LINUX22_ENV)
     {
        struct inode *ip = AFSTOI(tvc);
+#if defined(AFS_LINUX24_ENV)
        struct address_space *mapping = &ip->i_data;
+#endif
 
 #if defined(AFS_LINUX26_ENV)
        inode_init_once(ip);
@@ -952,6 +953,17 @@ restart:
 #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
+#if defined(STRUCT_INODE_HAS_INOTIFY_LOCK) || defined(STRUCT_INODE_HAS_INOTIFY_SEM)
+       INIT_LIST_HEAD(&ip->inotify_watches); 
+#if defined(STRUCT_INODE_HAS_INOTIFY_SEM) 
+       sema_init(&ip->inotify_sem, 1); 
+#else
+       spin_lock_init(&ip->inotify_lock); 
+#endif 
+#endif 
     }
 #endif
 
@@ -959,7 +971,7 @@ restart:
     /* 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 */
@@ -968,7 +980,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);
 
@@ -1031,17 +1043,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.
      */
@@ -1061,9 +1062,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
@@ -1184,7 +1182,7 @@ 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)
@@ -1206,7 +1204,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);
@@ -1221,7 +1219,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)
@@ -1234,7 +1232,7 @@ 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
                }
@@ -1244,23 +1242,11 @@ afs_FlushActiveVcaches(register afs_int32 doflocks)
                ObtainReadLock(&afs_xvcache);
                AFS_FAST_RELE(tvc);
                if (didCore) {
-#ifdef AFS_GFS_ENV
-                   VREFCOUNT_DEC(tvc);
-#else
                    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);
@@ -1602,9 +1588,6 @@ afs_ProcessFS(register struct vcache *avc,
 #ifdef AFS_LINUX22_ENV
     vcache2inode(avc);         /* Set the inode attr cache */
 #endif
-#ifdef AFS_DARWIN_ENV
-    osi_VM_Setup(avc, 1);
-#endif
 
 }                              /*afs_ProcessFS */
 
@@ -1739,9 +1722,6 @@ afs_GetVCache(register struct VenusFid *afid, struct vrequest *areq,
        vcache2inode(tvc);
 #endif
        ReleaseWriteLock(&tvc->lock);
-#ifdef AFS_DARWIN_ENV
-       osi_VM_Setup(tvc, 0);
-#endif
        return tvc;
     }
 #if defined(AFS_OSF_ENV)
@@ -1750,12 +1730,7 @@ 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
+#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
@@ -1768,24 +1743,51 @@ 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
@@ -2074,22 +2076,6 @@ afs_GetRootVCache(struct VenusFid *afid, struct vrequest *areq,
            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;
-           }
-#endif
            break;
        }
     }
@@ -2546,6 +2532,16 @@ afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
        if (retry && *retry)
            return 0;
 #endif
+#ifdef AFS_DARWIN_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)
         */
@@ -2590,10 +2586,6 @@ afs_FindVCache(struct VenusFid *afid, afs_int32 * retry, afs_int32 flag)
     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 */