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