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