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