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 <afs/param.h>
17 #include <netinet/in.h>
21 #include <sys/types.h>
25 #include "error_macros.h"
26 #include "budb_errs.h"
27 #include <afs/cellconfig.h>
28 #include "afs/audit.h"
34 * 1) volInfo structures refering to a volume of the same name are
35 * chained together, i.e. the volumes described differ in volid, partition
36 * etc. The structure at the head of this list (the sameNameChain) is
37 * treated specially. When a delete volInfo request is processed, heads
38 * are not deleted unless all other items on the sameNameChain are gone.
40 * The result is that volInfo (head) structures may be present
41 * even if no tape structures refer to them. These structures are
42 * unreachable in a top-down tree walk.
44 * 1) make the verify tolerant of errors. Want to get a summary statistic
45 * indicating how may dumps are lost and how many text blocks lost
46 * 2) Make the recreation instructions write out whatever is good. This
47 * is only for the off-line case.
50 /* flags associated with each structure. These are set and checked in
51 * the blockMap entries
54 #define MAP_DUMPHASH 1 /* dump name hash checked */
55 #define MAP_TAPEHASH 2 /* tape name hash checked */
56 #define MAP_VOLHASH 4 /* volume name hash checked */
57 #define MAP_IDHASH 8 /* dump id hash checked */
59 #define MAP_HASHES (MAP_DUMPHASH | MAP_TAPEHASH | MAP_VOLHASH | MAP_IDHASH)
61 #define MAP_FREE 0x10 /* item is free */
62 #define MAP_RECREATE 0x20
63 #define MAP_HTBLOCK 0x40 /* hash table block */
64 #define MAP_TAPEONDUMP 0x100
65 #define MAP_VOLFRAGONTAPE 0x200
66 #define MAP_VOLFRAGONVOL 0x400
67 #define MAP_VOLINFOONNAME 0x800
68 #define MAP_VOLINFONAMEHEAD 0x1000
69 #define MAP_TEXTBLOCK 0x2000 /* block of text */
70 #define MAP_APPENDEDDUMP 0x4000
72 /* one blockMap for every block in the database. Each element of the entries
73 * array describes the status of a data structure/entry in that block
78 struct blockHeader header; /* copy of the block header */
79 char free; /* on free list */
80 int nEntries; /* size of the entries arrays */
81 afs_uint32 entries[1]; /* describes each entry */
84 /* status for verify call */
87 char hostname[64]; /* host on which checked */
88 afs_int32 status; /* ok, not ok */
91 int nBlocks; /* total number of blocks in db */
93 struct misc_hash_stats { /* stats for hashing */
94 int max; /* longest chain length */
95 double avg; /* avg length */
96 double std_dev; /* standard deviation of length */
100 int errors; /* errors encountered */
101 int maxErrors; /* abort after this many errors */
102 int nBlocks; /* number of database blocks */
103 int nDump, nTape, nVolInfo, nVolFrag; /* counts of each type */
104 int nVolName; /* volInfos w/ head==0 */
105 int maxVolsPerVolInfo; /* maximum list lengths */
108 int maxVolInfosPerName;
110 int maxAppendsPerDump;
111 int freeLength[NBLOCKTYPES]; /* length of free lists */
112 int fullyFree[NBLOCKTYPES]; /* free blocks full of free entries */
113 int veryLongChain; /* length of chain to report */
114 int checkFragCount; /* report fragment count errors */
115 struct misc_hash_stats dumpName, dumpIden, tapeName, volName;
116 FILE *recreate; /* stream for recreate instructions */
118 struct misc_data *misc;
120 struct blockMap **blockMap = 0; /* initial block map */
122 /* describes number of entries for each type of block */
124 int blockEntries[NBLOCKTYPES] =
131 1, /* hashTable_BLOCK */
135 int blockEntrySize[NBLOCKTYPES] =
138 sizeof(((struct vfBlock *)0)->a[0]),
139 sizeof(((struct viBlock *)0)->a[0]),
140 sizeof(((struct tBlock *)0)->a[0]),
141 sizeof(((struct dBlock *)0)->a[0]),
144 char *typeName[NBLOCKTYPES] =
154 int hashBlockType[HT_MAX_FUNCTION+1] =
163 /* Compatibility table for the bits in the blockMap. */
165 struct mapCompatability
167 short trigger; /* these bits trigger this element */
172 MAP_DUMPHASH | MAP_IDHASH,
173 MAP_TAPEHASH | MAP_TAPEONDUMP,
175 MAP_VOLINFONAMEHEAD | MAP_VOLHASH,
176 MAP_VOLFRAGONTAPE | MAP_VOLFRAGONVOL,
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] =
192 extern int sizeFunctions[];
193 extern int nHTBuckets;
195 #define DBBAD BUDB_DATABASEINCONSISTENT
197 /* ------------------------------------
198 * supporting routines
199 * ------------------------------------
203 * increment the error count
206 * 1 - maximum errors exceeded
212 if ( ++miscData.errors >= miscData.maxErrors )
218 /* convertDiskAddress
219 * given a disk address, break it down into a block and entry index. These
220 * permit access to the block map information. The conversion process
221 * compares the supplied address with the alignment/type information
222 * stored in the block map.
225 * BUDB_ADDR - address alignment checks failed
229 checkDiskAddress(address, type, blockIndexPtr, entryIndexPtr)
230 unsigned long address;
237 if (blockIndexPtr) *blockIndexPtr = -1;
238 if (entryIndexPtr) *entryIndexPtr = -1;
240 /* This is safest way to handle totally bogus addresses (eg 0x80001234). */
241 if ( (address < sizeof(db.h)) || (address >= ntohl(db.h.eofPtr)) )
244 address -= sizeof(db.h);
245 index = address / BLOCKSIZE;
246 offset = address - (index * BLOCKSIZE);
247 if (offset % sizeof(afs_int32)) /* alignment check */
249 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])
258 if (blockIndexPtr) *blockIndexPtr = index;
259 if (entryIndexPtr) *entryIndexPtr = offset;
263 /* ConvertDiskAddress
264 * given a disk address, break it down into a block and entry index. These
265 * permit access to the block map information. The conversion process
266 * compares the supplied address with the alignment/type information
267 * stored in the block map.
270 * BUDB_ADDR - address alignment checks failed
274 ConvertDiskAddress(address, blockIndexPtr, entryIndexPtr)
282 index = (address - sizeof(db.h)) / BLOCKSIZE;
283 type = blockMap[index]->header.type;
285 code = checkDiskAddress(address, type, blockIndexPtr, entryIndexPtr);
293 static char error[16];
295 if ( (index < 0) || (index >= NBLOCKTYPES))
297 sprintf(error, "UNKNOWN_TYPE", index);
300 return(typeName[index]);
303 getDumpID(ut, tapePtr, dumpID)
304 struct tape *tapePtr;
311 code = dbread(ut, ntohl(tapePtr->dump), &d, sizeof(d));
313 *dumpID = ntohl(d.id);
316 /* ------------------------------------
317 * verification routines - structure specific
318 * ------------------------------------
322 * Follow the tapes entries hanging off of a dump and verify they belong
326 verifyDumpEntry( ut, dumpAddr, ai, ao, dumpPtr)
327 struct ubik_trans *ut;
330 struct dump *dumpPtr;
333 afs_int32 tapeAddr, tapeCount=0, volCount=0, appDumpCount=0;
334 afs_int32 appDumpAddr, appDumpIndex, appDumpOffset;
336 int tapeIndex, tapeOffset, ccheck=1;
337 afs_int32 code = 0, tcode;
338 int dumpIndex, dumpOffset;
340 tcode = ConvertDiskAddress(dumpAddr, &dumpIndex, &dumpOffset);
343 Log("verifyDumpEntry: Invalid dump entry addr 0x%x\n", dumpAddr);
344 if (BumpErrors()) ERROR(DBBAD);
348 /* Step though list of tapes hanging off of this dump */
349 for (tapeAddr=ntohl(dumpPtr->firstTape); tapeAddr; tapeAddr=ntohl(tape.nextTape))
351 tcode = ConvertDiskAddress(tapeAddr, &tapeIndex, &tapeOffset);
354 Log("verifyDumpEntry: Invalid tape entry addr 0x%x (on DumpID %u)\n",
355 tapeAddr, ntohl(dumpPtr->id));
356 Log(" Skipping remainder of tapes in dump\n");
357 if (BumpErrors()) ERROR(DBBAD);
362 tcode = dbread(ut, tapeAddr, &tape, sizeof(tape));
363 if (tcode) ERROR(BUDB_IO);
365 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",
373 ntohl(dumpPtr->id), dumpAddr, did, ntohl(tape.dump));
374 if (BumpErrors()) return(DBBAD);
377 /* Check if this tape entry has been examine already */
378 if ( blockMap[tapeIndex]->entries[tapeOffset] & MAP_TAPEONDUMP )
380 Log("verifyDumpEntry: Tape '%s' (addr 0x%x) on multiple dumps\n",
381 tape.name, tapeAddr);
382 if (BumpErrors()) return(DBBAD);
384 blockMap[tapeIndex]->entries[tapeOffset] |= MAP_TAPEONDUMP;
387 volCount += ntohl(tape.nVolumes);
390 if (ccheck && (ntohl(dumpPtr->nVolumes) != volCount))
392 Log("verifyDumpEntry: DumpID %u (addr 0x%x) volume count of %d is wrong (should be %d)\n",
393 ntohl(dumpPtr->id), dumpAddr, ntohl(dumpPtr->nVolumes), volCount);
394 if (BumpErrors()) return(DBBAD);
397 if (volCount > misc->maxVolsPerDump) misc->maxVolsPerDump = volCount;
398 if (tapeCount > misc->maxTapesPerDump) misc->maxTapesPerDump = tapeCount;
400 /* If this is an initial dump, then step though list of appended dumps
401 * hanging off of this dump.
403 if (ntohl(dumpPtr->initialDumpID) == 0) {
404 for (appDumpAddr=ntohl(dumpPtr->appendedDumpChain); appDumpAddr;
405 appDumpAddr=ntohl(appDump.appendedDumpChain)) {
407 tcode = ConvertDiskAddress(appDumpAddr, &appDumpIndex, &appDumpOffset);
409 Log("verifyDumpEntry: Invalid appended dump entry addr 0x%x\n", appDumpAddr);
410 Log("Skipping remainder of appended dumps\n");
411 if (BumpErrors()) ERROR(DBBAD);
415 /* Read the appended dump in */
416 tcode = dbread(ut, appDumpAddr, &appDump, sizeof(appDump));
417 if (tcode) ERROR(BUDB_IO);
419 /* Verify that it points to the parent dump */
420 if (ntohl(appDump.initialDumpID) != ntohl(dumpPtr->id)) {
421 Log("verifyDumpEntry: DumpID %u (addr 0x%x) initial DumpID incorrect (is %u, should be %u)\n",
422 ntohl(appDump.id), appDumpAddr,
423 ntohl(appDump.initialDumpID), ntohl(dumpPtr->id));
424 if (BumpErrors()) return(DBBAD);
427 /* Check if this appended dump entry has been examined already */
428 if ( blockMap[appDumpIndex]->entries[appDumpOffset] & MAP_APPENDEDDUMP ) {
429 Log("verifyDumpEntry: DumpID %u (addr %u) is on multiple appended dump chains\n",
430 ntohl(appDump.id), appDumpAddr);
431 Log("Skipping remainder of appended dumps\n");
432 if (BumpErrors()) return(DBBAD);
435 blockMap[appDumpIndex]->entries[appDumpOffset] |= MAP_APPENDEDDUMP;
441 if (appDumpCount > misc->maxAppendsPerDump) misc->maxAppendsPerDump = appDumpCount;
450 * Follw the volume fragments hanging off of a tape entry and verify
451 * they belong to the tape.
454 verifyTapeEntry (ut, tapeAddr, ai, ao, tapePtr)
455 struct ubik_trans *ut;
458 struct tape *tapePtr;
460 int volCount = 0, ccheck=1;
461 afs_int32 volFragAddr;
462 int blockIndex, entryIndex;
463 struct volFragment volFragment;
464 afs_int32 code = 0, tcode;
466 for (volFragAddr=ntohl(tapePtr->firstVol);
468 volFragAddr=ntohl(volFragment.sameTapeChain))
470 tcode = ConvertDiskAddress(volFragAddr, &blockIndex, &entryIndex);
475 getDumpID(ut, tapePtr, &did);
476 Log("verifyTapeEntry: Invalid volFrag addr 0x%x (on tape '%s' DumpID %u)\n",
477 volFragAddr, tapePtr->name, did);
478 Log(" Skipping remainder of volumes on tape\n");
479 if (BumpErrors()) ERROR(DBBAD);
484 tcode = dbread (ut, volFragAddr, &volFragment, sizeof(volFragment));
485 if (tcode) ERROR(tcode);
487 if ( ntohl(volFragment.tape) != tapeAddr )
491 getDumpID(ut, tapePtr, &did);
492 Log("verifyTapeEntry: VolFrag (addr 0x%x) doesn't point to \n",
494 Log (" tape '%s' DumpID %u (addr 0x%x). Points to addr 0x%x\n",
495 tapePtr->name, did, tapeAddr, ntohl(volFragment.tape));
496 if (BumpErrors()) ERROR(DBBAD);
499 /* Has this volume fragment already been examined */
500 if ( blockMap[blockIndex]->entries[entryIndex] & MAP_VOLFRAGONTAPE )
502 Log("verifyTapeEntry: VolFrag (addr %d) on multiple tapes\n",
504 if (BumpErrors()) ERROR(DBBAD);
506 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLFRAGONTAPE;
511 /* now check computed vs. recorded volume counts */
512 if (ccheck && (ntohl(tapePtr->nVolumes) != volCount))
516 getDumpID(ut, tapePtr, &did);
517 Log("verifyTapeEntry: Tape '%s' DumpID %u (addr 0x%x) volFrag count of %d is wrong (should be %d)\n",
518 tapePtr->name, did, tapeAddr, ntohl(tapePtr->nVolumes), volCount);
519 if (BumpErrors()) ERROR(DBBAD);
522 if (volCount > misc->maxVolsPerTape) misc->maxVolsPerTape = volCount;
531 * volume fragments are the lowest leaf describing a dump (nothing hangs off of it).
532 * So no check is done agaist it.
535 verifyVolFragEntry (ut, va, ai, ao, v)
536 struct ubik_trans *ut;
539 struct volFragment *v;
545 /* verifyVolInfoEntry
546 * Follow the volume fragments hanging off of a volinfo structure and
547 * verify they belong to the volinfo structure.
548 * If the volinfo structure is at the head of the same name chain, then
549 * also verify all entries are also on the chain.
552 verifyVolInfoEntry (ut, volInfoAddr, ai, ao, volInfo)
553 struct ubik_trans *ut;
554 afs_int32 volInfoAddr;
556 struct volInfo *volInfo;
558 int volCount = 0, ccheck=1;
560 afs_int32 volFragAddr;
561 int blockIndex, entryIndex, bindex, eindex;
562 struct volFragment volFragment;
563 afs_int32 code = 0, tcode;
565 /* check each fragment attached to this volinfo structure */
566 for (volFragAddr=ntohl(volInfo->firstFragment); volFragAddr;
567 volFragAddr=ntohl(volFragment.sameNameChain))
569 tcode = ConvertDiskAddress (volFragAddr, &blockIndex, &entryIndex);
572 Log("verifyVolInfoEntry: Invalid volFrag entry addr 0x%x (on volume '%s')\n",
573 volFragAddr, volInfo->name);
574 Log(" Skipping remainder of volumes on tape\n");
575 if (BumpErrors()) ERROR(DBBAD);
580 tcode = dbread(ut, volFragAddr, &volFragment, sizeof(volFragment));
581 if (tcode) ERROR(tcode);
583 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));
589 if (BumpErrors()) ERROR(DBBAD);
592 /* volume fragment already on a volinfo chain? */
593 if ( blockMap[blockIndex]->entries[entryIndex] & MAP_VOLFRAGONVOL )
595 Log("verifyVolInfoEntry: VolFrag (addr %d) on multiple volInfo chains\n",
597 if (BumpErrors()) ERROR(DBBAD);
599 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLFRAGONVOL;
604 /* check computed vs. recorded number of fragments */
605 if (ccheck && misc->checkFragCount && (ntohl(volInfo->nFrags) != volCount))
607 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) volFrag count of %d is wrong (should be %d)\n",
608 volInfo->name, volInfoAddr, ntohl(volInfo->nFrags), volCount);
609 if (BumpErrors()) ERROR(DBBAD);
612 if (volCount > misc->maxVolsPerVolInfo) misc->maxVolsPerVolInfo = volCount;
614 /* Check that all volInfo structures with same name point to the same
615 * head. If sameNameHead == 0, this is the head structure so we check,
618 if (volInfo->sameNameHead == 0)
620 int viCount = 1; /* count this one */
624 for (tviAddr=ntohl(volInfo->sameNameChain); tviAddr; tviAddr = ntohl(tvi.sameNameChain))
627 tcode = ConvertDiskAddress (tviAddr, &blockIndex, &entryIndex);
630 Log("verifyVolInfoEntry: Invalid volInfo entry %s addr 0x%x\n",
631 volInfo->name, tviAddr);
632 Log(" Skipping remainder of volumes on same name chain\n");
633 if (BumpErrors()) ERROR(DBBAD);
638 tcode = dbread(ut, tviAddr, &tvi, sizeof(tvi));
639 if (tcode) ERROR(tcode);
641 if ( ntohl(tvi.sameNameHead) != volInfoAddr)
643 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) doesn't point to \n",
644 volInfo->name, tviAddr);
645 Log(" head of sameName volInfo (addr 0x%x). Points to addr 0x%x\n",
646 volInfoAddr, ntohl(tvi.sameNameHead));
647 if (BumpErrors()) ERROR(DBBAD);
650 if (blockMap[blockIndex]->entries[entryIndex] & MAP_VOLINFOONNAME)
652 Log("verifyVolInfoEntry: VolInfo (addr 0x%x) on multiple same name chains\n",
654 if (BumpErrors()) ERROR(DBBAD);
656 blockMap[blockIndex]->entries[entryIndex] |= MAP_VOLINFOONNAME;
659 /* select the passed in structure flags */
660 if ( blockMap[ai]->entries[ao] & MAP_VOLINFONAMEHEAD)
662 Log("verifyVolInfoEntry: VolInfo '%s' (addr 0x%x) is name head multiple times\n",
663 volInfo->name, volInfoAddr);
664 if (BumpErrors()) ERROR(DBBAD);
666 blockMap[ai]->entries[ao] |= MAP_VOLINFONAMEHEAD;
668 if (viCount > misc->maxVolInfosPerName) 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++)
703 /* To avoid the call from timing out, do a poll every 256 blocks */
705 /* read the block header */
706 blockAddr = sizeof(db.h) + (i * BLOCKSIZE);
707 tcode = dbread(ut, blockAddr, (char *) &block.h, sizeof(block.h));
708 if (tcode) ERROR(tcode);
710 /* check the block type */
711 blocktype = block.h.type; /* char */
712 if ( (blocktype < 0) || (blocktype >= NBLOCKTYPES) )
714 Log("Block (index %d addr %d) has invalid type of %d\n", i, blockAddr, blocktype);
715 ERROR(BUDB_BLOCKTYPE);
718 /* allocate the block map memory */
719 bmsize = sizeof(*ablockMap) + (blockEntries[blocktype]-1) * sizeof(ablockMap->entries[0]);
720 ablockMap = (struct blockMap *) malloc(bmsize);
721 if (!ablockMap) ERROR(BUDB_NOMEM);
722 bzero(ablockMap, bmsize);
724 ablockMap->nEntries = blockEntries[blocktype];
726 /* save the block header in the block map */
727 bcopy(&block.h, &ablockMap->header, 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++ )
772 entryAddr = ntohl(htBlockPtr->bucket[i]);
774 /* if this is the old hash table, all entries below the progress mark
775 * should have been moved to the new hash table
777 if (old && (index < mhtPtr->progress) && entryAddr)
779 Log("Old hash table not empty (entry %d) below progress (%d)\n",
780 i, mhtPtr->progress);
781 if (BumpErrors()) ERROR(DBBAD);
784 /* now walk down the chain of each bucket */
785 for (count=0; entryAddr; count++)
787 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);
792 if (BumpErrors()) ERROR(DBBAD);
797 if (blockMap[blockIndex]->header.type != blockType)
799 Log("Hash table chain (block index %d) incorrect\n", blockIndex);
800 Log(" Table type %d traverses block type %d\n",
801 blockType, blockMap[blockIndex]->header.type);
802 Log(" Skipping remainder of bucket %d\n", index);
803 if (BumpErrors()) ERROR(DBBAD);
807 if ( dbread(ut, entryAddr, &entry[0], entrySize) )
810 hash = ht_HashEntry(mhtPtr, &entry[0]) % length;
811 if (hash != index) /* if it hashed to some other place */
813 Log("Entry (addr 0x%x) bucket %d, should be hashed into bucket %d\n",
814 entryAddr, index, hash);
815 if (BumpErrors()) ERROR(DBBAD);
818 /* check if entry has been examined */
819 if (blockMap[blockIndex]->entries[entryIndex] & mapBit)
821 Log("Entry (addr 0x%x) multiply referenced\n", entryAddr);
822 if (BumpErrors()) ERROR(DBBAD);
824 blockMap[blockIndex]->entries[entryIndex] |= mapBit;
826 entryAddr = ntohl( *((dbadr *)(entry + mhtPtr->threadOffset)));
829 /* Log("Bucket %4d contains %d entries\n", index+1, count); */
831 if (count < minvols) minvols = count;
832 if (count > maxvols) maxvols = 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++ )
870 tableLength = (old ? mhtPtr->oldLength : mhtPtr->length);
871 if (tableLength == 0) continue;
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++)
880 tcode = ConvertDiskAddress(tableAddr, &blockIndex, &entryIndex);
883 Log("verifyHashTable: Invalid hash table chain addr 0x%x\n", tableAddr);
884 Log(" Skipping remainder of hash table chain\n");
885 if (BumpErrors()) return(DBBAD);
890 if ( blockMap[blockIndex]->header.type != hashTable_BLOCK )
892 Log("Hashtable block (index %d addr 0x%x) hashtype %d - type %d, expected type %d\n",
893 i+1, tableAddr, ntohl(htPtr->functionType),
894 blockMap[blockIndex]->header.type, hashTable_BLOCK);
895 Log(" Skipping remainder of hash table chain\n");
896 if (BumpErrors()) ERROR(BUDB_BLOCKTYPE);
900 /* check if we've examined this block before */
901 /* mark this (hash table) block as examined */
902 if (blockMap[blockIndex]->entries[entryIndex] & MAP_HTBLOCK)
904 Log("Hash table block (index %d addr 0x%x) multiple ref\n", i+1, tableAddr);
905 if (BumpErrors()) ERROR(BUDB_DATABASEINCONSISTENT);
907 blockMap[blockIndex]->entries[entryIndex] |= MAP_HTBLOCK;
909 /* Read the actual hash table block */
910 tcode = dbread(ut, tableAddr, &hashTableBlock, sizeof(hashTableBlock));
911 if (tcode) ERROR(tcode);
913 /* Verify its entries */
914 tcode = verifyHashTableBlock(ut, mhtPtr, &hashTableBlock, old,
915 tableLength, (i*nHTBuckets), mapBit);
916 if (tcode) ERROR(tcode);
918 tableAddr = ntohl(hashTableBlock.h.next);
921 /* Verify numb hash blocks is as it says */
924 Log("Incorrect number of hash chain blocks: %d (expected %d), hashtype %d\n",
925 i, hashblocks, ntohl(htPtr->functionType));
926 if (BumpErrors()) ERROR(BUDB_DATABASEINCONSISTENT);
930 Log("%d entries; %u buckets; min = %d; max = %d\n",
931 ttlvols, tableLength, minvols, maxvols);
939 * do a linear walk of all the blocks. Check each block of structures
940 * to see if the actual free matches the recorded free. Also check
941 * the integrity of each allocated structure.
944 verifyEntryChains(ut)
945 struct ubik_trans *ut;
949 int blockIndex, entryIndex;
950 char entry[sizeof(struct block)];
955 static afs_int32 (*checkEntry[NBLOCKTYPES])()
956 = { 0, /* free block */
964 for (blockIndex=0; blockIndex<nBlocks; blockIndex++)
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++)
976 offset = 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 */
984 /* Is it on any hash chain? */
985 if ( blockMap[blockIndex]->entries[entryIndex] & MAP_HASHES )
987 Log("Entry: blockindex %d, entryindex %d - marked free but hashed 0x%x\n",
988 blockIndex, entryIndex, blockMap[blockIndex]->entries[entryIndex]);
989 if (BumpErrors()) return DBBAD;
992 blockMap[blockIndex]->entries[entryIndex] |= MAP_FREE;
997 /* check the entry itself for consistency */
998 code = (*(checkEntry[type]))(ut, offset, blockIndex, entryIndex, &entry[0]);
999 if (code) return code;
1003 /* check computed free with recorded free entries */
1004 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);
1008 if (BumpErrors()) return DBBAD;
1021 int blockIndex, entryIndex;
1025 /* for each free list */
1026 for (i=0; i<NBLOCKTYPES; i++)
1028 misc->fullyFree[i] = misc->freeLength[i] = 0;
1030 for (addr=ntohl(db.h.freePtrs[i]); addr; addr=ntohl(blockMap[blockIndex]->header.next))
1032 misc->freeLength[i]++;
1034 code = ConvertDiskAddress (addr, &blockIndex, &entryIndex);
1035 if (code || (entryIndex != 0))
1037 Log("verifyFreeLists: Invalid free chain addr 0x%x in %s free chain\n",
1039 Log(" Skipping remainder of free chain\n");
1040 if (BumpErrors()) return(DBBAD);
1045 /* check block type */
1046 if (blockMap[blockIndex]->header.type != i)
1048 Log("verifyFreeLists: Found %s type in %s free chain\n",
1049 TypeName(blockMap[blockIndex]->header.type), TypeName(i), addr);
1050 if (BumpErrors()) return(DBBAD);
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)
1057 if ((nFree <= 0) || (nFree > blockEntries[i]))
1059 Log ("verifyFreeLists: Illegal free count %d on %s free chain\n",
1060 nFree, TypeName(i));
1061 if (BumpErrors()) return(DBBAD);
1063 else if (nFree == blockEntries[i])
1065 misc->fullyFree[i]++;
1069 /* Check if already examined the block */
1070 if (blockMap[blockIndex]->free)
1072 Log("verifyFreeLists: %s free chain block at addr 0x%x multiply threaded\n",
1074 if (BumpErrors()) return DBBAD;
1076 blockMap[blockIndex]->free++;
1084 * Examines each entry to make sure it was traversed appropriately by
1085 * checking the bits for compatibility.
1090 int blockIndex, entryIndex, i, entrySize, type, bits;
1093 for (blockIndex=0; blockIndex<nBlocks; blockIndex++)
1095 /* If no entries in this block, then the block should be marked free */
1096 if ((blockMap[blockIndex]->nEntries == 0) && !blockMap[blockIndex]->free)
1098 Log("verifyMapBits: Orphan free block (index %d)\n", blockIndex);
1099 if (BumpErrors()) return DBBAD;
1102 /* check each entry */
1103 for (entryIndex=0; entryIndex<blockMap[blockIndex]->nEntries; entryIndex++)
1105 if ((entryIndex%1024) == 0) IOMGR_Poll();
1107 bits = blockMap[blockIndex]->entries[entryIndex];
1109 for (i=0; i<NMAPCs; i++)
1110 if ((bits & mapC[i].trigger) == mapC[i].trigger) break;
1116 type = blockMap[blockIndex]->header.type;
1117 entrySize = blockEntrySize[type];
1118 offset = sizeof(db.h) + (blockIndex * BLOCKSIZE) +
1119 sizeof(struct blockHeader) + (entryIndex * entrySize);
1121 sprintf(logstr, "%s entry Block %d, Entry %d, (addr 0x%x)",
1122 TypeName(type), blockIndex, entryIndex, offset);
1124 if (!bits) strcat(logstr, ": An orphaned entry");
1125 if (bits & MAP_FREE) strcat(logstr, ": A valid free block");
1126 if (bits & MAP_HTBLOCK) strcat(logstr, ": A valid hash-table block");
1127 if (bits & MAP_TEXTBLOCK) strcat(logstr, ": A valid text block");
1128 if (bits & (MAP_DUMPHASH | MAP_IDHASH))
1130 if (!(bits & MAP_DUMPHASH))
1131 strcat(logstr, ": 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))
1139 if (!(bits & MAP_TAPEHASH))
1140 strcat(logstr, ": 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)
1147 strcat(logstr, ": A valid volInfo on a volume-name chain");
1148 if (bits & (MAP_VOLINFONAMEHEAD | MAP_VOLHASH))
1150 if (!(bits & MAP_VOLINFONAMEHEAD))
1151 strcat(logstr, ": A volInfo not the head of a volume-name hash-chain");
1152 else if (!(bits & MAP_VOLHASH))
1153 strcat(logstr, ": A volInfo not on volume-name hash-chain");
1155 strcat(logstr, ": A valid volInfo in volume-name hash-chain");
1157 if (bits & (MAP_VOLFRAGONTAPE | MAP_VOLFRAGONVOL))
1159 if (!(bits & MAP_VOLFRAGONTAPE))
1160 strcat(logstr, ": A volFrag not associated with a tape");
1161 else if (!(bits & MAP_VOLFRAGONVOL))
1162 strcat(logstr, ": A volFrag not associated with a volume");
1164 strcat(logstr, ": A valid volFrag entry");
1166 Log("%s\n", logstr);
1168 if (BumpErrors()) return DBBAD;
1178 struct ubik_trans *ut;
1182 extern afs_int32 verifyTextChain();
1184 /* check each of the text types in use */
1185 for (i=0; i<TB_NUM; i++)
1187 Log("Verify Text: %s", textName[i]);
1188 code = verifyTextChain(ut, &db.h.textBlock[i]);
1189 if (code) return(code);
1195 * check the integrity of a text chain. Also checks the new chain.
1198 verifyTextChain(ut, tbPtr)
1199 struct ubik_trans *ut;
1200 struct textBlock *tbPtr;
1203 int blockIndex, entryIndex;
1207 afs_int32 code = 0, tcode;
1209 for (new=0; new<2; new++)
1212 blockAddr = ntohl(tbPtr->textAddr);
1214 for (blockAddr = (new ? ntohl(tbPtr->newTextAddr) : ntohl(tbPtr->textAddr));
1215 blockAddr; blockAddr = ntohl(block.h.next))
1217 tcode = ConvertDiskAddress(blockAddr, &blockIndex, &entryIndex);
1220 Log("verifyTextChain: Invalid %s text block addr 0x%x\n",
1221 (new?"new":""), blockAddr);
1222 Log(" Skipping remainder of text chain\n");
1223 if (BumpErrors()) ERROR(tcode);
1227 tcode = dbread(ut, blockAddr, &block, sizeof(block));
1228 if (tcode) ERROR(tcode);
1230 if ( blockMap[blockIndex]->entries[entryIndex] & MAP_TEXTBLOCK )
1232 Log("verifyTextChain: Text block (addr 0x%x) multiply chained\n", blockAddr);
1233 if (BumpErrors()) ERROR(DBBAD);
1235 blockMap[blockIndex]->entries[entryIndex] |= MAP_TEXTBLOCK;
1237 size += BLOCK_DATA_SIZE;
1240 if ( ntohl(new?tbPtr->newsize:tbPtr->size) > size )
1242 Log("verifyTextChain: Text block %s size %d > computed capacity %d\n",
1243 (new?"new":""), ntohl(new?tbPtr->newsize:tbPtr->size), size);
1244 if (BumpErrors()) ERROR(DBBAD);
1252 /* -----------------------------------------
1253 * verification driver routines
1254 * -----------------------------------------
1258 * Check the integrity of the database
1262 verifyDatabase(ut, recreateFile)
1263 struct ubik_trans *ut;
1264 FILE *recreateFile; /* not used */
1268 afs_int32 code = 0, tcode;
1270 extern int nBlocks; /* no. blocks in database */
1271 extern struct ubik_dbase *BU_dbase;
1273 /* clear verification statistics */
1275 bzero (&miscData, sizeof(miscData));
1278 miscData.maxErrors = 1000000;
1280 miscData.maxErrors = 50; /* Catch the first 50 errors */
1282 miscData.veryLongChain = 0;
1283 miscData.checkFragCount = 1; /* check frags */
1286 eof = ntohl(db.h.eofPtr);
1287 eof -= sizeof(db.h); /* subtract header */
1288 nBlocks = eof / BLOCKSIZE;
1290 Log("Verify of backup database started\n");
1291 Log("Database is %u. %d blocks of %d Bytes\n", eof, nBlocks, BLOCKSIZE);
1293 if ((eof < 0) || (nBlocks*BLOCKSIZE != eof))
1295 Log("Database eofPtr (%d) bad, blocksize %d\n", eof, BLOCKSIZE);
1299 /* set size of database */
1300 miscData.nBlocks = nBlocks;
1302 if (nBlocks == 0) ERROR(0); /* Nothing to check? */
1304 /* construct block map - first level is the array of pointers */
1305 bmsize = nBlocks*sizeof(struct blockMap *);
1306 blockMap = (struct blockMap **) malloc(bmsize);
1307 if (!blockMap) ERROR(BUDB_NOMEM);
1308 bzero(blockMap, bmsize);
1310 /* verify blocks and construct the block map */
1311 Log("Read header of every block\n");
1312 tcode = verifyBlocks(ut);
1313 if (tcode) ERROR(tcode);
1315 /* check the various hash tables */
1316 Log("Verify volume name hash table\n");
1317 tcode = verifyHashTable(ut, &db.volName, MAP_VOLHASH);
1318 if (tcode) ERROR(tcode);
1320 Log("Verify tape name hash table\n");
1321 tcode = verifyHashTable(ut, &db.tapeName, MAP_TAPEHASH);
1322 if (tcode) ERROR(tcode);
1324 Log("Verify dump name hash table\n");
1325 tcode = verifyHashTable(ut, &db.dumpName, MAP_DUMPHASH);
1326 if (tcode) ERROR(tcode);
1328 Log("Verify dump id hash table\n");
1329 tcode = verifyHashTable(ut, &db.dumpIden, MAP_IDHASH);
1330 if (tcode) ERROR(tcode);
1332 /* check the entry chains */
1333 Log("Verify all blocks and entries\n");
1334 tcode = verifyEntryChains(ut);
1335 if (tcode) ERROR(tcode);
1337 /* check text blocks - Log message in verifyText */
1338 tcode = verifyText(ut);
1339 if (tcode) ERROR(tcode);
1341 /* check free list */
1342 Log("Verify Free Lists\n");
1343 tcode = verifyFreeLists();
1344 if (tcode) ERROR(tcode);
1346 /* check entry map bit compatibility */
1348 Log("Verify Map bits\n");
1349 tcode = verifyMapBits();
1350 if (tcode) ERROR(tcode);
1353 /* free the block map */
1354 if ( blockMap != 0 )
1358 /* free all the individual maps */
1359 for ( i = 0; i < nBlocks; i++ )
1365 /* free the pointer array */
1371 Log("# 2K database blocks = %d\n", miscData.nBlocks);
1372 Log("# Dump entries found = %d. 3 dumps per block\n", miscData.nDump);
1373 Log(" max tapes on a dump = %d\n", miscData.maxTapesPerDump);
1374 Log(" max volumes on a dump = %d\n", miscData.maxVolsPerDump);
1375 Log(" max appends on a dump = %d\n", miscData.maxAppendsPerDump);
1376 Log(" # Blocks with space = %d\n", miscData.freeLength[4]);
1377 Log(" # of those fully free = %d\n", miscData.fullyFree[4]);
1378 Log("# Tape entries found = %d. 20 tapes per block\n", miscData.nTape);
1379 Log(" max volumes on a tape = %d\n", miscData.maxVolsPerTape);
1380 Log(" # Blocks with space = %d\n", miscData.freeLength[3]);
1381 Log(" # of those fully free = %d\n", miscData.fullyFree[3]);
1382 Log("# VolInfo entries found = %d. 20 volInfos per block\n", miscData.nVolInfo);
1383 Log(" # head of sameNameCh = %d\n", miscData.nVolName);
1384 Log(" max on a sameNameCh = %d\n", miscData.maxVolInfosPerName);
1385 Log(" max VolFrags on chain = %d\n", miscData.maxVolsPerVolInfo);
1386 Log(" # Blocks with space = %d\n", miscData.freeLength[2]);
1387 Log(" # of those fully free = %d\n", miscData.fullyFree[2]);
1388 Log("# VolFrag entries found = %d. 45 VolFrags per block\n", miscData.nVolFrag);
1389 Log(" # Blocks with space = %d\n", miscData.freeLength[1]);
1390 Log(" # of those fully free = %d\n", miscData.fullyFree[1]);
1391 Log("# free blocks = %d\n", miscData.freeLength[0]);
1394 Log("Verify of database completed. %d errors found\n", miscData.errors);
1396 if (miscData.errors && !code) code = DBBAD;
1401 /* -----------------------------
1402 * interface routines
1403 * -----------------------------
1407 * check the integrity of the database
1409 * status - integrity: 0, ok; n, not ok (error value)
1410 * orphans - no. of orphan blocks
1411 * host - address of host that did verification
1413 afs_int32 DbVerify();
1414 afs_int32 BUDB_DbVerify(call, status, orphans, host)
1415 struct rx_call *call;
1422 code = DbVerify(call, status, orphans, host);
1423 osi_auditU(call, BUDB_DBVfyEvent, code, AUD_END);
1427 afs_int32 DbVerify(call, status, orphans, host)
1428 struct rx_call *call;
1433 struct ubik_trans *ut = 0;
1434 afs_int32 code = 0, tcode;
1438 if ( callPermitted(call) == 0 )
1439 ERROR(BUDB_NOTPERMITTED);
1441 tcode = InitRPC (&ut, LOCKREAD, 1);
1442 if (tcode) ERROR(tcode);
1444 tcode = verifyDatabase(ut, 0); /* check the database */
1445 if (tcode) ERROR(tcode);
1449 if (code) ubik_AbortTrans(ut);
1450 else code = ubik_EndTrans(ut);
1456 gethostname(hostname, sizeof(hostname));
1457 th = gethostbyname(hostname);
1461 bcopy(th->h_addr, host, sizeof(afs_int32));
1462 *host = ntohl(*host);
1468 /* ----------------------
1470 * ----------------------
1474 * do a simple sanity check on the database header
1477 check_header(callerst)
1480 static int iteration_count = 0;
1483 eof = ntohl(db.h.eofPtr);
1484 if ( (eof == 0) || (eof < 0) )
1486 Log("Eof check failed, caller %s, eof 0x%x\n",
1490 eof -= sizeof(db.h);
1493 Log("Adjusted Eof check failed, caller %s, eof 0x%x\n",
1498 if ( iteration_count >= 10 )
1500 Log("Eof ptr is 0x%x\n", eof);
1501 iteration_count = 0;