7d001e3940dc09371cb38885f4a80c02e5c341ce
[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 int
141 cm_Analyze(cm_conn_t *connp, cm_user_t *userp, cm_req_t *reqp,
142         struct cm_fid *fidp,
143         AFSVolSync *volSyncp, cm_callbackRequest_t *cbrp, long errorCode)
144 {
145         cm_server_t *serverp;
146         cm_serverRef_t *serversp, *tsrp;
147         cm_ucell_t *ucellp;
148         int retry = 0;
149         int dead_session;
150         
151         osi_Log2(afsd_logp, "cm_Analyze connp 0x%x, code %d",
152                  (long) connp, errorCode);
153
154         /* no locking required, since connp->serverp never changes after
155          * creation */
156         dead_session = (userp->cellInfop == NULL);
157         if (connp)
158                 serverp = connp->serverp;
159
160         /* Update callback pointer */
161         if (cbrp && errorCode == 0) cbrp->serverp = connp->serverp;
162
163         /* If not allowed to retry, don't */
164         if (reqp->flags & CM_REQ_NORETRY)
165                 goto out;
166
167         /* if timeout - check that is did not exceed the SMB timeout
168            and retry */
169         if (errorCode == CM_ERROR_TIMEDOUT)
170     {
171             long timeUsed, timeLeft;
172             /* timeleft - get if from reqp the same way as cmXonnByMServers does */
173 #ifndef DJGPP
174             timeUsed = (GetCurrentTime() - reqp->startTime) / 1000;
175 #else
176             gettimeofday(&now, NULL);
177             timeUsed = sub_time(now, reqp->startTime) / 1000;
178 #endif
179             
180             /* leave 5 seconds margin for sleep */
181             timeLeft = RDRtimeout - timeUsed;
182             if (timeLeft > 5)
183         {
184             thrd_Sleep(3000);
185             cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
186             retry = 1;
187         } 
188     }
189
190     /* if all servers are offline, mark them non-busy and start over */
191         if (errorCode == CM_ERROR_ALLOFFLINE) {
192             osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_ALLOFFLINE.");
193             thrd_Sleep(5000);
194             /* cm_ForceUpdateVolume marks all servers as non_busy */
195             cm_ForceUpdateVolume(fidp, userp, reqp);
196             retry = 1;
197         }
198
199         /* if all servers are busy, mark them non-busy and start over */
200         if (errorCode == CM_ERROR_ALLBUSY) {
201                 cm_GetServerList(fidp, userp, reqp, &serversp);
202                 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
203                         if (tsrp->status == busy)
204                                 tsrp->status = not_busy;
205                 }
206                 thrd_Sleep(5000);
207                 retry = 1;
208         }
209
210         /* special codes:  VBUSY and VRESTARTING */
211         if (errorCode == VBUSY || errorCode == VRESTARTING) {
212                 cm_GetServerList(fidp, userp, reqp, &serversp);
213                 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
214                         if (tsrp->server == serverp
215                             && tsrp->status == not_busy) {
216                                 tsrp->status = busy;
217                                 break;
218                         }
219                 }
220                 retry = 1;
221         }
222
223         /* special codes:  missing volumes */
224         if (errorCode == VNOVOL || errorCode == VMOVED || errorCode == VOFFLINE
225             || errorCode == VSALVAGE || errorCode == VNOSERVICE) {
226                 /* Log server being offline for this volume */
227                 osi_Log4(afsd_logp, "cm_Analyze found server %d.%d.%d.%d marked offline for a volume",
228                          ((serverp->addr.sin_addr.s_addr & 0xff)),
229                          ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
230                          ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
231                          ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
232                 /* Create Event Log message */ 
233                 {
234                     HANDLE h;
235                     char *ptbuf[1];
236                     char s[100];
237                     h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
238                     sprintf(s, "cm_Analyze: Server %d.%d.%d.%d reported volume %d as missing.",
239                             ((serverp->addr.sin_addr.s_addr & 0xff)),
240                             ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
241                             ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
242                             ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24),
243                             fidp->volume);
244                     ptbuf[0] = s;
245                     ReportEvent(h, EVENTLOG_WARNING_TYPE, 0, 1009, NULL,
246                                 1, 0, ptbuf, NULL);
247                     DeregisterEventSource(h);
248                 }
249
250                 /* Mark server offline for this volume */
251                 cm_GetServerList(fidp, userp, reqp, &serversp);
252
253                 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
254                         if (tsrp->server == serverp)
255                                 tsrp->status = offline;
256                 }
257                 retry = 1;
258         }
259
260         /* RX codes */
261         if (errorCode == RX_CALL_TIMEOUT) {
262                 /* server took longer than hardDeadTime 
263                  * don't mark server as down but don't retry
264                  * this is to prevent the SMB session from timing out
265                  * In addition, we log an event to the event log 
266                  */
267 #ifndef DJGPP
268                 HANDLE h;
269                 char *ptbuf[1];
270                 char s[100];
271                 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
272                 sprintf(s, "cm_Analyze: HardDeadTime exceeded.");
273                 ptbuf[0] = s;
274                 ReportEvent(h, EVENTLOG_WARNING_TYPE, 0, 1009, NULL,
275                         1, 0, ptbuf, NULL);
276                 DeregisterEventSource(h);
277 #endif /* !DJGPP */
278           
279                 retry = 0;
280                 osi_Log0(afsd_logp, "cm_Analyze: hardDeadTime exceeded");
281         }
282         else if (errorCode >= -64 && errorCode < 0) {
283                 /* mark server as down */
284                 lock_ObtainMutex(&serverp->mx);
285                 serverp->flags |= CM_SERVERFLAG_DOWN;
286                 lock_ReleaseMutex(&serverp->mx);
287                 retry = 1;
288         }
289
290         if (errorCode == RXKADEXPIRED && !dead_session) {
291                 lock_ObtainMutex(&userp->mx);
292                 ucellp = cm_GetUCell(userp, serverp->cellp);
293                 if (ucellp->ticketp) {
294                         free(ucellp->ticketp);
295                         ucellp->ticketp = NULL;
296                 }
297                 ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
298                 ucellp->gen++;
299                 lock_ReleaseMutex(&userp->mx);
300                 retry = 1;
301         }
302
303         if (retry && dead_session)
304                 retry = 0;
305  
306 out:
307         /* drop this on the way out */
308         if (connp)
309                 cm_PutConn(connp);
310
311         /* retry until we fail to find a connection */
312         return retry;
313 }
314
315 long cm_ConnByMServers(cm_serverRef_t *serversp, cm_user_t *usersp,
316         cm_req_t *reqp, cm_conn_t **connpp)
317 {
318         long code;
319         cm_serverRef_t *tsrp;
320         cm_server_t *tsp;
321         long firstError = 0;
322         int someBusy = 0, someOffline = 0, allDown = 1;
323         long timeUsed, timeLeft, hardTimeLeft;
324 #ifdef DJGPP
325         struct timeval now;
326 #endif /* DJGPP */        
327
328         *connpp = NULL;
329
330 #ifndef DJGPP
331         timeUsed = (GetCurrentTime() - reqp->startTime) / 1000;
332 #else
333         gettimeofday(&now, NULL);
334         timeUsed = sub_time(now, reqp->startTime) / 1000;
335 #endif
336         
337         /* leave 5 seconds margin of safety */
338         timeLeft = RDRtimeout - timeUsed - 5;
339         hardTimeLeft = timeLeft;
340
341         /* Time enough to do an RPC? */
342         if (timeLeft < 1) {
343                 return CM_ERROR_TIMEDOUT;
344         }
345
346         lock_ObtainWrite(&cm_serverLock);
347
348     for(tsrp = serversp; tsrp; tsrp=tsrp->next) {
349         tsp = tsrp->server;
350         tsp->refCount++;
351         lock_ReleaseWrite(&cm_serverLock);
352         if (!(tsp->flags & CM_SERVERFLAG_DOWN)) {
353             allDown = 0;
354             if (tsrp->status == busy)
355                 someBusy = 1;
356             else if (tsrp->status == offline)
357                 someOffline = 1;
358             else {
359                 code = cm_ConnByServer(tsp, usersp, connpp);
360                 if (code == 0) {
361                     cm_PutServer(tsp);
362                     /* Set RPC timeout */
363                     if (timeLeft > CM_CONN_CONNDEADTIME)
364                         timeLeft = CM_CONN_CONNDEADTIME;
365
366                     if (hardTimeLeft > CM_CONN_HARDDEADTIME) 
367                         hardTimeLeft = CM_CONN_HARDDEADTIME;
368
369                     lock_ObtainMutex(&(*connpp)->mx);
370                     rx_SetConnDeadTime((*connpp)->callp,
371                                         timeLeft);
372                     rx_SetConnHardDeadTime((*connpp)->callp, 
373                                             (u_short) hardTimeLeft);
374                     lock_ReleaseMutex(&(*connpp)->mx);
375
376                     return 0;
377                 }
378                 if (firstError == 0) 
379                     firstError = code;
380             }
381                 } 
382         lock_ObtainWrite(&cm_serverLock);
383         osi_assert(tsp->refCount-- > 0);
384     }   
385
386         lock_ReleaseWrite(&cm_serverLock);
387         if (firstError == 0) {
388                 if (someBusy) 
389                         firstError = CM_ERROR_ALLBUSY;
390                 else if (someOffline) 
391                         firstError = CM_ERROR_ALLOFFLINE;
392                 else if (!allDown && serversp) 
393                         firstError = CM_ERROR_TIMEDOUT;
394                 /* Only return CM_ERROR_NOSUCHVOLUME if there are no
395                    servers for this volume */
396                 else 
397                         firstError = CM_ERROR_NOSUCHVOLUME;
398         }
399         osi_Log1(afsd_logp, "cm_ConnByMServers returning %x", firstError);
400     return firstError;
401 }
402
403 /* called with a held server to GC all bad connections hanging off of the server */
404 void cm_GCConnections(cm_server_t *serverp)
405 {
406         cm_conn_t *tcp;
407         cm_conn_t **lcpp;
408         cm_user_t *userp;
409
410         lock_ObtainWrite(&cm_connLock);
411         lcpp = &serverp->connsp;
412         for(tcp = *lcpp; tcp; tcp = *lcpp) {
413                 userp = tcp->userp;
414                 if (userp && tcp->refCount == 0 && (userp->vcRefs == 0)) {
415                         /* do the deletion of this guy */
416                         cm_ReleaseUser(userp);
417                         *lcpp = tcp->nextp;
418                         rx_DestroyConnection(tcp->callp);
419                         lock_FinalizeMutex(&tcp->mx);
420                         free(tcp);
421                 }
422                 else {
423                         /* just advance to the next */
424                         lcpp = &tcp->nextp;
425                 }
426         }
427         lock_ReleaseWrite(&cm_connLock);
428 }
429
430 static void cm_NewRXConnection(cm_conn_t *tcp, cm_ucell_t *ucellp,
431         cm_server_t *serverp)
432 {
433         unsigned short port;
434         int serviceID;
435         int secIndex;
436         struct rx_securityClass *secObjp;
437         afs_int32 level;
438
439         if (serverp->type == CM_SERVER_VLDB) {
440                 port = htons(7003);
441                 serviceID = 52;
442         }
443         else {
444                 osi_assert(serverp->type == CM_SERVER_FILE);
445                 port = htons(7000);
446                 serviceID = 1;
447         }
448         if (ucellp->flags & CM_UCELLFLAG_RXKAD) {
449                 secIndex = 2;
450                 if (cryptall) {
451                         level = rxkad_crypt;
452                         tcp->cryptlevel = rxkad_crypt;
453                 } else {
454                         level = rxkad_clear;
455                 }
456                 secObjp = rxkad_NewClientSecurityObject(level,
457                         &ucellp->sessionKey, ucellp->kvno,
458                         ucellp->ticketLen, ucellp->ticketp);
459         }
460         else {
461                 /* normal auth */
462                 secIndex = 0;
463                 secObjp = rxnull_NewClientSecurityObject();
464         }
465         osi_assert(secObjp != NULL);
466         tcp->callp = rx_NewConnection(serverp->addr.sin_addr.s_addr,
467                 port,
468                 serviceID,
469                 secObjp,
470                 secIndex);
471         rx_SetConnDeadTime(tcp->callp, CM_CONN_CONNDEADTIME);
472         rx_SetConnHardDeadTime(tcp->callp, CM_CONN_HARDDEADTIME);
473         tcp->ucgen = ucellp->gen;
474 }
475
476 long cm_ConnByServer(cm_server_t *serverp, cm_user_t *userp, cm_conn_t **connpp)
477 {
478         cm_conn_t *tcp;
479         cm_ucell_t *ucellp;
480
481         lock_ObtainMutex(&userp->mx);
482         lock_ObtainWrite(&cm_connLock);
483         for(tcp = serverp->connsp; tcp; tcp=tcp->nextp) {
484                 if (tcp->userp == userp) break;
485         }
486         /* find ucell structure */
487         ucellp = cm_GetUCell(userp, serverp->cellp);
488         if (!tcp) {
489                 tcp = malloc(sizeof(*tcp));
490                 memset(tcp, 0, sizeof(*tcp));
491                 tcp->nextp = serverp->connsp;
492                 serverp->connsp = tcp;
493                 tcp->userp = userp;
494                 cm_HoldUser(userp);
495                 lock_InitializeMutex(&tcp->mx, "cm_conn_t mutex");
496                 tcp->serverp = serverp;
497                 tcp->cryptlevel = rxkad_clear;
498                 cm_NewRXConnection(tcp, ucellp, serverp);
499                 tcp->refCount = 1;
500         }
501         else {
502                 if ((tcp->ucgen < ucellp->gen) || (tcp->cryptlevel != cryptall))
503                 {
504                         rx_DestroyConnection(tcp->callp);
505                         cm_NewRXConnection(tcp, ucellp, serverp);
506                 }
507                 tcp->refCount++;
508         }
509         lock_ReleaseWrite(&cm_connLock);
510         lock_ReleaseMutex(&userp->mx);
511
512         /* return this pointer to our caller */
513         osi_Log1(afsd_logp, "cm_ConnByServer returning conn 0x%x", (long) tcp);
514         *connpp = tcp;
515
516         return 0;
517 }
518
519 long cm_Conn(struct cm_fid *fidp, struct cm_user *userp, cm_req_t *reqp,
520         cm_conn_t **connpp)
521 {
522         long code;
523
524         cm_serverRef_t *serversp;
525
526         code = cm_GetServerList(fidp, userp, reqp, &serversp);
527         if (code) {
528                 *connpp = NULL;
529                 return code;
530         }
531
532         code = cm_ConnByMServers(serversp, userp, reqp, connpp);
533         return code;
534 }