vldb_check: remove unreferenced mh entries with -fix
[openafs.git] / src / vlserver / vldb_check.c
index c351696..dc6dedf 100644 (file)
@@ -1,12 +1,28 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
  */
 
+#include <afsconfig.h>
+#include <afs/param.h>
+
+#include <roken.h>
+
+#ifdef AFS_NT40_ENV
+#include <WINNT/afsevent.h>
+#endif
+
+#include <ubik.h>
+#include <afs/afsutil.h>
+#include <afs/cmd.h>
+
+#include "vlserver.h"
+#include "vldbint.h"
+
 /* Read a VLDB file and verify it for correctness */
 
 #define VL  0x001              /* good volume entry */
 #define vldbread(x,y,z) vldbio(x,y,z,0)
 #define vldbwrite(x,y,z) vldbio(x,y,z,1)
 
-#include <afsconfig.h>
-#include <afs/param.h>
-
-
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <string.h>
-#ifdef AFS_NT40_ENV
-#include <winsock2.h>
-#include <WINNT/afsevent.h>
-#include <io.h>
-#else
-#include <sys/socket.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#endif
-
-#include "vlserver.h"
-#include "vldbint.h"
-#include <ubik.h>
-#include <afs/afsutil.h>
-#include <afs/cmd.h>
-
 #define ADDR(x) (x/sizeof(struct nvlentry))
 
 int fd;
@@ -77,9 +67,9 @@ int listentries, listservers, listheader, listuheader, verbose, quiet;
 int fix = 0;
 int passes = 0;
 /* if quiet, don't send anything to stdout */
-int quiet = 0; 
+int quiet = 0;
 /*  error level. 0 = no error, 1 = warning, 2 = error, 4 = fatal */
