b83433994ffcdfcecea66e3d68fc38f1949cab7a
[openafs.git] / src / vol / test / listVicepx.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11 **      Lists all files in a /vicepX partition. This started as an
12 **      exercise to know the layout of AFS data in a fileserver
13 **      partition. Later on, it proved useful in debugging problems 
14 **      at customer sites too. 
15 **
16 */
17 #include <afsconfig.h>
18 #include <afs/param.h>
19
20
21 #include <rx/xdr.h>
22 #include <afs/afsint.h>
23 #include <ctype.h>
24 #include <sys/param.h>
25 #if !defined(AFS_SGI_ENV)
26 #ifdef  AFS_OSF_ENV
27 #include <ufs/fs.h>
28 #else /* AFS_OSF_ENV */
29 #ifdef AFS_VFSINCL_ENV
30 #define VFS
31 #ifdef  AFS_SUN5_ENV
32 #include <sys/fs/ufs_fs.h>
33 #else
34 #include <ufs/fs.h>
35 #endif
36 #else /* AFS_VFSINCL_ENV */
37 #ifndef AFS_AIX_ENV
38 #include <sys/fs.h>
39 #endif
40 #endif /* AFS_VFSINCL_ENV */
41 #endif /* AFS_OSF_ENV */
42 #endif /* AFS_SGI_ENV */
43 #include <sys/errno.h>
44 #include <sys/stat.h>
45 #include <stdio.h>
46 #include <sys/file.h>
47 #if defined(AFS_SGI_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_BSD_ENV))
48 #include <dirent.h>
49 #else
50 #include <sys/dir.h>
51 #endif
52 #ifdef  AFS_AIX_ENV
53 #include <sys/vfs.h>
54 #include <fcntl.h>
55 #else
56 #ifdef  AFS_HPUX_ENV
57 #include <fcntl.h>
58 #include <mntent.h>
59 #else
60 #if     defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
61 #ifdef  AFS_SUN5_ENV
62 #include <sys/mnttab.h>
63 #include <sys/mntent.h>
64 #else
65 #include <mntent.h>
66 #endif
67 #else
68 #if defined(AFS_SGI_ENV)
69 #include <fcntl.h>
70 #include <mntent.h>
71
72 /*
73 #include <sys/fs/efs.h>
74 */
75 #include "efs.h"                /* until 5.1 release */
76
77 #define ROOTINO EFS_ROOTINO
78 #else
79 #include <fstab.h>
80 #endif
81 #endif /* AFS_SGI_ENV */
82 #endif /* AFS_HPUX_ENV */
83 #endif
84 #include <netdb.h>
85 #include <netinet/in.h>
86 #include <sys/wait.h>
87 #include <setjmp.h>
88 #ifndef ITIMER_REAL
89 #include <sys/time.h>
90 #endif /* ITIMER_REAL */
91
92 #include "nfs.h"
93 #include <afs/errors.h>
94 #include "lock.h"
95 #include "lwp.h"
96 #include "vnode.h"
97 #include "volume.h"
98 #include "vldb.h"
99 #include "partition.h"
100 #include "filesignal.h"
101 #include "vutils.h"
102 #include "daemon_com.h"
103 #include "fssync.h"
104 #include <afs/auxinode.h>
105 #include <afs/dir.h>
106 #include <unistd.h>
107
108 #ifdef  AFS_OSF_ENV
109 extern void *calloc(), *realloc();
110 #endif
111 #include "salvage.h"
112 int volumeId;
113 int VolumeChanged;              /* to satisfy library libdir use */
114
115 #include "listVicepx.h"
116 char *orphan_NoVnode = "ORPHANED_NoVnode";
117 char *orphan_NoUnique = "ORPHANED_NoUnique";
118
119 #define allNull         0x00
120 #define allFiles        0x01    /* equivalent to /bin/ls */
121 #define lFlag           0x02    /* equivalent to /bin/ls -l */
122 #define allDirs         0x04    /* equivalent to /bin/ls -ld */
123 #define contentsDInode  0x08    /* list contents of dir inode */
124 #define volInfo         0x10    /* list info from vol header */
125
126 extern DirEnt *lookup();
127 extern char *getFileName(), *getDirName(), *printStack();
128 extern DirEnt *hash[];
129
130 int
131 Usage(name)
132      char *name;
133 {
134     assert(name);
135     printf
136         ("Usage is %s -p <partition name> -v <volume name> [-ls | -lsl | -ld] [-volInfo] [-dir <directory inode>] \n",
137          name);
138     printf("-ls  : lists all files\n");
139     printf("-lsl : lists all files along with its properties\n");
140     printf("-ld :  lists all directories\n");
141     printf("-volInfo : lists volume header \n");
142     printf("-dir <inode number>: prints contents of directory inode\n");
143     exit(1);
144 }
145
146
147 main(argc, argv)
148      int argc;
149      char *argv[];
150 {
151     char fullName[32 + VNAMESIZE + sizeof(VHDREXT) + 4];
152     char partition[32], volume[VNAMESIZE];
153     struct stat statBuf;
154     struct VolumeHeader volumeHeader;
155     int fd, i, sawPart = 0, sawVolume = 0, sawDirContents = 0;
156     char option = allNull;      /* no default options */
157     Inode dirInode;             /* list contents of this dir Inode */
158
159     for (i = 1; i < argc; i++) {
160         if (!strcmp(argv[i], "-p")) {
161             if ((i + 1) >= argc)
162                 Usage(argv[0]);
163             assert(strlen(argv[i + 1]) < 32);
164             strcpy(partition, argv[++i]);
165             sawPart = 1;
166         } else if (!strcmp(argv[i], "-v")) {
167             if ((i + 1) >= argc)
168                 Usage(argv[0]);
169             assert(strlen(argv[i + 1]) < VNAMESIZE);
170             strcpy(volume, argv[++i]);
171             sawVolume = 1;
172         } else if (!strcmp(argv[i], "-dir")) {
173             if ((i + 1) >= argc)
174                 Usage(argv[0]);
175             dirInode = atoi(argv[++i]);
176             option |= contentsDInode;
177             sawDirContents = 1;
178         } else if (!strcmp(argv[i], "-ls"))
179             option |= allFiles;
180         else if (!strcmp(argv[i], "-lsl"))
181             option |= (allFiles | lFlag);
182         else if (!strcmp(argv[i], "-ld"))
183             option |= allDirs;
184         else if (!strcmp(argv[i], "-volInfo"))
185             option |= volInfo;
186         else
187             Usage(argv[0]);
188     }
189     /* check input parameters */
190     if (!sawPart || !sawVolume)
191         Usage(argv[0]);
192
193     /* extract volume id */
194     volumeId = atoi(volume);
195
196     /* construct  unix file name */
197     strcpy(fullName, partition);
198     strcat(fullName, "/V");
199     strcat(fullName, volume);
200     strcat(fullName, VHDREXT);
201
202     /* check to see that volume exists */
203     if (stat(fullName, &statBuf) < 0) {
204         printf("Error in stat(%s) : %d\n", fullName, errno);
205         exit(2);
206     }
207
208     /* read volume header */
209     if ((fd = open(fullName, O_RDONLY)) < 0) {
210         printf("Error in open(%s) : %d\n", fullName, errno);
211         exit(3);
212     }
213     if (read(fd, &volumeHeader, sizeof(struct VolumeHeader)) <
214         sizeof(struct VolumeHeader)) {
215         printf("Error in reading Volume Header : %d\n", errno);
216         exit(4);
217     }
218
219     switch (option) {
220     case volInfo:               /* volume header info */
221         printf
222             ("VolId:%d VolInfo:%d mag:%x vers:%d smallVnodeIndex:%d largeVnodeIndex:%d VoAcl:%d volMntTab:%d\n",
223              volumeHeader.id, volumeHeader.volumeInfo,
224              volumeHeader.stamp.magic, volumeHeader.stamp.version,
225              volumeHeader.smallVnodeIndex, volumeHeader.largeVnodeIndex,
226              volumeHeader.volumeAcl, volumeHeader.volumeMountTable);
227         break;
228
229     case contentsDInode:        /* list directory entries */
230         printContentsOfDirInode(statBuf.st_dev, dirInode, fullName, option);
231         break;
232     }
233
234     scanLargeVnode(statBuf.st_dev, volumeHeader.largeVnodeIndex, fullName,
235                    option);
236     if (option & allDirs)
237         printDirs(fullName);
238
239     if (option & allFiles)
240         scanSmallVnode(statBuf.st_dev, volumeHeader.smallVnodeIndex, fullName,
241                        option);
242     close(fd);
243 }
244
245 int
246 scanLargeVnode(dev, node, partitionName, option)
247      dev_t dev;
248      Inode node;
249      char *partitionName;
250      char option;               /* user options */
251 {
252     afs_int32 diskSize = SIZEOF_LARGEDISKVNODE;
253     int nVnodes, fdi, vnodeIndex, offset = 0;
254     char buf[SIZEOF_LARGEDISKVNODE];
255     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
256     FILE *file;
257     struct stat statBuf;
258
259     /* open this largeVodeIndex */
260     if ((fdi = iopen(dev, node, O_RDONLY)) < 0) {
261         printf("Error in reading node : %d\n", errno);
262         exit(5);
263     }
264
265     /* get a FILE pointer */
266     if ((file = fdopen(fdi, "r")) == 0) {
267         printf("fdopen failed : %d\n", errno);
268         exit(6);
269     }
270
271     /*find out how many directories are there in this volume */
272     if (fstat(fdi, &statBuf) < 0) {
273         printf("Error in stat(fd=%d): %d\n", fdi, errno);
274         exit(6);
275     }
276     nVnodes = (statBuf.st_size / diskSize) - 1;
277     if (nVnodes > 0)
278         fseek(file, diskSize, 0);
279     else
280         nVnodes = 0;
281
282     /* scan all entries in this volume */
283     DInit(10);                  /* initialise directory buffer */
284
285     for (vnodeIndex = 0; nVnodes && fread(vnode, diskSize, 1, file) == 1;
286          nVnodes--, vnodeIndex++, offset += diskSize) {
287         /* scan this directory */
288         int createDirEnt();
289         if ((vnode->type == vDirectory) && (vnode->inodeNumber)) {
290             DirHandle dir;
291             DirEnt *dirEntry;
292
293             dir.volume = volumeId;
294             dir.device = dev;
295             dir.cacheCheck = 0;
296             dir.inode = vnode->inodeNumber;
297 #ifdef  DEBUG
298             printf
299                 ("Directory inode %d (parent vnode = %d) contains the entries :\n",
300                  vnode->inodeNumber, vnode->parent);
301 #endif
302
303             assert(dirEntry = (DirEnt *) malloc(sizeof(DirEnt)));
304             dirEntry->inode = vnode->inodeNumber;
305             dirEntry->numEntries = 0;
306             dirEntry->vnodeName = NULL;
307             EnumerateDir(&dir, &createDirEnt, dirEntry);
308             insertHash(dirEntry);       /* insert in hash table */
309         }
310     }
311     fclose(file);
312 #ifdef DEBUG
313     printHash();
314 #endif
315 }
316
317
318
319 int
320 createDirEnt(dirEntry, fileName, vnode, unique)
321      DirEnt *dirEntry;
322      char *fileName;
323      afs_int32 vnode;
324      afs_int32 unique;
325 {
326     int fdi;
327     FILE *file;
328     struct stat statBuf;
329
330     /* fil up special fields for itself and parent */
331     if (strcmp(fileName, ".") == 0) {
332         dirEntry->vnode = vnode;
333         return;
334     }
335     if (strcmp(fileName, "..") == 0) {
336         dirEntry->vnodeParent = vnode;
337         return;
338     }
339
340     (dirEntry->numEntries)++;
341     assert(dirEntry->vnodeName =
342            (VnodeName *) realloc(dirEntry->vnodeName,
343                                  dirEntry->numEntries * sizeof(VnodeName)));
344     dirEntry->vnodeName[dirEntry->numEntries - 1].vnode = vnode;
345     dirEntry->vnodeName[dirEntry->numEntries - 1].vunique = unique;
346     dirEntry->vnodeName[dirEntry->numEntries - 1].name =
347         (char *)malloc(strlen(fileName) + 1);
348     assert(dirEntry->vnodeName[dirEntry->numEntries - 1].name);
349     strcpy(dirEntry->vnodeName[dirEntry->numEntries - 1].name, fileName);
350 }
351
352
353 int
354 scanSmallVnode(dev, node, partitionName, option)
355      dev_t dev;
356      Inode node;
357      char *partitionName;
358      char option;               /* user options */
359 {
360     afs_int32 diskSize = SIZEOF_SMALLDISKVNODE;
361     int nVnodes, fdi, vnodeIndex, offset = 0;
362     char buf[SIZEOF_LARGEDISKVNODE];
363     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
364     FILE *file;
365     struct stat statBuf;
366
367     /* open this smallVodeIndex */
368     if ((fdi = iopen(dev, node, O_RDONLY)) < 0) {
369         printf("Error in reading node : %d\n", errno);
370         exit(5);
371     }
372
373     /* get a FILE pointer */
374     if ((file = fdopen(fdi, "r")) == 0) {
375         printf("fdopen failed : %d\n", errno);
376         exit(6);
377     }
378
379     /*find out how many files are there in this volume */
380     if (fstat(fdi, &statBuf) < 0) {
381         printf("Error in stat(fd=%d): %d\n", fdi, errno);
382         exit(6);
383     }
384     nVnodes = (statBuf.st_size / diskSize) - 1;
385     if (nVnodes > 0)
386         fseek(file, diskSize, 0);
387     else
388         nVnodes = 0;
389
390     /* scan all entries in this volume */
391     for (vnodeIndex = 0; nVnodes && fread(vnode, diskSize, 1, file) == 1;
392          nVnodes--, vnodeIndex++, offset += 1)
393         if ((vnode->type == vFile) || (vnode->type == vSymlink)) {
394             char *name, *fullPathName;
395             int pNode, nNode, orphan = 0;
396             DirEnt *dir;
397 #ifdef FILE_DEBUG
398             printf(" File Inode = %d parent vnode=%d ", vnode->inodeNumber,
399                    vnode->parent);
400 #endif
401
402             if ((dir = lookup(vnode->parent)) == 0)     /* orphaned */
403                 orphan = 1, pushStack(orphan_NoVnode);
404             if (!orphan) {
405                 name = getFileName(dir, vnode->uniquifier);
406                 if (name == 0)
407                     orphan = 1, pushStack(orphan_NoUnique);
408             }
409             if (!orphan) {
410                 /* push the file name on stack */
411                 pushStack(name);
412                 pNode = vnode->parent;
413                 nNode = dir->vnodeParent;
414             }
415             while (!orphan && (pNode != nNode)) {
416                 if ((dir = lookup(nNode)) == 0) {
417                     orphan = 1, pushStack(orphan_NoVnode);
418                     break;
419                 }
420                 if ((name = getDirName(dir, pNode)) == 0) {
421                     orphan = 1, pushStack(orphan_NoVnode);
422                     break;
423                 }
424                 pushStack(name);
425                 pNode = nNode;
426                 nNode = dir->vnodeParent;
427             }
428             fullPathName = printStack();        /* full name of file or symLink */
429             if (vnode->type == vSymlink) {      /* check if mount point */
430                 /* read contents of link */
431                 struct stat statLink;
432                 int fdLink;
433                 char *symLink;
434                 if ((fdLink = iopen(dev, vnode->inodeNumber, O_RDONLY)) < 0) {
435                     printf("Error in opening symbolic link : %d\n", errno);
436                     exit(10);
437                 }
438                 if (fstat(fdLink, &statLink) < 0) {
439                     printf("Error in symLink stat(fd=%d): %d\n", fdLink,
440                            errno);
441                     exit(12);
442                 }
443                 assert(symLink = (char *)malloc(statLink.st_size + 1));
444                 if (read(fdLink, symLink, statLink.st_size) < 0) {
445                     printf("Error in reading symbolic link : %d\n", errno);
446                     exit(11);
447                 }
448                 symLink[statLink.st_size] = 0;  /* null termination */
449                 if (symLink[0] == '#')  /* this is a mount point */
450                     printf("Volume %s mounted on %s%s\n", symLink,
451                            partitionName, fullPathName);
452                 free(symLink);
453                 close(fdLink);
454             }
455             if (option & allFiles) {
456                 if (option & lFlag) {
457                     switch (vnode->type) {
458                     case vFile:
459                         printf("F ");
460                         break;
461                     case vDirectory:
462                         printf("D ");
463                         break;
464                     case vSymlink:
465                         printf("S ");
466                         break;
467                     default:
468                         printf("U ");
469                         break;
470                     }
471                     printf("Ind:%d ", vnode->inodeNumber);
472                     printf("Mod:%x ", vnode->modeBits);
473                     printf("Lnk:%d ", vnode->linkCount);
474                     printf("Own:%d ", vnode->owner);
475                     printf("Grp:%d ", vnode->group);
476                     printf("Siz:%d ", vnode->length);
477                 }
478                 printf("~%s\n", fullPathName);
479             }
480         }
481
482     fclose(file);
483 }
484
485 /* Lists all directories in the volume */
486 printDirs(partitionName)
487      char *partitionName;
488 {
489     int i, j, vnode, inode;
490     DirEnt *ptr, *dir, *tmpDir;
491
492     /* The root level vnode for this volume */
493     tmpDir = lookup(1);         /* root vnode is 1 */
494     if (tmpDir == 0)
495         printf("Root vnode(1) does not exists :%s\n", partitionName);
496     else
497         printf("D Ind:%d Vnd:1 ~\n", tmpDir->inode);
498
499     for (i = 0; i < MAX_HASH_SIZE; i++)
500         for (ptr = (DirEnt *) hash[i]; ptr; ptr = ptr->next)
501             for (j = 0; j < ptr->numEntries; j++) {
502                 int nVnode, pVnode;
503                 char *fullPathName, *name;
504
505                 pVnode = ptr->vnodeParent;      /* parent vnode */
506                 nVnode = ptr->vnode;    /* this dir vnode */
507                 vnode = ptr->vnodeName[j].vnode;        /* my Vnode */
508
509                 /* directory vnode numbers are odd */
510                 if ((vnode % 2) == 0)
511                     continue;
512
513                 tmpDir = lookup(vnode);
514                 if (!tmpDir) {  /* orphaned directory */
515                     printf("%s : vnode:%d \n", orphan_NoVnode, vnode);
516                     continue;
517                 }
518                 inode = tmpDir->inode;  /* the inode for this vnode */
519
520                 pushStack(ptr->vnodeName[j].name);
521
522                 while (pVnode != 1) {
523                     dir = lookup(pVnode);
524                     if (dir == 0) {     /* orphan */
525                         pushStack(orphan_NoVnode);
526                         break;
527                     }
528                     name = getDirName(dir, nVnode);
529                     if (name == 0) {
530                         pushStack(orphan_NoVnode);
531                         break;
532                     }
533                     pushStack(name);
534                     nVnode = pVnode;
535                     pVnode = dir->vnodeParent;
536                 }
537                 fullPathName = printStack();    /* full path name of directory */
538                 printf("D Ind:%d Vnd:%d ~%s\n", inode, vnode, fullPathName);
539             }
540 }
541
542 /* figure out how many pages in use in a directory, given ptr to its (locked) he
543 ader */
544 static
545 ComputeUsedPages(dhp)
546      struct DirHeader *dhp;
547 {
548     afs_int32 usedPages, i;
549
550     if (dhp->header.pgcount != 0) {
551         /* new style */
552         usedPages = ntohs(dhp->header.pgcount);
553     } else {
554         /* old style */
555         usedPages = 0;
556         for (i = 0; i < MAXPAGES; i++) {
557             if (dhp->alloMap[i] == EPP) {
558                 usedPages = i;
559                 break;
560             }
561         }
562         if (usedPages == 0)
563             usedPages = MAXPAGES;
564     }
565     return usedPages;
566 }
567
568 printContentsOfDirInode(device, dirInode, fullName, options)
569      dev_t device;
570      Inode dirInode;
571      char *fullName;
572      char options;
573 {
574     int fd, i, j, usedPages, pages;
575     FILE *file;
576     struct stat statBuf;
577     char dirPage[2048];
578     struct DirHeader *dhp = (struct DirHeader *)&dirPage[0];
579     struct DirEntry *de;
580     struct PageHeader *pg;
581
582     fd = iopen(device, dirInode, O_RDONLY);
583     if (fd <= 0) {
584         printf("Cannot open direcory inode %d\n", dirInode);
585         return -1;
586     }
587     if ((file = fdopen(fd, "r")) == 0) {        /* for buffered read */
588         printf("fdopen failed : %d\n", errno);
589         close(fd);
590         return -1;
591     }
592     if (fstat(fd, &statBuf) < 0) {
593         printf("Error in stat(fd=%d): %d\n", fd, errno);
594         return -1;
595     }
596     /* read first page */
597     if (fread(&dirPage, sizeof(dirPage), 1, file) != 1) {
598         printf("Cannot read dir header from inode %d(errno %d)\n", dirInode,
599                errno);
600         fclose(file);
601         return -1;
602     }
603     usedPages = ComputeUsedPages(dhp);
604
605     printf("Alloc map: ");
606     for (i = 0; i < MAXPAGES; i++) {
607         if ((i % 16) == 0)
608             printf("\n");
609         printf("%.2x ", (unsigned char)dhp->alloMap[i]);
610     }
611     printf("\nHash table:");
612     for (i = 0; i < NHASHENT; i++) {
613         if ((i % 16) == 0)
614             printf("\n");
615         printf("%.2d ", dhp->hashTable[i]);
616     }
617     printf("\n");
618
619     /* print header of first page */
620     printf("--------------- Page 0 ---------------\n");
621     printf("pgcnt      :%d\n", usedPages);
622     printf("tag        :%d\n", dhp->header.tag);
623     printf("freecnt    :%d(not used)\n", dhp->header.freecount);
624     printf("freebitmap :");
625     for (i = 0; i < EPP / 8; i++)
626         printf("%.2x ", (unsigned char)(dhp->header.freebitmap[i]));
627     printf("\n");
628
629     /* print slots in the first page of this directory */
630     de = ((struct DirPage0 *)dirPage)->entry;
631     for (i = DHE + 1; i < EPP; i++, de = (struct DirEntry *)((char *)de + 32))
632         printf("ent %d: f=%d l=%d n=%d vn=%d vu=%d name:%s\n", i, de->flag,
633                de->length, de->next, de->fid.vnode, de->fid.vunique,
634                de->name);
635
636     /* read all succeeding pages of this directory */
637     for (pages = 1; pages < usedPages; pages++) {
638         if (fread(&dirPage, sizeof(dirPage), 1, file) != 1) {
639             printf("Cannot read %s page  from inode %d(errno %d)\n", pages,
640                    dirInode, errno);
641             fclose(file);
642             return -1;
643         }
644         pg = &((struct DirPage1 *)dirPage)->header;     /* page header */
645         de = ((struct DirPage1 *)dirPage)->entry;
646
647         /* print page header info */
648         printf("--------------- Page %d ---------------\n", pages);
649         printf("pgcnt      :%d\n", pg->pgcount);
650         printf("tag        :%d\n", pg->tag);
651         printf("freecnt    :%d(not used)\n", pg->freecount);
652         printf("freebitmap :");
653         for (i = 0; i < EPP / 8; i++)
654             printf("%.2x ", (unsigned char)(pg->freebitmap[i]));
655         printf("\n");
656
657         /* print slots in this page */
658         for (i = 1; i < EPP; i++, de = (struct DirEntry *)((char *)de + 32))
659             printf("ent %d: f=%d l=%d n=%d vn=%d vu=%d name:%s\n", i,
660                    de->flag, de->length, de->next, de->fid.vnode,
661                    de->fid.vunique, de->name);
662     }
663
664     fclose(file);
665     return 0;
666 }