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 afs_uint32 (*judgeInode) (), afs_uint32 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)
114 #include "volinodes.h"
115 #include "partition.h"
118 /*@+fcnmacros +macrofcndecl@*/
121 extern off64_t afs_lseek(int FD, off64_t O, int F);
122 #endif /*S_SPLINT_S */
123 #define afs_lseek(FD, O, F) lseek64(FD, (off64_t) (O), F)
124 #define afs_stat stat64
125 #define afs_fstat fstat64
126 #define afs_open open64
127 #define afs_fopen fopen64
128 #else /* !O_LARGEFILE */
130 extern off_t afs_lseek(int FD, off_t O, int F);
131 #endif /*S_SPLINT_S */
132 #define afs_lseek(FD, O, F) lseek(FD, (off_t) (O), F)
133 #define afs_stat stat
134 #define afs_fstat fstat
135 #define afs_open open
136 #define afs_fopen fopen
137 #endif /* !O_LARGEFILE */
138 /*@=fcnmacros =macrofcndecl@*/
140 /* Notice: parts of this module have been cribbed from vfsck.c */
143 static char *partition;
148 #include <jfs/filsys.h>
151 #define FSBSIZE (4096) /* filesystem block size */
152 #define FSBSHIFT (12) /* log2(FSBSIZE) */
153 #define FSBMASK (FSBSIZE - 1) /* FSBSIZE mask */
155 #define MIN_FSIZE DISKMAP_B /* minimum fs size (FSblocks) */
156 #define LAST_RSVD_I 15 /* last reserved inode */
161 * This will hopefully eventually make it into the system include files
163 #define INOPB (FSBSIZE / sizeof (struct dinode))
171 #endif /* AFS_AIX41_ENV */
174 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
175 XX This was lifted from some `com/cmd/fs/fshlpr_aix3/Fs.h', which indicated X
176 XX a longing to see it make it into a readily accessible include file. XXXXXX
177 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
179 * itoo - inode number to offset within disk block
182 #define itoo(x) (int) ((unsigned)(x) % INOPB)
184 int Bsize = FSBSIZE; /* block size for this system */
185 daddr_t fmax; /* total number of blocks n file system */
186 ino_t imax, inum; /* total number of I-nodes in file system */
188 static struct superblock fs;
189 struct dinode *ginode();
193 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
194 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
195 char *wpath, void *rock)
197 FILE *inodeFile = NULL;
198 char dev[50], rdev[51];
201 struct ViceInodeInfo info;
202 struct stat root_inode;
203 int ninodes = 0, err = 0;
205 pfd = -1; /* initialize so we don't close on error output below. */
208 sleep(1); /* simulate operator */
214 partition = mountedOn;
215 sprintf(dev, "/dev/%s", devname);
216 sprintf(rdev, "/dev/r%s", devname);
218 if (stat(mountedOn, &root_inode) < 0) {
219 Log("cannot stat: %s\n", mountedOn);
223 if (root_inode.st_ino != ROOTDIR_I) {
224 Log("%s is not root of a filesystem\n", mountedOn);
230 * done with the superblock, now try to read the raw device.
232 if (ReadSuper(&fs, dev) < 0)
237 case FM_CLEAN: /* clean and unmounted */
238 Log("Most peculiar - Super blk in FM_CLEAN state!\n");
240 case FM_MOUNT: /* mounted cleanly */
243 case FM_MDIRTY: /* dirty when mounted or commit fail */
244 case FM_LOGREDO: /* log redo attempted but failed */
245 Log("File system %s is in a bad state.\n", rdev);
246 Log("Call your IBM representative.\n");
250 if (IsBigFilesFileSystem(&fs, (char *)0)) {
251 Log("%s is a big files filesystem, can't salvage.\n", mountedOn);
255 if (strncmp(fs.s_magic, fsv3magic, strlen(fsv3magic)) != 0) {
257 if ((strncmp(fs.s_magic, fsv3pmagic, strlen(fsv3pmagic)) != 0)
258 || (fs.s_version != fsv3pvers)) {
259 Log("Super block doesn't have the problem magic (%s vs v3magic %s v3pmagic %s)\n", fs.s_magic, fsv3magic, fsv3pmagic);
263 Log("Super block doesn't have the problem magic (%s vs v3magic %s)\n",
264 fs.s_magic, fsv3magic);
271 fragsize = (fs.s_fragsize) ? fs.s_fragsize : FSBSIZE;
272 iagsize = (fs.s_iagsize) ? fs.s_iagsize : fs.s_agsize;
273 ag512 = fragsize * fs.s_agsize / 512;
274 agblocks = fragsize * fs.s_agsize >> BSHIFT;
275 #endif /* AFS_AIX41_ENV */
277 fmax = fs.s_fsize / (FSBSIZE / 512); /* first invalid blk num */
279 pfd = afs_open(rdev, O_RDONLY);
281 Log("Unable to open `%s' inode for reading\n", rdev);
286 inodeFile = fopen(resultFile, "w");
287 if (inodeFile == NULL) {
288 Log("Unable to create inode description file %s\n", resultFile);
294 * calculate the maximum number of inodes possible
297 imax = iagsize * (fs.s_fsize / ag512) - 1;
298 #else /* AFS_AIX41_ENV */
300 ((fmax / fs.s_agsize +
301 ((fmax % fs.s_agsize) >= fs.s_agsize / INOPB ? 1 : 0))
303 #endif /* AFS_AIX41_ENV */
306 * check for "FORCESALVAGE" equivalent:
307 * LAST_RSVD_I is a vice inode, with dead beef, and
308 * di_nlink == 2 to indicate the FORCE.
310 assert(p = ginode(LAST_RSVD_I));
312 if (p->di_vicemagic == VICEMAGIC && p->di_vicep1 == 0xdeadbeef
313 && p->di_nlink == 2) {
315 idec(root_inode.st_dev, LAST_RSVD_I, 0xdeadbeef);
318 for (inum = LAST_RSVD_I + 1; inum <= imax; ++inum) {
319 if ((p = ginode(inum)) == NULL || p->di_vicemagic != VICEMAGIC
320 || (p->di_mode & IFMT) != IFREG)
323 info.inodeNumber = inum;
324 info.byteCount = p->di_size;
325 info.linkCount = p->di_nlink;
326 info.u.param[0] = p->di_vicep1;
327 info.u.param[1] = p->di_vicep2;
328 info.u.param[2] = p->di_vicep3;
329 info.u.param[3] = p->di_vicep4;
331 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
335 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
336 Log("Error writing inode file for partition %s\n", partition);
344 if (fflush(inodeFile) == EOF) {
345 Log("Unable to successfully flush inode file for %s\n", partition);
349 if (fsync(fileno(inodeFile)) == -1) {
350 Log("Unable to successfully fsync inode file for %s\n", partition);
354 if (fclose(inodeFile) == EOF) {
355 Log("Unable to successfully close inode file for %s\n", partition);
361 * Paranoia: check that the file is really the right size
363 if (stat(resultFile, &status) == -1) {
364 Log("Unable to successfully stat inode file for %s\n", partition);
368 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
369 Log("Wrong size (%d instead of %d) in inode file for %s\n",
370 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
390 /* Read in the superblock for devName */
392 ReadSuper(struct superblock *fs, char *devName)
396 pfd = afs_open(devName, O_RDONLY);
398 Log("Unable to open inode on %s for reading superblock.\n", devName);
402 if (bread(pfd, fs, SUPER_B, sizeof(struct superblock)) < 0) {
403 Log("Unable to read superblock on %s.\n", devName);
411 /* IsBigFilesFileSystem returns 1 if it's a big files filesystem, 0 otherwise. */
413 IsBigFilesFileSystem(struct superblock *sb)
415 if ((strncmp(sb->s_magic, fsv3pmagic, 4) == 0)
416 && (sb->s_version == fsbigfile)
430 static char buf[FSBSIZE];
431 static daddr_t last_blk = -1;
437 0) ? INODES_B + inum / INOPB : ag * agblocks + (inum -
440 #else /* AFS_AIX41_ENV */
441 ag = inum / fs.s_agsize;
444 0) ? INODES_B + inum / INOPB : ag * fs.s_agsize + (inum -
448 #endif /* AFS_AIX41_ENV */
450 if (last_blk != pblk) {
451 if (bread(pfd, buf, pblk, sizeof(buf)) < 0) {
458 dp = (struct dinode *)buf;
463 #else /* !AFS_AIX31_ENV */
465 #if defined(AFS_SGI_ENV)
467 /* libefs.h includes <assert.h>, which we don't want */
470 #ifdef AFS_SGI_XFS_IOPS_ENV
472 #include <afs/xfsattrs.h>
473 /* xfs_ListViceInodes
475 * xfs_ListViceInodes verifies and correct the XFS namespace as it collects
476 * the inode information. The name is required for the idec operation to work.
477 * Steps 2 and 3 below are for the AFS_XFS_NAME_VERS == 1. If the name space
478 * changes, the algorithm will need to change.
479 * 1) If the parent inode number does not match the directory's inod number,
480 * change it in the attribute.
481 * 2) If the unqifier in the attribute does not match the name, rename the
482 * file. This is done by doing an exclusive open, incrementing the tag
483 * number until a file can be created. If the tag changes, then the
484 * attribute will need updating.
485 * 3) If the tag in the attribute does not match the name, change the
487 * 4) Verify uid = RW volume id and gid = XFS_VICEMAGIC.
493 * Does the verifications listed above.
494 * We can't change the names until the readdir is complete, so we set the
495 * rename flag if the file needs renaming.
498 xfs_VerifyInode(char *dir, uint64_t pino, char *name, i_list_inode_t * info,
505 int update_chown = 0;
512 (void)sprintf(path, "%s/%s", dir, name);
513 /* Verify uid and gid fields */
514 if (info->ili_magic != XFS_VICEMAGIC) {
515 Log("%s magic 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_magic,
523 vno = info->ili_info.param[0];
524 if (info->ili_vno != AFS_XFS_VNO_CLIP(vno)) {
525 Log("%s volume id for %s/%s (inode %s) from %d to %d\n",
526 Testing ? "Would have changed" : "Changing", dir, name,
527 PrintInode(NULL, info->ili_info.inodeNumber), info->ili_vno,
528 AFS_XFS_VNO_CLIP(vno));
534 if (chown(path, AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC) < 0) {
535 Log("Can't chown %s to uid=%d, gid=0x%x\n", path,
536 AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC);
541 /* Check Parent inode number. */
542 if (info->ili_pino != pino) {
543 afs_ino_str_t sino, sipino, spino;
544 (void)PrintInode(sino, info->ili_info.inodeNumber);
545 (void)PrintInode(sipino, info->ili_pino);
546 (void)PrintInode(spino, pino);
547 Log("%s parent ino for %s (inode %s) from %s to %s.\n",
548 Testing ? "Would have changed" : "Changing", path, sino, sipino,
554 /* Verify the file name. */
555 (void)strcpy(tmpName, ".");
556 (void)strcat(tmpName, int_to_base64(stmp, info->ili_info.param[2]));
557 if (strncmp(name, tmpName, strlen(tmpName))) {
558 Log("%s name %s (inode %s) in directory %s, unique=%d, tag=%d\n",
559 Testing ? "Would have returned bad" : "Bad", name,
560 PrintInode(NULL, info->ili_info.inodeNumber), dir,
561 info->ili_info.param[2], info->ili_tag);
567 /* update the tag? */
568 (void)strcat(tmpName, ".");
569 (void)strcat(tmpName, int_to_base64(stmp, info->ili_tag));
570 if (strcmp(name, tmpName)) {
572 (void)strcpy(tmpName, name);
573 p = strchr(tmpName + 1, '.');
575 Log("No tag found on name %s (inode %s)in directory, %s.\n",
576 name, PrintInode(NULL, info->ili_info.inodeNumber), dir,
577 Testing ? "would have renamed" : "will rename");
581 tag = base64_to_int(p + 1);
582 Log("%s the tag for %s (inode %s) from %d to %d.\n",
583 Testing ? "Would have changed" : "Will change", path,
584 PrintInode(NULL, info->ili_info.inodeNumber), dir, tag,
592 if (update_pino || update_tag) {
593 afs_xfs_attr_t attrs;
596 length = SIZEOF_XFS_ATTR_T;
597 if (attr_get(path, AFS_XFS_ATTR, (char *)&attrs, &length, ATTR_ROOT) <
599 Log("Can't get AFS attribute for %s\n", path);
603 attrs.at_pino = pino;
607 (path, AFS_XFS_ATTR, (char *)&attrs, length,
608 ATTR_ROOT | ATTR_REPLACE) < 0) {
609 Log("Can't set AFS attribute into %s\n", path);
623 xfs_RenameFiles(char *dir, xfs_Rename_t * renames, int n_renames)
626 char opath[128], nbase[128], npath[128];
627 afs_xfs_attr_t attrs;
628 int length = SIZEOF_XFS_ATTR_T;
633 for (i = 0; i < n_renames; i++) {
634 (void)sprintf(opath, "%s/%s", dir, renames[i].name);
635 (void)sprintf(nbase, "%s/.%s", dir,
636 int_to_base64(stmp, renames[i].uniq));
637 for (tag = 2, j = 0; j < 64; tag++, j++) {
638 (void)sprintf(npath, "%s.%s", nbase, int_to_base64(stmp, tag));
639 fd = afs_open(npath, O_CREAT | O_EXCL | O_RDWR, 0);
646 Log("Can't find a new name for %s\n", opath);
649 if (rename(opath, npath) < 0) {
650 Log("Can't rename %s to %s\n", opath, npath);
653 Log("Renamed %s to %s\n", opath, npath);
660 xfs_ListViceInodes(char *devname, char *mountedOn, char *resultFile,
661 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep,
662 int forceR, char *wpath, void *rock)
664 FILE *inodeFile = NULL;
666 int info_size = sizeof(i_list_inode_t);
669 dirent64_t *top_direntp;
671 dirent64_t *vol_direntp;
672 struct stat64 sdirbuf;
673 struct stat64 sfilebuf;
674 afs_xfs_attr_t attrs;
675 afs_xfs_dattr_t dattrs;
677 char vol_dirname[1024];
680 xfs_Rename_t *renames = (xfs_Rename_t *) 0;
682 #define N_RENAME_STEP 64
691 if (stat64(mountedOn, &sdirbuf) < 0) {
692 perror("xfs_ListViceInodes: stat64");
697 inodeFile = fopen(resultFile, "w");
698 if (inodeFile == NULL) {
699 Log("Unable to create inode description file %s\n", resultFile);
704 if ((top_dirp = opendir(mountedOn)) == NULL) {
705 Log("Can't open directory %s to read inodes.\n", mountedOn);
709 while (top_direntp = readdir64(top_dirp)) {
710 /* Only descend directories with the AFSDIR attribute set.
711 * Could also verify the contents of the atribute, but currently
713 * Performance could be improved for single volume salvages by
714 * only going through the directory containing the volume's inodes.
715 * But I'm being complete as a first pass.
717 (void)sprintf(vol_dirname, "%s/%s", mountedOn, top_direntp->d_name);
718 length = SIZEOF_XFS_DATTR_T;
720 (vol_dirname, AFS_XFS_DATTR, (char *)&dattrs, &length, ATTR_ROOT))
723 if ((vol_dirp = opendir(vol_dirname)) == NULL) {
724 if (errno == ENOTDIR)
726 Log("Can't open directory %s to read inodes.\n", vol_dirname);
730 pino = top_direntp->d_ino;
732 while (vol_direntp = readdir64(vol_dirp)) {
733 if (vol_direntp->d_name[1] == '\0'
734 || vol_direntp->d_name[1] == '.')
737 info.ili_version = AFS_XFS_ILI_VERSION;
738 info_size = sizeof(i_list_inode_t);
740 ilistinode64(sdirbuf.st_dev, vol_direntp->d_ino, &info,
743 /* Where possible, give more explicit messages. */
747 Log("%s (device id %d) is not on an XFS filesystem.\n",
748 vol_dirname, sdirbuf.st_dev);
753 if (info_size != sizeof(i_list_inode_t)
754 || info.ili_version != AFS_XFS_ILI_VERSION) {
755 Log("Version skew between kernel and salvager.\n");
760 /* Continue, so we collect all the errors in the first pass. */
761 Log("Error listing inode named %s/%s: %s\n", vol_dirname,
762 vol_direntp->d_name, strerror(errno));
767 if (info.ili_attr_version != AFS_XFS_ATTR_VERS) {
768 Log("Unrecognized XFS attribute version %d in %s/%s. Upgrade salvager\n", info.ili_attr_version, vol_dirname, vol_direntp->d_name);
772 if (judgeInode && (*judgeInode) (&info.ili_info, judgeParam, rock) == 0)
777 (vol_dirname, pino, vol_direntp->d_name, &info,
783 /* Add this name to the list of items to rename. */
784 if (n_renames >= n_avail) {
785 n_avail += N_RENAME_STEP;
786 if (n_avail == N_RENAME_STEP)
787 renames = (xfs_Rename_t *)
788 malloc(n_avail * sizeof(xfs_Rename_t));
790 renames = (xfs_Rename_t *)
791 realloc((char *)renames,
792 n_avail * sizeof(xfs_Rename_t));
794 Log("Can't %salloc %lu bytes for rename list.\n",
795 (n_avail == N_RENAME_STEP) ? "m" : "re",
796 n_avail * sizeof(xfs_Rename_t));
800 (void)strcpy(renames[n_renames].name, vol_direntp->d_name);
801 renames[n_renames].uniq = info.ili_info.param[2];
807 (&info.ili_info, sizeof(vice_inode_info_t), 1, inodeFile)
809 Log("Error writing inode file for partition %s\n", mountedOn);
815 } /* end while vol_direntp */
818 vol_dirp = (DIR *) 0;
820 Log("Renaming files.\n");
821 if (xfs_RenameFiles(vol_dirname, renames, n_renames) < 0) {
829 free((char *)renames);
831 if (fflush(inodeFile) == EOF) {
832 ("Unable to successfully flush inode file for %s\n", mountedOn);
834 return errors ? -1 : -2;
836 if (fsync(fileno(inodeFile)) == -1) {
837 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
839 return errors ? -1 : -2;
841 if (fclose(inodeFile) == EOF) {
842 Log("Unable to successfully close inode file for %s\n", mountedOn);
843 return errors ? -1 : -2;
846 * Paranoia: check that the file is really the right size
848 if (stat(resultFile, &status) == -1) {
849 Log("Unable to successfully stat inode file for %s\n", partition);
850 return errors ? -1 : -2;
852 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
853 Log("Wrong size (%d instead of %d) in inode file for %s\n",
854 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
856 return errors ? -1 : -2;
861 Log("Errors encontered listing inodes, not salvaging partition.\n");
873 free((char *)renames);
882 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
883 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
884 char *wpath, void *rock)
886 FILE *inodeFile = NULL;
887 char dev[50], rdev[51];
889 struct efs_dinode *p;
890 struct ViceInodeInfo info;
891 struct stat root_inode;
892 int ninodes = 0, err = 0;
893 struct efs_dinode *dinodeBuf = NULL;
895 ino_t imax, inum; /* total number of I-nodes in file system */
899 sleep(1); /* simulate operator */
905 if (stat(mountedOn, &root_inode) < 0) {
906 Log("cannot stat: %s\n", mountedOn);
909 #ifdef AFS_SGI_XFS_IOPS_ENV
910 if (!strcmp("xfs", root_inode.st_fstype)) {
911 return xfs_ListViceInodes(devname, mountedOn, resultFile, judgeInode,
912 judgeParam, forcep, forceR, wpath, rock);
916 Log("%s is not root of a filesystem\n", mountedOn);
921 #else /* AFS_SGI_ENV */
924 #define SPERB (MAXBSIZE / sizeof(short))
925 #define MAXNINDIR (MAXBSIZE / sizeof(daddr_t))
928 struct bufarea *b_next; /* must be first */
932 char b_buf[MAXBSIZE]; /* buffer space */
933 short b_lnks[SPERB]; /* link counts */
934 daddr_t b_indir[MAXNINDIR]; /* indirect block */
935 struct fs b_fs; /* super block */
936 struct cg b_cg; /* cylinder group */
940 typedef struct bufarea BUFAREA;
943 #define sblock sblk.b_un.b_fs
944 #endif /* AFS_HPUX_ENV */
946 extern char *afs_rawname();
948 ListViceInodes(char *devname, char *mountedOn, char *resultFile,
949 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
950 char *wpath, void *rock)
956 #else /* !AFS_AIX_ENV */
961 int i, c, e, bufsize, code, err = 0;
962 FILE *inodeFile = NULL;
963 char dev[50], rdev[100], err1[512], *ptr1;
964 struct dinode *inodes = NULL, *einodes, *dptr;
968 struct ViceInodeInfo info;
971 partition = mountedOn;
972 sprintf(rdev, "%s/%s", wpath, devname);
973 ptr1 = afs_rawname(rdev);
977 /* Bletch: this is terrible; is there a better way to do this? Does this work? vfsck doesn't even sleep!! */
979 sleep(5); /* Trying a smaller one for aix */
984 pfd = afs_open(rdev, O_RDONLY);
986 sprintf(err1, "Could not open device %s to get inode list\n", rdev);
991 if (bread(pfd, (char *)&super.fs, SUPERB, sizeof super.fs) == -1) {
994 if (bread(pfd, (char *)&sblock, SBLOCK, SBSIZE) == -1) {
996 if (bread(pfd, super.block, SBLOCK, SBSIZE) == -1) {
997 #endif /* AFS_HPUX_ENV */
999 Log("Unable to read superblock, partition %s\n", partition);
1004 inodeFile = fopen(resultFile, "w");
1005 if (inodeFile == NULL) {
1006 Log("Unable to create inode description file %s\n", resultFile);
1012 * char *FSlabel(), *fslabel=0;
1013 * fslabel = FSlabel(&super.fs);
1015 if (super.fs.s_bsize == 0)
1016 super.fs.s_bsize = 512;
1017 if (super.fs.s_bsize != BSIZE) {
1018 Log("SuperBlk: Cluster size not %d; run vfsck\n", BSIZE);
1021 fmax = super.fs.s_fsize; /* first invalid blk num */
1022 imax = ((ino_t) super.fs.s_isize - (SUPERB + 1)) * INOPB;
1024 Log("Size check: imax==0!\n");
1027 if (GetAuxInodeFile(partition, &status) == 0) {
1028 Log("Can't access Aux inode file for partition %s, aborting\n",
1032 for (inum = 1; inum <= imax; inum++) {
1033 struct dauxinode *auxp;
1034 if ((auxp = IsAfsInode(inum)) == NULL) {
1035 /* Not an afs inode, keep going */
1038 if ((p = ginode(inum)) == NULL)
1040 /* deleted/non-existent inode when di_mode == 0 */
1043 info.inodeNumber = (int)inum;
1044 info.byteCount = p->di_size;
1045 info.linkCount = p->di_nlink;
1046 info.u.param[0] = auxp->aux_param1;
1047 info.u.param[1] = auxp->aux_param2;
1048 info.u.param[2] = auxp->aux_param3;
1049 info.u.param[3] = auxp->aux_param4;
1050 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1053 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1054 Log("Error writing inode file for partition %s\n", partition);
1062 * run a few consistency checks of the superblock
1063 * (Cribbed from vfsck)
1066 #if defined(FD_FSMAGIC)
1067 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)
1068 && (sblock.fs_magic != FD_FSMAGIC)
1069 #if defined(AFS_HPUX101_ENV)
1070 && (sblock.fs_magic != FD_FSMAGIC_2)
1074 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)) {
1076 Log("There's something wrong with the superblock for partition %s; bad magic (%d) run vfsck\n", partition, sblock.fs_magic);
1079 if (sblock.fs_ncg < 1) {
1080 Log("There's something wrong with the superblock for partition %s; NCG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_ncg);
1083 if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG) {
1084 Log("There's something wrong with the superblock for partition %s; CPG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_cpg);
1087 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl
1088 || (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) {
1089 Log("There's something wrong with the superblock for partition %s; NCYL LESS THAN NCG*CPG run vfsck\n", partition);
1092 if (sblock.fs_sbsize > SBSIZE) {
1093 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);
1097 if ((super.fs.fs_magic != FS_MAGIC)
1098 || (super.fs.fs_ncg < 1)
1099 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1100 || (super.fs.fs_cpg < 1)
1102 || (super.fs.fs_cpg < 1 || super.fs.fs_cpg > MAXCPG)
1104 || (super.fs.fs_ncg * super.fs.fs_cpg < super.fs.fs_ncyl
1105 || (super.fs.fs_ncg - 1) * super.fs.fs_cpg >= super.fs.fs_ncyl)
1106 || (super.fs.fs_sbsize > SBSIZE)) {
1107 Log("There's something wrong with the superblock for partition %s; run vfsck\n", partition);
1110 #endif /* AFS_HPUX_ENV */
1113 bufsize = sblock.fs_ipg * sizeof(struct dinode);
1115 bufsize = super.fs.fs_ipg * sizeof(struct dinode);
1116 #endif /* AFS_HPUX_ENV */
1117 inodes = (struct dinode *)malloc(bufsize);
1118 einodes = (struct dinode *)(((char *)inodes) + bufsize);
1119 if (inodes == NULL) {
1120 Log("Unable to allocate enough memory to scan inodes; help!\n");
1123 Log("Scanning inodes on device %s...\n", rdev);
1125 for (c = 0; c < sblock.fs_ncg; c++) {
1126 i = c * sblock.fs_ipg;
1127 e = i + sblock.fs_ipg;
1128 #if defined(AFS_HPUX102_ENV)
1129 if (afs_lseek(pfd, dbtoo(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1132 if (afs_lseek(pfd, dbtob(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1136 for (c = 0; c < super.fs.fs_ncg; c++) {
1138 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1140 #if defined(AFS_DARWIN_ENV)
1141 #define offset_t off_t
1142 #define llseek lseek
1145 #endif /* AFS_SUN5_ENV */
1146 i = c * super.fs.fs_ipg;
1147 e = i + super.fs.fs_ipg;
1149 dblk1 = fsbtodb(&super.fs, itod(&super.fs, i));
1150 if (afs_lseek(pfd, (off_t) ((off_t) dblk1 * DEV_BSIZE), L_SET) == -1) {
1152 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1153 f1 = fsbtodb(&super.fs, itod(&super.fs, i));
1154 off = (offset_t) f1 << DEV_BSHIFT;
1155 if (llseek(pfd, off, L_SET) == -1) {
1157 if (afs_lseek(pfd, dbtob(fsbtodb(&super.fs, itod(&super.fs, i))), L_SET)
1159 #endif /* AFS_SUN5_ENV */
1160 #endif /* AFS_OSF_ENV */
1161 #endif /* AFS_HPUX_ENV */
1162 Log("Error reading inodes for partition %s; run vfsck\n",
1168 if (read(pfd, inodes, bufsize) != bufsize) {
1169 Log("Error reading inodes for partition %s; run vfsck\n",
1174 register int bj, bk;
1176 for (bj = bk = 0; bj < bufsize; bj = bj + 512, bk++) {
1177 if ((code = read(pfd, dptr, 512)) != 512) {
1178 Log("Error reading inode %d? for partition %s (errno = %d); run vfsck\n", bk + i, partition, errno);
1179 if (afs_lseek(pfd, 512, L_SET) == -1) {
1180 Log("Lseek failed\n");
1195 for (p = inodes; p < einodes && i < e; i++, p++) {
1197 Log("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1198 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1199 p->di_size, p->di_nlink);
1201 ("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1202 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1203 p->di_size, p->di_nlink);
1206 #ifdef AFS_3DISPARES
1207 /* Check to see if this inode is a pre-"OSF1 4.0D" inode */
1208 if ((p->di_uid || p->di_gid)
1209 && !(p->di_flags & (IC_XUID | IC_XGID))) {
1210 Log("Found unconverted inode %d: Use 'fs_conv_dux40D convert' on partition %s\n", i, partition);
1214 assert(0); /* define AFS_3DISPARES in param.h */
1217 #if defined(AFS_SUN56_ENV)
1218 /* if this is a pre-sol2.6 unconverted inode, bail out */
1220 afs_uint32 p1, p2, p3, p4;
1224 q = (quad *) & (p->di_ic.ic_lsize);
1226 p2 = p->di_ic.ic_flags;
1228 p4 = p->di_ic.ic_uid;
1229 p5 = p->di_ic.ic_gid;
1231 if ((p2 || p3) && !p4 && (p5 == -2)) {
1232 Log("Found unconverted inode %d\n", i);
1233 Log("You should run the AFS file conversion utility\n");
1238 if (IS_DVICEMAGIC(p) && (p->di_mode & IFMT) == IFREG) {
1239 afs_uint32 p2 = p->di_vicep2, p3 = DI_VICEP3(p);
1241 info.u.param[0] = p->di_vicep1;
1242 #ifdef AFS_3DISPARES
1243 if (((p2 >> 3) == INODESPECIAL) && (p2 & 0x3)) {
1244 info.u.param[1] = INODESPECIAL;
1245 info.u.param[2] = p3;
1246 info.u.param[3] = p2 & 0x3;
1248 info.u.param[1] = ((p2 >> 27) << 16) + (p3 & 0xffff);
1249 info.u.param[2] = (p2 & 0x3fffff);
1251 (((p2 >> 22) & 0x1f) << 16) + (p3 >> 16);
1254 info.u.param[1] = p->di_vicep2;
1255 info.u.param[2] = DI_VICEP3(p);
1256 info.u.param[3] = p->di_vicep4;
1258 info.inodeNumber = i;
1259 info.byteCount = p->di_size;
1260 info.linkCount = p->di_nlink;
1261 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1264 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1265 Log("Error writing inode file for partition %s\n",
1279 if (fflush(inodeFile) == EOF) {
1280 Log("Unable to successfully flush inode file for %s\n", partition);
1284 if (fsync(fileno(inodeFile)) == -1) {
1285 Log("Unable to successfully fsync inode file for %s\n", partition);
1289 if (fclose(inodeFile) == EOF) {
1290 Log("Unable to successfully close inode file for %s\n", partition);
1296 * Paranoia: check that the file is really the right size
1298 if (stat(resultFile, &status) == -1) {
1299 Log("Unable to successfully stat inode file for %s\n", partition);
1303 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1304 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1305 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1324 #endif /* !AFS_SGI_ENV */
1325 #endif /* !AFS_AIX31_ENV */
1327 #ifdef AFS_DARWIN_ENV
1329 #define dbtob(db) ((unsigned)(db) << DEV_BSHIFT)
1333 bread(int fd, char *buf, daddr_t blk, afs_int32 size)
1336 #ifdef AFS_AIX41_ENV
1337 offset_t off = (offset_t) blk << FSBSHIFT;
1338 if (llseek(fd, off, 0) < 0) {
1339 Log("Unable to seek to offset %llu for block %u\n", off, blk);
1342 #else /* AFS_AIX41_ENV */
1343 if (afs_lseek(fd, blk * Bsize, 0) < 0) {
1344 Log("Unable to seek to offset %u for block %u\n", blk * Bsize, blk);
1346 #endif /* AFS_AIX41_ENV */
1348 if (afs_lseek(fd, (off_t) dbtob(blk), L_SET) < 0) {
1349 Log("Unable to seek to offset %u for block %u\n", dbtob(blk), blk);
1352 if (read(fd, buf, size) != size) {
1353 Log("Unable to read block %d, partition %s\n", blk, partition);
1359 #endif /* AFS_LINUX20_ENV */
1361 convertVolumeInfo(int fdr, int fdw, afs_uint32 vid)
1363 struct VolumeDiskData vd;
1366 if (read(fdr, &vd, sizeof(struct VolumeDiskData)) !=
1367 sizeof(struct VolumeDiskData)) {
1368 Log("1 convertiVolumeInfo: read failed for %lu with code %d\n", vid,
1372 vd.restoredFromId = vd.id; /* remember the RO volume here */
1374 vd.id = vd.parentId;
1378 vd.uniquifier += 5000; /* just in case there are still file copies
1379 from the old RW volume around */
1381 p = strrchr(vd.name, '.');
1382 if (p && !strcmp(p, ".readonly")) {
1386 if (write(fdw, &vd, sizeof(struct VolumeDiskData)) !=
1387 sizeof(struct VolumeDiskData)) {
1388 Log("1 convertiVolumeInfo: write failed for %lu with code %d\n", vid,
1396 afs_int32 inodeType;
1403 UpdateThisVolume(struct ViceInodeInfo *inodeinfo, VolumeId singleVolumeNumber,
1404 struct specino *specinos)
1407 if ((inodeinfo->u.vnode.vnodeNumber == INODESPECIAL) &&
1408 (inodeinfo->u.vnode.volumeId == singleVolumeNumber)) {
1409 specinos[inodeinfo->u.special.type].inodeNumber =
1410 inodeinfo->inodeNumber;
1412 return 0; /* We aren't using a result file, we're caching */
1416 getDevName(char *pbuffer, char *wpath)
1418 char pbuf[128], *ptr;
1419 strcpy(pbuf, pbuffer);
1420 ptr = (char *)strrchr(pbuf, '/');
1423 strcpy(wpath, pbuf);
1426 ptr = (char *)strrchr(pbuffer, '/');
1428 strcpy(pbuffer, ptr + 1);
1434 #ifdef FSSYNC_BUILD_CLIENT
1436 inode_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
1438 char dir_name[512], oldpath[512], newpath[512];
1440 char headername[16];
1442 int fd, err, forcep, len, j, code;
1444 struct DiskPartition64 *partP;
1445 struct ViceInodeInfo info;
1446 struct VolumeDiskHeader h;
1447 IHandle_t *ih, *ih2;
1448 FdHandle_t *fdP, *fdP2;
1450 char tmpDevName[100];
1452 struct specino specinos[VI_LINKTABLE+1];
1453 Inode nearInode = 0;
1455 memset(&specinos, 0, sizeof(specinos));
1457 (void)afs_snprintf(headername, sizeof headername, VFORMAT, afs_cast_uint32(volumeId));
1458 (void)afs_snprintf(oldpath, sizeof oldpath, "%s/%s", pname, headername);
1459 fd = open(oldpath, O_RDONLY);
1461 Log("1 inode_ConvertROtoRWvolume: Couldn't open header for RO-volume %lu.\n", volumeId);
1464 if (read(fd, &h, sizeof(h)) != sizeof(h)) {
1465 Log("1 inode_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n", volumeId);
1470 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
1472 /* now do the work */
1474 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
1475 partP = partP->next);
1477 Log("1 inode_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
1481 strcpy(tmpDevName, partP->devName);
1482 name = getDevName(tmpDevName, wpath);
1484 if ((err = ListViceInodes(name, VPartitionPath(partP),
1485 NULL, UpdateThisVolume, volumeId,
1486 &forcep, 0, wpath, &specinos)) < 0)
1488 Log("1 inode_ConvertROtoRWvolume: Couldn't get special inodes\n");
1492 #if defined(NEARINODE_HINT)
1493 nearInodeHash(volumeId, nearInode);
1494 nearInode %= partP->f_files;
1497 for (j = VI_VOLINFO; j < VI_LINKTABLE+1; j++) {
1498 if (specinos[j].inodeNumber > 0) {
1499 specinos[j].ninodeNumber =
1500 IH_CREATE(NULL, partP->device, VPartitionPath(partP),
1501 nearInode, h.parent, INODESPECIAL, j, h.parent);
1502 IH_INIT(ih, partP->device, volumeId,
1503 specinos[j].inodeNumber);
1506 Log("1 inode_ConvertROtoRWvolume: Couldn't find special inode %d for %d\n", j, volumeId);
1510 IH_INIT(ih2, partP->device, h.parent, specinos[j].ninodeNumber);
1511 fdP2 = IH_OPEN(ih2);
1513 Log("1 inode_ConvertROtoRWvolume: Couldn't find special inode %d for %d\n", j, h.parent);
1517 if (j == VI_VOLINFO)
1518 convertVolumeInfo(fdP->fd_fd, fdP2->fd_fd, ih2->ih_vid);
1521 len = read(fdP->fd_fd, buffer, sizeof(buffer));
1526 code = write(fdP2->fd_fd, buffer, len);
1540 #ifdef AFS_64BIT_IOPS_ENV
1541 h.volumeInfo_lo = (afs_int32)specinos[VI_VOLINFO].ninodeNumber & 0xffffffff;
1542 h.volumeInfo_hi = (afs_int32)(specinos[VI_VOLINFO].ninodeNumber >> 32) && 0xffffffff;
1543 h.smallVnodeIndex_lo = (afs_int32)specinos[VI_SMALLINDEX].ninodeNumber & 0xffffffff;
1544 h.smallVnodeIndex_hi = (afs_int32)(specinos[VI_SMALLINDEX].ninodeNumber >> 32) & 0xffffffff;
1545 h.largeVnodeIndex_lo = (afs_int32)specinos[VI_LARGEINDEX].ninodeNumber & 0xffffffff;
1546 h.largeVnodeIndex_hi = (afs_int32)(specinos[VI_LARGEINDEX].ninodeNumber >> 32) & 0xffffffff;
1547 if (specinos[VI_LINKTABLE].ninodeNumber) {
1548 h.linkTable_lo = (afs_int32)specinos[VI_LINKTABLE].ninodeNumber & 0xffffffff;
1549 h.linkTable_hi = (afs_int32)specinos[VI_LINKTABLE].ninodeNumber & 0xffffffff;
1552 h.volumeInfo_lo = specinos[VI_VOLINFO].ninodeNumber;
1553 h.smallVnodeIndex_lo = specinos[VI_SMALLINDEX].ninodeNumber;
1554 h.largeVnodeIndex_lo = specinos[VI_LARGEINDEX].ninodeNumber;
1555 if (specinos[VI_LINKTABLE].ninodeNumber) {
1556 h.linkTable_lo = specinos[VI_LINKTABLE].ninodeNumber;
1560 (void)afs_snprintf(headername, sizeof headername, VFORMAT, afs_cast_uint32(h.id));
1561 (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", pname, headername);
1562 fd = open(newpath, O_CREAT | O_EXCL | O_RDWR, 0644);
1564 Log("1 inode_ConvertROtoRWvolume: Couldn't create header for RW-volume %lu.\n", h.id);
1567 if (write(fd, &h, sizeof(h)) != sizeof(h)) {
1568 Log("1 inode_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu.\n", h.id);
1573 if (unlink(oldpath) < 0) {
1574 Log("1 inode_ConvertROtoRWvolume: Couldn't unlink RO header, error = %d\n", errno);
1576 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
1577 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
1580 #endif /* FSSYNC_BUILD_CLIENT */
1581 #endif /* AFS_NAMEI_ENV */