Don't allocate objects of 0 length
[openafs.git] / src / viced / callback.c
index bb6ec68..5460702 100644 (file)
 #include <sys/file.h>
 #endif
 
+#include <afs/opr.h>
+#include <opr/lock.h>
 #include <afs/nfs.h>           /* yuck.  This is an abomination. */
-#include <lwp.h>
 #include <rx/rx.h>
+#include <rx/rx_queue.h>
 #include <afs/afscbint.h>
 #include <afs/afsutil.h>
-#include <lock.h>
 #include <afs/ihandle.h>
+#include <afs/partition.h>
 #include <afs/vnode.h>
 #include <afs/volume.h>
 #include "viced_prototypes.h"
 #include "host.h"
 #include "callback.h"
 #ifdef AFS_DEMAND_ATTACH_FS
-#include "../tviced/serialize_state.h"
+#include "serialize_state.h"
 #endif /* AFS_DEMAND_ATTACH_FS */
 
 
@@ -410,7 +412,7 @@ FDel(struct FileEntry *fe)
 
     while (*p && *p != fei)
        p = &itofe(*p)->fnext;
-    osi_Assert(*p);
+    opr_Assert(*p);
     *p = fe->fnext;
     FreeFE(fe);
     return 0;
@@ -420,11 +422,13 @@ FDel(struct FileEntry *fe)
 int
 InitCallBack(int nblks)
 {
+    opr_Assert(nblks > 0);
+
     H_LOCK;
-    tfirst = CBtime(FT_ApproxTime());
+    tfirst = CBtime(time(NULL));
     /* N.B. The "-1", below, is because
      * FE[0] and CB[0] are not used--and not allocated */
-    FE = ((struct FileEntry *)(calloc(nblks, sizeof(struct FileEntry))));
+    FE = calloc(nblks, sizeof(struct FileEntry));
     if (!FE) {
        ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
     }
@@ -432,7 +436,7 @@ InitCallBack(int nblks)
     cbstuff.nFEs = nblks;
     while (cbstuff.nFEs)
        FreeFE(&FE[cbstuff.nFEs]);      /* This is correct */
-    CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack))));
+    CB = calloc(nblks, sizeof(struct CallBack));
     if (!CB) {
        ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
     }
@@ -457,10 +461,8 @@ XCallBackBulk_r(struct host * ahost, struct AFSFid * fids, afs_int32 nfids)
     int j;
     struct rx_connection *cb_conn = NULL;
 
-#ifdef ADAPT_MTU
     rx_SetConnDeadTime(ahost->callback_rxcon, 4);
     rx_SetConnHardDeadTime(ahost->callback_rxcon, AFS_HARDDEADTIME);
-#endif
 
     code = 0;
     j = 0;
