windows-cellservdb-lookup-20090525
[openafs.git] / src / WINNT / afsd / cm_cell.c
index 19820d3..12bbbf5 100644 (file)
@@ -28,7 +28,7 @@ osi_rwlock_t cm_cellLock;
  *
  * At the present time the return value is ignored by the caller.
  */
-long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep)
+long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep, unsigned short ipRank)
 {
     cm_server_t *tsp;
     cm_serverRef_t *tsrp;
@@ -52,7 +52,9 @@ long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep)
         }
     }       
     else
-        tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp, probe ? 0 : CM_FLAG_NOPROBE);
+        tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp, NULL, probe ? 0 : CM_FLAG_NOPROBE);
+
+    tsp->ipRank = ipRank;
 
     /* Insert the vlserver into a sorted list, sorted by server rank */
     tsrp = cm_NewServerRef(tsp, 0);
@@ -69,7 +71,7 @@ long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep)
  * and check to make sure we have a valid set of volume servers
  * this function must be called with a Write Lock on cm_cellLock
  */
-static cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
+cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
 {
     long code = 0;
     cm_cell_rock_t rock;
@@ -87,7 +89,10 @@ static cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
         || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
          ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
 #endif
-            ) {
+            ) 
+    {
+        lock_ReleaseMutex(&cp->mx);
+
         /* must empty cp->vlServersp */
         if (cp->vlServersp) {
             cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
@@ -96,17 +101,26 @@ static cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
 
         rock.cellp = cp;
         rock.flags = flags;
-        code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, &rock);
+        code = cm_SearchCellRegistry(1, cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
+        if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
+            code = cm_SearchCellFileEx(cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
+        if (code == 0) {
+            lock_ObtainMutex(&cp->mx);
+           cp->timeout = time(0) + 7200;
+            lock_ReleaseMutex(&cp->mx);
+        }
 #ifdef AFS_AFSDB_ENV
-        if (code) {
+        else {
             if (cm_dnsEnabled) {
                 int ttl;
 
                 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
                 if (code == 0) {   /* got cell from DNS */
+                    lock_ObtainMutex(&cp->mx);
                     cp->flags |= CM_CELLFLAG_DNS;
                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
                    cp->timeout = time(0) + ttl;
+                    lock_ReleaseMutex(&cp->mx);
 #ifdef DEBUG
                     fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
 #endif
@@ -114,43 +128,72 @@ static cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
                     /* if we fail to find it this time, we'll just do nothing and leave the
                      * current entry alone 
                     */
+                    lock_ObtainMutex(&cp->mx);
                     cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
+                    lock_ReleaseMutex(&cp->mx);
                 }
            }
-       } else 
+       }
 #endif /* AFS_AFSDB_ENV */
-       {
-           cp->timeout = time(0) + 7200;
-       }       
+    } else {
+        lock_ReleaseMutex(&cp->mx);
     }
-    lock_ReleaseMutex(&cp->mx);
     return code ? NULL : cp;
 }
 
-/* load up a cell structure from the cell database, afsdcell.ini */
+/* load up a cell structure from the cell database, AFS_CELLSERVDB */
 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
 {
     return cm_GetCell_Gen(namep, NULL, flags);
 }
 
+void cm_FreeCell(cm_cell_t *cellp)
+{
+    if (cellp->vlServersp)
+        cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
+    cellp->name[0] = '\0';    
+
+    cellp->freeNextp = cm_data.freeCellsp;
+    cm_data.freeCellsp = cellp;
+}
+
 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
 {
     cm_cell_t *cp, *cp2;
     long code;
-    char fullname[200]="";
+    char fullname[CELL_MAXNAMELEN]="";
+    char linkedName[CELL_MAXNAMELEN]="";
+    char name[CELL_MAXNAMELEN]="";
     int  hasWriteLock = 0;
+    int  hasMutex = 0;
     afs_uint32 hash;
     cm_cell_rock_t rock;
+    size_t len;
 
-    if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
+    if (namep == NULL || !namep[0] || !strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
+        return NULL;
+
+    /* 
+     * Strip off any trailing dots at the end of the cell name.
+     * Failure to do so results in an undesireable alias as the
+     * result of DNS AFSDB record lookups where a trailing dot
+     * has special meaning.
+     */
+    strncpy(name, namep, CELL_MAXNAMELEN);
+    for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
+        name[len-1] = '\0';
+    }
+    if (len == 0)
         return NULL;
+    namep = name;
 
     hash = CM_CELL_NAME_HASH(namep);
 
     lock_ObtainRead(&cm_cellLock);
     for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
-        if (stricmp(namep, cp->name) == 0) {
-            strcpy(fullname, cp->name);
+        if (cm_stricmp_utf8(namep, cp->name) == 0) {
+            strncpy(fullname, cp->name, CELL_MAXNAMELEN);
+            fullname[CELL_MAXNAMELEN-1] = '\0';
             break;
         }
     }
@@ -158,26 +201,27 @@ cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
     if (!cp) {
         for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
             if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
-                strcpy(fullname, cp->name);
+                strncpy(fullname, cp->name, CELL_MAXNAMELEN);
+                fullname[CELL_MAXNAMELEN-1] = '\0';
                 break;
             }
         }   
     }
 