-int error_level  = 0; 
+int error_level  = 0;
 
 struct er {
     long addr;
@@ -87,39 +77,38 @@ struct er {
 } *record;
 afs_int32 maxentries;
 int serveraddrs[MAXSERVERID + 2];
+u_char serverxref[MAXSERVERID + 2];  /**< to resolve cross-linked mh entries */
+
+struct mhinfo {
+    afs_uint32 addr;                   /**< vldb file record */
+    char orphan[VL_MHSRV_PERBLK];      /**< unreferenced mh enties */
+} mhinfo[VL_MAX_ADDREXTBLKS];
+
 
 /*  Used to control what goes to stdout based on quiet flag */
-void 
+void
 quiet_println(const char *fmt,...) {
-    va_list args;                                             
+    va_list args;
     if (!quiet) {
-        va_start(args, fmt);                                      
-        vfprintf(stdout, fmt, args);                              
-        va_end(args);                                             
+        va_start(args, fmt);
+        vfprintf(stdout, fmt, args);
+        va_end(args);
     }
 }
 
 /*  Used to set the error level and ship messages to stderr */
-void                                                   
-log_error(int eval, const char *fmt, ...)                          
-{                                                             
-    va_list args;                                             
+void
+log_error(int eval, const char *fmt, ...)
+{
+    va_list args;
     if (error_level < eval) error_level  = eval ;  /*  bump up the severity */
-    va_start(args, fmt);                                      
-    vfprintf(stderr, fmt, args);                              
-    va_end(args);                                             
+    va_start(args, fmt);
+    vfprintf(stderr, fmt, args);
+    va_end(args);
 
     if (error_level  == VLDB_CHECK_FATAL) exit(VLDB_CHECK_FATAL);
-}  
-
-
-#if 0
-int
-writeUbikHeader()
-{
-    /* Bump the version number?? We could cheat and push a new db... */
 }
-#endif
+
 
 #define HDRSIZE 64
 int
@@ -178,9 +167,9 @@ vldbio(int position, void *buffer, int size, int rdwr)
        return (-1);
     }
 
-    if (rdwr == 1) 
+    if (rdwr == 1)
        r = write(fd, buffer, size);
-    else 
+    else
        r = read(fd, buffer, size);
 
     if (r != size) {
@@ -277,7 +266,7 @@ readheader(struct vlheader *headerp)
            ntohl(headerp->vital_header.totalEntries[1]);
 
     headerp->SIT = ntohl(headerp->SIT);
-    for (i = 0; i < MAXSERVERID; i++)
+    for (i = 0; i <= MAXSERVERID; i++)
        headerp->IpMappedAddr[i] = ntohl(headerp->IpMappedAddr[i]);
     for (i = 0; i < HASHSIZE; i++)
        headerp->VolnameHash[i] = ntohl(headerp->VolnameHash[i]);
@@ -341,7 +330,7 @@ writeheader(struct vlheader *headerp)
            htonl(headerp->vital_header.totalEntries[1]);
 
     headerp->SIT = htonl(headerp->SIT);
-    for (i = 0; i < MAXSERVERID; i++)
+    for (i = 0; i <= MAXSERVERID; i++)
        headerp->IpMappedAddr[i] = htonl(headerp->IpMappedAddr[i]);
     for (i = 0; i < HASHSIZE; i++)
        headerp->VolnameHash[i] = htonl(headerp->VolnameHash[i]);
@@ -353,18 +342,20 @@ writeheader(struct vlheader *headerp)
 }
 
 void
-readMH(afs_int32 addr, struct extentaddr *mhblockP)
+readMH(afs_uint32 addr, int block, struct extentaddr *mhblockP)
 {
     int i, j;
     struct extentaddr *e;
 
     vldbread(addr, (char *)mhblockP, VL_ADDREXTBLK_SIZE);
 
-    mhblockP->ex_count = ntohl(mhblockP->ex_count);
-    mhblockP->ex_flags = ntohl(mhblockP->ex_flags);
-    for (i = 0; i < VL_MAX_ADDREXTBLKS; i++)
-       mhblockP->ex_contaddrs[i] = ntohl(mhblockP->ex_contaddrs[i]);
-
+    if (block == 0) {
+        mhblockP->ex_count = ntohl(mhblockP->ex_count);
+        mhblockP->ex_hdrflags = ntohl(mhblockP->ex_hdrflags);
+        for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
+           mhblockP->ex_contaddrs[i] = ntohl(mhblockP->ex_contaddrs[i]);
+        }
+    }
     for (i = 1; i < VL_MHSRV_PERBLK; i++) {
        e = &(mhblockP[i]);
 
@@ -469,6 +460,33 @@ readentry(afs_int32 addr, struct nvlentry *vlentryp, afs_int32 *type)
 }
 
 void
+writeMH(afs_int32 addr, int block, struct extentaddr *mhblockP)
+{
+    int i, j;
+    struct extentaddr *e;
+
+    if (verbose) {
+       quiet_println("Writing back MH block % at addr %u\n", block,  addr);
+    }
+    if (block == 0) {
+       mhblockP->ex_count = htonl(mhblockP->ex_count);
+       mhblockP->ex_hdrflags = htonl(mhblockP->ex_hdrflags);
+       for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
+           mhblockP->ex_contaddrs[i] = htonl(mhblockP->ex_contaddrs[i]);
+       }
+    }
+    for (i = 1; i < VL_MHSRV_PERBLK; i++) {
+       e = &(mhblockP[i]);
+       /* hostuuid was not converted */
+       e->ex_uniquifier = htonl(e->ex_uniquifier);
+       for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
+           e->ex_addrs[j] = htonl(e->ex_addrs[j]);
+       }
+    }
+    vldbwrite(addr, (char *)mhblockP, VL_ADDREXTBLK_SIZE);
+}
+
+void
 writeentry(afs_int32 addr, struct nvlentry *vlentryp)
 {
     int i;
@@ -492,61 +510,6 @@ writeentry(afs_int32 addr, struct nvlentry *vlentryp)
     vldbwrite(addr, (char *)vlentryp, sizeof(*vlentryp));
 }
 
