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;
185 if (namep == NULL || !namep[0] || !strcmp(namep,CM_IOCTL_FILENAME_NOSLASH))
189 * Strip off any trailing dots at the end of the cell name.
190 * Failure to do so results in an undesireable alias as the
191 * result of DNS AFSDB record lookups where a trailing dot
192 * has special meaning.
194 strncpy(name, namep, CELL_MAXNAMELEN);
195 for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
202 hash = CM_CELL_NAME_HASH(namep);
204 lock_ObtainRead(&cm_cellLock);
205 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
206 if (cm_stricmp_utf8(namep, cp->name) == 0) {
207 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
208 fullname[CELL_MAXNAMELEN-1] = '\0';
214 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
215 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
216 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
217 fullname[CELL_MAXNAMELEN-1] = '\0';
224 lock_ReleaseRead(&cm_cellLock);
225 cm_UpdateCell(cp, flags);
226 } else if (flags & CM_FLAG_CREATE) {
227 lock_ConvertRToW(&cm_cellLock);
230 /* when we dropped the lock the cell could have been added
231 * to the list so check again while holding the write lock
233 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
234 if (cm_stricmp_utf8(namep, cp->name) == 0) {
235 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
236 fullname[CELL_MAXNAMELEN-1] = '\0';
244 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
245 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
246 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
247 fullname[CELL_MAXNAMELEN-1] = '\0';
253 lock_ReleaseWrite(&cm_cellLock);
254 lock_ObtainMutex(&cp->mx);
255 lock_ObtainWrite(&cm_cellLock);
256 cm_AddCellToNameHashTable(cp);
257 cm_AddCellToIDHashTable(cp);
258 lock_ReleaseMutex(&cp->mx);
262 if ( cm_data.freeCellsp != NULL ) {
263 cp = cm_data.freeCellsp;
264 cm_data.freeCellsp = cp->freeNextp;
267 * The magic, cellID, and mx fields are already set.
270 if ( cm_data.currentCells >= cm_data.maxCells )
271 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
274 * the cellID cannot be 0.
275 * If there is a name collision, one of the entries
276 * will end up on cm_data.freeCellsp for reuse.
278 cellID = InterlockedIncrement(&cm_data.currentCells);
279 cp = &cm_data.cellBaseAddress[cellID - 1];
280 memset(cp, 0, sizeof(cm_cell_t));
281 cp->magic = CM_CELL_MAGIC;
284 /* otherwise we found the cell, and so we're nearly done */
285 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
288 lock_ReleaseWrite(&cm_cellLock);
293 code = cm_SearchCellRegistry(1, namep, fullname, linkedName, cm_AddCellProc, &rock);
294 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
295 code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
297 osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s",
298 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
299 osi_LogSaveString(afsd_logp,linkedName));
304 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
306 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
307 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
308 lock_ObtainMutex(&cp->mx);
309 lock_ObtainWrite(&cm_cellLock);
311 cm_RemoveCellFromIDHashTable(cp);
312 cm_RemoveCellFromNameHashTable(cp);
313 lock_ReleaseMutex(&cp->mx);
317 } else { /* got cell from DNS */
318 lock_ObtainMutex(&cp->mx);
320 _InterlockedOr(&cp->flags, CM_CELLFLAG_DNS);
321 _InterlockedAnd(&cp->flags, ~CM_CELLFLAG_VLSERVER_INVALID);
322 cp->timeout = time(0) + ttl;
327 lock_ObtainMutex(&cp->mx);
328 lock_ObtainWrite(&cm_cellLock);
330 cm_RemoveCellFromIDHashTable(cp);
331 cm_RemoveCellFromNameHashTable(cp);
332 lock_ReleaseMutex(&cp->mx);
338 lock_ObtainMutex(&cp->mx);
340 cp->timeout = time(0) + 7200; /* two hour timeout */
343 /* we have now been given the fullname of the cell. It may
344 * be that we already have a cell with that name. If so,
345 * we should use it instead of completing the allocation
348 lock_ObtainRead(&cm_cellLock);
349 hash = CM_CELL_NAME_HASH(fullname);
350 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
351 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
358 lock_ObtainMutex(&cp->mx);
361 lock_ConvertRToW(&cm_cellLock);
363 cm_RemoveCellFromIDHashTable(cp);
364 cm_RemoveCellFromNameHashTable(cp);
365 lock_ReleaseMutex(&cp->mx);
371 lock_ReleaseRead(&cm_cellLock);
373 /* randomise among those vlservers having the same rank*/
374 cm_RandomizeServer(&cp->vlServersp);
377 lock_ObtainMutex(&cp->mx);
380 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
381 cp->name[CELL_MAXNAMELEN-1] = '\0';
383 strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
384 cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
386 lock_ObtainWrite(&cm_cellLock);
388 cm_AddCellToNameHashTable(cp);
389 cm_AddCellToIDHashTable(cp);
390 lock_ReleaseMutex(&cp->mx);
393 /* append cell to global list */
394 if (cm_data.allCellsp == NULL) {
395 cm_data.allCellsp = cp;
397 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
404 lock_ReleaseRead(&cm_cellLock);
408 lock_ReleaseMutex(&cp->mx);
410 lock_ReleaseWrite(&cm_cellLock);
412 /* fullname is not valid if cp == NULL */
415 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
416 newnamep[CELL_MAXNAMELEN-1]='\0';
422 if (cp && cp->linkedName[0]) {
423 cm_cell_t * linkedCellp = NULL;
425 if (!strcmp(cp->name, cp->linkedName)) {
426 cp->linkedName[0] = '\0';
427 } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
428 linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
430 lock_ObtainWrite(&cm_cellLock);
432 (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
433 cp->linkedName[0] = '\0';
435 strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
436 linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
438 lock_ReleaseWrite(&cm_cellLock);
444 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
449 lock_ObtainRead(&cm_cellLock);
451 hash = CM_CELL_ID_HASH(cellID);
453 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
454 if (cellID == cp->cellID)
457 lock_ReleaseRead(&cm_cellLock);
460 cm_UpdateCell(cp, flags);
466 cm_ValidateCell(void)
469 afs_uint32 count1, count2;
471 for (cellp = cm_data.allCellsp, count1 = 0; cellp; cellp=cellp->allNextp, count1++) {
472 if ( cellp->magic != CM_CELL_MAGIC ) {
473 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
474 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
477 if ( count1 != 0 && cellp == cm_data.allCellsp ||
478 count1 > cm_data.maxCells ) {
479 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
480 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
485 for (cellp = cm_data.freeCellsp, count2 = 0; cellp; cellp=cellp->freeNextp, count2++) {
486 if ( count2 != 0 && cellp == cm_data.freeCellsp ||
487 count2 > cm_data.maxCells ) {
488 afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
489 fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
494 if ( (count1 + count2) != cm_data.currentCells ) {
495 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
496 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
505 cm_ShutdownCell(void)
509 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
510 lock_FinalizeMutex(&cellp->mx);
516 void cm_InitCell(int newFile, long maxCells)
518 static osi_once_t once;
520 if (osi_Once(&once)) {
523 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
526 cm_data.allCellsp = NULL;
527 cm_data.currentCells = 0;
528 cm_data.maxCells = maxCells;
529 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
530 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
532 #ifdef AFS_FREELANCE_CLIENT
533 /* Generate a dummy entry for the Freelance cell whether or not
534 * freelance mode is being used in this session
537 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
538 memset(cellp, 0, sizeof(cm_cell_t));
539 cellp->magic = CM_CELL_MAGIC;
541 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
543 lock_ObtainMutex(&cellp->mx);
544 lock_ObtainWrite(&cm_cellLock);
547 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
548 cellp->name[CELL_MAXNAMELEN-1] = '\0';
550 /* thread on global list */
551 cellp->allNextp = cm_data.allCellsp;
552 cm_data.allCellsp = cellp;
554 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
555 cellp->vlServersp = NULL;
556 _InterlockedOr(&cellp->flags, CM_CELLFLAG_FREELANCE);
558 cm_AddCellToNameHashTable(cellp);
559 cm_AddCellToIDHashTable(cellp);
560 lock_ReleaseWrite(&cm_cellLock);
561 lock_ReleaseMutex(&cellp->mx);
564 lock_ObtainRead(&cm_cellLock);
565 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
566 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
567 cellp->vlServersp = NULL;
568 _InterlockedOr(&cellp->flags, CM_CELLFLAG_VLSERVER_INVALID);
570 lock_ReleaseRead(&cm_cellLock);
577 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
582 cp = tsp->cellp; /* cell that this vlserver belongs to */
584 lock_ObtainMutex(&cp->mx);
585 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
587 if ( !code ) /* if the server list was rearranged */
588 cm_RandomizeServer(&cp->vlServersp);
590 lock_ReleaseMutex(&cp->mx);
594 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
601 lock_ObtainRead(&cm_cellLock);
603 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
604 cookie, cm_data.currentCells, cm_data.maxCells);
605 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
607 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
608 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
609 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
610 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
613 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
614 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
617 lock_ReleaseRead(&cm_cellLock);
622 /* call with volume write-locked and mutex held */
623 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
627 lock_AssertWrite(&cm_cellLock);
628 lock_AssertMutex(&cellp->mx);
630 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
633 i = CM_CELL_NAME_HASH(cellp->name);
635 cellp->nameNextp = cm_data.cellNameHashTablep[i];
636 cm_data.cellNameHashTablep[i] = cellp;
637 _InterlockedOr(&cellp->flags, CM_CELLFLAG_IN_NAMEHASH);
640 /* call with cell write-locked and mutex held */
641 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
647 lock_AssertWrite(&cm_cellLock);
648 lock_AssertMutex(&cellp->mx);
650 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
651 /* hash it out first */
652 i = CM_CELL_NAME_HASH(cellp->name);
653 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
655 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
656 if (tcellp == cellp) {
657 *lcellpp = cellp->nameNextp;
658 _InterlockedAnd(&cellp->flags, ~CM_CELLFLAG_IN_NAMEHASH);
659 cellp->nameNextp = NULL;
666 /* call with cell write-locked and mutex held */
667 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
671 lock_AssertWrite(&cm_cellLock);
672 lock_AssertMutex(&cellp->mx);
674 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
677 i = CM_CELL_ID_HASH(cellp->cellID);
679 cellp->idNextp = cm_data.cellIDHashTablep[i];
680 cm_data.cellIDHashTablep[i] = cellp;
681 _InterlockedOr(&cellp->flags, CM_CELLFLAG_IN_IDHASH);
684 /* call with cell write-locked and mutex held */
685 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
691 lock_AssertWrite(&cm_cellLock);
692 lock_AssertMutex(&cellp->mx);
694 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
695 /* hash it out first */
696 i = CM_CELL_ID_HASH(cellp->cellID);
697 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
699 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
700 if (tcellp == cellp) {
701 *lcellpp = cellp->idNextp;
702 _InterlockedAnd(&cellp->flags, ~CM_CELLFLAG_IN_IDHASH);
703 cellp->idNextp = NULL;
711 cm_CreateCellWithInfo( char * cellname,
712 char * linked_cellname,
713 unsigned short vlport,
714 afs_uint32 host_count,
721 struct sockaddr_in vlSockAddr;
724 rock.cellp = cm_GetCell(cellname, CM_FLAG_CREATE | CM_FLAG_NOPROBE);
727 cm_FreeServerList(&rock.cellp->vlServersp, CM_FREESERVERLIST_DELETE);
729 if (!(flags & CM_CELLFLAG_DNS)) {
730 for (i = 0; i < host_count; i++) {
731 thp = gethostbyname(hostname[i]);
734 for (j=0 ; thp->h_addr_list[j]; j++) {
735 if (thp->h_addrtype != AF_INET)
737 memcpy(&vlSockAddr.sin_addr.s_addr,
740 vlSockAddr.sin_port = htons(vlport ? vlport : 7003);
741 vlSockAddr.sin_family = AF_INET;
742 cm_AddCellProc(&rock, &vlSockAddr, hostname[i], CM_FLAG_NOPROBE);
746 lock_ObtainMutex(&rock.cellp->mx);
747 _InterlockedAnd(&rock.cellp->flags, ~CM_CELLFLAG_DNS);
748 } else if (cm_dnsEnabled) {
751 code = cm_SearchCellByDNS(rock.cellp->name, NULL, &ttl, cm_AddCellProc, &rock);
752 lock_ObtainMutex(&rock.cellp->mx);
753 if (code == 0) { /* got cell from DNS */
754 _InterlockedOr(&rock.cellp->flags, CM_CELLFLAG_DNS);
755 rock.cellp->timeout = time(0) + ttl;
757 fprintf(stderr, "cell %s: ttl=%d\n", rock.cellp->name, ttl);
761 lock_ObtainMutex(&rock.cellp->mx);
762 rock.cellp->flags &= ~CM_CELLFLAG_DNS;
764 _InterlockedOr(&rock.cellp->flags, CM_CELLFLAG_VLSERVER_INVALID);
765 StringCbCopy(rock.cellp->linkedName, CELL_MAXNAMELEN, linked_cellname);
766 lock_ReleaseMutex(&rock.cellp->mx);
768 if (rock.cellp->vlServersp)
769 cm_RandomizeServer(&rock.cellp->vlServersp);