doc: document volinfo -filenames option
[openafs.git] / src / vol / vol-info.c
index 6b92e21..432b55f 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
    System:             VICE-TWO
    Module:             vol-info.c
    Institution:        The Information Technology Center, Carnegie-Mellon University
-   
+
    */
 
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
+#include <roken.h>
 
 #include <ctype.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <stdio.h>
-#include <string.h>
-#ifdef AFS_NT40_ENV
-#include <fcntl.h>
-#include <time.h>
-#include <io.h>
-#else
-#include <sys/param.h>
+
+#ifdef HAVE_SYS_FILE_H
 #include <sys/file.h>
-#include <sys/time.h>
 #endif
+
 #include <afs/cmd.h>
+#include <afs/dir.h>
 
 #include <rx/xdr.h>
 #include <afs/afsint.h>
@@ -48,24 +40,8 @@ RCSID
 #include "volume.h"
 #include "partition.h"
 #include "viceinode.h"
-#include "volinodes.h"
 #include <afs/afssyscalls.h>
-
-#ifdef _AIX
-#include <time.h>
-#endif
-
-#include <dirent.h>
-
-#ifdef O_LARGEFILE
-#define afs_stat       stat64
-#define afs_fstat      fstat64
-#define afs_open       open64
-#else /* !O_LARGEFILE */
-#define afs_stat       stat
-#define afs_fstat      fstat
-#define afs_open       open
-#endif /* !O_LARGEFILE */
+#include <afs/afsutil.h>
 
 int DumpVnodes = 0;            /* Dump everything, i.e. summary of all vnodes */
 int DumpInodeNumber = 0;       /* Dump inode numbers with vnodes */
@@ -78,24 +54,25 @@ int online = 0;
 int dheader = 0;
 int dsizeOnly = 0, totvolsize = 0, Vauxsize = 0, Vdiskused = 0, Vvnodesize =
     0;
+int Vvnodesize_k = 0, Vauxsize_k = 0;
 int Totvolsize = 0, TVauxsize = 0, TVdiskused = 0, TVvnodesize = 0;
 int Stotvolsize = 0, SVauxsize = 0, SVdiskused = 0, SVvnodesize = 0;
 int fixheader = 0, saveinodes = 0, orphaned = 0;
 int VolumeChanged;
 
 /* Forward Declarations */
-void PrintHeader(register Volume * vp);
+void PrintHeader(Volume * vp);
 void HandleAllPart(void);
-void HandlePart(struct DiskPartition *partP);
-void HandleVolume(struct DiskPartition *partP, char *name);
-struct DiskPartition *FindCurrentPartition(void);
-Volume *AttachVolume(struct DiskPartition *dp, char *volname,
-                    register struct VolumeHeader *header);
+void HandlePart(struct DiskPartition64 *partP);
+void HandleVolume(struct DiskPartition64 *partP, char *name);
+struct DiskPartition64 *FindCurrentPartition(void);
+Volume *AttachVolume(struct DiskPartition64 *dp, char *volname,
+                    struct VolumeHeader *header);
 #if defined(AFS_NAMEI_ENV)
-void PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
+void PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
                Inode ino, Volume * vp);
 #else
-void PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
+void PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
                Inode ino);
 #endif
 void PrintVnodes(Volume * vp, VnodeClass class);
@@ -105,13 +82,13 @@ date(time_t date)
 {
 #define MAX_DATE_RESULT        100
     static char results[8][MAX_DATE_RESULT];
-    static next;
+    static int next;
     struct tm *tm = localtime(&date);
     char buf[32];
 
     (void)strftime(buf, 32, "%Y/%m/%d.%H:%M:%S", tm);  /* NT does not have %T */
-    (void)afs_snprintf(results[next = (next + 1) & 7], MAX_DATE_RESULT,
-                      "%lu (%s)", (unsigned long)date, buf);
+    snprintf(results[next = (next + 1) & 7], MAX_DATE_RESULT,
+            "%lu (%s)", (unsigned long)date, buf);
     return results[next];
 }
 
