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