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>
27 #include <sys/types.h>
31 #include "error_macros.h"
32 #include "budb_errs.h"
33 #include <afs/cellconfig.h>
34 #include "afs/audit.h"
40 * 1) volInfo structures refering to a volume of the same name are
41 * chained together, i.e. the volumes described differ in volid, partition
42 * etc. The structure at the head of this list (the sameNameChain) is
43 * treated specially. When a delete volInfo request is processed, heads
44 * are not deleted unless all other items on the sameNameChain are gone.
46 * The result is that volInfo (head) structures may be present
47 * even if no tape structures refer to them. These structures are
48 * unreachable in a top-down tree walk.
50 * 1) make the verify tolerant of errors. Want to get a summary statistic
51 * indicating how may dumps are lost and how many text blocks lost
52 * 2) Make the recreation instructions write out whatever is good. This
53 * is only for the off-line case.
56 /* flags associated with each structure. These are set and checked in
57 * the blockMap entries
60 #define MAP_DUMPHASH 1 /* dump name hash checked */
61 #define MAP_TAPEHASH 2 /* tape name hash checked */
62 #define MAP_VOLHASH 4 /* volume name hash checked */
63 #define MAP_IDHASH 8 /* dump id hash checked */
65 #define MAP_HASHES (MAP_DUMPHASH | MAP_TAPEHASH | MAP_VOLHASH | MAP_IDHASH)
67 #define MAP_FREE 0x10 /* item is free */
68 #define MAP_RECREATE 0x20
69 #define MAP_HTBLOCK 0x40 /* hash table block */
70 #define MAP_TAPEONDUMP 0x100
71 #define MAP_VOLFRAGONTAPE 0x200
72 #define MAP_VOLFRAGONVOL 0x400
73 #define MAP_VOLINFOONNAME 0x800
74 #define MAP_VOLINFONAMEHEAD 0x1000
75 #define MAP_TEXTBLOCK 0x2000 /* block of text */
76 #define MAP_APPENDEDDUMP 0x4000
78 /* one blockMap for every block in the database. Each element of the entries
79 * array describes the status of a data structure/entry in that block
83 struct blockHeader header; /* copy of the block header */
84 char free; /* on free list */
85 int nEntries; /* size of the entries arrays */
86 afs_uint32 entries[1]; /* describes each entry */
89 /* status for verify call */
91 char hostname[64]; /* host on which checked */
92 afs_int32 status; /* ok, not ok */
95 int nBlocks; /* total number of blocks in db */
97 struct misc_hash_stats { /* stats for hashing */
98 int max; /* longest chain length */
99 double avg; /* avg length */
100 double std_dev; /* standard deviation of length */
104 int errors; /* errors encountered */
105 int maxErrors; /* abort after this many errors */
106 int nBlocks; /* number of database blocks */
107 int nDump, nTape, nVolInfo, nVolFrag; /* counts of each type */
108 int nVolName; /* volInfos w/ head==0 */
109 int maxVolsPerVolInfo; /* maximum list lengths */
112 int maxVolInfosPerName;
114 int maxAppendsPerDump;
115 int freeLength[NBLOCKTYPES]; /* length of free lists */
116 int fullyFree[NBLOCKTYPES]; /* free blocks full of free entries */
117 int veryLongChain; /* length of chain to report */
118 int checkFragCount; /* report fragment count errors */
119 struct misc_hash_stats dumpName, dumpIden, tapeName, volName;
120 FILE *recreate; /* stream for recreate instructions */
122 struct misc_data *misc;
124 struct blockMap **blockMap = 0; /* initial block map */
126 /* describes number of entries for each type of block */
128 int blockEntries[NBLOCKTYPES] = {
134 1, /* hashTable_BLOCK */
138 int blockEntrySize[NBLOCKTYPES] = {
140 sizeof(((struct vfBlock *) NULL)->a[0]),
141 sizeof(((struct viBlock *) NULL)->a[0]),
142 sizeof(((struct tBlock *) NULL)->a[0]),
143 sizeof(((struct dBlock *) NULL)->a[0]),
148 char *typeName[NBLOCKTYPES] = {
158 int hashBlockType[HT_MAX_FUNCTION + 1] = {
166 /* Compatibility table for the bits in the blockMap. */
168 struct mapCompatability {
169 short trigger; /* these bits trigger this element */
171 MAP_FREE, MAP_HTBLOCK, MAP_DUMPHASH | MAP_IDHASH,
172 MAP_TAPEHASH | MAP_TAPEONDUMP, MAP_VOLINFOONNAME,
173 MAP_VOLINFONAMEHEAD | MAP_VOLHASH,
174 MAP_VOLFRAGONTAPE | MAP_VOLFRAGONVOL, MAP_TEXTBLOCK};
176 /* no. of entries in the mapC array */
177 int NMAPCs = (sizeof(mapC) / sizeof(mapC[0]));
179 /* for identifying stored textual information */
181 char *textName[TB_NUM] = {
187 extern int sizeFunctions[];
188 extern int nHTBuckets;
190 #define DBBAD BUDB_DATABASEINCONSISTENT
192 /* ------------------------------------
193 * supporting routines
194 * ------------------------------------
198 * increment the error count
201 * 1 - maximum errors exceeded
207 if (++miscData.errors >= miscData.maxErrors)
213 /* convertDiskAddress
214 * given a disk address, break it down into a block and entry index. These
215 * permit access to the block map information. The conversion process
216 * compares the supplied address with the alignment/type information
217 * stored in the block map.
220 * BUDB_ADDR - address alignment checks failed
224 checkDiskAddress(address, type, blockIndexPtr, entryIndexPtr)
225 unsigned long address;
237 /* This is safest way to handle totally bogus addresses (eg 0x80001234). */
238 if ((address < sizeof(db.h)) || (address >= ntohl(db.h.eofPtr)))
241 address -= sizeof(db.h);
242 index = address / BLOCKSIZE;
243 offset = address - (index * BLOCKSIZE);
244 if (offset % sizeof(afs_int32)) /* alignment check */
246 if (offset && (type > 0) && (type <= MAX_STRUCTURE_BLOCK_TYPE)) {
247 offset -= sizeof(struct blockHeader);
248 if ((offset < 0) || (offset % blockEntrySize[type]))
250 offset /= blockEntrySize[type];
251 if (offset >= blockEntries[type])
255 *blockIndexPtr = index;
257 *entryIndexPtr = offset;
261 /* ConvertDiskAddress
262 * given a disk address, break it down into a block and entry index. These
263 * permit access to the block map information. The conversion process
264 * compares the supplied address with the alignment/type information
265 * stored in the block map.
268 * BUDB_ADDR - address alignment checks failed
272 ConvertDiskAddress(address, blockIndexPtr, entryIndexPtr)
280 index = (address - sizeof(db.h)) / BLOCKSIZE;
281 type = blockMap[index]->header.type;
283 code = checkDiskAddress(address, type, blockIndexPtr, entryIndexPtr);
291 static char error[36];
293 if ((index < 0) || (index >= NBLOCKTYPES)) {
294 sprintf(error, "UNKNOWN_TYPE %d", index);
297 return (typeName[index]);
301 getDumpID(struct ubik_trans *ut,
302 struct tape *tapePtr,
309 code = dbread(ut, ntohl(tapePtr->dump), &d, sizeof(d));
311 *dumpID = ntohl(d.id);
315 /* ------------------------------------
316 * verification routines - structure specific
317 * ------------------------------------
321 * Follow the tapes entries hanging off of a dump and verify they belong
325 verifyDumpEntry(ut, dumpAddr, ai, ao, dumpPtr)
326 struct ubik_trans *ut;
329 struct dump *dumpPtr;
332 afs_int32 tapeAddr, tapeCount = 0, volCount = 0, appDumpCount = 0;
333 afs_int32 appDumpAddr, appDumpIndex, appDumpOffset;
335 int tapeIndex, tapeOffset, ccheck = 1;
336 afs_int32 code = 0, tcode;
337 int dumpIndex, dumpOffset;
339 tcode = ConvertDiskAddress(dumpAddr, &dumpIndex, &dumpOffset);
341 Log("verifyDumpEntry: Invalid dump entry addr 0x%x\n", dumpAddr);
347 /* Step though list of tapes hanging off of this dump */
348 for (tapeAddr = ntohl(dumpPtr->firstTape); tapeAddr;
349 tapeAddr = ntohl(tape.nextTape)) {
350 tcode = ConvertDiskAddress(tapeAddr, &tapeIndex, &tapeOffset);
352 Log("verifyDumpEntry: Invalid tape entry addr 0x%x (on DumpID %u)\n", tapeAddr, ntohl(dumpPtr->id));
353 Log(" Skipping remainder of tapes in dump\n");
360 tcode = dbread(ut, tapeAddr, &tape, sizeof(tape));
364 if (ntohl(tape.dump) != dumpAddr) {
367 getDumpID(ut, &tape, &did);
368 Log("verifyDumpEntry: Tape '%s' (addr 0x%x) doesn't point to\n",
369 tape.name, tapeAddr);
370 Log(" dumpID %u (addr 0x%x). Points to DumpID %u (addr 0x%x)\n", ntohl(dumpPtr->id), dumpAddr, did, ntohl(tape.dump));
375 /* Check if this tape entry has been examine already */
376 if (blockMap[tapeIndex]->entries[tapeOffset] & MAP_TAPEONDUMP) {
377 Log("verifyDumpEntry: Tape '%s' (addr 0x%x) on multiple dumps\n",
378 tape.name, tapeAddr);
382 blockMap[tapeIndex]->entries[tapeOffset] |= MAP_TAPEONDUMP;
385 volCount += ntohl(tape.nVolumes);
388 if (ccheck && (ntohl(dumpPtr->nVolumes) != volCount)) {
389 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);
394 if (volCount > misc->maxVolsPerDump)
395 misc->maxVolsPerDump = volCount;
396 if (tapeCount > misc->maxTapesPerDump)
397 misc->maxTapesPerDump = tapeCount;
399 /* If this is an initial dump, then step though list of appended dumps
400 * hanging off of this dump.
402 if (ntohl(dumpPtr->initialDumpID) == 0) {
403 for (appDumpAddr = ntohl(dumpPtr->appendedDumpChain); appDumpAddr;
404 appDumpAddr = ntohl(appDump.appendedDumpChain)) {
407 ConvertDiskAddress(appDumpAddr, &appDumpIndex,
410 Log("verifyDumpEntry: Invalid appended dump entry addr 0x%x\n", appDumpAddr);
411 Log("Skipping remainder of appended dumps\n");
417 /* Read the appended dump in */
418 tcode = dbread(ut, appDumpAddr, &appDump, sizeof(appDump));
422 /* Verify that it points to the parent dump */
423 if (ntohl(appDump.initialDumpID) != ntohl(dumpPtr->id)) {
424 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));
429 /* Check if this appended dump entry has been examined already */
430 if (blockMap[appDumpIndex]->
431 entries[appDumpOffset] & MAP_APPENDEDDUMP) {
432 Log("verifyDumpEntry: DumpID %u (addr %u) is on multiple appended dump chains\n", ntohl(appDump.id), appDumpAddr);
433 Log("Skipping remainder of appended dumps\n");
438 blockMap[appDumpIndex]->entries[appDumpOffset] |=
445 if (appDumpCount > misc->maxAppendsPerDump)
446 misc->maxAppendsPerDump = appDumpCount;
455 * Follw the volume fragments hanging off of a tape entry and verify
456 * they belong to the tape.
459 verifyTapeEntry(ut, tapeAddr, ai, ao, tapePtr)
460 struct ubik_trans *ut;
463 struct tape *tapePtr;
465 int volCount = 0, ccheck = 1;
466 afs_int32 volFragAddr;
467 int blockIndex, entryIndex;
468 struct volFragment volFragment;
469 afs_int32 code = 0, tcode;
471 for (volFragAddr = ntohl(tapePtr->firstVol); volFragAddr;
472 volFragAddr = ntohl(volFragment.sameTapeChain)) {
473 tcode = ConvertDiskAddress(volFragAddr, &blockIndex, &entryIndex);
477 getDumpID(ut, tapePtr, &did);
478 Log("verifyTapeEntry: Invalid volFrag addr 0x%x (on tape '%s' DumpID %u)\n", volFragAddr, tapePtr->name, did);
479 Log(" Skipping remainder of volumes on tape\n");
486 tcode = dbread(ut, volFragAddr, &volFragment, sizeof(volFragment));
490 if (ntohl(volFragment.tape) != tapeAddr) {
493 getDumpID(ut, tapePtr, &did);
494 Log("verifyTapeEntry: VolFrag (addr 0x%x) doesn't point to \n",
496 Log(" tape '%s' DumpID %u (addr 0x%x). Points to addr 0x%x\n",
497 tapePtr->name, did, tapeAddr, ntohl(volFragment.tape));
502 /* Has this volume fragment already been examined */
503 if (blockMap[blockIndex]->entries[entryIndex] & MAP_VOLFRAGONTAPE) {
504 Log("verifyTapeEntry: VolFrag (addr %d) on multiple tapes\n",
509 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLFRAGONTAPE;
514 /* now check computed vs. recorded volume counts */
515 if (ccheck && (ntohl(tapePtr->nVolumes) != volCount)) {
518 getDumpID(ut, tapePtr, &did);
519 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);
524 if (volCount > misc->maxVolsPerTape)
525 misc->maxVolsPerTape = volCount;
534 * volume fragments are the lowest leaf describing a dump (nothing hangs off of it).
535 * So no check is done agaist it.
538 verifyVolFragEntry(ut, va, ai, ao, v)
539 struct ubik_trans *ut;
542 struct volFragment *v;
548 /* verifyVolInfoEntry
549 * Follow the volume fragments hanging off of a volinfo structure and
550 * verify they belong to the volinfo structure.
551 * If the volinfo structure is at the head of the same name chain, then
552 * also verify all entries are also on the chain.
555 verifyVolInfoEntry(ut, volInfoAddr, ai, ao, volInfo)
556 struct ubik_trans *ut;
557 afs_int32 volInfoAddr;
559 struct volInfo *volInfo;
561 int volCount = 0, ccheck = 1;
562 afs_int32 volFragAddr;
563 int blockIndex, entryIndex;
564 struct volFragment volFragment;
565 afs_int32 code = 0, tcode;
567 /* check each fragment attached to this volinfo structure */
568 for (volFragAddr = ntohl(volInfo->firstFragment); volFragAddr;
569 volFragAddr = ntohl(volFragment.sameNameChain)) {
570 tcode = ConvertDiskAddress(volFragAddr, &blockIndex, &entryIndex);
572 Log("verifyVolInfoEntry: Invalid volFrag entry addr 0x%x (on volume '%s')\n", volFragAddr, volInfo->name);
573 Log(" Skipping remainder of volumes on tape\n");
580 tcode = dbread(ut, volFragAddr, &volFragment, sizeof(volFragment));
584 if (ntohl(volFragment.vol) != volInfoAddr) {
585 Log("verifyVolInfoEntry: volFrag (addr 0x%x) doesn't point to \n",
587 Log(" volInfo '%s' (addr 0x%x). Points to addr 0x%x\n",
588 volInfo->name, volInfoAddr, ntohl(volFragment.vol));
593 /* volume fragment already on a volinfo chain? */
594 if (blockMap[blockIndex]->entries[entryIndex] & MAP_VOLFRAGONVOL) {
595 Log("verifyVolInfoEntry: VolFrag (addr %d) on multiple volInfo chains\n", volFragAddr);
599 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLFRAGONVOL;
604 /* check computed vs. recorded number of fragments */
605 if (ccheck && misc->checkFragCount
606 && (ntohl(volInfo->nFrags) != volCount)) {
607 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) volFrag count of %d is wrong (should be %d)\n", volInfo->name, volInfoAddr, ntohl(volInfo->nFrags), volCount);
612 if (volCount > misc->maxVolsPerVolInfo)
613 misc->maxVolsPerVolInfo = volCount;
615 /* Check that all volInfo structures with same name point to the same
616 * head. If sameNameHead == 0, this is the head structure so we check,
619 if (volInfo->sameNameHead == 0) { /*i */
620 int viCount = 1; /* count this one */
624 for (tviAddr = ntohl(volInfo->sameNameChain); tviAddr;
625 tviAddr = ntohl(tvi.sameNameChain)) {
627 tcode = ConvertDiskAddress(tviAddr, &blockIndex, &entryIndex);
629 Log("verifyVolInfoEntry: Invalid volInfo entry %s addr 0x%x\n", volInfo->name, tviAddr);
630 Log(" Skipping remainder of volumes on same name chain\n");
637 tcode = dbread(ut, tviAddr, &tvi, sizeof(tvi));
641 if (ntohl(tvi.sameNameHead) != volInfoAddr) {
642 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) doesn't point to \n", volInfo->name, tviAddr);
643 Log(" head of sameName volInfo (addr 0x%x). Points to addr 0x%x\n", volInfoAddr, ntohl(tvi.sameNameHead));
648 if (blockMap[blockIndex]->entries[entryIndex] & MAP_VOLINFOONNAME) {
649 Log("verifyVolInfoEntry: VolInfo (addr 0x%x) on multiple same name chains\n", tviAddr);
653 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLINFOONNAME;
656 /* select the passed in structure flags */
657 if (blockMap[ai]->entries[ao] & MAP_VOLINFONAMEHEAD) {
658 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) is name head multiple times\n", volInfo->name, volInfoAddr);
662 blockMap[ai]->entries[ao] |= MAP_VOLINFONAMEHEAD;
664 if (viCount > misc->maxVolInfosPerName)
665 misc->maxVolInfosPerName = viCount;
676 /* ------------------------------------
677 * verification routines - general
678 * ------------------------------------
682 * Read each block header of every 2K block and remember it in our global
683 * blockMap array. Also check that the type of block is good.
687 struct ubik_trans *ut;
692 struct blockMap *ablockMap = 0;
695 afs_int32 code = 0, tcode;
697 /* Remember every header of every block in the database */
698 for (i = 0; i < nBlocks; i++) {
699 /* To avoid the call from timing out, do a poll every 256 blocks */
701 /* read the block header */
702 blockAddr = sizeof(db.h) + (i * BLOCKSIZE);
703 tcode = dbread(ut, blockAddr, (char *)&block.h, sizeof(block.h));
707 /* check the block type */
708 blocktype = block.h.type; /* char */
709 if ((blocktype < 0) || (blocktype >= NBLOCKTYPES)) {
710 Log("Block (index %d addr %d) has invalid type of %d\n", i,
711 blockAddr, blocktype);
712 ERROR(BUDB_BLOCKTYPE);
715 /* allocate the block map memory */
717 sizeof(*ablockMap) + (blockEntries[blocktype] -
718 1) * sizeof(ablockMap->entries[0]);
719 ablockMap = (struct blockMap *)malloc(bmsize);
722 memset(ablockMap, 0, bmsize);
724 ablockMap->nEntries = blockEntries[blocktype];
726 /* save the block header in the block map */
727 memcpy(&ablockMap->header, &block.h, sizeof(ablockMap->header));
728 blockMap[i] = ablockMap;
735 int minvols, maxvols, ttlvols;
737 /* verifyHashTableBlock
738 * Take a 2K hash table block and traverse its entries. Make sure each entry
739 * is of the correct type for the hash table, is hashed into the correct
740 * entry and is not threaded on multiple lists.
743 verifyHashTableBlock(ut, mhtPtr, htBlockPtr, old, length, index, mapBit)
744 struct ubik_trans *ut;
745 struct memoryHashTable *mhtPtr;
746 struct htBlock *htBlockPtr;
748 afs_int32 length; /* size of whole hash table */
749 int index; /* base index of this block */
752 int type; /* hash table type */
753 int entrySize; /* hashed entry size */
754 int blockType; /* block type for this hash table */
755 int blockIndex, entryIndex;
756 char entry[sizeof(struct block)];
758 int hash; /* calculated hash value for entry */
760 afs_int32 code = 0, tcode;
762 type = ntohl(mhtPtr->ht->functionType);
763 entrySize = sizeFunctions[type];
764 blockType = hashBlockType[type];
766 /* step through this hash table block, being careful to stop
767 * before the end of the overall hash table
770 for (i = 0; (i < nHTBuckets) && (index < length); i++, index++) { /*f */
771 entryAddr = ntohl(htBlockPtr->bucket[i]);
773 /* if this is the old hash table, all entries below the progress mark
774 * should have been moved to the new hash table
776 if (old && (index < mhtPtr->progress) && entryAddr) {
777 Log("Old hash table not empty (entry %d) below progress (%d)\n",
778 i, mhtPtr->progress);
783 /* now walk down the chain of each bucket */
784 for (count = 0; entryAddr; count++) { /*w */
785 tcode = ConvertDiskAddress(entryAddr, &blockIndex, &entryIndex);
787 Log("verifyHashTableBlock: Invalid hash table chain addr 0x%x\n", entryAddr);
788 Log(" Skipping remainder of bucket %d\n", index);
795 if (blockMap[blockIndex]->header.type != blockType) {
796 Log("Hash table chain (block index %d) incorrect\n",
798 Log(" Table type %d traverses block type %d\n", blockType,
799 blockMap[blockIndex]->header.type);
800 Log(" Skipping remainder of bucket %d\n", index);
806 if (dbread(ut, entryAddr, &entry[0], entrySize))
809 hash = ht_HashEntry(mhtPtr, &entry[0]) % length;
810 if (hash != index) { /* if it hashed to some other place */
811 Log("Entry (addr 0x%x) bucket %d, should be hashed into bucket %d\n", entryAddr, index, hash);
816 /* check if entry has been examined */
817 if (blockMap[blockIndex]->entries[entryIndex] & mapBit) {
818 Log("Entry (addr 0x%x) multiply referenced\n", entryAddr);
822 blockMap[blockIndex]->entries[entryIndex] |= mapBit;
824 entryAddr = ntohl(*((dbadr *) (entry + mhtPtr->threadOffset)));
827 /* Log("Bucket %4d contains %d entries\n", index+1, count); */
840 * Read each 2K block a hashtable has (both its old hastable and
841 * new hashtable) and verify the block has not been read before.
842 * Will also make call to verify entries within each 2K block of
846 verifyHashTable(ut, mhtPtr, mapBit)
847 struct ubik_trans *ut;
848 struct memoryHashTable *mhtPtr;
851 struct hashTable *htPtr = mhtPtr->ht;
853 struct htBlock hashTableBlock;
854 int tableLength; /* # entries */
855 int hashblocks; /* # blocks */
856 dbadr tableAddr; /* disk addr of hash block */
857 int blockIndex, entryIndex;
860 afs_int32 code = 0, tcode;
862 extern int nHTBuckets; /* # buckets in a hash table */
864 LogDebug(4, "Htable: length %d oldlength %d progress %d\n",
865 mhtPtr->length, mhtPtr->oldLength, mhtPtr->progress);
867 /* check both old and current tables */
868 for (old = 0; old <= 1; old++) { /*fo */
869 tableLength = (old ? mhtPtr->oldLength : mhtPtr->length);
870 if (tableLength == 0)
872 tableAddr = (old ? ntohl(htPtr->oldTable) : ntohl(htPtr->table));
874 maxvols = ttlvols = 0;
876 /* follow the chain of hashtable blocks - avoid infinite loops */
877 hashblocks = ((tableLength - 1) / nHTBuckets) + 1; /* numb of 2K hashtable blocks */
878 for (i = 0; (tableAddr && (i < hashblocks + 5)); i++) {
879 tcode = ConvertDiskAddress(tableAddr, &blockIndex, &entryIndex);
881 Log("verifyHashTable: Invalid hash table chain addr 0x%x\n",
883 Log(" Skipping remainder of hash table chain\n");
890 if (blockMap[blockIndex]->header.type != hashTable_BLOCK) {
891 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);
892 Log(" Skipping remainder of hash table chain\n");
894 ERROR(BUDB_BLOCKTYPE);
898 /* check if we've examined this block before */
899 /* mark this (hash table) block as examined */
900 if (blockMap[blockIndex]->entries[entryIndex] & MAP_HTBLOCK) {
901 Log("Hash table block (index %d addr 0x%x) multiple ref\n",
904 ERROR(BUDB_DATABASEINCONSISTENT);
906 blockMap[blockIndex]->entries[entryIndex] |= MAP_HTBLOCK;
908 /* Read the actual hash table block */
910 dbread(ut, tableAddr, &hashTableBlock,
911 sizeof(hashTableBlock));
915 /* Verify its entries */
917 verifyHashTableBlock(ut, mhtPtr, &hashTableBlock, old,
918 tableLength, (i * nHTBuckets), mapBit);
922 tableAddr = ntohl(hashTableBlock.h.next);
925 /* Verify numb hash blocks is as it says */
926 if (i != hashblocks) {
927 Log("Incorrect number of hash chain blocks: %d (expected %d), hashtype %d\n", i, hashblocks, ntohl(htPtr->functionType));
929 ERROR(BUDB_DATABASEINCONSISTENT);
933 Log("%d entries; %u buckets; min = %d; max = %d\n", ttlvols,
934 tableLength, minvols, maxvols);
942 * do a linear walk of all the blocks. Check each block of structures
943 * to see if the actual free matches the recorded free. Also check
944 * the integrity of each allocated structure.
947 verifyEntryChains(ut)
948 struct ubik_trans *ut;
952 int blockIndex, entryIndex;
953 char entry[sizeof(struct block)];
958 static afs_int32(*checkEntry[NBLOCKTYPES]) ()
960 /* FIXME: this list does not match typeName[] and may be incorrect */
962 verifyVolFragEntry, verifyVolInfoEntry, verifyTapeEntry, verifyDumpEntry, 0 /* text block */
965 for (blockIndex = 0; blockIndex < nBlocks; blockIndex++) { /*f */
966 /* ignore non-structure or blocks with invalid type */
967 type = blockMap[blockIndex]->header.type;
968 if ((type <= 0) || (type > MAX_STRUCTURE_BLOCK_TYPE))
971 entrySize = blockEntrySize[type];
974 for (entryIndex = 0; entryIndex < blockMap[blockIndex]->nEntries; entryIndex++) { /*f */
976 sizeof(db.h) + (blockIndex * BLOCKSIZE) +
977 sizeof(struct blockHeader) + (entryIndex * entrySize);
978 if (dbread(ut, offset, &entry[0], entrySize))
981 /* check if entry is free by looking at the first "afs_int32" of the structure */
982 if (*((afs_int32 *) & entry[0]) == 0) { /* zero is free */
983 /* Is it on any hash chain? */
984 if (blockMap[blockIndex]->entries[entryIndex] & MAP_HASHES) {
985 Log("Entry: blockindex %d, entryindex %d - marked free but hashed 0x%x\n", blockIndex, entryIndex, blockMap[blockIndex]->entries[entryIndex]);
990 blockMap[blockIndex]->entries[entryIndex] |= MAP_FREE;
993 /* check the entry itself for consistency */
995 (*(checkEntry[type])) (ut, offset, blockIndex, entryIndex,
1002 /* check computed free with recorded free entries */
1003 if (nFree != ntohs(blockMap[blockIndex]->header.nFree)) {
1004 Log("Block (index %d) free count %d has %d free structs\n",
1005 blockIndex, ntohs(blockMap[blockIndex]->header.nFree), nFree);
1020 int blockIndex, entryIndex;
1024 /* for each free list */
1025 for (i = 0; i < NBLOCKTYPES; i++) {
1026 misc->fullyFree[i] = misc->freeLength[i] = 0;
1028 for (addr = ntohl(db.h.freePtrs[i]); addr;
1029 addr = ntohl(blockMap[blockIndex]->header.next)) {
1030 misc->freeLength[i]++;
1032 code = ConvertDiskAddress(addr, &blockIndex, &entryIndex);
1033 if (code || (entryIndex != 0)) {
1034 Log("verifyFreeLists: Invalid free chain addr 0x%x in %s free chain\n", addr, TypeName(i));
1035 Log(" Skipping remainder of free chain\n");
1042 /* check block type */
1043 if (blockMap[blockIndex]->header.type != i) {
1044 Log("verifyFreeLists: Found %s type in %s free chain (addr 0x%x)\n",
1045 TypeName(blockMap[blockIndex]->header.type), TypeName(i),
1051 /* If entire block isn't free, check if count of free entries is ok */
1052 nFree = ntohs(blockMap[blockIndex]->header.nFree);
1053 if (i != free_BLOCK) {
1054 if ((nFree <= 0) || (nFree > blockEntries[i])) {
1055 Log("verifyFreeLists: Illegal free count %d on %s free chain\n", nFree, TypeName(i));
1058 } else if (nFree == blockEntries[i]) {
1059 misc->fullyFree[i]++;
1063 /* Check if already examined the block */
1064 if (blockMap[blockIndex]->free) {
1065 Log("verifyFreeLists: %s free chain block at addr 0x%x multiply threaded\n", TypeName(i), addr);
1069 blockMap[blockIndex]->free++;
1077 * Examines each entry to make sure it was traversed appropriately by
1078 * checking the bits for compatibility.
1083 int blockIndex, entryIndex, i, entrySize, type, bits;
1086 for (blockIndex = 0; blockIndex < nBlocks; blockIndex++) {
1087 /* If no entries in this block, then the block should be marked free */
1088 if ((blockMap[blockIndex]->nEntries == 0)
1089 && !blockMap[blockIndex]->free) {
1090 Log("verifyMapBits: Orphan free block (index %d)\n", blockIndex);
1095 /* check each entry */
1096 for (entryIndex = 0; entryIndex < blockMap[blockIndex]->nEntries; entryIndex++) { /*f */
1097 if ((entryIndex % 1024) == 0)
1100 bits = blockMap[blockIndex]->entries[entryIndex];
1102 for (i = 0; i < NMAPCs; i++)
1103 if ((bits & mapC[i].trigger) == mapC[i].trigger)
1109 type = blockMap[blockIndex]->header.type;
1110 entrySize = blockEntrySize[type];
1112 sizeof(db.h) + (blockIndex * BLOCKSIZE) +
1113 sizeof(struct blockHeader) + (entryIndex * entrySize);
1115 sprintf(logstr, "%s entry Block %d, Entry %d, (addr 0x%x)",
1116 TypeName(type), blockIndex, entryIndex, offset);
1119 strcat(logstr, ": An orphaned entry");
1120 if (bits & MAP_FREE)
1121 strcat(logstr, ": A valid free block");
1122 if (bits & MAP_HTBLOCK)
1123 strcat(logstr, ": A valid hash-table block");
1124 if (bits & MAP_TEXTBLOCK)
1125 strcat(logstr, ": A valid text block");
1126 if (bits & (MAP_DUMPHASH | MAP_IDHASH)) {
1127 if (!(bits & MAP_DUMPHASH))
1129 ": A dump not on dump-name hash-chain");
1130 else if (!(bits & MAP_IDHASH))
1131 strcat(logstr, ": A dump not on dump-id hash-chain");
1133 strcat(logstr, ": A valid dump entry");
1135 if (bits & (MAP_TAPEHASH | MAP_TAPEONDUMP)) {
1136 if (!(bits & MAP_TAPEHASH))
1138 ": A tape not on tape-name hash-chain");
1139 else if (!(bits & MAP_TAPEONDUMP))
1140 strcat(logstr, ": A tape not associated with a dump");
1142 strcat(logstr, ": A valid tape entry");
1144 if (bits & MAP_VOLINFOONNAME)
1146 ": A valid volInfo on a volume-name chain");
1147 if (bits & (MAP_VOLINFONAMEHEAD | MAP_VOLHASH)) {
1148 if (!(bits & MAP_VOLINFONAMEHEAD))
1150 ": A volInfo not the head of a volume-name hash-chain");
1151 else if (!(bits & MAP_VOLHASH))
1153 ": A volInfo not on volume-name hash-chain");
1156 ": A valid volInfo in volume-name hash-chain");
1158 if (bits & (MAP_VOLFRAGONTAPE | MAP_VOLFRAGONVOL)) {
1159 if (!(bits & MAP_VOLFRAGONTAPE))
1161 ": A volFrag not associated with a tape");
1162 else if (!(bits & MAP_VOLFRAGONVOL))
1164 ": A volFrag not associated with a volume");
1166 strcat(logstr, ": A valid volFrag entry");
1168 Log("%s\n", logstr);
1181 struct ubik_trans *ut;
1185 extern afs_int32 verifyTextChain();
1187 /* check each of the text types in use */
1188 for (i = 0; i < TB_NUM; i++) {
1189 Log("Verify Text: %s", textName[i]);
1190 code = verifyTextChain(ut, &db.h.textBlock[i]);
1198 * check the integrity of a text chain. Also checks the new chain.
1201 verifyTextChain(ut, tbPtr)
1202 struct ubik_trans *ut;
1203 struct textBlock *tbPtr;
1206 int blockIndex, entryIndex;
1210 afs_int32 code = 0, tcode;
1212 for (new = 0; new < 2; new++) {
1214 blockAddr = ntohl(tbPtr->textAddr);
1217 (new ? ntohl(tbPtr->newTextAddr) : ntohl(tbPtr->textAddr));
1218 blockAddr; blockAddr = ntohl(block.h.next)) {
1219 tcode = ConvertDiskAddress(blockAddr, &blockIndex, &entryIndex);
1221 Log("verifyTextChain: Invalid %s text block addr 0x%x\n",
1222 (new ? "new" : ""), blockAddr);
1223 Log(" Skipping remainder of text chain\n");
1229 tcode = dbread(ut, blockAddr, &block, sizeof(block));
1233 if (blockMap[blockIndex]->entries[entryIndex] & MAP_TEXTBLOCK) {
1234 Log("verifyTextChain: Text block (addr 0x%x) multiply chained\n", blockAddr);
1238 blockMap[blockIndex]->entries[entryIndex] |= MAP_TEXTBLOCK;
1240 size += BLOCK_DATA_SIZE;
1243 if (ntohl(new ? tbPtr->newsize : tbPtr->size) > size) {
1244 Log("verifyTextChain: Text block %s size %d > computed capacity %d\n", (new ? "new" : ""), ntohl(new ? tbPtr->newsize : tbPtr->size), size);
1254 /* -----------------------------------------
1255 * verification driver routines
1256 * -----------------------------------------
1260 * Check the integrity of the database
1264 verifyDatabase(ut, recreateFile)
1265 struct ubik_trans *ut;
1266 FILE *recreateFile; /* not used */
1270 afs_int32 code = 0, tcode;
1272 extern int nBlocks; /* no. blocks in database */
1273 extern struct ubik_dbase *BU_dbase;
1275 /* clear verification statistics */
1277 memset(&miscData, 0, sizeof(miscData));
1280 miscData.maxErrors = 1000000;
1282 miscData.maxErrors = 50; /* Catch the first 50 errors */
1284 miscData.veryLongChain = 0;
1285 miscData.checkFragCount = 1; /* check frags */
1288 eof = ntohl(db.h.eofPtr);
1289 eof -= sizeof(db.h); /* subtract header */
1290 nBlocks = eof / BLOCKSIZE;
1292 Log("Verify of backup database started\n");
1293 Log("Database is %u. %d blocks of %d Bytes\n", eof, nBlocks, BLOCKSIZE);
1295 if ((eof < 0) || (nBlocks * BLOCKSIZE != eof)) {
1296 Log("Database eofPtr (%d) bad, blocksize %d\n", eof, BLOCKSIZE);
1300 /* set size of database */
1301 miscData.nBlocks = nBlocks;
1304 ERROR(0); /* Nothing to check? */
1306 /* construct block map - first level is the array of pointers */
1307 bmsize = nBlocks * sizeof(struct blockMap *);
1308 blockMap = (struct blockMap **)malloc(bmsize);
1311 memset(blockMap, 0, bmsize);
1313 /* verify blocks and construct the block map */
1314 Log("Read header of every block\n");
1315 tcode = verifyBlocks(ut);
1319 /* check the various hash tables */
1320 Log("Verify volume name hash table\n");
1321 tcode = verifyHashTable(ut, &db.volName, MAP_VOLHASH);
1325 Log("Verify tape name hash table\n");
1326 tcode = verifyHashTable(ut, &db.tapeName, MAP_TAPEHASH);
1330 Log("Verify dump name hash table\n");
1331 tcode = verifyHashTable(ut, &db.dumpName, MAP_DUMPHASH);
1335 Log("Verify dump id hash table\n");
1336 tcode = verifyHashTable(ut, &db.dumpIden, MAP_IDHASH);
1340 /* check the entry chains */
1341 Log("Verify all blocks and entries\n");
1342 tcode = verifyEntryChains(ut);
1346 /* check text blocks - Log message in verifyText */
1347 tcode = verifyText(ut);
1351 /* check free list */
1352 Log("Verify Free Lists\n");
1353 tcode = verifyFreeLists();
1357 /* check entry map bit compatibility */
1359 Log("Verify Map bits\n");
1360 tcode = verifyMapBits();
1365 /* free the block map */
1366 if (blockMap != 0) {
1369 /* free all the individual maps */
1370 for (i = 0; i < nBlocks; i++) {
1375 /* free the pointer array */
1381 Log("# 2K database blocks = %d\n", miscData.nBlocks);
1382 Log("# Dump entries found = %d. 3 dumps per block\n",
1384 Log(" max tapes on a dump = %d\n", miscData.maxTapesPerDump);
1385 Log(" max volumes on a dump = %d\n", miscData.maxVolsPerDump);
1386 Log(" max appends on a dump = %d\n", miscData.maxAppendsPerDump);
1387 Log(" # Blocks with space = %d\n", miscData.freeLength[4]);
1388 Log(" # of those fully free = %d\n", miscData.fullyFree[4]);
1389 Log("# Tape entries found = %d. 20 tapes per block\n",
1391 Log(" max volumes on a tape = %d\n", miscData.maxVolsPerTape);
1392 Log(" # Blocks with space = %d\n", miscData.freeLength[3]);
1393 Log(" # of those fully free = %d\n", miscData.fullyFree[3]);
1394 Log("# VolInfo entries found = %d. 20 volInfos per block\n",
1396 Log(" # head of sameNameCh = %d\n", miscData.nVolName);
1397 Log(" max on a sameNameCh = %d\n", miscData.maxVolInfosPerName);
1398 Log(" max VolFrags on chain = %d\n", miscData.maxVolsPerVolInfo);
1399 Log(" # Blocks with space = %d\n", miscData.freeLength[2]);
1400 Log(" # of those fully free = %d\n", miscData.fullyFree[2]);
1401 Log("# VolFrag entries found = %d. 45 VolFrags per block\n",
1403 Log(" # Blocks with space = %d\n", miscData.freeLength[1]);
1404 Log(" # of those fully free = %d\n", miscData.fullyFree[1]);
1405 Log("# free blocks = %d\n", miscData.freeLength[0]);
1408 Log("Verify of database completed. %d errors found\n", miscData.errors);
1410 if (miscData.errors && !code)
1416 /* -----------------------------
1417 * interface routines
1418 * -----------------------------
1422 * check the integrity of the database
1424 * status - integrity: 0, ok; n, not ok (error value)
1425 * orphans - no. of orphan blocks
1426 * host - address of host that did verification
1428 afs_int32 DbVerify();
1430 SBUDB_DbVerify(call, status, orphans, host)
1431 struct rx_call *call;
1438 code = DbVerify(call, status, orphans, host);
1439 osi_auditU(call, BUDB_DBVfyEvent, code, AUD_END);
1444 DbVerify(call, status, orphans, host)
1445 struct rx_call *call;
1450 struct ubik_trans *ut = 0;
1451 afs_int32 code = 0, tcode;
1455 if (callPermitted(call) == 0)
1456 ERROR(BUDB_NOTPERMITTED);
1458 tcode = InitRPC(&ut, LOCKREAD, 1);
1462 tcode = verifyDatabase(ut, 0); /* check the database */
1469 ubik_AbortTrans(ut);
1471 code = ubik_EndTrans(ut);
1477 gethostname(hostname, sizeof(hostname));
1478 th = gethostbyname(hostname);
1482 memcpy(host, th->h_addr, sizeof(afs_int32));
1483 *host = ntohl(*host);
1489 /* ----------------------
1491 * ----------------------
1495 * do a simple sanity check on the database header
1498 check_header(callerst)
1501 static int iteration_count = 0;
1504 eof = ntohl(db.h.eofPtr);
1505 if ((eof == 0) || (eof < 0)) {
1506 Log("Eof check failed, caller %s, eof 0x%x\n", callerst, eof);
1509 eof -= sizeof(db.h);
1511 Log("Adjusted Eof check failed, caller %s, eof 0x%x\n", callerst,
1516 if (iteration_count >= 10) {
1517 Log("Eof ptr is 0x%x\n", eof);
1518 iteration_count = 0;