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, 0);
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, CM_FREESERVERLIST_DELETE);
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_ObtainRead(&cm_cellLock);
131 for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
132 if (stricmp(namep, cp->name) == 0) {
133 strcpy(fullname, cp->name);
137 lock_ReleaseRead(&cm_cellLock);
140 cp = cm_UpdateCell(cp);
141 } else if (flags & CM_FLAG_CREATE) {
142 lock_ObtainWrite(&cm_cellLock);
144 /* when we dropped the lock the cell could have been added
145 * to the list so check again while holding the write lock
147 for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
148 if (stricmp(namep, cp->name) == 0) {
149 strcpy(fullname, cp->name);
155 lock_ReleaseWrite(&cm_cellLock);
159 if ( cm_data.currentCells >= cm_data.maxCells )
160 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
162 /* don't increment currentCells until we know that we
163 * are going to keep this entry
165 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
166 memset(cp, 0, sizeof(cm_cell_t));
167 cp->magic = CM_CELL_MAGIC;
169 code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, cp);
171 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s",
172 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
178 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, cp);
180 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
181 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
184 } else { /* got cell from DNS */
185 cp->flags |= CM_CELLFLAG_DNS;
186 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
187 cp->timeout = time(0) + ttl;
197 cp->timeout = time(0) + 7200; /* two hour timeout */
200 /* we have now been given the fullname of the cell. It may
201 * be that we already have a cell with that name. If so,
202 * we should use it instead of completing the allocation
205 for (cp2 = cm_data.allCellsp; cp2; cp2=cp2->nextp) {
206 if (stricmp(fullname, cp2->name) == 0) {
212 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
214 lock_ReleaseWrite(&cm_cellLock);
219 /* randomise among those vlservers having the same rank*/
220 cm_RandomizeServer(&cp->vlServersp);
222 /* otherwise we found the cell, and so we're nearly done */
223 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
226 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
227 cp->name[CELL_MAXNAMELEN-1] = '\0';
229 /* thread on global list */
230 cp->nextp = cm_data.allCellsp;
231 cm_data.allCellsp = cp;
233 /* the cellID cannot be 0 */
234 cp->cellID = ++cm_data.currentCells;
235 lock_ReleaseWrite(&cm_cellLock);
239 /* fullname is not valid if cp == NULL */
241 strcpy(newnamep, fullname);
246 cm_cell_t *cm_FindCellByID(afs_int32 cellID)
250 lock_ObtainRead(&cm_cellLock);
251 for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
252 if (cellID == cp->cellID)
255 lock_ReleaseRead(&cm_cellLock);
258 cp = cm_UpdateCell(cp);
264 cm_ValidateCell(void)
269 for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->nextp, count++) {
270 if ( cellp->magic != CM_CELL_MAGIC ) {
271 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
272 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
275 if ( count != 0 && cellp == cm_data.allCellsp ||
276 count > cm_data.maxCells ) {
277 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
278 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
283 if ( count != cm_data.currentCells ) {
284 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
285 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
294 cm_ShutdownCell(void)
298 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp)
299 lock_FinalizeMutex(&cellp->mx);
305 void cm_InitCell(int newFile, long maxCells)
307 static osi_once_t once;
309 if (osi_Once(&once)) {
312 lock_InitializeRWLock(&cm_cellLock, "cell global lock");
315 cm_data.allCellsp = NULL;
316 cm_data.currentCells = 0;
317 cm_data.maxCells = maxCells;
319 #ifdef AFS_FREELANCE_CLIENT
320 /* Generate a dummy entry for the Freelance cell whether or not
321 * freelance mode is being used in this session
324 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
325 memset(cellp, 0, sizeof(cm_cell_t));
326 cellp->magic = CM_CELL_MAGIC;
328 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
331 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
333 /* thread on global list */
334 cellp->nextp = cm_data.allCellsp;
335 cm_data.allCellsp = cellp;
337 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
338 cellp->vlServersp = NULL;
339 cellp->flags = CM_CELLFLAG_FREELANCE;
342 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp) {
343 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
344 cellp->vlServersp = NULL;
352 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
357 cp = tsp->cellp; /* cell that this vlserver belongs to */
359 lock_ObtainMutex(&cp->mx);
360 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
362 if ( !code ) /* if the server list was rearranged */
363 cm_RandomizeServer(&cp->vlServersp);
365 lock_ReleaseMutex(&cp->mx);
369 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
376 lock_ObtainRead(&cm_cellLock);
378 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
379 cookie, cm_data.currentCells, cm_data.maxCells);
380 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
382 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp) {
383 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x\r\n",
384 cookie, cellp, cellp->name, cellp->cellID, cellp->flags);
385 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
388 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
389 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
392 lock_ReleaseRead(&cm_cellLock);