2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
13 #include <afsconfig.h>
14 #include "afs/param.h"
19 #include "afs/sysincludes.h" /* Standard vendor system headers */
23 #include <netinet/in.h>
26 #include "h/hashing.h"
28 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
29 #include <netinet/in_var.h>
30 #endif /* ! AFS_HPUX110_ENV */
31 #endif /* !defined(UKERNEL) */
33 #include "afsincludes.h" /* Afs-based standard headers */
34 #include "afs/afs_stats.h" /* afs statistics */
36 #if defined(AFS_SUN56_ENV)
38 #include <inet/common.h>
39 #if defined(AFS_SUN58_ENV)
40 #include <netinet/ip6.h>
45 /* Exported variables */
46 afs_rwlock_t afs_xconn; /* allocation lock for new things */
47 afs_rwlock_t afs_xinterface; /* for multiple client address */
48 afs_int32 cryptall = 0; /* encrypt all communications */
51 unsigned int VNOSERVERS = 0;
52 struct conn *afs_Conn(register struct VenusFid *afid,
53 register struct vrequest *areq, afs_int32 locktype)
55 u_short fsport=AFS_FSPORT;
57 struct conn *tconn = NULL;
58 struct srvAddr *lowp= NULL;
64 AFS_STATCNT(afs_Conn);
65 tv = afs_GetVolume(afid, areq, READ_LOCK);
68 afs_FinalizeReq(areq);
69 areq->volumeError = 1;
74 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
75 fsport = tv->serverHost[0]->cell->fsport;
81 /* First is always lowest rank, if it's up */
82 if ((tv->status[0] == not_busy) && tv->serverHost[0] &&
83 !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN))
84 lowp = tv->serverHost[0]->addr;
86 /* Otherwise we look at all of them. There are seven levels of
87 * not_busy. This means we will check a volume seven times before it
88 * is marked offline. Ideally, we only need two levels, but this
89 * serves a second purpose of waiting some number of seconds before
90 * the client decides the volume is offline (ie: a clone could finish
93 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
94 for (i=0 ;i<MAXHOSTS && tv->serverHost[i];i++) {
95 if (tv->status[i] != notbusy) {
96 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
99 } else if (tv->status[i] == offline) {
100 if (!areq->volumeError)
101 areq->volumeError = VOLMISSING;
105 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
106 if (sa1p->sa_flags & SRVR_ISDOWN)
108 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
113 afs_PutVolume(tv, READ_LOCK);
116 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
117 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu,
118 0/*!force*/, 1/*create*/, locktype);
120 afs_PutUser(tu, SHARED_LOCK);
127 struct conn *afs_ConnBySA(struct srvAddr *sap, unsigned short aport,
128 afs_int32 acell, struct unixuser *tu,
129 int force_if_down, afs_int32 create, afs_int32 locktype)
132 struct rx_securityClass *csec; /*Security class object*/
133 int isec; /*Security index*/
136 if (!sap || ((sap->sa_flags & SRVR_ISDOWN) && !force_if_down)) {
137 /* sa is known down, and we don't want to force it. */
141 ObtainSharedLock(&afs_xconn,15);
142 for (tc = sap->conns; tc; tc=tc->next) {
143 if (tc->user == tu && tc->port == aport) {
148 if (!tc && !create) {
149 ReleaseSharedLock(&afs_xconn);
154 /* No such connection structure exists. Create one and splice it in.
155 * Make sure the server record has been marked as used (for the purposes
156 * of calculating up & down times, it's now considered to be an
157 * ``active'' server). Also make sure the server's lastUpdateEvalTime
158 * gets set, marking the time of its ``birth''.
160 UpgradeSToWLock(&afs_xconn,37);
161 tc = (struct conn *) afs_osi_Alloc(sizeof(struct conn));
162 memset((char *)tc, 0, sizeof(struct conn));
167 tc->refCount = 0; /* bumped below */
168 tc->forceConnectFS = 1;
169 tc->id = (struct rx_connection *) 0;
170 tc->next = sap->conns;
172 afs_ActivateServer(sap);
174 ConvertWToSLock(&afs_xconn);
178 if (tu->states & UTokensBad) {
179 /* we may still have an authenticated RPC connection here,
180 * we'll have to create a new, unauthenticated, connection.
181 * Perhaps a better way to do this would be to set
182 * conn->forceConnectFS on all conns when the token first goes
183 * bad, but that's somewhat trickier, due to locking
184 * constraints (though not impossible).
186 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
187 tc->forceConnectFS = 1; /* force recreation of connection */
189 tu->vid = UNDEFVID; /* forcibly disconnect the authentication info */
192 if (tc->forceConnectFS) {
193 UpgradeSToWLock(&afs_xconn,38);
194 csec = (struct rx_securityClass *) 0;
197 rx_DestroyConnection(tc->id);
201 * Stupid hack to determine if using vldb service or file system
204 if (aport == sap->server->cell->vlport)
209 if (tu->vid != UNDEFVID) {
218 /* kerberos tickets on channel 2 */
219 csec = rxkad_NewClientSecurityObject(level, tu->ct.HandShakeKey,
225 csec = rxnull_NewClientSecurityObject();
227 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
230 rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
233 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
236 ConvertWToSLock(&afs_xconn);
239 ReleaseSharedLock(&afs_xconn);
245 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
246 * is true only if we know that the tokens are bad. We thus clear this flag
247 * when we get a new set of tokens..
248 * Having force... true and UTokensBad true simultaneously means that the tokens
249 * went bad and we're supposed to create a new, unauthenticated, connection.
251 struct conn *afs_ConnByHost(struct server *aserver, unsigned short aport,
252 afs_int32 acell, struct vrequest *areq, int aforce, afs_int32 locktype)
256 struct srvAddr *sa=0;
258 AFS_STATCNT(afs_ConnByHost);
260 1. look for an existing connection
261 2. create a connection at an address believed to be up
262 (if aforce is true, create a connection at the first address)
265 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
267 for (sa = aserver->addr; sa; sa = sa->next_sa) {
268 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
269 0/*don't create one*/,
276 for (sa = aserver->addr; sa; sa = sa->next_sa) {
277 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
285 afs_PutUser(tu, SHARED_LOCK);
291 struct conn *afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
292 afs_int32 acell, register struct vrequest *areq, afs_int32 locktype)
294 register afs_int32 i;
295 register struct conn *tconn;
296 register struct server *ts;
298 /* try to find any connection from the set */
299 AFS_STATCNT(afs_ConnByMHosts);
300 for (i=0;i<MAXCELLHOSTS;i++) {
301 if ((ts = ahosts[i]) == NULL) break;
302 tconn = afs_ConnByHost(ts, aport, acell,
310 } /*afs_ConnByMHosts*/
313 void afs_PutConn(register struct conn *ac, afs_int32 locktype)
315 AFS_STATCNT(afs_PutConn);
320 /* for multi homed clients, an RPC may timeout because of a
321 client network interface going down. We need to reopen new
322 connections in this case
324 void ForceNewConnections(struct srvAddr *sap)
328 if ( !sap) return; /* defensive check */
330 /* if client is not multihomed, do nothing */
331 ObtainReadLock(&afs_xinterface);
332 if ( afs_cb_interface.numberOfInterfaces <= 1 )
334 ReleaseReadLock(&afs_xinterface);
337 ReleaseReadLock(&afs_xinterface);
339 ObtainWriteLock(&afs_xconn,413);
340 for (tc = sap->conns; tc; tc=tc->next)
341 tc->forceConnectFS=1;
342 ReleaseWriteLock(&afs_xconn);