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