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"
20 #include "afs/sysincludes.h" /* Standard vendor system headers */
23 #if !defined(AFS_LINUX20_ENV)
26 #include <netinet/in.h>
29 #include "h/hashing.h"
31 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN60_ENV)
32 #include <netinet/in_var.h>
33 #endif /* ! AFS_HPUX110_ENV */
34 #endif /* !defined(UKERNEL) */
36 #include "afsincludes.h" /* Afs-based standard headers */
37 #include "afs/afs_stats.h" /* afs statistics */
39 #if defined(AFS_SUN56_ENV)
41 #include <inet/common.h>
42 #if defined(AFS_SUN58_ENV)
43 #include <netinet/ip6.h>
48 /* Exported variables */
49 afs_rwlock_t afs_xconn; /* allocation lock for new things */
50 afs_rwlock_t afs_xinterface; /* for multiple client address */
51 afs_int32 cryptall = 0; /* encrypt all communications */
54 unsigned int VNOSERVERS = 0;
56 afs_Conn(register struct VenusFid *afid, register struct vrequest *areq,
59 u_short fsport = AFS_FSPORT;
61 struct conn *tconn = NULL;
62 struct srvAddr *lowp = NULL;
68 AFS_STATCNT(afs_Conn);
69 tv = afs_GetVolume(afid, areq, READ_LOCK);
72 afs_FinalizeReq(areq);
73 areq->volumeError = 1;
78 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
79 fsport = tv->serverHost[0]->cell->fsport;
84 /* First is always lowest rank, if it's up */
85 if ((tv->status[0] == not_busy) && tv->serverHost[0]
86 && !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN))
87 lowp = tv->serverHost[0]->addr;
89 /* Otherwise we look at all of them. There are seven levels of
90 * not_busy. This means we will check a volume seven times before it
91 * is marked offline. Ideally, we only need two levels, but this
92 * serves a second purpose of waiting some number of seconds before
93 * the client decides the volume is offline (ie: a clone could finish
96 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
97 for (i = 0; i < MAXHOSTS && tv->serverHost[i]; i++) {
98 if (tv->status[i] != notbusy) {
99 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
100 if (!areq->busyCount)
102 } else if (tv->status[i] == offline) {
103 if (!areq->volumeError)
104 areq->volumeError = VOLMISSING;
108 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
109 if (sa1p->sa_flags & SRVR_ISDOWN)
111 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
116 afs_PutVolume(tv, READ_LOCK);
119 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
120 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu, 0 /*!force */ ,
121 1 /*create */ , locktype);
123 afs_PutUser(tu, SHARED_LOCK);
131 afs_ConnBySA(struct srvAddr *sap, unsigned short aport, afs_int32 acell,
132 struct unixuser *tu, int force_if_down, afs_int32 create,
136 struct rx_securityClass *csec; /*Security class object */
137 int isec; /*Security index */
140 if (!sap || ((sap->sa_flags & SRVR_ISDOWN) && !force_if_down)) {
141 /* sa is known down, and we don't want to force it. */
145 ObtainSharedLock(&afs_xconn, 15);
146 for (tc = sap->conns; tc; tc = tc->next) {
147 if (tc->user == tu && tc->port == aport) {
152 if (!tc && !create) {
153 ReleaseSharedLock(&afs_xconn);
158 /* No such connection structure exists. Create one and splice it in.
159 * Make sure the server record has been marked as used (for the purposes
160 * of calculating up & down times, it's now considered to be an
161 * ``active'' server). Also make sure the server's lastUpdateEvalTime
162 * gets set, marking the time of its ``birth''.
164 UpgradeSToWLock(&afs_xconn, 37);
165 tc = (struct conn *)afs_osi_Alloc(sizeof(struct conn));
166 memset((char *)tc, 0, sizeof(struct conn));
171 tc->refCount = 0; /* bumped below */
172 tc->forceConnectFS = 1;
173 tc->id = (struct rx_connection *)0;
174 tc->next = sap->conns;
176 afs_ActivateServer(sap);
178 ConvertWToSLock(&afs_xconn);
182 if (tu->states & UTokensBad) {
183 /* we may still have an authenticated RPC connection here,
184 * we'll have to create a new, unauthenticated, connection.
185 * Perhaps a better way to do this would be to set
186 * conn->forceConnectFS on all conns when the token first goes
187 * bad, but that's somewhat trickier, due to locking
188 * constraints (though not impossible).
190 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
191 tc->forceConnectFS = 1; /* force recreation of connection */
193 tu->vid = UNDEFVID; /* forcibly disconnect the authentication info */
196 if (tc->forceConnectFS) {
197 UpgradeSToWLock(&afs_xconn, 38);
198 csec = (struct rx_securityClass *)0;
201 rx_DestroyConnection(tc->id);
205 * Stupid hack to determine if using vldb service or file system
208 if (aport == sap->server->cell->vlport)
213 if (tu->vid != UNDEFVID) {
222 /* kerberos tickets on channel 2 */
223 csec = rxkad_NewClientSecurityObject(level,
224 (struct ktc_encryptionKey *)tu->ct.HandShakeKey,
226 tu->ct.AuthHandle, tu->stLen,
230 csec = rxnull_NewClientSecurityObject();
232 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
235 rx_SetConnHardDeadTime(tc->id, afs_rx_harddead);
238 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
241 ConvertWToSLock(&afs_xconn);
244 ReleaseSharedLock(&afs_xconn);
251 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
252 * is true only if we know that the tokens are bad. We thus clear this flag
253 * when we get a new set of tokens..
254 * Having force... true and UTokensBad true simultaneously means that the tokens
255 * went bad and we're supposed to create a new, unauthenticated, connection.
258 afs_ConnByHost(struct server *aserver, unsigned short aport, afs_int32 acell,
259 struct vrequest *areq, int aforce, afs_int32 locktype)
263 struct srvAddr *sa = 0;
265 AFS_STATCNT(afs_ConnByHost);
267 1. look for an existing connection
268 2. create a connection at an address believed to be up
269 (if aforce is true, create a connection at the first address)
272 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
274 for (sa = aserver->addr; sa; sa = sa->next_sa) {
275 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
276 0 /*don't create one */ ,
283 for (sa = aserver->addr; sa; sa = sa->next_sa) {
284 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
292 afs_PutUser(tu, SHARED_LOCK);
295 } /*afs_ConnByHost */
299 afs_ConnByMHosts(struct server *ahosts[], unsigned short aport,
300 afs_int32 acell, register struct vrequest *areq,
303 register afs_int32 i;
304 register struct conn *tconn;
305 register struct server *ts;
307 /* try to find any connection from the set */
308 AFS_STATCNT(afs_ConnByMHosts);
309 for (i = 0; i < MAXCELLHOSTS; i++) {
310 if ((ts = ahosts[i]) == NULL)
312 tconn = afs_ConnByHost(ts, aport, acell, areq, 0, locktype);
319 } /*afs_ConnByMHosts */
323 afs_PutConn(register struct conn *ac, afs_int32 locktype)
325 AFS_STATCNT(afs_PutConn);
330 /* for multi homed clients, an RPC may timeout because of a
331 client network interface going down. We need to reopen new
332 connections in this case
335 ForceNewConnections(struct srvAddr *sap)
340 return; /* defensive check */
342 /* if client is not multihomed, do nothing */
343 ObtainReadLock(&afs_xinterface);
344 if (afs_cb_interface.numberOfInterfaces <= 1) {
345 ReleaseReadLock(&afs_xinterface);
348 ReleaseReadLock(&afs_xinterface);
350 ObtainWriteLock(&afs_xconn, 413);
351 for (tc = sap->conns; tc; tc = tc->next)
352 tc->forceConnectFS = 1;
353 ReleaseWriteLock(&afs_xconn);