@@ -560,18 +562,18 @@ AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead, int type,
     fe = FindFE(fid);
     if (type == CB_NORMAL) {
        time_out =
-           TimeCeiling(FT_ApproxTime() + TimeOut(fe ? fe->ncbs : 0) +
+           TimeCeiling(time(NULL) + TimeOut(fe ? fe->ncbs : 0) +
                        ServerBias);
        Thead = THead(CBtime(time_out));
     } else if (type == CB_VOLUME) {
-       time_out = TimeCeiling((60 * 120 + FT_ApproxTime()) + ServerBias);
+       time_out = TimeCeiling((60 * 120 + time(NULL)) + ServerBias);
        Thead = THead(CBtime(time_out));
     } else if (type == CB_BULK) {
        /* bulk status can get so many callbacks all at once, and most of them
         * are probably not for things that will be used for long.
         */
        time_out =
-           TimeCeiling(FT_ApproxTime() + ServerBias +
+           TimeCeiling(time(NULL) + ServerBias +
                        TimeOut(22 + (fe ? fe->ncbs : 0)));
        Thead = THead(CBtime(time_out));
     }
@@ -679,7 +681,7 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
     static struct AFSCBs tc = { 0, 0 };
     int multi_to_cba_map[MAX_CB_HOSTS];
 
-    osi_Assert(ncbas <= MAX_CB_HOSTS);
+    opr_Assert(ncbas <= MAX_CB_HOSTS);
 
     /* sort cba list to avoid makecall issues */
     qsort(cba, ncbas, sizeof(struct cbstruct), CompareCBA);
@@ -694,10 +696,8 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
        multi_to_cba_map[j] = i;
        conns[j++] = thishost->callback_rxcon;
 
-#ifdef ADAPT_MTU
        rx_SetConnDeadTime(thishost->callback_rxcon, 4);
        rx_SetConnHardDeadTime(thishost->callback_rxcon, AFS_HARDDEADTIME);
-#endif
     }
 
     if (j) {                   /* who knows what multi would do with 0 conns? */
@@ -798,10 +798,15 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
     int hostindex;
     char hoststr[16];
 
-    ViceLog(7,
-           ("BCB: BreakCallBack(Host %p all but %s:%d, (%u,%u,%u))\n",
-            xhost, afs_inet_ntoa_r(xhost->host, hoststr), ntohs(xhost->port),
-            fid->Volume, fid->Vnode, fid->Unique));
+    if (xhost)
+       ViceLog(7,
+               ("BCB: BreakCallBack(Host %p all but %s:%d, (%u,%u,%u))\n",
+                xhost, afs_inet_ntoa_r(xhost->host, hoststr), ntohs(xhost->port),
+                fid->Volume, fid->Vnode, fid->Unique));
+    else
+       ViceLog(7,
+               ("BCB: BreakCallBack(No Host, (%u,%u,%u))\n",
+               fid->Volume, fid->Vnode, fid->Unique));
 
     H_LOCK;
     cbstuff.BreakCallBacks++;
@@ -809,7 +814,7 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
     if (!fe) {
        goto done;
     }
-    hostindex = h_htoi(xhost);
+    hostindex = xhost ? h_htoi(xhost) : 0;
     cb = itocb(fe->firstcb);
     if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
        /* the most common case is what follows the || */
@@ -1138,7 +1143,7 @@ MultiBreakVolumeCallBack_r(struct host *host,
        h_Unlock_r(host);
        return 0;
     }
-    osi_Assert(parms->ncbas <= MAX_CB_HOSTS);
+    opr_Assert(parms->ncbas <= MAX_CB_HOSTS);
 
     /* Do not call MultiBreakCallBack on the current host structure
      ** because it would prematurely release the hold on the host
@@ -1187,14 +1192,10 @@ MultiBreakVolumeLaterCallBack(struct host *host, void *rock)
  * Now uses multi-RX for CallBack RPC in a different thread,
  * only marking them here.
  */
-#ifdef AFS_PTHREAD_ENV
 extern pthread_cond_t fsync_cond;
-#else
-extern char fsync_wait[];
-#endif
 
 int
-BreakVolumeCallBacksLater(afs_uint32 volume)
+BreakVolumeCallBacksLater(VolumeId volume)
 {
     int hash;
     afs_uint32 *feip;
@@ -1203,7 +1204,8 @@ BreakVolumeCallBacksLater(afs_uint32 volume)
     struct host *host;
     int found = 0;
 
-    ViceLog(25, ("Setting later on volume %u\n", volume));
+    ViceLog(25, ("Setting later on volume %" AFS_VOLID_FMT "\n",
+                afs_printable_VolumeId_lu(volume)));
     H_LOCK;
     for (hash = 0; hash < FEHASH_SIZE; hash++) {
        for (feip = &HashTable[hash]; (fe = itofe(*feip)) != NULL; ) {
@@ -1230,13 +1232,9 @@ BreakVolumeCallBacksLater(afs_uint32 volume)
     }
 
     ViceLog(25, ("Fsync thread wakeup\n"));
-#ifdef AFS_PTHREAD_ENV
     FSYNC_LOCK;
-    CV_BROADCAST(&fsync_cond);
+    opr_cv_broadcast(&fsync_cond);
     FSYNC_UNLOCK;
-#else
-    LWP_NoYieldSignal(fsync_wait);
-#endif
     return 0;
 }
 
@@ -1268,8 +1266,8 @@ BreakLaterCallBacks(void)
                /* Ugly, but used to avoid left side casting */
                struct object *tmpfe;
                ViceLog(125,
-                       ("Unchaining for %u:%u:%u\n", fe->vnode, fe->unique,
-                        fe->volid));
+                       ("Unchaining for %u:%u:%" AFS_VOLID_FMT "\n", fe->vnode,
+                        fe->unique, afs_printable_VolumeId_lu(fe->volid)));
                fid.Volume = fe->volid;
                *feip = fe->fnext;
                fe->status &= ~FE_LATER; /* not strictly needed */
@@ -1309,9 +1307,10 @@ BreakLaterCallBacks(void)
                /* leave flag for MultiBreakVolumeCallBack to clear */
            } else {
                ViceLog(125,
-                       ("Found host %p (%s:%d) non-DELAYED cb for %u:%u:%u\n",
+                       ("Found host %p (%s:%d) non-DELAYED cb for %u:%u:%" AFS_VOLID_FMT "\n",
                         host, afs_inet_ntoa_r(host->host, hoststr),
-                        ntohs(host->port), fe->vnode, fe->unique, fe->volid));
+                        ntohs(host->port), fe->vnode, fe->unique,
+                        afs_printable_VolumeId_lu(fe->volid)));
            }
        }
        myfe = fe;
