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);
60 /* Insert the vlserver into a sorted list, sorted by server rank */
61 tsrp = cm_NewServerRef(tsp, 0);
62 cm_InsertServerList(&cellp->vlServersp, tsrp);
63 /* drop the allocation reference */
64 lock_ObtainWrite(&cm_serverLock);
66 lock_ReleaseWrite(&cm_serverLock);
71 /* if it's from DNS, see if it has expired
72 * and check to make sure we have a valid set of volume servers
73 * this function must not be called with a lock on cm_cellLock
75 cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
83 lock_ObtainMutex(&cp->mx);
84 if ((cp->vlServersp == NULL
85 #ifdef AFS_FREELANCE_CLIENT
86 && !(cp->flags & CM_CELLFLAG_FREELANCE)
88 ) || (time(0) > cp->timeout)
89 || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
90 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
93 lock_ReleaseMutex(&cp->mx);
95 /* must empty cp->vlServersp */
97 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
98 cp->vlServersp = NULL;
103 code = cm_SearchCellRegistry(1, cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
104 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
105 code = cm_SearchCellFileEx(cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
107 lock_ObtainMutex(&cp->mx);
108 cp->timeout = time(0) + 7200;
109 lock_ReleaseMutex(&cp->mx);
115 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
116 if (code == 0) { /* got cell from DNS */
117 lock_ObtainMutex(&cp->mx);
118 cp->flags |= CM_CELLFLAG_DNS;
119 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
120 cp->timeout = time(0) + ttl;
121 lock_ReleaseMutex(&cp->mx);
123 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
126 /* if we fail to find it this time, we'll just do nothing and leave the
127 * current entry alone
129 lock_ObtainMutex(&cp->mx);
130 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
131 lock_ReleaseMutex(&cp->mx);
136 lock_ReleaseMutex(&cp->mx);
140 cm_RandomizeServer(&cp->vlServersp);
142 return code ? NULL : cp;
145 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
146 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
148 return cm_GetCell_Gen(namep, NULL, flags);
151 void cm_FreeCell(cm_cell_t *cellp)
153 lock_AssertWrite(&cm_cellLock);
155 if (cellp->vlServersp)
156 cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
157 cellp->name[0] = '\0';
159 cellp->freeNextp = cm_data.freeCellsp;
160 cm_data.freeCellsp = cellp;
163 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
167 char fullname[CELL_MAXNAMELEN]="";
168 char linkedName[CELL_MAXNAMELEN]="";
169 char name[CELL_MAXNAMELEN]="";
170 int hasWriteLock = 0;
176 if (namep == NULL || !namep[0] || !strcmp(namep,CM_IOCTL_FILENAME_NOSLASH))
180 * Strip off any trailing dots at the end of the cell name.
181 * Failure to do so results in an undesireable alias as the
182 * result of DNS AFSDB record lookups where a trailing dot
183 * has special meaning.
185 strncpy(name, namep, CELL_MAXNAMELEN);
186 for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
193 hash = CM_CELL_NAME_HASH(namep);
195 lock_ObtainRead(&cm_cellLock);
196 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
197 if (cm_stricmp_utf8(namep, cp->name) == 0) {
198 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
199 fullname[CELL_MAXNAMELEN-1] = '\0';
205 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
206 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
207 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
208 fullname[CELL_MAXNAMELEN-1] = '\0';
215 lock_ReleaseRead(&cm_cellLock);
216 cm_UpdateCell(cp, flags);
217 } else if (flags & CM_FLAG_CREATE) {
218 lock_ConvertRToW(&cm_cellLock);
221 /* when we dropped the lock the cell could have been added
222 * to the list so check again while holding the write lock
224 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
225 if (cm_stricmp_utf8(namep, cp->name) == 0) {
226 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
227 fullname[CELL_MAXNAMELEN-1] = '\0';
235 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
236 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
237 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
238 fullname[CELL_MAXNAMELEN-1] = '\0';
244 lock_ReleaseWrite(&cm_cellLock);
245 lock_ObtainMutex(&cp->mx);
246 lock_ObtainWrite(&cm_cellLock);
247 cm_AddCellToNameHashTable(cp);
248 cm_AddCellToIDHashTable(cp);
249 lock_ReleaseMutex(&cp->mx);
253 if ( cm_data.freeCellsp != NULL ) {
254 cp = cm_data.freeCellsp;
255 cm_data.freeCellsp = cp->freeNextp;
258 * The magic, cellID, and mx fields are already set.
261 if ( cm_data.currentCells >= cm_data.maxCells )
262 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
264 /* don't increment currentCells until we know that we
265 * are going to keep this entry
267 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
268 memset(cp, 0, sizeof(cm_cell_t));
269 cp->magic = CM_CELL_MAGIC;
271 /* the cellID cannot be 0 */
272 cp->cellID = ++cm_data.currentCells;
274 /* otherwise we found the cell, and so we're nearly done */
275 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
278 lock_ReleaseWrite(&cm_cellLock);
283 code = cm_SearchCellRegistry(1, namep, fullname, linkedName, cm_AddCellProc, &rock);
284 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
285 code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
287 osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s",
288 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
289 osi_LogSaveString(afsd_logp,linkedName));
295 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
297 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
298 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
299 lock_ObtainMutex(&cp->mx);
300 lock_ObtainWrite(&cm_cellLock);
302 cm_RemoveCellFromIDHashTable(cp);
303 cm_RemoveCellFromNameHashTable(cp);
304 lock_ReleaseMutex(&cp->mx);
308 } else { /* got cell from DNS */
309 lock_ObtainMutex(&cp->mx);
311 cp->flags |= CM_CELLFLAG_DNS;
312 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
313 cp->timeout = time(0) + ttl;
319 lock_ObtainMutex(&cp->mx);
320 lock_ObtainWrite(&cm_cellLock);
322 cm_RemoveCellFromIDHashTable(cp);
323 cm_RemoveCellFromNameHashTable(cp);
324 lock_ReleaseMutex(&cp->mx);
330 lock_ObtainMutex(&cp->mx);
332 cp->timeout = time(0) + 7200; /* two hour timeout */
335 /* we have now been given the fullname of the cell. It may
336 * be that we already have a cell with that name. If so,
337 * we should use it instead of completing the allocation
340 lock_ObtainRead(&cm_cellLock);
341 hash = CM_CELL_NAME_HASH(fullname);
342 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
343 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
350 lock_ObtainMutex(&cp->mx);
353 lock_ConvertRToW(&cm_cellLock);
355 cm_RemoveCellFromIDHashTable(cp);
356 cm_RemoveCellFromNameHashTable(cp);
357 lock_ReleaseMutex(&cp->mx);
363 lock_ReleaseRead(&cm_cellLock);
365 /* randomise among those vlservers having the same rank*/
366 cm_RandomizeServer(&cp->vlServersp);
369 lock_ObtainMutex(&cp->mx);
372 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
373 cp->name[CELL_MAXNAMELEN-1] = '\0';
375 strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
376 cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
378 lock_ObtainWrite(&cm_cellLock);
380 cm_AddCellToNameHashTable(cp);
381 cm_AddCellToIDHashTable(cp);
382 lock_ReleaseMutex(&cp->mx);
385 /* append cell to global list */
386 if (cm_data.allCellsp == NULL) {
387 cm_data.allCellsp = cp;
389 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
396 lock_ReleaseRead(&cm_cellLock);
400 lock_ReleaseMutex(&cp->mx);
402 lock_ReleaseWrite(&cm_cellLock);
404 /* fullname is not valid if cp == NULL */
407 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
408 newnamep[CELL_MAXNAMELEN-1]='\0';
414 if (cp && cp->linkedName[0]) {
415 cm_cell_t * linkedCellp = NULL;
417 if (!strcmp(cp->name, cp->linkedName)) {
418 cp->linkedName[0] = '\0';
419 } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
420 linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
422 lock_ObtainWrite(&cm_cellLock);
424 (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
425 cp->linkedName[0] = '\0';
427 strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
428 linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
430 lock_ReleaseWrite(&cm_cellLock);
436 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
441 lock_ObtainRead(&cm_cellLock);
443 hash = CM_CELL_ID_HASH(cellID);
445 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
446 if (cellID == cp->cellID)
449 lock_ReleaseRead(&cm_cellLock);
452 cm_UpdateCell(cp, flags);
458 cm_ValidateCell(void)
461 afs_uint32 count1, count2;
463 for (cellp = cm_data.allCellsp, count1 = 0; cellp; cellp=cellp->allNextp, count1++) {
464 if ( cellp->magic != CM_CELL_MAGIC ) {
465 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
466 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
469 if ( count1 != 0 && cellp == cm_data.allCellsp ||
470 count1 > cm_data.maxCells ) {
471 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
472 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
477 for (cellp = cm_data.freeCellsp, count2 = 0; cellp; cellp=cellp->freeNextp, count2++) {
478 if ( count2 != 0 && cellp == cm_data.freeCellsp ||
479 count2 > cm_data.maxCells ) {
480 afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
481 fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
486 if ( (count1 + count2) != cm_data.currentCells ) {
487 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
488 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
497 cm_ShutdownCell(void)
501 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
502 lock_FinalizeMutex(&cellp->mx);
508 void cm_InitCell(int newFile, long maxCells)
510 static osi_once_t once;
512 if (osi_Once(&once)) {
515 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
518 cm_data.allCellsp = NULL;
519 cm_data.currentCells = 0;
520 cm_data.maxCells = maxCells;
521 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
522 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
524 #ifdef AFS_FREELANCE_CLIENT
525 /* Generate a dummy entry for the Freelance cell whether or not
526 * freelance mode is being used in this session
529 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
530 memset(cellp, 0, sizeof(cm_cell_t));
531 cellp->magic = CM_CELL_MAGIC;
533 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
535 lock_ObtainMutex(&cellp->mx);
536 lock_ObtainWrite(&cm_cellLock);
539 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
540 cellp->name[CELL_MAXNAMELEN-1] = '\0';
542 /* thread on global list */
543 cellp->allNextp = cm_data.allCellsp;
544 cm_data.allCellsp = cellp;
546 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
547 cellp->vlServersp = NULL;
548 cellp->flags = CM_CELLFLAG_FREELANCE;
550 cm_AddCellToNameHashTable(cellp);
551 cm_AddCellToIDHashTable(cellp);
552 lock_ReleaseWrite(&cm_cellLock);
553 lock_ReleaseMutex(&cellp->mx);
556 lock_ObtainRead(&cm_cellLock);
557 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
558 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
559 cellp->vlServersp = NULL;
560 cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
562 lock_ReleaseRead(&cm_cellLock);
569 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
574 cp = tsp->cellp; /* cell that this vlserver belongs to */
576 lock_ObtainMutex(&cp->mx);
577 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
579 if ( !code ) /* if the server list was rearranged */
580 cm_RandomizeServer(&cp->vlServersp);
582 lock_ReleaseMutex(&cp->mx);
586 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
593 lock_ObtainRead(&cm_cellLock);
595 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
596 cookie, cm_data.currentCells, cm_data.maxCells);
597 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
599 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
600 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
601 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
602 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
605 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
606 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
609 lock_ReleaseRead(&cm_cellLock);
614 /* call with volume write-locked and mutex held */
615 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
619 lock_AssertWrite(&cm_cellLock);
620 lock_AssertMutex(&cellp->mx);
622 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
625 i = CM_CELL_NAME_HASH(cellp->name);
627 cellp->nameNextp = cm_data.cellNameHashTablep[i];
628 cm_data.cellNameHashTablep[i] = cellp;
629 cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
632 /* call with cell write-locked and mutex held */
633 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
639 lock_AssertWrite(&cm_cellLock);
640 lock_AssertMutex(&cellp->mx);
642 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
643 /* hash it out first */
644 i = CM_CELL_NAME_HASH(cellp->name);
645 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
647 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
648 if (tcellp == cellp) {
649 *lcellpp = cellp->nameNextp;
650 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
651 cellp->nameNextp = NULL;
658 /* call with cell write-locked and mutex held */
659 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
663 lock_AssertWrite(&cm_cellLock);
664 lock_AssertMutex(&cellp->mx);
666 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
669 i = CM_CELL_ID_HASH(cellp->cellID);
671 cellp->idNextp = cm_data.cellIDHashTablep[i];
672 cm_data.cellIDHashTablep[i] = cellp;
673 cellp->flags |= CM_CELLFLAG_IN_IDHASH;
676 /* call with cell write-locked and mutex held */
677 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
683 lock_AssertWrite(&cm_cellLock);
684 lock_AssertMutex(&cellp->mx);
686 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
687 /* hash it out first */
688 i = CM_CELL_ID_HASH(cellp->cellID);
689 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
691 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
692 if (tcellp == cellp) {
693 *lcellpp = cellp->idNextp;
694 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
695 cellp->idNextp = NULL;