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 #include <afsconfig.h>
11 #include <afs/param.h>
16 #include <WINNT/afsevent.h>
20 #include <afs/afsutil.h>
26 /* Read a VLDB file and verify it for correctness */
28 #define VL 0x001 /* good volume entry */
29 #define FR 0x002 /* free volume entry */
30 #define MH 0x004 /* multi-homed entry */
32 #define RWH 0x010 /* on rw hash chain */
33 #define ROH 0x020 /* on ro hash chain */
34 #define BKH 0x040 /* on bk hash chain */
35 #define NH 0x080 /* on name hash chain */
37 #define MHC 0x100 /* on multihomed chain */
38 #define FRC 0x200 /* on free chain */
40 #define REFRW 0x1000 /* linked from something (RW) */
41 #define REFRO 0x2000 /* linked from something (RO) */
42 #define REFBK 0x4000 /* linked from something (BK) */
43 #define REFN 0x8000 /* linked from something (name) */
45 #define MULTRW 0x10000 /* multiply-chained (RW) */
46 #define MULTRO 0x20000 /* multiply-chained (RO) */
47 #define MULTBK 0x40000 /* multiply-chained (BK) */
48 #define MULTN 0x80000 /* multiply-chained (name) */
50 #define MISRWH 0x100000 /* mischained (RW) */
51 #define MISROH 0x200000 /* mischained (RO) */
52 #define MISBKH 0x400000 /* mischained (BK) */
53 #define MISNH 0x800000 /* mischained (name) */
55 #define VLDB_CHECK_NO_VLDB_CHECK_ERROR 0
56 #define VLDB_CHECK_WARNING 1
57 #define VLDB_CHECK_ERROR 2
58 #define VLDB_CHECK_FATAL 4
59 #define vldbread(x,y,z) vldbio(x,y,z,0)
60 #define vldbwrite(x,y,z) vldbio(x,y,z,1)
62 #define ADDR(x) (x/sizeof(struct nvlentry))
65 int listentries, listservers, listheader, listuheader, verbose, quiet;
69 /* if quiet, don't send anything to stdout */
71 /* error level. 0 = no error, 1 = warning, 2 = error, 4 = fatal */
79 int serveraddrs[MAXSERVERID + 2];
81 /* Used to control what goes to stdout based on quiet flag */
83 quiet_println(const char *fmt,...) {
87 vfprintf(stdout, fmt, args);
92 /* Used to set the error level and ship messages to stderr */
94 log_error(int eval, const char *fmt, ...)
97 if (error_level < eval) error_level = eval ; /* bump up the severity */
99 vfprintf(stderr, fmt, args);
102 if (error_level == VLDB_CHECK_FATAL) exit(VLDB_CHECK_FATAL);
111 struct ubik_hdr uheader;
113 offset = lseek(fd, 0, 0);
115 log_error(VLDB_CHECK_FATAL,"error: lseek to 0 failed: %d %d\n", offset, errno);
116 return (VLDB_CHECK_FATAL);
119 /* now read the info */
120 r = read(fd, &uheader, sizeof(uheader));
121 if (r != sizeof(uheader)) {
122 log_error(VLDB_CHECK_FATAL,"error: read of %lu bytes failed: %d %d\n", sizeof(uheader), r,
124 return (VLDB_CHECK_FATAL);
127 uheader.magic = ntohl(uheader.magic);
128 uheader.size = ntohs(uheader.size);
129 uheader.version.epoch = ntohl(uheader.version.epoch);
130 uheader.version.counter = ntohl(uheader.version.counter);
133 quiet_println("Ubik Header\n");
134 quiet_println(" Magic = 0x%x\n", uheader.magic);
135 quiet_println(" Size = %u\n", uheader.size);
136 quiet_println(" Version.epoch = %u\n", uheader.version.epoch);
137 quiet_println(" Version.counter = %u\n", uheader.version.counter);
140 if (uheader.size != HDRSIZE)
141 log_error(VLDB_CHECK_WARNING,"VLDB_CHECK_WARNING: Ubik header size is %u (should be %u)\n", uheader.size,
143 if (uheader.magic != UBIK_MAGIC)
144 log_error(VLDB_CHECK_ERROR,"Ubik header magic is 0x%x (should be 0x%x)\n", uheader.magic,
151 vldbio(int position, void *buffer, int size, int rdwr)
155 /* seek to the correct spot. skip ubik stuff */
156 p = position + HDRSIZE;
157 offset = lseek(fd, p, 0);
159 log_error(VLDB_CHECK_FATAL,"error: lseek to %d failed: %d %d\n", p, offset, errno);
164 r = write(fd, buffer, size);
166 r = read(fd, buffer, size);
169 log_error(VLDB_CHECK_FATAL,"error: %s of %d bytes failed: %d %d\n", rdwr==1?"write":"read",
193 NameHash(char *volname)
199 for (vchar = volname + strlen(volname) - 1; vchar >= volname; vchar--)
200 hash = (hash * 63) + (*((unsigned char *)vchar) - 63);
201 return (hash % HASHSIZE);
205 IdHash(afs_uint32 volid)
207 return ((abs(volid)) % HASHSIZE);
210 #define LEGALCHARS ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
212 InvalidVolname(char *volname)
218 slen = strlen(volname);
219 if (slen >= VL_MAXNAMELEN)
221 return (slen != strspn(volname, map));
225 validVolumeAddr(afs_uint32 fileOffset)
227 if (ADDR(fileOffset) >= maxentries) {
228 /* Are we in range */
232 * We cannot test whether the offset is aligned
233 * since the vl entries are not in a regular array
239 readheader(struct vlheader *headerp)
243 vldbread(0, (char *)headerp, sizeof(*headerp));
245 headerp->vital_header.vldbversion =
246 ntohl(headerp->vital_header.vldbversion);
247 headerp->vital_header.headersize =
248 ntohl(headerp->vital_header.headersize);
249 headerp->vital_header.freePtr = ntohl(headerp->vital_header.freePtr);
250 headerp->vital_header.eofPtr = ntohl(headerp->vital_header.eofPtr);
251 headerp->vital_header.allocs = ntohl(headerp->vital_header.allocs);
252 headerp->vital_header.frees = ntohl(headerp->vital_header.frees);
253 headerp->vital_header.MaxVolumeId =
254 ntohl(headerp->vital_header.MaxVolumeId);
255 headerp->vital_header.totalEntries[0] =
256 ntohl(headerp->vital_header.totalEntries[0]);
257 for (i = 0; i < MAXTYPES; i++)
258 headerp->vital_header.totalEntries[i] =
259 ntohl(headerp->vital_header.totalEntries[1]);
261 headerp->SIT = ntohl(headerp->SIT);
262 for (i = 0; i < MAXSERVERID; i++)
263 headerp->IpMappedAddr[i] = ntohl(headerp->IpMappedAddr[i]);
264 for (i = 0; i < HASHSIZE; i++)
265 headerp->VolnameHash[i] = ntohl(headerp->VolnameHash[i]);
266 for (i = 0; i < MAXTYPES; i++)
267 for (j = 0; j < HASHSIZE; j++)
268 headerp->VolidHash[i][j] = ntohl(headerp->VolidHash[i][j]);
271 quiet_println("vldb header\n");
272 quiet_println(" vldbversion = %u\n",
273 headerp->vital_header.vldbversion);
274 quiet_println(" headersize = %u [actual=%lu]\n",
275 headerp->vital_header.headersize, sizeof(*headerp));
276 quiet_println(" freePtr = 0x%x\n", headerp->vital_header.freePtr);
277 quiet_println(" eofPtr = %u\n", headerp->vital_header.eofPtr);
278 quiet_println(" allocblock calls = %10u\n", headerp->vital_header.allocs);
279 quiet_println(" freeblock calls = %10u\n", headerp->vital_header.frees);
280 quiet_println(" MaxVolumeId = %u\n",
281 headerp->vital_header.MaxVolumeId);
282 quiet_println(" rw vol entries = %u\n",
283 headerp->vital_header.totalEntries[0]);
284 quiet_println(" ro vol entries = %u\n",
285 headerp->vital_header.totalEntries[1]);
286 quiet_println(" bk vol entries = %u\n",
287 headerp->vital_header.totalEntries[2]);
288 quiet_println(" multihome info = 0x%x (%u)\n", headerp->SIT,
290 quiet_println(" server ip addr table: size = %d entries\n",
292 quiet_println(" volume name hash table: size = %d buckets\n", HASHSIZE);
293 quiet_println(" volume id hash table: %d tables with %d buckets each\n",
297 /* Check the header size */
298 if (headerp->vital_header.headersize != sizeof(*headerp))
299 log_error(VLDB_CHECK_WARNING,"Header reports its size as %d (should be %lu)\n",
300 headerp->vital_header.headersize, sizeof(*headerp));
305 writeheader(struct vlheader *headerp)
309 headerp->vital_header.vldbversion =
310 htonl(headerp->vital_header.vldbversion);
311 headerp->vital_header.headersize =
312 htonl(headerp->vital_header.headersize);
313 headerp->vital_header.freePtr = htonl(headerp->vital_header.freePtr);
314 headerp->vital_header.eofPtr = htonl(headerp->vital_header.eofPtr);
315 headerp->vital_header.allocs = htonl(headerp->vital_header.allocs);
316 headerp->vital_header.frees = htonl(headerp->vital_header.frees);
317 headerp->vital_header.MaxVolumeId =
318 htonl(headerp->vital_header.MaxVolumeId);
319 headerp->vital_header.totalEntries[0] =
320 htonl(headerp->vital_header.totalEntries[0]);
321 for (i = 0; i < MAXTYPES; i++)
322 headerp->vital_header.totalEntries[i] =
323 htonl(headerp->vital_header.totalEntries[1]);
325 headerp->SIT = htonl(headerp->SIT);
326 for (i = 0; i < MAXSERVERID; i++)
327 headerp->IpMappedAddr[i] = htonl(headerp->IpMappedAddr[i]);
328 for (i = 0; i < HASHSIZE; i++)
329 headerp->VolnameHash[i] = htonl(headerp->VolnameHash[i]);
330 for (i = 0; i < MAXTYPES; i++)
331 for (j = 0; j < HASHSIZE; j++)
332 headerp->VolidHash[i][j] = htonl(headerp->VolidHash[i][j]);
334 vldbwrite(0, (char *)headerp, sizeof(*headerp));
338 readMH(afs_int32 addr, struct extentaddr *mhblockP)
341 struct extentaddr *e;
343 vldbread(addr, (char *)mhblockP, VL_ADDREXTBLK_SIZE);
345 mhblockP->ex_count = ntohl(mhblockP->ex_count);
346 mhblockP->ex_hdrflags = ntohl(mhblockP->ex_hdrflags);
347 for (i = 0; i < VL_MAX_ADDREXTBLKS; i++)
348 mhblockP->ex_contaddrs[i] = ntohl(mhblockP->ex_contaddrs[i]);
350 for (i = 1; i < VL_MHSRV_PERBLK; i++) {
353 /* won't convert hostuuid */
354 e->ex_uniquifier = ntohl(e->ex_uniquifier);
355 for (j = 0; j < VL_MAXIPADDRS_PERMH; j++)
356 e->ex_addrs[j] = ntohl(e->ex_addrs[j]);
362 readentry(afs_int32 addr, struct nvlentry *vlentryp, afs_int32 *type)
366 vldbread(addr, (char *)vlentryp, sizeof(*vlentryp));
368 for (i = 0; i < MAXTYPES; i++)
369 vlentryp->volumeId[i] = ntohl(vlentryp->volumeId[i]);
370 vlentryp->flags = ntohl(vlentryp->flags);
371 vlentryp->LockAfsId = ntohl(vlentryp->LockAfsId);
372 vlentryp->LockTimestamp = ntohl(vlentryp->LockTimestamp);
373 vlentryp->cloneId = ntohl(vlentryp->cloneId);
374 for (i = 0; i < MAXTYPES; i++)
375 vlentryp->nextIdHash[i] = ntohl(vlentryp->nextIdHash[i]);
376 vlentryp->nextNameHash = ntohl(vlentryp->nextNameHash);
377 for (i = 0; i < NMAXNSERVERS; i++) {
378 /* make sure not to ntohl these, as they're chars, not ints */
379 vlentryp->serverNumber[i] = vlentryp->serverNumber[i];
380 vlentryp->serverPartition[i] = vlentryp->serverPartition[i];
381 vlentryp->serverFlags[i] = vlentryp->serverFlags[i];
384 if (vlentryp->flags == VLCONTBLOCK) {
386 } else if (vlentryp->flags == VLFREE) {
393 quiet_println("address %u: ", addr);
394 if (vlentryp->flags == VLCONTBLOCK) {
395 quiet_println("mh extension block\n");
396 } else if (vlentryp->flags == VLFREE) {
397 quiet_println("free vlentry\n");
399 quiet_println("vlentry %s\n", vlentryp->name);
400 quiet_println(" rw id = %u ; ro id = %u ; bk id = %u\n",
401 vlentryp->volumeId[0], vlentryp->volumeId[1],
402 vlentryp->volumeId[2]);
403 quiet_println(" flags =");
404 if (vlentryp->flags & VLF_RWEXISTS)
405 quiet_println(" rw");
406 if (vlentryp->flags & VLF_ROEXISTS)
407 quiet_println(" ro");
408 if (vlentryp->flags & VLF_BACKEXISTS)
409 quiet_println(" bk");
410 if (vlentryp->flags & VLOP_MOVE)
411 quiet_println(" lock_move");
412 if (vlentryp->flags & VLOP_RELEASE)
413 quiet_println(" lock_release");
414 if (vlentryp->flags & VLOP_BACKUP)
415 quiet_println(" lock_backup");
416 if (vlentryp->flags & VLOP_DELETE)
417 quiet_println(" lock_delete");
418 if (vlentryp->flags & VLOP_DUMP)
419 quiet_println(" lock_dump");
421 /* all bits not covered by VLF_* and VLOP_* constants */
422 if (vlentryp->flags & 0xffff8e0f)
423 quiet_println(" errorflag(0x%x)", vlentryp->flags);
425 quiet_println(" LockAfsId = %d\n", vlentryp->LockAfsId);
426 quiet_println(" LockTimestamp = %d\n", vlentryp->LockTimestamp);
427 quiet_println(" cloneId = %u\n", vlentryp->cloneId);
429 (" next hash for rw = %u ; ro = %u ; bk = %u ; name = %u\n",
430 vlentryp->nextIdHash[0], vlentryp->nextIdHash[1],
431 vlentryp->nextIdHash[2], vlentryp->nextNameHash);
432 for (i = 0; i < NMAXNSERVERS; i++) {
433 if (vlentryp->serverNumber[i] != 255) {
434 quiet_println(" server %d ; partition %d ; flags =",
435 vlentryp->serverNumber[i],
436 vlentryp->serverPartition[i]);
437 if (vlentryp->serverFlags[i] & VLSF_RWVOL)
438 quiet_println(" rw");
439 if (vlentryp->serverFlags[i] & VLSF_ROVOL)
440 quiet_println(" ro");
441 if (vlentryp->serverFlags[i] & VLSF_BACKVOL)
442 quiet_println(" bk");
443 if (vlentryp->serverFlags[i] & VLSF_NEWREPSITE)
444 quiet_println(" newro");
454 writeentry(afs_int32 addr, struct nvlentry *vlentryp)
458 if (verbose) quiet_println("Writing back entry at addr %u\n", addr);
459 for (i = 0; i < MAXTYPES; i++)
460 vlentryp->volumeId[i] = htonl(vlentryp->volumeId[i]);
461 vlentryp->flags = htonl(vlentryp->flags);
462 vlentryp->LockAfsId = htonl(vlentryp->LockAfsId);
463 vlentryp->LockTimestamp = htonl(vlentryp->LockTimestamp);
464 vlentryp->cloneId = htonl(vlentryp->cloneId);
465 for (i = 0; i < MAXTYPES; i++)
466 vlentryp->nextIdHash[i] = htonl(vlentryp->nextIdHash[i]);
467 vlentryp->nextNameHash = htonl(vlentryp->nextNameHash);
468 for (i = 0; i < NMAXNSERVERS; i++) {
469 /* make sure not to htonl these, as they're chars, not ints */
470 vlentryp->serverNumber[i] = vlentryp->serverNumber[i] ;
471 vlentryp->serverPartition[i] = vlentryp->serverPartition[i] ;
472 vlentryp->serverFlags[i] = vlentryp->serverFlags[i] ;
474 vldbwrite(addr, (char *)vlentryp, sizeof(*vlentryp));
478 * Read each entry in the database:
479 * Record what type of entry it is and its address in the record array.
480 * Remember what the maximum volume id we found is and check against the header.
483 ReadAllEntries(struct vlheader *header)
485 afs_int32 type, rindex, i, j, e;
486 int freecount = 0, mhcount = 0, vlcount = 0;
487 int rwcount = 0, rocount = 0, bkcount = 0;
488 struct nvlentry vlentry;
490 afs_uint32 entrysize = 0;
491 afs_uint32 maxvolid = 0;
493 if (verbose) quiet_println("Read each entry in the database\n");
494 for (addr = header->vital_header.headersize;
495 addr < header->vital_header.eofPtr; addr += entrysize) {
497 /* Remember the highest volume id */
498 readentry(addr, &vlentry, &type);
500 if (!(vlentry.flags & VLF_RWEXISTS))
501 log_error(VLDB_CHECK_WARNING,"VLDB_CHECK_WARNING: VLDB entry '%s' has no RW volume\n",
504 for (i = 0; i < MAXTYPES; i++)
505 if (maxvolid < vlentry.volumeId[i])
506 maxvolid = vlentry.volumeId[i];
509 for (j = 0; j < NMAXNSERVERS; j++) {
510 if (vlentry.serverNumber[j] == 255)
512 if (vlentry.serverFlags[j] & (VLSF_ROVOL | VLSF_NEWREPSITE)) {
516 if (vlentry.serverFlags[j] & VLSF_RWVOL) {
518 if (vlentry.flags & VLF_BACKEXISTS)
522 if (!vlentry.serverFlags[j]) {
528 (VLDB_CHECK_ERROR,"VLDB entry '%s' contains an unknown RW/RO index serverFlag\n",
533 (" index %d : serverNumber %d : serverPartition %d : serverFlag %d\n",
534 j, vlentry.serverNumber[j], vlentry.serverPartition[j],
535 vlentry.serverFlags[j]);
539 rindex = addr / sizeof(vlentry);
540 if (record[rindex].type) {
541 log_error(VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: record holder %d already in use\n",
545 record[rindex].addr = addr;
546 record[rindex].type = type;
548 /* Determine entrysize and keep count */
550 entrysize = sizeof(vlentry);
552 } else if (type == FR) {
553 entrysize = sizeof(vlentry);
555 } else if (type == MH) {
556 entrysize = VL_ADDREXTBLK_SIZE;
559 log_error(VLDB_CHECK_ERROR, "Unknown entry at %u. Aborting\n", addr);
564 quiet_println("Found %d entries, %d free entries, %d multihomed blocks\n",
565 vlcount, freecount, mhcount);
566 quiet_println("Found %d RW volumes, %d BK volumes, %d RO volumes\n", rwcount,
570 /* Check the maxmimum volume id in the header */
571 if (maxvolid != header->vital_header.MaxVolumeId - 1)
573 ("Header's maximum volume id is %u and largest id found in VLDB is %u\n",
574 header->vital_header.MaxVolumeId, maxvolid);
578 * Follow each Name hash bucket marking it as read in the record array.
579 * Record we found it in the name hash within the record array.
580 * Check that the name is hashed correctly.
583 FollowNameHash(struct vlheader *header)
585 int count = 0, longest = 0, shortest = -1, chainlength;
586 struct nvlentry vlentry;
588 afs_int32 i, type, rindex;
590 /* Now follow the Name Hash Table */
591 if (verbose) quiet_println("Check Volume Name Hash\n");
592 for (i = 0; i < HASHSIZE; i++) {
595 if (!validVolumeAddr(header->VolnameHash[i])) {
596 log_error(VLDB_CHECK_ERROR,"Name Hash %d: Bad entry %u is out of range\n",
597 i, header->VolnameHash[i]);
601 for (addr = header->VolnameHash[i]; addr; addr = vlentry.nextNameHash) {
602 readentry(addr, &vlentry, &type);
604 log_error(VLDB_CHECK_ERROR,"Name Hash %d: Bad entry at %u: Not a valid vlentry\n",
612 * we know that the address is valid because we
613 * checked it either above or below
615 if (record[rindex].addr != addr && record[rindex].addr) {
617 (VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: addresses %ld and %u use same record slot %d\n",
618 record[rindex].addr, addr, rindex);
620 if (record[rindex].type & NH) {
622 (VLDB_CHECK_ERROR,"Name Hash %d: Bad entry '%s': Already in the name hash\n",
624 record[rindex].type |= MULTN;
628 if (!validVolumeAddr(vlentry.nextNameHash)) {
629 log_error(VLDB_CHECK_ERROR,"Name Hash forward link of '%s' is out of range\n",
631 record[rindex].type |= MULTN;
635 record[rindex].type |= NH;
636 record[rindex].type |= REFN;
641 /* Hash the name and check if in correct hash table */
642 if (NameHash(vlentry.name) != i) {
644 (VLDB_CHECK_ERROR,"Name Hash %d: Bad entry '%s': Incorrect name hash chain (should be in %d)\n",
645 i, vlentry.name, NameHash(vlentry.name));
646 record[rindex].type |= MULTN;
649 if (chainlength > longest)
650 longest = chainlength;
651 if ((shortest == -1) || (chainlength < shortest))
652 shortest = chainlength;
656 ("%d entries in name hash, longest is %d, shortest is %d, average length is %f\n",
657 count, longest, shortest, ((float)count / (float)HASHSIZE));
663 * Follow the ID hash chains for the RW, RO, and BK hash tables.
664 * Record we found it in the id hash within the record array.
665 * Check that the ID is hashed correctly.
668 FollowIdHash(struct vlheader *header)
670 int count = 0, longest = 0, shortest = -1, chainlength;
671 struct nvlentry vlentry;
673 afs_int32 i, j, hash, type, rindex, ref, badref, badhash;
675 /* Now follow the RW, RO, and BK Hash Tables */
676 if (verbose) quiet_println("Check RW, RO, and BK id Hashes\n");
677 for (i = 0; i < MAXTYPES; i++) {
678 hash = ((i == 0) ? RWH : ((i == 1) ? ROH : BKH));
679 ref = ((i == 0) ? REFRW : ((i == 1) ? REFRO : REFBK));
680 badref = ((i == 0) ? MULTRW : ((i == 1) ? MULTRO : MULTBK));
681 badhash = ((i == 0) ? MULTRW : ((i == 1) ? MULTRO : MULTBK));
685 for (j = 0; j < HASHSIZE; j++) {
687 if (!validVolumeAddr(header->VolidHash[i][j])) {
688 log_error(VLDB_CHECK_ERROR,"%s Hash %d: Bad entry %u is out of range\n",
689 vtype(i), j, header->VolidHash[i][j]);
693 for (addr = header->VolidHash[i][j]; addr;
694 addr = vlentry.nextIdHash[i]) {
695 readentry(addr, &vlentry, &type);
698 (VLDB_CHECK_ERROR,"%s Id Hash %d: Bad entry at %u: Not a valid vlentry\n",
704 if (record[rindex].addr != addr && record[rindex].addr) {
706 (VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: addresses %ld and %u use same record slot %d\n",
707 record[rindex].addr, addr, rindex);
709 if (record[rindex].type & hash) {
711 (VLDB_CHECK_ERROR,"%s Id Hash %d: Bad entry '%s': Already in the hash table\n",
712 vtype(i), j, vlentry.name);
713 record[rindex].type |= badref;
717 if (!validVolumeAddr(vlentry.nextIdHash[i])) {
718 log_error(VLDB_CHECK_ERROR,"%s Id Hash forward link of '%s' is out of range\n",
719 vtype(i), vlentry.name);
720 record[rindex].type |= badref;
724 record[rindex].type |= hash;
725 record[rindex].type |= ref;
730 /* Hash the id and check if in correct hash table */
731 if (IdHash(vlentry.volumeId[i]) != j) {
733 (VLDB_CHECK_ERROR,"%s Id Hash %d: Bad entry '%s': Incorrect Id hash chain (should be in %d)\n",
734 vtype(i), j, vlentry.name,
735 IdHash(vlentry.volumeId[i]));
736 record[rindex].type |= badhash;
740 if (chainlength > longest)
741 longest = chainlength;
742 if ((shortest == -1) || (chainlength < shortest))
743 shortest = chainlength;
747 ("%d entries in %s hash, longest is %d, shortest is %d, average length is %f\n",
748 count, vtype(i), longest, shortest,((float)count / (float)HASHSIZE));
755 * Follow the free chain.
756 * Record we found it in the free chain within the record array.
759 FollowFreeChain(struct vlheader *header)
762 struct nvlentry vlentry;
764 afs_int32 type, rindex;
766 /* Now follow the Free Chain */
767 if (verbose) quiet_println("Check Volume Free Chain\n");
768 for (addr = header->vital_header.freePtr; addr;
769 addr = vlentry.nextIdHash[0]) {
770 readentry(addr, &vlentry, &type);
773 (VLDB_CHECK_ERROR,"Free Chain %d: Bad entry at %u: Not a valid free vlentry (0x%x)\n",
778 rindex = addr / sizeof(vlentry);
779 if (record[rindex].addr != addr && record[rindex].addr) {
781 (VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: addresses %u and %ld use same record slot %d\n",
782 record[rindex].addr, addr, rindex);
784 if (record[rindex].type & FRC) {
785 log_error(VLDB_CHECK_ERROR,"Free Chain: Bad entry at %u: Already in the free chain\n",
789 record[rindex].type |= FRC;
794 quiet_println("%d entries on free chain\n", count);
799 * Read each multihomed block and mark it as found in the record.
800 * Read each entry in each multihomed block and mark the serveraddrs
801 * array with the number of ip addresses found for this entry.
803 * Then read the IpMappedAddr array in the header.
804 * Verify that multihomed entries base and index are valid and points to
805 * a good multhomed entry.
806 * Mark the serveraddrs array with 1 ip address for regular entries.
808 * By the end, the severaddrs array will have a 0 if the entry has no
809 * IP addresses in it or the count of the number of IP addresses.
811 * The code does not verify if there are duplicate IP addresses in the
812 * list. The vlserver does this when a fileserver registeres itself.
815 CheckIpAddrs(struct vlheader *header)
818 afs_int32 i, j, m, rindex;
819 afs_int32 mhentries, regentries;
820 afs_uint32 caddrs[VL_MAX_ADDREXTBLKS];
821 char mhblock[VL_ADDREXTBLK_SIZE];
822 struct extentaddr *MHblock = (struct extentaddr *)mhblock;
823 struct extentaddr *e;
824 int ipindex, ipaddrs;
827 memset(&nulluuid, 0, sizeof(nulluuid));
830 quiet_println("Check Multihomed blocks\n");
833 /* Read the first MH block and from it, gather the
834 * addresses of all the mh blocks.
836 readMH(header->SIT, MHblock);
837 if (MHblock->ex_hdrflags != VLCONTBLOCK) {
839 (VLDB_CHECK_ERROR,"Multihomed Block 0: Bad entry at %u: Not a valid multihomed block\n",
843 for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
844 caddrs[i] = MHblock->ex_contaddrs[i];
847 if (header->SIT != caddrs[0]) {
849 (VLDB_CHECK_ERROR,"MH block does not point to self %u in header, %u in block\n",
850 header->SIT, caddrs[0]);
853 /* Now read each MH block and record it in the record array */
854 for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
858 readMH(caddrs[i], MHblock);
859 if (MHblock->ex_hdrflags != VLCONTBLOCK) {
861 (VLDB_CHECK_ERROR,"Multihomed Block 0: Bad entry at %u: Not a valid multihomed block\n",
865 rindex = caddrs[i] / sizeof(vlentry);
866 if (record[rindex].addr != caddrs[i] && record[rindex].addr) {
868 (VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: addresses %u and %u use same record slot %d\n",
869 record[rindex].addr, caddrs[i], rindex);
871 if (record[rindex].type & FRC) {
873 (VLDB_CHECK_ERROR,"MH Blocks Chain %d: Bad entry at %ld: Already a MH block\n",
874 i, record[rindex].addr);
877 record[rindex].type |= MHC;
881 /* Read each entry in a multihomed block.
882 * Find the pointer to the entry in the IpMappedAddr array and
883 * verify that the entry is good (has IP addresses in it).
886 for (j = 1; j < VL_MHSRV_PERBLK; j++) {
887 e = (struct extentaddr *)&(MHblock[j]);
889 /* Search the IpMappedAddr array for the reference to this entry */
890 for (ipindex = 0; ipindex < MAXSERVERID; ipindex++) {
891 if (((header->IpMappedAddr[ipindex] & 0xff000000) ==
895 IpMappedAddr[ipindex] & 0x00ff0000) >> 16) == i)
896 && ((header->IpMappedAddr[ipindex] & 0x0000ffff) ==
901 if (ipindex >= MAXSERVERID)
904 serveraddrs[ipindex] = -1;
906 if (memcmp(&e->ex_hostuuid, &nulluuid, sizeof(afsUUID)) == 0) {
909 (VLDB_CHECK_ERROR,"Server Addrs index %d references null MH block %d, index %d\n",
911 serveraddrs[ipindex] = 0; /* avoids printing 2nd error below */
916 /* Step through each ip address and count the good addresses */
918 for (m = 0; m < VL_MAXIPADDRS_PERMH; m++) {
923 /* If we found any good ip addresses, mark it in the serveraddrs record */
928 (VLDB_CHECK_ERROR,"MH block %d, index %d: Not referenced by server addrs\n",
931 serveraddrs[ipindex] = ipaddrs; /* It is good */
935 if (listservers && ipaddrs) {
936 quiet_println("MH block %d, index %d:", i, j);
937 for (m = 0; m < VL_MAXIPADDRS_PERMH; m++) {
940 quiet_println(" %d.%d.%d.%d",
941 (e->ex_addrs[m] & 0xff000000) >> 24,
942 (e->ex_addrs[m] & 0x00ff0000) >> 16,
943 (e->ex_addrs[m] & 0x0000ff00) >> 8,
944 (e->ex_addrs[m] & 0x000000ff));
950 * if (mhentries != MHblock->ex_count) {
951 * quiet_println("MH blocks says it has %d entries (found %d)\n",
952 * MHblock->ex_count, mhentries);
958 quiet_println("%d multihomed blocks\n", mhblocks);
960 /* Check the server addresses */
962 quiet_println("Check server addresses\n");
963 mhentries = regentries = 0;
964 for (i = 0; i <= MAXSERVERID; i++) {
965 if (header->IpMappedAddr[i]) {
966 if ((header->IpMappedAddr[i] & 0xff000000) == 0xff000000) {
968 if (((header->IpMappedAddr[i] & 0x00ff0000) >> 16) >
971 (VLDB_CHECK_ERROR,"IP Addr for entry %d: Multihome block is bad (%d)\n",
972 i, ((header->IpMappedAddr[i] & 0x00ff0000) >> 16));
973 if (((header->IpMappedAddr[i] & 0x0000ffff) > VL_MHSRV_PERBLK)
974 || ((header->IpMappedAddr[i] & 0x0000ffff) < 1))
976 (VLDB_CHECK_ERROR,"IP Addr for entry %d: Multihome index is bad (%d)\n",
977 i, (header->IpMappedAddr[i] & 0x0000ffff));
978 if (serveraddrs[i] == -1) {
980 (VLDB_CHECK_WARNING,"warning: IP Addr for entry %d: Multihome entry has no ip addresses\n",
985 quiet_println(" Server ip addr %d = MH block %d, index %d\n",
986 i, (header->IpMappedAddr[i] & 0x00ff0000) >> 16,
987 (header->IpMappedAddr[i] & 0x0000ffff));
991 serveraddrs[i] = 1; /* It is good */
993 quiet_println(" Server ip addr %d = %d.%d.%d.%d\n", i,
994 (header->IpMappedAddr[i] & 0xff000000) >> 24,
995 (header->IpMappedAddr[i] & 0x00ff0000) >> 16,
996 (header->IpMappedAddr[i] & 0x0000ff00) >> 8,
997 (header->IpMappedAddr[i] & 0x000000ff));
1003 quiet_println("%d simple entries, %d multihomed entries, Total = %d\n",
1004 regentries, mhentries, mhentries + regentries);
1010 nameForAddr(afs_uint32 addr, int hashtype, afs_uint32 *hash, char *buffer)
1013 * We need to simplify the reporting, while retaining
1014 * legible messages. This is a helper function. The return address
1015 * is either a fixed char or the provided buffer - so don't use the
1016 * name after the valid lifetime of the buffer.
1019 struct nvlentry entry;
1021 /* Distinguished, invalid, hash */
1024 } else if (!validVolumeAddr(addr)) {
1025 /* Different, invalid, hash */
1029 readentry(addr, &entry, &type);
1034 if (hashtype >= MAXTYPES) {
1035 *hash = NameHash(entry.name);
1037 *hash = IdHash(entry.volumeId[hashtype]);
1039 sprintf(buffer, "for '%s'", entry.name);
1044 reportHashChanges(struct vlheader *header, afs_uint32 oldnamehash[HASHSIZE], afs_uint32 oldidhash[MAXTYPES][HASHSIZE])
1047 afs_uint32 oldhash, newhash;
1048 char oldNameBuffer[10 + VL_MAXNAMELEN];
1049 char newNameBuffer[10 + VL_MAXNAMELEN];
1050 char *oldname, *newname;
1052 * report hash changes
1055 for (i = 0; i < HASHSIZE; i++) {
1056 if (oldnamehash[i] != header->VolnameHash[i]) {
1058 oldname = nameForAddr(oldnamehash[i], MAXTYPES, &oldhash, oldNameBuffer);
1059 newname = nameForAddr(header->VolnameHash[i], MAXTYPES, &newhash, newNameBuffer);
1060 if (verbose || (oldhash != newhash)) {
1061 quiet_println("FIX: Name hash header at %d was %s, is now %s\n", i, oldname, newname);
1064 for (j = 0; j < MAXTYPES; j++) {
1065 if (oldidhash[j][i] != header->VolidHash[j][i]) {
1067 oldname = nameForAddr(oldidhash[j][i], j, &oldhash, oldNameBuffer);
1068 newname = nameForAddr(header->VolidHash[j][i], j, &newhash, newNameBuffer);
1069 if (verbose || (oldhash != newhash)) {
1070 quiet_println("FIX: %s hash header at %d was %s, is now %s\n", vtype(j), i, oldname, newname);
1078 WorkerBee(struct cmd_syndesc *as, void *arock)
1082 struct vlheader header;
1083 struct nvlentry vlentry, vlentry2;
1085 afs_uint32 oldnamehash[HASHSIZE];
1086 afs_uint32 oldidhash[MAXTYPES][HASHSIZE];
1088 error_level = 0; /* start clean with no error status */
1089 dbfile = as->parms[0].items->data; /* -database */
1090 listuheader = (as->parms[1].items ? 1 : 0); /* -uheader */
1091 listheader = (as->parms[2].items ? 1 : 0); /* -vheader */
1092 listservers = (as->parms[3].items ? 1 : 0); /* -servers */
1093 listentries = (as->parms[4].items ? 1 : 0); /* -entries */
1094 verbose = (as->parms[5].items ? 1 : 0); /* -verbose */
1095 quiet = (as->parms[6].items ? 1 : 0); /* -quiet */
1096 fix = (as->parms[7].items ? 1 : 0); /* -fix */
1099 if (quiet && (verbose || listuheader || listheader ||listservers \
1101 log_error(VLDB_CHECK_FATAL," -quiet cannot be used other display flags\n");
1102 return VLDB_CHECK_FATAL;
1106 /* open the vldb database file */
1107 fd = open(dbfile, (fix > 0)?O_RDWR:O_RDONLY, 0);
1109 log_error(VLDB_CHECK_FATAL,"can't open file '%s'. error = %d\n", dbfile, errno);
1113 /* read the ubik header and the vldb database header */
1115 readheader(&header);
1116 if (header.vital_header.vldbversion < 3) {
1117 log_error(VLDB_CHECK_FATAL,"does not support vldb with version less than 3\n");
1118 return VLDB_CHECK_FATAL;
1121 maxentries = (header.vital_header.eofPtr / sizeof(vlentry)) + 1;
1122 record = calloc(maxentries, sizeof(struct er));
1123 memset(serveraddrs, 0, sizeof(serveraddrs));
1125 /* Will fill in the record array of entries it found */
1126 ReadAllEntries(&header);
1127 listentries = 0; /* Listed all the entries */
1129 /* Check the multihomed blocks for valid entries as well as
1130 * the IpMappedAddrs array in the header for valid entries.
1132 CheckIpAddrs(&header);
1134 /* Follow the hash tables */
1135 FollowNameHash(&header);
1136 FollowIdHash(&header);
1138 /* Follow the chain of free entries */
1139 FollowFreeChain(&header);
1141 /* Now check the record we have been keeping for inconsistencies
1142 * For valid vlentries, also check that the server we point to is
1143 * valid (the serveraddrs array).
1146 quiet_println("Verify each volume entry\n");
1147 for (i = 0; i < maxentries; i++) {
1152 if (record[i].type == 0)
1155 /* If a vlentry, verify that its name is valid, its name and ids are
1156 * on the hash chains, and its server numbers are good.
1158 if (record[i].type & VL) {
1160 int foundbroken = 0;
1163 readentry(record[i].addr, &vlentry, &type);
1165 if (InvalidVolname(vlentry.name))
1166 log_error(VLDB_CHECK_ERROR,"Volume '%s' at addr %ld has an invalid name\n",
1167 vlentry.name, record[i].addr);
1169 if (!(record[i].type & NH)) {
1170 hash = NameHash(vlentry.name);
1176 if (vlentry.volumeId[0] && !(record[i].type & RWH)) {
1177 hash = IdHash(vlentry.volumeId[0]);
1179 sprintf(volidbuf, "id %u ", vlentry.volumeId[0]);
1183 if (vlentry.volumeId[1] && !(record[i].type & ROH)) {
1184 hash = IdHash(vlentry.volumeId[1]);
1186 sprintf(volidbuf, "id %u ", vlentry.volumeId[1]);
1190 if (vlentry.volumeId[2] && !(record[i].type & BKH)) {
1191 hash = IdHash(vlentry.volumeId[2]);
1193 sprintf(volidbuf, "id %u ", vlentry.volumeId[2]);
1197 if (!validVolumeAddr(vlentry.nextNameHash) ||
1198 record[ADDR(vlentry.nextNameHash)].type & MULTN) {
1199 hash = NameHash(vlentry.name);
1202 if (validVolumeAddr(vlentry.nextNameHash)) {
1203 readentry(vlentry.nextNameHash, &vlentry2, &type);
1204 nexthash = NameHash(vlentry2.name);
1206 nexthash = 0xFFFFFFFF;
1208 if (hash != nexthash)
1212 if (!validVolumeAddr(vlentry.nextIdHash[0]) ||
1213 record[ADDR(vlentry.nextIdHash[0])].type & MULTRW) {
1214 hash = IdHash(vlentry.volumeId[0]);
1216 sprintf(volidbuf, "id %u ", vlentry.volumeId[0]);
1217 if (validVolumeAddr(vlentry.nextIdHash[0])) {
1218 readentry(vlentry.nextIdHash[0], &vlentry2, &type);
1219 nexthash = IdHash(vlentry2.volumeId[0]);
1221 nexthash = 0xFFFFFFFF;
1223 if (hash != nexthash)
1227 if (!validVolumeAddr(vlentry.nextIdHash[1]) ||
1228 record[ADDR(vlentry.nextIdHash[1])].type & MULTRO) {
1229 hash = IdHash(vlentry.volumeId[1]);
1231 sprintf(volidbuf, "id %u ", vlentry.volumeId[1]);
1232 if (validVolumeAddr(vlentry.nextIdHash[1])) {
1233 readentry(vlentry.nextIdHash[1], &vlentry2, &type);
1234 nexthash = IdHash(vlentry2.volumeId[1]);
1236 nexthash = 0xFFFFFFFF;
1238 if (hash != nexthash)
1242 if (!validVolumeAddr(vlentry.nextIdHash[2]) ||
1243 record[ADDR(vlentry.nextIdHash[2])].type & MULTBK) {
1244 hash = IdHash(vlentry.volumeId[2]);
1246 sprintf(volidbuf, "id %u ", vlentry.volumeId[2]);
1247 if (validVolumeAddr(vlentry.nextIdHash[2])) {
1248 readentry(vlentry.nextIdHash[2], &vlentry2, &type);
1249 nexthash = IdHash(vlentry2.volumeId[2]);
1251 nexthash = 0xFFFFFFFF;
1253 if (hash != nexthash)
1258 log_error(VLDB_CHECK_ERROR, "%d: Volume '%s' %s forward link in %s hash chain is broken (hash %d != %d)\n", i,
1259 vlentry.name, volidbuf, which, hash, nexthash);
1260 } else if (foundbad) {
1261 log_error(VLDB_CHECK_ERROR, "%d: Volume '%s' %snot found in %s hash %d\n", i,
1262 vlentry.name, volidbuf, which, hash);
1265 for (j = 0; j < NMAXNSERVERS; j++) {
1266 if ((vlentry.serverNumber[j] != 255)
1267 && (serveraddrs[vlentry.serverNumber[j]] == 0)) {
1269 (VLDB_CHECK_ERROR,"Volume '%s', index %d points to empty server entry %d\n",
1270 vlentry.name, j, vlentry.serverNumber[j]);
1274 if (record[i].type & 0xffff0f00)
1276 (VLDB_CHECK_ERROR,"Volume '%s' id %u also found on other chains (0x%x)\n",
1277 vlentry.name, vlentry.volumeId[0], record[i].type);
1280 } else if (record[i].type & FR) {
1281 if (!(record[i].type & FRC))
1282 log_error(VLDB_CHECK_ERROR,"Free vlentry at %ld not on free chain\n",
1285 if (record[i].type & 0xfffffdf0)
1287 (VLDB_CHECK_ERROR,"Free vlentry at %ld also found on other chains (0x%x)\n",
1288 record[i].addr, record[i].type);
1290 /* A multihomed entry */
1291 } else if (record[i].type & MH) {
1292 if (!(record[i].type & MHC))
1293 log_error(VLDB_CHECK_ERROR,"Multihomed block at %ld is orphaned\n",
1296 if (record[i].type & 0xfffffef0)
1298 (VLDB_CHECK_ERROR,"Multihomed block at %ld also found on other chains (0x%x)\n",
1299 record[i].addr, record[i].type);
1302 log_error(VLDB_CHECK_ERROR,"Unknown entry type at %u (0x%x)\n", record[i].addr,
1309 * If we are fixing we will rebuild all the hash lists from the ground up
1311 memcpy(oldnamehash, header.VolnameHash, sizeof(oldnamehash));
1312 memset(header.VolnameHash, 0, sizeof(header.VolnameHash));
1314 memcpy(oldidhash, header.VolidHash, sizeof(oldidhash));
1315 memset(header.VolidHash, 0, sizeof(header.VolidHash));
1316 quiet_println("Rebuilding %u entries\n", maxentries);
1318 quiet_println("Scanning %u entries for possible repairs\n", maxentries);
1320 for (i = 0; i < maxentries; i++) {
1322 if (record[i].type & VL) {
1323 readentry(record[i].addr, &vlentry, &type);
1324 if (!(record[i].type & REFN)) {
1325 log_error(VLDB_CHECK_ERROR,"%d: Record %ld (type 0x%x) not in a name chain\n", i,
1326 record[i].addr, record[i].type);
1328 if (vlentry.volumeId[0] && !(record[i].type & REFRW)) {
1329 log_error(VLDB_CHECK_ERROR,"%d: Record %ld (type 0x%x) not in a RW chain\n", i,
1330 record[i].addr, record[i].type);
1332 if (vlentry.volumeId[1] && !(record[i].type & REFRO)) {
1333 log_error(VLDB_CHECK_ERROR,"%d: Record %ld (type 0x%x) not in a RO chain\n", i,
1334 record[i].addr, record[i].type);
1336 if (vlentry.volumeId[2] && !(record[i].type & REFBK)) {
1337 log_error(VLDB_CHECK_ERROR,"%d: Record %ld (type 0x%x) not in a BK chain\n", i,
1338 record[i].addr, record[i].type);
1341 afs_uint32 oldhash, newhash;
1342 char oldNameBuffer[10 + VL_MAXNAMELEN];
1343 char newNameBuffer[10 + VL_MAXNAMELEN];
1344 char *oldname, *newname;
1347 * Put the current hash table contexts into our 'next'
1348 * and our address into the hash table.
1350 hash = NameHash(vlentry.name);
1352 if (vlentry.nextNameHash != header.VolnameHash[hash]) {
1353 oldname = nameForAddr(vlentry.nextNameHash, MAXTYPES, &oldhash, oldNameBuffer);
1354 newname = nameForAddr(header.VolnameHash[hash], MAXTYPES, &newhash, newNameBuffer);
1355 if (verbose || ((oldhash != newhash) &&
1356 (0 != vlentry.nextNameHash) &&
1357 (0 != header.VolnameHash[hash]))) {
1359 * That is, only report if we are verbose
1360 * or the hash is changing (and one side wasn't NULL
1362 quiet_println("FIX: Name hash link for '%s' was %s, is now %s\n",
1363 vlentry.name, oldname, newname);
1367 vlentry.nextNameHash = header.VolnameHash[hash];
1368 header.VolnameHash[hash] = record[i].addr;
1370 for (j = 0; j < MAXTYPES; j++) {
1372 if (0 == vlentry.volumeId[j]) {
1374 * No volume of that type. Continue
1378 hash = IdHash(vlentry.volumeId[j]);
1380 if (vlentry.nextIdHash[j] != header.VolidHash[j][hash]) {
1381 oldname = nameForAddr(vlentry.nextIdHash[j], j, &oldhash, oldNameBuffer);
1382 newname = nameForAddr(header.VolidHash[j][hash], j, &newhash, newNameBuffer);
1383 if (verbose || ((oldhash != newhash) &&
1384 (0 != vlentry.nextIdHash[j]) &&
1385 (0 != header.VolidHash[j][hash]))) {
1386 quiet_println("FIX: %s hash link for '%s' was %s, is now %s\n",
1387 vtype(j), vlentry.name, oldname, newname);
1391 vlentry.nextIdHash[j] = header.VolidHash[j][hash];
1392 header.VolidHash[j][hash] = record[i].addr;
1394 writeentry(record[i].addr, &vlentry);
1399 reportHashChanges(&header, oldnamehash, oldidhash);
1400 writeheader(&header);
1409 main(int argc, char **argv)
1411 struct cmd_syndesc *ts;
1415 ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "vldb check");
1416 cmd_AddParm(ts, "-database", CMD_SINGLE, CMD_REQUIRED, "vldb_file");
1417 cmd_AddParm(ts, "-uheader", CMD_FLAG, CMD_OPTIONAL,
1418 "Display UBIK header");
1419 cmd_AddParm(ts, "-vheader", CMD_FLAG, CMD_OPTIONAL,
1420 "Display VLDB header");
1421 cmd_AddParm(ts, "-servers", CMD_FLAG, CMD_OPTIONAL,
1422 "Display server list");
1423 cmd_AddParm(ts, "-entries", CMD_FLAG, CMD_OPTIONAL, "Display entries");
1424 cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");
1425 cmd_AddParm(ts, "-quiet", CMD_FLAG, CMD_OPTIONAL, "quiet");
1426 cmd_AddParm(ts, "-fix", CMD_FLAG, CMD_OPTIONAL, "attempt to patch the database (potentially dangerous)");
1428 return cmd_Dispatch(argc, argv);