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 ipRank)
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);
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);
87 if ((cp->vlServersp == NULL
88 #ifdef AFS_FREELANCE_CLIENT
89 && !(cp->flags & CM_CELLFLAG_FREELANCE)
91 ) || (time(0) > cp->timeout)
92 || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
93 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
96 lock_ReleaseMutex(&cp->mx);
99 /* must empty cp->vlServersp */
101 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
105 code = cm_SearchCellRegistry(1, cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
106 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
107 code = cm_SearchCellFileEx(cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
109 lock_ObtainMutex(&cp->mx);
111 cp->timeout = time(0) + 7200;
117 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
118 if (code == 0) { /* got cell from DNS */
119 lock_ObtainMutex(&cp->mx);
121 _InterlockedOr(&cp->flags, CM_CELLFLAG_DNS);
122 _InterlockedAnd(&cp->flags, ~CM_CELLFLAG_VLSERVER_INVALID);
123 cp->timeout = time(0) + ttl;
125 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
128 /* if we fail to find it this time, we'll just do nothing and leave the
129 * current entry alone
131 lock_ObtainMutex(&cp->mx);
133 _InterlockedOr(&cp->flags, CM_CELLFLAG_VLSERVER_INVALID);
140 cm_RandomizeServer(&cp->vlServersp);
143 lock_ReleaseMutex(&cp->mx);
145 return code ? NULL : cp;
148 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
149 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
151 return cm_GetCell_Gen(namep, NULL, flags);
154 void cm_FreeCell(cm_cell_t *cellp)
156 lock_AssertWrite(&cm_cellLock);
158 if (cellp->vlServersp)
159 cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
160 cellp->name[0] = '\0';
162 cellp->freeNextp = cm_data.freeCellsp;
163 cm_data.freeCellsp = cellp;
166 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
170 char fullname[CELL_MAXNAMELEN]="";
171 char linkedName[CELL_MAXNAMELEN]="";
172 char name[CELL_MAXNAMELEN]="";
173 int hasWriteLock = 0;
179 if (namep == NULL || !namep[0] || !strcmp(namep,CM_IOCTL_FILENAME_NOSLASH))
183 * Strip off any trailing dots at the end of the cell name.
184 * Failure to do so results in an undesireable alias as the
185 * result of DNS AFSDB record lookups where a trailing dot
186 * has special meaning.
188 strncpy(name, namep, CELL_MAXNAMELEN);
189 for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
196 hash = CM_CELL_NAME_HASH(namep);
198 lock_ObtainRead(&cm_cellLock);
199 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
200 if (cm_stricmp_utf8(namep, cp->name) == 0) {
201 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
202 fullname[CELL_MAXNAMELEN-1] = '\0';
208 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
209 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
210 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
211 fullname[CELL_MAXNAMELEN-1] = '\0';
218 lock_ReleaseRead(&cm_cellLock);
219 cm_UpdateCell(cp, flags);
220 } else if (flags & CM_FLAG_CREATE) {
221 lock_ConvertRToW(&cm_cellLock);
224 /* when we dropped the lock the cell could have been added
225 * to the list so check again while holding the write lock
227 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
228 if (cm_stricmp_utf8(namep, cp->name) == 0) {
229 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
230 fullname[CELL_MAXNAMELEN-1] = '\0';
238 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
239 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
240 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
241 fullname[CELL_MAXNAMELEN-1] = '\0';
247 lock_ReleaseWrite(&cm_cellLock);
248 lock_ObtainMutex(&cp->mx);
249 lock_ObtainWrite(&cm_cellLock);
250 cm_AddCellToNameHashTable(cp);
251 cm_AddCellToIDHashTable(cp);
252 lock_ReleaseMutex(&cp->mx);
256 if ( cm_data.freeCellsp != NULL ) {
257 cp = cm_data.freeCellsp;
258 cm_data.freeCellsp = cp->freeNextp;
261 * The magic, cellID, and mx fields are already set.
264 if ( cm_data.currentCells >= cm_data.maxCells )
265 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
267 /* don't increment currentCells until we know that we
268 * are going to keep this entry
270 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
271 memset(cp, 0, sizeof(cm_cell_t));
272 cp->magic = CM_CELL_MAGIC;
274 /* the cellID cannot be 0 */
275 cp->cellID = ++cm_data.currentCells;
277 /* otherwise we found the cell, and so we're nearly done */
278 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
281 lock_ReleaseWrite(&cm_cellLock);
286 code = cm_SearchCellRegistry(1, namep, fullname, linkedName, cm_AddCellProc, &rock);
287 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
288 code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
290 osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s",
291 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
292 osi_LogSaveString(afsd_logp,linkedName));
297 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
299 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
300 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
301 lock_ObtainMutex(&cp->mx);
302 lock_ObtainWrite(&cm_cellLock);
304 cm_RemoveCellFromIDHashTable(cp);
305 cm_RemoveCellFromNameHashTable(cp);
306 lock_ReleaseMutex(&cp->mx);
310 } else { /* got cell from DNS */
311 lock_ObtainMutex(&cp->mx);
313 _InterlockedOr(&cp->flags, CM_CELLFLAG_DNS);
314 _InterlockedAnd(&cp->flags, ~CM_CELLFLAG_VLSERVER_INVALID);
315 cp->timeout = time(0) + ttl;
320 lock_ObtainMutex(&cp->mx);
321 lock_ObtainWrite(&cm_cellLock);
323 cm_RemoveCellFromIDHashTable(cp);
324 cm_RemoveCellFromNameHashTable(cp);
325 lock_ReleaseMutex(&cp->mx);
331 lock_ObtainMutex(&cp->mx);
333 cp->timeout = time(0) + 7200; /* two hour timeout */
336 /* we have now been given the fullname of the cell. It may
337 * be that we already have a cell with that name. If so,
338 * we should use it instead of completing the allocation
341 lock_ObtainRead(&cm_cellLock);
342 hash = CM_CELL_NAME_HASH(fullname);
343 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
344 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
351 lock_ObtainMutex(&cp->mx);
354 lock_ConvertRToW(&cm_cellLock);
356 cm_RemoveCellFromIDHashTable(cp);
357 cm_RemoveCellFromNameHashTable(cp);
358 lock_ReleaseMutex(&cp->mx);
364 lock_ReleaseRead(&cm_cellLock);
366 /* randomise among those vlservers having the same rank*/
367 cm_RandomizeServer(&cp->vlServersp);
370 lock_ObtainMutex(&cp->mx);
373 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
374 cp->name[CELL_MAXNAMELEN-1] = '\0';
376 strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
377 cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
379 lock_ObtainWrite(&cm_cellLock);
381 cm_AddCellToNameHashTable(cp);
382 cm_AddCellToIDHashTable(cp);
383 lock_ReleaseMutex(&cp->mx);
386 /* append cell to global list */
387 if (cm_data.allCellsp == NULL) {
388 cm_data.allCellsp = cp;
390 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
397 lock_ReleaseRead(&cm_cellLock);
401 lock_ReleaseMutex(&cp->mx);
403 lock_ReleaseWrite(&cm_cellLock);
405 /* fullname is not valid if cp == NULL */
408 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
409 newnamep[CELL_MAXNAMELEN-1]='\0';
415 if (cp && cp->linkedName[0]) {
416 cm_cell_t * linkedCellp = NULL;
418 if (!strcmp(cp->name, cp->linkedName)) {
419 cp->linkedName[0] = '\0';
420 } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
421 linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
423 lock_ObtainWrite(&cm_cellLock);
425 (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
426 cp->linkedName[0] = '\0';
428 strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
429 linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
431 lock_ReleaseWrite(&cm_cellLock);
437 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
442 lock_ObtainRead(&cm_cellLock);
444 hash = CM_CELL_ID_HASH(cellID);
446 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
447 if (cellID == cp->cellID)
450 lock_ReleaseRead(&cm_cellLock);
453 cm_UpdateCell(cp, flags);
459 cm_ValidateCell(void)
462 afs_uint32 count1, count2;
464 for (cellp = cm_data.allCellsp, count1 = 0; cellp; cellp=cellp->allNextp, count1++) {
465 if ( cellp->magic != CM_CELL_MAGIC ) {
466 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
467 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
470 if ( count1 != 0 && cellp == cm_data.allCellsp ||
471 count1 > cm_data.maxCells ) {
472 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
473 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
478 for (cellp = cm_data.freeCellsp, count2 = 0; cellp; cellp=cellp->freeNextp, count2++) {
479 if ( count2 != 0 && cellp == cm_data.freeCellsp ||
480 count2 > cm_data.maxCells ) {
481 afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
482 fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
487 if ( (count1 + count2) != cm_data.currentCells ) {
488 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
489 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
498 cm_ShutdownCell(void)
502 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
503 lock_FinalizeMutex(&cellp->mx);
509 void cm_InitCell(int newFile, long maxCells)
511 static osi_once_t once;
513 if (osi_Once(&once)) {
516 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
519 cm_data.allCellsp = NULL;
520 cm_data.currentCells = 0;
521 cm_data.maxCells = maxCells;
522 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
523 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
525 #ifdef AFS_FREELANCE_CLIENT
526 /* Generate a dummy entry for the Freelance cell whether or not
527 * freelance mode is being used in this session
530 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
531 memset(cellp, 0, sizeof(cm_cell_t));
532 cellp->magic = CM_CELL_MAGIC;
534 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
536 lock_ObtainMutex(&cellp->mx);
537 lock_ObtainWrite(&cm_cellLock);
540 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
541 cellp->name[CELL_MAXNAMELEN-1] = '\0';
543 /* thread on global list */
544 cellp->allNextp = cm_data.allCellsp;
545 cm_data.allCellsp = cellp;
547 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
548 cellp->vlServersp = NULL;
549 _InterlockedOr(&cellp->flags, CM_CELLFLAG_FREELANCE);
551 cm_AddCellToNameHashTable(cellp);
552 cm_AddCellToIDHashTable(cellp);
553 lock_ReleaseWrite(&cm_cellLock);
554 lock_ReleaseMutex(&cellp->mx);
557 lock_ObtainRead(&cm_cellLock);
558 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
559 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
560 cellp->vlServersp = NULL;
561 _InterlockedOr(&cellp->flags, CM_CELLFLAG_VLSERVER_INVALID);
563 lock_ReleaseRead(&cm_cellLock);
570 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
575 cp = tsp->cellp; /* cell that this vlserver belongs to */
577 lock_ObtainMutex(&cp->mx);
578 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
580 if ( !code ) /* if the server list was rearranged */
581 cm_RandomizeServer(&cp->vlServersp);
583 lock_ReleaseMutex(&cp->mx);
587 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
594 lock_ObtainRead(&cm_cellLock);
596 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
597 cookie, cm_data.currentCells, cm_data.maxCells);
598 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
600 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
601 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
602 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
603 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
606 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
607 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
610 lock_ReleaseRead(&cm_cellLock);
615 /* call with volume write-locked and mutex held */
616 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
620 lock_AssertWrite(&cm_cellLock);
621 lock_AssertMutex(&cellp->mx);
623 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
626 i = CM_CELL_NAME_HASH(cellp->name);
628 cellp->nameNextp = cm_data.cellNameHashTablep[i];
629 cm_data.cellNameHashTablep[i] = cellp;
630 _InterlockedOr(&cellp->flags, CM_CELLFLAG_IN_NAMEHASH);
633 /* call with cell write-locked and mutex held */
634 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
640 lock_AssertWrite(&cm_cellLock);
641 lock_AssertMutex(&cellp->mx);
643 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
644 /* hash it out first */
645 i = CM_CELL_NAME_HASH(cellp->name);
646 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
648 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
649 if (tcellp == cellp) {
650 *lcellpp = cellp->nameNextp;
651 _InterlockedAnd(&cellp->flags, ~CM_CELLFLAG_IN_NAMEHASH);
652 cellp->nameNextp = NULL;
659 /* call with cell write-locked and mutex held */
660 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
664 lock_AssertWrite(&cm_cellLock);
665 lock_AssertMutex(&cellp->mx);
667 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
670 i = CM_CELL_ID_HASH(cellp->cellID);
672 cellp->idNextp = cm_data.cellIDHashTablep[i];
673 cm_data.cellIDHashTablep[i] = cellp;
674 _InterlockedOr(&cellp->flags, CM_CELLFLAG_IN_IDHASH);
677 /* call with cell write-locked and mutex held */
678 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
684 lock_AssertWrite(&cm_cellLock);
685 lock_AssertMutex(&cellp->mx);
687 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
688 /* hash it out first */
689 i = CM_CELL_ID_HASH(cellp->cellID);
690 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
692 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
693 if (tcellp == cellp) {
694 *lcellpp = cellp->idNextp;
695 _InterlockedAnd(&cellp->flags, ~CM_CELLFLAG_IN_IDHASH);
696 cellp->idNextp = NULL;
704 cm_CreateCellWithInfo( char * cellname,
705 char * linked_cellname,
706 unsigned short vlport,
707 afs_uint32 host_count,
714 struct sockaddr_in vlSockAddr;
717 rock.cellp = cm_GetCell(cellname, CM_FLAG_CREATE | CM_FLAG_NOPROBE);
720 cm_FreeServerList(&rock.cellp->vlServersp, CM_FREESERVERLIST_DELETE);
722 if (!(flags & CM_CELLFLAG_DNS)) {
723 for (i = 0; i < host_count; i++) {
724 thp = gethostbyname(hostname[i]);
727 for (j=0 ; thp->h_addr_list[j]; j++) {
728 if (thp->h_addrtype != AF_INET)
730 memcpy(&vlSockAddr.sin_addr.s_addr,
733 vlSockAddr.sin_port = htons(vlport ? vlport : 7003);
734 vlSockAddr.sin_family = AF_INET;
735 cm_AddCellProc(&rock, &vlSockAddr, hostname[i], CM_FLAG_NOPROBE);
739 lock_ObtainMutex(&rock.cellp->mx);
740 _InterlockedAnd(&rock.cellp->flags, ~CM_CELLFLAG_DNS);
741 } else if (cm_dnsEnabled) {
744 code = cm_SearchCellByDNS(rock.cellp->name, NULL, &ttl, cm_AddCellProc, &rock);
745 lock_ObtainMutex(&rock.cellp->mx);
746 if (code == 0) { /* got cell from DNS */
747 _InterlockedOr(&rock.cellp->flags, CM_CELLFLAG_DNS);
748 rock.cellp->timeout = time(0) + ttl;
750 fprintf(stderr, "cell %s: ttl=%d\n", rock.cellp->name, ttl);
754 lock_ObtainMutex(&rock.cellp->mx);
755 rock.cellp->flags &= ~CM_CELLFLAG_DNS;
757 _InterlockedOr(&rock.cellp->flags, CM_CELLFLAG_VLSERVER_INVALID);
758 StringCbCopy(rock.cellp->linkedName, CELL_MAXNAMELEN, linked_cellname);
759 lock_ReleaseMutex(&rock.cellp->mx);
761 if (rock.cellp->vlServersp)
762 cm_RandomizeServer(&rock.cellp->vlServersp);