-    lock_ReleaseRead(&cm_cellLock);
-
     if (cp) {
+        lock_ReleaseRead(&cm_cellLock);
         cm_UpdateCell(cp, flags);
     } else if (flags & CM_FLAG_CREATE) {
-        lock_ObtainWrite(&cm_cellLock);
+        lock_ConvertRToW(&cm_cellLock);
         hasWriteLock = 1;
 
         /* when we dropped the lock the cell could have been added
          * to the list so check again while holding the write lock 
          */
         for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
-            if (stricmp(namep, cp->name) == 0) {
-                strcpy(fullname, cp->name);
+            if (cm_stricmp_utf8(namep, cp->name) == 0) {
+                strncpy(fullname, cp->name, CELL_MAXNAMELEN);
+                fullname[CELL_MAXNAMELEN-1] = '\0';
                 break;
             }
         }   
@@ -187,30 +231,57 @@ cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
 
         for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
             if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
-                strcpy(fullname, cp->name);
+                strncpy(fullname, cp->name, CELL_MAXNAMELEN);
+                fullname[CELL_MAXNAMELEN-1] = '\0';
                 break;
             }
         }   
 
-        if (cp)
+        if (cp) {
+            lock_ObtainMutex(&cp->mx);
+            cm_AddCellToNameHashTable(cp);
+            cm_AddCellToIDHashTable(cp);           
+            lock_ReleaseMutex(&cp->mx);
             goto done;
+        }
 
-        if ( cm_data.currentCells >= cm_data.maxCells )
-            osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
+        if ( cm_data.freeCellsp != NULL ) {
+            cp = cm_data.freeCellsp;
+            cm_data.freeCellsp = cp->freeNextp;
+
+            /* 
+             * The magic, cellID, and mx fields are already set.
+             */
+        } else {
+            if ( cm_data.currentCells >= cm_data.maxCells )
+                osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
+
+            /* don't increment currentCells until we know that we 
+             * are going to keep this entry 
+             */
+            cp = &cm_data.cellBaseAddress[cm_data.currentCells];
+            memset(cp, 0, sizeof(cm_cell_t));
+            cp->magic = CM_CELL_MAGIC;
+
+            /* the cellID cannot be 0 */
+            cp->cellID = ++cm_data.currentCells;
+
+            /* otherwise we found the cell, and so we're nearly done */
+            lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
+        }
+
+        lock_ReleaseWrite(&cm_cellLock);
+        hasWriteLock = 0;
 
-        /* don't increment currentCells until we know that we 
-         * are going to keep this entry 
-         */
-        cp = &cm_data.cellBaseAddress[cm_data.currentCells];
-        memset(cp, 0, sizeof(cm_cell_t));
-        cp->magic = CM_CELL_MAGIC;
-        
         rock.cellp = cp;
         rock.flags = flags;
-        code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, &rock);
+        code = cm_SearchCellRegistry(1, namep, fullname, linkedName, cm_AddCellProc, &rock);
+        if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
+            code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
         if (code) {
-            osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s", 
-                      osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
+            osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s", 
+                      osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
+                      osi_LogSaveString(afsd_logp,linkedName));
 
 #ifdef AFS_AFSDB_ENV
             if (cm_dnsEnabled) {
@@ -220,21 +291,27 @@ cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
                 if ( code ) {
                     osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s", 
                              osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
+                    cm_FreeCell(cp);
                     cp = NULL;
                     goto done;
                 } else {   /* got cell from DNS */
+                    lock_ObtainMutex(&cp->mx);
+                    hasMutex = 1;
                     cp->flags |= CM_CELLFLAG_DNS;
                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
                     cp->timeout = time(0) + ttl;
                 }
-            } else {
+            } 
+            else 
 #endif
+            {
+                cm_FreeCell(cp);
                 cp = NULL;
                 goto done;
-#ifdef AFS_AFSDB_ENV
            }
-#endif
         } else {
+            lock_ObtainMutex(&cp->mx);
+            hasMutex = 1;
            cp->timeout = time(0) + 7200;       /* two hour timeout */
        }
 
@@ -245,32 +322,39 @@ cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
          */
         hash = CM_CELL_NAME_HASH(fullname);
         for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
