2 * Copyright 2000, International Business Machines Corporation and others.
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
13 Institution: The Information Technology Center, Carnegie-Mellon University
17 #include <afsconfig.h>
18 #include <afs/param.h>
24 #ifdef HAVE_SYS_FILE_H
32 #include <afs/afsint.h>
34 #include <afs/errors.h>
37 #include <afs/afssyscalls.h>
41 #include "partition.h"
42 #include "viceinode.h"
43 #include <afs/afssyscalls.h>
44 #include <afs/afsutil.h>
46 static const char *progname = "volinfo";
48 int DumpVnodes = 0; /* Dump everything, i.e. summary of all vnodes */
49 int DumpInodeNumber = 0; /* Dump inode numbers with vnodes */
50 int DumpDate = 0; /* Dump vnode date (server modify date) with vnode */
51 int InodeTimes = 0; /* Dump some of the dates associated with inodes */
52 #if defined(AFS_NAMEI_ENV)
53 int PrintFileNames = 0;
57 int dsizeOnly = 0, totvolsize = 0, Vauxsize = 0, Vdiskused = 0, Vvnodesize =
59 int Vvnodesize_k = 0, Vauxsize_k = 0;
60 int Totvolsize = 0, TVauxsize = 0, TVdiskused = 0, TVvnodesize = 0;
61 int Stotvolsize = 0, SVauxsize = 0, SVdiskused = 0, SVvnodesize = 0;
62 int fixheader = 0, saveinodes = 0, orphaned = 0;
65 /* Forward Declarations */
66 void PrintHeader(Volume * vp);
67 void HandleAllPart(void);
68 void HandlePart(struct DiskPartition64 *partP);
69 void HandleVolume(struct DiskPartition64 *partP, char *name);
70 struct DiskPartition64 *FindCurrentPartition(void);
71 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
72 struct VolumeHeader *header);
73 #if defined(AFS_NAMEI_ENV)
74 void PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
75 Inode ino, Volume * vp);
77 void PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
80 void PrintVnodes(Volume * vp, VnodeClass class);
85 #define MAX_DATE_RESULT 100
86 static char results[8][MAX_DATE_RESULT];
88 struct tm *tm = localtime(&date);
91 (void)strftime(buf, 32, "%Y/%m/%d.%H:%M:%S", tm); /* NT does not have %T */
92 snprintf(results[next = (next + 1) & 7], MAX_DATE_RESULT,
93 "%lu (%s)", (unsigned long)date, buf);
98 #include "AFS_component_version_number.c"
101 char name[VMAXPATHLEN];
106 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
108 struct versionStamp *vsn;
112 vsn = (struct versionStamp *)to;
114 code = IH_IREAD(ih, 0, to, size);
118 if (vsn->magic != magic) {
120 fprintf(stderr, "%s: Inode %s: Bad magic %x (%x): IGNORED\n",
121 progname, PrintInode(NULL, ih->ih_ino), vsn->magic, magic);
124 /* Check is conditional, in case caller wants to inspect version himself */
125 if (version && vsn->version != version) {
127 fprintf(stderr, "%s: Inode %s: Bad version %x (%x): IGNORED\n",
129 PrintInode(NULL, ih->ih_ino), vsn->version, version);
131 if (bad && fixheader) {
133 vsn->version = version;
134 printf("Special index inode %s has a bad header. Reconstructing...\n",
135 PrintInode(NULL, ih->ih_ino));
136 code = IH_IWRITE(ih, 0, to, size);
139 "%s: Write failed for inode %s; header left in damaged state\n",
140 progname, PrintInode(NULL, ih->ih_ino));
143 if (!dsizeOnly && !saveinodes) {
144 printf("Inode %s: Good magic %x and version %x\n",
145 PrintInode(NULL, ih->ih_ino), magic, version);
153 AttachVolume(struct DiskPartition64 * dp, char *volname,
154 struct VolumeHeader * header)
159 vp = (Volume *) calloc(1, sizeof(Volume));
160 vp->specialStatus = 0;
161 vp->device = dp->device;
163 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
164 header->largeVnodeIndex);
165 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
166 header->smallVnodeIndex);
167 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
169 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
170 vp->cacheCheck = 0; /* XXXX */
171 vp->shuttingDown = 0;
172 vp->goingOffline = 0;
174 vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
175 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
176 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
178 struct IndexFileHeader iHead;
179 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
180 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
183 struct IndexFileHeader iHead;
184 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
185 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
189 struct versionStamp stamp;
190 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
191 LINKTABLEMAGIC, LINKTABLEVERSION);
201 handleit(struct cmd_syndesc *as, void *arock)
205 afs_uint32 volumeId = 0;
207 struct DiskPartition64 *partP = NULL;
211 if (geteuid() != 0) {
212 fprintf(stderr, "%s: Must be run as root; sorry\n", progname);
217 if (as->parms[0].items)
221 if (as->parms[1].items)
225 if (as->parms[2].items)
229 if (as->parms[3].items)
233 if (as->parms[4].items)
237 if ((ti = as->parms[5].items))
239 if ((ti = as->parms[6].items))
240 volumeId = strtoul(ti->data, NULL, 10);
241 if (as->parms[7].items)
245 if (as->parms[8].items) {
251 if (as->parms[9].items) {
255 if (as->parms[10].items) {
261 if (as->parms[11].items) {
266 #if defined(AFS_NAMEI_ENV)
267 if (as->parms[12].items) {
276 err = VAttachPartitions();
278 fprintf(stderr, "%s: %d partitions had errors during attach.\n",
283 partP = VGetPartition(partName, 0);
285 fprintf(stderr, "%s: %s is not an AFS partition name on this server.\n",
301 partP = FindCurrentPartition();
304 "%s: Current partition is not a vice partition.\n",
309 snprintf(name1, sizeof name1, VFORMAT,
310 afs_printable_uint32_lu(volumeId));
311 if (dsizeOnly && !saveinodes)
313 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
314 HandleVolume(partP, name1);
321 struct DiskPartition64 *
322 FindCurrentPartition(void)
324 int dr = _getdrive();
325 struct DiskPartition64 *dp;
328 for (dp = DiskPartitionList; dp; dp = dp->next) {
329 if (*dp->devName - 'A' == dr)
333 fprintf(stderr, "%s: Current drive is not a valid vice partition.\n",
339 struct DiskPartition64 *
340 FindCurrentPartition(void)
345 struct DiskPartition64 *dp;
347 if (!getcwd(partName, 1023)) {
348 fprintf(stderr, "%s: Failed to get current working directory: ",
353 p = strchr(&partName[1], OS_DIRSEPC);
358 if (!(dp = VGetPartition(partName, 0))) {
361 fprintf(stderr, "%s: %s is not a valid vice partition.\n", progname,
372 struct DiskPartition64 *partP;
375 for (partP = DiskPartitionList; partP; partP = partP->next) {
376 printf("Processing Partition %s:\n", partP->name);
378 Stotvolsize += Totvolsize;
379 SVauxsize += TVauxsize;
380 SVvnodesize += TVvnodesize;
381 SVdiskused += TVdiskused;
385 printf("\nServer Totals%12d%9d%10d%10d%9d\n", SVdiskused, SVauxsize,
386 SVvnodesize, Stotvolsize, Stotvolsize - SVdiskused);
392 HandlePart(struct DiskPartition64 *partP)
400 (void)sprintf(pname, "%s\\", VPartitionPath(partP));
402 char *p = VPartitionPath(partP);
405 if ((dirp = opendir(p)) == NULL) {
406 fprintf(stderr, "%s: Can't read directory %s; giving up\n", progname,
410 if (dsizeOnly && !saveinodes)
412 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
413 while ((dp = readdir(dirp))) {
414 p = (char *)strrchr(dp->d_name, '.');
415 if (p != NULL && strcmp(p, VHDREXT) == 0) {
416 HandleVolume(partP, dp->d_name);
417 Totvolsize += totvolsize;
418 TVauxsize += Vauxsize;
419 TVvnodesize += Vvnodesize;
420 TVdiskused += Vdiskused;
426 printf("\nPart Totals %12d%9d%10d%10d%9d (%d volumes)\n\n",
427 TVdiskused, TVauxsize, TVvnodesize, Totvolsize,
428 Totvolsize - TVdiskused, nvols);
434 HandleVolume(struct DiskPartition64 *dp, char *name)
436 struct VolumeHeader header;
437 struct VolumeDiskHeader diskHeader;
438 FD_t fd = INVALID_FD;
441 char headerName[1024];
444 fprintf(stderr, "%s: -online not supported\n", progname);
449 snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
450 VPartitionPath(dp), name);
451 if ((fd = OS_OPEN(headerName, O_RDONLY, 0666)) == INVALID_FD
452 || OS_SIZE(fd) < 0) {
453 fprintf(stderr, "%s: Cannot read volume header %s\n", progname,
458 n = OS_READ(fd, &diskHeader, sizeof(diskHeader));
460 if (n != sizeof(diskHeader)
461 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
462 fprintf(stderr, "%s: Error reading volume header %s\n", progname,
466 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
468 "%s: Volume %s, version number is incorrect; volume needs salvage\n",
472 DiskToVolumeHeader(&header, &diskHeader);
476 afs_sfsize_t size = 0;
479 if (!dsizeOnly && !saveinodes) {
481 printf("Volume header (size = %d):\n", (int)size);
482 printf("\tstamp\t= 0x%x\n", header.stamp.version);
483 printf("\tVolId\t= %u\n", header.id);
486 IH_INIT(ih, dp->device, header.parent, header.volumeInfo);
489 fprintf(stderr, "%s: Error opening volume info: ", progname);
493 code = FDH_SIZE(fdP);
495 fprintf(stderr, "%s: Error getting size of volume info: ",
500 FDH_REALLYCLOSE(fdP);
503 if (!dsizeOnly && !saveinodes) {
504 printf("\tparent\t= %u\n", header.parent);
505 printf("\tInfo inode\t= %s (size = %d)\n",
506 PrintInode(NULL, header.volumeInfo), (int)code);
509 IH_INIT(ih, dp->device, header.parent, header.smallVnodeIndex);
512 fprintf(stderr, "%s: Error opening small vnode index: ",
517 code = FDH_SIZE(fdP);
520 "%s: Error getting size of small vnode index: ",
525 FDH_REALLYCLOSE(fdP);
528 if (!dsizeOnly && !saveinodes) {
529 printf("\tSmall inode\t= %s (size = %d)\n",
530 PrintInode(NULL, header.smallVnodeIndex), (int)code);
533 IH_INIT(ih, dp->device, header.parent, header.largeVnodeIndex);
536 fprintf(stderr, "%s: Error opening large vnode index: ",
541 code = FDH_SIZE(fdP);
544 "%s: Error getting size of large vnode index: ",
549 FDH_REALLYCLOSE(fdP);
552 if (!dsizeOnly && !saveinodes) {
553 printf("\tLarge inode\t= %s (size = %d)\n",
554 PrintInode(NULL, header.largeVnodeIndex), (int)code);
557 IH_INIT(ih, dp->device, header.parent, header.linkTable);
560 fprintf(stderr, "%s: Error opening link table index: ",
565 code = FDH_SIZE(fdP);
568 "%s: Error getting size of link table index: ",
573 FDH_REALLYCLOSE(fdP);
576 if (!dsizeOnly && !saveinodes) {
577 printf("\tLink inode\t= %s (size = %d)\n",
578 PrintInode(NULL, header.linkTable), (int)code);
581 printf("Total aux volume size = %d\n\n", (int)size);
583 Vauxsize_k = size / 1024;
586 vp = AttachVolume(dp, name, &header);
588 fprintf(stderr, "%s: Error attaching volume header %s\n",
595 if (!dsizeOnly && !saveinodes)
596 printf("\nLarge vnodes (directories)\n");
597 PrintVnodes(vp, vLarge);
598 if (!dsizeOnly && !saveinodes) {
599 printf("\nSmall vnodes(files, symbolic links)\n");
603 printf("Saving all volume files to current directory ...\n");
604 PrintVnodes(vp, vSmall);
607 totvolsize = Vauxsize_k + Vvnodesize_k;
610 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
611 printf("%u\t%9d%9d%10d%10d%9d\t%24s\n", V_id(vp), Vdiskused,
612 Vauxsize_k, Vvnodesize_k, totvolsize, totvolsize - Vdiskused,
620 main(int argc, char **argv)
622 struct cmd_syndesc *ts;
625 ts = cmd_CreateSyntax(NULL, handleit, NULL, "Dump volume's internal state");
626 cmd_AddParm(ts, "-online", CMD_FLAG, CMD_OPTIONAL,
627 "Get info from running fileserver");
628 cmd_AddParm(ts, "-vnode", CMD_FLAG, CMD_OPTIONAL, "Dump vnode info");
629 cmd_AddParm(ts, "-date", CMD_FLAG, CMD_OPTIONAL,
630 "Also dump vnode's mod date");
631 cmd_AddParm(ts, "-inode", CMD_FLAG, CMD_OPTIONAL,
632 "Also dump vnode's inode number");
633 cmd_AddParm(ts, "-itime", CMD_FLAG, CMD_OPTIONAL,
634 "Dump special inode's mod times");
635 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL,
636 "AFS partition name (default current partition)");
637 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
638 cmd_AddParm(ts, "-header", CMD_FLAG, CMD_OPTIONAL,
639 "Dump volume's header info");
640 cmd_AddParm(ts, "-sizeOnly", CMD_FLAG, CMD_OPTIONAL,
641 "Dump volume's size");
642 cmd_AddParm(ts, "-fixheader", CMD_FLAG, CMD_OPTIONAL,
643 "Try to fix header");
644 cmd_AddParm(ts, "-saveinodes", CMD_FLAG, CMD_OPTIONAL,
645 "Try to save all inodes");
646 cmd_AddParm(ts, "-orphaned", CMD_FLAG, CMD_OPTIONAL,
647 "List all dir/files without a parent");
648 #if defined(AFS_NAMEI_ENV)
649 cmd_AddParm(ts, "-filenames", CMD_FLAG, CMD_OPTIONAL, "Also dump vnode's namei filename");
651 code = cmd_Dispatch(argc, argv);
655 #define typestring(type) (type == RWVOL? "read/write": type == ROVOL? "readonly": type == BACKVOL? "backup": "unknown")
658 PrintHeader(Volume * vp)
660 Vdiskused = V_diskused(vp);
661 if (dsizeOnly || saveinodes)
663 printf("Volume header for volume %u (%s)\n", V_id(vp), V_name(vp));
664 printf("stamp.magic = %x, stamp.version = %u\n", V_stamp(vp).magic,
665 V_stamp(vp).version);
667 ("inUse = %d, inService = %d, blessed = %d, needsSalvaged = %d, dontSalvage = %d\n",
668 V_inUse(vp), V_inService(vp), V_blessed(vp), V_needsSalvaged(vp),
671 ("type = %d (%s), uniquifier = %u, needsCallback = %d, destroyMe = %x\n",
672 V_type(vp), typestring(V_type(vp)), V_uniquifier(vp),
673 V_needsCallback(vp), V_destroyMe(vp));
675 ("id = %u, parentId = %u, cloneId = %u, backupId = %u, restoredFromId = %u\n",
676 V_id(vp), V_parentId(vp), V_cloneId(vp), V_backupId(vp),
677 V_restoredFromId(vp));
679 ("maxquota = %d, minquota = %d, maxfiles = %d, filecount = %d, diskused = %d\n",
680 V_maxquota(vp), V_minquota(vp), V_maxfiles(vp), V_filecount(vp),
682 printf("creationDate = %s, copyDate = %s\n", date(V_creationDate(vp)),
683 date(V_copyDate(vp)));
684 printf("backupDate = %s, expirationDate = %s\n", date(V_backupDate(vp)),
685 date(V_expirationDate(vp)));
686 printf("accessDate = %s, updateDate = %s\n", date(V_accessDate(vp)),
687 date(V_updateDate(vp)));
688 printf("owner = %u, accountNumber = %u\n", V_owner(vp),
689 V_accountNumber(vp));
691 ("dayUse = %u; week = (%u, %u, %u, %u, %u, %u, %u), dayUseDate = %s\n",
692 V_dayUse(vp), V_weekUse(vp)[0], V_weekUse(vp)[1], V_weekUse(vp)[2],
693 V_weekUse(vp)[3], V_weekUse(vp)[4], V_weekUse(vp)[5],
694 V_weekUse(vp)[6], date(V_dayUseDate(vp)));
695 printf("volUpdateCounter = %u\n", V_volUpCounter(vp));
699 * OS independent file info. Die on failure.
703 NT_date(FILETIME * ft)
705 static char result[8][64];
710 if (!FileTimeToLocalFileTime(ft, &lft)
711 || !FileTimeToSystemTime(&lft, &st)) {
712 fprintf(stderr, "%s: Time conversion failed.\n", progname);
715 sprintf(result[next = ((next + 1) & 7)], "%4d/%02d/%02d.%2d:%2d:%2d",
716 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
722 GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime)
725 BY_HANDLE_FILE_INFORMATION fi;
726 if (!GetFileInformationByHandle(fd, &fi)) {
727 fprintf(stderr, "%s: GetFileInformationByHandle failed, exiting\n",
731 *size = (int)fi.nFileSizeLow;
733 *mtime = NT_date(&fi.ftLastWriteTime);
734 *atime = NT_date(&fi.ftLastAccessTime);
736 struct afs_stat_st status;
737 if (afs_fstat(fd, &status) == -1) {
738 fprintf(stderr, "%s: fstat failed %d\n", progname, errno);
741 *size = (int)status.st_size;
742 *ctime = date(status.st_ctime);
743 *mtime = date(status.st_mtime);
744 *atime = date(status.st_atime);
749 PrintVnodes(Volume * vp, VnodeClass class)
752 (class == vSmall ? SIZEOF_SMALLDISKVNODE : SIZEOF_LARGEDISKVNODE);
753 char buf[SIZEOF_LARGEDISKVNODE];
754 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
755 StreamHandle_t *file;
756 int vnodeIndex, nVnodes;
757 afs_foff_t offset = 0;
759 IHandle_t *ih = vp->vnodeIndex[class].handle;
762 char *ctime, *atime, *mtime;
763 char nfile[50], buffer[256];
770 fprintf(stderr, "%s: open failed: ", progname);
774 file = FDH_FDOPEN(fdP, "r");
776 fprintf(stderr, "%s: fdopen failed\n", progname);
780 GetFileInfo(fdP->fd_fd, &size, &ctime, &atime, &mtime);
781 if (InodeTimes && !dsizeOnly) {
782 printf("ichanged : %s\nimodified: %s\niaccessed: %s\n\n", ctime,
786 nVnodes = (size / diskSize) - 1;
788 STREAM_ASEEK(file, diskSize);
793 nVnodes && STREAM_READ(vnode, diskSize, 1, file) == 1;
794 nVnodes--, vnodeIndex++, offset += diskSize) {
796 ino = VNDISK_GET_INO(vnode);
798 if (!VALID_INO(ino)) {
801 if (dsizeOnly && (class == vLarge)) {
802 afs_fsize_t fileLength;
804 VNDISK_GET_LEN(fileLength, vnode);
805 Vvnodesize += fileLength;
806 Vvnodesize_k += fileLength / 1024;
807 } else if (class == vSmall) {
810 IH_INIT(ih1, V_device(vp), V_parentId(vp), ino);
814 "%s: Can't open inode %s error %d (ignored)\n",
815 progname, PrintInode(NULL, ino), errno);
818 snprintf(nfile, sizeof nfile, "TmpInode.%s",
819 PrintInode(NULL, ino));
820 ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600);
823 "%s: Can't create file %s; error %d (ignored)\n",
824 progname, nfile, errno);
830 len = FDH_PREAD(fdP1, buffer, sizeof(buffer), total);
832 FDH_REALLYCLOSE(fdP1);
837 "%s: Error while reading from inode %s (%d - ignored)\n",
838 progname, PrintInode(NULL, ino), errno);
843 break; /* No more input */
844 nBytes = write(ofd, buffer, len);
846 FDH_REALLYCLOSE(fdP1);
851 "%s: Error while writing to \"%s\" (%d - ignored)\n",
852 progname, nfile, errno);
860 FDH_REALLYCLOSE(fdP1);
863 printf("... Copied inode %s to file %s (%lu bytes)\n",
864 PrintInode(NULL, ino), nfile, (unsigned long)total);
867 #if defined(AFS_NAMEI_ENV)
868 PrintVnode(offset, vnode,
869 bitNumberToVnodeNumber(vnodeIndex, class), ino, vp);
871 PrintVnode(offset, vnode,
872 bitNumberToVnodeNumber(vnodeIndex, class), ino);
880 #if defined(AFS_NAMEI_ENV)
882 PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
883 Inode ino, Volume * vp)
886 PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
890 #if defined(AFS_NAMEI_ENV)
894 afs_fsize_t fileLength;
896 VNDISK_GET_LEN(fileLength, vnode);
897 Vvnodesize += fileLength;
898 Vvnodesize_k += fileLength / 1024;
901 if (orphaned && (fileLength == 0 || vnode->parent || !offset))
904 ("%10lld Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
905 (long long)offset, vnodeNumber, vnode->uniquifier, vnode->dataVersion,
906 vnode->cloned, (afs_uintmax_t) fileLength, vnode->linkCount,
909 printf(" inode: %s", PrintInode(NULL, ino));
911 printf(" ServerModTime: %s", date(vnode->serverModifyTime));
912 #if defined(AFS_NAMEI_ENV)
913 if (PrintFileNames) {
914 IH_INIT(ihtmpp, V_device(vp), V_parentId(vp), ino);
915 namei_HandleToName(&filename, ihtmpp);
916 #if !defined(AFS_NT40_ENV)
917 printf(" UFS-Filename: %s", filename.n_path);
919 printf(" NTFS-Filename: %s", filename.n_path);