Windows: power management suspend/resume changes
[openafs.git] / src / WINNT / afsd / afsd_service.c
index 73bebb3..3ae290a 100644 (file)
@@ -1,37 +1,16 @@
 /*
  * 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
  */
-/* copyright (c) 2005
- * the regents of the university of michigan
- * all rights reserved
- * 
- * permission is granted to use, copy, create derivative works and
- * redistribute this software and such derivative works for any purpose,
- * so long as the name of the university of michigan is not used in
- * any advertising or publicity pertaining to the use or distribution
- * of this software without specific, written prior authorization.  if
- * the above copyright notice or any other identification of the
- * university of michigan is included in any copy of any portion of
- * this software, then the disclaimer below must also be included.
- * 
- * this software is provided as is, without representation from the
- * university of michigan as to its fitness for any purpose, and without
- * warranty by the university of michigan of any kind, either express 
- * or implied, including without limitation the implied warranties of
- * merchantability and fitness for a particular purpose.  the regents
- * of the university of michigan shall not be liable for any damages,   
- * including special, indirect, incidental, or consequential damages, 
- * with respect to any claim arising out or in connection with the use
- * of the software, even if it has been or is hereafter advised of the
- * possibility of such damages.
- */
 
+#include <afsconfig.h>
 #include <afs/param.h>
+#include <roken.h>
+
 #include <afs/stds.h>
 
 #include <windows.h>
 #include <setjmp.h>
 #include "afsd.h"
 #include "afsd_init.h"
+#include "lanahelper.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <winsock2.h>
 #include <WINNT\afsreg.h>
+#include "cm_btree.h"
+#include "cm_rpc.h"
+#include "smb.h"
 
 #include <osi.h>
 
@@ -55,7 +38,6 @@
 #ifdef _DEBUG
 #include <crtdbg.h>
 #endif
-#include "afsdifs.h"
 
 //#define REGISTER_POWER_NOTIFICATIONS 1
 #include "afsd_flushvol.h"
@@ -64,15 +46,13 @@ extern void afsi_log(char *pattern, ...);
 
 static SERVICE_STATUS          ServiceStatus;
 static SERVICE_STATUS_HANDLE   StatusHandle;
+static BOOL bRunningAsService = TRUE;
 
 HANDLE hAFSDMainThread = NULL;
-#ifdef AFSIFS
-HANDLE hAFSDWorkerThread[WORKER_THREADS];
-#endif
 
-HANDLE WaitToTerminate, DoTerminate;
+HANDLE WaitToTerminate;
 
-int GlobalStatus;
+static int GlobalStatus;
 
 #ifdef JUMP
 unsigned int MainThreadId;
@@ -82,28 +62,27 @@ jmp_buf notifier_jmp;
 extern int traceOnPanic;
 extern HANDLE afsi_file;
 
-int powerEventsRegistered = 0;
+static int powerEventsRegistered = 0;
+extern int powerStateSuspended = 0;
+extern int RDR_Initialized = 0;
+
+static VOID (WINAPI* pRtlCaptureContext)(PCONTEXT ContextRecord) = NULL;
 
 /*
  * 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;
-       int i;
+    CONTEXT context;
+
+    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;
 
@@ -112,40 +91,46 @@ static void afsd_notifier(char *msgp, char *filep, long line)
     afsd_ForceTrace(TRUE);
     buf_ForceTrace(TRUE);
 
+    if (pRtlCaptureContext) {
+        pRtlCaptureContext(&context);
+        afsd_printStack(GetCurrentThread(), &context);
+    }
+
+#ifdef DEBUG
+    if (IsDebuggerPresent())
+        DebugBreak();
+#endif
+
     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");
-#endif
     cm_DumpBufHashTable(afsi_file, "a", 0);
-    smb_DumpVCP(afsi_file, "a", 0);                    
+    cm_DumpServers(afsi_file, "a", 0);
+    smb_DumpVCP(afsi_file, "a", 0);
+    rx_DumpPackets(afsi_file, "a");
+    rx_DumpCalls(afsi_file, "a");
     afsi_log("--- end   dump ---");
-    
-#ifdef DEBUG
-    DebugBreak();      
-#endif
 
-#ifndef AFSIFS
-       SetEvent(WaitToTerminate);
-#else
-       SetEvent(DoTerminate);
-       WaitForMultipleObjects(WORKER_THREADS, hAFSDWorkerThread, TRUE, INFINITE);
-       for (i = 0; i < WORKER_THREADS; i++)
-               CloseHandle(hAFSDWorkerThread[i]);
-#endif
+    GenerateMiniDump(NULL);
+
+    SetEvent(WaitToTerminate);
 
 #ifdef JUMP
     if (GetCurrentThreadId() == MainThreadId)
         longjmp(notifier_jmp, 1);
 #endif /* JUMP */
 
-    ServiceStatus.dwCurrentState = SERVICE_STOPPED;
-    ServiceStatus.dwWin32ExitCode = NO_ERROR;
-    ServiceStatus.dwCheckPoint = 0;
-    ServiceStatus.dwWaitHint = 0;
-    ServiceStatus.dwControlsAccepted = 0;
-    SetServiceStatus(StatusHandle, &ServiceStatus);
-
+    if (bRunningAsService) {
+        ServiceStatus.dwCurrentState = SERVICE_STOPPED;
+        ServiceStatus.dwWin32ExitCode = ERROR_EXCEPTION_IN_SERVICE;
+        ServiceStatus.dwCheckPoint = 0;
+        ServiceStatus.dwWaitHint = 0;
+        ServiceStatus.dwControlsAccepted = 0;
+        SetServiceStatus(StatusHandle, &ServiceStatus);
+    }
     exit(1);
 }
 
