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