volid-unsigned-int32-20090323
[openafs.git] / src / vol / vol-info.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-info.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 "volinodes.h"
52 #include <afs/afssyscalls.h>
53 #include <afs/afsutil.h>
54     
55 #ifdef _AIX
56 #include <time.h>
57 #endif
58
59 #include <dirent.h>
60
61 #ifdef O_LARGEFILE
62 #define afs_stat        stat64
63 #define afs_fstat       fstat64
64 #define afs_open        open64
65 #else /* !O_LARGEFILE */
66 #define afs_stat        stat
67 #define afs_fstat       fstat
68 #define afs_open        open
69 #endif /* !O_LARGEFILE */
70
71 int DumpVnodes = 0;             /* Dump everything, i.e. summary of all vnodes */
72 int DumpInodeNumber = 0;        /* Dump inode numbers with vnodes */
73 int DumpDate = 0;               /* Dump vnode date (server modify date) with vnode */
74 int InodeTimes = 0;             /* Dump some of the dates associated with inodes */
75 #if defined(AFS_NAMEI_ENV)
76 int PrintFileNames = 0;
77 #endif
78 int online = 0;
79 int dheader = 0;
80 int dsizeOnly = 0, totvolsize = 0, Vauxsize = 0, Vdiskused = 0, Vvnodesize =
81     0;
82 int Vvnodesize_k = 0, Vauxsize_k = 0;
83 int Totvolsize = 0, TVauxsize = 0, TVdiskused = 0, TVvnodesize = 0;
84 int Stotvolsize = 0, SVauxsize = 0, SVdiskused = 0, SVvnodesize = 0;
85 int fixheader = 0, saveinodes = 0, orphaned = 0;
86 int VolumeChanged;
87
88 /* Forward Declarations */
89 void PrintHeader(register Volume * vp);
90 void HandleAllPart(void);
91 void HandlePart(struct DiskPartition64 *partP);
92 void HandleVolume(struct DiskPartition64 *partP, char *name);
93 struct DiskPartition64 *FindCurrentPartition(void);
94 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
95                      register struct VolumeHeader *header);
96 #if defined(AFS_NAMEI_ENV)
97 void PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
98                 Inode ino, Volume * vp);
99 #else
100 void PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
101                 Inode ino);
102 #endif
103 void PrintVnodes(Volume * vp, VnodeClass class);
104
105 char *
106 date(time_t date)
107 {
108 #define MAX_DATE_RESULT 100
109     static char results[8][MAX_DATE_RESULT];
110     static int next;
111     struct tm *tm = localtime(&date);
112     char buf[32];
113
114     (void)strftime(buf, 32, "%Y/%m/%d.%H:%M:%S", tm);   /* NT does not have %T */
115     (void)afs_snprintf(results[next = (next + 1) & 7], MAX_DATE_RESULT,
116                        "%lu (%s)", (unsigned long)date, buf);
117     return results[next];
118 }
119
120 #ifndef AFS_NT40_ENV
121 #include "AFS_component_version_number.c"
122 #endif
123
124 char name[VMAXPATHLEN];
125
126 char BU[1000];
127
128 int
129 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
130 {
131     struct versionStamp *vsn;
132     int bad = 0;
133     int code;
134
135     vsn = (struct versionStamp *)to;
136
137     code = IH_IREAD(ih, 0, to, size);
138     if (code != size)
139         return -1;
140
141     if (vsn->magic != magic) {
142         bad++;
143         printf("Inode %s: Bad magic %x (%x): IGNORED\n",
144                PrintInode(NULL, ih->ih_ino), vsn->magic, magic);
145     }
146
147     /* Check is conditional, in case caller wants to inspect version himself */
148     if (version && vsn->version != version) {
149         bad++;
150         printf("Inode %s: Bad version %x (%x): IGNORED\n",
151                PrintInode(NULL, ih->ih_ino), vsn->version, version);
152     }
153     if (bad && fixheader) {
154         vsn->magic = magic;
155         vsn->version = version;
156         printf("Special index inode %s has a bad header. Reconstructing...\n",
157                PrintInode(NULL, ih->ih_ino));
158         code = IH_IWRITE(ih, 0, to, size);
159         if (code != size) {
160             printf("Write failed; header left in damaged state\n");
161         }
162     } else {
163         if (!dsizeOnly && !saveinodes) {
164             printf("Inode %s: Good magic %x and version %x\n",
165                    PrintInode(NULL, ih->ih_ino), magic, version);
166         }
167     }
168     return 0;
169 }
170
171
172 Volume *
173 AttachVolume(struct DiskPartition64 * dp, char *volname,
174              register struct VolumeHeader * header)
175 {
176     register Volume *vp;
177     afs_int32 ec = 0;
178
179     vp = (Volume *) calloc(1, sizeof(Volume));
180     vp->specialStatus = 0;
181     vp->device = dp->device;
182     vp->partition = dp;
183     IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
184             header->largeVnodeIndex);
185     IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
186             header->smallVnodeIndex);
187     IH_INIT(vp->diskDataHandle, dp->device, header->parent,
188             header->volumeInfo);
189     IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
190     vp->cacheCheck = 0;         /* XXXX */
191     vp->shuttingDown = 0;
192     vp->goingOffline = 0;
193     vp->nUsers = 1;
194     vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
195     ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
196                   sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
197     if (!ec) {
198         struct IndexFileHeader iHead;
199         ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
200                       sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
201     }
202     if (!ec) {
203         struct IndexFileHeader iHead;
204         ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
205                       sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
206     }
207 #ifdef AFS_NAMEI_ENV
208     if (!ec) {
209         struct versionStamp stamp;
210         ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
211                       LINKTABLEMAGIC, LINKTABLEVERSION);
212     }
213 #endif
214     if (ec)
215         return (Volume *) 0;
216     return vp;
217 }
218
219
220 static int
221 handleit(struct cmd_syndesc *as, void *arock)
222 {
223     register struct cmd_item *ti;
224     int err = 0;
225     afs_uint32 volumeId = 0;
226     char *partName = 0;
227     struct DiskPartition64 *partP = NULL;
228
229
230 #ifndef AFS_NT40_ENV
231     if (geteuid() != 0) {
232         printf("vol-info must be run as root; sorry\n");
233         exit(1);
234     }
235 #endif
236
237     if (as->parms[0].items)
238         online = 1;
239     else
240         online = 0;
241     if (as->parms[1].items)
242         DumpVnodes = 1;
243     else
244         DumpVnodes = 0;
245     if (as->parms[2].items)
246         DumpDate = 1;
247     else
248         DumpDate = 0;
249     if (as->parms[3].items)
250         DumpInodeNumber = 1;
251     else
252         DumpInodeNumber = 0;
253     if (as->parms[4].items)
254         InodeTimes = 1;
255     else
256         InodeTimes = 0;
257     if ((ti = as->parms[5].items))
258         partName = ti->data;
259     if ((ti = as->parms[6].items))
260         volumeId = strtoul(ti->data, NULL, 10);
261     if (as->parms[7].items)
262         dheader = 1;
263     else
264         dheader = 0;
265     if (as->parms[8].items) {
266         dsizeOnly = 1;
267         dheader = 1;
268         DumpVnodes = 1;
269     } else
270         dsizeOnly = 0;
271     if (as->parms[9].items) {
272         fixheader = 1;
273     } else
274         fixheader = 0;
275     if (as->parms[10].items) {
276         saveinodes = 1;
277         dheader = 1;
278         DumpVnodes = 1;
279     } else
280         saveinodes = 0;
281     if (as->parms[11].items) {
282         orphaned = 1;
283         DumpVnodes = 1;
284     } else
285 #if defined(AFS_NAMEI_ENV)
286     if (as->parms[12].items) {
287         PrintFileNames = 1;
288         DumpVnodes = 1;
289     } else
290 #endif
291         orphaned = 0;
292
293     DInit(10);
294
295     err = VAttachPartitions();
296     if (err) {
297         printf("%d partitions had errors during attach.\n", err);
298     }
299
300     if (partName) {
301         partP = VGetPartition(partName, 0);
302         if (!partP) {
303             printf("%s is not an AFS partition name on this server.\n",
304                    partName);
305             exit(1);
306         }
307     }
308
309     if (!volumeId) {
310         if (!partP) {
311             HandleAllPart();
312         } else {
313             HandlePart(partP);
314         }
315     } else {
316         char name1[128];
317
318         if (!partP) {
319             partP = FindCurrentPartition();
320             if (!partP) {
321                 printf("Current partition is not a vice partition.\n");
322                 exit(1);
323             }
324         }
325         (void)afs_snprintf(name1, sizeof name1, VFORMAT,
326                            afs_cast_uint32(volumeId));
327         if (dsizeOnly && !saveinodes)
328             printf
329                 ("Volume-Id\t  Volsize  Auxsize Inodesize  AVolsize SizeDiff                (VolName)\n");
330         HandleVolume(partP, name1);
331     }
332     return 0;
333 }
334
335 #ifdef AFS_NT40_ENV
336 #include <direct.h>
337 struct DiskPartition64 *
338 FindCurrentPartition(void)
339 {
340     int dr = _getdrive();
341     struct DiskPartition64 *dp;
342
343     dr--;
344     for (dp = DiskPartitionList; dp; dp = dp->next) {
345         if (*dp->devName - 'A' == dr)
346             break;
347     }
348     if (!dp) {
349         printf("Current drive is not a valid vice partition.\n");
350     }
351     return dp;
352 }
353 #else
354 struct DiskPartition64 *
355 FindCurrentPartition(void)
356 {
357     char partName[1024];
358     char tmp = '\0';
359     char *p;
360     struct DiskPartition64 *dp;
361
362     if (!getcwd(partName, 1023)) {
363         perror("pwd");
364         exit(1);
365     }
366     p = strchr(&partName[1], '/');
367     if (p) {
368         tmp = *p;
369         *p = '\0';
370     }
371     if (!(dp = VGetPartition(partName, 0))) {
372         if (tmp)
373             *p = tmp;
374         printf("%s is not a valid vice partition.\n", partName);
375         exit(1);
376     }
377     return dp;
378 }
379 #endif
380
381 void
382 HandleAllPart(void)
383 {
384     struct DiskPartition64 *partP;
385
386
387     for (partP = DiskPartitionList; partP; partP = partP->next) {
388         printf("Processing Partition %s:\n", partP->name);
389         HandlePart(partP);
390         Stotvolsize += Totvolsize;
391         SVauxsize += TVauxsize;
392         SVvnodesize += TVvnodesize;
393         SVdiskused += TVdiskused;
394     }
395
396     if (dsizeOnly) {
397         printf("\nServer Totals%12d%9d%10d%10d%9d\n", SVdiskused, SVauxsize,
398                SVvnodesize, Stotvolsize, Stotvolsize - SVdiskused);
399     }
400 }
401
402
403 void
404 HandlePart(struct DiskPartition64 *partP)
405 {
406     int nvols = 0;
407     DIR *dirp;
408     struct dirent *dp;
409 #ifdef AFS_NT40_ENV
410     char pname[64];
411     char *p = pname;
412     (void)sprintf(pname, "%s\\", VPartitionPath(partP));
413 #else
414     char *p = VPartitionPath(partP);
415 #endif
416
417     if ((dirp = opendir(p)) == NULL) {
418         printf("Can't read directory %s; giving up\n", p);
419         exit(1);
420     }
421     if (dsizeOnly && !saveinodes)
422         printf
423             ("Volume-Id\t  Volsize  Auxsize Inodesize  AVolsize SizeDiff                (VolName)\n");
424     while ((dp = readdir(dirp))) {
425         p = (char *)strrchr(dp->d_name, '.');
426         if (p != NULL && strcmp(p, VHDREXT) == 0) {
427             HandleVolume(partP, dp->d_name);
428             Totvolsize += totvolsize;
429             TVauxsize += Vauxsize;
430             TVvnodesize += Vvnodesize;
431             TVdiskused += Vdiskused;
432             nvols++;
433         }
434     }
435     closedir(dirp);
436     if (dsizeOnly) {
437         printf("\nPart Totals  %12d%9d%10d%10d%9d (%d volumes)\n\n",
438                TVdiskused, TVauxsize, TVvnodesize, Totvolsize,
439                Totvolsize - TVdiskused, nvols);
440     }
441 }
442
443
444 void
445 HandleVolume(struct DiskPartition64 *dp, char *name)
446 {
447     struct VolumeHeader header;
448     struct VolumeDiskHeader diskHeader;
449     struct afs_stat status, stat;
450     register int fd;
451     Volume *vp;
452     IHandle_t *ih;
453     char headerName[1024];
454
455     if (online) {
456         printf("volinfo: -online not supported\n");
457         exit(1);
458     } else {
459         afs_int32 n;
460
461         (void)afs_snprintf(headerName, sizeof headerName, "%s/%s",
462                            VPartitionPath(dp), name);
463         if ((fd = afs_open(headerName, O_RDONLY)) == -1
464             || afs_fstat(fd, &status) == -1) {
465             printf("Volinfo: Cannot read volume header %s\n", name);
466             close(fd);
467             exit(1);
468         }
469         n = read(fd, &diskHeader, sizeof(diskHeader));
470
471         if (n != sizeof(diskHeader)
472             || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
473             printf("Volinfo: Error reading volume header %s\n", name);
474             exit(1);
475         }
476         if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
477             printf
478                 ("Volinfo: Volume %s, version number is incorrect; volume needs salvage\n",
479                  name);
480             exit(1);
481         }
482         DiskToVolumeHeader(&header, &diskHeader);
483
484         if (dheader) {
485             FdHandle_t *fdP;
486             int size = 0;
487             int code;
488
489             if (afs_fstat(fd, &stat) == -1) {
490                 perror("stat");
491                 exit(1);
492             }
493             if (!dsizeOnly && !saveinodes) {
494                 printf("Volume header (size = %d):\n", size = stat.st_size);
495                 printf("\tstamp\t= 0x%x\n", header.stamp.version);
496                 printf("\tVolId\t= %u\n", header.id);
497             }
498
499             IH_INIT(ih, dp->device, header.parent, header.volumeInfo);
500             fdP = IH_OPEN(ih);
501             if (fdP == NULL) {
502                 perror("opening volume info");
503                 exit(1);
504             }
505             code = FDH_SIZE(fdP);
506             if (code == -1) {
507                 perror("fstat");
508                 exit(1);
509             }
510             FDH_REALLYCLOSE(fdP);
511             IH_RELEASE(ih);
512             size += code;
513             if (!dsizeOnly && !saveinodes) {
514                 printf("\tparent\t= %u\n", header.parent);
515                 printf("\tInfo inode\t= %s (size = %d)\n",
516                        PrintInode(NULL, header.volumeInfo), code);
517             }
518
519             IH_INIT(ih, dp->device, header.parent, header.smallVnodeIndex);
520             fdP = IH_OPEN(ih);
521             if (fdP == NULL) {
522                 perror("opening small vnode index");
523                 exit(1);
524             }
525             code = FDH_SIZE(fdP);
526             if (code == -1) {
527                 perror("fstat");
528                 exit(1);
529             }
530             FDH_REALLYCLOSE(fdP);
531             IH_RELEASE(ih);
532             size += code;
533             if (!dsizeOnly && !saveinodes) {
534                 printf("\tSmall inode\t= %s (size = %d)\n",
535                        PrintInode(NULL, header.smallVnodeIndex), code);
536             }
537
538             IH_INIT(ih, dp->device, header.parent, header.largeVnodeIndex);
539             fdP = IH_OPEN(ih);
540             if (fdP == NULL) {
541                 perror("opening large vnode index");
542                 exit(1);
543             }
544             code = FDH_SIZE(fdP);
545             if (code == -1) {
546                 perror("fstat");
547                 exit(1);
548             }
549             FDH_REALLYCLOSE(fdP);
550             IH_RELEASE(ih);
551             size += code;
552             if (!dsizeOnly && !saveinodes) {
553                 printf("\tLarge inode\t= %s (size = %d)\n",
554                        PrintInode(NULL, header.largeVnodeIndex), code);
555 #ifndef AFS_NT40_ENV
556                 printf("Total aux volume size = %d\n\n", size);
557 #endif
558             }
559 #ifdef AFS_NAMEI_ENV
560             IH_INIT(ih, dp->device, header.parent, header.linkTable);
561             fdP = IH_OPEN(ih);
562             if (fdP == NULL) {
563                 perror("opening link table index");
564                 exit(1);
565             }
566             code = FDH_SIZE(fdP);
567             if (code == -1) {
568                 perror("fstat");
569                 exit(1);
570             }
571             FDH_REALLYCLOSE(fdP);
572             IH_RELEASE(ih);
573             size += code;
574             if (!dsizeOnly && !saveinodes) {
575                 printf("\tLink inode\t= %s (size = %d)\n",
576                        PrintInode(NULL, header.linkTable), code);
577                 printf("Total aux volume size = %d\n\n", size);
578             }
579 #endif
580             Vauxsize = size;
581             Vauxsize_k = size / 1024;
582         }
583         close(fd);
584         vp = AttachVolume(dp, name, &header);
585         if (!vp) {
586             printf("Volinfo: Error attaching volume header %s\n", name);
587             return;
588         }
589     }
590     PrintHeader(vp);
591     if (DumpVnodes) {
592         if (!dsizeOnly && !saveinodes)
593             printf("\nLarge vnodes (directories)\n");
594         PrintVnodes(vp, vLarge);
595         if (!dsizeOnly && !saveinodes) {
596             printf("\nSmall vnodes(files, symbolic links)\n");
597             fflush(stdout);
598         }
599         if (saveinodes)
600             printf("Saving all volume files to current directory ...\n");
601         PrintVnodes(vp, vSmall);
602     }
603     if (dsizeOnly) {
604         totvolsize = Vauxsize_k + Vvnodesize_k;
605         if (saveinodes)
606             printf
607                 ("Volume-Id\t  Volsize  Auxsize Inodesize  AVolsize SizeDiff                (VolName)\n");
608         printf("%u\t%9d%9d%10d%10d%9d\t%24s\n", V_id(vp), Vdiskused,
609                Vauxsize_k, Vvnodesize_k, totvolsize, totvolsize - Vdiskused,
610                V_name(vp));
611     }
612     free(vp->header);
613     free(vp);
614 }
615
616 int
617 main(int argc, char **argv)
618 {
619     register struct cmd_syndesc *ts;
620     afs_int32 code;
621
622     ts = cmd_CreateSyntax(NULL, handleit, NULL, "Dump volume's internal state");
623     cmd_AddParm(ts, "-online", CMD_FLAG, CMD_OPTIONAL,
624                 "Get info from running fileserver");
625     cmd_AddParm(ts, "-vnode", CMD_FLAG, CMD_OPTIONAL, "Dump vnode info");
626     cmd_AddParm(ts, "-date", CMD_FLAG, CMD_OPTIONAL,
627                 "Also dump vnode's mod date");
628     cmd_AddParm(ts, "-inode", CMD_FLAG, CMD_OPTIONAL,
629                 "Dump vnode's inode number");
630     cmd_AddParm(ts, "-itime", CMD_FLAG, CMD_OPTIONAL,
631                 "Dump special inode's mod times");
632     cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL,
633                 "AFS partition name (default current partition)");
634     cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
635     cmd_AddParm(ts, "-header", CMD_FLAG, CMD_OPTIONAL,
636                 "Dump volume's header info");
637     cmd_AddParm(ts, "-sizeOnly", CMD_FLAG, CMD_OPTIONAL,
638                 "Dump volume's size");
639     cmd_AddParm(ts, "-fixheader", CMD_FLAG, CMD_OPTIONAL,
640                 "Try to fix header");
641     cmd_AddParm(ts, "-saveinodes", CMD_FLAG, CMD_OPTIONAL,
642                 "Try to save all inodes");
643     cmd_AddParm(ts, "-orphaned", CMD_FLAG, CMD_OPTIONAL,
644                 "List all dir/files without a parent");
645 #if defined(AFS_NAMEI_ENV)
646     cmd_AddParm(ts, "-filenames", CMD_FLAG, CMD_OPTIONAL, "Print filenames");
647 #endif
648     code = cmd_Dispatch(argc, argv);
649     return code;
650 }
651
652 #define typestring(type) (type == RWVOL? "read/write": type == ROVOL? "readonly": type == BACKVOL? "backup": "unknown")
653
654 void
655 PrintHeader(register Volume * vp)
656 {
657     Vdiskused = V_diskused(vp);
658     if (dsizeOnly || saveinodes)
659         return;
660     printf("Volume header for volume %u (%s)\n", V_id(vp), V_name(vp));
661     printf("stamp.magic = %x, stamp.version = %u\n", V_stamp(vp).magic,
662            V_stamp(vp).version);
663     printf
664         ("inUse = %d, inService = %d, blessed = %d, needsSalvaged = %d, dontSalvage = %d\n",
665          V_inUse(vp), V_inService(vp), V_blessed(vp), V_needsSalvaged(vp),
666          V_dontSalvage(vp));
667     printf
668         ("type = %d (%s), uniquifier = %u, needsCallback = %d, destroyMe = %x\n",
669          V_type(vp), typestring(V_type(vp)), V_uniquifier(vp),
670          V_needsCallback(vp), V_destroyMe(vp));
671     printf
672         ("id = %u, parentId = %u, cloneId = %u, backupId = %u, restoredFromId = %u\n",
673          V_id(vp), V_parentId(vp), V_cloneId(vp), V_backupId(vp),
674          V_restoredFromId(vp));
675     printf
676         ("maxquota = %d, minquota = %d, maxfiles = %d, filecount = %d, diskused = %d\n",
677          V_maxquota(vp), V_minquota(vp), V_maxfiles(vp), V_filecount(vp),
678          V_diskused(vp));
679     printf("creationDate = %s, copyDate = %s\n", date(V_creationDate(vp)),
680            date(V_copyDate(vp)));
681     printf("backupDate = %s, expirationDate = %s\n", date(V_backupDate(vp)),
682            date(V_expirationDate(vp)));
683     printf("accessDate = %s, updateDate = %s\n", date(V_accessDate(vp)),
684            date(V_updateDate(vp)));
685     printf("owner = %u, accountNumber = %u\n", V_owner(vp),
686            V_accountNumber(vp));
687     printf
688         ("dayUse = %u; week = (%u, %u, %u, %u, %u, %u, %u), dayUseDate = %s\n",
689          V_dayUse(vp), V_weekUse(vp)[0], V_weekUse(vp)[1], V_weekUse(vp)[2],
690          V_weekUse(vp)[3], V_weekUse(vp)[4], V_weekUse(vp)[5],
691          V_weekUse(vp)[6], date(V_dayUseDate(vp)));
692     printf("volUpdateCounter = %u\n", V_volUpCounter(vp));
693 }
694
695 /* GetFileInfo
696  * OS independent file info. Die on failure.
697  */
698 #ifdef AFS_NT40_ENV
699 char *
700 NT_date(FILETIME * ft)
701 {
702     static char result[8][64];
703     static int next = 0;
704     SYSTEMTIME st;
705     FILETIME lft;
706
707     if (!FileTimeToLocalFileTime(ft, &lft)
708         || !FileTimeToSystemTime(&lft, &st)) {
709         printf("Time conversion failed.\n");
710         exit(1);
711     }
712     sprintf(result[next = ((next + 1) & 7)], "%4d/%02d/%02d.%2d:%2d:%2d",
713             st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
714     return result[next];
715 }
716 #endif
717
718 static void
719 GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime)
720 {
721 #ifdef AFS_NT40_ENV
722     BY_HANDLE_FILE_INFORMATION fi;
723     if (!GetFileInformationByHandle(fd, &fi)) {
724         printf("GetFileInformationByHandle failed, exiting\n");
725         exit(1);
726     }
727     *size = (int)fi.nFileSizeLow;
728     *ctime = "N/A";
729     *mtime = NT_date(&fi.ftLastWriteTime);
730     *atime = NT_date(&fi.ftLastAccessTime);
731 #else
732     struct afs_stat status;
733     if (afs_fstat(fd, &status) == -1) {
734         printf("fstat failed %d\n", errno);
735         exit(1);
736     }
737     *size = (int)status.st_size;
738     *ctime = date(status.st_ctime);
739     *mtime = date(status.st_mtime);
740     *atime = date(status.st_atime);
741 #endif
742 }
743
744 void
745 PrintVnodes(Volume * vp, VnodeClass class)
746 {
747     afs_int32 diskSize =
748         (class == vSmall ? SIZEOF_SMALLDISKVNODE : SIZEOF_LARGEDISKVNODE);
749     char buf[SIZEOF_LARGEDISKVNODE];
750     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
751     StreamHandle_t *file;
752     register int vnodeIndex, nVnodes, offset = 0;
753     Inode ino;
754     IHandle_t *ih = vp->vnodeIndex[class].handle;
755     FdHandle_t *fdP;
756     int size;
757     char *ctime, *atime, *mtime;
758     char nfile[50], buffer[256];
759     int total, ofd, len, code, bad = 0;
760
761     fdP = IH_OPEN(ih);
762     if (fdP == NULL) {
763         printf("open failed\n");
764         exit(1);
765     }
766
767     file = FDH_FDOPEN(fdP, "r");
768     if (!file) {
769         printf("fdopen failed\n");
770         exit(1);
771     }
772
773     GetFileInfo(fdP->fd_fd, &size, &ctime, &atime, &mtime);
774     if (InodeTimes && !dsizeOnly) {
775         printf("ichanged : %s\nimodified: %s\niaccessed: %s\n\n", ctime,
776                mtime, atime);
777     }
778
779     nVnodes = (size / diskSize) - 1;
780     if (nVnodes > 0) {
781         STREAM_SEEK(file, diskSize, 0);
782     } else
783         nVnodes = 0;
784
785     for (vnodeIndex = 0;
786          nVnodes && STREAM_READ(vnode, diskSize, 1, file) == 1;
787          nVnodes--, vnodeIndex++, offset += diskSize) {
788
789         ino = VNDISK_GET_INO(vnode);
790         if (saveinodes) {
791             if (VALID_INO(ino) && (class == vSmall)) {
792                 IHandle_t *ih1;
793                 FdHandle_t *fdP1;
794                 IH_INIT(ih1, V_device(vp), V_parentId(vp), ino);
795                 fdP1 = IH_OPEN(ih1);
796                 if (fdP1 == NULL) {
797                     printf("Can't open inode %s error %d (ignored)\n",
798                            PrintInode(NULL, ino), errno);
799                     continue;
800                 }
801                 (void)afs_snprintf(nfile, sizeof nfile, "TmpInode.%s",
802                                    PrintInode(NULL, ino));
803                 ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600);
804                 if (ofd < 0) {
805                     printf("Can't create file %s; error %d (ignored)\n",
806                            nfile, errno);
807                     continue;
808                 }
809                 total = bad = 0;
810                 while (1) {
811                     len = FDH_READ(fdP1, buffer, sizeof(buffer));
812                     if (len < 0) {
813                         FDH_REALLYCLOSE(fdP1);
814                         IH_RELEASE(ih1);
815                         close(ofd);
816                         unlink(nfile);
817                         printf
818                             ("Error while reading from inode %s (%d - ignored)\n",
819                              PrintInode(NULL, ino), errno);
820                         bad = 1;
821                         break;
822                     }
823                     if (len == 0)
824                         break;  /* No more input */
825                     code = write(ofd, buffer, len);
826                     if (code != len) {
827                         FDH_REALLYCLOSE(fdP1);
828                         IH_RELEASE(ih1);
829                         close(ofd);
830                         unlink(nfile);
831                         printf
832                             ("Error while writing to \"%s\" (%d - ignored)\n",
833                              nfile, errno);
834                         bad = 1;
835                         break;
836                     }
837                     total += len;
838                 }
839                 if (bad)
840                     continue;
841                 FDH_REALLYCLOSE(fdP1);
842                 IH_RELEASE(ih1);
843                 close(ofd);
844                 printf("... Copied inode %s to file %s (%d bytes)\n",
845                        PrintInode(NULL, ino), nfile, total);
846             }
847         } else {
848 #if defined(AFS_NAMEI_ENV)
849             PrintVnode(offset, vnode,
850                        bitNumberToVnodeNumber(vnodeIndex, class), ino, vp);
851 #else
852             PrintVnode(offset, vnode,
853                        bitNumberToVnodeNumber(vnodeIndex, class), ino);
854 #endif
855         }
856     }
857     STREAM_CLOSE(file);
858     FDH_CLOSE(fdP);
859 }
860
861 #if defined(AFS_NAMEI_ENV)
862 void
863 PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
864            Inode ino, Volume * vp)
865 #else
866 void
867 PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
868            Inode ino)
869 #endif
870 {
871 #if defined(AFS_NAMEI_ENV)
872     IHandle_t *ihtmpp;
873 #if !defined(AFS_NT40_ENV)
874     namei_t filename;
875 #else
876     char filename[MAX_PATH];
877 #endif
878 #endif
879     afs_fsize_t fileLength;
880
881     VNDISK_GET_LEN(fileLength, vnode);
882     Vvnodesize += fileLength;
883     Vvnodesize_k += fileLength / 1024;
884     if (dsizeOnly)
885         return;
886     if (orphaned && (fileLength == 0 || vnode->parent || !offset))
887         return;
888     printf
889         ("%10d Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
890          offset, vnodeNumber, vnode->uniquifier, vnode->dataVersion,
891          vnode->cloned, (afs_uintmax_t) fileLength, vnode->linkCount,
892          vnode->parent);
893     if (DumpInodeNumber)
894         printf(" inode: %s", PrintInode(NULL, ino));
895     if (DumpDate)
896         printf(" ServerModTime: %s", date(vnode->serverModifyTime));
897 #if defined(AFS_NAMEI_ENV)
898     if (PrintFileNames) {
899         IH_INIT(ihtmpp, V_device(vp), V_parentId(vp), ino);
900 #if !defined(AFS_NT40_ENV)
901         namei_HandleToName(&filename, ihtmpp);
902         printf(" UFS-Filename: %s", filename.n_path);
903 #else
904         nt_HandleToName(filename, ihtmpp);
905         printf(" NTFS-Filename: %s", filename);
906 #endif
907     }
908 #endif
909     printf("\n");
910 }