Windows: disable short names on Win7 and 2008 R2
[openafs.git] / src / WINNT / afsd / afsd_init.c
index d6d1d46..4d523b6 100644 (file)
@@ -1,13 +1,17 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
  */
 
+#include <afsconfig.h>
 #include <afs/param.h>
+
+#include <roken.h>
+
 #include <afs/stds.h>
 #include <afs/afs_args.h>
 
@@ -68,10 +72,15 @@ DWORD cm_mountRootLen;
 clientchar_t cm_mountRootC[1024];
 DWORD cm_mountRootCLen;
 
+int cm_readonlyVolumeVersioning = 0;
 int cm_logChunkSize;
 int cm_chunkSize;
+int cm_virtualCache = 0;
+afs_int32 cm_verifyData = 0;
+int cm_shortNames = 1;
 
 int smb_UseV3 = 1;
+afs_uint32 smb_Enabled = 1;
 
 int LANadapter;
 
@@ -96,9 +105,10 @@ BOOL reportSessionStartups = FALSE;
 
 cm_initparams_v1 cm_initParams;
 
-clientchar_t *cm_sysName = 0;
 unsigned int  cm_sysNameCount = 0;
 clientchar_t *cm_sysNameList[MAXNUMSYSNAMES];
+unsigned int  cm_sysName64Count = 0;
+clientchar_t *cm_sysName64List[MAXNUMSYSNAMES];
 
 DWORD TraceOption = 0;
 
@@ -112,9 +122,7 @@ DWORD TraceOption = 0;
 
 HANDLE afsi_file;
 
-#ifdef AFS_AFSDB_ENV
 int cm_dnsEnabled = 1;
-#endif
 
 
 static int afsi_log_useTimestamp = 1;
@@ -136,7 +144,7 @@ afsi_log(char *pattern, ...)
             WriteFile(afsi_file, u, (DWORD)strlen(u), &zilch, NULL);
 #ifdef NOTSERVICE
         printf("%s", u);
-#endif 
+#endif
     } else {
         if (afsi_file != INVALID_HANDLE_VALUE)
             WriteFile(afsi_file, s, (DWORD)strlen(s), &zilch, NULL);
@@ -203,10 +211,10 @@ afsi_start()
     afsi_log("OEM Code Page = %d", GetOEMCP());
     afsi_log("locale =  %s", setlocale(LC_ALL,NULL));
 #ifdef COMMENT
-    /* Two things to look into.  First, should mbstowcs() be performing 
-     * character set translations from OEM to Unicode in smb3.c; 
-     * Second, do we need to set this translation in each function 
-     * due to multi-threading. 
+    /* Two things to look into.  First, should mbstowcs() be performing
+     * character set translations from OEM to Unicode in smb3.c;
+     * Second, do we need to set this translation in each function
+     * due to multi-threading.
      */
     afsi_log("locale -> %s", setlocale(LC_ALL, ".OCP"));
     afsi_log("_setmbcp = %d -> %d", _setmbcp(_MB_CP_OEM), _getmbcp());
@@ -223,7 +231,7 @@ void afsd_ForceTrace(BOOL flush)
     int len;
     char buf[256];
 
-    if (!logReady) 
+    if (!logReady)
         return;
 
     len = GetTempPath(sizeof(buf)-10, buf);
@@ -252,7 +260,7 @@ static void afsd_InitServerPreferences(void)
     struct sockaddr_in saddr;
     cm_server_t       *tsp;
 
-    if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
+    if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                       AFSREG_CLT_OPENAFS_SUBKEY "\\Server Preferences\\VLDB",
                       0,
                       KEY_READ|KEY_QUERY_VALUE,
@@ -297,15 +305,17 @@ static void afsd_InitServerPreferences(void)
 
                 saddr.sin_addr.S_un.S_addr = *(unsigned long *)pEntry->h_addr;
             }
+            saddr.sin_port = htons(7003);
             saddr.sin_family = AF_INET;
             dwRank += (rand() & 0x000f);
 
-            tsp = cm_FindServer(&saddr, CM_SERVER_VLDB);
+            tsp = cm_FindServer(&saddr, CM_SERVER_VLDB, FALSE);
             if ( tsp )         /* an existing server - ref count increased */
             {
                 lock_ObtainMutex(&tsp->mx);
-                tsp->ipRank = (USHORT)dwRank;
-                tsp->flags |= CM_SERVERFLAG_PREF_SET;
+                tsp->adminRank = (USHORT)dwRank;
+                _InterlockedOr(&tsp->flags, CM_SERVERFLAG_PREF_SET);
+                cm_RankServer(tsp);
                 lock_ReleaseMutex(&tsp->mx);
 
                 /* set preferences for an existing vlserver */
@@ -316,8 +326,9 @@ static void afsd_InitServerPreferences(void)
             {
                 tsp = cm_NewServer(&saddr, CM_SERVER_VLDB, NULL, NULL, CM_FLAG_NOPROBE); /* refcount = 1 */
                 lock_ObtainMutex(&tsp->mx);
-                tsp->ipRank = (USHORT)dwRank;
-                tsp->flags |= CM_SERVERFLAG_PREF_SET;
+                tsp->adminRank = (USHORT)dwRank;
+                _InterlockedOr(&tsp->flags, CM_SERVERFLAG_PREF_SET);
+                cm_RankServer(tsp);
                 lock_ReleaseMutex(&tsp->mx);
             }
         }
@@ -325,7 +336,7 @@ static void afsd_InitServerPreferences(void)
         RegCloseKey(hkPrefs);
     }
 
