large-partition-support-20080305
[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                         perror("unable to allocate inode");
1229                         Log("1 Volser: ReadVnodes: Restore aborted\n");
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                         IH_RELEASE(tmpH);
1238                         return VOLSERREAD_DUMPERROR;
1239                     }
1240                     vnodeLength =
1241                         volser_WriteFile(vnodeNumber, iodp, fdP, tag, &error);
1242                     VNDISK_SET_LEN(vnode, vnodeLength);
1243                     FDH_REALLYCLOSE(fdP);
1244                     IH_RELEASE(tmpH);
1245                     if (error) {
1246                         Log("1 Volser: ReadVnodes: IDEC inode %llu\n",
1247                             (afs_uintmax_t) ino);
1248                         IH_DEC(V_linkHandle(vp), ino, V_parentId(vp));
1249                         return VOLSERREAD_DUMPERROR;
1250                     }
1251                     break;
1252                 }
1253             }
1254         }
1255
1256         class = vnodeIdToClass(vnodeNumber);
1257         vcp = &VnodeClassInfo[class];
1258
1259         /* Mark this vnode as in this dump - so we don't delete it later */
1260         if (!delo) {
1261             idx = (vnodeIndexOffset(vcp, vnodeNumber) >> vcp->logSize) - 1;
1262             if (class == vLarge) {
1263                 if (Lbuf && (idx < s1))
1264                     Lbuf[idx] = 0;
1265             } else {
1266                 if (Sbuf && (idx < s2))
1267                     Sbuf[idx] = 0;
1268             }
1269         }
1270
1271         if (haveStuff) {
1272             FdHandle_t *fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1273             if (fdP == NULL) {
1274                 Log("1 Volser: ReadVnodes: Error opening vnode index; restore aborted\n");
1275                 return VOLSERREAD_DUMPERROR;
1276             }
1277             if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, vnodeNumber), SEEK_SET) <
1278                 0) {
1279                 Log("1 Volser: ReadVnodes: Error seeking into vnode index; restore aborted\n");
1280                 FDH_REALLYCLOSE(fdP);
1281                 return VOLSERREAD_DUMPERROR;
1282             }
1283             if (FDH_READ(fdP, &oldvnode, sizeof(oldvnode)) ==
1284                 sizeof(oldvnode)) {
1285                 if (oldvnode.type != vNull && VNDISK_GET_INO(&oldvnode)) {
1286                     IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(&oldvnode),
1287                            V_parentId(vp));
1288                 }
1289             }
1290             vnode->vnodeMagic = vcp->magic;
1291             if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, vnodeNumber), SEEK_SET) <
1292                 0) {
1293                 Log("1 Volser: ReadVnodes: Error seeking into vnode index; restore aborted\n");
1294                 FDH_REALLYCLOSE(fdP);
1295                 return VOLSERREAD_DUMPERROR;
1296             }
1297             if (FDH_WRITE(fdP, vnode, vcp->diskSize) != vcp->diskSize) {
1298                 Log("1 Volser: ReadVnodes: Error writing vnode index; restore aborted\n");
1299                 FDH_REALLYCLOSE(fdP);
1300                 return VOLSERREAD_DUMPERROR;
1301             }
1302             FDH_CLOSE(fdP);
1303         }
1304     }
1305     iod_ungetc(iodp, tag);
1306
1307     return 0;
1308 }
1309
1310
1311 /* called with disk file only.  Note that we don't have to worry about rx_Read
1312  * needing to read an ungetc'd character, since the ReadInt32 will have read
1313  * it instead.
1314  */
1315 static afs_fsize_t
1316 volser_WriteFile(int vn, struct iod *iodp, FdHandle_t * handleP, int tag,
1317                  Error * status)
1318 {
1319     afs_int32 code;
1320     afs_sfsize_t lcode;
1321     afs_fsize_t filesize;
1322     afs_fsize_t written = 0;
1323     register afs_uint32 size = 8192;
1324     register afs_fsize_t nbytes;
1325     unsigned char *p;
1326
1327
1328     *status = 0;
1329 #ifdef AFS_64BIT_ENV
1330     {
1331         afs_uint32 filesize_high = 0L, filesize_low = 0L;
1332 #ifdef AFS_LARGEFILE_ENV
1333         if (tag == 'h') {
1334             if (!ReadInt32(iodp, &filesize_high)) {
1335                 *status = 1;
1336                 return 0;
1337             }
1338         }
1339 #endif /* !AFS_LARGEFILE_ENV */
1340         if (!ReadInt32(iodp, &filesize_low)) {
1341             *status = 1;
1342             return 0;
1343         }
1344         FillInt64(filesize, filesize_high, filesize_low);
1345     }
1346 #else /* !AFS_64BIT_ENV */
1347     if (!ReadInt32(iodp, &filesize)) {
1348         *status = 1;
1349         return (0);
1350     }
1351 #endif /* !AFS_64BIT_ENV */
1352     p = (unsigned char *)malloc(size);
1353     if (p == NULL) {
1354         *status = 2;
1355         return (0);
1356     }
1357     for (nbytes = filesize; nbytes; nbytes -= size) {
1358         if (nbytes < size)
1359             size = nbytes;
1360
1361         if ((code = iod_Read(iodp, p, size)) != size) {
1362             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);
1363             *status = 3;
1364             break;
1365         }
1366         lcode = FDH_WRITE(handleP, p, size);
1367         if (lcode > 0)
1368             written += lcode;
1369         if (lcode != size) {
1370             Log("1 Volser: WriteFile: Error writing (%d,%u) bytes to vnode %d; restore aborted\n", (int)(lcode>>32), (int)(lcode & 0xffffffff), vn);
1371             *status = 4;
1372             break;
1373         }
1374     }
1375     free(p);
1376     return (written);
1377 }
1378
1379 static int
1380 ReadDumpHeader(register struct iod *iodp, struct DumpHeader *hp)
1381 {
1382     register tag;
1383     afs_uint32 beginMagic;
1384     if (iod_getc(iodp) != D_DUMPHEADER || !ReadInt32(iodp, &beginMagic)
1385         || !ReadInt32(iodp, (afs_uint32 *) & hp->version)
1386         || beginMagic != DUMPBEGINMAGIC)
1387         return 0;
1388     hp->volumeId = 0;
1389     hp->nDumpTimes = 0;
1390     while ((tag = iod_getc(iodp)) > D_MAX) {
1391         unsigned short arrayLength;
1392         register int i;
1393         switch (tag) {
1394         case 'v':
1395             if (!ReadInt32(iodp, &hp->volumeId))
1396                 return 0;
1397             break;
1398         case 'n':
1399             ReadString(iodp, hp->volumeName, sizeof(hp->volumeName));
1400             break;
1401         case 't':
1402             if (!ReadShort(iodp, &arrayLength))
1403                 return 0;
1404             hp->nDumpTimes = (arrayLength >> 1);
1405             for (i = 0; i < hp->nDumpTimes; i++)
1406                 if (!ReadInt32(iodp, (afs_uint32 *) & hp->dumpTimes[i].from)
1407                     || !ReadInt32(iodp, (afs_uint32 *) & hp->dumpTimes[i].to))
1408                     return 0;
1409             break;
1410         }
1411     }
1412     if (!hp->volumeId || !hp->nDumpTimes) {
1413         return 0;
1414     }
1415     iod_ungetc(iodp, tag);
1416     return 1;
1417 }
1418
1419
1420 /* ----- Below are the calls that calculate dump size ----- */
1421
1422 static int
1423 SizeDumpVolumeHeader(register struct iod *iodp, register Volume * vp,
1424                      register struct volintSize *v_size)
1425 {
1426     int code = 0;
1427     static char nullString[1] = "";     /*The ``contents'' of motd */
1428     afs_uint64 addvar;
1429
1430 /*     if (!code) code = DumpTag(iodp, D_VOLUMEHEADER); */
1431     FillInt64(addvar,0, 1);
1432     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1433 /*     if (!code) {code = DumpInt32(iodp, 'i',V_id(vp));} */
1434     FillInt64(addvar,0, 5);
1435     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1436 /*     if (!code) code = DumpInt32(iodp, 'v',V_stamp(vp).version); */
1437     FillInt64(addvar,0, 5);
1438     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1439 /*     if (!code) code = DumpString(iodp, 'n',V_name(vp)); */
1440     FillInt64(addvar,0, (2 + strlen(V_name(vp))));
1441     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1442 /*     if (!code) code = DumpBool(iodp, 's',V_inService(vp)); */
1443     FillInt64(addvar,0, 2);
1444     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1445 /*     if (!code) code = DumpBool(iodp, 'b',V_blessed(vp)); */
1446     FillInt64(addvar,0, 2);
1447     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1448 /*     if (!code) code = DumpInt32(iodp, 'u',V_uniquifier(vp)); */
1449     FillInt64(addvar,0, 5);
1450     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1451 /*     if (!code) code = DumpByte(iodp, 't',(byte)V_type(vp)); */
1452     FillInt64(addvar,0, 2);
1453     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1454 /*     if (!code){ code = DumpInt32(iodp, 'p',V_parentId(vp));} */
1455     FillInt64(addvar,0, 5);
1456     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1457 /*     if (!code) code = DumpInt32(iodp, 'c',V_cloneId(vp)); */
1458     FillInt64(addvar,0, 5);
1459     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1460 /*     if (!code) code = DumpInt32(iodp, 'q',V_maxquota(vp)); */
1461     FillInt64(addvar,0, 5);
1462     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1463 /*     if (!code) code = DumpInt32(iodp, 'm',V_minquota(vp)); */
1464     FillInt64(addvar,0, 5);
1465     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1466 /*     if (!code) code = DumpInt32(iodp, 'd',V_diskused(vp)); */
1467     FillInt64(addvar,0, 5);
1468     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1469 /*     if (!code) code = DumpInt32(iodp, 'f',V_filecount(vp)); */
1470     FillInt64(addvar,0, 5);
1471     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1472 /*     if (!code) code = DumpInt32(iodp, 'a', V_accountNumber(vp)); */
1473     FillInt64(addvar,0, 5);
1474     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1475 /*     if (!code) code = DumpInt32(iodp, 'o', V_owner(vp)); */
1476     FillInt64(addvar,0, 5);
1477     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1478 /*     if (!code) code = DumpInt32(iodp, 'C',V_creationDate(vp));       /\* Rw volume creation date *\/ */
1479     FillInt64(addvar,0, 5);
1480     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1481 /*     if (!code) code = DumpInt32(iodp, 'A',V_accessDate(vp)); */
1482     FillInt64(addvar,0, 5);
1483     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1484 /*     if (!code) code = DumpInt32(iodp, 'U',V_updateDate(vp)); */
1485     FillInt64(addvar,0, 5);
1486     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1487 /*     if (!code) code = DumpInt32(iodp, 'E',V_expirationDate(vp)); */
1488     FillInt64(addvar,0, 5);
1489     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1490 /*     if (!code) code = DumpInt32(iodp, 'B',V_backupDate(vp));         /\* Rw volume backup clone date *\/ */
1491     FillInt64(addvar,0, 5);
1492     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1493 /*     if (!code) code = DumpString(iodp, 'O',V_offlineMessage(vp)); */
1494     FillInt64(addvar,0, (2 + strlen(V_offlineMessage(vp))));
1495     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1496 /*     /\* */
1497 /*      * We do NOT dump the detailed volume statistics residing in the old */
1498 /*      * motd field, since we cannot tell from the info in a dump whether */
1499 /*      * statistics data has been put there.  Instead, we dump a null string, */
1500 /*      * just as if that was what the motd contained. */
1501 /*      *\/ */
1502 /*     if (!code) code = DumpString(iodp, 'M', nullString); */
1503     FillInt64(addvar,0, (2 + strlen(nullString)));
1504     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1505 /*     if (!code) code = DumpArrayInt32(iodp, 'W', (afs_uint32 *)V_weekUse(vp), sizeof(V_weekUse(vp))/sizeof(V_weekUse(vp)[0])); */
1506     FillInt64(addvar,0, (3 + 4 * (sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]))));
1507     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1508 /*     if (!code) code = DumpInt32(iodp, 'D', V_dayUseDate(vp)); */
1509     FillInt64(addvar,0, 5);
1510     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1511 /*     if (!code) code = DumpInt32(iodp, 'Z', V_dayUse(vp)); */
1512     FillInt64(addvar,0, 5);
1513     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1514 /*     if (!code) code = DumpInt32(iodp, 'V', V_volUpCounter(vp)); */
1515     FillInt64(addvar,0, 5);
1516     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1517     return code;
1518 }
1519
1520 static int
1521 SizeDumpEnd(register struct iod *iodp, register struct volintSize *v_size)
1522 {
1523     int code = 0;
1524     afs_uint64 addvar;
1525     FillInt64(addvar,0, 5);
1526     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1527     return code;
1528 }
1529
1530 int
1531 SizeDumpVolume(register struct rx_call *call, register Volume * vp,
1532                afs_int32 fromtime, int dumpAllDirs,
1533                register struct volintSize *v_size)
1534 {
1535     int code = 0;
1536     register struct iod *iodp = (struct iod *)0;
1537 /*    iod_Init(iodp, call); */
1538
1539     if (!code)
1540         code = SizeDumpDumpHeader(iodp, vp, fromtime, v_size);
1541     if (!code)
1542         code = SizeDumpPartial(iodp, vp, fromtime, dumpAllDirs, v_size);
1543     if (!code)
1544         code = SizeDumpEnd(iodp, v_size);
1545
1546     return code;
1547 }
1548
1549 static int
1550 SizeDumpDumpHeader(register struct iod *iodp, register Volume * vp,
1551                    afs_int32 fromtime, register struct volintSize *v_size)
1552 {
1553     int code = 0;
1554     int UseLatestReadOnlyClone = 1;
1555 /*    afs_int32 dumpTimes[2]; */
1556     afs_uint64 addvar;
1557 /*    iodp->device = vp->device; */
1558 /*    iodp->parentId = V_parentId(vp); */
1559 /*    iodp->dumpPartition = vp->partition; */
1560
1561     ZeroInt64(v_size->dump_size);       /* initialize the size */
1562 /*     if (!code) code = DumpDouble(iodp, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION); */
1563     FillInt64(addvar,0, 9);
1564     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1565 /*     if (!code) code = DumpInt32(iodp, 'v', UseLatestReadOnlyClone? V_id(vp): V_parentId(vp)); */
1566     FillInt64(addvar,0, 5);
1567     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1568 /*     if (!code) code = DumpString(iodp, 'n',V_name(vp)); */
1569     FillInt64(addvar,0, (2 + strlen(V_name(vp))));
1570     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1571 /*     dumpTimes[0] = fromtime; */
1572 /*     dumpTimes[1] = V_backupDate(vp); /\* Until the time the clone was made *\/ */
1573 /*     if (!code) code = DumpArrayInt32(iodp, 't', (afs_uint32 *)dumpTimes, 2); */
1574     FillInt64(addvar,0, (3 + 4 * 2));
1575     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1576     return code;
1577 }
1578
1579 static int
1580 SizeDumpVnode(register struct iod *iodp, struct VnodeDiskObject *v, int volid,
1581               int vnodeNumber, int dumpEverything,
1582               register struct volintSize *v_size)
1583 {
1584     int code = 0;
1585     afs_uint64 addvar;
1586
1587     if (!v || v->type == vNull)
1588         return code;
1589 /*     if (!code) code = DumpDouble(iodp, D_VNODE, vnodeNumber, v->uniquifier); */
1590     FillInt64(addvar,0, 9);
1591     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1592     if (!dumpEverything)
1593         return code;
1594 /*     if (!code)  code = DumpByte(iodp, 't',(byte)v->type); */
1595     FillInt64(addvar,0, 2);
1596     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1597 /*     if (!code) code = DumpShort(iodp, 'l', v->linkCount); /\* May not need this *\/ */
1598     FillInt64(addvar,0, 3);
1599     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1600 /*     if (!code) code = DumpInt32(iodp, 'v', v->dataVersion); */
1601     FillInt64(addvar,0, 5);
1602     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1603 /*     if (!code) code = DumpInt32(iodp, 'm', v->unixModifyTime); */
1604     FillInt64(addvar,0, 5);
1605     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1606 /*     if (!code) code = DumpInt32(iodp, 'a', v->author); */
1607     FillInt64(addvar,0, 5);
1608     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1609 /*     if (!code) code = DumpInt32(iodp, 'o', v->owner); */
1610     FillInt64(addvar,0, 5);
1611     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1612 /*     if (!code && v->group) code = DumpInt32(iodp, 'g', v->group);    /\* default group is 0 *\/ */
1613     if (v->group) {
1614         FillInt64(addvar,0, 5);
1615         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1616     }
1617 /*     if (!code) code = DumpShort(iodp, 'b', v->modeBits); */
1618     FillInt64(addvar,0, 3);
1619     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1620 /*     if (!code) code = DumpInt32(iodp, 'p', v->parent); */
1621     FillInt64(addvar,0, 5);
1622     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1623 /*     if (!code) code = DumpInt32(iodp, 's', v->serverModifyTime); */
1624     FillInt64(addvar,0, 5);
1625     AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1626     if (v->type == vDirectory) {
1627 /*      acl_HtonACL(VVnodeDiskACL(v)); */
1628 /*      if (!code) code = DumpByteString(iodp, 'A', (byte *) VVnodeDiskACL(v), VAclDiskSize(v)); */
1629         FillInt64(addvar,0, (1 + VAclDiskSize(v)));
1630         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1631     }
1632
1633     if (VNDISK_GET_INO(v)) {
1634         FillInt64(addvar,0, (v->length + 5));
1635         AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
1636     }
1637     return code;
1638 }
1639
1640 /* A partial dump (no dump header) */
1641 static int
1642 SizeDumpPartial(register struct iod *iodp, register Volume * vp,
1643                 afs_int32 fromtime, int dumpAllDirs,
1644                 register struct volintSize *v_size)
1645 {
1646     int code = 0;
1647     if (!code)
1648         code = SizeDumpVolumeHeader(iodp, vp, v_size);
1649     if (!code)
1650         code =
1651             SizeDumpVnodeIndex(iodp, vp, vLarge, fromtime, dumpAllDirs,
1652                                v_size);
1653     if (!code)
1654         code = SizeDumpVnodeIndex(iodp, vp, vSmall, fromtime, 0, v_size);
1655     return code;
1656 }
1657
1658 static int
1659 SizeDumpVnodeIndex(register struct iod *iodp, Volume * vp, VnodeClass class,
1660                    afs_int32 fromtime, int forcedump,
1661                    register struct volintSize *v_size)
1662 {
1663     register int code = 0;
1664     register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
1665     char buf[SIZEOF_LARGEDISKVNODE];
1666     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
1667     StreamHandle_t *file;
1668     FdHandle_t *fdP;
1669     int size;
1670     int flag;
1671     register int vnodeIndex, nVnodes;
1672
1673     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
1674     assert(fdP != NULL);
1675     file = FDH_FDOPEN(fdP, "r+");
1676     assert(file != NULL);
1677     size = OS_SIZE(fdP->fd_fd);
1678     assert(size != -1);
1679     nVnodes = (size / vcp->diskSize) - 1;
1680     if (nVnodes > 0) {
1681         assert((nVnodes + 1) * vcp->diskSize == size);
1682         assert(STREAM_SEEK(file, vcp->diskSize, 0) == 0);
1683     } else
1684         nVnodes = 0;
1685     for (vnodeIndex = 0;
1686          nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
1687          nVnodes--, vnodeIndex++) {
1688         flag = forcedump || (vnode->serverModifyTime >= fromtime);
1689         /* Note:  the >= test is very important since some old volumes may not have
1690          * a serverModifyTime.  For an epoch dump, this results in 0>=0 test, which
1691          * does dump the file! */
1692         if (!code)
1693             code =
1694                 SizeDumpVnode(iodp, vnode, V_id(vp),
1695                               bitNumberToVnodeNumber(vnodeIndex, class), flag,
1696                               v_size);
1697     }
1698     STREAM_CLOSE(file);
1699     FDH_CLOSE(fdP);
1700     return code;
1701 }