@@ -192,7 +177,7 @@ afsd_ServiceFlushVolume(DWORD dwlpEventData)
 
 
 /* service control handler used in nt4 only for backward compat. */
-VOID WINAPI 
+VOID WINAPI
 afsd_ServiceControlHandler(DWORD ctrlCode)
 {
     HKEY parmKey;
@@ -215,7 +200,8 @@ afsd_ServiceControlHandler(DWORD ctrlCode)
             afsi_log("SERVICE_CONTROL_SHUTDOWN");
 
         /* Write all dirty buffers back to server */
-        buf_CleanAndReset();
+       if (cm_noIPAddr > 0)
+           buf_CleanAndReset();
 
         /* Force trace if requested */
         code = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
@@ -237,11 +223,7 @@ afsd_ServiceControlHandler(DWORD ctrlCode)
         }
 
       doneTrace:
-#ifndef AFSIFS
         SetEvent(WaitToTerminate);
-#else
-               SetEvent(DoTerminate);
-#endif
         break;
 
     case SERVICE_CONTROL_INTERROGATE:
@@ -255,7 +237,7 @@ afsd_ServiceControlHandler(DWORD ctrlCode)
         /* XXX handle system shutdown */
         /* XXX handle pause & continue */
     }
-}       
+}
 
 
 /*
@@ -274,11 +256,23 @@ afsd_ServiceControlHandlerEx(
     DWORD dummyLen, doTrace;
     long code;
     DWORD dwRet = ERROR_CALL_NOT_IMPLEMENTED;
+    OSVERSIONINFO osVersion;
+    DWORD dwCurrentState = ServiceStatus.dwCurrentState;
 
-    switch (ctrlCode) 
+    /* 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;
@@ -287,7 +281,9 @@ afsd_ServiceControlHandlerEx(
         SetServiceStatus(StatusHandle, &ServiceStatus);
 
         /* Write all dirty buffers back to server */
-        buf_CleanAndReset();
+       if (dwCurrentState == SERVICE_RUNNING &&
+            cm_noIPAddr > 0)
+           buf_CleanAndReset();
 
         /* Force trace if requested */
         code = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
@@ -309,11 +305,7 @@ afsd_ServiceControlHandlerEx(
         }
 
       doneTrace:
-#ifndef AFSIFS
         SetEvent(WaitToTerminate);
-#else
-               SetEvent(DoTerminate);
-#endif
         dwRet = NO_ERROR;
         break;
 
