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 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *namep)
37 /* if this server was previously created by fs setserverprefs */
38 if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
44 tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp);
46 /* Insert the vlserver into a sorted list, sorted by server rank */
47 tsrp = cm_NewServerRef(tsp);
48 cm_InsertServerList(&cellp->vlServersp, tsrp);
49 /* drop the allocation reference */
50 lock_ObtainWrite(&cm_serverLock);
52 lock_ReleaseWrite(&cm_serverLock);
56 /* if it's from DNS, see if it has expired
57 * and check to make sure we have a valid set of volume servers
58 * this function must be called with a Write Lock on cm_cellLock
60 cm_cell_t *cm_UpdateCell(cm_cell_t * cp)
67 lock_ObtainMutex(&cp->mx);
68 if ((cp->vlServersp == NULL
69 #ifdef AFS_FREELANCE_CLIENT
70 && !(cp->flags & CM_CELLFLAG_FREELANCE)
72 ) || (time(0) > cp->timeout)
74 || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
75 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
78 /* must empty cp->vlServersp */
80 cm_FreeServerList(&cp->vlServersp);
81 cp->vlServersp = NULL;
84 code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, cp);
90 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, cp);
91 if (code == 0) { /* got cell from DNS */
92 cp->flags |= CM_CELLFLAG_DNS;
93 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
94 cp->timeout = time(0) + ttl;
96 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
99 /* if we fail to find it this time, we'll just do nothing and leave the
100 * current entry alone
102 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
106 #endif /* AFS_AFSDB_ENV */
108 cp->timeout = time(0) + 7200;
111 lock_ReleaseMutex(&cp->mx);
112 return code ? NULL : cp;
115 /* load up a cell structure from the cell database, afsdcell.ini */
116 cm_cell_t *cm_GetCell(char *namep, long flags)
118 return cm_GetCell_Gen(namep, NULL, flags);
121 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, long flags)
125 char fullname[200]="";
127 if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
130 lock_ObtainWrite(&cm_cellLock);
131 for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
132 if (stricmp(namep, cp->name) == 0) {
133 strcpy(fullname, cp->name);
139 cp = cm_UpdateCell(cp);
140 } else if (flags & CM_FLAG_CREATE) {
141 if ( cm_data.currentCells >= cm_data.maxCells )
142 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
144 /* don't increment currentCells until we know that we
145 * are going to keep this entry
147 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
148 memset(cp, 0, sizeof(cm_cell_t));
149 cp->magic = CM_CELL_MAGIC;
151 code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, cp);
153 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s",
154 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
160 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, cp);
162 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
163 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
166 } else { /* got cell from DNS */
167 cp->flags |= CM_CELLFLAG_DNS;
168 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
169 cp->timeout = time(0) + ttl;
179 cp->timeout = time(0) + 7200; /* two hour timeout */
182 /* randomise among those vlservers having the same rank*/
183 cm_RandomizeServer(&cp->vlServersp);
185 /* otherwise we found the cell, and so we're nearly done */
186 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
189 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
190 cp->name[CELL_MAXNAMELEN-1] = '\0';
192 /* thread on global list */
193 cp->nextp = cm_data.allCellsp;
194 cm_data.allCellsp = cp;
196 /* the cellID cannot be 0 */
197 cp->cellID = ++cm_data.currentCells;
201 /* fullname is not valid if cp == NULL */
203 strcpy(newnamep, fullname);
205 lock_ReleaseWrite(&cm_cellLock);
209 cm_cell_t *cm_FindCellByID(long cellID)
213 lock_ObtainWrite(&cm_cellLock);
214 for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
215 if (cellID == cp->cellID)
220 cp = cm_UpdateCell(cp);
222 lock_ReleaseWrite(&cm_cellLock);
227 cm_ValidateCell(void)
232 for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->nextp, count++) {
233 if ( cellp->magic != CM_CELL_MAGIC ) {
234 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
235 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
238 if ( count != 0 && cellp == cm_data.allCellsp ||
239 count > cm_data.maxCells ) {
240 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
241 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
246 if ( count != cm_data.currentCells ) {
247 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
248 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
257 cm_ShutdownCell(void)
261 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp)
262 lock_FinalizeMutex(&cellp->mx);
268 void cm_InitCell(int newFile, long maxCells)
270 static osi_once_t once;
272 if (osi_Once(&once)) {
275 lock_InitializeRWLock(&cm_cellLock, "cell global lock");
278 cm_data.allCellsp = NULL;
279 cm_data.currentCells = 0;
280 cm_data.maxCells = maxCells;
282 #ifdef AFS_FREELANCE_CLIENT
283 /* Generate a dummy entry for the Freelance cell whether or not
284 * freelance mode is being used in this session
287 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
288 memset(cellp, 0, sizeof(cm_cell_t));
289 cellp->magic = CM_CELL_MAGIC;
291 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
294 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
296 /* thread on global list */
297 cellp->nextp = cm_data.allCellsp;
298 cm_data.allCellsp = cellp;
300 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
301 cellp->vlServersp = NULL;
302 cellp->flags = CM_CELLFLAG_FREELANCE;
305 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp) {
306 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
307 cellp->vlServersp = NULL;
315 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
320 cp = tsp->cellp; /* cell that this vlserver belongs to */
322 lock_ObtainMutex(&cp->mx);
323 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
325 if ( !code ) /* if the server list was rearranged */
326 cm_RandomizeServer(&cp->vlServersp);
328 lock_ReleaseMutex(&cp->mx);