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