Move epoch and cid generation into the rx core
[openafs.git] / src / afs / afs_daemons.c
index fb89d44..fb79170 100644 (file)
@@ -26,7 +26,7 @@
 
 #if defined(AFS_CACHE_BYPASS)
 #include "afs/afs_bypasscache.h"
-#endif// defined(AFS_CACHE_BYPASS)
+#endif /* AFS_CACHE_BYPASS */
 /* background request queue size */
 afs_lock_t afs_xbrs;           /* lock for brs */
 static int brsInit = 0;
@@ -36,10 +36,6 @@ struct brequest afs_brs[NBRS];       /* request structures */
 struct afs_osi_WaitHandle AFS_WaitHandler, AFS_CSWaitHandler;
 static int afs_brs_count = 0;  /* request counter, to service reqs in order */
 
-static int rxepoch_checked = 0;
-#define afs_CheckRXEpoch() {if (rxepoch_checked == 0 && rxkad_EpochWasSet) { \
-       rxepoch_checked = 1; afs_GCUserData(/* force flag */ 1);  } }
-
 /* PAG garbage collection */
 /* We induce a compile error if param.h does not define AFS_GCPAGS */
 afs_int32 afs_gcpags = AFS_GCPAGS;
@@ -90,7 +86,7 @@ afs_CheckServerDaemon(void)
     last10MinCheck = lastCheck = osi_Time();
     while (1) {
        if (afs_termState == AFSOP_STOP_CS) {
-           afs_termState = AFSOP_STOP_BKG;
+           afs_termState = AFSOP_STOP_TRUNCDAEMON;
            afs_osi_Wakeup(&afs_termState);
            break;
        }
@@ -108,7 +104,7 @@ afs_CheckServerDaemon(void)
        }
        /* shutdown check. */
        if (afs_termState == AFSOP_STOP_CS) {
-           afs_termState = AFSOP_STOP_BKG;
+           afs_termState = AFSOP_STOP_TRUNCDAEMON;
            afs_osi_Wakeup(&afs_termState);
            break;
        }
@@ -143,8 +139,6 @@ afs_Daemon(void)
     char cs_warned = 0;
 
     AFS_STATCNT(afs_Daemon);
-    last1MinCheck = last3MinCheck = last60MinCheck = last10MinCheck =
-    last5MinCheck = lastNMinCheck = 0;
 
     afs_rootFid.Fid.Volume = 0;
     while (afs_initState < 101)
@@ -182,25 +176,6 @@ afs_Daemon(void)
            afs_FlushActiveVcaches(0);  /* flush NFS writes */
        afs_FlushVCBs(1);       /* flush queued callbacks */
 
-#if defined(AFS_NBSD50_ENV)
-       /* XXXX */
-       {
-         int c1, c2;
-         c1 = ISAFS_GLOCK(); /* this thread owns the GLOCK */
-         if (!c1) {
-           c2 = mutex_tryenter(&afs_global_mtx); /* not held either */
-           if (c2)
-             AFS_GUNLOCK();
-         }
-         else
-           c2 = 0;
-         printf("afs_daemons periodic glock check: curthread owns glock %s; "
-                "glock held somewhere %s\n",
-                c1 ? "true" : "false",
-                c2 ? "true" : "false");
-       }
-#endif
-
        afs_MaybeWakeupTruncateDaemon();        /* free cache space if have too */
        rx_CheckPackets();      /* Does RX need more packets? */
 
@@ -223,7 +198,6 @@ afs_Daemon(void)
 #if 0
            afs_StoreDirtyVcaches();
 #endif
-           afs_CheckRXEpoch();
            last1MinCheck = now;
        }
 
@@ -235,15 +209,13 @@ afs_Daemon(void)
 
         if (afsd_dynamic_vcaches && (last5MinCheck + 300 < now)) {
             /* start with trying to drop us back to our base usage */
-            int anumber;
-            if (afs_maxvcount <= afs_cacheStats)
-                anumber = VCACHE_FREE;
-            else
-                anumber = VCACHE_FREE + (afs_maxvcount - afs_cacheStats);
-
-           ObtainWriteLock(&afs_xvcache, 734);
-            afs_ShakeLooseVCaches(anumber);
-           ReleaseWriteLock(&afs_xvcache);
+            int anumber = VCACHE_FREE + (afs_vcount - afs_cacheStats);
+
+           if (anumber > 0) {
+               ObtainWriteLock(&afs_xvcache, 734);
+               afs_ShakeLooseVCaches(anumber);
+               ReleaseWriteLock(&afs_xvcache);
+           }
             last5MinCheck = now;
         }
 
@@ -330,7 +302,7 @@ afs_Daemon(void)
            if (afs_CheckServerDaemonStarted)
                afs_termState = AFSOP_STOP_CS;
            else
