windows-rxkad-auth-20080902
[openafs.git] / src / WINNT / afsd / afsd_init.c
index 94d68e8..82b989e 100644 (file)
 #include <locale.h>
 #include <mbctype.h>
 #include <winsock2.h>
+#include <ErrorRep.h>
 
 #include <osi.h>
 #include "afsd.h"
+#ifdef USE_BPLUS
+#include "cm_btree.h"
+#endif
 #include <rx\rx.h>
 #include <rx\rx_null.h>
 #include <WINNT/syscfg.h>
 #include "cm_rpc.h"
 #include "lanahelper.h"
 #include <strsafe.h>
-#include "afsicf.h"
 #include "cm_memmap.h"
+#ifdef DEBUG
+#include <crtdbg.h>
+#endif
 
 extern int RXAFSCB_ExecuteRequest(struct rx_call *z_call);
 extern int RXSTATS_ExecuteRequest(struct rx_call *z_call);
 
-extern afs_int32 cryptall;
+extern afs_uint32 cryptall;
+extern afs_uint32 cm_anonvldb;
+extern int cm_enableServerLocks;
+extern int cm_followBackupPath;
+extern int cm_deleteReadOnly;
+#ifdef USE_BPLUS
+extern afs_int32 cm_BPlusTrees;
+#endif
+extern afs_int32 cm_OfflineROIsValid;
+extern afs_int32 cm_giveUpAllCBs;
+extern const clientchar_t **smb_ExecutableExtensions;
 
 osi_log_t *afsd_logp;
 
 cm_config_data_t        cm_data;
 
-char cm_rootVolumeName[VL_MAXNAMELEN];
+fschar_t cm_rootVolumeName[VL_MAXNAMELEN];
 DWORD cm_rootVolumeNameLen;
-char cm_mountRoot[1024];
+
+fschar_t cm_mountRoot[1024];
 DWORD cm_mountRootLen;
+
+clientchar_t cm_mountRootC[1024];
+DWORD cm_mountRootCLen;
+
 int cm_logChunkSize;
 int cm_chunkSize;
-#ifdef AFS_FREELANCE_CLIENT
-char *cm_FakeRootDir;
-#endif /* freelance */
 
 int smb_UseV3 = 1;
 
@@ -66,22 +84,21 @@ int logReady = 0;
 
 char cm_HostName[200];
 long cm_HostAddr;
+unsigned short cm_callbackport = CM_DEFAULT_CALLBACKPORT;
 
 char cm_NetbiosName[MAX_NB_NAME_LENGTH] = "";
+clientchar_t cm_NetbiosNameC[MAX_NB_NAME_LENGTH] = _C("");
 
 char cm_CachePath[MAX_PATH];
-DWORD cm_CachePathLen;
 DWORD cm_ValidateCache = 1;
 
-BOOL isGateway = FALSE;
-
 BOOL reportSessionStartups = FALSE;
 
 cm_initparams_v1 cm_initParams;
 
-char *cm_sysName = 0;
-unsigned int   cm_sysNameCount = 0;
-char *cm_sysNameList[MAXNUMSYSNAMES];
+clientchar_t *cm_sysName = 0;
+unsigned int  cm_sysNameCount = 0;
+clientchar_t *cm_sysNameList[MAXNUMSYSNAMES];
 
 DWORD TraceOption = 0;
 
@@ -114,47 +131,35 @@ afsi_log(char *pattern, ...)
     if ( afsi_log_useTimestamp ) {
         GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
         GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, d, sizeof(d));
-        StringCbPrintfA(u, sizeof(u), "%s %s: %s\n", d, t, s);
+        StringCbPrintfA(u, sizeof(u), "%s %s: %s\r\n", d, t, s);
         if (afsi_file != INVALID_HANDLE_VALUE)
-            WriteFile(afsi_file, u, strlen(u), &zilch, NULL);
+            WriteFile(afsi_file, u, (DWORD)strlen(u), &zilch, NULL);
 #ifdef NOTSERVICE
         printf("%s", u);
 #endif 
     } else {
         if (afsi_file != INVALID_HANDLE_VALUE)
-            WriteFile(afsi_file, s, strlen(s), &zilch, NULL);
+            WriteFile(afsi_file, s, (DWORD)strlen(s), &zilch, NULL);
     }
 }
 
-extern initUpperCaseTable();
-void afsd_initUpperCaseTable() 
-{
-    initUpperCaseTable();
-}
-
 void
 afsi_start()
 {
-    char wd[100];
+    char wd[MAX_PATH+1];
     char t[100], u[100], *p, *path;
     int zilch;
-    int code;
+    DWORD code;
     DWORD dwLow, dwHigh;
     HKEY parmKey;
     DWORD dummyLen;
     DWORD maxLogSize = 100 * 1024;
 
     afsi_file = INVALID_HANDLE_VALUE;
-    if (getenv("TEMP"))
-    {
-        StringCbCopyA(wd, sizeof(wd), getenv("TEMP"));
-    }
-    else
-    {
-        code = GetWindowsDirectory(wd, sizeof(wd));
-        if (code == 0) 
-            return;
-    }
+    code = GetTempPath(sizeof(wd)-15, wd);
+    if ( code == 0 || code > (sizeof(wd)-15) )
+        return;         /* unable to create a log */
+
     StringCbCatA(wd, sizeof(wd), "\\afsd_init.log");
     GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, t, sizeof(t));
     afsi_file = CreateFile(wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
@@ -180,15 +185,18 @@ afsi_start()
 
     SetFilePointer(afsi_file, 0, NULL, FILE_END);
     GetTimeFormat(LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, u, sizeof(u));
-    StringCbCatA(t, sizeof(t), ": Create log file\n");
-    StringCbCatA(u, sizeof(u), ": Created log file\n");
-    WriteFile(afsi_file, t, strlen(t), &zilch, NULL);
-    WriteFile(afsi_file, u, strlen(u), &zilch, NULL);
+    StringCbCatA(t, sizeof(t), ": Create log file\r\n");
+    StringCbCatA(u, sizeof(u), ": Created log file\r\n");
+    WriteFile(afsi_file, t, (DWORD)strlen(t), &zilch, NULL);
+    WriteFile(afsi_file, u, (DWORD)strlen(u), &zilch, NULL);
     p = "PATH=";
-    path = getenv("PATH");
-    WriteFile(afsi_file, p, strlen(p), &zilch, NULL);
-    WriteFile(afsi_file, path, strlen(path), &zilch, NULL);
-    WriteFile(afsi_file, "\n", 1, &zilch, NULL);
+    code = GetEnvironmentVariable("PATH", NULL, 0);
+    path = malloc(code);
+    code = GetEnvironmentVariable("PATH", path, code);
+    WriteFile(afsi_file, p, (DWORD)strlen(p), &zilch, NULL);
+    WriteFile(afsi_file, path, (DWORD)strlen(path), &zilch, NULL);
+    WriteFile(afsi_file, "\r\n", (DWORD)1, &zilch, NULL);
+    free(path);
 
     /* Initialize C RTL Code Page conversion functions */
     /* All of the path info obtained from the SMB client is in the OEM code page */
@@ -268,7 +276,7 @@ configureBackConnectionHostNames(void)
     HKEY hkMSV10;
     HKEY hkClient;
     DWORD dwType;
-    DWORD dwSize;
+    DWORD dwSize, dwAllocSize;
     DWORD dwValue;
     PBYTE pHostNames = NULL, pName = NULL;
     BOOL  bNameFound = FALSE;   
@@ -279,32 +287,41 @@ configureBackConnectionHostNames(void)
                        KEY_READ|KEY_WRITE,
                        &hkMSV10) == ERROR_SUCCESS )
     {
-        if (RegQueryValueEx( hkMSV10, "BackConnectionHostNames", 0, &dwType, NULL, &dwSize) == ERROR_SUCCESS) {
-            pHostNames = malloc(dwSize + strlen(cm_NetbiosName) + 1);
-            RegQueryValueEx( hkMSV10, "BackConnectionHostNames", 0, &dwType, pHostNames, &dwSize);
-
-            for (pName = pHostNames; *pName ; pName += strlen(pName) + 1)
+        if ((RegQueryValueEx( hkMSV10, "BackConnectionHostNames", 0, 
+                            &dwType, NULL, &dwAllocSize) == ERROR_SUCCESS) &&
+            (dwType == REG_MULTI_SZ)) 
+        {
+           dwAllocSize += 1 /* in case the source string is not nul terminated */
+               + strlen(cm_NetbiosName) + 2;
+           pHostNames = malloc(dwAllocSize);
+           dwSize = dwAllocSize;
+            if (RegQueryValueEx( hkMSV10, "BackConnectionHostNames", 0, &dwType, 
+                                pHostNames, &dwSize) == ERROR_SUCCESS) 
             {
-                if ( !stricmp(pName, cm_NetbiosName) ) {
-                    bNameFound = TRUE;
-                    break;
-                }   
-            }
+               for (pName = pHostNames; 
+                    (pName - pHostNames < (int) dwSize) && *pName ; 
+                    pName += strlen(pName) + 1)
+               {
+                   if ( !stricmp(pName, cm_NetbiosName) ) {
+                       bNameFound = TRUE;
+                       break;
+                   }   
+               }
+           }
         }
              
         if ( !bNameFound ) {
-            int size = strlen(cm_NetbiosName) + 2;
+            size_t size = strlen(cm_NetbiosName) + 2;
             if ( !pHostNames ) {
                 pHostNames = malloc(size);
-                dwSize = 1;
+               pName = pHostNames;
             }
-            pName = pHostNames;
             StringCbCopyA(pName, size, cm_NetbiosName);
             pName += size - 1;
             *pName = '\0';  /* add a second nul terminator */
 
             dwType = REG_MULTI_SZ;
-            dwSize += strlen(cm_NetbiosName) + 1;
+           dwSize = pName - pHostNames + 1;
             RegSetValueEx( hkMSV10, "BackConnectionHostNames", 0, dwType, pHostNames, dwSize);
 
             if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
@@ -370,9 +387,12 @@ configureBackConnectionHostNames(void)
         }
         RegCloseKey(hkMSV10);
     }
