windows-cell-aliases-20071228
[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         
37     cellp = rockp;
38
39     /* if this server was previously created by fs setserverprefs */
40     if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
41     {
42         if ( !tsp->cellp )
43             tsp->cellp = cellp;
44         else if (tsp->cellp != cellp) {
45             osi_Log3(afsd_logp, "found a vlserver %s associated with two cells named %s and %s",
46                      osi_LogSaveString(afsd_logp,hostnamep),
47                      osi_LogSaveString(afsd_logp,tsp->cellp->name), 
48                      osi_LogSaveString(afsd_logp,cellp->name));
49         }
50     }       
51     else
52         tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp);
53
54     /* Insert the vlserver into a sorted list, sorted by server rank */
55     tsrp = cm_NewServerRef(tsp, 0);
56     cm_InsertServerList(&cellp->vlServersp, tsrp);
57     /* drop the allocation reference */
58     lock_ObtainWrite(&cm_serverLock);
59     tsrp->refCount--;
60     lock_ReleaseWrite(&cm_serverLock);
61
62     return 0;
63 }
64
65 /* if it's from DNS, see if it has expired 
66  * and check to make sure we have a valid set of volume servers
67  * this function must be called with a Write Lock on cm_cellLock
68  */
69 cm_cell_t *cm_UpdateCell(cm_cell_t * cp)
70 {
71     long code = 0;
72
73     if (cp == NULL)
74         return NULL;
75
76     lock_ObtainMutex(&cp->mx);
77     if ((cp->vlServersp == NULL 
78 #ifdef AFS_FREELANCE_CLIENT
79           && !(cp->flags & CM_CELLFLAG_FREELANCE)
80 #endif
81           ) || (time(0) > cp->timeout)
82 #ifdef AFS_AFSDB_ENV
83         || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
84          ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
85 #endif
86             ) {
87         /* must empty cp->vlServersp */
88         if (cp->vlServersp) {
89             cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
90             cp->vlServersp = NULL;
91         }
92
93         code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, cp);
94 #ifdef AFS_AFSDB_ENV
95         if (code) {
96             if (cm_dnsEnabled) {
97                 int ttl;
98
99                 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, cp);
100                 if (code == 0) {   /* got cell from DNS */
101                     cp->flags |= CM_CELLFLAG_DNS;
102                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
103                     cp->timeout = time(0) + ttl;
104 #ifdef DEBUG
105                     fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
106 #endif
107                 } else {
108                     /* if we fail to find it this time, we'll just do nothing and leave the
109                      * current entry alone 
110                      */
111                     cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
112                 }
113             }
114         } else 
115 #endif /* AFS_AFSDB_ENV */
116         {
117             cp->timeout = time(0) + 7200;
118         }       
119     }
120     lock_ReleaseMutex(&cp->mx);
121     return code ? NULL : cp;
122 }
123
124 /* load up a cell structure from the cell database, afsdcell.ini */
125 cm_cell_t *cm_GetCell(char *namep, afs_uint32 flags)
126 {
127     return cm_GetCell_Gen(namep, NULL, flags);
128 }
129
130 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, afs_uint32 flags)
131 {
132     cm_cell_t *cp, *cp2;
133     long code;
134     char fullname[200]="";
135     int  hasWriteLock = 0;
136     afs_uint32 hash;
137
138     if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
139         return NULL;
140
141     hash = CM_CELL_NAME_HASH(namep);
142
143     lock_ObtainRead(&cm_cellLock);
144     for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
145         if (stricmp(namep, cp->name) == 0) {
146             strcpy(fullname, cp->name);
147             break;
148         }
149     }
150
151     if (!cp) {
152         for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
153             if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
154                 strcpy(fullname, cp->name);
155                 break;
156             }
157         }   
158     }
159
160     lock_ReleaseRead(&cm_cellLock);
161
162     if (cp) {
163         cm_UpdateCell(cp);
164     } else if (flags & CM_FLAG_CREATE) {
165         lock_ObtainWrite(&cm_cellLock);
166         hasWriteLock = 1;
167
168         /* when we dropped the lock the cell could have been added
169          * to the list so check again while holding the write lock 
170          */
171         for (cp = cm_data.cellNameHashTablep[hash]; cp; cp=cp->nameNextp) {
172             if (stricmp(namep, cp->name) == 0) {
173                 strcpy(fullname, cp->name);
174                 break;
175             }
176         }   
177
178         if (cp)
179             goto done;
180
181         for (cp = cm_data.allCellsp; cp; cp=cp->allNextp) {
182             if (strnicmp(namep, cp->name, strlen(namep)) == 0) {
183                 strcpy(fullname, cp->name);
184                 break;
185             }
186         }   
187
188         if (cp)
189             goto done;
190
191         if ( cm_data.currentCells >= cm_data.maxCells )
192             osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
193
194         /* don't increment currentCells until we know that we 
195          * are going to keep this entry 
196          */
197         cp = &cm_data.cellBaseAddress[cm_data.currentCells];
198         memset(cp, 0, sizeof(cm_cell_t));
199         cp->magic = CM_CELL_MAGIC;
200         
201         code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, cp);
202         if (code) {
203             osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s", 
204                       osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
205
206 #ifdef AFS_AFSDB_ENV
207             if (cm_dnsEnabled) {
208                 int ttl;
209
210                 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, cp);
211                 if ( code ) {
212                     osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s", 
213                              osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
214                     cp = NULL;
215                     goto done;
216                 } else {   /* got cell from DNS */
217                     cp->flags |= CM_CELLFLAG_DNS;
218                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
219                     cp->timeout = time(0) + ttl;
220                 }
221             } else {
222 #endif
223                 cp = NULL;
224                 goto done;
225 #ifdef AFS_AFSDB_ENV
226             }
227 #endif
228         } else {
229             cp->timeout = time(0) + 7200;       /* two hour timeout */
230         }
231
232         /* we have now been given the fullname of the cell.  It may
233          * be that we already have a cell with that name.  If so,
234          * we should use it instead of completing the allocation
235          * of a new cm_cell_t 
236          */
237         hash = CM_CELL_NAME_HASH(fullname);
238         for (cp2 = cm_data.cellNameHashTablep[hash]; cp2; cp2=cp2->nameNextp) {
239             if (stricmp(fullname, cp2->name) == 0) {
240                 break;
241             }
242         }   
243
244         if (cp2) {
245             cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
246             cp = cp2;
247             goto done;
248         }
249
250
251         /* randomise among those vlservers having the same rank*/ 
252         cm_RandomizeServer(&cp->vlServersp);
253
254         /* otherwise we found the cell, and so we're nearly done */
255         lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
256
257         /* copy in name */
258         strncpy(cp->name, fullname, CELL_MAXNAMELEN);
259         cp->name[CELL_MAXNAMELEN-1] = '\0';
260
261         /* the cellID cannot be 0 */
262         cp->cellID = ++cm_data.currentCells;
263
264                 /* append cell to global list */
265         if (cm_data.allCellsp == NULL) {
266             cm_data.allCellsp = cp;
267         } else {
268             for (cp2 = cm_data.allCellsp; cp2->allNextp; cp2=cp2->allNextp)
269                 ;
270             cp2->allNextp = cp;
271         }
272         cp->allNextp = NULL;
273
274         cm_AddCellToNameHashTable(cp);
275         cm_AddCellToIDHashTable(cp);           
276     }
277
278   done:
279     if (hasWriteLock)
280         lock_ReleaseWrite(&cm_cellLock);
281     
282     /* fullname is not valid if cp == NULL */
283     if (cp && newnamep)
284         strcpy(newnamep, fullname);
285     
286     return cp;
287 }
288
289 cm_cell_t *cm_FindCellByID(afs_int32 cellID)
290 {
291     cm_cell_t *cp;
292     afs_uint32 hash;
293
294     lock_ObtainRead(&cm_cellLock);
295
296     hash = CM_CELL_ID_HASH(cellID);
297
298     for (cp = cm_data.cellIDHashTablep[hash]; cp; cp=cp->idNextp) {
299         if (cellID == cp->cellID) 
300             break;
301     }
302     lock_ReleaseRead(&cm_cellLock);     
303
304     if (cp)
305         cm_UpdateCell(cp);
306
307     return cp;
308 }
309
310 long 
311 cm_ValidateCell(void)
312 {
313     cm_cell_t * cellp;
314     afs_uint32 count;
315
316     for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->allNextp, count++) {
317         if ( cellp->magic != CM_CELL_MAGIC ) {
318             afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
319             fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
320             return -1;
321         }
322         if ( count != 0 && cellp == cm_data.allCellsp ||
323              count > cm_data.maxCells ) {
324             afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
325             fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
326             return -2;
327         }
328     }
329
330     if ( count != cm_data.currentCells ) {
331         afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
332         fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
333         return -3;
334     }
335     
336     return 0;
337 }
338
339
340 long 
341 cm_ShutdownCell(void)
342 {
343     cm_cell_t * cellp;
344
345     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp)
346         lock_FinalizeMutex(&cellp->mx);
347
348     return 0;
349 }
350
351
352 void cm_InitCell(int newFile, long maxCells)
353 {
354     static osi_once_t once;
355         
356     if (osi_Once(&once)) {
357         cm_cell_t * cellp;
358
359         lock_InitializeRWLock(&cm_cellLock, "cell global lock");
360
361         if ( newFile ) {
362             cm_data.allCellsp = NULL;
363             cm_data.currentCells = 0;
364             cm_data.maxCells = maxCells;
365             memset(cm_data.cellNameHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
366             memset(cm_data.cellIDHashTablep, 0, sizeof(cm_cell_t *) * cm_data.cellHashTableSize);
367         
368 #ifdef AFS_FREELANCE_CLIENT
369             /* Generate a dummy entry for the Freelance cell whether or not 
370              * freelance mode is being used in this session 
371              */
372
373             cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
374             memset(cellp, 0, sizeof(cm_cell_t));
375             cellp->magic = CM_CELL_MAGIC;
376
377             lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
378
379             /* copy in name */
380             strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
381
382             /* thread on global list */
383             cellp->allNextp = cm_data.allCellsp;
384             cm_data.allCellsp = cellp;
385                 
386             cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
387             cellp->vlServersp = NULL;
388             cellp->flags = CM_CELLFLAG_FREELANCE;
389
390             cm_AddCellToNameHashTable(cellp);
391             cm_AddCellToIDHashTable(cellp);           
392 #endif  
393         } else {
394             for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
395                 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
396                 cellp->vlServersp = NULL;
397             }
398         }
399
400         osi_EndOnce(&once);
401     }
402 }
403
404 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
405 {
406     cm_cell_t *cp;
407     int code;
408
409     cp = tsp->cellp;    /* cell that this vlserver belongs to */
410     if (cp) {
411         lock_ObtainMutex(&cp->mx);
412         code = cm_ChangeRankServer(&cp->vlServersp, tsp);
413
414         if ( !code )            /* if the server list was rearranged */
415             cm_RandomizeServer(&cp->vlServersp);
416
417         lock_ReleaseMutex(&cp->mx);
418     }
419 }       
420
421 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
422 {
423     cm_cell_t *cellp;
424     int zilch;
425     char output[1024];
426
427     if (lock)
428         lock_ObtainRead(&cm_cellLock);
429
430     sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n", 
431             cookie, cm_data.currentCells, cm_data.maxCells);
432     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
433
434     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->allNextp) {
435         sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x\r\n", 
436                 cookie, cellp, cellp->name, cellp->cellID, cellp->flags);
437         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
438     }
439
440     sprintf(output, "%s - Done dumping cells.\r\n", cookie);
441     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
442
443     if (lock)
444         lock_ReleaseRead(&cm_cellLock);
445
446     return(0);
447 }
448
449 /* call with volume write-locked and mutex held */
450 void cm_AddCellToNameHashTable(cm_cell_t *cellp)
451 {
452     int i;
453     
454     if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH)
455         return;
456
457     i = CM_CELL_NAME_HASH(cellp->name);
458
459     cellp->nameNextp = cm_data.cellNameHashTablep[i];
460     cm_data.cellNameHashTablep[i] = cellp;
461     cellp->flags |= CM_CELLFLAG_IN_NAMEHASH;
462 }
463
464 /* call with cell write-locked and mutex held */
465 void cm_RemoveCellFromNameHashTable(cm_cell_t *cellp)
466 {
467     cm_cell_t **lcellpp;
468     cm_cell_t *tcellp;
469     int i;
470         
471     if (cellp->flags & CM_CELLFLAG_IN_NAMEHASH) {
472         /* hash it out first */
473         i = CM_CELL_NAME_HASH(cellp->name);
474         for (lcellpp = &cm_data.cellNameHashTablep[i], tcellp = cm_data.cellNameHashTablep[i];
475              tcellp;
476              lcellpp = &tcellp->nameNextp, tcellp = tcellp->nameNextp) {
477             if (tcellp == cellp) {
478                 *lcellpp = cellp->nameNextp;
479                 cellp->flags &= ~CM_CELLFLAG_IN_NAMEHASH;
480                 cellp->nameNextp = NULL;
481                 break;
482             }
483         }
484     }
485 }
486
487 /* call with cell write-locked and mutex held */
488 void cm_AddCellToIDHashTable(cm_cell_t *cellp)
489 {
490     int i;
491     
492     if (cellp->flags & CM_CELLFLAG_IN_IDHASH)
493         return;
494
495     i = CM_CELL_ID_HASH(cellp->cellID);
496
497     cellp->idNextp = cm_data.cellIDHashTablep[i];
498     cm_data.cellIDHashTablep[i] = cellp;
499     cellp->flags |= CM_CELLFLAG_IN_IDHASH;
500 }
501
502 /* call with cell write-locked and mutex held */
503 void cm_RemoveCellFromIDHashTable(cm_cell_t *cellp)
504 {
505     cm_cell_t **lcellpp;
506     cm_cell_t *tcellp;
507     int i;
508         
509     if (cellp->flags & CM_CELLFLAG_IN_IDHASH) {
510         /* hash it out first */
511         i = CM_CELL_ID_HASH(cellp->cellID);
512         for (lcellpp = &cm_data.cellIDHashTablep[i], tcellp = cm_data.cellIDHashTablep[i];
513              tcellp;
514              lcellpp = &tcellp->idNextp, tcellp = tcellp->idNextp) {
515             if (tcellp == cellp) {
516                 *lcellpp = cellp->idNextp;
517                 cellp->flags &= ~CM_CELLFLAG_IN_IDHASH;
518                 cellp->idNextp = NULL;
519                 break;
520             }
521         }
522     }
523 }
524