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, NULL, 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_SearchCellFileEx(cp->name, NULL, cp->linkedName, 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 char linkedName[CELL_MAXNAMELEN]="";
162 char name[CELL_MAXNAMELEN]="";
163 int hasWriteLock = 0;
169 if (namep == NULL || !namep[0] || !strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
173 * Strip off any trailing dots at the end of the cell name.
174 * Failure to do so results in an undesireable alias as the
175 * result of DNS AFSDB record lookups where a trailing dot
176 * has special meaning.
178 strncpy(name, namep, CELL_MAXNAMELEN);
179 for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
186 hash = CM_CELL_NAME_HASH(namep);
188 lock_ObtainRead(&cm_cellLock);
189 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
190 if (cm_stricmp_utf8(namep, cp->name) == 0) {
191 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
192 fullname[CELL_MAXNAMELEN-1] = '\0';
198 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
199 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
200 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
201 fullname[CELL_MAXNAMELEN-1] = '\0';
208 lock_ReleaseRead(&cm_cellLock);
209 cm_UpdateCell(cp, flags);
210 } else if (flags & CM_FLAG_CREATE) {
211 lock_ConvertRToW(&cm_cellLock);
214 /* when we dropped the lock the cell could have been added
215 * to the list so check again while holding the write lock
217 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
218 if (cm_stricmp_utf8(namep, cp->name) == 0) {
219 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
220 fullname[CELL_MAXNAMELEN-1] = '\0';
228 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
229 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
230 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
231 fullname[CELL_MAXNAMELEN-1] = '\0';
237 lock_ObtainMutex(&cp->mx);
238 cm_AddCellToNameHashTable(cp);
239 cm_AddCellToIDHashTable(cp);
240 lock_ReleaseMutex(&cp->mx);
244 if ( cm_data.freeCellsp != NULL ) {
245 cp = cm_data.freeCellsp;
246 cm_data.freeCellsp = cp->freeNextp;
249 * The magic, cellID, and mx fields are already set.
252 if ( cm_data.currentCells >= cm_data.maxCells )
253 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
255 /* don't increment currentCells until we know that we
256 * are going to keep this entry
258 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
259 memset(cp, 0, sizeof(cm_cell_t));
260 cp->magic = CM_CELL_MAGIC;
262 /* the cellID cannot be 0 */
263 cp->cellID = ++cm_data.currentCells;
265 /* otherwise we found the cell, and so we're nearly done */
266 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
269 lock_ReleaseWrite(&cm_cellLock);
274 code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
276 osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s",
277 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
278 osi_LogSaveString(afsd_logp,linkedName));
284 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
286 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
287 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
291 } else { /* got cell from DNS */
292 lock_ObtainMutex(&cp->mx);
294 cp->flags |= CM_CELLFLAG_DNS;
295 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
296 cp->timeout = time(0) + ttl;
307 lock_ObtainMutex(&cp->mx);
309 cp->timeout = time(0) + 7200; /* two hour timeout */
312 /* we have now been given the fullname of the cell. It may
313 * be that we already have a cell with that name. If so,
314 * we should use it instead of completing the allocation
317 hash = CM_CELL_NAME_HASH(fullname);
318 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
319 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
326 lock_ReleaseMutex(&cp->mx);
334 /* randomise among those vlservers having the same rank*/
335 cm_RandomizeServer(&cp->vlServersp);
338 lock_ObtainMutex(&cp->mx);
340 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
341 cp->name[CELL_MAXNAMELEN-1] = '\0';
343 strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
344 cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
346 cm_AddCellToNameHashTable(cp);
347 cm_AddCellToIDHashTable(cp);
348 lock_ReleaseMutex(&cp->mx);
351 /* append cell to global list */
352 if (cm_data.allCellsp == NULL) {
353 cm_data.allCellsp = cp;
355 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
362 lock_ReleaseRead(&cm_cellLock);
366 lock_ReleaseMutex(&cp->mx);
368 lock_ReleaseWrite(&cm_cellLock);
370 /* fullname is not valid if cp == NULL */
373 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
374 newnamep[CELL_MAXNAMELEN-1]='\0';
380 if (cp && cp->linkedName[0]) {
381 cm_cell_t * linkedCellp = NULL;
383 if (!strcmp(cp->name, cp->linkedName)) {
384 cp->linkedName[0] = '\0';
385 } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
386 linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
388 lock_ObtainWrite(&cm_cellLock);
390 (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
391 cp->linkedName[0] = '\0';
393 strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
394 linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
396 lock_ReleaseWrite(&cm_cellLock);
402 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
407 lock_ObtainRead(&cm_cellLock);
409 hash = CM_CELL_ID_HASH(cellID);
411 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
412 if (cellID == cp->cellID)
415 lock_ReleaseRead(&cm_cellLock);
418 cm_UpdateCell(cp, flags);
424 cm_ValidateCell(void)
429 for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->allNextp, count++) {
430 if ( cellp->magic != CM_CELL_MAGIC ) {
431 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
432 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
435 if ( count != 0 && cellp == cm_data.allCellsp ||
436 count > cm_data.maxCells ) {
437 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
438 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
443 for (cellp = cm_data.freeCellsp; cellp; cellp=cellp->freeNextp, count++) {
444 if ( count != 0 && cellp == cm_data.freeCellsp ||
445 count > cm_data.maxCells ) {
446 afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
447 fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
452 if ( count != cm_data.currentCells ) {
453 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
454 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
463 cm_ShutdownCell(void)
467 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
468 lock_FinalizeMutex(&cellp->mx);
474 void cm_InitCell(int newFile, long maxCells)
476 static osi_once_t once;
478 if (osi_Once(&once)) {
481 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
484 cm_data.allCellsp = NULL;
485 cm_data.currentCells = 0;
486 cm_data.maxCells = maxCells;
487 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
488 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
490 #ifdef AFS_FREELANCE_CLIENT
491 /* Generate a dummy entry for the Freelance cell whether or not
492 * freelance mode is being used in this session
495 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
496 memset(cellp, 0, sizeof(cm_cell_t));
497 cellp->magic = CM_CELL_MAGIC;
499 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
502 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
503 cellp->name[CELL_MAXNAMELEN-1] = '\0';
505 /* thread on global list */
506 cellp->allNextp = cm_data.allCellsp;
507 cm_data.allCellsp = cellp;
509 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
510 cellp->vlServersp = NULL;
511 cellp->flags = CM_CELLFLAG_FREELANCE;
513 lock_ObtainMutex(&cellp->mx);
514 cm_AddCellToNameHashTable(cellp);
515 cm_AddCellToIDHashTable(cellp);
516 lock_ReleaseMutex(&cellp->mx);
519 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
520 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
521 cellp->vlServersp = NULL;
522 cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
530 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
535 cp = tsp->cellp; /* cell that this vlserver belongs to */
537 lock_ObtainMutex(&cp->mx);
538 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
540 if ( !code ) /* if the server list was rearranged */
541 cm_RandomizeServer(&cp->vlServersp);
543 lock_ReleaseMutex(&cp->mx);
547 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
554 lock_ObtainRead(&cm_cellLock);
556 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
557 cookie, cm_data.currentCells, cm_data.maxCells);
558 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
560 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
561 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
562 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
563 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
566 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
567 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
570 lock_ReleaseRead(&cm_cellLock);
575 /* call with volume write-locked and mutex held */
576 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
580 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
583 i = CM_CELL_NAME_HASH(cellp->name);
585 cellp->nameNextp = cm_data.cellNameHashTablep[i];
586 cm_data.cellNameHashTablep[i] = cellp;
587 cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
590 /* call with cell write-locked and mutex held */
591 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
597 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
598 /* hash it out first */
599 i = CM_CELL_NAME_HASH(cellp->name);
600 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
602 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
603 if (tcellp == cellp) {
604 *lcellpp = cellp->nameNextp;
605 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
606 cellp->nameNextp = NULL;
613 /* call with cell write-locked and mutex held */
614 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
618 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
621 i = CM_CELL_ID_HASH(cellp->cellID);
623 cellp->idNextp = cm_data.cellIDHashTablep[i];
624 cm_data.cellIDHashTablep[i] = cellp;
625 cellp->flags |= CM_CELLFLAG_IN_IDHASH;
628 /* call with cell write-locked and mutex held */
629 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
635 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
636 /* hash it out first */
637 i = CM_CELL_ID_HASH(cellp->cellID);
638 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
640 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
641 if (tcellp == cellp) {
642 *lcellpp = cellp->idNextp;
643 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
644 cellp->idNextp = NULL;