post-1-3-70-windows-changes-20040816
[openafs.git] / src / WINNT / afsd / cm_conn.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 #endif /* !DJGPP */
16 #include <string.h>
17 #include <malloc.h>
18 #include <osi.h>
19 #include <rx/rx.h>
20 #ifndef DJGPP
21 #include <rx/rxkad.h>
22 #else
23 #include <rx/rxkad.h>
24 #endif
25
26 #include "afsd.h"
27
28 osi_rwlock_t cm_connLock;
29
30 long RDRtimeout = CM_CONN_DEFAULTRDRTIMEOUT;
31 long ConnDeadtimeout = CM_CONN_CONNDEADTIME;
32 long HardDeadtimeout = CM_CONN_HARDDEADTIME;
33
34 #define LANMAN_WKS_PARAM_KEY "SYSTEM\\CurrentControlSet\\Services\\lanmanworkstation\\parameters"
35 #define LANMAN_WKS_SESSION_TIMEOUT "SessTimeout"
36
37 afs_int32 cryptall = 0;
38
39 void cm_PutConn(cm_conn_t *connp)
40 {
41         lock_ObtainWrite(&cm_connLock);
42         osi_assert(connp->refCount-- > 0);
43         lock_ReleaseWrite(&cm_connLock);
44 }
45
46 void cm_InitConn(void)
47 {
48         static osi_once_t once;
49         long code;
50         DWORD sessTimeout;
51         HKEY parmKey;
52         
53     if (osi_Once(&once)) {
54                 lock_InitializeRWLock(&cm_connLock, "connection global lock");
55
56         /* keisa - read timeout value for lanmanworkstation  service.
57          * jaltman - as per 
58          *   http://support.microsoft.com:80/support/kb/articles/Q102/0/67.asp&NoWebContent=1
59          * the SessTimeout is a minimum timeout not a maximum timeout.  Therefore, 
60          * I believe that the default should not be short.  Instead, we should wait until
61          * RX times out before reporting a timeout to the SMB client.
62          */
63                 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, LANMAN_WKS_PARAM_KEY,
64                             0, KEY_QUERY_VALUE, &parmKey);
65                 if (code == ERROR_SUCCESS)
66         {
67                     DWORD dummyLen = sizeof(sessTimeout);
68                     code = RegQueryValueEx(parmKey, LANMAN_WKS_SESSION_TIMEOUT, NULL, NULL, 
69                                    (BYTE *) &sessTimeout, &dummyLen);
70                     if (code == ERROR_SUCCESS)
71             {
72                 afsi_log("lanmanworkstation : SessTimeout %d", sessTimeout);
73                 RDRtimeout = sessTimeout;
74                 if ( ConnDeadtimeout < RDRtimeout + 15 ) {
75                     ConnDeadtimeout = RDRtimeout + 15;
76                     afsi_log("ConnDeadTimeout increased to %d", ConnDeadtimeout);
77                 }
78                 if ( HardDeadtimeout < 2 * ConnDeadtimeout ) {
79                     HardDeadtimeout = 2 * ConnDeadtimeout;
80                     afsi_log("HardDeadTimeout increased to %d", HardDeadtimeout);
81                 }
82             }
83         }
84
85         osi_EndOnce(&once);
86     }
87 }
88
89 void cm_InitReq(cm_req_t *reqp)
90 {
91         memset((char *)reqp, 0, sizeof(cm_req_t));
92 #ifndef DJGPP
93         reqp->startTime = GetCurrentTime();
94 #else
95         gettimeofday(&reqp->startTime, NULL);
96 #endif
97 }
98
99 static long cm_GetServerList(struct cm_fid *fidp, struct cm_user *userp,
100         struct cm_req *reqp, cm_serverRef_t ***serversppp)
101 {
102         long code;
103     cm_volume_t *volp = NULL;
104     cm_cell_t *cellp = NULL;
105
106     if (!fidp) {
107                 *serversppp = NULL;
108                 return 0;
109         }
110
111         cellp = cm_FindCellByID(fidp->cell);
112     if (!cellp) return CM_ERROR_NOSUCHCELL;
113
114     code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, &volp);
115     if (code) return code;
116     
117     *serversppp = cm_GetVolServers(volp, fidp->volume);
118
119     cm_PutVolume(volp);
120         return 0;
121 }
122
123 /*
124  * Analyze the error return from an RPC.  Determine whether or not to retry,
125  * and if we're going to retry, determine whether failover is appropriate,
126  * and whether timed backoff is appropriate.
127  *
128  * If the error code is from cm_Conn() or friends, it will be a CM_ERROR code.
129  * Otherwise it will be an RPC code.  This may be a UNIX code (e.g. EDQUOT), or
130  * it may be an RX code, or it may be a special code (e.g. VNOVOL), or it may
131  * be a security code (e.g. RXKADEXPIRED).
132  *
133  * If the error code is from cm_Conn() or friends, connp will be NULL.
134  *
135  * For VLDB calls, fidp will be NULL.
136  *
137  * volSyncp and/or cbrp may also be NULL.
138  */
139 int
140 cm_Analyze(cm_conn_t *connp, cm_user_t *userp, cm_req_t *reqp,
141         struct cm_fid *fidp, 
142         AFSVolSync *volSyncp, 
143         cm_serverRef_t * serversp,
144         cm_callbackRequest_t *cbrp, long errorCode)
145 {
146     cm_server_t *serverp = 0;
147     cm_serverRef_t **serverspp = 0;
148         cm_serverRef_t *tsrp;
149         cm_ucell_t *ucellp;
150     int retry = 0;
151     int free_svr_list = 0;
152         int dead_session;
153     long timeUsed, timeLeft;
154         
155         osi_Log2(afsd_logp, "cm_Analyze connp 0x%x, code %d",
156                  (long) connp, errorCode);
157
158         /* no locking required, since connp->serverp never changes after
159          * creation */
160         dead_session = (userp->cellInfop == NULL);
161         if (connp)
162                 serverp = connp->serverp;
163
164         /* Update callback pointer */
165     if (cbrp && serverp && errorCode == 0) {
166         if (cbrp->serverp) {
167             if ( cbrp->serverp != serverp ) {
168                 lock_ObtainWrite(&cm_serverLock);
169                 cm_PutServerNoLock(cbrp->serverp);
170                 cm_GetServerNoLock(serverp);
171                 lock_ReleaseWrite(&cm_serverLock);
172             }
173         } else {
174             cm_GetServer(serverp);
175         }
176         lock_ObtainWrite(&cm_callbackLock);
177         cbrp->serverp = serverp;
178         lock_ReleaseWrite(&cm_callbackLock);
179     }
180
181         /* If not allowed to retry, don't */
182         if (reqp->flags & CM_REQ_NORETRY)
183                 goto out;
184
185         /* if timeout - check that it did not exceed the SMB timeout
186      * and retry */
187     
188             /* timeleft - get if from reqp the same way as cmXonnByMServers does */
189 #ifndef DJGPP
190             timeUsed = (GetCurrentTime() - reqp->startTime) / 1000;
191 #else
192             gettimeofday(&now, NULL);
193             timeUsed = sub_time(now, reqp->startTime) / 1000;
194 #endif
195             
196             /* leave 5 seconds margin for sleep */
197             timeLeft = RDRtimeout - timeUsed;
198
199     if (errorCode == CM_ERROR_TIMEDOUT && timeLeft > 5 ) {
200             thrd_Sleep(3000);
201             cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
202             retry = 1;
203         } 
204
205     /* if all servers are offline, mark them non-busy and start over */
206     if (errorCode == CM_ERROR_ALLOFFLINE && timeLeft > 7) {
207             osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_ALLOFFLINE.");
208             thrd_Sleep(5000);
209             /* cm_ForceUpdateVolume marks all servers as non_busy */
210                 /* No it doesn't and it won't do anything if all of the 
211                  * the servers are marked as DOWN.  So clear the DOWN
212                  * flag and reset the busy state as well.
213                  */
214         if (!serversp) {
215             cm_GetServerList(fidp, userp, reqp, &serverspp);
216             serversp = *serverspp;
217             free_svr_list = 1;
218         }
219         if (serversp) {
220             lock_ObtainWrite(&cm_serverLock);
221             for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
222                 tsrp->server->flags &= ~CM_SERVERFLAG_DOWN;
223                 if (tsrp->status == busy)
224                     tsrp->status = not_busy;
225             }
226             lock_ReleaseWrite(&cm_serverLock);
227             if (free_svr_list) {
228                 cm_FreeServerList(&serversp);
229                 *serverspp = serversp;
230             }
231             retry = 1;
232         }
233
234         if (fidp != NULL)   /* Not a VLDB call */
235             cm_ForceUpdateVolume(fidp, userp, reqp);
236         }
237
238         /* if all servers are busy, mark them non-busy and start over */
239     if (errorCode == CM_ERROR_ALLBUSY && timeLeft > 7) {
240         thrd_Sleep(5000);
241         if (!serversp) {
242             cm_GetServerList(fidp, userp, reqp, &serverspp);
243             serversp = *serverspp;
244             free_svr_list = 1;
245         }
246                 lock_ObtainWrite(&cm_serverLock);
247                 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
248                         if (tsrp->status == busy)
249                                 tsrp->status = not_busy;
250                 }
251         lock_ReleaseWrite(&cm_serverLock);
252         if (free_svr_list) {
253             cm_FreeServerList(&serversp);
254             *serverspp = serversp;
255         }
256                 retry = 1;
257         }
258
259         /* special codes:  VBUSY and VRESTARTING */
260         if (errorCode == VBUSY || errorCode == VRESTARTING) {
261         if (!serversp) {
262             cm_GetServerList(fidp, userp, reqp, &serverspp);
263             serversp = *serverspp;
264             free_svr_list = 1;
265         }
266                 lock_ObtainWrite(&cm_serverLock);
267                 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
268                         if (tsrp->server == serverp
269                             && tsrp->status == not_busy) {
270                                 tsrp->status = busy;
271                                 break;
272                         }
273                 }
274         lock_ReleaseWrite(&cm_serverLock);
275         if (free_svr_list) {
276             cm_FreeServerList(&serversp);
277             *serverspp = serversp;
278         }
279                 retry = 1;
280         }
281
282         /* special codes:  missing volumes */
283         if (errorCode == VNOVOL || errorCode == VMOVED || errorCode == VOFFLINE
284             || errorCode == VSALVAGE || errorCode == VNOSERVICE) {
285                 /* Log server being offline for this volume */
286                 osi_Log4(afsd_logp, "cm_Analyze found server %d.%d.%d.%d marked offline for a volume",
287                          ((serverp->addr.sin_addr.s_addr & 0xff)),
288                          ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
289                          ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
290                          ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
291                 /* Create Event Log message */ 
292                 {
293                     HANDLE h;
294                     char *ptbuf[1];
295                     char s[100];
296                     h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
297                     sprintf(s, "cm_Analyze: Server %d.%d.%d.%d reported volume %d as missing.",
298                             ((serverp->addr.sin_addr.s_addr & 0xff)),
299                             ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
300                             ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
301                             ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24),
302                             fidp->volume);
303                     ptbuf[0] = s;
304                     ReportEvent(h, EVENTLOG_WARNING_TYPE, 0, 1009, NULL,
305                                 1, 0, ptbuf, NULL);
306                     DeregisterEventSource(h);
307                 }
308
309                 /* Mark server offline for this volume */
310         if (!serversp) {
311             cm_GetServerList(fidp, userp, reqp, &serverspp);
312             serversp = *serverspp;
313             free_svr_list = 1;
314         }
315                 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
316                         if (tsrp->server == serverp)
317                                 tsrp->status = offline;
318                 }
319         if (free_svr_list) {
320             cm_FreeServerList(&serversp);
321             *serverspp = serversp;
322         }
323         if ( timeLeft > 2 )
324                 retry = 1;
325         }
326
327         /* RX codes */
328         if (errorCode == RX_CALL_TIMEOUT) {
329                 /* server took longer than hardDeadTime 
330                  * don't mark server as down but don't retry
331                  * this is to prevent the SMB session from timing out
332                  * In addition, we log an event to the event log 
333                  */
334 #ifndef DJGPP
335                 HANDLE h;
336                 char *ptbuf[1];
337                 char s[100];
338                 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
339                 sprintf(s, "cm_Analyze: HardDeadTime exceeded.");
340                 ptbuf[0] = s;
341                 ReportEvent(h, EVENTLOG_WARNING_TYPE, 0, 1009, NULL,
342                         1, 0, ptbuf, NULL);
343                 DeregisterEventSource(h);
344 #endif /* !DJGPP */
345           
346                 retry = 0;
347                 osi_Log0(afsd_logp, "cm_Analyze: hardDeadTime exceeded");
348         }
349         else if (errorCode >= -64 && errorCode < 0) {
350                 /* mark server as down */
351                 lock_ObtainMutex(&serverp->mx);
352         serverp->flags |= CM_SERVERFLAG_DOWN;
353                 lock_ReleaseMutex(&serverp->mx);
354             if ( timeLeft > 2 )
355         retry = 1;
356     }
357
358         if (errorCode == RXKADEXPIRED && !dead_session) {
359                 lock_ObtainMutex(&userp->mx);
360                 ucellp = cm_GetUCell(userp, serverp->cellp);
361                 if (ucellp->ticketp) {
362                         free(ucellp->ticketp);
363                         ucellp->ticketp = NULL;
364                 }
365                 ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
366                 ucellp->gen++;
367                 lock_ReleaseMutex(&userp->mx);
368             if ( timeLeft > 2 )
369                 retry = 1;
370         }
371
372         if (retry && dead_session)
373                 retry = 0;
374  
375 out:
376         /* drop this on the way out */
377         if (connp)
378                 cm_PutConn(connp);
379
380         /* retry until we fail to find a connection */
381         return retry;
382 }
383
384 long cm_ConnByMServers(cm_serverRef_t *serversp, cm_user_t *usersp,
385         cm_req_t *reqp, cm_conn_t **connpp)
386 {
387         long code;
388         cm_serverRef_t *tsrp;
389     cm_server_t *tsp;
390     long firstError = 0;
391         int someBusy = 0, someOffline = 0, allBusy = 1, allDown = 1;
392         long timeUsed, timeLeft, hardTimeLeft;
393 #ifdef DJGPP
394     struct timeval now;
395 #endif /* DJGPP */        
396
397     *connpp = NULL;
398
399 #ifndef DJGPP
400         timeUsed = (GetCurrentTime() - reqp->startTime) / 1000;
401 #else
402     gettimeofday(&now, NULL);
403     timeUsed = sub_time(now, reqp->startTime) / 1000;
404 #endif
405         
406         /* leave 5 seconds margin of safety */
407         timeLeft =  ConnDeadtimeout - timeUsed - 5;
408         hardTimeLeft = HardDeadtimeout - timeUsed - 5;
409
410         lock_ObtainWrite(&cm_serverLock);
411     for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
412         tsp = tsrp->server;
413         cm_GetServerNoLock(tsp);
414         lock_ReleaseWrite(&cm_serverLock);
415         if (!(tsp->flags & CM_SERVERFLAG_DOWN)) {
416             allDown = 0;
417             if (tsrp->status == busy)
418                 someBusy = 1;
419             else if (tsrp->status == offline)
420                 someOffline = 1;
421             else {
422                                 allBusy = 0;
423                 code = cm_ConnByServer(tsp, usersp, connpp);
424                 if (code == 0) {
425                     cm_PutServer(tsp);
426                     /* Set RPC timeout */
427                     if (timeLeft > ConnDeadtimeout)
428                         timeLeft = ConnDeadtimeout;
429
430                     if (hardTimeLeft > HardDeadtimeout) 
431                         hardTimeLeft = HardDeadtimeout;
432
433                     lock_ObtainMutex(&(*connpp)->mx);
434                     rx_SetConnDeadTime((*connpp)->callp, timeLeft);
435                     rx_SetConnHardDeadTime((*connpp)->callp, (u_short) hardTimeLeft);
436                     lock_ReleaseMutex(&(*connpp)->mx);
437                     return 0;
438                 }
439                 if (firstError == 0) 
440                     firstError = code;
441             }
442                 } 
443         lock_ObtainWrite(&cm_serverLock);
444         cm_PutServerNoLock(tsp);
445     }   
446
447         lock_ReleaseWrite(&cm_serverLock);
448         if (firstError == 0) {
449         if (serversp == NULL)
450                         firstError = CM_ERROR_NOSUCHVOLUME;
451         else if (allDown) 
452                         firstError = CM_ERROR_ALLOFFLINE;
453                 else if (allBusy) 
454                         firstError = CM_ERROR_ALLBUSY;
455                 else
456                         firstError = CM_ERROR_TIMEDOUT;
457         }
458
459         osi_Log1(afsd_logp, "cm_ConnByMServers returning %x", firstError);
460     return firstError;
461 }
462
463 /* called with a held server to GC all bad connections hanging off of the server */
464 void cm_GCConnections(cm_server_t *serverp)
465 {
466         cm_conn_t *tcp;
467     cm_conn_t **lcpp;
468     cm_user_t *userp;
469
470         lock_ObtainWrite(&cm_connLock);
471         lcpp = &serverp->connsp;
472         for(tcp = *lcpp; tcp; tcp = *lcpp) {
473                 userp = tcp->userp;
474                 if (userp && tcp->refCount == 0 && (userp->vcRefs == 0)) {
475                         /* do the deletion of this guy */
476             cm_PutServer(tcp->serverp);
477             cm_ReleaseUser(userp);
478             *lcpp = tcp->nextp;
479                         rx_DestroyConnection(tcp->callp);
480             lock_FinalizeMutex(&tcp->mx);
481             free(tcp);
482         }
483         else {
484                         /* just advance to the next */
485             lcpp = &tcp->nextp;
486         }
487     }
488         lock_ReleaseWrite(&cm_connLock);
489 }
490
491 static void cm_NewRXConnection(cm_conn_t *tcp, cm_ucell_t *ucellp,
492         cm_server_t *serverp)
493 {
494     unsigned short port;
495     int serviceID;
496     int secIndex;
497     struct rx_securityClass *secObjp;
498         afs_int32 level;
499
500         if (serverp->type == CM_SERVER_VLDB) {
501                 port = htons(7003);
502         serviceID = 52;
503     }
504     else {
505                 osi_assert(serverp->type == CM_SERVER_FILE);
506         port = htons(7000);
507         serviceID = 1;
508     }
509         if (ucellp->flags & CM_UCELLFLAG_RXKAD) {
510                 secIndex = 2;
511                 if (cryptall) {
512                         level = rxkad_crypt;
513                         tcp->cryptlevel = rxkad_crypt;
514                 } else {
515                         level = rxkad_clear;
516                 }
517         secObjp = rxkad_NewClientSecurityObject(level,
518                                                 &ucellp->sessionKey, ucellp->kvno,
519                                                 ucellp->ticketLen, ucellp->ticketp);    
520     }
521     else {
522         /* normal auth */
523         secIndex = 0;
524         secObjp = rxnull_NewClientSecurityObject();
525     }
526         osi_assert(secObjp != NULL);
527     tcp->callp = rx_NewConnection(serverp->addr.sin_addr.s_addr,
528                                   port,
529                                   serviceID,
530                                   secObjp,
531                                   secIndex);
532         rx_SetConnDeadTime(tcp->callp, ConnDeadtimeout);
533         rx_SetConnHardDeadTime(tcp->callp, HardDeadtimeout);
534         tcp->ucgen = ucellp->gen;
535     if (secObjp)
536         rxs_Release(secObjp);   /* Decrement the initial refCount */
537 }
538
539 long cm_ConnByServer(cm_server_t *serverp, cm_user_t *userp, cm_conn_t **connpp)
540 {
541         cm_conn_t *tcp;
542     cm_ucell_t *ucellp;
543
544         lock_ObtainMutex(&userp->mx);
545         lock_ObtainWrite(&cm_connLock);
546         for(tcp = serverp->connsp; tcp; tcp=tcp->nextp) {
547         if (tcp->userp == userp) 
548             break;
549     }
550     
551         /* find ucell structure */
552     ucellp = cm_GetUCell(userp, serverp->cellp);
553         if (!tcp) {
554         cm_GetServer(serverp);
555                 tcp = malloc(sizeof(*tcp));
556         memset(tcp, 0, sizeof(*tcp));
557         tcp->nextp = serverp->connsp;
558         serverp->connsp = tcp;
559         cm_HoldUser(userp);
560         tcp->userp = userp;
561         lock_InitializeMutex(&tcp->mx, "cm_conn_t mutex");
562         tcp->serverp = serverp;
563                 tcp->cryptlevel = rxkad_clear;
564                 cm_NewRXConnection(tcp, ucellp, serverp);
565                 tcp->refCount = 1;
566     }
567         else {
568                 if ((tcp->ucgen < ucellp->gen) || (tcp->cryptlevel != cryptall))
569                 {
570                         rx_DestroyConnection(tcp->callp);
571                         cm_NewRXConnection(tcp, ucellp, serverp);
572                 }
573         tcp->refCount++;
574         }
575         lock_ReleaseWrite(&cm_connLock);
576     lock_ReleaseMutex(&userp->mx);
577
578         /* return this pointer to our caller */
579     osi_Log1(afsd_logp, "cm_ConnByServer returning conn 0x%x", (long) tcp);
580         *connpp = tcp;
581
582     return 0;
583 }
584
585 long cm_Conn(struct cm_fid *fidp, struct cm_user *userp, cm_req_t *reqp,
586         cm_conn_t **connpp)
587 {
588         long code;
589
590         cm_serverRef_t **serverspp;
591
592         code = cm_GetServerList(fidp, userp, reqp, &serverspp);
593         if (code) {
594                 *connpp = NULL;
595                 return code;
596         }
597
598         code = cm_ConnByMServers(*serverspp, userp, reqp, connpp);
599     cm_FreeServerList(serverspp);
600     return code;
601 }