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