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 "../afs/param.h" /* Should be always first */
14 #include "../afs/stds.h"
15 #include "../afs/sysincludes.h" /* Standard vendor system headers */
19 #include <netinet/in.h>
22 #include "../h/hashing.h"
24 #if !defined(AFS_HPUX110_ENV) && !defined(AFS_LINUX20_ENV)
25 #include <netinet/in_var.h>
26 #endif /* ! AFS_HPUX110_ENV */
27 #endif /* !defined(UKERNEL) */
29 #include "../afs/afsincludes.h" /* Afs-based standard headers */
30 #include "../afs/afs_stats.h" /* afs statistics */
32 #if defined(AFS_SUN56_ENV)
34 #include <inet/common.h>
38 /* Imported variables */
39 /* these are for storing alternate interface addresses - from afs_callback.c */
40 extern struct interfaceAddr afs_cb_interface;
43 /* Imported functions. */
44 struct rx_securityClass *rxnull_NewClientSecurityObject();
45 struct rx_securityClass *rxkad_NewClientSecurityObject();
47 /* Exported variables */
48 afs_rwlock_t afs_xconn; /* allocation lock for new things */
49 afs_rwlock_t afs_xinterface; /* for multiple client address */
52 afs_int32 cryptall = 0;
55 unsigned int VNOSERVERS = 0;
56 struct conn *afs_Conn(afid, areq, locktype)
57 register struct VenusFid *afid;
58 register struct vrequest *areq;
61 u_short fsport=AFS_FSPORT;
63 struct conn *tconn = (struct conn *)0;
64 struct srvAddr *lowp= (struct srvAddr *)0;
70 AFS_STATCNT(afs_Conn);
71 tv = afs_GetVolume(afid, areq, READ_LOCK);
74 afs_FinalizeReq(areq);
75 areq->volumeError = 1;
77 return (struct conn *) 0;
80 if (tv->serverHost[0] && tv->serverHost[0]->cell) {
81 fsport = tv->serverHost[0]->cell->fsport;
87 /* First is always lowest rank, if it's up */
88 if ((tv->status[0] == not_busy) && tv->serverHost[0] &&
89 !(tv->serverHost[0]->addr->sa_flags & SRVR_ISDOWN))
90 lowp = tv->serverHost[0]->addr;
92 /* Otherwise we look at all of them. There are seven levels of
93 * not_busy. This means we will check a volume seven times before it
94 * is marked offline. Ideally, we only need two levels, but this
95 * serves a second purpose of waiting some number of seconds before
96 * the client decides the volume is offline (ie: a clone could finish
99 for (notbusy = not_busy; (!lowp && (notbusy <= end_not_busy)); notbusy++) {
100 for (i=0 ;i<MAXHOSTS && tv->serverHost[i];i++) {
101 if (tv->status[i] != notbusy) {
102 if (tv->status[i] == rd_busy || tv->status[i] == rdwr_busy) {
103 if (!areq->busyCount)
105 } else if (tv->status[i] == offline) {
106 if (!areq->volumeError)
107 areq->volumeError = VOLMISSING;
111 for (sa1p = tv->serverHost[i]->addr; sa1p; sa1p = sa1p->next_sa) {
112 if (sa1p->sa_flags & SRVR_ISDOWN)
114 if (!lowp || (lowp->sa_iprank > sa1p->sa_iprank))
119 afs_PutVolume(tv, READ_LOCK);
122 tu = afs_GetUser(areq->uid, afid->Cell, SHARED_LOCK);
123 tconn = afs_ConnBySA(lowp, fsport, afid->Cell, tu,
124 0/*!force*/, 1/*create*/, locktype);
126 afs_PutUser(tu, SHARED_LOCK);
133 struct conn *afs_ConnBySA(struct srvAddr *sap, unsigned short aport,
134 afs_int32 acell, struct unixuser *tu,
135 int force_if_down, afs_int32 create, afs_int32 locktype)
138 struct rx_securityClass *csec; /*Security class object*/
139 int isec; /*Security index*/
142 if (!sap || ((sap->sa_flags & SRVR_ISDOWN) && !force_if_down)) {
143 /* sa is known down, and we don't want to force it. */
144 return (struct conn *)0;
147 ObtainSharedLock(&afs_xconn,15);
148 for (tc = sap->conns; tc; tc=tc->next) {
149 if (tc->user == tu && tc->port == aport) {
154 if (!tc && !create) {
155 ReleaseSharedLock(&afs_xconn);
156 return (struct conn *)0;
160 /* No such connection structure exists. Create one and splice it in.
161 * Make sure the server record has been marked as used (for the purposes
162 * of calculating up & down times, it's now considered to be an
163 * ``active'' server). Also make sure the server's lastUpdateEvalTime
164 * gets set, marking the time of its ``birth''.
166 UpgradeSToWLock(&afs_xconn,37);
167 tc = (struct conn *) afs_osi_Alloc(sizeof(struct conn));
168 bzero((char *)tc, sizeof(struct conn));
173 tc->refCount = 0; /* bumped below */
174 tc->forceConnectFS = 1;
175 tc->id = (struct rx_connection *) 0;
176 tc->next = sap->conns;
178 afs_ActivateServer(sap);
180 ConvertWToSLock(&afs_xconn);
184 if (tu->states & UTokensBad) {
185 /* we may still have an authenticated RPC connection here,
186 * we'll have to create a new, unauthenticated, connection.
187 * Perhaps a better way to do this would be to set
188 * conn->forceConnectFS on all conns when the token first goes
189 * bad, but that's somewhat trickier, due to locking
190 * constraints (though not impossible).
192 if (tc->id && (rx_SecurityClassOf(tc->id) != 0)) {
193 tc->forceConnectFS = 1; /* force recreation of connection */
195 tu->vid = UNDEFVID; /* forcibly disconnect the authentication info */
198 if (tc->forceConnectFS) {
199 UpgradeSToWLock(&afs_xconn,38);
200 csec = (struct rx_securityClass *) 0;
203 rx_DestroyConnection(tc->id);
207 * Stupid hack to determine if using vldb service or file system
210 if (aport == sap->server->cell->vlport)
215 if (tu->vid != UNDEFVID) {
226 /* kerberos tickets on channel 2 */
227 csec = rxkad_NewClientSecurityObject(level, tu->ct.HandShakeKey,
228 /* kvno */ tu->ct.AuthHandle, tu->stLen, tu->stp);
231 csec = rxnull_NewClientSecurityObject();
233 tc->id = rx_NewConnection(sap->sa_ip, aport, service, csec, isec);
236 rx_SetConnHardDeadTime(tc->id, AFS_HARDDEADTIME);
240 tc->forceConnectFS = 0; /* apparently we're appropriately connected now */
243 ConvertWToSLock(&afs_xconn);
246 ReleaseSharedLock(&afs_xconn);
252 * forceConnectFS is set whenever we must recompute the connection. UTokensBad
253 * is true only if we know that the tokens are bad. We thus clear this flag
254 * when we get a new set of tokens..
255 * Having force... true and UTokensBad true simultaneously means that the tokens
256 * went bad and we're supposed to create a new, unauthenticated, connection.
258 struct conn *afs_ConnByHost(aserver, aport, acell, areq, aforce, locktype)
259 struct server *aserver;
261 unsigned short aport;
262 struct vrequest *areq;
269 struct srvAddr *sa=0;
271 AFS_STATCNT(afs_ConnByHost);
273 1. look for an existing connection
274 2. create a connection at an address believed to be up
275 (if aforce is true, create a connection at the first address)
278 tu = afs_GetUser(areq->uid, acell, SHARED_LOCK);
280 for (sa = aserver->addr; sa; sa = sa->next_sa) {
281 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
282 0/*don't create one*/,
289 for (sa = aserver->addr; sa; sa = sa->next_sa) {
290 tc = afs_ConnBySA(sa, aport, acell, tu, aforce,
298 afs_PutUser(tu, SHARED_LOCK);
304 struct conn *afs_ConnByMHosts(ahosts, aport, acell, areq, locktype)
305 struct server *ahosts[];
307 unsigned short aport;
308 register struct vrequest *areq;
311 register afs_int32 i;
312 register struct conn *tconn;
313 register struct server *ts;
315 /* try to find any connection from the set */
316 AFS_STATCNT(afs_ConnByMHosts);
317 for (i=0;i<MAXCELLHOSTS;i++) {
318 if ((ts = ahosts[i]) == (struct server *) 0) break;
319 tconn = afs_ConnByHost(ts, aport, acell,
325 return (struct conn *) 0;
327 } /*afs_ConnByMHosts*/
330 void afs_PutConn(ac, locktype)
331 register struct conn *ac;
334 AFS_STATCNT(afs_PutConn);
339 /* for multi homed clients, an RPC may timeout because of a
340 client network interface going down. We need to reopen new
341 connections in this case
343 ForceNewConnections(sap)
348 if ( !sap) return; /* defensive check */
350 /* if client is not multihomed, do nothing */
351 ObtainReadLock(&afs_xinterface);
352 if ( afs_cb_interface.numberOfInterfaces <= 1 )
354 ReleaseReadLock(&afs_xinterface);
357 ReleaseReadLock(&afs_xinterface);
359 ObtainWriteLock(&afs_xconn,413);
360 for (tc = sap->conns; tc; tc=tc->next)
361 tc->forceConnectFS=1;
362 ReleaseWriteLock(&afs_xconn);