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
10 /* ol_verify - online database verification */
12 #include <afsconfig.h>
13 #include <afs/param.h>
22 #include <netinet/in.h>
33 #include <sys/types.h>
37 #include "error_macros.h"
38 #include "budb_errs.h"
39 #include <afs/cellconfig.h>
40 #include "afs/audit.h"
46 * 1) volInfo structures refering to a volume of the same name are
47 * chained together, i.e. the volumes described differ in volid, partition
48 * etc. The structure at the head of this list (the sameNameChain) is
49 * treated specially. When a delete volInfo request is processed, heads
50 * are not deleted unless all other items on the sameNameChain are gone.
52 * The result is that volInfo (head) structures may be present
53 * even if no tape structures refer to them. These structures are
54 * unreachable in a top-down tree walk.
56 * 1) make the verify tolerant of errors. Want to get a summary statistic
57 * indicating how may dumps are lost and how many text blocks lost
58 * 2) Make the recreation instructions write out whatever is good. This
59 * is only for the off-line case.
62 /* flags associated with each structure. These are set and checked in
63 * the blockMap entries
66 #define MAP_DUMPHASH 1 /* dump name hash checked */
67 #define MAP_TAPEHASH 2 /* tape name hash checked */
68 #define MAP_VOLHASH 4 /* volume name hash checked */
69 #define MAP_IDHASH 8 /* dump id hash checked */
71 #define MAP_HASHES (MAP_DUMPHASH | MAP_TAPEHASH | MAP_VOLHASH | MAP_IDHASH)
73 #define MAP_FREE 0x10 /* item is free */
74 #define MAP_RECREATE 0x20
75 #define MAP_HTBLOCK 0x40 /* hash table block */
76 #define MAP_TAPEONDUMP 0x100
77 #define MAP_VOLFRAGONTAPE 0x200
78 #define MAP_VOLFRAGONVOL 0x400
79 #define MAP_VOLINFOONNAME 0x800
80 #define MAP_VOLINFONAMEHEAD 0x1000
81 #define MAP_TEXTBLOCK 0x2000 /* block of text */
82 #define MAP_APPENDEDDUMP 0x4000
84 /* one blockMap for every block in the database. Each element of the entries
85 * array describes the status of a data structure/entry in that block
89 struct blockHeader header; /* copy of the block header */
90 char free; /* on free list */
91 int nEntries; /* size of the entries arrays */
92 afs_uint32 entries[1]; /* describes each entry */
95 /* status for verify call */
97 char hostname[64]; /* host on which checked */
98 afs_int32 status; /* ok, not ok */
101 int nBlocks; /* total number of blocks in db */
103 struct misc_hash_stats { /* stats for hashing */
104 int max; /* longest chain length */
105 double avg; /* avg length */
106 double std_dev; /* standard deviation of length */
110 int errors; /* errors encountered */
111 int maxErrors; /* abort after this many errors */
112 int nBlocks; /* number of database blocks */
113 int nDump, nTape, nVolInfo, nVolFrag; /* counts of each type */
114 int nVolName; /* volInfos w/ head==0 */
115 int maxVolsPerVolInfo; /* maximum list lengths */
118 int maxVolInfosPerName;
120 int maxAppendsPerDump;
121 int freeLength[NBLOCKTYPES]; /* length of free lists */
122 int fullyFree[NBLOCKTYPES]; /* free blocks full of free entries */
123 int veryLongChain; /* length of chain to report */
124 int checkFragCount; /* report fragment count errors */
125 struct misc_hash_stats dumpName, dumpIden, tapeName, volName;
126 FILE *recreate; /* stream for recreate instructions */
128 struct misc_data *misc;
130 struct blockMap **blockMap = 0; /* initial block map */
132 /* describes number of entries for each type of block */
134 int blockEntries[NBLOCKTYPES] = {
140 1, /* hashTable_BLOCK */
144 int blockEntrySize[NBLOCKTYPES] = {
146 sizeof(((struct vfBlock *) NULL)->a[0]),
147 sizeof(((struct viBlock *) NULL)->a[0]),
148 sizeof(((struct tBlock *) NULL)->a[0]),
149 sizeof(((struct dBlock *) NULL)->a[0]),
153 char *typeName[NBLOCKTYPES] = {
162 int hashBlockType[HT_MAX_FUNCTION + 1] = {
170 /* Compatibility table for the bits in the blockMap. */
172 struct mapCompatability {
173 short trigger; /* these bits trigger this element */
175 MAP_FREE, MAP_HTBLOCK, MAP_DUMPHASH | MAP_IDHASH,
176 MAP_TAPEHASH | MAP_TAPEONDUMP, MAP_VOLINFOONNAME,
177 MAP_VOLINFONAMEHEAD | MAP_VOLHASH,
178 MAP_VOLFRAGONTAPE | MAP_VOLFRAGONVOL, MAP_TEXTBLOCK};
180 /* no. of entries in the mapC array */
181 int NMAPCs = (sizeof(mapC) / sizeof(mapC[0]));
183 /* for identifying stored textual information */
185 char *textName[TB_NUM] = {
191 extern int sizeFunctions[];
192 extern int nHTBuckets;
194 #define DBBAD BUDB_DATABASEINCONSISTENT
196 /* ------------------------------------
197 * supporting routines
198 * ------------------------------------
202 * increment the error count
205 * 1 - maximum errors exceeded
211 if (++miscData.errors >= miscData.maxErrors)
217 /* convertDiskAddress
218 * given a disk address, break it down into a block and entry index. These
219 * permit access to the block map information. The conversion process
220 * compares the supplied address with the alignment/type information
221 * stored in the block map.
224 * BUDB_ADDR - address alignment checks failed
228 checkDiskAddress(address, type, blockIndexPtr, entryIndexPtr)
229 unsigned long address;
241 /* This is safest way to handle totally bogus addresses (eg 0x80001234). */
242 if ((address < sizeof(db.h)) || (address >= ntohl(db.h.eofPtr)))
245 address -= sizeof(db.h);
246 index = address / BLOCKSIZE;
247 offset = address - (index * BLOCKSIZE);
248 if (offset % sizeof(afs_int32)) /* alignment check */
250 if (offset && (type > 0) && (type <= MAX_STRUCTURE_BLOCK_TYPE)) {
251 offset -= sizeof(struct blockHeader);
252 if ((offset < 0) || (offset % blockEntrySize[type]))
254 offset /= blockEntrySize[type];
255 if (offset >= blockEntries[type])
259 *blockIndexPtr = index;
261 *entryIndexPtr = offset;
265 /* ConvertDiskAddress
266 * given a disk address, break it down into a block and entry index. These
267 * permit access to the block map information. The conversion process
268 * compares the supplied address with the alignment/type information
269 * stored in the block map.
272 * BUDB_ADDR - address alignment checks failed
276 ConvertDiskAddress(address, blockIndexPtr, entryIndexPtr)
284 index = (address - sizeof(db.h)) / BLOCKSIZE;
285 type = blockMap[index]->header.type;
287 code = checkDiskAddress(address, type, blockIndexPtr, entryIndexPtr);
295 static char error[16];
297 if ((index < 0) || (index >= NBLOCKTYPES)) {
298 sprintf(error, "UNKNOWN_TYPE", index);
301 return (typeName[index]);
304 getDumpID(ut, tapePtr, dumpID)
305 struct tape *tapePtr;
312 code = dbread(ut, ntohl(tapePtr->dump), &d, sizeof(d));
314 *dumpID = ntohl(d.id);
317 /* ------------------------------------
318 * verification routines - structure specific
319 * ------------------------------------
323 * Follow the tapes entries hanging off of a dump and verify they belong
327 verifyDumpEntry(ut, dumpAddr, ai, ao, dumpPtr)
328 struct ubik_trans *ut;
331 struct dump *dumpPtr;
334 afs_int32 tapeAddr, tapeCount = 0, volCount = 0, appDumpCount = 0;
335 afs_int32 appDumpAddr, appDumpIndex, appDumpOffset;
337 int tapeIndex, tapeOffset, ccheck = 1;
338 afs_int32 code = 0, tcode;
339 int dumpIndex, dumpOffset;
341 tcode = ConvertDiskAddress(dumpAddr, &dumpIndex, &dumpOffset);
343 Log("verifyDumpEntry: Invalid dump entry addr 0x%x\n", dumpAddr);
349 /* Step though list of tapes hanging off of this dump */
350 for (tapeAddr = ntohl(dumpPtr->firstTape); tapeAddr;
351 tapeAddr = ntohl(tape.nextTape)) {
352 tcode = ConvertDiskAddress(tapeAddr, &tapeIndex, &tapeOffset);
354 Log("verifyDumpEntry: Invalid tape entry addr 0x%x (on DumpID %u)\n", tapeAddr, ntohl(dumpPtr->id));
355 Log(" Skipping remainder of tapes in dump\n");
362 tcode = dbread(ut, tapeAddr, &tape, sizeof(tape));
366 if (ntohl(tape.dump) != dumpAddr) {
369 getDumpID(ut, &tape, &did);
370 Log("verifyDumpEntry: Tape '%s' (addr 0x%x) doesn't point to\n",
371 tape.name, tapeAddr);
372 Log(" dumpID %u (addr 0x%x). Points to DumpID %u (addr 0x%x)\n", ntohl(dumpPtr->id), dumpAddr, did, ntohl(tape.dump));
377 /* Check if this tape entry has been examine already */
378 if (blockMap[tapeIndex]->entries[tapeOffset] & MAP_TAPEONDUMP) {
379 Log("verifyDumpEntry: Tape '%s' (addr 0x%x) on multiple dumps\n",
380 tape.name, tapeAddr);
384 blockMap[tapeIndex]->entries[tapeOffset] |= MAP_TAPEONDUMP;
387 volCount += ntohl(tape.nVolumes);
390 if (ccheck && (ntohl(dumpPtr->nVolumes) != volCount)) {
391 Log("verifyDumpEntry: DumpID %u (addr 0x%x) volume count of %d is wrong (should be %d)\n", ntohl(dumpPtr->id), dumpAddr, ntohl(dumpPtr->nVolumes), volCount);
396 if (volCount > misc->maxVolsPerDump)
397 misc->maxVolsPerDump = volCount;
398 if (tapeCount > misc->maxTapesPerDump)
399 misc->maxTapesPerDump = tapeCount;
401 /* If this is an initial dump, then step though list of appended dumps
402 * hanging off of this dump.
404 if (ntohl(dumpPtr->initialDumpID) == 0) {
405 for (appDumpAddr = ntohl(dumpPtr->appendedDumpChain); appDumpAddr;
406 appDumpAddr = ntohl(appDump.appendedDumpChain)) {
409 ConvertDiskAddress(appDumpAddr, &appDumpIndex,
412 Log("verifyDumpEntry: Invalid appended dump entry addr 0x%x\n", appDumpAddr);
413 Log("Skipping remainder of appended dumps\n");
419 /* Read the appended dump in */
420 tcode = dbread(ut, appDumpAddr, &appDump, sizeof(appDump));
424 /* Verify that it points to the parent dump */
425 if (ntohl(appDump.initialDumpID) != ntohl(dumpPtr->id)) {
426 Log("verifyDumpEntry: DumpID %u (addr 0x%x) initial DumpID incorrect (is %u, should be %u)\n", ntohl(appDump.id), appDumpAddr, ntohl(appDump.initialDumpID), ntohl(dumpPtr->id));
431 /* Check if this appended dump entry has been examined already */
432 if (blockMap[appDumpIndex]->
433 entries[appDumpOffset] & MAP_APPENDEDDUMP) {
434 Log("verifyDumpEntry: DumpID %u (addr %u) is on multiple appended dump chains\n", ntohl(appDump.id), appDumpAddr);
435 Log("Skipping remainder of appended dumps\n");
440 blockMap[appDumpIndex]->entries[appDumpOffset] |=
447 if (appDumpCount > misc->maxAppendsPerDump)
448 misc->maxAppendsPerDump = appDumpCount;
457 * Follw the volume fragments hanging off of a tape entry and verify
458 * they belong to the tape.
461 verifyTapeEntry(ut, tapeAddr, ai, ao, tapePtr)
462 struct ubik_trans *ut;
465 struct tape *tapePtr;
467 int volCount = 0, ccheck = 1;
468 afs_int32 volFragAddr;
469 int blockIndex, entryIndex;
470 struct volFragment volFragment;
471 afs_int32 code = 0, tcode;
473 for (volFragAddr = ntohl(tapePtr->firstVol); volFragAddr;
474 volFragAddr = ntohl(volFragment.sameTapeChain)) {
475 tcode = ConvertDiskAddress(volFragAddr, &blockIndex, &entryIndex);
479 getDumpID(ut, tapePtr, &did);
480 Log("verifyTapeEntry: Invalid volFrag addr 0x%x (on tape '%s' DumpID %u)\n", volFragAddr, tapePtr->name, did);
481 Log(" Skipping remainder of volumes on tape\n");
488 tcode = dbread(ut, volFragAddr, &volFragment, sizeof(volFragment));
492 if (ntohl(volFragment.tape) != tapeAddr) {
495 getDumpID(ut, tapePtr, &did);
496 Log("verifyTapeEntry: VolFrag (addr 0x%x) doesn't point to \n",
498 Log(" tape '%s' DumpID %u (addr 0x%x). Points to addr 0x%x\n",
499 tapePtr->name, did, tapeAddr, ntohl(volFragment.tape));
504 /* Has this volume fragment already been examined */
505 if (blockMap[blockIndex]->entries[entryIndex] & MAP_VOLFRAGONTAPE) {
506 Log("verifyTapeEntry: VolFrag (addr %d) on multiple tapes\n",
511 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLFRAGONTAPE;
516 /* now check computed vs. recorded volume counts */
517 if (ccheck && (ntohl(tapePtr->nVolumes) != volCount)) {
520 getDumpID(ut, tapePtr, &did);
521 Log("verifyTapeEntry: Tape '%s' DumpID %u (addr 0x%x) volFrag count of %d is wrong (should be %d)\n", tapePtr->name, did, tapeAddr, ntohl(tapePtr->nVolumes), volCount);
526 if (volCount > misc->maxVolsPerTape)
527 misc->maxVolsPerTape = volCount;
536 * volume fragments are the lowest leaf describing a dump (nothing hangs off of it).
537 * So no check is done agaist it.
540 verifyVolFragEntry(ut, va, ai, ao, v)
541 struct ubik_trans *ut;
544 struct volFragment *v;
550 /* verifyVolInfoEntry
551 * Follow the volume fragments hanging off of a volinfo structure and
552 * verify they belong to the volinfo structure.
553 * If the volinfo structure is at the head of the same name chain, then
554 * also verify all entries are also on the chain.
557 verifyVolInfoEntry(ut, volInfoAddr, ai, ao, volInfo)
558 struct ubik_trans *ut;
559 afs_int32 volInfoAddr;
561 struct volInfo *volInfo;
563 int volCount = 0, ccheck = 1;
565 afs_int32 volFragAddr;
566 int blockIndex, entryIndex, bindex, eindex;
567 struct volFragment volFragment;
568 afs_int32 code = 0, tcode;
570 /* check each fragment attached to this volinfo structure */
571 for (volFragAddr = ntohl(volInfo->firstFragment); volFragAddr;
572 volFragAddr = ntohl(volFragment.sameNameChain)) {
573 tcode = ConvertDiskAddress(volFragAddr, &blockIndex, &entryIndex);
575 Log("verifyVolInfoEntry: Invalid volFrag entry addr 0x%x (on volume '%s')\n", volFragAddr, volInfo->name);
576 Log(" Skipping remainder of volumes on tape\n");
583 tcode = dbread(ut, volFragAddr, &volFragment, sizeof(volFragment));
587 if (ntohl(volFragment.vol) != volInfoAddr) {
588 Log("verifyVolInfoEntry: volFrag (addr 0x%x) doesn't point to \n",
590 Log(" volInfo '%s' (addr 0x%x). Points to addr 0x%x\n",
591 volInfo->name, volInfoAddr, ntohl(volFragment.vol));
596 /* volume fragment already on a volinfo chain? */
597 if (blockMap[blockIndex]->entries[entryIndex] & MAP_VOLFRAGONVOL) {
598 Log("verifyVolInfoEntry: VolFrag (addr %d) on multiple volInfo chains\n", volFragAddr);
602 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLFRAGONVOL;
607 /* check computed vs. recorded number of fragments */
608 if (ccheck && misc->checkFragCount
609 && (ntohl(volInfo->nFrags) != volCount)) {
610 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) volFrag count of %d is wrong (should be %d)\n", volInfo->name, volInfoAddr, ntohl(volInfo->nFrags), volCount);
615 if (volCount > misc->maxVolsPerVolInfo)
616 misc->maxVolsPerVolInfo = volCount;
618 /* Check that all volInfo structures with same name point to the same
619 * head. If sameNameHead == 0, this is the head structure so we check,
622 if (volInfo->sameNameHead == 0) { /*i */
623 int viCount = 1; /* count this one */
627 for (tviAddr = ntohl(volInfo->sameNameChain); tviAddr;
628 tviAddr = ntohl(tvi.sameNameChain)) {
630 tcode = ConvertDiskAddress(tviAddr, &blockIndex, &entryIndex);
632 Log("verifyVolInfoEntry: Invalid volInfo entry %s addr 0x%x\n", volInfo->name, tviAddr);
633 Log(" Skipping remainder of volumes on same name chain\n");
640 tcode = dbread(ut, tviAddr, &tvi, sizeof(tvi));
644 if (ntohl(tvi.sameNameHead) != volInfoAddr) {
645 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) doesn't point to \n", volInfo->name, tviAddr);
646 Log(" head of sameName volInfo (addr 0x%x). Points to addr 0x%x\n", volInfoAddr, ntohl(tvi.sameNameHead));
651 if (blockMap[blockIndex]->entries[entryIndex] & MAP_VOLINFOONNAME) {
652 Log("verifyVolInfoEntry: VolInfo (addr 0x%x) on multiple same name chains\n", tviAddr);
656 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLINFOONNAME;
659 /* select the passed in structure flags */
660 if (blockMap[ai]->entries[ao] & MAP_VOLINFONAMEHEAD) {
661 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) is name head multiple times\n", volInfo->name, volInfoAddr);
665 blockMap[ai]->entries[ao] |= MAP_VOLINFONAMEHEAD;
667 if (viCount > misc->maxVolInfosPerName)
668 misc->maxVolInfosPerName = viCount;
679 /* ------------------------------------
680 * verification routines - general
681 * ------------------------------------
685 * Read each block header of every 2K block and remember it in our global
686 * blockMap array. Also check that the type of block is good.
690 struct ubik_trans *ut;
695 struct blockMap *ablockMap = 0;
698 afs_int32 code = 0, tcode;
700 /* Remember every header of every block in the database */
701 for (i = 0; i < nBlocks; i++) {
702 /* To avoid the call from timing out, do a poll every 256 blocks */
704 /* read the block header */
705 blockAddr = sizeof(db.h) + (i * BLOCKSIZE);
706 tcode = dbread(ut, blockAddr, (char *)&block.h, sizeof(block.h));
710 /* check the block type */
711 blocktype = block.h.type; /* char */
712 if ((blocktype < 0) || (blocktype >= NBLOCKTYPES)) {
713 Log("Block (index %d addr %d) has invalid type of %d\n", i,
714 blockAddr, blocktype);
715 ERROR(BUDB_BLOCKTYPE);
718 /* allocate the block map memory */
720 sizeof(*ablockMap) + (blockEntries[blocktype] -
721 1) * sizeof(ablockMap->entries[0]);
722 ablockMap = (struct blockMap *)malloc(bmsize);
725 memset(ablockMap, 0, bmsize);
727 ablockMap->nEntries = blockEntries[blocktype];
729 /* save the block header in the block map */
730 memcpy(&ablockMap->header, &block.h, sizeof(ablockMap->header));
731 blockMap[i] = ablockMap;
738 int minvols, maxvols, ttlvols;
740 /* verifyHashTableBlock
741 * Take a 2K hash table block and traverse its entries. Make sure each entry
742 * is of the correct type for the hash table, is hashed into the correct
743 * entry and is not threaded on multiple lists.
746 verifyHashTableBlock(ut, mhtPtr, htBlockPtr, old, length, index, mapBit)
747 struct ubik_trans *ut;
748 struct memoryHashTable *mhtPtr;
749 struct htBlock *htBlockPtr;
751 afs_int32 length; /* size of whole hash table */
752 int index; /* base index of this block */
755 int type; /* hash table type */
756 int entrySize; /* hashed entry size */
757 int blockType; /* block type for this hash table */
758 int blockIndex, entryIndex;
759 char entry[sizeof(struct block)];
761 int hash; /* calculated hash value for entry */
763 afs_int32 code = 0, tcode;
765 type = ntohl(mhtPtr->ht->functionType);
766 entrySize = sizeFunctions[type];
767 blockType = hashBlockType[type];
769 /* step through this hash table block, being careful to stop
770 * before the end of the overall hash table
773 for (i = 0; (i < nHTBuckets) && (index < length); i++, index++) { /*f */
774 entryAddr = ntohl(htBlockPtr->bucket[i]);
776 /* if this is the old hash table, all entries below the progress mark
777 * should have been moved to the new hash table
779 if (old && (index < mhtPtr->progress) && entryAddr) {
780 Log("Old hash table not empty (entry %d) below progress (%d)\n",
781 i, mhtPtr->progress);
786 /* now walk down the chain of each bucket */
787 for (count = 0; entryAddr; count++) { /*w */
788 tcode = ConvertDiskAddress(entryAddr, &blockIndex, &entryIndex);
790 Log("verifyHashTableBlock: Invalid hash table chain addr 0x%x\n", entryAddr);
791 Log(" Skipping remainder of bucket %d\n", index);
798 if (blockMap[blockIndex]->header.type != blockType) {
799 Log("Hash table chain (block index %d) incorrect\n",
801 Log(" Table type %d traverses block type %d\n", blockType,
802 blockMap[blockIndex]->header.type);
803 Log(" Skipping remainder of bucket %d\n", index);
809 if (dbread(ut, entryAddr, &entry[0], entrySize))
812 hash = ht_HashEntry(mhtPtr, &entry[0]) % length;
813 if (hash != index) { /* if it hashed to some other place */
814 Log("Entry (addr 0x%x) bucket %d, should be hashed into bucket %d\n", entryAddr, index, hash);
819 /* check if entry has been examined */
820 if (blockMap[blockIndex]->entries[entryIndex] & mapBit) {
821 Log("Entry (addr 0x%x) multiply referenced\n", entryAddr);
825 blockMap[blockIndex]->entries[entryIndex] |= mapBit;
827 entryAddr = ntohl(*((dbadr *) (entry + mhtPtr->threadOffset)));
830 /* Log("Bucket %4d contains %d entries\n", index+1, count); */
843 * Read each 2K block a hashtable has (both its old hastable and
844 * new hashtable) and verify the block has not been read before.
845 * Will also make call to verify entries within each 2K block of
849 verifyHashTable(ut, mhtPtr, mapBit)
850 struct ubik_trans *ut;
851 struct memoryHashTable *mhtPtr;
854 struct hashTable *htPtr = mhtPtr->ht;
856 struct htBlock hashTableBlock;
857 int tableLength; /* # entries */
858 int hashblocks; /* # blocks */
859 dbadr tableAddr; /* disk addr of hash block */
860 int blockIndex, entryIndex;
863 afs_int32 code = 0, tcode;
865 extern int nHTBuckets; /* # buckets in a hash table */
867 LogDebug(4, "Htable: length %d oldlength %d progress %d\n",
868 mhtPtr->length, mhtPtr->oldLength, mhtPtr->progress);
870 /* check both old and current tables */
871 for (old = 0; old <= 1; old++) { /*fo */
872 tableLength = (old ? mhtPtr->oldLength : mhtPtr->length);
873 if (tableLength == 0)
875 tableAddr = (old ? ntohl(htPtr->oldTable) : ntohl(htPtr->table));
877 maxvols = ttlvols = 0;
879 /* follow the chain of hashtable blocks - avoid infinite loops */
880 hashblocks = ((tableLength - 1) / nHTBuckets) + 1; /* numb of 2K hashtable blocks */
881 for (i = 0; (tableAddr && (i < hashblocks + 5)); i++) {
882 tcode = ConvertDiskAddress(tableAddr, &blockIndex, &entryIndex);
884 Log("verifyHashTable: Invalid hash table chain addr 0x%x\n",
886 Log(" Skipping remainder of hash table chain\n");
893 if (blockMap[blockIndex]->header.type != hashTable_BLOCK) {
894 Log("Hashtable block (index %d addr 0x%x) hashtype %d - type %d, expected type %d\n", i + 1, tableAddr, ntohl(htPtr->functionType), blockMap[blockIndex]->header.type, hashTable_BLOCK);
895 Log(" Skipping remainder of hash table chain\n");
897 ERROR(BUDB_BLOCKTYPE);
901 /* check if we've examined this block before */
902 /* mark this (hash table) block as examined */
903 if (blockMap[blockIndex]->entries[entryIndex] & MAP_HTBLOCK) {
904 Log("Hash table block (index %d addr 0x%x) multiple ref\n",
907 ERROR(BUDB_DATABASEINCONSISTENT);
909 blockMap[blockIndex]->entries[entryIndex] |= MAP_HTBLOCK;
911 /* Read the actual hash table block */
913 dbread(ut, tableAddr, &hashTableBlock,
914 sizeof(hashTableBlock));
918 /* Verify its entries */
920 verifyHashTableBlock(ut, mhtPtr, &hashTableBlock, old,
921 tableLength, (i * nHTBuckets), mapBit);
925 tableAddr = ntohl(hashTableBlock.h.next);
928 /* Verify numb hash blocks is as it says */
929 if (i != hashblocks) {
930 Log("Incorrect number of hash chain blocks: %d (expected %d), hashtype %d\n", i, hashblocks, ntohl(htPtr->functionType));
932 ERROR(BUDB_DATABASEINCONSISTENT);
936 Log("%d entries; %u buckets; min = %d; max = %d\n", ttlvols,
937 tableLength, minvols, maxvols);
945 * do a linear walk of all the blocks. Check each block of structures
946 * to see if the actual free matches the recorded free. Also check
947 * the integrity of each allocated structure.
950 verifyEntryChains(ut)
951 struct ubik_trans *ut;
955 int blockIndex, entryIndex;
956 char entry[sizeof(struct block)];
961 static afs_int32(*checkEntry[NBLOCKTYPES]) ()
964 verifyVolFragEntry, verifyVolInfoEntry, verifyTapeEntry, verifyDumpEntry, 0 /* text block */
967 for (blockIndex = 0; blockIndex < nBlocks; blockIndex++) { /*f */
968 /* ignore non-structure or blocks with invalid type */
969 type = blockMap[blockIndex]->header.type;
970 if ((type <= 0) || (type > MAX_STRUCTURE_BLOCK_TYPE))
973 entrySize = blockEntrySize[type];
976 for (entryIndex = 0; entryIndex < blockMap[blockIndex]->nEntries; entryIndex++) { /*f */
978 sizeof(db.h) + (blockIndex * BLOCKSIZE) +
979 sizeof(struct blockHeader) + (entryIndex * entrySize);
980 if (dbread(ut, offset, &entry[0], entrySize))
983 /* check if entry is free by looking at the first "afs_int32" of the structure */
984 if (*((afs_int32 *) & entry[0]) == 0) { /* zero is free */
985 /* Is it on any hash chain? */
986 if (blockMap[blockIndex]->entries[entryIndex] & MAP_HASHES) {
987 Log("Entry: blockindex %d, entryindex %d - marked free but hashed 0x%x\n", blockIndex, entryIndex, blockMap[blockIndex]->entries[entryIndex]);
992 blockMap[blockIndex]->entries[entryIndex] |= MAP_FREE;
995 /* check the entry itself for consistency */
997 (*(checkEntry[type])) (ut, offset, blockIndex, entryIndex,
1004 /* check computed free with recorded free entries */
1005 if (nFree != ntohs(blockMap[blockIndex]->header.nFree)) {
1006 Log("Block (index %d) free count %d has %d free structs\n",
1007 blockIndex, ntohs(blockMap[blockIndex]->header.nFree), nFree);
1022 int blockIndex, entryIndex;
1026 /* for each free list */
1027 for (i = 0; i < NBLOCKTYPES; i++) {
1028 misc->fullyFree[i] = misc->freeLength[i] = 0;
1030 for (addr = ntohl(db.h.freePtrs[i]); addr;
1031 addr = ntohl(blockMap[blockIndex]->header.next)) {
1032 misc->freeLength[i]++;
1034 code = ConvertDiskAddress(addr, &blockIndex, &entryIndex);
1035 if (code || (entryIndex != 0)) {
1036 Log("verifyFreeLists: Invalid free chain addr 0x%x in %s free chain\n", addr, TypeName(i));
1037 Log(" Skipping remainder of free chain\n");
1044 /* check block type */
1045 if (blockMap[blockIndex]->header.type != i) {
1046 Log("verifyFreeLists: Found %s type in %s free chain\n",
1047 TypeName(blockMap[blockIndex]->header.type), TypeName(i),
1053 /* If entire block isn't free, check if count of free entries is ok */
1054 nFree = ntohs(blockMap[blockIndex]->header.nFree);
1055 if (i != free_BLOCK) {
1056 if ((nFree <= 0) || (nFree > blockEntries[i])) {
1057 Log("verifyFreeLists: Illegal free count %d on %s free chain\n", nFree, TypeName(i));
1060 } else if (nFree == blockEntries[i]) {
1061 misc->fullyFree[i]++;
1065 /* Check if already examined the block */
1066 if (blockMap[blockIndex]->free) {
1067 Log("verifyFreeLists: %s free chain block at addr 0x%x multiply threaded\n", TypeName(i), addr);
1071 blockMap[blockIndex]->free++;
1079 * Examines each entry to make sure it was traversed appropriately by
1080 * checking the bits for compatibility.
1085 int blockIndex, entryIndex, i, entrySize, type, bits;
1088 for (blockIndex = 0; blockIndex < nBlocks; blockIndex++) {
1089 /* If no entries in this block, then the block should be marked free */
1090 if ((blockMap[blockIndex]->nEntries == 0)
1091 && !blockMap[blockIndex]->free) {
1092 Log("verifyMapBits: Orphan free block (index %d)\n", blockIndex);
1097 /* check each entry */
1098 for (entryIndex = 0; entryIndex < blockMap[blockIndex]->nEntries; entryIndex++) { /*f */
1099 if ((entryIndex % 1024) == 0)
1102 bits = blockMap[blockIndex]->entries[entryIndex];
1104 for (i = 0; i < NMAPCs; i++)
1105 if ((bits & mapC[i].trigger) == mapC[i].trigger)
1111 type = blockMap[blockIndex]->header.type;
1112 entrySize = blockEntrySize[type];
1114 sizeof(db.h) + (blockIndex * BLOCKSIZE) +
1115 sizeof(struct blockHeader) + (entryIndex * entrySize);
1117 sprintf(logstr, "%s entry Block %d, Entry %d, (addr 0x%x)",
1118 TypeName(type), blockIndex, entryIndex, offset);
1121 strcat(logstr, ": An orphaned entry");
1122 if (bits & MAP_FREE)
1123 strcat(logstr, ": A valid free block");
1124 if (bits & MAP_HTBLOCK)
1125 strcat(logstr, ": A valid hash-table block");
1126 if (bits & MAP_TEXTBLOCK)
1127 strcat(logstr, ": A valid text block");
1128 if (bits & (MAP_DUMPHASH | MAP_IDHASH)) {
1129 if (!(bits & MAP_DUMPHASH))
1131 ": A dump not on dump-name hash-chain");
1132 else if (!(bits & MAP_IDHASH))
1133 strcat(logstr, ": A dump not on dump-id hash-chain");
1135 strcat(logstr, ": A valid dump entry");
1137 if (bits & (MAP_TAPEHASH | MAP_TAPEONDUMP)) {
1138 if (!(bits & MAP_TAPEHASH))
1140 ": A tape not on tape-name hash-chain");
1141 else if (!(bits & MAP_TAPEONDUMP))
1142 strcat(logstr, ": A tape not associated with a dump");
1144 strcat(logstr, ": A valid tape entry");
1146 if (bits & MAP_VOLINFOONNAME)
1148 ": A valid volInfo on a volume-name chain");
1149 if (bits & (MAP_VOLINFONAMEHEAD | MAP_VOLHASH)) {
1150 if (!(bits & MAP_VOLINFONAMEHEAD))
1152 ": A volInfo not the head of a volume-name hash-chain");
1153 else if (!(bits & MAP_VOLHASH))
1155 ": A volInfo not on volume-name hash-chain");
1158 ": A valid volInfo in volume-name hash-chain");
1160 if (bits & (MAP_VOLFRAGONTAPE | MAP_VOLFRAGONVOL)) {
1161 if (!(bits & MAP_VOLFRAGONTAPE))
1163 ": A volFrag not associated with a tape");
1164 else if (!(bits & MAP_VOLFRAGONVOL))
1166 ": A volFrag not associated with a volume");
1168 strcat(logstr, ": A valid volFrag entry");
1170 Log("%s\n", logstr);
1183 struct ubik_trans *ut;
1187 extern afs_int32 verifyTextChain();
1189 /* check each of the text types in use */
1190 for (i = 0; i < TB_NUM; i++) {
1191 Log("Verify Text: %s", textName[i]);
1192 code = verifyTextChain(ut, &db.h.textBlock[i]);
1200 * check the integrity of a text chain. Also checks the new chain.
1203 verifyTextChain(ut, tbPtr)
1204 struct ubik_trans *ut;
1205 struct textBlock *tbPtr;
1208 int blockIndex, entryIndex;
1212 afs_int32 code = 0, tcode;
1214 for (new = 0; new < 2; new++) {
1216 blockAddr = ntohl(tbPtr->textAddr);
1219 (new ? ntohl(tbPtr->newTextAddr) : ntohl(tbPtr->textAddr));
1220 blockAddr; blockAddr = ntohl(block.h.next)) {
1221 tcode = ConvertDiskAddress(blockAddr, &blockIndex, &entryIndex);
1223 Log("verifyTextChain: Invalid %s text block addr 0x%x\n",
1224 (new ? "new" : ""), blockAddr);
1225 Log(" Skipping remainder of text chain\n");
1231 tcode = dbread(ut, blockAddr, &block, sizeof(block));
1235 if (blockMap[blockIndex]->entries[entryIndex] & MAP_TEXTBLOCK) {
1236 Log("verifyTextChain: Text block (addr 0x%x) multiply chained\n", blockAddr);
1240 blockMap[blockIndex]->entries[entryIndex] |= MAP_TEXTBLOCK;
1242 size += BLOCK_DATA_SIZE;
1245 if (ntohl(new ? tbPtr->newsize : tbPtr->size) > size) {
1246 Log("verifyTextChain: Text block %s size %d > computed capacity %d\n", (new ? "new" : ""), ntohl(new ? tbPtr->newsize : tbPtr->size), size);
1256 /* -----------------------------------------
1257 * verification driver routines
1258 * -----------------------------------------
1262 * Check the integrity of the database
1266 verifyDatabase(ut, recreateFile)
1267 struct ubik_trans *ut;
1268 FILE *recreateFile; /* not used */
1272 afs_int32 code = 0, tcode;
1274 extern int nBlocks; /* no. blocks in database */
1275 extern struct ubik_dbase *BU_dbase;
1277 /* clear verification statistics */
1279 memset(&miscData, 0, sizeof(miscData));
1282 miscData.maxErrors = 1000000;
1284 miscData.maxErrors = 50; /* Catch the first 50 errors */
1286 miscData.veryLongChain = 0;
1287 miscData.checkFragCount = 1; /* check frags */
1290 eof = ntohl(db.h.eofPtr);
1291 eof -= sizeof(db.h); /* subtract header */
1292 nBlocks = eof / BLOCKSIZE;
1294 Log("Verify of backup database started\n");
1295 Log("Database is %u. %d blocks of %d Bytes\n", eof, nBlocks, BLOCKSIZE);
1297 if ((eof < 0) || (nBlocks * BLOCKSIZE != eof)) {
1298 Log("Database eofPtr (%d) bad, blocksize %d\n", eof, BLOCKSIZE);
1302 /* set size of database */
1303 miscData.nBlocks = nBlocks;
1306 ERROR(0); /* Nothing to check? */
1308 /* construct block map - first level is the array of pointers */
1309 bmsize = nBlocks * sizeof(struct blockMap *);
1310 blockMap = (struct blockMap **)malloc(bmsize);
1313 memset(blockMap, 0, bmsize);
1315 /* verify blocks and construct the block map */
1316 Log("Read header of every block\n");
1317 tcode = verifyBlocks(ut);
1321 /* check the various hash tables */
1322 Log("Verify volume name hash table\n");
1323 tcode = verifyHashTable(ut, &db.volName, MAP_VOLHASH);
1327 Log("Verify tape name hash table\n");
1328 tcode = verifyHashTable(ut, &db.tapeName, MAP_TAPEHASH);
1332 Log("Verify dump name hash table\n");
1333 tcode = verifyHashTable(ut, &db.dumpName, MAP_DUMPHASH);
1337 Log("Verify dump id hash table\n");
1338 tcode = verifyHashTable(ut, &db.dumpIden, MAP_IDHASH);
1342 /* check the entry chains */
1343 Log("Verify all blocks and entries\n");
1344 tcode = verifyEntryChains(ut);
1348 /* check text blocks - Log message in verifyText */
1349 tcode = verifyText(ut);
1353 /* check free list */
1354 Log("Verify Free Lists\n");
1355 tcode = verifyFreeLists();
1359 /* check entry map bit compatibility */
1361 Log("Verify Map bits\n");
1362 tcode = verifyMapBits();
1367 /* free the block map */
1368 if (blockMap != 0) {
1371 /* free all the individual maps */
1372 for (i = 0; i < nBlocks; i++) {
1377 /* free the pointer array */
1383 Log("# 2K database blocks = %d\n", miscData.nBlocks);
1384 Log("# Dump entries found = %d. 3 dumps per block\n",
1386 Log(" max tapes on a dump = %d\n", miscData.maxTapesPerDump);
1387 Log(" max volumes on a dump = %d\n", miscData.maxVolsPerDump);
1388 Log(" max appends on a dump = %d\n", miscData.maxAppendsPerDump);
1389 Log(" # Blocks with space = %d\n", miscData.freeLength[4]);
1390 Log(" # of those fully free = %d\n", miscData.fullyFree[4]);
1391 Log("# Tape entries found = %d. 20 tapes per block\n",
1393 Log(" max volumes on a tape = %d\n", miscData.maxVolsPerTape);
1394 Log(" # Blocks with space = %d\n", miscData.freeLength[3]);
1395 Log(" # of those fully free = %d\n", miscData.fullyFree[3]);
1396 Log("# VolInfo entries found = %d. 20 volInfos per block\n",
1398 Log(" # head of sameNameCh = %d\n", miscData.nVolName);
1399 Log(" max on a sameNameCh = %d\n", miscData.maxVolInfosPerName);
1400 Log(" max VolFrags on chain = %d\n", miscData.maxVolsPerVolInfo);
1401 Log(" # Blocks with space = %d\n", miscData.freeLength[2]);
1402 Log(" # of those fully free = %d\n", miscData.fullyFree[2]);
1403 Log("# VolFrag entries found = %d. 45 VolFrags per block\n",
1405 Log(" # Blocks with space = %d\n", miscData.freeLength[1]);
1406 Log(" # of those fully free = %d\n", miscData.fullyFree[1]);
1407 Log("# free blocks = %d\n", miscData.freeLength[0]);
1410 Log("Verify of database completed. %d errors found\n", miscData.errors);
1412 if (miscData.errors && !code)
1418 /* -----------------------------
1419 * interface routines
1420 * -----------------------------
1424 * check the integrity of the database
1426 * status - integrity: 0, ok; n, not ok (error value)
1427 * orphans - no. of orphan blocks
1428 * host - address of host that did verification
1430 afs_int32 DbVerify();
1432 SBUDB_DbVerify(call, status, orphans, host)
1433 struct rx_call *call;
1440 code = DbVerify(call, status, orphans, host);
1441 osi_auditU(call, BUDB_DBVfyEvent, code, AUD_END);
1446 DbVerify(call, status, orphans, host)
1447 struct rx_call *call;
1452 struct ubik_trans *ut = 0;
1453 afs_int32 code = 0, tcode;
1457 if (callPermitted(call) == 0)
1458 ERROR(BUDB_NOTPERMITTED);
1460 tcode = InitRPC(&ut, LOCKREAD, 1);
1464 tcode = verifyDatabase(ut, 0); /* check the database */
1471 ubik_AbortTrans(ut);
1473 code = ubik_EndTrans(ut);
1479 gethostname(hostname, sizeof(hostname));
1480 th = gethostbyname(hostname);
1484 memcpy(host, th->h_addr, sizeof(afs_int32));
1485 *host = ntohl(*host);
1491 /* ----------------------
1493 * ----------------------
1497 * do a simple sanity check on the database header
1500 check_header(callerst)
1503 static int iteration_count = 0;
1506 eof = ntohl(db.h.eofPtr);
1507 if ((eof == 0) || (eof < 0)) {
1508 Log("Eof check failed, caller %s, eof 0x%x\n", callerst, eof);
1511 eof -= sizeof(db.h);
1513 Log("Adjusted Eof check failed, caller %s, eof 0x%x\n", callerst,
1518 if (iteration_count >= 10) {
1519 Log("Eof ptr is 0x%x\n", eof);
1520 iteration_count = 0;