+
+    if (pHostNames)
+       free(pHostNames);
 }
 
-#if !defined(DJGPP)
+
 static void afsd_InitServerPreferences(void)
 {
     HKEY hkPrefs = 0;
@@ -444,7 +464,7 @@ static void afsd_InitServerPreferences(void)
             }
             else       /* add a new server without a cell */
             {
-                tsp = cm_NewServer(&saddr, CM_SERVER_VLDB, NULL); /* refcount = 1 */
+                tsp = cm_NewServer(&saddr, CM_SERVER_VLDB, NULL, CM_FLAG_NOPROBE); /* refcount = 1 */
                 tsp->ipRank = (USHORT)dwRank;
             }
         }
@@ -514,7 +534,7 @@ static void afsd_InitServerPreferences(void)
             }
             else       /* add a new server without a cell */
             {
-                tsp = cm_NewServer(&saddr, CM_SERVER_FILE, NULL); /* refcount = 1 */
+                tsp = cm_NewServer(&saddr, CM_SERVER_FILE, NULL, CM_FLAG_NOPROBE); /* refcount = 1 */
                 tsp->ipRank = (USHORT)dwRank;
             }
         }
@@ -522,7 +542,6 @@ static void afsd_InitServerPreferences(void)
         RegCloseKey(hkPrefs);
     }
 }
-#endif /* DJGPP */
 
 /*
  * AFSD Initialization
@@ -531,16 +550,24 @@ static void afsd_InitServerPreferences(void)
 int afsd_InitCM(char **reasonP)
 {
     osi_uid_t debugID;
-    DWORD cacheBlocks;
+    afs_uint64 cacheBlocks;
     DWORD cacheSize;
+    DWORD blockSize;
     long logChunkSize;
     DWORD stats;
+    DWORD volumes;
+    DWORD cells;
+    DWORD dwValue;
+    DWORD rx_enable_peer_stats;
+    DWORD rx_enable_process_stats;
+    DWORD rx_udpbufsize = -1;
+    DWORD lockOrderValidation;
     long traceBufSize;
     long maxcpus;
     long ltt, ltto;
     long rx_nojumbo;
     long virtualCache = 0;
-    char rootCellName[256];
+    fschar_t rootCellName[256];
     struct rx_service *serverp;
     static struct rx_securityClass *nullServerSecurityClassp;
     struct hostent *thp;
@@ -552,9 +579,7 @@ int afsd_InitCM(char **reasonP)
     long code;
     /*int freelanceEnabled;*/
     WSADATA WSAjunk;
-    lana_number_t lanaNum;
     int i;
-    char *p, *q; 
     int cm_noIPAddr;         /* number of client network interfaces */
     int cm_IPAddr[CM_MAXINTERFACE_ADDR];    /* client's IP address in host order */
     int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
@@ -563,16 +588,19 @@ int afsd_InitCM(char **reasonP)
 
     WSAStartup(0x0101, &WSAjunk);
 
-    afsd_initUpperCaseTable();
+    init_et_to_sys_error();
 
     /* setup osidebug server at RPC slot 1000 */
     osi_LongToUID(1000, &debugID);
     code = osi_InitDebug(&debugID);
     afsi_log("osi_InitDebug code %d", code);
 
-    // osi_LockTypeSetDefault("stat"); /* comment this out for speed *
+    // osi_LockTypeSetDefault("stat"); /* comment this out for speed */
     if (code != 0) {
-        *reasonP = "unknown error";
+        if (code == RPC_S_NO_PROTSEQS)
+            *reasonP = "No RPC Protocol Sequences registered.  Check HKLM\\SOFTWARE\\Microsoft\\RPC\\ClientProtocols";
+        else
+            *reasonP = "unknown error";
         return -1;
     }
 
@@ -587,7 +615,7 @@ int afsd_InitCM(char **reasonP)
 
     /* Look up configuration parameters in Registry */
     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
-                         0, KEY_QUERY_VALUE, &parmKey);
+                        0, KEY_QUERY_VALUE, &parmKey);
     if (code != ERROR_SUCCESS) {
         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM
                        | FORMAT_MESSAGE_ALLOCATE_BUFFER,
@@ -598,6 +626,19 @@ int afsd_InitCM(char **reasonP)
         osi_panic(buf, __FILE__, __LINE__);
     }
 
