2 * Copyright 2000, International Business Machines Corporation and others.
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
10 #include <afs/param.h>
28 osi_rwlock_t cm_connLock;
30 long RDRtimeout = CM_CONN_DEFAULTRDRTIMEOUT;
31 long ConnDeadtimeout = CM_CONN_CONNDEADTIME;
32 long HardDeadtimeout = CM_CONN_HARDDEADTIME;
34 #define LANMAN_WKS_PARAM_KEY "SYSTEM\\CurrentControlSet\\Services\\lanmanworkstation\\parameters"
35 #define LANMAN_WKS_SESSION_TIMEOUT "SessTimeout"
37 afs_int32 cryptall = 0;
39 void cm_PutConn(cm_conn_t *connp)
41 lock_ObtainWrite(&cm_connLock);
42 osi_assert(connp->refCount-- > 0);
43 lock_ReleaseWrite(&cm_connLock);
46 void cm_InitConn(void)
48 static osi_once_t once;
53 if (osi_Once(&once)) {
54 lock_InitializeRWLock(&cm_connLock, "connection global lock");
56 /* keisa - read timeout value for lanmanworkstation service.
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.
63 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, LANMAN_WKS_PARAM_KEY,
64 0, KEY_QUERY_VALUE, &parmKey);
65 if (code == ERROR_SUCCESS)
67 DWORD dummyLen = sizeof(sessTimeout);
68 code = RegQueryValueEx(parmKey, LANMAN_WKS_SESSION_TIMEOUT, NULL, NULL,
69 (BYTE *) &sessTimeout, &dummyLen);
70 if (code == ERROR_SUCCESS)
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);
78 if ( HardDeadtimeout < 2 * ConnDeadtimeout ) {
79 HardDeadtimeout = 2 * ConnDeadtimeout;
80 afsi_log("HardDeadTimeout increased to %d", HardDeadtimeout);
89 void cm_InitReq(cm_req_t *reqp)
91 memset((char *)reqp, 0, sizeof(cm_req_t));
93 reqp->startTime = GetCurrentTime();
95 gettimeofday(&reqp->startTime, NULL);
100 long cm_GetServerList(struct cm_fid *fidp, struct cm_user *userp,
101 struct cm_req *reqp, cm_serverRef_t **serverspp)
104 cm_volume_t *volp = NULL;
105 cm_serverRef_t *serversp = NULL;
106 cm_cell_t *cellp = NULL;
113 cellp = cm_FindCellByID(fidp->cell);
114 if (!cellp) return CM_ERROR_NOSUCHCELL;
116 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, &volp);
117 if (code) return code;
119 if (fidp->volume == volp->rwID)
120 serversp = volp->rwServersp;
121 else if (fidp->volume == volp->roID)
122 serversp = volp->roServersp;
123 else if (fidp->volume == volp->bkID)
124 serversp = volp->bkServersp;
129 *serverspp = serversp;
134 * Analyze the error return from an RPC. Determine whether or not to retry,
135 * and if we're going to retry, determine whether failover is appropriate,
136 * and whether timed backoff is appropriate.
138 * If the error code is from cm_Conn() or friends, it will be a CM_ERROR code.
139 * Otherwise it will be an RPC code. This may be a UNIX code (e.g. EDQUOT), or
140 * it may be an RX code, or it may be a special code (e.g. VNOVOL), or it may
141 * be a security code (e.g. RXKADEXPIRED).
143 * If the error code is from cm_Conn() or friends, connp will be NULL.
145 * For VLDB calls, fidp will be NULL.
147 * volSyncp and/or cbrp may also be NULL.
150 cm_Analyze(cm_conn_t *connp, cm_user_t *userp, cm_req_t *reqp,
152 AFSVolSync *volSyncp, cm_callbackRequest_t *cbrp, long errorCode)
154 cm_server_t *serverp;
155 cm_serverRef_t *serversp, *tsrp;
160 osi_Log2(afsd_logp, "cm_Analyze connp 0x%x, code %d",
161 (long) connp, errorCode);
163 /* no locking required, since connp->serverp never changes after
165 dead_session = (userp->cellInfop == NULL);
167 serverp = connp->serverp;
169 /* Update callback pointer */
170 if (cbrp && errorCode == 0) cbrp->serverp = connp->serverp;
172 /* If not allowed to retry, don't */
173 if (reqp->flags & CM_REQ_NORETRY)
176 /* if timeout - check that it did not exceed the SMB timeout
178 if (errorCode == CM_ERROR_TIMEDOUT)
180 long timeUsed, timeLeft;
181 /* timeleft - get if from reqp the same way as cmXonnByMServers does */
183 timeUsed = (GetCurrentTime() - reqp->startTime) / 1000;
185 gettimeofday(&now, NULL);
186 timeUsed = sub_time(now, reqp->startTime) / 1000;
189 /* leave 5 seconds margin for sleep */
190 timeLeft = RDRtimeout - timeUsed;
194 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, NULL);
199 /* if all servers are offline, mark them non-busy and start over */
200 if (errorCode == CM_ERROR_ALLOFFLINE) {
201 osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_ALLOFFLINE.");
203 /* cm_ForceUpdateVolume marks all servers as non_busy */
204 /* No it doesn't and it won't do anything if all of the
205 * the servers are marked as DOWN. So clear the DOWN
206 * flag and reset the busy state as well.
208 cm_GetServerList(fidp, userp, reqp, &serversp);
210 lock_ObtainWrite(&cm_serverLock);
211 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
212 tsrp->server->flags &= ~CM_SERVERFLAG_DOWN;
213 if (tsrp->status == busy)
214 tsrp->status = not_busy;
216 lock_ReleaseWrite(&cm_serverLock);
221 if (fidp != NULL) /* Not a VLDB call */
222 cm_ForceUpdateVolume(fidp, userp, reqp);
225 /* if all servers are busy, mark them non-busy and start over */
226 if (errorCode == CM_ERROR_ALLBUSY) {
227 cm_GetServerList(fidp, userp, reqp, &serversp);
228 lock_ObtainWrite(&cm_serverLock);
229 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
230 if (tsrp->status == busy)
231 tsrp->status = not_busy;
233 lock_ReleaseWrite(&cm_serverLock);
238 /* special codes: VBUSY and VRESTARTING */
239 if (errorCode == VBUSY || errorCode == VRESTARTING) {
240 cm_GetServerList(fidp, userp, reqp, &serversp);
241 lock_ObtainWrite(&cm_serverLock);
242 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
243 if (tsrp->server == serverp
244 && tsrp->status == not_busy) {
249 lock_ReleaseWrite(&cm_serverLock);
253 /* special codes: missing volumes */
254 if (errorCode == VNOVOL || errorCode == VMOVED || errorCode == VOFFLINE
255 || errorCode == VSALVAGE || errorCode == VNOSERVICE) {
256 /* Log server being offline for this volume */
257 osi_Log4(afsd_logp, "cm_Analyze found server %d.%d.%d.%d marked offline for a volume",
258 ((serverp->addr.sin_addr.s_addr & 0xff)),
259 ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
260 ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
261 ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
262 /* Create Event Log message */
267 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
268 sprintf(s, "cm_Analyze: Server %d.%d.%d.%d reported volume %d as missing.",
269 ((serverp->addr.sin_addr.s_addr & 0xff)),
270 ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
271 ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
272 ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24),
275 ReportEvent(h, EVENTLOG_WARNING_TYPE, 0, 1009, NULL,
277 DeregisterEventSource(h);
280 /* Mark server offline for this volume */
281 cm_GetServerList(fidp, userp, reqp, &serversp);
283 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
284 if (tsrp->server == serverp)
285 tsrp->status = offline;
291 if (errorCode == RX_CALL_TIMEOUT) {
292 /* server took longer than hardDeadTime
293 * don't mark server as down but don't retry
294 * this is to prevent the SMB session from timing out
295 * In addition, we log an event to the event log
301 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
302 sprintf(s, "cm_Analyze: HardDeadTime exceeded.");
304 ReportEvent(h, EVENTLOG_WARNING_TYPE, 0, 1009, NULL,
306 DeregisterEventSource(h);
310 osi_Log0(afsd_logp, "cm_Analyze: hardDeadTime exceeded");
312 else if (errorCode >= -64 && errorCode < 0) {
313 /* mark server as down */
314 lock_ObtainMutex(&serverp->mx);
315 serverp->flags |= CM_SERVERFLAG_DOWN;
316 lock_ReleaseMutex(&serverp->mx);
320 if (errorCode == RXKADEXPIRED && !dead_session) {
321 lock_ObtainMutex(&userp->mx);
322 ucellp = cm_GetUCell(userp, serverp->cellp);
323 if (ucellp->ticketp) {
324 free(ucellp->ticketp);
325 ucellp->ticketp = NULL;
327 ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
329 lock_ReleaseMutex(&userp->mx);
333 if (retry && dead_session)
337 /* drop this on the way out */
341 /* retry until we fail to find a connection */
345 long cm_ConnByMServers(cm_serverRef_t *serversp, cm_user_t *usersp,
346 cm_req_t *reqp, cm_conn_t **connpp)
349 cm_serverRef_t *tsrp;
352 int someBusy = 0, someOffline = 0, allBusy = 1, allDown = 1;
353 long timeUsed, timeLeft, hardTimeLeft;
361 timeUsed = (GetCurrentTime() - reqp->startTime) / 1000;
363 gettimeofday(&now, NULL);
364 timeUsed = sub_time(now, reqp->startTime) / 1000;
367 /* leave 5 seconds margin of safety */
368 timeLeft = ConnDeadtimeout - timeUsed - 5;
369 hardTimeLeft = HardDeadtimeout - timeUsed - 5;
371 lock_ObtainWrite(&cm_serverLock);
373 for(tsrp = serversp; tsrp; tsrp=tsrp->next) {
376 lock_ReleaseWrite(&cm_serverLock);
377 if (!(tsp->flags & CM_SERVERFLAG_DOWN)) {
379 if (tsrp->status == busy)
381 else if (tsrp->status == offline)
385 code = cm_ConnByServer(tsp, usersp, connpp);
388 /* Set RPC timeout */
389 if (timeLeft > ConnDeadtimeout)
390 timeLeft = ConnDeadtimeout;
392 if (hardTimeLeft > HardDeadtimeout)
393 hardTimeLeft = HardDeadtimeout;
395 lock_ObtainMutex(&(*connpp)->mx);
396 rx_SetConnDeadTime((*connpp)->callp,
398 rx_SetConnHardDeadTime((*connpp)->callp,
399 (u_short) hardTimeLeft);
400 lock_ReleaseMutex(&(*connpp)->mx);
408 lock_ObtainWrite(&cm_serverLock);
409 osi_assert(tsp->refCount-- > 0);
412 lock_ReleaseWrite(&cm_serverLock);
413 if (firstError == 0) {
414 if (serversp == NULL)
415 firstError = CM_ERROR_NOSUCHVOLUME;
417 firstError = CM_ERROR_ALLOFFLINE;
419 firstError = CM_ERROR_ALLBUSY;
421 firstError = CM_ERROR_TIMEDOUT;
423 osi_Log1(afsd_logp, "cm_ConnByMServers returning %x", firstError);
427 /* called with a held server to GC all bad connections hanging off of the server */
428 void cm_GCConnections(cm_server_t *serverp)
434 lock_ObtainWrite(&cm_connLock);
435 lcpp = &serverp->connsp;
436 for(tcp = *lcpp; tcp; tcp = *lcpp) {
438 if (userp && tcp->refCount == 0 && (userp->vcRefs == 0)) {
439 /* do the deletion of this guy */
440 cm_ReleaseUser(userp);
442 rx_DestroyConnection(tcp->callp);
443 lock_FinalizeMutex(&tcp->mx);
447 /* just advance to the next */
451 lock_ReleaseWrite(&cm_connLock);
454 static void cm_NewRXConnection(cm_conn_t *tcp, cm_ucell_t *ucellp,
455 cm_server_t *serverp)
460 struct rx_securityClass *secObjp;
463 if (serverp->type == CM_SERVER_VLDB) {
468 osi_assert(serverp->type == CM_SERVER_FILE);
472 if (ucellp->flags & CM_UCELLFLAG_RXKAD) {
476 tcp->cryptlevel = rxkad_crypt;
480 secObjp = rxkad_NewClientSecurityObject(level,
481 &ucellp->sessionKey, ucellp->kvno,
482 ucellp->ticketLen, ucellp->ticketp);
487 secObjp = rxnull_NewClientSecurityObject();
489 osi_assert(secObjp != NULL);
490 tcp->callp = rx_NewConnection(serverp->addr.sin_addr.s_addr,
495 rx_SetConnDeadTime(tcp->callp, ConnDeadtimeout);
496 rx_SetConnHardDeadTime(tcp->callp, HardDeadtimeout);
497 tcp->ucgen = ucellp->gen;
499 rxs_Release(secObjp); /* Decrement the initial refCount */
502 long cm_ConnByServer(cm_server_t *serverp, cm_user_t *userp, cm_conn_t **connpp)
507 lock_ObtainMutex(&userp->mx);
508 lock_ObtainWrite(&cm_connLock);
509 for(tcp = serverp->connsp; tcp; tcp=tcp->nextp) {
510 if (tcp->userp == userp) break;
512 /* find ucell structure */
513 ucellp = cm_GetUCell(userp, serverp->cellp);
515 tcp = malloc(sizeof(*tcp));
516 memset(tcp, 0, sizeof(*tcp));
517 tcp->nextp = serverp->connsp;
518 serverp->connsp = tcp;
521 lock_InitializeMutex(&tcp->mx, "cm_conn_t mutex");
522 tcp->serverp = serverp;
523 tcp->cryptlevel = rxkad_clear;
524 cm_NewRXConnection(tcp, ucellp, serverp);
528 if ((tcp->ucgen < ucellp->gen) || (tcp->cryptlevel != cryptall))
530 rx_DestroyConnection(tcp->callp);
531 cm_NewRXConnection(tcp, ucellp, serverp);
535 lock_ReleaseWrite(&cm_connLock);
536 lock_ReleaseMutex(&userp->mx);
538 /* return this pointer to our caller */
539 osi_Log1(afsd_logp, "cm_ConnByServer returning conn 0x%x", (long) tcp);
545 long cm_Conn(struct cm_fid *fidp, struct cm_user *userp, cm_req_t *reqp,
550 cm_serverRef_t *serversp;
552 code = cm_GetServerList(fidp, userp, reqp, &serversp);
558 code = cm_ConnByMServers(serversp, userp, reqp, connpp);