DEVEL15-vol-split-nearino-20090324
[openafs.git] / src / volser / vol-dump.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 /*
11    System:              VICE-TWO
12    Module:              vol-dump.c
13    Institution: The Information Technology Center, Carnegie-Mellon University
14    
15    */
16
17 #include <afsconfig.h>
18 #include <afs/param.h>
19
20 RCSID
21     ("$Header$");
22
23 #include <ctype.h>
24 #include <errno.h>
25 #include <sys/stat.h>
26 #include <stdio.h>
27 #include <string.h>
28 #ifdef AFS_NT40_ENV
29 #include <fcntl.h>
30 #include <time.h>
31 #include <io.h>
32 #else
33 #include <sys/param.h>
34 #include <sys/file.h>
35 #include <sys/time.h>
36 #endif
37 #include <afs/cmd.h>
38
39 #include <rx/xdr.h>
40 #include <afs/afsint.h>
41 #include "nfs.h"
42 #include <afs/errors.h>
43 #include "lock.h"
44 #include "lwp.h"
45 #include <afs/afssyscalls.h>
46 #include "ihandle.h"
47 #include "vnode.h"
48 #include "volume.h"
49 #include "partition.h"
50 #include "viceinode.h"
51 #include <afs/afssyscalls.h>
52 #include "acl.h"
53
54 #ifdef HAVE_UNISTD_H
55 #include <unistd.h>
56 #endif
57
58 #ifdef HAVE_STDLIB_H
59 #include <stdlib.h>
60 #endif
61
62 #ifdef HAVE_FCNTL_H
63 #include <fcntl.h>
64 #endif
65
66 #ifdef _AIX
67 #include <time.h>
68 #endif
69
70 #include <dirent.h>
71
72 #include "volser/volser.h"
73 #include "volser/volint.h"
74 #include "volser/dump.h"
75
76 #define putint32(p, v)  *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
77 #define putshort(p, v) *p++ = v>>8, *p++ = v
78
79 #ifdef O_LARGEFILE
80 #define afs_stat        stat64
81 #define afs_fstat       fstat64
82 #define afs_open        open64
83 #else /* !O_LARGEFILE */
84 #define afs_stat        stat
85 #define afs_fstat       fstat
86 #define afs_open        open
87 #endif /* !O_LARGEFILE */
88
89 int VolumeChanged;              /* needed by physio - leave alone */
90 int verbose = 0;
91
92 /* Forward Declarations */
93 void HandleVolume(struct DiskPartition64 *partP, char *name, char *filename, int fromtime);
94 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
95                      register struct VolumeHeader *header);
96 static void DoMyVolDump(Volume * vp, struct DiskPartition64 *dp,
97                         char *dumpfile, int fromtime);
98
99 #ifndef AFS_NT40_ENV
100 #include "AFS_component_version_number.c"
101 #endif
102
103 char name[VMAXPATHLEN];
104
105
106 int
107 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
108 {
109     int bad = 0;
110     int code;
111
112     code = IH_IREAD(ih, 0, to, size);
113     if (code != size)
114         return -1;
115
116     return 0;
117 }
118
119
120 Volume *
121 AttachVolume(struct DiskPartition64 * dp, char *volname,
122              register struct VolumeHeader * header)
123 {
124     register Volume *vp;
125     afs_int32 ec = 0;
126
127     vp = (Volume *) calloc(1, sizeof(Volume));
128     vp->specialStatus = 0;
129     vp->device = dp->device;
130     vp->partition = dp;
131     IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
132             header->largeVnodeIndex);
133     IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
134             header->smallVnodeIndex);
135     IH_INIT(vp->diskDataHandle, dp->device, header->parent,
136             header->volumeInfo);
137     IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
138     vp->cacheCheck = 0;         /* XXXX */
139     vp->shuttingDown = 0;
140     vp->goingOffline = 0;
141     vp->nUsers = 1;
142     vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
143     ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
144                   sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
145     if (!ec) {
146         struct IndexFileHeader iHead;
147         ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
148                       sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
149     }
150     if (!ec) {
151         struct IndexFileHeader iHead;
152         ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
153                       sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
154     }
155 #ifdef AFS_NAMEI_ENV
156     if (!ec) {
157         struct versionStamp stamp;
158         ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
159                       LINKTABLEMAGIC, LINKTABLEVERSION);
160     }
161 #endif
162     if (ec)
163         return (Volume *) 0;
164     return vp;
165 }
166
167
168 static int
169 handleit(struct cmd_syndesc *as, void *arock)
170 {
171     register struct cmd_item *ti;
172     int err = 0;
173     afs_uint32 volumeId = 0;
174     char *partName = 0;
175     char *fileName = NULL;
176     struct DiskPartition64 *partP = NULL;
177     char name1[128];
178     char tmpPartName[20];
179     int fromtime = 0;
180     afs_int32 code;
181
182
183 #ifndef AFS_NT40_ENV
184 #if 0
185     if (geteuid() != 0) {
186         fprintf(stderr, "voldump must be run as root; sorry\n");
187         exit(1);
188     }
189 #endif
190 #endif
191
192     if ((ti = as->parms[0].items))
193         partName = ti->data;
194     if ((ti = as->parms[1].items))
195         volumeId = (afs_uint32)atoi(ti->data);
196     if ((ti = as->parms[2].items))
197         fileName = ti->data;
198     if ((ti = as->parms[3].items))
199         verbose = 1;
200     if (as->parms[4].items && strcmp(as->parms[4].items->data, "0")) {
201         code = ktime_DateToInt32(as->parms[4].items->data, &fromtime);
202         if (code) {
203             fprintf(STDERR, "failed to parse date '%s' (error=%d))\n",
204                 as->parms[4].items->data, code);
205                 return code;
206         }
207     }
208
209     DInit(10);
210
211     err = VAttachPartitions();
212     if (err) {
213         fprintf(stderr, "%d partitions had errors during attach.\n", err);
214     }
215
216     if (partName) {
217         if (strlen(partName) == 1) {
218             if (partName[0] >= 'a' && partName[0] <= 'z') {
219                 strcpy(tmpPartName, "/vicepa");
220                 tmpPartName[6] = partName[0];
221                 partP = VGetPartition(tmpPartName, 0);
222             }
223         } else {
224             partP = VGetPartition(partName, 0);
225         }
226         if (!partP) {
227             fprintf(stderr,
228                     "%s is not an AFS partition name on this server.\n",
229                     partName);
230             exit(1);
231         }
232     }
233
234     if (!volumeId) {
235         fprintf(stderr, "Must specify volume id!\n");
236         exit(1);
237     }
238
239     if (!partP) {
240         fprintf(stderr, "must specify vice partition.\n");
241         exit(1);
242     }
243
244     (void)afs_snprintf(name1, sizeof name1, VFORMAT, (unsigned long)volumeId);
245     HandleVolume(partP, name1, fileName, fromtime);
246     return 0;
247 }
248
249 void
250 HandleVolume(struct DiskPartition64 *dp, char *name, char *filename, int fromtime)
251 {
252     struct VolumeHeader header;
253     struct VolumeDiskHeader diskHeader;
254     struct afs_stat status, stat;
255     register int fd;
256     Volume *vp;
257     IHandle_t *ih;
258     char headerName[1024];
259
260     afs_int32 n;
261
262     (void)afs_snprintf(headerName, sizeof headerName, "%s/%s",
263                        VPartitionPath(dp), name);
264     if ((fd = afs_open(headerName, O_RDONLY)) == -1
265         || afs_fstat(fd, &status) == -1) {
266         fprintf(stderr, "Cannot read volume header %s\n", name);
267         close(fd);
268         exit(1);
269     }
270     n = read(fd, &diskHeader, sizeof(diskHeader));
271
272     if (n != sizeof(diskHeader)
273         || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
274         fprintf(stderr, "Error reading volume header %s\n", name);
275         exit(1);
276     }
277     if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
278         fprintf(stderr,
279                 "Volume %s, version number is incorrect; volume needs salvage\n",
280                 name);
281         exit(1);
282     }
283     DiskToVolumeHeader(&header, &diskHeader);
284
285     close(fd);
286     vp = AttachVolume(dp, name, &header);
287     if (!vp) {
288         fprintf(stderr, "Error attaching volume header %s\n", name);
289         exit(1);
290     }
291
292     DoMyVolDump(vp, dp, filename, fromtime);
293 }
294
295
296 int
297 main(int argc, char **argv)
298 {
299     register struct cmd_syndesc *ts;
300     afs_int32 code;
301
302     VInitVolumePackage(volumeUtility, 5, 5, DONT_CONNECT_FS, 0);
303
304     ts = cmd_CreateSyntax(NULL, handleit, NULL,
305                           "Dump a volume to a 'vos dump' format file without using volserver");
306     cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL, "AFS partition name");
307     cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
308     cmd_AddParm(ts, "-file", CMD_LIST, CMD_OPTIONAL, "Dump filename");
309     cmd_AddParm(ts, "-verbose", CMD_FLAG, CMD_OPTIONAL,
310                 "Trace dump progress (very verbose)");
311     cmd_AddParm(ts, "-time", CMD_SINGLE, CMD_OPTIONAL, "dump from time");
312     code = cmd_Dispatch(argc, argv);
313     return code;
314 }
315
316
317
318
319 static int
320 DumpDouble(int dumpfd, char tag, register afs_uint32 value1,
321            register afs_uint32 value2)
322 {
323     int res;
324     char tbuffer[9];
325     register byte *p = (unsigned char *)tbuffer;
326     *p++ = tag;
327     putint32(p, value1);
328     putint32(p, value2);
329
330     res = write(dumpfd, tbuffer, 9);
331     return ((res == 9) ? 0 : VOLSERDUMPERROR);
332 }
333
334 static int
335 DumpInt32(int dumpfd, char tag, register afs_uint32 value)
336 {
337     char tbuffer[5];
338     register byte *p = (unsigned char *)tbuffer;
339     *p++ = tag;
340     putint32(p, value);
341     return ((write(dumpfd, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
342 }
343
344 static int
345 DumpString(int dumpfd, char tag, register char *s)
346 {
347     register int n;
348     int code = 0;
349     code = write(dumpfd, &tag, 1);
350     if (code != 1)
351         return VOLSERDUMPERROR;
352     n = strlen(s) + 1;
353     code = write(dumpfd, s, n);
354     if (code != n)
355         return VOLSERDUMPERROR;
356     return 0;
357 }
358
359
360 static int
361 DumpArrayInt32(int dumpfd, char tag, register afs_uint32 * array,
362                register int nelem)
363 {
364     char tbuffer[4];
365     register afs_uint32 v;
366     int code = 0;
367     register byte *p = (unsigned char *)tbuffer;
368     *p++ = tag;
369     putshort(p, nelem);
370     code = write(dumpfd, tbuffer, 3);
371     if (code != 3)
372         return VOLSERDUMPERROR;
373     while (nelem--) {
374         p = (unsigned char *)tbuffer;
375         v = *array++;           /*this was register */
376
377         putint32(p, v);
378         code = write(dumpfd, tbuffer, 4);
379         if (code != 4)
380             return VOLSERDUMPERROR;
381     }
382     return 0;
383 }
384
385
386
387
388 static int
389 DumpDumpHeader(int dumpfd, register Volume * vp, afs_int32 fromtime)
390 {
391     int code = 0;
392     afs_int32 dumpTimes[2];
393
394     if (verbose)
395         fprintf(stderr, "dumping dump header\n");
396
397     if (!code)
398         code = DumpDouble(dumpfd, D_DUMPHEADER, DUMPBEGINMAGIC, DUMPVERSION);
399
400     if (!code)
401         code = DumpInt32(dumpfd, 'v', V_id(vp));
402
403     if (!code)
404         code = DumpString(dumpfd, 'n', V_name(vp));
405
406     dumpTimes[0] = fromtime;
407     dumpTimes[1] = V_backupDate(vp);    /* Until the time the clone was made */
408     if (!code)
409         code = DumpArrayInt32(dumpfd, 't', (afs_uint32 *) dumpTimes, 2);
410
411     return code;
412 }
413
414
415 static int
416 DumpEnd(int dumpfd)
417 {
418     return (DumpInt32(dumpfd, D_DUMPEND, DUMPENDMAGIC));
419 }
420
421 static int
422 DumpByte(int dumpfd, char tag, byte value)
423 {
424     char tbuffer[2];
425     register byte *p = (unsigned char *)tbuffer;
426     *p++ = tag;
427     *p = value;
428     return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
429 }
430
431 static int
432 DumpTag(int dumpfd, register int tag)
433 {
434     char p;
435
436     p = tag;
437     return ((write(dumpfd, &p, 1) == 1) ? 0 : VOLSERDUMPERROR);
438
439 }
440
441 static int
442 DumpBool(int dumpfd, char tag, unsigned int value)
443 {
444     char tbuffer[2];
445     register byte *p = (unsigned char *)tbuffer;
446     *p++ = tag;
447     *p = value;
448     return ((write(dumpfd, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
449 }
450
451
452
453 static int
454 DumpVolumeHeader(int dumpfd, register Volume * vp)
455 {
456     int code = 0;
457
458     if (verbose)
459         fprintf(stderr, "dumping volume header\n");
460
461     if (!code)
462         code = DumpTag(dumpfd, D_VOLUMEHEADER);
463     if (!code)
464         code = DumpInt32(dumpfd, 'i', V_id(vp));
465     if (!code)
466         code = DumpInt32(dumpfd, 'v', V_stamp(vp).version);
467     if (!code)
468         code = DumpString(dumpfd, 'n', V_name(vp));
469     if (!code)
470         code = DumpBool(dumpfd, 's', V_inService(vp));
471     if (!code)
472         code = DumpBool(dumpfd, 'b', V_blessed(vp));
473     if (!code)
474         code = DumpInt32(dumpfd, 'u', V_uniquifier(vp));
475     if (!code)
476         code = DumpByte(dumpfd, 't', (byte) V_type(vp));
477     if (!code)
478         code = DumpInt32(dumpfd, 'p', V_parentId(vp));
479     if (!code)
480         code = DumpInt32(dumpfd, 'c', V_cloneId(vp));
481     if (!code)
482         code = DumpInt32(dumpfd, 'q', V_maxquota(vp));
483     if (!code)
484         code = DumpInt32(dumpfd, 'm', V_minquota(vp));
485     if (!code)
486         code = DumpInt32(dumpfd, 'd', V_diskused(vp));
487     if (!code)
488         code = DumpInt32(dumpfd, 'f', V_filecount(vp));
489     if (!code)
490         code = DumpInt32(dumpfd, 'a', V_accountNumber(vp));
491     if (!code)
492         code = DumpInt32(dumpfd, 'o', V_owner(vp));
493     if (!code)
494         code = DumpInt32(dumpfd, 'C', V_creationDate(vp));      /* Rw volume creation date */
495     if (!code)
496         code = DumpInt32(dumpfd, 'A', V_accessDate(vp));
497     if (!code)
498         code = DumpInt32(dumpfd, 'U', V_updateDate(vp));
499     if (!code)
500         code = DumpInt32(dumpfd, 'E', V_expirationDate(vp));
501     if (!code)
502         code = DumpInt32(dumpfd, 'B', V_backupDate(vp));        /* Rw volume backup clone date */
503     if (!code)
504         code = DumpString(dumpfd, 'O', V_offlineMessage(vp));
505
506     /*
507      * We do NOT dump the detailed volume statistics residing in the old
508      * motd field, since we cannot tell from the info in a dump whether
509      * statistics data has been put there.  Instead, we dump a null string,
510      * just as if that was what the motd contained.
511      */
512     if (!code)
513         code = DumpString(dumpfd, 'M', "");
514     if (!code)
515         code =
516             DumpArrayInt32(dumpfd, 'W', (afs_uint32 *) V_weekUse(vp),
517                            sizeof(V_weekUse(vp)) / sizeof(V_weekUse(vp)[0]));
518     if (!code)
519         code = DumpInt32(dumpfd, 'D', V_dayUseDate(vp));
520     if (!code)
521         code = DumpInt32(dumpfd, 'Z', V_dayUse(vp));
522     if (!code)
523         code = DumpInt32(dumpfd, 'V', V_volUpCounter(vp));
524     return code;
525 }
526
527 static int
528 DumpShort(int dumpfd, char tag, unsigned int value)
529 {
530     char tbuffer[3];
531     register byte *p = (unsigned char *)tbuffer;
532     *p++ = tag;
533     *p++ = value >> 8;
534     *p = value;
535     return ((write(dumpfd, tbuffer, 3) == 3) ? 0 : VOLSERDUMPERROR);
536 }
537
538 static int
539 DumpByteString(int dumpfd, char tag, register byte * bs, register int nbytes)
540 {
541     int code = 0;
542
543     code = write(dumpfd, &tag, 1);
544     if (code != 1)
545         return VOLSERDUMPERROR;
546     code = write(dumpfd, (char *)bs, nbytes);
547     if (code != nbytes)
548         return VOLSERDUMPERROR;
549     return 0;
550 }
551
552
553 static int
554 DumpFile(int dumpfd, int vnode, FdHandle_t * handleP,  struct VnodeDiskObject *v)
555 {
556     int code = 0, failed_seek = 0, failed_write = 0;
557     afs_int32 pad = 0;
558     afs_int32 offset = 0;
559     afs_sfsize_t n, nbytes, howMany, howBig;
560     byte *p;
561 #ifndef AFS_NT40_ENV
562     struct afs_stat status;
563 #endif
564     afs_sfsize_t size, tmpsize;
565 #ifdef  AFS_AIX_ENV
566 #include <sys/statfs.h>
567     struct statfs tstatfs;
568 #endif
569
570     if (verbose)
571         fprintf(stderr, "dumping file for vnode %d\n", vnode);
572
573 #ifdef AFS_NT40_ENV
574     howBig = _filelength(handleP->fd_fd);
575     howMany = 4096;
576
577 #else
578     afs_fstat(handleP->fd_fd, &status);
579     howBig = status.st_size;
580
581 #ifdef  AFS_AIX_ENV
582     /* Unfortunately in AIX valuable fields such as st_blksize are 
583      * gone from the stat structure.
584      */
585     fstatfs(handleP->fd_fd, &tstatfs);
586     howMany = tstatfs.f_bsize;
587 #else
588     howMany = status.st_blksize;
589 #endif /* AFS_AIX_ENV */
590 #endif /* AFS_NT40_ENV */
591
592
593     size = FDH_SIZE(handleP);
594
595     if (verbose)
596         fprintf(stderr, "  howBig = %u, howMany = %u, fdh size = %u\n",
597                 howBig, howMany, size);
598
599 #ifdef AFS_LARGEFILE_ENV
600     {
601         afs_uint32 hi, lo;
602         SplitInt64(size, hi, lo);
603         if (hi == 0L) {
604             code = DumpInt32(dumpfd, 'f', lo);
605         } else {
606             code = DumpDouble(dumpfd, 'h', hi, lo);
607         }
608     }
609 #else /* !AFS_LARGEFILE_ENV */
610     code = DumpInt32(dumpfd, 'f', size);
611 #endif /* !AFS_LARGEFILE_ENV */
612     if (code) {
613         return VOLSERDUMPERROR;
614     }
615
616     p = (unsigned char *)malloc(howMany);
617     if (!p) {
618         fprintf(stderr, "out of memory!\n");
619         return VOLSERDUMPERROR;
620     }
621
622     /* loop through whole file, while we still have bytes left, and no errors, in chunks of howMany bytes */
623     for (nbytes = size; (nbytes && !failed_write); nbytes -= howMany) {
624         if (nbytes < howMany)
625             howMany = nbytes;
626
627         /* Read the data - unless we know we can't */
628         n = (failed_seek ? 0 : FDH_READ(handleP, p, howMany));
629
630         /* If read any good data and we null padded previously, log the
631          * amount that we had null padded.
632          */
633         if ((n > 0) && pad) {
634             fprintf(stderr, "Null padding file %d bytes at offset %u\n", pad,
635                     offset);
636             pad = 0;
637         }
638
639         /* If didn't read enough data, null padd the rest of the buffer. This
640          * can happen if, for instance, the media has some bad spots. We don't
641          * want to quit the dump, so we start null padding.
642          */
643         if (n < howMany) {
644
645                 if (verbose) fprintf(stderr, "  read %u instead of %u bytes.\n", n, howMany);
646
647             /* Record the read error */
648             if (n < 0) {
649                 n = 0;
650                 fprintf(stderr, "Error %d reading inode %s for vnode %d\n",
651                         errno, PrintInode(NULL, handleP->fd_ih->ih_ino),
652                         vnode);
653             } else if (!pad) {
654                 fprintf(stderr, "Error reading inode %s for vnode %d\n",
655                         PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
656             }
657
658             /* Pad the rest of the buffer with zeros. Remember offset we started 
659              * padding. Keep total tally of padding.
660              */
661             memset(p + n, 0, howMany - n);
662             if (!pad)
663                 offset = (howBig - nbytes) + n;
664             pad += (howMany - n);
665
666             /* Now seek over the data we could not get. An error here means we
667              * can't do the next read.
668              */
669             failed_seek = FDH_SEEK(handleP, ((size - nbytes) + howMany), SEEK_SET);
670             if (failed_seek != ((size - nbytes) + howMany)) {
671                 if (failed_seek < 0) {
672                     fprintf(stderr,
673                             "Error %d seeking in inode %s for vnode %d\n",
674                             errno, PrintInode(NULL, handleP->fd_ih->ih_ino),
675                             vnode);
676                 } else {
677                     fprintf(stderr,
678                             "Error seeking in inode %s for vnode %d\n",
679                             PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
680                     failed_seek = -1;
681                 }
682             } else {
683                 failed_seek = 0;
684             }
685         }
686
687         /* Now write the data out */
688         if (write(dumpfd, (char *)p, howMany) != howMany)
689             failed_write = VOLSERDUMPERROR;
690     }
691
692     if (pad) {                  /* Any padding we hadn't reported yet */
693         fprintf(stderr, "Null padding file: %d bytes at offset %u\n", pad,
694                 offset);
695     }
696
697     free(p);
698     return failed_write;
699 }
700
701
702 static int
703 DumpVnode(int dumpfd, struct VnodeDiskObject *v, int volid, int vnodeNumber,
704           int dumpEverything, struct Volume *vp)
705 {
706     int code = 0;
707     IHandle_t *ihP;
708     FdHandle_t *fdP;
709
710     if (verbose)
711         fprintf(stderr, "dumping vnode %d\n", vnodeNumber);
712
713     if (!v || v->type == vNull)
714         return code;
715     if (!code)
716         code = DumpDouble(dumpfd, D_VNODE, vnodeNumber, v->uniquifier);
717     if (!dumpEverything)
718         return code;
719     if (!code)
720         code = DumpByte(dumpfd, 't', (byte) v->type);
721     if (!code)
722         code = DumpShort(dumpfd, 'l', v->linkCount);    /* May not need this */
723     if (!code)
724         code = DumpInt32(dumpfd, 'v', v->dataVersion);
725     if (!code)
726         code = DumpInt32(dumpfd, 'm', v->unixModifyTime);
727     if (!code)
728         code = DumpInt32(dumpfd, 'a', v->author);
729     if (!code)
730         code = DumpInt32(dumpfd, 'o', v->owner);
731     if (!code && v->group)
732         code = DumpInt32(dumpfd, 'g', v->group);        /* default group is 0 */
733     if (!code)
734         code = DumpShort(dumpfd, 'b', v->modeBits);
735     if (!code)
736         code = DumpInt32(dumpfd, 'p', v->parent);
737     if (!code)
738         code = DumpInt32(dumpfd, 's', v->serverModifyTime);
739     if (v->type == vDirectory) {
740         acl_HtonACL(VVnodeDiskACL(v));
741         if (!code)
742             code =
743                 DumpByteString(dumpfd, 'A', (byte *) VVnodeDiskACL(v),
744                                VAclDiskSize(v));
745     }
746
747     if (VNDISK_GET_INO(v)) {
748         IH_INIT(ihP, V_device(vp), V_parentId(vp), VNDISK_GET_INO(v));
749         fdP = IH_OPEN(ihP);
750         if (fdP == NULL) {
751             fprintf(stderr,
752                     "Unable to open inode %s for vnode %u (volume %i); not dumped, error %d\n",
753                     PrintInode(NULL, VNDISK_GET_INO(v)), vnodeNumber, volid,
754                     errno);
755         }
756         else
757         {
758                 if (verbose)
759                     fprintf(stderr, "about to dump inode %s for vnode %u\n",
760                             PrintInode(NULL, VNDISK_GET_INO(v)), vnodeNumber);
761                 code = DumpFile(dumpfd, vnodeNumber, fdP, v);
762                 FDH_CLOSE(fdP);
763         }
764         IH_RELEASE(ihP);
765     }
766
767     if (verbose)
768         fprintf(stderr, "done dumping vnode %d\n", vnodeNumber);
769     return code;
770 }
771
772
773 static int
774 DumpVnodeIndex(int dumpfd, Volume * vp, VnodeClass class, afs_int32 fromtime,
775                int forcedump)
776 {
777     register int code = 0;
778     register struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
779     char buf[SIZEOF_LARGEDISKVNODE];
780     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
781     StreamHandle_t *file;
782     FdHandle_t *fdP;
783     int size;
784     int flag;
785     int offset = 0;
786     register int vnodeIndex, nVnodes = 0;
787
788     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
789     file = FDH_FDOPEN(fdP, "r+");
790     size = OS_SIZE(fdP->fd_fd);
791     nVnodes = (size / vcp->diskSize) - 1;
792
793     if (nVnodes > 0) {
794         STREAM_SEEK(file, vcp->diskSize, 0);
795     } else
796         nVnodes = 0;
797     for (vnodeIndex = 0;
798          nVnodes && STREAM_READ(vnode, vcp->diskSize, 1, file) == 1 && !code;
799          nVnodes--, vnodeIndex++, offset += vcp->diskSize) {
800         flag = forcedump || (vnode->serverModifyTime >= fromtime);
801         /* Note:  the >= test is very important since some old volumes may not have
802          * a serverModifyTime.  For an epoch dump, this results in 0>=0 test, which
803          * does dump the file! */
804         if (verbose)
805             fprintf(stderr, "about to dump %s vnode %u (vnode offset = %u)\n",
806                         class == vSmall ? "vSmall" : "vLarge",
807                     bitNumberToVnodeNumber(vnodeIndex, class), offset);
808         if (!code)
809             code =
810                 DumpVnode(dumpfd, vnode, V_id(vp),
811                           bitNumberToVnodeNumber(vnodeIndex, class), flag,
812                           vp);
813     }
814     STREAM_CLOSE(file);
815     FDH_CLOSE(fdP);
816     return code;
817 }
818
819
820
821 /* A partial dump (no dump header) */
822 static int
823 DumpPartial(int dumpfd, register Volume * vp, afs_int32 fromtime,
824             int dumpAllDirs)
825 {
826     int code = 0;
827
828     if (verbose)
829         fprintf(stderr, "about to dump the volume header\n");
830     if (!code)
831         code = DumpVolumeHeader(dumpfd, vp);
832
833     if (verbose)
834         fprintf(stderr, "about to dump the large vnode index\n");
835     if (!code)
836         code = DumpVnodeIndex(dumpfd, vp, vLarge, fromtime, dumpAllDirs);
837
838     if (verbose)
839         fprintf(stderr, "about to dump the small vnode index\n");
840     if (!code)
841         code = DumpVnodeIndex(dumpfd, vp, vSmall, fromtime, 0);
842     return code;
843 }
844
845
846
847 static void
848 DoMyVolDump(Volume * vp, struct DiskPartition64 *dp, char *dumpfile, int fromtime)
849 {
850     int code = 0;
851     int dumpAllDirs = 0;
852     int dumpfd = 0;
853
854     if (dumpfile) {
855         unlink(dumpfile);
856         dumpfd =
857             afs_open(dumpfile, O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
858         if (dumpfd < 0) {
859             fprintf(stderr, "Failed to open dump file! Exiting.\n");
860             exit(1);
861         }
862     } else {
863         dumpfd = 1;             /* stdout */
864     }
865
866     if (verbose)
867         fprintf(stderr, "about to dump the dump header\n");
868     if (!code)
869         code = DumpDumpHeader(dumpfd, vp, fromtime);
870
871     if (verbose)
872         fprintf(stderr, "about to dump volume contents\n");
873     if (!code)
874         code = DumpPartial(dumpfd, vp, fromtime, dumpAllDirs);
875
876     if (verbose)
877         fprintf(stderr, "about to dump the dump postamble\n");
878     if (!code)
879         code = DumpEnd(dumpfd);
880
881     if (verbose)
882         fprintf(stderr, "finished dump\n");
883     close(dumpfd);              /* might be closing stdout, no harm */
884 }