Windows: Modify cm_GetVolServers and cm_GetServerList
[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 <afsconfig.h>
11 #include <afs/param.h>
12 #include <roken.h>
13
14 #include <afs/stds.h>
15
16 #include <windows.h>
17 #include <string.h>
18 #include <malloc.h>
19 #include <osi.h>
20 #include "afsd.h"
21 #include <rx/rx.h>
22 #include <rx/rxkad.h>
23 #include <afs/unified_afs.h>
24 #include <afs/vlserver.h>
25 #include <WINNT/afsreg.h>
26
27 osi_rwlock_t cm_connLock;
28
29 DWORD RDRtimeout = CM_CONN_DEFAULTRDRTIMEOUT;
30 unsigned short ConnDeadtimeout = CM_CONN_CONNDEADTIME;
31 unsigned short HardDeadtimeout = CM_CONN_HARDDEADTIME;
32 unsigned short IdleDeadtimeout = CM_CONN_IDLEDEADTIME;
33 unsigned short ReplicaIdleDeadtimeout = CM_CONN_IDLEDEADTIME_REP;
34 unsigned short NatPingInterval = CM_CONN_NATPINGINTERVAL;
35
36 #define LANMAN_WKS_PARAM_KEY "SYSTEM\\CurrentControlSet\\Services\\lanmanworkstation\\parameters"
37 #define LANMAN_WKS_SESSION_TIMEOUT "SessTimeout"
38 #define LANMAN_WKS_EXT_SESSION_TIMEOUT "ExtendedSessTimeout"
39
40 afs_uint32 cryptall = 0;
41 afs_uint32 cm_anonvldb = 0;
42 afs_uint32 rx_pmtu_discovery = 0;
43
44 void cm_PutConn(cm_conn_t *connp)
45 {
46     afs_int32 refCount = InterlockedDecrement(&connp->refCount);
47     osi_assertx(refCount >= 0, "cm_conn_t refcount underflow");
48 }
49
50 void cm_InitConn(void)
51 {
52     static osi_once_t once;
53     long code;
54     DWORD dwValue;
55     DWORD dummyLen;
56     HKEY parmKey;
57
58     if (osi_Once(&once)) {
59         lock_InitializeRWLock(&cm_connLock, "connection global lock",
60                                LOCK_HIERARCHY_CONN_GLOBAL);
61
62         /* keisa - read timeout value for lanmanworkstation  service.
63          * jaltman - as per
64          *   http://support.microsoft.com:80/support/kb/articles/Q102/0/67.asp&NoWebContent=1
65          * the SessTimeout is a minimum timeout not a maximum timeout.  Therefore,
66          * I believe that the default should not be short.  Instead, we should wait until
67          * RX times out before reporting a timeout to the SMB client.
68          */
69         code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, LANMAN_WKS_PARAM_KEY,
70                             0, KEY_QUERY_VALUE, &parmKey);
71         if (code == ERROR_SUCCESS)
72         {
73             BOOL extTimeouts = msftSMBRedirectorSupportsExtendedTimeouts();
74
75             if (extTimeouts) {
76                 /*
77                  * The default value is 1000 seconds.  However, this default
78                  * will not apply to "\\AFS" unless "AFS" is listed in
79                  * ServersWithExtendedSessTimeout which we will add when we
80                  * create the ExtendedSessTimeout value in smb_Init()
81                  */
82                 dummyLen = sizeof(DWORD);
83                 code = RegQueryValueEx(parmKey,
84                                        LANMAN_WKS_EXT_SESSION_TIMEOUT,
85                                         NULL, NULL,
86                                         (BYTE *) &dwValue, &dummyLen);
87                 if (code == ERROR_SUCCESS) {
88                     RDRtimeout = dwValue;
89                     afsi_log("lanmanworkstation : ExtSessTimeout %u", RDRtimeout);
90                 }
91             }
92             if (!extTimeouts || code != ERROR_SUCCESS) {
93                 dummyLen = sizeof(DWORD);
94                 code = RegQueryValueEx(parmKey,
95                                        LANMAN_WKS_SESSION_TIMEOUT,
96                                        NULL, NULL,
97                                        (BYTE *) &dwValue, &dummyLen);
98                 if (code == ERROR_SUCCESS) {
99                     RDRtimeout = dwValue;
100                     afsi_log("lanmanworkstation : SessTimeout %u", RDRtimeout);
101                 }
102             }
103             RegCloseKey(parmKey);
104         }
105
106         code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
107                              0, KEY_QUERY_VALUE, &parmKey);
108         if (code == ERROR_SUCCESS) {
109             dummyLen = sizeof(DWORD);
110             code = RegQueryValueEx(parmKey, "ConnDeadTimeout", NULL, NULL,
111                                     (BYTE *) &dwValue, &dummyLen);
112             if (code == ERROR_SUCCESS) {
113                 ConnDeadtimeout = (unsigned short)dwValue;
114                 afsi_log("ConnDeadTimeout is %d", ConnDeadtimeout);
115             }
116             dummyLen = sizeof(DWORD);
117             code = RegQueryValueEx(parmKey, "HardDeadTimeout", NULL, NULL,
118                                     (BYTE *) &dwValue, &dummyLen);
119             if (code == ERROR_SUCCESS) {
120                 HardDeadtimeout = (unsigned short)dwValue;
121                 afsi_log("HardDeadTimeout is %d", HardDeadtimeout);
122             }
123             dummyLen = sizeof(DWORD);
124             code = RegQueryValueEx(parmKey, "IdleDeadTimeout", NULL, NULL,
125                                     (BYTE *) &dwValue, &dummyLen);
126             if (code == ERROR_SUCCESS) {
127                 IdleDeadtimeout = (unsigned short)dwValue;
128                 afsi_log("IdleDeadTimeout is %d", IdleDeadtimeout);
129             }
130             dummyLen = sizeof(DWORD);
131             code = RegQueryValueEx(parmKey, "ReplicaIdleDeadTimeout", NULL, NULL,
132                                     (BYTE *) &dwValue, &dummyLen);
133             if (code == ERROR_SUCCESS) {
134                 ReplicaIdleDeadtimeout = (unsigned short)dwValue;
135                 afsi_log("ReplicaIdleDeadTimeout is %d", ReplicaIdleDeadtimeout);
136             }
137             dummyLen = sizeof(DWORD);
138             code = RegQueryValueEx(parmKey, "NatPingInterval", NULL, NULL,
139                                     (BYTE *) &dwValue, &dummyLen);
140             if (code == ERROR_SUCCESS) {
141                 NatPingInterval = (unsigned short)dwValue;
142             }
143             afsi_log("NatPingInterval is %d", NatPingInterval);
144             RegCloseKey(parmKey);
145         }
146
147         /*
148          * If these values were not set via cpp macro or obtained
149          * from the registry, we use a value that is derived from
150          * the smb redirector session timeout (RDRtimeout).
151          *
152          * The UNIX cache manager uses 120 seconds for the hard dead
153          * timeout and 50 seconds for the connection and idle timeouts.
154          *
155          * We base our values on those while making sure we leave
156          * enough time for overhead.
157          *
158          * To further complicate matters we need to take into account
159          * file server hard dead timeouts as they affect the length
160          * of time it takes the file server to give up when attempting
161          * to break callbacks to unresponsive clients.  The file
162          * server hard dead timeout is 120 seconds.
163          *
164          * For SMB, we have no choice but to timeout quickly.  For
165          * the AFS redirector, we can wait.
166          */
167         if (smb_Enabled) {
168             afsi_log("lanmanworkstation : SessTimeout %u", RDRtimeout);
169             if (ConnDeadtimeout == 0) {
170                 ConnDeadtimeout = (unsigned short) ((RDRtimeout / 2) < 50 ? (RDRtimeout / 2) : 50);
171                 afsi_log("ConnDeadTimeout is %d", ConnDeadtimeout);
172             }
173             if (HardDeadtimeout == 0) {
174                 HardDeadtimeout = (unsigned short) (RDRtimeout > 125 ? 120 : (RDRtimeout - 5));
175                 afsi_log("HardDeadTimeout is %d", HardDeadtimeout);
176             }
177             if (IdleDeadtimeout == 0) {
178                 IdleDeadtimeout = 10 * (unsigned short) HardDeadtimeout;
179                 afsi_log("IdleDeadTimeout is %d", IdleDeadtimeout);
180             }
181             if (ReplicaIdleDeadtimeout == 0) {
182                 ReplicaIdleDeadtimeout = (unsigned short) HardDeadtimeout;
183                 afsi_log("ReplicaIdleDeadTimeout is %d", ReplicaIdleDeadtimeout);
184             }
185         } else {
186             if (ConnDeadtimeout == 0) {
187                 ConnDeadtimeout = CM_CONN_IFS_CONNDEADTIME;
188                 afsi_log("ConnDeadTimeout is %d", ConnDeadtimeout);
189             }
190             if (HardDeadtimeout == 0) {
191                 HardDeadtimeout = CM_CONN_IFS_HARDDEADTIME;
192                 afsi_log("HardDeadTimeout is %d", HardDeadtimeout);
193             }
194             if (IdleDeadtimeout == 0) {
195                 IdleDeadtimeout = CM_CONN_IFS_IDLEDEADTIME;
196                 afsi_log("IdleDeadTimeout is %d", IdleDeadtimeout);
197             }
198             if (IdleDeadtimeout == 0) {
199                 ReplicaIdleDeadtimeout = CM_CONN_IFS_IDLEDEADTIME_REP;
200                 afsi_log("ReplicaIdleDeadTimeout is %d", ReplicaIdleDeadtimeout);
201             }
202         }
203         osi_EndOnce(&once);
204     }
205 }
206
207 void cm_InitReq(cm_req_t *reqp)
208 {
209         memset(reqp, 0, sizeof(cm_req_t));
210         reqp->startTime = GetTickCount();
211 }
212
213 long cm_GetServerList(struct cm_fid *fidp, struct cm_user *userp,
214         struct cm_req *reqp, afs_uint32 *replicated, cm_serverRef_t ***serversppp)
215 {
216     long code;
217     cm_volume_t *volp = NULL;
218     cm_cell_t *cellp = NULL;
219
220     if (!fidp) {
221         *serversppp = NULL;
222         return CM_ERROR_INVAL;
223     }
224
225     cellp = cm_FindCellByID(fidp->cell, 0);
226     if (!cellp)
227         return CM_ERROR_NOSUCHCELL;
228
229     code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp, CM_GETVOL_FLAG_CREATE, &volp);
230     if (code)
231         return code;
232
233     *serversppp = cm_GetVolServers(volp, fidp->volume, userp, reqp, replicated);
234
235     lock_ObtainRead(&cm_volumeLock);
236     cm_PutVolume(volp);
237     lock_ReleaseRead(&cm_volumeLock);
238     return (*serversppp ? 0 : CM_ERROR_NOSUCHVOLUME);
239 }
240
241 void
242 cm_SetServerBusyStatus(cm_serverRef_t *serversp, cm_server_t *serverp)
243 {
244     cm_serverRef_t *tsrp;
245
246     lock_ObtainWrite(&cm_serverLock);
247     for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
248         if (tsrp->status == srv_deleted)
249             continue;
250         if (cm_ServerEqual(tsrp->server, serverp) && tsrp->status == srv_not_busy) {
251             tsrp->status = srv_busy;
252             break;
253         }
254     }
255     lock_ReleaseWrite(&cm_serverLock);
256 }
257
258 void
259 cm_ResetServerBusyStatus(cm_serverRef_t *serversp)
260 {
261     cm_serverRef_t *tsrp;
262
263     lock_ObtainWrite(&cm_serverLock);
264     for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
265         if (tsrp->status == srv_deleted)
266             continue;
267         if (tsrp->status == srv_busy) {
268             tsrp->status = srv_not_busy;
269         }
270     }
271     lock_ReleaseWrite(&cm_serverLock);
272 }
273
274 /*
275  * Analyze the error return from an RPC.  Determine whether or not to retry,
276  * and if we're going to retry, determine whether failover is appropriate,
277  * and whether timed backoff is appropriate.
278  *
279  * If the error code is from cm_ConnFromFID() or friends, it will be a CM_ERROR code.
280  * Otherwise it will be an RPC code.  This may be a UNIX code (e.g. EDQUOT), or
281  * it may be an RX code, or it may be a special code (e.g. VNOVOL), or it may
282  * be a security code (e.g. RXKADEXPIRED).
283  *
284  * If the error code is from cm_ConnFromFID() or friends, connp will be NULL.
285  *
286  * For VLDB calls, fidp will be NULL and cellp will not be.
287  *
288  * volSyncp and/or cbrp may also be NULL.
289  */
290 int
291 cm_Analyze(cm_conn_t *connp,
292            cm_user_t *userp,
293            cm_req_t *reqp,
294            struct cm_fid *fidp,
295            cm_cell_t *cellp,
296            afs_uint32 storeOp,
297            AFSVolSync *volSyncp,
298            cm_serverRef_t * serversp,
299            cm_callbackRequest_t *cbrp,
300            long errorCode)
301 {
302     cm_server_t *serverp = NULL;
303     cm_serverRef_t **serverspp = NULL;
304     cm_serverRef_t *tsrp;
305     cm_ucell_t *ucellp;
306     cm_volume_t * volp = NULL;
307     cm_vol_state_t *statep = NULL;
308     cm_scache_t * scp = NULL;
309     afs_uint32 replicated;
310     int retry = 0;
311     int free_svr_list = 0;
312     int dead_session;
313     long timeUsed, timeLeft;
314     long code;
315     char addr[16]="unknown";
316     int forcing_new = 0;
317     int location_updated = 0;
318     char *format;
319     DWORD msgID;
320
321     osi_Log2(afsd_logp, "cm_Analyze connp 0x%p, code 0x%x",
322              connp, errorCode);
323
324     /* no locking required, since connp->serverp never changes after
325      * creation */
326     dead_session = (userp->cellInfop == NULL);
327     if (connp)
328         serverp = connp->serverp;
329
330     /* Update callback pointer */
331     if (cbrp && serverp && errorCode == 0) {
332         if (cbrp->serverp) {
333             if ( cbrp->serverp != serverp ) {
334                 lock_ObtainWrite(&cm_serverLock);
335                 cm_PutServerNoLock(cbrp->serverp);
336                 cm_GetServerNoLock(serverp);
337                 lock_ReleaseWrite(&cm_serverLock);
338             }
339         } else {
340             cm_GetServer(serverp);
341         }
342         cbrp->serverp = serverp;
343     }
344
345     /* if timeout - check that it did not exceed the HardDead timeout
346      * and retry */
347
348     /* timeleft - get it from reqp the same way as cm_ConnByMServers does */
349     timeUsed = (GetTickCount() - reqp->startTime) / 1000;
350     if ( reqp->flags & CM_REQ_SOURCE_SMB )
351         timeLeft = HardDeadtimeout - timeUsed;
352     else
353         timeLeft = 0x0FFFFFFF;
354
355     /* get a pointer to the cell */
356     if (errorCode) {
357         if (cellp == NULL && serverp)
358             cellp = serverp->cellp;
359         if (cellp == NULL && serversp) {
360             struct cm_serverRef * refp;
361             for ( refp=serversp ; cellp == NULL && refp != NULL; refp=refp->next) {
362                 if (refp->status == srv_deleted)
363                     continue;
364                 if ( refp->server )
365                     cellp = refp->server->cellp;
366             }
367         }
368         if (cellp == NULL && fidp) {
369             cellp = cm_FindCellByID(fidp->cell, 0);
370         }
371     }
372
373     if (errorCode == CM_ERROR_TIMEDOUT) {
374         if ( timeLeft > 5 ) {
375             thrd_Sleep(3000);
376             cm_CheckServers(CM_FLAG_CHECKDOWNSERVERS, cellp);
377             retry = 1;
378         }
379     }
380
381     else if (errorCode == UAEWOULDBLOCK || errorCode == EWOULDBLOCK ||
382               errorCode == UAEAGAIN || errorCode == EAGAIN) {
383         osi_Log0(afsd_logp, "cm_Analyze passed EWOULDBLOCK or EAGAIN.");
384         if ( timeLeft > 5 ) {
385             thrd_Sleep(1000);
386             retry = 1;
387         }
388     }
389
390     /* if there is nosuchvolume, then we have a situation in which a
391      * previously known volume no longer has a set of servers
392      * associated with it.  Either the volume has moved or
393      * the volume has been deleted.  Try to find a new server list
394      * until the timeout period expires.
395      */
396     else if (errorCode == CM_ERROR_NOSUCHVOLUME) {
397         osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_NOSUCHVOLUME.");
398         /*
399          * The VNOVOL or VL_NOENT error has already been translated
400          * to CM_ERROR_NOSUCHVOLUME.  There is nothing for us to do.
401          */
402     }
403
404     else if (errorCode == CM_ERROR_EMPTY) {
405         /*
406          * The server list is empty (or all entries have been deleted).
407          * If fidp is NULL, this was a vlServer list and we can attempt
408          * to force a cell lookup.  If fidp is not NULL, we can attempt
409          * to refresh the volume location list.
410          */
411         if (fidp) {
412             code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp,
413                                      CM_GETVOL_FLAG_NO_LRU_UPDATE,
414                                      &volp);
415             if (code == 0) {
416                 if (cm_UpdateVolumeLocation(cellp, userp, reqp, volp) == 0) {
417                     code = cm_GetServerList(fidp, userp, reqp, &replicated, &serverspp);
418                     if (code == 0) {
419                         if (!cm_IsServerListEmpty(*serverspp))
420                             retry = 1;
421                         cm_FreeServerList(serverspp, 0);
422                     }
423                 }
424
425                 lock_ObtainRead(&cm_volumeLock);
426                 cm_PutVolume(volp);
427                 lock_ReleaseRead(&cm_volumeLock);
428                 volp = NULL;
429             }
430         } else {
431             cm_cell_t * newCellp = cm_UpdateCell( cellp, 0);
432             if (newCellp)
433                 retry = 1;
434         }
435     }
436     else if (errorCode == CM_ERROR_ALLDOWN) {
437         /* Servers marked DOWN will be restored by the background daemon
438          * thread as they become available.  The volume status is
439          * updated as the server state changes.
440          */
441         if (fidp) {
442             osi_Log2(afsd_logp, "cm_Analyze passed CM_ERROR_DOWN (FS cell %s vol 0x%x)",
443                       cellp->name, fidp->volume);
444             msgID = MSG_ALL_SERVERS_DOWN;
445             format = "All servers are unreachable when accessing cell %s volume %d.";
446             LogEvent(EVENTLOG_WARNING_TYPE, msgID, cellp->name, fidp->volume);
447         } else {
448             osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_ALLDOWN (VL Server)");
449         }
450     }
451     else if (errorCode == CM_ERROR_ALLOFFLINE) {
452         /* Volume instances marked offline will be restored by the
453          * background daemon thread as they become available
454          */
455         if (fidp) {
456             osi_Log2(afsd_logp, "cm_Analyze passed CM_ERROR_ALLOFFLINE (FS cell %s vol 0x%x)",
457                       cellp->name, fidp->volume);
458             msgID = MSG_ALL_SERVERS_OFFLINE;
459             format = "All servers are offline when accessing cell %s volume %d.";
460             LogEvent(EVENTLOG_WARNING_TYPE, msgID, cellp->name, fidp->volume);
461
462             if (!serversp) {
463                 code = cm_GetServerList(fidp, userp, reqp, &replicated, &serverspp);
464                 if (code == 0) {
465                     serversp = *serverspp;
466                     free_svr_list = 1;
467                 }
468             }
469             cm_ResetServerBusyStatus(serversp);
470             if (free_svr_list) {
471                 cm_FreeServerList(serverspp, 0);
472                 free_svr_list = 0;
473                 serversp = NULL;
474             }
475
476             code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp,
477                                       CM_GETVOL_FLAG_NO_LRU_UPDATE,
478                                       &volp);
479             if (code == 0) {
480                 /*
481                  * Do not perform a cm_CheckOfflineVolume() if cm_Analyze()
482                  * was called by cm_CheckOfflineVolumeState().
483                  */
484                 if (!(reqp->flags & CM_REQ_OFFLINE_VOL_CHK) && timeLeft > 7) {
485                     thrd_Sleep(5000);
486
487                     /* cm_CheckOfflineVolume() resets the serverRef state */
488                     if (cm_CheckOfflineVolume(volp, fidp->volume))
489                         retry = 1;
490                 } else {
491                     cm_UpdateVolumeStatus(volp, fidp->volume);
492                 }
493                 lock_ObtainRead(&cm_volumeLock);
494                 cm_PutVolume(volp);
495                 lock_ReleaseRead(&cm_volumeLock);
496                 volp = NULL;
497             }
498         } else {
499             osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_ALLOFFLINE (VL Server)");
500         }
501     }
502     else if (errorCode == CM_ERROR_ALLBUSY) {
503         /* Volumes that are busy cannot be determined to be non-busy
504          * without actually attempting to access them.
505          */
506         if (fidp) { /* File Server query */
507             osi_Log2(afsd_logp, "cm_Analyze passed CM_ERROR_ALLBUSY (FS cell %s vol 0x%x)",
508                      cellp->name, fidp->volume);
509             msgID = MSG_ALL_SERVERS_BUSY;
510             format = "All servers are busy when accessing cell %s volume %d.";
511             LogEvent(EVENTLOG_WARNING_TYPE, msgID, cellp->name, fidp->volume);
512
513             if (!serversp) {
514                 code = cm_GetServerList(fidp, userp, reqp, &replicated, &serverspp);
515                 if (code == 0) {
516                     serversp = *serverspp;
517                     free_svr_list = 1;
518                 }
519             }
520             cm_ResetServerBusyStatus(serversp);
521             if (free_svr_list) {
522                 cm_FreeServerList(serverspp, 0);
523                 free_svr_list = 0;
524                 serversp = NULL;
525             }
526
527             code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp,
528                                      CM_GETVOL_FLAG_NO_LRU_UPDATE,
529                                      &volp);
530             if (code == 0) {
531                 if (timeLeft > 7) {
532                     thrd_Sleep(5000);
533                     statep = cm_VolumeStateByID(volp, fidp->volume);
534                     retry = 1;
535                 }
536                 cm_UpdateVolumeStatus(volp, fidp->volume);
537
538                 lock_ObtainRead(&cm_volumeLock);
539                 cm_PutVolume(volp);
540                 lock_ReleaseRead(&cm_volumeLock);
541                 volp = NULL;
542             }
543         } else {    /* VL Server query */
544             osi_Log0(afsd_logp, "cm_Analyze passed CM_ERROR_ALLBUSY (VL Server).");
545
546             if (timeLeft > 7) {
547                 thrd_Sleep(5000);
548
549                 if (serversp) {
550                     cm_ResetServerBusyStatus(serversp);
551                     retry = 1;
552                 }
553             }
554         }
555     }
556
557     /* special codes:  VBUSY and VRESTARTING */
558     else if (errorCode == VBUSY || errorCode == VRESTARTING) {
559         if (!serversp && fidp) {
560             code = cm_GetServerList(fidp, userp, reqp, &replicated, &serverspp);
561             if (code == 0) {
562                 serversp = *serverspp;
563                 free_svr_list = 1;
564             }
565         }
566
567         switch ( errorCode ) {
568         case VBUSY:
569             msgID = MSG_SERVER_REPORTS_VBUSY;
570             format = "Server %s reported busy when accessing volume %d in cell %s.";
571             break;
572         case VRESTARTING:
573             msgID = MSG_SERVER_REPORTS_VRESTARTING;
574             format = "Server %s reported restarting when accessing volume %d in cell %s.";
575             break;
576         }
577
578         if (serverp && fidp) {
579             /* Log server being offline for this volume */
580             sprintf(addr, "%d.%d.%d.%d",
581                    ((serverp->addr.sin_addr.s_addr & 0xff)),
582                    ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
583                    ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
584                    ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
585
586             osi_Log3(afsd_logp, format, osi_LogSaveString(afsd_logp,addr), fidp->volume, cellp->name);
587             LogEvent(EVENTLOG_WARNING_TYPE, msgID, addr, fidp->volume, cellp->name);
588         }
589
590         cm_SetServerBusyStatus(serversp, serverp);
591
592         if (fidp) { /* File Server query */
593             code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp,
594                                       CM_GETVOL_FLAG_NO_LRU_UPDATE,
595                                       &volp);
596             if (code == 0) {
597                 statep = cm_VolumeStateByID(volp, fidp->volume);
598
599                 if (statep)
600                     cm_UpdateVolumeStatus(volp, statep->ID);
601
602                 lock_ObtainRead(&cm_volumeLock);
603                 cm_PutVolume(volp);
604                 lock_ReleaseRead(&cm_volumeLock);
605                 volp = NULL;
606             }
607         }
608
609         if (free_svr_list) {
610             cm_FreeServerList(serverspp, 0);
611             serversp = NULL;
612             free_svr_list = 0;
613         }
614         retry = 1;
615     }
616
617     /* special codes:  missing volumes */
618     else if (errorCode == VNOVOL || errorCode == VMOVED || errorCode == VOFFLINE ||
619              errorCode == VSALVAGE || errorCode == VIO)
620     {
621         /* In case of timeout */
622         reqp->volumeError = errorCode;
623
624         switch ( errorCode ) {
625         case VNOVOL:
626             msgID = MSG_SERVER_REPORTS_VNOVOL;
627             format = "Server %s reported volume %d in cell %s as not attached (may have been moved or deleted).";
628             break;
629         case VMOVED:
630             msgID = MSG_SERVER_REPORTS_VMOVED;
631             format = "Server %s reported volume %d in cell %s as moved.";
632             break;
633         case VOFFLINE:
634             msgID = MSG_SERVER_REPORTS_VOFFLINE;
635             format = "Server %s reported volume %d in cell %s as offline.";
636             break;
637         case VSALVAGE:
638             msgID = MSG_SERVER_REPORTS_VSALVAGE;
639             format = "Server %s reported volume %d in cell %s as needs salvage.";
640             break;
641         case VIO:
642             msgID = MSG_SERVER_REPORTS_VIO;
643             format = "Server %s reported volume %d in cell %s as temporarily unaccessible.";
644             break;
645         }
646
647         if (fidp) { /* File Server query */
648             if (serverp) {
649                 /* Log server being unavailable for this volume */
650                 sprintf(addr, "%d.%d.%d.%d",
651                          ((serverp->addr.sin_addr.s_addr & 0xff)),
652                          ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
653                          ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
654                          ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
655
656                 osi_Log3(afsd_logp, format, osi_LogSaveString(afsd_logp,addr), fidp->volume, cellp->name);
657                 LogEvent(EVENTLOG_WARNING_TYPE, msgID, addr, fidp->volume, cellp->name);
658             }
659
660             code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp,
661                                       CM_GETVOL_FLAG_NO_LRU_UPDATE,
662                                       &volp);
663             if (code == 0)
664                 statep = cm_VolumeStateByID(volp, fidp->volume);
665
666             if ((errorCode == VMOVED || errorCode == VNOVOL || errorCode == VOFFLINE) &&
667                 !(reqp->flags & CM_REQ_VOLUME_UPDATED))
668             {
669                 LONG_PTR oldSum, newSum;
670
671                 oldSum = cm_ChecksumVolumeServerList(fidp, userp, reqp);
672
673                 code = cm_ForceUpdateVolume(fidp, userp, reqp);
674                 if (code == 0) {
675                     location_updated = 1;
676                     newSum = cm_ChecksumVolumeServerList(fidp, userp, reqp);
677                 }
678
679                 /*
680                  * Even if the update fails, there might still be another replica.
681                  * If the volume location list changed, permit another update on
682                  * a subsequent error.
683                  */
684                 if (code || oldSum == newSum)
685                     reqp->flags |= CM_REQ_VOLUME_UPDATED;
686
687                 osi_Log3(afsd_logp, "cm_Analyze called cm_ForceUpdateVolume cell %u vol %u code 0x%x",
688                          fidp->cell, fidp->volume, code);
689             }
690
691             if (statep) {
692                 cm_UpdateVolumeStatus(volp, statep->ID);
693                 osi_Log3(afsd_logp, "cm_Analyze NewVolState cell %u vol %u state %u",
694                          fidp->cell, fidp->volume, statep->state);
695             }
696
697             if (volp) {
698                 lock_ObtainRead(&cm_volumeLock);
699                 cm_PutVolume(volp);
700                 lock_ReleaseRead(&cm_volumeLock);
701                 volp = NULL;
702             }
703
704             /*
705              * Mark server offline for this volume or delete the volume
706              * from the server list if it was moved or is not present.
707              */
708             if (!serversp || location_updated) {
709                 code = cm_GetServerList(fidp, userp, reqp, &replicated, &serverspp);
710                 if (code == 0) {
711                     serversp = *serverspp;
712                     free_svr_list = 1;
713                 }
714             }
715         }
716
717         lock_ObtainWrite(&cm_serverLock);
718         for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
719             if (tsrp->status == srv_deleted)
720                 continue;
721
722             sprintf(addr, "%d.%d.%d.%d",
723                      ((tsrp->server->addr.sin_addr.s_addr & 0xff)),
724                      ((tsrp->server->addr.sin_addr.s_addr & 0xff00)>> 8),
725                      ((tsrp->server->addr.sin_addr.s_addr & 0xff0000)>> 16),
726                      ((tsrp->server->addr.sin_addr.s_addr & 0xff000000)>> 24));
727
728             if (cm_ServerEqual(tsrp->server, serverp)) {
729                 /* REDIRECT */
730                 switch (errorCode) {
731                 case VMOVED:
732                     osi_Log2(afsd_logp, "volume %u moved from server %s",
733                              fidp->volume, osi_LogSaveString(afsd_logp,addr));
734                     tsrp->status = srv_deleted;
735                     if (fidp)
736                         cm_RemoveVolumeFromServer(serverp, fidp->volume);
737                     break;
738                 case VNOVOL:
739                     /*
740                      * The 1.6.0 and 1.6.1 file servers send transient VNOVOL errors which
741                      * are no indicative of the volume not being present.  For example,
742                      * VNOVOL can be sent during a transition to a VBUSY state prior to
743                      * salvaging or when cloning a .backup volume instance.  As a result
744                      * the cache manager must attempt at least one retry when a VNOVOL is
745                      * receive but there are no changes to the volume location information.
746                      */
747                     if (reqp->vnovolError > 0 && cm_ServerEqual(reqp->errorServp, serverp)) {
748                         osi_Log2(afsd_logp, "volume %u not present on server %s",
749                                   fidp->volume, osi_LogSaveString(afsd_logp,addr));
750                         tsrp->status = srv_deleted;
751                         if (fidp)
752                             cm_RemoveVolumeFromServer(serverp, fidp->volume);
753                     } else {
754                         osi_Log2(afsd_logp, "VNOVOL received for volume %u from server %s",
755                                  fidp->volume, osi_LogSaveString(afsd_logp,addr));
756                         if (replicated) {
757                             if (tsrp->status == srv_not_busy)
758                                 tsrp->status = srv_busy;
759                         } else {
760                             Sleep(2000);
761                         }
762                     }
763                     break;
764                 default:
765                     osi_Log3(afsd_logp, "volume %u exists on server %s with status %u",
766                              fidp->volume, osi_LogSaveString(afsd_logp,addr), tsrp->status);
767                 }
768             }
769         }
770         lock_ReleaseWrite(&cm_serverLock);
771
772         /* Remember that the VNOVOL error occurred */
773         if (errorCode == VNOVOL) {
774             reqp->errorServp = serverp;
775             reqp->vnovolError++;
776         }
777
778         /* Free the server list before cm_ForceUpdateVolume is called */
779         if (free_svr_list) {
780             cm_FreeServerList(serverspp, 0);
781             serversp = NULL;
782             free_svr_list = 0;
783         }
784
785         if ( timeLeft > 2 )
786             retry = 1;
787     } else if ( errorCode == VNOVNODE ) {
788         if ( fidp ) {
789             osi_Log4(afsd_logp, "cm_Analyze passed VNOVNODE cell %u vol %u vn %u uniq %u.",
790                       fidp->cell, fidp->volume, fidp->vnode, fidp->unique);
791
792             scp = cm_FindSCache(fidp);
793             if (scp) {
794                 cm_scache_t *pscp = NULL;
795
796                 if (scp->fileType != CM_SCACHETYPE_DIRECTORY)
797                     pscp = cm_FindSCacheParent(scp);
798
799                 lock_ObtainWrite(&scp->rw);
800                 scp->flags |= CM_SCACHEFLAG_DELETED;
801                 lock_ObtainWrite(&cm_scacheLock);
802                 cm_AdjustScacheLRU(scp);
803                 cm_RemoveSCacheFromHashTable(scp);
804                 lock_ReleaseWrite(&cm_scacheLock);
805                 cm_LockMarkSCacheLost(scp);
806                 lock_ReleaseWrite(&scp->rw);
807                 if (RDR_Initialized)
808                     RDR_InvalidateObject(scp->fid.cell, scp->fid.volume, scp->fid.vnode, scp->fid.unique,
809                                           scp->fid.hash, scp->fileType, AFS_INVALIDATE_DELETED);
810                 cm_ReleaseSCache(scp);
811
812                 if (pscp) {
813                     if (cm_HaveCallback(pscp)) {
814                         lock_ObtainWrite(&pscp->rw);
815                         cm_DiscardSCache(pscp);
816                         lock_ReleaseWrite(&pscp->rw);
817
818                         if (RDR_Initialized)
819                             RDR_InvalidateObject(pscp->fid.cell, pscp->fid.volume, pscp->fid.vnode, pscp->fid.unique,
820                                                  pscp->fid.hash, pscp->fileType, AFS_INVALIDATE_EXPIRED);
821
822                     }
823                     cm_ReleaseSCache(pscp);
824                 }
825             }
826         } else {
827             osi_Log0(afsd_logp, "cm_Analyze passed VNOVNODE unknown fid.");
828         }
829     }
830
831     /* RX codes */
832     else if (errorCode == RX_CALL_TIMEOUT) {
833         /* RPC took longer than hardDeadTime or the server
834          * reported idle for longer than idleDeadTime
835          * don't mark server as down but don't retry
836          * this is to prevent the SMB session from timing out
837          * In addition, we log an event to the event log
838          */
839
840         if (serverp) {
841             sprintf(addr, "%d.%d.%d.%d",
842                     ((serverp->addr.sin_addr.s_addr & 0xff)),
843                     ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
844                     ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
845                     ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
846
847             LogEvent(EVENTLOG_WARNING_TYPE, MSG_RX_HARD_DEAD_TIME_EXCEEDED, addr);
848             osi_Log1(afsd_logp, "cm_Analyze: hardDeadTime or idleDeadtime exceeded addr[%s]",
849                      osi_LogSaveString(afsd_logp,addr));
850             reqp->errorServp = serverp;
851             reqp->idleError++;
852         }
853
854         if (fidp && storeOp)
855             scp = cm_FindSCache(fidp);
856         if (scp) {
857             if (cm_HaveCallback(scp)) {
858                 lock_ObtainWrite(&scp->rw);
859                 cm_DiscardSCache(scp);
860                 lock_ReleaseWrite(&scp->rw);
861
862                 /*
863                 * We really should notify the redirector that we discarded
864                 * the status information but doing so in this case is not
865                 * safe as it can result in a deadlock with extent release
866                 * processing.
867                 */
868             }
869             cm_ReleaseSCache(scp);
870         }
871
872         if (timeLeft > 2) {
873             if (!fidp) { /* vldb */
874                 retry = 1;
875             } else { /* file */
876                 cm_volume_t *volp = cm_GetVolumeByFID(fidp);
877                 if (volp) {
878                     if (fidp->volume == cm_GetROVolumeID(volp))
879                         retry = 1;
880                     cm_PutVolume(volp);
881                 }
882             }
883         }
884     }
885     else if (errorCode == RX_MSGSIZE) {
886         /*
887          * RPC failed because a transmitted rx packet
888          * may have grown larger than the path mtu.
889          * Force a retry and the Rx library will try
890          * with a smaller mtu size.
891          */
892
893         if (serverp)
894             sprintf(addr, "%d.%d.%d.%d",
895                     ((serverp->addr.sin_addr.s_addr & 0xff)),
896                     ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
897                     ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
898                     ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
899
900         LogEvent(EVENTLOG_WARNING_TYPE, MSG_RX_MSGSIZE_EXCEEDED, addr);
901         osi_Log1(afsd_logp, "cm_Analyze: Path MTU may have been exceeded addr[%s]",
902                  osi_LogSaveString(afsd_logp,addr));
903
904         retry = 1;
905     }
906     else if (errorCode == RX_CALL_BUSY) {
907         /*
908          * RPC failed because the selected call channel
909          * is currently busy on the server.  Unconditionally
910          * retry the request so an alternate call channel can be used.
911          */
912         if (serverp)
913             sprintf(addr, "%d.%d.%d.%d",
914                     ((serverp->addr.sin_addr.s_addr & 0xff)),
915                     ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
916                     ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
917                     ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
918
919         LogEvent(EVENTLOG_WARNING_TYPE, MSG_RX_BUSY_CALL_CHANNEL, addr);
920         osi_Log1(afsd_logp, "cm_Analyze: Retry RPC due to busy call channel addr[%s]",
921                  osi_LogSaveString(afsd_logp,addr));
922         retry = 1;
923     }
924     else if (errorCode == VNOSERVICE) {
925         /*
926          * The server did not service the RPC.
927          * If this was a file server RPC it means that for at
928          * least the file server's idle dead timeout period the
929          * file server did not receive any new data packets from
930          * client.
931          *
932          * The RPC was not serviced so it can be retried and any
933          * existing status information is still valid.
934          */
935         if (fidp) {
936             if (serverp)
937                 sprintf(addr, "%d.%d.%d.%d",
938                         ((serverp->addr.sin_addr.s_addr & 0xff)),
939                         ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
940                         ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
941                         ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
942
943             LogEvent(EVENTLOG_WARNING_TYPE, MSG_SERVER_REPORTS_VNOSERVICE,
944                      addr, fidp->volume, cellp->name);
945             osi_Log3(afsd_logp, "Server %s reported rpc to volume %d in cell %s as not serviced.",
946                      osi_LogSaveString(afsd_logp,addr), fidp->volume, cellp->name);
947         }
948
949         if (timeLeft > 2)
950             retry = 1;
951     }
952     else if (errorCode == RX_CALL_IDLE) {
953         /*
954          * RPC failed because the server failed to respond with data
955          * within the idle dead timeout period.  This could be for a variety
956          * of reasons:
957          *  1. The server could have a bad partition such as a failed
958          *     disk or iSCSI target and all I/O to that partition is
959          *     blocking on the server and will never complete.
960          *
961          *  2. The server vnode may be locked by another client request
962          *     that is taking a very long time.
963          *
964          *  3. The server may have a very long queue of requests
965          *     pending and is unable to process this request.
966          *
967          *  4. The server could be malicious and is performing a denial
968          *     of service attack against the client.
969          *
970          * If this is a request against a .readonly with alternate sites
971          * the server should be marked down for this request and the
972          * client should fail over to another server.  If this is a
973          * request against a single source, the client may retry once.
974          */
975         if (serverp)
976             sprintf(addr, "%d.%d.%d.%d",
977                     ((serverp->addr.sin_addr.s_addr & 0xff)),
978                     ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
979                     ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
980                     ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
981
982         if (fidp) {
983             code = cm_FindVolumeByID(cellp, fidp->volume, userp, reqp,
984                                      CM_GETVOL_FLAG_NO_LRU_UPDATE,
985                                      &volp);
986             if (code == 0) {
987                 statep = cm_VolumeStateByID(volp, fidp->volume);
988
989                 if (statep)
990                     replicated = (statep->flags & CM_VOL_STATE_FLAG_REPLICATED);
991
992                 lock_ObtainRead(&cm_volumeLock);
993                 cm_PutVolume(volp);
994                 lock_ReleaseRead(&cm_volumeLock);
995                 volp = NULL;
996             }
997
998             if (storeOp)
999                 scp = cm_FindSCache(fidp);
1000             if (scp) {
1001                 if (cm_HaveCallback(scp)) {
1002                     lock_ObtainWrite(&scp->rw);
1003                     cm_DiscardSCache(scp);
1004                     lock_ReleaseWrite(&scp->rw);
1005
1006                     /*
1007                      * We really should notify the redirector that we discarded
1008                      * the status information but doing so in this case is not
1009                      * safe as it can result in a deadlock with extent release
1010                      * processing.
1011                      */
1012                 }
1013                 cm_ReleaseSCache(scp);
1014             }
1015         }
1016
1017         if (replicated && serverp) {
1018             reqp->errorServp = serverp;
1019             reqp->tokenError = errorCode;
1020
1021             if (timeLeft > 2)
1022                 retry = 1;
1023         }
1024
1025         LogEvent(EVENTLOG_WARNING_TYPE, MSG_RX_IDLE_DEAD_TIMEOUT, addr, retry);
1026         osi_Log2(afsd_logp, "cm_Analyze: RPC failed due to idle dead timeout addr[%s] retry=%u",
1027                  osi_LogSaveString(afsd_logp,addr), retry);
1028     }
1029     else if (errorCode == RX_CALL_DEAD) {
1030         /* mark server as down */
1031         if (serverp)
1032             sprintf(addr, "%d.%d.%d.%d",
1033                     ((serverp->addr.sin_addr.s_addr & 0xff)),
1034                     ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
1035                     ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
1036                     ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
1037
1038         osi_Log2(afsd_logp, "cm_Analyze: Rx Call Dead addr[%s] forcedNew[%s]",
1039                  osi_LogSaveString(afsd_logp,addr),
1040                  (reqp->flags & CM_REQ_NEW_CONN_FORCED ? "yes" : "no"));
1041
1042         if (serverp) {
1043             if ((reqp->flags & CM_REQ_NEW_CONN_FORCED)) {
1044                 lock_ObtainMutex(&serverp->mx);
1045                 if (!(serverp->flags & CM_SERVERFLAG_DOWN)) {
1046                     _InterlockedOr(&serverp->flags, CM_SERVERFLAG_DOWN);
1047                     serverp->downTime = time(NULL);
1048                 }
1049                 lock_ReleaseMutex(&serverp->mx);
1050             } else {
1051                 reqp->flags |= CM_REQ_NEW_CONN_FORCED;
1052                 forcing_new = 1;
1053                 cm_ForceNewConnections(serverp);
1054             }
1055         }
1056
1057         if (fidp && storeOp)
1058             scp = cm_FindSCache(fidp);
1059         if (scp) {
1060             if (cm_HaveCallback(scp)) {
1061                 lock_ObtainWrite(&scp->rw);
1062                 cm_DiscardSCache(scp);
1063                 lock_ReleaseWrite(&scp->rw);
1064
1065                 /*
1066                 * We really should notify the redirector that we discarded
1067                 * the status information but doing so in this case is not
1068                 * safe as it can result in a deadlock with extent release
1069                 * processing.
1070                 */
1071             }
1072             cm_ReleaseSCache(scp);
1073         }
1074
1075         if ( timeLeft > 2 )
1076             retry = 1;
1077     }
1078     else if (errorCode >= -64 && errorCode < 0) {
1079         /* mark server as down */
1080         if (serverp)
1081             sprintf(addr, "%d.%d.%d.%d",
1082                     ((serverp->addr.sin_addr.s_addr & 0xff)),
1083                     ((serverp->addr.sin_addr.s_addr & 0xff00)>> 8),
1084                     ((serverp->addr.sin_addr.s_addr & 0xff0000)>> 16),
1085                     ((serverp->addr.sin_addr.s_addr & 0xff000000)>> 24));
1086
1087         osi_Log3(afsd_logp, "cm_Analyze: Rx Misc Error[%d] addr[%s] forcedNew[%s]",
1088                  errorCode,
1089                  osi_LogSaveString(afsd_logp,addr),
1090                  (reqp->flags & CM_REQ_NEW_CONN_FORCED ? "yes" : "no"));
1091
1092         if (serverp) {
1093             if (reqp->flags & CM_REQ_NEW_CONN_FORCED) {
1094                 reqp->errorServp = serverp;
1095                 reqp->tokenError = errorCode;
1096             } else {
1097                 reqp->flags |= CM_REQ_NEW_CONN_FORCED;
1098                 forcing_new = 1;
1099                 cm_ForceNewConnections(serverp);
1100             }
1101         }
1102         if ( timeLeft > 2 )
1103             retry = 1;
1104     }
1105     else if (errorCode == RXKADEXPIRED) {
1106         osi_Log1(afsd_logp, "cm_Analyze: rxkad error code 0x%x (RXKADEXPIRED)",
1107                  errorCode);
1108         if (!dead_session) {
1109             lock_ObtainMutex(&userp->mx);
1110             ucellp = cm_GetUCell(userp, cellp);
1111             if (ucellp->ticketp) {
1112                 free(ucellp->ticketp);
1113                 ucellp->ticketp = NULL;
1114             }
1115             _InterlockedAnd(&ucellp->flags, ~CM_UCELLFLAG_RXKAD);
1116             ucellp->gen++;
1117             lock_ReleaseMutex(&userp->mx);
1118             if ( timeLeft > 2 )
1119                 retry = 1;
1120         }
1121     } else if (errorCode >= ERROR_TABLE_BASE_RXK && errorCode < ERROR_TABLE_BASE_RXK + 256) {
1122         char * s = "unknown error";
1123         switch ( errorCode ) {
1124         case RXKADINCONSISTENCY: s = "RXKADINCONSISTENCY"; break;
1125         case RXKADPACKETSHORT  : s = "RXKADPACKETSHORT";   break;
1126         case RXKADLEVELFAIL    : s = "RXKADLEVELFAIL";     break;
1127         case RXKADTICKETLEN    : s = "RXKADTICKETLEN";     break;
1128         case RXKADOUTOFSEQUENCE: s = "RXKADOUTOFSEQUENCE"; break;
1129         case RXKADNOAUTH       : s = "RXKADNOAUTH";        break;
1130         case RXKADBADKEY       : s = "RXKADBADKEY";        break;
1131         case RXKADBADTICKET    : s = "RXKADBADTICKET";     break;
1132         case RXKADUNKNOWNKEY   : s = "RXKADUNKNOWNKEY";    break;
1133         case RXKADEXPIRED      : s = "RXKADEXPIRED";       break;
1134         case RXKADSEALEDINCON  : s = "RXKADSEALEDINCON";   break;
1135         case RXKADDATALEN      : s = "RXKADDATALEN";       break;
1136         case RXKADILLEGALLEVEL : s = "RXKADILLEGALLEVEL";  break;
1137         }
1138         osi_Log2(afsd_logp, "cm_Analyze: rxkad error code 0x%x (%s)",
1139                   errorCode, s);
1140
1141         if (serverp) {
1142             reqp->errorServp = serverp;
1143             reqp->tokenError = errorCode;
1144             retry = 1;
1145         }
1146     } else if (errorCode >= ERROR_TABLE_BASE_U && errorCode < ERROR_TABLE_BASE_U + 256) {
1147         /*
1148          * We received a ubik error.  its possible that the server we are
1149          * communicating with has a corrupted database or is partitioned
1150          * from the rest of the servers and another server might be able
1151          * to answer our query.  Therefore, we will retry the request
1152          * and force the use of another server.
1153          */
1154         if (serverp) {
1155             reqp->errorServp = serverp;
1156             reqp->tokenError = errorCode;
1157             retry = 1;
1158         }
1159     } else if (errorCode == VICECONNBAD || errorCode == VICETOKENDEAD) {
1160         cm_ForceNewConnections(serverp);
1161         if ( timeLeft > 2 )
1162             retry = 1;
1163     } else {
1164         if (errorCode) {
1165             char * s = "unknown error";
1166             switch ( errorCode ) {
1167             case VSALVAGE          : s = "VSALVAGE";           break;
1168             case VNOVNODE          : s = "VNOVNODE";           break;
1169             case VNOVOL            : s = "VNOVOL";             break;
1170             case VVOLEXISTS        : s = "VVOLEXISTS";         break;
1171             case VNOSERVICE        : s = "VNOSERVICE";         break;
1172             case VOFFLINE          : s = "VOFFLINE";           break;
1173             case VONLINE           : s = "VONLINE";            break;
1174             case VDISKFULL         : s = "VDISKFULL";          break;
1175             case VOVERQUOTA        : s = "VOVERQUOTA";         break;
1176             case VBUSY             : s = "VBUSY";              break;
1177             case VMOVED            : s = "VMOVED";             break;
1178             case VIO               : s = "VIO";                break;
1179             case VRESTRICTED       : s = "VRESTRICTED";        break;
1180             case VRESTARTING       : s = "VRESTARTING";        break;
1181             case VREADONLY         : s = "VREADONLY";          break;
1182             case EAGAIN            : s = "EAGAIN";             break;
1183             case UAEAGAIN          : s = "UAEAGAIN";           break;
1184             case EINVAL            : s = "EINVAL";             break;
1185             case UAEINVAL          : s = "UAEINVAL";           break;
1186             case EACCES            : s = "EACCES";             break;
1187             case UAEACCES          : s = "UAEACCES";           break;
1188             case ENOENT            : s = "ENOENT";             break;
1189             case UAENOENT          : s = "UAENOENT";           break;
1190             case EEXIST            : s = "EEXIST";             break;
1191             case UAEEXIST          : s = "UAEEXIST";           break;
1192             case VICECONNBAD       : s = "VICECONNBAD";        break;
1193             case VICETOKENDEAD     : s = "VICETOKENDEAD";      break;
1194             case WSAEWOULDBLOCK    : s = "WSAEWOULDBLOCK";     break;
1195             case UAEWOULDBLOCK     : s = "UAEWOULDBLOCK";      break;
1196             case VL_IDEXIST        : s = "VL_IDEXIST";         break;
1197             case VL_IO             : s = "VL_IO";              break;
1198             case VL_NAMEEXIST      : s = "VL_NAMEEXIST";       break;
1199             case VL_CREATEFAIL     : s = "VL_CREATEFAIL";      break;
1200             case VL_NOENT          : s = "VL_NOENT";           break;
1201             case VL_EMPTY          : s = "VL_EMPTY";           break;
1202             case VL_ENTDELETED     : s = "VL_ENTDELETED";      break;
1203             case VL_BADNAME        : s = "VL_BADNAME";         break;
1204             case VL_BADINDEX       : s = "VL_BADINDEX";        break;
1205             case VL_BADVOLTYPE     : s = "VL_BADVOLTYPE";      break;
1206             case VL_BADSERVER      : s = "VL_BADSERVER";       break;
1207             case VL_BADPARTITION   : s = "VL_BADPARTITION";    break;
1208             case VL_REPSFULL       : s = "VL_REPSFULL";        break;
1209             case VL_NOREPSERVER    : s = "VL_NOREPSERVER";     break;
1210             case VL_DUPREPSERVER   : s = "VL_DUPREPSERVER";    break;
1211             case VL_RWNOTFOUND     : s = "VL_RWNOTFOUND";      break;
1212             case VL_BADREFCOUNT    : s = "VL_BADREFCOUNT";     break;
1213             case VL_SIZEEXCEEDED   : s = "VL_SIZEEXCEEDED";    break;
1214             case VL_BADENTRY       : s = "VL_BADENTRY";        break;
1215             case VL_BADVOLIDBUMP   : s = "VL_BADVOLIDBUMP";    break;
1216             case VL_IDALREADYHASHED: s = "VL_IDALREADYHASHED"; break;
1217             case VL_ENTRYLOCKED    : s = "VL_ENTRYLOCKED";     break;
1218             case VL_BADVOLOPER     : s = "VL_BADVOLOPER";      break;
1219             case VL_BADRELLOCKTYPE : s = "VL_BADRELLOCKTYPE";  break;
1220             case VL_RERELEASE      : s = "VL_RERELEASE";       break;
1221             case VL_BADSERVERFLAG  : s = "VL_BADSERVERFLAG";   break;
1222             case VL_PERM           : s = "VL_PERM";            break;
1223             case VL_NOMEM          : s = "VL_NOMEM";           break;
1224             case VL_BADVERSION     : s = "VL_BADVERSION";      break;
1225             case VL_INDEXERANGE    : s = "VL_INDEXERANGE";     break;
1226             case VL_MULTIPADDR     : s = "VL_MULTIPADDR";      break;
1227             case VL_BADMASK        : s = "VL_BADMASK";         break;
1228             case CM_ERROR_NOSUCHCELL        : s = "CM_ERROR_NOSUCHCELL";         break;
1229             case CM_ERROR_NOSUCHVOLUME      : s = "CM_ERROR_NOSUCHVOLUME";       break;
1230             case CM_ERROR_TIMEDOUT          : s = "CM_ERROR_TIMEDOUT";           break;
1231             case CM_ERROR_RETRY             : s = "CM_ERROR_RETRY";              break;
1232             case CM_ERROR_NOACCESS          : s = "CM_ERROR_NOACCESS";           break;
1233             case CM_ERROR_NOSUCHFILE        : s = "CM_ERROR_NOSUCHFILE";         break;
1234             case CM_ERROR_STOPNOW           : s = "CM_ERROR_STOPNOW";            break;
1235             case CM_ERROR_TOOBIG            : s = "CM_ERROR_TOOBIG";             break;
1236             case CM_ERROR_INVAL             : s = "CM_ERROR_INVAL";              break;
1237             case CM_ERROR_BADFD             : s = "CM_ERROR_BADFD";              break;
1238             case CM_ERROR_BADFDOP           : s = "CM_ERROR_BADFDOP";            break;
1239             case CM_ERROR_EXISTS            : s = "CM_ERROR_EXISTS";             break;
1240             case CM_ERROR_CROSSDEVLINK      : s = "CM_ERROR_CROSSDEVLINK";       break;
1241             case CM_ERROR_BADOP             : s = "CM_ERROR_BADOP";              break;
1242             case CM_ERROR_BADPASSWORD       : s = "CM_ERROR_BADPASSWORD";        break;
1243             case CM_ERROR_NOTDIR            : s = "CM_ERROR_NOTDIR";             break;
1244             case CM_ERROR_ISDIR             : s = "CM_ERROR_ISDIR";              break;
1245             case CM_ERROR_READONLY          : s = "CM_ERROR_READONLY";           break;
1246             case CM_ERROR_WOULDBLOCK        : s = "CM_ERROR_WOULDBLOCK";         break;
1247             case CM_ERROR_QUOTA             : s = "CM_ERROR_QUOTA";              break;
1248             case CM_ERROR_SPACE             : s = "CM_ERROR_SPACE";              break;
1249             case CM_ERROR_BADSHARENAME      : s = "CM_ERROR_BADSHARENAME";       break;
1250             case CM_ERROR_BADTID            : s = "CM_ERROR_BADTID";             break;
1251             case CM_ERROR_UNKNOWN           : s = "CM_ERROR_UNKNOWN";            break;
1252             case CM_ERROR_NOMORETOKENS      : s = "CM_ERROR_NOMORETOKENS";       break;
1253             case CM_ERROR_NOTEMPTY          : s = "CM_ERROR_NOTEMPTY";           break;
1254             case CM_ERROR_USESTD            : s = "CM_ERROR_USESTD";             break;
1255             case CM_ERROR_REMOTECONN        : s = "CM_ERROR_REMOTECONN";         break;
1256             case CM_ERROR_ATSYS             : s = "CM_ERROR_ATSYS";              break;
1257             case CM_ERROR_NOSUCHPATH        : s = "CM_ERROR_NOSUCHPATH";         break;
1258             case CM_ERROR_CLOCKSKEW         : s = "CM_ERROR_CLOCKSKEW";          break;
1259             case CM_ERROR_BADSMB            : s = "CM_ERROR_BADSMB";             break;
1260             case CM_ERROR_ALLBUSY           : s = "CM_ERROR_ALLBUSY";            break;
1261             case CM_ERROR_NOFILES           : s = "CM_ERROR_NOFILES";            break;
1262             case CM_ERROR_PARTIALWRITE      : s = "CM_ERROR_PARTIALWRITE";       break;
1263             case CM_ERROR_NOIPC             : s = "CM_ERROR_NOIPC";              break;
1264             case CM_ERROR_BADNTFILENAME     : s = "CM_ERROR_BADNTFILENAME";      break;
1265             case CM_ERROR_BUFFERTOOSMALL    : s = "CM_ERROR_BUFFERTOOSMALL";     break;
1266             case CM_ERROR_RENAME_IDENTICAL  : s = "CM_ERROR_RENAME_IDENTICAL";   break;
1267             case CM_ERROR_ALLOFFLINE        : s = "CM_ERROR_ALLOFFLINE";         break;
1268             case CM_ERROR_AMBIGUOUS_FILENAME: s = "CM_ERROR_AMBIGUOUS_FILENAME"; break;
1269             case CM_ERROR_BADLOGONTYPE      : s = "CM_ERROR_BADLOGONTYPE";       break;
1270             case CM_ERROR_GSSCONTINUE       : s = "CM_ERROR_GSSCONTINUE";        break;
1271             case CM_ERROR_TIDIPC            : s = "CM_ERROR_TIDIPC";             break;
1272             case CM_ERROR_TOO_MANY_SYMLINKS : s = "CM_ERROR_TOO_MANY_SYMLINKS";  break;
1273             case CM_ERROR_PATH_NOT_COVERED  : s = "CM_ERROR_PATH_NOT_COVERED";   break;
1274             case CM_ERROR_LOCK_CONFLICT     : s = "CM_ERROR_LOCK_CONFLICT";      break;
1275             case CM_ERROR_SHARING_VIOLATION : s = "CM_ERROR_SHARING_VIOLATION";  break;
1276             case CM_ERROR_ALLDOWN           : s = "CM_ERROR_ALLDOWN";            break;
1277             case CM_ERROR_TOOFEWBUFS        : s = "CM_ERROR_TOOFEWBUFS";         break;
1278             case CM_ERROR_TOOMANYBUFS       : s = "CM_ERROR_TOOMANYBUFS";        break;
1279             }
1280             osi_Log2(afsd_logp, "cm_Analyze: ignoring error code 0x%x (%s)",
1281                      errorCode, s);
1282             retry = 0;
1283         }
1284     }
1285
1286     /* If not allowed to retry, don't */
1287     if (!forcing_new && (reqp->flags & CM_REQ_NORETRY) &&
1288         (errorCode != RX_MSGSIZE && errorCode != RX_CALL_BUSY))
1289         retry = 0;
1290     else if (retry && dead_session)
1291         retry = 0;
1292
1293     /* drop this on the way out */
1294     if (connp)
1295         cm_PutConn(connp);
1296
1297     /*
1298      * clear the volume updated flag if we succeed.
1299      * this way the flag will not prevent a subsequent volume
1300      * from being updated if necessary.
1301      */
1302     if (errorCode == 0)
1303     {
1304         reqp->flags &= ~CM_REQ_VOLUME_UPDATED;
1305     }
1306
1307     if ( serversp &&
1308          errorCode != VBUSY &&
1309          errorCode != VRESTARTING &&
1310          errorCode != CM_ERROR_ALLBUSY)
1311     {
1312         cm_ResetServerBusyStatus(serversp);
1313     }
1314
1315     /* retry until we fail to find a connection */
1316     return retry;
1317 }
1318
1319 long cm_ConnByMServers(cm_serverRef_t *serversp, afs_uint32 replicated, cm_user_t *usersp,
1320                        cm_req_t *reqp, cm_conn_t **connpp)
1321 {
1322     long code;
1323     cm_serverRef_t *tsrp;
1324     cm_server_t *tsp;
1325     long firstError = 0;
1326     int someBusy = 0, someOffline = 0, allOffline = 1, allBusy = 1, allDown = 1, allDeleted = 1;
1327 #ifdef SET_RX_TIMEOUTS_TO_TIMELEFT
1328     long timeUsed, timeLeft, hardTimeLeft;
1329 #endif
1330     *connpp = NULL;
1331
1332     if (serversp == NULL) {
1333         osi_Log1(afsd_logp, "cm_ConnByMServers returning 0x%x", CM_ERROR_EMPTY);
1334         return CM_ERROR_EMPTY;
1335     }
1336
1337 #ifdef SET_RX_TIMEOUTS_TO_TIMELEFT
1338     timeUsed = (GetTickCount() - reqp->startTime) / 1000;
1339
1340     /* leave 5 seconds margin of safety */
1341     timeLeft =  ConnDeadtimeout - timeUsed - 5;
1342     hardTimeLeft = HardDeadtimeout - timeUsed - 5;
1343 #endif
1344
1345     lock_ObtainRead(&cm_serverLock);
1346     for (tsrp = serversp; tsrp; tsrp=tsrp->next) {
1347         if (tsrp->status == srv_deleted)
1348             continue;
1349
1350         allDeleted = 0;
1351
1352         tsp = tsrp->server;
1353         if (reqp->errorServp) {
1354             /*
1355              * search the list until we find the server
1356              * that failed last time.  When we find it
1357              * clear the error, skip it and try the next one
1358              * in the list.
1359              */
1360             if (tsp == reqp->errorServp)
1361                 reqp->errorServp = NULL;
1362             continue;
1363         }
1364         if (tsp) {
1365             cm_GetServerNoLock(tsp);
1366             lock_ReleaseRead(&cm_serverLock);
1367             if (!(tsp->flags & CM_SERVERFLAG_DOWN)) {
1368                 allDown = 0;
1369                 if (tsrp->status == srv_busy) {
1370                     allOffline = 0;
1371                     someBusy = 1;
1372                 } else if (tsrp->status == srv_offline) {
1373                     allBusy = 0;
1374                     someOffline = 1;
1375                 } else {
1376                     allOffline = 0;
1377                     allBusy = 0;
1378                     code = cm_ConnByServer(tsp, usersp, replicated, connpp);
1379                     if (code == 0) {        /* cm_CBS only returns 0 */
1380                         cm_PutServer(tsp);
1381 #ifdef SET_RX_TIMEOUTS_TO_TIMELEFT
1382                         /* Set RPC timeout */
1383                         if (timeLeft > ConnDeadtimeout)
1384                             timeLeft = ConnDeadtimeout;
1385
1386                         if (hardTimeLeft > HardDeadtimeout)
1387                             hardTimeLeft = HardDeadtimeout;
1388
1389                         lock_ObtainMutex(&(*connpp)->mx);
1390                         rx_SetConnDeadTime((*connpp)->rxconnp, timeLeft);
1391                         rx_SetConnHardDeadTime((*connpp)->rxconnp, (u_short) hardTimeLeft);
1392                         lock_ReleaseMutex(&(*connpp)->mx);
1393 #endif
1394                         return 0;
1395                     }
1396
1397                     /* therefore, this code is never executed */
1398                     if (firstError == 0)
1399                         firstError = code;
1400                 }
1401             }
1402             lock_ObtainRead(&cm_serverLock);
1403             cm_PutServerNoLock(tsp);
1404         }
1405     }
1406     lock_ReleaseRead(&cm_serverLock);
1407
1408     if (firstError == 0) {
1409         if (allDeleted) {
1410             firstError = CM_ERROR_EMPTY;
1411         } else if (allDown) {
1412             firstError = (reqp->tokenError ? reqp->tokenError :
1413                           (reqp->idleError ? RX_CALL_TIMEOUT : CM_ERROR_ALLDOWN));
1414             /*
1415              * if we experienced either a token error or and idle dead time error
1416              * and now all of the servers are down, we have either tried them
1417              * all or lost connectivity.  Clear the error we are returning so
1418              * we will not return it indefinitely if the request is retried.
1419              */
1420             reqp->idleError = reqp->tokenError = 0;
1421         } else if (allBusy) {
1422             firstError = CM_ERROR_ALLBUSY;
1423         } else if (allOffline || (someBusy && someOffline)) {
1424             firstError = CM_ERROR_ALLOFFLINE;
1425         } else {
1426             osi_Log0(afsd_logp, "cm_ConnByMServers returning impossible error TIMEDOUT");
1427             firstError = CM_ERROR_TIMEDOUT;
1428         }
1429     }
1430
1431     osi_Log1(afsd_logp, "cm_ConnByMServers returning 0x%x", firstError);
1432     return firstError;
1433 }
1434
1435 /* called with a held server to GC all bad connections hanging off of the server */
1436 void cm_GCConnections(cm_server_t *serverp)
1437 {
1438     cm_conn_t *tcp;
1439     cm_conn_t **lcpp;
1440     cm_user_t *userp;
1441
1442     lock_ObtainWrite(&cm_connLock);
1443     lcpp = &serverp->connsp;
1444     for (tcp = *lcpp; tcp; tcp = *lcpp) {
1445         userp = tcp->userp;
1446         if (userp && tcp->refCount == 0 && (userp->vcRefs == 0)) {
1447             /* do the deletion of this guy */
1448             cm_PutServer(tcp->serverp);
1449             cm_ReleaseUser(userp);
1450             *lcpp = tcp->nextp;
1451             rx_SetConnSecondsUntilNatPing(tcp->rxconnp, 0);
1452             rx_DestroyConnection(tcp->rxconnp);
1453             lock_FinalizeMutex(&tcp->mx);
1454             free(tcp);
1455         }
1456         else {
1457             /* just advance to the next */
1458             lcpp = &tcp->nextp;
1459         }
1460     }
1461     lock_ReleaseWrite(&cm_connLock);
1462 }
1463
1464 static void cm_NewRXConnection(cm_conn_t *tcp, cm_ucell_t *ucellp,
1465                                cm_server_t *serverp, afs_uint32 replicated)
1466 {
1467     unsigned short port;
1468     int serviceID;
1469     int secIndex;
1470     struct rx_securityClass *secObjp;
1471
1472     port = serverp->addr.sin_port;
1473     switch (serverp->type) {
1474     case CM_SERVER_VLDB:
1475         if (port == 0)
1476             port = htons(7003);
1477         serviceID = 52;
1478         break;
1479     case CM_SERVER_FILE:
1480         if (port == 0)
1481             port = htons(7000);
1482         serviceID = 1;
1483         break;
1484     default:
1485         osi_panic("unknown server type", __FILE__, __LINE__);
1486     }
1487
1488     if (ucellp->flags & CM_UCELLFLAG_RXKAD) {
1489         secIndex = 2;
1490         switch (cryptall) {
1491         case 0:
1492             tcp->cryptlevel = rxkad_clear;
1493             break;
1494         case 2:
1495             tcp->cryptlevel = rxkad_auth;
1496             break;
1497         default:
1498             tcp->cryptlevel = rxkad_crypt;
1499         }
1500         secObjp = rxkad_NewClientSecurityObject(tcp->cryptlevel,
1501                                                 &ucellp->sessionKey, ucellp->kvno,
1502                                                 ucellp->ticketLen, ucellp->ticketp);
1503     } else {
1504         /* normal auth */
1505         secIndex = 0;
1506         tcp->cryptlevel = rxkad_clear;
1507         secObjp = rxnull_NewClientSecurityObject();
1508     }
1509     osi_assertx(secObjp != NULL, "null rx_securityClass");
1510     tcp->rxconnp = rx_NewConnection(serverp->addr.sin_addr.s_addr,
1511                                     port,
1512                                     serviceID,
1513                                     secObjp,
1514                                     secIndex);
1515     rx_SetConnDeadTime(tcp->rxconnp, ConnDeadtimeout);
1516     rx_SetConnHardDeadTime(tcp->rxconnp, HardDeadtimeout);
1517
1518     /*
1519      * Setting idle dead timeout to a non-zero value activates RX_CALL_IDLE errors
1520      */
1521     if (replicated) {
1522         tcp->flags &= CM_CONN_FLAG_REPLICATION;
1523         rx_SetConnIdleDeadTime(tcp->rxconnp, ReplicaIdleDeadtimeout);
1524     } else {
1525         rx_SetConnIdleDeadTime(tcp->rxconnp, IdleDeadtimeout);
1526     }
1527
1528     /*
1529      * Let the Rx library know that we can auto-retry if an
1530      * RX_MSGSIZE error is returned.
1531      */
1532     if (rx_pmtu_discovery)
1533         rx_SetMsgsizeRetryErr(tcp->rxconnp, RX_MSGSIZE);
1534
1535     /*
1536      * Attempt to limit NAT pings to the anonymous file server connections.
1537      * Only file servers implement client callbacks and we only need one ping
1538      * to be sent to each server.
1539      */
1540     if (NatPingInterval && serverp->type == CM_SERVER_FILE &&
1541          (ucellp->flags & CM_UCELLFLAG_ROOTUSER)) {
1542         rx_SetConnSecondsUntilNatPing(tcp->rxconnp, NatPingInterval);
1543     }
1544
1545     tcp->ucgen = ucellp->gen;
1546     if (secObjp)
1547         rxs_Release(secObjp);   /* Decrement the initial refCount */
1548 }
1549
1550 long cm_ConnByServer(cm_server_t *serverp, cm_user_t *userp, afs_uint32 replicated, cm_conn_t **connpp)
1551 {
1552     cm_conn_t *tcp;
1553     cm_ucell_t *ucellp;
1554
1555     *connpp = NULL;
1556
1557     if (cm_anonvldb && serverp->type == CM_SERVER_VLDB)
1558         userp = cm_rootUserp;
1559
1560     lock_ObtainMutex(&userp->mx);
1561     lock_ObtainRead(&cm_connLock);
1562     for (tcp = serverp->connsp; tcp; tcp=tcp->nextp) {
1563         if (tcp->userp == userp &&
1564             (replicated && (tcp->flags & CM_CONN_FLAG_REPLICATION) ||
1565              !replicated && !(tcp->flags & CM_CONN_FLAG_REPLICATION)))
1566             break;
1567     }
1568
1569     /* find ucell structure */
1570     ucellp = cm_GetUCell(userp, serverp->cellp);
1571     if (!tcp) {
1572         lock_ConvertRToW(&cm_connLock);
1573         for (tcp = serverp->connsp; tcp; tcp=tcp->nextp) {
1574             if (tcp->userp == userp)
1575                 break;
1576         }
1577         if (tcp) {
1578             lock_ReleaseWrite(&cm_connLock);
1579             goto haveconn;
1580         }
1581         cm_GetServer(serverp);
1582         tcp = malloc(sizeof(*tcp));
1583         memset(tcp, 0, sizeof(*tcp));
1584         cm_HoldUser(userp);
1585         tcp->userp = userp;
1586         lock_InitializeMutex(&tcp->mx, "cm_conn_t mutex", LOCK_HIERARCHY_CONN);
1587         tcp->serverp = serverp;
1588         tcp->cryptlevel = rxkad_clear;
1589         cm_NewRXConnection(tcp, ucellp, serverp, replicated);
1590         tcp->refCount = 1;
1591         tcp->nextp = serverp->connsp;
1592         serverp->connsp = tcp;
1593         lock_ReleaseWrite(&cm_connLock);
1594         lock_ReleaseMutex(&userp->mx);
1595     } else {
1596         lock_ReleaseRead(&cm_connLock);
1597       haveconn:
1598         lock_ReleaseMutex(&userp->mx);
1599         InterlockedIncrement(&tcp->refCount);
1600
1601         lock_ObtainMutex(&tcp->mx);
1602         if ((tcp->flags & CM_CONN_FLAG_FORCE_NEW) ||
1603             (tcp->ucgen < ucellp->gen) ||
1604             (tcp->cryptlevel != (ucellp->flags & CM_UCELLFLAG_RXKAD ? (cryptall == 1 ? rxkad_crypt : (cryptall == 2 ? rxkad_auth : rxkad_clear)) : rxkad_clear)))
1605         {
1606             if (tcp->ucgen < ucellp->gen)
1607                 osi_Log0(afsd_logp, "cm_ConnByServer replace connection due to token update");
1608             else
1609                 osi_Log0(afsd_logp, "cm_ConnByServer replace connection due to crypt change");
1610             tcp->flags &= ~CM_CONN_FLAG_FORCE_NEW;
1611             rx_SetConnSecondsUntilNatPing(tcp->rxconnp, 0);
1612             rx_DestroyConnection(tcp->rxconnp);
1613             cm_NewRXConnection(tcp, ucellp, serverp, replicated);
1614         }
1615         lock_ReleaseMutex(&tcp->mx);
1616     }
1617
1618     /* return this pointer to our caller */
1619     osi_Log1(afsd_logp, "cm_ConnByServer returning conn 0x%p", tcp);
1620     *connpp = tcp;
1621
1622     return 0;
1623 }
1624
1625 long cm_ServerAvailable(struct cm_fid *fidp, struct cm_user *userp)
1626 {
1627     long code;
1628     cm_req_t req;
1629     cm_serverRef_t **serverspp;
1630     cm_serverRef_t *tsrp;
1631     cm_server_t *tsp;
1632     int someBusy = 0, someOffline = 0, allOffline = 1, allBusy = 1, allDown = 1;
1633     afs_uint32 replicated;
1634
1635     cm_InitReq(&req);
1636
1637     code = cm_GetServerList(fidp, userp, &req, &replicated, &serverspp);
1638     if (code)
1639         return 0;
1640
1641     lock_ObtainRead(&cm_serverLock);
1642     for (tsrp = *serverspp; tsrp; tsrp=tsrp->next) {
1643         if (tsrp->status == srv_deleted)
1644             continue;
1645         tsp = tsrp->server;
1646         if (!(tsp->flags & CM_SERVERFLAG_DOWN)) {
1647             allDown = 0;
1648             if (tsrp->status == srv_busy) {
1649                 allOffline = 0;
1650                 someBusy = 1;
1651             } else if (tsrp->status == srv_offline) {
1652                 allBusy = 0;
1653                 someOffline = 1;
1654             } else {
1655                 allOffline = 0;
1656                 allBusy = 0;
1657             }
1658         }
1659     }
1660     lock_ReleaseRead(&cm_serverLock);
1661     cm_FreeServerList(serverspp, 0);
1662
1663     if (allDown)
1664         return 0;
1665     else if (allBusy)
1666         return 0;
1667     else if (allOffline || (someBusy && someOffline))
1668         return 0;
1669     else
1670         return 1;
1671 }
1672
1673 /*
1674  * The returned cm_conn_t ** object is released in the subsequent call
1675  * to cm_Analyze().
1676  */
1677 long cm_ConnFromFID(struct cm_fid *fidp, struct cm_user *userp, cm_req_t *reqp,
1678                     cm_conn_t **connpp)
1679 {
1680     long code;
1681     cm_serverRef_t **serverspp;
1682     afs_uint32 replicated;
1683
1684     *connpp = NULL;
1685
1686     code = cm_GetServerList(fidp, userp, reqp, &replicated, &serverspp);
1687     if (code)
1688         return code;
1689
1690     code = cm_ConnByMServers(*serverspp, replicated, userp, reqp, connpp);
1691     cm_FreeServerList(serverspp, 0);
1692     return code;
1693 }
1694
1695
1696 long cm_ConnFromVolume(struct cm_volume *volp, unsigned long volid, struct cm_user *userp, cm_req_t *reqp,
1697                        cm_conn_t **connpp)
1698 {
1699     long code;
1700     cm_serverRef_t **serverspp;
1701     afs_uint32 replicated;
1702
1703     *connpp = NULL;
1704
1705     serverspp = cm_GetVolServers(volp, volid, userp, reqp, &replicated);
1706
1707     code = cm_ConnByMServers(*serverspp, replicated, userp, reqp, connpp);
1708     cm_FreeServerList(serverspp, 0);
1709     return code;
1710 }
1711
1712
1713 extern struct rx_connection *
1714 cm_GetRxConn(cm_conn_t *connp)
1715 {
1716     struct rx_connection * rxconnp;
1717     lock_ObtainMutex(&connp->mx);
1718     rxconnp = connp->rxconnp;
1719     rx_GetConnection(rxconnp);
1720     lock_ReleaseMutex(&connp->mx);
1721     return rxconnp;
1722 }
1723
1724 void cm_ForceNewConnections(cm_server_t *serverp)
1725 {
1726     cm_conn_t *tcp;
1727
1728     lock_ObtainWrite(&cm_connLock);
1729     for (tcp = serverp->connsp; tcp; tcp=tcp->nextp) {
1730         lock_ObtainMutex(&tcp->mx);
1731         tcp->flags |= CM_CONN_FLAG_FORCE_NEW;
1732         lock_ReleaseMutex(&tcp->mx);
1733     }
1734     lock_ReleaseWrite(&cm_connLock);
1735 }