-    if (RegOpenKeyEx( HKEY_LOCAL_MACHINE, 
+    if (RegOpenKeyEx( HKEY_LOCAL_MACHINE,
                       AFSREG_CLT_OPENAFS_SUBKEY "\\Server Preferences\\File",
                       0,
                       KEY_READ|KEY_QUERY_VALUE,
@@ -370,20 +381,22 @@ static void afsd_InitServerPreferences(void)
 
                 saddr.sin_addr.S_un.S_addr = *(unsigned long *)pEntry->h_addr;
             }
+            saddr.sin_port = htons(7000);
             saddr.sin_family = AF_INET;
             dwRank += (rand() & 0x000f);
 
-            tsp = cm_FindServer(&saddr, CM_SERVER_FILE);
+            tsp = cm_FindServer(&saddr, CM_SERVER_FILE, FALSE);
             if ( tsp )         /* an existing server - ref count increased */
             {
                 lock_ObtainMutex(&tsp->mx);
-                tsp->ipRank = (USHORT)dwRank;
-                tsp->flags |= CM_SERVERFLAG_PREF_SET;
+                tsp->adminRank = (USHORT)dwRank;
+               _InterlockedOr(&tsp->flags, CM_SERVERFLAG_PREF_SET);
+                cm_RankServer(tsp);
                 lock_ReleaseMutex(&tsp->mx);
 
-                /* find volumes which might have RO copy 
-                /* on server and change the ordering of 
-                 * their RO list 
+                /* find volumes which might have RO copy
+                /* on server and change the ordering of
+                 * their RO list
                  */
                 cm_ChangeRankVolume(tsp);
                 cm_PutServer(tsp);  /* decrease refcount */
@@ -392,8 +405,9 @@ static void afsd_InitServerPreferences(void)
             {
                 tsp = cm_NewServer(&saddr, CM_SERVER_FILE, NULL, NULL, CM_FLAG_NOPROBE); /* refcount = 1 */
                 lock_ObtainMutex(&tsp->mx);
-                tsp->ipRank = (USHORT)dwRank;
-                tsp->flags |= CM_SERVERFLAG_PREF_SET;
+                tsp->adminRank = (USHORT)dwRank;
+                _InterlockedOr(&tsp->flags, CM_SERVERFLAG_PREF_SET);
+                cm_RankServer(tsp);
                 lock_ReleaseMutex(&tsp->mx);
             }
         }
@@ -438,7 +452,48 @@ is_wow64(void)
  * AFSD Initialization
  */
 
-int afsd_InitCM(char **reasonP)
+static int
+afsd_InitRoot(char **reasonP)
+{
+    long code;
+    cm_req_t req;
+
+    cm_InitReq(&req);
+
+    if (cm_freelanceEnabled) {
+        cm_FakeRootFid(&cm_data.rootFid);
+    } else {
+       int attempts = 10;
+
+        osi_Log0(afsd_logp, "Loading Root Volume from cell");
+       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;
+        }
+
+        /* compute the root fid */
+        cm_SetFid(&cm_data.rootFid, cm_data.rootCellp->cellID, cm_GetROVolumeID(cm_data.rootVolumep), 1, 1);
+    }
+
+    code = cm_GetSCache(&cm_data.rootFid, NULL, &cm_data.rootSCachep, cm_rootUserp, &req);
+    afsi_log("cm_GetSCache code %x scache %x", code,
+             (code ? (cm_scache_t *)-1 : cm_data.rootSCachep));
+    if (code != 0) {
+        *reasonP = "unknown error";
+        return -1;
+    }
+
+    return 0;
+}
+
+int
+afsd_InitCM(char **reasonP)
 {
     osi_uid_t debugID;
     afs_uint64 cacheBlocks;
@@ -457,7 +512,10 @@ int afsd_InitCM(char **reasonP)
     long maxcpus;
     long ltt, ltto;
     long rx_nojumbo;
-    long virtualCache = 0;
+    int  rx_max_rwin_size;
+    int  rx_max_swin_size;
+    int  rx_min_peer_timeout;
+    DWORD virtualCache = 0;
     fschar_t rootCellName[256];
     struct rx_service *serverp;
     static struct rx_securityClass *nullServerSecurityClassp;
@@ -477,11 +535,19 @@ int afsd_InitCM(char **reasonP)
     int cm_NetMtu[CM_MAXINTERFACE_ADDR];    /* client's MTU sizes */
     int cm_NetFlags[CM_MAXINTERFACE_ADDR];  /* network flags */
     DWORD dwPriority;
+    OSVERSIONINFO osVersion;
+
+    /* Get the version of Windows */
+    memset(&osVersion, 0x00, sizeof(osVersion));
+    osVersion.dwOSVersionInfoSize = sizeof(osVersion);
+    GetVersionEx(&osVersion);
 
     WSAStartup(0x0101, &WSAjunk);
 
     init_et_to_sys_error();
 
+    cm_utilsInit();
+
     /* setup osidebug server at RPC slot 1000 */
     osi_LongToUID(1000, &debugID);
     code = osi_InitDebug(&debugID);
@@ -552,7 +618,11 @@ int afsd_InitCM(char **reasonP)
     dummyLen = sizeof(maxcpus);
     code = RegQueryValueEx(parmKey, "MaxCPUs", NULL, NULL,
                             (BYTE *) &maxcpus, &dummyLen);
-    if (code == ERROR_SUCCESS) {
+    if (code != ERROR_SUCCESS) {
+        maxcpus = 2;
+    }
+
+    {
         HANDLE hProcess;
         DWORD_PTR processAffinityMask, systemAffinityMask;
 
@@ -561,7 +631,7 @@ int afsd_InitCM(char **reasonP)
         if ( hProcess != NULL &&
              GetProcessAffinityMask(hProcess, &processAffinityMask, &systemAffinityMask) )
         {
-            int i, n, bits;
+            int i, n, bits, cpu_count = 0;
             DWORD_PTR mask, newAffinityMask;
 
 #if defined(_WIN64)
@@ -569,19 +639,26 @@ int afsd_InitCM(char **reasonP)
 #else
             bits = 32;
 #endif
-            for ( i=0, n=0, mask=1, newAffinityMask=0; i<bits && n<maxcpus; i++ ) {
+            for ( i=0, n=0, mask=1, newAffinityMask=0; i<bits; i++ ) {
                 if ( processAffinityMask & mask ) {
-                    newAffinityMask |= mask;
-                    n++;
+                    cpu_count++;
+                    if (n<maxcpus) {
+                        newAffinityMask |= mask;
+                        n++;
+                    }
                 }
                 mask *= 2;
             }
 
-            SetProcessAffinityMask(hProcess, newAffinityMask);
+            if (maxcpus == 0) {
+                afsi_log("No CPU Restrictions; %d cpu(s) available", cpu_count);
+            } else {
+                SetProcessAffinityMask(hProcess, newAffinityMask);
+            }
             CloseHandle(hProcess);
-            afsi_log("CPU Restrictions set to %d cpu(s); %d cpu(s) available", maxcpus, n);
+            afsi_log("CPU Restrictions set to %d cpu(s); %d cpu(s) available", maxcpus, cpu_count);
         } else {
-            afsi_log("CPU Restrictions set to %d cpu(s); unable to access process information", maxcpus);
+            afsi_log("CPU Restrictions requested %d cpu(s); unable to access process information", maxcpus);
         }
     }
 
@@ -614,15 +691,61 @@ int afsd_InitCM(char **reasonP)
 
     osi_Log0(afsd_logp, "Log init");
 
+    dummyLen = sizeof(smb_monitorReqs);
+    code = RegQueryValueEx(parmKey, "SMBRequestMonitor", NULL, NULL,
+                           (BYTE *) &smb_monitorReqs, &dummyLen);
+    afsi_log("SMB request monitoring is %s", (smb_monitorReqs != 0)? "enabled": "disabled");
+
+    dummyLen = sizeof(virtualCache);
+    code = RegQueryValueEx(parmKey, "NonPersistentCaching", NULL, NULL,
+                            (LPBYTE)&virtualCache, &dummyLen);
+    if (!code)
+        cm_virtualCache = virtualCache ? 1 : 0;
+    afsi_log("Cache type is %s", (cm_virtualCache?"VIRTUAL":"FILE"));
+
+    if (!cm_virtualCache) {
+        dummyLen = sizeof(cm_ValidateCache);
+        code = RegQueryValueEx(parmKey, "ValidateCache", NULL, NULL,
+                               (LPBYTE)&cm_ValidateCache, &dummyLen);
+        if ( cm_ValidateCache < 0 || cm_ValidateCache > 2 )
+            cm_ValidateCache = 1;
+        switch (cm_ValidateCache) {
+        case 0:
+            afsi_log("Cache Validation disabled");
+            break;
+        case 1:
+            afsi_log("Cache Validation on Startup");
+            break;
+        case 2:
+            afsi_log("Cache Validation on Startup and Shutdown");
+            break;
+        }
+    }
+
     dummyLen = sizeof(cacheSize);
     code = RegQueryValueEx(parmKey, "CacheSize", NULL, NULL,
                             (BYTE *) &cacheSize, &dummyLen);
-    if (code == ERROR_SUCCESS)
-        afsi_log("Cache size %d", cacheSize);
-    else {
+    if (code != ERROR_SUCCESS)
         cacheSize = CM_CONFIGDEFAULT_CACHESIZE;
-        afsi_log("Default cache size %d", cacheSize);
+
+    if (cm_virtualCache) {
+        MEMORYSTATUSEX memStatus;
+        DWORD maxCacheSize;
+
+        memStatus.dwLength = sizeof(memStatus);
+        if (GlobalMemoryStatusEx(&memStatus)) {
+            /* Set maxCacheSize to 10% of physical memory */
+            maxCacheSize = (DWORD)(memStatus.ullTotalPhys / 1024 / 10);
+        } else {
+            /* Cannot determine physical memory, set limit to 64MB */
+            maxCacheSize = 65536;
+        }
+        if (cacheSize > maxCacheSize) {
+            afsi_log("Requested Cache size %u", cacheSize);
+            cacheSize = maxCacheSize;
+        }
     }
+    afsi_log("Allocated Cache size %u", cacheSize);
 
     dummyLen = sizeof(logChunkSize);
     code = RegQueryValueEx(parmKey, "ChunkSize", NULL, NULL,
@@ -644,13 +767,13 @@ int afsd_InitCM(char **reasonP)
     code = RegQueryValueEx(parmKey, "blockSize", NULL, NULL,
                             (BYTE *) &blockSize, &dummyLen);
     if (code == ERROR_SUCCESS) {
-        if (blockSize < 1 || 
-            (blockSize > 1024 && (blockSize % CM_CONFIGDEFAULT_BLOCKSIZE != 0))) 
+        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
              */
@@ -661,7 +784,7 @@ int afsd_InitCM(char **reasonP)
         blockSize = CM_CONFIGDEFAULT_BLOCKSIZE;
     }
     if (blockSize > cm_chunkSize) {
-        afsi_log("Block size (%d) cannot be larger than Chunk size (%d).", 
+        afsi_log("Block size (%d) cannot be larger than Chunk size (%d).",
                   blockSize, cm_chunkSize);
         blockSize = cm_chunkSize;
     }
@@ -740,10 +863,21 @@ int afsd_InitCM(char **reasonP)
             ltto = 120;
     } else {
         ltto = 0;
-    }   
+    }
     smb_LogoffTransferTimeout = ltto;
     afsi_log("Logoff token transfer timeout %d seconds", ltto);
 
+    dummyLen = sizeof(cm_NetbiosName);
+    code = RegQueryValueEx(parmKey, "NetbiosName", NULL, NULL,
+                            (LPBYTE) cm_NetbiosName, &dummyLen);
+    if (code == ERROR_SUCCESS)
+        afsi_log("NetbiosName %s", cm_NetbiosName);
+    else {
+        cm_FsStrCpy(cm_NetbiosName, lengthof(cm_NetbiosName), "AFS");
+        afsi_log("Default NetbiosName AFS");
+    }
+    cm_Utf8ToClientString(cm_NetbiosName, -1, cm_NetbiosNameC, MAX_NB_NAME_LENGTH);
+
     dummyLen = sizeof(cm_rootVolumeName);
     code = RegQueryValueEx(parmKey, "RootVolume", NULL, NULL,
                             (LPBYTE) cm_rootVolumeName, &dummyLen);
@@ -786,37 +920,13 @@ int afsd_InitCM(char **reasonP)
     } else {
         dummyLen = ExpandEnvironmentStrings("%TEMP%\\AFSCache", cm_CachePath, sizeof(cm_CachePath));
         if (dummyLen > sizeof(cm_CachePath)) {
-            afsi_log("Cache path [%%TEMP%%\\AFSCache] longer than %d after expanding env strings", 
+            afsi_log("Cache path [%%TEMP%%\\AFSCache] longer than %d after expanding env strings",
                      sizeof(cm_CachePath));
             osi_panic("CachePath too long", __FILE__, __LINE__);
         }
         afsi_log("Default cache path %s", cm_CachePath);
     }
 
-    dummyLen = sizeof(virtualCache);
-    code = RegQueryValueEx(parmKey, "NonPersistentCaching", NULL, NULL,
-                            (LPBYTE)&virtualCache, &dummyLen);
-    afsi_log("Cache type is %s", (virtualCache?"VIRTUAL":"FILE"));
-
-    if (!virtualCache) {
-        dummyLen = sizeof(cm_ValidateCache);
-        code = RegQueryValueEx(parmKey, "ValidateCache", NULL, NULL,
-                               (LPBYTE)&cm_ValidateCache, &dummyLen);
-        if ( cm_ValidateCache < 0 || cm_ValidateCache > 2 )
-            cm_ValidateCache = 1;
-        switch (cm_ValidateCache) {
-        case 0:
-            afsi_log("Cache Validation disabled");
-            break;
-        case 1:
-            afsi_log("Cache Validation on Startup");
-            break;
-        case 2:
-            afsi_log("Cache Validation on Startup and Shutdown");
-            break;
-        }
-    }
-
     dummyLen = sizeof(traceOnPanic);
     code = RegQueryValueEx(parmKey, "TrapOnPanic", NULL, NULL,
                             (BYTE *) &traceOnPanic, &dummyLen);
@@ -838,12 +948,15 @@ int afsd_InitCM(char **reasonP)
     for ( i=0; i < MAXNUMSYSNAMES; i++ ) {
         cm_sysNameList[i] = osi_Alloc(MAXSYSNAME * sizeof(clientchar_t));
         cm_sysNameList[i][0] = '\0';
+        cm_sysName64List[i] = osi_Alloc(MAXSYSNAME * sizeof(clientchar_t));
+        cm_sysName64List[i][0] = '\0';
     }
-    cm_sysName = cm_sysNameList[0];
 
+    /* Process SysName lists from the registry */
     {
         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]) {
@@ -855,7 +968,7 @@ int afsd_InitCM(char **reasonP)
             cm_ClientStrCpy(cbuf, lengthof(buf), _C("x86_win32 i386_w2k i386_nt40"));
 #endif
         }
-        afsi_log("Sys name %S", cbuf); 
+        afsi_log("Sys name list: %S", cbuf);
 
         /* breakup buf into individual search string entries */
         for (p = q = cbuf; p < cbuf + dummyLen; p++) {
@@ -865,16 +978,49 @@ int afsd_InitCM(char **reasonP)
                 cm_sysNameCount++;
                 do {
                     if (*p == '\0')
-                        goto done_sysname;
+                        goto done_sysname32;
                     p++;
                 } while (*p == '\0' || isspace(*p));
                 q = p;
                 p--;
             }
         }
+      done_sysname32:
+        ;
+
+#ifdef _WIN64
+        /*
+         * If there is a 64-bit list, process it.  Otherwise, we will leave
+         * it undefined which implies that the 32-bit list be used for both.
+         * The 64-bit list is only used for the native file system driver.
+         * The SMB redirector interface does not provide any means of indicating
+         * the source of the request.
+         */
+        dummyLen = sizeof(buf);
+        code = RegQueryValueExW(parmKey, L"SysName64", NULL, NULL, (LPBYTE) cbuf, &dummyLen);
+        if (code == ERROR_SUCCESS && cbuf[0]) {
+            afsi_log("Sys name 64 list: %S", cbuf);
+
+            /* breakup buf into individual search string entries */
+            for (p = q = cbuf; p < cbuf + dummyLen; p++) {
+                if (*p == '\0' || iswspace(*p)) {
+                    memcpy(cm_sysName64List[cm_sysName64Count],q,(p-q) * sizeof(clientchar_t));
+                    cm_sysName64List[cm_sysName64Count][p-q] = '\0';
+                    cm_sysName64Count++;
+                    do {
+                        if (*p == '\0')
+                            goto done_sysname64;
+                        p++;
+                    } while (*p == '\0' || isspace(*p));
+                    q = p;
+                    p--;
+                }
+            }
+        }
+      done_sysname64:
+        ;
+#endif
     }
-  done_sysname:
-    cm_ClientStrCpy(cm_sysName, MAXSYSNAME, cm_sysNameList[0]);
 
     dummyLen = sizeof(cryptall);
     code = RegQueryValueEx(parmKey, "SecurityLevel", NULL, NULL,
@@ -893,38 +1039,47 @@ int afsd_InitCM(char **reasonP)
     else
        LogEvent(EVENTLOG_INFORMATION_TYPE, MSG_CRYPT_OFF);
 
-    dummyLen = sizeof(cryptall);
+    dummyLen = sizeof(cm_verifyData);
+    code = RegQueryValueEx(parmKey, "VerifyData", NULL, NULL,
+                           (BYTE *) &cm_verifyData, &dummyLen);
+    afsi_log("VerifyData is %s", cm_verifyData?"on":"off");
+
+    dummyLen = sizeof(cm_anonvldb);
     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,
                             (BYTE *) &cm_dnsEnabled, &dummyLen);
     if (code == ERROR_SUCCESS) {
         afsi_log("DNS %s be used to find AFS cell servers",
                   cm_dnsEnabled ? "will" : "will not");
-    }       
+    }
     else {
         cm_dnsEnabled = 1;   /* default on */
         afsi_log("Default to use DNS to find AFS cell servers");
     }
-#else /* AFS_AFSDB_ENV */
-    afsi_log("AFS not built with DNS support to find AFS cell servers");
-#endif /* AFS_AFSDB_ENV */
 
 #ifdef AFS_FREELANCE_CLIENT
     dummyLen = sizeof(cm_freelanceEnabled);
     code = RegQueryValueEx(parmKey, "FreelanceClient", NULL, NULL,
                             (BYTE *) &cm_freelanceEnabled, &dummyLen);
-    if (code == ERROR_SUCCESS) {
-        afsi_log("Freelance client feature %s activated",
-                  cm_freelanceEnabled ? "is" : "is not");
-    }       
-    else {
-        cm_freelanceEnabled = 1;  /* default on */
-    }
+    afsi_log("Freelance client feature %s activated",
+              cm_freelanceEnabled ? "is" : "is not");
+
+    dummyLen = sizeof(cm_freelanceImportCellServDB);
+    code = RegQueryValueEx(parmKey, "FreelanceImportCellServDB", NULL, NULL,
+                            (BYTE *) &cm_freelanceImportCellServDB, &dummyLen);
+    afsi_log("Freelance client %s import CellServDB",
+              cm_freelanceImportCellServDB ? "does" : "does not");
+
+    dummyLen = sizeof(cm_freelanceDiscovery);
+    code = RegQueryValueEx(parmKey, "FreelanceDiscovery", NULL, NULL,
+                            (BYTE *) &cm_freelanceDiscovery, &dummyLen);
+    afsi_log("Freelance client discovery is %s",
+              cm_freelanceDiscovery ? "on" : "off");
+
 #endif /* AFS_FREELANCE_CLIENT */
 
     dummyLen = sizeof(smb_UseUnicode);
@@ -945,6 +1100,22 @@ int afsd_InitCM(char **reasonP)
     afsi_log("Dot files/dirs will %sbe marked hidden",
               smb_hideDotFiles ? "" : "not ");
 
+    dummyLen = sizeof(dwValue);
+    code = RegQueryValueEx(parmKey, "UnixModeFileDefault", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        smb_unixModeDefaultFile = (dwValue & 07777);
+    }
+    afsi_log("Default unix mode bits for files is 0%04o", smb_unixModeDefaultFile);
+
+    dummyLen = sizeof(dwValue);
+    code = RegQueryValueEx(parmKey, "UnixModeDirDefault", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        smb_unixModeDefaultDir = (dwValue & 07777);
+    }
+    afsi_log("Default unix mode bits for directories is 0%04o", smb_unixModeDefaultDir);
+
     dummyLen = sizeof(smb_maxMpxRequests);
     code = RegQueryValueEx(parmKey, "MaxMpxRequests", NULL, NULL,
                            (BYTE *) &smb_maxMpxRequests, &dummyLen);
@@ -965,12 +1136,38 @@ int afsd_InitCM(char **reasonP)
     code = RegQueryValueEx(parmKey, "SMBAuthType", NULL, NULL,
                             (BYTE *) &smb_authType, &dummyLen);
 
-    if (code != ERROR_SUCCESS || 
+    if (code != ERROR_SUCCESS ||
          (smb_authType != SMB_AUTH_EXTENDED && smb_authType != SMB_AUTH_NTLM && smb_authType != SMB_AUTH_NONE)) {
         smb_authType = SMB_AUTH_EXTENDED; /* default is to use extended authentication */
     }
     afsi_log("SMB authentication type is %s", ((smb_authType == SMB_AUTH_NONE)?"NONE":((smb_authType == SMB_AUTH_EXTENDED)?"EXTENDED":"NTLM")));
 
+    dummyLen = sizeof(rx_max_rwin_size);
+    code = RegQueryValueEx(parmKey, "RxMaxRecvWinSize", NULL, NULL,
+                           (BYTE *) &rx_max_rwin_size, &dummyLen);
+    if (code == ERROR_SUCCESS)
+        rx_SetMaxReceiveWindow(rx_max_rwin_size);
+    afsi_log("Rx Maximum Receive Window Size is %d", rx_GetMaxReceiveWindow());
+
+    dummyLen = sizeof(rx_max_swin_size);
+    code = RegQueryValueEx(parmKey, "RxMaxSendWinSize", NULL, NULL,
+                           (BYTE *) &rx_max_swin_size, &dummyLen);
+    if (code == ERROR_SUCCESS)
+        rx_SetMaxSendWindow(rx_max_swin_size);
+    afsi_log("Rx Maximum Send Window Size is %d", rx_GetMaxSendWindow());
+
+    dummyLen = sizeof(rx_min_peer_timeout);
+    code = RegQueryValueEx(parmKey, "RxMinPeerTimeout", NULL, NULL,
+                           (BYTE *) &rx_min_peer_timeout, &dummyLen);
+    if (code == ERROR_SUCCESS)
+        rx_SetMinPeerTimeout(rx_min_peer_timeout);
+    afsi_log("Rx Minimum Peer Timeout is %d ms", rx_GetMinPeerTimeout());
+
+    dummyLen = sizeof(rx_pmtu_discovery);
+    code = RegQueryValueEx(parmKey, "RxPMTUDiscovery", NULL, NULL,
+                           (BYTE *) &rx_pmtu_discovery, &dummyLen);
+    afsi_log("Rx PMTU Discovery is %d ms", rx_pmtu_discovery);
+
     dummyLen = sizeof(rx_nojumbo);
     code = RegQueryValueEx(parmKey, "RxNoJumbo", NULL, NULL,
                            (BYTE *) &rx_nojumbo, &dummyLen);
@@ -1063,7 +1260,7 @@ int afsd_InitCM(char **reasonP)
                            (BYTE *) &dwValue, &dummyLen);
     if (code == ERROR_SUCCESS) {
         cm_enableServerLocks = (unsigned short) dwValue;
-    } 
+    }
     switch (cm_enableServerLocks) {
     case 0:
        afsi_log("EnableServerLocks: never");
@@ -1082,16 +1279,16 @@ int afsd_InitCM(char **reasonP)
                            (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);
 
     if (cm_BPlusTrees && !cm_InitBPlusDir()) {
@@ -1102,14 +1299,14 @@ int afsd_InitCM(char **reasonP)
     afsi_log("CM BPlusTrees is not supported");
 #endif
 
-    if ((RegQueryValueExW( parmKey, L"PrefetchExecutableExtensions", 0, 
+    if ((RegQueryValueExW( parmKey, L"PrefetchExecutableExtensions", 0,
                            &regType, NULL, &dummyLen) == ERROR_SUCCESS) &&
-         (regType == REG_MULTI_SZ)) 
+         (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, 
+        if ((RegQueryValueExW( parmKey, L"PrefetchExecutableExtensions", 0, &regType,
                                (LPBYTE) pSz, &dummyLen) == ERROR_SUCCESS) &&
              (regType == REG_MULTI_SZ))
         {
@@ -1126,7 +1323,7 @@ int afsd_InitCM(char **reasonP)
             }
             smb_ExecutableExtensions[cnt] = NULL;
         }
-        
+
         if (!smb_ExecutableExtensions)
             free(pSz);
     }
@@ -1138,15 +1335,15 @@ int afsd_InitCM(char **reasonP)
                            (BYTE *) &dwValue, &dummyLen);
     if (code == ERROR_SUCCESS) {
         cm_OfflineROIsValid = (unsigned short) dwValue;
-    } 
-    afsi_log("CM OfflineReadOnlyIsValid is %u", cm_deleteReadOnly);
-    
+    }
+    afsi_log("CM OfflineReadOnlyIsValid is %u", cm_OfflineROIsValid);
+
     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);
@@ -1154,7 +1351,7 @@ int afsd_InitCM(char **reasonP)
                            (BYTE *) &dwValue, &dummyLen);
     if (code == ERROR_SUCCESS) {
         cm_followBackupPath = (unsigned short) dwValue;
-    } 
+    }
     afsi_log("CM FollowBackupPath is %u", cm_followBackupPath);
 
     dummyLen = sizeof(DWORD);
