Fix warnings in new dumptag code
[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(register struct iod *iodp, register Volume * vp,
72                           afs_int32 fromtime);
73 static int DumpPartial(register struct iod *iodp, register Volume * vp,
74                        afs_int32 fromtime, int dumpAllDirs);
75 static int DumpVnodeIndex(register struct iod *iodp, Volume * vp,
76                           VnodeClass class, afs_int32 fromtime,
77                           int forcedump);
78 static int DumpVnode(register struct iod *iodp, struct VnodeDiskObject *v,
79                      int volid, int vnodeNumber, int dumpEverything);
80 static int ReadDumpHeader(register struct iod *iodp, struct DumpHeader *hp);
81 static int ReadVnodes(register 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(register struct iod *iodp, register Volume * vp,
89                               afs_int32 fromtime,
90                               register struct volintSize *size);
91 static int SizeDumpPartial(register struct iod *iodp, register Volume * vp,
92                            afs_int32 fromtime, int dumpAllDirs,
93                            register struct volintSize *size);
94 static int SizeDumpVnodeIndex(register struct iod *iodp, Volume * vp,
95                               VnodeClass class, afs_int32 fromtime,
96                               int forcedump,
97                               register struct volintSize *size);
98 static int SizeDumpVnode(register struct iod *iodp, struct VnodeDiskObject *v,
99                          int volid, int vnodeNumber, int dumpEverything,
100                          register 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(register struct iod *iodp, register 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(register 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(register struct iod *iodp, register unsigned short *sp)
229 {
230     register 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(register struct iod *iodp, afs_uint32 * lp)
243 {
244     afs_uint32 register 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(register struct iod *iodp, register char *to, register int maxa)
263 {
264     register 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(register struct iod *iodp, register byte * to,
282                register 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(register 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(register 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(register struct iod *iodp, VolumeDiskData * vol)
390 {
391     register 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(register struct iod *iodp, register 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(register struct iod *iodp, char tag, byte value)
540 {
541     char tbuffer[2];
542     register 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(register struct iod *iodp, char tag, register afs_uint32 value1,
553            register afs_uint32 value2)
554 {
555     char tbuffer[9];
556     register 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(register struct iod *iodp, char tag, register afs_uint32 value)
565 {
566     char tbuffer[5];
567     register 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(register struct iod *iodp, char tag,
575                register afs_uint32 * array, register int nelem)
576 {
577     char tbuffer[4];
578     register afs_uint32 v;
579     int code = 0;
580     register 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(register struct iod *iodp, char tag, unsigned int value)
600 {
601     char tbuffer[3];
602     register 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(register struct iod *iodp, char tag, unsigned int value)
611 {
612     char tbuffer[2];
613     register 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(register struct iod *iodp, char tag, register char *s)
621 {
622     register 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(register struct iod *iodp, char tag, register byte * bs,
636                register 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(register 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_FUNCTION
671 static afs_int32
672 DumpStandardTagLen(register 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 n, nbytes, howMany, howBig;
712     afs_foff_t lcode = 0;
713     byte *p;
714 #ifndef AFS_NT40_ENV
715     struct afs_stat status;
716 #endif
717     afs_sfsize_t size;
718 #ifdef  AFS_AIX_ENV
719 #include <sys/statfs.h>
720 #if defined(AFS_AIX52_ENV) && defined(AFS_LARGEFILE_ENV)
721     struct statfs64 tstatfs;
722 #else /* !AFS_AIX52_ENV || !AFS_LARGEFILE_ENV */
723     struct statfs tstatfs;
724 #endif /* !AFS_AIX52_ENV || !AFS_LARGEFILE_ENV */
725     int statfs_code;
726 #endif
727
728 #ifdef AFS_NT40_ENV
729     howBig = _filelength(handleP->fd_fd);
730     howMany = 4096;
731
732 #else
733     afs_fstat(handleP->fd_fd, &status);
734     howBig = status.st_size;
735
736 #ifdef  AFS_AIX_ENV
737     /* Unfortunately in AIX valuable fields such as st_blksize are 
738      * gone from the stat structure.
739      */
740 #if defined(AFS_AIX52_ENV) && defined(AFS_LARGEFILE_ENV)
741     statfs_code = fstatfs64(handleP->fd_fd, &tstatfs);
742 #else /* !AFS_AIX52_ENV || !AFS_LARGEFILE_ENV */
743     statfs_code = fstatfs(handleP->fd_fd, &tstatfs);
744 #endif /* !AFS_AIX52_ENV || !AFS_LARGEFILE_ENV */
745     if (statfs_code != 0) {
746         Log("DumpFile: fstatfs returned error code %d on descriptor %d\n", errno, handleP->fd_fd);
747         return VOLSERDUMPERROR;
748     }
749     howMany = (afs_sfsize_t) tstatfs.f_bsize;
750 #else
751     howMany = status.st_blksize;
752 #endif /* AFS_AIX_ENV */
753 #endif /* AFS_NT40_ENV */
754
755
756     size = FDH_SIZE(handleP);
757 #ifdef AFS_LARGEFILE_ENV
758     {
759         afs_uint32 hi, lo;
760         SplitInt64(size, hi, lo);
761         if (hi == 0L) {
762             code = DumpInt32(iodp, 'f', lo);
763         } else {
764             code = DumpDouble(iodp, 'h', hi, lo);
765         }
766     }
767 #else /* !AFS_LARGEFILE_ENV */
768     code = DumpInt32(iodp, 'f', size);
769 #endif /* !AFS_LARGEFILE_ENV */
770     if (code) {
771         return VOLSERDUMPERROR;
772     }
773
774     p = (unsigned char *)malloc((size_t)howMany);
775     if (!p) {
776         Log("1 Volser: DumpFile: not enough memory to allocate %u bytes\n", howMany);
777         return VOLSERDUMPERROR;
778     }
779
780     for (nbytes = size; (nbytes && !error); nbytes -= howMany) {
781         if (nbytes < howMany)
782             howMany = nbytes;
783
784         /* Read the data - unless we know we can't */
785         n = (lcode ? 0 : FDH_READ(handleP, p, (size_t)howMany));
786
787         /* If read any good data and we null padded previously, log the
788          * amount that we had null padded.
789          */
790         if ((n > 0) && pad) {
791             Log("1 Volser: DumpFile: Null padding file %d bytes at offset %u\n", pad, offset);
792             pad = 0;
793         }
794
795         /* If didn't read enough data, null padd the rest of the buffer. This
796          * can happen if, for instance, the media has some bad spots. We don't
797          * want to quit the dump, so we start null padding.
798          */
799         if (n < howMany) {
800             /* Record the read error */
801             if (n < 0) {
802                 n = 0;
803                 Log("1 Volser: DumpFile: Error %d reading inode %s for vnode %d\n", errno, PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
804             } else if (!pad) {
805                 Log("1 Volser: DumpFile: Error reading inode %s for vnode %d\n", PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
806             }
807
808             /* Pad the rest of the buffer with zeros. Remember offset we started 
809              * padding. Keep total tally of padding.
810              */
811             memset(p + n, 0, howMany - n);
812             if (!pad)
813                 offset = (howBig - nbytes) + n;
814             pad += (howMany - n);
815
816             /* Now seek over the data we could not get. An error here means we
817              * can't do the next read.
818              */
819             lcode = FDH_SEEK(handleP, (size_t)((size - nbytes) + howMany), SEEK_SET);
820             if (lcode != ((size - nbytes) + howMany)) {
821                 if (lcode < 0) {
822                     Log("1 Volser: DumpFile: Error %d seeking in inode %s for vnode %d\n", errno, PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
823                 } else {
824                     Log("1 Volser: DumpFile: Error seeking in inode %s for vnode %d\n", PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
825                     lcode = -1;
826                 }
827             } else {
828                 lcode = 0;
829             }
830         }
831
832         /* Now write the data out */
833         if (iod_Write(iodp, (char *)p, (size_t)howMany) != howMany)
834             error = VOLSERDUMPERROR;
835 #ifndef AFS_PTHREAD_ENV
836         IOMGR_Poll();
837 #endif
838     }
839
840     if (pad) {                  /* Any padding we hadn't reported yet */
841         Log("1 Volser: DumpFile: Null padding file: %d bytes at offset %u\n",
842             pad, offset);
843     }
844
845     free(p);
846     return error;
847 }
848
849 static int
850 DumpVolumeHeader(register struct iod *iodp, register Volume * vp)
851 {
852     int code = 0;
853     static char nullString[1] = "";     /*The ``contents'' of motd */
854
855     if (!code)
856         code = DumpTag(iodp, D_VOLUMEHEADER);
857     if (!code) {
858         code = DumpInt32(iodp, 'i', V_id(vp));
859     }
860     if (!code)
861         code = DumpInt32(iodp, 'v', V_stamp(vp).version);
862     if (!code)
863         code = DumpString(iodp, 'n', V_name(vp));
864     if (!code)
865         code = DumpBool(iodp, 's', V_inService(vp));
866     if (!code)
867         code = DumpBool(iodp, 'b', V_blessed(vp));
868     if (!code)
869         code = DumpInt32(iodp, 'u', V_uniquifier(vp));
870     if (!code)
871         code = DumpByte(iodp, 't', (byte) V_type(vp));
872     if (!code) {
873         code = DumpInt32(iodp, 'p', V_parentId(vp));
874     }
875     if (!code)
876         code = DumpInt32(iodp, 'c', V_cloneId(vp));
877     if (!code)
878         code = DumpInt32(iodp, 'q', V_maxquota(vp));
879     if (!code)
880         code = DumpInt32(iodp, 'm', V_minquota(vp));
881     if (!code)
882         code = DumpInt32(iodp, 'd', V_diskused(vp));
883     if (!code)
884         code = DumpInt32(iodp, 'f', V_filecount(vp));
885     if (!code)
886         code = DumpInt32(iodp, 'a', V_accountNumber(vp));
887     if (!code)
888         code = DumpInt32(iodp, 'o', V_owner(vp));
889     if (!code)
890         code = DumpInt32(iodp, 'C', V_creationDate(vp));        /* Rw volume creation date */
891     if (!code)
892         code = DumpInt32(iodp, 'A', V_accessDate(vp));
893     if (!code)
894         code = DumpInt32(iodp, 'U', V_updateDate(vp));
895     if (!code)
896         code = DumpInt32(iodp, 'E', V_expirationDate(vp));
897     if (!code)
898         code = DumpInt32(iodp, 'B', V_backupDate(vp));  /* Rw volume backup clone date */
899     if (!code)
900         code = DumpString(iodp, 'O', V_offlineMessage(vp));
901     /*
902      * We do NOT dump the detailed volume statistics residing in the old
903      * motd field, since we cannot tell from the info in a dump whether
904      * statistics data has been put there.  Instead, we dump a null string,
905      * just as if that was what the motd contained.
906      */
907     if (!code)
908         code = DumpString(iodp, 'M', nullString);
909     if (!code)
910         code =
911             DumpArrayInt32(iodp, 'W', (afs_uint32 *) V_weekUse(vp),
912                            sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]));
913     if (!code)
914         code = DumpInt32(iodp, 'D', V_dayUseDate(vp));
915     if (!code)
916         code = DumpInt32(iodp, 'Z', V_dayUse(vp));
917     return code;
918 }
919
920 static int
921 DumpEnd(register struct iod *iodp)
922 {
923     return (DumpInt32(iodp, D_DUMPEND, DUMPENDMAGIC));
924 }
925
926 /* Guts of the dump code */
927
928 /* Dump a whole volume */
929 int
930 DumpVolume(register struct rx_call *call, register Volume * vp,
931            afs_int32 fromtime, int dumpAllDirs)
932 {
933     struct iod iod;
934     int code = 0;
935     register struct iod *iodp = &iod;
936     iod_Init(iodp, call);
937
938     if (!code)
939         code = DumpDumpHeader(iodp, vp, fromtime);
940
941     if (!code)
942         code = DumpPartial(iodp, vp, fromtime, dumpAllDirs);
943
944 /* hack follows.  Errors should be handled quite differently in this version of dump than they used to be.*/
945     if (rx_Error(iodp->call)) {
946         Log("1 Volser: DumpVolume: Rx call failed during dump, error %d\n",
947             rx_Error(iodp->call));
948         return VOLSERDUMPERROR;
949     }
950     if (!code)
951         code = DumpEnd(iodp);
952
953     return code;
954 }
955
956 /* Dump a volume to multiple places*/
957 int
958 DumpVolMulti(struct rx_call **calls, int ncalls, Volume * vp,
959              afs_int32 fromtime, int dumpAllDirs, int *codes)
960 {
961     struct iod iod;
962     int code = 0;
963     iod_InitMulti(&iod, calls, ncalls, codes);
964
965     if (!code)
966         code = DumpDumpHeader(&iod, vp, fromtime);
967     if (!code)
968         code = DumpPartial(&iod, vp, fromtime, dumpAllDirs);
969     if (!code)
970         code = DumpEnd(&iod);
971     return code;
972 }
973
974 /* A partial dump (no dump header) */
975 static int
976 DumpPartial(register struct iod *iodp, register Volume * vp,
977             afs_int32 fromtime, int dumpAllDirs)
978 {
979     int code = 0;
980     if (!code)
981         code = DumpVolumeHeader(iodp, vp);
982     if (!code)
983         code = DumpVnodeIndex(iodp, vp, vLarge, fromtime, dumpAllDirs);
984     if (!code)
985         code = DumpVnodeIndex(iodp, vp, vSmall, fromtime, 0);
986     return code;
987 }
988
989 static int
990 DumpVnodeIndex(register struct iod *iodp, Volume * vp, VnodeClass class,
991                afs_int32 fromtime, int forcedump)
992 {
993     register int code = 0;
994     register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
995     char buf[SIZEOF_LARGEDISKVNODE];
996     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
997     StreamHandle_t *file;
998     FdHandle_t *fdP;
999     int size;
1000     int flag;
1001     register int vnodeIndex, nVnodes;
1002
1003     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1004     assert(fdP != NULL);
1005     file = FDH_FDOPEN(fdP, "r+");
1006     assert(file != NULL);
1007     size = OS_SIZE(fdP->fd_fd);
1008     assert(size != -1);
1009     nVnodes = (size / vcp->diskSize) - 1;
1010     if (nVnodes > 0) {
1011         assert((nVnodes + 1) * vcp->diskSize == size);
1012         assert(STREAM_SEEK(file, vcp->diskSize, 0) == 0);
1013     } else
1014         nVnodes = 0;
1015     for (vnodeIndex = 0;
1016          nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
1017          nVnodes--, vnodeIndex++) {
1018         flag = forcedump || (vnode->serverModifyTime >= fromtime);
1019         /* Note:  the >= test is very important since some old volumes may not have
1020          * a serverModifyTime.  For an epoch dump, this results in 0>=0 test, which
1021          * does dump the file! */
1022         if (!code)
1023             code =
1024                 DumpVnode(iodp, vnode, V_id(vp),
1025                           bitNumberToVnodeNumber(vnodeIndex, class), flag);
1026 #ifndef AFS_PTHREAD_ENV
1027         if (!flag)
1028             IOMGR_Poll();       /* if we dont' xfr data, but scan instead, could lose conn */
1029 #endif
1030     }
1031     STREAM_CLOSE(file);
1032     FDH_CLOSE(fdP);
1033     return code;
1034 }
1035
1036 static int
1037 DumpDumpHeader(register struct iod *iodp, register Volume * vp,
1038                afs_int32 fromtime)
1039 {
1040     int code = 0;
1041     int UseLatestReadOnlyClone = 1;
1042     afs_int32 dumpTimes[2];
1043     iodp->device = vp->device;
1044     iodp->parentId = V_parentId(vp);
1045     iodp->dumpPartition = vp->partition;
1046     if (!code)
1047         code = DumpDouble(iodp, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
1048     if (!code)
1049         code =
1050             DumpInt32(iodp, 'v',
1051                       UseLatestReadOnlyClone ? V_id(vp) : V_parentId(vp));
1052     if (!code)
1053         code = DumpString(iodp, 'n', V_name(vp));
1054     dumpTimes[0] = fromtime;
1055     dumpTimes[1] = V_backupDate(vp);    /* Until the time the clone was made */
1056     if (!code)
1057         code = DumpArrayInt32(iodp, 't', (afs_uint32 *) dumpTimes, 2);
1058     return code;
1059 }
1060
1061 static int
1062 DumpVnode(register struct iod *iodp, struct VnodeDiskObject *v, int volid,
1063           int vnodeNumber, int dumpEverything)
1064 {
1065     int code = 0;
1066     IHandle_t *ihP;
1067     FdHandle_t *fdP;
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         IH_INIT(ihP, iodp->device, iodp->parentId, VNDISK_GET_INO(v));
1104         fdP = IH_OPEN(ihP);
1105         if (fdP == NULL) {
1106             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);
1107             IH_RELEASE(ihP);
1108             return VOLSERREAD_DUMPERROR;
1109         }
1110         code = DumpFile(iodp, vnodeNumber, fdP);
1111         FDH_CLOSE(fdP);
1112         IH_RELEASE(ihP);
1113     }
1114     return code;
1115 }
1116
1117
1118 int
1119 ProcessIndex(Volume * vp, VnodeClass class, afs_int32 ** Bufp, int *sizep,
1120              int del)
1121 {
1122     int i, nVnodes, offset, code;
1123     afs_int32 *Buf;
1124     int cnt = 0;
1125     int size;
1126     StreamHandle_t *afile;
1127     FdHandle_t *fdP;
1128     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
1129     char buf[SIZEOF_LARGEDISKVNODE], zero[SIZEOF_LARGEDISKVNODE];
1130     register struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1131
1132     memset(zero, 0, sizeof(zero));      /* zero out our proto-vnode */
1133     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1134     if (fdP == NULL)
1135         return -1;
1136     afile = FDH_FDOPEN(fdP, "r+");
1137     if (del) {
1138         int cnt1 = 0;
1139         Buf = *Bufp;
1140         for (i = 0; i < *sizep; i++) {
1141             if (Buf[i]) {
1142                 cnt++;
1143                 STREAM_SEEK(afile, Buf[i], 0);
1144                 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
1145                 if (code == 1) {
1146                     if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
1147                         cnt1++;
1148                         if (DoLogging) {
1149                             Log("RestoreVolume %u Cleanup: Removing old vnode=%u inode=%llu size=unknown\n", 
1150                      V_id(vp), bitNumberToVnodeNumber(i, class), 
1151                      (afs_uintmax_t) VNDISK_GET_INO(vnode));
1152                         }
1153                         IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(vnode),
1154                                V_parentId(vp));
1155                         DOPOLL;
1156                     }
1157                     STREAM_SEEK(afile, Buf[i], 0);
1158                     (void)STREAM_WRITE(zero, vcp->diskSize, 1, afile);  /* Zero it out */
1159                 }
1160                 Buf[i] = 0;
1161             }
1162         }
1163         if (DoLogging) {
1164             Log("RestoreVolume Cleanup: Removed %d inodes for volume %d\n",
1165                 cnt1, V_id(vp));
1166         }
1167         STREAM_FLUSH(afile);    /* ensure 0s are on the disk */
1168         OS_SYNC(afile->str_fd);
1169     } else {
1170         size = OS_SIZE(fdP->fd_fd);
1171         assert(size != -1);
1172         nVnodes =
1173             (size <=
1174              vcp->diskSize ? 0 : size - vcp->diskSize) >> vcp->logSize;
1175         if (nVnodes > 0) {
1176             if (DoLogging) {
1177                 Log("RestoreVolume ProcessIndex: Set up %d inodes for volume %d\n",
1178                     nVnodes, V_id(vp));
1179             }
1180             Buf = (afs_int32 *) malloc(nVnodes * sizeof(afs_int32));
1181             if (Buf == NULL) {
1182                 STREAM_CLOSE(afile);
1183                 FDH_CLOSE(fdP);
1184                 return -1;
1185             }
1186             memset((char *)Buf, 0, nVnodes * sizeof(afs_int32));
1187             STREAM_SEEK(afile, offset = vcp->diskSize, 0);
1188             while (1) {
1189                 code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
1190                 if (code != 1) {
1191                     break;
1192                 }
1193                 if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
1194                     Buf[(offset >> vcp->logSize) - 1] = offset;
1195                     cnt++;
1196                 }
1197                 offset += vcp->diskSize;
1198             }
1199             if (DoLogging) {
1200                 Log("RestoreVolume ProcessIndex: found %d inodes\n", cnt);
1201             }
1202             *Bufp = Buf;
1203             *sizep = nVnodes;
1204         }
1205     }
1206     STREAM_CLOSE(afile);
1207     FDH_CLOSE(fdP);
1208     return 0;
1209 }
1210
1211
1212 int
1213 RestoreVolume(register struct rx_call *call, Volume * avp, int incremental,
1214               struct restoreCookie *cookie)
1215 {
1216     VolumeDiskData vol;
1217     struct DumpHeader header;
1218     afs_uint32 endMagic;
1219     Error error = 0, vupdate;
1220     register Volume *vp;
1221     struct iod iod;
1222     register struct iod *iodp = &iod;
1223     afs_int32 *b1 = NULL, *b2 = NULL;
1224     int s1 = 0, s2 = 0, delo = 0, tdelo;
1225     int tag;
1226
1227     iod_Init(iodp, call);
1228
1229     vp = avp;
1230
1231     if (!ReadDumpHeader(iodp, &header)) {
1232         Log("1 Volser: RestoreVolume: Error reading header file for dump; aborted\n");
1233         return VOLSERREAD_DUMPERROR;
1234     }
1235     if (iod_getc(iodp) != D_VOLUMEHEADER) {
1236         Log("1 Volser: RestoreVolume: Volume header missing from dump; not restored\n");
1237         return VOLSERREAD_DUMPERROR;
1238     }
1239     if (ReadVolumeHeader(iodp, &vol) == VOLSERREAD_DUMPERROR)
1240         return VOLSERREAD_DUMPERROR;
1241
1242     if (!delo)
1243         delo = ProcessIndex(vp, vLarge, &b1, &s1, 0);
1244     if (!delo)
1245         delo = ProcessIndex(vp, vSmall, &b2, &s2, 0);
1246     if (delo < 0) {
1247         Log("1 Volser: RestoreVolume: ProcessIndex failed; not restored\n");
1248         error = VOLSERREAD_DUMPERROR;
1249         goto out;
1250     }
1251
1252     strncpy(vol.name, cookie->name, VOLSER_OLDMAXVOLNAME);
1253     vol.type = cookie->type;
1254     vol.cloneId = cookie->clone;
1255     vol.parentId = cookie->parent;
1256
1257     tdelo = delo;
1258     while (1) {
1259         if (ReadVnodes(iodp, vp, 0, b1, s1, b2, s2, tdelo)) {
1260             error = VOLSERREAD_DUMPERROR;
1261             goto clean;
1262         }
1263         tag = iod_getc(iodp);
1264         if (tag != D_VOLUMEHEADER)
1265             break;
1266
1267         if (ReadVolumeHeader(iodp, &vol) == VOLSERREAD_DUMPERROR) {
1268             error = VOLSERREAD_DUMPERROR;
1269             goto out;
1270         }
1271     }
1272     if (tag != D_DUMPEND || !ReadInt32(iodp, &endMagic)
1273         || endMagic != DUMPENDMAGIC) {
1274         Log("1 Volser: RestoreVolume: End of dump not found; restore aborted\n");
1275         error = VOLSERREAD_DUMPERROR;
1276         goto clean;
1277     }
1278
1279
1280     if (iod_getc(iodp) != EOF) {
1281         Log("1 Volser: RestoreVolume: Unrecognized postamble in dump; restore aborted\n");
1282         error = VOLSERREAD_DUMPERROR;
1283         goto clean;
1284     }
1285
1286     if (!delo) {
1287         delo = ProcessIndex(vp, vLarge, &b1, &s1, 1);
1288         if (!delo)
1289             delo = ProcessIndex(vp, vSmall, &b2, &s2, 1);
1290         if (delo < 0) {
1291             error = VOLSERREAD_DUMPERROR;
1292             goto clean;
1293         }
1294     }
1295
1296   clean:
1297     ClearVolumeStats(&vol);
1298     CopyVolumeHeader(&vol, &V_disk(vp));
1299     V_destroyMe(vp) = 0;
1300     VUpdateVolume(&vupdate, vp);
1301     if (vupdate) {
1302         Log("1 Volser: RestoreVolume: Unable to rewrite volume header; restore aborted\n");
1303         error = VOLSERREAD_DUMPERROR;
1304         goto out;
1305     }
1306   out:
1307     /* Free the malloced space above */
1308     if (b1)
1309         free((char *)b1);
1310     if (b2)
1311         free((char *)b2);
1312     return error;
1313 }
1314
1315 static int
1316 ReadVnodes(register struct iod *iodp, Volume * vp, int incremental,
1317            afs_int32 * Lbuf, afs_int32 s1, afs_int32 * Sbuf, afs_int32 s2,
1318            afs_int32 delo)
1319 {
1320     afs_int32 vnodeNumber;
1321     char buf[SIZEOF_LARGEDISKVNODE];
1322     register int tag;
1323     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1324     struct VnodeDiskObject oldvnode;
1325     int idx;
1326     VnodeClass class;
1327     struct VnodeClassInfo *vcp;
1328     IHandle_t *tmpH;
1329     FdHandle_t *fdP;
1330     Inode nearInode;
1331     afs_int32 critical = 0;
1332
1333     tag = iod_getc(iodp);
1334     V_pref(vp, nearInode);
1335     while (tag == D_VNODE) {
1336         int haveStuff = 0;
1337         memset(buf, 0, sizeof(buf));
1338         if (!ReadInt32(iodp, (afs_uint32 *) & vnodeNumber))
1339             break;
1340
1341         if (!ReadInt32(iodp, &vnode->uniquifier))
1342             return VOLSERREAD_DUMPERROR;
1343
1344         if (DoLogging) {
1345             Log("ReadVnodes: setup %d/%d\n", vnodeNumber, vnode->uniquifier);
1346         }
1347         while ((tag = iod_getc(iodp)) > D_MAX && tag != EOF) {
1348             haveStuff = 1;
1349             if (critical)
1350                 critical--;
1351             switch (tag) {
1352             case 't':
1353                 vnode->type = (VnodeType) iod_getc(iodp);
1354                 break;
1355             case 'l':
1356                 {
1357                     unsigned short tlc;
1358                     if (!ReadShort(iodp, &tlc))
1359                         return VOLSERREAD_DUMPERROR;
1360                     vnode->linkCount = (signed int)tlc;
1361                 }
1362                 break;
1363             case 'v':
1364                 if (!ReadInt32(iodp, &vnode->dataVersion))
1365                     return VOLSERREAD_DUMPERROR;
1366                 break;
1367             case 'm':
1368                 if (!ReadInt32(iodp, &vnode->unixModifyTime))
1369                     return VOLSERREAD_DUMPERROR;
1370                 break;
1371             case 's':
1372                 if (!ReadInt32(iodp, &vnode->serverModifyTime))
1373                     return VOLSERREAD_DUMPERROR;
1374                 break;
1375             case 'a':
1376                 if (!ReadInt32(iodp, &vnode->author))
1377                     return VOLSERREAD_DUMPERROR;
1378                 break;
1379             case 'o':
1380                 if (!ReadInt32(iodp, &vnode->owner))
1381                     return VOLSERREAD_DUMPERROR;
1382                 break;
1383             case 'g':
1384                 if (!ReadInt32(iodp, (afs_uint32 *) & vnode->group))
1385                     return VOLSERREAD_DUMPERROR;
1386                 break;
1387             case 'b':{
1388                     unsigned short modeBits;
1389                     if (!ReadShort(iodp, &modeBits))
1390                         return VOLSERREAD_DUMPERROR;
1391                     vnode->modeBits = (unsigned int)modeBits;
1392                     break;
1393                 }
1394             case 'p':
1395                 if (!ReadInt32(iodp, &vnode->parent))
1396                     return VOLSERREAD_DUMPERROR;
1397                 break;
1398             case 'A':
1399                 ReadByteString(iodp, (byte *) VVnodeDiskACL(vnode),
1400                                VAclDiskSize(vnode));
1401                 acl_NtohACL(VVnodeDiskACL(vnode));
1402                 break;
1403 #ifdef AFS_LARGEFILE_ENV
1404             case 'h':
1405 #endif
1406             case 'f':{
1407                     Inode ino;
1408                     Error error;
1409                     afs_fsize_t vnodeLength;
1410
1411                     ino =
1412                         IH_CREATE(V_linkHandle(vp), V_device(vp),
1413                                   VPartitionPath(V_partition(vp)), nearInode,
1414                                   V_parentId(vp), vnodeNumber,
1415                                   vnode->uniquifier, vnode->dataVersion);
1416                     if (!VALID_INO(ino)) {
1417                         Log("1 Volser: ReadVnodes: IH_CREATE: %s - restore aborted\n",
1418                             afs_error_message(errno));
1419                         return VOLSERREAD_DUMPERROR;
1420                     }
1421                     nearInode = ino;
1422                     VNDISK_SET_INO(vnode, ino);
1423                     IH_INIT(tmpH, vp->device, V_parentId(vp), ino);
1424                     fdP = IH_OPEN(tmpH);
1425                     if (fdP == NULL) {
1426                         Log("1 Volser: ReadVnodes: IH_OPEN: %s - restore aborted\n",
1427                             afs_error_message(errno));
1428                         IH_RELEASE(tmpH);
1429                         return VOLSERREAD_DUMPERROR;
1430                     }
1431                     vnodeLength =
1432                         volser_WriteFile(vnodeNumber, iodp, fdP, tag, &error);
1433                     VNDISK_SET_LEN(vnode, vnodeLength);
1434                     FDH_REALLYCLOSE(fdP);
1435                     IH_RELEASE(tmpH);
1436                     if (error) {
1437                         Log("1 Volser: ReadVnodes: IDEC inode %llu\n",
1438                             (afs_uintmax_t) ino);
1439                         IH_DEC(V_linkHandle(vp), ino, V_parentId(vp));
1440                         return VOLSERREAD_DUMPERROR;
1441                     }
1442                     break;
1443                 }
1444             case 0x7e:
1445                 critical = 2;
1446                 break;
1447             default:
1448                 if (!HandleUnknownTag(iodp, tag, 2, critical))
1449                     return VOLSERREAD_DUMPERROR;
1450             }
1451         }
1452
1453         class = vnodeIdToClass(vnodeNumber);
1454         vcp = &VnodeClassInfo[class];
1455
1456         /* Mark this vnode as in this dump - so we don't delete it later */
1457         if (!delo) {
1458             idx = (vnodeIndexOffset(vcp, vnodeNumber) >> vcp->logSize) - 1;
1459             if (class == vLarge) {
1460                 if (Lbuf && (idx < s1))
1461                     Lbuf[idx] = 0;
1462             } else {
1463                 if (Sbuf && (idx < s2))
1464                     Sbuf[idx] = 0;
1465             }
1466         }
1467
1468         if (haveStuff) {
1469             FdHandle_t *fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1470             if (fdP == NULL) {
1471                 Log("1 Volser: ReadVnodes: Error opening vnode index; restore aborted\n");
1472                 return VOLSERREAD_DUMPERROR;
1473             }
1474             if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, vnodeNumber), SEEK_SET) <
1475                 0) {
1476                 Log("1 Volser: ReadVnodes: Error seeking into vnode index; restore aborted\n");
1477                 FDH_REALLYCLOSE(fdP);
1478                 return VOLSERREAD_DUMPERROR;
1479             }
1480             if (FDH_READ(fdP, &oldvnode, sizeof(oldvnode)) ==
1481                 sizeof(oldvnode)) {
1482                 if (oldvnode.type != vNull && VNDISK_GET_INO(&oldvnode)) {
1483                     IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(&oldvnode),
1484                            V_parentId(vp));
1485                 }
1486             }
1487             vnode->vnodeMagic = vcp->magic;
1488             if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, vnodeNumber), SEEK_SET) <
1489                 0) {
1490                 Log("1 Volser: ReadVnodes: Error seeking into vnode index; restore aborted\n");
1491                 FDH_REALLYCLOSE(fdP);
1492                 return VOLSERREAD_DUMPERROR;
1493             }
1494             if (FDH_WRITE(fdP, vnode, vcp->diskSize) != vcp->diskSize) {
1495                 Log("1 Volser: ReadVnodes: Error writing vnode index; restore aborted\n");
1496                 FDH_REALLYCLOSE(fdP);
1497                 return VOLSERREAD_DUMPERROR;
1498             }
1499             FDH_CLOSE(fdP);
1500         }
1501     }
1502     iod_ungetc(iodp, tag);
1503
1504     return 0;
1505 }
1506
1507
1508 /* called with disk file only.  Note that we don't have to worry about rx_Read
1509  * needing to read an ungetc'd character, since the ReadInt32 will have read
1510  * it instead.
1511  */
1512 static afs_fsize_t
1513 volser_WriteFile(int vn, struct iod *iodp, FdHandle_t * handleP, int tag,
1514                  Error * status)
1515 {
1516     afs_int32 code;
1517     afs_sfsize_t lcode;
1518     afs_fsize_t filesize;
1519     afs_fsize_t written = 0;
1520     register afs_uint32 size = 8192;
1521     register afs_fsize_t nbytes;
1522     unsigned char *p;
1523
1524
1525     *status = 0;
1526 #ifdef AFS_64BIT_ENV
1527     {
1528         afs_uint32 filesize_high = 0L, filesize_low = 0L;
1529 #ifdef AFS_LARGEFILE_ENV
1530         if (tag == 'h') {
1531             if (!ReadInt32(iodp, &filesize_high)) {
1532                 *status = 1;
1533                 return 0;
1534             }
1535         }
1536 #endif /* !AFS_LARGEFILE_ENV */
1537         if (!ReadInt32(iodp, &filesize_low)) {
1538             *status = 1;
1539             return 0;
1540         }
1541         FillInt64(filesize, filesize_high, filesize_low);
1542     }
1543 #else /* !AFS_64BIT_ENV */
1544     if (!ReadInt32(iodp, &filesize)) {
1545         *status = 1;
1546         return (0);
1547     }
1548 #endif /* !AFS_64BIT_ENV */
1549     p = (unsigned char *)malloc(size);
1550     if (p == NULL) {
1551         *status = 2;
1552         return (0);
1553     }
1554     for (nbytes = filesize; nbytes; nbytes -= size) {
1555         if (nbytes < size)
1556             size = nbytes;
1557
1558         if ((code = iod_Read(iodp, (char *) p, size)) != size) {
1559             Log("1 Volser: WriteFile: Error reading dump file %d size=%llu nbytes=%u (%d of %u); restore aborted\n", vn, (afs_uintmax_t) filesize, nbytes, code, size);
1560             *status = 3;
1561             break;
1562         }
1563         lcode = FDH_WRITE(handleP, p, size);
1564         if (lcode > 0)
1565             written += lcode;
1566         if (lcode != size) {
1567             Log("1 Volser: WriteFile: Error writing (%d,%u) bytes to vnode %d; restore aborted\n", (int)(lcode>>32), (int)(lcode & 0xffffffff), vn);
1568             *status = 4;
1569             break;
1570         }
1571     }
1572     free(p);
1573     return (written);
1574 }
1575
1576 static int
1577 ReadDumpHeader(register struct iod *iodp, struct DumpHeader *hp)
1578 {
1579     register int tag;
1580     afs_uint32 beginMagic;
1581     afs_int32 critical = 0;
1582     if (iod_getc(iodp) != D_DUMPHEADER || !ReadInt32(iodp, &beginMagic)
1583         || !ReadInt32(iodp, (afs_uint32 *) & hp->version)
1584         || beginMagic != DUMPBEGINMAGIC)
1585         return 0;
1586     hp->volumeId = 0;
1587     hp->nDumpTimes = 0;
1588     while ((tag = iod_getc(iodp)) > D_MAX) {
1589         unsigned short arrayLength;
1590         register int i;
1591         if (critical)
1592             critical--;
1593         switch (tag) {
1594         case 'v':
1595             if (!ReadInt32(iodp, &hp->volumeId))
1596                 return 0;
1597             break;
1598         case 'n':
1599             ReadString(iodp, hp->volumeName, sizeof(hp->volumeName));
1600             break;
1601         case 't':
1602             if (!ReadShort(iodp, &arrayLength))
1603                 return 0;
1604             hp->nDumpTimes = (arrayLength >> 1);
1605             for (i = 0; i < hp->nDumpTimes; i++)
1606                 if (!ReadInt32(iodp, (afs_uint32 *) & hp->dumpTimes[i].from)
1607                     || !ReadInt32(iodp, (afs_uint32 *) & hp->dumpTimes[i].to))
1608                     return 0;
1609             break;
1610         case 0x7e:
1611             critical = 2;
1612             break;
1613         default:
1614             if (!HandleUnknownTag(iodp, tag, 0, critical))
1615                 return VOLSERREAD_DUMPERROR;
1616         }
1617     }
1618     if (!hp->volumeId || !hp->nDumpTimes) {
1619         return 0;
1620     }
1621     iod_ungetc(iodp, tag);
1622     return 1;
1623 }
1624
1625
1626 /* ----- Below are the calls that calculate dump size ----- */
1627
1628 static int
1629 SizeDumpVolumeHeader(register struct iod *iodp, register Volume * vp,
1630                      register struct volintSize *v_size)
1631 {
1632     int code = 0;
1633     static char nullString[1] = "";     /*The ``contents'' of motd */
1634     afs_uint64 addvar;
1635
1636 /*     if (!code) code = DumpTag(iodp, D_VOLUMEHEADER); */
1637     FillInt64(addvar,0, 1);
1638     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1639 /*     if (!code) {code = DumpInt32(iodp, 'i',V_id(vp));} */
1640     FillInt64(addvar,0, 5);
1641     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1642 /*     if (!code) code = DumpInt32(iodp, 'v',V_stamp(vp).version); */
1643     FillInt64(addvar,0, 5);
1644     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1645 /*     if (!code) code = DumpString(iodp, 'n',V_name(vp)); */
1646     FillInt64(addvar,0, (2 + strlen(V_name(vp))));
1647     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1648 /*     if (!code) code = DumpBool(iodp, 's',V_inService(vp)); */
1649     FillInt64(addvar,0, 2);
1650     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1651 /*     if (!code) code = DumpBool(iodp, 'b',V_blessed(vp)); */
1652     FillInt64(addvar,0, 2);
1653     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1654 /*     if (!code) code = DumpInt32(iodp, 'u',V_uniquifier(vp)); */
1655     FillInt64(addvar,0, 5);
1656     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1657 /*     if (!code) code = DumpByte(iodp, 't',(byte)V_type(vp)); */
1658     FillInt64(addvar,0, 2);
1659     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1660 /*     if (!code){ code = DumpInt32(iodp, 'p',V_parentId(vp));} */
1661     FillInt64(addvar,0, 5);
1662     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1663 /*     if (!code) code = DumpInt32(iodp, 'c',V_cloneId(vp)); */
1664     FillInt64(addvar,0, 5);
1665     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1666 /*     if (!code) code = DumpInt32(iodp, 'q',V_maxquota(vp)); */
1667     FillInt64(addvar,0, 5);
1668     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1669 /*     if (!code) code = DumpInt32(iodp, 'm',V_minquota(vp)); */
1670     FillInt64(addvar,0, 5);
1671     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1672 /*     if (!code) code = DumpInt32(iodp, 'd',V_diskused(vp)); */
1673     FillInt64(addvar,0, 5);
1674     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1675 /*     if (!code) code = DumpInt32(iodp, 'f',V_filecount(vp)); */
1676     FillInt64(addvar,0, 5);
1677     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1678 /*     if (!code) code = DumpInt32(iodp, 'a', V_accountNumber(vp)); */
1679     FillInt64(addvar,0, 5);
1680     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1681 /*     if (!code) code = DumpInt32(iodp, 'o', V_owner(vp)); */
1682     FillInt64(addvar,0, 5);
1683     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1684 /*     if (!code) code = DumpInt32(iodp, 'C',V_creationDate(vp));       /\* Rw volume creation date *\/ */
1685     FillInt64(addvar,0, 5);
1686     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1687 /*     if (!code) code = DumpInt32(iodp, 'A',V_accessDate(vp)); */
1688     FillInt64(addvar,0, 5);
1689     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1690 /*     if (!code) code = DumpInt32(iodp, 'U',V_updateDate(vp)); */
1691     FillInt64(addvar,0, 5);
1692     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1693 /*     if (!code) code = DumpInt32(iodp, 'E',V_expirationDate(vp)); */
1694     FillInt64(addvar,0, 5);
1695     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1696 /*     if (!code) code = DumpInt32(iodp, 'B',V_backupDate(vp));         /\* Rw volume backup clone date *\/ */
1697     FillInt64(addvar,0, 5);
1698     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1699 /*     if (!code) code = DumpString(iodp, 'O',V_offlineMessage(vp)); */
1700     FillInt64(addvar,0, (2 + strlen(V_offlineMessage(vp))));
1701     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1702 /*     /\* */
1703 /*      * We do NOT dump the detailed volume statistics residing in the old */
1704 /*      * motd field, since we cannot tell from the info in a dump whether */
1705 /*      * statistics data has been put there.  Instead, we dump a null string, */
1706 /*      * just as if that was what the motd contained. */
1707 /*      *\/ */
1708 /*     if (!code) code = DumpString(iodp, 'M', nullString); */
1709     FillInt64(addvar,0, (2 + strlen(nullString)));
1710     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1711 /*     if (!code) code = DumpArrayInt32(iodp, 'W', (afs_uint32 *)V_weekUse(vp), sizeof(V_weekUse(vp))/sizeof(V_weekUse(vp)[0])); */
1712     FillInt64(addvar,0, (3 + 4 * (sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]))));
1713     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1714 /*     if (!code) code = DumpInt32(iodp, 'D', V_dayUseDate(vp)); */
1715     FillInt64(addvar,0, 5);
1716     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1717 /*     if (!code) code = DumpInt32(iodp, 'Z', V_dayUse(vp)); */
1718     FillInt64(addvar,0, 5);
1719     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1720     return code;
1721 }
1722
1723 static int
1724 SizeDumpEnd(register struct iod *iodp, register struct volintSize *v_size)
1725 {
1726     int code = 0;
1727     afs_uint64 addvar;
1728     FillInt64(addvar,0, 5);
1729     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1730     return code;
1731 }
1732
1733 int
1734 SizeDumpVolume(register struct rx_call *call, register Volume * vp,
1735                afs_int32 fromtime, int dumpAllDirs,
1736                register struct volintSize *v_size)
1737 {
1738     int code = 0;
1739     register struct iod *iodp = (struct iod *)0;
1740 /*    iod_Init(iodp, call); */
1741
1742     if (!code)
1743         code = SizeDumpDumpHeader(iodp, vp, fromtime, v_size);
1744     if (!code)
1745         code = SizeDumpPartial(iodp, vp, fromtime, dumpAllDirs, v_size);
1746     if (!code)
1747         code = SizeDumpEnd(iodp, v_size);
1748
1749     return code;
1750 }
1751
1752 static int
1753 SizeDumpDumpHeader(register struct iod *iodp, register Volume * vp,
1754                    afs_int32 fromtime, register struct volintSize *v_size)
1755 {
1756     int code = 0;
1757 /*    int UseLatestReadOnlyClone = 1; */
1758 /*    afs_int32 dumpTimes[2]; */
1759     afs_uint64 addvar;
1760 /*    iodp->device = vp->device; */
1761 /*    iodp->parentId = V_parentId(vp); */
1762 /*    iodp->dumpPartition = vp->partition; */
1763
1764     ZeroInt64(v_size->dump_size);       /* initialize the size */
1765 /*     if (!code) code = DumpDouble(iodp, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION); */
1766     FillInt64(addvar,0, 9);
1767     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1768 /*     if (!code) code = DumpInt32(iodp, 'v', UseLatestReadOnlyClone? V_id(vp): V_parentId(vp)); */
1769     FillInt64(addvar,0, 5);
1770     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1771 /*     if (!code) code = DumpString(iodp, 'n',V_name(vp)); */
1772     FillInt64(addvar,0, (2 + strlen(V_name(vp))));
1773     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1774 /*     dumpTimes[0] = fromtime; */
1775 /*     dumpTimes[1] = V_backupDate(vp); /\* Until the time the clone was made *\/ */
1776 /*     if (!code) code = DumpArrayInt32(iodp, 't', (afs_uint32 *)dumpTimes, 2); */
1777     FillInt64(addvar,0, (3 + 4 * 2));
1778     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1779     return code;
1780 }
1781
1782 static int
1783 SizeDumpVnode(register struct iod *iodp, struct VnodeDiskObject *v, int volid,
1784               int vnodeNumber, int dumpEverything,
1785               register struct volintSize *v_size)
1786 {
1787     int code = 0;
1788     afs_uint64 addvar;
1789
1790     if (!v || v->type == vNull)
1791         return code;
1792 /*     if (!code) code = DumpDouble(iodp, D_VNODE, vnodeNumber, v->uniquifier); */
1793     FillInt64(addvar,0, 9);
1794     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1795     if (!dumpEverything)
1796         return code;
1797 /*     if (!code)  code = DumpByte(iodp, 't',(byte)v->type); */
1798     FillInt64(addvar,0, 2);
1799     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1800 /*     if (!code) code = DumpShort(iodp, 'l', v->linkCount); /\* May not need this *\/ */
1801     FillInt64(addvar,0, 3);
1802     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1803 /*     if (!code) code = DumpInt32(iodp, 'v', v->dataVersion); */
1804     FillInt64(addvar,0, 5);
1805     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1806 /*     if (!code) code = DumpInt32(iodp, 'm', v->unixModifyTime); */
1807     FillInt64(addvar,0, 5);
1808     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1809 /*     if (!code) code = DumpInt32(iodp, 'a', v->author); */
1810     FillInt64(addvar,0, 5);
1811     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1812 /*     if (!code) code = DumpInt32(iodp, 'o', v->owner); */
1813     FillInt64(addvar,0, 5);
1814     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1815 /*     if (!code && v->group) code = DumpInt32(iodp, 'g', v->group);    /\* default group is 0 *\/ */
1816     if (v->group) {
1817         FillInt64(addvar,0, 5);
1818         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1819     }
1820 /*     if (!code) code = DumpShort(iodp, 'b', v->modeBits); */
1821     FillInt64(addvar,0, 3);
1822     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1823 /*     if (!code) code = DumpInt32(iodp, 'p', v->parent); */
1824     FillInt64(addvar,0, 5);
1825     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1826 /*     if (!code) code = DumpInt32(iodp, 's', v->serverModifyTime); */
1827     FillInt64(addvar,0, 5);
1828     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1829     if (v->type == vDirectory) {
1830 /*      acl_HtonACL(VVnodeDiskACL(v)); */
1831 /*      if (!code) code = DumpByteString(iodp, 'A', (byte *) VVnodeDiskACL(v), VAclDiskSize(v)); */
1832         FillInt64(addvar,0, (1 + VAclDiskSize(v)));
1833         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1834     }
1835
1836     if (VNDISK_GET_INO(v)) {
1837         FillInt64(addvar,0, (v->length + 5));
1838         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1839     }
1840     return code;
1841 }
1842
1843 /* A partial dump (no dump header) */
1844 static int
1845 SizeDumpPartial(register struct iod *iodp, register Volume * vp,
1846                 afs_int32 fromtime, int dumpAllDirs,
1847                 register struct volintSize *v_size)
1848 {
1849     int code = 0;
1850     if (!code)
1851         code = SizeDumpVolumeHeader(iodp, vp, v_size);
1852     if (!code)
1853         code =
1854             SizeDumpVnodeIndex(iodp, vp, vLarge, fromtime, dumpAllDirs,
1855                                v_size);
1856     if (!code)
1857         code = SizeDumpVnodeIndex(iodp, vp, vSmall, fromtime, 0, v_size);
1858     return code;
1859 }
1860
1861 static int
1862 SizeDumpVnodeIndex(register struct iod *iodp, Volume * vp, VnodeClass class,
1863                    afs_int32 fromtime, int forcedump,
1864                    register struct volintSize *v_size)
1865 {
1866     register int code = 0;
1867     register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
1868     char buf[SIZEOF_LARGEDISKVNODE];
1869     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1870     StreamHandle_t *file;
1871     FdHandle_t *fdP;
1872     int size;
1873     int flag;
1874     register int vnodeIndex, nVnodes;
1875
1876     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1877     assert(fdP != NULL);
1878     file = FDH_FDOPEN(fdP, "r+");
1879     assert(file != NULL);
1880     size = OS_SIZE(fdP->fd_fd);
1881     assert(size != -1);
1882     nVnodes = (size / vcp->diskSize) - 1;
1883     if (nVnodes > 0) {
1884         assert((nVnodes + 1) * vcp->diskSize == size);
1885         assert(STREAM_SEEK(file, vcp->diskSize, 0) == 0);
1886     } else
1887         nVnodes = 0;
1888     for (vnodeIndex = 0;
1889          nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
1890          nVnodes--, vnodeIndex++) {
1891         flag = forcedump || (vnode->serverModifyTime >= fromtime);
1892         /* Note:  the >= test is very important since some old volumes may not have
1893          * a serverModifyTime.  For an epoch dump, this results in 0>=0 test, which
1894          * does dump the file! */
1895         if (!code)
1896             code =
1897                 SizeDumpVnode(iodp, vnode, V_id(vp),
1898                               bitNumberToVnodeNumber(vnodeIndex, class), flag,
1899                               v_size);
1900     }
1901     STREAM_CLOSE(file);
1902     FDH_CLOSE(fdP);
1903     return code;
1904 }