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 typedef struct cm_cell_rock {
36 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep)
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))
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, probe ? 0 : CM_FLAG_NOPROBE);
62 /* Insert the vlserver into a sorted list, sorted by server rank */
63 tsrp = cm_NewServerRef(tsp, 0);
64 cm_InsertServerList(&cellp->vlServersp, tsrp);
65 /* drop the allocation reference */
66 lock_ObtainWrite(&cm_serverLock);
68 lock_ReleaseWrite(&cm_serverLock);
73 /* if it's from DNS, see if it has expired
74 * and check to make sure we have a valid set of volume servers
75 * this function must be called with a Write Lock on cm_cellLock
77 static cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
85 lock_ObtainMutex(&cp->mx);
86 if ((cp->vlServersp == NULL
87 #ifdef AFS_FREELANCE_CLIENT
88 && !(cp->flags & CM_CELLFLAG_FREELANCE)
90 ) || (time(0) > cp->timeout)
92 || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
93 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
96 /* must empty cp->vlServersp */
98 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
99 cp->vlServersp = NULL;
104 code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, &rock);
110 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
111 if (code == 0) { /* got cell from DNS */
112 cp->flags |= CM_CELLFLAG_DNS;
113 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
114 cp->timeout = time(0) + ttl;
116 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
119 /* if we fail to find it this time, we'll just do nothing and leave the
120 * current entry alone
122 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
126 #endif /* AFS_AFSDB_ENV */
128 cp->timeout = time(0) + 7200;
131 lock_ReleaseMutex(&cp->mx);
132 return code ? NULL : cp;
135 /* load up a cell structure from the cell database, afsdcell.ini */
136 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
138 return cm_GetCell_Gen(namep, NULL, flags);
141 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
145 char fullname[200]="";
146 int hasWriteLock = 0;
150 if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
153 hash = CM_CELL_NAME_HASH(namep);
155 lock_ObtainRead(&cm_cellLock);
156 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
157 if (stricmp(namep, cp->name) == 0) {
158 strcpy(fullname, cp->name);
164 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
165 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
166 strcpy(fullname, cp->name);
172 lock_ReleaseRead(&cm_cellLock);
175 cm_UpdateCell(cp, flags);
176 } else if (flags & CM_FLAG_CREATE) {
177 lock_ObtainWrite(&cm_cellLock);
180 /* when we dropped the lock the cell could have been added
181 * to the list so check again while holding the write lock
183 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
184 if (stricmp(namep, cp->name) == 0) {
185 strcpy(fullname, cp->name);
193 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
194 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
195 strcpy(fullname, cp->name);
203 if ( cm_data.currentCells >= cm_data.maxCells )
204 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
206 /* don't increment currentCells until we know that we
207 * are going to keep this entry
209 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
210 memset(cp, 0, sizeof(cm_cell_t));
211 cp->magic = CM_CELL_MAGIC;
215 code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, &rock);
217 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s",
218 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
224 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
226 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
227 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
230 } else { /* got cell from DNS */
231 cp->flags |= CM_CELLFLAG_DNS;
232 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
233 cp->timeout = time(0) + ttl;
243 cp->timeout = time(0) + 7200; /* two hour timeout */
246 /* we have now been given the fullname of the cell. It may
247 * be that we already have a cell with that name. If so,
248 * we should use it instead of completing the allocation
251 hash = CM_CELL_NAME_HASH(fullname);
252 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
253 if (stricmp(fullname, cp2->name) == 0) {
259 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
265 /* randomise among those vlservers having the same rank*/
266 cm_RandomizeServer(&cp->vlServersp);
268 /* otherwise we found the cell, and so we're nearly done */
269 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
272 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
273 cp->name[CELL_MAXNAMELEN-1] = '\0';
275 /* the cellID cannot be 0 */
276 cp->cellID = ++cm_data.currentCells;
278 /* append cell to global list */
279 if (cm_data.allCellsp == NULL) {
280 cm_data.allCellsp = cp;
282 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
288 cm_AddCellToNameHashTable(cp);
289 cm_AddCellToIDHashTable(cp);
294 lock_ReleaseWrite(&cm_cellLock);
296 /* fullname is not valid if cp == NULL */
298 strcpy(newnamep, fullname);
303 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
308 lock_ObtainRead(&cm_cellLock);
310 hash = CM_CELL_ID_HASH(cellID);
312 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
313 if (cellID == cp->cellID)
316 lock_ReleaseRead(&cm_cellLock);
319 cm_UpdateCell(cp, flags);
325 cm_ValidateCell(void)
330 for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->allNextp, count++) {
331 if ( cellp->magic != CM_CELL_MAGIC ) {
332 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
333 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
336 if ( count != 0 && cellp == cm_data.allCellsp ||
337 count > cm_data.maxCells ) {
338 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
339 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
344 if ( count != cm_data.currentCells ) {
345 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
346 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
355 cm_ShutdownCell(void)
359 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
360 lock_FinalizeMutex(&cellp->mx);
366 void cm_InitCell(int newFile, long maxCells)
368 static osi_once_t once;
370 if (osi_Once(&once)) {
373 lock_InitializeRWLock(&cm_cellLock, "cell global lock");
376 cm_data.allCellsp = NULL;
377 cm_data.currentCells = 0;
378 cm_data.maxCells = maxCells;
379 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
380 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
382 #ifdef AFS_FREELANCE_CLIENT
383 /* Generate a dummy entry for the Freelance cell whether or not
384 * freelance mode is being used in this session
387 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
388 memset(cellp, 0, sizeof(cm_cell_t));
389 cellp->magic = CM_CELL_MAGIC;
391 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
394 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
396 /* thread on global list */
397 cellp->allNextp = cm_data.allCellsp;
398 cm_data.allCellsp = cellp;
400 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
401 cellp->vlServersp = NULL;
402 cellp->flags = CM_CELLFLAG_FREELANCE;
404 cm_AddCellToNameHashTable(cellp);
405 cm_AddCellToIDHashTable(cellp);
408 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
409 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
410 cellp->vlServersp = NULL;
418 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
423 cp = tsp->cellp; /* cell that this vlserver belongs to */
425 lock_ObtainMutex(&cp->mx);
426 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
428 if ( !code ) /* if the server list was rearranged */
429 cm_RandomizeServer(&cp->vlServersp);
431 lock_ReleaseMutex(&cp->mx);
435 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
442 lock_ObtainRead(&cm_cellLock);
444 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
445 cookie, cm_data.currentCells, cm_data.maxCells);
446 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
448 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
449 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x\r\n",
450 cookie, cellp, cellp->name, cellp->cellID, cellp->flags);
451 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
454 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
455 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
458 lock_ReleaseRead(&cm_cellLock);
463 /* call with volume write-locked and mutex held */
464 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
468 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
471 i = CM_CELL_NAME_HASH(cellp->name);
473 cellp->nameNextp = cm_data.cellNameHashTablep[i];
474 cm_data.cellNameHashTablep[i] = cellp;
475 cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
478 /* call with cell write-locked and mutex held */
479 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
485 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
486 /* hash it out first */
487 i = CM_CELL_NAME_HASH(cellp->name);
488 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
490 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
491 if (tcellp == cellp) {
492 *lcellpp = cellp->nameNextp;
493 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
494 cellp->nameNextp = NULL;
501 /* call with cell write-locked and mutex held */
502 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
506 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
509 i = CM_CELL_ID_HASH(cellp->cellID);
511 cellp->idNextp = cm_data.cellIDHashTablep[i];
512 cm_data.cellIDHashTablep[i] = cellp;
513 cellp->flags |= CM_CELLFLAG_IN_IDHASH;
516 /* call with cell write-locked and mutex held */
517 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
523 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
524 /* hash it out first */
525 i = CM_CELL_ID_HASH(cellp->cellID);
526 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
528 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
529 if (tcellp == cellp) {
530 *lcellpp = cellp->idNextp;
531 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
532 cellp->idNextp = NULL;