openafs-void-star-pointers-20071031
[openafs.git] / src / vlserver / cnvldb.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 RCSID
14     ("$Header$");
15
16 #include <afs/stds.h>
17 #include <sys/types.h>
18 #include <errno.h>
19 #include <stdio.h>
20 #include <sys/file.h>
21 #include <string.h>
22
23 #include "cnvldb.h"             /* CHANGEME! */
24 #include <netinet/in.h>
25 #include <afs/venus.h>
26 #include <afs/cmd.h>
27 #include <afs/afsutil.h>
28 #include <afs/fileutil.h>
29
30 #include "vlserver.h"
31
32 #define MAXSIZE 2048            /* most I'll get back from PIOCTL */
33 #define BADSERVERID     255     /* XXX */
34
35
36 static char pn[] = "cnvldb";
37 static char tempname[] = "XXnewvldb";
38 static char space[MAXSIZE];
39 static int MaxServers[3] = { 30, 254, 254 };    /* max server # permitted in this version */
40
41 static afs_int32 Conv4to3();
42
43 static int convert_vlentry();
44 static int rewrite_header();
45
46 static char tspace[1024];       /* chdir can't handle anything bigger, anyway */
47 /* return a static pointer to a buffer */
48 static char *
49 Parent(apath)
50      char *apath;
51 {
52     register char *tp;
53     strcpy(tspace, apath);
54     tp = strrchr(tspace, '/');
55     if (tp) {
56         *tp = 0;
57     } else
58         strcpy(tspace, ".");
59     return tspace;
60 }
61
62 int oldpos = 0;
63 int fromvers = 0, tovers = 0, showversion = 0;
64 afs_uint32 mhaddr;
65 afs_int32 dbsize;
66 char *pathname = NULL;
67 const char *dbPath;
68
69 static int
70 handleit(struct cmd_syndesc *as, void *arock)
71 {
72     register struct cmd_item *ti;
73     register afs_int32 code;
74     int w, old, new, rc, dump = 0, fromv = 0;
75     short uvers;
76     char ubik[80];              /* space for some ubik header */
77     union {
78         struct vlheader_1 header1;
79         struct vlheader_2 header2;
80         struct vlheader_3 header3;
81     } oldheader, oldheader1, newheader; /* large enough for either */
82
83     union {
84         struct vlentry_1 entry1;
85         struct vlentry_2 entry2;
86         struct vlentry_3 entry3;
87         char mhinfo_block[VL_ADDREXTBLK_SIZE];
88     } xvlentry;
89
90     pathname = (as->parms[2].items ? as->parms[2].items->data : dbPath);        /* -name */
91     showversion = (as->parms[3].items ? 1 : 0); /* -showversion */
92     dump = (as->parms[4].items ? 1 : 0);        /* -dumpvldb */
93     fromvers = (as->parms[1].items ? atoi(as->parms[1].items->data) : 0);       /* -fromversion */
94     tovers = (as->parms[0].items ? atoi(as->parms[0].items->data) : 0); /* -toversion */
95
96     /* should stat() the old vldb, get its size, and see if there's */
97     /* room for another.  It might be in AFS, so check the quota, too */
98     old = open(pathname, O_RDONLY);
99     if (old < 0) {
100         perror(pn);
101         exit(-1);
102     }
103
104     /* Read the version */
105     lseek(old, 64, L_SET);
106     read(old, &fromv, sizeof(int));
107     fromv = ntohl(fromv);
108     if ((fromv < 1) || (fromv > 4)) {
109         fprintf(stderr, pn);
110         fprintf(stderr, ": Unrecognized VLDB version %d.\n", fromv);
111         exit(-1);
112     }
113
114     /* Sequentially read the database converting the entries as we go */
115     lseek(old, 0, L_SET);
116     read(old, ubik, 64);
117     readheader(old, fromv, &oldheader);
118     if (fromv == 1) {
119         dbsize = ntohl(oldheader.header1.vital_header.eofPtr);
120         fromv = ntohl(oldheader.header1.vital_header.vldbversion);
121         mhaddr = 0;
122     } else if (fromv == 2) {
123         dbsize = ntohl(oldheader.header2.vital_header.eofPtr);
124         fromv = ntohl(oldheader.header2.vital_header.vldbversion);
125         mhaddr = 0;
126     } else {
127         int pos;
128
129         dbsize = ntohl(oldheader.header3.vital_header.eofPtr);
130         fromv = ntohl(oldheader.header3.vital_header.vldbversion);
131         mhaddr = ntohl(oldheader.header3.SIT);
132
133         /* Read the multihomed extent blocks in */
134         pos = oldpos;
135         read_mhentries(mhaddr, old);
136
137         /* Position back to this after header */
138         lseek(old, pos + 64, L_SET);
139         oldpos = pos;
140     }
141
142     if (showversion || dump) {
143         if (showversion)
144             fprintf(stdout, "%s has a version of %d\n", pathname, fromv);
145         if (dump) {
146             while (oldpos < dbsize) {
147                 rc = readentry(old, fromv, &xvlentry);
148                 if ((rc == 0) || (rc == EOF))
149                     break;
150                 printentry(fromv, &xvlentry);
151             }
152         }
153         exit(0);
154     }
155
156     if (!fromvers) {            /* not set */
157         fromvers = fromv;
158     } else if (fromvers != fromv) {
159         fprintf(stdout,
160                 "%s has a version of %d while the -fromversion specified was %d - aborting\n",
161                 pathname, fromv, fromvers);
162         exit(0);
163     }
164
165     if ((fromvers < 1) || (fromvers > 4)) {
166         fprintf(stderr, pn);
167         fprintf(stderr, ": VLDB version %d is not supported.\n", fromvers);
168         fprintf(stderr, pn);
169         fprintf(stderr, ": Only versions 1-4 are currently supported.\n");
170         exit(-1);
171     }
172
173     if (!tovers)
174         tovers = fromvers + 1;
175
176     if (tovers < 1 || tovers > 4) {
177         fprintf(stderr, pn);
178         fprintf(stderr, ": VLDB version %d is not supported.\n", tovers);
179         fprintf(stderr, pn);
180         fprintf(stderr, ": Only versions 1 - 4 are currently supported.\n");
181         exit(-1);
182     }
183
184     if (mhaddr && (tovers < 3)) {
185         fprintf(stderr, pn);
186         fprintf(stderr, ": Cannot convert. VLDB contains multihome info.\n");
187         exit(-1);
188     }
189
190     /* OK! let's get down to business... */
191
192     if (chdir(Parent(pathname))) {
193         perror(pn);
194         exit(-1);
195     }
196
197     new = open(tempname, O_WRONLY | O_CREAT | O_TRUNC, 0600);
198     if (new < 0) {
199         perror(pn);
200         exit(-1);
201     }
202
203     /* Write the UBIK data */
204     w = write(new, ubik, 64);
205     if (w != 64) {
206         printf("Write of ubik header failed %d; error %u\n", w, errno);
207         exit(1);
208     }
209
210     /* Because we know that all the vldb entries are the same size and type we 
211      * can just read them sequentially, fiddle with the fields, and write
212      * them out again.  If we invent a vldb format that has different
213      * types of entries, then we're going to have to invent new logic for
214      * converting the vldb-- we'll probably have to chase down the various
215      * linked lists in turn, doing lseeks and the like.
216      */
217
218     convert_header(old, new, fromvers, tovers, &oldheader, &newheader);
219     while (oldpos < dbsize) {
220         rc = readentry(old, fromvers, &xvlentry);
221         if ((rc == 0) || (rc == EOF))
222             break;
223         convert_vlentry(new, fromvers, tovers, &oldheader, &newheader,
224                         &xvlentry);
225     }
226
227     /* We have now finished sequentially reading and writing the database.
228      * Now randomly offset into database and update multihome entries.
229      */
230     convert_mhentries(old, new, &newheader, fromvers, tovers);
231     rewrite_header(new, tovers, &newheader);
232
233     close(old);
234     if (fsync(new)) {
235         perror(pn);
236         exit(-1);
237     }
238     close(new);
239
240     renamefile(tempname, pathname);
241     sleep(5);
242     exit(0);
243 }
244
245
246 readheader(fd, version, addr)
247      int fd;
248      int version;
249      char *addr;
250 {
251     int hdrsize, size = 0;
252
253     oldpos = 0;
254     if (version == 1)
255         hdrsize = sizeof(struct vlheader_1);
256     else
257         hdrsize = sizeof(struct vlheader_2);
258
259     size = read(fd, addr, hdrsize);
260     if (size > 0)
261         oldpos += size;
262
263     return;
264 }
265
266 readentry(fd, version, addr)
267      int fd;
268      int version;
269      char *addr;
270 {
271     int rc, rc1;
272     struct vlentry_3 *vl3p = (struct vlentry_3 *)addr;
273     int toread;
274
275     toread =
276         ((version ==
277           1) ? sizeof(struct vlentry_1) : sizeof(struct vlentry_2));
278     rc = read(fd, addr, toread);
279     if (rc != toread)
280         printf("Partial read of vlentry at pos %u: %d\n", oldpos, rc);
281     if (rc > 0)
282         oldpos += rc;
283
284     /* Read a mhblock entry if there is one */
285     if ((rc > 0) && (vl3p->flags == VLCONTBLOCK)) {
286         if (!mhaddr)            /* Remember first mh block */
287             mhaddr = oldpos - rc;
288
289         rc1 = read(fd, &addr[rc], VL_ADDREXTBLK_SIZE - rc);
290         if (rc1 != VL_ADDREXTBLK_SIZE - rc)
291             printf("Partial read of mhblock at pos %u: %d\n", oldpos + rc,
292                    rc1);
293         if (rc1 > 0) {
294             oldpos += rc1;
295             rc += rc1;
296         }
297     }
298
299     return rc;
300 }
301
302 printentry(version, addr)
303      int version;
304      char *addr;
305 {
306     struct vlentry_2 *vl2p = (struct vlentry_2 *)addr;
307     struct vlentry_3 *vl3p = (struct vlentry_3 *)addr;
308     int i;
309
310     /* Don't print anything if the entry is a mh info block */
311     if (vl3p->flags == VLCONTBLOCK) {
312         return;
313     }
314
315     if (version == 1 || version == 2) {
316         printf("%s\t%5d [%10d:%10d:%10d]%8X%8d\n", vl2p->name, vl2p->spares3,
317                vl2p->volumeId[0], vl2p->volumeId[1], vl2p->volumeId[2],
318                vl2p->flags, vl2p->LockAfsId);
319         printf("\t%8d%8d%8d [%7d%7d%7d]%7d% [%4d%4d%4d%4d][%4d%4d%4d%4d]\n",
320                vl2p->LockTimestamp, vl2p->cloneId, vl2p->spares0,
321                vl2p->nextIdHash[0], vl2p->nextIdHash[1], vl2p->nextIdHash[2],
322                vl2p->nextNameHash, vl2p->serverNumber[0],
323                vl2p->serverNumber[1], vl2p->serverNumber[2],
324                vl2p->serverNumber[3], vl2p->serverPartition[0],
325                vl2p->serverPartition[1], vl2p->serverPartition[2],
326                vl2p->serverPartition[3]);
327         printf("\t[%4d%4d%4d%4d]\n", vl2p->serverFlags[0],
328                vl2p->serverFlags[1], vl2p->serverFlags[2],
329                vl2p->serverFlags[3]);
330     } else {                    /* if (version >= 3) */
331
332         if (vl3p->flags == VLFREE)
333             return;
334         printf("%s\tPos=%d NextIdHash=[%d:%d:%d] NextNameHash=%d\n",
335                vl3p->name, (oldpos - sizeof(struct vlentry_3)),
336                vl3p->nextIdHash[0], vl3p->nextIdHash[1], vl3p->nextIdHash[2],
337                vl3p->nextNameHash);
338         printf("\tRW=%u RO=%u BK=%u CL=%u flags=0x%X lockBy=%d lockTime=%u\n",
339                vl3p->volumeId[0], vl3p->volumeId[1], vl3p->volumeId[2],
340                vl3p->cloneId, vl3p->flags, vl3p->LockAfsId,
341                vl3p->LockTimestamp);
342         for (i = 0; i < OMAXNSERVERS; i++) {
343             if ((vl3p->serverNumber[i] & 0xff) != 0xff) {
344                 printf("\tServer=%d Partition=%d flags=%X\n",
345                        vl3p->serverNumber[i], vl3p->serverPartition[i],
346                        vl3p->serverFlags[i]);
347             }
348         }
349     }
350     return;
351 }
352
353 int readmhentries = 0;
354 struct extentaddr *base[VL_MAX_ADDREXTBLKS];
355
356 /* Read the multihome extent blocks in. Check if they are good by
357  * verifying their address is not pass the EOF and the flags are good.
358  * If it's not good, then don't read the block in.
359  */
360 read_mhentries(mh_addr, oldfd)
361      int oldfd;
362      afs_uint32 mh_addr;
363 {
364     afs_uint32 sit, a;
365     afs_int32 code;
366     int j;
367
368     if (readmhentries)
369         return;
370     readmhentries = 1;
371
372     /* Initialize base pointers */
373     for (j = 0; j < VL_MAX_ADDREXTBLKS; j++)
374         base[j] = 0;
375
376     if (!mh_addr)
377         return;
378
379     /* Check if the first extent block is beyond eof. If 
380      * it is, it's not real.
381      */
382     if (mh_addr > dbsize - VL_ADDREXTBLK_SIZE)
383         return;
384
385     /* Now read the first mh extent block */
386     code = lseek(oldfd, mh_addr + 64, L_SET);
387     if (code < 0) {
388         perror("seek MH block");
389         exit(1);
390     }
391     base[0] = (struct extentaddr *)malloc(VL_ADDREXTBLK_SIZE);
392     if (!base[0]) {
393         perror("malloc1");
394         exit(1);
395     }
396     code = read(oldfd, (char *)base[0], VL_ADDREXTBLK_SIZE);
397     if (code != VL_ADDREXTBLK_SIZE) {
398         perror("read MH block");
399         free(base[0]);
400         base[0] = 0;
401         exit(1);
402     }
403
404     /* Verify that this block is the right one */
405     if (ntohl(base[0]->ex_flags) != VLCONTBLOCK) {      /* check if flag is correct */
406         free(base[0]);
407         base[0] = 0;
408         return;
409     }
410
411     /* The first block contains pointers to the other extent blocks.
412      * Check to see if the pointers are good and read them in if they are.
413      */
414     a = mh_addr;
415     for (j = 1; j < VL_MAX_ADDREXTBLKS; j++) {
416         if (!base[0]->ex_contaddrs[j])
417             continue;
418
419         sit = ntohl(base[0]->ex_contaddrs[j]);
420
421         /* Every time we allocate a new extent block, it is allocated after 
422          * the previous ones. But it must be before the EOF.
423          */
424         if ((sit < (a + VL_ADDREXTBLK_SIZE))
425             || (sit > dbsize - VL_ADDREXTBLK_SIZE)) {
426             continue;
427         }
428
429         /* Read the extent block in */
430         sit += 64;
431         code = lseek(oldfd, sit, L_SET);
432         if (code < 0) {
433             perror("seek MH block");
434             exit(1);
435         }
436         base[j] = (struct extentaddr *)malloc(VL_ADDREXTBLK_SIZE);
437         if (!base[j]) {
438             perror("malloc1");
439             exit(1);
440         }
441         code = read(oldfd, (char *)base[j], VL_ADDREXTBLK_SIZE);
442         if (code != VL_ADDREXTBLK_SIZE) {
443             perror("read MH block");
444             exit(1);
445         }
446
447         /* Verify that this block knows its an extent block */
448         if (ntohl(base[j]->ex_flags) != VLCONTBLOCK) {
449             free(base[j]);
450             base[j] = 0;
451             continue;
452         }
453
454         /* The extent block passed our tests */
455         a = ntohl(base[0]->ex_contaddrs[j]);
456     }
457 }
458
459 /* Follow the SIT pointer in the header (mhaddr) to the multihomed
460  * extent blocks and verify that the pointers are good. And fix.
461  * Then convert the multihomed addresses to single address if we
462  * are converting back from version 4.
463  * 
464  * Before this can be called, the routine read_mhentries must be called.
465  */
466 convert_mhentries(oldfd, newfd, header, fromver, tover)
467      int oldfd, newfd;
468      struct vlheader_2 *header;
469      int fromver, tover;
470 {
471     afs_uint32 sit;
472     afs_int32 code;
473     int i, j, modified = 0, w;
474     afs_uint32 raddr, addr;
475     struct extentaddr *exp;
476     int basei, index;
477
478     /* Check if the header says the extent block exists. If
479      * it does, then read_mhentries should have read it in.
480      */
481     if (mhaddr && !base[0]) {
482         printf("Fix Bad base extent block pointer\n");
483         header->SIT = mhaddr = 0;
484     } else if (mhaddr && base[0]) {
485
486         if ((ntohl(header->SIT) != mhaddr) && (tover == 4)) {
487             printf
488                 ("Fix pointer to first base extent block. Was 0x%x, now 0x%x\n",
489                  ntohl(header->SIT), mhaddr);
490             header->SIT = htonl(mhaddr);
491         }
492
493         /* Check if the first block points to itself. If not, then fix it */
494         if (ntohl(base[0]->ex_contaddrs[0]) != mhaddr) {
495             printf("Fix bad pointer in base extent block: Base 0\n");
496             base[0]->ex_contaddrs[0] = htonl(mhaddr);
497             modified = 1;
498         }
499
500         /* The first block contains pointers to the other extent blocks.
501          * Check to see if the pointers are good.
502          */
503         for (j = 1; j < VL_MAX_ADDREXTBLKS; j++) {
504             /* Check if the base extent block says the extent blocks exist.
505              * If it does, then read_mhentries should have read it in.
506              */
507             if (base[0]->ex_contaddrs[j] && !base[j]) {
508                 printf("Fix bad pointer in base extent block: Base %d\n", j);
509                 base[0]->ex_contaddrs[j] = 0;
510                 modified = 1;
511             }
512         }
513
514         /* Now write out the base extent blocks if it changed */
515         if (modified) {
516             code = lseek(newfd, mhaddr + 64, L_SET);
517             if (code < 0) {
518                 perror("seek MH Block");
519                 exit(1);
520             }
521             w = write(newfd, (char *)base[0], VL_ADDREXTBLK_SIZE);
522             if (w != VL_ADDREXTBLK_SIZE) {
523                 perror("write MH Block");
524                 exit(1);
525             }
526         }
527     }
528
529     /* If we are converting from version 4 to version 3, then 
530      * translate any multihome ptrs in the IpMappedAddr array
531      * to true IP addresses.
532      */
533     if ((fromver == 4) && (tover == 3)) {
534         /* Step through the fileserver addresses in the VLDB header
535          * and convert the pointers back to IP addresses.
536          */
537         for (i = 0; i < 254; i++) {
538             addr = ntohl(header->IpMappedAddr[i]);
539             if (addr && ((addr & 0xff000000) == 0xff000000)) {
540                 basei = (addr >> 16) & 0xff;
541                 index = addr & 0xffff;
542
543                 if ((basei >= VL_ADDREXTBLK_SIZE) || !base[basei]) {
544                     fprintf(stderr,
545                             "Warning: mh entry %d has no IP address; ignored!!\n",
546                             i);
547                     header->IpMappedAddr[i] = 0;
548                     continue;
549                 }
550                 exp = &base[basei][index];
551
552                 /* For now return the first ip address back */
553                 for (j = 0; j < VL_MAXIPADDRS_PERMH; j++) {
554                     if (exp->ex_addrs[j]) {
555                         raddr = ntohl(exp->ex_addrs[j]);
556                         break;
557                     }
558                 }
559                 if (j >= VL_MAXIPADDRS_PERMH) {
560                     fprintf(stderr,
561                             "Warning: mh entry %d has no ip address; ignored!!\n",
562                             i);
563                     raddr = 0;
564                 } else {
565                     printf
566                         ("Multi-homed addr: converting to single ip address %d.%d.%d.%d\n",
567                          (raddr >> 24 & 0xff), (raddr >> 16 & 0xff),
568                          (raddr >> 8 & 0xff), (raddr & 0xff));
569                 }
570                 header->IpMappedAddr[i] = htonl(raddr);
571             }
572         }
573         header->SIT = mhaddr = 0;       /* mhinfo block has been removed */
574
575         /* Now step through the hash tables in header updating them.
576          * Because we removed the mh info blocks and some entries they
577          * point to may have changed position.
578          * The VolnameHash
579          */
580         for (i = 0; i < 8191; i++) {
581             header->VolnameHash[i] = Conv4to3(header->VolnameHash[i]);
582         }
583         /* The VolidHash */
584         for (i = 0; i < 3; i++) {
585             for (j = 0; j < 8191; j++) {
586                 header->VolidHash[i][j] = Conv4to3(header->VolidHash[i][j]);
587             }
588         }
589
590         /* Update eofptr to take into account the removal of the mhinfo blocks */
591         header->vital_header.eofPtr = htonl(Conv4to3(dbsize));
592     }
593 }
594
595
596 convert_header(ofd, fd, fromv, tov, fromaddr, toaddr)
597      int ofd, fd, fromv, tov;
598      char *fromaddr, *toaddr;
599 {
600     struct vlheader_1 *tvp1;
601     struct vlheader_2 *tvp2;
602     int i, j, diff, w;
603
604     if (fromv == 1) {
605         if (tov == 1) {
606             memcpy(toaddr, fromaddr, sizeof(struct vlheader_1));
607             tvp1 = (struct vlheader_1 *)toaddr;
608
609             w = write(fd, tvp1, sizeof(struct vlheader_1));
610             if (w != sizeof(struct vlheader_1)) {
611                 printf("Write of header failed %d; error %u\n", w, errno);
612                 exit(1);
613             }
614
615             /* for garbage-collecting... */
616             for (i = 0; i < 31; i++)
617                 tvp1->IpMappedAddr[i] = 0;
618
619         } else if (tov == 2 || tov == 3) {
620             tvp1 = (struct vlheader_1 *)fromaddr;
621             tvp2 = (struct vlheader_2 *)toaddr;
622             memset(tvp2, 0, sizeof(struct vlheader_2));
623             tvp2->vital_header.vldbversion = htonl(tov);
624             tvp2->vital_header.headersize = htonl(sizeof(struct vlheader_2));
625             diff =
626                 ntohl(tvp2->vital_header.headersize) -
627                 ntohl(tvp1->vital_header.headersize);
628             if (ntohl(tvp1->vital_header.freePtr))
629                 tvp2->vital_header.freePtr =
630                     htonl(ntohl(tvp1->vital_header.freePtr) + diff);
631             if (ntohl(tvp1->vital_header.eofPtr))
632                 tvp2->vital_header.eofPtr =
633                     htonl(ntohl(tvp1->vital_header.eofPtr) + diff);
634             tvp2->vital_header.allocs = tvp1->vital_header.allocs;
635             tvp2->vital_header.frees = tvp1->vital_header.frees;
636             tvp2->vital_header.MaxVolumeId = tvp1->vital_header.MaxVolumeId;
637             for (i = 0; i < 3; i++)
638                 tvp2->vital_header.totalEntries[i] =
639                     tvp1->vital_header.totalEntries[i];
640
641             for (i = 0; i < 31; i++)
642                 tvp2->IpMappedAddr[i] = tvp1->IpMappedAddr[i];
643
644             for (i = 0; i < 8191; i++) {
645                 if (ntohl(tvp1->VolnameHash[i]))
646                     tvp2->VolnameHash[i] =
647                         htonl(ntohl(tvp1->VolnameHash[i]) + diff);
648             }
649
650             for (i = 0; i < 3; i++) {
651                 for (j = 0; j < 8191; j++) {
652                     if (ntohl(tvp1->VolidHash[i][j]))
653                         tvp2->VolidHash[i][j] =
654                             htonl(ntohl(tvp1->VolidHash[i][j]) + diff);
655                 }
656             }
657
658             w = write(fd, tvp2, sizeof(struct vlheader_2));
659             if (w != sizeof(struct vlheader_2)) {
660                 printf("Write of header failed %d; error %u\n", w, errno);
661                 exit(1);
662             }
663
664             /* for garbage-collecting... */
665             for (i = 0; i < 31; i++)
666                 tvp2->IpMappedAddr[i] = 0;
667         } else
668             return EINVAL;
669     } else if (fromv == 2 || fromv == 3 || fromv == 4) {
670         if (tov == 2 || tov == 3 || tov == 4) {
671             memcpy(toaddr, fromaddr, sizeof(struct vlheader_2));
672             tvp2 = (struct vlheader_2 *)toaddr;
673             tvp2->vital_header.vldbversion = htonl(tov);
674             w = write(fd, tvp2, sizeof(struct vlheader_2));
675             if (w != sizeof(struct vlheader_2)) {
676                 printf("Write of header failed %d; error %u\n", w, errno);
677                 exit(1);
678             }
679
680         } else if (tov == 1) {
681             tvp2 = (struct vlheader_2 *)fromaddr;
682             tvp1 = (struct vlheader_1 *)toaddr;
683             memset(tvp1, 0, sizeof(struct vlheader_1));
684             tvp1->vital_header.vldbversion = htonl(1);
685             tvp1->vital_header.headersize = htonl(sizeof(struct vlheader_1));
686             diff =
687                 ntohl(tvp1->vital_header.headersize) -
688                 ntohl(tvp2->vital_header.headersize);
689             if (ntohl(tvp2->vital_header.freePtr))
690                 tvp1->vital_header.freePtr =
691                     htonl(ntohl(tvp2->vital_header.freePtr) + diff);
692             if (ntohl(tvp2->vital_header.eofPtr))
693                 tvp1->vital_header.eofPtr =
694                     htonl(ntohl(tvp2->vital_header.eofPtr) + diff);
695             tvp1->vital_header.allocs = tvp2->vital_header.allocs;
696             tvp1->vital_header.frees = tvp2->vital_header.frees;
697             tvp1->vital_header.MaxVolumeId = tvp2->vital_header.MaxVolumeId;
698             for (i = 0; i < 3; i++)
699                 tvp1->vital_header.totalEntries[i] =
700                     tvp2->vital_header.totalEntries[i];
701
702             for (i = 0; i < 31; i++)
703                 tvp1->IpMappedAddr[i] = tvp2->IpMappedAddr[i];
704
705             for (i = 0; i < 8191; i++) {
706                 if (ntohl(tvp2->VolnameHash[i]))
707                     tvp1->VolnameHash[i] =
708                         htonl(ntohl(tvp2->VolnameHash[i]) + diff);
709             }
710
711             for (i = 0; i < 3; i++) {
712                 for (j = 0; j < 8191; j++) {
713                     if (ntohl(tvp2->VolidHash[i][j]))
714                         tvp1->VolidHash[i][j] =
715                             htonl(ntohl(tvp2->VolidHash[i][j]) + diff);
716                 }
717             }
718
719             w = write(fd, tvp1, sizeof(struct vlheader_1));
720             if (w != sizeof(struct vlheader_2)) {
721                 printf("Write of header failed %d; error %u\n", w, errno);
722                 exit(1);
723             }
724
725             /* for garbage-collecting... */
726             for (i = 0; i < 31; i++)
727                 tvp1->IpMappedAddr[i] = 0;
728         } else
729             return EINVAL;
730     } else
731         return EINVAL;
732     return 0;
733 }
734
735
736 /* Convert an address pointer to a vlentry from version 4 to version 3.
737  * This involves checking if the address is after any of the four
738  * MH block and if it is, subtract the size of the MH block. 
739  *
740  * In going from version 4 to 3, the mh blocks go away and all entries
741  * move up in their place. The adresses then need to be updated.
742  *
743  * Before this can be called, the routine read_mhentries must be called.
744  */
745 static afs_int32
746 Conv4to3(addr)
747      afs_int32 addr;
748 {
749     afs_int32 raddr;
750     int i;
751
752     if (!base[0] || !addr)
753         return (addr);
754
755     raddr = addr;
756     for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
757         if (base[i] && base[0]->ex_contaddrs[i]
758             && (addr > base[0]->ex_contaddrs[i]))
759             raddr -= VL_ADDREXTBLK_SIZE;
760     }
761
762     return (raddr);
763 }
764
765 /* this only works because the vlheader struct is essentially the same 
766  * from version 1 to version 2 -- that is, the first bunch of fields
767  * aren't any more or any larger, so they match up pretty well.
768 */
769
770 static int
771 convert_vlentry(new, fromvers, tovers, oldheader, newheader, vlentryp)
772      int new, fromvers, tovers;
773      struct vlheader_1 *oldheader, *newheader;  /* close enough */
774      struct vlentry_1 *vlentryp;        /* 1 and 2 are identical */
775 {
776     int diff, i, s, w;
777     struct vlentry_3 *vl3p = (struct vlentry_3 *)vlentryp;
778
779     /* For mh information blocks,
780      * If going to version 4 or greater, keep the mh info block.
781      * Otherwise, don't keep it (version 3 and earlier don't have them).
782      */
783     if (vl3p->flags == VLCONTBLOCK) {
784         if (tovers >= 4) {
785             w = write(new, vlentryp, VL_ADDREXTBLK_SIZE);
786             if (w != VL_ADDREXTBLK_SIZE) {
787                 printf("Write of mh info block failed %d; error %u\n", w,
788                        errno);
789                 exit(1);
790             }
791         }
792         return 0;
793     }
794
795     if (fromvers == 2 && tovers == 3) {
796         struct vlentry_3 vl;
797
798         vl.volumeId[0] = vlentryp->volumeId[0];
799         vl.volumeId[1] = vlentryp->volumeId[1];
800         vl.volumeId[2] = vlentryp->volumeId[2];
801         vl.flags = vlentryp->flags;
802         vl.LockAfsId = vlentryp->LockAfsId;
803         vl.LockTimestamp = vlentryp->LockTimestamp;
804         vl.cloneId = vlentryp->cloneId;
805         vl.nextIdHash[0] = vlentryp->nextIdHash[0];
806         vl.nextIdHash[1] = vlentryp->nextIdHash[1];
807         vl.nextIdHash[2] = vlentryp->nextIdHash[2];
808         vl.nextNameHash = vlentryp->nextNameHash;
809         memcpy(vl.name, vlentryp->name, 65);
810         for (i = 0; i < 8; i++) {
811             vl.serverNumber[i] = vlentryp->serverNumber[i];
812             vl.serverPartition[i] = vlentryp->serverPartition[i];
813             vl.serverFlags[i] = vlentryp->serverFlags[i];
814         }
815         for (; i < 13; i++)
816             vl.serverNumber[i] = vl.serverPartition[i] = vl.serverFlags[i] =
817                 BADSERVERID;
818         w = write(new, &vl, sizeof(struct vlentry_3));
819         if (w != sizeof(struct vlentry_3)) {
820             printf("Write of entry failed %d; error %u\n", w, errno);
821             exit(1);
822         }
823
824         return 0;
825     } else if (fromvers == 3 && tovers == 2) {
826         struct vlentry_2 vl;
827         struct vlentry_3 *xnvlentry = (struct vlentry_3 *)vlentryp;
828
829         memset((char *)&vl, 0, sizeof(struct vlentry_2));
830         vl.volumeId[0] = xnvlentry->volumeId[0];
831         vl.volumeId[1] = xnvlentry->volumeId[1];
832         vl.volumeId[2] = xnvlentry->volumeId[2];
833         vl.flags = xnvlentry->flags;
834         vl.LockAfsId = xnvlentry->LockAfsId;
835         vl.LockTimestamp = xnvlentry->LockTimestamp;
836         vl.cloneId = xnvlentry->cloneId;
837         for (i = 0; i < 3; i++) {
838             if (ntohl(xnvlentry->nextIdHash[i]))
839                 vl.nextIdHash[i] = xnvlentry->nextIdHash[i];
840         }
841         if (ntohl(xnvlentry->nextNameHash))
842             vl.nextNameHash = xnvlentry->nextNameHash;
843         memcpy(vl.name, xnvlentry->name, 65);
844         for (i = 0; i < 8; i++) {
845             vl.serverNumber[i] = xnvlentry->serverNumber[i];
846             vl.serverPartition[i] = xnvlentry->serverPartition[i];
847             vl.serverFlags[i] = xnvlentry->serverFlags[i];
848         }
849         w = write(new, &vl, sizeof(struct vlentry_2));
850         if (w != sizeof(struct vlentry_2)) {
851             printf("Write of entry failed %d; error %u\n", w, errno);
852             exit(1);
853         }
854         return 0;
855     } else if (fromvers == 3 && tovers == 1) {
856         struct vlentry_1 vl;
857         struct vlentry_3 *xnvlentry = (struct vlentry_3 *)vlentryp;
858
859         diff =
860             (tovers ==
861              1 ? sizeof(struct vlheader_1) : sizeof(struct vlheader_2))
862             - (fromvers ==
863                1 ? sizeof(struct vlheader_1) : sizeof(struct vlheader_2));
864         memset((char *)&vl, 0, sizeof(struct vlentry_1));
865         vl.volumeId[0] = xnvlentry->volumeId[0];
866         vl.volumeId[1] = xnvlentry->volumeId[1];
867         vl.volumeId[2] = xnvlentry->volumeId[2];
868         vl.flags = xnvlentry->flags;
869         vl.LockAfsId = xnvlentry->LockAfsId;
870         vl.LockTimestamp = xnvlentry->LockTimestamp;
871         vl.cloneId = xnvlentry->cloneId;
872         for (i = 0; i < 3; i++) {
873             if (ntohl(xnvlentry->nextIdHash[i]))
874                 vl.nextIdHash[i] =
875                     htonl(ntohl(xnvlentry->nextIdHash[i]) + diff);
876         }
877         if (ntohl(xnvlentry->nextNameHash))
878             vl.nextNameHash = htonl(ntohl(xnvlentry->nextNameHash) + diff);
879
880         memcpy(vl.name, xnvlentry->name, 65);
881         for (i = 0; i < 8; i++) {
882             vl.serverNumber[i] = xnvlentry->serverNumber[i];
883             vl.serverPartition[i] = xnvlentry->serverPartition[i];
884             vl.serverFlags[i] = xnvlentry->serverFlags[i];
885         }
886         for (i = 0; i < 8; i++) {
887             s = xnvlentry->serverNumber[i];
888             if (s != 255) {
889                 if (s > MaxServers[tovers - 1]) {
890                     fprintf(stderr,
891                             "%s: Too Many Servers (%d) for this version!\n",
892                             pn, s + 1);
893                     exit(-1);
894                 } else
895                     newheader->IpMappedAddr[s] = oldheader->IpMappedAddr[s];
896             }
897         }
898         w = write(new, &vl, sizeof(struct vlentry_1));
899         if (w != sizeof(struct vlentry_1)) {
900             printf("Write of entry failed %d; error %u\n", w, errno);
901             exit(1);
902         }
903         return 0;
904     } else if (fromvers == 4 && tovers == 3) {
905         struct vlentry_3 vl;
906         /* We are converting from version 4 to 3. In this conversion, mh info
907          * blocks go away and all vlentries after them move up in the vldb file.
908          * When this happens, the linked list pointers need to be updated.
909          */
910         memcpy(&vl, vlentryp, sizeof(vl));
911         for (i = 0; i < 3; i++) {
912             vl.nextIdHash[i] = Conv4to3(vl.nextIdHash[i]);
913         }
914         vl.nextNameHash = Conv4to3(vl.nextNameHash);
915
916         w = write(new, &vl, sizeof(vl));
917         if (w != sizeof(vl)) {
918             printf("Write of entry failed %d; error %u\n", w, errno);
919             exit(1);
920         }
921         return 0;
922     }
923
924     if (tovers == 1) {
925         w = write(new, vlentryp, sizeof(struct vlentry_1));
926         if (w != sizeof(struct vlentry_1)) {
927             printf("Write of entry failed %d; error %u\n", w, errno);
928             exit(1);
929         }
930     } else if (tovers == 2) {
931         w = write(new, vlentryp, sizeof(struct vlentry_2));
932         if (w != sizeof(struct vlentry_2)) {
933             printf("Write of entry failed %d; error %u\n", w, errno);
934             exit(1);
935         }
936     } else if (tovers == 3 || tovers == 4) {
937         w = write(new, vlentryp, sizeof(struct vlentry_3));
938         if (w != sizeof(struct vlentry_3)) {
939             printf("Write of entry failed %d; error %u\n", w, errno);
940             exit(1);
941         }
942     } else {
943         perror(pn);
944         fprintf(stderr,
945                 "Skipping vlentry write - db corrupted - bad toversion %d\n",
946                 tovers);
947     }
948
949     return;
950 }
951
952 static int
953 rewrite_header(new, tovers, newheader)
954      int new, tovers;
955      char *newheader;
956 {
957     int pos, w, towrite;
958
959     pos = lseek(new, 64, L_SET);        /* leave room for ubik */
960     if (pos == -1) {
961         perror(pn);
962         fprintf(stderr, "%s: no garbage colection\n", pn);
963         return;
964     } else if (pos != 64) {
965         fprintf(stderr, "%s: Can't rewind: no garbage collection\n", pn);
966         return;
967     }
968
969     towrite =
970         ((tovers ==
971           1) ? sizeof(struct vlheader_1) : sizeof(struct vlheader_2));
972     w = write(new, newheader, towrite);
973     if (w != towrite) {
974         printf("Write of entry failed %d; error %u\n", w, errno);
975         exit(1);
976     }
977
978     return;
979 }
980
981
982 #include "AFS_component_version_number.c"
983
984 main(argc, argv)
985      int argc;
986      char **argv;
987 {
988     register struct cmd_syndesc *ts;
989     afs_int32 code;
990
991     ts = cmd_CreateSyntax("initcmd", handleit, NULL, "optional");
992     cmd_AddParm(ts, "-to", CMD_SINGLE, CMD_OPTIONAL, "goal version");
993     cmd_AddParm(ts, "-from", CMD_SINGLE, CMD_OPTIONAL, "current version");
994     cmd_AddParm(ts, "-path", CMD_SINGLE, CMD_OPTIONAL, "pathname");
995     cmd_AddParm(ts, "-showversion", CMD_FLAG, CMD_OPTIONAL,
996                 "Just display version of current vldb");
997     cmd_AddParm(ts, "-dumpvldb", CMD_FLAG, CMD_OPTIONAL,
998                 "display all vldb entries");
999
1000 #ifdef DEBUG
1001     cmd_AddParm(ts, "-noGC", CMD_FLAG, CMD_OPTIONAL,
1002                 "Don't do garbage collection");
1003 #endif /* DEBUG */
1004
1005     dbPath = AFSDIR_SERVER_VLDB_FILEPATH;
1006
1007     code = cmd_Dispatch(argc, argv);
1008     exit(code);
1009 }