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