-void
-readSIT(int base, int addr)
-{
-    int i, j, a;
-    char sitbuf[VL_ADDREXTBLK_SIZE];
-    struct extentaddr *extent;
-
-    if (!addr)
-       return;
-    vldbread(addr, sitbuf, VL_ADDREXTBLK_SIZE);
-    extent = (struct extentaddr *)sitbuf;
-
-    quiet_println("multihome info block: base %d\n", base);
-    if (base == 0) {
-       quiet_println("   count = %u\n", ntohl(extent->ex_count));
-       quiet_println("   flags = %u\n", ntohl(extent->ex_flags));
-       for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
-           quiet_println("   contaddrs[%d] = %u\n", i,
-                  ntohl(extent->ex_contaddrs[i]));
-       }
-    }
-    for (i = 1; i < VL_MHSRV_PERBLK; i++) {
-       /* should we skip this entry */
-       for (j = 0; j < VL_MAX_ADDREXTBLKS; j++) {
-           if (extent[i].ex_addrs[j])
-               break;
-       }
-       if (j >= VL_MAX_ADDREXTBLKS)
-           continue;
-
-       quiet_println("   base %d index %d:\n", base, i);
-
-       quiet_println("       afsuuid    = (%x %x %x /%d/%d/ /%x/%x/%x/%x/%x/%x/)\n",
-              ntohl(extent[i].ex_hostuuid.time_low),
-              ntohl(extent[i].ex_hostuuid.time_mid),
-              ntohl(extent[i].ex_hostuuid.time_hi_and_version),
-              ntohl(extent[i].ex_hostuuid.clock_seq_hi_and_reserved),
-              ntohl(extent[i].ex_hostuuid.clock_seq_low),
-              ntohl(extent[i].ex_hostuuid.node[0]),
-              ntohl(extent[i].ex_hostuuid.node[1]),
-              ntohl(extent[i].ex_hostuuid.node[2]),
-              ntohl(extent[i].ex_hostuuid.node[3]),
-              ntohl(extent[i].ex_hostuuid.node[4]),
-              ntohl(extent[i].ex_hostuuid.node[5]));
-       quiet_println("       uniquifier = %u\n", ntohl(extent[i].ex_uniquifier));
-       for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
-           a = ntohl(extent[i].ex_addrs[j]);
-           if (a) {
-               quiet_println("       %d.%d.%d.%d\n", (a >> 24) & 0xff,
-                      (a >> 16) & 0xff, (a >> 8) & 0xff, (a) & 0xff);
-           }
-       }
-    }
-}
-
 /*
  * Read each entry in the database:
  * Record what type of entry it is and its address in the record array.
@@ -597,7 +560,7 @@ ReadAllEntries(struct vlheader *header)
                    continue;
                }
                if (e) {
-                  log_error 
+                  log_error
                        (VLDB_CHECK_ERROR,"VLDB entry '%s' contains an unknown RW/RO index serverFlag\n",
                         vlentry.name);
                    e = 0;
@@ -686,12 +649,12 @@ FollowNameHash(struct vlheader *header)
             * checked it either above or below
             */
            if (record[rindex].addr != addr && record[rindex].addr) {
-               log_error       
+               log_error
                    (VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: addresses %ld and %u use same record slot %d\n",
                     record[rindex].addr, addr, rindex);
            }
            if (record[rindex].type & NH) {
-               log_error       
+               log_error
                    (VLDB_CHECK_ERROR,"Name Hash %d: Bad entry '%s': Already in the name hash\n",
                     i, vlentry.name);
                record[rindex].type |= MULTN;
@@ -713,7 +676,7 @@ FollowNameHash(struct vlheader *header)
 
            /* Hash the name and check if in correct hash table */
            if (NameHash(vlentry.name) != i) {
-               log_error       
+               log_error
                    (VLDB_CHECK_ERROR,"Name Hash %d: Bad entry '%s': Incorrect name hash chain (should be in %d)\n",
                     i, vlentry.name, NameHash(vlentry.name));
                record[rindex].type |= MULTN;
@@ -802,7 +765,7 @@ FollowIdHash(struct vlheader *header)
 
                /* Hash the id and check if in correct hash table */
                if (IdHash(vlentry.volumeId[i]) != j) {
-                  log_error 
+                  log_error
                        (VLDB_CHECK_ERROR,"%s Id Hash %d: Bad entry '%s': Incorrect Id hash chain (should be in %d)\n",
                         vtype(i), j, vlentry.name,
                         IdHash(vlentry.volumeId[i]));
@@ -842,7 +805,7 @@ FollowFreeChain(struct vlheader *header)
         addr = vlentry.nextIdHash[0]) {
        readentry(addr, &vlentry, &type);
        if (type != FR) {
-          log_error 
+          log_error
                (VLDB_CHECK_ERROR,"Free Chain %d: Bad entry at %u: Not a valid free vlentry (0x%x)\n",
                 count, addr, type);
            continue;
@@ -850,7 +813,7 @@ FollowFreeChain(struct vlheader *header)
 
        rindex = addr / sizeof(vlentry);
        if (record[rindex].addr != addr && record[rindex].addr) {
-          log_error 
+          log_error
                (VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: addresses %u and %ld use same record slot %d\n",
                 record[rindex].addr, addr, rindex);
        }
@@ -872,16 +835,16 @@ FollowFreeChain(struct vlheader *header)
  * Read each multihomed block and mark it as found in the record.
  * Read each entry in each multihomed block and mark the serveraddrs
  * array with the number of ip addresses found for this entry.
- * 
+ *
  * Then read the IpMappedAddr array in the header.
  * Verify that multihomed entries base and index are valid and points to
  * a good multhomed entry.
  * Mark the serveraddrs array with 1 ip address for regular entries.
- * 
- * By the end, the severaddrs array will have a 0 if the entry has no 
+ *
+ * By the end, the severaddrs array will have a 0 if the entry has no
  * IP addresses in it or the count of the number of IP addresses.
  *
- * The code does not verify if there are duplicate IP addresses in the 
+ * The code does not verify if there are duplicate IP addresses in the
  * list. The vlserver does this when a fileserver registeres itself.
  */
 void
@@ -890,7 +853,6 @@ CheckIpAddrs(struct vlheader *header)
     int mhblocks = 0;
     afs_int32 i, j, m, rindex;
     afs_int32 mhentries, regentries;
-    afs_uint32 caddrs[VL_MAX_ADDREXTBLKS];
     char mhblock[VL_ADDREXTBLK_SIZE];
     struct extentaddr *MHblock = (struct extentaddr *)mhblock;
     struct extentaddr *e;
@@ -903,46 +865,46 @@ CheckIpAddrs(struct vlheader *header)
        quiet_println("Check Multihomed blocks\n");
 
     if (header->SIT) {
-       /* Read the first MH block and from it, gather the 
+       /* Read the first MH block and from it, gather the
         * addresses of all the mh blocks.
         */
-       readMH(header->SIT, MHblock);
-       if (MHblock->ex_flags != VLCONTBLOCK) {
-          log_error 
+       readMH(header->SIT, 0, MHblock);
+       if (MHblock->ex_hdrflags != VLCONTBLOCK) {
+          log_error
                (VLDB_CHECK_ERROR,"Multihomed Block 0: Bad entry at %u: Not a valid multihomed block\n",
                 header->SIT);
        }
 
        for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
-           caddrs[i] = MHblock->ex_contaddrs[i];
+           mhinfo[i].addr = MHblock->ex_contaddrs[i];
        }
 
-       if (header->SIT != caddrs[0]) {
-          log_error 
+       if (header->SIT != mhinfo[0].addr) {
+          log_error
                (VLDB_CHECK_ERROR,"MH block does not point to self %u in header, %u in block\n",
-                header->SIT, caddrs[0]);
+                header->SIT, mhinfo[0].addr);
        }
 
        /* Now read each MH block and record it in the record array */
        for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
-           if (!caddrs[i])
+           if (!mhinfo[i].addr)
                continue;
 
-           readMH(caddrs[i], MHblock);
-           if (MHblock->ex_flags != VLCONTBLOCK) {
-               log_error       
+           readMH(mhinfo[i].addr, i, MHblock);
+           if (MHblock->ex_hdrflags != VLCONTBLOCK) {
+               log_error
                    (VLDB_CHECK_ERROR,"Multihomed Block 0: Bad entry at %u: Not a valid multihomed block\n",
                     header->SIT);
            }
 
-           rindex = caddrs[i] / sizeof(vlentry);
-           if (record[rindex].addr != caddrs[i] && record[rindex].addr) {
-               log_error       
+           rindex = mhinfo[i].addr / sizeof(vlentry);
+           if (record[rindex].addr != mhinfo[i].addr && record[rindex].addr) {
+               log_error
                    (VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: addresses %u and %u use same record slot %d\n",
-                    record[rindex].addr, caddrs[i], rindex);
+                    record[rindex].addr, mhinfo[i].addr, rindex);
            }
            if (record[rindex].type & FRC) {
-               log_error       
+               log_error
                    (VLDB_CHECK_ERROR,"MH Blocks Chain %d: Bad entry at %ld: Already a MH block\n",
                     i, record[rindex].addr);
                break;
@@ -951,34 +913,35 @@ CheckIpAddrs(struct vlheader *header)
 
            mhblocks++;
 
-           /* Read each entry in a multihomed block. 
+           /* Read each entry in a multihomed block.
             * Find the pointer to the entry in the IpMappedAddr array and
             * verify that the entry is good (has IP addresses in it).
             */
            mhentries = 0;
            for (j = 1; j < VL_MHSRV_PERBLK; j++) {
+               int first_ipindex = -1;
                e = (struct extentaddr *)&(MHblock[j]);
 
-               /* Search the IpMappedAddr array for the reference to this entry */
-               for (ipindex = 0; ipindex < MAXSERVERID; ipindex++) {
-                   if (((header->IpMappedAddr[ipindex] & 0xff000000) ==
-                        0xff000000)
-                       &&
-                       (((header->
-                          IpMappedAddr[ipindex] & 0x00ff0000) >> 16) == i)
-                       && ((header->IpMappedAddr[ipindex] & 0x0000ffff) ==
-                           j)) {
-                       break;
+               /* Search the IpMappedAddr array for all the references to this entry. */
+               /* Use the first reference for checking the ip addresses of this entry. */
+               for (ipindex = 0; ipindex <= MAXSERVERID; ipindex++) {
+                   if (((header->IpMappedAddr[ipindex] & 0xff000000) == 0xff000000)
+                       && (((header-> IpMappedAddr[ipindex] & 0x00ff0000) >> 16) == i)
+                       && ((header->IpMappedAddr[ipindex] & 0x0000ffff) == j)) {
+                       if (first_ipindex == -1) {
+                           first_ipindex = ipindex;
+                       } else {
+                           serverxref[ipindex] = first_ipindex;
+                       }
                    }
                }
-               if (ipindex >= MAXSERVERID)
-                   ipindex = -1;
-               else
+               ipindex = first_ipindex;
+               if (ipindex != -1)
                    serveraddrs[ipindex] = -1;
 
                if (memcmp(&e->ex_hostuuid, &nulluuid, sizeof(afsUUID)) == 0) {
                    if (ipindex != -1) {
-                       log_error       
+                       log_error
                            (VLDB_CHECK_ERROR,"Server Addrs index %d references null MH block %d, index %d\n",
                             ipindex, i, j);
                        serveraddrs[ipindex] = 0;       /* avoids printing 2nd error below */
@@ -997,7 +960,8 @@ CheckIpAddrs(struct vlheader *header)
                if (ipaddrs) {
                    mhentries++;
                    if (ipindex == -1) {
-                       log_error       
+                       mhinfo[i].orphan[j] = 1;
+                       log_error
                            (VLDB_CHECK_ERROR,"MH block %d, index %d: Not referenced by server addrs\n",
                             i, j);
                    } else {
@@ -1040,20 +1004,34 @@ CheckIpAddrs(struct vlheader *header)
                mhentries++;
                if (((header->IpMappedAddr[i] & 0x00ff0000) >> 16) >
                    VL_MAX_ADDREXTBLKS)
-                  log_error 
+                  log_error
                        (VLDB_CHECK_ERROR,"IP Addr for entry %d: Multihome block is bad (%d)\n",
                         i, ((header->IpMappedAddr[i] & 0x00ff0000) >> 16));
+               if (mhinfo[(header->IpMappedAddr[i] & 0x00ff0000) >> 16].addr == 0)
+                   log_error(VLDB_CHECK_ERROR,"IP Addr for entry %d: No such multihome block (%d)\n",
+                        i, ((header->IpMappedAddr[i] & 0x00ff0000) >> 16));
                if (((header->IpMappedAddr[i] & 0x0000ffff) > VL_MHSRV_PERBLK)
                    || ((header->IpMappedAddr[i] & 0x0000ffff) < 1))
-                   log_error 
+                   log_error
                        (VLDB_CHECK_ERROR,"IP Addr for entry %d: Multihome index is bad (%d)\n",
                         i, (header->IpMappedAddr[i] & 0x0000ffff));
                if (serveraddrs[i] == -1) {
-                   log_error 
+                   log_error
                        (VLDB_CHECK_WARNING,"warning: IP Addr for entry %d: Multihome entry has no ip addresses\n",
                         i);
                    serveraddrs[i] = 0;
                }
+               if (serverxref[i] != BADSERVERID) {
+                   log_error
+                       (VLDB_CHECK_WARNING,
+                       "warning: MH block %d, index %d is cross-linked by server numbers %d and %d.\n",
+                       (header->IpMappedAddr[i] & 0x00ff0000) >> 16,
+                       (header->IpMappedAddr[i] & 0x0000ffff),
+                       i, serverxref[i]);
+                   /* set addresses found/not found for this server number,
+                    * using the first index to the mh we found above. */
+                   serveraddrs[i] = serveraddrs[serverxref[i]];
+               }
                if (listservers) {
                    quiet_println("   Server ip addr %d = MH block %d, index %d\n",
                           i, (header->IpMappedAddr[i] & 0x00ff0000) >> 16,
@@ -1192,9 +1170,12 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
     }
 
     maxentries = (header.vital_header.eofPtr / sizeof(vlentry)) + 1;
-    record = (struct er *)malloc(maxentries * sizeof(struct er));
-    memset(record, 0, (maxentries * sizeof(struct er)));
+    record = calloc(maxentries, sizeof(struct er));
     memset(serveraddrs, 0, sizeof(serveraddrs));
+    memset(mhinfo, 0, sizeof(mhinfo));
+    for (i = 0; i <= MAXSERVERID; i++) {
+       serverxref[i] = BADSERVERID;
+    }
 
     /* Will fill in the record array of entries it found */
     ReadAllEntries(&header);
@@ -1213,17 +1194,14 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
     FollowFreeChain(&header);
 
     /* Now check the record we have been keeping for inconsistencies
-     * For valid vlentries, also check that the server we point to is 
+     * For valid vlentries, also check that the server we point to is
      * valid (the serveraddrs array).
      */
     if (verbose)
        quiet_println("Verify each volume entry\n");
     for (i = 0; i < maxentries; i++) {
-       int nextp = 0;
-       int reft = 0;
        int hash = 0;
         int nexthash = 0;
-       afs_uint32 *nextpp = NULL;
        char *which = NULL;
 
        if (record[i].type == 0)
@@ -1244,40 +1222,28 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
                       vlentry.name, record[i].addr);
 
            if (!(record[i].type & NH)) {
-               nextp = ADDR(vlentry.nextNameHash);
-               reft = REFN;
                hash = NameHash(vlentry.name);
-               nextpp = &vlentry.nextNameHash;
                which = "name";
                volidbuf[0]='\0';
                foundbad = 1;
            }
 
            if (vlentry.volumeId[0] && !(record[i].type & RWH)) {
-               nextp = ADDR(vlentry.nextIdHash[0]);
-               reft = REFRW;
                hash = IdHash(vlentry.volumeId[0]);
-               nextpp = &(vlentry.nextIdHash[0]);
                which = "RW";
                sprintf(volidbuf, "id %u ", vlentry.volumeId[0]);
                foundbad = 1;
            }
 
            if (vlentry.volumeId[1] && !(record[i].type & ROH)) {
-               nextp = ADDR(vlentry.nextIdHash[1]);
-               reft = REFRO;
                hash = IdHash(vlentry.volumeId[1]);
-               nextpp = &(vlentry.nextIdHash[1]);
                which = "RO";
                sprintf(volidbuf, "id %u ", vlentry.volumeId[1]);
                foundbad = 1;
            }
 
            if (vlentry.volumeId[2] && !(record[i].type & BKH)) {
-               nextp = ADDR(vlentry.nextIdHash[2]);
-               reft = REFBK;
                hash = IdHash(vlentry.volumeId[2]);
-               nextpp = &(vlentry.nextIdHash[2]);
                which = "BK";
                sprintf(volidbuf, "id %u ", vlentry.volumeId[2]);
                foundbad = 1;
@@ -1285,10 +1251,7 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
 
            if (!validVolumeAddr(vlentry.nextNameHash) ||
                record[ADDR(vlentry.nextNameHash)].type & MULTN) {
-               nextp = ADDR(vlentry.nextNameHash);
-               reft = REFN;
                hash = NameHash(vlentry.name);
-               nextpp = &vlentry.nextNameHash;
                which = "name";
                volidbuf[0]='\0';
                if (validVolumeAddr(vlentry.nextNameHash)) {
@@ -1303,10 +1266,7 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
 
            if (!validVolumeAddr(vlentry.nextIdHash[0]) ||
                record[ADDR(vlentry.nextIdHash[0])].type & MULTRW) {
-               nextp = ADDR(vlentry.nextIdHash[0]);
-               reft = REFRW;
                hash = IdHash(vlentry.volumeId[0]);
-               nextpp = &(vlentry.nextIdHash[0]);
                which = "RW";
                sprintf(volidbuf, "id %u ", vlentry.volumeId[0]);
                if (validVolumeAddr(vlentry.nextIdHash[0])) {
@@ -1321,10 +1281,7 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
 
            if (!validVolumeAddr(vlentry.nextIdHash[1]) ||
                record[ADDR(vlentry.nextIdHash[1])].type & MULTRO) {
-               nextp = ADDR(vlentry.nextIdHash[1]);
-               reft = REFRO;
                hash = IdHash(vlentry.volumeId[1]);
-               nextpp = &(vlentry.nextIdHash[1]);
                which = "RO";
                sprintf(volidbuf, "id %u ", vlentry.volumeId[1]);
                if (validVolumeAddr(vlentry.nextIdHash[1])) {
@@ -1339,10 +1296,7 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
 
            if (!validVolumeAddr(vlentry.nextIdHash[2]) ||
                record[ADDR(vlentry.nextIdHash[2])].type & MULTBK) {
-               nextp = ADDR(vlentry.nextIdHash[2]);
-               reft = REFBK;
                hash = IdHash(vlentry.volumeId[2]);
-               nextpp = &(vlentry.nextIdHash[2]);
                which = "BK";
                sprintf(volidbuf, "id %u ", vlentry.volumeId[2]);
                if (validVolumeAddr(vlentry.nextIdHash[2])) {
@@ -1366,39 +1320,39 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
            for (j = 0; j < NMAXNSERVERS; j++) {
                if ((vlentry.serverNumber[j] != 255)
                    && (serveraddrs[vlentry.serverNumber[j]] == 0)) {
-                  log_error 
+                  log_error
                        (VLDB_CHECK_ERROR,"Volume '%s', index %d points to empty server entry %d\n",
                         vlentry.name, j, vlentry.serverNumber[j]);
                }
            }
-       
+
            if (record[i].type & 0xffff0f00)
-               log_error       
+               log_error
                    (VLDB_CHECK_ERROR,"Volume '%s' id %u also found on other chains (0x%x)\n",
                     vlentry.name, vlentry.volumeId[0], record[i].type);
-           
+
            /* A free entry */
        } else if (record[i].type & FR) {
            if (!(record[i].type & FRC))
                log_error(VLDB_CHECK_ERROR,"Free vlentry at %ld not on free chain\n",
                       record[i].addr);
-           
+
            if (record[i].type & 0xfffffdf0)
-               log_error       
+               log_error
                    (VLDB_CHECK_ERROR,"Free vlentry at %ld also found on other chains (0x%x)\n",
                     record[i].addr, record[i].type);
-           
+
            /* A multihomed entry */
        } else if (record[i].type & MH) {
            if (!(record[i].type & MHC))
                log_error(VLDB_CHECK_ERROR,"Multihomed block at %ld is orphaned\n",
                       record[i].addr);
-           
+
            if (record[i].type & 0xfffffef0)
-               log_error       
+               log_error
                    (VLDB_CHECK_ERROR,"Multihomed block at %ld also found on other chains (0x%x)\n",
                     record[i].addr, record[i].type);
-           
+
        } else {
            log_error(VLDB_CHECK_ERROR,"Unknown entry type at %u (0x%x)\n", record[i].addr,
                   record[i].type);
@@ -1423,7 +1377,7 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
        if (record[i].type & VL) {
            readentry(record[i].addr, &vlentry, &type);
            if (!(record[i].type & REFN)) {
-               log_error(VLDB_CHECK_ERROR,"%d: Record %ld (type 0x%x) not in a name chain\n", i, 
+               log_error(VLDB_CHECK_ERROR,"%d: Record %ld (type 0x%x) not in a name chain\n", i,
                       record[i].addr, record[i].type);
            }
            if (vlentry.volumeId[0] && !(record[i].type & REFRW)) {
@@ -1431,11 +1385,11 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
                       record[i].addr, record[i].type);
            }
            if (vlentry.volumeId[1] && !(record[i].type & REFRO)) {
-               log_error(VLDB_CHECK_ERROR,"%d: Record %ld (type 0x%x) not in a RO chain\n", i, 
+               log_error(VLDB_CHECK_ERROR,"%d: Record %ld (type 0x%x) not in a RO chain\n", i,
                       record[i].addr, record[i].type);
            }
            if (vlentry.volumeId[2] && !(record[i].type & REFBK)) {
-               log_error(VLDB_CHECK_ERROR,"%d: Record %ld (type 0x%x) not in a BK chain\n", i, 
+               log_error(VLDB_CHECK_ERROR,"%d: Record %ld (type 0x%x) not in a BK chain\n", i,
                       record[i].addr, record[i].type);
            }
            if (fix) {
@@ -1495,6 +1449,30 @@ WorkerBee(struct cmd_syndesc *as, void *arock)
                writeentry(record[i].addr, &vlentry);
            }
        }
+       else if (record[i].type & MH) {
+           int block, index;
+           char mhblock[VL_ADDREXTBLK_SIZE];
+           struct extentaddr *MHblock = (struct extentaddr *)mhblock;
+
+           if (fix) {
+               for (block = 0; block < VL_MAX_ADDREXTBLKS; block++) {
+                   if (mhinfo[block].addr == record[i].addr)
+                       break;
+               }
+               if (block == VL_MAX_ADDREXTBLKS) {
+                   continue;  /* skip orphaned extent block */
+               }
+               readMH(record[i].addr, block, MHblock);
+               for (index = 0; index < VL_MHSRV_PERBLK; index++) {
+                   if (mhinfo[block].orphan[index]) {
+                       quiet_println("FIX: Removing unreferenced mh entry; block %d, index %d\n",
+                               block, index);
+                       memset(&(MHblock[index]), 0, sizeof(struct extentaddr));
+                   }
+               }
+               writeMH(record[i].addr, block, MHblock);
+           }
+       }
     }
     if (fix) {
        reportHashChanges(&header, oldnamehash, oldidhash);