afs: Drop GLOCK for RXAFS_GetCapabilities
[openafs.git] / src / afs / afs_server.c
index 76be5a1..22d9bfd 100644 (file)
@@ -230,7 +230,7 @@ afs_MarkServerUpOrDown(struct srvAddr *sa, int a_isDown)
 
 
 afs_int32
-afs_ServerDown(struct srvAddr *sa, int code)
+afs_ServerDown(struct srvAddr *sa, int code, struct rx_connection *rxconn)
 {
     struct server *aserver = sa->server;
 
@@ -239,11 +239,11 @@ afs_ServerDown(struct srvAddr *sa, int code)
        return 0;
     afs_MarkServerUpOrDown(sa, SRVR_ISDOWN);
     if (sa->sa_portal == aserver->cell->vlport)
-       print_internet_address
-           ("afs: Lost contact with volume location server ", sa, "", 1, code);
+       print_internet_address("afs: Lost contact with volume location server ",
+                             sa, "", 1, code, rxconn);
     else
        print_internet_address("afs: Lost contact with file server ", sa, "",
-                              1, code);
+                              1, code, rxconn);
     return 1;
 }                              /*ServerDown */
 
@@ -301,7 +301,6 @@ CheckVLServer(struct srvAddr *sa, struct vrequest *areq)
     code = VL_ProbeServer(rxconn);
     RX_AFS_GLOCK();
     rx_SetConnDeadTime(rxconn, afs_rx_deadtime);
-    afs_PutConn(tc, rxconn, SHARED_LOCK);
     /*
      * If probe worked, or probe call not yet defined (for compatibility
      * with old vlsevers), then we treat this server as running again
@@ -310,9 +309,10 @@ CheckVLServer(struct srvAddr *sa, struct vrequest *areq)
        if (tc->parent->srvr == sa) {
            afs_MarkServerUpOrDown(sa, 0);
            print_internet_address("afs: volume location server ", sa,
-                                  " is back up", 2, code);
+                                  " is back up", 2, code, rxconn);
        }
     }
+    afs_PutConn(tc, rxconn, SHARED_LOCK);
 
 }                              /*CheckVLServer */
 
@@ -515,10 +515,13 @@ ForceAllNewConnections(void)
         sa = addrs[i];
        ForceNewConnections(sa);
     }
+
+    afs_osi_Free(addrs, srvAddrCount * sizeof(*addrs));
 }
 
 static void
-CkSrv_MarkUpDown(struct afs_conn **conns, int nconns, afs_int32 *results)
+CkSrv_MarkUpDown(struct afs_conn **conns, struct rx_connection **rxconns,
+                 int nconns, afs_int32 *results)
 {
     struct srvAddr *sa;
     struct afs_conn *tc;
@@ -532,7 +535,7 @@ CkSrv_MarkUpDown(struct afs_conn **conns, int nconns, afs_int32 *results)
            (tc->parent->srvr == sa)) {
            /* server back up */
            print_internet_address("afs: file server ", sa, " is back up", 2,
-                                  results[i]);
+                                  results[i], rxconns[i]);
 
            ObtainWriteLock(&afs_xserver, 244);
            ObtainWriteLock(&afs_xsrvAddr, 245);
@@ -546,7 +549,7 @@ CkSrv_MarkUpDown(struct afs_conn **conns, int nconns, afs_int32 *results)
        } else {
            if (results[i] < 0) {
                /* server crashed */
-               afs_ServerDown(sa, results[i]);
+               afs_ServerDown(sa, results[i], rxconns[i]);
                ForceNewConnections(sa);  /* multi homed clients */
            }
        }
@@ -597,7 +600,7 @@ CkSrv_GetCaps(int nconns, struct rx_connection **rxconns,
                caps[i].Capabilities_len = 0;
            }
     }
-    CkSrv_MarkUpDown(conns, nconns, results);
+    CkSrv_MarkUpDown(conns, rxconns, nconns, results);
 
     afs_osi_Free(caps, nconns * sizeof(Capabilities));
     afs_osi_Free(results, nconns * sizeof(afs_int32));
