windows-smb-started-synchronization-20080108
[openafs.git] / src / WINNT / afsd / afsd_service.c
index 53d750d..ac58d2a 100644 (file)
@@ -1,13 +1,4 @@
-/*
- * 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 <afs/param.h>
 #include <afs/stds.h>
 
 #include <windows.h>
@@ -18,6 +9,7 @@
 #include <setjmp.h>
 #include "afsd.h"
 #include "afsd_init.h"
+#include "lanahelper.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <winsock2.h>
@@ -31,6 +23,7 @@
 #ifdef _DEBUG
 #include <crtdbg.h>
 #endif
+#include "afsdifs.h"
 
 //#define REGISTER_POWER_NOTIFICATIONS 1
 #include "afsd_flushvol.h"
@@ -41,10 +34,13 @@ static SERVICE_STATUS               ServiceStatus;
 static SERVICE_STATUS_HANDLE   StatusHandle;
 
 HANDLE hAFSDMainThread = NULL;
+#ifdef AFSIFS
+HANDLE hAFSDWorkerThread[WORKER_THREADS];
+#endif
 
 HANDLE WaitToTerminate;
 
-int GlobalStatus;
+static int GlobalStatus;
 
 #ifdef JUMP
 unsigned int MainThreadId;
@@ -54,27 +50,25 @@ jmp_buf notifier_jmp;
 extern int traceOnPanic;
 extern HANDLE afsi_file;
 
-int powerEventsRegistered = 0;
+static int powerEventsRegistered = 0;
+extern int powerStateSuspended = 0;
 
 /*
  * Notifier function for use by osi_panic
  */
 static void afsd_notifier(char *msgp, char *filep, long line)
 {
-    char tbuffer[512];
-    char *ptbuf[1];
-    HANDLE h;
+#ifdef AFSIFS
+    int i;
+#endif
+    if (!msgp)
+        msgp = "unspecified assert";
 
     if (filep)
-        sprintf(tbuffer, "Error at file %s, line %d: %s",
+       LogEvent(EVENTLOG_ERROR_TYPE, MSG_SERVICE_ERROR_STOP_WITH_MSG_AND_LOCATION, 
                  filep, line, msgp);
     else
-        sprintf(tbuffer, "Error at unknown location: %s", msgp);
-
-    h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
-    ptbuf[0] = tbuffer;
-    ReportEvent(h, EVENTLOG_ERROR_TYPE, 0, line, NULL, 1, 0, ptbuf, NULL);
-    DeregisterEventSource(h);
+       LogEvent(EVENTLOG_ERROR_TYPE, MSG_SERVICE_ERROR_STOP_WITH_MSG, msgp);
 
     GlobalStatus = line;
 
@@ -84,6 +78,10 @@ static void afsd_notifier(char *msgp, char *filep, long line)
     buf_ForceTrace(TRUE);
 
     afsi_log("--- begin dump ---");
+    cm_MemDumpDirStats(afsi_file, "a", 0);
+    cm_MemDumpBPlusStats(afsi_file, "a", 0);
+    cm_DumpCells(afsi_file, "a", 0);
+    cm_DumpVolumes(afsi_file, "a", 0);
     cm_DumpSCache(afsi_file, "a", 0);
 #ifdef keisa
     cm_dnlcDump(afsi_file, "a");
@@ -93,10 +91,16 @@ static void afsd_notifier(char *msgp, char *filep, long line)
     afsi_log("--- end   dump ---");
     
 #ifdef DEBUG
-    DebugBreak();      
+    if (IsDebuggerPresent())
+        DebugBreak();  
 #endif
 
     SetEvent(WaitToTerminate);
+#ifdef AFSIFS
+    WaitForMultipleObjects(WORKER_THREADS, hAFSDWorkerThread, TRUE, INFINITE);
+    for (i = 0; i < WORKER_THREADS; i++)
+        CloseHandle(hAFSDWorkerThread[i]);
+#endif
 
 #ifdef JUMP
     if (GetCurrentThreadId() == MainThreadId)
@@ -179,7 +183,8 @@ afsd_ServiceControlHandler(DWORD ctrlCode)
             afsi_log("SERVICE_CONTROL_SHUTDOWN");
 
         /* Write all dirty buffers back to server */
-        buf_CleanAndReset();
+       if ( !lana_OnlyLoopback() )
+           buf_CleanAndReset();
 
         /* Force trace if requested */
         code = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
@@ -234,11 +239,22 @@ afsd_ServiceControlHandlerEx(
     DWORD dummyLen, doTrace;
     long code;
     DWORD dwRet = ERROR_CALL_NOT_IMPLEMENTED;
+    OSVERSIONINFO osVersion;
+
+    /* Get the version of Windows */
+    memset(&osVersion, 0x00, sizeof(osVersion));
+    osVersion.dwOSVersionInfoSize = sizeof(osVersion);
+    GetVersionEx(&osVersion);
 
     switch (ctrlCode) 
     {
     case SERVICE_CONTROL_SHUTDOWN:
     case SERVICE_CONTROL_STOP:
+       if (ctrlCode == SERVICE_CONTROL_SHUTDOWN)
+           afsi_log("SERVICE_CONTROL_SHUTDOWN");
+       else
+            afsi_log("SERVICE_CONTROL_STOP");
+
         ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
         ServiceStatus.dwWin32ExitCode = NO_ERROR;
         ServiceStatus.dwCheckPoint = 1;
@@ -247,7 +263,8 @@ afsd_ServiceControlHandlerEx(
         SetServiceStatus(StatusHandle, &ServiceStatus);
 
         /* Write all dirty buffers back to server */
-        buf_CleanAndReset();
+       if ( !lana_OnlyLoopback() )
+           buf_CleanAndReset();
 
         /* Force trace if requested */
         code = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
@@ -288,7 +305,9 @@ afsd_ServiceControlHandlerEx(
         /* XXX handle pause & continue */
     case SERVICE_CONTROL_POWEREVENT:                                              
         { 
-            afsi_log("SERVICE_CONTROL_POWEREVENT");
+#ifdef DEBUG
+           afsi_log("SERVICE_CONTROL_POWEREVENT");
+#endif
             /*                                                                                
             ** dwEventType of this notification == WPARAM of WM_POWERBROADCAST               
             ** Return NO_ERROR == return TRUE for that message, i.e. accept request          
@@ -301,34 +320,56 @@ afsd_ServiceControlHandlerEx(
                 case PBT_APMQUERYSUSPEND:       
                     afsi_log("SERVICE_CONTROL_APMQUERYSUSPEND"); 
                     /* Write all dirty buffers back to server */
-                    buf_CleanAndReset();
+                   if ( !lana_OnlyLoopback() ) {
+                       buf_CleanAndReset();
+                        cm_SuspendSCache();
+                    }
+                    afsi_log("SERVICE_CONTROL_APMQUERYSUSPEND buf_CleanAndReset complete"); 
                     dwRet = NO_ERROR;                       
                     break;                                  
                 case PBT_APMQUERYSTANDBY:                                                         
                     afsi_log("SERVICE_CONTROL_APMQUERYSTANDBY"); 
                     /* Write all dirty buffers back to server */
-                    buf_CleanAndReset();
+                   if ( !lana_OnlyLoopback() ) {
+                       buf_CleanAndReset();
+                        cm_SuspendSCache();
+                    }
+                    afsi_log("SERVICE_CONTROL_APMQUERYSTANDBY buf_CleanAndReset complete"); 
                     dwRet = NO_ERROR;                                                             
                     break;                                                                        
                                                                                                                          
                     /* allow remaining case PBT_WhatEver */                                           
                 case PBT_APMSUSPEND:                         
-                    afsi_log("SERVICE_CONTROL_APMSUSPEND"); 
+                    afsi_log("SERVICE_CONTROL_APMSUSPEND");
+                   powerStateSuspended = 1;
+                   if (osVersion.dwMajorVersion >= 6) {
+                        cm_SuspendSCache();
+                       smb_StopListeners(0);
+                    }
                     dwRet = NO_ERROR;                       
                     break;                                  
                 case PBT_APMSTANDBY:                  
                     afsi_log("SERVICE_CONTROL_APMSTANDBY"); 
+                   powerStateSuspended = 1;
+                   if (osVersion.dwMajorVersion >= 6) {
+                        cm_SuspendSCache();
+                       smb_StopListeners(0);
+                    }
                     dwRet = NO_ERROR;                       
                     break;                                  
                 case PBT_APMRESUMECRITICAL:             
                     afsi_log("SERVICE_CONTROL_APMRESUMECRITICAL"); 
+                   if (osVersion.dwMajorVersion >= 6)
+                       smb_RestartListeners(0);
                     dwRet = NO_ERROR;                       
                     break;                                  
                 case PBT_APMRESUMESUSPEND:                                                        
+                   /* User logged in after suspend */
                     afsi_log("SERVICE_CONTROL_APMRESUMESUSPEND"); 
                     dwRet = NO_ERROR;                       
                     break;                                  
-                case PBT_APMRESUMESTANDBY:                                                        
+                case PBT_APMRESUMESTANDBY:            
+                   /* User logged in after standby */
                     afsi_log("SERVICE_CONTROL_APMRESUMESTANDBY"); 
                     dwRet = NO_ERROR;                       
                     break;                                  
@@ -337,15 +378,23 @@ afsd_ServiceControlHandlerEx(
                     dwRet = NO_ERROR;                       
                     break;                                  
                 case PBT_APMPOWERSTATUSCHANGE:                                                    
-                    afsi_log("SERVICE_CONTROL_APMPOWERSTATUSCHANGE"); 
+#ifdef DEBUG
+                   afsi_log("SERVICE_CONTROL_APMPOWERSTATUSCHANGE");
+#endif
                     dwRet = NO_ERROR;                       
                     break;                                  
                 case PBT_APMOEMEVENT:                                                             
+#ifdef DEBUG
                     afsi_log("SERVICE_CONTROL_APMOEMEVENT"); 
+#endif
                     dwRet = NO_ERROR;                       
                     break;                                  
-                case PBT_APMRESUMEAUTOMATIC:                                                      
+                case PBT_APMRESUMEAUTOMATIC:          
+                   /* This is the message delivered once all devices are up */
                     afsi_log("SERVICE_CONTROL_APMRESUMEAUTOMATIC"); 
+                   powerStateSuspended = 0;
+                   if (osVersion.dwMajorVersion >= 6)
+                       smb_RestartListeners(0);
                     dwRet = NO_ERROR;                       
                     break;                                  
                 default:                                                                          
@@ -354,6 +403,14 @@ afsd_ServiceControlHandlerEx(
                 }   
             }
         }
+        break;
+    case SERVICE_CONTROL_CUSTOM_DUMP: 
+        {
+            afsi_log("SERVICE_CONTROL_CUSTOM_DUMP"); 
+            GenerateMiniDump(NULL);
+           dwRet = NO_ERROR;
+        }
+        break;
     }          /* end switch(ctrlCode) */                                                        
     return dwRet;   
 }
@@ -363,10 +420,13 @@ afsd_ServiceControlHandlerEx(
  * Mount a drive into AFS if there global mapping
  */
 /* DEE Could check first if we are run as SYSTEM */
-#define MAX_RETRIES 30
-static void MountGlobalDrives(void)
+#define MAX_RETRIES 10
+#define MAX_DRIVES  23
+static DWORD __stdcall MountGlobalDrivesThread(void * notUsed)
 {
+#ifndef AFSIFS
     char szAfsPath[_MAX_PATH];
+#endif
     char szDriveToMapTo[5];
     DWORD dwResult;
     char szKeyName[256];
@@ -381,9 +441,9 @@ static void MountGlobalDrives(void)
 
     dwResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_QUERY_VALUE, &hKey);
     if (dwResult != ERROR_SUCCESS)
-        return;
+        return 0;
 
-    while (dwRetry < MAX_RETRIES) {
+    while (dwIndex < MAX_DRIVES) {
         dwDriveSize = sizeof(szDriveToMapTo);
         dwSubMountSize = sizeof(szSubMount);
         dwResult = RegEnumValue(hKey, dwIndex++, szDriveToMapTo, &dwDriveSize, 0, &dwType, szSubMount, &dwSubMountSize);
@@ -395,21 +455,22 @@ static void MountGlobalDrives(void)
             }
         }
 
-        for ( ; dwRetry < MAX_RETRIES; dwRetry++)
-               {
-                   NETRESOURCE nr;
-                   memset (&nr, 0x00, sizeof(NETRESOURCE));
-                   sprintf(szAfsPath,"\\\\%s\\%s",cm_NetbiosName,szSubMount);
-                   
-                   nr.dwScope = RESOURCE_GLOBALNET;              /* ignored parameter */
-                   nr.dwType=RESOURCETYPE_DISK;
-                   nr.lpLocalName=szDriveToMapTo;
-                   nr.lpRemoteName=szAfsPath;
-                   nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE; /* ignored parameter */
-                   nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;       /* ignored parameter */
-
-                   dwResult = WNetAddConnection2(&nr,NULL,NULL,0);
+#ifndef AFSIFS
+        for (dwRetry = 0 ; dwRetry < MAX_RETRIES; dwRetry++)
+        {
+            NETRESOURCE nr;
+            memset (&nr, 0x00, sizeof(NETRESOURCE));
+
+            sprintf(szAfsPath,"\\\\%s\\%s",cm_NetbiosName,szSubMount);
+
+            nr.dwScope = RESOURCE_GLOBALNET;              /* ignored parameter */
+            nr.dwType=RESOURCETYPE_DISK;
+            nr.lpLocalName=szDriveToMapTo;
+            nr.lpRemoteName=szAfsPath;
+            nr.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE; /* ignored parameter */
+            nr.dwUsage = RESOURCEUSAGE_CONNECTABLE;       /* ignored parameter */
+
+            dwResult = WNetAddConnection2(&nr,NULL,NULL,0);
             afsi_log("GlobalAutoMap of %s to %s %s (%d)", szDriveToMapTo, szSubMount, 
                      (dwResult == NO_ERROR) ? "succeeded" : "failed", dwResult);
             if (dwResult == NO_ERROR) {
@@ -421,23 +482,63 @@ static void MountGlobalDrives(void)
             /* Disconnect any previous mappings */
             dwResult = WNetCancelConnection2(szDriveToMapTo, 0, TRUE);
         }
+#else
+       /* FIXFIX: implement */
+       afsi_log("GlobalAutoMap of %s to %s not implemented", szDriveToMapTo, szSubMount);
+#endif
     }        
 
     RegCloseKey(hKey);
+    return 0;
+}
+
+static HANDLE hThreadMountGlobalDrives = NULL;
+
+static void MountGlobalDrives()
+{
+    DWORD tid;
+
+    hThreadMountGlobalDrives = CreateThread(NULL, 0, MountGlobalDrivesThread, 0, 0, &tid);
+
+    if ( hThreadMountGlobalDrives ) {
+        DWORD rc = WaitForSingleObject( hThreadMountGlobalDrives, 15000 );
+       if (rc == WAIT_TIMEOUT) {
+           afsi_log("GlobalAutoMap thread failed to complete after 15 seconds");
+       } else if (rc == WAIT_OBJECT_0) {
+           afsi_log("GlobalAutoMap thread completed");
+           CloseHandle( hThreadMountGlobalDrives );
+           hThreadMountGlobalDrives = NULL;
+       }
+    }
 }
 
 static void DismountGlobalDrives()
 {
+#ifndef AFSIFS
     char szAfsPath[_MAX_PATH];
     char szDriveToMapTo[5];
-    DWORD dwResult;
-    char szKeyName[256];
-    HKEY hKey;
-    DWORD dwIndex = 0;
     DWORD dwDriveSize;
     DWORD dwSubMountSize;
     char szSubMount[256];
     DWORD dwType;
+#endif
+    DWORD dwResult;
+    char szKeyName[256];
+    HKEY hKey;
+    DWORD dwIndex = 0;
+
+    if ( hThreadMountGlobalDrives ) {
+        DWORD rc = WaitForSingleObject(hThreadMountGlobalDrives, 0);
+
+       if (rc == WAIT_TIMEOUT) {
+           afsi_log("GlobalAutoMap thread failed to complete before service shutdown");
+       }
+       else if (rc == WAIT_OBJECT_0) {
+           afsi_log("GlobalAutoMap thread completed");
+           CloseHandle( hThreadMountGlobalDrives );
+           hThreadMountGlobalDrives = NULL;
+       }
+    }
 
     sprintf(szKeyName, "%s\\GlobalAutoMapper", AFSREG_CLT_SVC_PARAM_SUBKEY);
 
@@ -445,7 +546,10 @@ static void DismountGlobalDrives()
     if (dwResult != ERROR_SUCCESS)
         return;
 
-    while (1) {
+#ifdef AFSIFS    
+    /* FIXFIX: implement */
+#else
+    while (dwIndex < MAX_DRIVES) {
         dwDriveSize = sizeof(szDriveToMapTo);
         dwSubMountSize = sizeof(szSubMount);
         dwResult = RegEnumValue(hKey, dwIndex++, szDriveToMapTo, &dwDriveSize, 0, &dwType, szSubMount, &dwSubMountSize);
@@ -464,6 +568,7 @@ static void DismountGlobalDrives()
         
         afsi_log("Disconnect from GlobalAutoMap of %s to %s %s", szDriveToMapTo, szSubMount, dwResult ? "succeeded" : "failed");
     }        
+#endif
 
     RegCloseKey(hKey);
 }
@@ -900,7 +1005,11 @@ BOOL AFSModulesVerify(void)
         RegCloseKey (parmKey);
     }
 
-    if (verifyServiceSig && cacheSize < 716800) {
+    if (verifyServiceSig 
+#ifndef _WIN64
+         && cacheSize < 716800
+#endif
+         ) {
         trustVerified = VerifyTrust(filename);
     } else {
         afsi_log("Signature Verification disabled");
@@ -987,10 +1096,6 @@ BOOL AFSModulesVerify(void)
     return success;
 }
 
-typedef BOOL ( APIENTRY * AfsdInitHook )(void);
-#define AFSD_INIT_HOOK "AfsdInitHook"
-#define AFSD_HOOK_DLL  "afsdhook.dll"
-
 /*
 control serviceex exists only on 2000/xp. These functions will be loaded dynamically.
 */
@@ -1009,15 +1114,20 @@ afsd_Main(DWORD argc, LPTSTR *argv)
 #ifdef JUMP
     int jmpret;
 #endif /* JUMP */
-    HANDLE hInitHookDll;
-    HANDLE hAdvApi32;
-    AfsdInitHook initHook;
+    HMODULE hHookDll;
+    HMODULE hAdvApi32;
+#ifdef AFSIFS
+    int cnt;
+#endif
 
 #ifdef _DEBUG
+    afsd_DbgBreakAllocInit();
     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF /*| _CRTDBG_CHECK_ALWAYS_DF*/ | 
                    _CRTDBG_CHECK_CRT_DF /* | _CRTDBG_DELAY_FREE_MEM_DF */ );
 #endif 
 
+    afsd_SetUnhandledExceptionFilter();
+       
     osi_InitPanic(afsd_notifier);
     osi_InitTraceOption();
 
@@ -1053,19 +1163,13 @@ afsd_Main(DWORD argc, LPTSTR *argv)
     ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
     ServiceStatus.dwWin32ExitCode = NO_ERROR;
     ServiceStatus.dwCheckPoint = 1;
-    ServiceStatus.dwWaitHint = 30000;
+    ServiceStatus.dwWaitHint = 120000;
     /* accept Power Events */
-    ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT;
+    ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_PARAMCHANGE;
     SetServiceStatus(StatusHandle, &ServiceStatus);
 #endif
 
-    {       
-    HANDLE h; char *ptbuf[1];
-    h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
-    ptbuf[0] = "AFS start pending";
-    ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
-    DeregisterEventSource(h);
-    }
+    LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_SERVICE_START_PENDING);
 
 #ifdef REGISTER_POWER_NOTIFICATIONS
     {
@@ -1103,23 +1207,25 @@ afsd_Main(DWORD argc, LPTSTR *argv)
         ServiceStatus.dwWaitHint = 0;
         ServiceStatus.dwControlsAccepted = 0;
         SetServiceStatus(StatusHandle, &ServiceStatus);
-                       
+
+       LogEvent(EVENTLOG_ERROR_TYPE, MSG_SERVICE_INCORRECT_VERSIONS);
+
         /* exit if initialization failed */
         return;
     }
 
     /* allow an exit to be called prior to any initialization */
-    hInitHookDll = LoadLibrary(AFSD_HOOK_DLL);
-    if (hInitHookDll)
+    hHookDll = LoadLibrary(AFSD_HOOK_DLL);
+    if (hHookDll)
     {
-        BOOL hookRc = FALSE;
-        initHook = ( AfsdInitHook ) GetProcAddress(hInitHookDll, AFSD_INIT_HOOK);
+        BOOL hookRc = TRUE;
+        AfsdInitHook initHook = ( AfsdInitHook ) GetProcAddress(hHookDll, AFSD_INIT_HOOK);
         if (initHook)
         {
             hookRc = initHook();
         }
-        FreeLibrary(hInitHookDll);
-        hInitHookDll = NULL;
+        FreeLibrary(hHookDll);
+        hHookDll = NULL;
 
         if (hookRc == FALSE)
         {
@@ -1135,19 +1241,22 @@ afsd_Main(DWORD argc, LPTSTR *argv)
         }
         else
         {
-            /* allow another 15 seconds to start */
+            /* allow another 120 seconds to start */
             ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
             ServiceStatus.dwServiceSpecificExitCode = 0;
             ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
             ServiceStatus.dwWin32ExitCode = NO_ERROR;
             ServiceStatus.dwCheckPoint = 2;
-            ServiceStatus.dwWaitHint = 20000;
+            ServiceStatus.dwWaitHint = 120000;
             /* accept Power Events */
-            ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT;
+            ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_PARAMCHANGE;
             SetServiceStatus(StatusHandle, &ServiceStatus);
         }
     }
 
+    /* Perform Volume Status Notification Initialization */
+    cm_VolStatus_Initialization();
+
 #ifdef JUMP
     MainThreadId = GetCurrentThreadId();
     jmpret = setjmp(notifier_jmp);
@@ -1162,8 +1271,8 @@ afsd_Main(DWORD argc, LPTSTR *argv)
         }
 
 #ifndef NOTSERVICE
-        ServiceStatus.dwCheckPoint++;
-        ServiceStatus.dwWaitHint -= 5000;
+        ServiceStatus.dwCheckPoint = 3;
+        ServiceStatus.dwWaitHint = 30000;
         SetServiceStatus(StatusHandle, &ServiceStatus);
 #endif
         code = afsd_InitDaemons(&reason);
@@ -1172,51 +1281,168 @@ afsd_Main(DWORD argc, LPTSTR *argv)
                        osi_panic(reason, __FILE__, __LINE__);
         }
 
+        /* allow an exit to be called post rx initialization */
+        hHookDll = LoadLibrary(AFSD_HOOK_DLL);
+        if (hHookDll)
+        {
+            BOOL hookRc = TRUE;
+            AfsdRxStartedHook rxStartedHook = ( AfsdRxStartedHook ) GetProcAddress(hHookDll, AFSD_RX_STARTED_HOOK);
+            if (rxStartedHook)
+            {
+                hookRc = rxStartedHook();
+            }
+            FreeLibrary(hHookDll);
+            hHookDll = NULL;
+
+            if (hookRc == FALSE)
+            {
+                ServiceStatus.dwCurrentState = SERVICE_STOPPED;
+                ServiceStatus.dwWin32ExitCode = NO_ERROR;
+                ServiceStatus.dwCheckPoint = 0;
+                ServiceStatus.dwWaitHint = 0;
+                ServiceStatus.dwControlsAccepted = 0;
+                SetServiceStatus(StatusHandle, &ServiceStatus);
+                       
+                /* exit if initialization failed */
+                return;
+            }
+        }
+
 #ifndef NOTSERVICE
-        ServiceStatus.dwCheckPoint++;
-        ServiceStatus.dwWaitHint -= 5000;
+        ServiceStatus.dwCheckPoint = 4;
+        ServiceStatus.dwWaitHint = 15000;
         SetServiceStatus(StatusHandle, &ServiceStatus);
 #endif
+
+        /* Notify any volume status handlers that the cache manager has started */
+        cm_VolStatus_Service_Started();
+
+/* the following ifdef chooses the mode of operation for the service.  to enable
+ * a runtime flag (instead of compile-time), pioctl() would need to dynamically
+ * determine the mode, in order to use the correct ioctl special-file path. */
+#ifndef AFSIFS
         code = afsd_InitSMB(&reason, MessageBox);
         if (code != 0) {
             afsi_log("afsd_InitSMB failed: %s (code = %d)", reason, code);
             osi_panic(reason, __FILE__, __LINE__);
         }
+#else
+        code = ifs_Init(&reason);
+        if (code != 0) {
+            afsi_log("ifs_Init failed: %s (code = %d)", reason, code);
+            osi_panic(reason, __FILE__, __LINE__);
+        }     
+        for (cnt = 0; cnt < WORKER_THREADS; cnt++)
+            hAFSDWorkerThread[cnt] = CreateThread(NULL, 0, ifs_MainLoop, 0, 0, NULL);
+#endif  
+
+        /* allow an exit to be called post smb initialization */
+        hHookDll = LoadLibrary(AFSD_HOOK_DLL);
+        if (hHookDll)
+        {
+            BOOL hookRc = TRUE;
+            AfsdSmbStartedHook smbStartedHook = ( AfsdSmbStartedHook ) GetProcAddress(hHookDll, AFSD_SMB_STARTED_HOOK);
+            if (smbStartedHook)
+            {
+                hookRc = smbStartedHook();
+            }
+            FreeLibrary(hHookDll);
+            hHookDll = NULL;
+
+            if (hookRc == FALSE)
+            {
+                ServiceStatus.dwCurrentState = SERVICE_STOPPED;
+                ServiceStatus.dwWin32ExitCode = NO_ERROR;
+                ServiceStatus.dwCheckPoint = 0;
+                ServiceStatus.dwWaitHint = 0;
+                ServiceStatus.dwControlsAccepted = 0;
+                SetServiceStatus(StatusHandle, &ServiceStatus);
+                       
+                /* exit if initialization failed */
+                return;
+            }
+        }
 
         MountGlobalDrives();
 
 #ifndef NOTSERVICE
         ServiceStatus.dwCurrentState = SERVICE_RUNNING;
         ServiceStatus.dwWin32ExitCode = NO_ERROR;
-        ServiceStatus.dwCheckPoint = 0;
+        ServiceStatus.dwCheckPoint = 5;
         ServiceStatus.dwWaitHint = 0;
 
         /* accept Power events */
-        ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT;
+        ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_PARAMCHANGE;
         SetServiceStatus(StatusHandle, &ServiceStatus);
 #endif  
+
+       LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_SERVICE_RUNNING);
+    }
+
+    /* allow an exit to be called when started */
+    hHookDll = LoadLibrary(AFSD_HOOK_DLL);
+    if (hHookDll)
+    {
+        BOOL hookRc = TRUE;
+        AfsdStartedHook startedHook = ( AfsdStartedHook ) GetProcAddress(hHookDll, AFSD_STARTED_HOOK);
+        if (startedHook)
         {
-           HANDLE h; char *ptbuf[1];
-            h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
-            ptbuf[0] = "AFS running";
-            ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
-            DeregisterEventSource(h);
+            hookRc = startedHook();
+        }
+        FreeLibrary(hHookDll);
+        hHookDll = NULL;
+
+        if (hookRc == FALSE)
+        {
+            ServiceStatus.dwCurrentState = SERVICE_STOPPED;
+            ServiceStatus.dwWin32ExitCode = NO_ERROR;
+            ServiceStatus.dwCheckPoint = 0;
+            ServiceStatus.dwWaitHint = 0;
+            ServiceStatus.dwControlsAccepted = 0;
+            SetServiceStatus(StatusHandle, &ServiceStatus);
+                       
+            /* exit if initialization failed */
+            return;
         }
     }
 