@@ -1162,13 +1359,37 @@ int afsd_InitCM(char **reasonP)
                            (BYTE *) &dwValue, &dummyLen);
     if (code == ERROR_SUCCESS) {
         cm_accessPerFileCheck = (int) dwValue;
-    } 
+    }
     afsi_log("CM PerFileAccessCheck is %d", cm_accessPerFileCheck);
 
+    dummyLen = sizeof(DWORD);
+    code = RegQueryValueEx(parmKey, "ReadOnlyVolumeVersioning", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        cm_readonlyVolumeVersioning = (unsigned short) dwValue;
+    }
+    afsi_log("CM ReadOnlyVolumeVersioning is %u", cm_readonlyVolumeVersioning);
+
+    dummyLen = sizeof(DWORD);
+    code = RegQueryValueEx(parmKey, "ShortNames", NULL, NULL,
+                           (BYTE *) &dwValue, &dummyLen);
+    if (code == ERROR_SUCCESS) {
+        cm_shortNames = (unsigned short) dwValue;
+    } else {
+        /* disable by default on Win7, Win 8, Server 2008 R2 and Server 2012 */
+        if (osVersion.dwMajorVersion > 6 ||
+            osVersion.dwMajorVersion == 6 &&
+            osVersion.dwMinorVersion >= 1)
+            cm_shortNames = 0;
+        else
+            cm_shortNames = 1;
+    }
+    afsi_log("CM ShortNames is %u", cm_shortNames);
+
     RegCloseKey (parmKey);
 
     cacheBlocks = ((afs_uint64)cacheSize * 1024) / blockSize;