@@ -325,98 +317,204 @@ afsd_ServiceControlHandlerEx(
         ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT;
         SetServiceStatus(StatusHandle, &ServiceStatus);
         afsi_log("SERVICE_CONTROL_INTERROGATE");
+        osi_Log0(afsd_logp, "SERVICE_CONTROL_INTERROGATE");
         dwRet = NO_ERROR;
         break;
 
         /* XXX handle system shutdown */
         /* XXX handle pause & continue */
-    case SERVICE_CONTROL_POWEREVENT:                                              
-        { 
-            afsi_log("SERVICE_CONTROL_POWEREVENT");
-            /*                                                                                
-            ** dwEventType of this notification == WPARAM of WM_POWERBROADCAST               
-            ** Return NO_ERROR == return TRUE for that message, i.e. accept request          
-            ** Return any error code to deny request,                                        
-            ** i.e. as if returning BROADCAST_QUERY_DENY                                     
-            */                                                                                
+    case SERVICE_CONTROL_POWEREVENT:
+        {
+#ifdef DEBUG
+           afsi_log("SERVICE_CONTROL_POWEREVENT");
+#endif
+           osi_Log0(afsd_logp, "SERVICE_CONTROL_POWEREVENT");
+            /*
+            ** dwEventType of this notification == WPARAM of WM_POWERBROADCAST
+            ** Return NO_ERROR == return TRUE for that message, i.e. accept request
+            ** Return any error code to deny request,
+            ** i.e. as if returning BROADCAST_QUERY_DENY
+            */
             if (powerEventsRegistered) {
-                switch((int) dwEventType)                                                         
-                {                                                                               
-                case PBT_APMQUERYSUSPEND:       
-                    afsi_log("SERVICE_CONTROL_APMQUERYSUSPEND"); 
+               cm_UpdateIFInfo();
+
+               switch((int) dwEventType)
+                {
+                case PBT_APMQUERYSUSPEND:
+                    afsi_log("SERVICE_CONTROL_APMQUERYSUSPEND");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMQUERYSUSPEND");
                     /* Write all dirty buffers back to server */
-                    buf_CleanAndReset();
-                    dwRet = NO_ERROR;                       
-                    break;                                  
-                case PBT_APMQUERYSTANDBY:                                                         
-                    afsi_log("SERVICE_CONTROL_APMQUERYSTANDBY"); 
+                   if (cm_noIPAddr > 0)
+                       buf_CleanAndReset();
+                    afsi_log("SERVICE_CONTROL_APMQUERYSUSPEND buf_CleanAndReset complete");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMQUERYSUSPEND buf_CleanAndReset complete");
+                    dwRet = NO_ERROR;
+                    break;
+                case PBT_APMQUERYSTANDBY:
+                    afsi_log("SERVICE_CONTROL_APMQUERYSTANDBY");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMQUERYSTANDBY");
                     /* Write all dirty buffers back to server */
-                    buf_CleanAndReset();
-                    dwRet = NO_ERROR;                                                             
-                    break;                                                                        
-                                                                                                                         
-                    /* allow remaining case PBT_WhatEver */                                           
-                case PBT_APMSUSPEND:                         
-                    afsi_log("SERVICE_CONTROL_APMSUSPEND"); 
-                    dwRet = NO_ERROR;                       
-                    break;                                  
-                case PBT_APMSTANDBY:                  
-                    afsi_log("SERVICE_CONTROL_APMSTANDBY"); 
-                    dwRet = NO_ERROR;                       
-                    break;                                  
-                case PBT_APMRESUMECRITICAL:             
-                    afsi_log("SERVICE_CONTROL_APMRESUMECRITICAL"); 
-                    dwRet = NO_ERROR;                       
-                    break;                                  
-                case PBT_APMRESUMESUSPEND:                                                        
-                    afsi_log("SERVICE_CONTROL_APMRESUMESUSPEND"); 
-                    dwRet = NO_ERROR;                       
-                    break;                                  
-                case PBT_APMRESUMESTANDBY:                                                        
-                    afsi_log("SERVICE_CONTROL_APMRESUMESTANDBY"); 
-                    dwRet = NO_ERROR;                       
-                    break;                                  
-                case PBT_APMBATTERYLOW:                                                           
-                    afsi_log("SERVICE_CONTROL_APMBATTERYLOW"); 
-                    dwRet = NO_ERROR;                       
-                    break;                                  
-                case PBT_APMPOWERSTATUSCHANGE:                                                    
-                    afsi_log("SERVICE_CONTROL_APMPOWERSTATUSCHANGE"); 
-                    dwRet = NO_ERROR;                       
-                    break;                                  
-                case PBT_APMOEMEVENT:                                                             
-                    afsi_log("SERVICE_CONTROL_APMOEMEVENT"); 
-                    dwRet = NO_ERROR;                       
-                    break;                                  
-                case PBT_APMRESUMEAUTOMATIC:                                                      
-                    afsi_log("SERVICE_CONTROL_APMRESUMEAUTOMATIC"); 
-                    dwRet = NO_ERROR;                       
-                    break;                                  
-                default:                                                                          
-                    afsi_log("SERVICE_CONTROL_unknown"); 
-                    dwRet = NO_ERROR;                       
-                }   
+                   if (cm_noIPAddr > 0)
+                       buf_CleanAndReset();
+                    afsi_log("SERVICE_CONTROL_APMQUERYSTANDBY buf_CleanAndReset complete");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMQUERYSTANDBY buf_CleanAndReset complete");
+                    dwRet = NO_ERROR;
+                    break;
+
+                    /* allow remaining case PBT_WhatEver */
+                case PBT_APMSUSPEND:
+                    afsi_log("SERVICE_CONTROL_APMSUSPEND");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMSUSPEND");
+                   if (!powerStateSuspended) {
+                       powerStateSuspended = 1;
+                       if (osVersion.dwMajorVersion >= 6)
+                           smb_StopListeners(0);
+
+                       if (RDR_Initialized)
+                           RDR_Suspend();
+                       cm_SuspendSCache();
+                   }
+                    dwRet = NO_ERROR;
+                    afsi_log("SERVICE_CONTROL_APMSUSPEND complete");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMSUSPEND complete");
+                    break;
+                case PBT_APMSTANDBY:
+                    afsi_log("SERVICE_CONTROL_APMSTANDBY");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMSTANDBY");
+                   if (!powerStateSuspended) {
+                       powerStateSuspended = 1;
+                       if (osVersion.dwMajorVersion >= 6)
+                           smb_StopListeners(0);
+                       if (RDR_Initialized)
+                           RDR_Suspend();
+                       cm_SuspendSCache();
+                   }
+                    dwRet = NO_ERROR;
+                    afsi_log("SERVICE_CONTROL_APMSTANDBY complete");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMSTANDBY complete");
+                    break;
+                case PBT_APMRESUMECRITICAL:
+                    afsi_log("SERVICE_CONTROL_APMRESUMECRITICAL");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMRESUMECRITICAL");
+                   if (powerStateSuspended) {
+                       powerStateSuspended = 0;
+                       if (osVersion.dwMajorVersion >= 6)
+                           smb_RestartListeners(0);
+                       cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS
+                                        | CM_FLAG_CHECKUPSERVERS, NULL);
+                       if (RDR_Initialized)
+                           RDR_Resume();
+                   }
+                    dwRet = NO_ERROR;
+                    afsi_log("SERVICE_CONTROL_APMRESUMECRITICAL complete");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMRESUMECRITICAL complete");
+                    break;
+                case PBT_APMRESUMESUSPEND:
+                   /* User logged in after suspend */
+                    afsi_log("SERVICE_CONTROL_APMRESUMESUSPEND");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMRESUMESUSPEND");
+                   if (powerStateSuspended) {
+                       powerStateSuspended = 0;
+                       cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS
+                                        | CM_FLAG_CHECKUPSERVERS, NULL);
+                       if (osVersion.dwMajorVersion >= 6)
+                           smb_RestartListeners(0);
+                       if (smb_Enabled && osVersion.dwMajorVersion >= 6) {
+                           smb_SetLanAdapterChangeDetected();
+                       }
+                       if (RDR_Initialized)
+                           RDR_Resume();
+                   }
+                    dwRet = NO_ERROR;
+                    afsi_log("SERVICE_CONTROL_APMRESUMESUSPEND complete");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMRESUMESUSPEND complete");
+                    break;
+                case PBT_APMRESUMESTANDBY:
+                   /* User logged in after standby */
+                    afsi_log("SERVICE_CONTROL_APMRESUMESTANDBY");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMRESUMESTANDBY");
+                   if (powerStateSuspended) {
+                       powerStateSuspended = 0;
+                       cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS
+                                        | CM_FLAG_CHECKUPSERVERS, NULL);
+                       if (osVersion.dwMajorVersion >= 6)
+                           smb_RestartListeners(0);
+                       if (smb_Enabled && osVersion.dwMajorVersion >= 6) {
+                           smb_SetLanAdapterChangeDetected();
+                       }
+                       if (RDR_Initialized)
+                           RDR_Resume();
+                   }
+                    dwRet = NO_ERROR;
+                    afsi_log("SERVICE_CONTROL_APMRESUMESTANDBY complete");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMRESUMESTANDBY complete");
+                    break;
+                case PBT_APMBATTERYLOW:
+                    afsi_log("SERVICE_CONTROL_APMBATTERYLOW");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMBATTERYLOW");
+                    dwRet = NO_ERROR;
+                    break;
+                case PBT_APMPOWERSTATUSCHANGE:
+#ifdef DEBUG
+                   afsi_log("SERVICE_CONTROL_APMPOWERSTATUSCHANGE");
+#endif
+                   osi_Log0(afsd_logp,"SERVICE_CONTROL_APMPOWERSTATUSCHANGE");
+                    dwRet = NO_ERROR;
+                    break;
+                case PBT_APMOEMEVENT:
+#ifdef DEBUG
+                    afsi_log("SERVICE_CONTROL_APMOEMEVENT");
+#endif
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMOEMEVENT");
+                    dwRet = NO_ERROR;
+                    break;
+                case PBT_APMRESUMEAUTOMATIC:
+                   /* This is the message delivered once all devices are up */
+                    afsi_log("SERVICE_CONTROL_APMRESUMEAUTOMATIC");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMRESUMEAUTOMATIC");
+                   if (powerStateSuspended) {
+                       powerStateSuspended = 0;
+                       cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS
+                                        | CM_FLAG_CHECKUPSERVERS, NULL);
+                       if (osVersion.dwMajorVersion >= 6)
+                           smb_RestartListeners(0);
+                       if (smb_Enabled && osVersion.dwMajorVersion >= 6) {
+                           smb_SetLanAdapterChangeDetected();
+                       }
+                       if (RDR_Initialized)
+                           RDR_Resume();
+                   }
+                    dwRet = NO_ERROR;
+                    afsi_log("SERVICE_CONTROL_APMRESUMEAUTOMATIC complete");
+                    osi_Log0(afsd_logp,"SERVICE_CONTROL_APMRESUMEAUTOMATIC complete");
+                    break;
+                default:
+                    afsi_log("SERVICE_CONTROL_unknown");
+                    osi_Log1(afsd_logp, "SERVICE_CONTROL_unknown: 0x%x", dwEventType);
+                    dwRet = NO_ERROR;
+                }
             }
         }
         break;