+    dummyLen = sizeof(lockOrderValidation);
+    code = RegQueryValueEx(parmKey, "LockOrderValidation", NULL, NULL,
+                            (BYTE *) &lockOrderValidation, &dummyLen);
+    if (code != ERROR_SUCCESS) {
+#ifdef DEBUG
+        lockOrderValidation = 1;
+#else
+        lockOrderValidation = 0;
+#endif
+    }
+    osi_SetLockOrderValidation(lockOrderValidation);
+    afsi_log("Lock Order Validation %s", lockOrderValidation ? "On" : "Off");
+
     dummyLen = sizeof(maxcpus);
     code = RegQueryValueEx(parmKey, "MaxCPUs", NULL, NULL,
                             (BYTE *) &maxcpus, &dummyLen);
@@ -637,7 +678,7 @@ int afsd_InitCM(char **reasonP)
     dummyLen = sizeof(TraceOption);
     code = RegQueryValueEx(parmKey, "TraceOption", NULL, NULL,
                             (BYTE *) &TraceOption, &dummyLen);
-    afsi_log("Event Log Tracing = %lX", TraceOption);
+    afsi_log("Trace Options = %lX", TraceOption);
 
     dummyLen = sizeof(traceBufSize);
     code = RegQueryValueEx(parmKey, "TraceBufferSize", NULL, NULL,
@@ -651,8 +692,14 @@ int afsd_InitCM(char **reasonP)
 
     /* setup and enable debug log */
     afsd_logp = osi_LogCreate("afsd", traceBufSize);
-    afsi_log("osi_LogCreate log addr %x", (int)afsd_logp);
-    osi_LogEnable(afsd_logp);
+    afsi_log("osi_LogCreate log addr %x", PtrToUlong(afsd_logp));
+    if ((TraceOption & 0x8)
+#ifdef DEBUG
+        || 1
+#endif
+        ) {
+       osi_LogEnable(afsd_logp);
+    }
     logReady = 1;
 
     osi_Log0(afsd_logp, "Log init");
@@ -676,20 +723,53 @@ int afsd_InitCM(char **reasonP)
                       logChunkSize);
             logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
         }
-        afsi_log("Chunk size %d", logChunkSize);
     } else {
         logChunkSize = CM_CONFIGDEFAULT_CHUNKSIZE;
-        afsi_log("Default chunk size %d", logChunkSize);
     }
     cm_logChunkSize = logChunkSize;
     cm_chunkSize = 1 << logChunkSize;
+    afsi_log("Chunk size %u (%d)", cm_chunkSize, cm_logChunkSize);
+
+    dummyLen = sizeof(blockSize);
+    code = RegQueryValueEx(parmKey, "blockSize", NULL, NULL,
+                            (BYTE *) &blockSize, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        if (blockSize < 1 || 
+            (blockSize > 1024 && (blockSize % CM_CONFIGDEFAULT_BLOCKSIZE != 0))) 
+        {
+            afsi_log("Invalid block size %u specified, using default", blockSize);
+            blockSize = CM_CONFIGDEFAULT_BLOCKSIZE;
+        } else {
+            /* 
+             * if the blockSize is less than 1024 we permit the blockSize to be
+             * specified in multiples of the default blocksize
+             */
+            if (blockSize <= 1024)
+                blockSize *= CM_CONFIGDEFAULT_BLOCKSIZE;
+        }
+    } else {
+        blockSize = CM_CONFIGDEFAULT_BLOCKSIZE;
+    }
+    if (blockSize > cm_chunkSize) {
+        afsi_log("Block size (%d) cannot be larger than Chunk size (%d).", 
+                  blockSize, cm_chunkSize);
+        blockSize = cm_chunkSize;
+    }
+    if (cm_chunkSize % blockSize != 0) {
+        afsi_log("Block size (%d) must be a factor of Chunk size (%d).",
+                  blockSize, cm_chunkSize);
+        blockSize = CM_CONFIGDEFAULT_BLOCKSIZE;
+    }
+    afsi_log("Block size %u", blockSize);
 
     dummyLen = sizeof(numBkgD);
     code = RegQueryValueEx(parmKey, "Daemons", NULL, NULL,
                             (BYTE *) &numBkgD, &dummyLen);
-    if (code == ERROR_SUCCESS)
+    if (code == ERROR_SUCCESS) {
+        if (numBkgD > CM_MAX_DAEMONS)
+            numBkgD = CM_MAX_DAEMONS;
         afsi_log("%d background daemons", numBkgD);
-    else {
+    } else {
         numBkgD = CM_CONFIGDEFAULT_DAEMONS;
         afsi_log("Defaulting to %d background daemons", numBkgD);
     }
@@ -708,65 +788,80 @@ int afsd_InitCM(char **reasonP)
     code = RegQueryValueEx(parmKey, "Stats", NULL, NULL,
                             (BYTE *) &stats, &dummyLen);
     if (code == ERROR_SUCCESS)
-        afsi_log("Status cache size %d", stats);
+        afsi_log("Status cache entries: %d", stats);
     else {
         stats = CM_CONFIGDEFAULT_STATS;
-        afsi_log("Default status cache size %d", stats);
+        afsi_log("Default status cache entries: %d", stats);
+    }
+
+    dummyLen = sizeof(volumes);
+    code = RegQueryValueEx(parmKey, "Volumes", NULL, NULL,
+                            (BYTE *) &volumes, &dummyLen);
+    if (code == ERROR_SUCCESS)
+        afsi_log("Volumes cache entries: %d", volumes);
+    else {
+        volumes = CM_CONFIGDEFAULT_STATS / 3;
+        afsi_log("Default volume cache entries: %d", volumes);
+    }
+
+    dummyLen = sizeof(cells);
+    code = RegQueryValueEx(parmKey, "Cells", NULL, NULL,
+                            (BYTE *) &cells, &dummyLen);
+    if (code == ERROR_SUCCESS)
+        afsi_log("Cell cache entries: %d", cells);
+    else {
+        cells = CM_CONFIGDEFAULT_CELLS;
+        afsi_log("Default cell cache entries: %d", cells);
     }
 
     dummyLen = sizeof(ltt);
     code = RegQueryValueEx(parmKey, "LogoffTokenTransfer", NULL, NULL,
                             (BYTE *) &ltt, &dummyLen);
-    if (code == ERROR_SUCCESS)
-        afsi_log("Logoff token transfer %s",  (ltt ? "on" : "off"));
-    else {
+    if (code != ERROR_SUCCESS)
         ltt = 1;
-        afsi_log("Logoff token transfer on by default");
-    }
     smb_LogoffTokenTransfer = ltt;
-    afsi_log("Logoff token transfer is currently ignored");
+    afsi_log("Logoff token transfer %s",  (ltt ? "on" : "off"));
 
     if (ltt) {
         dummyLen = sizeof(ltto);
         code = RegQueryValueEx(parmKey, "LogoffTokenTransferTimeout",
                                 NULL, NULL, (BYTE *) &ltto, &dummyLen);
-        if (code == ERROR_SUCCESS)
-            afsi_log("Logoff token tranfer timeout %d seconds", ltto);
-        else {
-            ltto = 10;
-            afsi_log("Default logoff token transfer timeout 10 seconds");
-        }
+        if (code != ERROR_SUCCESS)
+            ltto = 120;
     } else {
         ltto = 0;
     }   
     smb_LogoffTransferTimeout = ltto;
-    afsi_log("Default logoff token is currently ignored");
+    afsi_log("Logoff token transfer timeout %d seconds", ltto);
 
     dummyLen = sizeof(cm_rootVolumeName);
     code = RegQueryValueEx(parmKey, "RootVolume", NULL, NULL,
-                            cm_rootVolumeName, &dummyLen);
+                            (LPBYTE) cm_rootVolumeName, &dummyLen);
     if (code == ERROR_SUCCESS)
         afsi_log("Root volume %s", cm_rootVolumeName);
     else {
-        StringCbCopyA(cm_rootVolumeName, sizeof(cm_rootVolumeName), "root.afs");
+        cm_FsStrCpy(cm_rootVolumeName, lengthof(cm_rootVolumeName), "root.afs");
         afsi_log("Default root volume name root.afs");
     }
 
