Windows: Rewrite LargeSearchTime conversions
[openafs.git] / src / WINNT / afsd / cm_utils.c
index 58a94cb..6c7718e 100644 (file)
@@ -1,13 +1,16 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
  */
 
+#include <afsconfig.h>
 #include <afs/param.h>
+#include <roken.h>
+
 #include <afs/stds.h>
 
 #include <errno.h>
@@ -192,7 +195,7 @@ et_to_sys_error(afs_int32 in)
 
 long cm_MapRPCError(long error, cm_req_t *reqp)
 {
-    if (error == 0) 
+    if (error == 0)
         return 0;
 
     /* If we had to stop retrying, report our saved error code. */
@@ -209,19 +212,28 @@ long cm_MapRPCError(long error, cm_req_t *reqp)
     error = et_to_sys_error(error);
 
     if (error == RX_CALL_DEAD ||
-        error == RX_CALL_TIMEOUT)
-        error = CM_ERROR_RETRY;
+        error == RX_CALL_TIMEOUT ||
+        error == RX_CALL_BUSY ||
+        error == RX_MSGSIZE ||
+        error == VNOSERVICE)
+       error = CM_ERROR_TIMEDOUT;
+    else if (error == RX_CALL_IDLE)
+        error = EIO;
+    else if (error == RX_INVALID_OPERATION)
+        error = CM_ERROR_INVAL_NET_RESP;
     else if (error < 0)
         error = CM_ERROR_UNKNOWN;
-    else if (error == EROFS) 
+    else if (error == EINVAL)
+        error = CM_ERROR_INVAL;
+    else if (error == EROFS)
         error = CM_ERROR_READONLY;
-    else if (error == EACCES) 
+    else if (error == EACCES)
         error = CM_ERROR_NOACCESS;
-    else if (error == EXDEV) 
+    else if (error == EXDEV)
         error = CM_ERROR_CROSSDEVLINK;
-    else if (error == EEXIST) 
+    else if (error == EEXIST)
         error = CM_ERROR_EXISTS;
-    else if (error == ENOTDIR) 
+    else if (error == ENOTDIR)
         error = CM_ERROR_NOTDIR;
     else if (error == ENOENT)
         error = CM_ERROR_NOSUCHFILE;
@@ -242,7 +254,7 @@ long cm_MapRPCError(long error, cm_req_t *reqp)
         error = CM_ERROR_QUOTA;
     else if (error == VNOVNODE)
         error = CM_ERROR_BADFD;
-    else if (error == VNOSERVICE || error == VSALVAGE || error == VOFFLINE)
+    else if (error == VSALVAGE || error == VOFFLINE)
         error = CM_ERROR_ALLOFFLINE;
     else if (error == VBUSY || error == VRESTARTING)
         error = CM_ERROR_ALLBUSY;
@@ -253,7 +265,7 @@ long cm_MapRPCError(long error, cm_req_t *reqp)
 
 long cm_MapRPCErrorRmdir(long error, cm_req_t *reqp)
 {
-    if (error == 0) 
+    if (error == 0)
         return 0;
 
     /* If we had to stop retrying, report our saved error code. */
@@ -270,32 +282,41 @@ long cm_MapRPCErrorRmdir(long error, cm_req_t *reqp)
     error = et_to_sys_error(error);
 
     if (error == RX_CALL_DEAD ||
-        error == RX_CALL_TIMEOUT)
-        error = CM_ERROR_RETRY;
+        error == RX_CALL_TIMEOUT ||
+        error == RX_CALL_BUSY ||
+        error == RX_CALL_IDLE ||
+        error == RX_MSGSIZE ||
+        error == VNOSERVICE)
+       error = CM_ERROR_TIMEDOUT;
     else if (error == VNOVNODE)
         error = CM_ERROR_BADFD;
-    else if (error == VNOSERVICE || error == VSALVAGE || error == VOFFLINE)
+    else if (error == VSALVAGE || error == VOFFLINE)
         error = CM_ERROR_ALLOFFLINE;
     else if (error == VBUSY || error == VRESTARTING)
         error = CM_ERROR_ALLBUSY;
+    else if (error == RX_INVALID_OPERATION)
+        error = CM_ERROR_INVAL_NET_RESP;
     else if (error < 0)
         error = CM_ERROR_UNKNOWN;
-    else if (error == EROFS) 
+    else if (error == EROFS)
         error = CM_ERROR_READONLY;
-    else if (error == ENOTDIR) 
+    else if (error == ENOTDIR)
         error = CM_ERROR_NOTDIR;
-    else if (error == EACCES) 
+    else if (error == EACCES)
         error = CM_ERROR_NOACCESS;