-    case SERVICE_CONTROL_CUSTOM_DUMP: 
+    case SERVICE_CONTROL_CUSTOM_DUMP:
         {
-            afsi_log("SERVICE_CONTROL_CUSTOM_DUMP"); 
+            afsi_log("SERVICE_CONTROL_CUSTOM_DUMP");
             GenerateMiniDump(NULL);
-                       dwRet = NO_ERROR;
+           dwRet = NO_ERROR;
         }
         break;
-    }          /* end switch(ctrlCode) */                                                        
-    return dwRet;   
+    }          /* end switch(ctrlCode) */
+    return dwRet;
 }
 
 /* There is similar code in client_config\drivemap.cpp GlobalMountDrive()
- * 
+ *
  * 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)
 {
     char szAfsPath[_MAX_PATH];
     char szDriveToMapTo[5];
@@ -433,9 +531,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);
@@ -447,8 +545,7 @@ static void MountGlobalDrives(void)
             }
         }
 
-#ifndef AFSIFS
-        for ( ; dwRetry < MAX_RETRIES; dwRetry++)
+        for (dwRetry = 0 ; dwRetry < MAX_RETRIES; dwRetry++)
         {
             NETRESOURCE nr;
             memset (&nr, 0x00, sizeof(NETRESOURCE));
@@ -457,43 +554,74 @@ static void MountGlobalDrives(void)
 
             nr.dwScope = RESOURCE_GLOBALNET;              /* ignored parameter */
             nr.dwType=RESOURCETYPE_DISK;
-            nr.lpLocalName=szDriveToMapTo;
+            nr.lpLocalName=strlen(szDriveToMapTo) > 0 ? szDriveToMapTo : NULL;
             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, 
+            afsi_log("GlobalAutoMap of %s to %s %s (%d)", szDriveToMapTo, szSubMount,
                      (dwResult == NO_ERROR) ? "succeeded" : "failed", dwResult);
             if (dwResult == NO_ERROR) {
                 break;
             }
             /* wait for smb server to come up */
-            Sleep((DWORD)1000 /* miliseconds */);              
+            Sleep((DWORD)1000 /* miliseconds */);
 
             /* Disconnect any previous mappings */
             dwResult = WNetCancelConnection2(szDriveToMapTo, 0, TRUE);
         }
-#else
-       /* FIXFIX */
-#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()
 {
     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;
+    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);
 
@@ -501,8 +629,7 @@ static void DismountGlobalDrives()
     if (dwResult != ERROR_SUCCESS)
         return;
 
