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