-        
+
     /* get network related info */
     cm_noIPAddr = CM_MAXINTERFACE_ADDR;
     code = syscfg_GetIFInfo(&cm_noIPAddr,
@@ -1182,7 +1403,7 @@ int afsd_InitCM(char **reasonP)
                   cm_IPAddr[0], cm_SubnetMask[0]);
 
     /*
-     * Save client configuration for GetCacheConfig requests
+     * Save client configuration for GetCacheConf\eig requests
      */
     cm_initParams.nChunkFiles = 0;
     cm_initParams.nStatCaches = stats;
@@ -1200,29 +1421,30 @@ int afsd_InitCM(char **reasonP)
     cm_InitConn();
 
     cm_InitServer();
-        
+
     cm_InitIoctl();
-        
+
     smb_InitIoctl();
-        
+
     cm_InitCallback();
 
     cm_InitNormalization();
 
-    code = cm_InitMappedMemory(virtualCache, cm_CachePath, stats, volumes, cells, cm_chunkSize, cacheBlocks, blockSize);
+    code = cm_InitMappedMemory(cm_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";
         return -1;
     }
 
-#ifdef AFS_AFSDB_ENV
+    /* Must be called after cm_InitMappedMemory. */
+    cm_EAccesInitCache();
+
 #if !defined(_WIN32_WINNT) || (_WIN32_WINNT < 0x0500)
     if (cm_InitDNS(cm_dnsEnabled) == -1)
         cm_dnsEnabled = 0;  /* init failed, so deactivate */
     afsi_log("cm_InitDNS %d", cm_dnsEnabled);
 #endif
