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
14 Institution: The Information Technology Center, Carnegie-Mellon University
18 #define ITC /* Required by inode.h */
20 #include <afsconfig.h>
21 #include <afs/param.h>
27 #if defined(AFS_LINUX20_ENV) || defined(AFS_SUN4_ENV)
32 * -1 - Unable to read the inodes.
33 * -2 - Unable to completely write temp file. Produces warning message in log.
36 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
37 int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
38 char *wpath, void *rock)
40 Log("ListViceInodes not implemented for this platform!\n");
45 #include <sys/param.h>
46 #if defined(AFS_SGI_ENV)
50 #else /* AFS_OSF_ENV */
51 #ifdef AFS_VFSINCL_ENV
54 #include <sys/fs/ufs_fs.h>
56 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
57 #include <ufs/ufs/dinode.h>
58 #include <ufs/ffs/fs.h>
59 #define itod ino_to_fsba
64 #else /* AFS_VFSINCL_ENV */
66 #include <sys/filsys.h>
70 #endif /* AFS_VFSINCL_ENV */
71 #endif /* AFS_OSF_ENV */
73 #ifdef AFS_VFSINCL_ENV
74 #include <sys/vnode.h>
76 #include <sys/fs/ufs_inode.h>
78 #if !defined(AFS_DARWIN_ENV)
79 #include <ufs/inode.h>
82 #else /* AFS_VFSINCL_ENV */
84 #include <ufs/inode.h>
85 #else /* AFS_OSF_ENV */
86 #include <sys/inode.h>
88 #endif /* AFS_VFSINCL_ENV */
89 #endif /* AFS_SGI_ENV */
90 #include <afs/osi_inode.h>
94 #include <afs/afsint.h>
96 #include <afs/afssyscalls.h>
97 #include "viceinode.h"
99 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV)
102 #ifdef AFS_PTHREAD_ENV
104 #else /* AFS_PTHREAD_ENV */
105 #include <afs/assert.h>
106 #endif /* AFS_PTHREAD_ENV */
107 #if defined(AFS_HPUX101_ENV)
110 #include "partition.h"
112 /*@+fcnmacros +macrofcndecl@*/
115 extern off64_t afs_lseek(int FD, off64_t O, int F);
116 #endif /*S_SPLINT_S */
117 #define afs_lseek(FD, O, F) lseek64(FD, (off64_t) (O), F)
118 #define afs_stat stat64
119 #define afs_fstat fstat64
120 #define afs_open open64
121 #define afs_fopen fopen64
122 #else /* !O_LARGEFILE */
124 extern off_t afs_lseek(int FD, off_t O, int F);
125 #endif /*S_SPLINT_S */
126 #define afs_lseek(FD, O, F) lseek(FD, (off_t) (O), F)
127 #define afs_stat stat
128 #define afs_fstat fstat
129 #define afs_open open
130 #define afs_fopen fopen
131 #endif /* !O_LARGEFILE */
132 /*@=fcnmacros =macrofcndecl@*/
134 /* Notice: parts of this module have been cribbed from vfsck.c */
137 static char *partition;
142 #include <jfs/filsys.h>
145 #define FSBSIZE (4096) /* filesystem block size */
146 #define FSBSHIFT (12) /* log2(FSBSIZE) */
147 #define FSBMASK (FSBSIZE - 1) /* FSBSIZE mask */
149 #define MIN_FSIZE DISKMAP_B /* minimum fs size (FSblocks) */
150 #define LAST_RSVD_I 15 /* last reserved inode */
155 * This will hopefully eventually make it into the system include files
157 #define INOPB (FSBSIZE / sizeof (struct dinode))
165 #endif /* AFS_AIX41_ENV */
168 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
169 XX This was lifted from some `com/cmd/fs/fshlpr_aix3/Fs.h', which indicated X
170 XX a longing to see it make it into a readily accessible include file. XXXXXX
171 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
173 * itoo - inode number to offset within disk block
176 #define itoo(x) (int) ((unsigned)(x) % INOPB)
178 int Bsize = FSBSIZE; /* block size for this system */
179 daddr_t fmax; /* total number of blocks n file system */
180 ino_t imax, inum; /* total number of I-nodes in file system */
182 static struct superblock fs;
183 struct dinode *ginode();
187 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
188 int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
189 char *wpath, void *rock)
191 FILE *inodeFile = NULL;
192 char dev[50], rdev[51];
195 struct ViceInodeInfo info;
196 struct stat root_inode;
197 int ninodes = 0, err = 0;
199 pfd = -1; /* initialize so we don't close on error output below. */
202 sleep(1); /* simulate operator */
208 partition = mountedOn;
209 sprintf(dev, "/dev/%s", devname);
210 sprintf(rdev, "/dev/r%s", devname);
212 if (stat(mountedOn, &root_inode) < 0) {
213 Log("cannot stat: %s\n", mountedOn);
217 if (root_inode.st_ino != ROOTDIR_I) {
218 Log("%s is not root of a filesystem\n", mountedOn);
224 * done with the superblock, now try to read the raw device.
226 if (ReadSuper(&fs, dev) < 0)
231 case FM_CLEAN: /* clean and unmounted */
232 Log("Most peculiar - Super blk in FM_CLEAN state!\n");
234 case FM_MOUNT: /* mounted cleanly */
237 case FM_MDIRTY: /* dirty when mounted or commit fail */
238 case FM_LOGREDO: /* log redo attempted but failed */
239 Log("File system %s is in a bad state.\n", rdev);
240 Log("Call your IBM representative.\n");
244 if (IsBigFilesFileSystem(&fs, (char *)0)) {
245 Log("%s is a big files filesystem, can't salvage.\n", mountedOn);
249 if (strncmp(fs.s_magic, fsv3magic, strlen(fsv3magic)) != 0) {
251 if ((strncmp(fs.s_magic, fsv3pmagic, strlen(fsv3pmagic)) != 0)
252 || (fs.s_version != fsv3pvers)) {
253 Log("Super block doesn't have the problem magic (%s vs v3magic %s v3pmagic %s)\n", fs.s_magic, fsv3magic, fsv3pmagic);
257 Log("Super block doesn't have the problem magic (%s vs v3magic %s)\n",
258 fs.s_magic, fsv3magic);
265 fragsize = (fs.s_fragsize) ? fs.s_fragsize : FSBSIZE;
266 iagsize = (fs.s_iagsize) ? fs.s_iagsize : fs.s_agsize;
267 ag512 = fragsize * fs.s_agsize / 512;
268 agblocks = fragsize * fs.s_agsize >> BSHIFT;
269 #endif /* AFS_AIX41_ENV */
271 fmax = fs.s_fsize / (FSBSIZE / 512); /* first invalid blk num */
273 pfd = afs_open(rdev, O_RDONLY);
275 Log("Unable to open `%s' inode for reading\n", rdev);
279 inodeFile = fopen(resultFile, "w");
280 if (inodeFile == NULL) {
281 Log("Unable to create inode description file %s\n", resultFile);
286 * calculate the maximum number of inodes possible
289 imax = iagsize * (fs.s_fsize / ag512) - 1;
290 #else /* AFS_AIX41_ENV */
292 ((fmax / fs.s_agsize +
293 ((fmax % fs.s_agsize) >= fs.s_agsize / INOPB ? 1 : 0))
295 #endif /* AFS_AIX41_ENV */
298 * check for "FORCESALVAGE" equivalent:
299 * LAST_RSVD_I is a vice inode, with dead beef, and
300 * di_nlink == 2 to indicate the FORCE.
302 assert(p = ginode(LAST_RSVD_I));
304 if (p->di_vicemagic == VICEMAGIC && p->di_vicep1 == 0xdeadbeef
305 && p->di_nlink == 2) {
307 idec(root_inode.st_dev, LAST_RSVD_I, 0xdeadbeef);
310 for (inum = LAST_RSVD_I + 1; inum <= imax; ++inum) {
311 if ((p = ginode(inum)) == NULL || p->di_vicemagic != VICEMAGIC
312 || (p->di_mode & IFMT) != IFREG)
315 info.inodeNumber = inum;
316 info.byteCount = p->di_size;
317 info.linkCount = p->di_nlink;
318 info.u.param[0] = p->di_vicep1;
319 info.u.param[1] = p->di_vicep2;
320 info.u.param[2] = p->di_vicep3;
321 info.u.param[3] = p->di_vicep4;
323 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
326 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
327 Log("Error writing inode file for partition %s\n", partition);
333 if (fflush(inodeFile) == EOF) {
334 Log("Unable to successfully flush inode file for %s\n", partition);
338 if (fsync(fileno(inodeFile)) == -1) {
339 Log("Unable to successfully fsync inode file for %s\n", partition);
343 if (fclose(inodeFile) == EOF) {
344 Log("Unable to successfully close inode file for %s\n", partition);
350 * Paranoia: check that the file is really the right size
352 if (stat(resultFile, &status) == -1) {
353 Log("Unable to successfully stat inode file for %s\n", partition);
357 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
358 Log("Wrong size (%d instead of %d) in inode file for %s\n",
359 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
378 /* Read in the superblock for devName */
380 ReadSuper(struct superblock *fs, char *devName)
384 pfd = afs_open(devName, O_RDONLY);
386 Log("Unable to open inode on %s for reading superblock.\n", devName);
390 if (bread(pfd, fs, SUPER_B, sizeof(struct superblock)) < 0) {
391 Log("Unable to read superblock on %s.\n", devName);
399 /* IsBigFilesFileSystem returns 1 if it's a big files filesystem, 0 otherwise. */
401 IsBigFilesFileSystem(struct superblock *sb)
403 if ((strncmp(sb->s_magic, fsv3pmagic, 4) == 0)
404 && (sb->s_version == fsbigfile)
418 static char buf[FSBSIZE];
419 static daddr_t last_blk = -1;
425 0) ? INODES_B + inum / INOPB : ag * agblocks + (inum -
428 #else /* AFS_AIX41_ENV */
429 ag = inum / fs.s_agsize;
432 0) ? INODES_B + inum / INOPB : ag * fs.s_agsize + (inum -
436 #endif /* AFS_AIX41_ENV */
438 if (last_blk != pblk) {
439 if (bread(pfd, buf, pblk, sizeof(buf)) < 0) {
446 dp = (struct dinode *)buf;
451 #else /* !AFS_AIX31_ENV */
453 #if defined(AFS_SGI_ENV)
455 /* libefs.h includes <assert.h>, which we don't want */
458 #ifdef AFS_SGI_XFS_IOPS_ENV
460 #include <afs/xfsattrs.h>
461 /* xfs_ListViceInodes
463 * xfs_ListViceInodes verifies and correct the XFS namespace as it collects
464 * the inode information. The name is required for the idec operation to work.
465 * Steps 2 and 3 below are for the AFS_XFS_NAME_VERS == 1. If the name space
466 * changes, the algorithm will need to change.
467 * 1) If the parent inode number does not match the directory's inod number,
468 * change it in the attribute.
469 * 2) If the unqifier in the attribute does not match the name, rename the
470 * file. This is done by doing an exclusive open, incrementing the tag
471 * number until a file can be created. If the tag changes, then the
472 * attribute will need updating.
473 * 3) If the tag in the attribute does not match the name, change the
475 * 4) Verify uid = RW volume id and gid = XFS_VICEMAGIC.
481 * Does the verifications listed above.
482 * We can't change the names until the readdir is complete, so we set the
483 * rename flag if the file needs renaming.
486 xfs_VerifyInode(char *dir, uint64_t pino, char *name, i_list_inode_t * info,
493 int update_chown = 0;
500 (void)sprintf(path, "%s/%s", dir, name);
501 /* Verify uid and gid fields */
502 if (info->ili_magic != XFS_VICEMAGIC) {
503 Log("%s magic for %s/%s (inode %s) from %d to %d\n",
504 Testing ? "Would have changed" : "Changing", dir, name,
505 PrintInode(NULL, info->ili_info.inodeNumber), info->ili_magic,
511 vno = info->ili_info.param[0];
512 if (info->ili_vno != AFS_XFS_VNO_CLIP(vno)) {
513 Log("%s volume id for %s/%s (inode %s) from %d to %d\n",
514 Testing ? "Would have changed" : "Changing", dir, name,
515 PrintInode(NULL, info->ili_info.inodeNumber), info->ili_vno,
516 AFS_XFS_VNO_CLIP(vno));
522 if (chown(path, AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC) < 0) {
523 Log("Can't chown %s to uid=%d, gid=0x%x\n", path,
524 AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC);
529 /* Check Parent inode number. */
530 if (info->ili_pino != pino) {
531 afs_ino_str_t sino, sipino, spino;
532 (void)PrintInode(sino, info->ili_info.inodeNumber);
533 (void)PrintInode(sipino, info->ili_pino);
534 (void)PrintInode(spino, pino);
535 Log("%s parent ino for %s (inode %s) from %s to %s.\n",
536 Testing ? "Would have changed" : "Changing", path, sino, sipino,
542 /* Verify the file name. */
543 (void)strcpy(tmpName, ".");
544 (void)strcat(tmpName, int_to_base64(stmp, info->ili_info.param[2]));
545 if (strncmp(name, tmpName, strlen(tmpName))) {
546 Log("%s name %s (inode %s) in directory %s, unique=%d, tag=%d\n",
547 Testing ? "Would have returned bad" : "Bad", name,
548 PrintInode(NULL, info->ili_info.inodeNumber), dir,
549 info->ili_info.param[2], info->ili_tag);
555 /* update the tag? */
556 (void)strcat(tmpName, ".");
557 (void)strcat(tmpName, int_to_base64(stmp, info->ili_tag));
558 if (strcmp(name, tmpName)) {
560 (void)strcpy(tmpName, name);
561 p = strchr(tmpName + 1, '.');
563 Log("No tag found on name %s (inode %s)in directory, %s.\n",
564 name, PrintInode(NULL, info->ili_info.inodeNumber), dir,
565 Testing ? "would have renamed" : "will rename");
569 tag = base64_to_int(p + 1);
570 Log("%s the tag for %s (inode %s) from %d to %d.\n",
571 Testing ? "Would have changed" : "Will change", path,
572 PrintInode(NULL, info->ili_info.inodeNumber), dir, tag,
580 if (update_pino || update_tag) {
581 afs_xfs_attr_t attrs;
584 length = SIZEOF_XFS_ATTR_T;
585 if (attr_get(path, AFS_XFS_ATTR, (char *)&attrs, &length, ATTR_ROOT) <
587 Log("Can't get AFS attribute for %s\n", path);
591 attrs.at_pino = pino;
595 (path, AFS_XFS_ATTR, (char *)&attrs, length,
596 ATTR_ROOT | ATTR_REPLACE) < 0) {
597 Log("Can't set AFS attribute into %s\n", path);
611 xfs_RenameFiles(char *dir, xfs_Rename_t * renames, int n_renames)
614 char opath[128], nbase[128], npath[128];
615 afs_xfs_attr_t attrs;
616 int length = SIZEOF_XFS_ATTR_T;
621 for (i = 0; i < n_renames; i++) {
622 (void)sprintf(opath, "%s/%s", dir, renames[i].name);
623 (void)sprintf(nbase, "%s/.%s", dir,
624 int_to_base64(stmp, renames[i].uniq));
625 for (tag = 2, j = 0; j < 64; tag++, j++) {
626 (void)sprintf(npath, "%s.%s", nbase, int_to_base64(stmp, tag));
627 fd = afs_open(npath, O_CREAT | O_EXCL | O_RDWR, 0);
634 Log("Can't find a new name for %s\n", opath);
637 if (rename(opath, npath) < 0) {
638 Log("Can't rename %s to %s\n", opath, npath);
641 Log("Renamed %s to %s\n", opath, npath);
648 xfs_ListViceInodes(char *devname, char *mountedOn, char *resultFile,
649 int (*judgeInode) (), int judgeParam, int *forcep,
650 int forceR, char *wpath, void *rock)
652 FILE *inodeFile = NULL;
654 int info_size = sizeof(i_list_inode_t);
657 dirent64_t *top_direntp;
659 dirent64_t *vol_direntp;
660 struct stat64 sdirbuf;
661 struct stat64 sfilebuf;
662 afs_xfs_attr_t attrs;
663 afs_xfs_dattr_t dattrs;
665 char vol_dirname[1024];
668 xfs_Rename_t *renames = (xfs_Rename_t *) 0;
670 #define N_RENAME_STEP 64
679 if (stat64(mountedOn, &sdirbuf) < 0) {
680 perror("xfs_ListViceInodes: stat64");
684 inodeFile = fopen(resultFile, "w");
685 if (inodeFile == NULL) {
686 Log("Unable to create inode description file %s\n", resultFile);
690 if ((top_dirp = opendir(mountedOn)) == NULL) {
691 Log("Can't open directory %s to read inodes.\n", mountedOn);
695 while (top_direntp = readdir64(top_dirp)) {
696 /* Only descend directories with the AFSDIR attribute set.
697 * Could also verify the contents of the atribute, but currently
699 * Performance could be improved for single volume salvages by
700 * only going through the directory containing the volume's inodes.
701 * But I'm being complete as a first pass.
703 (void)sprintf(vol_dirname, "%s/%s", mountedOn, top_direntp->d_name);
704 length = SIZEOF_XFS_DATTR_T;
706 (vol_dirname, AFS_XFS_DATTR, (char *)&dattrs, &length, ATTR_ROOT))
709 if ((vol_dirp = opendir(vol_dirname)) == NULL) {
710 if (errno == ENOTDIR)
712 Log("Can't open directory %s to read inodes.\n", vol_dirname);
716 pino = top_direntp->d_ino;
718 while (vol_direntp = readdir64(vol_dirp)) {
719 if (vol_direntp->d_name[1] == '\0'
720 || vol_direntp->d_name[1] == '.')
723 info.ili_version = AFS_XFS_ILI_VERSION;
724 info_size = sizeof(i_list_inode_t);
726 ilistinode64(sdirbuf.st_dev, vol_direntp->d_ino, &info,
729 /* Where possible, give more explicit messages. */
733 Log("%s (device id %d) is not on an XFS filesystem.\n",
734 vol_dirname, sdirbuf.st_dev);
739 if (info_size != sizeof(i_list_inode_t)
740 || info.ili_version != AFS_XFS_ILI_VERSION) {
741 Log("Version skew between kernel and salvager.\n");
746 /* Continue, so we collect all the errors in the first pass. */
747 Log("Error listing inode named %s/%s: %s\n", vol_dirname,
748 vol_direntp->d_name, strerror(errno));
753 if (info.ili_attr_version != AFS_XFS_ATTR_VERS) {
754 Log("Unrecognized XFS attribute version %d in %s/%s. Upgrade salvager\n", info.ili_attr_version, vol_dirname, vol_direntp->d_name);
758 if (judgeInode && (*judgeInode) (&info.ili_info, judgeParam, rock) == 0)
763 (vol_dirname, pino, vol_direntp->d_name, &info,
769 /* Add this name to the list of items to rename. */
770 if (n_renames >= n_avail) {
771 n_avail += N_RENAME_STEP;
772 if (n_avail == N_RENAME_STEP)
773 renames = (xfs_Rename_t *)
774 malloc(n_avail * sizeof(xfs_Rename_t));
776 renames = (xfs_Rename_t *)
777 realloc((char *)renames,
778 n_avail * sizeof(xfs_Rename_t));
780 Log("Can't %salloc %lu bytes for rename list.\n",
781 (n_avail == N_RENAME_STEP) ? "m" : "re",
782 n_avail * sizeof(xfs_Rename_t));
786 (void)strcpy(renames[n_renames].name, vol_direntp->d_name);
787 renames[n_renames].uniq = info.ili_info.param[2];
792 (&info.ili_info, sizeof(vice_inode_info_t), 1, inodeFile)
794 Log("Error writing inode file for partition %s\n", mountedOn);
799 } /* end while vol_direntp */
802 vol_dirp = (DIR *) 0;
804 Log("Renaming files.\n");
805 if (xfs_RenameFiles(vol_dirname, renames, n_renames) < 0) {
813 free((char *)renames);
814 if (fflush(inodeFile) == EOF) {
815 ("Unable to successfully flush inode file for %s\n", mountedOn);
817 return errors ? -1 : -2;
819 if (fsync(fileno(inodeFile)) == -1) {
820 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
822 return errors ? -1 : -2;
824 if (fclose(inodeFile) == EOF) {
825 Log("Unable to successfully close inode file for %s\n", mountedOn);
826 return errors ? -1 : -2;
829 * Paranoia: check that the file is really the right size
831 if (stat(resultFile, &status) == -1) {
832 Log("Unable to successfully stat inode file for %s\n", partition);
833 return errors ? -1 : -2;
835 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
836 Log("Wrong size (%d instead of %d) in inode file for %s\n",
837 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
839 return errors ? -1 : -2;
843 Log("Errors encontered listing inodes, not salvaging partition.\n");
855 free((char *)renames);
864 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
865 int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
866 char *wpath, void *rock)
868 FILE *inodeFile = NULL;
869 char dev[50], rdev[51];
871 struct efs_dinode *p;
872 struct ViceInodeInfo info;
873 struct stat root_inode;
874 int ninodes = 0, err = 0;
875 struct efs_dinode *dinodeBuf = NULL;
877 ino_t imax, inum; /* total number of I-nodes in file system */
881 sleep(1); /* simulate operator */
887 if (stat(mountedOn, &root_inode) < 0) {
888 Log("cannot stat: %s\n", mountedOn);
891 #ifdef AFS_SGI_XFS_IOPS_ENV
892 if (!strcmp("xfs", root_inode.st_fstype)) {
893 return xfs_ListViceInodes(devname, mountedOn, resultFile, judgeInode,
894 judgeParam, forcep, forceR, wpath, rock);
898 Log("%s is not root of a filesystem\n", mountedOn);
903 #else /* AFS_SGI_ENV */
906 #define SPERB (MAXBSIZE / sizeof(short))
907 #define MAXNINDIR (MAXBSIZE / sizeof(daddr_t))
910 struct bufarea *b_next; /* must be first */
914 char b_buf[MAXBSIZE]; /* buffer space */
915 short b_lnks[SPERB]; /* link counts */
916 daddr_t b_indir[MAXNINDIR]; /* indirect block */
917 struct fs b_fs; /* super block */
918 struct cg b_cg; /* cylinder group */
922 typedef struct bufarea BUFAREA;
925 #define sblock sblk.b_un.b_fs
926 #endif /* AFS_HPUX_ENV */
928 extern char *afs_rawname();
930 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
931 int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
932 char *wpath, void *rock)
938 #else /* !AFS_AIX_ENV */
943 int i, c, e, bufsize, code, err = 0;
944 FILE *inodeFile = NULL;
945 char dev[50], rdev[100], err1[512], *ptr1;
946 struct dinode *inodes = NULL, *einodes, *dptr;
950 struct ViceInodeInfo info;
953 partition = mountedOn;
954 sprintf(rdev, "%s/%s", wpath, devname);
955 ptr1 = afs_rawname(rdev);
959 /* Bletch: this is terrible; is there a better way to do this? Does this work? vfsck doesn't even sleep!! */
961 sleep(5); /* Trying a smaller one for aix */
966 pfd = afs_open(rdev, O_RDONLY);
968 sprintf(err1, "Could not open device %s to get inode list\n", rdev);
973 if (bread(pfd, (char *)&super.fs, SUPERB, sizeof super.fs) == -1) {
976 if (bread(pfd, (char *)&sblock, SBLOCK, SBSIZE) == -1) {
978 if (bread(pfd, super.block, SBLOCK, SBSIZE) == -1) {
979 #endif /* AFS_HPUX_ENV */
981 Log("Unable to read superblock, partition %s\n", partition);
985 inodeFile = fopen(resultFile, "w");
986 if (inodeFile == NULL) {
987 Log("Unable to create inode description file %s\n", resultFile);
992 * char *FSlabel(), *fslabel=0;
993 * fslabel = FSlabel(&super.fs);
995 if (super.fs.s_bsize == 0)
996 super.fs.s_bsize = 512;
997 if (super.fs.s_bsize != BSIZE) {
998 Log("SuperBlk: Cluster size not %d; run vfsck\n", BSIZE);
1001 fmax = super.fs.s_fsize; /* first invalid blk num */
1002 imax = ((ino_t) super.fs.s_isize - (SUPERB + 1)) * INOPB;
1004 Log("Size check: imax==0!\n");
1007 if (GetAuxInodeFile(partition, &status) == 0) {
1008 Log("Can't access Aux inode file for partition %s, aborting\n",
1012 for (inum = 1; inum <= imax; inum++) {
1013 struct dauxinode *auxp;
1014 if ((auxp = IsAfsInode(inum)) == NULL) {
1015 /* Not an afs inode, keep going */
1018 if ((p = ginode(inum)) == NULL)
1020 /* deleted/non-existent inode when di_mode == 0 */
1023 info.inodeNumber = (int)inum;
1024 info.byteCount = p->di_size;
1025 info.linkCount = p->di_nlink;
1026 info.u.param[0] = auxp->aux_param1;
1027 info.u.param[1] = auxp->aux_param2;
1028 info.u.param[2] = auxp->aux_param3;
1029 info.u.param[3] = auxp->aux_param4;
1030 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1032 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1033 Log("Error writing inode file for partition %s\n", partition);
1040 * run a few consistency checks of the superblock
1041 * (Cribbed from vfsck)
1044 #if defined(FD_FSMAGIC)
1045 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)
1046 && (sblock.fs_magic != FD_FSMAGIC)
1047 #if defined(AFS_HPUX101_ENV)
1048 && (sblock.fs_magic != FD_FSMAGIC_2)
1052 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)) {
1054 Log("There's something wrong with the superblock for partition %s; bad magic (%d) run vfsck\n", partition, sblock.fs_magic);
1057 if (sblock.fs_ncg < 1) {
1058 Log("There's something wrong with the superblock for partition %s; NCG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_ncg);
1061 if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG) {
1062 Log("There's something wrong with the superblock for partition %s; CPG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_cpg);
1065 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl
1066 || (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) {
1067 Log("There's something wrong with the superblock for partition %s; NCYL LESS THAN NCG*CPG run vfsck\n", partition);
1070 if (sblock.fs_sbsize > SBSIZE) {
1071 Log("There's something wrong with the superblock for partition %s; bsize too large (%d vs. %d) run vfsck\n", partition, sblock.fs_sbsize, sblock.fs_bsize);
1075 if ((super.fs.fs_magic != FS_MAGIC)
1076 || (super.fs.fs_ncg < 1)
1077 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1078 || (super.fs.fs_cpg < 1)
1080 || (super.fs.fs_cpg < 1 || super.fs.fs_cpg > MAXCPG)
1082 || (super.fs.fs_ncg * super.fs.fs_cpg < super.fs.fs_ncyl
1083 || (super.fs.fs_ncg - 1) * super.fs.fs_cpg >= super.fs.fs_ncyl)
1084 || (super.fs.fs_sbsize > SBSIZE)) {
1085 Log("There's something wrong with the superblock for partition %s; run vfsck\n", partition);
1088 #endif /* AFS_HPUX_ENV */
1091 bufsize = sblock.fs_ipg * sizeof(struct dinode);
1093 bufsize = super.fs.fs_ipg * sizeof(struct dinode);
1094 #endif /* AFS_HPUX_ENV */
1095 inodes = (struct dinode *)malloc(bufsize);
1096 einodes = (struct dinode *)(((char *)inodes) + bufsize);
1097 if (inodes == NULL) {
1098 Log("Unable to allocate enough memory to scan inodes; help!\n");
1101 Log("Scanning inodes on device %s...\n", rdev);
1103 for (c = 0; c < sblock.fs_ncg; c++) {
1104 i = c * sblock.fs_ipg;
1105 e = i + sblock.fs_ipg;
1106 #if defined(AFS_HPUX102_ENV)
1107 if (afs_lseek(pfd, dbtoo(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1110 if (afs_lseek(pfd, dbtob(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1114 for (c = 0; c < super.fs.fs_ncg; c++) {
1116 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1118 #if defined(AFS_DARWIN_ENV)
1119 #define offset_t off_t
1120 #define llseek lseek
1123 #endif /* AFS_SUN5_ENV */
1124 i = c * super.fs.fs_ipg;
1125 e = i + super.fs.fs_ipg;
1127 dblk1 = fsbtodb(&super.fs, itod(&super.fs, i));
1128 if (afs_lseek(pfd, (off_t) ((off_t) dblk1 * DEV_BSIZE), L_SET) == -1) {
1130 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1131 f1 = fsbtodb(&super.fs, itod(&super.fs, i));
1132 off = (offset_t) f1 << DEV_BSHIFT;
1133 if (llseek(pfd, off, L_SET) == -1) {
1135 if (afs_lseek(pfd, dbtob(fsbtodb(&super.fs, itod(&super.fs, i))), L_SET)
1137 #endif /* AFS_SUN5_ENV */
1138 #endif /* AFS_OSF_ENV */
1139 #endif /* AFS_HPUX_ENV */
1140 Log("Error reading inodes for partition %s; run vfsck\n",
1146 if (read(pfd, inodes, bufsize) != bufsize) {
1147 Log("Error reading inodes for partition %s; run vfsck\n",
1152 register int bj, bk;
1154 for (bj = bk = 0; bj < bufsize; bj = bj + 512, bk++) {
1155 if ((code = read(pfd, dptr, 512)) != 512) {
1156 Log("Error reading inode %d? for partition %s (errno = %d); run vfsck\n", bk + i, partition, errno);
1157 if (afs_lseek(pfd, 512, L_SET) == -1) {
1158 Log("Lseek failed\n");
1173 for (p = inodes; p < einodes && i < e; i++, p++) {
1175 Log("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1176 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1177 p->di_size, p->di_nlink);
1179 ("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1180 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1181 p->di_size, p->di_nlink);
1184 #ifdef AFS_3DISPARES
1185 /* Check to see if this inode is a pre-"OSF1 4.0D" inode */
1186 if ((p->di_uid || p->di_gid)
1187 && !(p->di_flags & (IC_XUID | IC_XGID))) {
1188 Log("Found unconverted inode %d: Use 'fs_conv_dux40D convert' on partition %s\n", i, partition);
1192 assert(0); /* define AFS_3DISPARES in param.h */
1195 #if defined(AFS_SUN56_ENV)
1196 /* if this is a pre-sol2.6 unconverted inode, bail out */
1198 afs_uint32 p1, p2, p3, p4;
1202 q = (quad *) & (p->di_ic.ic_lsize);
1204 p2 = p->di_ic.ic_flags;
1206 p4 = p->di_ic.ic_uid;
1207 p5 = p->di_ic.ic_gid;
1209 if ((p2 || p3) && !p4 && (p5 == -2)) {
1210 Log("Found unconverted inode %d\n", i);
1211 Log("You should run the AFS file conversion utility\n");
1216 if (IS_DVICEMAGIC(p) && (p->di_mode & IFMT) == IFREG) {
1217 afs_uint32 p2 = p->di_vicep2, p3 = DI_VICEP3(p);
1219 info.u.param[0] = p->di_vicep1;
1220 #ifdef AFS_3DISPARES
1221 if (((p2 >> 3) == INODESPECIAL) && (p2 & 0x3)) {
1222 info.u.param[1] = INODESPECIAL;
1223 info.u.param[2] = p3;
1224 info.u.param[3] = p2 & 0x3;
1226 info.u.param[1] = ((p2 >> 27) << 16) + (p3 & 0xffff);
1227 info.u.param[2] = (p2 & 0x3fffff);
1229 (((p2 >> 22) & 0x1f) << 16) + (p3 >> 16);
1232 info.u.param[1] = p->di_vicep2;
1233 info.u.param[2] = DI_VICEP3(p);
1234 info.u.param[3] = p->di_vicep4;
1236 info.inodeNumber = i;
1237 info.byteCount = p->di_size;
1238 info.linkCount = p->di_nlink;
1239 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1241 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1242 Log("Error writing inode file for partition %s\n",
1254 if (fflush(inodeFile) == EOF) {
1255 Log("Unable to successfully flush inode file for %s\n", partition);
1259 if (fsync(fileno(inodeFile)) == -1) {
1260 Log("Unable to successfully fsync inode file for %s\n", partition);
1264 if (fclose(inodeFile) == EOF) {
1265 Log("Unable to successfully close inode file for %s\n", partition);
1271 * Paranoia: check that the file is really the right size
1273 if (stat(resultFile, &status) == -1) {
1274 Log("Unable to successfully stat inode file for %s\n", partition);
1278 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1279 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1280 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1298 #endif /* !AFS_SGI_ENV */
1299 #endif /* !AFS_AIX31_ENV */
1301 #ifdef AFS_DARWIN_ENV
1303 #define dbtob(db) ((unsigned)(db) << DEV_BSHIFT)
1307 bread(int fd, char *buf, daddr_t blk, afs_int32 size)
1310 #ifdef AFS_AIX41_ENV
1311 offset_t off = (offset_t) blk << FSBSHIFT;
1312 if (llseek(fd, off, 0) < 0) {
1313 Log("Unable to seek to offset %llu for block %u\n", off, blk);
1316 #else /* AFS_AIX41_ENV */
1317 if (afs_lseek(fd, blk * Bsize, 0) < 0) {
1318 Log("Unable to seek to offset %u for block %u\n", blk * Bsize, blk);
1320 #endif /* AFS_AIX41_ENV */
1322 if (afs_lseek(fd, (off_t) dbtob(blk), L_SET) < 0) {
1323 Log("Unable to seek to offset %u for block %u\n", dbtob(blk), blk);
1326 if (read(fd, buf, size) != size) {
1327 Log("Unable to read block %d, partition %s\n", blk, partition);
1333 #endif /* AFS_LINUX20_ENV */
1334 #endif /* AFS_NAMEI_ENV */