-#ifndef AFSIFS    
-       while (1) {
+    while (dwIndex < MAX_DRIVES) {
         dwDriveSize = sizeof(szDriveToMapTo);
         dwSubMountSize = sizeof(szSubMount);
         dwResult = RegEnumValue(hKey, dwIndex++, szDriveToMapTo, &dwDriveSize, 0, &dwType, szSubMount, &dwSubMountSize);
@@ -515,15 +642,12 @@ static void DismountGlobalDrives()
         }
 
         sprintf(szAfsPath,"\\\\%s\\%s",cm_NetbiosName,szSubMount);
-                   
+
         dwResult = WNetCancelConnection2(szDriveToMapTo, 0, TRUE);
         dwResult = WNetCancelConnection(szAfsPath, TRUE);
-        
+
         afsi_log("Disconnect from GlobalAutoMap of %s to %s %s", szDriveToMapTo, szSubMount, dwResult ? "succeeded" : "failed");
-    }        
-#else
-       /* FIXFIX */
-#endif
+    }
 
     RegCloseKey(hKey);
 }
@@ -541,7 +665,7 @@ GetVersionInfo( CHAR * filename, CHAR * szOutput, DWORD dwOutput )
     DWORD size = GetFileVersionInfoSize(filename, &dwVersionHandle);
 
     if (!size) {
-        afsi_log("GetFileVersionInfoSize failed");
+       afsi_log("GetFileVersionInfoSize(%s) failed", filename);
         return GetLastError();
     }
 
@@ -628,7 +752,7 @@ void LoadCrypt32(void)
     (FARPROC) pCryptMsgClose = GetProcAddress( hCrypt32, "CryptMsgClose" );
     (FARPROC) pCertCompareCertificate = GetProcAddress( hCrypt32, "CertCompareCertificate" );
     (FARPROC) pCertFreeCertificateContext = GetProcAddress( hCrypt32, "CertFreeCertificateContext" );
-    
+
     if ( !pCertGetNameString ||
          !pCryptQueryObject ||
          !pCryptMsgGetParam ||
@@ -709,7 +833,7 @@ PCCERT_CONTEXT GetCertCtx(CHAR * filename)
                                0,
                                (PVOID)pSignerInfo,
                                &dwSignerInfo);
-    
+
     if (!fResult) {
         afsi_log("CryptMsgGetParam failed for [%s] with error 0x%x",
                 filename,
@@ -763,7 +887,7 @@ BOOL VerifyTrust(CHAR * filename)
     LONG (WINAPI *pWinVerifyTrust)(HWND hWnd, GUID* pgActionID, WINTRUST_DATA* pWinTrustData) = NULL;
     HINSTANCE hWinTrust;
 
-    if (filename == NULL ) 
+    if (filename == NULL )
         return FALSE;
 
     hWinTrust = LoadLibrary("wintrust");
@@ -801,7 +925,7 @@ BOOL VerifyTrust(CHAR * filename)
         switch (gle) {
         case TRUST_E_PROVIDER_UNKNOWN:
             afsi_log("VerifyTrust failed: \"Generic Verify V2\" Provider Unknown");
-            break;  
+            break;
         case TRUST_E_NOSIGNATURE:
             afsi_log("VerifyTrust failed: Unsigned executable");
             break;
@@ -941,7 +1065,7 @@ BOOL AFSModulesVerify(void)
     }
 
 
-    code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, 
+    code = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                         AFSREG_CLT_SVC_PARAM_SUBKEY,
                         0, KEY_QUERY_VALUE, &parmKey);
     if (code == ERROR_SUCCESS) {
@@ -960,7 +1084,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");
@@ -1013,7 +1141,7 @@ BOOL AFSModulesVerify(void)
                         if ( !VerifyTrust(szModName) ) {
                             afsi_log("Signature Verification failed: %s", szModName);
                             success = FALSE;
-                        } 
+                        }
                         else if (pCtxService) {
                             PCCERT_CONTEXT pCtx = GetCertCtx(szModName);
 
@@ -1023,10 +1151,10 @@ BOOL AFSModulesVerify(void)
                                 afsi_log("Certificate mismatch: %s", szModName);
                                 if (pCtx)
                                     LogCertCtx(pCtx);
-                                
+
                                 success = FALSE;
                             }
-                            
+
                             if (pCtx)
                                 pCertFreeCertificateContext(pCtx);
                         }
@@ -1055,7 +1183,7 @@ typedef SERVICE_STATUS_HANDLE ( * RegisterServiceCtrlHandlerExFunc )(  LPCTSTR ,
 typedef SERVICE_STATUS_HANDLE ( * RegisterServiceCtrlHandlerFunc   )(  LPCTSTR ,  LPHANDLER_FUNCTION );
 
 RegisterServiceCtrlHandlerExFunc pRegisterServiceCtrlHandlerEx = NULL;
-RegisterServiceCtrlHandlerFunc   pRegisterServiceCtrlHandler   = NULL; 
+RegisterServiceCtrlHandlerFunc   pRegisterServiceCtrlHandler   = NULL;
 
 VOID WINAPI
 afsd_Main(DWORD argc, LPTSTR *argv)
@@ -1067,16 +1195,29 @@ afsd_Main(DWORD argc, LPTSTR *argv)
 #endif /* JUMP */
     HMODULE hHookDll;
     HMODULE hAdvApi32;
-       int cnt;
+    HMODULE hKernel32;
 
 #ifdef _DEBUG
-    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF /*| _CRTDBG_CHECK_ALWAYS_DF*/ | 
+    void afsd_DbgBreakAllocInit();
+
+    afsd_DbgBreakAllocInit();
+    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF /*| _CRTDBG_CHECK_ALWAYS_DF*/ |
                    _CRTDBG_CHECK_CRT_DF /* | _CRTDBG_DELAY_FREE_MEM_DF */ );
-#endif 
+#endif
+
+    afsd_SetUnhandledExceptionFilter();
 
     osi_InitPanic(afsd_notifier);
     osi_InitTraceOption();
 
