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