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