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