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