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>
26 osi_rwlock_t cm_cellLock;
28 /* function called as callback proc from cm_SearchCellFile. Return 0 to
29 * continue processing.
31 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *namep)
39 /* if this server was previously created by fs setserverprefs */
40 if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
46 tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp);
48 /* Insert the vlserver into a sorted list, sorted by server rank */
49 tsrp = cm_NewServerRef(tsp);
50 cm_InsertServerList(&cellp->vlServersp, tsrp);
51 /* drop the allocation reference */
52 lock_ObtainWrite(&cm_serverLock);
54 lock_ReleaseWrite(&cm_serverLock);
58 /* if it's from DNS, see if it has expired
59 * and check to make sure we have a valid set of volume servers
60 * this function must be called with a Write Lock on cm_cellLock
62 cm_cell_t *cm_UpdateCell(cm_cell_t * cp)
69 if ((cp->vlServersp == NULL
70 #ifdef AFS_FREELANCE_CLIENT
71 && !(cp->flags & CM_CELLFLAG_FREELANCE)
73 ) || (time(0) > cp->timeout)
75 || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
76 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
79 /* must empty cp->vlServersp */
81 cm_FreeServerList(&cp->vlServersp);
82 cp->vlServersp = NULL;
85 code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, cp);
91 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, cp);
92 if (code == 0) { /* got cell from DNS */
93 cp->flags |= CM_CELLFLAG_DNS;
94 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
95 cp->timeout = time(0) + ttl;
97 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
100 /* if we fail to find it this time, we'll just do nothing and leave the
101 * current entry alone
103 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
104 cp = NULL; /* return NULL to indicate failure */
107 #endif /* AFS_AFSDB_ENV */
109 cp = NULL; /* return NULL to indicate failure */
112 cp->timeout = time(0) + 7200;
118 /* load up a cell structure from the cell database, afsdcell.ini */
119 cm_cell_t *cm_GetCell(char *namep, long flags)
121 return cm_GetCell_Gen(namep, NULL, flags);
124 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, long flags)
128 char fullname[200]="";
130 if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
133 lock_ObtainWrite(&cm_cellLock);
134 for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
135 if (strcmp(namep, cp->name) == 0) {
136 strcpy(fullname, cp->name);
142 cp = cm_UpdateCell(cp);
143 } else if (flags & CM_FLAG_CREATE) {
144 if ( cm_data.currentCells >= cm_data.maxCells )
145 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
147 /* don't increment currentCells until we know that we
148 * are going to keep this entry
150 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
151 memset(cp, 0, sizeof(cm_cell_t));
152 cp->magic = CM_CELL_MAGIC;
154 code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, cp);
156 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s",
157 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
163 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, cp);
165 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
166 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
169 } else { /* got cell from DNS */
170 cp->flags |= CM_CELLFLAG_DNS;
171 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
172 cp->timeout = time(0) + ttl;
182 cp->timeout = time(0) + 7200; /* two hour timeout */
185 /* randomise among those vlservers having the same rank*/
186 cm_RandomizeServer(&cp->vlServersp);
188 /* otherwise we found the cell, and so we're nearly done */
189 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
192 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
193 cp->name[CELL_MAXNAMELEN-1] = '\0';
195 /* thread on global list */
196 cp->nextp = cm_data.allCellsp;
197 cm_data.allCellsp = cp;
199 /* the cellID cannot be 0 */
200 cp->cellID = ++cm_data.currentCells;
204 /* fullname is not valid if cp == NULL */
206 strcpy(newnamep, fullname);
208 lock_ReleaseWrite(&cm_cellLock);
212 cm_cell_t *cm_FindCellByID(long cellID)
216 lock_ObtainWrite(&cm_cellLock);
217 for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
218 if (cellID == cp->cellID)
223 cp = cm_UpdateCell(cp);
225 lock_ReleaseWrite(&cm_cellLock);
230 cm_ValidateCell(void)
235 for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->nextp, count++) {
236 if ( cellp->magic != CM_CELL_MAGIC ) {
237 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
238 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
241 if ( count != 0 && cellp == cm_data.allCellsp ||
242 count > cm_data.maxCells ) {
243 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
244 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
249 if ( count != cm_data.currentCells ) {
250 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
251 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
260 cm_ShutdownCell(void)
264 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp)
265 lock_FinalizeMutex(&cellp->mx);
271 void cm_InitCell(int newFile, long maxCells)
273 static osi_once_t once;
275 if (osi_Once(&once)) {
278 lock_InitializeRWLock(&cm_cellLock, "cell global lock");
281 cm_data.allCellsp = NULL;
282 cm_data.currentCells = 0;
283 cm_data.maxCells = maxCells;
285 #ifdef AFS_FREELANCE_CLIENT
286 /* Generate a dummy entry for the Freelance cell whether or not
287 * freelance mode is being used in this session
290 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
291 memset(cellp, 0, sizeof(cm_cell_t));
292 cellp->magic = CM_CELL_MAGIC;
294 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
297 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
299 /* thread on global list */
300 cellp->nextp = cm_data.allCellsp;
301 cm_data.allCellsp = cellp;
303 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
304 cellp->vlServersp = NULL;
305 cellp->flags = CM_CELLFLAG_FREELANCE;
308 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp) {
309 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
310 cellp->vlServersp = NULL;
318 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
323 cp = tsp->cellp; /* cell that this vlserver belongs to */
326 lock_ObtainMutex(&cp->mx);
327 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
329 if ( !code ) /* if the server list was rearranged */
330 cm_RandomizeServer(&cp->vlServersp);
332 lock_ReleaseMutex(&cp->mx);