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)
36 cm_cell_rock_t *cellrockp = (cm_cell_rock_t *)rockp;
39 cellp = cellrockp->cellp;
40 probe = !(cellrockp->flags & CM_FLAG_NOPROBE);
42 /* if this server was previously created by fs setserverprefs */
43 if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
47 else if (tsp->cellp != cellp) {
48 osi_Log3(afsd_logp, "found a vlserver %s associated with two cells named %s and %s",
49 osi_LogSaveString(afsd_logp,hostnamep),
50 osi_LogSaveString(afsd_logp,tsp->cellp->name),
51 osi_LogSaveString(afsd_logp,cellp->name));
55 tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp, probe ? 0 : CM_FLAG_NOPROBE);
57 /* Insert the vlserver into a sorted list, sorted by server rank */
58 tsrp = cm_NewServerRef(tsp, 0);
59 cm_InsertServerList(&cellp->vlServersp, tsrp);
60 /* drop the allocation reference */
61 lock_ObtainWrite(&cm_serverLock);
63 lock_ReleaseWrite(&cm_serverLock);
68 /* if it's from DNS, see if it has expired
69 * and check to make sure we have a valid set of volume servers
70 * this function must be called with a Write Lock on cm_cellLock
72 cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
80 lock_ObtainMutex(&cp->mx);
81 if ((cp->vlServersp == NULL
82 #ifdef AFS_FREELANCE_CLIENT
83 && !(cp->flags & CM_CELLFLAG_FREELANCE)
85 ) || (time(0) > cp->timeout)
87 || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
88 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
91 /* must empty cp->vlServersp */
93 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
94 cp->vlServersp = NULL;
99 code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, &rock);
105 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
106 if (code == 0) { /* got cell from DNS */
107 cp->flags |= CM_CELLFLAG_DNS;
108 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
109 cp->timeout = time(0) + ttl;
111 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
114 /* if we fail to find it this time, we'll just do nothing and leave the
115 * current entry alone
117 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
121 #endif /* AFS_AFSDB_ENV */
123 cp->timeout = time(0) + 7200;
126 lock_ReleaseMutex(&cp->mx);
127 return code ? NULL : cp;
130 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
131 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
133 return cm_GetCell_Gen(namep, NULL, flags);
136 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
140 char fullname[CELL_MAXNAMELEN]="";
141 int hasWriteLock = 0;
145 if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
148 hash = CM_CELL_NAME_HASH(namep);
150 lock_ObtainRead(&cm_cellLock);
151 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
152 if (cm_stricmp_utf8(namep, cp->name) == 0) {
153 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
154 fullname[CELL_MAXNAMELEN-1] = '\0';
160 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
161 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
162 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
163 fullname[CELL_MAXNAMELEN-1] = '\0';
169 lock_ReleaseRead(&cm_cellLock);
172 cm_UpdateCell(cp, flags);
173 } else if (flags & CM_FLAG_CREATE) {
174 lock_ObtainWrite(&cm_cellLock);
177 /* when we dropped the lock the cell could have been added
178 * to the list so check again while holding the write lock
180 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
181 if (cm_stricmp_utf8(namep, cp->name) == 0) {
182 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
183 fullname[CELL_MAXNAMELEN-1] = '\0';
191 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
192 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
193 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
194 fullname[CELL_MAXNAMELEN-1] = '\0';
202 if ( cm_data.currentCells >= cm_data.maxCells )
203 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
205 /* don't increment currentCells until we know that we
206 * are going to keep this entry
208 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
209 memset(cp, 0, sizeof(cm_cell_t));
210 cp->magic = CM_CELL_MAGIC;
214 code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, &rock);
216 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s",
217 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
223 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
225 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
226 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
229 } else { /* got cell from DNS */
230 cp->flags |= CM_CELLFLAG_DNS;
231 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
232 cp->timeout = time(0) + ttl;
242 cp->timeout = time(0) + 7200; /* two hour timeout */
245 /* we have now been given the fullname of the cell. It may
246 * be that we already have a cell with that name. If so,
247 * we should use it instead of completing the allocation
250 hash = CM_CELL_NAME_HASH(fullname);
251 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
252 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
258 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
264 /* randomise among those vlservers having the same rank*/
265 cm_RandomizeServer(&cp->vlServersp);
267 /* otherwise we found the cell, and so we're nearly done */
268 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
271 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
272 cp->name[CELL_MAXNAMELEN-1] = '\0';
274 /* the cellID cannot be 0 */
275 cp->cellID = ++cm_data.currentCells;
277 /* append cell to global list */
278 if (cm_data.allCellsp == NULL) {
279 cm_data.allCellsp = cp;
281 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
287 cm_AddCellToNameHashTable(cp);
288 cm_AddCellToIDHashTable(cp);
293 lock_ReleaseWrite(&cm_cellLock);
295 /* fullname is not valid if cp == NULL */
296 if (cp && newnamep) {
297 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
298 newnamep[CELL_MAXNAMELEN-1]='\0';
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*/
395 cellp->name[CELL_MAXNAMELEN-1] = '\0';
397 /* thread on global list */
398 cellp->allNextp = cm_data.allCellsp;
399 cm_data.allCellsp = cellp;
401 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
402 cellp->vlServersp = NULL;
403 cellp->flags = CM_CELLFLAG_FREELANCE;
405 cm_AddCellToNameHashTable(cellp);
406 cm_AddCellToIDHashTable(cellp);
409 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
410 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
411 cellp->vlServersp = NULL;
412 cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
420 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
425 cp = tsp->cellp; /* cell that this vlserver belongs to */
427 lock_ObtainMutex(&cp->mx);
428 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
430 if ( !code ) /* if the server list was rearranged */
431 cm_RandomizeServer(&cp->vlServersp);
433 lock_ReleaseMutex(&cp->mx);
437 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
444 lock_ObtainRead(&cm_cellLock);
446 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
447 cookie, cm_data.currentCells, cm_data.maxCells);
448 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
450 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
451 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x\r\n",
452 cookie, cellp, cellp->name, cellp->cellID, cellp->flags);
453 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
456 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
457 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
460 lock_ReleaseRead(&cm_cellLock);
465 /* call with volume write-locked and mutex held */
466 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
470 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
473 i = CM_CELL_NAME_HASH(cellp->name);
475 cellp->nameNextp = cm_data.cellNameHashTablep[i];
476 cm_data.cellNameHashTablep[i] = cellp;
477 cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
480 /* call with cell write-locked and mutex held */
481 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
487 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
488 /* hash it out first */
489 i = CM_CELL_NAME_HASH(cellp->name);
490 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
492 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
493 if (tcellp == cellp) {
494 *lcellpp = cellp->nameNextp;
495 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
496 cellp->nameNextp = NULL;
503 /* call with cell write-locked and mutex held */
504 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
508 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
511 i = CM_CELL_ID_HASH(cellp->cellID);
513 cellp->idNextp = cm_data.cellIDHashTablep[i];
514 cm_data.cellIDHashTablep[i] = cellp;
515 cellp->flags |= CM_CELLFLAG_IN_IDHASH;
518 /* call with cell write-locked and mutex held */
519 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
525 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
526 /* hash it out first */
527 i = CM_CELL_ID_HASH(cellp->cellID);
528 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
530 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
531 if (tcellp == cellp) {
532 *lcellpp = cellp->idNextp;
533 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
534 cellp->idNextp = NULL;