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