cacb4da2abb171cf90f898eff7d719eb8ad6ce37
[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 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #ifdef AFS_NT40_ENV
16 #include <WINNT/afsevent.h>
17 #endif
18
19 #include <ubik.h>
20 #include <afs/afsutil.h>
21 #include <afs/cmd.h>
22
23 #include "vlserver.h"
24 #include "vldbint.h"
25
26 /* Read a VLDB file and verify it for correctness */
27
28 #define VL  0x001               /* good volume entry */
29 #define FR  0x002               /* free volume entry */
30 #define MH  0x004               /* multi-homed entry */
31
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 */
36
37 #define MHC 0x100               /* on multihomed chain */
38 #define FRC 0x200               /* on free chain */
39
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) */
44
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) */
49
50 #define MISRWH 0x100000          /* mischained (RW) */
51 #define MISROH 0x200000          /* mischained (RO) */
52 #define MISBKH 0x400000          /* mischained (BK) */
53 #define MISNH  0x800000          /* mischained (name) */
54
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)
61
62 #define ADDR(x) (x/sizeof(struct nvlentry))
63
64 int fd;
65 int listentries, listservers, listheader, listuheader, verbose, quiet;
66
67 int fix = 0;
68 int passes = 0;
69 /* if quiet, don't send anything to stdout */
70 int quiet = 0;
71 /*  error level. 0 = no error, 1 = warning, 2 = error, 4 = fatal */
72 int error_level  = 0;
73
74 struct er {
75     long addr;
76     int type;
77 } *record;
78 afs_int32 maxentries;
79 int serveraddrs[MAXSERVERID + 2];
80
81 /*  Used to control what goes to stdout based on quiet flag */
82 void
83 quiet_println(const char *fmt,...) {
84     va_list args;
85     if (!quiet) {
86         va_start(args, fmt);
87         vfprintf(stdout, fmt, args);
88         va_end(args);
89     }
90 }
91
92 /*  Used to set the error level and ship messages to stderr */
93 void
94 log_error(int eval, const char *fmt, ...)
95 {
96     va_list args;
97     if (error_level < eval) error_level  = eval ;  /*  bump up the severity */
98     va_start(args, fmt);
99     vfprintf(stderr, fmt, args);
100     va_end(args);
101
102     if (error_level  == VLDB_CHECK_FATAL) exit(VLDB_CHECK_FATAL);
103 }
104
105
106 #define HDRSIZE 64
107 int
108 readUbikHeader(void)
109 {
110     int offset, r;
111     struct ubik_hdr uheader;
112
113     offset = lseek(fd, 0, 0);
114     if (offset != 0) {
115         log_error(VLDB_CHECK_FATAL,"error: lseek to 0 failed: %d %d\n", offset, errno);
116         return (VLDB_CHECK_FATAL);
117     }
118
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,
123                errno);
124         return (VLDB_CHECK_FATAL);
125     }
126
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);
131
132     if (listuheader) {
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);
138     }
139
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,
142                HDRSIZE);
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,
145                UBIK_MAGIC);
146
147     return (0);
148 }
149
150 int
151 vldbio(int position, void *buffer, int size, int rdwr)
152 {
153     int offset, r, p;
154
155     /* seek to the correct spot. skip ubik stuff */
156     p = position + HDRSIZE;
157     offset = lseek(fd, p, 0);
158     if (offset != p) {
159         log_error(VLDB_CHECK_FATAL,"error: lseek to %d failed: %d %d\n", p, offset, errno);
160         return (-1);
161     }
162
163     if (rdwr == 1)
164         r = write(fd, buffer, size);
165     else
166         r = read(fd, buffer, size);
167
168     if (r != size) {
169         log_error(VLDB_CHECK_FATAL,"error: %s of %d bytes failed: %d %d\n", rdwr==1?"write":"read",
170                size, r, errno);
171         return (-1);
172     }
173     return (0);
174 }
175
176 char *
177 vtype(int type)
178 {
179     static char Type[3];
180
181     if (type == 0)
182         strcpy(Type, "rw");
183     else if (type == 1)
184         strcpy(Type, "ro");
185     else if (type == 2)
186         strcpy(Type, "bk");
187     else
188         strcpy(Type, "??");
189     return (Type);
190 }
191
192 afs_int32
193 NameHash(char *volname)
194 {
195     unsigned int hash;
196     char *vchar;
197
198     hash = 0;
199     for (vchar = volname + strlen(volname) - 1; vchar >= volname; vchar--)
200         hash = (hash * 63) + (*((unsigned char *)vchar) - 63);
201     return (hash % HASHSIZE);
202 }
203
204 afs_int32
205 IdHash(afs_uint32 volid)
206 {
207     return ((abs(volid)) % HASHSIZE);
208 }
209
210 #define LEGALCHARS ".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
211 int
212 InvalidVolname(char *volname)
213 {
214     char *map;
215     size_t slen;
216
217     map = LEGALCHARS;
218     slen = strlen(volname);
219     if (slen >= VL_MAXNAMELEN)
220         return 1;
221     return (slen != strspn(volname, map));
222 }
223
224 int
225 validVolumeAddr(afs_uint32 fileOffset)
226 {
227     if (ADDR(fileOffset) >= maxentries) {
228         /* Are we in range */
229         return 0;
230     }
231     /*
232      * We cannot test whether the offset is aligned
233      * since the vl entries are not in a regular array
234      */
235     return 1;
236 }
237
238 void
239 readheader(struct vlheader *headerp)
240 {
241     int i, j;
242
243     vldbread(0, (char *)headerp, sizeof(*headerp));
244
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]);
260
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]);
269
270     if (listheader) {
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,
289                headerp->SIT);
290         quiet_println("   server ip addr   table: size = %d entries\n",
291                MAXSERVERID + 1);
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",
294                MAXTYPES, HASHSIZE);
295     }
296
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));
301     return;
302 }
303
304 void
305 writeheader(struct vlheader *headerp)
306 {
307     int i, j;
308
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]);
324
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]);
333
334     vldbwrite(0, (char *)headerp, sizeof(*headerp));
335 }
336
337 void
338 readMH(afs_int32 addr, struct extentaddr *mhblockP)
339 {
340     int i, j;
341     struct extentaddr *e;
342
343     vldbread(addr, (char *)mhblockP, VL_ADDREXTBLK_SIZE);
344
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]);
349
350     for (i = 1; i < VL_MHSRV_PERBLK; i++) {
351         e = &(mhblockP[i]);
352
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]);
357     }
358     return;
359 }
360
361 void
362 readentry(afs_int32 addr, struct nvlentry *vlentryp, afs_int32 *type)
363 {
364     int i;
365
366     vldbread(addr, (char *)vlentryp, sizeof(*vlentryp));
367
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];
382     }
383
384     if (vlentryp->flags == VLCONTBLOCK) {
385         *type = MH;
386     } else if (vlentryp->flags == VLFREE) {
387         *type = FR;
388     } else {
389         *type = VL;
390     }
391
392     if (listentries) {
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");
398         } else {
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");
420
421             /* all bits not covered by VLF_* and VLOP_* constants */
422             if (vlentryp->flags & 0xffff8e0f)
423                 quiet_println(" errorflag(0x%x)", vlentryp->flags);
424             quiet_println("\n");
425             quiet_println("   LockAfsId     = %d\n", vlentryp->LockAfsId);
426             quiet_println("   LockTimestamp = %d\n", vlentryp->LockTimestamp);
427             quiet_println("   cloneId       = %u\n", vlentryp->cloneId);
428             quiet_println
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");
445                     quiet_println("\n");
446                 }
447             }
448         }
449     }
450     return;
451 }
452
453 void
454 writeentry(afs_int32 addr, struct nvlentry *vlentryp)
455 {
456     int i;
457
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] ;
473     }
474     vldbwrite(addr, (char *)vlentryp, sizeof(*vlentryp));
475 }
476
477 /*
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.
481  */
482 void
483 ReadAllEntries(struct vlheader *header)
484 {
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;
489     afs_uint32 addr;
490     afs_uint32 entrysize = 0;
491     afs_uint32 maxvolid = 0;
492
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) {
496
497         /* Remember the highest volume id */
498         readentry(addr, &vlentry, &type);
499         if (type == VL) {
500             if (!(vlentry.flags & VLF_RWEXISTS))
501                 log_error(VLDB_CHECK_WARNING,"VLDB_CHECK_WARNING: VLDB entry '%s' has no RW volume\n",
502                        vlentry.name);
503
504             for (i = 0; i < MAXTYPES; i++)
505                 if (maxvolid < vlentry.volumeId[i])
506                     maxvolid = vlentry.volumeId[i];
507
508             e = 1;
509             for (j = 0; j < NMAXNSERVERS; j++) {
510                 if (vlentry.serverNumber[j] == 255)
511                     continue;
512                 if (vlentry.serverFlags[j] & (VLSF_ROVOL | VLSF_NEWREPSITE)) {
513                     rocount++;
514                     continue;
515                 }
516                 if (vlentry.serverFlags[j] & VLSF_RWVOL) {
517                     rwcount++;
518                     if (vlentry.flags & VLF_BACKEXISTS)
519                         bkcount++;
520                     continue;
521                 }
522                 if (!vlentry.serverFlags[j]) {
523                     /*e = 0;*/
524                     continue;
525                 }
526                 if (e) {
527                    log_error
528                         (VLDB_CHECK_ERROR,"VLDB entry '%s' contains an unknown RW/RO index serverFlag\n",
529                          vlentry.name);
530                     e = 0;
531                 }
532                 quiet_println
533                     ("   index %d : serverNumber %d : serverPartition %d : serverFlag %d\n",
534                      j, vlentry.serverNumber[j], vlentry.serverPartition[j],
535                      vlentry.serverFlags[j]);
536             }
537         }
538
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",
542                    rindex);
543             return;
544         }
545         record[rindex].addr = addr;
546         record[rindex].type = type;
547
548         /* Determine entrysize and keep count */
549         if (type == VL) {
550             entrysize = sizeof(vlentry);
551             vlcount++;
552         } else if (type == FR) {
553             entrysize = sizeof(vlentry);
554             freecount++;
555         } else if (type == MH) {
556             entrysize = VL_ADDREXTBLK_SIZE;
557             mhcount++;
558         } else {
559             log_error(VLDB_CHECK_ERROR, "Unknown entry at %u. Aborting\n", addr);
560             break;
561         }
562     }
563     if (verbose) {
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,
567                bkcount, rocount);
568     }
569
570     /* Check the maxmimum volume id in the header */
571     if (maxvolid != header->vital_header.MaxVolumeId - 1)
572         quiet_println
573             ("Header's maximum volume id is %u and largest id found in VLDB is %u\n",
574              header->vital_header.MaxVolumeId, maxvolid);
575 }
576
577 /*
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.
581  */
582 void
583 FollowNameHash(struct vlheader *header)
584 {
585     int count = 0, longest = 0, shortest = -1, chainlength;
586     struct nvlentry vlentry;
587     afs_uint32 addr;
588     afs_int32 i, type, rindex;
589
590     /* Now follow the Name Hash Table */
591     if (verbose) quiet_println("Check Volume Name Hash\n");
592     for (i = 0; i < HASHSIZE; i++) {
593         chainlength = 0;
594
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]);
598             continue;
599         }
600
601         for (addr = header->VolnameHash[i]; addr; addr = vlentry.nextNameHash) {
602             readentry(addr, &vlentry, &type);
603             if (type != VL) {
604                 log_error(VLDB_CHECK_ERROR,"Name Hash %d: Bad entry at %u: Not a valid vlentry\n",
605                        i, addr);
606                 continue;
607             }
608
609             rindex = ADDR(addr);
610
611             /*
612              * we know that the address is valid because we
613              * checked it either above or below
614              */
615             if (record[rindex].addr != addr && record[rindex].addr) {
616                 log_error
617                     (VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: addresses %ld and %u use same record slot %d\n",
618                      record[rindex].addr, addr, rindex);
619             }
620             if (record[rindex].type & NH) {
621                 log_error
622                     (VLDB_CHECK_ERROR,"Name Hash %d: Bad entry '%s': Already in the name hash\n",
623                      i, vlentry.name);
624                 record[rindex].type |= MULTN;
625                 break;
626             }
627
628             if (!validVolumeAddr(vlentry.nextNameHash)) {
629                 log_error(VLDB_CHECK_ERROR,"Name Hash forward link of '%s' is out of range\n",
630                           vlentry.name);
631                 record[rindex].type |= MULTN;
632                 break;
633             }
634
635             record[rindex].type |= NH;
636             record[rindex].type |= REFN;
637
638             chainlength++;
639             count++;
640
641             /* Hash the name and check if in correct hash table */
642             if (NameHash(vlentry.name) != i) {
643                 log_error
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;
647             }
648         }
649         if (chainlength > longest)
650             longest = chainlength;
651         if ((shortest == -1) || (chainlength < shortest))
652             shortest = chainlength;
653     }
654     if (verbose) {
655         quiet_println
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));
658     }
659     return;
660 }
661
662 /*
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.
666  */
667 void
668 FollowIdHash(struct vlheader *header)
669 {
670     int count = 0, longest = 0, shortest = -1, chainlength;
671     struct nvlentry vlentry;
672     afs_uint32 addr;
673     afs_int32 i, j, hash, type, rindex, ref, badref, badhash;
674
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));
682         count = longest = 0;
683         shortest = -1;
684
685         for (j = 0; j < HASHSIZE; j++) {
686             chainlength = 0;
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]);
690                 continue;
691             }
692
693             for (addr = header->VolidHash[i][j]; addr;
694                  addr = vlentry.nextIdHash[i]) {
695                 readentry(addr, &vlentry, &type);
696                 if (type != VL) {
697                     log_error
698                         (VLDB_CHECK_ERROR,"%s Id Hash %d: Bad entry at %u: Not a valid vlentry\n",
699                          vtype(i), j, addr);
700                     continue;
701                 }
702
703                 rindex = ADDR(addr);
704                 if (record[rindex].addr != addr && record[rindex].addr) {
705                     log_error
706                         (VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: addresses %ld and %u use same record slot %d\n",
707                          record[rindex].addr, addr, rindex);
708                 }
709                 if (record[rindex].type & hash) {
710                     log_error
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;
714                     break;
715                 }
716
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;
721                     break;
722                 }
723
724                 record[rindex].type |= hash;
725                 record[rindex].type |= ref;
726
727                 chainlength++;
728                 count++;
729
730                 /* Hash the id and check if in correct hash table */
731                 if (IdHash(vlentry.volumeId[i]) != j) {
732                    log_error
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;
737                 }
738             }
739
740             if (chainlength > longest)
741                 longest = chainlength;
742             if ((shortest == -1) || (chainlength < shortest))
743                 shortest = chainlength;
744         }
745         if (verbose) {
746             quiet_println
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));
749         }
750     }
751     return;
752 }
753
754 /*
755  * Follow the free chain.
756  * Record we found it in the free chain within the record array.
757  */
758 void
759 FollowFreeChain(struct vlheader *header)
760 {
761     afs_int32 count = 0;
762     struct nvlentry vlentry;
763     afs_uint32 addr;
764     afs_int32 type, rindex;
765
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);
771         if (type != FR) {
772            log_error
773                 (VLDB_CHECK_ERROR,"Free Chain %d: Bad entry at %u: Not a valid free vlentry (0x%x)\n",
774                  count, addr, type);
775             continue;
776         }
777
778         rindex = addr / sizeof(vlentry);
779         if (record[rindex].addr != addr && record[rindex].addr) {
780            log_error
781                 (VLDB_CHECK_ERROR,"INTERNAL VLDB_CHECK_ERROR: addresses %u and %ld use same record slot %d\n",
782                  record[rindex].addr, addr, rindex);
783         }
784         if (record[rindex].type & FRC) {
785             log_error(VLDB_CHECK_ERROR,"Free Chain: Bad entry at %u: Already in the free chain\n",
786                    addr);
787             break;
788         }
789         record[rindex].type |= FRC;
790
791         count++;
792     }
793     if (verbose)
794      quiet_println("%d entries on free chain\n", count);
795     return;
796 }
797
798 /*
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.
802  *
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.
807  *
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.
810  *
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.
813  */
814 void
815 CheckIpAddrs(struct vlheader *header)
816 {
817     int mhblocks = 0;
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;
825     afsUUID nulluuid;
826
827     memset(&nulluuid, 0, sizeof(nulluuid));
828
829     if (verbose)
830         quiet_println("Check Multihomed blocks\n");
831
832     if (header->SIT) {
833         /* Read the first MH block and from it, gather the
834          * addresses of all the mh blocks.
835          */
836         readMH(header->SIT, MHblock);
837         if (MHblock->ex_hdrflags != VLCONTBLOCK) {
838            log_error
839                 (VLDB_CHECK_ERROR,"Multihomed Block 0: Bad entry at %u: Not a valid multihomed block\n",
840                  header->SIT);
841         }
842
843         for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
844             caddrs[i] = MHblock->ex_contaddrs[i];
845         }
846
847         if (header->SIT != caddrs[0]) {
848            log_error
849                 (VLDB_CHECK_ERROR,"MH block does not point to self %u in header, %u in block\n",
850                  header->SIT, caddrs[0]);
851         }
852
853         /* Now read each MH block and record it in the record array */
854         for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
855             if (!caddrs[i])
856                 continue;
857
858             readMH(caddrs[i], MHblock);
859             if (MHblock->ex_hdrflags != VLCONTBLOCK) {
860                 log_error
861                     (VLDB_CHECK_ERROR,"Multihomed Block 0: Bad entry at %u: Not a valid multihomed block\n",
862                      header->SIT);
863             }
864
865             rindex = caddrs[i] / sizeof(vlentry);
866             if (record[rindex].addr != caddrs[i] && record[rindex].addr) {
867                 log_error
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);
870             }
871             if (record[rindex].type & FRC) {
872                 log_error
873                     (VLDB_CHECK_ERROR,"MH Blocks Chain %d: Bad entry at %ld: Already a MH block\n",
874                      i, record[rindex].addr);
875                 break;
876             }
877             record[rindex].type |= MHC;
878
879             mhblocks++;
880
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).
884              */
885             mhentries = 0;
886             for (j = 1; j < VL_MHSRV_PERBLK; j++) {
887                 e = (struct extentaddr *)&(MHblock[j]);
888
889                 /* Search the IpMappedAddr array for the reference to this entry */
890                 for (ipindex = 0; ipindex < MAXSERVERID; ipindex++) {
891                     if (((header->IpMappedAddr[ipindex] & 0xff000000) ==
892                          0xff000000)
893                         &&
894                         (((header->
895                            IpMappedAddr[ipindex] & 0x00ff0000) >> 16) == i)
896                         && ((header->IpMappedAddr[ipindex] & 0x0000ffff) ==
897                             j)) {
898                         break;
899                     }
900                 }
901                 if (ipindex >= MAXSERVERID)
902                     ipindex = -1;
903                 else
904                     serveraddrs[ipindex] = -1;
905
906                 if (memcmp(&e->ex_hostuuid, &nulluuid, sizeof(afsUUID)) == 0) {
907                     if (ipindex != -1) {
908                         log_error
909                             (VLDB_CHECK_ERROR,"Server Addrs index %d references null MH block %d, index %d\n",
910                              ipindex, i, j);
911                         serveraddrs[ipindex] = 0;       /* avoids printing 2nd error below */
912                     }
913                     continue;
914                 }
915
916                 /* Step through each ip address and count the good addresses */
917                 ipaddrs = 0;
918                 for (m = 0; m < VL_MAXIPADDRS_PERMH; m++) {
919                     if (e->ex_addrs[m])
920                         ipaddrs++;
921                 }
922
923                 /* If we found any good ip addresses, mark it in the serveraddrs record */
924                 if (ipaddrs) {
925                     mhentries++;
926                     if (ipindex == -1) {
927                         log_error
928                             (VLDB_CHECK_ERROR,"MH block %d, index %d: Not referenced by server addrs\n",
929                              i, j);
930                     } else {
931                         serveraddrs[ipindex] = ipaddrs; /* It is good */
932                     }
933                 }
934
935                 if (listservers && ipaddrs) {
936                     quiet_println("MH block %d, index %d:", i, j);
937                     for (m = 0; m < VL_MAXIPADDRS_PERMH; m++) {
938                         if (!e->ex_addrs[m])
939                             continue;
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));
945                     }
946                     quiet_println("\n");
947                 }
948             }
949 /*
950  *      if (mhentries != MHblock->ex_count) {
951  *         quiet_println("MH blocks says it has %d entries (found %d)\n",
952  *                MHblock->ex_count, mhentries);
953  *      }
954  */
955         }
956     }
957     if (verbose)
958         quiet_println("%d multihomed blocks\n", mhblocks);
959
960     /* Check the server addresses */
961     if (verbose)
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) {
967                 mhentries++;
968                 if (((header->IpMappedAddr[i] & 0x00ff0000) >> 16) >
969                     VL_MAX_ADDREXTBLKS)
970                    log_error
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))
975                     log_error
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) {
979                     log_error
980                         (VLDB_CHECK_WARNING,"warning: IP Addr for entry %d: Multihome entry has no ip addresses\n",
981                          i);
982                     serveraddrs[i] = 0;
983                 }
984                 if (listservers) {
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));
988                 }
989             } else {
990                 regentries++;
991                 serveraddrs[i] = 1;     /* It is good */
992                 if (listservers) {
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));
998                 }
999             }
1000         }
1001     }
1002     if (verbose) {
1003         quiet_println("%d simple entries, %d multihomed entries, Total = %d\n",
1004                regentries, mhentries, mhentries + regentries);
1005     }
1006     return;
1007 }
1008
1009 char *
1010 nameForAddr(afs_uint32 addr, int hashtype, afs_uint32 *hash, char *buffer)
1011 {
1012     /*
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.
1017      */
1018     afs_int32 type;
1019     struct nvlentry entry;
1020     if (!addr) {
1021         /* Distinguished, invalid, hash */
1022         *hash = 0xFFFFFFFF;
1023         return "empty";
1024     } else if (!validVolumeAddr(addr)) {
1025         /* Different, invalid, hash */
1026         *hash = 0XFFFFFFFE;
1027         return "invalid";
1028     }
1029     readentry(addr, &entry, &type);
1030     if (VL != type) {
1031         *hash = 0XFFFFFFFE;
1032         return "invalid";
1033     }
1034     if (hashtype >= MAXTYPES) {
1035         *hash = NameHash(entry.name);
1036     } else {
1037         *hash = IdHash(entry.volumeId[hashtype]);
1038     }
1039     sprintf(buffer, "for '%s'", entry.name);
1040     return buffer;
1041 }
1042
1043 void
1044 reportHashChanges(struct vlheader *header, afs_uint32 oldnamehash[HASHSIZE], afs_uint32 oldidhash[MAXTYPES][HASHSIZE])
1045 {
1046     int i, j;
1047     afs_uint32 oldhash, newhash;
1048     char oldNameBuffer[10 + VL_MAXNAMELEN];
1049     char newNameBuffer[10 + VL_MAXNAMELEN];
1050     char *oldname, *newname;
1051     /*
1052      * report hash changes
1053      */
1054
1055     for (i = 0; i < HASHSIZE; i++) {
1056         if (oldnamehash[i] != header->VolnameHash[i]) {
1057
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);
1062             }
1063         }
1064         for (j = 0; j < MAXTYPES; j++) {
1065             if (oldidhash[j][i] != header->VolidHash[j][i]) {
1066
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);
1071                 }
1072             }
1073         }
1074     }
1075 }
1076
1077 int
1078 WorkerBee(struct cmd_syndesc *as, void *arock)
1079 {
1080     char *dbfile;
1081     afs_int32 type;
1082     struct vlheader header;
1083     struct nvlentry vlentry, vlentry2;
1084     int i, j;
1085     afs_uint32 oldnamehash[HASHSIZE];
1086     afs_uint32 oldidhash[MAXTYPES][HASHSIZE];
1087
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  */
1097
1098     /* sanity check */
1099     if (quiet && (verbose || listuheader || listheader ||listservers \
1100                 || listentries)) {
1101         log_error(VLDB_CHECK_FATAL," -quiet cannot be used other display flags\n");
1102         return VLDB_CHECK_FATAL;
1103     }
1104
1105
1106     /* open the vldb database file */
1107     fd = open(dbfile, (fix > 0)?O_RDWR:O_RDONLY, 0);
1108     if (fd < 0) {
1109         log_error(VLDB_CHECK_FATAL,"can't open file '%s'. error = %d\n", dbfile, errno);
1110         return 0;
1111     }
1112
1113     /* read the ubik header and the vldb database header */
1114     readUbikHeader();
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;
1119     }
1120
1121     maxentries = (header.vital_header.eofPtr / sizeof(vlentry)) + 1;
1122     record = calloc(maxentries, sizeof(struct er));
1123     memset(serveraddrs, 0, sizeof(serveraddrs));
1124
1125     /* Will fill in the record array of entries it found */
1126     ReadAllEntries(&header);
1127     listentries = 0;            /* Listed all the entries */
1128
1129     /* Check the multihomed blocks for valid entries as well as
1130      * the IpMappedAddrs array in the header for valid entries.
1131      */
1132     CheckIpAddrs(&header);
1133
1134     /* Follow the hash tables */
1135     FollowNameHash(&header);
1136     FollowIdHash(&header);
1137
1138     /* Follow the chain of free entries */
1139     FollowFreeChain(&header);
1140
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).
1144      */
1145     if (verbose)
1146         quiet_println("Verify each volume entry\n");
1147     for (i = 0; i < maxentries; i++) {
1148         int hash = 0;
1149         int nexthash = 0;
1150         char *which = NULL;
1151
1152         if (record[i].type == 0)
1153             continue;
1154
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.
1157          */
1158         if (record[i].type & VL) {
1159             int foundbad = 0;
1160             int foundbroken = 0;
1161             char volidbuf[256];
1162
1163             readentry(record[i].addr, &vlentry, &type);
1164
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);
1168
1169             if (!(record[i].type & NH)) {
1170                 hash = NameHash(vlentry.name);
1171                 which = "name";
1172                 volidbuf[0]='\0';
1173                 foundbad = 1;
1174             }
1175
1176             if (vlentry.volumeId[0] && !(record[i].type & RWH)) {
1177                 hash = IdHash(vlentry.volumeId[0]);
1178                 which = "RW";
1179                 sprintf(volidbuf, "id %u ", vlentry.volumeId[0]);
1180                 foundbad = 1;
1181             }
1182
1183             if (vlentry.volumeId[1] && !(record[i].type & ROH)) {
1184                 hash = IdHash(vlentry.volumeId[1]);
1185                 which = "RO";
1186                 sprintf(volidbuf, "id %u ", vlentry.volumeId[1]);
1187                 foundbad = 1;
1188             }
1189
1190             if (vlentry.volumeId[2] && !(record[i].type & BKH)) {
1191                 hash = IdHash(vlentry.volumeId[2]);
1192                 which = "BK";
1193                 sprintf(volidbuf, "id %u ", vlentry.volumeId[2]);
1194                 foundbad = 1;
1195             }
1196
1197             if (!validVolumeAddr(vlentry.nextNameHash) ||
1198                 record[ADDR(vlentry.nextNameHash)].type & MULTN) {
1199                 hash = NameHash(vlentry.name);
1200                 which = "name";
1201                 volidbuf[0]='\0';
1202                 if (validVolumeAddr(vlentry.nextNameHash)) {
1203                     readentry(vlentry.nextNameHash, &vlentry2, &type);
1204                     nexthash = NameHash(vlentry2.name);
1205                 } else {
1206                     nexthash = 0xFFFFFFFF;
1207                 }
1208                 if (hash != nexthash)
1209                     foundbroken = 1;
1210             }
1211
1212             if (!validVolumeAddr(vlentry.nextIdHash[0]) ||
1213                 record[ADDR(vlentry.nextIdHash[0])].type & MULTRW) {
1214                 hash = IdHash(vlentry.volumeId[0]);
1215                 which = "RW";
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]);
1220                 } else {
1221                     nexthash = 0xFFFFFFFF;
1222                 }
1223                 if (hash != nexthash)
1224                     foundbroken = 1;
1225             }
1226
1227             if (!validVolumeAddr(vlentry.nextIdHash[1]) ||
1228                 record[ADDR(vlentry.nextIdHash[1])].type & MULTRO) {
1229                 hash = IdHash(vlentry.volumeId[1]);
1230                 which = "RO";
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]);
1235                 } else {
1236                     nexthash = 0xFFFFFFFF;
1237                 }
1238                 if (hash != nexthash)
1239                     foundbroken = 1;
1240             }
1241
1242             if (!validVolumeAddr(vlentry.nextIdHash[2]) ||
1243                 record[ADDR(vlentry.nextIdHash[2])].type & MULTBK) {
1244                 hash = IdHash(vlentry.volumeId[2]);
1245                 which = "BK";
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]);
1250                 } else {
1251                     nexthash = 0xFFFFFFFF;
1252                 }
1253                 if (hash != nexthash)
1254                     foundbroken = 1;
1255             }
1256
1257             if (foundbroken) {
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);
1263             }
1264
1265             for (j = 0; j < NMAXNSERVERS; j++) {
1266                 if ((vlentry.serverNumber[j] != 255)
1267                     && (serveraddrs[vlentry.serverNumber[j]] == 0)) {
1268                    log_error
1269                         (VLDB_CHECK_ERROR,"Volume '%s', index %d points to empty server entry %d\n",
1270                          vlentry.name, j, vlentry.serverNumber[j]);
1271                 }
1272             }
1273
1274             if (record[i].type & 0xffff0f00)
1275                 log_error
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);
1278
1279             /* A free entry */
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",
1283                        record[i].addr);
1284
1285             if (record[i].type & 0xfffffdf0)
1286                 log_error
1287                     (VLDB_CHECK_ERROR,"Free vlentry at %ld also found on other chains (0x%x)\n",
1288                      record[i].addr, record[i].type);
1289
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",
1294                        record[i].addr);
1295
1296             if (record[i].type & 0xfffffef0)
1297                 log_error
1298                     (VLDB_CHECK_ERROR,"Multihomed block at %ld also found on other chains (0x%x)\n",
1299                      record[i].addr, record[i].type);
1300
1301         } else {
1302             log_error(VLDB_CHECK_ERROR,"Unknown entry type at %u (0x%x)\n", record[i].addr,
1303                    record[i].type);
1304         }
1305     }
1306
1307     if (fix) {
1308         /*
1309          * If we are fixing we will rebuild all the hash lists from the ground up
1310          */
1311         memcpy(oldnamehash, header.VolnameHash, sizeof(oldnamehash));
1312         memset(header.VolnameHash, 0, sizeof(header.VolnameHash));
1313
1314         memcpy(oldidhash, header.VolidHash, sizeof(oldidhash));
1315         memset(header.VolidHash, 0, sizeof(header.VolidHash));
1316         quiet_println("Rebuilding %u entries\n", maxentries);
1317     } else {
1318         quiet_println("Scanning %u entries for possible repairs\n", maxentries);
1319     }
1320     for (i = 0; i < maxentries; i++) {
1321         afs_uint32 hash;
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);
1327             }
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);
1331             }
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);
1335             }
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);
1339             }
1340             if (fix) {
1341                 afs_uint32 oldhash, newhash;
1342                 char oldNameBuffer[10 + VL_MAXNAMELEN];
1343                 char newNameBuffer[10 + VL_MAXNAMELEN];
1344                 char *oldname, *newname;
1345
1346                 /*
1347                  * Put the current hash table contexts into our 'next'
1348                  * and our address into the hash table.
1349                  */
1350                 hash = NameHash(vlentry.name);
1351
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]))) {
1358                         /*
1359                          * That is, only report if we are verbose
1360                          * or the hash is changing (and one side wasn't NULL
1361                          */
1362                         quiet_println("FIX: Name hash link for '%s' was %s, is now %s\n",
1363                               vlentry.name, oldname, newname);
1364                     }
1365                 }
1366
1367                 vlentry.nextNameHash = header.VolnameHash[hash];
1368                 header.VolnameHash[hash] = record[i].addr;
1369
1370                 for (j = 0; j < MAXTYPES; j++) {
1371
1372                     if (0 == vlentry.volumeId[j]) {
1373                         /*
1374                          * No volume of that type.  Continue
1375                          */
1376                         continue;
1377                     }
1378                     hash = IdHash(vlentry.volumeId[j]);
1379
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);
1388                         }
1389                     }
1390
1391                     vlentry.nextIdHash[j] = header.VolidHash[j][hash];
1392                     header.VolidHash[j][hash] = record[i].addr;
1393                 }
1394                 writeentry(record[i].addr, &vlentry);
1395             }
1396         }
1397     }
1398     if (fix) {
1399         reportHashChanges(&header, oldnamehash, oldidhash);
1400         writeheader(&header);
1401     }
1402
1403     close(fd);
1404
1405     return error_level;
1406 }
1407
1408 int
1409 main(int argc, char **argv)
1410 {
1411     struct cmd_syndesc *ts;
1412
1413     setlinebuf(stdout);
1414
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)");
1427
1428     return cmd_Dispatch(argc, argv);
1429 }