-#endif
 
     /* Set RX parameters before initializing RX */
     if ( rx_nojumbo ) {
@@ -1231,8 +1453,6 @@ int afsd_InitCM(char **reasonP)
     }
 
     if ( rx_mtu != -1 ) {
-        extern void rx_SetMaxMTU(int);
-
         rx_SetMaxMTU(rx_mtu);
         afsi_log("rx_SetMaxMTU %d successful", rx_mtu);
     }
@@ -1242,7 +1462,9 @@ int afsd_InitCM(char **reasonP)
         afsi_log("rx_SetUdpBufSize %d", rx_udpbufsize);
     }
 
-    /* initialize RX, and tell it to listen to the callbackport, 
+    rx_SetBusyChannelError(1);  /* Activate busy call channel reporting */
+
+    /* initialize RX, and tell it to listen to the callbackport,
      * which is used for callback RPC messages.
      */
     code = rx_Init(htons(cm_callbackport));
@@ -1279,7 +1501,7 @@ int afsd_InitCM(char **reasonP)
     }
     rx_SetMinProcs(serverp, 2);
     rx_SetMaxProcs(serverp, 4);
-        
+
     /* start server threads, *not* donating this one to the pool */
     rx_StartServer(0);
     afsi_log("rx_StartServer");
@@ -1291,21 +1513,21 @@ int afsd_InitCM(char **reasonP)
        rx_enableProcessRPCStats();
 
     code = cm_GetRootCellName(rootCellName);
