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)
39 /* if this server was previously created by fs setserverprefs */
40 if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
44 else if (tsp->cellp != cellp) {
45 osi_Log3(afsd_logp, "found a vlserver %s associated with two cells named %s and %s",
46 osi_LogSaveString(afsd_logp,hostnamep),
47 osi_LogSaveString(afsd_logp,tsp->cellp->name),
48 osi_LogSaveString(afsd_logp,cellp->name));
52 tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp);
54 /* Insert the vlserver into a sorted list, sorted by server rank */
55 tsrp = cm_NewServerRef(tsp, 0);
56 cm_InsertServerList(&cellp->vlServersp, tsrp);
57 /* drop the allocation reference */
58 lock_ObtainWrite(&cm_serverLock);
60 lock_ReleaseWrite(&cm_serverLock);
65 /* if it's from DNS, see if it has expired
66 * and check to make sure we have a valid set of volume servers
67 * this function must be called with a Write Lock on cm_cellLock
69 cm_cell_t *cm_UpdateCell(cm_cell_t * cp)
76 lock_ObtainMutex(&cp->mx);
77 if ((cp->vlServersp == NULL
78 #ifdef AFS_FREELANCE_CLIENT
79 && !(cp->flags & CM_CELLFLAG_FREELANCE)
81 ) || (time(0) > cp->timeout)
83 || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
84 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
87 /* must empty cp->vlServersp */
89 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
90 cp->vlServersp = NULL;
93 code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, cp);
99 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, cp);
100 if (code == 0) { /* got cell from DNS */
101 cp->flags |= CM_CELLFLAG_DNS;
102 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
103 cp->timeout = time(0) + ttl;
105 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
108 /* if we fail to find it this time, we'll just do nothing and leave the
109 * current entry alone
111 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
115 #endif /* AFS_AFSDB_ENV */
117 cp->timeout = time(0) + 7200;
120 lock_ReleaseMutex(&cp->mx);
121 return code ? NULL : cp;
124 /* load up a cell structure from the cell database, afsdcell.ini */
125 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
127 return cm_GetCell_Gen(namep, NULL, flags);
130 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
134 char fullname[200]="";
135 int hasWriteLock = 0;
138 if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
141 hash = CM_CELL_NAME_HASH(namep);
143 lock_ObtainRead(&cm_cellLock);
144 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
145 if (stricmp(namep, cp->name) == 0) {
146 strcpy(fullname, cp->name);
152 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
153 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
154 strcpy(fullname, cp->name);
160 lock_ReleaseRead(&cm_cellLock);
164 } else if (flags & CM_FLAG_CREATE) {
165 lock_ObtainWrite(&cm_cellLock);
168 /* when we dropped the lock the cell could have been added
169 * to the list so check again while holding the write lock
171 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
172 if (stricmp(namep, cp->name) == 0) {
173 strcpy(fullname, cp->name);
181 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
182 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
183 strcpy(fullname, cp->name);
191 if ( cm_data.currentCells >= cm_data.maxCells )
192 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
194 /* don't increment currentCells until we know that we
195 * are going to keep this entry
197 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
198 memset(cp, 0, sizeof(cm_cell_t));
199 cp->magic = CM_CELL_MAGIC;
201 code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, cp);
203 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s",
204 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
210 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, cp);
212 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
213 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
216 } else { /* got cell from DNS */
217 cp->flags |= CM_CELLFLAG_DNS;
218 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
219 cp->timeout = time(0) + ttl;
229 cp->timeout = time(0) + 7200; /* two hour timeout */
232 /* we have now been given the fullname of the cell. It may
233 * be that we already have a cell with that name. If so,
234 * we should use it instead of completing the allocation
237 hash = CM_CELL_NAME_HASH(fullname);
238 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
239 if (stricmp(fullname, cp2->name) == 0) {
245 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
251 /* randomise among those vlservers having the same rank*/
252 cm_RandomizeServer(&cp->vlServersp);
254 /* otherwise we found the cell, and so we're nearly done */
255 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
258 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
259 cp->name[CELL_MAXNAMELEN-1] = '\0';
261 /* the cellID cannot be 0 */
262 cp->cellID = ++cm_data.currentCells;
264 /* append cell to global list */
265 if (cm_data.allCellsp == NULL) {
266 cm_data.allCellsp = cp;
268 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
274 cm_AddCellToNameHashTable(cp);
275 cm_AddCellToIDHashTable(cp);
280 lock_ReleaseWrite(&cm_cellLock);
282 /* fullname is not valid if cp == NULL */
284 strcpy(newnamep, fullname);
289 cm_cell_t *cm_FindCellByID(afs_int32 cellID)
294 lock_ObtainRead(&cm_cellLock);
296 hash = CM_CELL_ID_HASH(cellID);
298 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
299 if (cellID == cp->cellID)
302 lock_ReleaseRead(&cm_cellLock);
311 cm_ValidateCell(void)
316 for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->allNextp, count++) {
317 if ( cellp->magic != CM_CELL_MAGIC ) {
318 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
319 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
322 if ( count != 0 && cellp == cm_data.allCellsp ||
323 count > cm_data.maxCells ) {
324 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
325 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
330 if ( count != cm_data.currentCells ) {
331 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
332 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
341 cm_ShutdownCell(void)
345 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
346 lock_FinalizeMutex(&cellp->mx);
352 void cm_InitCell(int newFile, long maxCells)
354 static osi_once_t once;
356 if (osi_Once(&once)) {
359 lock_InitializeRWLock(&cm_cellLock, "cell global lock");
362 cm_data.allCellsp = NULL;
363 cm_data.currentCells = 0;
364 cm_data.maxCells = maxCells;
365 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
366 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
368 #ifdef AFS_FREELANCE_CLIENT
369 /* Generate a dummy entry for the Freelance cell whether or not
370 * freelance mode is being used in this session
373 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
374 memset(cellp, 0, sizeof(cm_cell_t));
375 cellp->magic = CM_CELL_MAGIC;
377 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
380 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
382 /* thread on global list */
383 cellp->allNextp = cm_data.allCellsp;
384 cm_data.allCellsp = cellp;
386 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
387 cellp->vlServersp = NULL;
388 cellp->flags = CM_CELLFLAG_FREELANCE;
390 cm_AddCellToNameHashTable(cellp);
391 cm_AddCellToIDHashTable(cellp);
394 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
395 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
396 cellp->vlServersp = NULL;
404 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
409 cp = tsp->cellp; /* cell that this vlserver belongs to */
411 lock_ObtainMutex(&cp->mx);
412 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
414 if ( !code ) /* if the server list was rearranged */
415 cm_RandomizeServer(&cp->vlServersp);
417 lock_ReleaseMutex(&cp->mx);
421 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
428 lock_ObtainRead(&cm_cellLock);
430 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
431 cookie, cm_data.currentCells, cm_data.maxCells);
432 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
434 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
435 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x\r\n",
436 cookie, cellp, cellp->name, cellp->cellID, cellp->flags);
437 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
440 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
441 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
444 lock_ReleaseRead(&cm_cellLock);
449 /* call with volume write-locked and mutex held */
450 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
454 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
457 i = CM_CELL_NAME_HASH(cellp->name);
459 cellp->nameNextp = cm_data.cellNameHashTablep[i];
460 cm_data.cellNameHashTablep[i] = cellp;
461 cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
464 /* call with cell write-locked and mutex held */
465 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
471 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
472 /* hash it out first */
473 i = CM_CELL_NAME_HASH(cellp->name);
474 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
476 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
477 if (tcellp == cellp) {
478 *lcellpp = cellp->nameNextp;
479 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
480 cellp->nameNextp = NULL;
487 /* call with cell write-locked and mutex held */
488 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
492 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
495 i = CM_CELL_ID_HASH(cellp->cellID);
497 cellp->idNextp = cm_data.cellIDHashTablep[i];
498 cm_data.cellIDHashTablep[i] = cellp;
499 cellp->flags |= CM_CELLFLAG_IN_IDHASH;
502 /* call with cell write-locked and mutex held */
503 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
509 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
510 /* hash it out first */
511 i = CM_CELL_ID_HASH(cellp->cellID);
512 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
514 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
515 if (tcellp == cellp) {
516 *lcellpp = cellp->idNextp;
517 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
518 cellp->idNextp = NULL;