+#ifndef AFSIFS
     WaitForSingleObject(WaitToTerminate, INFINITE);
+#else
+    WaitForMultipleObjects(WORKER_THREADS, hAFSDWorkerThread, TRUE, INFINITE);
+    for (cnt = 0; cnt < WORKER_THREADS; cnt++)
+        CloseHandle(hAFSDWorkerThread[cnt]);
+#endif
+
+    ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
+    ServiceStatus.dwWin32ExitCode = NO_ERROR;
+    ServiceStatus.dwCheckPoint = 6;
+    ServiceStatus.dwWaitHint = 120000;
+    ServiceStatus.dwControlsAccepted = 0;
+    SetServiceStatus(StatusHandle, &ServiceStatus);
 
     afsi_log("Received Termination Signal, Stopping Service");
 
-    {   
-        HANDLE h; char *ptbuf[1];
-       h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
-       ptbuf[0] = "AFS quitting";
-       ReportEvent(h, GlobalStatus ? EVENTLOG_ERROR_TYPE : EVENTLOG_INFORMATION_TYPE,
-                0, 0, NULL, 1, 0, ptbuf, NULL);
-        DeregisterEventSource(h);
+    if ( GlobalStatus )
+       LogEvent(EVENTLOG_ERROR_TYPE, MSG_SERVICE_ERROR_STOP);
+    else
+       LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_SERVICE_STOPPING);
+
+    /* allow an exit to be called prior to stopping the service */
+    hHookDll = LoadLibrary(AFSD_HOOK_DLL);
+    if (hHookDll)
+    {
+        BOOL hookRc = TRUE;
+        AfsdStoppingHook stoppingHook = ( AfsdStoppingHook ) GetProcAddress(hHookDll, AFSD_STOPPING_HOOK);
+        if (stoppingHook)
+        {
+            hookRc = stoppingHook();
+        }
+        FreeLibrary(hHookDll);
+        hHookDll = NULL;
     }
 