-            if (stricmp(fullname, cp2->name) == 0) {
+            if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
                 break;
             }
         }   
 
         if (cp2) {
-            cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
+            if (hasMutex) {
+                lock_ReleaseMutex(&cp->mx);
+                hasMutex = 0;
+            }
+            cm_FreeCell(cp);
             cp = cp2;
             goto done;
         }
 
-
         /* randomise among those vlservers having the same rank*/ 
         cm_RandomizeServer(&cp->vlServersp);
 
-        /* otherwise we found the cell, and so we're nearly done */
-        lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
-
+        if (!hasMutex)
+            lock_ObtainMutex(&cp->mx);
         /* copy in name */
         strncpy(cp->name, fullname, CELL_MAXNAMELEN);
         cp->name[CELL_MAXNAMELEN-1] = '\0';
 
-        /* the cellID cannot be 0 */
-        cp->cellID = ++cm_data.currentCells;
+        strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
+        cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
 
-               /* append cell to global list */
+        cm_AddCellToNameHashTable(cp);
+        cm_AddCellToIDHashTable(cp);           
+        lock_ReleaseMutex(&cp->mx);
+        hasMutex = 0;
+
+        /* append cell to global list */
         if (cm_data.allCellsp == NULL) {
             cm_data.allCellsp = cp;
         } else {
@@ -280,18 +364,44 @@ cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
         }
         cp->allNextp = NULL;
 
-        cm_AddCellToNameHashTable(cp);
-        cm_AddCellToIDHashTable(cp);           
+    } else {
+        lock_ReleaseRead(&cm_cellLock);
     }
-
   done:
+    if (hasMutex && cp)
+        lock_ReleaseMutex(&cp->mx);
     if (hasWriteLock)
         lock_ReleaseWrite(&cm_cellLock);
     
     /* fullname is not valid if cp == NULL */
-    if (cp && newnamep)
-        strcpy(newnamep, fullname);
-    
+    if (newnamep) {
+        if (cp) {
+            strncpy(newnamep, fullname, CELL_MAXNAMELEN);
+            newnamep[CELL_MAXNAMELEN-1]='\0';
+        } else {
+            newnamep[0] = '\0';
+        }
+    }
+
+    if (cp && cp->linkedName[0]) {
+        cm_cell_t * linkedCellp = NULL;
+        
+        if (!strcmp(cp->name, cp->linkedName)) {
+            cp->linkedName[0] = '\0'; 
+        } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
+            linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
+
+            lock_ObtainWrite(&cm_cellLock);
+            if (!linkedCellp || 
+                (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
+                cp->linkedName[0] = '\0';
+            } else {
+                strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
+                linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
+            }
+            lock_ReleaseWrite(&cm_cellLock);
+        }
+    }
     return cp;
 }
 
@@ -336,10 +446,19 @@ cm_ValidateCell(void)
         }
     }
 
+    for (cellp = cm_data.freeCellsp; cellp; cellp=cellp->freeNextp, count++) {
+        if ( count != 0 && cellp == cm_data.freeCellsp ||
+             count > cm_data.maxCells ) {
+            afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
+            fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
+            return -3;
+        }
+    }
+
     if ( count != cm_data.currentCells ) {
         afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
         fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
-        return -3;
+        return -4;
     }
     
     return 0;
@@ -365,7 +484,7 @@ void cm_InitCell(int newFile, long maxCells)
     if (osi_Once(&once)) {
         cm_cell_t * cellp;
 
-        lock_InitializeRWLock(&cm_cellLock, "cell global lock");
+        lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
 
         if ( newFile ) {
             cm_data.allCellsp = NULL;
@@ -383,10 +502,11 @@ void cm_InitCell(int newFile, long maxCells)
             memset(cellp, 0, sizeof(cm_cell_t));
             cellp->magic = CM_CELL_MAGIC;
 
-            lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
+            lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
 
             /* copy in name */
             strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
+            cellp->name[CELL_MAXNAMELEN-1] = '\0';
 
             /* thread on global list */
             cellp->allNextp = cm_data.allCellsp;
@@ -396,13 +516,16 @@ void cm_InitCell(int newFile, long maxCells)
             cellp->vlServersp = NULL;
             cellp->flags = CM_CELLFLAG_FREELANCE;
 
-           cm_AddCellToNameHashTable(cellp);
-           cm_AddCellToIDHashTable(cellp);           
+            lock_ObtainMutex(&cellp->mx);
+            cm_AddCellToNameHashTable(cellp);
+            cm_AddCellToIDHashTable(cellp);           
+            lock_ReleaseMutex(&cellp->mx);
 #endif  
         } else {
             for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
-                lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
+                lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
                 cellp->vlServersp = NULL;
+                cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
             }
         }
 
@@ -441,8 +564,8 @@ int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
 
     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
-        sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x\r\n", 
-                cookie, cellp, cellp->name, cellp->cellID, cellp->flags);
+        sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n", 
+                cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
     }