@@ -620,7 +623,7 @@ afs_LoopServers(int adown, struct cell *acellp, int vlalso,
                void (*func2) (int nservers, struct rx_connection **rxconns,
                               struct afs_conn **conns))
 {
-    struct vrequest treq;
+    struct vrequest *treq = NULL;
     struct server *ts;
     struct srvAddr *sa;
     struct afs_conn *tc = NULL;
@@ -643,7 +646,7 @@ afs_LoopServers(int adown, struct cell *acellp, int vlalso,
     if (AFS_IS_DISCONNECTED)
         return;
 
-    if ((code = afs_InitReq(&treq, afs_osi_credp)))
+    if ((code = afs_CreateReq(&treq, afs_osi_credp)))
        return;
     ObtainReadLock(&afs_xserver);      /* Necessary? */
     ObtainReadLock(&afs_xsrvAddr);
@@ -697,7 +700,7 @@ afs_LoopServers(int adown, struct cell *acellp, int vlalso,
        /* check vlserver with special code */
        if (sa->sa_portal == AFS_VLPORT) {
            if (vlalso)
-               CheckVLServer(sa, &treq);
+               CheckVLServer(sa, treq);
            continue;
        }
 
@@ -705,7 +708,7 @@ afs_LoopServers(int adown, struct cell *acellp, int vlalso,
            continue;           /* have just been added by setsprefs */
 
        /* get a connection, even if host is down; bumps conn ref count */
-       tu = afs_GetUser(treq.uid, ts->cell->cellNum, SHARED_LOCK);
+       tu = afs_GetUser(treq->uid, ts->cell->cellNum, SHARED_LOCK);
        tc = afs_ConnBySA(sa, ts->cell->fsport, ts->cell->cellNum, tu,
                          1 /*force */ , 1 /*create */ , SHARED_LOCK, 0,
                          &rxconn);
@@ -745,6 +748,7 @@ afs_LoopServers(int adown, struct cell *acellp, int vlalso,
     afs_osi_Free(conns, j * sizeof(struct afs_conn *));
     afs_osi_Free(rxconns, j * sizeof(struct rx_connection *));
     afs_osi_Free(conntimer, j * sizeof(afs_int32));
+    afs_DestroyReq(treq);
 
 } /*afs_CheckServers*/
 
@@ -822,10 +826,12 @@ afs_random(void)
        osi_timeval_t t;
        osi_GetTime(&t);
        /*
-        * 0xfffffff0 was changed to (~0 << 4) since it works no matter how many
-        * bits are in a tv_usec
+        * Clear the low nybble of tv_usec in a size-independent manner before adding
+        * in the rest of the state.
         */
-       state = (t.tv_usec & (~0 << 4)) + (rxi_getaddr() & 0xff);
+       state = t.tv_usec;
+       state ^= (state & 0x0f);
+       state += rxi_getaddr() & 0xff;
        state += (t.tv_sec & 0xff);
        for (i = 0; i < 30; i++) {
            ranstage(state);
@@ -1154,7 +1160,7 @@ afsi_enum_set_rank(struct hashbucket *h, caddr_t mkey, caddr_t arg1,
 }
 #endif                         /* AFS_SGI62_ENV */
 static int
-afs_SetServerPrefs(struct srvAddr *sa)
+afs_SetServerPrefs(struct srvAddr *const sa)
 {
 #if     defined(AFS_USERSPACE_IP_ADDR)
     int i;
@@ -1176,8 +1182,7 @@ afs_SetServerPrefs(struct srvAddr *sa)
 #endif
     int subnet, subnetmask, net, netmask;
 
-    if (sa)
-         sa->sa_iprank = 0;
+    sa->sa_iprank = 0;
 #ifdef AFS_SUN510_ENV
     rw_enter(&afsifinfo_lock, RW_READER);
 
@@ -1279,13 +1284,8 @@ afs_SetServerPrefs(struct srvAddr *sa)
     struct in_ifaddr *ifad = (struct in_ifaddr *)0;
     struct sockaddr_in *sin;
 
-    if (!sa) {
-       return;
-    }
     sa->sa_iprank = 0;
-#ifdef ADAPT_MTU
     ifn = rxi_FindIfnet(sa->sa_ip, &ifad);
-#endif
     if (ifn) {                 /* local, more or less */
 #ifdef IFF_LOOPBACK
        if (ifn->if_flags & IFF_LOOPBACK) {
@@ -1321,8 +1321,7 @@ afs_SetServerPrefs(struct srvAddr *sa)
     }
 #else                          /* USEIFADDR */
 
-    if (sa)
-       sa->sa_iprank = LO;
+    sa->sa_iprank = LO;
 #ifdef AFS_SGI62_ENV
     (void)hash_enum(&hashinfo_inaddr, afsi_enum_set_rank, HTF_INET, NULL,
                    (caddr_t) sa, NULL);
@@ -1358,13 +1357,12 @@ afs_SetServerPrefs(struct srvAddr *sa)
 #elif defined(AFS_FBSD_ENV)
     {
        struct in_ifaddr *ifa;
-#if defined(AFS_FBSD80_ENV)
-         TAILQ_FOREACH(ifa, &V_in_ifaddrhead, ia_link) {
-#else
-         TAILQ_FOREACH(ifa, &in_ifaddrhead, ia_link) {
-#endif
+       CURVNET_SET(rx_socket->so_vnet);
+       TAILQ_FOREACH(ifa, &V_in_ifaddrhead, ia_link) {
            afsi_SetServerIPRank(sa, &ifa->ia_ifa);
-    }}
+       }
+       CURVNET_RESTORE();
+    }
 #elif defined(AFS_OBSD_ENV)
     {
        extern struct in_ifaddrhead in_ifaddr;
@@ -1392,8 +1390,7 @@ afs_SetServerPrefs(struct srvAddr *sa)
 #endif
 #endif                         /* AFS_SUN5_ENV */
 #endif                         /* else AFS_USERSPACE_IP_ADDR */
-    if (sa)
-         sa->sa_iprank += afs_randomMod15();
+    sa->sa_iprank += afs_randomMod15();
 
     return 0;
 }                              /* afs_SetServerPrefs */
@@ -1496,7 +1493,7 @@ void
 afs_GetCapabilities(struct server *ts)
 {
     Capabilities caps = {0, NULL};
-    struct vrequest treq;
+    struct vrequest *treq = NULL;
     struct afs_conn *tc;
     struct unixuser *tu;
     struct rx_connection *rxconn;
@@ -1507,29 +1504,36 @@ afs_GetCapabilities(struct server *ts)
     if ( !afs_osi_credp )
        return;
 
-    if ((code = afs_InitReq(&treq, afs_osi_credp)))
+    if ((code = afs_CreateReq(&treq, afs_osi_credp)))
        return;
-    tu = afs_GetUser(treq.uid, ts->cell->cellNum, SHARED_LOCK);
-    if ( !tu )
+    tu = afs_GetUser(treq->uid, ts->cell->cellNum, SHARED_LOCK);
+    if ( !tu ) {
+       afs_DestroyReq(treq);
        return;
+    }
     tc = afs_ConnBySA(ts->addr, ts->cell->fsport, ts->cell->cellNum, tu, 0, 1,
                      SHARED_LOCK, 0, &rxconn);
     afs_PutUser(tu, SHARED_LOCK);
-    if ( !tc )
+    if ( !tc ) {
+       afs_DestroyReq(treq);
        return;
+    }
     /* InitCallBackStateN, triggered by our RPC, may need this */
     ReleaseWriteLock(&afs_xserver);
+    AFS_GUNLOCK();
     code = RXAFS_GetCapabilities(rxconn, &caps);
+    AFS_GLOCK();
     ObtainWriteLock(&afs_xserver, 723);
     /* we forced a conn above; important we mark it down if needed */
     if ((code < 0) && (code != RXGEN_OPCODE)) {
-       afs_ServerDown(tc->parent->srvr, code);
+       afs_ServerDown(tc->parent->srvr, code, rxconn);
        ForceNewConnections(tc->parent->srvr); /* multi homed clients */
     }
     afs_PutConn(tc, rxconn, SHARED_LOCK);
     if ( code && code != RXGEN_OPCODE ) {
        afs_warn("RXAFS_GetCapabilities failed with code %d\n", code);
        /* better not be anything to free. we failed! */
+       afs_DestroyReq(treq);
        return;
     }
 
@@ -1542,6 +1546,7 @@ afs_GetCapabilities(struct server *ts)
        caps.Capabilities_val = NULL;
     }
 
+    afs_DestroyReq(treq);
 }
 
 static struct server *
@@ -1675,7 +1680,8 @@ afs_GetServer(afs_uint32 *aserverp, afs_int32 nservers, afs_int32 acell,
        newts->flags |= SRVR_MULTIHOMED;
     }
     if (acell)
-       newts->cell = afs_GetCell(acell, 0);
+       /* Use the afs_GetCellStale variant to avoid afs_GetServer recursion. */
+       newts->cell = afs_GetCellStale(acell, 0);
 
     /* For each IP address we are registering */
     for (k = 0; k < nservers; k++) {
@@ -1764,7 +1770,8 @@ afs_GetServer(afs_uint32 *aserverp, afs_int32 nservers, afs_int32 acell,
                afs_servers[iphash] = orphts;
 
                if (acell)
-                   orphts->cell = afs_GetCell(acell, 0);
+                   /* Use the afs_GetCellStale variant to avoid afs_GetServer recursion. */
+                   orphts->cell = afs_GetCellStale(acell, 0);
            }
 
            /* Hang the srvAddr struct off of the server structure. The server