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>
20 #include <netinet/in.h>
25 #include <sys/types.h>
29 #include "error_macros.h"
30 #include "budb_errs.h"
31 #include "budb_internal.h"
32 #include <afs/cellconfig.h>
33 #include "afs/audit.h"
39 * 1) volInfo structures refering to a volume of the same name are
40 * chained together, i.e. the volumes described differ in volid, partition
41 * etc. The structure at the head of this list (the sameNameChain) is
42 * treated specially. When a delete volInfo request is processed, heads
43 * are not deleted unless all other items on the sameNameChain are gone.
45 * The result is that volInfo (head) structures may be present
46 * even if no tape structures refer to them. These structures are
47 * unreachable in a top-down tree walk.
49 * 1) make the verify tolerant of errors. Want to get a summary statistic
50 * indicating how may dumps are lost and how many text blocks lost
51 * 2) Make the recreation instructions write out whatever is good. This
52 * is only for the off-line case.
55 /* flags associated with each structure. These are set and checked in
56 * the blockMap entries
59 #define MAP_DUMPHASH 1 /* dump name hash checked */
60 #define MAP_TAPEHASH 2 /* tape name hash checked */
61 #define MAP_VOLHASH 4 /* volume name hash checked */
62 #define MAP_IDHASH 8 /* dump id hash checked */
64 #define MAP_HASHES (MAP_DUMPHASH | MAP_TAPEHASH | MAP_VOLHASH | MAP_IDHASH)
66 #define MAP_FREE 0x10 /* item is free */
67 #define MAP_RECREATE 0x20
68 #define MAP_HTBLOCK 0x40 /* hash table block */
69 #define MAP_TAPEONDUMP 0x100
70 #define MAP_VOLFRAGONTAPE 0x200
71 #define MAP_VOLFRAGONVOL 0x400
72 #define MAP_VOLINFOONNAME 0x800
73 #define MAP_VOLINFONAMEHEAD 0x1000
74 #define MAP_TEXTBLOCK 0x2000 /* block of text */
75 #define MAP_APPENDEDDUMP 0x4000
77 /* one blockMap for every block in the database. Each element of the entries
78 * array describes the status of a data structure/entry in that block
82 struct blockHeader header; /* copy of the block header */
83 char free; /* on free list */
84 int nEntries; /* size of the entries arrays */
85 afs_uint32 entries[1]; /* describes each entry */
88 /* status for verify call */
90 char hostname[64]; /* host on which checked */
91 afs_int32 status; /* ok, not ok */
94 int nBlocks; /* total number of blocks in db */
96 struct misc_hash_stats { /* stats for hashing */
97 int max; /* longest chain length */
98 double avg; /* avg length */
99 double std_dev; /* standard deviation of length */
103 int errors; /* errors encountered */
104 int maxErrors; /* abort after this many errors */
105 int nBlocks; /* number of database blocks */
106 int nDump, nTape, nVolInfo, nVolFrag; /* counts of each type */
107 int nVolName; /* volInfos w/ head==0 */
108 int maxVolsPerVolInfo; /* maximum list lengths */
111 int maxVolInfosPerName;
113 int maxAppendsPerDump;
114 int freeLength[NBLOCKTYPES]; /* length of free lists */
115 int fullyFree[NBLOCKTYPES]; /* free blocks full of free entries */
116 int veryLongChain; /* length of chain to report */
117 int checkFragCount; /* report fragment count errors */
118 struct misc_hash_stats dumpName, dumpIden, tapeName, volName;
119 FILE *recreate; /* stream for recreate instructions */
121 struct misc_data *misc;
123 struct blockMap **blockMap = 0; /* initial block map */
125 /* describes number of entries for each type of block */
127 int blockEntries[NBLOCKTYPES] = {
133 1, /* hashTable_BLOCK */
137 int blockEntrySize[NBLOCKTYPES] = {
139 sizeof(((struct vfBlock *) NULL)->a[0]),
140 sizeof(((struct viBlock *) NULL)->a[0]),
141 sizeof(((struct tBlock *) NULL)->a[0]),
142 sizeof(((struct dBlock *) NULL)->a[0]),
147 char *typeName[NBLOCKTYPES] = {
157 int hashBlockType[HT_MAX_FUNCTION + 1] = {
165 /* Compatibility table for the bits in the blockMap. */
167 struct mapCompatability {
168 short trigger; /* these bits trigger this element */
170 {MAP_FREE}, {MAP_HTBLOCK}, {MAP_DUMPHASH | MAP_IDHASH},
171 {MAP_TAPEHASH | MAP_TAPEONDUMP}, {MAP_VOLINFOONNAME},
172 {MAP_VOLINFONAMEHEAD | MAP_VOLHASH},
173 {MAP_VOLFRAGONTAPE | MAP_VOLFRAGONVOL}, {MAP_TEXTBLOCK}};
175 /* no. of entries in the mapC array */
176 int NMAPCs = (sizeof(mapC) / sizeof(mapC[0]));
178 /* for identifying stored textual information */
180 char *textName[TB_NUM] = {
186 extern int sizeFunctions[];
187 extern int nHTBuckets;
189 afs_int32 DbVerify(struct rx_call *call, afs_int32 *status,
190 afs_int32 *orphans, afs_int32 *host);
191 afs_int32 verifyTextChain(struct ubik_trans *ut, struct textBlock *tbPtr);
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(unsigned long address, int type, int *blockIndexPtr,
238 /* This is safest way to handle totally bogus addresses (eg 0x80001234). */
239 if ((address < sizeof(db.h)) || (address >= ntohl(db.h.eofPtr)))
242 address -= sizeof(db.h);
243 index = address / BLOCKSIZE;
244 offset = address - (index * BLOCKSIZE);
245 if (offset % sizeof(afs_int32)) /* alignment check */
247 if (offset && (type > 0) && (type <= MAX_STRUCTURE_BLOCK_TYPE)) {
248 offset -= sizeof(struct blockHeader);
249 if ((offset < 0) || (offset % blockEntrySize[type]))
251 offset /= blockEntrySize[type];
252 if (offset >= blockEntries[type])
256 *blockIndexPtr = index;
258 *entryIndexPtr = offset;
262 /* ConvertDiskAddress
263 * given a disk address, break it down into a block and entry index. These
264 * permit access to the block map information. The conversion process
265 * compares the supplied address with the alignment/type information
266 * stored in the block map.
269 * BUDB_ADDR - address alignment checks failed
273 ConvertDiskAddress(afs_uint32 address, int *blockIndexPtr, int *entryIndexPtr)
278 index = (address - sizeof(db.h)) / BLOCKSIZE;
279 type = blockMap[index]->header.type;
281 code = checkDiskAddress(address, type, blockIndexPtr, entryIndexPtr);
288 static char error[36];
290 if ((index < 0) || (index >= NBLOCKTYPES)) {
291 sprintf(error, "UNKNOWN_TYPE %d", index);
294 return (typeName[index]);
298 getDumpID(struct ubik_trans *ut,
299 struct tape *tapePtr,
306 code = dbread(ut, ntohl(tapePtr->dump), &d, sizeof(d));
308 *dumpID = ntohl(d.id);
312 /* ------------------------------------
313 * verification routines - structure specific
314 * ------------------------------------
318 * Follow the tapes entries hanging off of a dump and verify they belong
322 verifyDumpEntry(struct ubik_trans *ut, afs_int32 dumpAddr, int ai, int ao,
325 struct dump *dumpPtr = (struct dump *)param;
328 afs_int32 tapeAddr, tapeCount = 0, volCount = 0, appDumpCount = 0;
329 afs_int32 appDumpAddr, appDumpIndex, appDumpOffset;
331 int tapeIndex, tapeOffset, ccheck = 1;
332 afs_int32 code = 0, tcode;
333 int dumpIndex, dumpOffset;
335 tcode = ConvertDiskAddress(dumpAddr, &dumpIndex, &dumpOffset);
337 Log("verifyDumpEntry: Invalid dump entry addr 0x%x\n", dumpAddr);
343 /* Step though list of tapes hanging off of this dump */
344 for (tapeAddr = ntohl(dumpPtr->firstTape); tapeAddr;
345 tapeAddr = ntohl(tape.nextTape)) {
346 tcode = ConvertDiskAddress(tapeAddr, &tapeIndex, &tapeOffset);
348 Log("verifyDumpEntry: Invalid tape entry addr 0x%x (on DumpID %u)\n", tapeAddr, ntohl(dumpPtr->id));
349 Log(" Skipping remainder of tapes in dump\n");
356 tcode = dbread(ut, tapeAddr, &tape, sizeof(tape));
360 if (ntohl(tape.dump) != dumpAddr) {
363 getDumpID(ut, &tape, &did);
364 Log("verifyDumpEntry: Tape '%s' (addr 0x%x) doesn't point to\n",
365 tape.name, tapeAddr);
366 Log(" dumpID %u (addr 0x%x). Points to DumpID %u (addr 0x%x)\n", ntohl(dumpPtr->id), dumpAddr, did, ntohl(tape.dump));
371 /* Check if this tape entry has been examine already */
372 if (blockMap[tapeIndex]->entries[tapeOffset] & MAP_TAPEONDUMP) {
373 Log("verifyDumpEntry: Tape '%s' (addr 0x%x) on multiple dumps\n",
374 tape.name, tapeAddr);
378 blockMap[tapeIndex]->entries[tapeOffset] |= MAP_TAPEONDUMP;
381 volCount += ntohl(tape.nVolumes);
384 if (ccheck && (ntohl(dumpPtr->nVolumes) != volCount)) {
385 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);
390 if (volCount > misc->maxVolsPerDump)
391 misc->maxVolsPerDump = volCount;
392 if (tapeCount > misc->maxTapesPerDump)
393 misc->maxTapesPerDump = tapeCount;
395 /* If this is an initial dump, then step though list of appended dumps
396 * hanging off of this dump.
398 if (ntohl(dumpPtr->initialDumpID) == 0) {
399 for (appDumpAddr = ntohl(dumpPtr->appendedDumpChain); appDumpAddr;
400 appDumpAddr = ntohl(appDump.appendedDumpChain)) {
403 ConvertDiskAddress(appDumpAddr, &appDumpIndex,
406 Log("verifyDumpEntry: Invalid appended dump entry addr 0x%x\n", appDumpAddr);
407 Log("Skipping remainder of appended dumps\n");
413 /* Read the appended dump in */
414 tcode = dbread(ut, appDumpAddr, &appDump, sizeof(appDump));
418 /* Verify that it points to the parent dump */
419 if (ntohl(appDump.initialDumpID) != ntohl(dumpPtr->id)) {
420 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));
425 /* Check if this appended dump entry has been examined already */
426 if (blockMap[appDumpIndex]->
427 entries[appDumpOffset] & MAP_APPENDEDDUMP) {
428 Log("verifyDumpEntry: DumpID %u (addr %u) is on multiple appended dump chains\n", ntohl(appDump.id), appDumpAddr);
429 Log("Skipping remainder of appended dumps\n");
434 blockMap[appDumpIndex]->entries[appDumpOffset] |=
441 if (appDumpCount > misc->maxAppendsPerDump)
442 misc->maxAppendsPerDump = appDumpCount;
451 * Follw the volume fragments hanging off of a tape entry and verify
452 * they belong to the tape.
455 verifyTapeEntry(struct ubik_trans *ut, afs_int32 tapeAddr, int ai, int ao,
458 struct tape *tapePtr = (struct tape *) param;
459 int volCount = 0, ccheck = 1;
460 afs_int32 volFragAddr;
461 int blockIndex, entryIndex;
462 struct volFragment volFragment;
463 afs_int32 code = 0, tcode;
465 for (volFragAddr = ntohl(tapePtr->firstVol); volFragAddr;
466 volFragAddr = ntohl(volFragment.sameTapeChain)) {
467 tcode = ConvertDiskAddress(volFragAddr, &blockIndex, &entryIndex);
471 getDumpID(ut, tapePtr, &did);
472 Log("verifyTapeEntry: Invalid volFrag addr 0x%x (on tape '%s' DumpID %u)\n", volFragAddr, tapePtr->name, did);
473 Log(" Skipping remainder of volumes on tape\n");
480 tcode = dbread(ut, volFragAddr, &volFragment, sizeof(volFragment));
484 if (ntohl(volFragment.tape) != tapeAddr) {
487 getDumpID(ut, tapePtr, &did);
488 Log("verifyTapeEntry: VolFrag (addr 0x%x) doesn't point to \n",
490 Log(" tape '%s' DumpID %u (addr 0x%x). Points to addr 0x%x\n",
491 tapePtr->name, did, tapeAddr, ntohl(volFragment.tape));
496 /* Has this volume fragment already been examined */
497 if (blockMap[blockIndex]->entries[entryIndex] & MAP_VOLFRAGONTAPE) {
498 Log("verifyTapeEntry: VolFrag (addr %d) on multiple tapes\n",
503 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLFRAGONTAPE;
508 /* now check computed vs. recorded volume counts */
509 if (ccheck && (ntohl(tapePtr->nVolumes) != volCount)) {
512 getDumpID(ut, tapePtr, &did);
513 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);
518 if (volCount > misc->maxVolsPerTape)
519 misc->maxVolsPerTape = volCount;
528 * volume fragments are the lowest leaf describing a dump (nothing hangs off of it).
529 * So no check is done agaist it.
532 verifyVolFragEntry(struct ubik_trans *ut, afs_int32 va, int ai, int ao,
535 /* struct volFragment *v = (struct volFragment *)param; */
540 /* verifyVolInfoEntry
541 * Follow the volume fragments hanging off of a volinfo structure and
542 * verify they belong to the volinfo structure.
543 * If the volinfo structure is at the head of the same name chain, then
544 * also verify all entries are also on the chain.
547 verifyVolInfoEntry(struct ubik_trans *ut, afs_int32 volInfoAddr, int ai,
550 struct volInfo *volInfo = (struct volInfo *) param;
552 int volCount = 0, ccheck = 1;
553 afs_int32 volFragAddr;
554 int blockIndex, entryIndex;
555 struct volFragment volFragment;
556 afs_int32 code = 0, tcode;
558 /* check each fragment attached to this volinfo structure */
559 for (volFragAddr = ntohl(volInfo->firstFragment); volFragAddr;
560 volFragAddr = ntohl(volFragment.sameNameChain)) {
561 tcode = ConvertDiskAddress(volFragAddr, &blockIndex, &entryIndex);
563 Log("verifyVolInfoEntry: Invalid volFrag entry addr 0x%x (on volume '%s')\n", volFragAddr, volInfo->name);
564 Log(" Skipping remainder of volumes on tape\n");
571 tcode = dbread(ut, volFragAddr, &volFragment, sizeof(volFragment));
575 if (ntohl(volFragment.vol) != volInfoAddr) {
576 Log("verifyVolInfoEntry: volFrag (addr 0x%x) doesn't point to \n",
578 Log(" volInfo '%s' (addr 0x%x). Points to addr 0x%x\n",
579 volInfo->name, volInfoAddr, ntohl(volFragment.vol));
584 /* volume fragment already on a volinfo chain? */
585 if (blockMap[blockIndex]->entries[entryIndex] & MAP_VOLFRAGONVOL) {
586 Log("verifyVolInfoEntry: VolFrag (addr %d) on multiple volInfo chains\n", volFragAddr);
590 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLFRAGONVOL;
595 /* check computed vs. recorded number of fragments */
596 if (ccheck && misc->checkFragCount
597 && (ntohl(volInfo->nFrags) != volCount)) {
598 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) volFrag count of %d is wrong (should be %d)\n", volInfo->name, volInfoAddr, ntohl(volInfo->nFrags), volCount);
603 if (volCount > misc->maxVolsPerVolInfo)
604 misc->maxVolsPerVolInfo = volCount;
606 /* Check that all volInfo structures with same name point to the same
607 * head. If sameNameHead == 0, this is the head structure so we check,
610 if (volInfo->sameNameHead == 0) { /*i */
611 int viCount = 1; /* count this one */
615 for (tviAddr = ntohl(volInfo->sameNameChain); tviAddr;
616 tviAddr = ntohl(tvi.sameNameChain)) {
618 tcode = ConvertDiskAddress(tviAddr, &blockIndex, &entryIndex);
620 Log("verifyVolInfoEntry: Invalid volInfo entry %s addr 0x%x\n", volInfo->name, tviAddr);
621 Log(" Skipping remainder of volumes on same name chain\n");
628 tcode = dbread(ut, tviAddr, &tvi, sizeof(tvi));
632 if (ntohl(tvi.sameNameHead) != volInfoAddr) {
633 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) doesn't point to \n", volInfo->name, tviAddr);
634 Log(" head of sameName volInfo (addr 0x%x). Points to addr 0x%x\n", volInfoAddr, ntohl(tvi.sameNameHead));
639 if (blockMap[blockIndex]->entries[entryIndex] & MAP_VOLINFOONNAME) {
640 Log("verifyVolInfoEntry: VolInfo (addr 0x%x) on multiple same name chains\n", tviAddr);
644 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLINFOONNAME;
647 /* select the passed in structure flags */
648 if (blockMap[ai]->entries[ao] & MAP_VOLINFONAMEHEAD) {
649 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) is name head multiple times\n", volInfo->name, volInfoAddr);
653 blockMap[ai]->entries[ao] |= MAP_VOLINFONAMEHEAD;
655 if (viCount > misc->maxVolInfosPerName)
656 misc->maxVolInfosPerName = viCount;
667 /* ------------------------------------
668 * verification routines - general
669 * ------------------------------------
673 * Read each block header of every 2K block and remember it in our global
674 * blockMap array. Also check that the type of block is good.
677 verifyBlocks(struct ubik_trans *ut)
682 struct blockMap *ablockMap = 0;
685 afs_int32 code = 0, tcode;
687 /* Remember every header of every block in the database */
688 for (i = 0; i < nBlocks; i++) {
689 /* To avoid the call from timing out, do a poll every 256 blocks */
691 /* read the block header */
692 blockAddr = sizeof(db.h) + (i * BLOCKSIZE);
693 tcode = dbread(ut, blockAddr, (char *)&block.h, sizeof(block.h));
697 /* check the block type */
698 blocktype = block.h.type; /* char */
699 if ((blocktype < 0) || (blocktype >= NBLOCKTYPES)) {
700 Log("Block (index %d addr %d) has invalid type of %d\n", i,
701 blockAddr, blocktype);
702 ERROR(BUDB_BLOCKTYPE);
705 /* allocate the block map memory */
707 sizeof(*ablockMap) + (blockEntries[blocktype] -
708 1) * sizeof(ablockMap->entries[0]);
709 ablockMap = (struct blockMap *)malloc(bmsize);
712 memset(ablockMap, 0, bmsize);
714 ablockMap->nEntries = blockEntries[blocktype];
716 /* save the block header in the block map */
717 memcpy(&ablockMap->header, &block.h, sizeof(ablockMap->header));
718 blockMap[i] = ablockMap;
725 int minvols, maxvols, ttlvols;
727 /* verifyHashTableBlock
728 * Take a 2K hash table block and traverse its entries. Make sure each entry
729 * is of the correct type for the hash table, is hashed into the correct
730 * entry and is not threaded on multiple lists.
733 verifyHashTableBlock(struct ubik_trans *ut,
734 struct memoryHashTable *mhtPtr,
735 struct htBlock *htBlockPtr,
737 afs_int32 length, /* size of whole hash table */
738 int index, /* base index of this block */
741 int type; /* hash table type */
742 int entrySize; /* hashed entry size */
743 int blockType; /* block type for this hash table */
744 int blockIndex, entryIndex;
745 char entry[sizeof(struct block)];
747 int hash; /* calculated hash value for entry */
749 afs_int32 code = 0, tcode;
751 type = ntohl(mhtPtr->ht->functionType);
752 entrySize = sizeFunctions[type];
753 blockType = hashBlockType[type];
755 /* step through this hash table block, being careful to stop
756 * before the end of the overall hash table
759 for (i = 0; (i < nHTBuckets) && (index < length); i++, index++) { /*f */
760 entryAddr = ntohl(htBlockPtr->bucket[i]);
762 /* if this is the old hash table, all entries below the progress mark
763 * should have been moved to the new hash table
765 if (old && (index < mhtPtr->progress) && entryAddr) {
766 Log("Old hash table not empty (entry %d) below progress (%d)\n",
767 i, mhtPtr->progress);
772 /* now walk down the chain of each bucket */
773 for (count = 0; entryAddr; count++) { /*w */
774 tcode = ConvertDiskAddress(entryAddr, &blockIndex, &entryIndex);
776 Log("verifyHashTableBlock: Invalid hash table chain addr 0x%x\n", entryAddr);
777 Log(" Skipping remainder of bucket %d\n", index);
784 if (blockMap[blockIndex]->header.type != blockType) {
785 Log("Hash table chain (block index %d) incorrect\n",
787 Log(" Table type %d traverses block type %d\n", blockType,
788 blockMap[blockIndex]->header.type);
789 Log(" Skipping remainder of bucket %d\n", index);
795 if (dbread(ut, entryAddr, &entry[0], entrySize))
798 hash = ht_HashEntry(mhtPtr, &entry[0]) % length;
799 if (hash != index) { /* if it hashed to some other place */
800 Log("Entry (addr 0x%x) bucket %d, should be hashed into bucket %d\n", entryAddr, index, hash);
805 /* check if entry has been examined */
806 if (blockMap[blockIndex]->entries[entryIndex] & mapBit) {
807 Log("Entry (addr 0x%x) multiply referenced\n", entryAddr);
811 blockMap[blockIndex]->entries[entryIndex] |= mapBit;
813 entryAddr = ntohl(*((dbadr *) (entry + mhtPtr->threadOffset)));
816 /* Log("Bucket %4d contains %d entries\n", index+1, count); */
829 * Read each 2K block a hashtable has (both its old hastable and
830 * new hashtable) and verify the block has not been read before.
831 * Will also make call to verify entries within each 2K block of
835 verifyHashTable(struct ubik_trans *ut, struct memoryHashTable *mhtPtr,
838 struct hashTable *htPtr = mhtPtr->ht;
840 struct htBlock hashTableBlock;
841 int tableLength; /* # entries */
842 int hashblocks; /* # blocks */
843 dbadr tableAddr; /* disk addr of hash block */
844 int blockIndex, entryIndex;
847 afs_int32 code = 0, tcode;
849 extern int nHTBuckets; /* # buckets in a hash table */
851 LogDebug(4, "Htable: length %d oldlength %d progress %d\n",
852 mhtPtr->length, mhtPtr->oldLength, mhtPtr->progress);
854 /* check both old and current tables */
855 for (old = 0; old <= 1; old++) { /*fo */
856 tableLength = (old ? mhtPtr->oldLength : mhtPtr->length);
857 if (tableLength == 0)
859 tableAddr = (old ? ntohl(htPtr->oldTable) : ntohl(htPtr->table));
861 maxvols = ttlvols = 0;
863 /* follow the chain of hashtable blocks - avoid infinite loops */
864 hashblocks = ((tableLength - 1) / nHTBuckets) + 1; /* numb of 2K hashtable blocks */
865 for (i = 0; (tableAddr && (i < hashblocks + 5)); i++) {
866 tcode = ConvertDiskAddress(tableAddr, &blockIndex, &entryIndex);
868 Log("verifyHashTable: Invalid hash table chain addr 0x%x\n",
870 Log(" Skipping remainder of hash table chain\n");
877 if (blockMap[blockIndex]->header.type != hashTable_BLOCK) {
878 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);
879 Log(" Skipping remainder of hash table chain\n");
881 ERROR(BUDB_BLOCKTYPE);
885 /* check if we've examined this block before */
886 /* mark this (hash table) block as examined */
887 if (blockMap[blockIndex]->entries[entryIndex] & MAP_HTBLOCK) {
888 Log("Hash table block (index %d addr 0x%x) multiple ref\n",
891 ERROR(BUDB_DATABASEINCONSISTENT);
893 blockMap[blockIndex]->entries[entryIndex] |= MAP_HTBLOCK;
895 /* Read the actual hash table block */
897 dbread(ut, tableAddr, &hashTableBlock,
898 sizeof(hashTableBlock));
902 /* Verify its entries */
904 verifyHashTableBlock(ut, mhtPtr, &hashTableBlock, old,
905 tableLength, (i * nHTBuckets), mapBit);
909 tableAddr = ntohl(hashTableBlock.h.next);
912 /* Verify numb hash blocks is as it says */
913 if (i != hashblocks) {
914 Log("Incorrect number of hash chain blocks: %d (expected %d), hashtype %d\n", i, hashblocks, ntohl(htPtr->functionType));
916 ERROR(BUDB_DATABASEINCONSISTENT);
920 Log("%d entries; %u buckets; min = %d; max = %d\n", ttlvols,
921 tableLength, minvols, maxvols);
929 * do a linear walk of all the blocks. Check each block of structures
930 * to see if the actual free matches the recorded free. Also check
931 * the integrity of each allocated structure.
934 verifyEntryChains(struct ubik_trans *ut)
939 int blockIndex, entryIndex;
940 char entry[sizeof(struct block)];
945 static afs_int32(*checkEntry[NBLOCKTYPES]) (struct ubik_trans *,
946 afs_int32, int, int, void *)
948 /* FIXME: this list does not match typeName[] and may be incorrect */
950 verifyVolFragEntry, verifyVolInfoEntry, verifyTapeEntry, verifyDumpEntry, 0 /* text block */
953 for (blockIndex = 0; blockIndex < nBlocks; blockIndex++) { /*f */
954 /* ignore non-structure or blocks with invalid type */
955 type = blockMap[blockIndex]->header.type;
956 if ((type <= 0) || (type > MAX_STRUCTURE_BLOCK_TYPE))
959 entrySize = blockEntrySize[type];
962 for (entryIndex = 0; entryIndex < blockMap[blockIndex]->nEntries; entryIndex++) { /*f */
964 sizeof(db.h) + (blockIndex * BLOCKSIZE) +
965 sizeof(struct blockHeader) + (entryIndex * entrySize);
966 if (dbread(ut, offset, &entry[0], entrySize))
969 /* check if entry is free by looking at the first "afs_int32" of the structure */
970 memcpy(&start, entry, sizeof(start));
971 if (start == 0) { /* zero is free */
972 /* Is it on any hash chain? */
973 if (blockMap[blockIndex]->entries[entryIndex] & MAP_HASHES) {
974 Log("Entry: blockindex %d, entryindex %d - marked free but hashed 0x%x\n", blockIndex, entryIndex, blockMap[blockIndex]->entries[entryIndex]);
979 blockMap[blockIndex]->entries[entryIndex] |= MAP_FREE;
982 /* check the entry itself for consistency */
984 (*(checkEntry[type])) (ut, offset, blockIndex, entryIndex,
991 /* check computed free with recorded free entries */
992 if (nFree != ntohs(blockMap[blockIndex]->header.nFree)) {
993 Log("Block (index %d) free count %d has %d free structs\n",
994 blockIndex, ntohs(blockMap[blockIndex]->header.nFree), nFree);
1005 verifyFreeLists(void)
1009 int blockIndex, entryIndex;
1013 /* for each free list */
1014 for (i = 0; i < NBLOCKTYPES; i++) {
1015 misc->fullyFree[i] = misc->freeLength[i] = 0;
1017 for (addr = ntohl(db.h.freePtrs[i]); addr;
1018 addr = ntohl(blockMap[blockIndex]->header.next)) {
1019 misc->freeLength[i]++;
1021 code = ConvertDiskAddress(addr, &blockIndex, &entryIndex);
1022 if (code || (entryIndex != 0)) {
1023 Log("verifyFreeLists: Invalid free chain addr 0x%x in %s free chain\n", addr, TypeName(i));
1024 Log(" Skipping remainder of free chain\n");
1031 /* check block type */
1032 if (blockMap[blockIndex]->header.type != i) {
1033 Log("verifyFreeLists: Found %s type in %s free chain (addr 0x%x)\n",
1034 TypeName(blockMap[blockIndex]->header.type), TypeName(i),
1040 /* If entire block isn't free, check if count of free entries is ok */
1041 nFree = ntohs(blockMap[blockIndex]->header.nFree);
1042 if (i != free_BLOCK) {
1043 if ((nFree <= 0) || (nFree > blockEntries[i])) {
1044 Log("verifyFreeLists: Illegal free count %d on %s free chain\n", nFree, TypeName(i));
1047 } else if (nFree == blockEntries[i]) {
1048 misc->fullyFree[i]++;
1052 /* Check if already examined the block */
1053 if (blockMap[blockIndex]->free) {
1054 Log("verifyFreeLists: %s free chain block at addr 0x%x multiply threaded\n", TypeName(i), addr);
1058 blockMap[blockIndex]->free++;
1066 * Examines each entry to make sure it was traversed appropriately by
1067 * checking the bits for compatibility.
1072 int blockIndex, entryIndex, i, entrySize, type, bits;
1075 for (blockIndex = 0; blockIndex < nBlocks; blockIndex++) {
1076 /* If no entries in this block, then the block should be marked free */
1077 if ((blockMap[blockIndex]->nEntries == 0)
1078 && !blockMap[blockIndex]->free) {
1079 Log("verifyMapBits: Orphan free block (index %d)\n", blockIndex);
1084 /* check each entry */
1085 for (entryIndex = 0; entryIndex < blockMap[blockIndex]->nEntries; entryIndex++) { /*f */
1086 #ifndef AFS_PTHREAD_ENV
1087 if ((entryIndex % 1024) == 0)
1090 bits = blockMap[blockIndex]->entries[entryIndex];
1092 for (i = 0; i < NMAPCs; i++)
1093 if ((bits & mapC[i].trigger) == mapC[i].trigger)
1099 type = blockMap[blockIndex]->header.type;
1100 entrySize = blockEntrySize[type];
1102 sizeof(db.h) + (blockIndex * BLOCKSIZE) +
1103 sizeof(struct blockHeader) + (entryIndex * entrySize);
1105 sprintf(logstr, "%s entry Block %d, Entry %d, (addr 0x%x)",
1106 TypeName(type), blockIndex, entryIndex, offset);
1109 strcat(logstr, ": An orphaned entry");
1110 if (bits & MAP_FREE)
1111 strcat(logstr, ": A valid free block");
1112 if (bits & MAP_HTBLOCK)
1113 strcat(logstr, ": A valid hash-table block");
1114 if (bits & MAP_TEXTBLOCK)
1115 strcat(logstr, ": A valid text block");
1116 if (bits & (MAP_DUMPHASH | MAP_IDHASH)) {
1117 if (!(bits & MAP_DUMPHASH))
1119 ": A dump not on dump-name hash-chain");
1120 else if (!(bits & MAP_IDHASH))
1121 strcat(logstr, ": A dump not on dump-id hash-chain");
1123 strcat(logstr, ": A valid dump entry");
1125 if (bits & (MAP_TAPEHASH | MAP_TAPEONDUMP)) {
1126 if (!(bits & MAP_TAPEHASH))
1128 ": A tape not on tape-name hash-chain");
1129 else if (!(bits & MAP_TAPEONDUMP))
1130 strcat(logstr, ": A tape not associated with a dump");
1132 strcat(logstr, ": A valid tape entry");
1134 if (bits & MAP_VOLINFOONNAME)
1136 ": A valid volInfo on a volume-name chain");
1137 if (bits & (MAP_VOLINFONAMEHEAD | MAP_VOLHASH)) {
1138 if (!(bits & MAP_VOLINFONAMEHEAD))
1140 ": A volInfo not the head of a volume-name hash-chain");
1141 else if (!(bits & MAP_VOLHASH))
1143 ": A volInfo not on volume-name hash-chain");
1146 ": A valid volInfo in volume-name hash-chain");
1148 if (bits & (MAP_VOLFRAGONTAPE | MAP_VOLFRAGONVOL)) {
1149 if (!(bits & MAP_VOLFRAGONTAPE))
1151 ": A volFrag not associated with a tape");
1152 else if (!(bits & MAP_VOLFRAGONVOL))
1154 ": A volFrag not associated with a volume");
1156 strcat(logstr, ": A valid volFrag entry");
1158 Log("%s\n", logstr);
1170 verifyText(struct ubik_trans *ut)
1175 /* check each of the text types in use */
1176 for (i = 0; i < TB_NUM; i++) {
1177 Log("Verify Text: %s", textName[i]);
1178 code = verifyTextChain(ut, &db.h.textBlock[i]);
1186 * check the integrity of a text chain. Also checks the new chain.
1189 verifyTextChain(struct ubik_trans *ut, struct textBlock *tbPtr)
1192 int blockIndex, entryIndex;
1196 afs_int32 code = 0, tcode;
1198 for (new = 0; new < 2; new++) {
1200 blockAddr = ntohl(tbPtr->textAddr);
1203 (new ? ntohl(tbPtr->newTextAddr) : ntohl(tbPtr->textAddr));
1204 blockAddr; blockAddr = ntohl(block.h.next)) {
1205 tcode = ConvertDiskAddress(blockAddr, &blockIndex, &entryIndex);
1207 Log("verifyTextChain: Invalid %s text block addr 0x%x\n",
1208 (new ? "new" : ""), blockAddr);
1209 Log(" Skipping remainder of text chain\n");
1215 tcode = dbread(ut, blockAddr, &block, sizeof(block));
1219 if (blockMap[blockIndex]->entries[entryIndex] & MAP_TEXTBLOCK) {
1220 Log("verifyTextChain: Text block (addr 0x%x) multiply chained\n", blockAddr);
1224 blockMap[blockIndex]->entries[entryIndex] |= MAP_TEXTBLOCK;
1226 size += BLOCK_DATA_SIZE;
1229 if (ntohl(new ? tbPtr->newsize : tbPtr->size) > size) {
1230 Log("verifyTextChain: Text block %s size %d > computed capacity %d\n", (new ? "new" : ""), ntohl(new ? tbPtr->newsize : tbPtr->size), size);
1240 /* -----------------------------------------
1241 * verification driver routines
1242 * -----------------------------------------
1246 * Check the integrity of the database
1250 verifyDatabase(struct ubik_trans *ut,
1251 FILE *recreateFile) /* not used */
1255 afs_int32 code = 0, tcode = 0;
1257 extern int nBlocks; /* no. blocks in database */
1259 /* clear verification statistics */
1261 memset(&miscData, 0, sizeof(miscData));
1264 miscData.maxErrors = 1000000;
1266 miscData.maxErrors = 50; /* Catch the first 50 errors */
1268 miscData.veryLongChain = 0;
1269 miscData.checkFragCount = 1; /* check frags */
1272 eof = ntohl(db.h.eofPtr);
1273 eof -= sizeof(db.h); /* subtract header */
1274 nBlocks = eof / BLOCKSIZE;
1276 Log("Verify of backup database started\n");
1277 Log("Database is %u. %d blocks of %d Bytes\n", eof, nBlocks, BLOCKSIZE);
1279 if ((eof < 0) || (nBlocks * BLOCKSIZE != eof)) {
1280 Log("Database eofPtr (%d) bad, blocksize %d\n", eof, BLOCKSIZE);
1284 /* set size of database */
1285 miscData.nBlocks = nBlocks;
1288 ERROR(0); /* Nothing to check? */
1290 /* construct block map - first level is the array of pointers */
1291 bmsize = nBlocks * sizeof(struct blockMap *);
1292 blockMap = (struct blockMap **)malloc(bmsize);
1295 memset(blockMap, 0, bmsize);
1297 /* verify blocks and construct the block map */
1298 Log("Read header of every block\n");
1299 tcode = verifyBlocks(ut);
1303 /* check the various hash tables */
1304 Log("Verify volume name hash table\n");
1305 tcode = verifyHashTable(ut, &db.volName, MAP_VOLHASH);
1309 Log("Verify tape name hash table\n");
1310 tcode = verifyHashTable(ut, &db.tapeName, MAP_TAPEHASH);
1314 Log("Verify dump name hash table\n");
1315 tcode = verifyHashTable(ut, &db.dumpName, MAP_DUMPHASH);
1319 Log("Verify dump id hash table\n");
1320 tcode = verifyHashTable(ut, &db.dumpIden, MAP_IDHASH);
1324 /* check the entry chains */
1325 Log("Verify all blocks and entries\n");
1326 tcode = verifyEntryChains(ut);
1330 /* check text blocks - Log message in verifyText */
1331 tcode = verifyText(ut);
1335 /* check free list */
1336 Log("Verify Free Lists\n");
1337 tcode = verifyFreeLists();
1341 /* check entry map bit compatibility */
1343 Log("Verify Map bits\n");
1344 tcode = verifyMapBits();
1349 /* free the block map */
1350 if (blockMap != 0) {
1353 /* free all the individual maps */
1354 for (i = 0; i < nBlocks; i++) {
1359 /* free the pointer array */
1365 Log("# 2K database blocks = %d\n", miscData.nBlocks);
1366 Log("# Dump entries found = %d. 3 dumps per block\n",
1368 Log(" max tapes on a dump = %d\n", miscData.maxTapesPerDump);
1369 Log(" max volumes on a dump = %d\n", miscData.maxVolsPerDump);
1370 Log(" max appends on a dump = %d\n", miscData.maxAppendsPerDump);
1371 Log(" # Blocks with space = %d\n", miscData.freeLength[4]);
1372 Log(" # of those fully free = %d\n", miscData.fullyFree[4]);
1373 Log("# Tape entries found = %d. 20 tapes per block\n",
1375 Log(" max volumes on a tape = %d\n", miscData.maxVolsPerTape);
1376 Log(" # Blocks with space = %d\n", miscData.freeLength[3]);
1377 Log(" # of those fully free = %d\n", miscData.fullyFree[3]);
1378 Log("# VolInfo entries found = %d. 20 volInfos per block\n",
1380 Log(" # head of sameNameCh = %d\n", miscData.nVolName);
1381 Log(" max on a sameNameCh = %d\n", miscData.maxVolInfosPerName);
1382 Log(" max VolFrags on chain = %d\n", miscData.maxVolsPerVolInfo);
1383 Log(" # Blocks with space = %d\n", miscData.freeLength[2]);
1384 Log(" # of those fully free = %d\n", miscData.fullyFree[2]);
1385 Log("# VolFrag entries found = %d. 45 VolFrags per block\n",
1387 Log(" # Blocks with space = %d\n", miscData.freeLength[1]);
1388 Log(" # of those fully free = %d\n", miscData.fullyFree[1]);
1389 Log("# free blocks = %d\n", miscData.freeLength[0]);
1392 Log("Verify of database completed. %d errors found\n", miscData.errors);
1394 if (miscData.errors && !code)
1400 /* -----------------------------
1401 * interface routines
1402 * -----------------------------
1406 * check the integrity of the database
1408 * status - integrity: 0, ok; n, not ok (error value)
1409 * orphans - no. of orphan blocks
1410 * host - address of host that did verification
1413 SBUDB_DbVerify(struct rx_call *call, afs_int32 *status, afs_int32 *orphans,
1418 code = DbVerify(call, status, orphans, host);
1419 osi_auditU(call, BUDB_DBVfyEvent, code, AUD_END);
1424 DbVerify(struct rx_call *call, afs_int32 *status, afs_int32 *orphans,
1427 struct ubik_trans *ut = 0;
1428 afs_int32 code = 0, tcode;
1432 if (callPermitted(call) == 0)
1433 ERROR(BUDB_NOTPERMITTED);
1435 tcode = InitRPC(&ut, LOCKREAD, 1);
1439 tcode = verifyDatabase(ut, 0); /* check the database */
1446 ubik_AbortTrans(ut);
1448 code = ubik_EndTrans(ut);
1454 gethostname(hostname, sizeof(hostname));
1455 th = gethostbyname(hostname);
1459 memcpy(host, th->h_addr, sizeof(afs_int32));
1460 *host = ntohl(*host);
1466 /* ----------------------
1468 * ----------------------
1472 * do a simple sanity check on the database header
1475 check_header(char *callerst)
1477 static int iteration_count = 0;
1480 eof = ntohl(db.h.eofPtr);
1481 if ((eof == 0) || (eof < 0)) {
1482 Log("Eof check failed, caller %s, eof 0x%x\n", callerst, eof);
1485 eof -= sizeof(db.h);
1487 Log("Adjusted Eof check failed, caller %s, eof 0x%x\n", callerst,
1492 if (iteration_count >= 10) {
1493 Log("Eof ptr is 0x%x\n", eof);
1494 iteration_count = 0;