-    afsi_log("cm_GetRootCellName code %d, cm_freelanceEnabled= %d, rcn= %s", 
+    afsi_log("cm_GetRootCellName code %d, cm_freelanceEnabled= %d, rcn= %s",
              code, cm_freelanceEnabled, (code ? "<none>" : rootCellName));
-    if (code != 0 && !cm_freelanceEnabled) 
+    if (code != 0 && !cm_freelanceEnabled)
     {
         *reasonP = "can't find root cell name in " AFS_CELLSERVDB;
         return -1;
-    }   
+    }
     else if (cm_freelanceEnabled)
         cm_data.rootCellp = NULL;
 
-    if (code == 0 && !cm_freelanceEnabled) 
+    if (code == 0 && !cm_freelanceEnabled)
     {
         cm_data.rootCellp = cm_GetCell(rootCellName, CM_FLAG_CREATE);
         afsi_log("cm_GetCell addr %x", PtrToUlong(cm_data.rootCellp));
-        if (cm_data.rootCellp == NULL) 
+        if (cm_data.rootCellp == NULL)
         {
             *reasonP = "can't find root cell in " AFS_CELLSERVDB;
             return -1;
@@ -1324,7 +1546,10 @@ int afsd_InitCM(char **reasonP)
     MSRPC_Init();
 
     afsd_InitServerPreferences();
-    return 0;
+
+    code = afsd_InitRoot(reasonP);
+
+    return code;
 }
 
 int afsd_ShutdownCM(void)
@@ -1333,6 +1558,8 @@ int afsd_ShutdownCM(void)
 
     cm_ReleaseSCache(cm_data.rootSCachep);
 
+    cm_utilsCleanup();
+
     cm_shutdown = 1;
 
     return 0;
@@ -1340,44 +1567,6 @@ int afsd_ShutdownCM(void)
 
 int afsd_InitDaemons(char **reasonP)
 {
-    long code;
-    cm_req_t req;
-
-    cm_InitReq(&req);
-
-    /* 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");
-       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;
-        }
-    }
-
-    /* compute the root fid */
-    if (!cm_freelanceEnabled) {
-        cm_SetFid(&cm_data.rootFid, cm_data.rootCellp->cellID, cm_GetROVolumeID(cm_data.rootVolumep), 1, 1);
-    }
-    else
-        cm_FakeRootFid(&cm_data.rootFid);
-        
-    code = cm_GetSCache(&cm_data.rootFid, &cm_data.rootSCachep, cm_rootUserp, &req);
-    afsi_log("cm_GetSCache code %x scache %x", code,
-             (code ? (cm_scache_t *)-1 : cm_data.rootSCachep));
-    if (code != 0) {
-        *reasonP = "unknown error";
-        return -1;
-    }
-
     cm_InitDaemon(numBkgD);
     afsi_log("cm_InitDaemon complete");
 
@@ -1419,18 +1608,35 @@ int afsd_InitSMB(char **reasonP, void *aMBfunc)
                 smb_AsyncStoreSize = cm_data.buf_blockSize;
             else
                 smb_AsyncStoreSize = (dwValue & ~(cm_data.buf_blockSize-1));
-        } else 
+        } else
             smb_AsyncStoreSize = CM_CONFIGDEFAULT_ASYNCSTORESIZE;
         afsi_log("SMBAsyncStoreSize = %d", smb_AsyncStoreSize);
