viced: Yell when we GetSomeSpace_r
[openafs.git] / src / viced / callback.c
index 1d52fc7..3533906 100644 (file)
 
 #include <afsconfig.h>
 #include <afs/param.h>
+#include <afs/stds.h>
 
+#include <roken.h>
 
-#include <stdio.h>
-#include <stdlib.h>            /* for malloc() */
-#include <time.h>              /* ANSI standard location for time stuff */
-#include <string.h>
-#ifdef AFS_NT40_ENV
-#include <fcntl.h>
-#include <io.h>
-#else
-#include <sys/time.h>
+#ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
-#include <unistd.h>
 #endif
-#include <afs/afs_assert.h>
-
-#include <afs/stds.h>
 
 #include <afs/nfs.h>           /* yuck.  This is an abomination. */
 #include <lwp.h>
@@ -190,11 +180,10 @@ static int FDel(struct FileEntry *fe);
 static int AddCallBack1_r(struct host *host, AFSFid * fid, afs_uint32 * thead,
                          int type, int locked);
 static void MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
-                                struct AFSCBFids *afidp, struct host *xhost);
-static int MultiBreakVolumeCallBack_r(struct host *host, int isheld,
+                                struct AFSCBFids *afidp);
+static int MultiBreakVolumeCallBack_r(struct host *host,
                                      struct VCBParams *parms, int deletefe);
-static int MultiBreakVolumeLaterCallBack(struct host *host, int isheld,
-                                        void *rock);
+static int MultiBreakVolumeLaterCallBack(struct host *host, void *rock);
 static int GetSomeSpace_r(struct host *hostp, int locked);
 static int ClearHostCallbacks_r(struct host *hp, int locked);
 static int DumpCallBackState_r(void);
@@ -354,11 +343,9 @@ CDel(struct CallBack *cb, int deletefe)
     for (safety = 0, cbp = &fe->firstcb; *cbp && *cbp != cbi;
         cbp = &itocb(*cbp)->cnext, safety++) {
        if (safety > cbstuff.nblks + 10) {
-           osi_Panic("CDel: Internal Error -- shutting down: wanted %d from %d, now at %d\n",
-                     cbi, fe->firstcb, *cbp);
-           ViceLog(0,
-                   ("CDel: Internal Error -- shutting down: wanted %d from %d, now at %d\n",
-                    cbi, fe->firstcb, *cbp));
+           ViceLogThenPanic(0, ("CDel: Internal Error -- shutting down: "
+                                "wanted %d from %d, now at %d\n",
+                                cbi, fe->firstcb, *cbp));
            DumpCallBackState_r();
            ShutDownAndCore(PANIC);
        }
@@ -439,8 +426,7 @@ InitCallBack(int nblks)
      * FE[0] and CB[0] are not used--and not allocated */
     FE = ((struct FileEntry *)(calloc(nblks, sizeof(struct FileEntry))));
     if (!FE) {
-       ViceLog(0, ("Failed malloc in InitCallBack\n"));
-       osi_Panic("Failed malloc in InitCallBack\n");
+       ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
     }
     FE--;  /* FE[0] is supposed to point to junk */
     cbstuff.nFEs = nblks;
@@ -448,8 +434,7 @@ InitCallBack(int nblks)
        FreeFE(&FE[cbstuff.nFEs]);      /* This is correct */
     CB = ((struct CallBack *)(calloc(nblks, sizeof(struct CallBack))));
     if (!CB) {
-       ViceLog(0, ("Failed malloc in InitCallBack\n"));
-       osi_Panic("Failed malloc in InitCallBack\n");
+       ViceLogThenPanic(0, ("Failed malloc in InitCallBack\n"));
     }
     CB--;  /* CB[0] is supposed to point to junk */
     cbstuff.nCBs = nblks;
@@ -668,8 +653,8 @@ CompareCBA(const void *e1, const void *e2)
 }
 
 /* Take an array full of hosts, all held.  Break callbacks to them, and
- * release the holds once you're done, except don't release xhost.  xhost
- * may be NULL.  Currently only works for a single Fid in afidp array.
+ * release the holds once you're done.
+ * Currently only works for a single Fid in afidp array.
  * If you want to make this work with multiple fids, you need to fix
  * the error handling.  One approach would be to force a reset if a
  * multi-fid call fails, or you could add delayed callbacks for each
@@ -687,7 +672,7 @@ CompareCBA(const void *e1, const void *e2)
  * wherever that is done. */
 static void
 MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
