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>
31 #include <sys/param.h>
39 #include <afs/afsint.h>
41 #include <afs/errors.h>
44 #include <afs/afssyscalls.h>
48 #include "partition.h"
49 #include "viceinode.h"
50 #include <afs/afssyscalls.h>
51 #include <afs/afsutil.h>
60 #define afs_stat stat64
61 #define afs_fstat fstat64
62 #define afs_open open64
63 #else /* !O_LARGEFILE */
65 #define afs_fstat fstat
67 #endif /* !O_LARGEFILE */
69 int DumpVnodes = 0; /* Dump everything, i.e. summary of all vnodes */
70 int DumpInodeNumber = 0; /* Dump inode numbers with vnodes */
71 int DumpDate = 0; /* Dump vnode date (server modify date) with vnode */
72 int InodeTimes = 0; /* Dump some of the dates associated with inodes */
73 #if defined(AFS_NAMEI_ENV)
74 int PrintFileNames = 0;
78 int dsizeOnly = 0, totvolsize = 0, Vauxsize = 0, Vdiskused = 0, Vvnodesize =
80 int Vvnodesize_k = 0, Vauxsize_k = 0;
81 int Totvolsize = 0, TVauxsize = 0, TVdiskused = 0, TVvnodesize = 0;
82 int Stotvolsize = 0, SVauxsize = 0, SVdiskused = 0, SVvnodesize = 0;
83 int fixheader = 0, saveinodes = 0, orphaned = 0;
86 /* Forward Declarations */
87 void PrintHeader(register Volume * vp);
88 void HandleAllPart(void);
89 void HandlePart(struct DiskPartition64 *partP);
90 void HandleVolume(struct DiskPartition64 *partP, char *name);
91 struct DiskPartition64 *FindCurrentPartition(void);
92 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
93 register struct VolumeHeader *header);
94 #if defined(AFS_NAMEI_ENV)
95 void PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
96 Inode ino, Volume * vp);
98 void PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
101 void PrintVnodes(Volume * vp, VnodeClass class);
106 #define MAX_DATE_RESULT 100
107 static char results[8][MAX_DATE_RESULT];
109 struct tm *tm = localtime(&date);
112 (void)strftime(buf, 32, "%Y/%m/%d.%H:%M:%S", tm); /* NT does not have %T */
113 (void)afs_snprintf(results[next = (next + 1) & 7], MAX_DATE_RESULT,
114 "%lu (%s)", (unsigned long)date, buf);
115 return results[next];
119 #include "AFS_component_version_number.c"
122 char name[VMAXPATHLEN];
127 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
129 struct versionStamp *vsn;
133 vsn = (struct versionStamp *)to;
135 code = IH_IREAD(ih, 0, to, size);
139 if (vsn->magic != magic) {
141 printf("Inode %s: Bad magic %x (%x): IGNORED\n",
142 PrintInode(NULL, ih->ih_ino), vsn->magic, magic);
145 /* Check is conditional, in case caller wants to inspect version himself */
146 if (version && vsn->version != version) {
148 printf("Inode %s: Bad version %x (%x): IGNORED\n",
149 PrintInode(NULL, ih->ih_ino), vsn->version, version);
151 if (bad && fixheader) {
153 vsn->version = version;
154 printf("Special index inode %s has a bad header. Reconstructing...\n",
155 PrintInode(NULL, ih->ih_ino));
156 code = IH_IWRITE(ih, 0, to, size);
158 printf("Write failed; header left in damaged state\n");
161 if (!dsizeOnly && !saveinodes) {
162 printf("Inode %s: Good magic %x and version %x\n",
163 PrintInode(NULL, ih->ih_ino), magic, version);
171 AttachVolume(struct DiskPartition64 * dp, char *volname,
172 register struct VolumeHeader * header)
177 vp = (Volume *) calloc(1, sizeof(Volume));
178 vp->specialStatus = 0;
179 vp->device = dp->device;
181 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
182 header->largeVnodeIndex);
183 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
184 header->smallVnodeIndex);
185 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
187 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
188 vp->cacheCheck = 0; /* XXXX */
189 vp->shuttingDown = 0;
190 vp->goingOffline = 0;
192 vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
193 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
194 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
196 struct IndexFileHeader iHead;
197 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
198 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
201 struct IndexFileHeader iHead;
202 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
203 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
207 struct versionStamp stamp;
208 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
209 LINKTABLEMAGIC, LINKTABLEVERSION);
219 handleit(struct cmd_syndesc *as, void *arock)
221 register struct cmd_item *ti;
223 afs_uint32 volumeId = 0;
225 struct DiskPartition64 *partP = NULL;
229 if (geteuid() != 0) {
230 printf("vol-info must be run as root; sorry\n");
235 if (as->parms[0].items)
239 if (as->parms[1].items)
243 if (as->parms[2].items)
247 if (as->parms[3].items)
251 if (as->parms[4].items)
255 if ((ti = as->parms[5].items))
257 if ((ti = as->parms[6].items))
258 volumeId = strtoul(ti->data, NULL, 10);
259 if (as->parms[7].items)
263 if (as->parms[8].items) {
269 if (as->parms[9].items) {
273 if (as->parms[10].items) {
279 if (as->parms[11].items) {
283 #if defined(AFS_NAMEI_ENV)
284 if (as->parms[12].items) {
293 err = VAttachPartitions();
295 printf("%d partitions had errors during attach.\n", err);
299 partP = VGetPartition(partName, 0);
301 printf("%s is not an AFS partition name on this server.\n",
317 partP = FindCurrentPartition();
319 printf("Current partition is not a vice partition.\n");
323 (void)afs_snprintf(name1, sizeof name1, VFORMAT,
324 afs_printable_uint32_lu(volumeId));
325 if (dsizeOnly && !saveinodes)
327 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
328 HandleVolume(partP, name1);
335 struct DiskPartition64 *
336 FindCurrentPartition(void)
338 int dr = _getdrive();
339 struct DiskPartition64 *dp;
342 for (dp = DiskPartitionList; dp; dp = dp->next) {
343 if (*dp->devName - 'A' == dr)
347 printf("Current drive is not a valid vice partition.\n");
352 struct DiskPartition64 *
353 FindCurrentPartition(void)
358 struct DiskPartition64 *dp;
360 if (!getcwd(partName, 1023)) {
364 p = strchr(&partName[1], '/');
369 if (!(dp = VGetPartition(partName, 0))) {
372 printf("%s is not a valid vice partition.\n", partName);
382 struct DiskPartition64 *partP;
385 for (partP = DiskPartitionList; partP; partP = partP->next) {
386 printf("Processing Partition %s:\n", partP->name);
388 Stotvolsize += Totvolsize;
389 SVauxsize += TVauxsize;
390 SVvnodesize += TVvnodesize;
391 SVdiskused += TVdiskused;
395 printf("\nServer Totals%12d%9d%10d%10d%9d\n", SVdiskused, SVauxsize,
396 SVvnodesize, Stotvolsize, Stotvolsize - SVdiskused);
402 HandlePart(struct DiskPartition64 *partP)
410 (void)sprintf(pname, "%s\\", VPartitionPath(partP));
412 char *p = VPartitionPath(partP);
415 if ((dirp = opendir(p)) == NULL) {
416 printf("Can't read directory %s; giving up\n", p);
419 if (dsizeOnly && !saveinodes)
421 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
422 while ((dp = readdir(dirp))) {
423 p = (char *)strrchr(dp->d_name, '.');
424 if (p != NULL && strcmp(p, VHDREXT) == 0) {
425 HandleVolume(partP, dp->d_name);
426 Totvolsize += totvolsize;
427 TVauxsize += Vauxsize;
428 TVvnodesize += Vvnodesize;
429 TVdiskused += Vdiskused;
435 printf("\nPart Totals %12d%9d%10d%10d%9d (%d volumes)\n\n",
436 TVdiskused, TVauxsize, TVvnodesize, Totvolsize,
437 Totvolsize - TVdiskused, nvols);
443 HandleVolume(struct DiskPartition64 *dp, char *name)
445 struct VolumeHeader header;
446 struct VolumeDiskHeader diskHeader;
447 struct afs_stat status, stat;
451 char headerName[1024];
454 printf("volinfo: -online not supported\n");
459 (void)afs_snprintf(headerName, sizeof headerName, "%s/%s",
460 VPartitionPath(dp), name);
461 if ((fd = afs_open(headerName, O_RDONLY)) == -1
462 || afs_fstat(fd, &status) == -1) {
463 printf("Volinfo: Cannot read volume header %s\n", name);
467 n = read(fd, &diskHeader, sizeof(diskHeader));
469 if (n != sizeof(diskHeader)
470 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
471 printf("Volinfo: Error reading volume header %s\n", name);
474 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
476 ("Volinfo: Volume %s, version number is incorrect; volume needs salvage\n",
480 DiskToVolumeHeader(&header, &diskHeader);
487 if (afs_fstat(fd, &stat) == -1) {
491 if (!dsizeOnly && !saveinodes) {
492 printf("Volume header (size = %d):\n", size = stat.st_size);
493 printf("\tstamp\t= 0x%x\n", header.stamp.version);
494 printf("\tVolId\t= %u\n", header.id);
497 IH_INIT(ih, dp->device, header.parent, header.volumeInfo);
500 perror("opening volume info");
503 code = FDH_SIZE(fdP);
508 FDH_REALLYCLOSE(fdP);
511 if (!dsizeOnly && !saveinodes) {
512 printf("\tparent\t= %u\n", header.parent);
513 printf("\tInfo inode\t= %s (size = %d)\n",
514 PrintInode(NULL, header.volumeInfo), code);
517 IH_INIT(ih, dp->device, header.parent, header.smallVnodeIndex);
520 perror("opening small vnode index");
523 code = FDH_SIZE(fdP);
528 FDH_REALLYCLOSE(fdP);
531 if (!dsizeOnly && !saveinodes) {
532 printf("\tSmall inode\t= %s (size = %d)\n",
533 PrintInode(NULL, header.smallVnodeIndex), code);
536 IH_INIT(ih, dp->device, header.parent, header.largeVnodeIndex);
539 perror("opening large vnode index");
542 code = FDH_SIZE(fdP);
547 FDH_REALLYCLOSE(fdP);
550 if (!dsizeOnly && !saveinodes) {
551 printf("\tLarge inode\t= %s (size = %d)\n",
552 PrintInode(NULL, header.largeVnodeIndex), code);
554 printf("Total aux volume size = %d\n\n", size);
558 IH_INIT(ih, dp->device, header.parent, header.linkTable);
561 perror("opening link table index");
564 code = FDH_SIZE(fdP);
569 FDH_REALLYCLOSE(fdP);
572 if (!dsizeOnly && !saveinodes) {
573 printf("\tLink inode\t= %s (size = %d)\n",
574 PrintInode(NULL, header.linkTable), code);
575 printf("Total aux volume size = %d\n\n", size);
579 Vauxsize_k = size / 1024;
582 vp = AttachVolume(dp, name, &header);
584 printf("Volinfo: Error attaching volume header %s\n", name);
590 if (!dsizeOnly && !saveinodes)
591 printf("\nLarge vnodes (directories)\n");
592 PrintVnodes(vp, vLarge);
593 if (!dsizeOnly && !saveinodes) {
594 printf("\nSmall vnodes(files, symbolic links)\n");
598 printf("Saving all volume files to current directory ...\n");
599 PrintVnodes(vp, vSmall);
602 totvolsize = Vauxsize_k + Vvnodesize_k;
605 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
606 printf("%u\t%9d%9d%10d%10d%9d\t%24s\n", V_id(vp), Vdiskused,
607 Vauxsize_k, Vvnodesize_k, totvolsize, totvolsize - Vdiskused,
615 main(int argc, char **argv)
617 register struct cmd_syndesc *ts;
620 ts = cmd_CreateSyntax(NULL, handleit, NULL, "Dump volume's internal state");
621 cmd_AddParm(ts, "-online", CMD_FLAG, CMD_OPTIONAL,
622 "Get info from running fileserver");
623 cmd_AddParm(ts, "-vnode", CMD_FLAG, CMD_OPTIONAL, "Dump vnode info");
624 cmd_AddParm(ts, "-date", CMD_FLAG, CMD_OPTIONAL,
625 "Also dump vnode's mod date");
626 cmd_AddParm(ts, "-inode", CMD_FLAG, CMD_OPTIONAL,
627 "Dump vnode's inode number");
628 cmd_AddParm(ts, "-itime", CMD_FLAG, CMD_OPTIONAL,
629 "Dump special inode's mod times");
630 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL,
631 "AFS partition name (default current partition)");
632 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
633 cmd_AddParm(ts, "-header", CMD_FLAG, CMD_OPTIONAL,
634 "Dump volume's header info");
635 cmd_AddParm(ts, "-sizeOnly", CMD_FLAG, CMD_OPTIONAL,
636 "Dump volume's size");
637 cmd_AddParm(ts, "-fixheader", CMD_FLAG, CMD_OPTIONAL,
638 "Try to fix header");
639 cmd_AddParm(ts, "-saveinodes", CMD_FLAG, CMD_OPTIONAL,
640 "Try to save all inodes");
641 cmd_AddParm(ts, "-orphaned", CMD_FLAG, CMD_OPTIONAL,
642 "List all dir/files without a parent");
643 #if defined(AFS_NAMEI_ENV)
644 cmd_AddParm(ts, "-filenames", CMD_FLAG, CMD_OPTIONAL, "Print filenames");
646 code = cmd_Dispatch(argc, argv);
650 #define typestring(type) (type == RWVOL? "read/write": type == ROVOL? "readonly": type == BACKVOL? "backup": "unknown")
653 PrintHeader(register Volume * vp)
655 Vdiskused = V_diskused(vp);
656 if (dsizeOnly || saveinodes)
658 printf("Volume header for volume %u (%s)\n", V_id(vp), V_name(vp));
659 printf("stamp.magic = %x, stamp.version = %u\n", V_stamp(vp).magic,
660 V_stamp(vp).version);
662 ("inUse = %d, inService = %d, blessed = %d, needsSalvaged = %d, dontSalvage = %d\n",
663 V_inUse(vp), V_inService(vp), V_blessed(vp), V_needsSalvaged(vp),
666 ("type = %d (%s), uniquifier = %u, needsCallback = %d, destroyMe = %x\n",
667 V_type(vp), typestring(V_type(vp)), V_uniquifier(vp),
668 V_needsCallback(vp), V_destroyMe(vp));
670 ("id = %u, parentId = %u, cloneId = %u, backupId = %u, restoredFromId = %u\n",
671 V_id(vp), V_parentId(vp), V_cloneId(vp), V_backupId(vp),
672 V_restoredFromId(vp));
674 ("maxquota = %d, minquota = %d, maxfiles = %d, filecount = %d, diskused = %d\n",
675 V_maxquota(vp), V_minquota(vp), V_maxfiles(vp), V_filecount(vp),
677 printf("creationDate = %s, copyDate = %s\n", date(V_creationDate(vp)),
678 date(V_copyDate(vp)));
679 printf("backupDate = %s, expirationDate = %s\n", date(V_backupDate(vp)),
680 date(V_expirationDate(vp)));
681 printf("accessDate = %s, updateDate = %s\n", date(V_accessDate(vp)),
682 date(V_updateDate(vp)));
683 printf("owner = %u, accountNumber = %u\n", V_owner(vp),
684 V_accountNumber(vp));
686 ("dayUse = %u; week = (%u, %u, %u, %u, %u, %u, %u), dayUseDate = %s\n",
687 V_dayUse(vp), V_weekUse(vp)[0], V_weekUse(vp)[1], V_weekUse(vp)[2],
688 V_weekUse(vp)[3], V_weekUse(vp)[4], V_weekUse(vp)[5],
689 V_weekUse(vp)[6], date(V_dayUseDate(vp)));
690 printf("volUpdateCounter = %u\n", V_volUpCounter(vp));
694 * OS independent file info. Die on failure.
698 NT_date(FILETIME * ft)
700 static char result[8][64];
705 if (!FileTimeToLocalFileTime(ft, &lft)
706 || !FileTimeToSystemTime(&lft, &st)) {
707 printf("Time conversion failed.\n");
710 sprintf(result[next = ((next + 1) & 7)], "%4d/%02d/%02d.%2d:%2d:%2d",
711 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
717 GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime)
720 BY_HANDLE_FILE_INFORMATION fi;
721 if (!GetFileInformationByHandle(fd, &fi)) {
722 printf("GetFileInformationByHandle failed, exiting\n");
725 *size = (int)fi.nFileSizeLow;
727 *mtime = NT_date(&fi.ftLastWriteTime);
728 *atime = NT_date(&fi.ftLastAccessTime);
730 struct afs_stat status;
731 if (afs_fstat(fd, &status) == -1) {
732 printf("fstat failed %d\n", errno);
735 *size = (int)status.st_size;
736 *ctime = date(status.st_ctime);
737 *mtime = date(status.st_mtime);
738 *atime = date(status.st_atime);
743 PrintVnodes(Volume * vp, VnodeClass class)
746 (class == vSmall ? SIZEOF_SMALLDISKVNODE : SIZEOF_LARGEDISKVNODE);
747 char buf[SIZEOF_LARGEDISKVNODE];
748 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
749 StreamHandle_t *file;
750 register int vnodeIndex, nVnodes, offset = 0;
752 IHandle_t *ih = vp->vnodeIndex[class].handle;
755 char *ctime, *atime, *mtime;
756 char nfile[50], buffer[256];
763 printf("open failed\n");
767 file = FDH_FDOPEN(fdP, "r");
769 printf("fdopen failed\n");
773 GetFileInfo(fdP->fd_fd, &size, &ctime, &atime, &mtime);
774 if (InodeTimes && !dsizeOnly) {
775 printf("ichanged : %s\nimodified: %s\niaccessed: %s\n\n", ctime,
779 nVnodes = (size / diskSize) - 1;
781 STREAM_SEEK(file, diskSize, 0);
786 nVnodes && STREAM_READ(vnode, diskSize, 1, file) == 1;
787 nVnodes--, vnodeIndex++, offset += diskSize) {
789 ino = VNDISK_GET_INO(vnode);
791 if (VALID_INO(ino) && (class == vSmall)) {
794 IH_INIT(ih1, V_device(vp), V_parentId(vp), ino);
797 printf("Can't open inode %s error %d (ignored)\n",
798 PrintInode(NULL, ino), errno);
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);
805 printf("Can't create file %s; error %d (ignored)\n",
812 len = FDH_READ(fdP1, buffer, sizeof(buffer));
814 FDH_REALLYCLOSE(fdP1);
819 ("Error while reading from inode %s (%d - ignored)\n",
820 PrintInode(NULL, ino), errno);
825 break; /* No more input */
826 nBytes = write(ofd, buffer, len);
828 FDH_REALLYCLOSE(fdP1);
833 ("Error while writing to \"%s\" (%d - ignored)\n",
842 FDH_REALLYCLOSE(fdP1);
845 printf("... Copied inode %s to file %s (%lu bytes)\n",
846 PrintInode(NULL, ino), nfile, (unsigned long)total);
849 #if defined(AFS_NAMEI_ENV)
850 PrintVnode(offset, vnode,
851 bitNumberToVnodeNumber(vnodeIndex, class), ino, vp);
853 PrintVnode(offset, vnode,
854 bitNumberToVnodeNumber(vnodeIndex, class), ino);
862 #if defined(AFS_NAMEI_ENV)
864 PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
865 Inode ino, Volume * vp)
868 PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
872 #if defined(AFS_NAMEI_ENV)
874 #if !defined(AFS_NT40_ENV)
877 char filename[MAX_PATH];
880 afs_fsize_t fileLength;
882 VNDISK_GET_LEN(fileLength, vnode);
883 Vvnodesize += fileLength;
884 Vvnodesize_k += fileLength / 1024;
887 if (orphaned && (fileLength == 0 || vnode->parent || !offset))
890 ("%10d Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
891 offset, vnodeNumber, vnode->uniquifier, vnode->dataVersion,
892 vnode->cloned, (afs_uintmax_t) fileLength, vnode->linkCount,
895 printf(" inode: %s", PrintInode(NULL, ino));
897 printf(" ServerModTime: %s", date(vnode->serverModifyTime));
898 #if defined(AFS_NAMEI_ENV)
899 if (PrintFileNames) {
900 IH_INIT(ihtmpp, V_device(vp), V_parentId(vp), ino);
901 #if !defined(AFS_NT40_ENV)
902 namei_HandleToName(&filename, ihtmpp);
903 printf(" UFS-Filename: %s", filename.n_path);
905 nt_HandleToName(filename, ihtmpp);
906 printf(" NTFS-Filename: %s", filename);