-        
+
+        dummyLen = sizeof(DWORD);
+        code = RegQueryValueEx(parmKey, "SMBInterfaceEnabled", NULL, NULL,
+                                (BYTE *) &dwValue, &dummyLen);
+        if (code == ERROR_SUCCESS)
+            smb_Enabled = dwValue ? 1 : 0;
+        afsi_log("SMBInterfaceEnabled = %d", smb_Enabled);
+
         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, smb_UseV3, numSvThreads, aMBfunc);
-    afsi_log("smb_Init complete");
+    if ( smb_Enabled ) {
+        /* Do this last so that we don't handle requests before init is done.
+         * Here we initialize the SMB listener.
+         */
+        smb_Init(afsd_logp, smb_UseV3, numSvThreads, aMBfunc);
+        afsi_log("smb_Init complete");
+    } else {
+        smb_configureBackConnectionHostNames(FALSE);
+
+        if (msftSMBRedirectorSupportsExtendedTimeouts()) {
+            afsi_log("Microsoft SMB Redirector supports Extended Timeouts");
+            smb_configureExtendedSMBSessionTimeouts(FALSE);
+        }
+        afsi_log("smb_Init skipped");
+    }
 
     return 0;
 }
@@ -1460,33 +1666,33 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
 #endif
     DWORD symOptions;
     char functionName[MAXNAMELEN];
-  
+
     IMAGEHLP_MODULE Module;
     IMAGEHLP_LINE Line;
-  
+
     STACKFRAME s;
     IMAGEHLP_SYMBOL *pSym;
-  
+
     afsi_log_useTimestamp = 0;
-  
+
     pSym = (IMAGEHLP_SYMBOL *) GlobalAlloc(0, sizeof (IMAGEHLP_SYMBOL) + MAXNAMELEN);
-  
+
     memset( &s, '\0', sizeof s );
     if (!SymInitialize(hProcess, NULL, 1) )
     {
         afsi_log("SymInitialize(): GetLastError() = %lu\n", GetLastError() );
-      
+
         SymCleanup( hProcess );
         GlobalFree(pSym);
-      
+
         return;
     }
-  
+
     symOptions = SymGetOptions();
     symOptions |= SYMOPT_LOAD_LINES;
     symOptions &= ~SYMOPT_UNDNAME;
     SymSetOptions( symOptions );
-  
+
     /*
      * init STACKFRAME for first call
      * Notes: AddrModeFlat is just an assumption. I hate VDM debugging.
@@ -1511,55 +1717,55 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
     memset( pSym, '\0', sizeof (IMAGEHLP_SYMBOL) + MAXNAMELEN );
     pSym->SizeOfStruct = sizeof (IMAGEHLP_SYMBOL);
     pSym->MaxNameLength = MAXNAMELEN;
-  
+
     memset( &Line, '\0', sizeof Line );
     Line.SizeOfStruct = sizeof Line;
-  
+
     memset( &Module, '\0', sizeof Module );
     Module.SizeOfStruct = sizeof Module;
-  
+
     offset = 0;
-  
+
     afsi_log("\n--# FV EIP----- RetAddr- FramePtr StackPtr Symbol" );
-  
+
     for ( frameNum = 0; ; ++ frameNum )
     {
         /*
-         * get next stack frame (StackWalk(), SymFunctionTableAccess(), 
+         * get next stack frame (StackWalk(), SymFunctionTableAccess(),
          * SymGetModuleBase()). if this returns ERROR_INVALID_ADDRESS (487) or
          * ERROR_NOACCESS (998), you can assume that either you are done, or
          * that the stack is so hosed that the next deeper frame could not be
          * found.
          */
