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