windows-talocale-20060829
[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 long cm_AddCellProc(void *rockp, struct sockaddr_in *addrp, char *namep)
30 {
31     cm_server_t *tsp;
32     cm_serverRef_t *tsrp;
33     cm_cell_t *cellp;
34         
35     cellp = rockp;
36
37     /* if this server was previously created by fs setserverprefs */
38     if ( tsp = cm_FindServer(addrp, CM_SERVER_VLDB))
39     {
40         if ( !tsp->cellp )
41             tsp->cellp = cellp;
42     }       
43     else
44         tsp = cm_NewServer(addrp, CM_SERVER_VLDB, cellp);
45
46     /* Insert the vlserver into a sorted list, sorted by server rank */
47     tsrp = cm_NewServerRef(tsp);
48     cm_InsertServerList(&cellp->vlServersp, tsrp);
49     /* drop the allocation reference */
50     lock_ObtainWrite(&cm_serverLock);
51     tsrp->refCount--;
52     lock_ReleaseWrite(&cm_serverLock);
53     return 0;
54 }
55
56 /* if it's from DNS, see if it has expired 
57  * and check to make sure we have a valid set of volume servers
58  * this function must be called with a Write Lock on cm_cellLock
59  */
60 cm_cell_t *cm_UpdateCell(cm_cell_t * cp)
61 {
62     long code = 0;
63
64     if (cp == NULL)
65         return NULL;
66
67     lock_ObtainMutex(&cp->mx);
68     if ((cp->vlServersp == NULL 
69 #ifdef AFS_FREELANCE_CLIENT
70           && !(cp->flags & CM_CELLFLAG_FREELANCE)
71 #endif
72           ) || (time(0) > cp->timeout)
73 #ifdef AFS_AFSDB_ENV
74         || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
75          ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID)))
76 #endif
77             ) {
78         /* must empty cp->vlServersp */
79         if (cp->vlServersp) {
80             cm_FreeServerList(&cp->vlServersp);
81             cp->vlServersp = NULL;
82         }
83
84         code = cm_SearchCellFile(cp->name, NULL, cm_AddCellProc, cp);
85 #ifdef AFS_AFSDB_ENV
86         if (code) {
87             if (cm_dnsEnabled) {
88                 int ttl;
89
90                 code = cm_SearchCellByDNS(cp->name, NULL, &ttl, cm_AddCellProc, cp);
91                 if (code == 0) {   /* got cell from DNS */
92                     cp->flags |= CM_CELLFLAG_DNS;
93                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
94                     cp->timeout = time(0) + ttl;
95 #ifdef DEBUG
96                     fprintf(stderr, "cell %s: ttl=%d\n", cp->name, ttl);
97 #endif
98                 } else {
99                     /* if we fail to find it this time, we'll just do nothing and leave the
100                      * current entry alone 
101                      */
102                     cp->flags |= CM_CELLFLAG_VLSERVER_INVALID;
103                 }
104             }
105         } else 
106 #endif /* AFS_AFSDB_ENV */
107         {
108             cp->timeout = time(0) + 7200;
109         }       
110     }
111     lock_ReleaseMutex(&cp->mx);
112     return code ? NULL : cp;
113 }
114
115 /* load up a cell structure from the cell database, afsdcell.ini */
116 cm_cell_t *cm_GetCell(char *namep, long flags)
117 {
118     return cm_GetCell_Gen(namep, NULL, flags);
119 }
120
121 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, long flags)
122 {
123     cm_cell_t *cp;
124     long code;
125     char fullname[200]="";
126
127     if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
128         return NULL;
129
130     lock_ObtainWrite(&cm_cellLock);
131     for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
132         if (stricmp(namep, cp->name) == 0) {
133             strcpy(fullname, cp->name);
134             break;
135         }
136     }   
137
138     if (cp) {
139         cp = cm_UpdateCell(cp);
140     } else if (flags & CM_FLAG_CREATE) {
141         if ( cm_data.currentCells >= cm_data.maxCells )
142             osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
143
144         /* don't increment currentCells until we know that we 
145          * are going to keep this entry 
146          */
147         cp = &cm_data.cellBaseAddress[cm_data.currentCells];
148         memset(cp, 0, sizeof(cm_cell_t));
149         cp->magic = CM_CELL_MAGIC;
150         
151         code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, cp);
152         if (code) {
153             osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s", 
154                       osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
155
156 #ifdef AFS_AFSDB_ENV
157             if (cm_dnsEnabled) {
158                 int ttl;
159
160                 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, cp);
161                 if ( code ) {
162                     osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s", 
163                              osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
164                     cp = NULL;
165                     goto done;
166                 } else {   /* got cell from DNS */
167                     cp->flags |= CM_CELLFLAG_DNS;
168                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
169                     cp->timeout = time(0) + ttl;
170                 }
171             } else {
172 #endif
173                 cp = NULL;
174                 goto done;
175 #ifdef AFS_AFSDB_ENV
176             }
177 #endif
178         } else {
179             cp->timeout = time(0) + 7200;       /* two hour timeout */
180         }
181
182         /* randomise among those vlservers having the same rank*/ 
183         cm_RandomizeServer(&cp->vlServersp);
184
185         /* otherwise we found the cell, and so we're nearly done */
186         lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
187
188         /* copy in name */
189         strncpy(cp->name, fullname, CELL_MAXNAMELEN);
190         cp->name[CELL_MAXNAMELEN-1] = '\0';
191
192         /* thread on global list */
193         cp->nextp = cm_data.allCellsp;
194         cm_data.allCellsp = cp;
195            
196         /* the cellID cannot be 0 */
197         cp->cellID = ++cm_data.currentCells;
198     }
199
200   done:
201     /* fullname is not valid if cp == NULL */
202     if (cp && newnamep)
203         strcpy(newnamep, fullname);
204     
205     lock_ReleaseWrite(&cm_cellLock);
206     return cp;
207 }
208
209 cm_cell_t *cm_FindCellByID(long cellID)
210 {
211     cm_cell_t *cp;
212
213     lock_ObtainWrite(&cm_cellLock);
214     for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
215         if (cellID == cp->cellID) 
216             break;
217     }
218
219     if (cp)
220         cp = cm_UpdateCell(cp);
221
222     lock_ReleaseWrite(&cm_cellLock);    
223     return cp;
224 }
225
226 long 
227 cm_ValidateCell(void)
228 {
229     cm_cell_t * cellp;
230     afs_uint32 count;
231
232     for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->nextp, count++) {
233         if ( cellp->magic != CM_CELL_MAGIC ) {
234             afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
235             fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
236             return -1;
237         }
238         if ( count != 0 && cellp == cm_data.allCellsp ||
239              count > cm_data.maxCells ) {
240             afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
241             fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
242             return -2;
243         }
244     }
245
246     if ( count != cm_data.currentCells ) {
247         afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
248         fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
249         return -3;
250     }
251     
252     return 0;
253 }
254
255
256 long 
257 cm_ShutdownCell(void)
258 {
259     cm_cell_t * cellp;
260
261     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp)
262         lock_FinalizeMutex(&cellp->mx);
263
264     return 0;
265 }
266
267
268 void cm_InitCell(int newFile, long maxCells)
269 {
270     static osi_once_t once;
271         
272     if (osi_Once(&once)) {
273         cm_cell_t * cellp;
274
275         lock_InitializeRWLock(&cm_cellLock, "cell global lock");
276
277         if ( newFile ) {
278             cm_data.allCellsp = NULL;
279             cm_data.currentCells = 0;
280             cm_data.maxCells = maxCells;
281         
282 #ifdef AFS_FREELANCE_CLIENT
283             /* Generate a dummy entry for the Freelance cell whether or not 
284              * freelance mode is being used in this session 
285              */
286
287             cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
288             memset(cellp, 0, sizeof(cm_cell_t));
289             cellp->magic = CM_CELL_MAGIC;
290
291             lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
292
293             /* copy in name */
294             strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
295
296             /* thread on global list */
297             cellp->nextp = cm_data.allCellsp;
298             cm_data.allCellsp = cellp;
299                 
300             cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
301             cellp->vlServersp = NULL;
302             cellp->flags = CM_CELLFLAG_FREELANCE;
303 #endif  
304         } else {
305             for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp) {
306                 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
307                 cellp->vlServersp = NULL;
308             }
309         }
310
311         osi_EndOnce(&once);
312     }
313 }
314
315 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
316 {
317     cm_cell_t *cp;
318     int code;
319
320     cp = tsp->cellp;    /* cell that this vlserver belongs to */
321     if (cp) {
322         lock_ObtainMutex(&cp->mx);
323         code = cm_ChangeRankServer(&cp->vlServersp, tsp);
324
325         if ( !code )            /* if the server list was rearranged */
326             cm_RandomizeServer(&cp->vlServersp);
327
328         lock_ReleaseMutex(&cp->mx);
329     }
330 }       
331