smb-notify-callback-expire-20040817
[openafs.git] / src / WINNT / afsd / cm_server.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 <winsock2.h>
16 #include <nb30.h>
17 #else
18 #include <sys/socket.h>
19 #endif /* !DJGPP */
20 #include <stdlib.h>
21 #include <malloc.h>
22 #include <string.h>
23
24 #include <osi.h>
25 #include <rx/rx.h>
26 #include "afsd.h"
27
28 osi_rwlock_t cm_serverLock;
29
30 cm_server_t *cm_allServersp;
31
32 int cm_noIPAddr;         /* number of client network interfaces */
33 int cm_IPAddr[CM_MAXINTERFACE_ADDR];    /* client's IP address in host order */
34 int cm_SubnetMask[CM_MAXINTERFACE_ADDR];/* client's subnet mask in host order*/
35 int cm_NetMtu[CM_MAXINTERFACE_ADDR];    /* client's MTU sizes */
36 int cm_NetFlags[CM_MAXINTERFACE_ADDR];  /* network flags */
37
38 void cm_CheckServers(long flags, cm_cell_t *cellp)
39 {
40         /* ping all file servers, up or down, with unauthenticated connection,
41          * to find out whether we have all our callbacks from the server still.
42          * Also, ping down VLDBs.
43          */
44         cm_server_t *tsp;
45         long code;
46         long secs;
47         long usecs;
48         int doPing;
49         int serverType;
50         long now;
51         int wasDown;
52         cm_conn_t *connp;
53
54         lock_ObtainWrite(&cm_serverLock);
55         for(tsp = cm_allServersp; tsp; tsp = tsp->allNextp) {
56         cm_GetServerNoLock(tsp);
57                 lock_ReleaseWrite(&cm_serverLock);
58
59                 /* now process the server */
60                 lock_ObtainMutex(&tsp->mx);
61
62                 /* what time is it? */
63                 now = osi_Time();
64
65                 serverType = tsp->type;
66                 doPing = 0;
67                 wasDown = tsp->flags & CM_SERVERFLAG_DOWN;
68
69                 /* only do the ping if the cell matches the requested cell, or we're
70                  * matching all cells (cellp == NULL), and if we've requested to ping
71                  * this type of {up, down} servers.
72                  */
73                 if ((cellp == NULL || cellp == tsp->cellp) &&
74                         ((wasDown && (flags & CM_FLAG_CHECKDOWNSERVERS)) ||
75                          (!wasDown && (flags & CM_FLAG_CHECKUPSERVERS)))) {
76
77                         doPing = 1;
78                 }       /* we're supposed to check this up/down server */
79                 lock_ReleaseMutex(&tsp->mx);
80                         
81                 /* at this point, we've adjusted the server state, so do the ping and
82                  * adjust things.
83                  */
84                 if (doPing) {
85                         code = cm_ConnByServer(tsp, cm_rootUserp, &connp);
86                         if (code == 0) {
87                                 /* now call the appropriate ping call.  Drop the timeout if
88                                  * the server is known to be down, so that we don't waste a
89                                  * lot of time retiming out down servers.
90                                  */
91                                 if (wasDown)
92                                         rx_SetConnDeadTime(connp->callp, 10);
93                                 if (serverType == CM_SERVER_VLDB) {
94                                         code = VL_ProbeServer(connp->callp);
95                                 }
96                                 else {
97                                         /* file server */
98                                         code = RXAFS_GetTime(connp->callp, &secs, &usecs);
99                                 }
100                                 if (wasDown)
101                                         rx_SetConnDeadTime(connp->callp, ConnDeadtimeout);
102                                 cm_PutConn(connp);
103                         }       /* got an unauthenticated connection to this server */
104
105                         lock_ObtainMutex(&tsp->mx);
106                         if (code == 0) {
107                                 /* mark server as up */
108                                 tsp->flags &= ~CM_SERVERFLAG_DOWN;
109                         }
110                         else {
111                                 /* mark server as down */
112                                 tsp->flags |= CM_SERVERFLAG_DOWN;
113                         }
114                         lock_ReleaseMutex(&tsp->mx);
115                 }
116                         
117                 /* also, run the GC function for connections on all of the
118                  * server's connections.
119                  */
120                 cm_GCConnections(tsp);
121
122                 lock_ObtainWrite(&cm_serverLock);
123         cm_PutServerNoLock(tsp);
124         }
125         lock_ReleaseWrite(&cm_serverLock);
126 }
127
128 void cm_InitServer(void)
129 {
130         static osi_once_t once;
131         
132         if (osi_Once(&once)) {
133                 lock_InitializeRWLock(&cm_serverLock, "cm_serverLock");
134                 osi_EndOnce(&once);
135         }
136 }
137
138 void cm_GetServer(cm_server_t *serverp)
139 {
140     lock_ObtainWrite(&cm_serverLock);
141     serverp->refCount++;
142     lock_ReleaseWrite(&cm_serverLock);
143 }
144
145 void cm_GetServerNoLock(cm_server_t *serverp)
146 {
147     serverp->refCount++;
148 }
149
150 void cm_PutServer(cm_server_t *serverp)
151 {
152         lock_ObtainWrite(&cm_serverLock);
153         osi_assert(serverp->refCount-- > 0);
154         lock_ReleaseWrite(&cm_serverLock);
155 }
156
157 void cm_PutServerNoLock(cm_server_t *serverp)
158 {
159         osi_assert(serverp->refCount-- > 0);
160 }
161
162 void cm_SetServerPrefs(cm_server_t * serverp)
163 {
164         unsigned long   serverAddr;     /* in host byte order */
165         unsigned long   myAddr, myNet, mySubnet;/* in host byte order */
166         unsigned long   netMask;
167         int             i;
168
169         /* implement server prefs for fileservers only */
170         if ( serverp->type == CM_SERVER_FILE )
171         {
172             serverAddr = ntohl(serverp->addr.sin_addr.s_addr);
173             serverp->ipRank  = CM_IPRANK_LOW;   /* default setings */
174
175             for ( i=0; i < cm_noIPAddr; i++)
176             {
177                 /* loop through all the client's IP address and compare
178                 ** each of them against the server's IP address */
179
180                 myAddr = cm_IPAddr[i];
181                 if ( IN_CLASSA(myAddr) )
182                     netMask = IN_CLASSA_NET;
183                 else if ( IN_CLASSB(myAddr) )
184                     netMask = IN_CLASSB_NET;
185                 else if ( IN_CLASSC(myAddr) )
186                     netMask = IN_CLASSC_NET;
187                 else
188                     netMask = 0;
189
190                 myNet    =  myAddr & netMask;
191                 mySubnet =  myAddr & cm_SubnetMask[i];
192
193                 if ( (serverAddr & netMask) == myNet ) 
194                 {
195                     if ( (serverAddr & cm_SubnetMask[i]) == mySubnet)
196                     {
197                         if ( serverAddr == myAddr ) 
198                             serverp->ipRank = min(serverp->ipRank,
199                                               CM_IPRANK_TOP);/* same machine */
200                         else serverp->ipRank = min(serverp->ipRank,
201                                               CM_IPRANK_HI); /* same subnet */
202                     }
203                     else serverp->ipRank = min(serverp->ipRank,CM_IPRANK_MED);
204                                                            /* same net */
205                 }       
206                                                  /* random between 0..15*/
207                 serverp->ipRank += min(serverp->ipRank, rand() % 0x000f);
208             } /* and of for loop */
209         }
210     else 
211         serverp->ipRank = 10000 + (rand() % 0x00ff); /* VL server */
212 }
213
214 cm_server_t *cm_NewServer(struct sockaddr_in *socketp, int type, cm_cell_t *cellp) {
215         cm_server_t *tsp;
216
217         osi_assert(socketp->sin_family == AF_INET);
218
219         tsp = malloc(sizeof(*tsp));
220     memset(tsp, 0, sizeof(*tsp));
221         tsp->type = type;
222     tsp->cellp = cellp;
223     tsp->refCount = 1;
224         lock_InitializeMutex(&tsp->mx, "cm_server_t mutex");
225         tsp->addr = *socketp;
226
227         cm_SetServerPrefs(tsp); 
228
229     lock_ObtainWrite(&cm_serverLock); /* get server lock */
230         tsp->allNextp = cm_allServersp;
231     cm_allServersp = tsp;
232     lock_ReleaseWrite(&cm_serverLock); /* release server lock */
233
234     return tsp;
235 }
236
237 /* find a server based on its properties */
238 cm_server_t *cm_FindServer(struct sockaddr_in *addrp, int type)
239 {
240         cm_server_t *tsp;
241
242         osi_assert(addrp->sin_family == AF_INET);
243         
244         lock_ObtainWrite(&cm_serverLock);
245         for(tsp = cm_allServersp; tsp; tsp=tsp->allNextp) {
246                 if (tsp->type == type &&
247                         tsp->addr.sin_addr.s_addr == addrp->sin_addr.s_addr) break;
248         }
249
250         /* bump ref count if we found the server */
251     if (tsp) 
252         cm_GetServerNoLock(tsp);
253
254         /* drop big table lock */
255         lock_ReleaseWrite(&cm_serverLock);
256         
257         /* return what we found */
258         return tsp;
259 }
260
261 cm_serverRef_t *cm_NewServerRef(cm_server_t *serverp)
262 {
263         cm_serverRef_t *tsrp;
264
265     cm_GetServer(serverp);
266         tsrp = malloc(sizeof(*tsrp));
267         tsrp->server = serverp;
268         tsrp->status = not_busy;
269         tsrp->next = NULL;
270     tsrp->refCount = 1;
271
272         return tsrp;
273 }
274
275 long cm_ChecksumServerList(cm_serverRef_t *serversp)
276 {
277         long sum = 0;
278         int first = 1;
279         cm_serverRef_t *tsrp;
280
281     lock_ObtainWrite(&cm_serverLock);
282         for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
283                 if (first)
284                         first = 0;
285                 else
286                         sum <<= 1;
287                 sum ^= (long) tsrp->server;
288         }
289
290     lock_ReleaseWrite(&cm_serverLock);
291         return sum;
292 }
293
294 /*
295 ** Insert a server into the server list keeping the list sorted in 
296 ** asending order of ipRank. 
297 ** 
298 ** The refCount of the cm_serverRef_t is increased
299 */
300 void cm_InsertServerList(cm_serverRef_t** list, cm_serverRef_t* element)
301 {
302         cm_serverRef_t  *current=*list;
303         unsigned short ipRank = element->server->ipRank;
304
305     lock_ObtainWrite(&cm_serverLock);
306     element->refCount++;                /* increase refCount */
307
308     /* insertion into empty list  or at the beginning of the list */
309         if ( !current || (current->server->ipRank > ipRank) )
310         {
311                 element->next = *list;
312                 *list = element;
313         lock_ReleaseWrite(&cm_serverLock);
314                 return ;        
315         }
316         
317         while ( current->next ) /* find appropriate place to insert */
318         {
319                 if ( current->next->server->ipRank > ipRank )
320                         break;
321                 else current = current->next;
322         }
323         element->next = current->next;
324         current->next = element;
325     lock_ReleaseWrite(&cm_serverLock);
326 }
327 /*
328 ** Re-sort the server list with the modified rank
329 ** returns 0 if element was changed successfully. 
330 ** returns 1 if  list remained unchanged.
331 */
332 long cm_ChangeRankServer(cm_serverRef_t** list, cm_server_t*    server)
333 {
334         cm_serverRef_t  **current=list;
335         cm_serverRef_t  *element=0;
336
337         /* if there is max of one element in the list, nothing to sort */
338         if ( (!*current) || !((*current)->next)  )
339                 return 1;               /* list unchanged: return success */
340
341     lock_ObtainWrite(&cm_serverLock);
342         /* if the server is on the list, delete it from list */
343         while ( *current )
344         {
345                 if ( (*current)->server == server)
346                 {
347                         element = (*current);
348                         *current = (*current)->next; /* delete it */
349                         break;
350                 }
351                 current = & ( (*current)->next);        
352         }
353     lock_ReleaseWrite(&cm_serverLock);
354
355     /* if this volume is not replicated on this server  */
356         if (!element)
357                 return 1;       /* server is not on list */
358
359         /* re-insert deleted element into the list with modified rank*/
360         cm_InsertServerList(list, element);
361
362     /* reduce refCount which was increased by cm_InsertServerList */
363     lock_ObtainWrite(&cm_serverLock);
364     element->refCount--;
365     lock_ReleaseWrite(&cm_serverLock);
366         return 0;
367 }
368 /*
369 ** If there are more than one server on the list and the first n servers on 
370 ** the list have the same rank( n>1), then randomise among the first n servers.
371 */
372 void cm_RandomizeServer(cm_serverRef_t** list)
373 {
374         int             count, picked;
375         cm_serverRef_t* tsrp = *list, *lastTsrp;
376         unsigned short  lowestRank;
377
378         /* an empty list or a list with only one element */
379         if ( !tsrp || ! tsrp->next )
380                 return ; 
381
382     lock_ObtainWrite(&cm_serverLock);
383
384         /* count the number of servers with the lowest rank */
385         lowestRank = tsrp->server->ipRank;
386         for ( count=1, tsrp=tsrp->next; tsrp; tsrp=tsrp->next)
387         {
388                 if ( tsrp->server->ipRank != lowestRank)
389                         break;
390                 else
391                         count++;
392         }       
393
394         /* if there is only one server with the lowest rank, we are done */
395         if ( count <= 1 ) {
396         lock_ReleaseWrite(&cm_serverLock);
397                 return ;
398     }
399
400         picked = rand() % count;
401         if ( !picked ) {
402         lock_ReleaseWrite(&cm_serverLock);
403                 return ;
404     }
405
406         tsrp = *list;
407         while (--picked >= 0)
408         {
409                 lastTsrp = tsrp;
410                 tsrp = tsrp->next;
411         }
412         lastTsrp->next = tsrp->next;  /* delete random element from list*/
413         tsrp->next     = *list; /* insert element at the beginning of list */
414         *list          = tsrp;
415     lock_ReleaseWrite(&cm_serverLock);
416 }
417
418 /* call cm_FreeServer while holding a write lock on cm_serverLock */
419 void cm_FreeServer(cm_server_t* serverp)
420 {
421     cm_PutServerNoLock(serverp);
422     if (serverp->refCount == 0)
423     {
424         /* we need to check to ensure that all of the connections
425          * for this server have a 0 refCount; otherwise, they will
426          * not be garbage collected 
427          */
428         cm_GCConnections(serverp);  /* connsp */
429
430         lock_FinalizeMutex(&serverp->mx);
431         if ( cm_allServersp == serverp )
432             cm_allServersp = serverp->allNextp;
433         else {
434             cm_server_t *tsp;
435
436             for(tsp = cm_allServersp; tsp->allNextp; tsp=tsp->allNextp) {
437                 if ( tsp->allNextp == serverp ) {
438                     tsp->allNextp = serverp->allNextp;
439                     break;
440                 }
441             }
442         }
443     }
444  }
445
446 void cm_FreeServerList(cm_serverRef_t** list)
447 {
448     cm_serverRef_t  **current = list;
449     cm_serverRef_t  **nextp = 0;
450         cm_serverRef_t  * next = 0;
451
452     lock_ObtainWrite(&cm_serverLock);
453
454     while (*current)
455     {
456         nextp = &(*current)->next;
457         if (--((*current)->refCount) == 0) {
458                         next = *nextp;
459             cm_FreeServer((*current)->server);
460             free(*current);
461             *current = next;
462         } else {
463            current = nextp;
464         }
465     }
466   
467     lock_ReleaseWrite(&cm_serverLock);
468 }
469