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