+    hKernel32 = LoadLibrary("kernel32.dll");
+    if (hKernel32 == NULL)
+    {
+        afsi_log("Fatal: cannot load kernel32.dll");
+        return;
+    }
+    pRtlCaptureContext = GetProcAddress(hKernel32, "RtlCaptureContext");
+
     GlobalStatus = 0;
 
     afsi_start();
@@ -1085,12 +1226,6 @@ afsd_Main(DWORD argc, LPTSTR *argv)
     if ( GetLastError() == ERROR_ALREADY_EXISTS )
         afsi_log("Event Object Already Exists: %s", TEXT("afsd_service_WaitToTerminate"));
 
-#ifdef AFSIFS
-       DoTerminate = CreateEvent(NULL, TRUE, FALSE, TEXT("afsd_service_DoTerminate"));
-    if ( GetLastError() == ERROR_ALREADY_EXISTS )
-        afsi_log("Event Object Already Exists: %s", TEXT("afsd_service_DoTerminate"));
-#endif
-
 #ifndef NOTSERVICE
     hAdvApi32 = LoadLibrary("advapi32.dll");
     if (hAdvApi32 == NULL)
@@ -1099,35 +1234,31 @@ afsd_Main(DWORD argc, LPTSTR *argv)
         return;
     }
 
