67a4ef67fbfa02182fe5f059a084b38cf981106b
[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     int  hasWriteLock = 0;
162     int  hasMutex = 0;
163     afs_uint32 hash;
164     cm_cell_rock_t rock;
165
166     if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
167         return NULL;
168
169     hash = CM_CELL_NAME_HASH(namep);
170
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';
176             break;
177         }
178     }
179
180     if (!cp) {
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';
185                 break;
186             }
187         }   
188     }
189
190     if (cp) {
191         lock_ReleaseRead(&cm_cellLock);
192         cm_UpdateCell(cp, flags);
193     } else if (flags & CM_FLAG_CREATE) {
194         lock_ConvertRToW(&cm_cellLock);
195         hasWriteLock = 1;
196
197         /* when we dropped the lock the cell could have been added
198          * to the list so check again while holding the write lock 
199          */
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';
204                 break;
205             }
206         }   
207
208         if (cp)
209             goto done;
210
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';
215                 break;
216             }
217         }   
218
219         if (cp) {
220             lock_ObtainMutex(&cp->mx);
221             cm_AddCellToNameHashTable(cp);
222             cm_AddCellToIDHashTable(cp);           
223             lock_ReleaseMutex(&cp->mx);
224             goto done;
225         }
226
227         if ( cm_data.freeCellsp != NULL ) {
228             cp = cm_data.freeCellsp;
229             cm_data.freeCellsp = cp->freeNextp;
230
231             /* 
232              * The magic, cellID, and mx fields are already set.
233              */
234         } else {
235             if ( cm_data.currentCells >= cm_data.maxCells )
236                 osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
237
238             /* don't increment currentCells until we know that we 
239              * are going to keep this entry 
240              */
241             cp = &cm_data.cellBaseAddress[cm_data.currentCells];
242             memset(cp, 0, sizeof(cm_cell_t));
243             cp->magic = CM_CELL_MAGIC;
244
245             /* the cellID cannot be 0 */
246             cp->cellID = ++cm_data.currentCells;
247
248             /* otherwise we found the cell, and so we're nearly done */
249             lock_InitializeMutex(&cp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
250         }
251
252         lock_ReleaseWrite(&cm_cellLock);
253         hasWriteLock = 0;
254
255         rock.cellp = cp;
256         rock.flags = flags;
257         code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, &rock);
258         if (code) {
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));
261
262 #ifdef AFS_AFSDB_ENV
263             if (cm_dnsEnabled) {
264                 int ttl;
265
266                 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, &rock);
267                 if ( code ) {
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));
270                     cm_FreeCell(cp);
271                     cp = NULL;
272                     goto done;
273                 } else {   /* got cell from DNS */
274                     lock_ObtainMutex(&cp->mx);
275                     hasMutex = 1;
276                     cp->flags |= CM_CELLFLAG_DNS;
277                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
278                     cp->timeout = time(0) + ttl;
279                 }
280             } 
281             else 
282 #endif
283             {
284                 cm_FreeCell(cp);
285                 cp = NULL;
286                 goto done;
287             }
288         } else {
289             lock_ObtainMutex(&cp->mx);
290             hasMutex = 1;
291             cp->timeout = time(0) + 7200;       /* two hour timeout */
292         }
293
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
297          * of a new cm_cell_t 
298          */
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) {
302                 break;
303             }
304         }   
305
306         if (cp2) {
307             if (hasMutex) {
308                 lock_ReleaseMutex(&cp->mx);
309                 hasMutex = 0;
310             }
311             cm_FreeCell(cp);
312             cp = cp2;
313             goto done;
314         }
315
316         /* randomise among those vlservers having the same rank*/ 
317         cm_RandomizeServer(&cp->vlServersp);
318
319         if (!hasMutex)
320             lock_ObtainMutex(&cp->mx);
321         /* copy in name */
322         strncpy(cp->name, fullname, CELL_MAXNAMELEN);
323         cp->name[CELL_MAXNAMELEN-1] = '\0';
324
325         cm_AddCellToNameHashTable(cp);
326         cm_AddCellToIDHashTable(cp);           
327         lock_ReleaseMutex(&cp->mx);
328         hasMutex = 0;
329
330         /* append cell to global list */
331         if (cm_data.allCellsp == NULL) {
332             cm_data.allCellsp = cp;
333         } else {
334             for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
335                 ;
336             cp2->allNextp = cp;
337         }
338         cp->allNextp = NULL;
339
340     } else {
341         lock_ReleaseRead(&cm_cellLock);
342     }
343   done:
344     if (hasMutex && cp)
345         lock_ReleaseMutex(&cp->mx);
346     if (hasWriteLock)
347         lock_ReleaseWrite(&cm_cellLock);
348     
349     /* fullname is not valid if cp == NULL */
350     if (newnamep) {
351         if (cp) {
352             strncpy(newnamep, fullname, CELL_MAXNAMELEN);
353             newnamep[CELL_MAXNAMELEN-1]='\0';
354         } else {
355             newnamep[0] = '\0';
356         }
357     }
358     return cp;
359 }
360
361 cm_cell_t *cm_FindCellByID(afs_int32 cellID, afs_uint32 flags)
362 {
363     cm_cell_t *cp;
364     afs_uint32 hash;
365
366     lock_ObtainRead(&cm_cellLock);
367
368     hash = CM_CELL_ID_HASH(cellID);
369
370     for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
371         if (cellID == cp->cellID) 
372             break;
373     }
374     lock_ReleaseRead(&cm_cellLock);     
375
376     if (cp)
377         cm_UpdateCell(cp, flags);
378
379     return cp;
380 }
381
382 long 
383 cm_ValidateCell(void)
384 {
385     cm_cell_t * cellp;
386     afs_uint32 count;
387
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");
392             return -1;
393         }
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");
398             return -2;
399         }
400     }
401
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");
405         return -3;
406     }
407     
408     return 0;
409 }
410
411
412 long 
413 cm_ShutdownCell(void)
414 {
415     cm_cell_t * cellp;
416
417     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
418         lock_FinalizeMutex(&cellp->mx);
419
420     return 0;
421 }
422
423
424 void cm_InitCell(int newFile, long maxCells)
425 {
426     static osi_once_t once;
427         
428     if (osi_Once(&once)) {
429         cm_cell_t * cellp;
430
431         lock_InitializeRWLock(&cm_cellLock, "cell global lock", LOCK_HIERARCHY_CELL_GLOBAL);
432
433         if ( newFile ) {
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);
439         
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 
443              */
444
445             cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
446             memset(cellp, 0, sizeof(cm_cell_t));
447             cellp->magic = CM_CELL_MAGIC;
448
449             lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex", LOCK_HIERARCHY_CELL);
450
451             /* copy in name */
452             strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
453             cellp->name[CELL_MAXNAMELEN-1] = '\0';
454
455             /* thread on global list */
456             cellp->allNextp = cm_data.allCellsp;
457             cm_data.allCellsp = cellp;
458                 
459             cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
460             cellp->vlServersp = NULL;
461             cellp->flags = CM_CELLFLAG_FREELANCE;
462
463             lock_ObtainMutex(&cellp->mx);
464             cm_AddCellToNameHashTable(cellp);
465             cm_AddCellToIDHashTable(cellp);           
466             lock_ReleaseMutex(&cellp->mx);
467 #endif  
468         } else {
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;
473             }
474         }
475
476         osi_EndOnce(&once);
477     }
478 }
479
480 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
481 {
482     cm_cell_t *cp;
483     int code;
484
485     cp = tsp->cellp;    /* cell that this vlserver belongs to */
486     if (cp) {
487         lock_ObtainMutex(&cp->mx);
488         code = cm_ChangeRankServer(&cp->vlServersp, tsp);
489
490         if ( !code )            /* if the server list was rearranged */
491             cm_RandomizeServer(&cp->vlServersp);
492
493         lock_ReleaseMutex(&cp->mx);
494     }
495 }       
496
497 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
498 {
499     cm_cell_t *cellp;
500     int zilch;
501     char output[1024];
502
503     if (lock)
504         lock_ObtainRead(&cm_cellLock);
505
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);
509
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);
514     }
515
516     sprintf(output, "%s - Done dumping cells.\r\n", cookie);
517     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
518
519     if (lock)
520         lock_ReleaseRead(&cm_cellLock);
521
522     return(0);
523 }
524
525 /* call with volume write-locked and mutex held */
526 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
527 {
528     int i;
529     
530     if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
531         return;
532
533     i = CM_CELL_NAME_HASH(cellp->name);
534
535     cellp->nameNextp = cm_data.cellNameHashTablep[i];
536     cm_data.cellNameHashTablep[i] = cellp;
537     cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
538 }
539
540 /* call with cell write-locked and mutex held */
541 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
542 {
543     cm_cell_t **lcellpp;
544     cm_cell_t *tcellp;
545     int i;
546         
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];
551              tcellp;
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;
557                 break;
558             }
559         }
560     }
561 }
562
563 /* call with cell write-locked and mutex held */
564 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
565 {
566     int i;
567     
568     if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
569         return;
570
571     i = CM_CELL_ID_HASH(cellp->cellID);
572
573     cellp->idNextp = cm_data.cellIDHashTablep[i];
574     cm_data.cellIDHashTablep[i] = cellp;
575     cellp->flags |= CM_CELLFLAG_IN_IDHASH;
576 }
577
578 /* call with cell write-locked and mutex held */
579 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
580 {
581     cm_cell_t **lcellpp;
582     cm_cell_t *tcellp;
583     int i;
584         
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];
589              tcellp;
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;
595                 break;
596             }
597         }
598     }
599 }
600