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>
20 #include <afs/unified_afs.h>
21 #include <WINNT/afsreg.h>
23 osi_rwlock_t cm_connLock;
25 DWORD RDRtimeout = CM_CONN_DEFAULTRDRTIMEOUT;
26 unsigned short ConnDeadtimeout = CM_CONN_CONNDEADTIME;
27 unsigned short HardDeadtimeout = CM_CONN_HARDDEADTIME;
29 #define LANMAN_WKS_PARAM_KEY "SYSTEM\\CurrentControlSet\\Services\\lanmanworkstation\\parameters"
30 #define LANMAN_WKS_SESSION_TIMEOUT "SessTimeout"
32 afs_uint32 cryptall = 0;
33 afs_uint32 cm_anonvldb = 0;
35 void cm_PutConn(cm_conn_t *connp)
37 lock_ObtainWrite(&cm_connLock);
38 osi_assertx(connp->refCount-- > 0, "cm_conn_t refcount 0");
39 lock_ReleaseWrite(&cm_connLock);
42 void cm_InitConn(void)
44 static osi_once_t once;
50 if (osi_Once(&once)) {
51 lock_InitializeRWLock(&cm_connLock, "connection global lock");
53 /* keisa - read timeout value for lanmanworkstation service.
55 * http://support.microsoft.com:80/support/kb/articles/Q102/0/67.asp&NoWebContent=1
56 * the SessTimeout is a minimum timeout not a maximum timeout. Therefore,
57 * I believe that the default should not be short. Instead, we should wait until
58 * RX times out before reporting a timeout to the SMB client.
60 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, LANMAN_WKS_PARAM_KEY,
61 0, KEY_QUERY_VALUE, &parmKey);
62 if (code == ERROR_SUCCESS)
64 dummyLen = sizeof(DWORD);
65 code = RegQueryValueEx(parmKey, LANMAN_WKS_SESSION_TIMEOUT, NULL, NULL,
66 (BYTE *) &dwValue, &dummyLen);
67 if (code == ERROR_SUCCESS)
72 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
73 0, KEY_QUERY_VALUE, &parmKey);
74 if (code == ERROR_SUCCESS) {
75 dummyLen = sizeof(DWORD);
76 code = RegQueryValueEx(parmKey, "ConnDeadTimeout", NULL, NULL,
77 (BYTE *) &dwValue, &dummyLen);
78 if (code == ERROR_SUCCESS)
79 ConnDeadtimeout = (unsigned short)dwValue;
81 dummyLen = sizeof(DWORD);
82 code = RegQueryValueEx(parmKey, "HardDeadTimeout", NULL, NULL,
83 (BYTE *) &dwValue, &dummyLen);
84 if (code == ERROR_SUCCESS)
85 HardDeadtimeout = (unsigned short)dwValue;
86 afsi_log("HardDeadTimeout is %d", HardDeadtimeout);
90 afsi_log("lanmanworkstation : SessTimeout %u", RDRtimeout);
91 if (ConnDeadtimeout == 0)
92 ConnDeadtimeout = (unsigned short) (RDRtimeout / 2);
93 afsi_log("ConnDeadTimeout is %d", ConnDeadtimeout);
94 if (HardDeadtimeout == 0)
95 HardDeadtimeout = (unsigned short) RDRtimeout;
96 afsi_log("HardDeadTimeout is %d", HardDeadtimeout);
102 void cm_InitReq(cm_req_t *reqp)
104 memset((char *)reqp, 0, sizeof(cm_req_t));
105 reqp->startTime = GetTickCount();
108 static long cm_GetServerList(struct cm_fid *fidp, struct cm_user *userp,
109 struct cm_req *reqp, cm_serverRef_t ***serversppp)
112 cm_volume_t *volp = NULL;
113 cm_cell_t *cellp = NULL;
117 return CM_ERROR_INVAL;
120 cellp = cm_FindCellByID(fidp->cell, 0);
122 return CM_ERROR_NOSUCHCELL;
124 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
128 *serversppp = cm_GetVolServers(volp, fidp->volume);
135 * Analyze the error return from an RPC. Determine whether or not to retry,
136 * and if we're going to retry, determine whether failover is appropriate,
137 * and whether timed backoff is appropriate.
139 * If the error code is from cm_ConnFromFID() or friends, it will be a CM_ERROR code.
140 * Otherwise it will be an RPC code. This may be a UNIX code (e.g. EDQUOT), or
141 * it may be an RX code, or it may be a special code (e.g. VNOVOL), or it may
142 * be a security code (e.g. RXKADEXPIRED).
144 * If the error code is from cm_ConnFromFID() or friends, connp will be NULL.
146 * For VLDB calls, fidp will be NULL.
148 * volSyncp and/or cbrp may also be NULL.
151 cm_Analyze(cm_conn_t *connp, cm_user_t *userp, cm_req_t *reqp,
153 AFSVolSync *volSyncp,
154 cm_serverRef_t * serversp,
155 cm_callbackRequest_t *cbrp, long errorCode)
157 cm_server_t *serverp = NULL;
158 cm_serverRef_t **serverspp = NULL;
159 cm_serverRef_t *tsrp;
160 cm_cell_t *cellp = NULL;
162 cm_volume_t * volp = NULL;
163 cm_vol_state_t *statep = NULL;
165 int free_svr_list = 0;
167 long timeUsed, timeLeft;
172 osi_Log2(afsd_logp, "cm_Analyze connp 0x%p, code 0x%x",
175 /* no locking required, since connp->serverp never changes after
177 dead_session = (userp->cellInfop == NULL);
179 serverp = connp->serverp;
181 /* Update callback pointer */
182 if (cbrp && serverp && errorCode == 0) {
184 if ( cbrp->serverp != serverp ) {
185 lock_ObtainWrite(&cm_serverLock);
186 cm_PutServerNoLock(cbrp->serverp);
187 cm_GetServerNoLock(serverp);
188 lock_ReleaseWrite(&cm_serverLock);
191 cm_GetServer(serverp);
193 lock_ObtainWrite(&cm_callbackLock);
194 cbrp->serverp = serverp;
195 lock_ReleaseWrite(&cm_callbackLock);
198 /* if timeout - check that it did not exceed the HardDead timeout
201 /* timeleft - get if from reqp the same way as cmXonnByMServers does */
202 timeUsed = (GetTickCount() - reqp->startTime) / 1000;
204 /* leave 5 seconds margin for sleep */
205 if (reqp->flags & CM_REQ_NORETRY)
208 timeLeft = HardDeadtimeout - timeUsed;
210 /* get a pointer to the cell */
212 if (cellp == NULL && serverp)
213 cellp = serverp->cellp;
214 if (cellp == NULL && serversp) {
215 struct cm_serverRef * refp;
216 for ( refp=serversp ; cellp == NULL && refp != NULL; refp=refp->next) {
218 cellp = refp->server->cellp;
221 if (cellp == NULL && fidp) {
222 cellp = cm_FindCellByID(fidp->cell, 0);
226 if (errorCode == CM_ERROR_TIMEDOUT) {
227 if ( timeLeft > 5 ) {
229 cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, cellp);
234 else if (errorCode == UAEWOULDBLOCK || errorCode == EWOULDBLOCK ||
235 errorCode == UAEAGAIN || errorCode == EAGAIN) {
236 osi_Log0(afsd_logp, "cm_Analyze passed EWOULDBLOCK or EAGAIN.");
237 if ( timeLeft > 5 ) {
243 /* if there is nosuchvolume, then we have a situation in which a
244 * previously known volume no longer has a set of servers
245 * associated with it. Either the volume has moved or
246 * the volume has been deleted. Try to find a new server list
247 * until the timeout period expires.
249 else if (errorCode == CM_ERROR_NOSUCHVOLUME) {
250 osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_NOSUCHVOLUME.");
256 if (fidp != NULL) /* Not a VLDB call */
257 cm_ForceUpdateVolume(fidp, userp, reqp);
261 else if (errorCode == CM_ERROR_ALLDOWN) {
262 osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_ALLDOWN.");
263 /* Servers marked DOWN will be restored by the background daemon
264 * thread as they become available.
268 else if (errorCode == CM_ERROR_ALLOFFLINE) {
269 osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_ALLOFFLINE.");
270 /* Volume instances marked offline will be restored by the
271 * background daemon thread as they become available
273 if (timeLeft > 7 && fidp) {
276 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp,
277 CM_GETVOL_FLAG_NO_LRU_UPDATE,
280 if (fidp->volume == volp->rw.ID)
282 else if (fidp->volume == volp->ro.ID)
284 else if (fidp->volume == volp->bk.ID)
287 if (statep->state != vl_offline && statep->state != vl_unknown) {
290 if (cm_CheckOfflineVolume(volp, statep->ID))
298 else if (errorCode == CM_ERROR_ALLBUSY) {
299 /* Volumes that are busy cannot be determined to be non-busy
300 * without actually attempting to access them.
302 osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_ALLBUSY.");
307 if (fidp) { /* File Server query */
308 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp,
309 CM_GETVOL_FLAG_NO_LRU_UPDATE,
312 if (fidp->volume == volp->rw.ID)
314 else if (fidp->volume == volp->ro.ID)
316 else if (fidp->volume == volp->bk.ID)
319 if (statep->state != vl_offline &&
320 statep->state != vl_busy &&
321 statep->state != vl_unknown) {
325 code = cm_GetServerList(fidp, userp, reqp, &serverspp);
327 serversp = *serverspp;
331 lock_ObtainWrite(&cm_serverLock);
332 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
333 if (tsrp->status == srv_busy) {
334 tsrp->status = srv_not_busy;
337 lock_ReleaseWrite(&cm_serverLock);
339 cm_FreeServerList(&serversp, 0);
340 *serverspp = serversp;
343 cm_UpdateVolumeStatus(volp, statep->ID);
349 } else { /* VL Server query */
351 lock_ObtainWrite(&cm_serverLock);
352 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
353 if (tsrp->status == srv_busy) {
354 tsrp->status = srv_not_busy;
357 lock_ReleaseWrite(&cm_serverLock);
364 /* special codes: VBUSY and VRESTARTING */
365 else if (errorCode == VBUSY || errorCode == VRESTARTING) {
366 if (!serversp && fidp) {
367 code = cm_GetServerList(fidp, userp, reqp, &serverspp);
369 serversp = *serverspp;
373 lock_ObtainWrite(&cm_serverLock);
374 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
375 if (tsrp->server == serverp && tsrp->status == srv_not_busy) {
376 tsrp->status = srv_busy;
377 if (fidp) { /* File Server query */
378 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp,
379 CM_GETVOL_FLAG_NO_LRU_UPDATE,
382 if (fidp->volume == volp->rw.ID)
384 else if (fidp->volume == volp->ro.ID)
386 else if (fidp->volume == volp->bk.ID)
393 lock_ReleaseWrite(&cm_serverLock);
396 cm_UpdateVolumeStatus(volp, statep->ID);
401 cm_FreeServerList(&serversp, 0);
402 *serverspp = serversp;
407 /* special codes: missing volumes */
408 else if (errorCode == VNOVOL || errorCode == VMOVED || errorCode == VOFFLINE ||
409 errorCode == VSALVAGE || errorCode == VNOSERVICE || errorCode == VIO)
414 switch ( errorCode ) {
416 msgID = MSG_SERVER_REPORTS_VNOVOL;
417 format = "Server %s reported volume %d as not attached.";
420 msgID = MSG_SERVER_REPORTS_VMOVED;
421 format = "Server %s reported volume %d as moved.";
424 msgID = MSG_SERVER_REPORTS_VOFFLINE;
425 format = "Server %s reported volume %d as offline.";
428 msgID = MSG_SERVER_REPORTS_VSALVAGE;
429 format = "Server %s reported volume %d as needs salvage.";
432 msgID = MSG_SERVER_REPORTS_VNOSERVICE;
433 format = "Server %s reported volume %d as not in service.";
436 msgID = MSG_SERVER_REPORTS_VIO;
437 format = "Server %s reported volume %d as temporarily unaccessible.";
441 if (serverp && fidp) {
442 /* Log server being offline for this volume */
443 sprintf(addr, "%d.%d.%d.%d",
444 ((serverp->addr.sin_addr.s_addr & 0xff)),
445 ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
446 ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
447 ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
449 osi_Log2(afsd_logp, format, osi_LogSaveString(afsd_logp,addr), fidp->volume);
450 LogEvent(EVENTLOG_WARNING_TYPE, msgID, addr, fidp->volume);
453 /* Mark server offline for this volume */
454 if (!serversp && fidp) {
455 code = cm_GetServerList(fidp, userp, reqp, &serverspp);
457 serversp = *serverspp;
462 lock_ObtainWrite(&cm_serverLock);
463 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
464 if (tsrp->server == serverp) {
466 if (errorCode == VMOVED) {
467 tsrp->status = srv_deleted;
469 tsrp->status = srv_offline;
472 if (fidp) { /* File Server query */
473 code = cm_GetVolumeByID(cellp, fidp->volume, userp, reqp,
474 CM_GETVOL_FLAG_NO_LRU_UPDATE,
477 if (fidp->volume == volp->rw.ID)
479 else if (fidp->volume == volp->ro.ID)
481 else if (fidp->volume == volp->bk.ID)
487 lock_ReleaseWrite(&cm_serverLock);
489 if (fidp && errorCode == VMOVED)
490 cm_ForceUpdateVolume(fidp, userp, reqp);
493 cm_UpdateVolumeStatus(volp, statep->ID);
498 cm_FreeServerList(&serversp, 0);
499 *serverspp = serversp;
503 } else if ( errorCode == VNOVNODE ) {
506 osi_Log4(afsd_logp, "cm_Analyze passed VNOVNODE cell %u vol %u vn %u uniq %u.",
507 fidp->cell, fidp->volume, fidp->vnode, fidp->unique);
509 scp = cm_FindSCache(fidp);
511 cm_scache_t *pscp = NULL;
513 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
514 pscp = cm_FindSCacheParent(scp);
517 lock_ObtainMutex(&scp->mx);
518 lock_ObtainWrite(&cm_scacheLock);
519 cm_RemoveSCacheFromHashTable(scp);
520 lock_ReleaseWrite(&cm_scacheLock);
521 cm_LockMarkSCacheLost(scp);
522 scp->flags |= CM_SCACHEFLAG_DELETED;
523 lock_ReleaseMutex(&scp->mx);
524 cm_ReleaseSCache(scp);
527 if (cm_HaveCallback(pscp)) {
528 lock_ObtainMutex(&pscp->mx);
529 cm_DiscardSCache(pscp);
530 lock_ReleaseMutex(&pscp->mx);
532 cm_ReleaseSCache(pscp);
536 osi_Log0(afsd_logp, "cm_Analyze passed VNOVNODE unknown fid.");
541 else if (errorCode == RX_CALL_TIMEOUT) {
542 /* server took longer than hardDeadTime
543 * don't mark server as down but don't retry
544 * this is to prevent the SMB session from timing out
545 * In addition, we log an event to the event log
548 /* Log server being offline for this volume */
549 sprintf(addr, "%d.%d.%d.%d",
550 ((serverp->addr.sin_addr.s_addr & 0xff)),
551 ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
552 ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
553 ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
555 LogEvent(EVENTLOG_WARNING_TYPE, MSG_RX_HARD_DEAD_TIME_EXCEEDED, addr);
558 osi_Log1(afsd_logp, "cm_Analyze: hardDeadTime exceeded addr[%s]",
559 osi_LogSaveString(afsd_logp,addr));
561 else if (errorCode >= -64 && errorCode < 0) {
562 /* mark server as down */
563 lock_ObtainMutex(&serverp->mx);
564 if (reqp->flags & CM_REQ_NEW_CONN_FORCED) {
565 if (!(serverp->flags & CM_SERVERFLAG_DOWN)) {
566 serverp->flags |= CM_SERVERFLAG_DOWN;
567 serverp->downTime = time(NULL);
570 reqp->flags |= CM_REQ_NEW_CONN_FORCED;
573 lock_ReleaseMutex(&serverp->mx);
574 cm_ForceNewConnections(serverp);
578 else if (errorCode == RXKADEXPIRED) {
580 lock_ObtainMutex(&userp->mx);
581 ucellp = cm_GetUCell(userp, serverp->cellp);
582 if (ucellp->ticketp) {
583 free(ucellp->ticketp);
584 ucellp->ticketp = NULL;
586 ucellp->flags &= ~CM_UCELLFLAG_RXKAD;
588 lock_ReleaseMutex(&userp->mx);
592 } else if (errorCode == VICECONNBAD || errorCode == VICETOKENDEAD) {
593 cm_ForceNewConnections(serverp);
598 char * s = "unknown error";
599 switch ( errorCode ) {
600 case RXKADINCONSISTENCY: s = "RXKADINCONSISTENCY"; break;
601 case RXKADPACKETSHORT : s = "RXKADPACKETSHORT"; break;
602 case RXKADLEVELFAIL : s = "RXKADLEVELFAIL"; break;
603 case RXKADTICKETLEN : s = "RXKADTICKETLEN"; break;
604 case RXKADOUTOFSEQUENCE: s = "RXKADOUTOFSEQUENCE"; break;
605 case RXKADNOAUTH : s = "RXKADNOAUTH"; break;
606 case RXKADBADKEY : s = "RXKADBADKEY"; break;
607 case RXKADBADTICKET : s = "RXKADBADTICKET"; break;
608 case RXKADUNKNOWNKEY : s = "RXKADUNKNOWNKEY"; break;
609 case RXKADEXPIRED : s = "RXKADEXPIRED"; break;
610 case RXKADSEALEDINCON : s = "RXKADSEALEDINCON"; break;
611 case RXKADDATALEN : s = "RXKADDATALEN"; break;
612 case RXKADILLEGALLEVEL : s = "RXKADILLEGALLEVEL"; break;
613 case VSALVAGE : s = "VSALVAGE"; break;
614 case VNOVNODE : s = "VNOVNODE"; break;
615 case VNOVOL : s = "VNOVOL"; break;
616 case VVOLEXISTS : s = "VVOLEXISTS"; break;
617 case VNOSERVICE : s = "VNOSERVICE"; break;
618 case VOFFLINE : s = "VOFFLINE"; break;
619 case VONLINE : s = "VONLINE"; break;
620 case VDISKFULL : s = "VDISKFULL"; break;
621 case VOVERQUOTA : s = "VOVERQUOTA"; break;
622 case VBUSY : s = "VBUSY"; break;
623 case VMOVED : s = "VMOVED"; break;
624 case VIO : s = "VIO"; break;
625 case VRESTRICTED : s = "VRESTRICTED"; break;
626 case VRESTARTING : s = "VRESTARTING"; break;
627 case VREADONLY : s = "VREADONLY"; break;
628 case EAGAIN : s = "EAGAIN"; break;
629 case UAEAGAIN : s = "UAEAGAIN"; break;
630 case EINVAL : s = "EINVAL"; break;
631 case UAEINVAL : s = "UAEINVAL"; break;
632 case EACCES : s = "EACCES"; break;
633 case UAEACCES : s = "UAEACCES"; break;
634 case ENOENT : s = "ENOENT"; break;
635 case UAENOENT : s = "UAENOENT"; break;
636 case VICECONNBAD : s = "VICECONNBAD"; break;
637 case VICETOKENDEAD : s = "VICETOKENDEAD"; break;
638 case WSAEWOULDBLOCK : s = "WSAEWOULDBLOCK"; break;
639 case UAEWOULDBLOCK : s = "UAEWOULDBLOCK"; break;
640 case CM_ERROR_NOSUCHCELL : s = "CM_ERROR_NOSUCHCELL"; break;
641 case CM_ERROR_NOSUCHVOLUME : s = "CM_ERROR_NOSUCHVOLUME"; break;
642 case CM_ERROR_TIMEDOUT : s = "CM_ERROR_TIMEDOUT"; break;
643 case CM_ERROR_RETRY : s = "CM_ERROR_RETRY"; break;
644 case CM_ERROR_NOACCESS : s = "CM_ERROR_NOACCESS"; break;
645 case CM_ERROR_NOSUCHFILE : s = "CM_ERROR_NOSUCHFILE"; break;
646 case CM_ERROR_STOPNOW : s = "CM_ERROR_STOPNOW"; break;
647 case CM_ERROR_TOOBIG : s = "CM_ERROR_TOOBIG"; break;
648 case CM_ERROR_INVAL : s = "CM_ERROR_INVAL"; break;
649 case CM_ERROR_BADFD : s = "CM_ERROR_BADFD"; break;
650 case CM_ERROR_BADFDOP : s = "CM_ERROR_BADFDOP"; break;
651 case CM_ERROR_EXISTS : s = "CM_ERROR_EXISTS"; break;
652 case CM_ERROR_CROSSDEVLINK : s = "CM_ERROR_CROSSDEVLINK"; break;
653 case CM_ERROR_BADOP : s = "CM_ERROR_BADOP"; break;
654 case CM_ERROR_BADPASSWORD : s = "CM_ERROR_BADPASSWORD"; break;
655 case CM_ERROR_NOTDIR : s = "CM_ERROR_NOTDIR"; break;
656 case CM_ERROR_ISDIR : s = "CM_ERROR_ISDIR"; break;
657 case CM_ERROR_READONLY : s = "CM_ERROR_READONLY"; break;
658 case CM_ERROR_WOULDBLOCK : s = "CM_ERROR_WOULDBLOCK"; break;
659 case CM_ERROR_QUOTA : s = "CM_ERROR_QUOTA"; break;
660 case CM_ERROR_SPACE : s = "CM_ERROR_SPACE"; break;
661 case CM_ERROR_BADSHARENAME : s = "CM_ERROR_BADSHARENAME"; break;
662 case CM_ERROR_BADTID : s = "CM_ERROR_BADTID"; break;
663 case CM_ERROR_UNKNOWN : s = "CM_ERROR_UNKNOWN"; break;
664 case CM_ERROR_NOMORETOKENS : s = "CM_ERROR_NOMORETOKENS"; break;
665 case CM_ERROR_NOTEMPTY : s = "CM_ERROR_NOTEMPTY"; break;
666 case CM_ERROR_USESTD : s = "CM_ERROR_USESTD"; break;
667 case CM_ERROR_REMOTECONN : s = "CM_ERROR_REMOTECONN"; break;
668 case CM_ERROR_ATSYS : s = "CM_ERROR_ATSYS"; break;
669 case CM_ERROR_NOSUCHPATH : s = "CM_ERROR_NOSUCHPATH"; break;
670 case CM_ERROR_CLOCKSKEW : s = "CM_ERROR_CLOCKSKEW"; break;
671 case CM_ERROR_BADSMB : s = "CM_ERROR_BADSMB"; break;
672 case CM_ERROR_ALLBUSY : s = "CM_ERROR_ALLBUSY"; break;
673 case CM_ERROR_NOFILES : s = "CM_ERROR_NOFILES"; break;
674 case CM_ERROR_PARTIALWRITE : s = "CM_ERROR_PARTIALWRITE"; break;
675 case CM_ERROR_NOIPC : s = "CM_ERROR_NOIPC"; break;
676 case CM_ERROR_BADNTFILENAME : s = "CM_ERROR_BADNTFILENAME"; break;
677 case CM_ERROR_BUFFERTOOSMALL : s = "CM_ERROR_BUFFERTOOSMALL"; break;
678 case CM_ERROR_RENAME_IDENTICAL : s = "CM_ERROR_RENAME_IDENTICAL"; break;
679 case CM_ERROR_ALLOFFLINE : s = "CM_ERROR_ALLOFFLINE"; break;
680 case CM_ERROR_AMBIGUOUS_FILENAME: s = "CM_ERROR_AMBIGUOUS_FILENAME"; break;
681 case CM_ERROR_BADLOGONTYPE : s = "CM_ERROR_BADLOGONTYPE"; break;
682 case CM_ERROR_GSSCONTINUE : s = "CM_ERROR_GSSCONTINUE"; break;
683 case CM_ERROR_TIDIPC : s = "CM_ERROR_TIDIPC"; break;
684 case CM_ERROR_TOO_MANY_SYMLINKS : s = "CM_ERROR_TOO_MANY_SYMLINKS"; break;
685 case CM_ERROR_PATH_NOT_COVERED : s = "CM_ERROR_PATH_NOT_COVERED"; break;
686 case CM_ERROR_LOCK_CONFLICT : s = "CM_ERROR_LOCK_CONFLICT"; break;
687 case CM_ERROR_SHARING_VIOLATION : s = "CM_ERROR_SHARING_VIOLATION"; break;
688 case CM_ERROR_ALLDOWN : s = "CM_ERROR_ALLDOWN"; break;
689 case CM_ERROR_TOOFEWBUFS : s = "CM_ERROR_TOOFEWBUFS"; break;
690 case CM_ERROR_TOOMANYBUFS : s = "CM_ERROR_TOOMANYBUFS"; break;
692 osi_Log2(afsd_logp, "cm_Analyze: ignoring error code 0x%x (%s)",
698 /* If not allowed to retry, don't */
699 if (!forcing_new && (reqp->flags & CM_REQ_NORETRY))
701 else if (retry && dead_session)
705 /* drop this on the way out */
709 /* retry until we fail to find a connection */
713 long cm_ConnByMServers(cm_serverRef_t *serversp, cm_user_t *usersp,
714 cm_req_t *reqp, cm_conn_t **connpp)
717 cm_serverRef_t *tsrp;
720 int someBusy = 0, someOffline = 0, allOffline = 1, allBusy = 1, allDown = 1;
721 long timeUsed, timeLeft, hardTimeLeft;
725 if (serversp == NULL) {
726 osi_Log1(afsd_logp, "cm_ConnByMServers returning 0x%x", CM_ERROR_ALLDOWN);
727 return CM_ERROR_ALLDOWN;
730 timeUsed = (GetTickCount() - reqp->startTime) / 1000;
732 /* leave 5 seconds margin of safety */
733 timeLeft = ConnDeadtimeout - timeUsed - 5;
734 hardTimeLeft = HardDeadtimeout - timeUsed - 5;
736 lock_ObtainWrite(&cm_serverLock);
737 for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
739 cm_GetServerNoLock(tsp);
740 lock_ReleaseWrite(&cm_serverLock);
741 if (!(tsp->flags & CM_SERVERFLAG_DOWN)) {
743 if (tsrp->status == srv_deleted) {
744 /* skip this entry. no longer valid. */;
745 } else if (tsrp->status == srv_busy) {
748 } else if (tsrp->status == srv_offline) {
754 code = cm_ConnByServer(tsp, usersp, connpp);
755 if (code == 0) { /* cm_CBS only returns 0 */
757 /* Set RPC timeout */
758 if (timeLeft > ConnDeadtimeout)
759 timeLeft = ConnDeadtimeout;
761 if (hardTimeLeft > HardDeadtimeout)
762 hardTimeLeft = HardDeadtimeout;
764 lock_ObtainMutex(&(*connpp)->mx);
765 rx_SetConnDeadTime((*connpp)->callp, timeLeft);
766 rx_SetConnHardDeadTime((*connpp)->callp, (u_short) hardTimeLeft);
767 lock_ReleaseMutex(&(*connpp)->mx);
771 /* therefore, this code is never executed */
776 lock_ObtainWrite(&cm_serverLock);
777 cm_PutServerNoLock(tsp);
779 lock_ReleaseWrite(&cm_serverLock);
781 if (firstError == 0) {
783 firstError = CM_ERROR_ALLDOWN;
785 firstError = CM_ERROR_ALLBUSY;
786 else if (allOffline || (someBusy && someOffline))
787 firstError = CM_ERROR_ALLOFFLINE;
789 osi_Log0(afsd_logp, "cm_ConnByMServers returning impossible error TIMEDOUT");
790 firstError = CM_ERROR_TIMEDOUT;
794 osi_Log1(afsd_logp, "cm_ConnByMServers returning 0x%x", firstError);
798 /* called with a held server to GC all bad connections hanging off of the server */
799 void cm_GCConnections(cm_server_t *serverp)
805 lock_ObtainWrite(&cm_connLock);
806 lcpp = &serverp->connsp;
807 for (tcp = *lcpp; tcp; tcp = *lcpp) {
809 if (userp && tcp->refCount == 0 && (userp->vcRefs == 0)) {
810 /* do the deletion of this guy */
811 cm_PutServer(tcp->serverp);
812 cm_ReleaseUser(userp);
814 rx_DestroyConnection(tcp->callp);
815 lock_FinalizeMutex(&tcp->mx);
819 /* just advance to the next */
823 lock_ReleaseWrite(&cm_connLock);
826 static void cm_NewRXConnection(cm_conn_t *tcp, cm_ucell_t *ucellp,
827 cm_server_t *serverp)
832 struct rx_securityClass *secObjp;
834 if (serverp->type == CM_SERVER_VLDB) {
839 osi_assertx(serverp->type == CM_SERVER_FILE, "incorrect server type");
843 if (ucellp->flags & CM_UCELLFLAG_RXKAD) {
846 tcp->cryptlevel = rxkad_crypt;
848 tcp->cryptlevel = rxkad_clear;
850 secObjp = rxkad_NewClientSecurityObject(tcp->cryptlevel,
851 &ucellp->sessionKey, ucellp->kvno,
852 ucellp->ticketLen, ucellp->ticketp);
856 tcp->cryptlevel = rxkad_clear;
857 secObjp = rxnull_NewClientSecurityObject();
859 osi_assertx(secObjp != NULL, "null rx_securityClass");
860 tcp->callp = rx_NewConnection(serverp->addr.sin_addr.s_addr,
865 rx_SetConnDeadTime(tcp->callp, ConnDeadtimeout);
866 rx_SetConnHardDeadTime(tcp->callp, HardDeadtimeout);
867 tcp->ucgen = ucellp->gen;
869 rxs_Release(secObjp); /* Decrement the initial refCount */
872 long cm_ConnByServer(cm_server_t *serverp, cm_user_t *userp, cm_conn_t **connpp)
879 if (cm_anonvldb && serverp->type == CM_SERVER_VLDB)
880 userp = cm_rootUserp;
882 lock_ObtainMutex(&userp->mx);
883 lock_ObtainWrite(&cm_connLock);
884 for (tcp = serverp->connsp; tcp; tcp=tcp->nextp) {
885 if (tcp->userp == userp)
889 /* find ucell structure */
890 ucellp = cm_GetUCell(userp, serverp->cellp);
892 cm_GetServer(serverp);
893 tcp = malloc(sizeof(*tcp));
894 memset(tcp, 0, sizeof(*tcp));
895 tcp->nextp = serverp->connsp;
896 serverp->connsp = tcp;
899 lock_InitializeMutex(&tcp->mx, "cm_conn_t mutex");
900 lock_ObtainMutex(&tcp->mx);
901 tcp->serverp = serverp;
902 tcp->cryptlevel = rxkad_clear;
903 cm_NewRXConnection(tcp, ucellp, serverp);
905 lock_ReleaseMutex(&tcp->mx);
907 if ((tcp->flags & CM_CONN_FLAG_FORCE_NEW) ||
908 (tcp->ucgen < ucellp->gen) ||
909 (tcp->cryptlevel != (cryptall ? (ucellp->flags & CM_UCELLFLAG_RXKAD ? rxkad_crypt : rxkad_clear) : rxkad_clear)))
911 if (tcp->ucgen < ucellp->gen)
912 osi_Log0(afsd_logp, "cm_ConnByServer replace connection due to token update");
914 osi_Log0(afsd_logp, "cm_ConnByServer replace connection due to crypt change");
915 lock_ObtainMutex(&tcp->mx);
916 tcp->flags &= ~CM_CONN_FLAG_FORCE_NEW;
917 rx_DestroyConnection(tcp->callp);
918 cm_NewRXConnection(tcp, ucellp, serverp);
919 lock_ReleaseMutex(&tcp->mx);
923 lock_ReleaseWrite(&cm_connLock);
924 lock_ReleaseMutex(&userp->mx);
926 /* return this pointer to our caller */
927 osi_Log1(afsd_logp, "cm_ConnByServer returning conn 0x%p", tcp);
933 long cm_ServerAvailable(struct cm_fid *fidp, struct cm_user *userp)
937 cm_serverRef_t **serverspp;
938 cm_serverRef_t *tsrp;
940 int someBusy = 0, someOffline = 0, allOffline = 1, allBusy = 1, allDown = 1;
944 code = cm_GetServerList(fidp, userp, &req, &serverspp);
948 lock_ObtainWrite(&cm_serverLock);
949 for (tsrp = *serverspp; tsrp; tsrp=tsrp->next) {
951 cm_GetServerNoLock(tsp);
952 if (!(tsp->flags & CM_SERVERFLAG_DOWN)) {
954 if (tsrp->status == srv_busy) {
957 } else if (tsrp->status == srv_offline) {
965 cm_PutServerNoLock(tsp);
967 lock_ReleaseWrite(&cm_serverLock);
968 cm_FreeServerList(serverspp, 0);
974 else if (allOffline || (someBusy && someOffline))
981 * The returned cm_conn_t ** object is released in the subsequent call
984 long cm_ConnFromFID(struct cm_fid *fidp, struct cm_user *userp, cm_req_t *reqp,
988 cm_serverRef_t **serverspp;
992 code = cm_GetServerList(fidp, userp, reqp, &serverspp);
997 code = cm_ConnByMServers(*serverspp, userp, reqp, connpp);
998 cm_FreeServerList(serverspp, 0);
1003 long cm_ConnFromVolume(struct cm_volume *volp, unsigned long volid, struct cm_user *userp, cm_req_t *reqp,
1007 cm_serverRef_t **serverspp;
1011 serverspp = cm_GetVolServers(volp, volid);
1013 code = cm_ConnByMServers(*serverspp, userp, reqp, connpp);
1014 cm_FreeServerList(serverspp, 0);
1019 extern struct rx_connection *
1020 cm_GetRxConn(cm_conn_t *connp)
1022 struct rx_connection * rxconn;
1023 lock_ObtainMutex(&connp->mx);
1024 rxconn = connp->callp;
1025 rx_GetConnection(rxconn);
1026 lock_ReleaseMutex(&connp->mx);
1030 void cm_ForceNewConnections(cm_server_t *serverp)
1034 lock_ObtainWrite(&cm_connLock);
1035 for (tcp = serverp->connsp; tcp; tcp=tcp->nextp) {
1036 lock_ObtainMutex(&tcp->mx);
1037 tcp->flags |= CM_CONN_FLAG_FORCE_NEW;
1038 lock_ReleaseMutex(&tcp->mx);
1040 lock_ReleaseWrite(&cm_connLock);