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