-               afs_termState = AFSOP_STOP_BKG;
+               afs_termState = AFSOP_STOP_TRUNCDAEMON;
            afs_osi_Wakeup(&afs_termState);
            return;
        }
@@ -387,7 +359,7 @@ afs_CheckRootVolume(void)
                 */
 #ifdef AFS_LINUX20_ENV
                {
-                   struct vrequest treq;
+                   struct vrequest *treq = NULL;
                    struct vattr vattr;
                    cred_t *credp;
                    struct dentry *dp;
@@ -398,9 +370,9 @@ afs_CheckRootVolume(void)
                    afs_rootFid.Fid.Unique = 1;
 
                    credp = crref();
-                   if (afs_InitReq(&treq, credp))
+                   if (afs_CreateReq(&treq, credp))
                        goto out;
-                   vcp = afs_GetVCache(&afs_rootFid, &treq, NULL, NULL);
+                   vcp = afs_GetVCache(&afs_rootFid, treq, NULL, NULL);
                    if (!vcp)
                        goto out;
                    afs_getattr(vcp, &vattr, credp);
@@ -409,19 +381,32 @@ afs_CheckRootVolume(void)
                    dp = d_find_alias(AFSTOV(afs_globalVp));
 
 #if defined(AFS_LINUX24_ENV)
+#if defined(HAVE_DCACHE_LOCK)
                    spin_lock(&dcache_lock);
+#else
+                   spin_lock(&AFSTOV(vcp)->i_lock);
+#endif
 #if defined(AFS_LINUX26_ENV)
                    spin_lock(&dp->d_lock);
 #endif
 #endif
+#if defined(D_ALIAS_IS_HLIST)
+                   hlist_del_init(&dp->d_alias);
+                   hlist_add_head(&dp->d_alias, &(AFSTOV(vcp)->i_dentry));
+#else
                    list_del_init(&dp->d_alias);
                    list_add(&dp->d_alias, &(AFSTOV(vcp)->i_dentry));
+#endif
                    dp->d_inode = AFSTOV(vcp);
 #if defined(AFS_LINUX24_ENV)
 #if defined(AFS_LINUX26_ENV)
                    spin_unlock(&dp->d_lock);
 #endif
+#if defined(HAVE_DCACHE_LOCK)
                    spin_unlock(&dcache_lock);
+#else
+                   spin_unlock(&AFSTOV(vcp)->i_lock);
+#endif
 #endif
                    dput(dp);
 
@@ -429,6 +414,7 @@ afs_CheckRootVolume(void)
                    afs_globalVp = vcp;
                out:
                    crfree(credp);
+                   afs_DestroyReq(treq);
                }
 #else
 #ifdef AFS_DARWIN80_ENV
@@ -466,12 +452,13 @@ BPath(struct brequest *ab)
     struct dentry *dp = NULL;
 #endif
     afs_size_t offset, len;
-    struct vrequest treq;
+    struct vrequest *treq = NULL;
     afs_int32 code;
 
     AFS_STATCNT(BPath);
-    if ((code = afs_InitReq(&treq, ab->cred)))
+    if ((code = afs_CreateReq(&treq, ab->cred))) {
        return;
+    }
     AFS_GUNLOCK();
 #ifdef AFS_LINUX22_ENV
     code = gop_lookupname((char *)ab->ptr_parm[0], AFS_UIOSYS, 1, &dp);
@@ -482,8 +469,10 @@ BPath(struct brequest *ab)
 #endif
     AFS_GLOCK();
     osi_FreeLargeSpace((char *)ab->ptr_parm[0]);       /* free path name buffer here */
-    if (code)
+    if (code) {
+       afs_DestroyReq(treq);
        return;
+    }
     /* now path may not have been in afs, so check that before calling our cache manager */
     if (!tvn || !IsAfsVnode(tvn)) {
        /* release it and give up */
@@ -494,11 +483,12 @@ BPath(struct brequest *ab)
            AFS_RELE(tvn);
 #endif
        }
+       afs_DestroyReq(treq);
        return;
     }
     tvc = VTOAFS(tvn);
     /* here we know its an afs vnode, so we can get the data for the chunk */
-    tdc = afs_GetDCache(tvc, ab->size_parm[0], &treq, &offset, &len, 1);
+    tdc = afs_GetDCache(tvc, ab->size_parm[0], treq, &offset, &len, 1);
     if (tdc) {
        afs_PutDCache(tdc);
     }
@@ -507,6 +497,7 @@ BPath(struct brequest *ab)
 #else
     AFS_RELE(tvn);
 #endif
