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 be called with a Write 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 if (cellp->vlServersp)
153 cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
154 cellp->name[0] = '\0';
156 cellp->freeNextp = cm_data.freeCellsp;
157 cm_data.freeCellsp = cellp;
160 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
164 char fullname[CELL_MAXNAMELEN]="";
165 char linkedName[CELL_MAXNAMELEN]="";
166 char name[CELL_MAXNAMELEN]="";
167 int hasWriteLock = 0;
173 if (namep == NULL || !namep[0] || !strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
177 * Strip off any trailing dots at the end of the cell name.
178 * Failure to do so results in an undesireable alias as the
179 * result of DNS AFSDB record lookups where a trailing dot
180 * has special meaning.
182 strncpy(name, namep, CELL_MAXNAMELEN);
183 for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
190 hash = CM_CELL_NAME_HASH(namep);
192 lock_ObtainRead(&cm_cellLock);
193 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
194 if (cm_stricmp_utf8(namep, cp->name) == 0) {
195 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
196 fullname[CELL_MAXNAMELEN-1] = '\0';
202 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
203 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
204 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
205 fullname[CELL_MAXNAMELEN-1] = '\0';
212 lock_ReleaseRead(&cm_cellLock);
213 cm_UpdateCell(cp, flags);
214 } else if (flags & CM_FLAG_CREATE) {
215 lock_ConvertRToW(&cm_cellLock);
218 /* when we dropped the lock the cell could have been added
219 * to the list so check again while holding the write lock
221 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
222 if (cm_stricmp_utf8(namep, cp->name) == 0) {
223 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
224 fullname[CELL_MAXNAMELEN-1] = '\0';
232 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
233 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
234 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
235 fullname[CELL_MAXNAMELEN-1] = '\0';
241 lock_ObtainMutex(&cp->mx);
242 cm_AddCellToNameHashTable(cp);
243 cm_AddCellToIDHashTable(cp);
244 lock_ReleaseMutex(&cp->mx);
248 if ( cm_data.freeCellsp != NULL ) {
249 cp = cm_data.freeCellsp;
250 cm_data.freeCellsp = cp->freeNextp;
253 * The magic, cellID, and mx fields are already set.
256 if ( cm_data.currentCells >= cm_data.maxCells )
257 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
259 /* don't increment currentCells until we know that we
260 * are going to keep this entry
262 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
263 memset(cp, 0, sizeof(cm_cell_t));
264 cp->magic = CM_CELL_MAGIC;
266 /* the cellID cannot be 0 */
267 cp->cellID = ++cm_data.currentCells;
269 /* otherwise we found the cell, and so we're nearly done */
270 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
273 lock_ReleaseWrite(&cm_cellLock);
278 code = cm_SearchCellRegistry(1, namep, fullname, linkedName, cm_AddCellProc, &rock);
279 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
280 code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
282 osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s",
283 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
284 osi_LogSaveString(afsd_logp,linkedName));
290 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
292 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
293 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
297 } else { /* got cell from DNS */
298 lock_ObtainMutex(&cp->mx);
300 cp->flags |= CM_CELLFLAG_DNS;
301 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
302 cp->timeout = time(0) + ttl;
313 lock_ObtainMutex(&cp->mx);
315 cp->timeout = time(0) + 7200; /* two hour timeout */
318 /* we have now been given the fullname of the cell. It may
319 * be that we already have a cell with that name. If so,
320 * we should use it instead of completing the allocation
323 hash = CM_CELL_NAME_HASH(fullname);
324 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
325 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
332 lock_ReleaseMutex(&cp->mx);
340 /* randomise among those vlservers having the same rank*/
341 cm_RandomizeServer(&cp->vlServersp);
344 lock_ObtainMutex(&cp->mx);
346 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
347 cp->name[CELL_MAXNAMELEN-1] = '\0';
349 strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
350 cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
352 cm_AddCellToNameHashTable(cp);
353 cm_AddCellToIDHashTable(cp);
354 lock_ReleaseMutex(&cp->mx);
357 /* append cell to global list */
358 if (cm_data.allCellsp == NULL) {
359 cm_data.allCellsp = cp;
361 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
368 lock_ReleaseRead(&cm_cellLock);
372 lock_ReleaseMutex(&cp->mx);
374 lock_ReleaseWrite(&cm_cellLock);
376 /* fullname is not valid if cp == NULL */
379 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
380 newnamep[CELL_MAXNAMELEN-1]='\0';
386 if (cp && cp->linkedName[0]) {
387 cm_cell_t * linkedCellp = NULL;
389 if (!strcmp(cp->name, cp->linkedName)) {
390 cp->linkedName[0] = '\0';
391 } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
392 linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
394 lock_ObtainWrite(&cm_cellLock);
396 (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
397 cp->linkedName[0] = '\0';
399 strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
400 linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
402 lock_ReleaseWrite(&cm_cellLock);
408 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
413 lock_ObtainRead(&cm_cellLock);
415 hash = CM_CELL_ID_HASH(cellID);
417 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
418 if (cellID == cp->cellID)
421 lock_ReleaseRead(&cm_cellLock);
424 cm_UpdateCell(cp, flags);
430 cm_ValidateCell(void)
435 for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->allNextp, count++) {
436 if ( cellp->magic != CM_CELL_MAGIC ) {
437 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
438 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
441 if ( count != 0 && cellp == cm_data.allCellsp ||
442 count > cm_data.maxCells ) {
443 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
444 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
449 for (cellp = cm_data.freeCellsp; cellp; cellp=cellp->freeNextp, count++) {
450 if ( count != 0 && cellp == cm_data.freeCellsp ||
451 count > cm_data.maxCells ) {
452 afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
453 fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
458 if ( count != cm_data.currentCells ) {
459 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
460 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
469 cm_ShutdownCell(void)
473 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
474 lock_FinalizeMutex(&cellp->mx);
480 void cm_InitCell(int newFile, long maxCells)
482 static osi_once_t once;
484 if (osi_Once(&once)) {
487 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
490 cm_data.allCellsp = NULL;
491 cm_data.currentCells = 0;
492 cm_data.maxCells = maxCells;
493 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
494 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
496 #ifdef AFS_FREELANCE_CLIENT
497 /* Generate a dummy entry for the Freelance cell whether or not
498 * freelance mode is being used in this session
501 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
502 memset(cellp, 0, sizeof(cm_cell_t));
503 cellp->magic = CM_CELL_MAGIC;
505 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
508 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
509 cellp->name[CELL_MAXNAMELEN-1] = '\0';
511 /* thread on global list */
512 cellp->allNextp = cm_data.allCellsp;
513 cm_data.allCellsp = cellp;
515 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
516 cellp->vlServersp = NULL;
517 cellp->flags = CM_CELLFLAG_FREELANCE;
519 lock_ObtainMutex(&cellp->mx);
520 cm_AddCellToNameHashTable(cellp);
521 cm_AddCellToIDHashTable(cellp);
522 lock_ReleaseMutex(&cellp->mx);
525 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
526 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
527 cellp->vlServersp = NULL;
528 cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
536 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
541 cp = tsp->cellp; /* cell that this vlserver belongs to */
543 lock_ObtainMutex(&cp->mx);
544 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
546 if ( !code ) /* if the server list was rearranged */
547 cm_RandomizeServer(&cp->vlServersp);
549 lock_ReleaseMutex(&cp->mx);
553 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
560 lock_ObtainRead(&cm_cellLock);
562 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
563 cookie, cm_data.currentCells, cm_data.maxCells);
564 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
566 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
567 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
568 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
569 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
572 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
573 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
576 lock_ReleaseRead(&cm_cellLock);
581 /* call with volume write-locked and mutex held */
582 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
586 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
589 i = CM_CELL_NAME_HASH(cellp->name);
591 cellp->nameNextp = cm_data.cellNameHashTablep[i];
592 cm_data.cellNameHashTablep[i] = cellp;
593 cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
596 /* call with cell write-locked and mutex held */
597 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
603 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
604 /* hash it out first */
605 i = CM_CELL_NAME_HASH(cellp->name);
606 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
608 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
609 if (tcellp == cellp) {
610 *lcellpp = cellp->nameNextp;
611 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
612 cellp->nameNextp = NULL;
619 /* call with cell write-locked and mutex held */
620 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
624 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
627 i = CM_CELL_ID_HASH(cellp->cellID);
629 cellp->idNextp = cm_data.cellIDHashTablep[i];
630 cm_data.cellIDHashTablep[i] = cellp;
631 cellp->flags |= CM_CELLFLAG_IN_IDHASH;
634 /* call with cell write-locked and mutex held */
635 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
641 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
642 /* hash it out first */
643 i = CM_CELL_ID_HASH(cellp->cellID);
644 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
646 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
647 if (tcellp == cellp) {
648 *lcellpp = cellp->idNextp;
649 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
650 cellp->idNextp = NULL;