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 <afs/param.h>
24 osi_rwlock_t cm_cellLock;
26 /* function called as callback proc from cm_SearchCellFile. Return 0 to
27 * continue processing.
29 * At the present time the return value is ignored by the caller.
31 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep, unsigned short ipRank)
36 cm_cell_rock_t *cellrockp = (cm_cell_rock_t *)rockp;
39 cellp = cellrockp->cellp;
40 probe = !(cellrockp->flags & CM_FLAG_NOPROBE);
42 /* if this server was previously created by fs setserverprefs */
43 if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
47 else if (tsp->cellp != cellp) {
48 osi_Log3(afsd_logp, "found a vlserver %s associated with two cells named %s and %s",
49 osi_LogSaveString(afsd_logp,hostnamep),
50 osi_LogSaveString(afsd_logp,tsp->cellp->name),
51 osi_LogSaveString(afsd_logp,cellp->name));
55 tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp, NULL, probe ? 0 : CM_FLAG_NOPROBE);
59 /* Insert the vlserver into a sorted list, sorted by server rank */
60 tsrp = cm_NewServerRef(tsp, 0);
61 cm_InsertServerList(&cellp->vlServersp, tsrp);
62 /* drop the allocation reference */
63 lock_ObtainWrite(&cm_serverLock);
65 lock_ReleaseWrite(&cm_serverLock);
70 /* if it's from DNS, see if it has expired
71 * and check to make sure we have a valid set of volume servers
72 * this function must not be called with a lock on cm_cellLock
74 cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
82 lock_ObtainMutex(&cp->mx);
83 if ((cp->vlServersp == NULL
84 #ifdef AFS_FREELANCE_CLIENT
85 && !(cp->flags & CM_CELLFLAG_FREELANCE)
87 ) || (time(0) > cp->timeout)
89 || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
90 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
94 lock_ReleaseMutex(&cp->mx);
96 /* must empty cp->vlServersp */
98 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
99 cp->vlServersp = NULL;
104 code = cm_SearchCellRegistry(1, cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
105 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
106 code = cm_SearchCellFileEx(cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
108 lock_ObtainMutex(&cp->mx);
109 cp->timeout = time(0) + 7200;
110 lock_ReleaseMutex(&cp->mx);
117 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
118 if (code == 0) { /* got cell from DNS */
119 lock_ObtainMutex(&cp->mx);
120 cp->flags |= CM_CELLFLAG_DNS;
121 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
122 cp->timeout = time(0) + ttl;
123 lock_ReleaseMutex(&cp->mx);
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);
132 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
133 lock_ReleaseMutex(&cp->mx);
137 #endif /* AFS_AFSDB_ENV */
139 lock_ReleaseMutex(&cp->mx);
141 return code ? NULL : cp;
144 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
145 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
147 return cm_GetCell_Gen(namep, NULL, flags);
150 void cm_FreeCell(cm_cell_t *cellp)
152 lock_AssertWrite(&cm_cellLock);
154 if (cellp->vlServersp)
155 cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
156 cellp->name[0] = '\0';
158 cellp->freeNextp = cm_data.freeCellsp;
159 cm_data.freeCellsp = cellp;
162 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
166 char fullname[CELL_MAXNAMELEN]="";
167 char linkedName[CELL_MAXNAMELEN]="";
168 char name[CELL_MAXNAMELEN]="";
169 int hasWriteLock = 0;
175 if (namep == NULL || !namep[0] || !strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
179 * Strip off any trailing dots at the end of the cell name.
180 * Failure to do so results in an undesireable alias as the
181 * result of DNS AFSDB record lookups where a trailing dot
182 * has special meaning.
184 strncpy(name, namep, CELL_MAXNAMELEN);
185 for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
192 hash = CM_CELL_NAME_HASH(namep);
194 lock_ObtainRead(&cm_cellLock);
195 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
196 if (cm_stricmp_utf8(namep, cp->name) == 0) {
197 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
198 fullname[CELL_MAXNAMELEN-1] = '\0';
204 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
205 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
206 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
207 fullname[CELL_MAXNAMELEN-1] = '\0';
214 lock_ReleaseRead(&cm_cellLock);
215 cm_UpdateCell(cp, flags);
216 } else if (flags & CM_FLAG_CREATE) {
217 lock_ConvertRToW(&cm_cellLock);
220 /* when we dropped the lock the cell could have been added
221 * to the list so check again while holding the write lock
223 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
224 if (cm_stricmp_utf8(namep, cp->name) == 0) {
225 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
226 fullname[CELL_MAXNAMELEN-1] = '\0';
234 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
235 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
236 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
237 fullname[CELL_MAXNAMELEN-1] = '\0';
243 lock_ReleaseWrite(&cm_cellLock);
244 lock_ObtainMutex(&cp->mx);
245 lock_ObtainWrite(&cm_cellLock);
246 cm_AddCellToNameHashTable(cp);
247 cm_AddCellToIDHashTable(cp);
248 lock_ReleaseMutex(&cp->mx);
252 if ( cm_data.freeCellsp != NULL ) {
253 cp = cm_data.freeCellsp;
254 cm_data.freeCellsp = cp->freeNextp;
257 * The magic, cellID, and mx fields are already set.
260 if ( cm_data.currentCells >= cm_data.maxCells )
261 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
263 /* don't increment currentCells until we know that we
264 * are going to keep this entry
266 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
267 memset(cp, 0, sizeof(cm_cell_t));
268 cp->magic = CM_CELL_MAGIC;
270 /* the cellID cannot be 0 */
271 cp->cellID = ++cm_data.currentCells;
273 /* otherwise we found the cell, and so we're nearly done */
274 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
277 lock_ReleaseWrite(&cm_cellLock);
282 code = cm_SearchCellRegistry(1, namep, fullname, linkedName, cm_AddCellProc, &rock);
283 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
284 code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
286 osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s",
287 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
288 osi_LogSaveString(afsd_logp,linkedName));
294 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
296 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
297 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
298 lock_ObtainMutex(&cp->mx);
299 lock_ObtainWrite(&cm_cellLock);
301 cm_RemoveCellFromIDHashTable(cp);
302 cm_RemoveCellFromNameHashTable(cp);
303 lock_ReleaseMutex(&cp->mx);
307 } else { /* got cell from DNS */
308 lock_ObtainMutex(&cp->mx);
310 cp->flags |= CM_CELLFLAG_DNS;
311 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
312 cp->timeout = time(0) + ttl;
318 lock_ObtainMutex(&cp->mx);
319 lock_ObtainWrite(&cm_cellLock);
321 cm_RemoveCellFromIDHashTable(cp);
322 cm_RemoveCellFromNameHashTable(cp);
323 lock_ReleaseMutex(&cp->mx);
329 lock_ObtainMutex(&cp->mx);
331 cp->timeout = time(0) + 7200; /* two hour timeout */
334 /* we have now been given the fullname of the cell. It may
335 * be that we already have a cell with that name. If so,
336 * we should use it instead of completing the allocation
339 lock_ObtainRead(&cm_cellLock);
340 hash = CM_CELL_NAME_HASH(fullname);
341 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
342 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
349 lock_ObtainMutex(&cp->mx);
352 lock_ConvertRToW(&cm_cellLock);
354 cm_RemoveCellFromIDHashTable(cp);
355 cm_RemoveCellFromNameHashTable(cp);
356 lock_ReleaseMutex(&cp->mx);
362 lock_ReleaseRead(&cm_cellLock);
364 /* randomise among those vlservers having the same rank*/
365 cm_RandomizeServer(&cp->vlServersp);
368 lock_ObtainMutex(&cp->mx);
371 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
372 cp->name[CELL_MAXNAMELEN-1] = '\0';
374 strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
375 cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
377 lock_ObtainWrite(&cm_cellLock);
379 cm_AddCellToNameHashTable(cp);
380 cm_AddCellToIDHashTable(cp);
381 lock_ReleaseMutex(&cp->mx);
384 /* append cell to global list */
385 if (cm_data.allCellsp == NULL) {
386 cm_data.allCellsp = cp;
388 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
395 lock_ReleaseRead(&cm_cellLock);
399 lock_ReleaseMutex(&cp->mx);
401 lock_ReleaseWrite(&cm_cellLock);
403 /* fullname is not valid if cp == NULL */
406 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
407 newnamep[CELL_MAXNAMELEN-1]='\0';
413 if (cp && cp->linkedName[0]) {
414 cm_cell_t * linkedCellp = NULL;
416 if (!strcmp(cp->name, cp->linkedName)) {
417 cp->linkedName[0] = '\0';
418 } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
419 linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
421 lock_ObtainWrite(&cm_cellLock);
423 (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
424 cp->linkedName[0] = '\0';
426 strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
427 linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
429 lock_ReleaseWrite(&cm_cellLock);
435 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
440 lock_ObtainRead(&cm_cellLock);
442 hash = CM_CELL_ID_HASH(cellID);
444 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
445 if (cellID == cp->cellID)
448 lock_ReleaseRead(&cm_cellLock);
451 cm_UpdateCell(cp, flags);
457 cm_ValidateCell(void)
460 afs_uint32 count1, count2;
462 for (cellp = cm_data.allCellsp, count1 = 0; cellp; cellp=cellp->allNextp, count1++) {
463 if ( cellp->magic != CM_CELL_MAGIC ) {
464 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
465 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
468 if ( count1 != 0 && cellp == cm_data.allCellsp ||
469 count1 > cm_data.maxCells ) {
470 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
471 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
476 for (cellp = cm_data.freeCellsp, count2 = 0; cellp; cellp=cellp->freeNextp, count2++) {
477 if ( count2 != 0 && cellp == cm_data.freeCellsp ||
478 count2 > cm_data.maxCells ) {
479 afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
480 fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
485 if ( (count1 + count2) != cm_data.currentCells ) {
486 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
487 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
496 cm_ShutdownCell(void)
500 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
501 lock_FinalizeMutex(&cellp->mx);
507 void cm_InitCell(int newFile, long maxCells)
509 static osi_once_t once;
511 if (osi_Once(&once)) {
514 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
517 cm_data.allCellsp = NULL;
518 cm_data.currentCells = 0;
519 cm_data.maxCells = maxCells;
520 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
521 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
523 #ifdef AFS_FREELANCE_CLIENT
524 /* Generate a dummy entry for the Freelance cell whether or not
525 * freelance mode is being used in this session
528 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
529 memset(cellp, 0, sizeof(cm_cell_t));
530 cellp->magic = CM_CELL_MAGIC;
532 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
534 lock_ObtainMutex(&cellp->mx);
535 lock_ObtainWrite(&cm_cellLock);
538 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
539 cellp->name[CELL_MAXNAMELEN-1] = '\0';
541 /* thread on global list */
542 cellp->allNextp = cm_data.allCellsp;
543 cm_data.allCellsp = cellp;
545 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
546 cellp->vlServersp = NULL;
547 cellp->flags = CM_CELLFLAG_FREELANCE;
549 cm_AddCellToNameHashTable(cellp);
550 cm_AddCellToIDHashTable(cellp);
551 lock_ReleaseWrite(&cm_cellLock);
552 lock_ReleaseMutex(&cellp->mx);
555 lock_ObtainRead(&cm_cellLock);
556 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
557 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
558 cellp->vlServersp = NULL;
559 cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
561 lock_ReleaseRead(&cm_cellLock);
568 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
573 cp = tsp->cellp; /* cell that this vlserver belongs to */
575 lock_ObtainMutex(&cp->mx);
576 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
578 if ( !code ) /* if the server list was rearranged */
579 cm_RandomizeServer(&cp->vlServersp);
581 lock_ReleaseMutex(&cp->mx);
585 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
592 lock_ObtainRead(&cm_cellLock);
594 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
595 cookie, cm_data.currentCells, cm_data.maxCells);
596 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
598 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
599 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
600 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
601 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
604 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
605 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
608 lock_ReleaseRead(&cm_cellLock);
613 /* call with volume write-locked and mutex held */
614 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
618 lock_AssertWrite(&cm_cellLock);
619 lock_AssertMutex(&cellp->mx);
621 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
624 i = CM_CELL_NAME_HASH(cellp->name);
626 cellp->nameNextp = cm_data.cellNameHashTablep[i];
627 cm_data.cellNameHashTablep[i] = cellp;
628 cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
631 /* call with cell write-locked and mutex held */
632 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
638 lock_AssertWrite(&cm_cellLock);
639 lock_AssertMutex(&cellp->mx);
641 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
642 /* hash it out first */
643 i = CM_CELL_NAME_HASH(cellp->name);
644 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
646 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
647 if (tcellp == cellp) {
648 *lcellpp = cellp->nameNextp;
649 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
650 cellp->nameNextp = NULL;
657 /* call with cell write-locked and mutex held */
658 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
662 lock_AssertWrite(&cm_cellLock);
663 lock_AssertMutex(&cellp->mx);
665 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
668 i = CM_CELL_ID_HASH(cellp->cellID);
670 cellp->idNextp = cm_data.cellIDHashTablep[i];
671 cm_data.cellIDHashTablep[i] = cellp;
672 cellp->flags |= CM_CELLFLAG_IN_IDHASH;
675 /* call with cell write-locked and mutex held */
676 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
682 lock_AssertWrite(&cm_cellLock);
683 lock_AssertMutex(&cellp->mx);
685 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
686 /* hash it out first */
687 i = CM_CELL_ID_HASH(cellp->cellID);
688 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
690 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
691 if (tcellp == cellp) {
692 *lcellpp = cellp->idNextp;
693 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
694 cellp->idNextp = NULL;