windows-byte-range-locks-20050816
[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           )
74 #ifdef AFS_AFSDB_ENV
75         || (cm_dnsEnabled && (cp->flags & CM_CELLFLAG_DNS) &&
76          ((cp->flags & CM_CELLFLAG_VLSERVER_INVALID) || (time(0) > cp->timeout)))
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 #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                     cp = NULL;      /* return NULL to indicate failure */
104                 }
105             } else 
106 #endif /* AFS_AFSDB_ENV */
107             {
108                 cp = NULL;          /* return NULL to indicate failure */
109             }
110         }
111     }
112
113     return cp;
114 }
115
116 /* load up a cell structure from the cell database, afsdcell.ini */
117 cm_cell_t *cm_GetCell(char *namep, long flags)
118 {
119     return cm_GetCell_Gen(namep, NULL, flags);
120 }
121
122 cm_cell_t *cm_GetCell_Gen(char *namep, char *newnamep, long flags)
123 {
124     cm_cell_t *cp;
125     long code;
126     char fullname[200]="";
127
128     if (!strcmp(namep,SMB_IOCTL_FILENAME_NOSLASH))
129         return NULL;
130
131     lock_ObtainWrite(&cm_cellLock);
132     for (cp = cm_data.allCellsp; cp; cp=cp->nextp) {
133         if (strcmp(namep, cp->name) == 0) {
134             strcpy(fullname, cp->name);
135             break;
136         }
137     }   
138
139     if (cp) {
140         cp = cm_UpdateCell(cp);
141     } else if (flags & CM_FLAG_CREATE) {
142         if ( cm_data.currentCells >= cm_data.maxCells )
143             osi_panic("Exceeded Max Cells", __FILE__, __LINE__);
144
145         /* don't increment currentCells until we know that we 
146          * are going to keep this entry 
147          */
148         cp = &cm_data.cellBaseAddress[cm_data.currentCells];
149         memset(cp, 0, sizeof(cm_cell_t));
150         cp->magic = CM_CELL_MAGIC;
151         
152         code = cm_SearchCellFile(namep, fullname, cm_AddCellProc, cp);
153         if (code) {
154             osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellFile(%s) returns code= %d fullname= %s", 
155                       osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
156
157 #ifdef AFS_AFSDB_ENV
158             if (cm_dnsEnabled) {
159                 int ttl;
160
161                 code = cm_SearchCellByDNS(namep, fullname, &ttl, cm_AddCellProc, cp);
162                 if ( code ) {
163                     osi_Log3(afsd_logp,"in cm_GetCell_gen cm_SearchCellByDNS(%s) returns code= %d fullname= %s", 
164                              osi_LogSaveString(afsd_logp,namep), code, osi_LogSaveString(afsd_logp,fullname));
165                     cp = NULL;
166                     goto done;
167                 } else {   /* got cell from DNS */
168                     cp->flags |= CM_CELLFLAG_DNS;
169                     cp->flags &= ~CM_CELLFLAG_VLSERVER_INVALID;
170                     cp->timeout = time(0) + ttl;
171                 }
172             } else 
173 #endif
174             {   
175                 cp = NULL;
176                 goto done;
177             }
178         }
179
180         /* randomise among those vlservers having the same rank*/ 
181         cm_RandomizeServer(&cp->vlServersp);
182
183         /* otherwise we found the cell, and so we're nearly done */
184         lock_InitializeMutex(&cp->mx, "cm_cell_t mutex");
185
186         /* copy in name */
187         strncpy(cp->name, fullname, CELL_MAXNAMELEN);
188         cp->name[CELL_MAXNAMELEN-1] = '\0';
189
190         /* thread on global list */
191         cp->nextp = cm_data.allCellsp;
192         cm_data.allCellsp = cp;
193            
194         /* the cellID cannot be 0 */
195         cp->cellID = ++cm_data.currentCells;
196     }
197
198   done:
199     /* fullname is not valid if cp == NULL */
200     if (cp && newnamep)
201         strcpy(newnamep, fullname);
202     
203     lock_ReleaseWrite(&cm_cellLock);
204     return cp;
205 }
206
207 cm_cell_t *cm_FindCellByID(long cellID)
208 {
209     cm_cell_t *cp;
210     int ttl;
211     int code;
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     osi_assert(cp);
322
323     lock_ObtainMutex(&cp->mx);
324     code = cm_ChangeRankServer(&cp->vlServersp, tsp);
325
326     if ( !code )                /* if the server list was rearranged */
327         cm_RandomizeServer(&cp->vlServersp);
328
329     lock_ReleaseMutex(&cp->mx);
330 }       
331