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