joe-beuhler-patches-20031122
[openafs.git] / src / vlserver / vldb_check.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /* Read a VLDB file and verify it for correctness */
11
12 #define VL  0x001               /* good volume entry */
13 #define FR  0x002               /* free volume entry */
14 #define MH  0x004               /* multi-homed entry */
15
16 #define RWH 0x010               /* on rw hash chain */
17 #define ROH 0x020               /* on ro hash chain */
18 #define BKH 0x040               /* on bk hash chain */
19 #define NH  0x080               /* on name hash chain */
20
21 #define MHC 0x100               /* on multihomed chain */
22 #define FRC 0x200               /* on free chain */
23
24 #include <afsconfig.h>
25 #include <afs/param.h>
26
27 RCSID
28     ("$Header$");
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #ifdef AFS_NT40_ENV
35 #include <winsock2.h>
36 #include <WINNT/afsevent.h>
37 #include <io.h>
38 #else
39 #include <sys/socket.h>
40 #include <netdb.h>
41 #include <netinet/in.h>
42 #endif
43
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #else
47 #ifdef HAVE_STRINGS_H
48 #include <strings.h>
49 #endif
50 #endif
51
52 #include "vlserver.h"
53 #include "vldbint.h"
54 #include <ubik.h>
55 #include <afs/afsutil.h>
56 #include <afs/cmd.h>
57
58 int fd;
59 int listentries, listservers, listheader, listuheader, verbose;
60
61 struct er {
62     long addr;
63     int type;
64 } *record;
65 int serveraddrs[MAXSERVERID + 2];
66
67
68 #define HDRSIZE 64
69 int
70 readUbikHeader()
71 {
72     int offset, r;
73     struct ubik_hdr uheader;
74
75     offset = lseek(fd, 0, 0);
76     if (offset != 0) {
77         printf("error: lseek to 0 failed: %d %d\n", offset, errno);
78         return (-1);
79     }
80
81     /* now read the info */
82     r = read(fd, &uheader, sizeof(uheader));
83     if (r != sizeof(uheader)) {
84         printf("error: read of %d bytes failed: %d %d\n", sizeof(uheader), r,
85                errno);
86         return (-1);
87     }
88
89     uheader.magic = ntohl(uheader.magic);
90     uheader.size = ntohl(uheader.size);
91     uheader.version.epoch = ntohl(uheader.version.epoch);
92     uheader.version.counter = ntohl(uheader.version.counter);
93
94     if (listuheader) {
95         printf("Ubik Header\n");
96         printf("   Magic           = 0x%x\n", uheader.magic);
97         printf("   Size            = %u\n", uheader.size);
98         printf("   Version.epoch   = %u\n", uheader.version.epoch);
99         printf("   Version.counter = %u\n", uheader.version.counter);
100     }
101
102     if (uheader.size != HDRSIZE)
103         printf("Ubik header size is %u (should be %u)\n", uheader.size,
104                HDRSIZE);
105     if (uheader.magic != UBIK_MAGIC)
106         printf("Ubik header magic is 0x%x (should be 0x%x)\n", uheader.magic,
107                UBIK_MAGIC);
108
109     return (0);
110 }
111
112 int
113 vldbread(position, buffer, size)
114      int position;
115      char *buffer;
116      int size;
117 {
118     int offset, r, p;
119
120     /* seek to the correct spot. skip ubik stuff */
121     p = position + HDRSIZE;
122     offset = lseek(fd, p, 0);
123     if (offset != p) {
124         printf("error: lseek to %d failed: %d %d\n", p, offset, errno);
125         return (-1);
126     }
127
128     /* now read the info */
129     r = read(fd, buffer, size);
130     if (r != size) {
131         printf("error: read of %d bytes failed: %d %d\n", size, r, errno);
132         return (-1);
133     }
134     return (0);
135 }
136
137 char *
138 vtype(type)
139      int type;
140 {
141     static char Type[3];
142
143     if (type == 0)
144         strcpy(Type, "rw");
145     else if (type == 1)
146         strcpy(Type, "ro");
147     else if (type == 2)
148         strcpy(Type, "bk");
149     else
150         strcpy(Type, "??");
151     return (Type);
152 }
153
154 afs_int32
155 NameHash(volname)
156      char *volname;
157 {
158     unsigned int hash;
159     char *vchar;
160
161     hash = 0;
162     for (vchar = volname + strlen(volname) - 1; vchar >= volname; vchar--)
163         hash = (hash * 63) + (*((unsigned char *)vchar) - 63);
164     return (hash % HASHSIZE);
165 }
166
167 afs_int32
168 IdHash(volid)
169      afs_int32 volid;
170 {
171     return ((abs(volid)) % HASHSIZE);
172 }
173
174 #define LEGALCHARS ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
175 int
176 InvalidVolname(volname)
177      char *volname;
178 {
179     char *map;
180     size_t slen;
181
182     map = LEGALCHARS;
183     slen = strlen(volname);
184     if (slen >= VL_MAXNAMELEN)
185         return 1;
186     return (slen != strspn(volname, map));
187 }
188
189 readheader(headerp)
190      struct vlheader *headerp;
191 {
192     int i, j;
193
194     vldbread(0, headerp, sizeof(*headerp));
195
196     headerp->vital_header.vldbversion =
197         ntohl(headerp->vital_header.vldbversion);
198     headerp->vital_header.headersize =
199         ntohl(headerp->vital_header.headersize);
200     headerp->vital_header.freePtr = ntohl(headerp->vital_header.freePtr);
201     headerp->vital_header.eofPtr = ntohl(headerp->vital_header.eofPtr);
202     headerp->vital_header.allocs = ntohl(headerp->vital_header.allocs);
203     headerp->vital_header.frees = ntohl(headerp->vital_header.frees);
204     headerp->vital_header.MaxVolumeId =
205         ntohl(headerp->vital_header.MaxVolumeId);
206     headerp->vital_header.totalEntries[0] =
207         ntohl(headerp->vital_header.totalEntries[0]);
208     for (i = 0; i < MAXTYPES; i++)
209         headerp->vital_header.totalEntries[i] =
210             ntohl(headerp->vital_header.totalEntries[1]);
211
212     headerp->SIT = ntohl(headerp->SIT);
213     for (i = 0; i < MAXSERVERID; i++)
214         headerp->IpMappedAddr[i] = ntohl(headerp->IpMappedAddr[i]);
215     for (i = 0; i < HASHSIZE; i++)
216         headerp->VolnameHash[i] = ntohl(headerp->VolnameHash[i]);
217     for (i = 0; i < MAXTYPES; i++)
218         for (j = 0; j < HASHSIZE; j++)
219             headerp->VolidHash[i][j] = ntohl(headerp->VolidHash[i][j]);
220
221     if (listheader) {
222         printf("vldb header\n");
223         printf("   vldbversion      = %u\n",
224                headerp->vital_header.vldbversion);
225         printf("   headersize       = %u [actual=%u]\n",
226                headerp->vital_header.headersize, sizeof(*headerp));
227         printf("   freePtr          = 0x%x\n", headerp->vital_header.freePtr);
228         printf("   eofPtr           = %u\n", headerp->vital_header.eofPtr);
229         printf("   allocblock calls = %10u\n", headerp->vital_header.allocs);
230         printf("   freeblock  calls = %10u\n", headerp->vital_header.frees);
231         printf("   MaxVolumeId      = %u\n",
232                headerp->vital_header.MaxVolumeId);
233         printf("   rw vol entries   = %u\n",
234                headerp->vital_header.totalEntries[0]);
235         printf("   ro vol entries   = %u\n",
236                headerp->vital_header.totalEntries[1]);
237         printf("   bk vol entries   = %u\n",
238                headerp->vital_header.totalEntries[2]);
239         printf("   multihome info   = 0x%x (%u)\n", headerp->SIT,
240                headerp->SIT);
241         printf("   server ip addr   table: size = %d entries\n",
242                MAXSERVERID + 1);
243         printf("   volume name hash table: size = %d buckets\n", HASHSIZE);
244         printf("   volume id   hash table: %d tables with %d buckets each\n",
245                MAXTYPES, HASHSIZE);
246     }
247
248     /* Check the header size */
249     if (headerp->vital_header.headersize != sizeof(*headerp))
250         printf("Header reports its size as %d (should be %d)\n",
251                headerp->vital_header.headersize, sizeof(*headerp));
252 }
253
254 readMH(addr, mhblockP)
255      afs_int32 addr;
256      struct extentaddr *mhblockP;
257 {
258     int i, j;
259     struct extentaddr *e;
260
261     vldbread(addr, mhblockP, VL_ADDREXTBLK_SIZE);
262
263     mhblockP->ex_count = ntohl(mhblockP->ex_count);
264     mhblockP->ex_flags = ntohl(mhblockP->ex_flags);
265     for (i = 0; i < VL_MAX_ADDREXTBLKS; i++)
266         mhblockP->ex_contaddrs[i] = ntohl(mhblockP->ex_contaddrs[i]);
267
268     for (i = 1; i < VL_MHSRV_PERBLK; i++) {
269         e = &(mhblockP[i]);
270
271         /* won't convert hostuuid */
272         e->ex_uniquifier = ntohl(e->ex_uniquifier);
273         for (j = 0; j < VL_MAXIPADDRS_PERMH; j++)
274             e->ex_addrs[j] = ntohl(e->ex_addrs[j]);
275     }
276 }
277
278 readentry(addr, vlentryp, type)
279      afs_int32 addr;
280      struct nvlentry *vlentryp;
281      afs_int32 *type;
282 {
283     int i;
284
285     vldbread(addr, vlentryp, sizeof(*vlentryp));
286
287     for (i = 0; i < MAXTYPES; i++)
288         vlentryp->volumeId[i] = ntohl(vlentryp->volumeId[i]);
289     vlentryp->flags = ntohl(vlentryp->flags);
290     vlentryp->LockAfsId = ntohl(vlentryp->LockAfsId);
291     vlentryp->LockTimestamp = ntohl(vlentryp->LockTimestamp);
292     vlentryp->cloneId = ntohl(vlentryp->cloneId);
293     for (i = 0; i < MAXTYPES; i++)
294         vlentryp->nextIdHash[i] = ntohl(vlentryp->nextIdHash[i]);
295     vlentryp->nextNameHash = ntohl(vlentryp->nextNameHash);
296     for (i = 0; i < NMAXNSERVERS; i++) {
297         vlentryp->serverNumber[i] = ntohl(vlentryp->serverNumber[i]);
298         vlentryp->serverPartition[i] = ntohl(vlentryp->serverPartition[i]);
299         vlentryp->serverFlags[i] = ntohl(vlentryp->serverFlags[i]);
300     }
301
302     if (vlentryp->flags == VLCONTBLOCK) {
303         *type = MH;
304     } else if (vlentryp->flags == VLFREE) {
305         *type = FR;
306     } else {
307         *type = VL;
308     }
309
310     if (listentries) {
311         printf("address %u: ", addr);
312         if (vlentryp->flags == VLCONTBLOCK) {
313             printf("mh extension block\n");
314         } else if (vlentryp->flags == VLFREE) {
315             printf("free vlentry\n");
316         } else {
317             printf("vlentry %s\n", vlentryp->name);
318             printf("   rw id = %u ; ro id = %u ; bk id = %u\n",
319                    vlentryp->volumeId[0], vlentryp->volumeId[1],
320                    vlentryp->volumeId[2]);
321             printf("   flags         =");
322             if (vlentryp->flags & VLF_RWEXISTS)
323                 printf(" rw");
324             if (vlentryp->flags & VLF_ROEXISTS)
325                 printf(" ro");
326             if (vlentryp->flags & VLF_BACKEXISTS)
327                 printf(" bk");
328             if (vlentryp->flags & 0xffff8fff)
329                 printf(" errorflag(0x%x)", vlentryp->flags);
330             printf("\n");
331             printf("   LockAfsId     = %d\n", vlentryp->LockAfsId);
332             printf("   LockTimestamp = %d\n", vlentryp->LockTimestamp);
333             printf("   cloneId       = %u\n", vlentryp->cloneId);
334             printf
335                 ("   next hash for rw = %u ; ro = %u ; bk = %u ; name = %u\n",
336                  vlentryp->nextIdHash[0], vlentryp->nextIdHash[1],
337                  vlentryp->nextIdHash[2], vlentryp->nextNameHash);
338             for (i = 0; i < NMAXNSERVERS; i++) {
339                 if (vlentryp->serverNumber[i] != 255) {
340                     printf("   server %d ; partition %d ; flags =",
341                            vlentryp->serverNumber[i],
342                            vlentryp->serverPartition[i]);
343                     if (vlentryp->serverFlags[i] & VLSF_RWVOL)
344                         printf(" rw");
345                     if (vlentryp->serverFlags[i] & VLSF_ROVOL)
346                         printf(" ro");
347                     if (vlentryp->serverFlags[i] & VLSF_BACKVOL)
348                         printf(" bk");
349                     if (vlentryp->serverFlags[i] & VLSF_NEWREPSITE)
350                         printf(" newro");
351                     printf("\n");
352                 }
353             }
354         }
355     }
356 }
357
358 void
359 readSIT(base, addr)
360      int base;
361      int addr;
362 {
363     int i, j, a;
364     char sitbuf[VL_ADDREXTBLK_SIZE];
365     struct extentaddr *extent;
366
367     if (!addr)
368         return;
369     vldbread(addr, sitbuf, VL_ADDREXTBLK_SIZE);
370     extent = (struct extentaddr *)sitbuf;
371
372     printf("multihome info block: base %d\n", base);
373     if (base == 0) {
374         printf("   count = %u\n", ntohl(extent->ex_count));
375         printf("   flags = %u\n", ntohl(extent->ex_flags));
376         for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
377             printf("   contaddrs[%d] = %u\n", i,
378                    ntohl(extent->ex_contaddrs[i]));
379         }
380     }
381     for (i = 1; i < VL_MHSRV_PERBLK; i++) {
382         /* should we skip this entry */
383         for (j = 0; j < VL_MAX_ADDREXTBLKS; j++) {
384             if (extent[i].ex_addrs[j])
385                 break;
386         }
387         if (j >= VL_MAX_ADDREXTBLKS)
388             continue;
389
390         printf("   base %d index %d:\n", base, i);
391
392         printf("       afsuuid    = (%x %x %x /%d/%d/ /%x/%x/%x/%x/%x/%x/)\n",
393                ntohl(extent[i].ex_hostuuid.time_low),
394                ntohl(extent[i].ex_hostuuid.time_mid),
395                ntohl(extent[i].ex_hostuuid.time_hi_and_version),
396                ntohl(extent[i].ex_hostuuid.clock_seq_hi_and_reserved),
397                ntohl(extent[i].ex_hostuuid.clock_seq_low),
398                ntohl(extent[i].ex_hostuuid.node[0]),
399                ntohl(extent[i].ex_hostuuid.node[1]),
400                ntohl(extent[i].ex_hostuuid.node[2]),
401                ntohl(extent[i].ex_hostuuid.node[3]),
402                ntohl(extent[i].ex_hostuuid.node[4]),
403                ntohl(extent[i].ex_hostuuid.node[5]));
404         printf("       uniquifier = %u\n", ntohl(extent[i].ex_uniquifier));
405         for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
406             a = ntohl(extent[i].ex_addrs[j]);
407             if (a) {
408                 printf("       %d.%d.%d.%d\n", (a >> 24) & 0xff,
409                        (a >> 16) & 0xff, (a >> 8) & 0xff, (a) & 0xff);
410             }
411         }
412     }
413 }
414
415 /*
416  * Read each entry in the database:
417  * Record what type of entry it is and its address in the record array.
418  * Remember what the maximum volume id we found is and check against the header.
419  */
420 void
421 ReadAllEntries(header)
422      struct vlheader *header;
423 {
424     afs_int32 type, rindex, i, j, e;
425     int freecount = 0, mhcount = 0, vlcount = 0;
426     int rwcount = 0, rocount = 0, bkcount = 0;
427     struct nvlentry vlentry;
428     afs_uint32 addr, entrysize, maxvolid = 0;
429
430     if (verbose)
431         printf("Read each entry in the database\n");
432     for (addr = header->vital_header.headersize;
433          addr < header->vital_header.eofPtr; addr += entrysize) {
434
435         /* Remember the highest volume id */
436         readentry(addr, &vlentry, &type);
437         if (type == VL) {
438             if (!(vlentry.flags & VLF_RWEXISTS))
439                 printf("WARNING: VLDB entry '%s' has no RW volume\n",
440                        vlentry.name);
441
442             for (i = 0; i < MAXTYPES; i++)
443                 if (maxvolid < vlentry.volumeId[i])
444                     maxvolid = vlentry.volumeId[i];
445
446             e = 1;
447             for (j = 0; j < NMAXNSERVERS; j++) {
448                 if (vlentry.serverNumber[j] == 255)
449                     continue;
450                 if (vlentry.serverFlags[j] & (VLSF_ROVOL | VLSF_NEWREPSITE)) {
451                     rocount++;
452                     continue;
453                 }
454                 if (vlentry.serverFlags[j] & VLSF_RWVOL) {
455                     rwcount++;
456                     if (vlentry.flags & VLF_BACKEXISTS)
457                         bkcount++;
458                     continue;
459                 }
460                 if (e) {
461                     printf
462                         ("VLDB entry '%s' contains an unknown RW/RO index serverFlag\n",
463                          vlentry.name);
464                     e = 0;
465                 }
466                 printf
467                     ("   index %d : serverNumber %d : serverPartition %d : serverFlag %d\n",
468                      j, vlentry.serverNumber[j], vlentry.serverPartition[j],
469                      vlentry.serverFlags[j]);
470             }
471         }
472
473         rindex = addr / sizeof(vlentry);
474         if (record[rindex].type) {
475             printf("INTERNAL ERROR: record holder %d already in use\n",
476                    rindex);
477             return;
478         }
479         record[rindex].addr = addr;
480         record[rindex].type = type;
481
482         /* Determine entrysize and keep count */
483         if (type == VL) {
484             entrysize = sizeof(vlentry);
485             vlcount++;
486         } else if (type == FR) {
487             entrysize = sizeof(vlentry);
488             freecount++;
489         } else if (type == MH) {
490             entrysize = VL_ADDREXTBLK_SIZE;
491             mhcount++;
492         } else {
493             printf("Unknown entry at %u\n", addr);
494         }
495     }
496     if (verbose) {
497         printf("Found %d entries, %d free entries, %d multihomed blocks\n",
498                vlcount, freecount, mhcount);
499         printf("Found %d RW volumes, %d BK volumes, %d RO volumes\n", rwcount,
500                bkcount, rocount);
501     }
502
503     /* Check the maxmimum volume id in the header */
504     if (maxvolid != header->vital_header.MaxVolumeId - 1)
505         printf
506             ("Header's maximum volume id is %u and largest id found in VLDB is %u\n",
507              header->vital_header.MaxVolumeId, maxvolid);
508 }
509
510
511 /*
512  * Follow each Name hash bucket marking it as read in the record array.
513  * Record we found it in the name hash within the record array.
514  * Check that the name is hashed correctly.
515  */
516 FollowNameHash(header)
517      struct vlheader *header;
518 {
519     int count = 0, longest = 0, shortest = -1, chainlength;
520     struct nvlentry vlentry;
521     afs_uint32 addr;
522     afs_int32 i, type, rindex;
523
524     /* Now follow the Name Hash Table */
525     if (verbose)
526         printf("Check Volume Name Hash\n");
527     for (i = 0; i < HASHSIZE; i++) {
528         chainlength = 0;
529         for (addr = header->VolnameHash[i]; addr; addr = vlentry.nextNameHash) {
530             readentry(addr, &vlentry, &type);
531             if (type != VL) {
532                 printf("Name Hash %d: Bad entry at %u: Not a valid vlentry\n",
533                        i, addr);
534                 continue;
535             }
536
537             rindex = addr / sizeof(vlentry);
538
539             if (record[rindex].addr != addr && record[rindex].addr) {
540                 printf
541                     ("INTERNAL ERROR: addresses %u and %u use same record slot %d\n",
542                      record[rindex].addr, addr, rindex);
543             }
544             if (record[rindex].type & NH) {
545                 printf
546                     ("Name Hash %d: Bad entry '%s': Already in the name hash\n",
547                      i, vlentry.name);
548                 break;
549             }
550             record[rindex].type |= NH;
551
552             chainlength++;
553             count++;
554
555             /* Hash the name and check if in correct hash table */
556             if (NameHash(vlentry.name) != i) {
557                 printf
558                     ("Name Hash %d: Bad entry '%s': Incorrect name hash chain (should be in %d)\n",
559                      i, vlentry.name, NameHash(vlentry.name));
560             }
561         }
562         if (chainlength > longest)
563             longest = chainlength;
564         if ((shortest == -1) || (chainlength < shortest))
565             shortest = chainlength;
566     }
567     if (verbose) {
568         printf
569             ("%d entries in name hash, longest is %d, shortest is %d, average length is %f\n",
570              count, longest, shortest, ((float)count / (float)HASHSIZE));
571     }
572 }
573
574 /*
575  * Follow the ID hash chains for the RW, RO, and BK hash tables.
576  * Record we found it in the id hash within the record array.
577  * Check that the ID is hashed correctly.
578  */
579 FollowIdHash(header)
580      struct vlheader *header;
581 {
582     int count = 0, longest = 0, shortest = -1, chainlength;
583     struct nvlentry vlentry;
584     afs_uint32 addr;
585     afs_int32 i, j, hash, type, rindex;
586
587     /* Now follow the RW, RO, and BK Hash Tables */
588     if (verbose)
589         printf("Check RW, RO, and BK id Hashes\n");
590     for (i = 0; i < MAXTYPES; i++) {
591         hash = ((i == 0) ? RWH : ((i == 1) ? ROH : BKH));
592         count = longest = 0;
593         shortest = -1;
594
595         for (j = 0; j < HASHSIZE; j++) {
596             chainlength = 0;
597             for (addr = header->VolidHash[i][j]; addr;
598                  addr = vlentry.nextIdHash[i]) {
599                 readentry(addr, &vlentry, &type);
600                 if (type != VL) {
601                     printf
602                         ("%s Id Hash %d: Bad entry at %u: Not a valid vlentry\n",
603                          vtype(i), j, addr);
604                     continue;
605                 }
606
607                 rindex = addr / sizeof(vlentry);
608                 if (record[rindex].addr != addr && record[rindex].addr) {
609                     printf
610                         ("INTERNAL ERROR: addresses %u and %u use same record slot %d\n",
611                          record[rindex].addr, addr, rindex);
612                 }
613                 if (record[rindex].type & hash) {
614                     printf
615                         ("%s Id Hash %d: Bad entry '%s': Already in the the hash table\n",
616                          vtype(i), j, vlentry.name);
617                     break;
618                 }
619                 record[rindex].type |= hash;
620
621                 chainlength++;
622                 count++;
623
624                 /* Hash the id and check if in correct hash table */
625                 if (IdHash(vlentry.volumeId[i]) != j) {
626                     printf
627                         ("%s Id Hash %d: Bad entry '%s': Incorrect Id hash chain (should be in %d)\n",
628                          vtype(i), j, vlentry.name,
629                          IdHash(vlentry.volumeId[i]));
630                 }
631             }
632
633             if (chainlength > longest)
634                 longest = chainlength;
635             if ((shortest == -1) || (chainlength < shortest))
636                 shortest = chainlength;
637         }
638         if (verbose) {
639             printf
640                 ("%d entries in %s hash, longest is %d, shortest is %d, average length is %f\n",
641                  count, vtype(i), longest, shortest,
642                  ((float)count / (float)HASHSIZE));
643         }
644     }
645 }
646
647 /*
648  * Follow the free chain.
649  * Record we found it in the free chain within the record array.
650  */
651 FollowFreeChain(header)
652      struct vlheader *header;
653 {
654     afs_int32 count = 0;
655     struct nvlentry vlentry;
656     afs_uint32 addr;
657     afs_int32 type, rindex;
658
659     /* Now follow the Free Chain */
660     if (verbose)
661         printf("Check Volume Free Chain\n");
662     for (addr = header->vital_header.freePtr; addr;
663          addr = vlentry.nextIdHash[0]) {
664         readentry(addr, &vlentry, &type);
665         if (type != FR) {
666             printf
667                 ("Free Chain %d: Bad entry at %u: Not a valid free vlentry (0x%x)\n",
668                  count, addr, type);
669             continue;
670         }
671
672         rindex = addr / sizeof(vlentry);
673         if (record[rindex].addr != addr && record[rindex].addr) {
674             printf
675                 ("INTERNAL ERROR: addresses %u and %u use same record slot %d\n",
676                  record[rindex].addr, addr, rindex);
677         }
678         if (record[rindex].type & FRC) {
679             printf("Free Chain: Bad entry at %u: Already in the free chain\n",
680                    addr);
681             break;
682         }
683         record[rindex].type |= FRC;
684
685         count++;
686     }
687     if (verbose)
688         printf("%d entries on free chain\n", count);
689 }
690
691 /*
692  * Read each multihomed block and mark it as found in the record.
693  * Read each entry in each multihomed block and mark the serveraddrs
694  * array with the number of ip addresses found for this entry.
695  * 
696  * Then read the IpMappedAddr array in the header.
697  * Verify that multihomed entries base and index are valid and points to
698  * a good multhomed entry.
699  * Mark the serveraddrs array with 1 ip address for regular entries.
700  * 
701  * By the end, the severaddrs array will have a 0 if the entry has no 
702  * IP addresses in it or the count of the number of IP addresses.
703  *
704  * The code does not verify if there are duplicate IP addresses in the 
705  * list. The vlserver does this when a fileserver registeres itself.
706  */
707 CheckIpAddrs(header)
708      struct vlheader *header;
709 {
710     int mhblocks = 0;
711     afs_int32 i, j, m, rindex;
712     afs_int32 mhentries, regentries;
713     afs_int32 caddrs[VL_MAX_ADDREXTBLKS];
714     char mhblock[VL_ADDREXTBLK_SIZE];
715     struct extentaddr *MHblock = (struct extentaddr *)mhblock;
716     struct extentaddr *e;
717     int ipindex, ipaddrs;
718     afsUUID nulluuid;
719
720     memset(&nulluuid, 0, sizeof(nulluuid));
721
722     if (verbose)
723         printf("Check Multihomed blocks\n");
724
725     if (header->SIT) {
726         /* Read the first MH block and from it, gather the 
727          * addresses of all the mh blocks.
728          */
729         readMH(header->SIT, MHblock);
730         if (MHblock->ex_flags != VLCONTBLOCK) {
731             printf
732                 ("Multihomed Block 0: Bad entry at %u: Not a valid multihomed block\n",
733                  header->SIT);
734         }
735
736         for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
737             caddrs[i] = MHblock->ex_contaddrs[i];
738         }
739
740         if (header->SIT != caddrs[0]) {
741             printf
742                 ("MH block does not point to self %u in header, %u in block\n",
743                  header->SIT, caddrs[0]);
744         }
745
746         /* Now read each MH block and record it in the record array */
747         for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
748             if (!caddrs[i])
749                 continue;
750
751             readMH(caddrs[i], MHblock);
752             if (MHblock->ex_flags != VLCONTBLOCK) {
753                 printf
754                     ("Multihomed Block 0: Bad entry at %u: Not a valid multihomed block\n",
755                      header->SIT);
756             }
757
758             rindex = caddrs[i] / sizeof(vlentry);
759             if (record[rindex].addr != caddrs[i] && record[rindex].addr) {
760                 printf
761                     ("INTERNAL ERROR: addresses %u and %u use same record slot %d\n",
762                      record[rindex].addr, caddrs[i], rindex);
763             }
764             if (record[rindex].type & FRC) {
765                 printf
766                     ("MH Blocks Chain %d: Bad entry at %u: Already a MH block\n",
767                      i, record[rindex].addr);
768                 break;
769             }
770             record[rindex].type |= MHC;
771
772             mhblocks++;
773
774             /* Read each entry in a multihomed block. 
775              * Find the pointer to the entry in the IpMappedAddr array and
776              * verify that the entry is good (has IP addresses in it).
777              */
778             mhentries = 0;
779             for (j = 1; j < VL_MHSRV_PERBLK; j++) {
780                 e = (struct extentaddr *)&(MHblock[j]);
781
782                 /* Search the IpMappedAddr array for the reference to this entry */
783                 for (ipindex = 0; ipindex < MAXSERVERID; ipindex++) {
784                     if (((header->IpMappedAddr[ipindex] & 0xff000000) ==
785                          0xff000000)
786                         &&
787                         (((header->
788                            IpMappedAddr[ipindex] & 0x00ff0000) >> 16) == i)
789                         && ((header->IpMappedAddr[ipindex] & 0x0000ffff) ==
790                             j)) {
791                         break;
792                     }
793                 }
794                 if (ipindex >= MAXSERVERID)
795                     ipindex = -1;
796                 else
797                     serveraddrs[ipindex] = -1;
798
799                 if (memcmp(&e->ex_hostuuid, &nulluuid, sizeof(afsUUID)) == 0) {
800                     if (ipindex != -1) {
801                         printf
802                             ("Server Addrs index %d references null MH block %d, index %d\n",
803                              ipindex, i, j);
804                         serveraddrs[ipindex] = 0;       /* avoids printing 2nd error below */
805                     }
806                     continue;
807                 }
808
809                 /* Step through each ip address and count the good addresses */
810                 ipaddrs = 0;
811                 for (m = 0; m < VL_MAXIPADDRS_PERMH; m++) {
812                     if (e->ex_addrs[m])
813                         ipaddrs++;
814                 }
815
816                 /* If we found any good ip addresses, mark it in the serveraddrs record */
817                 if (ipaddrs) {
818                     mhentries++;
819                     if (ipindex == -1) {
820                         printf
821                             ("MH block %d, index %d: Not referenced by server addrs\n",
822                              i, j);
823                     } else {
824                         serveraddrs[ipindex] = ipaddrs; /* It is good */
825                     }
826                 }
827
828                 if (listservers && ipaddrs) {
829                     printf("MH block %d, index %d:", i, j);
830                     for (m = 0; m < VL_MAXIPADDRS_PERMH; m++) {
831                         if (!e->ex_addrs[m])
832                             continue;
833                         printf(" %d.%d.%d.%d",
834                                (e->ex_addrs[m] & 0xff000000) >> 24,
835                                (e->ex_addrs[m] & 0x00ff0000) >> 16,
836                                (e->ex_addrs[m] & 0x0000ff00) >> 8,
837                                (e->ex_addrs[m] & 0x000000ff));
838                     }
839                     printf("\n");
840                 }
841             }
842 /*
843  *      if (mhentries != MHblock->ex_count) {
844  *         printf("MH blocks says it has %d entries (found %d)\n",
845  *                MHblock->ex_count, mhentries);
846  *      }
847  */
848         }
849     }
850     if (verbose)
851         printf("%d multihomed blocks\n", mhblocks);
852
853     /* Check the server addresses */
854     if (verbose)
855         printf("Check server addresses\n");
856     mhentries = regentries = 0;
857     for (i = 0; i <= MAXSERVERID; i++) {
858         if (header->IpMappedAddr[i]) {
859             if ((header->IpMappedAddr[i] & 0xff000000) == 0xff000000) {
860                 mhentries++;
861                 if (((header->IpMappedAddr[i] & 0x00ff0000) >> 16) >
862                     VL_MAX_ADDREXTBLKS)
863                     printf
864                         ("IP Addr for entry %d: Multihome block is bad (%d)\n",
865                          i, ((header->IpMappedAddr[i] & 0x00ff0000) >> 16));
866                 if (((header->IpMappedAddr[i] & 0x0000ffff) > VL_MHSRV_PERBLK)
867                     || ((header->IpMappedAddr[i] & 0x0000ffff) < 1))
868                     printf
869                         ("IP Addr for entry %d: Multihome index is bad (%d)\n",
870                          i, (header->IpMappedAddr[i] & 0x0000ffff));
871                 if (serveraddrs[i] == -1) {
872                     printf
873                         ("warning: IP Addr for entry %d: Multihome entry has no ip addresses\n",
874                          i);
875                     serveraddrs[i] = 0;
876                 }
877                 if (listservers) {
878                     printf("   Server ip addr %d = MH block %d, index %d\n",
879                            i, (header->IpMappedAddr[i] & 0x00ff0000) >> 16,
880                            (header->IpMappedAddr[i] & 0x0000ffff));
881                 }
882             } else {
883                 regentries++;
884                 serveraddrs[i] = 1;     /* It is good */
885                 if (listservers) {
886                     printf("   Server ip addr %d = %d.%d.%d.%d\n", i,
887                            (header->IpMappedAddr[i] & 0xff000000) >> 24,
888                            (header->IpMappedAddr[i] & 0x00ff0000) >> 16,
889                            (header->IpMappedAddr[i] & 0x0000ff00) >> 8,
890                            (header->IpMappedAddr[i] & 0x000000ff));
891                 }
892             }
893         }
894     }
895     if (verbose) {
896         printf("%d simple entries, %d multihomed entries, Total = %d\n",
897                regentries, mhentries, mhentries + regentries);
898     }
899
900 }
901
902 int
903 WorkerBee(as, arock)
904      struct cmd_syndesc *as;
905      char *arock;
906 {
907     char *dbfile;
908     afs_int32 maxentries, type;
909     struct vlheader header;
910     struct nvlentry vlentry;
911     int i, j, help = 0;
912
913     dbfile = as->parms[0].items->data;  /* -database */
914     listuheader = (as->parms[1].items ? 1 : 0); /* -uheader  */
915     listheader = (as->parms[2].items ? 1 : 0);  /* -vheader  */
916     listservers = (as->parms[3].items ? 1 : 0); /* -servers  */
917     listentries = (as->parms[4].items ? 1 : 0); /* -entries  */
918     verbose = (as->parms[5].items ? 1 : 0);     /* -verbose  */
919
920     /* open the vldb database file */
921     fd = open(dbfile, O_RDONLY, 0);
922     if (fd < 0) {
923         printf("can't open file '%s'. error = %d\n", dbfile, errno);
924         return 0;
925     }
926
927     /* read the ubik header and the vldb database header */
928     readUbikHeader();
929     readheader(&header);
930     if (header.vital_header.vldbversion < 3) {
931         printf("does not support vldb with version less than 3\n");
932         return 0;
933     }
934
935     maxentries = (header.vital_header.eofPtr / sizeof(vlentry)) + 1;
936     record = (struct er *)malloc(maxentries * sizeof(struct er));
937     memset((char *)record, 0, (maxentries * sizeof(struct er)));
938     memset((char *)serveraddrs, 0, sizeof(serveraddrs));
939
940     /* Will fill in the record array of entries it found */
941     ReadAllEntries(&header);
942     listentries = 0;            /* Listed all the entries */
943
944     /* Check the multihomed blocks for valid entries as well as
945      * the IpMappedAddrs array in the header for valid entries.
946      */
947     CheckIpAddrs(&header);
948
949     /* Follow the hash tables */
950     FollowNameHash(&header);
951     FollowIdHash(&header);
952
953     /* Follow the chain of free entries */
954     FollowFreeChain(&header);
955
956     /* Now check the record we have been keeping for inconsistancies
957      * For valid vlentries, also check that the server we point to is 
958      * valid (the serveraddrs array).
959      */
960     if (verbose)
961         printf("Verify each volume entry\n");
962     for (i = 0; i < maxentries; i++) {
963         if (record[i].type == 0)
964             continue;
965
966         /* If a vlentry, verify that its name is valid, its name and ids are
967          * on the hash chains, and its server numbers are good.
968          */
969         if (record[i].type & VL) {
970             readentry(record[i].addr, &vlentry, &type);
971
972             if (InvalidVolname(vlentry.name))
973                 printf("Volume '%s' at addr has an invalid name\n",
974                        vlentry.name, record[i].addr);
975
976             if (!(record[i].type & NH))
977                 printf("Volume '%s' not found in name hash\n", vlentry.name);
978
979             if (vlentry.volumeId[0] && !(record[i].type & RWH))
980                 printf("Volume '%s' id %u not found in RW hash chain\n",
981                        vlentry.name, vlentry.volumeId[0]);
982
983             if (vlentry.volumeId[1] && !(record[i].type & ROH))
984                 printf("Volume '%s' id %u not found in RO hash chain\n",
985                        vlentry.name, vlentry.volumeId[1]);
986
987             if (vlentry.volumeId[2] && !(record[i].type & BKH))
988                 printf("Volume '%s' id %u not found in BK hash chain\n",
989                        vlentry.name, vlentry.volumeId[2]);
990
991             for (j = 0; j < NMAXNSERVERS; j++) {
992                 if ((vlentry.serverNumber[j] != 255)
993                     && (serveraddrs[vlentry.serverNumber[j]] == 0)) {
994                     printf
995                         ("Volume '%s', index %d points to empty server entry %d\n",
996                          vlentry.name, j, vlentry.serverNumber[j]);
997                 }
998             }
999
1000             if (record[i].type & 0xffffff00)
1001                 printf
1002                     ("Volume '%s' id %u also found on other chains (0x%x)\n",
1003                      vlentry.name, vlentry.volumeId[0], record[i].type);
1004
1005             /* A free entry */
1006         } else if (record[i].type & FR) {
1007             if (!(record[i].type & FRC))
1008                 printf("Free vlentry at %u not on free chain\n",
1009                        record[i].addr);
1010
1011             if (record[i].type & 0xfffffdf0)
1012                 printf
1013                     ("Free vlentry at %u also found on other chains (0x%x)\n",
1014                      record[i].addr, record[i].type);
1015
1016             /* A multihomed entry */
1017         } else if (record[i].type & MH) {
1018             if (!(record[i].type & MHC))
1019                 printf("Multihomed block at %u is orphaned\n",
1020                        record[i].addr);
1021
1022             if (record[i].type & 0xfffffef0)
1023                 printf
1024                     ("Multihomed block at %u also found on other chains (0x%x)\n",
1025                      record[i].addr, record[i].type);
1026
1027         } else {
1028             printf("Unknown entry type at %u (0x%x)\n", record[i].addr,
1029                    record[i].type);
1030         }
1031     }
1032     return 0;
1033 }
1034
1035 main(argc, argv)
1036      int argc;
1037      char **argv;
1038 {
1039     struct cmd_syndesc *ts;
1040
1041     setlinebuf(stdout);
1042
1043     ts = cmd_CreateSyntax(NULL, WorkerBee, NULL, "vldb check");
1044     cmd_AddParm(ts, "-database", CMD_SINGLE, CMD_REQUIRED, "vldb_file");
1045     cmd_AddParm(ts, "-uheader", CMD_FLAG, CMD_OPTIONAL,
1046                 "Display UBIK header");
1047     cmd_AddParm(ts, "-vheader", CMD_FLAG, CMD_OPTIONAL,
1048                 "Display VLDB header");
1049     cmd_AddParm(ts, "-servers", CMD_FLAG, CMD_OPTIONAL,
1050                 "Display server list");
1051     cmd_AddParm(ts, "-entries", CMD_FLAG, CMD_OPTIONAL, "Display entries");
1052     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL, "verbose");
1053
1054     return cmd_Dispatch(argc, argv);
1055 }