-    else if (error == ENOENT) 
+    else if (error == ENOENT)
         error = CM_ERROR_NOSUCHFILE;
-    else if (error == ENOTEMPTY 
+    else if (error == EINVAL)
+        error = CM_ERROR_INVAL;
+    else if (error == ENOTEMPTY
+              || error == EEXIST
               || error == 17           /* AIX */
               || error == 66           /* SunOS 4, Digital UNIX */
               || error == 93           /* Solaris 2, IRIX */
               || error == 247) /* HP/UX */
         error = CM_ERROR_NOTEMPTY;
     return error;
-}       
+}
 
 long cm_MapVLRPCError(long error, cm_req_t *reqp)
 {
@@ -315,13 +336,21 @@ long cm_MapVLRPCError(long error, cm_req_t *reqp)
     error = et_to_sys_error(error);
 
     if (error == RX_CALL_DEAD ||
-        error == RX_CALL_TIMEOUT)
-        error = CM_ERROR_RETRY;
+        error == RX_CALL_TIMEOUT ||
+        error == RX_CALL_BUSY ||
+        error == RX_CALL_IDLE ||
+        error == RX_MSGSIZE ||
+        error == VNOSERVICE)
+       error = CM_ERROR_TIMEDOUT;
+    else if (error == RX_INVALID_OPERATION)
+        error = CM_ERROR_INVAL_NET_RESP;
     else if (error == RX_RESTARTING)
         error = CM_ERROR_ALLBUSY;
     else if (error < 0)
         error = CM_ERROR_UNKNOWN;
-    else if (error == VL_NOENT || error == VL_BADNAME) 
+    else if (error == EINVAL)
+        error = CM_ERROR_INVAL;
+    else if (error == VL_NOENT || error == VL_BADNAME)
        error = CM_ERROR_NOSUCHVOLUME;
     return error;
 }
@@ -339,7 +368,7 @@ cm_space_t *cm_GetSpace(void)
         else tsp = (cm_space_t *) malloc(sizeof(cm_space_t));
        (void) memset(tsp, 0, sizeof(cm_space_t));
         lock_ReleaseWrite(&cm_utilsLock);
-        
+
         return tsp;
 }
 
@@ -386,7 +415,7 @@ int cm_Is8Dot3(clientchar_t *namep)
     int sawDot = 0;
     clientchar_t tc;
     int charCount = 0;
-    
+
     if (namep == NULL || !namep[0])
         return 0;
 
@@ -424,13 +453,13 @@ int cm_Is8Dot3(clientchar_t *namep)
 
 /*
  * Number unparsing map for generating 8.3 names;
- * The version taken from DFS was on drugs.  
+ * The version taken from DFS was on drugs.
  * You can't include '&' and '@' in a file name.
  */
 char cm_8Dot3Mapping[42] =
 {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 
- 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
+ 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
  'V', 'W', 'X', 'Y', 'Z', '_', '-', '$', '#', '!', '{', '}'
 };
 int cm_8Dot3MapSize = sizeof(cm_8Dot3Mapping);
@@ -581,6 +610,43 @@ void cm_Gen8Dot3NameIntW(const clientchar_t * longname, cm_dirFid_t * pfid,
         *shortNameEndp = shortName;
 }
 
+void cm_Gen8Dot3VolNameW(afs_uint32 cell, afs_uint32 volume,
+                         clientchar_t *shortName, clientchar_t **shortNameEndp)
+{
+    clientchar_t number[12];
+    int i, nsize = 0;
+    int validExtension = 0;
+
+    /* Unparse the file's cell and volume numbers */
+    do {
+        number[nsize] = cm_8Dot3Mapping[cell % cm_8Dot3MapSize];
+        nsize++;
+        cell /= cm_8Dot3MapSize;
+    } while (cell);
+    do {
+        number[nsize] = cm_8Dot3Mapping[volume % cm_8Dot3MapSize];
+        nsize++;
+        volume /= cm_8Dot3MapSize;
+    } while (volume && nsize < 8);
+
+    /* Copy uniquifier characters */
+    for (i=0; i < nsize; i++) {
+        *shortName++ = number[i];
+    }
+
+    /* Add extension characters */
+    *shortName++ = '.';        /* copy dot */
+    *shortName++ = 'v';
+    *shortName++ = 'o';
+    *shortName++ = 'l';
+
+    /* Trailing null */
+    *shortName = 0;
+
+    if (shortNameEndp)
+        *shortNameEndp = shortName;
+}
+
 /*! \brief Compare 'pattern' (containing metacharacters '*' and '?') with the file name 'name'.
 
   \note This procedure works recursively calling itself.
@@ -590,8 +656,8 @@ void cm_Gen8Dot3NameIntW(const clientchar_t * longname, cm_dirFid_t * pfid,
 
   \return BOOL : TRUE/FALSE (match/mistmatch)
 */
