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>
29 #if defined(AFS_LINUX20_ENV) || defined(AFS_SUN4_ENV)
34 * -1 - Unable to read the inodes.
35 * -2 - Unable to completely write temp file. Produces warning message in log.
38 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
39 int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
40 char *wpath, void *rock)
42 Log("ListViceInodes not implemented for this platform!\n");
47 #include <sys/param.h>
48 #if defined(AFS_SGI_ENV)
52 #else /* AFS_OSF_ENV */
53 #ifdef AFS_VFSINCL_ENV
56 #include <sys/fs/ufs_fs.h>
58 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
59 #include <ufs/ufs/dinode.h>
60 #include <ufs/ffs/fs.h>
61 #define itod ino_to_fsba
66 #else /* AFS_VFSINCL_ENV */
68 #include <sys/filsys.h>
72 #endif /* AFS_VFSINCL_ENV */
73 #endif /* AFS_OSF_ENV */
75 #ifdef AFS_VFSINCL_ENV
76 #include <sys/vnode.h>
78 #include <sys/fs/ufs_inode.h>
80 #if !defined(AFS_DARWIN_ENV)
81 #include <ufs/inode.h>
84 #else /* AFS_VFSINCL_ENV */
86 #include <ufs/inode.h>
87 #else /* AFS_OSF_ENV */
88 #include <sys/inode.h>
90 #endif /* AFS_VFSINCL_ENV */
91 #endif /* AFS_SGI_ENV */
92 #include <afs/osi_inode.h>
96 #include <afs/afsint.h>
98 #include <afs/afssyscalls.h>
99 #include "viceinode.h"
100 #include <sys/stat.h>
101 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV)
104 #ifdef AFS_PTHREAD_ENV
106 #else /* AFS_PTHREAD_ENV */
107 #include <afs/assert.h>
108 #endif /* AFS_PTHREAD_ENV */
109 #if defined(AFS_HPUX101_ENV)
112 #include "partition.h"
114 /*@+fcnmacros +macrofcndecl@*/
117 extern off64_t afs_lseek(int FD, off64_t O, int F);
118 #endif /*S_SPLINT_S */
119 #define afs_lseek(FD, O, F) lseek64(FD, (off64_t) (O), F)
120 #define afs_stat stat64
121 #define afs_fstat fstat64
122 #define afs_open open64
123 #define afs_fopen fopen64
124 #else /* !O_LARGEFILE */
126 extern off_t afs_lseek(int FD, off_t O, int F);
127 #endif /*S_SPLINT_S */
128 #define afs_lseek(FD, O, F) lseek(FD, (off_t) (O), F)
129 #define afs_stat stat
130 #define afs_fstat fstat
131 #define afs_open open
132 #define afs_fopen fopen
133 #endif /* !O_LARGEFILE */
134 /*@=fcnmacros =macrofcndecl@*/
136 /* Notice: parts of this module have been cribbed from vfsck.c */
139 static char *partition;
144 #include <jfs/filsys.h>
147 #define FSBSIZE (4096) /* filesystem block size */
148 #define FSBSHIFT (12) /* log2(FSBSIZE) */
149 #define FSBMASK (FSBSIZE - 1) /* FSBSIZE mask */
151 #define MIN_FSIZE DISKMAP_B /* minimum fs size (FSblocks) */
152 #define LAST_RSVD_I 15 /* last reserved inode */
157 * This will hopefully eventually make it into the system include files
159 #define INOPB (FSBSIZE / sizeof (struct dinode))
167 #endif /* AFS_AIX41_ENV */
170 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
171 XX This was lifted from some `com/cmd/fs/fshlpr_aix3/Fs.h', which indicated X
172 XX a longing to see it make it into a readily accessible include file. XXXXXX
173 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
175 * itoo - inode number to offset within disk block
178 #define itoo(x) (int) ((unsigned)(x) % INOPB)
180 int Bsize = FSBSIZE; /* block size for this system */
181 daddr_t fmax; /* total number of blocks n file system */
182 ino_t imax, inum; /* total number of I-nodes in file system */
184 static struct superblock fs;
185 struct dinode *ginode();
189 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
190 int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
191 char *wpath, void *rock)
193 FILE *inodeFile = NULL;
194 char dev[50], rdev[51];
197 struct ViceInodeInfo info;
198 struct stat root_inode;
199 int ninodes = 0, err = 0;
201 pfd = -1; /* initialize so we don't close on error output below. */
204 sleep(1); /* simulate operator */
210 partition = mountedOn;
211 sprintf(dev, "/dev/%s", devname);
212 sprintf(rdev, "/dev/r%s", devname);
214 if (stat(mountedOn, &root_inode) < 0) {
215 Log("cannot stat: %s\n", mountedOn);
219 if (root_inode.st_ino != ROOTDIR_I) {
220 Log("%s is not root of a filesystem\n", mountedOn);
226 * done with the superblock, now try to read the raw device.
228 if (ReadSuper(&fs, dev) < 0)
233 case FM_CLEAN: /* clean and unmounted */
234 Log("Most peculiar - Super blk in FM_CLEAN state!\n");
236 case FM_MOUNT: /* mounted cleanly */
239 case FM_MDIRTY: /* dirty when mounted or commit fail */
240 case FM_LOGREDO: /* log redo attempted but failed */
241 Log("File system %s is in a bad state.\n", rdev);
242 Log("Call your IBM representative.\n");
246 if (IsBigFilesFileSystem(&fs, (char *)0)) {
247 Log("%s is a big files filesystem, can't salvage.\n", mountedOn);
251 if (strncmp(fs.s_magic, fsv3magic, strlen(fsv3magic)) != 0) {
253 if ((strncmp(fs.s_magic, fsv3pmagic, strlen(fsv3pmagic)) != 0)
254 || (fs.s_version != fsv3pvers)) {
255 Log("Super block doesn't have the problem magic (%s vs v3magic %s v3pmagic %s)\n", fs.s_magic, fsv3magic, fsv3pmagic);
259 Log("Super block doesn't have the problem magic (%s vs v3magic %s)\n",
260 fs.s_magic, fsv3magic);
267 fragsize = (fs.s_fragsize) ? fs.s_fragsize : FSBSIZE;
268 iagsize = (fs.s_iagsize) ? fs.s_iagsize : fs.s_agsize;
269 ag512 = fragsize * fs.s_agsize / 512;
270 agblocks = fragsize * fs.s_agsize >> BSHIFT;
271 #endif /* AFS_AIX41_ENV */
273 fmax = fs.s_fsize / (FSBSIZE / 512); /* first invalid blk num */
275 pfd = afs_open(rdev, O_RDONLY);
277 Log("Unable to open `%s' inode for reading\n", rdev);
281 inodeFile = fopen(resultFile, "w");
282 if (inodeFile == NULL) {
283 Log("Unable to create inode description file %s\n", resultFile);
288 * calculate the maximum number of inodes possible
291 imax = iagsize * (fs.s_fsize / ag512) - 1;
292 #else /* AFS_AIX41_ENV */
294 ((fmax / fs.s_agsize +
295 ((fmax % fs.s_agsize) >= fs.s_agsize / INOPB ? 1 : 0))
297 #endif /* AFS_AIX41_ENV */
300 * check for "FORCESALVAGE" equivalent:
301 * LAST_RSVD_I is a vice inode, with dead beef, and
302 * di_nlink == 2 to indicate the FORCE.
304 assert(p = ginode(LAST_RSVD_I));
306 if (p->di_vicemagic == VICEMAGIC && p->di_vicep1 == 0xdeadbeef
307 && p->di_nlink == 2) {
309 idec(root_inode.st_dev, LAST_RSVD_I, 0xdeadbeef);
312 for (inum = LAST_RSVD_I + 1; inum <= imax; ++inum) {
313 if ((p = ginode(inum)) == NULL || p->di_vicemagic != VICEMAGIC
314 || (p->di_mode & IFMT) != IFREG)
317 info.inodeNumber = inum;
318 info.byteCount = p->di_size;
319 info.linkCount = p->di_nlink;
320 info.u.param[0] = p->di_vicep1;
321 info.u.param[1] = p->di_vicep2;
322 info.u.param[2] = p->di_vicep3;
323 info.u.param[3] = p->di_vicep4;
325 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
328 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
329 Log("Error writing inode file for partition %s\n", partition);
335 if (fflush(inodeFile) == EOF) {
336 Log("Unable to successfully flush inode file for %s\n", partition);
340 if (fsync(fileno(inodeFile)) == -1) {
341 Log("Unable to successfully fsync inode file for %s\n", partition);
345 if (fclose(inodeFile) == EOF) {
346 Log("Unable to successfully close inode file for %s\n", partition);
352 * Paranoia: check that the file is really the right size
354 if (stat(resultFile, &status) == -1) {
355 Log("Unable to successfully stat inode file for %s\n", partition);
359 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
360 Log("Wrong size (%d instead of %d) in inode file for %s\n",
361 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
380 /* Read in the superblock for devName */
382 ReadSuper(struct superblock *fs, char *devName)
386 pfd = afs_open(devName, O_RDONLY);
388 Log("Unable to open inode on %s for reading superblock.\n", devName);
392 if (bread(pfd, fs, SUPER_B, sizeof(struct superblock)) < 0) {
393 Log("Unable to read superblock on %s.\n", devName);
401 /* IsBigFilesFileSystem returns 1 if it's a big files filesystem, 0 otherwise. */
403 IsBigFilesFileSystem(struct superblock *sb)
405 if ((strncmp(sb->s_magic, fsv3pmagic, 4) == 0)
406 && (sb->s_version == fsbigfile)
420 static char buf[FSBSIZE];
421 static daddr_t last_blk = -1;
427 0) ? INODES_B + inum / INOPB : ag * agblocks + (inum -
430 #else /* AFS_AIX41_ENV */
431 ag = inum / fs.s_agsize;
434 0) ? INODES_B + inum / INOPB : ag * fs.s_agsize + (inum -
438 #endif /* AFS_AIX41_ENV */
440 if (last_blk != pblk) {
441 if (bread(pfd, buf, pblk, sizeof(buf)) < 0) {
448 dp = (struct dinode *)buf;
453 #else /* !AFS_AIX31_ENV */
455 #if defined(AFS_SGI_ENV)
457 /* libefs.h includes <assert.h>, which we don't want */
460 #ifdef AFS_SGI_XFS_IOPS_ENV
462 #include <afs/xfsattrs.h>
463 /* xfs_ListViceInodes
465 * xfs_ListViceInodes verifies and correct the XFS namespace as it collects
466 * the inode information. The name is required for the idec operation to work.
467 * Steps 2 and 3 below are for the AFS_XFS_NAME_VERS == 1. If the name space
468 * changes, the algorithm will need to change.
469 * 1) If the parent inode number does not match the directory's inod number,
470 * change it in the attribute.
471 * 2) If the unqifier in the attribute does not match the name, rename the
472 * file. This is done by doing an exclusive open, incrementing the tag
473 * number until a file can be created. If the tag changes, then the
474 * attribute will need updating.
475 * 3) If the tag in the attribute does not match the name, change the
477 * 4) Verify uid = RW volume id and gid = XFS_VICEMAGIC.
483 * Does the verifications listed above.
484 * We can't change the names until the readdir is complete, so we set the
485 * rename flag if the file needs renaming.
488 xfs_VerifyInode(char *dir, uint64_t pino, char *name, i_list_inode_t * info,
495 int update_chown = 0;
502 (void)sprintf(path, "%s/%s", dir, name);
503 /* Verify uid and gid fields */
504 if (info->ili_magic != XFS_VICEMAGIC) {
505 Log("%s magic for %s/%s (inode %s) from %d to %d\n",
506 Testing ? "Would have changed" : "Changing", dir, name,
507 PrintInode(NULL, info->ili_info.inodeNumber), info->ili_magic,
513 vno = info->ili_info.param[0];
514 if (info->ili_vno != AFS_XFS_VNO_CLIP(vno)) {
515 Log("%s volume id for %s/%s (inode %s) from %d to %d\n",
516 Testing ? "Would have changed" : "Changing", dir, name,
517 PrintInode(NULL, info->ili_info.inodeNumber), info->ili_vno,
518 AFS_XFS_VNO_CLIP(vno));
524 if (chown(path, AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC) < 0) {
525 Log("Can't chown %s to uid=%d, gid=0x%x\n", path,
526 AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC);
531 /* Check Parent inode number. */
532 if (info->ili_pino != pino) {
533 afs_ino_str_t sino, sipino, spino;
534 (void)PrintInode(sino, info->ili_info.inodeNumber);
535 (void)PrintInode(sipino, info->ili_pino);
536 (void)PrintInode(spino, pino);
537 Log("%s parent ino for %s (inode %s) from %s to %s.\n",
538 Testing ? "Would have changed" : "Changing", path, sino, sipino,
544 /* Verify the file name. */
545 (void)strcpy(tmpName, ".");
546 (void)strcat(tmpName, int_to_base64(stmp, info->ili_info.param[2]));
547 if (strncmp(name, tmpName, strlen(tmpName))) {
548 Log("%s name %s (inode %s) in directory %s, unique=%d, tag=%d\n",
549 Testing ? "Would have returned bad" : "Bad", name,
550 PrintInode(NULL, info->ili_info.inodeNumber), dir,
551 info->ili_info.param[2], info->ili_tag);
557 /* update the tag? */
558 (void)strcat(tmpName, ".");
559 (void)strcat(tmpName, int_to_base64(stmp, info->ili_tag));
560 if (strcmp(name, tmpName)) {
562 (void)strcpy(tmpName, name);
563 p = strchr(tmpName + 1, '.');
565 Log("No tag found on name %s (inode %s)in directory, %s.\n",
566 name, PrintInode(NULL, info->ili_info.inodeNumber), dir,
567 Testing ? "would have renamed" : "will rename");
571 tag = base64_to_int(p + 1);
572 Log("%s the tag for %s (inode %s) from %d to %d.\n",
573 Testing ? "Would have changed" : "Will change", path,
574 PrintInode(NULL, info->ili_info.inodeNumber), dir, tag,
582 if (update_pino || update_tag) {
583 afs_xfs_attr_t attrs;
586 length = SIZEOF_XFS_ATTR_T;
587 if (attr_get(path, AFS_XFS_ATTR, (char *)&attrs, &length, ATTR_ROOT) <
589 Log("Can't get AFS attribute for %s\n", path);
593 attrs.at_pino = pino;
597 (path, AFS_XFS_ATTR, (char *)&attrs, length,
598 ATTR_ROOT | ATTR_REPLACE) < 0) {
599 Log("Can't set AFS attribute into %s\n", path);
613 xfs_RenameFiles(char *dir, xfs_Rename_t * renames, int n_renames)
616 char opath[128], nbase[128], npath[128];
617 afs_xfs_attr_t attrs;
618 int length = SIZEOF_XFS_ATTR_T;
623 for (i = 0; i < n_renames; i++) {
624 (void)sprintf(opath, "%s/%s", dir, renames[i].name);
625 (void)sprintf(nbase, "%s/.%s", dir,
626 int_to_base64(stmp, renames[i].uniq));
627 for (tag = 2, j = 0; j < 64; tag++, j++) {
628 (void)sprintf(npath, "%s.%s", nbase, int_to_base64(stmp, tag));
629 fd = afs_open(npath, O_CREAT | O_EXCL | O_RDWR, 0);
636 Log("Can't find a new name for %s\n", opath);
639 if (rename(opath, npath) < 0) {
640 Log("Can't rename %s to %s\n", opath, npath);
643 Log("Renamed %s to %s\n", opath, npath);
650 xfs_ListViceInodes(char *devname, char *mountedOn, char *resultFile,
651 int (*judgeInode) (), int judgeParam, int *forcep,
652 int forceR, char *wpath, void *rock)
654 FILE *inodeFile = NULL;
656 int info_size = sizeof(i_list_inode_t);
659 dirent64_t *top_direntp;
661 dirent64_t *vol_direntp;
662 struct stat64 sdirbuf;
663 struct stat64 sfilebuf;
664 afs_xfs_attr_t attrs;
665 afs_xfs_dattr_t dattrs;
667 char vol_dirname[1024];
670 xfs_Rename_t *renames = (xfs_Rename_t *) 0;
672 #define N_RENAME_STEP 64
681 if (stat64(mountedOn, &sdirbuf) < 0) {
682 perror("xfs_ListViceInodes: stat64");
686 inodeFile = fopen(resultFile, "w");
687 if (inodeFile == NULL) {
688 Log("Unable to create inode description file %s\n", resultFile);
692 if ((top_dirp = opendir(mountedOn)) == NULL) {
693 Log("Can't open directory %s to read inodes.\n", mountedOn);
697 while (top_direntp = readdir64(top_dirp)) {
698 /* Only descend directories with the AFSDIR attribute set.
699 * Could also verify the contents of the atribute, but currently
701 * Performance could be improved for single volume salvages by
702 * only going through the directory containing the volume's inodes.
703 * But I'm being complete as a first pass.
705 (void)sprintf(vol_dirname, "%s/%s", mountedOn, top_direntp->d_name);
706 length = SIZEOF_XFS_DATTR_T;
708 (vol_dirname, AFS_XFS_DATTR, (char *)&dattrs, &length, ATTR_ROOT))
711 if ((vol_dirp = opendir(vol_dirname)) == NULL) {
712 if (errno == ENOTDIR)
714 Log("Can't open directory %s to read inodes.\n", vol_dirname);
718 pino = top_direntp->d_ino;
720 while (vol_direntp = readdir64(vol_dirp)) {
721 if (vol_direntp->d_name[1] == '\0'
722 || vol_direntp->d_name[1] == '.')
725 info.ili_version = AFS_XFS_ILI_VERSION;
726 info_size = sizeof(i_list_inode_t);
728 ilistinode64(sdirbuf.st_dev, vol_direntp->d_ino, &info,
731 /* Where possible, give more explicit messages. */
735 Log("%s (device id %d) is not on an XFS filesystem.\n",
736 vol_dirname, sdirbuf.st_dev);
741 if (info_size != sizeof(i_list_inode_t)
742 || info.ili_version != AFS_XFS_ILI_VERSION) {
743 Log("Version skew between kernel and salvager.\n");
748 /* Continue, so we collect all the errors in the first pass. */
749 Log("Error listing inode named %s/%s: %s\n", vol_dirname,
750 vol_direntp->d_name, strerror(errno));
755 if (info.ili_attr_version != AFS_XFS_ATTR_VERS) {
756 Log("Unrecognized XFS attribute version %d in %s/%s. Upgrade salvager\n", info.ili_attr_version, vol_dirname, vol_direntp->d_name);
760 if (judgeInode && (*judgeInode) (&info.ili_info, judgeParam, rock) == 0)
765 (vol_dirname, pino, vol_direntp->d_name, &info,
771 /* Add this name to the list of items to rename. */
772 if (n_renames >= n_avail) {
773 n_avail += N_RENAME_STEP;
774 if (n_avail == N_RENAME_STEP)
775 renames = (xfs_Rename_t *)
776 malloc(n_avail * sizeof(xfs_Rename_t));
778 renames = (xfs_Rename_t *)
779 realloc((char *)renames,
780 n_avail * sizeof(xfs_Rename_t));
782 Log("Can't %salloc %lu bytes for rename list.\n",
783 (n_avail == N_RENAME_STEP) ? "m" : "re",
784 n_avail * sizeof(xfs_Rename_t));
788 (void)strcpy(renames[n_renames].name, vol_direntp->d_name);
789 renames[n_renames].uniq = info.ili_info.param[2];
794 (&info.ili_info, sizeof(vice_inode_info_t), 1, inodeFile)
796 Log("Error writing inode file for partition %s\n", mountedOn);
801 } /* end while vol_direntp */
804 vol_dirp = (DIR *) 0;
806 Log("Renaming files.\n");
807 if (xfs_RenameFiles(vol_dirname, renames, n_renames) < 0) {
815 free((char *)renames);
816 if (fflush(inodeFile) == EOF) {
817 ("Unable to successfully flush inode file for %s\n", mountedOn);
819 return errors ? -1 : -2;
821 if (fsync(fileno(inodeFile)) == -1) {
822 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
824 return errors ? -1 : -2;
826 if (fclose(inodeFile) == EOF) {
827 Log("Unable to successfully close inode file for %s\n", mountedOn);
828 return errors ? -1 : -2;
831 * Paranoia: check that the file is really the right size
833 if (stat(resultFile, &status) == -1) {
834 Log("Unable to successfully stat inode file for %s\n", partition);
835 return errors ? -1 : -2;
837 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
838 Log("Wrong size (%d instead of %d) in inode file for %s\n",
839 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
841 return errors ? -1 : -2;
845 Log("Errors encontered listing inodes, not salvaging partition.\n");
857 free((char *)renames);
866 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
867 int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
868 char *wpath, void *rock)
870 FILE *inodeFile = NULL;
871 char dev[50], rdev[51];
873 struct efs_dinode *p;
874 struct ViceInodeInfo info;
875 struct stat root_inode;
876 int ninodes = 0, err = 0;
877 struct efs_dinode *dinodeBuf = NULL;
879 ino_t imax, inum; /* total number of I-nodes in file system */
883 sleep(1); /* simulate operator */
889 if (stat(mountedOn, &root_inode) < 0) {
890 Log("cannot stat: %s\n", mountedOn);
893 #ifdef AFS_SGI_XFS_IOPS_ENV
894 if (!strcmp("xfs", root_inode.st_fstype)) {
895 return xfs_ListViceInodes(devname, mountedOn, resultFile, judgeInode,
896 judgeParam, forcep, forceR, wpath, rock);
900 Log("%s is not root of a filesystem\n", mountedOn);
905 #else /* AFS_SGI_ENV */
908 #define SPERB (MAXBSIZE / sizeof(short))
909 #define MAXNINDIR (MAXBSIZE / sizeof(daddr_t))
912 struct bufarea *b_next; /* must be first */
916 char b_buf[MAXBSIZE]; /* buffer space */
917 short b_lnks[SPERB]; /* link counts */
918 daddr_t b_indir[MAXNINDIR]; /* indirect block */
919 struct fs b_fs; /* super block */
920 struct cg b_cg; /* cylinder group */
924 typedef struct bufarea BUFAREA;
927 #define sblock sblk.b_un.b_fs
928 #endif /* AFS_HPUX_ENV */
930 extern char *afs_rawname();
932 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
933 int (*judgeInode) (), int judgeParam, int *forcep, int forceR,
934 char *wpath, void *rock)
940 #else /* !AFS_AIX_ENV */
945 int i, c, e, bufsize, code, err = 0;
946 FILE *inodeFile = NULL;
947 char dev[50], rdev[100], err1[512], *ptr1;
948 struct dinode *inodes = NULL, *einodes, *dptr;
952 struct ViceInodeInfo info;
955 partition = mountedOn;
956 sprintf(rdev, "%s/%s", wpath, devname);
957 ptr1 = afs_rawname(rdev);
961 /* Bletch: this is terrible; is there a better way to do this? Does this work? vfsck doesn't even sleep!! */
963 sleep(5); /* Trying a smaller one for aix */
968 pfd = afs_open(rdev, O_RDONLY);
970 sprintf(err1, "Could not open device %s to get inode list\n", rdev);
975 if (bread(pfd, (char *)&super.fs, SUPERB, sizeof super.fs) == -1) {
978 if (bread(pfd, (char *)&sblock, SBLOCK, SBSIZE) == -1) {
980 if (bread(pfd, super.block, SBLOCK, SBSIZE) == -1) {
981 #endif /* AFS_HPUX_ENV */
983 Log("Unable to read superblock, partition %s\n", partition);
987 inodeFile = fopen(resultFile, "w");
988 if (inodeFile == NULL) {
989 Log("Unable to create inode description file %s\n", resultFile);
994 * char *FSlabel(), *fslabel=0;
995 * fslabel = FSlabel(&super.fs);
997 if (super.fs.s_bsize == 0)
998 super.fs.s_bsize = 512;
999 if (super.fs.s_bsize != BSIZE) {
1000 Log("SuperBlk: Cluster size not %d; run vfsck\n", BSIZE);
1003 fmax = super.fs.s_fsize; /* first invalid blk num */
1004 imax = ((ino_t) super.fs.s_isize - (SUPERB + 1)) * INOPB;
1006 Log("Size check: imax==0!\n");
1009 if (GetAuxInodeFile(partition, &status) == 0) {
1010 Log("Can't access Aux inode file for partition %s, aborting\n",
1014 for (inum = 1; inum <= imax; inum++) {
1015 struct dauxinode *auxp;
1016 if ((auxp = IsAfsInode(inum)) == NULL) {
1017 /* Not an afs inode, keep going */
1020 if ((p = ginode(inum)) == NULL)
1022 /* deleted/non-existent inode when di_mode == 0 */
1025 info.inodeNumber = (int)inum;
1026 info.byteCount = p->di_size;
1027 info.linkCount = p->di_nlink;
1028 info.u.param[0] = auxp->aux_param1;
1029 info.u.param[1] = auxp->aux_param2;
1030 info.u.param[2] = auxp->aux_param3;
1031 info.u.param[3] = auxp->aux_param4;
1032 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1034 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1035 Log("Error writing inode file for partition %s\n", partition);
1042 * run a few consistency checks of the superblock
1043 * (Cribbed from vfsck)
1046 #if defined(FD_FSMAGIC)
1047 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)
1048 && (sblock.fs_magic != FD_FSMAGIC)
1049 #if defined(AFS_HPUX101_ENV)
1050 && (sblock.fs_magic != FD_FSMAGIC_2)
1054 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)) {
1056 Log("There's something wrong with the superblock for partition %s; bad magic (%d) run vfsck\n", partition, sblock.fs_magic);
1059 if (sblock.fs_ncg < 1) {
1060 Log("There's something wrong with the superblock for partition %s; NCG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_ncg);
1063 if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG) {
1064 Log("There's something wrong with the superblock for partition %s; CPG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_cpg);
1067 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl
1068 || (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) {
1069 Log("There's something wrong with the superblock for partition %s; NCYL LESS THAN NCG*CPG run vfsck\n", partition);
1072 if (sblock.fs_sbsize > SBSIZE) {
1073 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);
1077 if ((super.fs.fs_magic != FS_MAGIC)
1078 || (super.fs.fs_ncg < 1)
1079 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1080 || (super.fs.fs_cpg < 1)
1082 || (super.fs.fs_cpg < 1 || super.fs.fs_cpg > MAXCPG)
1084 || (super.fs.fs_ncg * super.fs.fs_cpg < super.fs.fs_ncyl
1085 || (super.fs.fs_ncg - 1) * super.fs.fs_cpg >= super.fs.fs_ncyl)
1086 || (super.fs.fs_sbsize > SBSIZE)) {
1087 Log("There's something wrong with the superblock for partition %s; run vfsck\n", partition);
1090 #endif /* AFS_HPUX_ENV */
1093 bufsize = sblock.fs_ipg * sizeof(struct dinode);
1095 bufsize = super.fs.fs_ipg * sizeof(struct dinode);
1096 #endif /* AFS_HPUX_ENV */
1097 inodes = (struct dinode *)malloc(bufsize);
1098 einodes = (struct dinode *)(((char *)inodes) + bufsize);
1099 if (inodes == NULL) {
1100 Log("Unable to allocate enough memory to scan inodes; help!\n");
1103 Log("Scanning inodes on device %s...\n", rdev);
1105 for (c = 0; c < sblock.fs_ncg; c++) {
1106 i = c * sblock.fs_ipg;
1107 e = i + sblock.fs_ipg;
1108 #if defined(AFS_HPUX102_ENV)
1109 if (afs_lseek(pfd, dbtoo(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1112 if (afs_lseek(pfd, dbtob(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1116 for (c = 0; c < super.fs.fs_ncg; c++) {
1118 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1120 #if defined(AFS_DARWIN_ENV)
1121 #define offset_t off_t
1122 #define llseek lseek
1125 #endif /* AFS_SUN5_ENV */
1126 i = c * super.fs.fs_ipg;
1127 e = i + super.fs.fs_ipg;
1129 dblk1 = fsbtodb(&super.fs, itod(&super.fs, i));
1130 if (afs_lseek(pfd, (off_t) ((off_t) dblk1 * DEV_BSIZE), L_SET) == -1) {
1132 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1133 f1 = fsbtodb(&super.fs, itod(&super.fs, i));
1134 off = (offset_t) f1 << DEV_BSHIFT;
1135 if (llseek(pfd, off, L_SET) == -1) {
1137 if (afs_lseek(pfd, dbtob(fsbtodb(&super.fs, itod(&super.fs, i))), L_SET)
1139 #endif /* AFS_SUN5_ENV */
1140 #endif /* AFS_OSF_ENV */
1141 #endif /* AFS_HPUX_ENV */
1142 Log("Error reading inodes for partition %s; run vfsck\n",
1148 if (read(pfd, inodes, bufsize) != bufsize) {
1149 Log("Error reading inodes for partition %s; run vfsck\n",
1154 register int bj, bk;
1156 for (bj = bk = 0; bj < bufsize; bj = bj + 512, bk++) {
1157 if ((code = read(pfd, dptr, 512)) != 512) {
1158 Log("Error reading inode %d? for partition %s (errno = %d); run vfsck\n", bk + i, partition, errno);
1159 if (afs_lseek(pfd, 512, L_SET) == -1) {
1160 Log("Lseek failed\n");
1175 for (p = inodes; p < einodes && i < e; i++, p++) {
1177 Log("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1178 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1179 p->di_size, p->di_nlink);
1181 ("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1182 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1183 p->di_size, p->di_nlink);
1186 #ifdef AFS_3DISPARES
1187 /* Check to see if this inode is a pre-"OSF1 4.0D" inode */
1188 if ((p->di_uid || p->di_gid)
1189 && !(p->di_flags & (IC_XUID | IC_XGID))) {
1190 Log("Found unconverted inode %d: Use 'fs_conv_dux40D convert' on partition %s\n", i, partition);
1194 assert(0); /* define AFS_3DISPARES in param.h */
1197 #if defined(AFS_SUN56_ENV)
1198 /* if this is a pre-sol2.6 unconverted inode, bail out */
1200 afs_uint32 p1, p2, p3, p4;
1204 q = (quad *) & (p->di_ic.ic_lsize);
1206 p2 = p->di_ic.ic_flags;
1208 p4 = p->di_ic.ic_uid;
1209 p5 = p->di_ic.ic_gid;
1211 if ((p2 || p3) && !p4 && (p5 == -2)) {
1212 Log("Found unconverted inode %d\n", i);
1213 Log("You should run the AFS file conversion utility\n");
1218 if (IS_DVICEMAGIC(p) && (p->di_mode & IFMT) == IFREG) {
1219 afs_uint32 p2 = p->di_vicep2, p3 = DI_VICEP3(p);
1221 info.u.param[0] = p->di_vicep1;
1222 #ifdef AFS_3DISPARES
1223 if (((p2 >> 3) == INODESPECIAL) && (p2 & 0x3)) {
1224 info.u.param[1] = INODESPECIAL;
1225 info.u.param[2] = p3;
1226 info.u.param[3] = p2 & 0x3;
1228 info.u.param[1] = ((p2 >> 27) << 16) + (p3 & 0xffff);
1229 info.u.param[2] = (p2 & 0x3fffff);
1231 (((p2 >> 22) & 0x1f) << 16) + (p3 >> 16);
1234 info.u.param[1] = p->di_vicep2;
1235 info.u.param[2] = DI_VICEP3(p);
1236 info.u.param[3] = p->di_vicep4;
1238 info.inodeNumber = i;
1239 info.byteCount = p->di_size;
1240 info.linkCount = p->di_nlink;
1241 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1243 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1244 Log("Error writing inode file for partition %s\n",
1256 if (fflush(inodeFile) == EOF) {
1257 Log("Unable to successfully flush inode file for %s\n", partition);
1261 if (fsync(fileno(inodeFile)) == -1) {
1262 Log("Unable to successfully fsync inode file for %s\n", partition);
1266 if (fclose(inodeFile) == EOF) {
1267 Log("Unable to successfully close inode file for %s\n", partition);
1273 * Paranoia: check that the file is really the right size
1275 if (stat(resultFile, &status) == -1) {
1276 Log("Unable to successfully stat inode file for %s\n", partition);
1280 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1281 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1282 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1300 #endif /* !AFS_SGI_ENV */
1301 #endif /* !AFS_AIX31_ENV */
1303 #ifdef AFS_DARWIN_ENV
1305 #define dbtob(db) ((unsigned)(db) << DEV_BSHIFT)
1309 bread(int fd, char *buf, daddr_t blk, afs_int32 size)
1312 #ifdef AFS_AIX41_ENV
1313 offset_t off = (offset_t) blk << FSBSHIFT;
1314 if (llseek(fd, off, 0) < 0) {
1315 Log("Unable to seek to offset %llu for block %u\n", off, blk);
1318 #else /* AFS_AIX41_ENV */
1319 if (afs_lseek(fd, blk * Bsize, 0) < 0) {
1320 Log("Unable to seek to offset %u for block %u\n", blk * Bsize, blk);
1322 #endif /* AFS_AIX41_ENV */
1324 if (afs_lseek(fd, (off_t) dbtob(blk), L_SET) < 0) {
1325 Log("Unable to seek to offset %u for block %u\n", dbtob(blk), blk);
1328 if (read(fd, buf, size) != size) {
1329 Log("Unable to read block %d, partition %s\n", blk, partition);
1335 #endif /* AFS_LINUX20_ENV */
1336 #endif /* AFS_NAMEI_ENV */