-    cm_mountRootLen = sizeof(cm_mountRoot);
-    code = RegQueryValueEx(parmKey, "MountRoot", NULL, NULL,
-                            cm_mountRoot, &cm_mountRootLen);
+    cm_mountRootCLen = sizeof(cm_mountRootC);
+    code = RegQueryValueExW(parmKey, L"MountRoot", NULL, NULL,
+                            (LPBYTE) cm_mountRootC, &cm_mountRootCLen);
     if (code == ERROR_SUCCESS) {
-        afsi_log("Mount root %s", cm_mountRoot);
-        cm_mountRootLen = strlen(cm_mountRoot);
+        afsi_log("Mount root %S", cm_mountRootC);
+        cm_mountRootCLen = cm_ClientStrLen(cm_mountRootC);
     } else {
-        StringCbCopyA(cm_mountRoot, sizeof(cm_mountRoot), "/afs");
-        cm_mountRootLen = 4;
+        cm_ClientStrCpy(cm_mountRootC, lengthof(cm_mountRootC), _C("/afs"));
+        cm_mountRootCLen = cm_ClientStrLen(cm_mountRootC);
         /* Don't log */
     }
 
+    cm_ClientStringToFsString(cm_mountRootC, -1, cm_mountRoot, lengthof(cm_mountRoot));
+    cm_mountRootLen = cm_FsStrLen(cm_mountRoot);
+
     dummyLen = sizeof(buf);
     code = RegQueryValueEx(parmKey, "CachePath", NULL, &regType,
-                            buf, &dummyLen);
+                           buf, &dummyLen);
     if (code == ERROR_SUCCESS && buf[0]) {
         if (regType == REG_EXPAND_SZ) {
             dummyLen = ExpandEnvironmentStrings(buf, cm_CachePath, sizeof(cm_CachePath));
@@ -815,13 +910,9 @@ int afsd_InitCM(char **reasonP)
     dummyLen = sizeof(traceOnPanic);
     code = RegQueryValueEx(parmKey, "TrapOnPanic", NULL, NULL,
                             (BYTE *) &traceOnPanic, &dummyLen);
-    if (code == ERROR_SUCCESS)
-        afsi_log("Set to %s on panic",
-                  traceOnPanic ? "trap" : "not trap");
-    else {  
-        traceOnPanic = 0;
-        /* Don't log */
-    }
+    if (code != ERROR_SUCCESS)
+        traceOnPanic = 1;              /* log */
+    afsi_log("Set to %s on panic", traceOnPanic ? "trap" : "not trap");
 
     dummyLen = sizeof(reportSessionStartups);
     code = RegQueryValueEx(parmKey, "ReportSessionStartups", NULL, NULL,
@@ -835,54 +926,68 @@ int afsd_InitCM(char **reasonP)
     }
 
     for ( i=0; i < MAXNUMSYSNAMES; i++ ) {
-        cm_sysNameList[i] = osi_Alloc(MAXSYSNAME);
+        cm_sysNameList[i] = osi_Alloc(MAXSYSNAME * sizeof(clientchar_t));
         cm_sysNameList[i][0] = '\0';
     }
     cm_sysName = cm_sysNameList[0];
 
-    dummyLen = sizeof(buf);
-    code = RegQueryValueEx(parmKey, "SysName", NULL, NULL, buf, &dummyLen);
-    if (code != ERROR_SUCCESS || !buf[0]) {
+    {
+        clientchar_t *p, *q;
+        clientchar_t * cbuf = (clientchar_t *) buf;
+        dummyLen = sizeof(buf);
+        code = RegQueryValueExW(parmKey, L"SysName", NULL, NULL, (LPBYTE) cbuf, &dummyLen);
+        if (code != ERROR_SUCCESS || !cbuf[0]) {
 #if defined(_IA64_)
-        StringCbCopyA(buf, sizeof(buf), "ia64_win64");
-#elif defined(_AMD64)
-        StringCbCopyA(buf, sizeof(buf), "amd64_win64");
+            cm_ClientStrCpy(cbuf, lengthof(buf), _C("ia64_win64"));
+#elif defined(_AMD64_)
+            cm_ClientStrCpy(cbuf, lengthof(buf), _C("amd64_win64 x86_win32 i386_w2k"));
 #else /* assume x86 32-bit */
-        StringCbCopyA(buf, sizeof(buf), "x86_win32 i386_w2k i386_nt40");
+            cm_ClientStrCpy(cbuf, lengthof(buf), _C("x86_win32 i386_w2k i386_nt40"));
 #endif
-    }
-    afsi_log("Sys name %s", buf); 
-
-    /* breakup buf into individual search string entries */
-    for (p = q = buf; p < buf + dummyLen; p++)
-    {
-        if (*p == '\0' || isspace(*p)) {
-            memcpy(cm_sysNameList[cm_sysNameCount],q,p-q);
-            cm_sysNameList[cm_sysNameCount][p-q] = '\0';
-            cm_sysNameCount++;
-
-            do {
-                if (*p == '\0')
-                    goto done_sysname;
-                p++;
-            } while (*p == '\0' || isspace(*p));
-            q = p;
-            p--;
+        }
+        afsi_log("Sys name %S", cbuf); 
+
+        /* breakup buf into individual search string entries */
+        for (p = q = cbuf; p < cbuf + dummyLen; p++) {
+            if (*p == '\0' || iswspace(*p)) {
+                memcpy(cm_sysNameList[cm_sysNameCount],q,(p-q) * sizeof(clientchar_t));
+                cm_sysNameList[cm_sysNameCount][p-q] = '\0';
+                cm_sysNameCount++;
+                do {
+                    if (*p == '\0')
+                        goto done_sysname;
+                    p++;
+                } while (*p == '\0' || isspace(*p));
+                q = p;
+                p--;
+            }
         }
     }
   done_sysname:
-    StringCbCopyA(cm_sysName, MAXSYSNAME, cm_sysNameList[0]);
+    cm_ClientStrCpy(cm_sysName, MAXSYSNAME, cm_sysNameList[0]);
 
     dummyLen = sizeof(cryptall);
     code = RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
-                            (BYTE *) &cryptall, &dummyLen);
+                           (BYTE *) &cryptall, &dummyLen);
     if (code == ERROR_SUCCESS) {
-        afsi_log("SecurityLevel is %s", cryptall?"crypt":"clear");
+        afsi_log("SecurityLevel is %s", cryptall == 1?"crypt": cryptall == 2?"auth":"clear");
     } else {
         cryptall = 0;
         afsi_log("Default SecurityLevel is clear");
     }
 
+    if (cryptall == 1)
+       LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_CRYPT_ON);
+    else if (cryptall == 2)
+       LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_CRYPT_AUTH);
+    else
+       LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_CRYPT_OFF);
+
+    dummyLen = sizeof(cryptall);
+    code = RegQueryValueEx(parmKey, "ForceAnonVLDB", NULL, NULL,
+                            (BYTE *) &cm_anonvldb, &dummyLen);
+    afsi_log("CM ForceAnonVLDB is %s", cm_anonvldb ? "on" : "off");
+
 #ifdef AFS_AFSDB_ENV
     dummyLen = sizeof(cm_dnsEnabled);
     code = RegQueryValueEx(parmKey, "UseDNS", NULL, NULL,
@@ -908,28 +1013,18 @@ int afsd_InitCM(char **reasonP)
                   cm_freelanceEnabled ? "is" : "is not");
     }       
     else {
-        cm_freelanceEnabled = 0;  /* default off */
+        cm_freelanceEnabled = 1;  /* default on */
     }
 #endif /* AFS_FREELANCE_CLIENT */
 
