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>
21 #define STRSAFE_NO_DEPRECATE
26 osi_rwlock_t cm_cellLock;
28 /* function called as callback proc from cm_SearchCellFile. Return 0 to
29 * continue processing.
31 * At the present time the return value is ignored by the caller.
33 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep, unsigned short ipRank)
38 cm_cell_rock_t *cellrockp = (cm_cell_rock_t *)rockp;
41 cellp = cellrockp->cellp;
42 probe = !(cellrockp->flags & CM_FLAG_NOPROBE);
44 /* if this server was previously created by fs setserverprefs */
45 if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
49 else if (tsp->cellp != cellp) {
50 osi_Log3(afsd_logp, "found a vlserver %s associated with two cells named %s and %s",
51 osi_LogSaveString(afsd_logp,hostnamep),
52 osi_LogSaveString(afsd_logp,tsp->cellp->name),
53 osi_LogSaveString(afsd_logp,cellp->name));
57 tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp, NULL, probe ? 0 : CM_FLAG_NOPROBE);
62 /* Insert the vlserver into a sorted list, sorted by server rank */
63 tsrp = cm_NewServerRef(tsp, 0);
64 cm_InsertServerList(&cellp->vlServersp, tsrp);
65 /* drop the allocation reference */
66 lock_ObtainWrite(&cm_serverLock);
68 lock_ReleaseWrite(&cm_serverLock);
73 /* if it's from DNS, see if it has expired
74 * and check to make sure we have a valid set of volume servers
75 * this function must not be called with a lock on cm_cellLock
77 cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
81 afs_uint32 mxheld = 0;
86 lock_ObtainMutex(&cp->mx);
88 if ((cp->vlServersp == NULL
89 #ifdef AFS_FREELANCE_CLIENT
90 && !(cp->flags & CM_CELLFLAG_FREELANCE)
92 ) || (time(0) > cp->timeout)
93 || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
94 ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
97 lock_ReleaseMutex(&cp->mx);
100 /* must empty cp->vlServersp */
102 cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
106 code = cm_SearchCellRegistry(1, cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
107 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
108 code = cm_SearchCellFileEx(cp->name, NULL, cp->linkedName, cm_AddCellProc, &rock);
110 lock_ObtainMutex(&cp->mx);
112 cp->timeout = time(0) + 7200;
118 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, &rock);
119 if (code == 0) { /* got cell from DNS */
120 lock_ObtainMutex(&cp->mx);
122 cp->flags |= CM_CELLFLAG_DNS;
123 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
124 cp->timeout = time(0) + ttl;
126 fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
129 /* if we fail to find it this time, we'll just do nothing and leave the
130 * current entry alone
132 lock_ObtainMutex(&cp->mx);
134 cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
141 cm_RandomizeServer(&cp->vlServersp);
144 lock_ReleaseMutex(&cp->mx);
146 return code ? NULL : cp;
149 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
150 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
152 return cm_GetCell_Gen(namep, NULL, flags);
155 void cm_FreeCell(cm_cell_t *cellp)
157 lock_AssertWrite(&cm_cellLock);
159 if (cellp->vlServersp)
160 cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
161 cellp->name[0] = '\0';
163 cellp->freeNextp = cm_data.freeCellsp;
164 cm_data.freeCellsp = cellp;
167 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
171 char fullname[CELL_MAXNAMELEN]="";
172 char linkedName[CELL_MAXNAMELEN]="";
173 char name[CELL_MAXNAMELEN]="";
174 int hasWriteLock = 0;
180 if (namep == NULL || !namep[0] || !strcmp(namep,CM_IOCTL_FILENAME_NOSLASH))
184 * Strip off any trailing dots at the end of the cell name.
185 * Failure to do so results in an undesireable alias as the
186 * result of DNS AFSDB record lookups where a trailing dot
187 * has special meaning.
189 strncpy(name, namep, CELL_MAXNAMELEN);
190 for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
197 hash = CM_CELL_NAME_HASH(namep);
199 lock_ObtainRead(&cm_cellLock);
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';
209 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
210 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
211 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
212 fullname[CELL_MAXNAMELEN-1] = '\0';
219 lock_ReleaseRead(&cm_cellLock);
220 cm_UpdateCell(cp, flags);
221 } else if (flags & CM_FLAG_CREATE) {
222 lock_ConvertRToW(&cm_cellLock);
225 /* when we dropped the lock the cell could have been added
226 * to the list so check again while holding the write lock
228 for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
229 if (cm_stricmp_utf8(namep, cp->name) == 0) {
230 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
231 fullname[CELL_MAXNAMELEN-1] = '\0';
239 for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
240 if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
241 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
242 fullname[CELL_MAXNAMELEN-1] = '\0';
248 lock_ReleaseWrite(&cm_cellLock);
249 lock_ObtainMutex(&cp->mx);
250 lock_ObtainWrite(&cm_cellLock);
251 cm_AddCellToNameHashTable(cp);
252 cm_AddCellToIDHashTable(cp);
253 lock_ReleaseMutex(&cp->mx);
257 if ( cm_data.freeCellsp != NULL ) {
258 cp = cm_data.freeCellsp;
259 cm_data.freeCellsp = cp->freeNextp;
262 * The magic, cellID, and mx fields are already set.
265 if ( cm_data.currentCells >= cm_data.maxCells )
266 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
268 /* don't increment currentCells until we know that we
269 * are going to keep this entry
271 cp = &cm_data.cellBaseAddress[cm_data.currentCells];
272 memset(cp, 0, sizeof(cm_cell_t));
273 cp->magic = CM_CELL_MAGIC;
275 /* the cellID cannot be 0 */
276 cp->cellID = ++cm_data.currentCells;
278 /* otherwise we found the cell, and so we're nearly done */
279 lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
282 lock_ReleaseWrite(&cm_cellLock);
287 code = cm_SearchCellRegistry(1, namep, fullname, linkedName, cm_AddCellProc, &rock);
288 if (code && code != CM_ERROR_FORCE_DNS_LOOKUP)
289 code = cm_SearchCellFileEx(namep, fullname, linkedName, cm_AddCellProc, &rock);
291 osi_Log4(afsd_logp,"in cm_GetCell_gen cm_SearchCellFileEx(%s) returns code= %d fullname= %s linkedName= %s",
292 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname),
293 osi_LogSaveString(afsd_logp,linkedName));
298 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
300 osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s",
301 osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
302 lock_ObtainMutex(&cp->mx);
303 lock_ObtainWrite(&cm_cellLock);
305 cm_RemoveCellFromIDHashTable(cp);
306 cm_RemoveCellFromNameHashTable(cp);
307 lock_ReleaseMutex(&cp->mx);
311 } else { /* got cell from DNS */
312 lock_ObtainMutex(&cp->mx);
314 cp->flags |= CM_CELLFLAG_DNS;
315 cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
316 cp->timeout = time(0) + ttl;
321 lock_ObtainMutex(&cp->mx);
322 lock_ObtainWrite(&cm_cellLock);
324 cm_RemoveCellFromIDHashTable(cp);
325 cm_RemoveCellFromNameHashTable(cp);
326 lock_ReleaseMutex(&cp->mx);
332 lock_ObtainMutex(&cp->mx);
334 cp->timeout = time(0) + 7200; /* two hour timeout */
337 /* we have now been given the fullname of the cell. It may
338 * be that we already have a cell with that name. If so,
339 * we should use it instead of completing the allocation
342 lock_ObtainRead(&cm_cellLock);
343 hash = CM_CELL_NAME_HASH(fullname);
344 for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
345 if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
352 lock_ObtainMutex(&cp->mx);
355 lock_ConvertRToW(&cm_cellLock);
357 cm_RemoveCellFromIDHashTable(cp);
358 cm_RemoveCellFromNameHashTable(cp);
359 lock_ReleaseMutex(&cp->mx);
365 lock_ReleaseRead(&cm_cellLock);
367 /* randomise among those vlservers having the same rank*/
368 cm_RandomizeServer(&cp->vlServersp);
371 lock_ObtainMutex(&cp->mx);
374 strncpy(cp->name, fullname, CELL_MAXNAMELEN);
375 cp->name[CELL_MAXNAMELEN-1] = '\0';
377 strncpy(cp->linkedName, linkedName, CELL_MAXNAMELEN);
378 cp->linkedName[CELL_MAXNAMELEN-1] = '\0';
380 lock_ObtainWrite(&cm_cellLock);
382 cm_AddCellToNameHashTable(cp);
383 cm_AddCellToIDHashTable(cp);
384 lock_ReleaseMutex(&cp->mx);
387 /* append cell to global list */
388 if (cm_data.allCellsp == NULL) {
389 cm_data.allCellsp = cp;
391 for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
398 lock_ReleaseRead(&cm_cellLock);
402 lock_ReleaseMutex(&cp->mx);
404 lock_ReleaseWrite(&cm_cellLock);
406 /* fullname is not valid if cp == NULL */
409 strncpy(newnamep, fullname, CELL_MAXNAMELEN);
410 newnamep[CELL_MAXNAMELEN-1]='\0';
416 if (cp && cp->linkedName[0]) {
417 cm_cell_t * linkedCellp = NULL;
419 if (!strcmp(cp->name, cp->linkedName)) {
420 cp->linkedName[0] = '\0';
421 } else if (!(flags & CM_FLAG_NOMOUNTCHASE)) {
422 linkedCellp = cm_GetCell(cp->linkedName, CM_FLAG_CREATE|CM_FLAG_NOPROBE|CM_FLAG_NOMOUNTCHASE);
424 lock_ObtainWrite(&cm_cellLock);
426 (linkedCellp->linkedName[0] && strcmp(cp->name, linkedCellp->linkedName))) {
427 cp->linkedName[0] = '\0';
429 strncpy(linkedCellp->linkedName, cp->name, CELL_MAXNAMELEN);
430 linkedCellp->linkedName[CELL_MAXNAMELEN-1]='\0';
432 lock_ReleaseWrite(&cm_cellLock);
438 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
443 lock_ObtainRead(&cm_cellLock);
445 hash = CM_CELL_ID_HASH(cellID);
447 for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
448 if (cellID == cp->cellID)
451 lock_ReleaseRead(&cm_cellLock);
454 cm_UpdateCell(cp, flags);
460 cm_ValidateCell(void)
463 afs_uint32 count1, count2;
465 for (cellp = cm_data.allCellsp, count1 = 0; cellp; cellp=cellp->allNextp, count1++) {
466 if ( cellp->magic != CM_CELL_MAGIC ) {
467 afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
468 fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
471 if ( count1 != 0 && cellp == cm_data.allCellsp ||
472 count1 > cm_data.maxCells ) {
473 afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
474 fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
479 for (cellp = cm_data.freeCellsp, count2 = 0; cellp; cellp=cellp->freeNextp, count2++) {
480 if ( count2 != 0 && cellp == cm_data.freeCellsp ||
481 count2 > cm_data.maxCells ) {
482 afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
483 fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
488 if ( (count1 + count2) != cm_data.currentCells ) {
489 afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
490 fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
499 cm_ShutdownCell(void)
503 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
504 lock_FinalizeMutex(&cellp->mx);
510 void cm_InitCell(int newFile, long maxCells)
512 static osi_once_t once;
514 if (osi_Once(&once)) {
517 lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
520 cm_data.allCellsp = NULL;
521 cm_data.currentCells = 0;
522 cm_data.maxCells = maxCells;
523 memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
524 memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
526 #ifdef AFS_FREELANCE_CLIENT
527 /* Generate a dummy entry for the Freelance cell whether or not
528 * freelance mode is being used in this session
531 cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
532 memset(cellp, 0, sizeof(cm_cell_t));
533 cellp->magic = CM_CELL_MAGIC;
535 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
537 lock_ObtainMutex(&cellp->mx);
538 lock_ObtainWrite(&cm_cellLock);
541 strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
542 cellp->name[CELL_MAXNAMELEN-1] = '\0';
544 /* thread on global list */
545 cellp->allNextp = cm_data.allCellsp;
546 cm_data.allCellsp = cellp;
548 cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
549 cellp->vlServersp = NULL;
550 cellp->flags = CM_CELLFLAG_FREELANCE;
552 cm_AddCellToNameHashTable(cellp);
553 cm_AddCellToIDHashTable(cellp);
554 lock_ReleaseWrite(&cm_cellLock);
555 lock_ReleaseMutex(&cellp->mx);
558 lock_ObtainRead(&cm_cellLock);
559 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
560 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
561 cellp->vlServersp = NULL;
562 cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
564 lock_ReleaseRead(&cm_cellLock);
571 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
576 cp = tsp->cellp; /* cell that this vlserver belongs to */
578 lock_ObtainMutex(&cp->mx);
579 code = cm_ChangeRankServer(&cp->vlServersp, tsp);
581 if ( !code ) /* if the server list was rearranged */
582 cm_RandomizeServer(&cp->vlServersp);
584 lock_ReleaseMutex(&cp->mx);
588 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
595 lock_ObtainRead(&cm_cellLock);
597 sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n",
598 cookie, cm_data.currentCells, cm_data.maxCells);
599 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
601 for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
602 sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n",
603 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
604 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
607 sprintf(output, "%s - Done dumping cells.\r\n", cookie);
608 WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
611 lock_ReleaseRead(&cm_cellLock);
616 /* call with volume write-locked and mutex held */
617 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
621 lock_AssertWrite(&cm_cellLock);
622 lock_AssertMutex(&cellp->mx);
624 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
627 i = CM_CELL_NAME_HASH(cellp->name);
629 cellp->nameNextp = cm_data.cellNameHashTablep[i];
630 cm_data.cellNameHashTablep[i] = cellp;
631 cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
634 /* call with cell write-locked and mutex held */
635 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
641 lock_AssertWrite(&cm_cellLock);
642 lock_AssertMutex(&cellp->mx);
644 if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
645 /* hash it out first */
646 i = CM_CELL_NAME_HASH(cellp->name);
647 for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
649 lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
650 if (tcellp == cellp) {
651 *lcellpp = cellp->nameNextp;
652 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
653 cellp->nameNextp = NULL;
660 /* call with cell write-locked and mutex held */
661 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
665 lock_AssertWrite(&cm_cellLock);
666 lock_AssertMutex(&cellp->mx);
668 if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
671 i = CM_CELL_ID_HASH(cellp->cellID);
673 cellp->idNextp = cm_data.cellIDHashTablep[i];
674 cm_data.cellIDHashTablep[i] = cellp;
675 cellp->flags |= CM_CELLFLAG_IN_IDHASH;
678 /* call with cell write-locked and mutex held */
679 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
685 lock_AssertWrite(&cm_cellLock);
686 lock_AssertMutex(&cellp->mx);
688 if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
689 /* hash it out first */
690 i = CM_CELL_ID_HASH(cellp->cellID);
691 for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
693 lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
694 if (tcellp == cellp) {
695 *lcellpp = cellp->idNextp;
696 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
697 cellp->idNextp = NULL;