+
 #ifdef AFS_FREELANCE_CLIENT
     cm_FreelanceShutdown();
     afsi_log("Freelance Shutdown complete");
@@ -1227,18 +1453,27 @@ afsd_Main(DWORD argc, LPTSTR *argv)
                                          
     cm_DaemonShutdown();                 
     afsi_log("Daemon shutdown complete");
-                                         
+    
+    afsd_ShutdownCM();
+
     buf_Shutdown();                      
     afsi_log("Buffer shutdown complete");
                                          
     rx_Finalize();                       
     afsi_log("rx finalization complete");
                                          
+#ifndef AFSIFS
     smb_Shutdown();                      
     afsi_log("smb shutdown complete");   
+#endif
                                          
     RpcShutdown();                       
 
+    cm_ReleaseAllLocks();
+
+    rx_Finalize();
+    afsi_log("rx finalization complete");
+
     cm_ShutdownMappedMemory();           
 
 #ifdef REGISTER_POWER_NOTIFICATIONS
@@ -1247,12 +1482,37 @@ afsd_Main(DWORD argc, LPTSTR *argv)
         PowerNotificationThreadExit();
 #endif
 
+    cm_DirDumpStats();
+#ifdef USE_BPLUS
+    cm_BPlusDumpStats();
+#endif
+
+    /* Notify any Volume Status Handlers that we are stopped */
+    cm_VolStatus_Service_Stopped();
+
+    /* Cleanup any Volume Status Notification Handler */
+    cm_VolStatus_Finalize();
+
+    /* allow an exit to be called after stopping the service */
+    hHookDll = LoadLibrary(AFSD_HOOK_DLL);
+    if (hHookDll)
+    {
+        BOOL hookRc = TRUE;
+        AfsdStoppedHook stoppedHook = ( AfsdStoppedHook ) GetProcAddress(hHookDll, AFSD_STOPPED_HOOK);
+        if (stoppedHook)
+        {
+            hookRc = stoppedHook();
+        }
+        FreeLibrary(hHookDll);
+        hHookDll = NULL;
+    }
+
     /* Remove the ExceptionFilter */
     SetUnhandledExceptionFilter(NULL);
 
     ServiceStatus.dwCurrentState = SERVICE_STOPPED;
     ServiceStatus.dwWin32ExitCode = GlobalStatus ? ERROR_EXCEPTION_IN_SERVICE : NO_ERROR;
-    ServiceStatus.dwCheckPoint = 0;
+    ServiceStatus.dwCheckPoint = 7;
     ServiceStatus.dwWaitHint = 0;
     ServiceStatus.dwControlsAccepted = 0;
     SetServiceStatus(StatusHandle, &ServiceStatus);
@@ -1304,6 +1564,9 @@ main(int argc, char * argv[])
             printf("Hit <Enter> to terminate OpenAFS Client Service\n");
             getchar();  
             SetEvent(WaitToTerminate);
+#ifdef AFSIFS
+           dc_release_hooks();
+#endif
         }
     }