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)
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, probe ? 0 : CM_FLAG_NOPROBE);
57 /* Insert the vlserver into a sorted list, sorted by server rank */
58 tsrp = cm_NewServerRef(tsp, 0);
59 cm_InsertServerList(&cellp->vlServersp, tsrp);
60 /* drop the allocation reference */
61 lock_ObtainWrite(&cm_serverLock);
63 lock_ReleaseWrite(&cm_serverLock);
68 /* if it's from DNS, see if it has expired
69 * and check to make sure we have a valid set of volume servers
70 * this function must be called with a Write Lock on cm_cellLock
72 cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
80 lock_ObtainMutex(&cp->mx);
81 if ((cp->vlServersp == NULL
82 #ifdef AFS_FREELANCE_CLIENT
83 && !(cp->flags & CM_CELLFLAG_FREELANCE)
85 ) || (time(0) > cp->timeout)
87 || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
88 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
92 lock_ReleaseMutex(&cp->mx);
94 /* must empty cp->vlServersp */
96 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
97 cp->vlServersp = NULL;
102 code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, &rock);
104 lock_ObtainMutex(&cp->mx);
105 cp->timeout = time(0) + 7200;
106 lock_ReleaseMutex(&cp->mx);
113 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
114 if (code == 0) { /* got cell from DNS */
115 lock_ObtainMutex(&cp->mx);
116 cp->flags |= CM_CELLFLAG_DNS;
117 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
118 cp->timeout = time(0) + ttl;
119 lock_ReleaseMutex(&cp->mx);
121 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
124 /* if we fail to find it this time, we'll just do nothing and leave the
125 * current entry alone
127 lock_ObtainMutex(&cp->mx);
128 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
129 lock_ReleaseMutex(&cp->mx);
133 #endif /* AFS_AFSDB_ENV */
135 lock_ReleaseMutex(&cp->mx);
137 return code ? NULL : cp;
140 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
141 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
143 return cm_GetCell_Gen(namep, NULL, flags);
146 void cm_FreeCell(cm_cell_t *cellp)
148 if (cellp->vlServersp)
149 cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
150 cellp->name[0] = '\0';
152 cellp->freeNextp = cm_data.freeCellsp;
153 cm_data.freeCellsp = cellp;
156 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
160 char fullname[CELL_MAXNAMELEN]="";
161 char name[CELL_MAXNAMELEN]="";
162 int hasWriteLock = 0;
168 if (namep == NULL || !namep[0] || !strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
172 * Strip off any trailing dots at the end of the cell name.
173 * Failure to do so results in an undesireable alias as the
174 * result of DNS AFSDB record lookups where a trailing dot
175 * has special meaning.
177 strncpy(name, namep, CELL_MAXNAMELEN);
178 for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
185 hash = CM_CELL_NAME_HASH(namep);
187 lock_ObtainRead(&cm_cellLock);
188 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
189 if (cm_stricmp_utf8(namep, cp->name) == 0) {
190 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
191 fullname[CELL_MAXNAMELEN-1] = '\0';
197 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
198 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
199 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
200 fullname[CELL_MAXNAMELEN-1] = '\0';
207 lock_ReleaseRead(&cm_cellLock);
208 cm_UpdateCell(cp, flags);
209 } else if (flags & CM_FLAG_CREATE) {
210 lock_ConvertRToW(&cm_cellLock);
213 /* when we dropped the lock the cell could have been added
214 * to the list so check again while holding the write lock
216 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
217 if (cm_stricmp_utf8(namep, cp->name) == 0) {
218 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
219 fullname[CELL_MAXNAMELEN-1] = '\0';
227 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
228 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
229 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
230 fullname[CELL_MAXNAMELEN-1] = '\0';
236 lock_ObtainMutex(&cp->mx);
237 cm_AddCellToNameHashTable(cp);
238 cm_AddCellToIDHashTable(cp);
239 lock_ReleaseMutex(&cp->mx);
243 if ( cm_data.freeCellsp != NULL ) {
244 cp = cm_data.freeCellsp;
245 cm_data.freeCellsp = cp->freeNextp;
248 * The magic, cellID, and mx fields are already set.
251 if ( cm_data.currentCells >= cm_data.maxCells )
252 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
254 /* don't increment currentCells until we know that we
255 * are going to keep this entry
257 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
258 memset(cp, 0, sizeof(cm_cell_t));
259 cp->magic = CM_CELL_MAGIC;
261 /* the cellID cannot be 0 */
262 cp->cellID = ++cm_data.currentCells;
264 /* otherwise we found the cell, and so we're nearly done */
265 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
268 lock_ReleaseWrite(&cm_cellLock);
273 code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, &rock);
275 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s",
276 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
282 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
284 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
285 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
289 } else { /* got cell from DNS */
290 lock_ObtainMutex(&cp->mx);
292 cp->flags |= CM_CELLFLAG_DNS;
293 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
294 cp->timeout = time(0) + ttl;
305 lock_ObtainMutex(&cp->mx);
307 cp->timeout = time(0) + 7200; /* two hour timeout */
310 /* we have now been given the fullname of the cell. It may
311 * be that we already have a cell with that name. If so,
312 * we should use it instead of completing the allocation
315 hash = CM_CELL_NAME_HASH(fullname);
316 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
317 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
324 lock_ReleaseMutex(&cp->mx);
332 /* randomise among those vlservers having the same rank*/
333 cm_RandomizeServer(&cp->vlServersp);
336 lock_ObtainMutex(&cp->mx);
338 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
339 cp->name[CELL_MAXNAMELEN-1] = '\0';
341 cm_AddCellToNameHashTable(cp);
342 cm_AddCellToIDHashTable(cp);
343 lock_ReleaseMutex(&cp->mx);
346 /* append cell to global list */
347 if (cm_data.allCellsp == NULL) {
348 cm_data.allCellsp = cp;
350 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
357 lock_ReleaseRead(&cm_cellLock);
361 lock_ReleaseMutex(&cp->mx);
363 lock_ReleaseWrite(&cm_cellLock);
365 /* fullname is not valid if cp == NULL */
368 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
369 newnamep[CELL_MAXNAMELEN-1]='\0';
377 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
382 lock_ObtainRead(&cm_cellLock);
384 hash = CM_CELL_ID_HASH(cellID);
386 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
387 if (cellID == cp->cellID)
390 lock_ReleaseRead(&cm_cellLock);
393 cm_UpdateCell(cp, flags);
399 cm_ValidateCell(void)
404 for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->allNextp, count++) {
405 if ( cellp->magic != CM_CELL_MAGIC ) {
406 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
407 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
410 if ( count != 0 && cellp == cm_data.allCellsp ||
411 count > cm_data.maxCells ) {
412 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
413 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
418 for (cellp = cm_data.freeCellsp; cellp; cellp=cellp->freeNextp, count++) {
419 if ( count != 0 && cellp == cm_data.freeCellsp ||
420 count > cm_data.maxCells ) {
421 afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
422 fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
427 if ( count != cm_data.currentCells ) {
428 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
429 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
438 cm_ShutdownCell(void)
442 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
443 lock_FinalizeMutex(&cellp->mx);
449 void cm_InitCell(int newFile, long maxCells)
451 static osi_once_t once;
453 if (osi_Once(&once)) {
456 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
459 cm_data.allCellsp = NULL;
460 cm_data.currentCells = 0;
461 cm_data.maxCells = maxCells;
462 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
463 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
465 #ifdef AFS_FREELANCE_CLIENT
466 /* Generate a dummy entry for the Freelance cell whether or not
467 * freelance mode is being used in this session
470 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
471 memset(cellp, 0, sizeof(cm_cell_t));
472 cellp->magic = CM_CELL_MAGIC;
474 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
477 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
478 cellp->name[CELL_MAXNAMELEN-1] = '\0';
480 /* thread on global list */
481 cellp->allNextp = cm_data.allCellsp;
482 cm_data.allCellsp = cellp;
484 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
485 cellp->vlServersp = NULL;
486 cellp->flags = CM_CELLFLAG_FREELANCE;
488 lock_ObtainMutex(&cellp->mx);
489 cm_AddCellToNameHashTable(cellp);
490 cm_AddCellToIDHashTable(cellp);
491 lock_ReleaseMutex(&cellp->mx);
494 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
495 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
496 cellp->vlServersp = NULL;
497 cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
505 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
510 cp = tsp->cellp; /* cell that this vlserver belongs to */
512 lock_ObtainMutex(&cp->mx);
513 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
515 if ( !code ) /* if the server list was rearranged */
516 cm_RandomizeServer(&cp->vlServersp);
518 lock_ReleaseMutex(&cp->mx);
522 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
529 lock_ObtainRead(&cm_cellLock);
531 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
532 cookie, cm_data.currentCells, cm_data.maxCells);
533 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
535 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
536 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
537 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
538 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
541 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
542 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
545 lock_ReleaseRead(&cm_cellLock);
550 /* call with volume write-locked and mutex held */
551 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
555 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
558 i = CM_CELL_NAME_HASH(cellp->name);
560 cellp->nameNextp = cm_data.cellNameHashTablep[i];
561 cm_data.cellNameHashTablep[i] = cellp;
562 cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
565 /* call with cell write-locked and mutex held */
566 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
572 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
573 /* hash it out first */
574 i = CM_CELL_NAME_HASH(cellp->name);
575 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
577 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
578 if (tcellp == cellp) {
579 *lcellpp = cellp->nameNextp;
580 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
581 cellp->nameNextp = NULL;
588 /* call with cell write-locked and mutex held */
589 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
593 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
596 i = CM_CELL_ID_HASH(cellp->cellID);
598 cellp->idNextp = cm_data.cellIDHashTablep[i];
599 cm_data.cellIDHashTablep[i] = cellp;
600 cellp->flags |= CM_CELLFLAG_IN_IDHASH;
603 /* call with cell write-locked and mutex held */
604 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
610 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
611 /* hash it out first */
612 i = CM_CELL_ID_HASH(cellp->cellID);
613 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
615 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
616 if (tcellp == cellp) {
617 *lcellpp = cellp->idNextp;
618 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
619 cellp->idNextp = NULL;