@@ -168,10 +145,10 @@ ReadHdr1(IHandle_t * ih, char *to, int size, u_int magic, u_int version)
 
 
 Volume *
-AttachVolume(struct DiskPartition * dp, char *volname,
-            register struct VolumeHeader * header)
+AttachVolume(struct DiskPartition64 * dp, char *volname,
+            struct VolumeHeader * header)
 {
-    register Volume *vp;
+    Volume *vp;
     afs_int32 ec = 0;
 
     vp = (Volume *) calloc(1, sizeof(Volume));
@@ -216,13 +193,13 @@ AttachVolume(struct DiskPartition * dp, char *volname,
 
 
 static int
-handleit(struct cmd_syndesc *as)
+handleit(struct cmd_syndesc *as, void *arock)
 {
-    register struct cmd_item *ti;
+    struct cmd_item *ti;
     int err = 0;
-    int volumeId = 0;
+    afs_uint32 volumeId = 0;
     char *partName = 0;
-    struct DiskPartition *partP = NULL;
+    struct DiskPartition64 *partP = NULL;
 
 
 #ifndef AFS_NT40_ENV
@@ -255,7 +232,7 @@ handleit(struct cmd_syndesc *as)
     if ((ti = as->parms[5].items))
        partName = ti->data;
     if ((ti = as->parms[6].items))
-       volumeId = atoi(ti->data);
+       volumeId = strtoul(ti->data, NULL, 10);
     if (as->parms[7].items)
        dheader = 1;
     else
@@ -280,13 +257,14 @@ handleit(struct cmd_syndesc *as)
        orphaned = 1;
        DumpVnodes = 1;
     } else
+       orphaned = 0;
 #if defined(AFS_NAMEI_ENV)
     if (as->parms[12].items) {
        PrintFileNames = 1;
        DumpVnodes = 1;
     } else
+       PrintFileNames = 0;
 #endif
-       orphaned = 0;
 
     DInit(10);
 
@@ -320,8 +298,8 @@ handleit(struct cmd_syndesc *as)
                exit(1);
            }
        }
-       (void)afs_snprintf(name1, sizeof name1, VFORMAT,
-                          (unsigned long)volumeId);
+       snprintf(name1, sizeof name1, VFORMAT,
+                afs_printable_uint32_lu(volumeId));
        if (dsizeOnly && !saveinodes)
            printf
                ("Volume-Id\t  Volsize  Auxsize Inodesize  AVolsize SizeDiff                (VolName)\n");
@@ -332,11 +310,11 @@ handleit(struct cmd_syndesc *as)
 
 #ifdef AFS_NT40_ENV
 #include <direct.h>
