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