+    afs_DestroyReq(treq);
 }
 
 /* size_parm 0 to the fetch is the chunk number,
@@ -519,15 +510,16 @@ BPrefetch(struct brequest *ab)
     struct dcache *tdc;
     struct vcache *tvc;
     afs_size_t offset, len, abyte, totallen = 0;
-    struct vrequest treq;
+    struct vrequest *treq = NULL;
+    int code;
 
     AFS_STATCNT(BPrefetch);
-    if ((len = afs_InitReq(&treq, ab->cred)))
+    if ((code = afs_CreateReq(&treq, ab->cred)))
        return;
     abyte = ab->size_parm[0];
     tvc = ab->vc;
     do {
-       tdc = afs_GetDCache(tvc, abyte, &treq, &offset, &len, 1);
+       tdc = afs_GetDCache(tvc, abyte, treq, &offset, &len, 1);
        if (tdc) {
            afs_PutDCache(tdc);
        }
@@ -551,22 +543,24 @@ BPrefetch(struct brequest *ab)
     if (ab->size_parm[1]) {
        afs_PutDCache(tdc);     /* put this one back, too */
     }
+    afs_DestroyReq(treq);
 }
 
 #if defined(AFS_CACHE_BYPASS)
 static void
 BPrefetchNoCache(struct brequest *ab)
 {
-    struct vrequest treq;
-    afs_size_t len;
+    struct vrequest *treq = NULL;
+    int code;
 
-    if ((len = afs_InitReq(&treq, ab->cred)))
+    if ((code = afs_CreateReq(&treq, ab->cred)))
        return;
 
 #ifndef UKERNEL
     /* OS-specific prefetch routine */
     afs_PrefetchNoCache(ab->vc, ab->cred, (struct nocache_read_request *) ab->ptr_parm[0]);
 #endif
+    afs_DestroyReq(treq);
 }
 #endif
 
@@ -575,15 +569,14 @@ BStore(struct brequest *ab)
 {
     struct vcache *tvc;
     afs_int32 code;
-    struct vrequest treq;
+    struct vrequest *treq = NULL;
 #if defined(AFS_SGI_ENV)
     struct cred *tmpcred;
 #endif
 
     AFS_STATCNT(BStore);
-    if ((code = afs_InitReq(&treq, ab->cred)))
+    if ((code = afs_CreateReq(&treq, ab->cred)))
        return;
-    code = 0;
     tvc = ab->vc;
 #if defined(AFS_SGI_ENV)
     /*
@@ -602,7 +595,7 @@ BStore(struct brequest *ab)
     AFS_RWLOCK((vnode_t *) tvc, 1);
 #endif
     ObtainWriteLock(&tvc->lock, 209);
-    code = afs_StoreOnLastReference(tvc, &treq);
+    code = afs_StoreOnLastReference(tvc, treq);
     ReleaseWriteLock(&tvc->lock);
 #if defined(AFS_SGI_ENV)
     OSI_SET_CURRENT_CRED(tmpcred);
@@ -610,13 +603,62 @@ BStore(struct brequest *ab)
 #endif
     /* now set final return code, and wakeup anyone waiting */
     if ((ab->flags & BUVALID) == 0) {
-       ab->code = afs_CheckCode(code, &treq, 43);      /* set final code, since treq doesn't go across processes */
+
+       /* To explain code_raw/code_checkcode:
+        * Anyone that's waiting won't have our treq, so they won't be able to
+        * call afs_CheckCode themselves on the return code we provide here.
+        * But if we give back only the afs_CheckCode value, they won't know
+        * what the "raw" value was. So give back both values, so the waiter
+        * can know the "raw" value for interpreting the value internally, as
+        * well as the afs_CheckCode value to give to the OS. */
+       ab->code_raw = code;
+       ab->code_checkcode = afs_CheckCode(code, treq, 430);
+
+       ab->flags |= BUVALID;
+       if (ab->flags & BUWAIT) {
+           ab->flags &= ~BUWAIT;
+           afs_osi_Wakeup(ab);
+       }
+    }
+    afs_DestroyReq(treq);
+}
+
+static void
+BPartialStore(struct brequest *ab)
+{
+    struct vcache *tvc;
+    afs_int32 code;
+    struct vrequest *treq = NULL;
+    int locked, shared_locked = 0;
+
+    AFS_STATCNT(BStore);
+    if ((code = afs_CreateReq(&treq, ab->cred)))
+       return;
+    tvc = ab->vc;
+    locked = tvc->lock.excl_locked? 1:0;
+    if (!locked)
+        ObtainWriteLock(&tvc->lock, 1209);
+    else if (!(tvc->lock.excl_locked & WRITE_LOCK)) {
+       shared_locked = 1;
+       ConvertSToRLock(&tvc->lock);
+    }
+    code = afs_StoreAllSegments(tvc, treq, AFS_ASYNC);
+    if (!locked)
+       ReleaseWriteLock(&tvc->lock);
+    else if (shared_locked)
+       ConvertSToRLock(&tvc->lock);
+    /* now set final return code, and wakeup anyone waiting */
+    if ((ab->flags & BUVALID) == 0) {
+       /* set final code, since treq doesn't go across processes */
+       ab->code_raw = code;
+       ab->code_checkcode = afs_CheckCode(code, treq, 43);
        ab->flags |= BUVALID;
        if (ab->flags & BUWAIT) {
            ab->flags &= ~BUWAIT;
            afs_osi_Wakeup(ab);
        }
     }
+    afs_DestroyReq(treq);
 }
 
 /* release a held request buffer */
