volser: update log formatting in dump and restore
[openafs.git] / src / volser / dumpstuff.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #include <ctype.h>
16
17 #include <afs/opr.h>
18 #include <rx/rx.h>
19 #include <rx/rx_queue.h>
20 #include <afs/afsint.h>
21 #include <afs/nfs.h>
22 #include <afs/errors.h>
23 #include <lock.h>
24 #include <lwp.h>
25 #include <afs/ihandle.h>
26 #include <afs/vnode.h>
27 #include <afs/volume.h>
28 #include <afs/partition.h>
29 #include <afs/daemon_com.h>
30 #include <afs/fssync.h>
31 #include <afs/acl.h>
32 #include <afs/com_err.h>
33 #include <afs/vol_prototypes.h>
34
35 #include "dump.h"
36 #include "volser.h"
37 #include "volint.h"
38 #include "dumpstuff.h"
39
40 #ifndef AFS_NT40_ENV
41 #ifdef O_LARGEFILE
42 #define afs_stat        stat64
43 #define afs_fstat       fstat64
44 #else /* !O_LARGEFILE */
45 #define afs_stat        stat
46 #define afs_fstat       fstat
47 #endif /* !O_LARGEFILE */
48 #endif /* !AFS_NT40_ENV */
49
50 /*@printflike@*/ extern void Log(const char *format, ...);
51
52 extern int DoLogging;
53 extern int DoPreserveVolumeStats;
54
55
56 /* Forward Declarations */
57 static int DumpDumpHeader(struct iod *iodp, Volume * vp,
58                           afs_int32 fromtime);
59 static int DumpPartial(struct iod *iodp, Volume * vp,
60                        afs_int32 fromtime, int dumpAllDirs);
61 static int DumpVnodeIndex(struct iod *iodp, Volume * vp,
62                           VnodeClass class, afs_int32 fromtime,
63                           int forcedump);
64 static int DumpVnode(struct iod *iodp, struct VnodeDiskObject *v,
65                      VolumeId volid, int vnodeNumber, int dumpEverything);
66 static int ReadDumpHeader(struct iod *iodp, struct DumpHeader *hp);
67 static int ReadVnodes(struct iod *iodp, Volume * vp, 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                          VolumeId volid, int vnodeNumber, int dumpEverything,
86                          struct volintSize *size);
87
88 #define MAX_SECTIONS    3
89
90 /* The TLV range must start above D_MAX */
91 #define MIN_TLV_TAG     21
92 #define MAX_TLV_TAG     0x60
93 #define MAX_STANDARD_TAG 0x7a
94 static afs_uint32 oldtags[MAX_SECTIONS][16];
95 int oldtagsInited = 0;
96
97 static void
98 RegisterTag(afs_int32 section, unsigned char tag)
99 {
100     afs_uint32 off = tag >> 5;
101     afs_uint32 mask = 1 << (tag & 0x1f);
102     oldtags[section][off] |= mask;
103 }
104
105 static void
106 initNonStandardTags(void)
107 {
108     RegisterTag(0, 'n');                /* volume name */
109     RegisterTag(0, 't');                /* fromtime, V_backupDate */
110     RegisterTag(1, 'A');                /* V_accessDate */
111     RegisterTag(1, 'C');                /* V_creationDate */
112     RegisterTag(1, 'D');                /* V_dayUseDate */
113     RegisterTag(1, 'E');                /* V_expirationDate */
114     RegisterTag(1, 'M');                /* nullstring (motd) */
115     RegisterTag(1, 'U');                /* V_updateDate */
116     RegisterTag(1, 'W');                /* V_weekUse */
117     RegisterTag(1, 'Z');                /* V_dayUse */
118     RegisterTag(1, 'O');                /* V_offlineMessage */
119     RegisterTag(1, 'b');                /* V_blessed */
120     RegisterTag(1, 'n');                /* V_name */
121     RegisterTag(1, 's');                /* V_inService */
122     RegisterTag(1, 't');                /* V_type */
123     RegisterTag(2, 'A');                /* VVnodeDiskACL */
124     RegisterTag(2, 'b');                /* modeBits */
125     RegisterTag(2, 'f');                /* small file */
126     RegisterTag(2, 'h');                /* large file */
127     RegisterTag(2, 'l');                /* linkcount */
128     RegisterTag(2, 't');                /* type */
129     oldtagsInited = 1;
130 }
131
132 static void
133 iod_Init(struct iod *iodp, struct rx_call *call)
134 {
135     iodp->call = call;
136     iodp->haveOldChar = 0;
137     iodp->ncalls = 1;
138     iodp->calls = (struct rx_call **)0;
139 }
140
141 static void
142 iod_InitMulti(struct iod *iodp, struct rx_call **calls, int ncalls,
143               int *codes)
144 {
145
146     iodp->calls = calls;
147     iodp->haveOldChar = 0;
148     iodp->ncalls = ncalls;
149     iodp->codes = codes;
150     iodp->call = (struct rx_call *)0;
151 }
152
153 /* N.B. iod_Read doesn't check for oldchar (see previous comment) */
154 #define iod_Read(iodp, buf, nbytes) rx_Read((iodp)->call, buf, nbytes)
155
156 /* For the single dump case, it's ok to just return the "bytes written"
157  * that rx_Write returns, since all the callers of iod_Write abort when
158  * the returned value is less than they expect.  For the multi dump case,
159  * I don't think we want half the replicas to go bad just because one
160  * connection timed out, but if they all time out, then we should give up.
161  */
162 static int
163 iod_Write(struct iod *iodp, char *buf, int nbytes)
164 {
165     int code, i;
166     int one_success = 0;
167
168     opr_Assert((iodp->call && iodp->ncalls == 1 && !iodp->calls)
169            || (!iodp->call && iodp->ncalls >= 1 && iodp->calls));
170
171     if (iodp->call) {
172         code = rx_Write(iodp->call, buf, nbytes);
173         return code;
174     }
175
176     for (i = 0; i < iodp->ncalls; i++) {
177         if (iodp->calls[i] && !iodp->codes[i]) {
178             code = rx_Write(iodp->calls[i], buf, nbytes);
179             if (code != nbytes) {       /* everything gets merged into a single error */
180                 iodp->codes[i] = VOLSERDUMPERROR;       /* but that's exactly what the */
181             } /* standard dump does, anyways */
182             else {
183                 one_success = TRUE;
184             }
185         }
186     }                           /* for all calls */
187
188     if (one_success)
189         return nbytes;
190     else
191         return 0;
192 }
193
194 static void
195 iod_ungetc(struct iod *iodp, int achar)
196 {
197     iodp->oldChar = achar;
198     iodp->haveOldChar = 1;
199 }
200
201 static int
202 iod_getc(struct iod *iodp)
203 {
204     unsigned char t;
205
206     if (iodp->haveOldChar) {
207         iodp->haveOldChar = 0;
208         return iodp->oldChar;
209     }
210     if (iod_Read(iodp, (char *) &t, 1) == 1)
211         return t;
212     return EOF;
213 }
214
215 static int
216 ReadShort(struct iod *iodp, unsigned short *sp)
217 {
218     int b1, b0;
219     b1 = iod_getc(iodp);
220     if (b1 == EOF)
221         return 0;
222     b0 = iod_getc(iodp);
223     if (b0 == EOF)
224         return 0;
225     *sp = (b1 << 8) | b0;
226     return 1;
227 }
228
229 static int
230 ReadInt32(struct iod *iodp, afs_uint32 * lp)
231 {
232     afs_uint32 b3, b2, b1, b0;
233     b3 = iod_getc(iodp);
234     if (b3 == EOF)
235         return 0;
236     b2 = iod_getc(iodp);
237     if (b2 == EOF)
238         return 0;
239     b1 = iod_getc(iodp);
240     if (b1 == EOF)
241         return 0;
242     b0 = iod_getc(iodp);
243     if (b0 == EOF)
244         return 0;
245     *lp = (((((b3 << 8) | b2) << 8) | b1) << 8) | b0;
246     return 1;
247 }
248
249 static void
250 ReadString(struct iod *iodp, char *to, int maxa)
251 {
252     int c;
253
254     *to = '\0';
255     if (maxa == 0)
256         return;
257
258     while (maxa--) {
259         if ((*to++ = c = iod_getc(iodp)) == 0 || c == EOF)
260             break;
261     }
262     if (to[-1]) {
263         while ((c = iod_getc(iodp)) && c != EOF);
264         to[-1] = '\0';
265     }
266 }
267
268 static 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     switch (V_type(vp)) {
1041     case readwriteVolume:
1042         dumpTimes[1] = V_updateDate(vp);        /* until last update */
1043         break;
1044     case readonlyVolume:
1045         dumpTimes[1] = V_creationDate(vp);      /* until clone was updated */
1046         break;
1047     case backupVolume:
1048         /* until backup was made */
1049         dumpTimes[1] = V_backupDate(vp) != 0 ? V_backupDate(vp) :
1050                                                V_creationDate(vp);
1051         break;
1052     default:
1053         code = EINVAL;
1054     }
1055     if (!code)
1056         code = DumpArrayInt32(iodp, 't', (afs_uint32 *) dumpTimes, 2);
1057     return code;
1058 }
1059
1060 static int
1061 DumpVnode(struct iod *iodp, struct VnodeDiskObject *v, VolumeId volid,
1062           int vnodeNumber, int dumpEverything)
1063 {
1064     int code = 0;
1065     IHandle_t *ihP;
1066     FdHandle_t *fdP;
1067     afs_ino_str_t stmp;
1068
1069     if (!v || v->type == vNull)
1070         return code;
1071     if (!code)
1072         code = DumpDouble(iodp, D_VNODE, vnodeNumber, v->uniquifier);
1073     if (!dumpEverything)
1074         return code;
1075     if (!code)
1076         code = DumpByte(iodp, 't', (byte) v->type);
1077     if (!code)
1078         code = DumpShort(iodp, 'l', v->linkCount);      /* May not need this */
1079     if (!code)
1080         code = DumpInt32(iodp, 'v', v->dataVersion);
1081     if (!code)
1082         code = DumpInt32(iodp, 'm', v->unixModifyTime);
1083     if (!code)
1084         code = DumpInt32(iodp, 'a', v->author);
1085     if (!code)
1086         code = DumpInt32(iodp, 'o', v->owner);
1087     if (!code && v->group)
1088         code = DumpInt32(iodp, 'g', v->group);  /* default group is 0 */
1089     if (!code)
1090         code = DumpShort(iodp, 'b', v->modeBits);
1091     if (!code)
1092         code = DumpInt32(iodp, 'p', v->parent);
1093     if (!code)
1094         code = DumpInt32(iodp, 's', v->serverModifyTime);
1095     if (v->type == vDirectory) {
1096         acl_HtonACL(VVnodeDiskACL(v));
1097         if (!code)
1098             code =
1099                 DumpByteString(iodp, 'A', (byte *) VVnodeDiskACL(v),
1100                                VAclDiskSize(v));
1101     }
1102     if (VNDISK_GET_INO(v)) {
1103         afs_sfsize_t indexlen, disklen;
1104         IH_INIT(ihP, iodp->device, iodp->parentId, VNDISK_GET_INO(v));
1105         fdP = IH_OPEN(ihP);
1106         if (fdP == NULL) {
1107             Log("1 Volser: DumpVnode: dump: Unable to open inode %s "
1108                 "for vnode %u (volume %" AFS_VOLID_FMT "); "
1109                 "not dumped, error %d\n",
1110                 PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber,
1111                 afs_printable_VolumeId_lu(volid), errno);
1112             IH_RELEASE(ihP);
1113             return VOLSERREAD_DUMPERROR;
1114         }
1115         VNDISK_GET_LEN(indexlen, v);
1116         disklen = FDH_SIZE(fdP);
1117         if (indexlen != disklen) {
1118             FDH_REALLYCLOSE(fdP);
1119             IH_RELEASE(ihP);
1120             Log("DumpVnode: volume %"AFS_VOLID_FMT" "
1121                 "vnode %lu has inconsistent length "
1122                 "(index %lu disk %lu); aborting dump\n",
1123                 afs_printable_VolumeId_lu(volid), (unsigned long)vnodeNumber,
1124                 (unsigned long)indexlen, (unsigned long)disklen);
1125             return VOLSERREAD_DUMPERROR;
1126         }
1127         code = DumpFile(iodp, vnodeNumber, fdP);
1128         FDH_CLOSE(fdP);
1129         IH_RELEASE(ihP);
1130     }
1131     return code;
1132 }
1133
1134
1135 int
1136 ProcessIndex(Volume * vp, VnodeClass class, afs_foff_t ** Bufp, int *sizep,
1137              int del)
1138 {
1139     int i, nVnodes, code;
1140     afs_foff_t offset;
1141     afs_foff_t *Buf;
1142     int cnt = 0;
1143     afs_sfsize_t size;
1144     StreamHandle_t *afile;
1145     FdHandle_t *fdP;
1146     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
1147     char buf[SIZEOF_LARGEDISKVNODE], zero[SIZEOF_LARGEDISKVNODE];
1148     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1149     afs_ino_str_t stmp;
1150
1151     memset(zero, 0, sizeof(zero));      /* zero out our proto-vnode */
1152     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1153     if (fdP == NULL)
1154         return -1;
1155     afile = FDH_FDOPEN(fdP, "r+");
1156     if (del) {
1157         int cnt1 = 0;
1158         Buf = *Bufp;
1159         for (i = 0; i < *sizep; i++) {
1160             if (Buf[i]) {
1161                 cnt++;
1162                 STREAM_ASEEK(afile, Buf[i]);
1163                 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
1164                 if (code == 1) {
1165                     if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
1166                         cnt1++;
1167                         if (DoLogging) {
1168                             Log("RestoreVolume %"AFS_VOLID_FMT" "
1169                                 "Cleanup: Removing old vnode=%u inode=%s "
1170                                 "size=unknown\n",
1171                                 afs_printable_VolumeId_lu(V_id(vp)),
1172                                 bitNumberToVnodeNumber(i, class),
1173                                 PrintInode(stmp, VNDISK_GET_INO(vnode)));
1174                         }
1175                         IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(vnode),
1176                                V_parentId(vp));
1177                         DOPOLL;
1178                     }
1179                     STREAM_ASEEK(afile, Buf[i]);
1180                     (void)STREAM_WRITE(zero, vcp->diskSize, 1, afile);  /* Zero it out */
1181                 }
1182                 Buf[i] = 0;
1183             }
1184         }
1185         if (DoLogging) {
1186             Log("RestoreVolume Cleanup: Removed %d inodes for volume %d\n",
1187                 cnt1, V_id(vp));
1188         }
1189         STREAM_FLUSH(afile);    /* ensure 0s are on the disk */
1190         OS_SYNC(afile->str_fd);
1191     } else {
1192         size = OS_SIZE(fdP->fd_fd);
1193         opr_Assert(size != -1);
1194         nVnodes =
1195             (size <=
1196              vcp->diskSize ? 0 : size - vcp->diskSize) >> vcp->logSize;
1197         if (nVnodes > 0) {
1198             Buf = calloc(nVnodes,  sizeof(afs_foff_t));
1199             if (Buf == NULL) {
1200                 STREAM_CLOSE(afile);
1201                 FDH_CLOSE(fdP);
1202                 return -1;
1203             }
1204             STREAM_ASEEK(afile, offset = vcp->diskSize);
1205             while (1) {
1206                 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
1207                 if (code != 1) {
1208                     break;
1209                 }
1210                 if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
1211                     Buf[(offset >> vcp->logSize) - 1] = offset;
1212                     cnt++;
1213                 }
1214                 offset += vcp->diskSize;
1215             }
1216             *Bufp = Buf;
1217             *sizep = nVnodes;
1218         }
1219     }
1220     STREAM_CLOSE(afile);
1221     FDH_CLOSE(fdP);
1222     return 0;
1223 }
1224
1225
1226 int
1227 RestoreVolume(struct rx_call *call, Volume * avp, int incremental,
1228               struct restoreCookie *cookie)
1229 {
1230     VolumeDiskData vol;
1231     struct DumpHeader header;
1232     afs_uint32 endMagic;
1233     Error error = 0, vupdate;
1234     Volume *vp;
1235     struct iod iod;
1236     struct iod *iodp = &iod;
1237     afs_foff_t *b1 = NULL, *b2 = NULL;
1238     int s1 = 0, s2 = 0, delo = 0, tdelo;
1239     int tag;
1240     VolumeDiskData saved_header;
1241
1242     iod_Init(iodp, call);
1243
1244     vp = avp;
1245
1246     if (DoPreserveVolumeStats) {
1247         CopyVolumeStats(&V_disk(vp), &saved_header);
1248     }
1249
1250     if (!ReadDumpHeader(iodp, &header)) {
1251         Log("1 Volser: RestoreVolume: Error reading header file for dump; aborted\n");
1252         return VOLSERREAD_DUMPERROR;
1253     }
1254     if (iod_getc(iodp) != D_VOLUMEHEADER) {
1255         Log("1 Volser: RestoreVolume: Volume header missing from dump; not restored\n");
1256         return VOLSERREAD_DUMPERROR;
1257     }
1258     if (ReadVolumeHeader(iodp, &vol) == VOLSERREAD_DUMPERROR)
1259         return VOLSERREAD_DUMPERROR;
1260
1261     if (!delo)
1262         delo = ProcessIndex(vp, vLarge, &b1, &s1, 0);
1263     if (!delo)
1264         delo = ProcessIndex(vp, vSmall, &b2, &s2, 0);
1265     if (delo < 0) {
1266         Log("1 Volser: RestoreVolume: ProcessIndex failed; not restored\n");
1267         error = VOLSERREAD_DUMPERROR;
1268         goto out;
1269     }
1270
1271     strncpy(vol.name, cookie->name, VOLSER_OLDMAXVOLNAME);
1272     vol.type = cookie->type;
1273     vol.cloneId = cookie->clone;
1274     vol.parentId = cookie->parent;
1275
1276     V_needsSalvaged(vp) = 0;
1277
1278     tdelo = delo;
1279     while (1) {
1280         if (ReadVnodes(iodp, vp, 0, b1, s1, b2, s2, tdelo)) {
1281             error = VOLSERREAD_DUMPERROR;
1282             goto clean;
1283         }
1284         tag = iod_getc(iodp);
1285         if (tag != D_VOLUMEHEADER)
1286             break;
1287
1288         if (ReadVolumeHeader(iodp, &vol) == VOLSERREAD_DUMPERROR) {
1289             error = VOLSERREAD_DUMPERROR;
1290             goto out;
1291         }
1292     }
1293     if (tag != D_DUMPEND || !ReadInt32(iodp, &endMagic)
1294         || endMagic != DUMPENDMAGIC) {
1295         Log("1 Volser: RestoreVolume: End of dump not found; restore aborted\n");
1296         error = VOLSERREAD_DUMPERROR;
1297         goto clean;
1298     }
1299
1300
1301     if (iod_getc(iodp) != EOF) {
1302         Log("1 Volser: RestoreVolume: Unrecognized postamble in dump; restore aborted\n");
1303         error = VOLSERREAD_DUMPERROR;
1304         goto clean;
1305     }
1306
1307     if (!delo) {
1308         delo = ProcessIndex(vp, vLarge, &b1, &s1, 1);
1309         if (!delo)
1310             delo = ProcessIndex(vp, vSmall, &b2, &s2, 1);
1311         if (delo < 0) {
1312             error = VOLSERREAD_DUMPERROR;
1313             goto clean;
1314         }
1315     }
1316
1317   clean:
1318     if (DoPreserveVolumeStats) {
1319         CopyVolumeStats(&saved_header, &vol);
1320     } else {
1321         ClearVolumeStats(&vol);
1322     }
1323     if (V_needsSalvaged(vp)) {
1324         /* needsSalvaged may have been set while we tried to write volume data.
1325          * prevent it from getting overwritten. */
1326         vol.needsSalvaged = V_needsSalvaged(vp);
1327     }
1328     CopyVolumeHeader(&vol, &V_disk(vp));
1329     V_destroyMe(vp) = 0;
1330     VUpdateVolume(&vupdate, vp);
1331     if (vupdate) {
1332         Log("1 Volser: RestoreVolume: Unable to rewrite volume header; restore aborted\n");
1333         error = VOLSERREAD_DUMPERROR;
1334         goto out;
1335     }
1336   out:
1337     /* Free the malloced space above */
1338     if (b1)
1339         free(b1);
1340     if (b2)
1341         free(b2);
1342     return error;
1343 }
1344
1345 static int
1346 ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
1347            afs_foff_t * Lbuf, afs_int32 s1, afs_foff_t * Sbuf, afs_int32 s2,
1348            afs_int32 delo)
1349 {
1350     afs_int32 vnodeNumber;
1351     char buf[SIZEOF_LARGEDISKVNODE];
1352     int tag;
1353     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1354     struct VnodeDiskObject oldvnode;
1355     int idx;
1356     VnodeClass class;
1357     struct VnodeClassInfo *vcp;
1358     IHandle_t *tmpH;
1359     FdHandle_t *fdP;
1360     Inode nearInode AFS_UNUSED;
1361     afs_int32 critical = 0;
1362
1363     tag = iod_getc(iodp);
1364     V_pref(vp, nearInode);
1365     while (tag == D_VNODE) {
1366         int haveStuff = 0;
1367         int saw_f = 0;
1368         memset(buf, 0, sizeof(buf));
1369         if (!ReadInt32(iodp, (afs_uint32 *) & vnodeNumber))
1370             break;
1371
1372         if (!ReadInt32(iodp, &vnode->uniquifier))
1373             return VOLSERREAD_DUMPERROR;
1374
1375         while ((tag = iod_getc(iodp)) > D_MAX && tag != EOF) {
1376             haveStuff = 1;
1377             if (critical)
1378                 critical--;
1379             switch (tag) {
1380             case 't':
1381                 vnode->type = (VnodeType) iod_getc(iodp);
1382                 break;
1383             case 'l':
1384                 {
1385                     unsigned short tlc;
1386                     if (!ReadShort(iodp, &tlc))
1387                         return VOLSERREAD_DUMPERROR;
1388                     vnode->linkCount = (signed int)tlc;
1389                 }
1390                 break;
1391             case 'v':
1392                 if (!ReadInt32(iodp, &vnode->dataVersion))
1393                     return VOLSERREAD_DUMPERROR;
1394                 break;
1395             case 'm':
1396                 if (!ReadInt32(iodp, &vnode->unixModifyTime))
1397                     return VOLSERREAD_DUMPERROR;
1398                 break;
1399             case 's':
1400                 if (!ReadInt32(iodp, &vnode->serverModifyTime))
1401                     return VOLSERREAD_DUMPERROR;
1402                 break;
1403             case 'a':
1404                 if (!ReadInt32(iodp, &vnode->author))
1405                     return VOLSERREAD_DUMPERROR;
1406                 break;
1407             case 'o':
1408                 if (!ReadInt32(iodp, &vnode->owner))
1409                     return VOLSERREAD_DUMPERROR;
1410                 break;
1411             case 'g':
1412                 if (!ReadInt32(iodp, (afs_uint32 *) & vnode->group))
1413                     return VOLSERREAD_DUMPERROR;
1414                 break;
1415             case 'b':{
1416                     unsigned short modeBits;
1417                     if (!ReadShort(iodp, &modeBits))
1418                         return VOLSERREAD_DUMPERROR;
1419                     vnode->modeBits = (unsigned int)modeBits;
1420                     break;
1421                 }
1422             case 'p':
1423                 if (!ReadInt32(iodp, &vnode->parent))
1424                     return VOLSERREAD_DUMPERROR;
1425                 break;
1426             case 'A':
1427                 ReadByteString(iodp, (byte *) VVnodeDiskACL(vnode),
1428                                VAclDiskSize(vnode));
1429                 acl_NtohACL(VVnodeDiskACL(vnode));
1430                 break;
1431             case 'h':
1432             case 'f':{
1433                     Inode ino;
1434                     Error error;
1435                     afs_fsize_t vnodeLength;
1436
1437                     if (saw_f) {
1438                         Log("Volser: ReadVnodes: warning: ignoring duplicate "
1439                             "file entries for vnode %lu in dump\n",
1440                             (unsigned long)vnodeNumber);
1441                         volser_WriteFile(vnodeNumber, iodp, NULL, tag, &error);
1442                         break;
1443                     }
1444                     saw_f = 1;
1445
1446                     tmpH =
1447                         IH_CREATE_INIT(V_linkHandle(vp), V_device(vp),
1448                                   VPartitionPath(V_partition(vp)), nearInode,
1449                                   V_parentId(vp), vnodeNumber,
1450                                   vnode->uniquifier, vnode->dataVersion);
1451                     if (!tmpH) {
1452                         Log("1 Volser: ReadVnodes: IH_CREATE: %s - restore aborted\n",
1453                             afs_error_message(errno));
1454                         V_needsSalvaged(vp) = 1;
1455                         return VOLSERREAD_DUMPERROR;
1456                     }
1457                     ino = tmpH->ih_ino;
1458                     nearInode = ino;
1459                     VNDISK_SET_INO(vnode, ino);
1460                     fdP = IH_OPEN(tmpH);
1461                     if (fdP == NULL) {
1462                         Log("1 Volser: ReadVnodes: IH_OPEN: %s - restore aborted\n",
1463                             afs_error_message(errno));
1464                         IH_RELEASE(tmpH);
1465                         V_needsSalvaged(vp) = 1;
1466                         return VOLSERREAD_DUMPERROR;
1467                     }
1468                     vnodeLength =
1469                         volser_WriteFile(vnodeNumber, iodp, fdP, tag, &error);
1470                     VNDISK_SET_LEN(vnode, vnodeLength);
1471                     FDH_REALLYCLOSE(fdP);
1472                     IH_RELEASE(tmpH);
1473                     if (error) {
1474                         Log("1 Volser: ReadVnodes: IDEC inode %llu\n",
1475                             (afs_uintmax_t) ino);
1476                         IH_DEC(V_linkHandle(vp), ino, V_parentId(vp));
1477                         V_needsSalvaged(vp) = 1;
1478                         return VOLSERREAD_DUMPERROR;
1479                     }
1480                     break;
1481                 }
1482             case 0x7e:
1483                 critical = 2;
1484                 break;
1485             default:
1486                 if (!HandleUnknownTag(iodp, tag, 2, critical))
1487                     return VOLSERREAD_DUMPERROR;
1488             }
1489         }
1490
1491         class = vnodeIdToClass(vnodeNumber);
1492         vcp = &VnodeClassInfo[class];
1493
1494         /* Mark this vnode as in this dump - so we don't delete it later */
1495         if (!delo) {
1496             idx = (vnodeIndexOffset(vcp, vnodeNumber) >> vcp->logSize) - 1;
1497             if (class == vLarge) {
1498                 if (Lbuf && (idx < s1))
1499                     Lbuf[idx] = 0;
1500             } else {
1501                 if (Sbuf && (idx < s2))
1502                     Sbuf[idx] = 0;
1503             }
1504         }
1505
1506         if (haveStuff) {
1507             FdHandle_t *fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1508             if (fdP == NULL) {
1509                 Log("1 Volser: ReadVnodes: Error opening vnode index: %s; restore aborted\n",
1510                     afs_error_message(errno));
1511                 V_needsSalvaged(vp) = 1;
1512                 return VOLSERREAD_DUMPERROR;
1513             }
1514             if (FDH_PREAD(fdP, &oldvnode, sizeof(oldvnode), vnodeIndexOffset(vcp, vnodeNumber)) ==
1515                 sizeof(oldvnode)) {
1516                 if (oldvnode.type != vNull && VNDISK_GET_INO(&oldvnode)) {
1517                     IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(&oldvnode),
1518                            V_parentId(vp));
1519                 }
1520             }
1521             vnode->vnodeMagic = vcp->magic;
1522             if (FDH_PWRITE(fdP, vnode, vcp->diskSize, vnodeIndexOffset(vcp, vnodeNumber)) != vcp->diskSize) {
1523                 Log("1 Volser: ReadVnodes: Error writing vnode index: %s; restore aborted\n",
1524                     afs_error_message(errno));
1525                 FDH_REALLYCLOSE(fdP);
1526                 V_needsSalvaged(vp) = 1;
1527                 return VOLSERREAD_DUMPERROR;
1528             }
1529             FDH_CLOSE(fdP);
1530         }
1531     }
1532     iod_ungetc(iodp, tag);
1533
1534     return 0;
1535 }
1536
1537
1538 /* called with disk file only.  Note that we don't have to worry about rx_Read
1539  * needing to read an ungetc'd character, since the ReadInt32 will have read
1540  * it instead.
1541  *
1542  * if handleP == NULL, don't write the file anywhere; just read and discard
1543  * the file contents
1544  */
1545 static afs_fsize_t
1546 volser_WriteFile(int vn, struct iod *iodp, FdHandle_t * handleP, int tag,
1547                  Error * status)
1548 {
1549     afs_int32 code;
1550     ssize_t nBytes;
1551     afs_fsize_t filesize;
1552     afs_fsize_t written = 0;
1553     size_t size = 8192;
1554     afs_fsize_t nbytes;
1555     unsigned char *p;
1556
1557
1558     *status = 0;
1559     {
1560         afs_uint32 filesize_high = 0L, filesize_low = 0L;
1561         if (tag == 'h') {
1562             if (!ReadInt32(iodp, &filesize_high)) {
1563                 *status = 1;
1564                 return 0;
1565             }
1566         }
1567         if (!ReadInt32(iodp, &filesize_low)) {
1568             *status = 1;
1569             return 0;
1570         }
1571         FillInt64(filesize, filesize_high, filesize_low);
1572     }
1573     p = malloc(size);
1574     if (p == NULL) {
1575         *status = 2;
1576         return (0);
1577     }
1578     for (nbytes = filesize; nbytes; nbytes -= size) {
1579         if (nbytes < size)
1580             size = nbytes;
1581
1582         if ((code = iod_Read(iodp, (char *) p, size)) != size) {
1583             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));
1584             *status = 3;
1585             break;
1586         }
1587         if (handleP) {
1588             nBytes = FDH_PWRITE(handleP, p, size, written);
1589             if (nBytes > 0)
1590                 written += nBytes;
1591             if (nBytes != size) {
1592                 Log("1 Volser: WriteFile: Error writing (%u) bytes to vnode %d; %s; restore aborted\n", (int)(nBytes & 0xffffffff), vn, afs_error_message(errno));
1593                 *status = 4;
1594                 break;
1595             }
1596         }
1597     }
1598     free(p);
1599     return (written);
1600 }
1601
1602 static int
1603 ReadDumpHeader(struct iod *iodp, struct DumpHeader *hp)
1604 {
1605     int tag;
1606     afs_uint32 beginMagic;
1607     afs_int32 critical = 0;
1608     if (iod_getc(iodp) != D_DUMPHEADER || !ReadInt32(iodp, &beginMagic)
1609         || !ReadInt32(iodp, (afs_uint32 *) & hp->version)
1610         || beginMagic != DUMPBEGINMAGIC)
1611         return 0;
1612     hp->volumeId = 0;
1613     hp->nDumpTimes = 0;
1614     while ((tag = iod_getc(iodp)) > D_MAX) {
1615         unsigned short arrayLength;
1616         int i;
1617         if (critical)
1618             critical--;
1619         switch (tag) {
1620         case 'v':
1621             if (!ReadInt32(iodp, &hp->volumeId))
1622                 return 0;
1623             break;
1624         case 'n':
1625             ReadString(iodp, hp->volumeName, sizeof(hp->volumeName));
1626             break;
1627         case 't':
1628             if (!ReadShort(iodp, &arrayLength))
1629                 return 0;
1630             hp->nDumpTimes = (arrayLength >> 1);
1631             for (i = 0; i < hp->nDumpTimes; i++)
1632                 if (!ReadInt32(iodp, (afs_uint32 *) & hp->dumpTimes[i].from)
1633                     || !ReadInt32(iodp, (afs_uint32 *) & hp->dumpTimes[i].to))
1634                     return 0;
1635             break;
1636         case 0x7e:
1637             critical = 2;
1638             break;
1639         default:
1640             if (!HandleUnknownTag(iodp, tag, 0, critical))
1641                 return VOLSERREAD_DUMPERROR;
1642         }
1643     }
1644     if (!hp->volumeId || !hp->nDumpTimes) {
1645         return 0;
1646     }
1647     iod_ungetc(iodp, tag);
1648     return 1;
1649 }
1650
1651
1652 /* ----- Below are the calls that calculate dump size ----- */
1653
1654 static int
1655 SizeDumpVolumeHeader(struct iod *iodp, Volume * vp,
1656                      struct volintSize *v_size)
1657 {
1658     int code = 0;
1659     static char nullString[1] = "";     /*The ``contents'' of motd */
1660     afs_uint64 addvar;
1661
1662 /*     if (!code) code = DumpTag(iodp, D_VOLUMEHEADER); */
1663     FillInt64(addvar,0, 1);
1664     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1665 /*     if (!code) {code = DumpInt32(iodp, 'i',V_id(vp));} */
1666     FillInt64(addvar,0, 5);
1667     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1668 /*     if (!code) code = DumpInt32(iodp, 'v',V_stamp(vp).version); */
1669     FillInt64(addvar,0, 5);
1670     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1671 /*     if (!code) code = DumpString(iodp, 'n',V_name(vp)); */
1672     FillInt64(addvar,0, (2 + strlen(V_name(vp))));
1673     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1674 /*     if (!code) code = DumpBool(iodp, 's',V_inService(vp)); */
1675     FillInt64(addvar,0, 2);
1676     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1677 /*     if (!code) code = DumpBool(iodp, 'b',V_blessed(vp)); */
1678     FillInt64(addvar,0, 2);
1679     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1680 /*     if (!code) code = DumpInt32(iodp, 'u',V_uniquifier(vp)); */
1681     FillInt64(addvar,0, 5);
1682     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1683 /*     if (!code) code = DumpByte(iodp, 't',(byte)V_type(vp)); */
1684     FillInt64(addvar,0, 2);
1685     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1686 /*     if (!code){ code = DumpInt32(iodp, 'p',V_parentId(vp));} */
1687     FillInt64(addvar,0, 5);
1688     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1689 /*     if (!code) code = DumpInt32(iodp, 'c',V_cloneId(vp)); */
1690     FillInt64(addvar,0, 5);
1691     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1692 /*     if (!code) code = DumpInt32(iodp, 'q',V_maxquota(vp)); */
1693     FillInt64(addvar,0, 5);
1694     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1695 /*     if (!code) code = DumpInt32(iodp, 'm',V_minquota(vp)); */
1696     FillInt64(addvar,0, 5);
1697     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1698 /*     if (!code) code = DumpInt32(iodp, 'd',V_diskused(vp)); */
1699     FillInt64(addvar,0, 5);
1700     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1701 /*     if (!code) code = DumpInt32(iodp, 'f',V_filecount(vp)); */
1702     FillInt64(addvar,0, 5);
1703     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1704 /*     if (!code) code = DumpInt32(iodp, 'a', V_accountNumber(vp)); */
1705     FillInt64(addvar,0, 5);
1706     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1707 /*     if (!code) code = DumpInt32(iodp, 'o', V_owner(vp)); */
1708     FillInt64(addvar,0, 5);
1709     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1710 /*     if (!code) code = DumpInt32(iodp, 'C',V_creationDate(vp));       /\* Rw volume creation date *\/ */
1711     FillInt64(addvar,0, 5);
1712     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1713 /*     if (!code) code = DumpInt32(iodp, 'A',V_accessDate(vp)); */
1714     FillInt64(addvar,0, 5);
1715     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1716 /*     if (!code) code = DumpInt32(iodp, 'U',V_updateDate(vp)); */
1717     FillInt64(addvar,0, 5);
1718     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1719 /*     if (!code) code = DumpInt32(iodp, 'E',V_expirationDate(vp)); */
1720     FillInt64(addvar,0, 5);
1721     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1722 /*     if (!code) code = DumpInt32(iodp, 'B',V_backupDate(vp));         /\* Rw volume backup clone date *\/ */
1723     FillInt64(addvar,0, 5);
1724     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1725 /*     if (!code) code = DumpString(iodp, 'O',V_offlineMessage(vp)); */
1726     FillInt64(addvar,0, (2 + strlen(V_offlineMessage(vp))));
1727     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1728 /*     /\* */
1729 /*      * We do NOT dump the detailed volume statistics residing in the old */
1730 /*      * motd field, since we cannot tell from the info in a dump whether */
1731 /*      * statistics data has been put there.  Instead, we dump a null string, */
1732 /*      * just as if that was what the motd contained. */
1733 /*      *\/ */
1734 /*     if (!code) code = DumpString(iodp, 'M', nullString); */
1735     FillInt64(addvar,0, (2 + strlen(nullString)));
1736     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1737 /*     if (!code) code = DumpArrayInt32(iodp, 'W', (afs_uint32 *)V_weekUse(vp), sizeof(V_weekUse(vp))/sizeof(V_weekUse(vp)[0])); */
1738     FillInt64(addvar,0, (3 + 4 * (sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]))));
1739     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1740 /*     if (!code) code = DumpInt32(iodp, 'D', V_dayUseDate(vp)); */
1741     FillInt64(addvar,0, 5);
1742     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1743 /*     if (!code) code = DumpInt32(iodp, 'Z', V_dayUse(vp)); */
1744     FillInt64(addvar,0, 5);
1745     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1746     return code;
1747 }
1748
1749 static int
1750 SizeDumpEnd(struct iod *iodp, struct volintSize *v_size)
1751 {
1752     int code = 0;
1753     afs_uint64 addvar;
1754     FillInt64(addvar,0, 5);
1755     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1756     return code;
1757 }
1758
1759 int
1760 SizeDumpVolume(struct rx_call *call, Volume * vp,
1761                afs_int32 fromtime, int dumpAllDirs,
1762                struct volintSize *v_size)
1763 {
1764     int code = 0;
1765     struct iod *iodp = (struct iod *)0;
1766 /*    iod_Init(iodp, call); */
1767
1768     if (!code)
1769         code = SizeDumpDumpHeader(iodp, vp, fromtime, v_size);
1770     if (!code)
1771         code = SizeDumpPartial(iodp, vp, fromtime, dumpAllDirs, v_size);
1772     if (!code)
1773         code = SizeDumpEnd(iodp, v_size);
1774
1775     return code;
1776 }
1777
1778 static int
1779 SizeDumpDumpHeader(struct iod *iodp, Volume * vp,
1780                    afs_int32 fromtime, struct volintSize *v_size)
1781 {
1782     int code = 0;
1783 /*    int UseLatestReadOnlyClone = 1; */
1784 /*    afs_int32 dumpTimes[2]; */
1785     afs_uint64 addvar;
1786 /*    iodp->device = vp->device; */
1787 /*    iodp->parentId = V_parentId(vp); */
1788 /*    iodp->dumpPartition = vp->partition; */
1789
1790     ZeroInt64(v_size->dump_size);       /* initialize the size */
1791 /*     if (!code) code = DumpDouble(iodp, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION); */
1792     FillInt64(addvar,0, 9);
1793     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1794 /*     if (!code) code = DumpInt32(iodp, 'v', UseLatestReadOnlyClone? V_id(vp): V_parentId(vp)); */
1795     FillInt64(addvar,0, 5);
1796     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1797 /*     if (!code) code = DumpString(iodp, 'n',V_name(vp)); */
1798     FillInt64(addvar,0, (2 + strlen(V_name(vp))));
1799     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1800 /*     dumpTimes[0] = fromtime; */
1801 /*     dumpTimes[1] = V_backupDate(vp); /\* Until the time the clone was made *\/ */
1802 /*     if (!code) code = DumpArrayInt32(iodp, 't', (afs_uint32 *)dumpTimes, 2); */
1803     FillInt64(addvar,0, (3 + 4 * 2));
1804     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1805     return code;
1806 }
1807
1808 static int
1809 SizeDumpVnode(struct iod *iodp, struct VnodeDiskObject *v, VolumeId volid,
1810               int vnodeNumber, int dumpEverything,
1811               struct volintSize *v_size)
1812 {
1813     int code = 0;
1814     afs_uint64 addvar;
1815
1816     if (!v || v->type == vNull)
1817         return code;
1818 /*     if (!code) code = DumpDouble(iodp, D_VNODE, vnodeNumber, v->uniquifier); */
1819     FillInt64(addvar,0, 9);
1820     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1821     if (!dumpEverything)
1822         return code;
1823 /*     if (!code)  code = DumpByte(iodp, 't',(byte)v->type); */
1824     FillInt64(addvar,0, 2);
1825     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1826 /*     if (!code) code = DumpShort(iodp, 'l', v->linkCount); /\* May not need this *\/ */
1827     FillInt64(addvar,0, 3);
1828     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1829 /*     if (!code) code = DumpInt32(iodp, 'v', v->dataVersion); */
1830     FillInt64(addvar,0, 5);
1831     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1832 /*     if (!code) code = DumpInt32(iodp, 'm', v->unixModifyTime); */
1833     FillInt64(addvar,0, 5);
1834     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1835 /*     if (!code) code = DumpInt32(iodp, 'a', v->author); */
1836     FillInt64(addvar,0, 5);
1837     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1838 /*     if (!code) code = DumpInt32(iodp, 'o', v->owner); */
1839     FillInt64(addvar,0, 5);
1840     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1841 /*     if (!code && v->group) code = DumpInt32(iodp, 'g', v->group);    /\* default group is 0 *\/ */
1842     if (v->group) {
1843         FillInt64(addvar,0, 5);
1844         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1845     }
1846 /*     if (!code) code = DumpShort(iodp, 'b', v->modeBits); */
1847     FillInt64(addvar,0, 3);
1848     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1849 /*     if (!code) code = DumpInt32(iodp, 'p', v->parent); */
1850     FillInt64(addvar,0, 5);
1851     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1852 /*     if (!code) code = DumpInt32(iodp, 's', v->serverModifyTime); */
1853     FillInt64(addvar,0, 5);
1854     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1855     if (v->type == vDirectory) {
1856 /*      acl_HtonACL(VVnodeDiskACL(v)); */
1857 /*      if (!code) code = DumpByteString(iodp, 'A', (byte *) VVnodeDiskACL(v), VAclDiskSize(v)); */
1858         FillInt64(addvar,0, (1 + VAclDiskSize(v)));
1859         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1860     }
1861
1862     if (VNDISK_GET_INO(v)) {
1863         VNDISK_GET_LEN(addvar, v);
1864         if (v->vn_length_hi)
1865             addvar += 9;
1866         else
1867             addvar += 5;
1868         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1869     }
1870     return code;
1871 }
1872
1873 /* A partial dump (no dump header) */
1874 static int
1875 SizeDumpPartial(struct iod *iodp, Volume * vp,
1876                 afs_int32 fromtime, int dumpAllDirs,
1877                 struct volintSize *v_size)
1878 {
1879     int code = 0;
1880     if (!code)
1881         code = SizeDumpVolumeHeader(iodp, vp, v_size);
1882     if (!code)
1883         code =
1884             SizeDumpVnodeIndex(iodp, vp, vLarge, fromtime, dumpAllDirs,
1885                                v_size);
1886     if (!code)
1887         code = SizeDumpVnodeIndex(iodp, vp, vSmall, fromtime, 0, v_size);
1888     return code;
1889 }
1890
1891 static int
1892 SizeDumpVnodeIndex(struct iod *iodp, Volume * vp, VnodeClass class,
1893                    afs_int32 fromtime, int forcedump,
1894                    struct volintSize *v_size)
1895 {
1896     int code = 0;
1897     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
1898     char buf[SIZEOF_LARGEDISKVNODE];
1899     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1900     StreamHandle_t *file;
1901     FdHandle_t *fdP;
1902     afs_sfsize_t size, nVnodes;
1903     int flag;
1904     int vnodeIndex;
1905
1906     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1907     opr_Assert(fdP != NULL);
1908     file = FDH_FDOPEN(fdP, "r+");
1909     opr_Assert(file != NULL);
1910     size = OS_SIZE(fdP->fd_fd);
1911     opr_Assert(size != -1);
1912     nVnodes = (size / vcp->diskSize) - 1;
1913     if (nVnodes > 0) {
1914         opr_Assert((nVnodes + 1) * vcp->diskSize == size);
1915         opr_Assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
1916     } else
1917         nVnodes = 0;
1918     for (vnodeIndex = 0;
1919          nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
1920          nVnodes--, vnodeIndex++) {
1921         flag = forcedump || (vnode->serverModifyTime >= fromtime);
1922         /* Note:  the >= test is very important since some old volumes may not have
1923          * a serverModifyTime.  For an epoch dump, this results in 0>=0 test, which
1924          * does dump the file! */
1925         if (!code)
1926             code =
1927                 SizeDumpVnode(iodp, vnode, V_id(vp),
1928                               bitNumberToVnodeNumber(vnodeIndex, class), flag,
1929                               v_size);
1930     }
1931     STREAM_CLOSE(file);
1932     FDH_CLOSE(fdP);
1933     return code;
1934 }