-                    struct AFSCBFids *afidp, struct host *xhost)
+                    struct AFSCBFids *afidp)
 {
     int i, j;
     struct rx_connection *conns[MAX_CB_HOSTS];
@@ -774,7 +759,7 @@ MultiBreakCallBack_r(struct cbstruct cba[], int ncbas,
     for (i = 0; i < ncbas; i++) {
        struct host *hp;
        hp = cba[i].hp;
-       if (hp && xhost != hp) {
+       if (hp) {
            h_Release_r(hp);
        }
     }
@@ -807,8 +792,8 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
 {
     struct FileEntry *fe;
     struct CallBack *cb, *nextcb;
-    struct cbstruct cba[MAX_CB_HOSTS];
-    int ncbas;
+    struct cbstruct cbaDef[MAX_CB_HOSTS], *cba = cbaDef;
+    unsigned int ncbas, cbaAlloc = MAX_CB_HOSTS;
     struct AFSCBFids tf;
     int hostindex;
     char hoststr[16];
@@ -833,8 +818,7 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
     tf.AFSCBFids_len = 1;
     tf.AFSCBFids_val = fid;
 
-    for (; cb;) {
-       for (ncbas = 0; cb && ncbas < MAX_CB_HOSTS; cb = nextcb) {
+       for (ncbas = 0; cb ; cb = nextcb) {
            nextcb = itocb(cb->cnext);
            if ((cb->hhead != hostindex || flag)
                && (cb->status == CB_BULK || cb->status == CB_NORMAL
@@ -851,6 +835,21 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
                } else {
                    if (!(thishost->hostFlags & HOSTDELETED)) {
                        h_Hold_r(thishost);
+                       if (ncbas == cbaAlloc) {        /* Need more space */
+                           int curLen = cbaAlloc*sizeof(cba[0]);
+                           struct cbstruct *cbaOld = (cba == cbaDef) ? NULL : cba;
+
+                           /* There are logical contraints elsewhere that the number of hosts
+                              (i.e. h_HTSPERBLOCK*h_MAXHOSTTABLES) remains in the realm of a signed "int".
+                              cbaAlloc is defined unsigned int hence doubling below cannot overflow
+                           */
+                           cbaAlloc = cbaAlloc<<1;     /* double */
+                           cba = realloc(cbaOld, cbaAlloc * sizeof(cba[0]));
+
+                           if (cbaOld == NULL) {       /* realloc wouldn't have copied from cbaDef */
+                               memcpy(cba, cbaDef, curLen);
+                           }
+                       }
                        cba[ncbas].hp = thishost;
                        cba[ncbas].thead = cb->thead;
                        ncbas++;
@@ -864,20 +863,16 @@ BreakCallBack(struct host *xhost, AFSFid * fid, int flag)
        }
 
        if (ncbas) {
-           MultiBreakCallBack_r(cba, ncbas, &tf, xhost);
+           struct cbstruct *cba2;
+           int num;
 
-           /* we need to to all these initializations again because MultiBreakCallBack may block */
-           fe = FindFE(fid);
-           if (!fe) {
-               goto done;
-           }
-           cb = itocb(fe->firstcb);
-           if (!cb || ((fe->ncbs == 1) && (cb->hhead == hostindex) && !flag)) {
-               /* the most common case is what follows the || */
-               goto done;
+           for (cba2 = cba, num = ncbas; ncbas > 0; cba2 += num, ncbas -= num) {
+               num = (ncbas > MAX_CB_HOSTS) ? MAX_CB_HOSTS : ncbas;
+               MultiBreakCallBack_r(cba2, num, &tf);
            }
        }
-    }
+
+       if (cba != cbaDef) free(cba);
 
   done:
     H_UNLOCK;
@@ -1113,18 +1108,14 @@ BreakDelayedCallBacks_r(struct host *host)
     return (host->hostFlags & VENUSDOWN);
 }
 
-/*
-** isheld is 0 if the host is held in h_Enumerate
-** isheld is 1 if the host is held in BreakVolumeCallBacks
-*/
 static int
-MultiBreakVolumeCallBack_r(struct host *host, int isheld,
+MultiBreakVolumeCallBack_r(struct host *host,
                           struct VCBParams *parms, int deletefe)
 {
     char hoststr[16];
 
     if (host->hostFlags & HOSTDELETED)
-       return 0;               /* host is deleted, release hold */
+       return 0;
 
     if (!(host->hostFlags & HCBREAK))
        return 0;               /* host is not flagged to notify */
@@ -1145,7 +1136,7 @@ MultiBreakVolumeCallBack_r(struct host *host, int isheld,
                                                 * later */
        host->hostFlags &= ~(RESETDONE|HCBREAK);        /* Do InitCallBackState when host returns */
        h_Unlock_r(host);
-       return 0;               /* parent will release hold */
+       return 0;
     }
     osi_Assert(parms->ncbas <= MAX_CB_HOSTS);
 
@@ -1159,27 +1150,28 @@ MultiBreakVolumeCallBack_r(struct host *host, int isheld,
        tf.AFSCBFids_val = parms->fid;
 
        /* this releases all the hosts */
-       MultiBreakCallBack_r(parms->cba, parms->ncbas, &tf, 0 /* xhost */ );
+       MultiBreakCallBack_r(parms->cba, parms->ncbas, &tf);
 
        parms->ncbas = 0;
     }
     parms->cba[parms->ncbas].hp = host;
     parms->cba[(parms->ncbas)++].thead = parms->thead;
     host->hostFlags &= ~HCBREAK;
-    return 1;          /* parent shouldn't release hold, more work to do */
+
+    /* we have more work to do on this host, so make sure we keep a reference
+     * to it */
+    h_Hold_r(host);
+
+    return 0;
 }
 
-/*
-** isheld is 0 if the host is held in h_Enumerate
-** isheld is 1 if the host is held in BreakVolumeCallBacks
-*/
 static int
-MultiBreakVolumeLaterCallBack(struct host *host, int isheld, void *rock)
+MultiBreakVolumeLaterCallBack(struct host *host, void *rock)
 {
     struct VCBParams *parms = (struct VCBParams *)rock;
     int retval;
     H_LOCK;
-    retval = MultiBreakVolumeCallBack_r(host, isheld, parms, 0);
+    retval = MultiBreakVolumeCallBack_r(host, parms, 0);
     H_UNLOCK;
     return retval;
 }
@@ -1340,7 +1332,7 @@ BreakLaterCallBacks(void)
            tf.AFSCBFids_len = 1;
            tf.AFSCBFids_val = &fid;
 
-           MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf, 0);
+           MultiBreakCallBack_r(henumParms.cba, henumParms.ncbas, &tf);
            henumParms.ncbas = 0;
        }
     }
@@ -1446,7 +1438,7 @@ struct lih_params {
  * theory not give these to us anyway, but be paranoid.
  */
 static int
-lih0_r(struct host *host, int flags, void *rock)
+lih0_r(struct host *host, void *rock)
 {
     struct lih_params *params = (struct lih_params *)rock;
 
@@ -1464,12 +1456,12 @@ lih0_r(struct host *host, int flags, void *rock)
        h_Hold_r(host);
        params->lih = host;
     }
-    return flags;
+    return 0;
 }
 
 /* same as lih0_r, except we do not prevent held hosts from being selected. */
 static int
-lih1_r(struct host *host, int flags, void *rock)
+lih1_r(struct host *host, void *rock)
 {
     struct lih_params *params = (struct lih_params *)rock;
 
@@ -1485,7 +1477,7 @@ lih1_r(struct host *host, int flags, void *rock)
        h_Hold_r(host);
        params->lih = host;
     }
-    return flags;
+    return 0;
 }
 
 /* This could be upgraded to get more space each time */
@@ -1507,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"));
@@ -2464,6 +2469,12 @@ cb_stateRestoreCBs(struct fs_dump_state * state, struct FileEntry * fe,
 
     for (idx = 0; idx < niovecs; idx++) {
        cbdsk = (struct CBDiskEntry *) iov[idx].iov_base;
+
+       if (cbdsk->cb.hhead < state->h_map.len &&
+           state->h_map.entries[cbdsk->cb.hhead].valid == FS_STATE_IDX_SKIPPED) {
+           continue;
+       }
+
        if ((cb = GetCB()) == NULL) {
            ViceLog(0, ("cb_stateRestoreCBs: ran out of free CallBack structures\n"));
            ret = 1;
@@ -2530,6 +2541,7 @@ cb_stateDiskEntryToFE(struct fs_dump_state * state,
        ret = 1;
        goto done;
     }
+    state->fe_map.entries[in->index].valid = FS_STATE_IDX_VALID;
     state->fe_map.entries[in->index].old_idx = in->index;
     state->fe_map.entries[in->index].new_idx = fetoi(out);
 
@@ -2560,6 +2572,7 @@ cb_stateDiskEntryToCB(struct fs_dump_state * state,
        ret = 1;
        goto done;
     }
+    state->cb_map.entries[in->index].valid = FS_STATE_IDX_VALID;
     state->cb_map.entries[in->index].old_idx = in->index;
     state->cb_map.entries[in->index].new_idx = cbtoi(out);
 
@@ -2594,7 +2607,8 @@ fe_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
     if (old >= state->fe_map.len) {
        ViceLog(0, ("fe_OldToNew: index %d is out of range\n", old));
        ret = 1;
-    } else if (state->fe_map.entries[old].old_idx != old) { /* sanity check */
+    } else if (state->fe_map.entries[old].valid != FS_STATE_IDX_VALID ||
+               state->fe_map.entries[old].old_idx != old) { /* sanity check */
        ViceLog(0, ("fe_OldToNew: index %d points to an invalid FileEntry record\n", old));
        ret = 1;
     } else {
@@ -2619,7 +2633,8 @@ cb_OldToNew(struct fs_dump_state * state, afs_uint32 old, afs_uint32 * new)
     if (old >= state->cb_map.len) {
        ViceLog(0, ("cb_OldToNew: index %d is out of range\n", old));
        ret = 1;
-    } else if (state->cb_map.entries[old].old_idx != old) { /* sanity check */
+    } else if (state->cb_map.entries[old].valid != FS_STATE_IDX_VALID ||
+               state->cb_map.entries[old].old_idx != old) { /* sanity check */
        ViceLog(0, ("cb_OldToNew: index %d points to an invalid CallBack record\n", old));
        ret = 1;
     } else {
@@ -2959,9 +2974,8 @@ MultiBreakCallBackAlternateAddress_r(struct host *host,
     interfaces = calloc(i, sizeof(struct AddrPort));
     conns = calloc(i, sizeof(struct rx_connection *));
     if (!interfaces || !conns) {
-       ViceLog(0,
-               ("Failed malloc in MultiBreakCallBackAlternateAddress_r\n"));
-       osi_Panic("Failed malloc in MultiBreakCallBackAlternateAddress_r\n");
+       ViceLogThenPanic(0, ("Failed malloc in "
+                            "MultiBreakCallBackAlternateAddress_r\n"));
     }
 
     /* initialize alternate rx connections */
@@ -3055,8 +3069,8 @@ MultiProbeAlternateAddress_r(struct host *host)
     interfaces = calloc(i, sizeof(struct AddrPort));
     conns = calloc(i, sizeof(struct rx_connection *));
     if (!interfaces || !conns) {
-       ViceLog(0, ("Failed malloc in MultiProbeAlternateAddress_r\n"));
-       osi_Panic("Failed malloc in MultiProbeAlternateAddress_r\n");
+       ViceLogThenPanic(0, ("Failed malloc in "
+                            "MultiProbeAlternateAddress_r\n"));
     }
 
     /* initialize alternate rx connections */