volser: Remove unused VolRestore flags args
[openafs.git] / src / volser / dumpstuff.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 #include <ctype.h>
16
17 #include <afs/opr.h>
18 #include <rx/rx.h>
19 #include <rx/rx_queue.h>
20 #include <afs/afsint.h>
21 #include <afs/nfs.h>
22 #include <afs/errors.h>
23 #include <lock.h>
24 #include <lwp.h>
25 #include <afs/ihandle.h>
26 #include <afs/vnode.h>
27 #include <afs/volume.h>
28 #include <afs/partition.h>
29 #include <afs/daemon_com.h>
30 #include <afs/fssync.h>
31 #include <afs/acl.h>
32 #include <afs/com_err.h>
33 #include <afs/vol_prototypes.h>
34
35 #include "dump.h"
36 #include "volser.h"
37 #include "volint.h"
38 #include "dumpstuff.h"
39
40 #ifndef AFS_NT40_ENV
41 #ifdef O_LARGEFILE
42 #define afs_stat        stat64
43 #define afs_fstat       fstat64
44 #else /* !O_LARGEFILE */
45 #define afs_stat        stat
46 #define afs_fstat       fstat
47 #endif /* !O_LARGEFILE */
48 #endif /* !AFS_NT40_ENV */
49
50 /*@printflike@*/ extern void Log(const char *format, ...);
51
52 extern int DoLogging;
53 extern int DoPreserveVolumeStats;
54
55
56 /* Forward Declarations */
57 static int DumpDumpHeader(struct iod *iodp, Volume * vp,
58                           afs_int32 fromtime);
59 static int DumpPartial(struct iod *iodp, Volume * vp,
60                        afs_int32 fromtime, int dumpAllDirs);
61 static int DumpVnodeIndex(struct iod *iodp, Volume * vp,
62                           VnodeClass class, afs_int32 fromtime,
63                           int forcedump);
64 static int DumpVnode(struct iod *iodp, struct VnodeDiskObject *v,
65                      VolumeId volid, int vnodeNumber, int dumpEverything);
66 static int ReadDumpHeader(struct iod *iodp, struct DumpHeader *hp);
67 static int ReadVnodes(struct iod *iodp, Volume * vp, afs_foff_t * Lbuf,
68                       afs_int32 s1, afs_foff_t * Sbuf, afs_int32 s2,
69                       afs_int32 delo);
70 static afs_fsize_t volser_WriteFile(int vn, struct iod *iodp,
71                                     FdHandle_t * handleP, int tag,
72                                     Error * status);
73
74 static int SizeDumpDumpHeader(struct iod *iodp, Volume * vp,
75                               afs_int32 fromtime,
76                               struct volintSize *size);
77 static int SizeDumpPartial(struct iod *iodp, Volume * vp,
78                            afs_int32 fromtime, int dumpAllDirs,
79                            struct volintSize *size);
80 static int SizeDumpVnodeIndex(struct iod *iodp, Volume * vp,
81                               VnodeClass class, afs_int32 fromtime,
82                               int forcedump,
83                               struct volintSize *size);
84 static int SizeDumpVnode(struct iod *iodp, struct VnodeDiskObject *v,
85                          VolumeId volid, int vnodeNumber, int dumpEverything,
86                          struct volintSize *size);
87
88 #define MAX_SECTIONS    3
89
90 /* The TLV range must start above D_MAX */
91 #define MIN_TLV_TAG     21
92 #define MAX_TLV_TAG     0x60
93 #define MAX_STANDARD_TAG 0x7a
94 static afs_uint32 oldtags[MAX_SECTIONS][16];
95 int oldtagsInited = 0;
96
97 static void
98 RegisterTag(afs_int32 section, unsigned char tag)
99 {
100     afs_uint32 off = tag >> 5;
101     afs_uint32 mask = 1 << (tag & 0x1f);
102     oldtags[section][off] |= mask;
103 }
104
105 static void
106 initNonStandardTags(void)
107 {
108     RegisterTag(0, 'n');                /* volume name */
109     RegisterTag(0, 't');                /* fromtime, V_backupDate */
110     RegisterTag(1, 'A');                /* V_accessDate */
111     RegisterTag(1, 'C');                /* V_creationDate */
112     RegisterTag(1, 'D');                /* V_dayUseDate */
113     RegisterTag(1, 'E');                /* V_expirationDate */
114     RegisterTag(1, 'M');                /* nullstring (motd) */
115     RegisterTag(1, 'U');                /* V_updateDate */
116     RegisterTag(1, 'W');                /* V_weekUse */
117     RegisterTag(1, 'Z');                /* V_dayUse */
118     RegisterTag(1, 'O');                /* V_offlineMessage */
119     RegisterTag(1, 'b');                /* V_blessed */
120     RegisterTag(1, 'n');                /* V_name */
121     RegisterTag(1, 's');                /* V_inService */
122     RegisterTag(1, 't');                /* V_type */
123     RegisterTag(2, 'A');                /* VVnodeDiskACL */
124     RegisterTag(2, 'b');                /* modeBits */
125     RegisterTag(2, 'f');                /* small file */
126     RegisterTag(2, 'h');                /* large file */
127     RegisterTag(2, 'l');                /* linkcount */
128     RegisterTag(2, 't');                /* type */
129     oldtagsInited = 1;
130 }
131
132 static void
133 iod_Init(struct iod *iodp, struct rx_call *call)
134 {
135     iodp->call = call;
136     iodp->haveOldChar = 0;
137     iodp->ncalls = 1;
138     iodp->calls = (struct rx_call **)0;
139 }
140
141 static void
142 iod_InitMulti(struct iod *iodp, struct rx_call **calls, int ncalls,
143               int *codes)
144 {
145
146     iodp->calls = calls;
147     iodp->haveOldChar = 0;
148     iodp->ncalls = ncalls;
149     iodp->codes = codes;
150     iodp->call = (struct rx_call *)0;
151 }
152
153 /* N.B. iod_Read doesn't check for oldchar (see previous comment) */
154 #define iod_Read(iodp, buf, nbytes) rx_Read((iodp)->call, buf, nbytes)
155
156 /* For the single dump case, it's ok to just return the "bytes written"
157  * that rx_Write returns, since all the callers of iod_Write abort when
158  * the returned value is less than they expect.  For the multi dump case,
159  * I don't think we want half the replicas to go bad just because one
160  * connection timed out, but if they all time out, then we should give up.
161  */
162 static int
163 iod_Write(struct iod *iodp, char *buf, int nbytes)
164 {
165     int code, i;
166     int one_success = 0;
167
168     opr_Assert((iodp->call && iodp->ncalls == 1 && !iodp->calls)
169            || (!iodp->call && iodp->ncalls >= 1 && iodp->calls));
170
171     if (iodp->call) {
172         code = rx_Write(iodp->call, buf, nbytes);
173         return code;
174     }
175
176     for (i = 0; i < iodp->ncalls; i++) {
177         if (iodp->calls[i] && !iodp->codes[i]) {
178             code = rx_Write(iodp->calls[i], buf, nbytes);
179             if (code != nbytes) {       /* everything gets merged into a single error */
180                 iodp->codes[i] = VOLSERDUMPERROR;       /* but that's exactly what the */
181             } /* standard dump does, anyways */
182             else {
183                 one_success = TRUE;
184             }
185         }
186     }                           /* for all calls */
187
188     if (one_success)
189         return nbytes;
190     else
191         return 0;
192 }
193
194 static void
195 iod_ungetc(struct iod *iodp, int achar)
196 {
197     iodp->oldChar = achar;
198     iodp->haveOldChar = 1;
199 }
200
201 static int
202 iod_getc(struct iod *iodp)
203 {
204     unsigned char t;
205
206     if (iodp->haveOldChar) {
207         iodp->haveOldChar = 0;
208         return iodp->oldChar;
209     }
210     if (iod_Read(iodp, (char *) &t, 1) == 1)
211         return t;
212     return EOF;
213 }
214
215 static int
216 ReadShort(struct iod *iodp, unsigned short *sp)
217 {
218     int b1, b0;
219     b1 = iod_getc(iodp);
220     if (b1 == EOF)
221         return 0;
222     b0 = iod_getc(iodp);
223     if (b0 == EOF)
224         return 0;
225     *sp = (b1 << 8) | b0;
226     return 1;
227 }
228
229 static int
230 ReadInt32(struct iod *iodp, afs_uint32 * lp)
231 {
232     afs_uint32 b3, b2, b1, b0;
233     b3 = iod_getc(iodp);
234     if (b3 == EOF)
235         return 0;
236     b2 = iod_getc(iodp);
237     if (b2 == EOF)
238         return 0;
239     b1 = iod_getc(iodp);
240     if (b1 == EOF)
241         return 0;
242     b0 = iod_getc(iodp);
243     if (b0 == EOF)
244         return 0;
245     *lp = (((((b3 << 8) | b2) << 8) | b1) << 8) | b0;
246     return 1;
247 }
248
249 static void
250 ReadString(struct iod *iodp, char *to, int maxa)
251 {
252     int c;
253
254     *to = '\0';
255     if (maxa == 0)
256         return;
257
258     while (maxa--) {
259         if ((*to++ = c = iod_getc(iodp)) == 0 || c == EOF)
260             break;
261     }
262     if (to[-1]) {
263         while ((c = iod_getc(iodp)) && c != EOF);
264         to[-1] = '\0';
265     }
266 }
267
268 static int
269 ReadByteString(struct iod *iodp, byte * to, int size)
270 {
271     int nbytes = 0;
272     int c;
273
274     while (size-- > 0 && (c = iod_getc(iodp)) != EOF) {
275         *to++ = c;
276         nbytes++;
277     }
278     return nbytes;
279 }
280
281 /*
282  * returns 1 on success and 0 otherwise
283  */
284 static afs_int32
285 ReadStandardTagLen(struct iod *iodp, unsigned char tag, afs_int32 section,
286                         afs_size_t *length)
287 {
288     afs_int32 code, i;
289     afs_uint32 off = tag >> 5;
290     afs_uint32 mask = 1 << (tag & 0x1f);
291     int len;
292     unsigned char buf[8], *p;
293
294     if (!oldtagsInited)
295         initNonStandardTags();
296
297     if (tag < MIN_TLV_TAG
298       || tag > MAX_STANDARD_TAG
299       || section >= MAX_SECTIONS
300       || (oldtags[section][ off] & mask)) {
301         Log("Trying to use ReadStandardTag with tag 0x%02x for section %d, aborting\n", tag, section);
302         return 0;
303     }
304     if (tag <= MAX_TLV_TAG) {
305         len = iod_getc(iodp);
306         if (len == EOF)
307             return VOLSERDUMPERROR;
308         else if (len < 128)
309             *length = len;
310         else {
311             len &= 0x7f;
312             if ((code = iod_Read(iodp, (char *)buf, len)) != len)
313                 return VOLSERDUMPERROR;
314             *length = 0;
315             p = (unsigned char *)&buf;
316             for (i=0; i<len; i++) {
317                 *length = ((*length) << 8) | *p++;
318             }
319         }
320     } else {
321         if (tag < MAX_STANDARD_TAG)
322             *length = 4;
323     }
324     return 1;
325 }
326
327 static char skipbuf[256];
328
329 static afs_int32
330 SkipData(struct iod *iodp, afs_size_t length)
331 {
332     while (length > 256) {
333         if (iod_Read(iodp, (char *)&skipbuf, 256) != 256)
334             return 0;
335         length -= 256;
336     }
337     if (iod_Read(iodp, (char *)&skipbuf, length) != length)
338         return 0;
339     return 1;
340 }
341
342 static char *secname[3] = {"ReadDumpHeader", "ReadVolumeHeader", "ReadVnodes"};
343
344 static int
345 HandleUnknownTag(struct iod *iodp, int tag, afs_int32 section,
346                 afs_int32 critical)
347 {
348     afs_size_t taglen = 0;
349     afs_uint32 trash;
350
351     if (critical) {
352         Log("%s: unknown critical tag x%02x, aborting\n",
353                 secname[section], tag);
354         return 0;
355     }
356     Log("%s: unknown tag x%02x found, skipping\n", secname[section], tag);
357     if (tag >= 0x06 && tag <= 0x60) {
358         if (!ReadStandardTagLen(iodp, tag, 1, &taglen)) {
359             Log("%s: error reading length field for tag x%02x, aborting\n",
360                 secname[section], tag);
361             return 0;
362         }
363         if (!SkipData(iodp, taglen)) {
364             Log("%s: error skipping %llu bytes for tag x%02x, aborting\n",
365                 secname[section], taglen, tag);
366             return 0;
367         }
368         return 1;
369     }
370     if (tag >= 0x61 && tag <= 0x7a) {
371         if (!ReadInt32(iodp, &trash)) {
372             Log("%s: error skipping int32 for tag x%02x, aborting\n",
373                 secname[section], tag);
374             return 0;
375         }
376         return 1;
377     }
378     if (tag >= 0x7b && tag < 0x80)      /* dataless tag */
379         return 1;
380     Log("%s: unknown invalid tag x%02x, aborting\n", secname[section], tag);
381     return 0;
382 }
383
384 static int
385 ReadVolumeHeader(struct iod *iodp, VolumeDiskData * vol)
386 {
387     int tag;
388     afs_uint32 trash;
389     afs_int32 critical = 0;
390     memset(vol, 0, sizeof(*vol));
391     while ((tag = iod_getc(iodp)) > D_MAX && tag != EOF) {
392         if (critical)
393             critical--;
394         switch (tag) {
395         case 'i':
396             if (!ReadInt32(iodp, &vol->id))
397                 return VOLSERREAD_DUMPERROR;
398             break;
399         case 'v':
400             if (!ReadInt32(iodp, &trash))
401                 return VOLSERREAD_DUMPERROR;
402             break;
403         case 'n':
404             ReadString(iodp, vol->name, sizeof(vol->name));
405             /*this means the name of the retsored volume could be possibly different. In conjunction with SAFSVolSignalRestore */
406             break;
407         case 's':
408             vol->inService = iod_getc(iodp);
409             break;
410         case 'b':
411             vol->blessed = iod_getc(iodp);
412             break;
413         case 'u':
414             if (!ReadInt32(iodp, &vol->uniquifier))
415                 return VOLSERREAD_DUMPERROR;
416             break;
417         case 't':
418             vol->type = iod_getc(iodp);
419             break;
420         case 'p':
421             if (!ReadInt32(iodp, &vol->parentId))
422                 return VOLSERREAD_DUMPERROR;
423             break;
424         case 'c':
425             if (!ReadInt32(iodp, &vol->cloneId))
426                 return VOLSERREAD_DUMPERROR;
427             break;
428         case 'q':
429             if (!ReadInt32(iodp, (afs_uint32 *) & vol->maxquota))
430                 return VOLSERREAD_DUMPERROR;
431             break;
432         case 'm':
433             if (!ReadInt32(iodp, (afs_uint32 *) & vol->minquota))
434                 return VOLSERREAD_DUMPERROR;
435             break;
436         case 'd':
437             if (!ReadInt32(iodp, (afs_uint32 *) & vol->diskused))
438                 return VOLSERREAD_DUMPERROR;    /* Bogus:  should calculate this */
439             break;
440         case 'f':
441             if (!ReadInt32(iodp, (afs_uint32 *) & vol->filecount))
442                 return VOLSERREAD_DUMPERROR;
443             break;
444         case 'a':
445             if (!ReadInt32(iodp, &vol->accountNumber))
446                 return VOLSERREAD_DUMPERROR;
447             break;
448         case 'o':
449             if (!ReadInt32(iodp, &vol->owner))
450                 return VOLSERREAD_DUMPERROR;
451             break;
452         case 'C':
453             if (!ReadInt32(iodp, &vol->creationDate))
454                 return VOLSERREAD_DUMPERROR;
455             break;
456         case 'A':
457             if (!ReadInt32(iodp, &vol->accessDate))
458                 return VOLSERREAD_DUMPERROR;
459             break;
460         case 'U':
461             if (!ReadInt32(iodp, &vol->updateDate))
462                 return VOLSERREAD_DUMPERROR;
463             break;
464         case 'E':
465             if (!ReadInt32(iodp, &vol->expirationDate))
466                 return VOLSERREAD_DUMPERROR;
467             break;
468         case 'B':
469             if (!ReadInt32(iodp, &vol->backupDate))
470                 return VOLSERREAD_DUMPERROR;
471             break;
472         case 'O':
473             ReadString(iodp, vol->offlineMessage,
474                        sizeof(vol->offlineMessage));
475             break;
476         case 'M':
477             /*
478              * Detailed volume statistics are never stored in dumps,
479              * so we just restore either the null string if this volume
480              * had already been set to store statistics, or the old motd
481              * contents otherwise.  It doesn't matter, since this field
482              * will soon get initialized anyway.
483              */
484             ReadString(iodp, (char *)(vol->stat_reads), VMSGSIZE);
485             break;
486         case 'W':{
487                 unsigned short length;
488                 int i;
489                 afs_uint32 data;
490                 if (!ReadShort(iodp, &length))
491                     return VOLSERREAD_DUMPERROR;
492                 for (i = 0; i < length; i++) {
493                     if (!ReadInt32(iodp, &data))
494                         return VOLSERREAD_DUMPERROR;
495                     if (i < sizeof(vol->weekUse) / sizeof(vol->weekUse[0]))
496                         vol->weekUse[i] = data;
497                 }
498                 break;
499             }
500         case 'D':
501             if (!ReadInt32(iodp, &vol->dayUseDate))
502                 return VOLSERREAD_DUMPERROR;
503             break;
504         case 'Z':
505             if (!ReadInt32(iodp, (afs_uint32 *) & vol->dayUse))
506                 return VOLSERREAD_DUMPERROR;
507             break;
508         case 'V':
509             if (!ReadInt32(iodp, (afs_uint32 *) &trash/*volUpdateCounter*/))
510                 return VOLSERREAD_DUMPERROR;
511             break;
512         case 0x7e:
513             critical = 2;
514             break;
515         default:
516             if (!HandleUnknownTag(iodp, tag, 1, critical))
517                 return VOLSERREAD_DUMPERROR;
518         }
519     }
520     iod_ungetc(iodp, tag);
521     return 0;
522 }
523
524 static int
525 DumpTag(struct iod *iodp, int tag)
526 {
527     char p;
528
529     p = tag;
530     return ((iod_Write(iodp, &p, 1) == 1) ? 0 : VOLSERDUMPERROR);
531
532 }
533
534 static int
535 DumpByte(struct iod *iodp, char tag, byte value)
536 {
537     char tbuffer[2];
538     byte *p = (unsigned char *)tbuffer;
539     *p++ = tag;
540     *p = value;
541     return ((iod_Write(iodp, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
542 }
543
544 #define afs_putint32(p, v)  *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
545 #define afs_putshort(p, v) *p++ = v>>8, *p++ = v
546
547 static int
548 DumpDouble(struct iod *iodp, char tag, afs_uint32 value1,
549            afs_uint32 value2)
550 {
551     char tbuffer[9];
552     byte *p = (unsigned char *)tbuffer;
553     *p++ = tag;
554     afs_putint32(p, value1);
555     afs_putint32(p, value2);
556     return ((iod_Write(iodp, tbuffer, 9) == 9) ? 0 : VOLSERDUMPERROR);
557 }
558
559 static int
560 DumpInt32(struct iod *iodp, char tag, afs_uint32 value)
561 {
562     char tbuffer[5];
563     byte *p = (unsigned char *)tbuffer;
564     *p++ = tag;
565     afs_putint32(p, value);
566     return ((iod_Write(iodp, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
567 }
568
569 static int
570 DumpArrayInt32(struct iod *iodp, char tag,
571                afs_uint32 * array, int nelem)
572 {
573     char tbuffer[4];
574     afs_uint32 v;
575     int code = 0;
576     byte *p = (unsigned char *)tbuffer;
577     *p++ = tag;
578     afs_putshort(p, nelem);
579     code = iod_Write(iodp, tbuffer, 3);
580     if (code != 3)
581         return VOLSERDUMPERROR;
582     while (nelem--) {
583         p = (unsigned char *)tbuffer;
584         v = *array++;           /*this was register */
585
586         afs_putint32(p, v);
587         code = iod_Write(iodp, tbuffer, 4);
588         if (code != 4)
589             return VOLSERDUMPERROR;
590     }
591     return 0;
592 }
593
594 static int
595 DumpShort(struct iod *iodp, char tag, unsigned int value)
596 {
597     char tbuffer[3];
598     byte *p = (unsigned char *)tbuffer;
599     *p++ = tag;
600     *p++ = value >> 8;
601     *p = value;
602     return ((iod_Write(iodp, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR);
603 }
604
605 static int
606 DumpBool(struct iod *iodp, char tag, unsigned int value)
607 {
608     char tbuffer[2];
609     byte *p = (unsigned char *)tbuffer;
610     *p++ = tag;
611     *p = value;
612     return ((iod_Write(iodp, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
613 }
614
615 static int
616 DumpString(struct iod *iodp, char tag, char *s)
617 {
618     int n;
619     int code = 0;
620     code = iod_Write(iodp, &tag, 1);
621     if (code != 1)
622         return VOLSERDUMPERROR;
623     n = strlen(s) + 1;
624     code = iod_Write(iodp, s, n);
625     if (code != n)
626         return VOLSERDUMPERROR;
627     return 0;
628 }
629
630 static int
631 DumpByteString(struct iod *iodp, char tag, byte * bs,
632                int nbytes)
633 {
634     int code = 0;
635
636     code = iod_Write(iodp, &tag, 1);
637     if (code != 1)
638         return VOLSERDUMPERROR;
639     code = iod_Write(iodp, (char *)bs, nbytes);
640     if (code != nbytes)
641         return VOLSERDUMPERROR;
642     return 0;
643 }
644
645 static afs_int32
646 DumpStandardTag(struct iod *iodp, char tag, afs_uint32 section)
647 {
648     afs_int32 code;
649     afs_uint32 off = tag >> 5;
650     afs_uint32 mask = 1 << (tag & 0x1f);
651
652     if (!oldtagsInited)
653         initNonStandardTags();
654
655     if (tag < MIN_TLV_TAG
656       || tag > MAX_STANDARD_TAG
657       || section >= MAX_SECTIONS
658       || (oldtags[section][ off] & mask)) {
659         Log("Trying to use DumpStandardTag with tag 0x%02x for section %d, aborting\n", tag, section);
660         return VOLSERDUMPERROR;
661     }
662     code = iod_Write(iodp, &tag, 1);
663     if (code != 1)
664         return VOLSERDUMPERROR;
665     return 0;
666 }
667
668 AFS_UNUSED
669 static afs_int32
670 DumpStandardTagLen(struct iod *iodp, char tag, afs_uint32 section,
671                         afs_size_t length)
672 {
673     char buf[10];
674     char *p;
675     afs_int32 code, len;
676
677     if (tag < MIN_TLV_TAG || tag > MAX_TLV_TAG) {
678         Log("Trying to use DumpStandardTagLen with tag 0x%02x for section %d, aborting\n", tag, section);
679         return VOLSERDUMPERROR;
680     }
681     code = DumpStandardTag(iodp, tag, section);
682     if (code)
683         return code;
684     p = &buf[9];
685     if (length < 128) { /* byte after tag contains length */
686         *p-- = length;
687         len = 1;
688     } else {            /* byte after tag contains length of length field | 0x80 */
689         for (len=0; length; length=length >> 8) {
690             *p-- = length;
691             len++;
692         }
693         *p-- = len + 128;
694         len += 1;
695     }
696     p++;
697     code = iod_Write(iodp, p, len);
698     if (code != len)
699         return VOLSERDUMPERROR;
700     return 0;
701 }
702
703 static int
704 DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
705 {
706     int code = 0, error = 0;
707     afs_int32 pad = 0;
708     afs_foff_t offset = 0;
709     afs_sfsize_t nbytes, howBig;
710     ssize_t n;
711     size_t howMany;
712     afs_foff_t howFar = 0;
713     byte *p;
714     afs_uint32 hi, lo;
715     afs_ino_str_t stmp;
716 #ifndef AFS_NT40_ENV
717     struct afs_stat status;
718 #else
719     LARGE_INTEGER fileSize;
720 #endif
721 #ifdef  AFS_AIX_ENV
722 #include <sys/statfs.h>
723 #if defined(AFS_AIX52_ENV)
724     struct statfs64 tstatfs;
725 #else /* !AFS_AIX52_ENV */
726     struct statfs tstatfs;
727 #endif /* !AFS_AIX52_ENV */
728     int statfs_code;
729 #endif
730
731 #ifdef AFS_NT40_ENV
732     if (!GetFileSizeEx(handleP->fd_fd, &fileSize)) {
733         Log("DumpFile: GetFileSizeEx returned error code %d on descriptor %d\n", GetLastError(), handleP->fd_fd);
734             return VOLSERDUMPERROR;
735     }
736     howBig = fileSize.QuadPart;
737     howMany = 4096;
738
739 #else
740     afs_fstat(handleP->fd_fd, &status);
741     howBig = status.st_size;
742
743 #ifdef  AFS_AIX_ENV
744     /* Unfortunately in AIX valuable fields such as st_blksize are
745      * gone from the stat structure.
746      */
747 #if defined(AFS_AIX52_ENV)
748     statfs_code = fstatfs64(handleP->fd_fd, &tstatfs);
749 #else /* !AFS_AIX52_ENV */
750     statfs_code = fstatfs(handleP->fd_fd, &tstatfs);
751 #endif /* !AFS_AIX52_ENV */
752     if (statfs_code != 0) {
753         Log("DumpFile: fstatfs returned error code %d on descriptor %d\n", errno, handleP->fd_fd);
754         return VOLSERDUMPERROR;
755     }
756     howMany = tstatfs.f_bsize;
757 #else
758     howMany = status.st_blksize;
759 #endif /* AFS_AIX_ENV */
760 #endif /* AFS_NT40_ENV */
761
762
763     SplitInt64(howBig, hi, lo);
764     if (hi == 0L) {
765         code = DumpInt32(iodp, 'f', lo);
766     } else {
767         code = DumpDouble(iodp, 'h', hi, lo);
768     }
769     if (code) {
770         return VOLSERDUMPERROR;
771     }
772
773     p = malloc(howMany);
774     if (!p) {
775         Log("1 Volser: DumpFile: not enough memory to allocate %u bytes\n", (unsigned)howMany);
776         return VOLSERDUMPERROR;
777     }
778
779     for (nbytes = howBig; (nbytes && !error); nbytes -= howMany) {
780         if (nbytes < howMany)
781             howMany = nbytes;
782
783         /* Read the data */
784         n = FDH_PREAD(handleP, p, howMany, howFar);
785         howFar += n;
786
787         /* If read any good data and we null padded previously, log the
788          * amount that we had null padded.
789          */
790         if ((n > 0) && pad) {
791             Log("1 Volser: DumpFile: Null padding file %d bytes at offset %lld\n", pad, (long long)offset);
792             pad = 0;
793         }
794
795         /* If didn't read enough data, null padd the rest of the buffer. This
796          * can happen if, for instance, the media has some bad spots. We don't
797          * want to quit the dump, so we start null padding.
798          */
799         if (n < howMany) {
800             /* Record the read error */
801             if (n < 0) {
802                 n = 0;
803                 Log("1 Volser: DumpFile: Error reading inode %s for vnode %d: %s\n", PrintInode(stmp, handleP->fd_ih->ih_ino), vnode, afs_error_message(errno));
804             } else if (!pad) {
805                 Log("1 Volser: DumpFile: Error reading inode %s for vnode %d\n", PrintInode(stmp, handleP->fd_ih->ih_ino), vnode);
806             }
807
808             /* Pad the rest of the buffer with zeros. Remember offset we started
809              * padding. Keep total tally of padding.
810              */
811             memset(p + n, 0, howMany - n);
812             if (!pad)
813                 offset = (howBig - nbytes) + n;
814             pad += (howMany - n);
815
816             /* Now seek over the data we could not get. An error here means we
817              * can't do the next read.
818              */
819             howFar = (size_t)((howBig - nbytes) + howMany);
820         }
821
822         /* Now write the data out */
823         if (iod_Write(iodp, (char *)p, howMany) != howMany)
824             error = VOLSERDUMPERROR;
825 #ifndef AFS_PTHREAD_ENV
826         IOMGR_Poll();
827 #endif
828     }
829
830     if (pad) {                  /* Any padding we hadn't reported yet */
831         Log("1 Volser: DumpFile: Null padding file: %d bytes at offset %lld\n",
832             pad, (long long)offset);
833     }
834
835     free(p);
836     return error;
837 }
838
839 static int
840 DumpVolumeHeader(struct iod *iodp, Volume * vp)
841 {
842     int code = 0;
843     static char nullString[1] = "";     /*The ``contents'' of motd */
844
845     if (!code)
846         code = DumpTag(iodp, D_VOLUMEHEADER);
847     if (!code) {
848         code = DumpInt32(iodp, 'i', V_id(vp));
849     }
850     if (!code)
851         code = DumpInt32(iodp, 'v', V_stamp(vp).version);
852     if (!code)
853         code = DumpString(iodp, 'n', V_name(vp));
854     if (!code)
855         code = DumpBool(iodp, 's', V_inService(vp));
856     if (!code)
857         code = DumpBool(iodp, 'b', V_blessed(vp));
858     if (!code)
859         code = DumpInt32(iodp, 'u', V_uniquifier(vp));
860     if (!code)
861         code = DumpByte(iodp, 't', (byte) V_type(vp));
862     if (!code) {
863         code = DumpInt32(iodp, 'p', V_parentId(vp));
864     }
865     if (!code)
866         code = DumpInt32(iodp, 'c', V_cloneId(vp));
867     if (!code)
868         code = DumpInt32(iodp, 'q', V_maxquota(vp));
869     if (!code)
870         code = DumpInt32(iodp, 'm', V_minquota(vp));
871     if (!code)
872         code = DumpInt32(iodp, 'd', V_diskused(vp));
873     if (!code)
874         code = DumpInt32(iodp, 'f', V_filecount(vp));
875     if (!code)
876         code = DumpInt32(iodp, 'a', V_accountNumber(vp));
877     if (!code)
878         code = DumpInt32(iodp, 'o', V_owner(vp));
879     if (!code)
880         code = DumpInt32(iodp, 'C', V_creationDate(vp));        /* Rw volume creation date */
881     if (!code)
882         code = DumpInt32(iodp, 'A', V_accessDate(vp));
883     if (!code)
884         code = DumpInt32(iodp, 'U', V_updateDate(vp));
885     if (!code)
886         code = DumpInt32(iodp, 'E', V_expirationDate(vp));
887     if (!code)
888         code = DumpInt32(iodp, 'B', V_backupDate(vp));  /* Rw volume backup clone date */
889     if (!code)
890         code = DumpString(iodp, 'O', V_offlineMessage(vp));
891     /*
892      * We do NOT dump the detailed volume statistics residing in the old
893      * motd field, since we cannot tell from the info in a dump whether
894      * statistics data has been put there.  Instead, we dump a null string,
895      * just as if that was what the motd contained.
896      */
897     if (!code)
898         code = DumpString(iodp, 'M', nullString);
899     if (!code)
900         code =
901             DumpArrayInt32(iodp, 'W', (afs_uint32 *) V_weekUse(vp),
902                            sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]));
903     if (!code)
904         code = DumpInt32(iodp, 'D', V_dayUseDate(vp));
905     if (!code)
906         code = DumpInt32(iodp, 'Z', V_dayUse(vp));
907     return code;
908 }
909
910 static int
911 DumpEnd(struct iod *iodp)
912 {
913     return (DumpInt32(iodp, D_DUMPEND, DUMPENDMAGIC));
914 }
915
916 /* Guts of the dump code */
917
918 /* Dump a whole volume */
919 int
920 DumpVolume(struct rx_call *call, Volume * vp,
921            afs_int32 fromtime, int dumpAllDirs)
922 {
923     struct iod iod;
924     int code = 0;
925     struct iod *iodp = &iod;
926     iod_Init(iodp, call);
927
928     if (!code)
929         code = DumpDumpHeader(iodp, vp, fromtime);
930
931     if (!code)
932         code = DumpPartial(iodp, vp, fromtime, dumpAllDirs);
933
934 /* hack follows.  Errors should be handled quite differently in this version of dump than they used to be.*/
935     if (rx_Error(iodp->call)) {
936         Log("1 Volser: DumpVolume: Rx call failed during dump, error %d\n",
937             rx_Error(iodp->call));
938         return VOLSERDUMPERROR;
939     }
940     if (!code)
941         code = DumpEnd(iodp);
942
943     return code;
944 }
945
946 /* Dump a volume to multiple places*/
947 int
948 DumpVolMulti(struct rx_call **calls, int ncalls, Volume * vp,
949              afs_int32 fromtime, int dumpAllDirs, int *codes)
950 {
951     struct iod iod;
952     int code = 0;
953     iod_InitMulti(&iod, calls, ncalls, codes);
954
955     if (!code)
956         code = DumpDumpHeader(&iod, vp, fromtime);
957     if (!code)
958         code = DumpPartial(&iod, vp, fromtime, dumpAllDirs);
959     if (!code)
960         code = DumpEnd(&iod);
961     return code;
962 }
963
964 /* A partial dump (no dump header) */
965 static int
966 DumpPartial(struct iod *iodp, Volume * vp,
967             afs_int32 fromtime, int dumpAllDirs)
968 {
969     int code = 0;
970     if (!code)
971         code = DumpVolumeHeader(iodp, vp);
972     if (!code)
973         code = DumpVnodeIndex(iodp, vp, vLarge, fromtime, dumpAllDirs);
974     if (!code)
975         code = DumpVnodeIndex(iodp, vp, vSmall, fromtime, 0);
976     return code;
977 }
978
979 static int
980 DumpVnodeIndex(struct iod *iodp, Volume * vp, VnodeClass class,
981                afs_int32 fromtime, int forcedump)
982 {
983     int code = 0;
984     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
985     char buf[SIZEOF_LARGEDISKVNODE];
986     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
987     StreamHandle_t *file;
988     FdHandle_t *fdP;
989     afs_sfsize_t size, nVnodes;
990     int flag;
991     int vnodeIndex;
992
993     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
994     opr_Assert(fdP != NULL);
995     file = FDH_FDOPEN(fdP, "r+");
996     opr_Assert(file != NULL);
997     size = OS_SIZE(fdP->fd_fd);
998     opr_Assert(size != -1);
999     nVnodes = (size / vcp->diskSize) - 1;
1000     if (nVnodes > 0) {
1001         opr_Assert((nVnodes + 1) * vcp->diskSize == size);
1002         opr_Assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
1003     } else
1004         nVnodes = 0;
1005     for (vnodeIndex = 0;
1006          nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
1007          nVnodes--, vnodeIndex++) {
1008         flag = forcedump || (vnode->serverModifyTime >= fromtime);
1009         /* Note:  the >= test is very important since some old volumes may not have
1010          * a serverModifyTime.  For an epoch dump, this results in 0>=0 test, which
1011          * does dump the file! */
1012         if (!code)
1013             code =
1014                 DumpVnode(iodp, vnode, V_id(vp),
1015                           bitNumberToVnodeNumber(vnodeIndex, class), flag);
1016 #ifndef AFS_PTHREAD_ENV
1017         if (!flag)
1018             IOMGR_Poll();       /* if we dont' xfr data, but scan instead, could lose conn */
1019 #endif
1020     }
1021     STREAM_CLOSE(file);
1022     FDH_CLOSE(fdP);
1023     return code;
1024 }
1025
1026 static int
1027 DumpDumpHeader(struct iod *iodp, Volume * vp,
1028                afs_int32 fromtime)
1029 {
1030     int code = 0;
1031     int UseLatestReadOnlyClone = 1;
1032     afs_int32 dumpTimes[2];
1033     iodp->device = vp->device;
1034     iodp->parentId = V_parentId(vp);
1035     iodp->dumpPartition = vp->partition;
1036     if (!code)
1037         code = DumpDouble(iodp, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
1038     if (!code)
1039         code =
1040             DumpInt32(iodp, 'v',
1041                       UseLatestReadOnlyClone ? V_id(vp) : V_parentId(vp));
1042     if (!code)
1043         code = DumpString(iodp, 'n', V_name(vp));
1044     dumpTimes[0] = fromtime;
1045     switch (V_type(vp)) {
1046     case readwriteVolume:
1047         dumpTimes[1] = V_updateDate(vp);        /* until last update */
1048         break;
1049     case readonlyVolume:
1050         dumpTimes[1] = V_creationDate(vp);      /* until clone was updated */
1051         break;
1052     case backupVolume:
1053         /* until backup was made */
1054         dumpTimes[1] = V_backupDate(vp) != 0 ? V_backupDate(vp) :
1055                                                V_creationDate(vp);
1056         break;
1057     default:
1058         code = EINVAL;
1059     }
1060     if (!code)
1061         code = DumpArrayInt32(iodp, 't', (afs_uint32 *) dumpTimes, 2);
1062     return code;
1063 }
1064
1065 static int
1066 DumpVnode(struct iod *iodp, struct VnodeDiskObject *v, VolumeId volid,
1067           int vnodeNumber, int dumpEverything)
1068 {
1069     int code = 0;
1070     IHandle_t *ihP;
1071     FdHandle_t *fdP;
1072     afs_ino_str_t stmp;
1073
1074     if (!v || v->type == vNull)
1075         return code;
1076     if (!code)
1077         code = DumpDouble(iodp, D_VNODE, vnodeNumber, v->uniquifier);
1078     if (!dumpEverything)
1079         return code;
1080     if (!code)
1081         code = DumpByte(iodp, 't', (byte) v->type);
1082     if (!code)
1083         code = DumpShort(iodp, 'l', v->linkCount);      /* May not need this */
1084     if (!code)
1085         code = DumpInt32(iodp, 'v', v->dataVersion);
1086     if (!code)
1087         code = DumpInt32(iodp, 'm', v->unixModifyTime);
1088     if (!code)
1089         code = DumpInt32(iodp, 'a', v->author);
1090     if (!code)
1091         code = DumpInt32(iodp, 'o', v->owner);
1092     if (!code && v->group)
1093         code = DumpInt32(iodp, 'g', v->group);  /* default group is 0 */
1094     if (!code)
1095         code = DumpShort(iodp, 'b', v->modeBits);
1096     if (!code)
1097         code = DumpInt32(iodp, 'p', v->parent);
1098     if (!code)
1099         code = DumpInt32(iodp, 's', v->serverModifyTime);
1100     if (v->type == vDirectory) {
1101         code = acl_HtonACL(VVnodeDiskACL(v));
1102         if (code) {
1103             Log("DumpVnode: Skipping invalid acl vnode %u (volume %"AFS_VOLID_FMT")\n",
1104                  vnodeNumber, afs_printable_VolumeId_lu(volid));
1105         }
1106         if (!code)
1107             code =
1108                 DumpByteString(iodp, 'A', (byte *) VVnodeDiskACL(v),
1109                                VAclDiskSize(v));
1110     }
1111     if (VNDISK_GET_INO(v)) {
1112         afs_sfsize_t indexlen, disklen;
1113         IH_INIT(ihP, iodp->device, iodp->parentId, VNDISK_GET_INO(v));
1114         fdP = IH_OPEN(ihP);
1115         if (fdP == NULL) {
1116             Log("1 Volser: DumpVnode: dump: Unable to open inode %s "
1117                 "for vnode %u (volume %" AFS_VOLID_FMT "); "
1118                 "not dumped, error %d\n",
1119                 PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber,
1120                 afs_printable_VolumeId_lu(volid), errno);
1121             IH_RELEASE(ihP);
1122             return VOLSERREAD_DUMPERROR;
1123         }
1124         VNDISK_GET_LEN(indexlen, v);
1125         disklen = FDH_SIZE(fdP);
1126         if (indexlen != disklen) {
1127             FDH_REALLYCLOSE(fdP);
1128             IH_RELEASE(ihP);
1129             Log("DumpVnode: volume %"AFS_VOLID_FMT" "
1130                 "vnode %lu has inconsistent length "
1131                 "(index %lu disk %lu); aborting dump\n",
1132                 afs_printable_VolumeId_lu(volid), (unsigned long)vnodeNumber,
1133                 (unsigned long)indexlen, (unsigned long)disklen);
1134             return VOLSERREAD_DUMPERROR;
1135         }
1136         code = DumpFile(iodp, vnodeNumber, fdP);
1137         FDH_CLOSE(fdP);
1138         IH_RELEASE(ihP);
1139     }
1140     return code;
1141 }
1142
1143
1144 int
1145 ProcessIndex(Volume * vp, VnodeClass class, afs_foff_t ** Bufp, int *sizep,
1146              int del)
1147 {
1148     int i, nVnodes, code;
1149     afs_foff_t offset;
1150     afs_foff_t *Buf;
1151     int cnt = 0;
1152     afs_sfsize_t size;
1153     StreamHandle_t *afile;
1154     FdHandle_t *fdP;
1155     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
1156     char buf[SIZEOF_LARGEDISKVNODE], zero[SIZEOF_LARGEDISKVNODE];
1157     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1158     afs_ino_str_t stmp;
1159
1160     memset(zero, 0, sizeof(zero));      /* zero out our proto-vnode */
1161     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1162     if (fdP == NULL)
1163         return -1;
1164     afile = FDH_FDOPEN(fdP, "r+");
1165     if (del) {
1166         int cnt1 = 0;
1167         Buf = *Bufp;
1168         for (i = 0; i < *sizep; i++) {
1169             if (Buf[i]) {
1170                 cnt++;
1171                 STREAM_ASEEK(afile, Buf[i]);
1172                 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
1173                 if (code == 1) {
1174                     if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
1175                         cnt1++;
1176                         if (DoLogging) {
1177                             Log("RestoreVolume %"AFS_VOLID_FMT" "
1178                                 "Cleanup: Removing old vnode=%u inode=%s "
1179                                 "size=unknown\n",
1180                                 afs_printable_VolumeId_lu(V_id(vp)),
1181                                 bitNumberToVnodeNumber(i, class),
1182                                 PrintInode(stmp, VNDISK_GET_INO(vnode)));
1183                         }
1184                         IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(vnode),
1185                                V_parentId(vp));
1186                         DOPOLL;
1187                     }
1188                     STREAM_ASEEK(afile, Buf[i]);
1189                     (void)STREAM_WRITE(zero, vcp->diskSize, 1, afile);  /* Zero it out */
1190                 }
1191                 Buf[i] = 0;
1192             }
1193         }
1194         if (DoLogging) {
1195             Log("RestoreVolume Cleanup: Removed %d inodes for volume %d\n",
1196                 cnt1, V_id(vp));
1197         }
1198         STREAM_FLUSH(afile);    /* ensure 0s are on the disk */
1199         OS_SYNC(afile->str_fd);
1200     } else {
1201         size = OS_SIZE(fdP->fd_fd);
1202         opr_Assert(size != -1);
1203         nVnodes =
1204             (size <=
1205              vcp->diskSize ? 0 : size - vcp->diskSize) >> vcp->logSize;
1206         if (nVnodes > 0) {
1207             Buf = calloc(nVnodes,  sizeof(afs_foff_t));
1208             if (Buf == NULL) {
1209                 STREAM_CLOSE(afile);
1210                 FDH_CLOSE(fdP);
1211                 return -1;
1212             }
1213             STREAM_ASEEK(afile, offset = vcp->diskSize);
1214             while (1) {
1215                 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
1216                 if (code != 1) {
1217                     break;
1218                 }
1219                 if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
1220                     Buf[(offset >> vcp->logSize) - 1] = offset;
1221                     cnt++;
1222                 }
1223                 offset += vcp->diskSize;
1224             }
1225             *Bufp = Buf;
1226             *sizep = nVnodes;
1227         }
1228     }
1229     STREAM_CLOSE(afile);
1230     FDH_CLOSE(fdP);
1231     return 0;
1232 }
1233
1234
1235 int
1236 RestoreVolume(struct rx_call *call, Volume * avp, struct restoreCookie *cookie)
1237 {
1238     VolumeDiskData vol;
1239     struct DumpHeader header;
1240     afs_uint32 endMagic;
1241     Error error = 0, vupdate;
1242     Volume *vp;
1243     struct iod iod;
1244     struct iod *iodp = &iod;
1245     afs_foff_t *b1 = NULL, *b2 = NULL;
1246     int s1 = 0, s2 = 0, delo = 0, tdelo;
1247     int tag;
1248     VolumeDiskData saved_header;
1249     afs_uint32 uptime, crtime;
1250
1251     iod_Init(iodp, call);
1252
1253     vp = avp;
1254
1255     if (DoPreserveVolumeStats) {
1256         CopyVolumeStats(&V_disk(vp), &saved_header);
1257     }
1258
1259     if (!ReadDumpHeader(iodp, &header)) {
1260         Log("1 Volser: RestoreVolume: Error reading header file for dump; aborted\n");
1261         return VOLSERREAD_DUMPERROR;
1262     }
1263     if (iod_getc(iodp) != D_VOLUMEHEADER) {
1264         Log("1 Volser: RestoreVolume: Volume header missing from dump; not restored\n");
1265         return VOLSERREAD_DUMPERROR;
1266     }
1267     if (ReadVolumeHeader(iodp, &vol) == VOLSERREAD_DUMPERROR) {
1268         Log("1 Volser: RestoreVolume: Error reading volume header (id: %u); aborted\n",
1269             V_id(vp));
1270         return VOLSERREAD_DUMPERROR;
1271     }
1272
1273     if (!delo)
1274         delo = ProcessIndex(vp, vLarge, &b1, &s1, 0);
1275     if (!delo)
1276         delo = ProcessIndex(vp, vSmall, &b2, &s2, 0);
1277     if (delo < 0) {
1278         Log("1 Volser: RestoreVolume: ProcessIndex failed; not restored\n");
1279         error = VOLSERREAD_DUMPERROR;
1280         goto out;
1281     }
1282
1283     strncpy(vol.name, cookie->name, VOLSER_OLDMAXVOLNAME);
1284     vol.type = cookie->type;
1285     vol.cloneId = cookie->clone;
1286     vol.parentId = cookie->parent;
1287
1288     V_needsSalvaged(vp) = 0;
1289
1290     tdelo = delo;
1291     while (1) {
1292         if (ReadVnodes(iodp, vp, b1, s1, b2, s2, tdelo)) {
1293             Log("1 Volser: RestoreVolume: Error reading vnodes (id: %u); aborted\n",
1294                 V_id(vp));
1295             error = VOLSERREAD_DUMPERROR;
1296             goto clean;
1297         }
1298         tag = iod_getc(iodp);
1299         if (tag != D_VOLUMEHEADER)
1300             break;
1301
1302         if (ReadVolumeHeader(iodp, &vol) == VOLSERREAD_DUMPERROR) {
1303             Log("1 Volser: RestoreVolume: Error reading volume header (id: %u); aborted\n",
1304                 V_id(vp));
1305             error = VOLSERREAD_DUMPERROR;
1306             goto out;
1307         }
1308     }
1309     if (tag != D_DUMPEND || !ReadInt32(iodp, &endMagic)
1310         || endMagic != DUMPENDMAGIC) {
1311         Log("1 Volser: RestoreVolume: End of dump not found; restore aborted\n");
1312         error = VOLSERREAD_DUMPERROR;
1313         goto clean;
1314     }
1315
1316
1317     if (iod_getc(iodp) != EOF) {
1318         Log("1 Volser: RestoreVolume: Unrecognized postamble in dump; restore aborted\n");
1319         error = VOLSERREAD_DUMPERROR;
1320         goto clean;
1321     }
1322
1323     if (!delo) {
1324         delo = ProcessIndex(vp, vLarge, &b1, &s1, 1);
1325         if (!delo)
1326             delo = ProcessIndex(vp, vSmall, &b2, &s2, 1);
1327         if (delo < 0) {
1328             error = VOLSERREAD_DUMPERROR;
1329             goto clean;
1330         }
1331     }
1332
1333   clean:
1334     if (DoPreserveVolumeStats) {
1335         CopyVolumeStats(&saved_header, &vol);
1336     } else {
1337         ClearVolumeStats(&vol);
1338     }
1339     if (V_needsSalvaged(vp)) {
1340         /* needsSalvaged may have been set while we tried to write volume data.
1341          * prevent it from getting overwritten. */
1342         vol.needsSalvaged = V_needsSalvaged(vp);
1343     }
1344     crtime = V_creationDate(vp);
1345     uptime = V_updateDate(vp);
1346     CopyVolumeHeader(&vol, &V_disk(vp));
1347     V_destroyMe(vp) = 0;
1348     VUpdateVolume(&vupdate, vp);
1349     if (vupdate) {
1350         Log("1 Volser: RestoreVolume: Unable to rewrite volume header; restore aborted\n");
1351         error = VOLSERREAD_DUMPERROR;
1352         goto out;
1353     } else {
1354         /*
1355          * If the volume was not a new empty volume and the restored dump was
1356          * older than the volume in question, this is probably a mistake, and
1357          * may mean the resulting volume is corrupted. Log the following message
1358          * to give a clue as to why this volume suddenly looks strange or corrupt.
1359          */
1360         if ((crtime != uptime) && (uptime > V_updateDate(vp))) {
1361             Log("1 Volser: RestoreVolume: volume %s (%u) appears to have been partially or "
1362                 "completely restored to an earlier version (updateDate went from %u to %u). "
1363                 "This is allowed, but may indicate a mistake in whatever tool is restoring "
1364                 "this volume. If this volume appears corrupted, this is probably why.\n",
1365                 V_name(vp), V_id(vp), uptime, V_updateDate(vp));
1366         }
1367     }
1368   out:
1369     /* Free the malloced space above */
1370     if (b1)
1371         free(b1);
1372     if (b2)
1373         free(b2);
1374     return error;
1375 }
1376
1377 static int
1378 ReadVnodes(struct iod *iodp, Volume * vp, afs_foff_t * Lbuf, afs_int32 s1,
1379            afs_foff_t * Sbuf, afs_int32 s2, afs_int32 delo)
1380 {
1381     afs_int32 vnodeNumber;
1382     char buf[SIZEOF_LARGEDISKVNODE];
1383     int tag;
1384     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1385     struct VnodeDiskObject oldvnode;
1386     int idx;
1387     VnodeClass class;
1388     struct VnodeClassInfo *vcp;
1389     IHandle_t *tmpH;
1390     FdHandle_t *fdP;
1391     Inode nearInode AFS_UNUSED;
1392     afs_int32 critical = 0;
1393     int nbytes;
1394
1395     tag = iod_getc(iodp);
1396     V_pref(vp, nearInode);
1397     while (tag == D_VNODE) {
1398         int haveStuff = 0;
1399         int saw_f = 0;
1400         memset(buf, 0, sizeof(buf));
1401         if (!ReadInt32(iodp, (afs_uint32 *) & vnodeNumber))
1402             break;
1403
1404         if (!ReadInt32(iodp, &vnode->uniquifier))
1405             return VOLSERREAD_DUMPERROR;
1406
1407         while ((tag = iod_getc(iodp)) > D_MAX && tag != EOF) {
1408             haveStuff = 1;
1409             if (critical)
1410                 critical--;
1411             switch (tag) {
1412             case 't':
1413                 vnode->type = (VnodeType) iod_getc(iodp);
1414                 break;
1415             case 'l':
1416                 {
1417                     unsigned short tlc;
1418                     if (!ReadShort(iodp, &tlc))
1419                         return VOLSERREAD_DUMPERROR;
1420                     vnode->linkCount = (signed int)tlc;
1421                 }
1422                 break;
1423             case 'v':
1424                 if (!ReadInt32(iodp, &vnode->dataVersion))
1425                     return VOLSERREAD_DUMPERROR;
1426                 break;
1427             case 'm':
1428                 if (!ReadInt32(iodp, &vnode->unixModifyTime))
1429                     return VOLSERREAD_DUMPERROR;
1430                 break;
1431             case 's':
1432                 if (!ReadInt32(iodp, &vnode->serverModifyTime))
1433                     return VOLSERREAD_DUMPERROR;
1434                 break;
1435             case 'a':
1436                 if (!ReadInt32(iodp, &vnode->author))
1437                     return VOLSERREAD_DUMPERROR;
1438                 break;
1439             case 'o':
1440                 if (!ReadInt32(iodp, &vnode->owner))
1441                     return VOLSERREAD_DUMPERROR;
1442                 break;
1443             case 'g':
1444                 if (!ReadInt32(iodp, (afs_uint32 *) & vnode->group))
1445                     return VOLSERREAD_DUMPERROR;
1446                 break;
1447             case 'b':{
1448                     unsigned short modeBits;
1449                     if (!ReadShort(iodp, &modeBits))
1450                         return VOLSERREAD_DUMPERROR;
1451                     vnode->modeBits = (unsigned int)modeBits;
1452                     break;
1453                 }
1454             case 'p':
1455                 if (!ReadInt32(iodp, &vnode->parent))
1456                     return VOLSERREAD_DUMPERROR;
1457                 break;
1458             case 'A':
1459                 nbytes = ReadByteString(iodp, (byte *) VVnodeDiskACL(vnode),
1460                                VAclDiskSize(vnode));
1461                 if (nbytes != VAclDiskSize(vnode)) {
1462                     Log("ReadVnodes: could not read acl for vnode %lu in dump.\n",
1463                          (unsigned long)vnodeNumber);
1464                     return VOLSERREAD_DUMPERROR;
1465                 }
1466                 if (acl_NtohACL(VVnodeDiskACL(vnode)) != 0) {
1467                     Log("ReadVnodes: invalid acl for vnode %lu in dump.\n",
1468                          (unsigned long)vnodeNumber);
1469                     return VOLSERREAD_DUMPERROR;
1470                 }
1471                 break;
1472             case 'h':
1473             case 'f':{
1474                     Inode ino;
1475                     Error error;
1476                     afs_fsize_t vnodeLength;
1477
1478                     if (saw_f) {
1479                         Log("Volser: ReadVnodes: warning: ignoring duplicate "
1480                             "file entries for vnode %lu in dump\n",
1481                             (unsigned long)vnodeNumber);
1482                         volser_WriteFile(vnodeNumber, iodp, NULL, tag, &error);
1483                         break;
1484                     }
1485                     saw_f = 1;
1486
1487                     tmpH =
1488                         IH_CREATE_INIT(V_linkHandle(vp), V_device(vp),
1489                                   VPartitionPath(V_partition(vp)), nearInode,
1490                                   V_parentId(vp), vnodeNumber,
1491                                   vnode->uniquifier, vnode->dataVersion);
1492                     if (!tmpH) {
1493                         Log("1 Volser: ReadVnodes: IH_CREATE: %s - restore aborted\n",
1494                             afs_error_message(errno));
1495                         V_needsSalvaged(vp) = 1;
1496                         return VOLSERREAD_DUMPERROR;
1497                     }
1498                     ino = tmpH->ih_ino;
1499                     nearInode = ino;
1500                     VNDISK_SET_INO(vnode, ino);
1501                     fdP = IH_OPEN(tmpH);
1502                     if (fdP == NULL) {
1503                         Log("1 Volser: ReadVnodes: IH_OPEN: %s - restore aborted\n",
1504                             afs_error_message(errno));
1505                         IH_RELEASE(tmpH);
1506                         V_needsSalvaged(vp) = 1;
1507                         return VOLSERREAD_DUMPERROR;
1508                     }
1509                     vnodeLength =
1510                         volser_WriteFile(vnodeNumber, iodp, fdP, tag, &error);
1511                     VNDISK_SET_LEN(vnode, vnodeLength);
1512                     FDH_REALLYCLOSE(fdP);
1513                     IH_RELEASE(tmpH);
1514                     if (error) {
1515                         Log("1 Volser: ReadVnodes: IDEC inode %llu\n",
1516                             (afs_uintmax_t) ino);
1517                         IH_DEC(V_linkHandle(vp), ino, V_parentId(vp));
1518                         V_needsSalvaged(vp) = 1;
1519                         return VOLSERREAD_DUMPERROR;
1520                     }
1521                     break;
1522                 }
1523             case 0x7e:
1524                 critical = 2;
1525                 break;
1526             default:
1527                 if (!HandleUnknownTag(iodp, tag, 2, critical))
1528                     return VOLSERREAD_DUMPERROR;
1529             }
1530         }
1531
1532         class = vnodeIdToClass(vnodeNumber);
1533         vcp = &VnodeClassInfo[class];
1534
1535         /* Mark this vnode as in this dump - so we don't delete it later */
1536         if (!delo) {
1537             idx = (vnodeIndexOffset(vcp, vnodeNumber) >> vcp->logSize) - 1;
1538             if (class == vLarge) {
1539                 if (Lbuf && (idx < s1))
1540                     Lbuf[idx] = 0;
1541             } else {
1542                 if (Sbuf && (idx < s2))
1543                     Sbuf[idx] = 0;
1544             }
1545         }
1546
1547         if (haveStuff) {
1548             FdHandle_t *fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1549             if (fdP == NULL) {
1550                 Log("1 Volser: ReadVnodes: Error opening vnode index: %s; restore aborted\n",
1551                     afs_error_message(errno));
1552                 V_needsSalvaged(vp) = 1;
1553                 return VOLSERREAD_DUMPERROR;
1554             }
1555             if (FDH_PREAD(fdP, &oldvnode, sizeof(oldvnode), vnodeIndexOffset(vcp, vnodeNumber)) ==
1556                 sizeof(oldvnode)) {
1557                 if (oldvnode.type != vNull && VNDISK_GET_INO(&oldvnode)) {
1558                     IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(&oldvnode),
1559                            V_parentId(vp));
1560                 }
1561             }
1562             vnode->vnodeMagic = vcp->magic;
1563             if (FDH_PWRITE(fdP, vnode, vcp->diskSize, vnodeIndexOffset(vcp, vnodeNumber)) != vcp->diskSize) {
1564                 Log("1 Volser: ReadVnodes: Error writing vnode index: %s; restore aborted\n",
1565                     afs_error_message(errno));
1566                 FDH_REALLYCLOSE(fdP);
1567                 V_needsSalvaged(vp) = 1;
1568                 return VOLSERREAD_DUMPERROR;
1569             }
1570             FDH_CLOSE(fdP);
1571         }
1572     }
1573     iod_ungetc(iodp, tag);
1574
1575     return 0;
1576 }
1577
1578
1579 /* called with disk file only.  Note that we don't have to worry about rx_Read
1580  * needing to read an ungetc'd character, since the ReadInt32 will have read
1581  * it instead.
1582  *
1583  * if handleP == NULL, don't write the file anywhere; just read and discard
1584  * the file contents
1585  */
1586 static afs_fsize_t
1587 volser_WriteFile(int vn, struct iod *iodp, FdHandle_t * handleP, int tag,
1588                  Error * status)
1589 {
1590     afs_int32 code;
1591     ssize_t nBytes;
1592     afs_fsize_t filesize;
1593     afs_fsize_t written = 0;
1594     size_t size = 8192;
1595     afs_fsize_t nbytes;
1596     unsigned char *p;
1597
1598
1599     *status = 0;
1600     {
1601         afs_uint32 filesize_high = 0L, filesize_low = 0L;
1602         if (tag == 'h') {
1603             if (!ReadInt32(iodp, &filesize_high)) {
1604                 *status = 1;
1605                 return 0;
1606             }
1607         }
1608         if (!ReadInt32(iodp, &filesize_low)) {
1609             *status = 1;
1610             return 0;
1611         }
1612         FillInt64(filesize, filesize_high, filesize_low);
1613     }
1614     p = malloc(size);
1615     if (p == NULL) {
1616         *status = 2;
1617         return (0);
1618     }
1619     for (nbytes = filesize; nbytes; nbytes -= size) {
1620         if (nbytes < size)
1621             size = nbytes;
1622
1623         if ((code = iod_Read(iodp, (char *) p, size)) != size) {
1624             Log("1 Volser: WriteFile: Error reading dump file %d size=%llu nbytes=%u (%d of %u): %s; restore aborted\n", vn, (afs_uintmax_t) filesize, nbytes, code, (unsigned)size, afs_error_message(errno));
1625             *status = 3;
1626             break;
1627         }
1628         if (handleP) {
1629             nBytes = FDH_PWRITE(handleP, p, size, written);
1630             if (nBytes > 0)
1631                 written += nBytes;
1632             if (nBytes != size) {
1633                 Log("1 Volser: WriteFile: Error writing (%u) bytes to vnode %d; %s; restore aborted\n", (int)(nBytes & 0xffffffff), vn, afs_error_message(errno));
1634                 *status = 4;
1635                 break;
1636             }
1637         }
1638     }
1639     free(p);
1640     return (written);
1641 }
1642
1643 static int
1644 ReadDumpHeader(struct iod *iodp, struct DumpHeader *hp)
1645 {
1646     int tag;
1647     afs_uint32 beginMagic;
1648     afs_int32 critical = 0;
1649     if (iod_getc(iodp) != D_DUMPHEADER || !ReadInt32(iodp, &beginMagic)
1650         || !ReadInt32(iodp, (afs_uint32 *) & hp->version)
1651         || beginMagic != DUMPBEGINMAGIC)
1652         return 0;
1653     hp->volumeId = 0;
1654     hp->nDumpTimes = 0;
1655     while ((tag = iod_getc(iodp)) > D_MAX) {
1656         unsigned short arrayLength;
1657         int i;
1658         if (critical)
1659             critical--;
1660         switch (tag) {
1661         case 'v':
1662             if (!ReadInt32(iodp, &hp->volumeId))
1663                 return 0;
1664             break;
1665         case 'n':
1666             ReadString(iodp, hp->volumeName, sizeof(hp->volumeName));
1667             break;
1668         case 't':
1669             if (!ReadShort(iodp, &arrayLength))
1670                 return 0;
1671             hp->nDumpTimes = (arrayLength >> 1);
1672             for (i = 0; i < hp->nDumpTimes; i++)
1673                 if (!ReadInt32(iodp, (afs_uint32 *) & hp->dumpTimes[i].from)
1674                     || !ReadInt32(iodp, (afs_uint32 *) & hp->dumpTimes[i].to))
1675                     return 0;
1676             break;
1677         case 0x7e:
1678             critical = 2;
1679             break;
1680         default:
1681             if (!HandleUnknownTag(iodp, tag, 0, critical))
1682                 return VOLSERREAD_DUMPERROR;
1683         }
1684     }
1685     if (!hp->volumeId || !hp->nDumpTimes) {
1686         return 0;
1687     }
1688     iod_ungetc(iodp, tag);
1689     return 1;
1690 }
1691
1692
1693 /* ----- Below are the calls that calculate dump size ----- */
1694
1695 static int
1696 SizeDumpVolumeHeader(struct iod *iodp, Volume * vp,
1697                      struct volintSize *v_size)
1698 {
1699     int code = 0;
1700     static char nullString[1] = "";     /*The ``contents'' of motd */
1701     afs_uint64 addvar;
1702
1703 /*     if (!code) code = DumpTag(iodp, D_VOLUMEHEADER); */
1704     FillInt64(addvar,0, 1);
1705     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1706 /*     if (!code) {code = DumpInt32(iodp, 'i',V_id(vp));} */
1707     FillInt64(addvar,0, 5);
1708     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1709 /*     if (!code) code = DumpInt32(iodp, 'v',V_stamp(vp).version); */
1710     FillInt64(addvar,0, 5);
1711     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1712 /*     if (!code) code = DumpString(iodp, 'n',V_name(vp)); */
1713     FillInt64(addvar,0, (2 + strlen(V_name(vp))));
1714     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1715 /*     if (!code) code = DumpBool(iodp, 's',V_inService(vp)); */
1716     FillInt64(addvar,0, 2);
1717     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1718 /*     if (!code) code = DumpBool(iodp, 'b',V_blessed(vp)); */
1719     FillInt64(addvar,0, 2);
1720     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1721 /*     if (!code) code = DumpInt32(iodp, 'u',V_uniquifier(vp)); */
1722     FillInt64(addvar,0, 5);
1723     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1724 /*     if (!code) code = DumpByte(iodp, 't',(byte)V_type(vp)); */
1725     FillInt64(addvar,0, 2);
1726     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1727 /*     if (!code){ code = DumpInt32(iodp, 'p',V_parentId(vp));} */
1728     FillInt64(addvar,0, 5);
1729     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1730 /*     if (!code) code = DumpInt32(iodp, 'c',V_cloneId(vp)); */
1731     FillInt64(addvar,0, 5);
1732     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1733 /*     if (!code) code = DumpInt32(iodp, 'q',V_maxquota(vp)); */
1734     FillInt64(addvar,0, 5);
1735     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1736 /*     if (!code) code = DumpInt32(iodp, 'm',V_minquota(vp)); */
1737     FillInt64(addvar,0, 5);
1738     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1739 /*     if (!code) code = DumpInt32(iodp, 'd',V_diskused(vp)); */
1740     FillInt64(addvar,0, 5);
1741     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1742 /*     if (!code) code = DumpInt32(iodp, 'f',V_filecount(vp)); */
1743     FillInt64(addvar,0, 5);
1744     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1745 /*     if (!code) code = DumpInt32(iodp, 'a', V_accountNumber(vp)); */
1746     FillInt64(addvar,0, 5);
1747     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1748 /*     if (!code) code = DumpInt32(iodp, 'o', V_owner(vp)); */
1749     FillInt64(addvar,0, 5);
1750     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1751 /*     if (!code) code = DumpInt32(iodp, 'C',V_creationDate(vp));       /\* Rw volume creation date *\/ */
1752     FillInt64(addvar,0, 5);
1753     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1754 /*     if (!code) code = DumpInt32(iodp, 'A',V_accessDate(vp)); */
1755     FillInt64(addvar,0, 5);
1756     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1757 /*     if (!code) code = DumpInt32(iodp, 'U',V_updateDate(vp)); */
1758     FillInt64(addvar,0, 5);
1759     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1760 /*     if (!code) code = DumpInt32(iodp, 'E',V_expirationDate(vp)); */
1761     FillInt64(addvar,0, 5);
1762     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1763 /*     if (!code) code = DumpInt32(iodp, 'B',V_backupDate(vp));         /\* Rw volume backup clone date *\/ */
1764     FillInt64(addvar,0, 5);
1765     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1766 /*     if (!code) code = DumpString(iodp, 'O',V_offlineMessage(vp)); */
1767     FillInt64(addvar,0, (2 + strlen(V_offlineMessage(vp))));
1768     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1769 /*     /\* */
1770 /*      * We do NOT dump the detailed volume statistics residing in the old */
1771 /*      * motd field, since we cannot tell from the info in a dump whether */
1772 /*      * statistics data has been put there.  Instead, we dump a null string, */
1773 /*      * just as if that was what the motd contained. */
1774 /*      *\/ */
1775 /*     if (!code) code = DumpString(iodp, 'M', nullString); */
1776     FillInt64(addvar,0, (2 + strlen(nullString)));
1777     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1778 /*     if (!code) code = DumpArrayInt32(iodp, 'W', (afs_uint32 *)V_weekUse(vp), sizeof(V_weekUse(vp))/sizeof(V_weekUse(vp)[0])); */
1779     FillInt64(addvar,0, (3 + 4 * (sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]))));
1780     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1781 /*     if (!code) code = DumpInt32(iodp, 'D', V_dayUseDate(vp)); */
1782     FillInt64(addvar,0, 5);
1783     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1784 /*     if (!code) code = DumpInt32(iodp, 'Z', V_dayUse(vp)); */
1785     FillInt64(addvar,0, 5);
1786     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1787     return code;
1788 }
1789
1790 static int
1791 SizeDumpEnd(struct iod *iodp, struct volintSize *v_size)
1792 {
1793     int code = 0;
1794     afs_uint64 addvar;
1795     FillInt64(addvar,0, 5);
1796     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1797     return code;
1798 }
1799
1800 int
1801 SizeDumpVolume(struct rx_call *call, Volume * vp,
1802                afs_int32 fromtime, int dumpAllDirs,
1803                struct volintSize *v_size)
1804 {
1805     int code = 0;
1806     struct iod *iodp = (struct iod *)0;
1807 /*    iod_Init(iodp, call); */
1808
1809     if (!code)
1810         code = SizeDumpDumpHeader(iodp, vp, fromtime, v_size);
1811     if (!code)
1812         code = SizeDumpPartial(iodp, vp, fromtime, dumpAllDirs, v_size);
1813     if (!code)
1814         code = SizeDumpEnd(iodp, v_size);
1815
1816     return code;
1817 }
1818
1819 static int
1820 SizeDumpDumpHeader(struct iod *iodp, Volume * vp,
1821                    afs_int32 fromtime, struct volintSize *v_size)
1822 {
1823     int code = 0;
1824 /*    int UseLatestReadOnlyClone = 1; */
1825 /*    afs_int32 dumpTimes[2]; */
1826     afs_uint64 addvar;
1827 /*    iodp->device = vp->device; */
1828 /*    iodp->parentId = V_parentId(vp); */
1829 /*    iodp->dumpPartition = vp->partition; */
1830
1831     ZeroInt64(v_size->dump_size);       /* initialize the size */
1832 /*     if (!code) code = DumpDouble(iodp, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION); */
1833     FillInt64(addvar,0, 9);
1834     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1835 /*     if (!code) code = DumpInt32(iodp, 'v', UseLatestReadOnlyClone? V_id(vp): V_parentId(vp)); */
1836     FillInt64(addvar,0, 5);
1837     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1838 /*     if (!code) code = DumpString(iodp, 'n',V_name(vp)); */
1839     FillInt64(addvar,0, (2 + strlen(V_name(vp))));
1840     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1841 /*     dumpTimes[0] = fromtime; */
1842 /*     dumpTimes[1] = V_backupDate(vp); /\* Until the time the clone was made *\/ */
1843 /*     if (!code) code = DumpArrayInt32(iodp, 't', (afs_uint32 *)dumpTimes, 2); */
1844     FillInt64(addvar,0, (3 + 4 * 2));
1845     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1846     return code;
1847 }
1848
1849 static int
1850 SizeDumpVnode(struct iod *iodp, struct VnodeDiskObject *v, VolumeId volid,
1851               int vnodeNumber, int dumpEverything,
1852               struct volintSize *v_size)
1853 {
1854     int code = 0;
1855     afs_uint64 addvar;
1856
1857     if (!v || v->type == vNull)
1858         return code;
1859 /*     if (!code) code = DumpDouble(iodp, D_VNODE, vnodeNumber, v->uniquifier); */
1860     FillInt64(addvar,0, 9);
1861     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1862     if (!dumpEverything)
1863         return code;
1864 /*     if (!code)  code = DumpByte(iodp, 't',(byte)v->type); */
1865     FillInt64(addvar,0, 2);
1866     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1867 /*     if (!code) code = DumpShort(iodp, 'l', v->linkCount); /\* May not need this *\/ */
1868     FillInt64(addvar,0, 3);
1869     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1870 /*     if (!code) code = DumpInt32(iodp, 'v', v->dataVersion); */
1871     FillInt64(addvar,0, 5);
1872     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1873 /*     if (!code) code = DumpInt32(iodp, 'm', v->unixModifyTime); */
1874     FillInt64(addvar,0, 5);
1875     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1876 /*     if (!code) code = DumpInt32(iodp, 'a', v->author); */
1877     FillInt64(addvar,0, 5);
1878     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1879 /*     if (!code) code = DumpInt32(iodp, 'o', v->owner); */
1880     FillInt64(addvar,0, 5);
1881     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1882 /*     if (!code && v->group) code = DumpInt32(iodp, 'g', v->group);    /\* default group is 0 *\/ */
1883     if (v->group) {
1884         FillInt64(addvar,0, 5);
1885         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1886     }
1887 /*     if (!code) code = DumpShort(iodp, 'b', v->modeBits); */
1888     FillInt64(addvar,0, 3);
1889     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1890 /*     if (!code) code = DumpInt32(iodp, 'p', v->parent); */
1891     FillInt64(addvar,0, 5);
1892     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1893 /*     if (!code) code = DumpInt32(iodp, 's', v->serverModifyTime); */
1894     FillInt64(addvar,0, 5);
1895     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1896     if (v->type == vDirectory) {
1897 /*      acl_HtonACL(VVnodeDiskACL(v)); */
1898 /*      if (!code) code = DumpByteString(iodp, 'A', (byte *) VVnodeDiskACL(v), VAclDiskSize(v)); */
1899         FillInt64(addvar,0, (1 + VAclDiskSize(v)));
1900         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1901     }
1902
1903     if (VNDISK_GET_INO(v)) {
1904         VNDISK_GET_LEN(addvar, v);
1905         if (v->vn_length_hi)
1906             addvar += 9;
1907         else
1908             addvar += 5;
1909         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1910     }
1911     return code;
1912 }
1913
1914 /* A partial dump (no dump header) */
1915 static int
1916 SizeDumpPartial(struct iod *iodp, Volume * vp,
1917                 afs_int32 fromtime, int dumpAllDirs,
1918                 struct volintSize *v_size)
1919 {
1920     int code = 0;
1921     if (!code)
1922         code = SizeDumpVolumeHeader(iodp, vp, v_size);
1923     if (!code)
1924         code =
1925             SizeDumpVnodeIndex(iodp, vp, vLarge, fromtime, dumpAllDirs,
1926                                v_size);
1927     if (!code)
1928         code = SizeDumpVnodeIndex(iodp, vp, vSmall, fromtime, 0, v_size);
1929     return code;
1930 }
1931
1932 static int
1933 SizeDumpVnodeIndex(struct iod *iodp, Volume * vp, VnodeClass class,
1934                    afs_int32 fromtime, int forcedump,
1935                    struct volintSize *v_size)
1936 {
1937     int code = 0;
1938     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
1939     char buf[SIZEOF_LARGEDISKVNODE];
1940     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1941     StreamHandle_t *file;
1942     FdHandle_t *fdP;
1943     afs_sfsize_t size, nVnodes;
1944     int flag;
1945     int vnodeIndex;
1946
1947     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1948     opr_Assert(fdP != NULL);
1949     file = FDH_FDOPEN(fdP, "r+");
1950     opr_Assert(file != NULL);
1951     size = OS_SIZE(fdP->fd_fd);
1952     opr_Assert(size != -1);
1953     nVnodes = (size / vcp->diskSize) - 1;
1954     if (nVnodes > 0) {
1955         opr_Assert((nVnodes + 1) * vcp->diskSize == size);
1956         opr_Assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
1957     } else
1958         nVnodes = 0;
1959     for (vnodeIndex = 0;
1960          nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
1961          nVnodes--, vnodeIndex++) {
1962         flag = forcedump || (vnode->serverModifyTime >= fromtime);
1963         /* Note:  the >= test is very important since some old volumes may not have
1964          * a serverModifyTime.  For an epoch dump, this results in 0>=0 test, which
1965          * does dump the file! */
1966         if (!code)
1967             code =
1968                 SizeDumpVnode(iodp, vnode, V_id(vp),
1969                               bitNumberToVnodeNumber(vnodeIndex, class), flag,
1970                               v_size);
1971     }
1972     STREAM_CLOSE(file);
1973     FDH_CLOSE(fdP);
1974     return code;
1975 }