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>
32 #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 <afs/afssyscalls.h>
52 #include <afs/afsutil.h>
60 int DumpVnodes = 0; /* Dump everything, i.e. summary of all vnodes */
61 int DumpInodeNumber = 0; /* Dump inode numbers with vnodes */
62 int DumpDate = 0; /* Dump vnode date (server modify date) with vnode */
63 int InodeTimes = 0; /* Dump some of the dates associated with inodes */
64 #if defined(AFS_NAMEI_ENV)
65 int PrintFileNames = 0;
69 int dsizeOnly = 0, totvolsize = 0, Vauxsize = 0, Vdiskused = 0, Vvnodesize =
71 int Vvnodesize_k = 0, Vauxsize_k = 0;
72 int Totvolsize = 0, TVauxsize = 0, TVdiskused = 0, TVvnodesize = 0;
73 int Stotvolsize = 0, SVauxsize = 0, SVdiskused = 0, SVvnodesize = 0;
74 int fixheader = 0, saveinodes = 0, orphaned = 0;
77 /* Forward Declarations */
78 void PrintHeader(Volume * vp);
79 void HandleAllPart(void);
80 void HandlePart(struct DiskPartition64 *partP);
81 void HandleVolume(struct DiskPartition64 *partP, char *name);
82 struct DiskPartition64 *FindCurrentPartition(void);
83 Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
84 struct VolumeHeader *header);
85 #if defined(AFS_NAMEI_ENV)
86 void PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
87 Inode ino, Volume * vp);
89 void PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
92 void PrintVnodes(Volume * vp, VnodeClass class);
97 #define MAX_DATE_RESULT 100
98 static char results[8][MAX_DATE_RESULT];
100 struct tm *tm = localtime(&date);
103 (void)strftime(buf, 32, "%Y/%m/%d.%H:%M:%S", tm); /* NT does not have %T */
104 snprintf(results[next = (next + 1) & 7], MAX_DATE_RESULT,
105 "%lu (%s)", (unsigned long)date, buf);
106 return results[next];
110 #include "AFS_component_version_number.c"
113 char name[VMAXPATHLEN];
118 ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
120 struct versionStamp *vsn;
124 vsn = (struct versionStamp *)to;
126 code = IH_IREAD(ih, 0, to, size);
130 if (vsn->magic != magic) {
132 printf("Inode %s: Bad magic %x (%x): IGNORED\n",
133 PrintInode(NULL, ih->ih_ino), vsn->magic, magic);
136 /* Check is conditional, in case caller wants to inspect version himself */
137 if (version && vsn->version != version) {
139 printf("Inode %s: Bad version %x (%x): IGNORED\n",
140 PrintInode(NULL, ih->ih_ino), vsn->version, version);
142 if (bad && fixheader) {
144 vsn->version = version;
145 printf("Special index inode %s has a bad header. Reconstructing...\n",
146 PrintInode(NULL, ih->ih_ino));
147 code = IH_IWRITE(ih, 0, to, size);
149 printf("Write failed; header left in damaged state\n");
152 if (!dsizeOnly && !saveinodes) {
153 printf("Inode %s: Good magic %x and version %x\n",
154 PrintInode(NULL, ih->ih_ino), magic, version);
162 AttachVolume(struct DiskPartition64 * dp, char *volname,
163 struct VolumeHeader * header)
168 vp = (Volume *) calloc(1, sizeof(Volume));
169 vp->specialStatus = 0;
170 vp->device = dp->device;
172 IH_INIT(vp->vnodeIndex[vLarge].handle, dp->device, header->parent,
173 header->largeVnodeIndex);
174 IH_INIT(vp->vnodeIndex[vSmall].handle, dp->device, header->parent,
175 header->smallVnodeIndex);
176 IH_INIT(vp->diskDataHandle, dp->device, header->parent,
178 IH_INIT(V_linkHandle(vp), dp->device, header->parent, header->linkTable);
179 vp->cacheCheck = 0; /* XXXX */
180 vp->shuttingDown = 0;
181 vp->goingOffline = 0;
183 vp->header = (struct volHeader *)calloc(1, sizeof(*vp->header));
184 ec = ReadHdr1(V_diskDataHandle(vp), (char *)&V_disk(vp),
185 sizeof(V_disk(vp)), VOLUMEINFOMAGIC, VOLUMEINFOVERSION);
187 struct IndexFileHeader iHead;
188 ec = ReadHdr1(vp->vnodeIndex[vSmall].handle, (char *)&iHead,
189 sizeof(iHead), SMALLINDEXMAGIC, SMALLINDEXVERSION);
192 struct IndexFileHeader iHead;
193 ec = ReadHdr1(vp->vnodeIndex[vLarge].handle, (char *)&iHead,
194 sizeof(iHead), LARGEINDEXMAGIC, LARGEINDEXVERSION);
198 struct versionStamp stamp;
199 ec = ReadHdr1(V_linkHandle(vp), (char *)&stamp, sizeof(stamp),
200 LINKTABLEMAGIC, LINKTABLEVERSION);
210 handleit(struct cmd_syndesc *as, void *arock)
214 afs_uint32 volumeId = 0;
216 struct DiskPartition64 *partP = NULL;
220 if (geteuid() != 0) {
221 printf("vol-info must be run as root; sorry\n");
226 if (as->parms[0].items)
230 if (as->parms[1].items)
234 if (as->parms[2].items)
238 if (as->parms[3].items)
242 if (as->parms[4].items)
246 if ((ti = as->parms[5].items))
248 if ((ti = as->parms[6].items))
249 volumeId = strtoul(ti->data, NULL, 10);
250 if (as->parms[7].items)
254 if (as->parms[8].items) {
260 if (as->parms[9].items) {
264 if (as->parms[10].items) {
270 if (as->parms[11].items) {
274 #if defined(AFS_NAMEI_ENV)
275 if (as->parms[12].items) {
284 err = VAttachPartitions();
286 printf("%d partitions had errors during attach.\n", err);
290 partP = VGetPartition(partName, 0);
292 printf("%s is not an AFS partition name on this server.\n",
308 partP = FindCurrentPartition();
310 printf("Current partition is not a vice partition.\n");
314 snprintf(name1, sizeof name1, VFORMAT,
315 afs_printable_uint32_lu(volumeId));
316 if (dsizeOnly && !saveinodes)
318 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
319 HandleVolume(partP, name1);
326 struct DiskPartition64 *
327 FindCurrentPartition(void)
329 int dr = _getdrive();
330 struct DiskPartition64 *dp;
333 for (dp = DiskPartitionList; dp; dp = dp->next) {
334 if (*dp->devName - 'A' == dr)
338 printf("Current drive is not a valid vice partition.\n");
343 struct DiskPartition64 *
344 FindCurrentPartition(void)
349 struct DiskPartition64 *dp;
351 if (!getcwd(partName, 1023)) {
355 p = strchr(&partName[1], OS_DIRSEPC);
360 if (!(dp = VGetPartition(partName, 0))) {
363 printf("%s is not a valid vice partition.\n", partName);
373 struct DiskPartition64 *partP;
376 for (partP = DiskPartitionList; partP; partP = partP->next) {
377 printf("Processing Partition %s:\n", partP->name);
379 Stotvolsize += Totvolsize;
380 SVauxsize += TVauxsize;
381 SVvnodesize += TVvnodesize;
382 SVdiskused += TVdiskused;
386 printf("\nServer Totals%12d%9d%10d%10d%9d\n", SVdiskused, SVauxsize,
387 SVvnodesize, Stotvolsize, Stotvolsize - SVdiskused);
393 HandlePart(struct DiskPartition64 *partP)
401 (void)sprintf(pname, "%s\\", VPartitionPath(partP));
403 char *p = VPartitionPath(partP);
406 if ((dirp = opendir(p)) == NULL) {
407 printf("Can't read directory %s; giving up\n", p);
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 printf("volinfo: -online not supported\n");
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 printf("Volinfo: Cannot read volume header %s\n", name);
457 n = OS_READ(fd, &diskHeader, sizeof(diskHeader));
459 if (n != sizeof(diskHeader)
460 || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
461 printf("Volinfo: Error reading volume header %s\n", name);
464 if (diskHeader.stamp.version != VOLUMEHEADERVERSION) {
466 ("Volinfo: Volume %s, version number is incorrect; volume needs salvage\n",
470 DiskToVolumeHeader(&header, &diskHeader);
474 afs_sfsize_t size = 0;
477 if (!dsizeOnly && !saveinodes) {
479 printf("Volume header (size = %d):\n", (int)size);
480 printf("\tstamp\t= 0x%x\n", header.stamp.version);
481 printf("\tVolId\t= %u\n", header.id);
484 IH_INIT(ih, dp->device, header.parent, header.volumeInfo);
487 perror("opening volume info");
490 code = FDH_SIZE(fdP);
495 FDH_REALLYCLOSE(fdP);
498 if (!dsizeOnly && !saveinodes) {
499 printf("\tparent\t= %u\n", header.parent);
500 printf("\tInfo inode\t= %s (size = %d)\n",
501 PrintInode(NULL, header.volumeInfo), (int)code);
504 IH_INIT(ih, dp->device, header.parent, header.smallVnodeIndex);
507 perror("opening small vnode index");
510 code = FDH_SIZE(fdP);
515 FDH_REALLYCLOSE(fdP);
518 if (!dsizeOnly && !saveinodes) {
519 printf("\tSmall inode\t= %s (size = %d)\n",
520 PrintInode(NULL, header.smallVnodeIndex), (int)code);
523 IH_INIT(ih, dp->device, header.parent, header.largeVnodeIndex);
526 perror("opening large vnode index");
529 code = FDH_SIZE(fdP);
534 FDH_REALLYCLOSE(fdP);
537 if (!dsizeOnly && !saveinodes) {
538 printf("\tLarge inode\t= %s (size = %d)\n",
539 PrintInode(NULL, header.largeVnodeIndex), (int)code);
541 printf("Total aux volume size = %d\n\n", (int)size);
545 IH_INIT(ih, dp->device, header.parent, header.linkTable);
548 perror("opening link table index");
551 code = FDH_SIZE(fdP);
556 FDH_REALLYCLOSE(fdP);
559 if (!dsizeOnly && !saveinodes) {
560 printf("\tLink inode\t= %s (size = %d)\n",
561 PrintInode(NULL, header.linkTable), (int)code);
562 printf("Total aux volume size = %d\n\n", (int)size);
566 Vauxsize_k = size / 1024;
569 vp = AttachVolume(dp, name, &header);
571 printf("Volinfo: Error attaching volume header %s\n", name);
577 if (!dsizeOnly && !saveinodes)
578 printf("\nLarge vnodes (directories)\n");
579 PrintVnodes(vp, vLarge);
580 if (!dsizeOnly && !saveinodes) {
581 printf("\nSmall vnodes(files, symbolic links)\n");
585 printf("Saving all volume files to current directory ...\n");
586 PrintVnodes(vp, vSmall);
589 totvolsize = Vauxsize_k + Vvnodesize_k;
592 ("Volume-Id\t Volsize Auxsize Inodesize AVolsize SizeDiff (VolName)\n");
593 printf("%u\t%9d%9d%10d%10d%9d\t%24s\n", V_id(vp), Vdiskused,
594 Vauxsize_k, Vvnodesize_k, totvolsize, totvolsize - Vdiskused,
602 main(int argc, char **argv)
604 struct cmd_syndesc *ts;
607 ts = cmd_CreateSyntax(NULL, handleit, NULL, "Dump volume's internal state");
608 cmd_AddParm(ts, "-online", CMD_FLAG, CMD_OPTIONAL,
609 "Get info from running fileserver");
610 cmd_AddParm(ts, "-vnode", CMD_FLAG, CMD_OPTIONAL, "Dump vnode info");
611 cmd_AddParm(ts, "-date", CMD_FLAG, CMD_OPTIONAL,
612 "Also dump vnode's mod date");
613 cmd_AddParm(ts, "-inode", CMD_FLAG, CMD_OPTIONAL,
614 "Dump vnode's inode number");
615 cmd_AddParm(ts, "-itime", CMD_FLAG, CMD_OPTIONAL,
616 "Dump special inode's mod times");
617 cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL,
618 "AFS partition name (default current partition)");
619 cmd_AddParm(ts, "-volumeid", CMD_LIST, CMD_OPTIONAL, "Volume id");
620 cmd_AddParm(ts, "-header", CMD_FLAG, CMD_OPTIONAL,
621 "Dump volume's header info");
622 cmd_AddParm(ts, "-sizeOnly", CMD_FLAG, CMD_OPTIONAL,
623 "Dump volume's size");
624 cmd_AddParm(ts, "-fixheader", CMD_FLAG, CMD_OPTIONAL,
625 "Try to fix header");
626 cmd_AddParm(ts, "-saveinodes", CMD_FLAG, CMD_OPTIONAL,
627 "Try to save all inodes");
628 cmd_AddParm(ts, "-orphaned", CMD_FLAG, CMD_OPTIONAL,
629 "List all dir/files without a parent");
630 #if defined(AFS_NAMEI_ENV)
631 cmd_AddParm(ts, "-filenames", CMD_FLAG, CMD_OPTIONAL, "Print filenames");
633 code = cmd_Dispatch(argc, argv);
637 #define typestring(type) (type == RWVOL? "read/write": type == ROVOL? "readonly": type == BACKVOL? "backup": "unknown")
640 PrintHeader(Volume * vp)
642 Vdiskused = V_diskused(vp);
643 if (dsizeOnly || saveinodes)
645 printf("Volume header for volume %u (%s)\n", V_id(vp), V_name(vp));
646 printf("stamp.magic = %x, stamp.version = %u\n", V_stamp(vp).magic,
647 V_stamp(vp).version);
649 ("inUse = %d, inService = %d, blessed = %d, needsSalvaged = %d, dontSalvage = %d\n",
650 V_inUse(vp), V_inService(vp), V_blessed(vp), V_needsSalvaged(vp),
653 ("type = %d (%s), uniquifier = %u, needsCallback = %d, destroyMe = %x\n",
654 V_type(vp), typestring(V_type(vp)), V_uniquifier(vp),
655 V_needsCallback(vp), V_destroyMe(vp));
657 ("id = %u, parentId = %u, cloneId = %u, backupId = %u, restoredFromId = %u\n",
658 V_id(vp), V_parentId(vp), V_cloneId(vp), V_backupId(vp),
659 V_restoredFromId(vp));
661 ("maxquota = %d, minquota = %d, maxfiles = %d, filecount = %d, diskused = %d\n",
662 V_maxquota(vp), V_minquota(vp), V_maxfiles(vp), V_filecount(vp),
664 printf("creationDate = %s, copyDate = %s\n", date(V_creationDate(vp)),
665 date(V_copyDate(vp)));
666 printf("backupDate = %s, expirationDate = %s\n", date(V_backupDate(vp)),
667 date(V_expirationDate(vp)));
668 printf("accessDate = %s, updateDate = %s\n", date(V_accessDate(vp)),
669 date(V_updateDate(vp)));
670 printf("owner = %u, accountNumber = %u\n", V_owner(vp),
671 V_accountNumber(vp));
673 ("dayUse = %u; week = (%u, %u, %u, %u, %u, %u, %u), dayUseDate = %s\n",
674 V_dayUse(vp), V_weekUse(vp)[0], V_weekUse(vp)[1], V_weekUse(vp)[2],
675 V_weekUse(vp)[3], V_weekUse(vp)[4], V_weekUse(vp)[5],
676 V_weekUse(vp)[6], date(V_dayUseDate(vp)));
677 printf("volUpdateCounter = %u\n", V_volUpCounter(vp));
681 * OS independent file info. Die on failure.
685 NT_date(FILETIME * ft)
687 static char result[8][64];
692 if (!FileTimeToLocalFileTime(ft, &lft)
693 || !FileTimeToSystemTime(&lft, &st)) {
694 printf("Time conversion failed.\n");
697 sprintf(result[next = ((next + 1) & 7)], "%4d/%02d/%02d.%2d:%2d:%2d",
698 st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
704 GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime)
707 BY_HANDLE_FILE_INFORMATION fi;
708 if (!GetFileInformationByHandle(fd, &fi)) {
709 printf("GetFileInformationByHandle failed, exiting\n");
712 *size = (int)fi.nFileSizeLow;
714 *mtime = NT_date(&fi.ftLastWriteTime);
715 *atime = NT_date(&fi.ftLastAccessTime);
717 struct afs_stat_st status;
718 if (afs_fstat(fd, &status) == -1) {
719 printf("fstat failed %d\n", errno);
722 *size = (int)status.st_size;
723 *ctime = date(status.st_ctime);
724 *mtime = date(status.st_mtime);
725 *atime = date(status.st_atime);
730 PrintVnodes(Volume * vp, VnodeClass class)
733 (class == vSmall ? SIZEOF_SMALLDISKVNODE : SIZEOF_LARGEDISKVNODE);
734 char buf[SIZEOF_LARGEDISKVNODE];
735 struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
736 StreamHandle_t *file;
737 int vnodeIndex, nVnodes;
738 afs_foff_t offset = 0;
740 IHandle_t *ih = vp->vnodeIndex[class].handle;
743 char *ctime, *atime, *mtime;
744 char nfile[50], buffer[256];
751 printf("open failed\n");
755 file = FDH_FDOPEN(fdP, "r");
757 printf("fdopen failed\n");
761 GetFileInfo(fdP->fd_fd, &size, &ctime, &atime, &mtime);
762 if (InodeTimes && !dsizeOnly) {
763 printf("ichanged : %s\nimodified: %s\niaccessed: %s\n\n", ctime,
767 nVnodes = (size / diskSize) - 1;
769 STREAM_ASEEK(file, diskSize);
774 nVnodes && STREAM_READ(vnode, diskSize, 1, file) == 1;
775 nVnodes--, vnodeIndex++, offset += diskSize) {
777 ino = VNDISK_GET_INO(vnode);
779 if (VALID_INO(ino) && (class == vSmall)) {
782 IH_INIT(ih1, V_device(vp), V_parentId(vp), ino);
785 printf("Can't open inode %s error %d (ignored)\n",
786 PrintInode(NULL, ino), errno);
789 snprintf(nfile, sizeof nfile, "TmpInode.%s",
790 PrintInode(NULL, ino));
791 ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600);
793 printf("Can't create file %s; error %d (ignored)\n",
800 len = FDH_PREAD(fdP1, buffer, sizeof(buffer), total);
802 FDH_REALLYCLOSE(fdP1);
807 ("Error while reading from inode %s (%d - ignored)\n",
808 PrintInode(NULL, ino), errno);
813 break; /* No more input */
814 nBytes = write(ofd, buffer, len);
816 FDH_REALLYCLOSE(fdP1);
821 ("Error while writing to \"%s\" (%d - ignored)\n",
830 FDH_REALLYCLOSE(fdP1);
833 printf("... Copied inode %s to file %s (%lu bytes)\n",
834 PrintInode(NULL, ino), nfile, (unsigned long)total);
837 #if defined(AFS_NAMEI_ENV)
838 PrintVnode(offset, vnode,
839 bitNumberToVnodeNumber(vnodeIndex, class), ino, vp);
841 PrintVnode(offset, vnode,
842 bitNumberToVnodeNumber(vnodeIndex, class), ino);
850 #if defined(AFS_NAMEI_ENV)
852 PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
853 Inode ino, Volume * vp)
856 PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
860 #if defined(AFS_NAMEI_ENV)
864 afs_fsize_t fileLength;
866 VNDISK_GET_LEN(fileLength, vnode);
867 Vvnodesize += fileLength;
868 Vvnodesize_k += fileLength / 1024;
871 if (orphaned && (fileLength == 0 || vnode->parent || !offset))
874 ("%10lld Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
875 (long long)offset, vnodeNumber, vnode->uniquifier, vnode->dataVersion,
876 vnode->cloned, (afs_uintmax_t) fileLength, vnode->linkCount,
879 printf(" inode: %s", PrintInode(NULL, ino));
881 printf(" ServerModTime: %s", date(vnode->serverModifyTime));
882 #if defined(AFS_NAMEI_ENV)
883 if (PrintFileNames) {
884 IH_INIT(ihtmpp, V_device(vp), V_parentId(vp), ino);
885 namei_HandleToName(&filename, ihtmpp);
886 #if !defined(AFS_NT40_ENV)
887 printf(" UFS-Filename: %s", filename.n_path);
889 printf(" NTFS-Filename: %s", filename.n_path);