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