windows-deadlock-20070619
[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, 0);
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, CM_FREESERVERLIST_DELETE);
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, *cp2;
124     long code;
125     char fullname[200]="";
126
127     if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
128         return NULL;
129
130     lock_ObtainRead(&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     lock_ReleaseRead(&cm_cellLock);     
138
139     if (cp) {
140         cp = cm_UpdateCell(cp);
141     } else if (flags & CM_FLAG_CREATE) {
142         lock_ObtainWrite(&cm_cellLock);
143
144         /* when we dropped the lock the cell could have been added
145          * to the list so check again while holding the write lock 
146          */
147         for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
148             if (stricmp(namep, cp->name) == 0) {
149                 strcpy(fullname, cp->name);
150                 break;
151             }
152         }   
153
154         if (cp) {
155             lock_ReleaseWrite(&cm_cellLock);
156             goto done;
157         }
158
159         if ( cm_data.currentCells >= cm_data.maxCells )
160             osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
161
162         /* don't increment currentCells until we know that we 
163          * are going to keep this entry 
164          */
165         cp = &cm_data.cellBaseAddress[cm_data.currentCells];
166         memset(cp, 0, sizeof(cm_cell_t));
167         cp->magic = CM_CELL_MAGIC;
168         
169         code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, cp);
170         if (code) {
171             osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s", 
172                       osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
173
174 #ifdef AFS_AFSDB_ENV
175             if (cm_dnsEnabled) {
176                 int ttl;
177
178                 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, cp);
179                 if ( code ) {
180                     osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s", 
181                              osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
182                     cp = NULL;
183                     goto done;
184                 } else {   /* got cell from DNS */
185                     cp->flags |= CM_CELLFLAG_DNS;
186                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
187                     cp->timeout = time(0) + ttl;
188                 }
189             } else {
190 #endif
191                 cp = NULL;
192                 goto done;
193 #ifdef AFS_AFSDB_ENV
194             }
195 #endif
196         } else {
197             cp->timeout = time(0) + 7200;       /* two hour timeout */
198         }
199
200         /* we have now been given the fullname of the cell.  It may
201          * be that we already have a cell with that name.  If so,
202          * we should use it instead of completing the allocation
203          * of a new cm_cell_t 
204          */
205         for (cp2 = cm_data.allCellsp; cp2; cp2=cp2->nextp) {
206             if (stricmp(fullname, cp2->name) == 0) {
207                 break;
208             }
209         }   
210
211         if (cp2) {
212             cm_FreeServerList(&cp->vlServersp, CM_FREESERVERLIST_DELETE);
213             cp = cp2;
214             lock_ReleaseWrite(&cm_cellLock);
215             goto done;
216         }
217
218
219         /* randomise among those vlservers having the same rank*/ 
220         cm_RandomizeServer(&cp->vlServersp);
221
222         /* otherwise we found the cell, and so we're nearly done */
223         lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
224
225         /* copy in name */
226         strncpy(cp->name, fullname, CELL_MAXNAMELEN);
227         cp->name[CELL_MAXNAMELEN-1] = '\0';
228
229         /* thread on global list */
230         cp->nextp = cm_data.allCellsp;
231         cm_data.allCellsp = cp;
232            
233         /* the cellID cannot be 0 */
234         cp->cellID = ++cm_data.currentCells;
235         lock_ReleaseWrite(&cm_cellLock);
236     }
237
238   done:
239     /* fullname is not valid if cp == NULL */
240     if (cp && newnamep)
241         strcpy(newnamep, fullname);
242     
243     return cp;
244 }
245
246 cm_cell_t *cm_FindCellByID(afs_int32 cellID)
247 {
248     cm_cell_t *cp;
249
250     lock_ObtainRead(&cm_cellLock);
251     for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
252         if (cellID == cp->cellID) 
253             break;
254     }
255     lock_ReleaseRead(&cm_cellLock);     
256
257     if (cp)
258         cp = cm_UpdateCell(cp);
259
260     return cp;
261 }
262
263 long 
264 cm_ValidateCell(void)
265 {
266     cm_cell_t * cellp;
267     afs_uint32 count;
268
269     for (cellp = cm_data.allCellsp, count = 0; cellp; cellp=cellp->nextp, count++) {
270         if ( cellp->magic != CM_CELL_MAGIC ) {
271             afsi_log("cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC");
272             fprintf(stderr, "cm_ValidateCell failure: cellp->magic != CM_CELL_MAGIC\n");
273             return -1;
274         }
275         if ( count != 0 && cellp == cm_data.allCellsp ||
276              count > cm_data.maxCells ) {
277             afsi_log("cm_ValidateCell failure: cm_data.allCellsp infinite loop");
278             fprintf(stderr, "cm_ValidateCell failure: cm_data.allCellsp infinite loop\n");
279             return -2;
280         }
281     }
282
283     if ( count != cm_data.currentCells ) {
284         afsi_log("cm_ValidateCell failure: count != cm_data.currentCells");
285         fprintf(stderr, "cm_ValidateCell failure: count != cm_data.currentCells\n");
286         return -3;
287     }
288     
289     return 0;
290 }
291
292
293 long 
294 cm_ShutdownCell(void)
295 {
296     cm_cell_t * cellp;
297
298     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp)
299         lock_FinalizeMutex(&cellp->mx);
300
301     return 0;
302 }
303
304
305 void cm_InitCell(int newFile, long maxCells)
306 {
307     static osi_once_t once;
308         
309     if (osi_Once(&once)) {
310         cm_cell_t * cellp;
311
312         lock_InitializeRWLock(&cm_cellLock, "cell global lock");
313
314         if ( newFile ) {
315             cm_data.allCellsp = NULL;
316             cm_data.currentCells = 0;
317             cm_data.maxCells = maxCells;
318         
319 #ifdef AFS_FREELANCE_CLIENT
320             /* Generate a dummy entry for the Freelance cell whether or not 
321              * freelance mode is being used in this session 
322              */
323
324             cellp = &cm_data.cellBaseAddress[cm_data.currentCells++];
325             memset(cellp, 0, sizeof(cm_cell_t));
326             cellp->magic = CM_CELL_MAGIC;
327
328             lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
329
330             /* copy in name */
331             strncpy(cellp->name, "Freelance.Local.Cell", CELL_MAXNAMELEN); /*safe*/
332
333             /* thread on global list */
334             cellp->nextp = cm_data.allCellsp;
335             cm_data.allCellsp = cellp;
336                 
337             cellp->cellID = AFS_FAKE_ROOT_CELL_ID;
338             cellp->vlServersp = NULL;
339             cellp->flags = CM_CELLFLAG_FREELANCE;
340 #endif  
341         } else {
342             for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp) {
343                 lock_InitializeMutex(&cellp->mx, "cm_cell_t mutex");
344                 cellp->vlServersp = NULL;
345             }
346         }
347
348         osi_EndOnce(&once);
349     }
350 }
351
352 void cm_ChangeRankCellVLServer(cm_server_t *tsp)
353 {
354     cm_cell_t *cp;
355     int code;
356
357     cp = tsp->cellp;    /* cell that this vlserver belongs to */
358     if (cp) {
359         lock_ObtainMutex(&cp->mx);
360         code = cm_ChangeRankServer(&cp->vlServersp, tsp);
361
362         if ( !code )            /* if the server list was rearranged */
363             cm_RandomizeServer(&cp->vlServersp);
364
365         lock_ReleaseMutex(&cp->mx);
366     }
367 }       
368
369 int cm_DumpCells(FILE *outputFile, char *cookie, int lock)
370 {
371     cm_cell_t *cellp;
372     int zilch;
373     char output[1024];
374
375     if (lock)
376         lock_ObtainRead(&cm_cellLock);
377
378     sprintf(output, "%s - dumping cells - cm_data.currentCells=%d, cm_data.maxCells=%d\r\n", 
379             cookie, cm_data.currentCells, cm_data.maxCells);
380     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
381
382     for (cellp = cm_data.allCellsp; cellp; cellp=cellp->nextp) {
383         sprintf(output, "%s cellp=0x%p,name=%s ID=%d flags=0x%x\r\n", 
384                 cookie, cellp, cellp->name, cellp->cellID, cellp->flags);
385         WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
386     }
387
388     sprintf(output, "%s - Done dumping cells.\r\n", cookie);
389     WriteFile(outputFile, output, (DWORD)strlen(output), &zilch, NULL);
390
391     if (lock)
392         lock_ReleaseRead(&cm_cellLock);
393
394     return(0);
395 }
396