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 int DumpVnodes = 0; /* Dump everything, i.e. summary of all vnodes */
47 int DumpInodeNumber = 0; /* Dump inode numbers with vnodes */
48 int DumpDate = 0; /* Dump vnode date (server modify date) with vnode */
49 int InodeTimes = 0; /* Dump some of the dates associated with inodes */
50 #if defined(AFS_NAMEI_ENV)
51 int PrintFileNames = 0;
55 int dsizeOnly = 0, totvolsize = 0, Vauxsize = 0, Vdiskused = 0, Vvnodesize =
57 int Vvnodesize_k = 0, Vauxsize_k = 0;
58 int Totvolsize = 0, TVauxsize = 0, TVdiskused = 0, TVvnodesize = 0;
59 int Stotvolsize = 0, SVauxsize = 0, SVdiskused = 0, SVvnodesize = 0;
60 int fixheader = 0, saveinodes = 0, orphaned = 0;
63 /* Forward Declarations */
64 void PrintHeader(Volume * vp);
65 void HandleAllPart(void);
66 void HandlePart(struct DiskPartition64 *partP);
67 void HandleVolume(struct DiskPartition64 *partP, char *name);
68 struct DiskPartition64 *FindCurrentPartition(void);
69 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
70 struct VolumeHeader *header);
71 #if defined(AFS_NAMEI_ENV)
72 void PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
73 Inode ino, Volume * vp);
75 void PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
78 void PrintVnodes(Volume * vp, VnodeClass class);
83 #define MAX_DATE_RESULT 100
84 static char results[8][MAX_DATE_RESULT];
86 struct tm *tm = localtime(&date);
89 (void)strftime(buf, 32, "%Y/%m/%d.%H:%M:%S", tm); /* NT does not have %T */
90 snprintf(results[next = (next + 1) & 7], MAX_DATE_RESULT,
91 "%lu (%s)", (unsigned long)date, buf);
96 #include "AFS_component_version_number.c"
99 char name[VMAXPATHLEN];
104 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
106 struct versionStamp *vsn;
110 vsn = (struct versionStamp *)to;
112 code = IH_IREAD(ih, 0, to, size);
116 if (vsn->magic != magic) {
118 printf("Inode %s: Bad magic %x (%x): IGNORED\n",
119 PrintInode(NULL, ih->ih_ino), vsn->magic, magic);
122 /* Check is conditional, in case caller wants to inspect version himself */
123 if (version && vsn->version != version) {
125 printf("Inode %s: Bad version %x (%x): IGNORED\n",
126 PrintInode(NULL, ih->ih_ino), vsn->version, version);
128 if (bad && fixheader) {
130 vsn->version = version;
131 printf("Special index inode %s has a bad header. Reconstructing...\n",
132 PrintInode(NULL, ih->ih_ino));
133 code = IH_IWRITE(ih, 0, to, size);
135 printf("Write failed; header left in damaged state\n");
138 if (!dsizeOnly && !saveinodes) {
139 printf("Inode %s: Good magic %x and version %x\n",
140 PrintInode(NULL, ih->ih_ino), magic, version);
148 AttachVolume(struct DiskPartition64 * dp, char *volname,
149 struct VolumeHeader * header)
154 vp = (Volume *) calloc(1, sizeof(Volume));
155 vp->specialStatus = 0;
156 vp->device = dp->device;
158 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
159 header->largeVnodeIndex);
160 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
161 header->smallVnodeIndex);
162 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
164 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
165 vp->cacheCheck = 0; /* XXXX */
166 vp->shuttingDown = 0;
167 vp->goingOffline = 0;
169 vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
170 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
171 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
173 struct IndexFileHeader iHead;
174 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
175 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
178 struct IndexFileHeader iHead;
179 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
180 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
184 struct versionStamp stamp;
185 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
186 LINKTABLEMAGIC, LINKTABLEVERSION);
196 handleit(struct cmd_syndesc *as, void *arock)
200 afs_uint32 volumeId = 0;
202 struct DiskPartition64 *partP = NULL;
206 if (geteuid() != 0) {
207 printf("vol-info must be run as root; sorry\n");
212 if (as->parms[0].items)
216 if (as->parms[1].items)
220 if (as->parms[2].items)
224 if (as->parms[3].items)
228 if (as->parms[4].items)
232 if ((ti = as->parms[5].items))
234 if ((ti = as->parms[6].items))
235 volumeId = strtoul(ti->data, NULL, 10);
236 if (as->parms[7].items)
240 if (as->parms[8].items) {
246 if (as->parms[9].items) {
250 if (as->parms[10].items) {
256 if (as->parms[11].items) {
260 #if defined(AFS_NAMEI_ENV)
261 if (as->parms[12].items) {
270 err = VAttachPartitions();
272 printf("%d partitions had errors during attach.\n", err);
276 partP = VGetPartition(partName, 0);
278 printf("%s is not an AFS partition name on this server.\n",
294 partP = FindCurrentPartition();
296 printf("Current partition is not a vice partition.\n");
300 snprintf(name1, sizeof name1, VFORMAT,
301 afs_printable_uint32_lu(volumeId));
302 if (dsizeOnly && !saveinodes)
304 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
305 HandleVolume(partP, name1);
312 struct DiskPartition64 *
313 FindCurrentPartition(void)
315 int dr = _getdrive();
316 struct DiskPartition64 *dp;
319 for (dp = DiskPartitionList; dp; dp = dp->next) {
320 if (*dp->devName - 'A' == dr)
324 printf("Current drive is not a valid vice partition.\n");
329 struct DiskPartition64 *
330 FindCurrentPartition(void)
335 struct DiskPartition64 *dp;
337 if (!getcwd(partName, 1023)) {
341 p = strchr(&partName[1], OS_DIRSEPC);
346 if (!(dp = VGetPartition(partName, 0))) {
349 printf("%s is not a valid vice partition.\n", partName);
359 struct DiskPartition64 *partP;
362 for (partP = DiskPartitionList; partP; partP = partP->next) {
363 printf("Processing Partition %s:\n", partP->name);
365 Stotvolsize += Totvolsize;
366 SVauxsize += TVauxsize;
367 SVvnodesize += TVvnodesize;
368 SVdiskused += TVdiskused;
372 printf("\nServer Totals%12d%9d%10d%10d%9d\n", SVdiskused, SVauxsize,
373 SVvnodesize, Stotvolsize, Stotvolsize - SVdiskused);
379 HandlePart(struct DiskPartition64 *partP)
387 (void)sprintf(pname, "%s\\", VPartitionPath(partP));
389 char *p = VPartitionPath(partP);
392 if ((dirp = opendir(p)) == NULL) {
393 printf("Can't read directory %s; giving up\n", p);
396 if (dsizeOnly && !saveinodes)
398 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
399 while ((dp = readdir(dirp))) {
400 p = (char *)strrchr(dp->d_name, '.');
401 if (p != NULL && strcmp(p, VHDREXT) == 0) {
402 HandleVolume(partP, dp->d_name);
403 Totvolsize += totvolsize;
404 TVauxsize += Vauxsize;
405 TVvnodesize += Vvnodesize;
406 TVdiskused += Vdiskused;
412 printf("\nPart Totals %12d%9d%10d%10d%9d (%d volumes)\n\n",
413 TVdiskused, TVauxsize, TVvnodesize, Totvolsize,
414 Totvolsize - TVdiskused, nvols);
420 HandleVolume(struct DiskPartition64 *dp, char *name)
422 struct VolumeHeader header;
423 struct VolumeDiskHeader diskHeader;
424 FD_t fd = INVALID_FD;
427 char headerName[1024];
430 printf("volinfo: -online not supported\n");
435 snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
436 VPartitionPath(dp), name);
437 if ((fd = OS_OPEN(headerName, O_RDONLY, 0666)) == INVALID_FD
438 || OS_SIZE(fd) < 0) {
439 printf("Volinfo: Cannot read volume header %s\n", name);
443 n = OS_READ(fd, &diskHeader, sizeof(diskHeader));
445 if (n != sizeof(diskHeader)
446 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
447 printf("Volinfo: Error reading volume header %s\n", name);
450 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
452 ("Volinfo: Volume %s, version number is incorrect; volume needs salvage\n",
456 DiskToVolumeHeader(&header, &diskHeader);
460 afs_sfsize_t size = 0;
463 if (!dsizeOnly && !saveinodes) {
465 printf("Volume header (size = %d):\n", (int)size);
466 printf("\tstamp\t= 0x%x\n", header.stamp.version);
467 printf("\tVolId\t= %u\n", header.id);
470 IH_INIT(ih, dp->device, header.parent, header.volumeInfo);
473 perror("opening volume info");
476 code = FDH_SIZE(fdP);
481 FDH_REALLYCLOSE(fdP);
484 if (!dsizeOnly && !saveinodes) {
485 printf("\tparent\t= %u\n", header.parent);
486 printf("\tInfo inode\t= %s (size = %d)\n",
487 PrintInode(NULL, header.volumeInfo), (int)code);
490 IH_INIT(ih, dp->device, header.parent, header.smallVnodeIndex);
493 perror("opening small vnode index");
496 code = FDH_SIZE(fdP);
501 FDH_REALLYCLOSE(fdP);
504 if (!dsizeOnly && !saveinodes) {
505 printf("\tSmall inode\t= %s (size = %d)\n",
506 PrintInode(NULL, header.smallVnodeIndex), (int)code);
509 IH_INIT(ih, dp->device, header.parent, header.largeVnodeIndex);
512 perror("opening large vnode index");
515 code = FDH_SIZE(fdP);
520 FDH_REALLYCLOSE(fdP);
523 if (!dsizeOnly && !saveinodes) {
524 printf("\tLarge inode\t= %s (size = %d)\n",
525 PrintInode(NULL, header.largeVnodeIndex), (int)code);
527 printf("Total aux volume size = %d\n\n", (int)size);
531 IH_INIT(ih, dp->device, header.parent, header.linkTable);
534 perror("opening link table index");
537 code = FDH_SIZE(fdP);
542 FDH_REALLYCLOSE(fdP);
545 if (!dsizeOnly && !saveinodes) {
546 printf("\tLink inode\t= %s (size = %d)\n",
547 PrintInode(NULL, header.linkTable), (int)code);
548 printf("Total aux volume size = %d\n\n", (int)size);
552 Vauxsize_k = size / 1024;
555 vp = AttachVolume(dp, name, &header);
557 printf("Volinfo: Error attaching volume header %s\n", name);
563 if (!dsizeOnly && !saveinodes)
564 printf("\nLarge vnodes (directories)\n");
565 PrintVnodes(vp, vLarge);
566 if (!dsizeOnly && !saveinodes) {
567 printf("\nSmall vnodes(files, symbolic links)\n");
571 printf("Saving all volume files to current directory ...\n");
572 PrintVnodes(vp, vSmall);
575 totvolsize = Vauxsize_k + Vvnodesize_k;
578 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
579 printf("%u\t%9d%9d%10d%10d%9d\t%24s\n", V_id(vp), Vdiskused,
580 Vauxsize_k, Vvnodesize_k, totvolsize, totvolsize - Vdiskused,
588 main(int argc, char **argv)
590 struct cmd_syndesc *ts;
593 ts = cmd_CreateSyntax(NULL, handleit, NULL, "Dump volume's internal state");
594 cmd_AddParm(ts, "-online", CMD_FLAG, CMD_OPTIONAL,
595 "Get info from running fileserver");
596 cmd_AddParm(ts, "-vnode", CMD_FLAG, CMD_OPTIONAL, "Dump vnode info");
597 cmd_AddParm(ts, "-date", CMD_FLAG, CMD_OPTIONAL,
598 "Also dump vnode's mod date");
599 cmd_AddParm(ts, "-inode", CMD_FLAG, CMD_OPTIONAL,
600 "Dump vnode's inode number");
601 cmd_AddParm(ts, "-itime", CMD_FLAG, CMD_OPTIONAL,
602 "Dump special inode's mod times");
603 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL,
604 "AFS partition name (default current partition)");
605 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
606 cmd_AddParm(ts, "-header", CMD_FLAG, CMD_OPTIONAL,
607 "Dump volume's header info");
608 cmd_AddParm(ts, "-sizeOnly", CMD_FLAG, CMD_OPTIONAL,
609 "Dump volume's size");
610 cmd_AddParm(ts, "-fixheader", CMD_FLAG, CMD_OPTIONAL,
611 "Try to fix header");
612 cmd_AddParm(ts, "-saveinodes", CMD_FLAG, CMD_OPTIONAL,
613 "Try to save all inodes");
614 cmd_AddParm(ts, "-orphaned", CMD_FLAG, CMD_OPTIONAL,
615 "List all dir/files without a parent");
616 #if defined(AFS_NAMEI_ENV)
617 cmd_AddParm(ts, "-filenames", CMD_FLAG, CMD_OPTIONAL, "Print filenames");
619 code = cmd_Dispatch(argc, argv);
623 #define typestring(type) (type == RWVOL? "read/write": type == ROVOL? "readonly": type == BACKVOL? "backup": "unknown")
626 PrintHeader(Volume * vp)
628 Vdiskused = V_diskused(vp);
629 if (dsizeOnly || saveinodes)
631 printf("Volume header for volume %u (%s)\n", V_id(vp), V_name(vp));
632 printf("stamp.magic = %x, stamp.version = %u\n", V_stamp(vp).magic,
633 V_stamp(vp).version);
635 ("inUse = %d, inService = %d, blessed = %d, needsSalvaged = %d, dontSalvage = %d\n",
636 V_inUse(vp), V_inService(vp), V_blessed(vp), V_needsSalvaged(vp),
639 ("type = %d (%s), uniquifier = %u, needsCallback = %d, destroyMe = %x\n",
640 V_type(vp), typestring(V_type(vp)), V_uniquifier(vp),
641 V_needsCallback(vp), V_destroyMe(vp));
643 ("id = %u, parentId = %u, cloneId = %u, backupId = %u, restoredFromId = %u\n",
644 V_id(vp), V_parentId(vp), V_cloneId(vp), V_backupId(vp),
645 V_restoredFromId(vp));
647 ("maxquota = %d, minquota = %d, maxfiles = %d, filecount = %d, diskused = %d\n",
648 V_maxquota(vp), V_minquota(vp), V_maxfiles(vp), V_filecount(vp),
650 printf("creationDate = %s, copyDate = %s\n", date(V_creationDate(vp)),
651 date(V_copyDate(vp)));
652 printf("backupDate = %s, expirationDate = %s\n", date(V_backupDate(vp)),
653 date(V_expirationDate(vp)));
654 printf("accessDate = %s, updateDate = %s\n", date(V_accessDate(vp)),
655 date(V_updateDate(vp)));
656 printf("owner = %u, accountNumber = %u\n", V_owner(vp),
657 V_accountNumber(vp));
659 ("dayUse = %u; week = (%u, %u, %u, %u, %u, %u, %u), dayUseDate = %s\n",
660 V_dayUse(vp), V_weekUse(vp)[0], V_weekUse(vp)[1], V_weekUse(vp)[2],
661 V_weekUse(vp)[3], V_weekUse(vp)[4], V_weekUse(vp)[5],
662 V_weekUse(vp)[6], date(V_dayUseDate(vp)));
663 printf("volUpdateCounter = %u\n", V_volUpCounter(vp));
667 * OS independent file info. Die on failure.
671 NT_date(FILETIME * ft)
673 static char result[8][64];
678 if (!FileTimeToLocalFileTime(ft, &lft)
679 || !FileTimeToSystemTime(&lft, &st)) {
680 printf("Time conversion failed.\n");
683 sprintf(result[next = ((next + 1) & 7)], "%4d/%02d/%02d.%2d:%2d:%2d",
684 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
690 GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime)
693 BY_HANDLE_FILE_INFORMATION fi;
694 if (!GetFileInformationByHandle(fd, &fi)) {
695 printf("GetFileInformationByHandle failed, exiting\n");
698 *size = (int)fi.nFileSizeLow;
700 *mtime = NT_date(&fi.ftLastWriteTime);
701 *atime = NT_date(&fi.ftLastAccessTime);
703 struct afs_stat_st status;
704 if (afs_fstat(fd, &status) == -1) {
705 printf("fstat failed %d\n", errno);
708 *size = (int)status.st_size;
709 *ctime = date(status.st_ctime);
710 *mtime = date(status.st_mtime);
711 *atime = date(status.st_atime);
716 PrintVnodes(Volume * vp, VnodeClass class)
719 (class == vSmall ? SIZEOF_SMALLDISKVNODE : SIZEOF_LARGEDISKVNODE);
720 char buf[SIZEOF_LARGEDISKVNODE];
721 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
722 StreamHandle_t *file;
723 int vnodeIndex, nVnodes;
724 afs_foff_t offset = 0;
726 IHandle_t *ih = vp->vnodeIndex[class].handle;
729 char *ctime, *atime, *mtime;
730 char nfile[50], buffer[256];
737 printf("open failed\n");
741 file = FDH_FDOPEN(fdP, "r");
743 printf("fdopen failed\n");
747 GetFileInfo(fdP->fd_fd, &size, &ctime, &atime, &mtime);
748 if (InodeTimes && !dsizeOnly) {
749 printf("ichanged : %s\nimodified: %s\niaccessed: %s\n\n", ctime,
753 nVnodes = (size / diskSize) - 1;
755 STREAM_ASEEK(file, diskSize);
760 nVnodes && STREAM_READ(vnode, diskSize, 1, file) == 1;
761 nVnodes--, vnodeIndex++, offset += diskSize) {
763 ino = VNDISK_GET_INO(vnode);
765 if (VALID_INO(ino) && (class == vSmall)) {
768 IH_INIT(ih1, V_device(vp), V_parentId(vp), ino);
771 printf("Can't open inode %s error %d (ignored)\n",
772 PrintInode(NULL, ino), errno);
775 snprintf(nfile, sizeof nfile, "TmpInode.%s",
776 PrintInode(NULL, ino));
777 ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600);
779 printf("Can't create file %s; error %d (ignored)\n",
786 len = FDH_PREAD(fdP1, buffer, sizeof(buffer), total);
788 FDH_REALLYCLOSE(fdP1);
793 ("Error while reading from inode %s (%d - ignored)\n",
794 PrintInode(NULL, ino), errno);
799 break; /* No more input */
800 nBytes = write(ofd, buffer, len);
802 FDH_REALLYCLOSE(fdP1);
807 ("Error while writing to \"%s\" (%d - ignored)\n",
816 FDH_REALLYCLOSE(fdP1);
819 printf("... Copied inode %s to file %s (%lu bytes)\n",
820 PrintInode(NULL, ino), nfile, (unsigned long)total);
823 #if defined(AFS_NAMEI_ENV)
824 PrintVnode(offset, vnode,
825 bitNumberToVnodeNumber(vnodeIndex, class), ino, vp);
827 PrintVnode(offset, vnode,
828 bitNumberToVnodeNumber(vnodeIndex, class), ino);
836 #if defined(AFS_NAMEI_ENV)
838 PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
839 Inode ino, Volume * vp)
842 PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
846 #if defined(AFS_NAMEI_ENV)
850 afs_fsize_t fileLength;
852 VNDISK_GET_LEN(fileLength, vnode);
853 Vvnodesize += fileLength;
854 Vvnodesize_k += fileLength / 1024;
857 if (orphaned && (fileLength == 0 || vnode->parent || !offset))
860 ("%10lld Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
861 (long long)offset, vnodeNumber, vnode->uniquifier, vnode->dataVersion,
862 vnode->cloned, (afs_uintmax_t) fileLength, vnode->linkCount,
865 printf(" inode: %s", PrintInode(NULL, ino));
867 printf(" ServerModTime: %s", date(vnode->serverModifyTime));
868 #if defined(AFS_NAMEI_ENV)
869 if (PrintFileNames) {
870 IH_INIT(ihtmpp, V_device(vp), V_parentId(vp), ino);
871 namei_HandleToName(&filename, ihtmpp);
872 #if !defined(AFS_NT40_ENV)
873 printf(" UFS-Filename: %s", filename.n_path);
875 printf(" NTFS-Filename: %s", filename.n_path);