@@ -1358,7 +1357,7 @@ CleanupTimedOutCallBacks(void)
 int
 CleanupTimedOutCallBacks_r(void)
 {
-    afs_uint32 now = CBtime(FT_ApproxTime());
+    afs_uint32 now = CBtime(time(NULL));
     afs_uint32 *thead;
     struct CallBack *cb;
     int ntimedout = 0;
@@ -1372,10 +1371,11 @@ CleanupTimedOutCallBacks_r(void)
                cb = itocb(cbi);
                cbi = cb->tnext;
                ViceLog(8,
-                       ("CCB: deleting timed out call back %x (%s:%d), (%u,%u,%u)\n",
+                       ("CCB: deleting timed out call back %x (%s:%d), (%" AFS_VOLID_FMT ",%u,%u)\n",
                          h_itoh(cb->hhead)->host,
                          afs_inet_ntoa_r(h_itoh(cb->hhead)->host, hoststr),
-                        h_itoh(cb->hhead)->port, itofe(cb->fhead)->volid,
+                        h_itoh(cb->hhead)->port,
+                        afs_printable_VolumeId_lu(itofe(cb->fhead)->volid),
                         itofe(cb->fhead)->vnode, itofe(cb->fhead)->unique));
                HDel(cb);
                CDel(cb, 1);
@@ -1499,6 +1499,19 @@ GetSomeSpace_r(struct host *hostp, int locked)
     struct lih_params params;
     int i = 0;
 
+    if (cbstuff.GotSomeSpaces == 0) {
+       /* only log this once; if GSS is getting called constantly, that's not
+        * good but don't make things worse by spamming the log. */
+       ViceLog(0, ("We have run out of callback space; forcing callback revocation. "
+                   "This suggests the fileserver is configured with insufficient "
+                   "callbacks; you probably want to increase the -cb fileserver "
+                   "parameter (current setting: %u). The fileserver will continue "
+                   "to operate, but this may indicate a severe performance problem\n",
+                   cbstuff.nblks));
+       ViceLog(0, ("This message is logged at most once; for more information "
+                   "see the OpenAFS documentation and fileserver xstat collection 3\n"));
+    }
+
     cbstuff.GotSomeSpaces++;
     ViceLog(5,
            ("GSS: First looking for timed out call backs via CleanupCallBacks\n"));
@@ -2637,7 +2650,7 @@ static int
 DumpCallBackState_r(void)
 {
     int fd, oflag;
-    afs_uint32 magic = MAGICV2, now = (afs_int32) FT_ApproxTime(), freelisthead;
+    afs_uint32 magic = MAGICV2, now = (afs_int32) time(NULL), freelisthead;
 
     oflag = O_WRONLY | O_CREAT | O_TRUNC;
 #ifdef AFS_NT40_ENV
@@ -2911,9 +2924,9 @@ PrintCB(struct CallBack *cb, afs_uint32 now)
     if (fe == NULL)
        return;
 
-    printf("vol=%u vn=%u cbs=%d hi=%d st=%d fest=%d, exp in %lu secs at %s",
-          fe->volid, fe->vnode, fe->ncbs, cb->hhead, cb->status, fe->status,
-          expires - now, ctime(&expires));
+    printf("vol=%" AFS_VOLID_FMT " vn=%u cbs=%d hi=%d st=%d fest=%d, exp in %lu secs at %s",
+          afs_printable_VolumeId_lu(fe->volid), fe->vnode, fe->ncbs,
+          cb->hhead, cb->status, fe->status, expires - now, ctime(&expires));
 }
 
 #endif
@@ -2981,7 +2994,7 @@ MultiBreakCallBackAlternateAddress_r(struct host *host,
        j++;
     }
 
-    osi_Assert(j);                     /* at least one alternate address */
+    opr_Assert(j);                     /* at least one alternate address */
     ViceLog(125,
            ("Starting multibreakcall back on all addr for host %p (%s:%d)\n",
              host, afs_inet_ntoa_r(host->host, hoststr), ntohs(host->port)));
@@ -2994,10 +3007,12 @@ MultiBreakCallBackAlternateAddress_r(struct host *host,
            if (host->callback_rxcon)
                rx_DestroyConnection(host->callback_rxcon);
            host->callback_rxcon = conns[multi_i];
-            h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
+           /* add then remove */
+           addInterfaceAddr_r(host, interfaces[multi_i].addr,
+                                    interfaces[multi_i].port);
+           removeInterfaceAddr_r(host, host->host, host->port);
            host->host = interfaces[multi_i].addr;
            host->port = interfaces[multi_i].port;
-            h_AddHostToAddrHashTable_r(host->host, host->port, host);
            connSuccess = conns[multi_i];
            rx_SetConnDeadTime(host->callback_rxcon, 50);
            rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);
@@ -3076,7 +3091,7 @@ MultiProbeAlternateAddress_r(struct host *host)
        j++;
     }
 
-    osi_Assert(j);                     /* at least one alternate address */
+    opr_Assert(j);                     /* at least one alternate address */
     ViceLog(125,
            ("Starting multiprobe on all addr for host %p (%s:%d)\n",
              host, afs_inet_ntoa_r(host->host, hoststr),
@@ -3090,10 +3105,12 @@ MultiProbeAlternateAddress_r(struct host *host)
            if (host->callback_rxcon)
                rx_DestroyConnection(host->callback_rxcon);
            host->callback_rxcon = conns[multi_i];
-            h_DeleteHostFromAddrHashTable_r(host->host, host->port, host);
+           /* add then remove */
+           addInterfaceAddr_r(host, interfaces[multi_i].addr,
+                                    interfaces[multi_i].port);
+           removeInterfaceAddr_r(host, host->host, host->port);
            host->host = interfaces[multi_i].addr;
            host->port = interfaces[multi_i].port;
-            h_AddHostToAddrHashTable_r(host->host, host->port, host);
            connSuccess = conns[multi_i];
            rx_SetConnDeadTime(host->callback_rxcon, 50);
            rx_SetConnHardDeadTime(host->callback_rxcon, AFS_HARDDEADTIME);