-    pRegisterServiceCtrlHandlerEx = (RegisterServiceCtrlHandlerExFunc)GetProcAddress(hAdvApi32, "RegisterServiceCtrlHandlerExA");
-    if (pRegisterServiceCtrlHandlerEx)
-    {
-        afsi_log("running on 2000+ - using RegisterServiceCtrlHandlerEx");
-        StatusHandle = RegisterServiceCtrlHandlerEx(AFS_DAEMON_SERVICE_NAME, afsd_ServiceControlHandlerEx, NULL );
-    }
-    else
-    {
-        StatusHandle = RegisterServiceCtrlHandler(AFS_DAEMON_SERVICE_NAME, afsd_ServiceControlHandler);
-    }
+    if (bRunningAsService) {
+        pRegisterServiceCtrlHandlerEx = (RegisterServiceCtrlHandlerExFunc)GetProcAddress(hAdvApi32, "RegisterServiceCtrlHandlerExA");
+        if (pRegisterServiceCtrlHandlerEx)
+        {
+            afsi_log("running on 2000+ - using RegisterServiceCtrlHandlerEx");
+            StatusHandle = RegisterServiceCtrlHandlerEx(AFS_DAEMON_SERVICE_NAME, afsd_ServiceControlHandlerEx, NULL );
+        }
+        else
+        {
+            StatusHandle = RegisterServiceCtrlHandler(AFS_DAEMON_SERVICE_NAME, afsd_ServiceControlHandler);
+        }
 
-    ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
-    ServiceStatus.dwServiceSpecificExitCode = 0;
-    ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
-    ServiceStatus.dwWin32ExitCode = NO_ERROR;
-    ServiceStatus.dwCheckPoint = 1;
-    ServiceStatus.dwWaitHint = 30000;
-    /* accept Power Events */
-    ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_PARAMCHANGE;
-    SetServiceStatus(StatusHandle, &ServiceStatus);
+        ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
+        ServiceStatus.dwServiceSpecificExitCode = 0;
+        ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
+        ServiceStatus.dwWin32ExitCode = NO_ERROR;
+        ServiceStatus.dwCheckPoint = 1;
+        ServiceStatus.dwWaitHint = 120000;
+        /* accept Power Events */
+        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
     {
@@ -1137,12 +1268,12 @@ afsd_Main(DWORD argc, LPTSTR *argv)
         int bpower = TRUE;
 
         /* see if we should handle power notifications */
-        code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY, 
+        code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
                             0, KEY_QUERY_VALUE, &hkParm);
         if (code == ERROR_SUCCESS) {
             dummyLen = sizeof(bpower);
             code = RegQueryValueEx(hkParm, "FlushOnHibernate", NULL, NULL,
-                (BYTE *) &bpower, &dummyLen);      
+                (BYTE *) &bpower, &dummyLen);
 
             if(code != ERROR_SUCCESS)
                 bpower = TRUE;
@@ -1159,19 +1290,22 @@ afsd_Main(DWORD argc, LPTSTR *argv)
 
     /* Verify the versions of the DLLs which were loaded */
     if (!AFSModulesVerify()) {
-        ServiceStatus.dwCurrentState = SERVICE_STOPPED;
-        ServiceStatus.dwWin32ExitCode = NO_ERROR;
-        ServiceStatus.dwCheckPoint = 0;
-        ServiceStatus.dwWaitHint = 0;
-        ServiceStatus.dwControlsAccepted = 0;
-        SetServiceStatus(StatusHandle, &ServiceStatus);
-                       
+        if (bRunningAsService) {
+            ServiceStatus.dwCurrentState = SERVICE_STOPPED;
+            ServiceStatus.dwWin32ExitCode = NO_ERROR;
+            ServiceStatus.dwCheckPoint = 0;
+            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 */
-    hHookDll = LoadLibrary(AFSD_HOOK_DLL);
+    hHookDll = cm_LoadAfsdHookLib();
     if (hHookDll)
     {
         BOOL hookRc = TRUE;
@@ -1185,36 +1319,42 @@ afsd_Main(DWORD argc, LPTSTR *argv)
 
         if (hookRc == FALSE)
         {
-            ServiceStatus.dwCurrentState = SERVICE_STOPPED;
-            ServiceStatus.dwWin32ExitCode = NO_ERROR;
-            ServiceStatus.dwCheckPoint = 0;
-            ServiceStatus.dwWaitHint = 0;
-            ServiceStatus.dwControlsAccepted = 0;
-            SetServiceStatus(StatusHandle, &ServiceStatus);
-                       
+            if (bRunningAsService) {
+                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;
         }
         else
         {
-            /* allow another 15 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;
-            /* accept Power Events */
-            ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_POWEREVENT | SERVICE_ACCEPT_PARAMCHANGE;
-            SetServiceStatus(StatusHandle, &ServiceStatus);
+            /* allow another 120 seconds to start */
+            if (bRunningAsService) {
+                ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
+                ServiceStatus.dwServiceSpecificExitCode = 0;
+                ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
+                ServiceStatus.dwWin32ExitCode = NO_ERROR;
+                ServiceStatus.dwCheckPoint = 2;
+                ServiceStatus.dwWaitHint = 120000;
+                /* accept Power Events */
+                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);
 
-    if (jmpret == 0) 
+    if (jmpret == 0)
 #endif /* JUMP */
     {
         code = afsd_InitCM(&reason);
@@ -1224,18 +1364,14 @@ afsd_Main(DWORD argc, LPTSTR *argv)
         }
 
 #ifndef NOTSERVICE
-        ServiceStatus.dwCheckPoint++;
-        ServiceStatus.dwWaitHint -= 5000;
-        SetServiceStatus(StatusHandle, &ServiceStatus);
-#endif
-        code = afsd_InitDaemons(&reason);
-        if (code != 0) {
-            afsi_log("afsd_InitDaemons failed: %s (code = %d)", reason, code);
-                       osi_panic(reason, __FILE__, __LINE__);
+        if (bRunningAsService) {
+            ServiceStatus.dwCheckPoint = 3;
+            ServiceStatus.dwWaitHint = 30000;
+            SetServiceStatus(StatusHandle, &ServiceStatus);
         }
-
+#endif
         /* allow an exit to be called post rx initialization */
-        hHookDll = LoadLibrary(AFSD_HOOK_DLL);
+        hHookDll = cm_LoadAfsdHookLib();
         if (hHookDll)
         {
             BOOL hookRc = TRUE;
@@ -1249,42 +1385,69 @@ afsd_Main(DWORD argc, LPTSTR *argv)
 
             if (hookRc == FALSE)
             {
-                ServiceStatus.dwCurrentState = SERVICE_STOPPED;
-                ServiceStatus.dwWin32ExitCode = NO_ERROR;
-                ServiceStatus.dwCheckPoint = 0;
-                ServiceStatus.dwWaitHint = 0;
-                ServiceStatus.dwControlsAccepted = 0;
-                SetServiceStatus(StatusHandle, &ServiceStatus);
-                       
+                if (bRunningAsService) {
+                    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;
-        SetServiceStatus(StatusHandle, &ServiceStatus);
+        if (bRunningAsService) {
+            ServiceStatus.dwCheckPoint = 4;
+            ServiceStatus.dwWaitHint = 15000;
+            SetServiceStatus(StatusHandle, &ServiceStatus);
+        }
 #endif
 
-#ifndef AFSIFS
+        /* Notify any volume status handlers that the cache manager has started */
+        cm_VolStatus_Service_Started();
+
+        code = RDR_Initialize();
+        if ( code == ERROR_SERVICE_DISABLED) {
+            afsi_log("RDR_Initialize failed: 1058 (Unable to load AFSRedirLib.sys)");
+            osi_panic(reason, __FILE__, __LINE__);
+        } else {
+            RDR_Initialized = !code;
+            afsi_log("RDR_Initialize returned: (code = %d)", code);
+        }
+
+        if (RDR_Initialized) {
+            if (cm_sysNameCount)
+                RDR_SysName( AFS_SYSNAME_ARCH_32BIT, cm_sysNameCount, cm_sysNameList );
+#ifdef _WIN64
+            if (cm_sysName64Count)
+                RDR_SysName( AFS_SYSNAME_ARCH_64BIT, cm_sysName64Count, cm_sysName64List );
+            else if (cm_sysNameCount)
+                RDR_SysName( AFS_SYSNAME_ARCH_64BIT, cm_sysNameCount, cm_sysNameList );
+#endif
+        }
+
+        /*
+         * Set the default for the SMB interface based upon the state of the
+         * Redirector interface.
+         */
+        smb_Enabled = !RDR_Initialized;
+
         code = afsd_InitSMB(&reason, MessageBox);
-        if (code != 0) {
+        if (smb_Enabled && 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
+
+        if (!smb_Enabled && !RDR_Initialized) {
+            afsi_log("Neither RDR nor SMB interfaces available");
+            osi_panic(reason, __FILE__, __LINE__);
+        }
 
         /* allow an exit to be called post smb initialization */
-        hHookDll = LoadLibrary(AFSD_HOOK_DLL);
+        hHookDll = cm_LoadAfsdHookLib();
         if (hHookDll)
         {
             BOOL hookRc = TRUE;
@@ -1298,13 +1461,14 @@ afsd_Main(DWORD argc, LPTSTR *argv)
 
             if (hookRc == FALSE)
             {
-                ServiceStatus.dwCurrentState = SERVICE_STOPPED;
-                ServiceStatus.dwWin32ExitCode = NO_ERROR;
-                ServiceStatus.dwCheckPoint = 0;
-                ServiceStatus.dwWaitHint = 0;
-                ServiceStatus.dwControlsAccepted = 0;
-                SetServiceStatus(StatusHandle, &ServiceStatus);
-                       
+                if (bRunningAsService) {
+                    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;
             }
@@ -1312,27 +1476,30 @@ afsd_Main(DWORD argc, LPTSTR *argv)
 
         MountGlobalDrives();
 
+        code = afsd_InitDaemons(&reason);
+        if (code != 0) {
+            afsi_log("afsd_InitDaemons failed: %s (code = %d)", reason, code);
+                       osi_panic(reason, __FILE__, __LINE__);
+        }
+
 #ifndef NOTSERVICE
-        ServiceStatus.dwCurrentState = SERVICE_RUNNING;
-        ServiceStatus.dwWin32ExitCode = NO_ERROR;
-        ServiceStatus.dwCheckPoint = 0;
-        ServiceStatus.dwWaitHint = 0;
+        if (bRunningAsService) {
+            ServiceStatus.dwCurrentState = SERVICE_RUNNING;
+            ServiceStatus.dwWin32ExitCode = NO_ERROR;
+            ServiceStatus.dwCheckPoint = 5;
+            ServiceStatus.dwWaitHint = 0;
 
-        /* accept Power events */
-        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 running";
-            ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, ptbuf, NULL);
-            DeregisterEventSource(h);
+            /* accept Power events */
+            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);
+    hHookDll = cm_LoadAfsdHookLib();
     if (hHookDll)
     {
         BOOL hookRc = TRUE;
@@ -1346,39 +1513,38 @@ afsd_Main(DWORD argc, LPTSTR *argv)
 
         if (hookRc == FALSE)
         {
-            ServiceStatus.dwCurrentState = SERVICE_STOPPED;
-            ServiceStatus.dwWin32ExitCode = NO_ERROR;
-            ServiceStatus.dwCheckPoint = 0;
-            ServiceStatus.dwWaitHint = 0;
-            ServiceStatus.dwControlsAccepted = 0;
-            SetServiceStatus(StatusHandle, &ServiceStatus);
-                       
+            if (bRunningAsService) {
+                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
+    WaitForSingleObject(WaitToTerminate, INFINITE);
 
+    if (bRunningAsService) {
+        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);
+    hHookDll = cm_LoadAfsdHookLib();
     if (hHookDll)
     {
         BOOL hookRc = TRUE;
@@ -1389,19 +1555,6 @@ afsd_Main(DWORD argc, LPTSTR *argv)
         }
         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;
-        }
     }
 
 
@@ -1412,22 +1565,37 @@ afsd_Main(DWORD argc, LPTSTR *argv)
 
     DismountGlobalDrives();
     afsi_log("Global Drives dismounted");
-                                         
-    cm_DaemonShutdown();                 
+
+    if (RDR_Initialized) {
+        RDR_ShutdownNotify();
+        cm_VolStatus_SetRDRNotifications(FALSE);
+        afsi_log("RDR notified of shutdown");
+    }
+
+    smb_Shutdown();
+    afsi_log("smb shutdown complete");
+
+    cm_ReleaseAllLocks();
+
+    cm_DaemonShutdown();
     afsi_log("Daemon shutdown complete");
-                                         
-    buf_Shutdown();                      
+
+    buf_Shutdown();
     afsi_log("Buffer shutdown complete");
-                                         
-    rx_Finalize();                       
-    afsi_log("rx finalization complete");
-                                         
-    smb_Shutdown();                      
-    afsi_log("smb shutdown complete");   
-                                         
-    RpcShutdown();                       
 
-    cm_ShutdownMappedMemory();           
+    afsd_ShutdownCM();
+
+    RpcShutdown();
+
+    cm_ShutdownMappedMemory();
+
+    if (RDR_Initialized) {
+        RDR_ShutdownFinal();
+        afsi_log("RDR shutdown complete");
+    }
+
+    rx_Finalize();
+    afsi_log("rx finalization complete");
 
 #ifdef REGISTER_POWER_NOTIFICATIONS
     /* terminate thread used to flush cache */
@@ -1435,8 +1603,19 @@ 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);
+    hHookDll = cm_LoadAfsdHookLib();
     if (hHookDll)
     {
         BOOL hookRc = TRUE;
@@ -1452,13 +1631,17 @@ afsd_Main(DWORD argc, LPTSTR *argv)
     /* Remove the ExceptionFilter */
     SetUnhandledExceptionFilter(NULL);
 
-    ServiceStatus.dwCurrentState = SERVICE_STOPPED;
-    ServiceStatus.dwWin32ExitCode = GlobalStatus ? ERROR_EXCEPTION_IN_SERVICE : NO_ERROR;
-    ServiceStatus.dwCheckPoint = 0;
-    ServiceStatus.dwWaitHint = 0;
-    ServiceStatus.dwControlsAccepted = 0;
-    SetServiceStatus(StatusHandle, &ServiceStatus);
-}       
+    LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_SERVICE_STOPPED);
+
+    if (bRunningAsService) {
+        ServiceStatus.dwCurrentState = SERVICE_STOPPED;
+        ServiceStatus.dwWin32ExitCode = GlobalStatus ? ERROR_EXCEPTION_IN_SERVICE : NO_ERROR;
+        ServiceStatus.dwCheckPoint = 7;
+        ServiceStatus.dwWaitHint = 0;
+        ServiceStatus.dwControlsAccepted = 0;
+        SetServiceStatus(StatusHandle, &ServiceStatus);
+    }
+}
 
 DWORD __stdcall afsdMain_thread(void* notUsed)
 {
@@ -1501,15 +1684,14 @@ main(int argc, char * argv[])
         if (status == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT)
         {
             DWORD tid;
+
+            bRunningAsService = FALSE;
+
             hAFSDMainThread = CreateThread(NULL, 0, afsdMain_thread, 0, 0, &tid);
-               
+
             printf("Hit <Enter> to terminate OpenAFS Client Service\n");
-            getchar();  
-#ifndef AFSIFS
+            getchar();
             SetEvent(WaitToTerminate);
-#else
-            SetEvent(DoTerminate);
-#endif
         }
     }