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)))
92 lock_ReleaseMutex(&cp->mx);
94 /* must empty cp->vlServersp */
96 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
97 cp->vlServersp = NULL;
102 code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, &rock);
104 lock_ObtainMutex(&cp->mx);
105 cp->timeout = time(0) + 7200;
106 lock_ReleaseMutex(&cp->mx);
113 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
114 if (code == 0) { /* got cell from DNS */
115 lock_ObtainMutex(&cp->mx);
116 cp->flags |= CM_CELLFLAG_DNS;
117 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
118 cp->timeout = time(0) + ttl;
119 lock_ReleaseMutex(&cp->mx);
121 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
124 /* if we fail to find it this time, we'll just do nothing and leave the
125 * current entry alone
127 lock_ObtainMutex(&cp->mx);
128 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
129 lock_ReleaseMutex(&cp->mx);
133 #endif /* AFS_AFSDB_ENV */
135 lock_ReleaseMutex(&cp->mx);
137 return code ? NULL : cp;
140 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
141 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
143 return cm_GetCell_Gen(namep, NULL, flags);
146 void cm_FreeCell(cm_cell_t *cellp)
148 if (cellp->vlServersp)
149 cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
150 cellp->name[0] = '\0';
152 cellp->freeNextp = cm_data.freeCellsp;
153 cm_data.freeCellsp = cellp;
156 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
160 char fullname[CELL_MAXNAMELEN]="";
161 int hasWriteLock = 0;
166 if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
169 hash = CM_CELL_NAME_HASH(namep);
171 lock_ObtainRead(&cm_cellLock);
172 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
173 if (cm_stricmp_utf8(namep, cp->name) == 0) {
174 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
175 fullname[CELL_MAXNAMELEN-1] = '\0';
181 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
182 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
183 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
184 fullname[CELL_MAXNAMELEN-1] = '\0';
191 lock_ReleaseRead(&cm_cellLock);
192 cm_UpdateCell(cp, flags);
193 } else if (flags & CM_FLAG_CREATE) {
194 lock_ConvertRToW(&cm_cellLock);
197 /* when we dropped the lock the cell could have been added
198 * to the list so check again while holding the write lock
200 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
201 if (cm_stricmp_utf8(namep, cp->name) == 0) {
202 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
203 fullname[CELL_MAXNAMELEN-1] = '\0';
211 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
212 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
213 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
214 fullname[CELL_MAXNAMELEN-1] = '\0';
220 lock_ObtainMutex(&cp->mx);
221 cm_AddCellToNameHashTable(cp);
222 cm_AddCellToIDHashTable(cp);
223 lock_ReleaseMutex(&cp->mx);
227 if ( cm_data.freeCellsp != NULL ) {
228 cp = cm_data.freeCellsp;
229 cm_data.freeCellsp = cp->freeNextp;
232 * The magic, cellID, and mx fields are already set.
235 if ( cm_data.currentCells >= cm_data.maxCells )
236 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
238 /* don't increment currentCells until we know that we
239 * are going to keep this entry
241 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
242 memset(cp, 0, sizeof(cm_cell_t));
243 cp->magic = CM_CELL_MAGIC;
245 /* the cellID cannot be 0 */
246 cp->cellID = ++cm_data.currentCells;
248 /* otherwise we found the cell, and so we're nearly done */
249 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
252 lock_ReleaseWrite(&cm_cellLock);
257 code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, &rock);
259 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s",
260 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
266 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
268 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
269 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
273 } else { /* got cell from DNS */
274 lock_ObtainMutex(&cp->mx);
276 cp->flags |= CM_CELLFLAG_DNS;
277 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
278 cp->timeout = time(0) + ttl;
289 lock_ObtainMutex(&cp->mx);
291 cp->timeout = time(0) + 7200; /* two hour timeout */
294 /* we have now been given the fullname of the cell. It may
295 * be that we already have a cell with that name. If so,
296 * we should use it instead of completing the allocation
299 hash = CM_CELL_NAME_HASH(fullname);
300 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
301 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
308 lock_ReleaseMutex(&cp->mx);
316 /* randomise among those vlservers having the same rank*/
317 cm_RandomizeServer(&cp->vlServersp);
320 lock_ObtainMutex(&cp->mx);
322 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
323 cp->name[CELL_MAXNAMELEN-1] = '\0';
325 cm_AddCellToNameHashTable(cp);
326 cm_AddCellToIDHashTable(cp);
327 lock_ReleaseMutex(&cp->mx);
330 /* append cell to global list */
331 if (cm_data.allCellsp == NULL) {
332 cm_data.allCellsp = cp;
334 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
341 lock_ReleaseRead(&cm_cellLock);
345 lock_ReleaseMutex(&cp->mx);
347 lock_ReleaseWrite(&cm_cellLock);
349 /* fullname is not valid if cp == NULL */
352 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
353 newnamep[CELL_MAXNAMELEN-1]='\0';
361 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
366 lock_ObtainRead(&cm_cellLock);
368 hash = CM_CELL_ID_HASH(cellID);
370 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
371 if (cellID == cp->cellID)
374 lock_ReleaseRead(&cm_cellLock);
377 cm_UpdateCell(cp, flags);
383 cm_ValidateCell(void)
388 for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->allNextp, count++) {
389 if ( cellp->magic != CM_CELL_MAGIC ) {
390 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
391 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
394 if ( count != 0 && cellp == cm_data.allCellsp ||
395 count > cm_data.maxCells ) {
396 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
397 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
402 if ( count != cm_data.currentCells ) {
403 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
404 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
413 cm_ShutdownCell(void)
417 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
418 lock_FinalizeMutex(&cellp->mx);
424 void cm_InitCell(int newFile, long maxCells)
426 static osi_once_t once;
428 if (osi_Once(&once)) {
431 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
434 cm_data.allCellsp = NULL;
435 cm_data.currentCells = 0;
436 cm_data.maxCells = maxCells;
437 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
438 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
440 #ifdef AFS_FREELANCE_CLIENT
441 /* Generate a dummy entry for the Freelance cell whether or not
442 * freelance mode is being used in this session
445 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
446 memset(cellp, 0, sizeof(cm_cell_t));
447 cellp->magic = CM_CELL_MAGIC;
449 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
452 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
453 cellp->name[CELL_MAXNAMELEN-1] = '\0';
455 /* thread on global list */
456 cellp->allNextp = cm_data.allCellsp;
457 cm_data.allCellsp = cellp;
459 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
460 cellp->vlServersp = NULL;
461 cellp->flags = CM_CELLFLAG_FREELANCE;
463 lock_ObtainMutex(&cellp->mx);
464 cm_AddCellToNameHashTable(cellp);
465 cm_AddCellToIDHashTable(cellp);
466 lock_ReleaseMutex(&cellp->mx);
469 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
470 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
471 cellp->vlServersp = NULL;
472 cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
480 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
485 cp = tsp->cellp; /* cell that this vlserver belongs to */
487 lock_ObtainMutex(&cp->mx);
488 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
490 if ( !code ) /* if the server list was rearranged */
491 cm_RandomizeServer(&cp->vlServersp);
493 lock_ReleaseMutex(&cp->mx);
497 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
504 lock_ObtainRead(&cm_cellLock);
506 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
507 cookie, cm_data.currentCells, cm_data.maxCells);
508 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
510 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
511 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
512 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
513 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
516 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
517 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
520 lock_ReleaseRead(&cm_cellLock);
525 /* call with volume write-locked and mutex held */
526 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
530 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
533 i = CM_CELL_NAME_HASH(cellp->name);
535 cellp->nameNextp = cm_data.cellNameHashTablep[i];
536 cm_data.cellNameHashTablep[i] = cellp;
537 cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
540 /* call with cell write-locked and mutex held */
541 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
547 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
548 /* hash it out first */
549 i = CM_CELL_NAME_HASH(cellp->name);
550 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
552 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
553 if (tcellp == cellp) {
554 *lcellpp = cellp->nameNextp;
555 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
556 cellp->nameNextp = NULL;
563 /* call with cell write-locked and mutex held */
564 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
568 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
571 i = CM_CELL_ID_HASH(cellp->cellID);
573 cellp->idNextp = cm_data.cellIDHashTablep[i];
574 cm_data.cellIDHashTablep[i] = cellp;
575 cellp->flags |= CM_CELLFLAG_IN_IDHASH;
578 /* call with cell write-locked and mutex held */
579 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
585 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
586 /* hash it out first */
587 i = CM_CELL_ID_HASH(cellp->cellID);
588 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
590 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
591 if (tcellp == cellp) {
592 *lcellpp = cellp->idNextp;
593 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
594 cellp->idNextp = NULL;