-#ifdef COMMENT
-    /* The netbios name is looked up in lana_GetUNCServerNameEx */
-    dummyLen = sizeof(buf);
-    code = RegQueryValueEx(parmKey, "NetbiosName", NULL, NULL,
-                           (BYTE *) &buf, &dummyLen);
-    if (code == ERROR_SUCCESS) {
-        DWORD len = ExpandEnvironmentStrings(buf, cm_NetbiosName, MAX_NB_NAME_LENGTH);
-        if ( len > 0 && len <= MAX_NB_NAME_LENGTH ) {
-            afsi_log("Explicit NetBios name is used %s", cm_NetbiosName);
-        } else {
-            afsi_log("Unable to Expand Explicit NetBios name: %s", buf);
-            cm_NetbiosName[0] = 0;  /* turn it off */
-        }
-    }
-    else {
-        cm_NetbiosName[0] = 0;   /* default off */
+    dummyLen = sizeof(smb_UseUnicode);
+    code = RegQueryValueEx(parmKey, "NegotiateUnicode", NULL, NULL,
+                           (BYTE *) &smb_UseUnicode, &dummyLen);
+    if (code != ERROR_SUCCESS) {
+        smb_UseUnicode = 1; /* default on */
     }
-#endif
+    afsi_log("SMB Server Unicode Support is %s",
+              smb_UseUnicode ? "enabled" : "disabled");
 
     dummyLen = sizeof(smb_hideDotFiles);
     code = RegQueryValueEx(parmKey, "HideDotFiles", NULL, NULL,
@@ -974,6 +1069,26 @@ int afsd_InitCM(char **reasonP)
     }
     if (rx_nojumbo)
         afsi_log("RX Jumbograms are disabled");
+    else
+        afsi_log("RX Jumbograms are enabled");
+
+    dummyLen = sizeof(rx_extraPackets);
+    code = RegQueryValueEx(parmKey, "RxExtraPackets", NULL, NULL,
+                           (BYTE *) &rx_extraPackets, &dummyLen);
+    if (code != ERROR_SUCCESS) {
+        rx_extraPackets = (numBkgD + numSvThreads + 5) * 64;
+    }
+    if (rx_extraPackets)
+        afsi_log("RX extraPackets is %d", rx_extraPackets);
+
+    dummyLen = sizeof(rx_udpbufsize);
+    code = RegQueryValueEx(parmKey, "RxUdpBufSize", NULL, NULL,
+                           (BYTE *) &rx_udpbufsize, &dummyLen);
+    if (code != ERROR_SUCCESS) {
+        rx_udpbufsize = 256*1024;
+    }
+    if (rx_udpbufsize != -1)
+        afsi_log("RX udpbufsize is %d", rx_udpbufsize);
 
     dummyLen = sizeof(rx_mtu);
     code = RegQueryValueEx(parmKey, "RxMaxMTU", NULL, NULL,
@@ -984,38 +1099,149 @@ int afsd_InitCM(char **reasonP)
     if (rx_mtu != -1)
         afsi_log("RX maximum MTU is %d", rx_mtu);
 
-    dummyLen = sizeof(ConnDeadtimeout);
-    code = RegQueryValueEx(parmKey, "ConnDeadTimeout", NULL, NULL,
-                           (BYTE *) &ConnDeadtimeout, &dummyLen);
-    afsi_log("ConnDeadTimeout is %d", ConnDeadtimeout);
+    dummyLen = sizeof(rx_enable_peer_stats);
+    code = RegQueryValueEx(parmKey, "RxEnablePeerStats", NULL, NULL,
+                           (BYTE *) &rx_enable_peer_stats, &dummyLen);
+    if (code != ERROR_SUCCESS) {
+        rx_enable_peer_stats = 1;
+    }
+    if (rx_enable_peer_stats)
+        afsi_log("RX Peer Statistics gathering is enabled");
+    else
+        afsi_log("RX Peer Statistics gathering is disabled");
+
+    dummyLen = sizeof(rx_enable_process_stats);
+    code = RegQueryValueEx(parmKey, "RxEnableProcessStats", NULL, NULL,
+                           (BYTE *) &rx_enable_process_stats, &dummyLen);
+    if (code != ERROR_SUCCESS) {
+        rx_enable_process_stats = 1;
+    }
+    if (rx_enable_process_stats)
+        afsi_log("RX Process Statistics gathering is enabled");
+    else
+        afsi_log("RX Process Statistics gathering is disabled");
+
+    dummyLen = sizeof(dwValue);
+    dwValue = 0;
+    code = RegQueryValueEx(parmKey, "RxEnableHotThread", NULL, NULL,
+                            (BYTE *) &dwValue, &dummyLen);
+     if (code != ERROR_SUCCESS || dwValue != 0) {
+         rx_EnableHotThread();
+         afsi_log("RX Hot Thread is enabled");
+     }
+     else
+         afsi_log("RX Hot Thread is disabled");
+
+    dummyLen = sizeof(DWORD);
+    code = RegQueryValueEx(parmKey, "CallBackPort", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        cm_callbackport = (unsigned short) dwValue;
+    }
+    afsi_log("CM CallBackPort is %u", cm_callbackport);
 
-    dummyLen = sizeof(HardDeadtimeout);
-    code = RegQueryValueEx(parmKey, "HardDeadTimeout", NULL, NULL,
-                           (BYTE *) &HardDeadtimeout, &dummyLen);
-    afsi_log("HardDeadTimeout is %d", HardDeadtimeout);
+    dummyLen = sizeof(DWORD);
+    code = RegQueryValueEx(parmKey, "EnableServerLocks", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        cm_enableServerLocks = (unsigned short) dwValue;
+    } 
+    switch (cm_enableServerLocks) {
+    case 0:
+       afsi_log("EnableServerLocks: never");
+       break;
+    case 2:
+       afsi_log("EnableServerLocks: always");
+       break;
+    case 1:
+    default:
+       afsi_log("EnableServerLocks: server requested");
+       break;
+    }
 
-    RegCloseKey (parmKey);
+    dummyLen = sizeof(DWORD);
+    code = RegQueryValueEx(parmKey, "DeleteReadOnly", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        cm_deleteReadOnly = (unsigned short) dwValue;
+    } 
+    afsi_log("CM DeleteReadOnly is %u", cm_deleteReadOnly);
+    
+#ifdef USE_BPLUS
+    dummyLen = sizeof(DWORD);
+    code = RegQueryValueEx(parmKey, "BPlusTrees", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        cm_BPlusTrees = (unsigned short) dwValue;
+    } 
+    afsi_log("CM BPlusTrees is %u", cm_BPlusTrees);
 
-    /* Call lanahelper to get Netbios name, lan adapter number and gateway flag */
-    if (SUCCEEDED(code = lana_GetUncServerNameEx(cm_NetbiosName, &lanaNum, &isGateway, LANA_NETBIOS_NAME_FULL))) {
-        LANadapter = (lanaNum == LANA_INVALID)? -1: lanaNum;
+    if (cm_BPlusTrees && !cm_InitBPlusDir()) {
+        cm_BPlusTrees = 0;
+        afsi_log("CM BPlusTree initialization failure; disabled for this session");
+    }
+#else
+    afsi_log("CM BPlusTrees is not supported");
+#endif
 
-        if (LANadapter != -1)
-            afsi_log("LAN adapter number %d", LANadapter);
-        else
-            afsi_log("LAN adapter number not determined");
+    if ((RegQueryValueExW( parmKey, L"PrefetchExecutableExtensions", 0, 
+                           &regType, NULL, &dummyLen) == ERROR_SUCCESS) &&
+         (regType == REG_MULTI_SZ)) 
+    {
+        clientchar_t * pSz;
+        dummyLen += 3; /* in case the source string is not nul terminated */
+        pSz = malloc(dummyLen);
+        if ((RegQueryValueExW( parmKey, L"PrefetchExecutableExtensions", 0, &regType, 
+                               (LPBYTE) pSz, &dummyLen) == ERROR_SUCCESS) &&
+             (regType == REG_MULTI_SZ))
+        {
+            int cnt;
+            clientchar_t * p;
 
-        if (isGateway)
-            afsi_log("Set for gateway service");
+            for (cnt = 0, p = pSz; (p - pSz < dummyLen) && *p; cnt++, p += cm_ClientStrLen(p) + 1);
 
-        afsi_log("Using >%s< as SMB server name", cm_NetbiosName);
-    } else {
-        /* something went horribly wrong.  We can't proceed without a netbios name */
-        StringCbPrintfA(buf,sizeof(buf),"Netbios name could not be determined: %li", code);
-        osi_panic(buf, __FILE__, __LINE__);
+            smb_ExecutableExtensions = malloc(sizeof(clientchar_t *) * (cnt+1));
+
+            for (cnt = 0, p = pSz; (p - pSz < dummyLen) && *p; cnt++, p += cm_ClientStrLen(p) + 1) {
+                smb_ExecutableExtensions[cnt] = p;
+                afsi_log("PrefetchExecutableExtension: \"%S\"", p);
+            }
+            smb_ExecutableExtensions[cnt] = NULL;
+        }
+        
+        if (!smb_ExecutableExtensions)
+            free(pSz);
     }
+    if (!smb_ExecutableExtensions)
+        afsi_log("No PrefetchExecutableExtensions");
+
+    dummyLen = sizeof(DWORD);
+    code = RegQueryValueEx(parmKey, "OfflineReadOnlyIsValid", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        cm_OfflineROIsValid = (unsigned short) dwValue;
+    } 
+    afsi_log("CM OfflineReadOnlyIsValid is %u", cm_deleteReadOnly);
+    
+    dummyLen = sizeof(DWORD);
+    code = RegQueryValueEx(parmKey, "GiveUpAllCallBacks", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        cm_giveUpAllCBs = (unsigned short) dwValue;
+    } 
+    afsi_log("CM GiveUpAllCallBacks is %u", cm_giveUpAllCBs);
+
+    dummyLen = sizeof(DWORD);
+    code = RegQueryValueEx(parmKey, "FollowBackupPath", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        cm_followBackupPath = (unsigned short) dwValue;
+    } 
+    afsi_log("CM FollowBackupPath is %u", cm_followBackupPath);
 
-    cacheBlocks = (cacheSize * 1024) / CM_CONFIGDEFAULT_BLOCKSIZE;
+    RegCloseKey (parmKey);
+
+    cacheBlocks = ((afs_uint64)cacheSize * 1024) / blockSize;
         
     /* get network related info */
     cm_noIPAddr = CM_MAXINTERFACE_ADDR;
@@ -1034,28 +1260,14 @@ int afsd_InitCM(char **reasonP)
      */
     cm_initParams.nChunkFiles = 0;
     cm_initParams.nStatCaches = stats;
-    cm_initParams.nDataCaches = cacheBlocks;
-    cm_initParams.nVolumeCaches = stats/2;
+    cm_initParams.nDataCaches = (afs_uint32)(cacheBlocks > 0xFFFFFFFF ? 0xFFFFFFFF : cacheBlocks);
+    cm_initParams.nVolumeCaches = volumes;
     cm_initParams.firstChunkSize = cm_chunkSize;
     cm_initParams.otherChunkSize = cm_chunkSize;
     cm_initParams.cacheSize = cacheSize;
     cm_initParams.setTime = 0;
     cm_initParams.memCache = 1;
 
-    /* Set RX parameters before initializing RX */
-    if ( rx_nojumbo ) {
-        rx_SetNoJumbo();
-        afsi_log("rx_SetNoJumbo successful");
-    }
-
-    if ( rx_mtu != -1 ) {
-        rx_SetMaxMTU(rx_mtu);
-        afsi_log("rx_SetMaxMTU %d successful", rx_mtu);
-    }
-
-    /* Open Microsoft Firewall to allow in port 7001 */
-    icf_CheckAndAddAFSPorts(AFS_PORTSET_CLIENT);
-
     /* Ensure the AFS Netbios Name is registered to allow loopback access */
     configureBackConnectionHostNames();
 
@@ -1071,8 +1283,10 @@ int afsd_InitCM(char **reasonP)
     smb_InitIoctl();
         
     cm_InitCallback();
-        
-    code = cm_InitMappedMemory(virtualCache, cm_CachePath, stats, cm_chunkSize, cacheBlocks);
+
+    cm_InitNormalization();
+
+    code = cm_InitMappedMemory(virtualCache, cm_CachePath, stats, volumes, cells, cm_chunkSize, cacheBlocks, blockSize);
     afsi_log("cm_InitMappedMemory code %x", code);
     if (code != 0) {
         *reasonP = "error initializing cache file";
@@ -1087,13 +1301,35 @@ int afsd_InitCM(char **reasonP)
 #endif
 #endif
 
-    /* initialize RX, and tell it to listen to port 7001, which is used for
-     * callback RPC messages.
+    /* Set RX parameters before initializing RX */
+    if ( rx_nojumbo ) {
+        rx_SetNoJumbo();
+        afsi_log("rx_SetNoJumbo successful");
+    }
+
+    if ( rx_mtu != -1 ) {
+        extern void rx_SetMaxMTU(int);
+
+        rx_SetMaxMTU(rx_mtu);
+        afsi_log("rx_SetMaxMTU %d successful", rx_mtu);
+    }
+
+    if ( rx_udpbufsize != -1 ) {
+        rx_SetUdpBufSize(rx_udpbufsize);
+        afsi_log("rx_SetUdpBufSize %d", rx_udpbufsize);
+    }
+
+    /* initialize RX, and tell it to listen to the callbackport, 
+     * which is used for callback RPC messages.
      */
-    code = rx_Init(htons(7001));
+    code = rx_Init(htons(cm_callbackport));
+    if (code != 0) {
+       afsi_log("rx_Init code %x - retrying with a random port number", code);
+       code = rx_Init(0);
+    }
     afsi_log("rx_Init code %x", code);
     if (code != 0) {
-        *reasonP = "afsd: failed to init rx client on port 7001";
+        *reasonP = "afsd: failed to init rx client";
         return -1;
     }
 
@@ -1101,7 +1337,7 @@ int afsd_InitCM(char **reasonP)
     nullServerSecurityClassp = rxnull_NewServerSecurityObject();
     serverp = rx_NewService(0, 1, "AFS", &nullServerSecurityClassp, 1,
                              RXAFSCB_ExecuteRequest);
-    afsi_log("rx_NewService addr %x", (int)serverp);
+    afsi_log("rx_NewService addr %x", PtrToUlong(serverp));
     if (serverp == NULL) {
         *reasonP = "unknown error";
         return -1;
@@ -1110,7 +1346,7 @@ int afsd_InitCM(char **reasonP)
     nullServerSecurityClassp = rxnull_NewServerSecurityObject();
     serverp = rx_NewService(0, RX_STATS_SERVICE_ID, "rpcstats",
                              &nullServerSecurityClassp, 1, RXSTATS_ExecuteRequest);
-    afsi_log("rx_NewService addr %x", (int)serverp);
+    afsi_log("rx_NewService addr %x", PtrToUlong(serverp));
     if (serverp == NULL) {
         *reasonP = "unknown error";
         return -1;
@@ -1120,12 +1356,18 @@ int afsd_InitCM(char **reasonP)
     rx_StartServer(0);
     afsi_log("rx_StartServer");
 
+    if (rx_enable_peer_stats)
+       rx_enablePeerRPCStats();
+
+    if (rx_enable_process_stats)
+       rx_enableProcessRPCStats();
+
     code = cm_GetRootCellName(rootCellName);
     afsi_log("cm_GetRootCellName code %d, cm_freelanceEnabled= %d, rcn= %s", 
-              code, cm_freelanceEnabled, (code ? "<none>" : rootCellName));
+             code, cm_freelanceEnabled, (code ? "<none>" : rootCellName));
     if (code != 0 && !cm_freelanceEnabled) 
     {
-        *reasonP = "can't find root cell name in afsd.ini";
+        *reasonP = "can't find root cell name in " AFS_CELLSERVDB;
         return -1;
     }   
     else if (cm_freelanceEnabled)
@@ -1134,10 +1376,10 @@ int afsd_InitCM(char **reasonP)
     if (code == 0 && !cm_freelanceEnabled) 
     {
         cm_data.rootCellp = cm_GetCell(rootCellName, CM_FLAG_CREATE);
-        afsi_log("cm_GetCell addr %x", (int)cm_data.rootCellp);
+        afsi_log("cm_GetCell addr %x", PtrToUlong(cm_data.rootCellp));
         if (cm_data.rootCellp == NULL) 
         {
-            *reasonP = "can't find root cell in afsdcell.ini";
+            *reasonP = "can't find root cell in " AFS_CELLSERVDB;
             return -1;
         }
     }
@@ -1150,9 +1392,16 @@ int afsd_InitCM(char **reasonP)
     /* Initialize the RPC server for session keys */
     RpcInit();
 
-#if !defined(DJGPP)
     afsd_InitServerPreferences();
-#endif
+    return 0;
+}
+
+int afsd_ShutdownCM(void)
+{
+    cm_ReleaseSCache(cm_data.rootSCachep);
+
+    cm_shutdown = 1;
+
     return 0;
 }
 
@@ -1166,11 +1415,15 @@ int afsd_InitDaemons(char **reasonP)
     /* this should really be in an init daemon from here on down */
 
     if (!cm_freelanceEnabled) {
+       int attempts = 10;
+
         osi_Log0(afsd_logp, "Loading Root Volume from cell");
-        code = cm_GetVolumeByName(cm_data.rootCellp, cm_rootVolumeName, cm_rootUserp,
-                                  &req, CM_FLAG_CREATE, &cm_data.rootVolumep);
-        afsi_log("cm_GetVolumeByName code %x root vol %x", code,
-                 (code ? (cm_volume_t *)-1 : cm_data.rootVolumep));
+       do {
+           code = cm_FindVolumeByName(cm_data.rootCellp, cm_rootVolumeName, cm_rootUserp,
+                                      &req, CM_GETVOL_FLAG_CREATE, &cm_data.rootVolumep);
+           afsi_log("cm_FindVolumeByName code %x root vol %x", code,
+                     (code ? (cm_volume_t *)-1 : cm_data.rootVolumep));
+       } while (code && --attempts);
         if (code != 0) {
             *reasonP = "can't find root volume in root cell";
             return -1;
@@ -1179,10 +1432,7 @@ int afsd_InitDaemons(char **reasonP)
 
     /* compute the root fid */
     if (!cm_freelanceEnabled) {
-        cm_data.rootFid.cell = cm_data.rootCellp->cellID;
-        cm_data.rootFid.volume = cm_GetROVolumeID(cm_data.rootVolumep);
-        cm_data.rootFid.vnode = 1;
-        cm_data.rootFid.unique = 1;
+        cm_SetFid(&cm_data.rootFid, cm_data.rootCellp->cellID, cm_GetROVolumeID(cm_data.rootVolumep), 1, 1);
     }
     else
         cm_FakeRootFid(&cm_data.rootFid);
@@ -1216,13 +1466,37 @@ int afsd_InitSMB(char **reasonP, void *aMBfunc)
                                 (BYTE *) &dwValue, &dummyLen);
         if (code == ERROR_SUCCESS)
             smb_StoreAnsiFilenames = dwValue ? 1 : 0;
+        afsi_log("StoreAnsiFilenames = %d", smb_StoreAnsiFilenames);
+
+        dummyLen = sizeof(DWORD);
+        code = RegQueryValueEx(parmKey, "EnableSMBAsyncStore", NULL, NULL,
+                                (BYTE *) &dwValue, &dummyLen);
+        if (code == ERROR_SUCCESS)
+            smb_AsyncStore = dwValue == 2 ? 2 : (dwValue ? 1 : 0);
+        afsi_log("EnableSMBAsyncStore = %d", smb_AsyncStore);
+
+        dummyLen = sizeof(DWORD);
+        code = RegQueryValueEx(parmKey, "SMBAsyncStoreSize", NULL, NULL,
+                                (BYTE *) &dwValue, &dummyLen);
+        if (code == ERROR_SUCCESS) {
+            /* Should check for >= blocksize && <= chunksize && round down to multiple of blocksize */
+            if (dwValue > cm_chunkSize)
+                smb_AsyncStoreSize = cm_chunkSize;
+            else if (dwValue <  cm_data.buf_blockSize)
+                smb_AsyncStoreSize = cm_data.buf_blockSize;
+            else
+                smb_AsyncStoreSize = (dwValue & ~(cm_data.buf_blockSize-1));
+        } else 
+            smb_AsyncStoreSize = CM_CONFIGDEFAULT_ASYNCSTORESIZE;
+        afsi_log("SMBAsyncStoreSize = %d", smb_AsyncStoreSize);
+        
         RegCloseKey (parmKey);
     }
 
     /* Do this last so that we don't handle requests before init is done.
      * Here we initialize the SMB listener.
      */
-    smb_Init(afsd_logp, cm_NetbiosName, smb_UseV3, LANadapter, numSvThreads, aMBfunc);
+    smb_Init(afsd_logp, smb_UseV3, numSvThreads, aMBfunc);
     afsi_log("smb_Init complete");
 
     return 0;
@@ -1246,7 +1520,11 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
 {
     HANDLE hProcess = GetCurrentProcess();
     int frameNum;
+#if defined(_AMD64_)
+    DWORD64 offset;
+#elif defined(_X86_)
     DWORD offset;
+#endif
     DWORD symOptions;
     char functionName[MAXNAMELEN];
   
@@ -1285,6 +1563,11 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
 #if defined (_ALPHA_) || defined (_MIPS_) || defined (_PPC_)
 #error The STACKFRAME initialization in afsd_printStack() for this platform
 #error must be properly configured
+#elif defined(_AMD64_)
+    s.AddrPC.Offset = c->Rip;
+    s.AddrPC.Mode = AddrModeFlat;
+    s.AddrFrame.Offset = c->Rbp;
+    s.AddrFrame.Mode = AddrModeFlat;
 #else
     s.AddrPC.Offset = c->Eip;
     s.AddrPC.Mode = AddrModeFlat;
@@ -1390,12 +1673,105 @@ static DWORD *afsd_crtDbgBreakCurrent = NULL;
 static DWORD afsd_crtDbgBreaks[256];
 #endif
 
+static EFaultRepRetVal (WINAPI *pReportFault)(LPEXCEPTION_POINTERS pep, DWORD dwMode) = NULL;
+static BOOL (WINAPI *pMiniDumpWriteDump)(HANDLE hProcess,DWORD ProcessId,HANDLE hFile,
+                                  MINIDUMP_TYPE DumpType,
+                                  PMINIDUMP_EXCEPTION_INFORMATION ExceptionParam,
+                                  PMINIDUMP_USER_STREAM_INFORMATION UserStreamParam,
+                                  PMINIDUMP_CALLBACK_INFORMATION CallbackParam) = NULL;
+
+
+static HANDLE
+OpenDumpFile(void)
+{
+    char wd[256];
+    DWORD code;
+
+    code = GetEnvironmentVariable("TEMP", wd, sizeof(wd));
+    if ( code == 0 || code > sizeof(wd) )
+    {
+        if (!GetWindowsDirectory(wd, sizeof(wd)))
+            return NULL;
+    }
+    StringCbCatA(wd, sizeof(wd), "\\afsd.dmp");
+    return CreateFile( wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
+                            CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
+}
+
+void 
+GenerateMiniDump(PEXCEPTION_POINTERS ep)
+{
+    if (IsDebuggerPresent())
+        return;
+
+    if (ep == NULL) 
+    {
+        // Generate exception to get proper context in dump
+        __try 
+        {
+            RaiseException(DBG_CONTINUE, 0, 0, NULL);
+        } 
+        __except(GenerateMiniDump(GetExceptionInformation()), EXCEPTION_CONTINUE_EXECUTION) 
+        {
+        }
+    } 
+    else
+    {
+        MINIDUMP_EXCEPTION_INFORMATION eInfo;
+        HANDLE hFile = NULL;
+        HMODULE hDbgHelp = NULL;
+
+        hDbgHelp = LoadLibrary("Dbghelp.dll");
+        if ( hDbgHelp == NULL )
+            return;
+
+        (FARPROC) pMiniDumpWriteDump = GetProcAddress(hDbgHelp, "MiniDumpWriteDump");
+        if ( pMiniDumpWriteDump == NULL ) {
+            FreeLibrary(hDbgHelp);
+            return;
+        }
+
+        hFile = OpenDumpFile();
+
+        if ( hFile ) {
+            HKEY parmKey;
+            DWORD dummyLen;
+            DWORD dwValue;
+            DWORD code;
+            DWORD dwMiniDumpType = MiniDumpWithDataSegs;
+
+            code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_OPENAFS_SUBKEY,
+                                 0, KEY_QUERY_VALUE, &parmKey);
+            if (code == ERROR_SUCCESS) {
+                dummyLen = sizeof(DWORD);
+                code = RegQueryValueEx(parmKey, "MiniDumpType", NULL, NULL,
+                                        (BYTE *) &dwValue, &dummyLen);
+                if (code == ERROR_SUCCESS)
+                    dwMiniDumpType = dwValue;
+                RegCloseKey (parmKey);
+            }
+
+            eInfo.ThreadId = GetCurrentThreadId();
+            eInfo.ExceptionPointers = ep;
+            eInfo.ClientPointers = FALSE;
+
+            pMiniDumpWriteDump( GetCurrentProcess(), GetCurrentProcessId(),
+                                hFile, dwMiniDumpType, ep ? &eInfo : NULL,
+                                NULL, NULL);
+
+            CloseHandle(hFile);
+        }
+        FreeLibrary(hDbgHelp);
+    }
+}
+
 LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
 {
     CONTEXT context;
 #ifdef _DEBUG  
     BOOL allocRequestBrk = FALSE;
 #endif 
+    HMODULE hLib = NULL;
   
     afsi_log("UnhandledException : code : 0x%x, address: 0x%x\n", 
              ep->ExceptionRecord->ExceptionCode, 
@@ -1414,7 +1790,17 @@ LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
     context = *ep->ContextRecord;
           
     afsd_printStack(GetCurrentThread(), &context);
-          
+
+    GenerateMiniDump(ep);
+
+    hLib = LoadLibrary("Faultrep.dll");
+    if ( hLib ) {
+        (FARPROC) pReportFault = GetProcAddress(hLib, "ReportFault");
+        if ( pReportFault )
+            pReportFault(ep, 0);
+        FreeLibrary(hLib);
+    }
+
     if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
     {
         afsi_log("\nEXCEPTION_BREAKPOINT - continue execution ...\n");
@@ -1426,8 +1812,12 @@ LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
             _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent);
         }
 #endif         
-    
+#if defined(_X86)    
         ep->ContextRecord->Eip++;
+#endif
+#if defined(_AMD64_)
+        ep->ContextRecord->Rip++;
+#endif
         return EXCEPTION_CONTINUE_EXECUTION;
     }
     else
@@ -1438,9 +1828,11 @@ LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
   
 void afsd_SetUnhandledExceptionFilter()
 {
+#ifndef NOTRACE
     SetUnhandledExceptionFilter(afsd_ExceptionFilter);
+#endif
 }
-  
+
 #ifdef _DEBUG
 void afsd_DbgBreakAllocInit()
 {