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