2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
24 #define STRSAFE_NO_DEPRECATE
29 osi_rwlock_t cm_cellLock;
31 /* function called as callback proc from cm_SearchCellFile. Return 0 to
32 * continue processing.
34 * At the present time the return value is ignored by the caller.
36 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep, unsigned short adminRank)
41 cm_cell_rock_t *cellrockp = (cm_cell_rock_t *)rockp;
44 cellp = cellrockp->cellp;
45 probe = !(cellrockp->flags & CM_FLAG_NOPROBE);
47 /* if this server was previously created by fs setserverprefs */
48 if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB, FALSE))
52 else if (tsp->cellp != cellp) {
53 osi_Log3(afsd_logp, "found a vlserver %s associated with two cells named %s and %s",
54 osi_LogSaveString(afsd_logp,hostnamep),
55 osi_LogSaveString(afsd_logp,tsp->cellp->name),
56 osi_LogSaveString(afsd_logp,cellp->name));
60 tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp, NULL, probe ? 0 : CM_FLAG_NOPROBE);
63 tsp->adminRank = adminRank;
65 /* Insert the vlserver into a sorted list, sorted by server rank */
66 tsrp = cm_NewServerRef(tsp, 0);
67 cm_InsertServerList(&cellp->vlServersp, tsrp);
72 /* if it's from DNS, see if it has expired
73 * and check to make sure we have a valid set of volume servers
74 * this function must not be called with a lock on cm_cellLock
76 cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
80 afs_uint32 mxheld = 0;
85 lock_ObtainMutex(&cp->mx);
88 #ifdef AFS_FREELANCE_CLIENT
89 if (cp->flags & CM_CELLFLAG_FREELANCE) {
90 lock_ReleaseMutex(&cp->mx);
95 if (cm_IsServerListEmpty(cp->vlServersp) ||
96 (time(0) > cp->timeout) ||
98 (cp->flags & CM_CELLFLAG_DNS) &&
99 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID))))
101 lock_ReleaseMutex(&cp->mx);
104 /* must empty cp->vlServersp */
106 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
110 code = cm_SearchCellRegistry(1, cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
111 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
112 code = cm_SearchCellFileEx(cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
114 lock_ObtainMutex(&cp->mx);
116 cp->timeout = time(0) + 7200;
122 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
123 if (code == 0) { /* got cell from DNS */
124 lock_ObtainMutex(&cp->mx);
126 _InterlockedOr(&cp->flags, CM_CELLFLAG_DNS);
127 _InterlockedAnd(&cp->flags, ~CM_CELLFLAG_VLSERVER_INVALID);
128 cp->timeout = time(0) + ttl;
130 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
133 /* if we fail to find it this time, we'll just do nothing and leave the
134 * current entry alone
136 lock_ObtainMutex(&cp->mx);
138 _InterlockedOr(&cp->flags, CM_CELLFLAG_VLSERVER_INVALID);
145 cm_RandomizeServer(&cp->vlServersp);
148 lock_ReleaseMutex(&cp->mx);
150 return code ? NULL : cp;
153 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
154 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
156 return cm_GetCell_Gen(namep, NULL, flags);
159 void cm_FreeCell(cm_cell_t *cellp)
161 lock_AssertWrite(&cm_cellLock);
163 if (cellp->vlServersp)
164 cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
165 cellp->name[0] = '\0';
167 cellp->freeNextp = cm_data.freeCellsp;
168 cm_data.freeCellsp = cellp;
171 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
175 char fullname[CELL_MAXNAMELEN]="";
176 char linkedName[CELL_MAXNAMELEN]="";
177 char name[CELL_MAXNAMELEN]="";
178 int hasWriteLock = 0;
184 if (namep == NULL || !namep[0] || !strcmp(namep,CM_IOCTL_FILENAME_NOSLASH))
188 * Strip off any trailing dots at the end of the cell name.
189 * Failure to do so results in an undesireable alias as the
190 * result of DNS AFSDB record lookups where a trailing dot
191 * has special meaning.
193 strncpy(name, namep, CELL_MAXNAMELEN);
194 for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
201 hash = CM_CELL_NAME_HASH(namep);
203 lock_ObtainRead(&cm_cellLock);
204 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
205 if (cm_stricmp_utf8(namep, cp->name) == 0) {
206 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
207 fullname[CELL_MAXNAMELEN-1] = '\0';
213 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
214 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
215 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
216 fullname[CELL_MAXNAMELEN-1] = '\0';
223 lock_ReleaseRead(&cm_cellLock);
224 cm_UpdateCell(cp, flags);
225 } else if (flags & CM_FLAG_CREATE) {
226 lock_ConvertRToW(&cm_cellLock);
229 /* when we dropped the lock the cell could have been added
230 * to the list so check again while holding the write lock
232 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
233 if (cm_stricmp_utf8(namep, cp->name) == 0) {
234 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
235 fullname[CELL_MAXNAMELEN-1] = '\0';
243 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
244 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
245 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
246 fullname[CELL_MAXNAMELEN-1] = '\0';
252 lock_ReleaseWrite(&cm_cellLock);
253 lock_ObtainMutex(&cp->mx);
254 lock_ObtainWrite(&cm_cellLock);
255 cm_AddCellToNameHashTable(cp);
256 cm_AddCellToIDHashTable(cp);
257 lock_ReleaseMutex(&cp->mx);
261 if ( cm_data.freeCellsp != NULL ) {
262 cp = cm_data.freeCellsp;
263 cm_data.freeCellsp = cp->freeNextp;
266 * The magic, cellID, and mx fields are already set.
269 if ( cm_data.currentCells >= cm_data.maxCells )
270 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
272 /* don't increment currentCells until we know that we
273 * are going to keep this entry
275 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
276 memset(cp, 0, sizeof(cm_cell_t));
277 cp->magic = CM_CELL_MAGIC;
279 /* the cellID cannot be 0 */
280 cp->cellID = ++cm_data.currentCells;
282 /* otherwise we found the cell, and so we're nearly done */
283 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
286 lock_ReleaseWrite(&cm_cellLock);
291 code = cm_SearchCellRegistry(1, namep, fullname, linkedName, cm_AddCellProc, &rock);
292 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
293 code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
295 osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s",
296 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
297 osi_LogSaveString(afsd_logp,linkedName));
302 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
304 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
305 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
306 lock_ObtainMutex(&cp->mx);
307 lock_ObtainWrite(&cm_cellLock);
309 cm_RemoveCellFromIDHashTable(cp);
310 cm_RemoveCellFromNameHashTable(cp);
311 lock_ReleaseMutex(&cp->mx);
315 } else { /* got cell from DNS */
316 lock_ObtainMutex(&cp->mx);
318 _InterlockedOr(&cp->flags, CM_CELLFLAG_DNS);
319 _InterlockedAnd(&cp->flags, ~CM_CELLFLAG_VLSERVER_INVALID);
320 cp->timeout = time(0) + ttl;
325 lock_ObtainMutex(&cp->mx);
326 lock_ObtainWrite(&cm_cellLock);
328 cm_RemoveCellFromIDHashTable(cp);
329 cm_RemoveCellFromNameHashTable(cp);
330 lock_ReleaseMutex(&cp->mx);
336 lock_ObtainMutex(&cp->mx);
338 cp->timeout = time(0) + 7200; /* two hour timeout */
341 /* we have now been given the fullname of the cell. It may
342 * be that we already have a cell with that name. If so,
343 * we should use it instead of completing the allocation
346 lock_ObtainRead(&cm_cellLock);
347 hash = CM_CELL_NAME_HASH(fullname);
348 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
349 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
356 lock_ObtainMutex(&cp->mx);
359 lock_ConvertRToW(&cm_cellLock);
361 cm_RemoveCellFromIDHashTable(cp);
362 cm_RemoveCellFromNameHashTable(cp);
363 lock_ReleaseMutex(&cp->mx);
369 lock_ReleaseRead(&cm_cellLock);
371 /* randomise among those vlservers having the same rank*/
372 cm_RandomizeServer(&cp->vlServersp);
375 lock_ObtainMutex(&cp->mx);
378 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
379 cp->name[CELL_MAXNAMELEN-1] = '\0';
381 strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
382 cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
384 lock_ObtainWrite(&cm_cellLock);
386 cm_AddCellToNameHashTable(cp);
387 cm_AddCellToIDHashTable(cp);
388 lock_ReleaseMutex(&cp->mx);
391 /* append cell to global list */
392 if (cm_data.allCellsp == NULL) {
393 cm_data.allCellsp = cp;
395 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
402 lock_ReleaseRead(&cm_cellLock);
406 lock_ReleaseMutex(&cp->mx);
408 lock_ReleaseWrite(&cm_cellLock);
410 /* fullname is not valid if cp == NULL */
413 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
414 newnamep[CELL_MAXNAMELEN-1]='\0';
420 if (cp && cp->linkedName[0]) {
421 cm_cell_t * linkedCellp = NULL;
423 if (!strcmp(cp->name, cp->linkedName)) {
424 cp->linkedName[0] = '\0';
425 } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
426 linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
428 lock_ObtainWrite(&cm_cellLock);
430 (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
431 cp->linkedName[0] = '\0';
433 strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
434 linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
436 lock_ReleaseWrite(&cm_cellLock);
442 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
447 lock_ObtainRead(&cm_cellLock);
449 hash = CM_CELL_ID_HASH(cellID);
451 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
452 if (cellID == cp->cellID)
455 lock_ReleaseRead(&cm_cellLock);
458 cm_UpdateCell(cp, flags);
464 cm_ValidateCell(void)
467 afs_uint32 count1, count2;
469 for (cellp = cm_data.allCellsp, count1 = 0; cellp; cellp=cellp->allNextp, count1++) {
470 if ( cellp->magic != CM_CELL_MAGIC ) {
471 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
472 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
475 if ( count1 != 0 && cellp == cm_data.allCellsp ||
476 count1 > cm_data.maxCells ) {
477 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
478 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
483 for (cellp = cm_data.freeCellsp, count2 = 0; cellp; cellp=cellp->freeNextp, count2++) {
484 if ( count2 != 0 && cellp == cm_data.freeCellsp ||
485 count2 > cm_data.maxCells ) {
486 afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
487 fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
492 if ( (count1 + count2) != cm_data.currentCells ) {
493 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
494 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
503 cm_ShutdownCell(void)
507 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
508 lock_FinalizeMutex(&cellp->mx);
514 void cm_InitCell(int newFile, long maxCells)
516 static osi_once_t once;
518 if (osi_Once(&once)) {
521 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
524 cm_data.allCellsp = NULL;
525 cm_data.currentCells = 0;
526 cm_data.maxCells = maxCells;
527 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
528 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
530 #ifdef AFS_FREELANCE_CLIENT
531 /* Generate a dummy entry for the Freelance cell whether or not
532 * freelance mode is being used in this session
535 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
536 memset(cellp, 0, sizeof(cm_cell_t));
537 cellp->magic = CM_CELL_MAGIC;
539 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
541 lock_ObtainMutex(&cellp->mx);
542 lock_ObtainWrite(&cm_cellLock);
545 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
546 cellp->name[CELL_MAXNAMELEN-1] = '\0';
548 /* thread on global list */
549 cellp->allNextp = cm_data.allCellsp;
550 cm_data.allCellsp = cellp;
552 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
553 cellp->vlServersp = NULL;
554 _InterlockedOr(&cellp->flags, CM_CELLFLAG_FREELANCE);
556 cm_AddCellToNameHashTable(cellp);
557 cm_AddCellToIDHashTable(cellp);
558 lock_ReleaseWrite(&cm_cellLock);
559 lock_ReleaseMutex(&cellp->mx);
562 lock_ObtainRead(&cm_cellLock);
563 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
564 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
565 cellp->vlServersp = NULL;
566 _InterlockedOr(&cellp->flags, CM_CELLFLAG_VLSERVER_INVALID);
568 lock_ReleaseRead(&cm_cellLock);
575 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
580 cp = tsp->cellp; /* cell that this vlserver belongs to */
582 lock_ObtainMutex(&cp->mx);
583 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
585 if ( !code ) /* if the server list was rearranged */
586 cm_RandomizeServer(&cp->vlServersp);
588 lock_ReleaseMutex(&cp->mx);
592 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
599 lock_ObtainRead(&cm_cellLock);
601 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
602 cookie, cm_data.currentCells, cm_data.maxCells);
603 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
605 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
606 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
607 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
608 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
611 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
612 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
615 lock_ReleaseRead(&cm_cellLock);
620 /* call with volume write-locked and mutex held */
621 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
625 lock_AssertWrite(&cm_cellLock);
626 lock_AssertMutex(&cellp->mx);
628 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
631 i = CM_CELL_NAME_HASH(cellp->name);
633 cellp->nameNextp = cm_data.cellNameHashTablep[i];
634 cm_data.cellNameHashTablep[i] = cellp;
635 _InterlockedOr(&cellp->flags, CM_CELLFLAG_IN_NAMEHASH);
638 /* call with cell write-locked and mutex held */
639 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
645 lock_AssertWrite(&cm_cellLock);
646 lock_AssertMutex(&cellp->mx);
648 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
649 /* hash it out first */
650 i = CM_CELL_NAME_HASH(cellp->name);
651 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
653 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
654 if (tcellp == cellp) {
655 *lcellpp = cellp->nameNextp;
656 _InterlockedAnd(&cellp->flags, ~CM_CELLFLAG_IN_NAMEHASH);
657 cellp->nameNextp = NULL;
664 /* call with cell write-locked and mutex held */
665 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
669 lock_AssertWrite(&cm_cellLock);
670 lock_AssertMutex(&cellp->mx);
672 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
675 i = CM_CELL_ID_HASH(cellp->cellID);
677 cellp->idNextp = cm_data.cellIDHashTablep[i];
678 cm_data.cellIDHashTablep[i] = cellp;
679 _InterlockedOr(&cellp->flags, CM_CELLFLAG_IN_IDHASH);
682 /* call with cell write-locked and mutex held */
683 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
689 lock_AssertWrite(&cm_cellLock);
690 lock_AssertMutex(&cellp->mx);
692 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
693 /* hash it out first */
694 i = CM_CELL_ID_HASH(cellp->cellID);
695 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
697 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
698 if (tcellp == cellp) {
699 *lcellpp = cellp->idNextp;
700 _InterlockedAnd(&cellp->flags, ~CM_CELLFLAG_IN_IDHASH);
701 cellp->idNextp = NULL;
709 cm_CreateCellWithInfo( char * cellname,
710 char * linked_cellname,
711 unsigned short vlport,
712 afs_uint32 host_count,
719 struct sockaddr_in vlSockAddr;
722 rock.cellp = cm_GetCell(cellname, CM_FLAG_CREATE | CM_FLAG_NOPROBE);
725 cm_FreeServerList(&rock.cellp->vlServersp, CM_FREESERVERLIST_DELETE);
727 if (!(flags & CM_CELLFLAG_DNS)) {
728 for (i = 0; i < host_count; i++) {
729 thp = gethostbyname(hostname[i]);
732 for (j=0 ; thp->h_addr_list[j]; j++) {
733 if (thp->h_addrtype != AF_INET)
735 memcpy(&vlSockAddr.sin_addr.s_addr,
738 vlSockAddr.sin_port = htons(vlport ? vlport : 7003);
739 vlSockAddr.sin_family = AF_INET;
740 cm_AddCellProc(&rock, &vlSockAddr, hostname[i], CM_FLAG_NOPROBE);
744 lock_ObtainMutex(&rock.cellp->mx);
745 _InterlockedAnd(&rock.cellp->flags, ~CM_CELLFLAG_DNS);
746 } else if (cm_dnsEnabled) {
749 code = cm_SearchCellByDNS(rock.cellp->name, NULL, &ttl, cm_AddCellProc, &rock);
750 lock_ObtainMutex(&rock.cellp->mx);
751 if (code == 0) { /* got cell from DNS */
752 _InterlockedOr(&rock.cellp->flags, CM_CELLFLAG_DNS);
753 rock.cellp->timeout = time(0) + ttl;
755 fprintf(stderr, "cell %s: ttl=%d\n", rock.cellp->name, ttl);
759 lock_ObtainMutex(&rock.cellp->mx);
760 rock.cellp->flags &= ~CM_CELLFLAG_DNS;
762 _InterlockedOr(&rock.cellp->flags, CM_CELLFLAG_VLSERVER_INVALID);
763 StringCbCopy(rock.cellp->linkedName, CELL_MAXNAMELEN, linked_cellname);
764 lock_ReleaseMutex(&rock.cellp->mx);
766 if (rock.cellp->vlServersp)
767 cm_RandomizeServer(&rock.cellp->vlServersp);