@@ -666,7 +708,9 @@ afs_BQueue(short aopcode, struct vcache *avc,
            tb->opcode = aopcode;
            tb->vc = avc;
            tb->cred = acred;
-           crhold(tb->cred);
+           if (tb->cred) {
+               crhold(tb->cred);
+           }
            if (avc) {
                AFS_FAST_HOLD(avc);
            }
@@ -677,7 +721,7 @@ afs_BQueue(short aopcode, struct vcache *avc,
            tb->ptr_parm[1] = apparm1;
            tb->ptr_parm[2] = apparm2;
            tb->flags = 0;
-           tb->code = 0;
+           tb->code_raw = tb->code_checkcode = 0;
            tb->ts = afs_brs_count++;
            /* if daemons are waiting for work, wake them up */
            if (afs_brsDaemons > 0) {
@@ -1007,7 +1051,7 @@ brequest_release(struct brequest *tb)
     afs_BRelease(tb);  /* this grabs and releases afs_xbrs lock */
 }
 
-#ifdef AFS_DARWIN80_ENV
+#ifdef AFS_NEW_BKG
 int
 afs_BackgroundDaemon(struct afs_uspc_param *uspc, void *param1, void *param2)
 #else
@@ -1024,7 +1068,7 @@ afs_BackgroundDaemon(void)
        /* Irix with "short stack" exits */
        afs_BackgroundDaemon_once();
 
-#ifdef AFS_DARWIN80_ENV
+#ifdef AFS_NEW_BKG
     /* If it's a re-entering syscall, complete the request and release */
     if (uspc->ts > -1) {
         tb = afs_brs;
@@ -1048,7 +1092,7 @@ afs_BackgroundDaemon(void)
 #endif
         /* Otherwise it's a new one */
        afs_nbrs++;
-#ifdef AFS_DARWIN80_ENV
+#ifdef AFS_NEW_BKG
     }
 #endif
 
@@ -1059,10 +1103,10 @@ afs_BackgroundDaemon(void)
 
        if (afs_termState == AFSOP_STOP_BKG) {
            if (--afs_nbrs <= 0)
-               afs_termState = AFSOP_STOP_TRUNCDAEMON;
+               afs_termState = AFSOP_STOP_RXCALLBACK;
            ReleaseWriteLock(&afs_xbrs);
            afs_osi_Wakeup(&afs_termState);
-#ifdef AFS_DARWIN80_ENV
+#ifdef AFS_NEW_BKG
            return -2;
 #else
            return;
@@ -1112,6 +1156,8 @@ afs_BackgroundDaemon(void)
                 return 0;
             }
 #endif
+           else if (tb->opcode == BOP_PARTIAL_STORE)
+               BPartialStore(tb);
            else
                panic("background bop");
            brequest_release(tb);
@@ -1126,7 +1172,7 @@ afs_BackgroundDaemon(void)
            afs_brsDaemons--;
        }
     }
-#ifdef AFS_DARWIN80_ENV
+#ifdef AFS_NEW_BKG
     return -2;
 #endif
 }
@@ -1138,7 +1184,7 @@ shutdown_daemons(void)
     AFS_STATCNT(shutdown_daemons);
     if (afs_cold_shutdown) {
        afs_brsDaemons = brsInit = 0;
-       rxepoch_checked = afs_nbrs = 0;
+       afs_nbrs = 0;
        memset(afs_brs, 0, sizeof(afs_brs));
        memset(&afs_xbrs, 0, sizeof(afs_lock_t));
        afs_brsWaiters = 0;
@@ -1190,7 +1236,7 @@ afs_sgidaemon(void)
            SPUNLOCK(afs_sgibklock, s);
            AFS_GLOCK();
            tdc->dflags &= ~DFEntryMod;
-           afs_WriteDCache(tdc, 1);
+           osi_Assert(afs_WriteDCache(tdc, 1) == 0);
            AFS_GUNLOCK();
            s = SPLOCK(afs_sgibklock);
        }