-        if ( ! StackWalk( IMAGE_FILE_MACHINE_I386, hProcess, hThread, &s, c, 
-                          NULL, SymFunctionTableAccess, SymGetModuleBase, 
+        if ( ! StackWalk( IMAGE_FILE_MACHINE_I386, hProcess, hThread, &s, c,
+                          NULL, SymFunctionTableAccess, SymGetModuleBase,
                           NULL ) )
             break;
-      
+
         /* display its contents */
         afsi_log("\n%3d %c%c %08lx %08lx %08lx %08lx ",
                  frameNum, s.Far? 'F': '.', s.Virtual? 'V': '.',
                  s.AddrPC.Offset, s.AddrReturn.Offset,
                  s.AddrFrame.Offset, s.AddrStack.Offset );
-      
+
         if ( s.AddrPC.Offset == 0 )
         {
             afsi_log("(-nosymbols- PC == 0)" );
         }
         else
-        { 
+        {
             /* show procedure info from a valid PC */
             if (!SymGetSymFromAddr(hProcess, s.AddrPC.Offset, &offset, pSym))
             {
                 if ( GetLastError() != ERROR_INVALID_ADDRESS )
                 {
-                    afsi_log("SymGetSymFromAddr(): errno = %lu", 
+                    afsi_log("SymGetSymFromAddr(): errno = %lu",
                              GetLastError());
                 }
             }
             else
             {
-                UnDecorateSymbolName(pSym->Name, functionName, MAXNAMELEN, 
+                UnDecorateSymbolName(pSym->Name, functionName, MAXNAMELEN,
                                      UNDNAME_NAME_ONLY);
                 afsi_log("%s", functionName );
 
@@ -1573,17 +1779,17 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
             {
                 if (GetLastError() != ERROR_INVALID_ADDRESS)
                 {
-                    afsi_log("Error: SymGetLineFromAddr(): errno = %lu", 
+                    afsi_log("Error: SymGetLineFromAddr(): errno = %lu",
                              GetLastError());
                 }
             }
             else
             {
-                afsi_log("    Line: %s(%lu) %+ld bytes", Line.FileName, 
+                afsi_log("    Line: %s(%lu) %+ld bytes", Line.FileName,
                          Line.LineNumber, offset);
             }
         }
-      
+
         /* no return address means no deeper stackframe */
         if (s.AddrReturn.Offset == 0)
         {
@@ -1591,12 +1797,12 @@ void afsd_printStack(HANDLE hThread, CONTEXT *c)
             break;
         }
     }
-  
+
     if (GetLastError() != 0)
     {
         afsi_log("\nStackWalk(): errno = %lu\n", GetLastError());
     }
-  
+
     SymCleanup(hProcess);
     GlobalFree(pSym);
 }
@@ -1617,37 +1823,42 @@ static BOOL (WINAPI *pMiniDumpWriteDump)(HANDLE hProcess,DWORD ProcessId,HANDLE
 static HANDLE
 OpenDumpFile(void)
 {
+    char tmp[256];
     char wd[256];
+    SYSTEMTIME st;
     DWORD code;
 
-    code = GetEnvironmentVariable("TEMP", wd, sizeof(wd));
-    if ( code == 0 || code > sizeof(wd) )
+    code = GetEnvironmentVariable("TEMP", tmp, sizeof(tmp));
+    if ( code == 0 || code > sizeof(tmp) )
     {
-        if (!GetWindowsDirectory(wd, sizeof(wd)))
+        if (!GetWindowsDirectory(tmp, sizeof(tmp)))
             return NULL;
     }
-    StringCbCatA(wd, sizeof(wd), "\\afsd.dmp");
+    GetLocalTime(&st);
+    StringCbPrintfA(wd, sizeof(wd),
+                    "%s\\afsd-%04d-%02d-%02d-%02d_%02d_%02d.dmp", tmp,
+                    st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
     return CreateFile( wd, GENERIC_WRITE, FILE_SHARE_READ, NULL,
                             CREATE_ALWAYS, FILE_FLAG_WRITE_THROUGH, NULL);
 }
 
-void 
+void
 GenerateMiniDump(PEXCEPTION_POINTERS ep)
 {
     if (IsDebuggerPresent())
         return;
 
-    if (ep == NULL) 
+    if (ep == NULL)
     {
         // Generate exception to get proper context in dump
-        __try 
+        __try
         {
             RaiseException(DBG_CONTINUE, 0, 0, NULL);
-        } 
-        __except(GenerateMiniDump(GetExceptionInformation()), EXCEPTION_CONTINUE_EXECUTION) 
+        }
+        __except(GenerateMiniDump(GetExceptionInformation()), EXCEPTION_CONTINUE_EXECUTION)
         {
         }
-    } 
+    }
     else
     {
         MINIDUMP_EXCEPTION_INFORMATION eInfo;
@@ -1701,27 +1912,27 @@ GenerateMiniDump(PEXCEPTION_POINTERS ep)
 LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
 {
     CONTEXT context;
-#ifdef _DEBUG  
+#ifdef _DEBUG
     BOOL allocRequestBrk = FALSE;
-#endif 
+#endif
     HMODULE hLib = NULL;
-  
-    afsi_log("UnhandledException : code : 0x%x, address: 0x%x\n", 
-             ep->ExceptionRecord->ExceptionCode, 
+
+    afsi_log("UnhandledException : code : 0x%x, address: 0x%x\n",
+             ep->ExceptionRecord->ExceptionCode,
              ep->ExceptionRecord->ExceptionAddress);
-          
+
 #ifdef _DEBUG
-    if (afsd_crtDbgBreakCurrent && 
+    if (afsd_crtDbgBreakCurrent &&
         *afsd_crtDbgBreakCurrent == _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent))
-    { 
+    {
         allocRequestBrk = TRUE;
         afsi_log("Breaking on alloc request # %d\n", *afsd_crtDbgBreakCurrent);
     }
 #endif
-          
+
     /* save context if we want to print the stack information */
     context = *ep->ContextRecord;
-          
+
     afsd_printStack(GetCurrentThread(), &context);
 
     GenerateMiniDump(ep);
@@ -1737,15 +1948,15 @@ LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
     if (ep->ExceptionRecord->ExceptionCode == EXCEPTION_BREAKPOINT)
     {
         afsi_log("\nEXCEPTION_BREAKPOINT - continue execution ...\n");
-    
+
 #ifdef _DEBUG
         if (allocRequestBrk)
         {
             afsd_crtDbgBreakCurrent++;
             _CrtSetBreakAlloc(*afsd_crtDbgBreakCurrent);
         }
-#endif         
-#if defined(_X86)    
+#endif
+#if defined(_X86)
         ep->ContextRecord->Eip++;
 #endif
 #if defined(_AMD64_)
@@ -1758,7 +1969,7 @@ LONG __stdcall afsd_ExceptionFilter(EXCEPTION_POINTERS *ep)
         return EXCEPTION_CONTINUE_SEARCH;
     }
 }
-  
+
 void afsd_SetUnhandledExceptionFilter()
 {
 #ifndef NOTRACE
@@ -1772,7 +1983,7 @@ void afsd_DbgBreakAllocInit()
     memset(afsd_crtDbgBreaks, -1, sizeof(afsd_crtDbgBreaks));
     afsd_crtDbgBreakCurrent = afsd_crtDbgBreaks;
 }
-  
+
 void afsd_DbgBreakAdd(DWORD requestNumber)
 {
     int i;