7047606a24ae9c8635bb176610eb197dddabbe1e
[openafs.git] / src / WINNT / afsd / cm_cell.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <nb30.h>
15 #include <winsock2.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <malloc.h>
19 #include <osi.h>
20 #include <string.h>
21
22 #include "afsd.h"
23
24 osi_rwlock_t cm_cellLock;
25
26 /* function called as callback proc from cm_SearchCellFile.  Return 0 to
27  * continue processing.  
28  *
29  * At the present time the return value is ignored by the caller.
30  */
31 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *hostnamep)
32 {
33     cm_server_t *tsp;
34     cm_serverRef_t *tsrp;
35     cm_cell_t *cellp;
36     cm_cell_rock_t *cellrockp = (cm_cell_rock_t *)rockp;
37     afs_uint32 probe;
38         
39     cellp = cellrockp->cellp;
40     probe = !(cellrockp->flags & CM_FLAG_NOPROBE);
41
42     /* if this server was previously created by fs setserverprefs */
43     if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
44     {
45         if ( !tsp->cellp )
46             tsp->cellp = cellp;
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));
52         }
53     }       
54     else
55         tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp, probe ? 0 : CM_FLAG_NOPROBE);
56
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);
62     tsrp->refCount--;
63     lock_ReleaseWrite(&cm_serverLock);
64
65     return 0;
66 }
67
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
71  */
72 cm_cell_t *cm_UpdateCell(cm_cell_t * cp, afs_uint32 flags)
73 {
74     long code = 0;
75     cm_cell_rock_t rock;
76
77     if (cp == NULL)
78         return NULL;
79
80     lock_ObtainMutex(&cp->mx);
81     if ((cp->vlServersp == NULL 
82 #ifdef AFS_FREELANCE_CLIENT
83           && !(cp->flags & CM_CELLFLAG_FREELANCE)
84 #endif
85           ) || (time(0) > cp->timeout)
86 #ifdef AFS_AFSDB_ENV
87         || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
88          ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
89 #endif
90             ) 
91     {
92         lock_ReleaseMutex(&cp->mx);
93
94         /* must empty cp->vlServersp */
95         if (cp->vlServersp) {
96             cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
97             cp->vlServersp = NULL;
98         }
99
100         rock.cellp = cp;
101         rock.flags = flags;
102         code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, &rock);
103         if (code == 0) {
104             lock_ObtainMutex(&cp->mx);
105             cp->timeout = time(0) + 7200;
106             lock_ReleaseMutex(&cp->mx);
107         }
108 #ifdef AFS_AFSDB_ENV
109         else {
110             if (cm_dnsEnabled) {
111                 int ttl;
112
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);
120 #ifdef DEBUG
121                     fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
122 #endif
123                 } else {
124                     /* if we fail to find it this time, we'll just do nothing and leave the
125                      * current entry alone 
126                      */
127                     lock_ObtainMutex(&cp->mx);
128                     cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
129                     lock_ReleaseMutex(&cp->mx);
130                 }
131             }
132         }
133 #endif /* AFS_AFSDB_ENV */
134     } else {
135         lock_ReleaseMutex(&cp->mx);
136     }
137     return code ? NULL : cp;
138 }
139
140 /* load up a cell structure from the cell database, AFS_CELLSERVDB */
141 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
142 {
143     return cm_GetCell_Gen(namep, NULL, flags);
144 }
145
146 void cm_FreeCell(cm_cell_t *cellp)
147 {
148     if (cellp->vlServersp)
149         cm_FreeServerList(&cellp->vlServersp, CM_FREESERVERLIST_DELETE);
150     cellp->name[0] = '\0';    
151
152     cellp->freeNextp = cm_data.freeCellsp;
153     cm_data.freeCellsp = cellp;
154 }
155
156 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
157 {
158     cm_cell_t *cp, *cp2;
159     long code;
160     char fullname[CELL_MAXNAMELEN]="";
161     char name[CELL_MAXNAMELEN]="";
162     int  hasWriteLock = 0;
163     int  hasMutex = 0;
164     afs_uint32 hash;
165     cm_cell_rock_t rock;
166     size_t len;
167
168     if (namep == NULL || !namep[0] || !strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
169         return NULL;
170
171     /* 
172      * Strip off any trailing dots at the end of the cell name.
173      * Failure to do so results in an undesireable alias as the
174      * result of DNS AFSDB record lookups where a trailing dot
175      * has special meaning.
176      */
177     strncpy(name, namep, CELL_MAXNAMELEN);
178     for (len = strlen(namep); len > 0 && namep[len-1] == '.'; len--) {
179         name[len-1] = '\0';
180     }
181     if (len == 0)
182         return NULL;
183     namep = name;
184
185     hash = CM_CELL_NAME_HASH(namep);
186
187     lock_ObtainRead(&cm_cellLock);
188     for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
189         if (cm_stricmp_utf8(namep, cp->name) == 0) {
190             strncpy(fullname, cp->name, CELL_MAXNAMELEN);
191             fullname[CELL_MAXNAMELEN-1] = '\0';
192             break;
193         }
194     }
195
196     if (!cp) {
197         for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
198             if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
199                 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
200                 fullname[CELL_MAXNAMELEN-1] = '\0';
201                 break;
202             }
203         }   
204     }
205
206     if (cp) {
207         lock_ReleaseRead(&cm_cellLock);
208         cm_UpdateCell(cp, flags);
209     } else if (flags & CM_FLAG_CREATE) {
210         lock_ConvertRToW(&cm_cellLock);
211         hasWriteLock = 1;
212
213         /* when we dropped the lock the cell could have been added
214          * to the list so check again while holding the write lock 
215          */
216         for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
217             if (cm_stricmp_utf8(namep, cp->name) == 0) {
218                 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
219                 fullname[CELL_MAXNAMELEN-1] = '\0';
220                 break;
221             }
222         }   
223
224         if (cp)
225             goto done;
226
227         for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
228             if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
229                 strncpy(fullname, cp->name, CELL_MAXNAMELEN);
230                 fullname[CELL_MAXNAMELEN-1] = '\0';
231                 break;
232             }
233         }   
234
235         if (cp) {
236             lock_ObtainMutex(&cp->mx);
237             cm_AddCellToNameHashTable(cp);
238             cm_AddCellToIDHashTable(cp);           
239             lock_ReleaseMutex(&cp->mx);
240             goto done;
241         }
242
243         if ( cm_data.freeCellsp != NULL ) {
244             cp = cm_data.freeCellsp;
245             cm_data.freeCellsp = cp->freeNextp;
246
247             /* 
248              * The magic, cellID, and mx fields are already set.
249              */
250         } else {
251             if ( cm_data.currentCells >= cm_data.maxCells )
252                 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
253
254             /* don't increment currentCells until we know that we 
255              * are going to keep this entry 
256              */
257             cp = &cm_data.cellBaseAddress[cm_data.currentCells];
258             memset(cp, 0, sizeof(cm_cell_t));
259             cp->magic = CM_CELL_MAGIC;
260
261             /* the cellID cannot be 0 */
262             cp->cellID = ++cm_data.currentCells;
263
264             /* otherwise we found the cell, and so we're nearly done */
265             lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
266         }
267
268         lock_ReleaseWrite(&cm_cellLock);
269         hasWriteLock = 0;
270
271         rock.cellp = cp;
272         rock.flags = flags;
273         code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, &rock);
274         if (code) {
275             osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s", 
276                       osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
277
278 #ifdef AFS_AFSDB_ENV
279             if (cm_dnsEnabled) {
280                 int ttl;
281
282                 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
283                 if ( code ) {
284                     osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s", 
285                              osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
286                     cm_FreeCell(cp);
287                     cp = NULL;
288                     goto done;
289                 } else {   /* got cell from DNS */
290                     lock_ObtainMutex(&cp->mx);
291                     hasMutex = 1;
292                     cp->flags |= CM_CELLFLAG_DNS;
293                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
294                     cp->timeout = time(0) + ttl;
295                 }
296             } 
297             else 
298 #endif
299             {
300                 cm_FreeCell(cp);
301                 cp = NULL;
302                 goto done;
303             }
304         } else {
305             lock_ObtainMutex(&cp->mx);
306             hasMutex = 1;
307             cp->timeout = time(0) + 7200;       /* two hour timeout */
308         }
309
310         /* we have now been given the fullname of the cell.  It may
311          * be that we already have a cell with that name.  If so,
312          * we should use it instead of completing the allocation
313          * of a new cm_cell_t 
314          */
315         hash = CM_CELL_NAME_HASH(fullname);
316         for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
317             if (cm_stricmp_utf8(fullname, cp2->name) == 0) {
318                 break;
319             }
320         }   
321
322         if (cp2) {
323             if (hasMutex) {
324                 lock_ReleaseMutex(&cp->mx);
325                 hasMutex = 0;
326             }
327             cm_FreeCell(cp);
328             cp = cp2;
329             goto done;
330         }
331
332         /* randomise among those vlservers having the same rank*/ 
333         cm_RandomizeServer(&cp->vlServersp);
334
335         if (!hasMutex)
336             lock_ObtainMutex(&cp->mx);
337         /* copy in name */
338         strncpy(cp->name, fullname, CELL_MAXNAMELEN);
339         cp->name[CELL_MAXNAMELEN-1] = '\0';
340
341         cm_AddCellToNameHashTable(cp);
342         cm_AddCellToIDHashTable(cp);           
343         lock_ReleaseMutex(&cp->mx);
344         hasMutex = 0;
345
346         /* append cell to global list */
347         if (cm_data.allCellsp == NULL) {
348             cm_data.allCellsp = cp;
349         } else {
350             for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
351                 ;
352             cp2->allNextp = cp;
353         }
354         cp->allNextp = NULL;
355
356     } else {
357         lock_ReleaseRead(&cm_cellLock);
358     }
359   done:
360     if (hasMutex && cp)
361         lock_ReleaseMutex(&cp->mx);
362     if (hasWriteLock)
363         lock_ReleaseWrite(&cm_cellLock);
364     
365     /* fullname is not valid if cp == NULL */
366     if (newnamep) {
367         if (cp) {
368             strncpy(newnamep, fullname, CELL_MAXNAMELEN);
369             newnamep[CELL_MAXNAMELEN-1]='\0';
370         } else {
371             newnamep[0] = '\0';
372         }
373     }
374     return cp;
375 }
376
377 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
378 {
379     cm_cell_t *cp;
380     afs_uint32 hash;
381
382     lock_ObtainRead(&cm_cellLock);
383
384     hash = CM_CELL_ID_HASH(cellID);
385
386     for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
387         if (cellID == cp->cellID) 
388             break;
389     }
390     lock_ReleaseRead(&cm_cellLock);     
391
392     if (cp)
393         cm_UpdateCell(cp, flags);
394
395     return cp;
396 }
397
398 long 
399 cm_ValidateCell(void)
400 {
401     cm_cell_t * cellp;
402     afs_uint32 count;
403
404     for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->allNextp, count++) {
405         if ( cellp->magic != CM_CELL_MAGIC ) {
406             afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
407             fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
408             return -1;
409         }
410         if ( count != 0 && cellp == cm_data.allCellsp ||
411              count > cm_data.maxCells ) {
412             afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
413             fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
414             return -2;
415         }
416     }
417
418     for (cellp = cm_data.freeCellsp; cellp; cellp=cellp->freeNextp, count++) {
419         if ( count != 0 && cellp == cm_data.freeCellsp ||
420              count > cm_data.maxCells ) {
421             afsi_log("cm_ValidateCell failure: cm_data.freeCellsp infinite loop");
422             fprintf(stderr, "cm_ValidateCell failure: cm_data.freeCellsp infinite loop\n");
423             return -3;
424         }
425     }
426
427     if ( count != cm_data.currentCells ) {
428         afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
429         fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
430         return -4;
431     }
432     
433     return 0;
434 }
435
436
437 long 
438 cm_ShutdownCell(void)
439 {
440     cm_cell_t * cellp;
441
442     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
443         lock_FinalizeMutex(&cellp->mx);
444
445     return 0;
446 }
447
448
449 void cm_InitCell(int newFile, long maxCells)
450 {
451     static osi_once_t once;
452         
453     if (osi_Once(&once)) {
454         cm_cell_t * cellp;
455
456         lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
457
458         if ( newFile ) {
459             cm_data.allCellsp = NULL;
460             cm_data.currentCells = 0;
461             cm_data.maxCells = maxCells;
462             memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
463             memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
464         
465 #ifdef AFS_FREELANCE_CLIENT
466             /* Generate a dummy entry for the Freelance cell whether or not 
467              * freelance mode is being used in this session 
468              */
469
470             cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
471             memset(cellp, 0, sizeof(cm_cell_t));
472             cellp->magic = CM_CELL_MAGIC;
473
474             lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
475
476             /* copy in name */
477             strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
478             cellp->name[CELL_MAXNAMELEN-1] = '\0';
479
480             /* thread on global list */
481             cellp->allNextp = cm_data.allCellsp;
482             cm_data.allCellsp = cellp;
483                 
484             cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
485             cellp->vlServersp = NULL;
486             cellp->flags = CM_CELLFLAG_FREELANCE;
487
488             lock_ObtainMutex(&cellp->mx);
489             cm_AddCellToNameHashTable(cellp);
490             cm_AddCellToIDHashTable(cellp);           
491             lock_ReleaseMutex(&cellp->mx);
492 #endif  
493         } else {
494             for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
495                 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
496                 cellp->vlServersp = NULL;
497                 cellp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
498             }
499         }
500
501         osi_EndOnce(&once);
502     }
503 }
504
505 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
506 {
507     cm_cell_t *cp;
508     int code;
509
510     cp = tsp->cellp;    /* cell that this vlserver belongs to */
511     if (cp) {
512         lock_ObtainMutex(&cp->mx);
513         code = cm_ChangeRankServer(&cp->vlServersp, tsp);
514
515         if ( !code )            /* if the server list was rearranged */
516             cm_RandomizeServer(&cp->vlServersp);
517
518         lock_ReleaseMutex(&cp->mx);
519     }
520 }       
521
522 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
523 {
524     cm_cell_t *cellp;
525     int zilch;
526     char output[1024];
527
528     if (lock)
529         lock_ObtainRead(&cm_cellLock);
530
531     sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n", 
532             cookie, cm_data.currentCells, cm_data.maxCells);
533     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
534
535     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
536         sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x timeout=%I64u\r\n", 
537                 cookie, cellp, cellp->name, cellp->cellID, cellp->flags, cellp->timeout);
538         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
539     }
540
541     sprintf(output, "%s - Done dumping cells.\r\n", cookie);
542     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
543
544     if (lock)
545         lock_ReleaseRead(&cm_cellLock);
546
547     return(0);
548 }
549
550 /* call with volume write-locked and mutex held */
551 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
552 {
553     int i;
554     
555     if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
556         return;
557
558     i = CM_CELL_NAME_HASH(cellp->name);
559
560     cellp->nameNextp = cm_data.cellNameHashTablep[i];
561     cm_data.cellNameHashTablep[i] = cellp;
562     cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
563 }
564
565 /* call with cell write-locked and mutex held */
566 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
567 {
568     cm_cell_t **lcellpp;
569     cm_cell_t *tcellp;
570     int i;
571         
572     if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
573         /* hash it out first */
574         i = CM_CELL_NAME_HASH(cellp->name);
575         for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
576              tcellp;
577              lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
578             if (tcellp == cellp) {
579                 *lcellpp = cellp->nameNextp;
580                 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
581                 cellp->nameNextp = NULL;
582                 break;
583             }
584         }
585     }
586 }
587
588 /* call with cell write-locked and mutex held */
589 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
590 {
591     int i;
592     
593     if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
594         return;
595
596     i = CM_CELL_ID_HASH(cellp->cellID);
597
598     cellp->idNextp = cm_data.cellIDHashTablep[i];
599     cm_data.cellIDHashTablep[i] = cellp;
600     cellp->flags |= CM_CELLFLAG_IN_IDHASH;
601 }
602
603 /* call with cell write-locked and mutex held */
604 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
605 {
606     cm_cell_t **lcellpp;
607     cm_cell_t *tcellp;
608     int i;
609         
610     if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
611         /* hash it out first */
612         i = CM_CELL_ID_HASH(cellp->cellID);
613         for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
614              tcellp;
615              lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
616             if (tcellp == cellp) {
617                 *lcellpp = cellp->idNextp;
618                 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
619                 cellp->idNextp = NULL;
620                 break;
621             }
622         }
623     }
624 }
625