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) {
261 #if defined(AFS_NAMEI_ENV)
262 if (as->parms[12].items) {
271 err = VAttachPartitions();
273 printf("%d partitions had errors during attach.\n", err);
277 partP = VGetPartition(partName, 0);
279 printf("%s is not an AFS partition name on this server.\n",
295 partP = FindCurrentPartition();
297 printf("Current partition is not a vice partition.\n");
301 snprintf(name1, sizeof name1, VFORMAT,
302 afs_printable_uint32_lu(volumeId));
303 if (dsizeOnly && !saveinodes)
305 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
306 HandleVolume(partP, name1);
313 struct DiskPartition64 *
314 FindCurrentPartition(void)
316 int dr = _getdrive();
317 struct DiskPartition64 *dp;
320 for (dp = DiskPartitionList; dp; dp = dp->next) {
321 if (*dp->devName - 'A' == dr)
325 printf("Current drive is not a valid vice partition.\n");
330 struct DiskPartition64 *
331 FindCurrentPartition(void)
336 struct DiskPartition64 *dp;
338 if (!getcwd(partName, 1023)) {
342 p = strchr(&partName[1], OS_DIRSEPC);
347 if (!(dp = VGetPartition(partName, 0))) {
350 printf("%s is not a valid vice partition.\n", partName);
360 struct DiskPartition64 *partP;
363 for (partP = DiskPartitionList; partP; partP = partP->next) {
364 printf("Processing Partition %s:\n", partP->name);
366 Stotvolsize += Totvolsize;
367 SVauxsize += TVauxsize;
368 SVvnodesize += TVvnodesize;
369 SVdiskused += TVdiskused;
373 printf("\nServer Totals%12d%9d%10d%10d%9d\n", SVdiskused, SVauxsize,
374 SVvnodesize, Stotvolsize, Stotvolsize - SVdiskused);
380 HandlePart(struct DiskPartition64 *partP)
388 (void)sprintf(pname, "%s\\", VPartitionPath(partP));
390 char *p = VPartitionPath(partP);
393 if ((dirp = opendir(p)) == NULL) {
394 printf("Can't read directory %s; giving up\n", p);
397 if (dsizeOnly && !saveinodes)
399 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
400 while ((dp = readdir(dirp))) {
401 p = (char *)strrchr(dp->d_name, '.');
402 if (p != NULL && strcmp(p, VHDREXT) == 0) {
403 HandleVolume(partP, dp->d_name);
404 Totvolsize += totvolsize;
405 TVauxsize += Vauxsize;
406 TVvnodesize += Vvnodesize;
407 TVdiskused += Vdiskused;
413 printf("\nPart Totals %12d%9d%10d%10d%9d (%d volumes)\n\n",
414 TVdiskused, TVauxsize, TVvnodesize, Totvolsize,
415 Totvolsize - TVdiskused, nvols);
421 HandleVolume(struct DiskPartition64 *dp, char *name)
423 struct VolumeHeader header;
424 struct VolumeDiskHeader diskHeader;
425 FD_t fd = INVALID_FD;
428 char headerName[1024];
431 printf("volinfo: -online not supported\n");
436 snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
437 VPartitionPath(dp), name);
438 if ((fd = OS_OPEN(headerName, O_RDONLY, 0666)) == INVALID_FD
439 || OS_SIZE(fd) < 0) {
440 printf("Volinfo: Cannot read volume header %s\n", name);
444 n = OS_READ(fd, &diskHeader, sizeof(diskHeader));
446 if (n != sizeof(diskHeader)
447 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
448 printf("Volinfo: Error reading volume header %s\n", name);
451 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
453 ("Volinfo: Volume %s, version number is incorrect; volume needs salvage\n",
457 DiskToVolumeHeader(&header, &diskHeader);
461 afs_sfsize_t size = 0;
464 if (!dsizeOnly && !saveinodes) {
466 printf("Volume header (size = %d):\n", (int)size);
467 printf("\tstamp\t= 0x%x\n", header.stamp.version);
468 printf("\tVolId\t= %u\n", header.id);
471 IH_INIT(ih, dp->device, header.parent, header.volumeInfo);
474 perror("opening volume info");
477 code = FDH_SIZE(fdP);
482 FDH_REALLYCLOSE(fdP);
485 if (!dsizeOnly && !saveinodes) {
486 printf("\tparent\t= %u\n", header.parent);
487 printf("\tInfo inode\t= %s (size = %d)\n",
488 PrintInode(NULL, header.volumeInfo), (int)code);
491 IH_INIT(ih, dp->device, header.parent, header.smallVnodeIndex);
494 perror("opening small vnode index");
497 code = FDH_SIZE(fdP);
502 FDH_REALLYCLOSE(fdP);
505 if (!dsizeOnly && !saveinodes) {
506 printf("\tSmall inode\t= %s (size = %d)\n",
507 PrintInode(NULL, header.smallVnodeIndex), (int)code);
510 IH_INIT(ih, dp->device, header.parent, header.largeVnodeIndex);
513 perror("opening large vnode index");
516 code = FDH_SIZE(fdP);
521 FDH_REALLYCLOSE(fdP);
524 if (!dsizeOnly && !saveinodes) {
525 printf("\tLarge inode\t= %s (size = %d)\n",
526 PrintInode(NULL, header.largeVnodeIndex), (int)code);
528 printf("Total aux volume size = %d\n\n", (int)size);
532 IH_INIT(ih, dp->device, header.parent, header.linkTable);
535 perror("opening link table index");
538 code = FDH_SIZE(fdP);
543 FDH_REALLYCLOSE(fdP);
546 if (!dsizeOnly && !saveinodes) {
547 printf("\tLink inode\t= %s (size = %d)\n",
548 PrintInode(NULL, header.linkTable), (int)code);
549 printf("Total aux volume size = %d\n\n", (int)size);
553 Vauxsize_k = size / 1024;
556 vp = AttachVolume(dp, name, &header);
558 printf("Volinfo: Error attaching volume header %s\n", name);
564 if (!dsizeOnly && !saveinodes)
565 printf("\nLarge vnodes (directories)\n");
566 PrintVnodes(vp, vLarge);
567 if (!dsizeOnly && !saveinodes) {
568 printf("\nSmall vnodes(files, symbolic links)\n");
572 printf("Saving all volume files to current directory ...\n");
573 PrintVnodes(vp, vSmall);
576 totvolsize = Vauxsize_k + Vvnodesize_k;
579 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
580 printf("%u\t%9d%9d%10d%10d%9d\t%24s\n", V_id(vp), Vdiskused,
581 Vauxsize_k, Vvnodesize_k, totvolsize, totvolsize - Vdiskused,
589 main(int argc, char **argv)
591 struct cmd_syndesc *ts;
594 ts = cmd_CreateSyntax(NULL, handleit, NULL, "Dump volume's internal state");
595 cmd_AddParm(ts, "-online", CMD_FLAG, CMD_OPTIONAL,
596 "Get info from running fileserver");
597 cmd_AddParm(ts, "-vnode", CMD_FLAG, CMD_OPTIONAL, "Dump vnode info");
598 cmd_AddParm(ts, "-date", CMD_FLAG, CMD_OPTIONAL,
599 "Also dump vnode's mod date");
600 cmd_AddParm(ts, "-inode", CMD_FLAG, CMD_OPTIONAL,
601 "Dump vnode's inode number");
602 cmd_AddParm(ts, "-itime", CMD_FLAG, CMD_OPTIONAL,
603 "Dump special inode's mod times");
604 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL,
605 "AFS partition name (default current partition)");
606 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
607 cmd_AddParm(ts, "-header", CMD_FLAG, CMD_OPTIONAL,
608 "Dump volume's header info");
609 cmd_AddParm(ts, "-sizeOnly", CMD_FLAG, CMD_OPTIONAL,
610 "Dump volume's size");
611 cmd_AddParm(ts, "-fixheader", CMD_FLAG, CMD_OPTIONAL,
612 "Try to fix header");
613 cmd_AddParm(ts, "-saveinodes", CMD_FLAG, CMD_OPTIONAL,
614 "Try to save all inodes");
615 cmd_AddParm(ts, "-orphaned", CMD_FLAG, CMD_OPTIONAL,
616 "List all dir/files without a parent");
617 #if defined(AFS_NAMEI_ENV)
618 cmd_AddParm(ts, "-filenames", CMD_FLAG, CMD_OPTIONAL, "Print filenames");
620 code = cmd_Dispatch(argc, argv);
624 #define typestring(type) (type == RWVOL? "read/write": type == ROVOL? "readonly": type == BACKVOL? "backup": "unknown")
627 PrintHeader(Volume * vp)
629 Vdiskused = V_diskused(vp);
630 if (dsizeOnly || saveinodes)
632 printf("Volume header for volume %u (%s)\n", V_id(vp), V_name(vp));
633 printf("stamp.magic = %x, stamp.version = %u\n", V_stamp(vp).magic,
634 V_stamp(vp).version);
636 ("inUse = %d, inService = %d, blessed = %d, needsSalvaged = %d, dontSalvage = %d\n",
637 V_inUse(vp), V_inService(vp), V_blessed(vp), V_needsSalvaged(vp),
640 ("type = %d (%s), uniquifier = %u, needsCallback = %d, destroyMe = %x\n",
641 V_type(vp), typestring(V_type(vp)), V_uniquifier(vp),
642 V_needsCallback(vp), V_destroyMe(vp));
644 ("id = %u, parentId = %u, cloneId = %u, backupId = %u, restoredFromId = %u\n",
645 V_id(vp), V_parentId(vp), V_cloneId(vp), V_backupId(vp),
646 V_restoredFromId(vp));
648 ("maxquota = %d, minquota = %d, maxfiles = %d, filecount = %d, diskused = %d\n",
649 V_maxquota(vp), V_minquota(vp), V_maxfiles(vp), V_filecount(vp),
651 printf("creationDate = %s, copyDate = %s\n", date(V_creationDate(vp)),
652 date(V_copyDate(vp)));
653 printf("backupDate = %s, expirationDate = %s\n", date(V_backupDate(vp)),
654 date(V_expirationDate(vp)));
655 printf("accessDate = %s, updateDate = %s\n", date(V_accessDate(vp)),
656 date(V_updateDate(vp)));
657 printf("owner = %u, accountNumber = %u\n", V_owner(vp),
658 V_accountNumber(vp));
660 ("dayUse = %u; week = (%u, %u, %u, %u, %u, %u, %u), dayUseDate = %s\n",
661 V_dayUse(vp), V_weekUse(vp)[0], V_weekUse(vp)[1], V_weekUse(vp)[2],
662 V_weekUse(vp)[3], V_weekUse(vp)[4], V_weekUse(vp)[5],
663 V_weekUse(vp)[6], date(V_dayUseDate(vp)));
664 printf("volUpdateCounter = %u\n", V_volUpCounter(vp));
668 * OS independent file info. Die on failure.
672 NT_date(FILETIME * ft)
674 static char result[8][64];
679 if (!FileTimeToLocalFileTime(ft, &lft)
680 || !FileTimeToSystemTime(&lft, &st)) {
681 printf("Time conversion failed.\n");
684 sprintf(result[next = ((next + 1) & 7)], "%4d/%02d/%02d.%2d:%2d:%2d",
685 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
691 GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime)
694 BY_HANDLE_FILE_INFORMATION fi;
695 if (!GetFileInformationByHandle(fd, &fi)) {
696 printf("GetFileInformationByHandle failed, exiting\n");
699 *size = (int)fi.nFileSizeLow;
701 *mtime = NT_date(&fi.ftLastWriteTime);
702 *atime = NT_date(&fi.ftLastAccessTime);
704 struct afs_stat_st status;
705 if (afs_fstat(fd, &status) == -1) {
706 printf("fstat failed %d\n", errno);
709 *size = (int)status.st_size;
710 *ctime = date(status.st_ctime);
711 *mtime = date(status.st_mtime);
712 *atime = date(status.st_atime);
717 PrintVnodes(Volume * vp, VnodeClass class)
720 (class == vSmall ? SIZEOF_SMALLDISKVNODE : SIZEOF_LARGEDISKVNODE);
721 char buf[SIZEOF_LARGEDISKVNODE];
722 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
723 StreamHandle_t *file;
724 int vnodeIndex, nVnodes;
725 afs_foff_t offset = 0;
727 IHandle_t *ih = vp->vnodeIndex[class].handle;
730 char *ctime, *atime, *mtime;
731 char nfile[50], buffer[256];
738 printf("open failed\n");
742 file = FDH_FDOPEN(fdP, "r");
744 printf("fdopen failed\n");
748 GetFileInfo(fdP->fd_fd, &size, &ctime, &atime, &mtime);
749 if (InodeTimes && !dsizeOnly) {
750 printf("ichanged : %s\nimodified: %s\niaccessed: %s\n\n", ctime,
754 nVnodes = (size / diskSize) - 1;
756 STREAM_ASEEK(file, diskSize);
761 nVnodes && STREAM_READ(vnode, diskSize, 1, file) == 1;
762 nVnodes--, vnodeIndex++, offset += diskSize) {
764 ino = VNDISK_GET_INO(vnode);
766 if (VALID_INO(ino) && (class == vSmall)) {
769 IH_INIT(ih1, V_device(vp), V_parentId(vp), ino);
772 printf("Can't open inode %s error %d (ignored)\n",
773 PrintInode(NULL, ino), errno);
776 snprintf(nfile, sizeof nfile, "TmpInode.%s",
777 PrintInode(NULL, ino));
778 ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600);
780 printf("Can't create file %s; error %d (ignored)\n",
787 len = FDH_PREAD(fdP1, buffer, sizeof(buffer), total);
789 FDH_REALLYCLOSE(fdP1);
794 ("Error while reading from inode %s (%d - ignored)\n",
795 PrintInode(NULL, ino), errno);
800 break; /* No more input */
801 nBytes = write(ofd, buffer, len);
803 FDH_REALLYCLOSE(fdP1);
808 ("Error while writing to \"%s\" (%d - ignored)\n",
817 FDH_REALLYCLOSE(fdP1);
820 printf("... Copied inode %s to file %s (%lu bytes)\n",
821 PrintInode(NULL, ino), nfile, (unsigned long)total);
824 #if defined(AFS_NAMEI_ENV)
825 PrintVnode(offset, vnode,
826 bitNumberToVnodeNumber(vnodeIndex, class), ino, vp);
828 PrintVnode(offset, vnode,
829 bitNumberToVnodeNumber(vnodeIndex, class), ino);
837 #if defined(AFS_NAMEI_ENV)
839 PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
840 Inode ino, Volume * vp)
843 PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
847 #if defined(AFS_NAMEI_ENV)
851 afs_fsize_t fileLength;
853 VNDISK_GET_LEN(fileLength, vnode);
854 Vvnodesize += fileLength;
855 Vvnodesize_k += fileLength / 1024;
858 if (orphaned && (fileLength == 0 || vnode->parent || !offset))
861 ("%10lld Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
862 (long long)offset, vnodeNumber, vnode->uniquifier, vnode->dataVersion,
863 vnode->cloned, (afs_uintmax_t) fileLength, vnode->linkCount,
866 printf(" inode: %s", PrintInode(NULL, ino));
868 printf(" ServerModTime: %s", date(vnode->serverModifyTime));
869 #if defined(AFS_NAMEI_ENV)
870 if (PrintFileNames) {
871 IH_INIT(ihtmpp, V_device(vp), V_parentId(vp), ino);
872 namei_HandleToName(&filename, ihtmpp);
873 #if !defined(AFS_NT40_ENV)
874 printf(" UFS-Filename: %s", filename.n_path);
876 printf(" NTFS-Filename: %s", filename.n_path);