-static BOOL 
-szWildCardMatchFileName(clientchar_t * pattern, clientchar_t * name, int casefold) 
+static BOOL
+szWildCardMatchFileName(clientchar_t * pattern, clientchar_t * name, int casefold)
 {
     clientchar_t upattern[MAX_PATH];
     clientchar_t uname[MAX_PATH];
@@ -678,14 +744,14 @@ szWildCardMatchFileName(clientchar_t * pattern, clientchar_t * name, int casefol
 /* do a case-folding search of the star name mask with the name in namep.
  * Return 1 if we match, otherwise 0.
  */
-int cm_MatchMask(clientchar_t *namep, clientchar_t *maskp, int flags) 
+int cm_MatchMask(clientchar_t *namep, clientchar_t *maskp, int flags)
 {
     clientchar_t *newmask, lastchar = _C('\0');
     int    i, j, casefold, retval;
     int  star = 0, qmark = 0, dot = 0;
 
     /* make sure we only match 8.3 names, if requested */
-    if ((flags & CM_FLAG_8DOT3) && !cm_Is8Dot3(namep)) 
+    if ((flags & CM_FLAG_8DOT3) && !cm_Is8Dot3(namep))
         return 0;
 
     casefold = (flags & CM_FLAG_CASEFOLD) ? 1 : 0;
@@ -750,7 +816,7 @@ cm_TargetPerceivedAsDirectory(const fschar_t *target)
     return FALSE;
 }
 
-HANDLE 
+HANDLE
 cm_LoadAfsdHookLib(void)
 {
     char dllname[260];
@@ -820,7 +886,7 @@ typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
 typedef BOOL (WINAPI *LPFN_DISABLEWOW64FSREDIRECTION) (PVOID *);
 typedef BOOL (WINAPI *LPFN_REVERTWOW64FSREDIRECTION) (PVOID);
 
-BOOL msftSMBRedirectorSupportsExtendedTimeouts(void) 
+BOOL msftSMBRedirectorSupportsExtendedTimeouts(void)
 {
     static BOOL fChecked = FALSE;
     static BOOL fSupportsExtendedTimeouts = FALSE;
@@ -837,7 +903,7 @@ BOOL msftSMBRedirectorSupportsExtendedTimeouts(void)
         LARGE_INTEGER fvFile, fvHotFixMin;
 
         h1 = GetModuleHandle("kernel32.dll"); /* no refcount increase */
-        /* 
+        /*
          * If we don't find the fnIsWow64Process function then we
          * are not running in a Wow64 environment
          */
@@ -848,7 +914,7 @@ BOOL msftSMBRedirectorSupportsExtendedTimeouts(void)
         Version.dwOSVersionInfoSize = sizeof(Version);
         GetVersionEx((OSVERSIONINFO *) &Version);
 
-        /* 
+        /*
          * Support is available as hot fixes / service packs on:
          *   XP SP2
          *   XP SP3
@@ -862,7 +928,7 @@ BOOL msftSMBRedirectorSupportsExtendedTimeouts(void)
             /* 32-bit XP */
             if (Version.dwMajorVersion == 5 &&
                 Version.dwMinorVersion == 1) {
-                
+
                 fvHotFixMin.HighPart = (5 << 16) | 1;
 
                 switch (Version.wServicePackMajor) {
@@ -881,7 +947,7 @@ BOOL msftSMBRedirectorSupportsExtendedTimeouts(void)
             /* 64-bit XP and Server 2003 */
             else if (Version.dwMajorVersion == 5 &&
                      Version.dwMinorVersion == 2) {
-                
+
                 fvHotFixMin.HighPart = (5 << 16) | 2;
 
                 switch (Version.wServicePackMajor) {
@@ -900,7 +966,7 @@ BOOL msftSMBRedirectorSupportsExtendedTimeouts(void)
             /* Vista and Server 2008 */
             else if (Version.dwMajorVersion == 6 &&
                      Version.dwMinorVersion == 0) {
-                
+
                 fvHotFixMin.HighPart = (6 << 16) | 0;
 
                 switch (Version.wServicePackMajor) {
@@ -922,7 +988,7 @@ BOOL msftSMBRedirectorSupportsExtendedTimeouts(void)
             }
 
             /* If wow64, disable wow64 redirection and preserve the existing state */
-            if (fnIsWow64Process && 
+            if (fnIsWow64Process &&
                  fnIsWow64Process(GetCurrentProcess(), &isWow64) &&
                  isWow64) {
                 fnDisableWow64FsRedirection =
@@ -931,7 +997,7 @@ BOOL msftSMBRedirectorSupportsExtendedTimeouts(void)
                     (LPFN_REVERTWOW64FSREDIRECTION)GetProcAddress(h1, "Wow64RevertWow64FsRedirection");
                 fnDisableWow64FsRedirection(&Wow64RedirectionState);
             }
-            
+
             if (cm_GetOSFileVersion("drivers\\mrxsmb.sys", &fvFile) ||
                 (fvFile.QuadPart >= fvHotFixMin.QuadPart))
                 fSupportsExtendedTimeouts = TRUE;
@@ -939,7 +1005,7 @@ BOOL msftSMBRedirectorSupportsExtendedTimeouts(void)
             /* If wow64, restore the previous redirection state */
             if (fnIsWow64Process && isWow64) {
                 fnRevertWow64FsRedirection(Wow64RedirectionState);
-            }            
+            }
         }
       checked:
         fChecked = TRUE;
@@ -968,11 +1034,11 @@ void cm_SetRequestStartTime()
 
        if (!TlsSetValue(cm_TlsRequestSlot, tp))
            free(tp);
-    }  
+    }
 }
 
 void cm_UpdateServerPriority()
-{      
+{
     time_t *tp = TlsGetValue(cm_TlsRequestSlot);
 
     if (tp) {
@@ -987,26 +1053,29 @@ void cm_UpdateServerPriority()
 void cm_LargeSearchTimeFromUnixTime(FILETIME *largeTimep, time_t unixTime)
 {
     // Note that LONGLONG is a 64-bit value
-    LONGLONG ll;
-
-    ll = Int32x32To64(unixTime, 10000000) + 116444736000000000;
-    largeTimep->dwLowDateTime = (DWORD)(ll & 0xFFFFFFFF);
-    largeTimep->dwHighDateTime = (DWORD)(ll >> 32);
+    LARGE_INTEGER ll;
+
+#ifdef _USE_32BIT_TIME_T
+    ll.QuadPart = UInt32x32To64(unixTime, 10000000) + 116444736000000000;
+#else
+    ll.QuadPart = unixTime * 10000000 + 116444736000000000;
+#endif
+    largeTimep->dwLowDateTime = ll.LowPart;
+    largeTimep->dwHighDateTime = ll.HighPart;
 }
 
 void cm_UnixTimeFromLargeSearchTime(time_t *unixTimep, FILETIME *largeTimep)
 {
     // Note that LONGLONG is a 64-bit value
-    LONGLONG ll;
+    LARGE_INTEGER ll;
 
-    ll = largeTimep->dwHighDateTime;
-    ll <<= 32;
-    ll += largeTimep->dwLowDateTime;
+    ll.HighPart = largeTimep->dwHighDateTime;
+    ll.LowPart  = largeTimep->dwLowDateTime;
 
-    ll -= 116444736000000000;
-    ll /= 10000000;
+    ll.QuadPart -= 116444736000000000;
+    ll.QuadPart /= 10000000;
 
-    *unixTimep = (DWORD)ll;
+    *unixTimep = (time_t)ll.QuadPart;
 }
 
 void cm_SearchTimeFromUnixTime(afs_uint32 *searchTimep, time_t unixTime)
@@ -1028,19 +1097,19 @@ void cm_SearchTimeFromUnixTime(afs_uint32 *searchTimep, time_t unixTime)
         localJunk.tm_hour = 0;
         localJunk.tm_min = 0;
         localJunk.tm_sec = 0;
-    }  
+    }
 
     dosDate = ((ltp->tm_year-80)<<9) | ((ltp->tm_mon+1) << 5) | (ltp->tm_mday);
     dosTime = (ltp->tm_hour<<11) | (ltp->tm_min << 5) | (ltp->tm_sec / 2);
     *searchTimep = (dosDate<<16) | dosTime;
-}      
+}
 
 void cm_UnixTimeFromSearchTime(time_t *unixTimep, afs_uint32 searchTime)
 {
     unsigned short dosDate;
     unsigned short dosTime;
     struct tm localTm;
-        
+
     dosDate = (unsigned short) (searchTime & 0xffff);
     dosTime = (unsigned short) ((searchTime >> 16) & 0xffff);
 
@@ -1054,3 +1123,16 @@ void cm_UnixTimeFromSearchTime(time_t *unixTimep, afs_uint32 searchTime)
 
     *unixTimep = mktime(&localTm);
 }
+
+afs_uint32
+cm_NextHighestPowerOf2(afs_uint32 n)
+{
+    n--;
+    n |= n >> 1;
+    n |= n >> 2;
+    n |= n >> 4;
+    n |= n >> 8;
+    n |= n >> 16;
+    n++;
+    return n;
+}