-struct DiskPartition *
-FindCurrentPartition()
+struct DiskPartition64 *
+FindCurrentPartition(void)
 {
     int dr = _getdrive();
-    struct DiskPartition *dp;
+    struct DiskPartition64 *dp;
 
     dr--;
     for (dp = DiskPartitionList; dp; dp = dp->next) {
@@ -349,19 +327,19 @@ FindCurrentPartition()
     return dp;
 }
 #else
-struct DiskPartition *
-FindCurrentPartition()
+struct DiskPartition64 *
+FindCurrentPartition(void)
 {
     char partName[1024];
     char tmp = '\0';
     char *p;
-    struct DiskPartition *dp;
+    struct DiskPartition64 *dp;
 
     if (!getcwd(partName, 1023)) {
        perror("pwd");
        exit(1);
     }
-    p = strchr(&partName[1], '/');
+    p = strchr(&partName[1], OS_DIRSEPC);
     if (p) {
        tmp = *p;
        *p = '\0';
@@ -379,7 +357,7 @@ FindCurrentPartition()
 void
 HandleAllPart(void)
 {
-    struct DiskPartition *partP;
+    struct DiskPartition64 *partP;
 
 
     for (partP = DiskPartitionList; partP; partP = partP->next) {
@@ -399,7 +377,7 @@ HandleAllPart(void)
 
 
 void
-HandlePart(struct DiskPartition *partP)
+HandlePart(struct DiskPartition64 *partP)
 {
     int nvols = 0;
     DIR *dirp;
@@ -412,11 +390,7 @@ HandlePart(struct DiskPartition *partP)
     char *p = VPartitionPath(partP);
 #endif
 
-    if (chdir(p) == -1) {
-       printf("Can't chdir to partition %s; giving up\n", p);
-       exit(1);
-    }
-    if ((dirp = opendir(".")) == NULL) {
+    if ((dirp = opendir(p)) == NULL) {
        printf("Can't read directory %s; giving up\n", p);
        exit(1);
     }
@@ -444,12 +418,11 @@ HandlePart(struct DiskPartition *partP)
 
 
 void
-HandleVolume(struct DiskPartition *dp, char *name)
+HandleVolume(struct DiskPartition64 *dp, char *name)
 {
     struct VolumeHeader header;
     struct VolumeDiskHeader diskHeader;
-    struct afs_stat status, stat;
-    register int fd;
+    FD_t fd = INVALID_FD;
     Volume *vp;
     IHandle_t *ih;
     char headerName[1024];
@@ -458,17 +431,17 @@ HandleVolume(struct DiskPartition *dp, char *name)
        printf("volinfo: -online not supported\n");
        exit(1);
     } else {
-       afs_int32 n;
+       afs_sfsize_t n;
 
-       (void)afs_snprintf(headerName, sizeof headerName, "%s/%s",
-                          VPartitionPath(dp), name);
-       if ((fd = afs_open(headerName, O_RDONLY)) == -1
-           || afs_fstat(fd, &status) == -1) {
+       snprintf(headerName, sizeof headerName, "%s" OS_DIRSEP "%s",
+                VPartitionPath(dp), name);
+       if ((fd = OS_OPEN(headerName, O_RDONLY, 0666)) == INVALID_FD
+           || OS_SIZE(fd) < 0) {
            printf("Volinfo: Cannot read volume header %s\n", name);
-           close(fd);
+           OS_CLOSE(fd);
            exit(1);
        }
-       n = read(fd, &diskHeader, sizeof(diskHeader));
+       n = OS_READ(fd, &diskHeader, sizeof(diskHeader));
 
        if (n != sizeof(diskHeader)
            || diskHeader.stamp.magic != VOLUMEHEADERMAGIC) {
@@ -485,20 +458,17 @@ HandleVolume(struct DiskPartition *dp, char *name)
 
        if (dheader) {
            FdHandle_t *fdP;
-           int size = 0;
-           int code;
+           afs_sfsize_t size = 0;
+           afs_sfsize_t code;
 
-           if (afs_fstat(fd, &stat) == -1) {
-               perror("stat");
-               exit(1);
-           }
            if (!dsizeOnly && !saveinodes) {
-               printf("Volume header (size = %d):\n", size = stat.st_size);
+               size = OS_SIZE(fd);
+               printf("Volume header (size = %d):\n", (int)size);
                printf("\tstamp\t= 0x%x\n", header.stamp.version);
                printf("\tVolId\t= %u\n", header.id);
            }
 
-           IH_INIT(ih, dp->device, header.id, header.volumeInfo);
+           IH_INIT(ih, dp->device, header.parent, header.volumeInfo);
            fdP = IH_OPEN(ih);
            if (fdP == NULL) {
                perror("opening volume info");
@@ -515,10 +485,10 @@ HandleVolume(struct DiskPartition *dp, char *name)
            if (!dsizeOnly && !saveinodes) {
                printf("\tparent\t= %u\n", header.parent);
                printf("\tInfo inode\t= %s (size = %d)\n",
-                      PrintInode(NULL, header.volumeInfo), code);
+                      PrintInode(NULL, header.volumeInfo), (int)code);
            }
 
-           IH_INIT(ih, dp->device, header.id, header.smallVnodeIndex);
+           IH_INIT(ih, dp->device, header.parent, header.smallVnodeIndex);
            fdP = IH_OPEN(ih);
            if (fdP == NULL) {
                perror("opening small vnode index");
@@ -534,10 +504,10 @@ HandleVolume(struct DiskPartition *dp, char *name)
            size += code;
            if (!dsizeOnly && !saveinodes) {
                printf("\tSmall inode\t= %s (size = %d)\n",
-                      PrintInode(NULL, header.smallVnodeIndex), code);
+                      PrintInode(NULL, header.smallVnodeIndex), (int)code);
            }
 
-           IH_INIT(ih, dp->device, header.id, header.largeVnodeIndex);
+           IH_INIT(ih, dp->device, header.parent, header.largeVnodeIndex);
            fdP = IH_OPEN(ih);
            if (fdP == NULL) {
                perror("opening large vnode index");
@@ -553,13 +523,13 @@ HandleVolume(struct DiskPartition *dp, char *name)
            size += code;
            if (!dsizeOnly && !saveinodes) {
                printf("\tLarge inode\t= %s (size = %d)\n",
-                      PrintInode(NULL, header.largeVnodeIndex), code);
+                      PrintInode(NULL, header.largeVnodeIndex), (int)code);
 #ifndef AFS_NT40_ENV
-               printf("Total aux volume size = %d\n\n", size);
+               printf("Total aux volume size = %d\n\n", (int)size);
 #endif
            }
 #ifdef AFS_NAMEI_ENV
-           IH_INIT(ih, dp->device, header.id, header.linkTable);
+           IH_INIT(ih, dp->device, header.parent, header.linkTable);
            fdP = IH_OPEN(ih);
            if (fdP == NULL) {
                perror("opening link table index");
@@ -575,17 +545,18 @@ HandleVolume(struct DiskPartition *dp, char *name)
            size += code;
            if (!dsizeOnly && !saveinodes) {
                printf("\tLink inode\t= %s (size = %d)\n",
-                      PrintInode(NULL, header.linkTable), code);
-               printf("Total aux volume size = %d\n\n", size);
+                      PrintInode(NULL, header.linkTable), (int)code);
+               printf("Total aux volume size = %d\n\n", (int)size);
            }
 #endif
            Vauxsize = size;
+           Vauxsize_k = size / 1024;
        }
-       close(fd);
+       OS_CLOSE(fd);
        vp = AttachVolume(dp, name, &header);
        if (!vp) {
            printf("Volinfo: Error attaching volume header %s\n", name);
-           exit(1);
+           return;
        }
     }
     PrintHeader(vp);
@@ -602,32 +573,32 @@ HandleVolume(struct DiskPartition *dp, char *name)
        PrintVnodes(vp, vSmall);
     }
     if (dsizeOnly) {
-       Vauxsize = Vauxsize / 1024;
-       Vvnodesize = Vvnodesize / 1024;
-       totvolsize = Vauxsize + Vvnodesize;
+       totvolsize = Vauxsize_k + Vvnodesize_k;
        if (saveinodes)
            printf
                ("Volume-Id\t  Volsize  Auxsize Inodesize  AVolsize SizeDiff                (VolName)\n");
-
-       printf("%u\t%9d%9d%10d%10d%9d\t%24s\n", V_id(vp), Vdiskused, Vauxsize,
-              Vvnodesize, totvolsize, totvolsize - Vdiskused, V_name(vp));
+       printf("%u\t%9d%9d%10d%10d%9d\t%24s\n", V_id(vp), Vdiskused,
+              Vauxsize_k, Vvnodesize_k, totvolsize, totvolsize - Vdiskused,
+              V_name(vp));
     }
+    free(vp->header);
+    free(vp);
 }
 
-
+int
 main(int argc, char **argv)
 {
-    register struct cmd_syndesc *ts;
+    struct cmd_syndesc *ts;
     afs_int32 code;
 
-    ts = cmd_CreateSyntax(NULL, handleit, 0, "Dump volume's internal state");
+    ts = cmd_CreateSyntax(NULL, handleit, NULL, "Dump volume's internal state");
     cmd_AddParm(ts, "-online", CMD_FLAG, CMD_OPTIONAL,
                "Get info from running fileserver");
     cmd_AddParm(ts, "-vnode", CMD_FLAG, CMD_OPTIONAL, "Dump vnode info");
     cmd_AddParm(ts, "-date", CMD_FLAG, CMD_OPTIONAL,
                "Also dump vnode's mod date");
     cmd_AddParm(ts, "-inode", CMD_FLAG, CMD_OPTIONAL,
-               "Dump vnode's inode number");
+               "Also dump vnode's inode number");
     cmd_AddParm(ts, "-itime", CMD_FLAG, CMD_OPTIONAL,
                "Dump special inode's mod times");
     cmd_AddParm(ts, "-part", CMD_LIST, CMD_OPTIONAL,
@@ -644,7 +615,7 @@ main(int argc, char **argv)
     cmd_AddParm(ts, "-orphaned", CMD_FLAG, CMD_OPTIONAL,
                "List all dir/files without a parent");
 #if defined(AFS_NAMEI_ENV)
-    cmd_AddParm(ts, "-filenames", CMD_FLAG, CMD_OPTIONAL, "Print filenames");
+    cmd_AddParm(ts, "-filenames", CMD_FLAG, CMD_OPTIONAL, "Also dump vnode's namei filename");
 #endif
     code = cmd_Dispatch(argc, argv);
     return code;
@@ -653,7 +624,7 @@ main(int argc, char **argv)
 #define typestring(type) (type == RWVOL? "read/write": type == ROVOL? "readonly": type == BACKVOL? "backup": "unknown")
 
 void
-PrintHeader(register Volume * vp)
+PrintHeader(Volume * vp)
 {
     Vdiskused = V_diskused(vp);
     if (dsizeOnly || saveinodes)
@@ -690,6 +661,7 @@ PrintHeader(register Volume * vp)
         V_dayUse(vp), V_weekUse(vp)[0], V_weekUse(vp)[1], V_weekUse(vp)[2],
         V_weekUse(vp)[3], V_weekUse(vp)[4], V_weekUse(vp)[5],
         V_weekUse(vp)[6], date(V_dayUseDate(vp)));
+    printf("volUpdateCounter = %u\n", V_volUpCounter(vp));
 }
 
 /* GetFileInfo
@@ -729,7 +701,7 @@ GetFileInfo(FD_t fd, int *size, char **ctime, char **mtime, char **atime)
     *mtime = NT_date(&fi.ftLastWriteTime);
     *atime = NT_date(&fi.ftLastAccessTime);
 #else
-    struct afs_stat status;
+    struct afs_stat_st status;
     if (afs_fstat(fd, &status) == -1) {
        printf("fstat failed %d\n", errno);
        exit(1);
@@ -749,14 +721,17 @@ PrintVnodes(Volume * vp, VnodeClass class)
     char buf[SIZEOF_LARGEDISKVNODE];
     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
     StreamHandle_t *file;
-    register int vnodeIndex, nVnodes, offset = 0;
+    int vnodeIndex, nVnodes;
+    afs_foff_t offset = 0;
     Inode ino;
     IHandle_t *ih = vp->vnodeIndex[class].handle;
     FdHandle_t *fdP;
     int size;
     char *ctime, *atime, *mtime;
     char nfile[50], buffer[256];
-    int total, ofd, len, code, bad = 0;
+    int ofd, bad = 0;
+    afs_foff_t total;
+    ssize_t len;
 
     fdP = IH_OPEN(ih);
     if (fdP == NULL) {
@@ -778,7 +753,7 @@ PrintVnodes(Volume * vp, VnodeClass class)
 
     nVnodes = (size / diskSize) - 1;
     if (nVnodes > 0) {
-       STREAM_SEEK(file, diskSize, 0);
+       STREAM_ASEEK(file, diskSize);
     } else
        nVnodes = 0;
 
@@ -798,8 +773,8 @@ PrintVnodes(Volume * vp, VnodeClass class)
                           PrintInode(NULL, ino), errno);
                    continue;
                }
-               (void)afs_snprintf(nfile, sizeof nfile, "TmpInode.%s",
-                                  PrintInode(NULL, ino));
+               snprintf(nfile, sizeof nfile, "TmpInode.%s",
+                        PrintInode(NULL, ino));
                ofd = afs_open(nfile, O_CREAT | O_RDWR | O_TRUNC, 0600);
                if (ofd < 0) {
                    printf("Can't create file %s; error %d (ignored)\n",
@@ -808,7 +783,8 @@ PrintVnodes(Volume * vp, VnodeClass class)
                }
                total = bad = 0;
                while (1) {
-                   len = FDH_READ(fdP1, buffer, sizeof(buffer));
+                   ssize_t nBytes;
+                   len = FDH_PREAD(fdP1, buffer, sizeof(buffer), total);
                    if (len < 0) {
                        FDH_REALLYCLOSE(fdP1);
                        IH_RELEASE(ih1);
@@ -822,8 +798,8 @@ PrintVnodes(Volume * vp, VnodeClass class)
                    }
                    if (len == 0)
                        break;  /* No more input */
-                   code = write(ofd, buffer, len);
-                   if (code != len) {
+                   nBytes = write(ofd, buffer, len);
+                   if (nBytes != len) {
                        FDH_REALLYCLOSE(fdP1);
                        IH_RELEASE(ih1);
                        close(ofd);
@@ -841,8 +817,8 @@ PrintVnodes(Volume * vp, VnodeClass class)
                FDH_REALLYCLOSE(fdP1);
                IH_RELEASE(ih1);
                close(ofd);
-               printf("... Copied inode %s to file %s (%d bytes)\n",
-                      PrintInode(NULL, ino), nfile, total);
+               printf("... Copied inode %s to file %s (%lu bytes)\n",
+                      PrintInode(NULL, ino), nfile, (unsigned long)total);
            }
        } else {
 #if defined(AFS_NAMEI_ENV)
@@ -860,33 +836,30 @@ PrintVnodes(Volume * vp, VnodeClass class)
 
 #if defined(AFS_NAMEI_ENV)
 void
-PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
+PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
           Inode ino, Volume * vp)
 #else
 void
-PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
+PrintVnode(afs_foff_t offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
           Inode ino)
 #endif
 {
 #if defined(AFS_NAMEI_ENV)
     IHandle_t *ihtmpp;
-#if !defined(AFS_NT40_ENV)
     namei_t filename;
-#else
-    char filename[MAX_PATH];
-#endif
 #endif
     afs_fsize_t fileLength;
 
     VNDISK_GET_LEN(fileLength, vnode);
     Vvnodesize += fileLength;
+    Vvnodesize_k += fileLength / 1024;
     if (dsizeOnly)
        return;
     if (orphaned && (fileLength == 0 || vnode->parent || !offset))
        return;
     printf
-       ("%10d Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
-        offset, vnodeNumber, vnode->uniquifier, vnode->dataVersion,
+       ("%10lld Vnode %u.%u.%u cloned: %u, length: %llu linkCount: %d parent: %u",
+        (long long)offset, vnodeNumber, vnode->uniquifier, vnode->dataVersion,
         vnode->cloned, (afs_uintmax_t) fileLength, vnode->linkCount,
         vnode->parent);
     if (DumpInodeNumber)
@@ -896,12 +869,11 @@ PrintVnode(int offset, VnodeDiskObject * vnode, VnodeId vnodeNumber,
 #if defined(AFS_NAMEI_ENV)
     if (PrintFileNames) {
        IH_INIT(ihtmpp, V_device(vp), V_parentId(vp), ino);
-#if !defined(AFS_NT40_ENV)
        namei_HandleToName(&filename, ihtmpp);
+#if !defined(AFS_NT40_ENV)
        printf(" UFS-Filename: %s", filename.n_path);
 #else
-       nt_HandleToName(filename, ihtmpp);
-       printf(" NTFS-Filename: %s", filename);
+       printf(" NTFS-Filename: %s", filename.n_path);
 #endif
     }
 #endif