/*
* Copyright 2000, International Business Machines Corporation and others.
* All Rights Reserved.
- *
+ *
* This software has been released under the terms of the IBM Public
* License. For details, see the LICENSE file in the top-level source
* directory or online at http://www.openafs.org/dl/license10.html
*/
+#include <afsconfig.h>
#include <afs/param.h>
+#include <roken.h>
+
#include <afs/stds.h>
#include <windows.h>
#include <string.h>
#include "afsd.h"
+#include "smb.h"
#include <rx/rx.h>
#include <rx/rx_prototypes.h>
long cm_daemonCheckVolInterval = 3600;
long cm_daemonCheckCBInterval = 60;
long cm_daemonCheckVolCBInterval = 0;
-long cm_daemonCheckLockInterval = 60;
+long cm_daemonCheckLockInterval = 2;
long cm_daemonTokenCheckInterval = 180;
long cm_daemonCheckOfflineVolInterval = 600;
long cm_daemonPerformanceTuningInterval = 0;
+long cm_daemonRankServerInterval = 600;
osi_rwlock_t cm_daemonLock;
cm_bkgRequest_t *cm_bkgListp; /* first elt in the list of requests */
cm_bkgRequest_t *cm_bkgListEndp; /* last elt in the list of requests */
+extern int powerStateSuspended;
int daemon_ShutdownFlag = 0;
static int cm_nDaemons = 0;
static time_t lastIPAddrChange = 0;
static EVENT_HANDLE cm_Daemon_ShutdownEvent = NULL;
+static EVENT_HANDLE cm_LockDaemon_ShutdownEvent = NULL;
static EVENT_HANDLE cm_IPAddrDaemon_ShutdownEvent = NULL;
-static EVENT_HANDLE cm_BkgDaemon_ShutdownEvent[CM_MAX_DAEMONS] =
+static EVENT_HANDLE cm_BkgDaemon_ShutdownEvent[CM_MAX_DAEMONS] =
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};
void cm_IpAddrDaemon(long parm)
while (daemon_ShutdownFlag == 0) {
DWORD Result;
-
+
thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
Result = NotifyAddrChange(NULL,NULL);
if (Result == NO_ERROR && daemon_ShutdownFlag == 0) {
smb_SetLanAdapterChangeDetected();
cm_SetLanAdapterChangeDetected();
thrd_ResetEvent(cm_IPAddrDaemon_ShutdownEvent);
- }
+ }
}
thrd_SetEvent(cm_IPAddrDaemon_ShutdownEvent);
cm_bkgRequest_t *rp;
afs_int32 code;
char name[32] = "";
- long daemonID = (long)parm;
+ long daemonID = (long)(LONG_PTR)parm;
- snprintf(name, sizeof(name), "cm_BkgDaemon_ShutdownEvent%d", daemonID);
+ snprintf(name, sizeof(name), "cm_BkgDaemon_ShutdownEvent%u", daemonID);
cm_BkgDaemon_ShutdownEvent[daemonID] = thrd_CreateEvent(NULL, FALSE, FALSE, name);
if ( GetLastError() == ERROR_ALREADY_EXISTS )
lock_ObtainWrite(&cm_daemonLock);
while (daemon_ShutdownFlag == 0) {
+ if (powerStateSuspended) {
+ Sleep(1000);
+ continue;
+ }
if (!cm_bkgListEndp) {
osi_SleepW((LONG_PTR)&cm_bkgListp, &cm_daemonLock);
lock_ObtainWrite(&cm_daemonLock);
continue;
}
-
+
/* we found a request */
for (rp = cm_bkgListEndp; rp; rp = (cm_bkgRequest_t *) osi_QPrev(&rp->q))
{
- if (cm_ServerAvailable(&rp->scp->fid, rp->userp) &&
- !(rp->scp->flags & CM_SCACHEFLAG_DATASTORING))
+ if (cm_ServerAvailable(&rp->scp->fid, rp->userp) ||
+ rp->scp->flags & CM_SCACHEFLAG_DELETED)
break;
}
if (rp == NULL) {
osi_Log1(afsd_logp,"cm_BkgDaemon processing request 0x%p", rp);
+ if (rp->scp->flags & CM_SCACHEFLAG_DELETED) {
+ osi_Log1(afsd_logp,"cm_BkgDaemon DELETED scp 0x%x",rp->scp);
+ code = CM_ERROR_BADFD;
+ } else {
#ifdef DEBUG_REFCOUNT
- osi_Log2(afsd_logp,"cm_BkgDaemon (before) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
+ osi_Log2(afsd_logp,"cm_BkgDaemon (before) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
#endif
- code = (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
-#ifdef DEBUG_REFCOUNT
- osi_Log2(afsd_logp,"cm_BkgDaemon (after) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
+ code = (*rp->procp)(rp->scp, rp->p1, rp->p2, rp->p3, rp->p4, rp->userp);
+#ifdef DEBUG_REFCOUNT
+ osi_Log2(afsd_logp,"cm_BkgDaemon (after) scp 0x%x ref %d",rp->scp, rp->scp->refCount);
#endif
+ }
- /*
+ /*
* Keep the following list synchronized with the
- * error code list in cm_BkgStore.
+ * error code list in cm_BkgStore.
* cm_SyncOpDone(CM_SCACHESYNC_ASYNCSTORE) will be called there unless
* one of these errors has occurred.
*/
- switch ( code ) {
- case CM_ERROR_TIMEDOUT: /* or server restarting */
- case CM_ERROR_RETRY:
- case CM_ERROR_WOULDBLOCK:
- case CM_ERROR_ALLBUSY:
- case CM_ERROR_ALLDOWN:
- case CM_ERROR_ALLOFFLINE:
- case CM_ERROR_PARTIALWRITE:
- osi_Log2(afsd_logp,"cm_BkgDaemon re-queueing failed request 0x%p code 0x%x",
- rp, code);
- lock_ObtainWrite(&cm_daemonLock);
- cm_bkgQueueCount++;
- osi_QAddT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **)&cm_bkgListEndp, &rp->q);
- break;
- case 0: /* success */
- default: /* other error */
- if (code == 0)
+ switch ( code ) {
+ case CM_ERROR_TIMEDOUT: /* or server restarting */
+ case CM_ERROR_RETRY:
+ case CM_ERROR_WOULDBLOCK:
+ case CM_ERROR_ALLBUSY:
+ case CM_ERROR_ALLDOWN:
+ case CM_ERROR_ALLOFFLINE:
+ case CM_ERROR_PARTIALWRITE:
+ if (rp->procp == cm_BkgStore) {
+ osi_Log2(afsd_logp,
+ "cm_BkgDaemon re-queueing failed request 0x%p code 0x%x",
+ rp, code);
+ lock_ObtainWrite(&cm_daemonLock);
+ cm_bkgQueueCount++;
+ osi_QAddT((osi_queue_t **) &cm_bkgListp, (osi_queue_t **)&cm_bkgListEndp, &rp->q);
+ break;
+ } /* otherwise fall through */
+ case 0: /* success */
+ default: /* other error */
+ if (code == 0) {
osi_Log1(afsd_logp,"cm_BkgDaemon SUCCESS: request 0x%p", rp);
- else
+ } else {
osi_Log2(afsd_logp,"cm_BkgDaemon FAILED: request dropped 0x%p code 0x%x",
- rp, code);
- cm_ReleaseUser(rp->userp);
- cm_ReleaseSCache(rp->scp);
- free(rp);
+ rp, code);
+ }
+ cm_ReleaseUser(rp->userp);
+ cm_ReleaseSCache(rp->scp);
+ free(rp);
lock_ObtainWrite(&cm_daemonLock);
- }
+ }
}
lock_ReleaseWrite(&cm_daemonLock);
-
thrd_SetEvent(cm_BkgDaemon_ShutdownEvent[daemonID]);
}
cm_user_t *userp)
{
cm_bkgRequest_t *rp;
-
+
rp = malloc(sizeof(*rp));
memset(rp, 0, sizeof(*rp));
-
+
cm_HoldSCache(scp);
rp->scp = scp;
cm_HoldUser(userp);
lock_ObtainWrite(&cm_daemonLock);
cm_bkgQueueCount++;
osi_QAdd((osi_queue_t **) &cm_bkgListp, &rp->q);
- if (!cm_bkgListEndp)
+ if (!cm_bkgListEndp)
cm_bkgListEndp = rp;
lock_ReleaseWrite(&cm_daemonLock);
if (code == ERROR_SUCCESS && dummy)
cm_daemonCheckOfflineVolInterval = dummy;
afsi_log("daemonCheckOfflineVolInterval is %d", cm_daemonCheckOfflineVolInterval);
-
+
dummyLen = sizeof(DWORD);
code = RegQueryValueEx(parmKey, "daemonPerformanceTuningInterval", NULL, NULL,
(BYTE *) &dummy, &dummyLen);
if (code == ERROR_SUCCESS)
cm_daemonPerformanceTuningInterval = dummy;
afsi_log("daemonPerformanceTuningInterval is %d", cm_daemonPerformanceTuningInterval);
-
+
+ dummyLen = sizeof(DWORD);
+ code = RegQueryValueEx(parmKey, "daemonRankServerInterval", NULL, NULL,
+ (BYTE *) &dummy, &dummyLen);
+ if (code == ERROR_SUCCESS && dummy)
+ cm_daemonRankServerInterval = dummy;
+ afsi_log("daemonRankServerInterval is %d", cm_daemonRankServerInterval);
+
RegCloseKey(parmKey);
if (cm_daemonPerformanceTuningInterval)
cm_PerformanceTuningInit();
}
+/* periodic lock check daemon */
+void cm_LockDaemon(long parm)
+{
+ time_t now;
+ time_t lastLockCheck;
+ char * name = "cm_LockDaemon_ShutdownEvent";
+
+ cm_LockDaemon_ShutdownEvent = thrd_CreateEvent(NULL, FALSE, FALSE, name);
+ if ( GetLastError() == ERROR_ALREADY_EXISTS )
+ afsi_log("Event Object Already Exists: %s", name);
+
+ now = osi_Time();
+ lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
+
+ while (daemon_ShutdownFlag == 0) {
+ if (powerStateSuspended) {
+ Sleep(1000);
+ continue;
+ }
+
+ now = osi_Time();
+
+ if (now > lastLockCheck + cm_daemonCheckLockInterval &&
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
+ lastLockCheck = now;
+ cm_CheckLocks();
+ if (daemon_ShutdownFlag == 1)
+ break;
+ }
+
+ thrd_Sleep(1000); /* sleep 1 second */
+ }
+ thrd_SetEvent(cm_LockDaemon_ShutdownEvent);
+}
+
/* periodic check daemon */
void cm_Daemon(long parm)
{
time_t now;
- time_t lastLockCheck;
time_t lastVolCheck;
time_t lastCBExpirationCheck;
time_t lastVolCBRenewalCheck;
time_t lastTokenCacheCheck;
time_t lastBusyVolCheck;
time_t lastPerformanceCheck;
+ time_t lastServerRankCheck;
char thostName[200];
unsigned long code;
struct hostent *thp;
afsi_log("No Windows Firewall detected");
}
+ if (cm_freelanceEnabled && cm_freelanceImportCellServDB)
+ cm_FreelanceImportCellServDB();
+
/* ping all file servers, up or down, with unauthenticated connection,
* to find out whether we have all our callbacks from the server still.
* Also, ping down VLDBs.
code = 0;
else
memcpy(&code, thp->h_addr_list[0], 4);
-
+
srand(ntohl(code));
cm_DaemonCheckInit();
lastCBExpirationCheck = now - cm_daemonCheckCBInterval/2 + (rand() % cm_daemonCheckCBInterval);
if (cm_daemonCheckVolCBInterval)
lastVolCBRenewalCheck = now - cm_daemonCheckVolCBInterval/2 + (rand() % cm_daemonCheckVolCBInterval);
- lastLockCheck = now - cm_daemonCheckLockInterval/2 + (rand() % cm_daemonCheckLockInterval);
lastDownServerCheck = now - cm_daemonCheckDownInterval/2 + (rand() % cm_daemonCheckDownInterval);
lastUpServerCheck = now - cm_daemonCheckUpInterval/2 + (rand() % cm_daemonCheckUpInterval);
lastTokenCacheCheck = now - cm_daemonTokenCheckInterval/2 + (rand() % cm_daemonTokenCheckInterval);
lastBusyVolCheck = now - cm_daemonCheckOfflineVolInterval/2 * (rand() % cm_daemonCheckOfflineVolInterval);
if (cm_daemonPerformanceTuningInterval)
lastPerformanceCheck = now - cm_daemonPerformanceTuningInterval/2 * (rand() % cm_daemonPerformanceTuningInterval);
+ lastServerRankCheck = now - cm_daemonRankServerInterval/2 * (rand() % cm_daemonRankServerInterval);
while (daemon_ShutdownFlag == 0) {
- /* check to see if the listener threads halted due to network
+ if (powerStateSuspended) {
+ Sleep(1000);
+ continue;
+ }
+ /* check to see if the listener threads halted due to network
* disconnect or other issues. If so, attempt to restart them.
*/
smb_RestartListeners(0);
default:
afsi_log("Unknown Windows Firewall Configuration error");
}
- }
+ }
/* find out what time it is */
now = osi_Time();
-
+
/* Determine whether an address change took place that we need to respond to */
if (bAddrChangeCheck)
bAddrChangeCheck = 0;
/* check down servers */
if ((bAddrChangeCheck || now > lastDownServerCheck + cm_daemonCheckDownInterval) &&
- daemon_ShutdownFlag == 0) {
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
lastDownServerCheck = now;
osi_Log0(afsd_logp, "cm_Daemon CheckDownServers");
cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
now = osi_Time();
}
- if (bAddrChangeCheck)
+ if (bAddrChangeCheck &&
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
cm_ForceNewConnectionsAllServers();
+ }
/* check up servers */
if ((bAddrChangeCheck || now > lastUpServerCheck + cm_daemonCheckUpInterval) &&
- daemon_ShutdownFlag == 0) {
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
lastUpServerCheck = now;
osi_Log0(afsd_logp, "cm_Daemon CheckUpServers");
cm_CheckServers(CM_FLAG_CHECKUPSERVERS, NULL);
now = osi_Time();
}
- if (bAddrChangeCheck) {
+ if (bAddrChangeCheck &&
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
smb_CheckVCs();
cm_VolStatus_Network_Addr_Change();
}
- if (now > lastVolCheck + cm_daemonCheckVolInterval &&
- daemon_ShutdownFlag == 0) {
+ /*
+ * Once every five minutes inspect the volume list and enforce
+ * the volume location expiration time.
+ */
+ if (now > lastVolCheck + 300 &&
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
lastVolCheck = now;
- cm_RefreshVolumes();
+ cm_RefreshVolumes(cm_daemonCheckVolInterval);
if (daemon_ShutdownFlag == 1)
break;
now = osi_Time();
}
- if (cm_daemonCheckVolCBInterval &&
+ /* Rank all up servers */
+ if ((now > lastServerRankCheck + cm_daemonRankServerInterval) &&
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
+ lastServerRankCheck = now;
+ osi_Log0(afsd_logp, "cm_Daemon RankServer");
+ cm_RankUpServers();
+ if(daemon_ShutdownFlag == 1)
+ break;
+ now = osi_Time();
+ }
+
+ if (cm_daemonCheckVolCBInterval &&
now > lastVolCBRenewalCheck + cm_daemonCheckVolCBInterval &&
- daemon_ShutdownFlag == 0) {
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
lastVolCBRenewalCheck = now;
cm_VolumeRenewROCallbacks();
if (daemon_ShutdownFlag == 1)
}
if ((bAddrChangeCheck || now > lastBusyVolCheck + cm_daemonCheckOfflineVolInterval) &&
- daemon_ShutdownFlag == 0) {
- lastVolCheck = now;
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
+ lastBusyVolCheck = now;
cm_CheckOfflineVolumes();
if (daemon_ShutdownFlag == 1)
break;
}
if (now > lastCBExpirationCheck + cm_daemonCheckCBInterval &&
- daemon_ShutdownFlag == 0) {
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
lastCBExpirationCheck = now;
cm_CheckCBExpiration();
if (daemon_ShutdownFlag == 1)
now = osi_Time();
}
- if (now > lastLockCheck + cm_daemonCheckLockInterval &&
- daemon_ShutdownFlag == 0) {
- lastLockCheck = now;
- cm_CheckLocks();
- if (daemon_ShutdownFlag == 1)
- break;
- now = osi_Time();
- }
-
if (now > lastTokenCacheCheck + cm_daemonTokenCheckInterval &&
- daemon_ShutdownFlag == 0) {
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
lastTokenCacheCheck = now;
cm_CheckTokenCache(now);
if (daemon_ShutdownFlag == 1)
{
SetEvent(WaitToTerminate);
}
- }
- if (daemon_ShutdownFlag == 1) {
- break;
+ if (daemon_ShutdownFlag == 1) {
+ break;
+ }
+ now = osi_Time();
}
if (cm_daemonPerformanceTuningInterval &&
now > lastPerformanceCheck + cm_daemonPerformanceTuningInterval &&
- daemon_ShutdownFlag == 0) {
+ daemon_ShutdownFlag == 0 &&
+ powerStateSuspended == 0) {
lastPerformanceCheck = now;
cm_PerformanceTuningCheck();
if (daemon_ShutdownFlag == 1)
break;
now = osi_Time();
}
-
- thrd_Sleep(10000); /* sleep 10 seconds */
+
+ /*
+ * sleep .5 seconds. if the thread blocks for a long time
+ * we risk not being able to close the cache before Windows
+ * kills our process during system shutdown.
+ */
+ thrd_Sleep(500);
}
+
thrd_SetEvent(cm_Daemon_ShutdownEvent);
-}
+}
void cm_DaemonShutdown(void)
{
osi_Wakeup((LONG_PTR) &cm_bkgListp);
/* wait for shutdown */
- if (cm_Daemon_ShutdownEvent)
- code = thrd_WaitForSingleObject_Event(cm_Daemon_ShutdownEvent, INFINITE);
-
for ( i=0; i<cm_nDaemons; i++) {
if (cm_BkgDaemon_ShutdownEvent[i])
code = thrd_WaitForSingleObject_Event(cm_BkgDaemon_ShutdownEvent[i], INFINITE);
}
+ if (cm_Daemon_ShutdownEvent)
+ code = thrd_WaitForSingleObject_Event(cm_Daemon_ShutdownEvent, INFINITE);
+
+ if (cm_LockDaemon_ShutdownEvent)
+ code = thrd_WaitForSingleObject_Event(cm_LockDaemon_ShutdownEvent, INFINITE);
+
+#if 0
+ /*
+ * Do not waste precious time waiting for the ipaddr daemon to shutdown.
+ * When it does it means we have lost our network connection and we need
+ * it during cache shutdown in order to notify the file servers that this
+ * client is giving up all callbacks.
+ */
if (cm_IPAddrDaemon_ShutdownEvent)
code = thrd_WaitForSingleObject_Event(cm_IPAddrDaemon_ShutdownEvent, INFINITE);
+#endif
}
void cm_InitDaemon(int nDaemons)
int i;
cm_nDaemons = (nDaemons > CM_MAX_DAEMONS) ? CM_MAX_DAEMONS : nDaemons;
-
+
if (osi_Once(&once)) {
- lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock",
+ lock_InitializeRWLock(&cm_daemonLock, "cm_daemonLock",
LOCK_HIERARCHY_DAEMON_GLOBAL);
osi_EndOnce(&once);
osi_assertx(phandle != NULL, "cm_Daemon thread creation failure");
thrd_CloseHandle(phandle);
+ phandle = thrd_Create((SecurityAttrib) 0, 0,
+ (ThreadFunc) cm_LockDaemon, 0, 0, &pid, "cm_LockDaemon");
+ osi_assertx(phandle != NULL, "cm_LockDaemon thread creation failure");
+ thrd_CloseHandle(phandle);
+
for(i=0; i < cm_nDaemons; i++) {
phandle = thrd_Create((SecurityAttrib) 0, 0,
- (ThreadFunc) cm_BkgDaemon, (LPVOID)i, 0, &pid,
+ (ThreadFunc) cm_BkgDaemon, (LPVOID)(LONG_PTR)i, 0, &pid,
"cm_BkgDaemon");
osi_assertx(phandle != NULL, "cm_BkgDaemon thread creation failure");
thrd_CloseHandle(phandle);