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>
33 #include <sys/param.h>
40 #include <afs/afsint.h>
42 #include <afs/errors.h>
45 #include <afs/afssyscalls.h>
49 #include "partition.h"
50 #include "viceinode.h"
51 #include "volinodes.h"
52 #include <afs/afssyscalls.h>
61 #define afs_stat stat64
62 #define afs_fstat fstat64
63 #define afs_open open64
64 #else /* !O_LARGEFILE */
66 #define afs_fstat fstat
68 #endif /* !O_LARGEFILE */
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;
79 int dsizeOnly = 0, totvolsize = 0, Vauxsize = 0, Vdiskused = 0, Vvnodesize =
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;
87 /* Forward Declarations */
88 void PrintHeader(register Volume * vp);
89 void HandleAllPart(void);
90 void HandlePart(struct DiskPartition *partP);
91 void HandleVolume(struct DiskPartition *partP, char *name);
92 struct DiskPartition *FindCurrentPartition(void);
93 Volume *AttachVolume(struct DiskPartition *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);
99 void PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
102 void PrintVnodes(Volume * vp, VnodeClass class);
107 #define MAX_DATE_RESULT 100
108 static char results[8][MAX_DATE_RESULT];
110 struct tm *tm = localtime(&date);
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];
120 #include "AFS_component_version_number.c"
123 char name[VMAXPATHLEN];
128 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
130 struct versionStamp *vsn;
134 vsn = (struct versionStamp *)to;
136 code = IH_IREAD(ih, 0, to, size);
140 if (vsn->magic != magic) {
142 printf("Inode %s: Bad magic %x (%x): IGNORED\n",
143 PrintInode(NULL, ih->ih_ino), vsn->magic, magic);
146 /* Check is conditional, in case caller wants to inspect version himself */
147 if (version && vsn->version != version) {
149 printf("Inode %s: Bad version %x (%x): IGNORED\n",
150 PrintInode(NULL, ih->ih_ino), vsn->version, version);
152 if (bad && fixheader) {
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);
159 printf("Write failed; header left in damaged state\n");
162 if (!dsizeOnly && !saveinodes) {
163 printf("Inode %s: Good magic %x and version %x\n",
164 PrintInode(NULL, ih->ih_ino), magic, version);
172 AttachVolume(struct DiskPartition * dp, char *volname,
173 register struct VolumeHeader * header)
178 vp = (Volume *) calloc(1, sizeof(Volume));
179 vp->specialStatus = 0;
180 vp->device = dp->device;
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,
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;
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);
197 struct IndexFileHeader iHead;
198 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
199 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
202 struct IndexFileHeader iHead;
203 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
204 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
208 struct versionStamp stamp;
209 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
210 LINKTABLEMAGIC, LINKTABLEVERSION);
220 handleit(struct cmd_syndesc *as, void *arock)
222 register struct cmd_item *ti;
226 struct DiskPartition *partP = NULL;
230 if (geteuid() != 0) {
231 printf("vol-info must be run as root; sorry\n");
236 if (as->parms[0].items)
240 if (as->parms[1].items)
244 if (as->parms[2].items)
248 if (as->parms[3].items)
252 if (as->parms[4].items)
256 if ((ti = as->parms[5].items))
258 if ((ti = as->parms[6].items))
259 volumeId = strtoul(ti->data, NULL, 10);
260 if (as->parms[7].items)
264 if (as->parms[8].items) {
270 if (as->parms[9].items) {
274 if (as->parms[10].items) {
280 if (as->parms[11].items) {
284 #if defined(AFS_NAMEI_ENV)
285 if (as->parms[12].items) {
294 err = VAttachPartitions();
296 printf("%d partitions had errors during attach.\n", err);
300 partP = VGetPartition(partName, 0);
302 printf("%s is not an AFS partition name on this server.\n",
318 partP = FindCurrentPartition();
320 printf("Current partition is not a vice partition.\n");
324 (void)afs_snprintf(name1, sizeof name1, VFORMAT,
325 (unsigned long)volumeId);
326 if (dsizeOnly && !saveinodes)
328 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
329 HandleVolume(partP, name1);
336 struct DiskPartition *
337 FindCurrentPartition()
339 int dr = _getdrive();
340 struct DiskPartition *dp;
343 for (dp = DiskPartitionList; dp; dp = dp->next) {
344 if (*dp->devName - 'A' == dr)
348 printf("Current drive is not a valid vice partition.\n");
353 struct DiskPartition *
354 FindCurrentPartition()
359 struct DiskPartition *dp;
361 if (!getcwd(partName, 1023)) {
365 p = strchr(&partName[1], '/');
370 if (!(dp = VGetPartition(partName, 0))) {
373 printf("%s is not a valid vice partition.\n", partName);
383 struct DiskPartition *partP;
386 for (partP = DiskPartitionList; partP; partP = partP->next) {
387 printf("Processing Partition %s:\n", partP->name);
389 Stotvolsize += Totvolsize;
390 SVauxsize += TVauxsize;
391 SVvnodesize += TVvnodesize;
392 SVdiskused += TVdiskused;
396 printf("\nServer Totals%12d%9d%10d%10d%9d\n", SVdiskused, SVauxsize,
397 SVvnodesize, Stotvolsize, Stotvolsize - SVdiskused);
403 HandlePart(struct DiskPartition *partP)
411 (void)sprintf(pname, "%s\\", VPartitionPath(partP));
413 char *p = VPartitionPath(partP);
416 if (chdir(p) == -1) {
417 printf("Can't chdir to partition %s; giving up\n", p);
420 if ((dirp = opendir(".")) == NULL) {
421 printf("Can't read directory %s; giving up\n", p);
424 if (dsizeOnly && !saveinodes)
426 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
427 while ((dp = readdir(dirp))) {
428 p = (char *)strrchr(dp->d_name, '.');
429 if (p != NULL && strcmp(p, VHDREXT) == 0) {
430 HandleVolume(partP, dp->d_name);
431 Totvolsize += totvolsize;
432 TVauxsize += Vauxsize;
433 TVvnodesize += Vvnodesize;
434 TVdiskused += Vdiskused;
440 printf("\nPart Totals %12d%9d%10d%10d%9d (%d volumes)\n\n",
441 TVdiskused, TVauxsize, TVvnodesize, Totvolsize,
442 Totvolsize - TVdiskused, nvols);
448 HandleVolume(struct DiskPartition *dp, char *name)
450 struct VolumeHeader header;
451 struct VolumeDiskHeader diskHeader;
452 struct afs_stat status, stat;
456 char headerName[1024];
459 printf("volinfo: -online not supported\n");
464 (void)afs_snprintf(headerName, sizeof headerName, "%s/%s",
465 VPartitionPath(dp), name);
466 if ((fd = afs_open(headerName, O_RDONLY)) == -1
467 || afs_fstat(fd, &status) == -1) {
468 printf("Volinfo: Cannot read volume header %s\n", name);
472 n = read(fd, &diskHeader, sizeof(diskHeader));
474 if (n != sizeof(diskHeader)
475 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
476 printf("Volinfo: Error reading volume header %s\n", name);
479 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
481 ("Volinfo: Volume %s, version number is incorrect; volume needs salvage\n",
485 DiskToVolumeHeader(&header, &diskHeader);
492 if (afs_fstat(fd, &stat) == -1) {
496 if (!dsizeOnly && !saveinodes) {
497 printf("Volume header (size = %d):\n", size = stat.st_size);
498 printf("\tstamp\t= 0x%x\n", header.stamp.version);
499 printf("\tVolId\t= %u\n", header.id);
502 IH_INIT(ih, dp->device, header.parent, header.volumeInfo);
505 perror("opening volume info");
508 code = FDH_SIZE(fdP);
513 FDH_REALLYCLOSE(fdP);
516 if (!dsizeOnly && !saveinodes) {
517 printf("\tparent\t= %u\n", header.parent);
518 printf("\tInfo inode\t= %s (size = %d)\n",
519 PrintInode(NULL, header.volumeInfo), code);
522 IH_INIT(ih, dp->device, header.parent, header.smallVnodeIndex);
525 perror("opening small vnode index");
528 code = FDH_SIZE(fdP);
533 FDH_REALLYCLOSE(fdP);
536 if (!dsizeOnly && !saveinodes) {
537 printf("\tSmall inode\t= %s (size = %d)\n",
538 PrintInode(NULL, header.smallVnodeIndex), code);
541 IH_INIT(ih, dp->device, header.parent, header.largeVnodeIndex);
544 perror("opening large vnode index");
547 code = FDH_SIZE(fdP);
552 FDH_REALLYCLOSE(fdP);
555 if (!dsizeOnly && !saveinodes) {
556 printf("\tLarge inode\t= %s (size = %d)\n",
557 PrintInode(NULL, header.largeVnodeIndex), code);
559 printf("Total aux volume size = %d\n\n", size);
563 IH_INIT(ih, dp->device, header.parent, header.linkTable);
566 perror("opening link table index");
569 code = FDH_SIZE(fdP);
574 FDH_REALLYCLOSE(fdP);
577 if (!dsizeOnly && !saveinodes) {
578 printf("\tLink inode\t= %s (size = %d)\n",
579 PrintInode(NULL, header.linkTable), code);
580 printf("Total aux volume size = %d\n\n", size);
584 Vauxsize_k = size / 1024;
587 vp = AttachVolume(dp, name, &header);
589 printf("Volinfo: Error attaching volume header %s\n", name);
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 register 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 "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, "Print filenames");
651 code = cmd_Dispatch(argc, argv);
655 #define typestring(type) (type == RWVOL? "read/write": type == ROVOL? "readonly": type == BACKVOL? "backup": "unknown")
658 PrintHeader(register 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 printf("Time conversion failed.\n");
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 printf("GetFileInformationByHandle failed, exiting\n");
730 *size = (int)fi.nFileSizeLow;
732 *mtime = NT_date(&fi.ftLastWriteTime);
733 *atime = NT_date(&fi.ftLastAccessTime);
735 struct afs_stat status;
736 if (afs_fstat(fd, &status) == -1) {
737 printf("fstat failed %d\n", errno);
740 *size = (int)status.st_size;
741 *ctime = date(status.st_ctime);
742 *mtime = date(status.st_mtime);
743 *atime = date(status.st_atime);
748 PrintVnodes(Volume * vp, VnodeClass class)
751 (class == vSmall ? SIZEOF_SMALLDISKVNODE : SIZEOF_LARGEDISKVNODE);
752 char buf[SIZEOF_LARGEDISKVNODE];
753 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
754 StreamHandle_t *file;
755 register int vnodeIndex, nVnodes, offset = 0;
757 IHandle_t *ih = vp->vnodeIndex[class].handle;
760 char *ctime, *atime, *mtime;
761 char nfile[50], buffer[256];
762 int total, ofd, len, code, bad = 0;
766 printf("open failed\n");
770 file = FDH_FDOPEN(fdP, "r");
772 printf("fdopen failed\n");
776 GetFileInfo(fdP->fd_fd, &size, &ctime, &atime, &mtime);
777 if (InodeTimes && !dsizeOnly) {
778 printf("ichanged : %s\nimodified: %s\niaccessed: %s\n\n", ctime,
782 nVnodes = (size / diskSize) - 1;
784 STREAM_SEEK(file, diskSize, 0);
789 nVnodes && STREAM_READ(vnode, diskSize, 1, file) == 1;
790 nVnodes--, vnodeIndex++, offset += diskSize) {
792 ino = VNDISK_GET_INO(vnode);
794 if (VALID_INO(ino) && (class == vSmall)) {
797 IH_INIT(ih1, V_device(vp), V_parentId(vp), ino);
800 printf("Can't open inode %s error %d (ignored)\n",
801 PrintInode(NULL, ino), errno);
804 (void)afs_snprintf(nfile, sizeof nfile, "TmpInode.%s",
805 PrintInode(NULL, ino));
806 ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600);
808 printf("Can't create file %s; error %d (ignored)\n",
814 len = FDH_READ(fdP1, buffer, sizeof(buffer));
816 FDH_REALLYCLOSE(fdP1);
821 ("Error while reading from inode %s (%d - ignored)\n",
822 PrintInode(NULL, ino), errno);
827 break; /* No more input */
828 code = write(ofd, buffer, len);
830 FDH_REALLYCLOSE(fdP1);
835 ("Error while writing to \"%s\" (%d - ignored)\n",
844 FDH_REALLYCLOSE(fdP1);
847 printf("... Copied inode %s to file %s (%d bytes)\n",
848 PrintInode(NULL, ino), nfile, total);
851 #if defined(AFS_NAMEI_ENV)
852 PrintVnode(offset, vnode,
853 bitNumberToVnodeNumber(vnodeIndex, class), ino, vp);
855 PrintVnode(offset, vnode,
856 bitNumberToVnodeNumber(vnodeIndex, class), ino);
864 #if defined(AFS_NAMEI_ENV)
866 PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
867 Inode ino, Volume * vp)
870 PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
874 #if defined(AFS_NAMEI_ENV)
876 #if !defined(AFS_NT40_ENV)
879 char filename[MAX_PATH];
882 afs_fsize_t fileLength;
884 VNDISK_GET_LEN(fileLength, vnode);
885 Vvnodesize += fileLength;
886 Vvnodesize_k += fileLength / 1024;
889 if (orphaned && (fileLength == 0 || vnode->parent || !offset))
892 ("%10d Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
893 offset, vnodeNumber, vnode->uniquifier, vnode->dataVersion,
894 vnode->cloned, (afs_uintmax_t) fileLength, vnode->linkCount,
897 printf(" inode: %s", PrintInode(NULL, ino));
899 printf(" ServerModTime: %s", date(vnode->serverModifyTime));
900 #if defined(AFS_NAMEI_ENV)
901 if (PrintFileNames) {
902 IH_INIT(ihtmpp, V_device(vp), V_parentId(vp), ino);
903 #if !defined(AFS_NT40_ENV)
904 namei_HandleToName(&filename, ihtmpp);
905 printf(" UFS-Filename: %s", filename.n_path);
907 nt_HandleToName(filename, ihtmpp);
908 printf(" NTFS-Filename: %s", filename);