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, FILE *inodeFile,
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 #include <afs/afs_assert.h>
103 #if defined(AFS_HPUX101_ENV)
110 #include "volinodes.h"
111 #include "partition.h"
113 #include "volume_inline.h"
115 /* Notice: parts of this module have been cribbed from vfsck.c */
118 static char *partition;
123 #include <jfs/filsys.h>
126 #define FSBSIZE (4096) /* filesystem block size */
127 #define FSBSHIFT (12) /* log2(FSBSIZE) */
128 #define FSBMASK (FSBSIZE - 1) /* FSBSIZE mask */
130 #define MIN_FSIZE DISKMAP_B /* minimum fs size (FSblocks) */
131 #define LAST_RSVD_I 15 /* last reserved inode */
136 * This will hopefully eventually make it into the system include files
138 #define INOPB (FSBSIZE / sizeof (struct dinode))
146 #endif /* AFS_AIX41_ENV */
149 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
150 XX This was lifted from some `com/cmd/fs/fshlpr_aix3/Fs.h', which indicated X
151 XX a longing to see it make it into a readily accessible include file. XXXXXX
152 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
154 * itoo - inode number to offset within disk block
157 #define itoo(x) (int) ((unsigned)(x) % INOPB)
159 int Bsize = FSBSIZE; /* block size for this system */
160 daddr_t fmax; /* total number of blocks n file system */
161 ino_t imax, inum; /* total number of I-nodes in file system */
163 static struct superblock fs;
164 struct dinode *ginode();
168 ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
169 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
170 char *wpath, void *rock)
172 char dev[50], rdev[51];
175 struct ViceInodeInfo info;
176 struct stat root_inode;
177 int ninodes = 0, err = 0;
179 pfd = -1; /* initialize so we don't close on error output below. */
182 sleep(1); /* simulate operator */
188 partition = mountedOn;
189 sprintf(dev, "/dev/%s", devname);
190 sprintf(rdev, "/dev/r%s", devname);
192 if (stat(mountedOn, &root_inode) < 0) {
193 Log("cannot stat: %s\n", mountedOn);
197 if (root_inode.st_ino != ROOTDIR_I) {
198 Log("%s is not root of a filesystem\n", mountedOn);
204 * done with the superblock, now try to read the raw device.
206 if (ReadSuper(&fs, dev) < 0)
211 case FM_CLEAN: /* clean and unmounted */
212 Log("Most peculiar - Super blk in FM_CLEAN state!\n");
214 case FM_MOUNT: /* mounted cleanly */
217 case FM_MDIRTY: /* dirty when mounted or commit fail */
218 case FM_LOGREDO: /* log redo attempted but failed */
219 Log("File system %s is in a bad state.\n", rdev);
220 Log("Call your IBM representative.\n");
224 if (IsBigFilesFileSystem(&fs, (char *)0)) {
225 Log("%s is a big files filesystem, can't salvage.\n", mountedOn);
229 if (strncmp(fs.s_magic, fsv3magic, strlen(fsv3magic)) != 0) {
231 if ((strncmp(fs.s_magic, fsv3pmagic, strlen(fsv3pmagic)) != 0)
232 || (fs.s_version != fsv3pvers)) {
233 Log("Super block doesn't have the problem magic (%s vs v3magic %s v3pmagic %s)\n", fs.s_magic, fsv3magic, fsv3pmagic);
237 Log("Super block doesn't have the problem magic (%s vs v3magic %s)\n",
238 fs.s_magic, fsv3magic);
245 fragsize = (fs.s_fragsize) ? fs.s_fragsize : FSBSIZE;
246 iagsize = (fs.s_iagsize) ? fs.s_iagsize : fs.s_agsize;
247 ag512 = fragsize * fs.s_agsize / 512;
248 agblocks = fragsize * fs.s_agsize >> BSHIFT;
249 #endif /* AFS_AIX41_ENV */
251 fmax = fs.s_fsize / (FSBSIZE / 512); /* first invalid blk num */
253 pfd = afs_open(rdev, O_RDONLY);
255 Log("Unable to open `%s' inode for reading\n", rdev);
260 * calculate the maximum number of inodes possible
263 imax = iagsize * (fs.s_fsize / ag512) - 1;
264 #else /* AFS_AIX41_ENV */
266 ((fmax / fs.s_agsize +
267 ((fmax % fs.s_agsize) >= fs.s_agsize / INOPB ? 1 : 0))
269 #endif /* AFS_AIX41_ENV */
272 * check for "FORCESALVAGE" equivalent:
273 * LAST_RSVD_I is a vice inode, with dead beef, and
274 * di_nlink == 2 to indicate the FORCE.
276 osi_Assert(p = ginode(LAST_RSVD_I));
278 if (p->di_vicemagic == VICEMAGIC && p->di_vicep1 == 0xdeadbeef
279 && p->di_nlink == 2) {
281 idec(root_inode.st_dev, LAST_RSVD_I, 0xdeadbeef);
284 for (inum = LAST_RSVD_I + 1; inum <= imax; ++inum) {
285 if ((p = ginode(inum)) == NULL || p->di_vicemagic != VICEMAGIC
286 || (p->di_mode & IFMT) != IFREG)
289 info.inodeNumber = inum;
290 info.byteCount = p->di_size;
291 info.linkCount = p->di_nlink;
292 info.u.param[0] = p->di_vicep1;
293 info.u.param[1] = p->di_vicep2;
294 info.u.param[2] = p->di_vicep3;
295 info.u.param[3] = p->di_vicep4;
297 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
301 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
302 Log("Error writing inode file for partition %s\n", partition);
310 if (fflush(inodeFile) == EOF) {
311 Log("Unable to successfully flush inode file for %s\n", partition);
315 if (fsync(fileno(inodeFile)) == -1) {
316 Log("Unable to successfully fsync inode file for %s\n", partition);
322 * Paranoia: check that the file is really the right size
324 if (fstat(fileno(inodeFile), &status) == -1) {
325 Log("Unable to successfully stat inode file for %s\n", partition);
329 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
330 Log("Wrong size (%d instead of %d) in inode file for %s\n",
331 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
349 /* Read in the superblock for devName */
351 ReadSuper(struct superblock *fs, char *devName)
355 pfd = afs_open(devName, O_RDONLY);
357 Log("Unable to open inode on %s for reading superblock.\n", devName);
361 if (bread(pfd, fs, SUPER_B, sizeof(struct superblock)) < 0) {
362 Log("Unable to read superblock on %s.\n", devName);
370 /* IsBigFilesFileSystem returns 1 if it's a big files filesystem, 0 otherwise. */
372 IsBigFilesFileSystem(struct superblock *sb)
374 if ((strncmp(sb->s_magic, fsv3pmagic, 4) == 0)
375 && (sb->s_version == fsbigfile)
389 static char buf[FSBSIZE];
390 static daddr_t last_blk = -1;
396 0) ? INODES_B + inum / INOPB : ag * agblocks + (inum -
399 #else /* AFS_AIX41_ENV */
400 ag = inum / fs.s_agsize;
403 0) ? INODES_B + inum / INOPB : ag * fs.s_agsize + (inum -
407 #endif /* AFS_AIX41_ENV */
409 if (last_blk != pblk) {
410 if (bread(pfd, buf, pblk, sizeof(buf)) < 0) {
417 dp = (struct dinode *)buf;
422 #else /* !AFS_AIX31_ENV */
424 #if defined(AFS_SGI_ENV)
426 /* libefs.h includes <assert.h>, which we don't want */
429 #ifdef AFS_SGI_XFS_IOPS_ENV
431 #include <afs/xfsattrs.h>
432 /* xfs_ListViceInodes
434 * xfs_ListViceInodes verifies and correct the XFS namespace as it collects
435 * the inode information. The name is required for the idec operation to work.
436 * Steps 2 and 3 below are for the AFS_XFS_NAME_VERS == 1. If the name space
437 * changes, the algorithm will need to change.
438 * 1) If the parent inode number does not match the directory's inod number,
439 * change it in the attribute.
440 * 2) If the unqifier in the attribute does not match the name, rename the
441 * file. This is done by doing an exclusive open, incrementing the tag
442 * number until a file can be created. If the tag changes, then the
443 * attribute will need updating.
444 * 3) If the tag in the attribute does not match the name, change the
446 * 4) Verify uid = RW volume id and gid = XFS_VICEMAGIC.
452 * Does the verifications listed above.
453 * We can't change the names until the readdir is complete, so we set the
454 * rename flag if the file needs renaming.
457 xfs_VerifyInode(char *dir, uint64_t pino, char *name, i_list_inode_t * info,
464 int update_chown = 0;
472 (void)sprintf(path, "%s/%s", dir, name);
473 /* Verify uid and gid fields */
474 if (info->ili_magic != XFS_VICEMAGIC) {
475 Log("%s magic for %s/%s (inode %s) from %d to %d\n",
476 Testing ? "Would have changed" : "Changing", dir, name,
477 PrintInode(stmp, info->ili_info.inodeNumber), info->ili_magic,
483 vno = info->ili_info.param[0];
484 if (info->ili_vno != AFS_XFS_VNO_CLIP(vno)) {
485 Log("%s volume id for %s/%s (inode %s) from %d to %d\n",
486 Testing ? "Would have changed" : "Changing", dir, name,
487 PrintInode(stmp, info->ili_info.inodeNumber), info->ili_vno,
488 AFS_XFS_VNO_CLIP(vno));
494 if (chown(path, AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC) < 0) {
495 Log("Can't chown %s to uid=%d, gid=0x%x\n", path,
496 AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC);
501 /* Check Parent inode number. */
502 if (info->ili_pino != pino) {
503 afs_ino_str_t sino, sipino, spino;
504 (void)PrintInode(sino, info->ili_info.inodeNumber);
505 (void)PrintInode(sipino, info->ili_pino);
506 (void)PrintInode(spino, pino);
507 Log("%s parent ino for %s (inode %s) from %s to %s.\n",
508 Testing ? "Would have changed" : "Changing", path, sino, sipino,
514 /* Verify the file name. */
515 (void)strcpy(tmpName, ".");
516 (void)strcat(tmpName, int_to_base64(stmp, info->ili_info.param[2]));
517 if (strncmp(name, tmpName, strlen(tmpName))) {
518 Log("%s name %s (inode %s) in directory %s, unique=%d, tag=%d\n",
519 Testing ? "Would have returned bad" : "Bad", name,
520 PrintInode(stmp, info->ili_info.inodeNumber), dir,
521 info->ili_info.param[2], info->ili_tag);
527 /* update the tag? */
528 (void)strcat(tmpName, ".");
529 (void)strcat(tmpName, int_to_base64(stmp, info->ili_tag));
530 if (strcmp(name, tmpName)) {
532 (void)strcpy(tmpName, name);
533 p = strchr(tmpName + 1, '.');
535 Log("No tag found on name %s (inode %s)in directory, %s.\n",
536 name, PrintInode(stmp, info->ili_info.inodeNumber), dir,
537 Testing ? "would have renamed" : "will rename");
541 tag = base64_to_int(p + 1);
542 Log("%s the tag for %s (inode %s) from %d to %d.\n",
543 Testing ? "Would have changed" : "Will change", path,
544 PrintInode(stmp, info->ili_info.inodeNumber), dir, tag,
552 if (update_pino || update_tag) {
553 afs_xfs_attr_t attrs;
556 length = SIZEOF_XFS_ATTR_T;
557 if (attr_get(path, AFS_XFS_ATTR, (char *)&attrs, &length, ATTR_ROOT) <
559 Log("Can't get AFS attribute for %s\n", path);
563 attrs.at_pino = pino;
567 (path, AFS_XFS_ATTR, (char *)&attrs, length,
568 ATTR_ROOT | ATTR_REPLACE) < 0) {
569 Log("Can't set AFS attribute into %s\n", path);
583 xfs_RenameFiles(char *dir, xfs_Rename_t * renames, int n_renames)
586 char opath[128], nbase[128], npath[128];
587 afs_xfs_attr_t attrs;
588 int length = SIZEOF_XFS_ATTR_T;
593 for (i = 0; i < n_renames; i++) {
594 (void)sprintf(opath, "%s/%s", dir, renames[i].name);
595 (void)sprintf(nbase, "%s/.%s", dir,
596 int_to_base64(stmp, renames[i].uniq));
597 for (tag = 2, j = 0; j < 64; tag++, j++) {
598 (void)sprintf(npath, "%s.%s", nbase, int_to_base64(stmp, tag));
599 fd = afs_open(npath, O_CREAT | O_EXCL | O_RDWR, 0);
606 Log("Can't find a new name for %s\n", opath);
609 if (rename(opath, npath) < 0) {
610 Log("Can't rename %s to %s\n", opath, npath);
613 Log("Renamed %s to %s\n", opath, npath);
620 xfs_ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
621 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep,
622 int forceR, char *wpath, void *rock)
625 int info_size = sizeof(i_list_inode_t);
628 dirent64_t *top_direntp;
630 dirent64_t *vol_direntp;
631 struct stat64 sdirbuf;
632 struct stat64 sfilebuf;
633 afs_xfs_attr_t attrs;
634 afs_xfs_dattr_t dattrs;
636 char vol_dirname[1024];
639 xfs_Rename_t *renames = (xfs_Rename_t *) 0;
641 #define N_RENAME_STEP 64
650 if (stat64(mountedOn, &sdirbuf) < 0) {
651 perror("xfs_ListViceInodes: stat64");
655 if ((top_dirp = opendir(mountedOn)) == NULL) {
656 Log("Can't open directory %s to read inodes.\n", mountedOn);
660 while (top_direntp = readdir64(top_dirp)) {
661 /* Only descend directories with the AFSDIR attribute set.
662 * Could also verify the contents of the atribute, but currently
664 * Performance could be improved for single volume salvages by
665 * only going through the directory containing the volume's inodes.
666 * But I'm being complete as a first pass.
668 (void)sprintf(vol_dirname, "%s/%s", mountedOn, top_direntp->d_name);
669 length = SIZEOF_XFS_DATTR_T;
671 (vol_dirname, AFS_XFS_DATTR, (char *)&dattrs, &length, ATTR_ROOT))
674 if ((vol_dirp = opendir(vol_dirname)) == NULL) {
675 if (errno == ENOTDIR)
677 Log("Can't open directory %s to read inodes.\n", vol_dirname);
681 pino = top_direntp->d_ino;
683 while (vol_direntp = readdir64(vol_dirp)) {
684 if (vol_direntp->d_name[1] == '\0'
685 || vol_direntp->d_name[1] == '.')
688 info.ili_version = AFS_XFS_ILI_VERSION;
689 info_size = sizeof(i_list_inode_t);
691 ilistinode64(sdirbuf.st_dev, vol_direntp->d_ino, &info,
694 /* Where possible, give more explicit messages. */
698 Log("%s (device id %d) is not on an XFS filesystem.\n",
699 vol_dirname, sdirbuf.st_dev);
704 if (info_size != sizeof(i_list_inode_t)
705 || info.ili_version != AFS_XFS_ILI_VERSION) {
706 Log("Version skew between kernel and salvager.\n");
711 /* Continue, so we collect all the errors in the first pass. */
712 Log("Error listing inode named %s/%s: %s\n", vol_dirname,
713 vol_direntp->d_name, strerror(errno));
718 if (info.ili_attr_version != AFS_XFS_ATTR_VERS) {
719 Log("Unrecognized XFS attribute version %d in %s/%s. Upgrade salvager\n", info.ili_attr_version, vol_dirname, vol_direntp->d_name);
723 if (judgeInode && (*judgeInode) (&info.ili_info, judgeParam, rock) == 0)
728 (vol_dirname, pino, vol_direntp->d_name, &info,
734 /* Add this name to the list of items to rename. */
735 if (n_renames >= n_avail) {
736 n_avail += N_RENAME_STEP;
737 if (n_avail == N_RENAME_STEP)
738 renames = (xfs_Rename_t *)
739 malloc(n_avail * sizeof(xfs_Rename_t));
741 renames = (xfs_Rename_t *)
742 realloc((char *)renames,
743 n_avail * sizeof(xfs_Rename_t));
745 Log("Can't %salloc %lu bytes for rename list.\n",
746 (n_avail == N_RENAME_STEP) ? "m" : "re",
747 n_avail * sizeof(xfs_Rename_t));
751 (void)strcpy(renames[n_renames].name, vol_direntp->d_name);
752 renames[n_renames].uniq = info.ili_info.param[2];
758 (&info.ili_info, sizeof(vice_inode_info_t), 1, inodeFile)
760 Log("Error writing inode file for partition %s\n", mountedOn);
766 } /* end while vol_direntp */
769 vol_dirp = (DIR *) 0;
771 Log("Renaming files.\n");
772 if (xfs_RenameFiles(vol_dirname, renames, n_renames) < 0) {
780 free((char *)renames);
782 if (fflush(inodeFile) == EOF) {
783 ("Unable to successfully flush inode file for %s\n", mountedOn);
784 return errors ? -1 : -2;
786 if (fsync(fileno(inodeFile)) == -1) {
787 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
788 return errors ? -1 : -2;
791 * Paranoia: check that the file is really the right size
793 if (fstat(fileno(inodeFile), &status) == -1) {
794 Log("Unable to successfully stat inode file for %s\n", partition);
795 return errors ? -1 : -2;
797 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
798 Log("Wrong size (%d instead of %d) in inode file for %s\n",
799 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
801 return errors ? -1 : -2;
806 Log("Errors encontered listing inodes, not salvaging partition.\n");
818 free((char *)renames);
825 ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
826 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
827 char *wpath, void *rock)
829 char dev[50], rdev[51];
831 struct efs_dinode *p;
832 struct ViceInodeInfo info;
833 struct stat root_inode;
834 int ninodes = 0, err = 0;
835 struct efs_dinode *dinodeBuf = NULL;
837 ino_t imax, inum; /* total number of I-nodes in file system */
841 sleep(1); /* simulate operator */
847 if (stat(mountedOn, &root_inode) < 0) {
848 Log("cannot stat: %s\n", mountedOn);
851 #ifdef AFS_SGI_XFS_IOPS_ENV
852 if (!strcmp("xfs", root_inode.st_fstype)) {
853 return xfs_ListViceInodes(devname, mountedOn, inodeFile, judgeInode,
854 judgeParam, forcep, forceR, wpath, rock);
858 Log("%s is not root of a filesystem\n", mountedOn);
863 #else /* AFS_SGI_ENV */
866 #define SPERB (MAXBSIZE / sizeof(short))
867 #define MAXNINDIR (MAXBSIZE / sizeof(daddr_t))
870 struct bufarea *b_next; /* must be first */
874 char b_buf[MAXBSIZE]; /* buffer space */
875 short b_lnks[SPERB]; /* link counts */
876 daddr_t b_indir[MAXNINDIR]; /* indirect block */
877 struct fs b_fs; /* super block */
878 struct cg b_cg; /* cylinder group */
882 typedef struct bufarea BUFAREA;
885 #define sblock sblk.b_un.b_fs
886 #endif /* AFS_HPUX_ENV */
888 extern char *afs_rawname();
890 ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
891 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
892 char *wpath, void *rock)
898 #else /* !AFS_AIX_ENV */
903 int i, c, e, bufsize, code, err = 0;
904 char dev[50], rdev[100], err1[512], *ptr1;
905 struct dinode *inodes = NULL, *einodes, *dptr;
909 struct ViceInodeInfo info;
912 partition = mountedOn;
913 sprintf(rdev, "%s/%s", wpath, devname);
914 ptr1 = afs_rawname(rdev);
918 /* Bletch: this is terrible; is there a better way to do this? Does this work? vfsck doesn't even sleep!! */
920 sleep(5); /* Trying a smaller one for aix */
925 pfd = afs_open(rdev, O_RDONLY);
927 sprintf(err1, "Could not open device %s to get inode list\n", rdev);
932 if (bread(pfd, (char *)&super.fs, SUPERB, sizeof super.fs) == -1) {
935 if (bread(pfd, (char *)&sblock, SBLOCK, SBSIZE) == -1) {
937 if (bread(pfd, super.block, SBLOCK, SBSIZE) == -1) {
938 #endif /* AFS_HPUX_ENV */
940 Log("Unable to read superblock, partition %s\n", partition);
946 * char *FSlabel(), *fslabel=0;
947 * fslabel = FSlabel(&super.fs);
949 if (super.fs.s_bsize == 0)
950 super.fs.s_bsize = 512;
951 if (super.fs.s_bsize != BSIZE) {
952 Log("SuperBlk: Cluster size not %d; run vfsck\n", BSIZE);
955 fmax = super.fs.s_fsize; /* first invalid blk num */
956 imax = ((ino_t) super.fs.s_isize - (SUPERB + 1)) * INOPB;
958 Log("Size check: imax==0!\n");
961 if (GetAuxInodeFile(partition, &status) == 0) {
962 Log("Can't access Aux inode file for partition %s, aborting\n",
966 for (inum = 1; inum <= imax; inum++) {
967 struct dauxinode *auxp;
968 if ((auxp = IsAfsInode(inum)) == NULL) {
969 /* Not an afs inode, keep going */
972 if ((p = ginode(inum)) == NULL)
974 /* deleted/non-existent inode when di_mode == 0 */
977 info.inodeNumber = (int)inum;
978 info.byteCount = p->di_size;
979 info.linkCount = p->di_nlink;
980 info.u.param[0] = auxp->aux_param1;
981 info.u.param[1] = auxp->aux_param2;
982 info.u.param[2] = auxp->aux_param3;
983 info.u.param[3] = auxp->aux_param4;
984 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
987 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
988 Log("Error writing inode file for partition %s\n", partition);
996 * run a few consistency checks of the superblock
997 * (Cribbed from vfsck)
1000 #if defined(FD_FSMAGIC)
1001 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)
1002 && (sblock.fs_magic != FD_FSMAGIC)
1003 #if defined(AFS_HPUX101_ENV)
1004 && (sblock.fs_magic != FD_FSMAGIC_2)
1008 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)) {
1010 Log("There's something wrong with the superblock for partition %s; bad magic (%d) run vfsck\n", partition, sblock.fs_magic);
1013 if (sblock.fs_ncg < 1) {
1014 Log("There's something wrong with the superblock for partition %s; NCG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_ncg);
1017 if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG) {
1018 Log("There's something wrong with the superblock for partition %s; CPG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_cpg);
1021 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl
1022 || (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) {
1023 Log("There's something wrong with the superblock for partition %s; NCYL LESS THAN NCG*CPG run vfsck\n", partition);
1026 if (sblock.fs_sbsize > SBSIZE) {
1027 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);
1031 if ((super.fs.fs_magic != FS_MAGIC)
1032 || (super.fs.fs_ncg < 1)
1033 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1034 || (super.fs.fs_cpg < 1)
1036 || (super.fs.fs_cpg < 1 || super.fs.fs_cpg > MAXCPG)
1038 || (super.fs.fs_ncg * super.fs.fs_cpg < super.fs.fs_ncyl
1039 || (super.fs.fs_ncg - 1) * super.fs.fs_cpg >= super.fs.fs_ncyl)
1040 || (super.fs.fs_sbsize > SBSIZE)) {
1041 Log("There's something wrong with the superblock for partition %s; run vfsck\n", partition);
1044 #endif /* AFS_HPUX_ENV */
1047 bufsize = sblock.fs_ipg * sizeof(struct dinode);
1049 bufsize = super.fs.fs_ipg * sizeof(struct dinode);
1050 #endif /* AFS_HPUX_ENV */
1051 inodes = (struct dinode *)malloc(bufsize);
1052 einodes = (struct dinode *)(((char *)inodes) + bufsize);
1053 if (inodes == NULL) {
1054 Log("Unable to allocate enough memory to scan inodes; help!\n");
1057 Log("Scanning inodes on device %s...\n", rdev);
1059 for (c = 0; c < sblock.fs_ncg; c++) {
1060 i = c * sblock.fs_ipg;
1061 e = i + sblock.fs_ipg;
1062 #if defined(AFS_HPUX102_ENV)
1063 if (afs_lseek(pfd, dbtoo(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1066 if (afs_lseek(pfd, dbtob(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1070 for (c = 0; c < super.fs.fs_ncg; c++) {
1072 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1074 #if defined(AFS_DARWIN_ENV)
1075 #define offset_t off_t
1076 #define llseek lseek
1079 #endif /* AFS_SUN5_ENV */
1080 i = c * super.fs.fs_ipg;
1081 e = i + super.fs.fs_ipg;
1083 dblk1 = fsbtodb(&super.fs, itod(&super.fs, i));
1084 if (afs_lseek(pfd, (off_t) ((off_t) dblk1 * DEV_BSIZE), L_SET) == -1) {
1086 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1087 f1 = fsbtodb(&super.fs, itod(&super.fs, i));
1088 off = (offset_t) f1 << DEV_BSHIFT;
1089 if (llseek(pfd, off, L_SET) == -1) {
1091 if (afs_lseek(pfd, dbtob(fsbtodb(&super.fs, itod(&super.fs, i))), L_SET)
1093 #endif /* AFS_SUN5_ENV */
1094 #endif /* AFS_OSF_ENV */
1095 #endif /* AFS_HPUX_ENV */
1096 Log("Error reading inodes for partition %s; run vfsck\n",
1102 if (read(pfd, inodes, bufsize) != bufsize) {
1103 Log("Error reading inodes for partition %s; run vfsck\n",
1110 for (bj = bk = 0; bj < bufsize; bj = bj + 512, bk++) {
1111 if ((code = read(pfd, dptr, 512)) != 512) {
1112 Log("Error reading inode %d? for partition %s (errno = %d); run vfsck\n", bk + i, partition, errno);
1113 if (afs_lseek(pfd, 512, L_SET) == -1) {
1114 Log("Lseek failed\n");
1129 for (p = inodes; p < einodes && i < e; i++, p++) {
1131 Log("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1132 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1133 p->di_size, p->di_nlink);
1135 ("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1136 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1137 p->di_size, p->di_nlink);
1140 #ifdef AFS_3DISPARES
1141 /* Check to see if this inode is a pre-"OSF1 4.0D" inode */
1142 if ((p->di_uid || p->di_gid)
1143 && !(p->di_flags & (IC_XUID | IC_XGID))) {
1144 Log("Found unconverted inode %d: Use 'fs_conv_dux40D convert' on partition %s\n", i, partition);
1148 osi_Panic("Tru64 needs AFS_3DISPARES\n");
1151 #if defined(AFS_SUN56_ENV)
1152 /* if this is a pre-sol2.6 unconverted inode, bail out */
1154 afs_uint32 p1, p2, p3, p4;
1158 q = (quad *) & (p->di_ic.ic_lsize);
1160 p2 = p->di_ic.ic_flags;
1162 p4 = p->di_ic.ic_uid;
1163 p5 = p->di_ic.ic_gid;
1165 if ((p2 || p3) && !p4 && (p5 == -2)) {
1166 Log("Found unconverted inode %d\n", i);
1167 Log("You should run the AFS file conversion utility\n");
1172 if (IS_DVICEMAGIC(p) && (p->di_mode & IFMT) == IFREG) {
1173 afs_uint32 p2 = p->di_vicep2, p3 = DI_VICEP3(p);
1175 info.u.param[0] = p->di_vicep1;
1176 #ifdef AFS_3DISPARES
1177 if (((p2 >> 3) == INODESPECIAL) && (p2 & 0x3)) {
1178 info.u.param[1] = INODESPECIAL;
1179 info.u.param[2] = p3;
1180 info.u.param[3] = p2 & 0x3;
1182 info.u.param[1] = ((p2 >> 27) << 16) + (p3 & 0xffff);
1183 info.u.param[2] = (p2 & 0x3fffff);
1185 (((p2 >> 22) & 0x1f) << 16) + (p3 >> 16);
1188 info.u.param[1] = p->di_vicep2;
1189 info.u.param[2] = DI_VICEP3(p);
1190 info.u.param[3] = p->di_vicep4;
1192 info.inodeNumber = i;
1193 info.byteCount = p->di_size;
1194 info.linkCount = p->di_nlink;
1195 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1198 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1199 Log("Error writing inode file for partition %s\n",
1213 if (fflush(inodeFile) == EOF) {
1214 Log("Unable to successfully flush inode file for %s\n", partition);
1218 if (fsync(fileno(inodeFile)) == -1) {
1219 Log("Unable to successfully fsync inode file for %s\n", partition);
1225 * Paranoia: check that the file is really the right size
1227 if (fstat(fileno(inodeFile), &status) == -1) {
1228 Log("Unable to successfully stat inode file for %s\n", partition);
1232 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1233 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1234 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1251 #endif /* !AFS_SGI_ENV */
1252 #endif /* !AFS_AIX31_ENV */
1254 #ifdef AFS_DARWIN_ENV
1256 #define dbtob(db) ((unsigned)(db) << DEV_BSHIFT)
1260 bread(int fd, char *buf, daddr_t blk, afs_int32 size)
1263 #ifdef AFS_AIX41_ENV
1264 offset_t off = (offset_t) blk << FSBSHIFT;
1265 if (llseek(fd, off, 0) < 0) {
1266 Log("Unable to seek to offset %llu for block %u\n", off, blk);
1269 #else /* AFS_AIX41_ENV */
1270 if (afs_lseek(fd, blk * Bsize, 0) < 0) {
1271 Log("Unable to seek to offset %u for block %u\n", blk * Bsize, blk);
1273 #endif /* AFS_AIX41_ENV */
1275 if (afs_lseek(fd, (off_t) dbtob(blk), L_SET) < 0) {
1276 Log("Unable to seek to offset %u for block %u\n", dbtob(blk), blk);
1279 if (read(fd, buf, size) != size) {
1280 Log("Unable to read block %d, partition %s\n", blk, partition);
1286 #endif /* AFS_LINUX20_ENV */
1288 convertVolumeInfo(FdHandle_t *fdhr, FdHandle_t *fdhw, afs_uint32 vid)
1290 struct VolumeDiskData vd;
1293 if (FDH_PREAD(fdhr, &vd, sizeof(struct VolumeDiskData), 0) !=
1294 sizeof(struct VolumeDiskData)) {
1295 Log("1 convertiVolumeInfo: read failed for %lu with code %d\n", vid,
1299 vd.restoredFromId = vd.id; /* remember the RO volume here */
1301 vd.id = vd.parentId;
1305 vd.uniquifier += 5000; /* just in case there are still file copies
1306 from the old RW volume around */
1308 /* For ROs, the copyDate contains the time that the RO volume was actually
1309 * created, and the creationDate just contains the last time the RO was
1310 * copied from the RW data. So, make the new RW creationDate more accurate
1311 * by setting it to copyDate, if copyDate is older. Since, we know the
1312 * volume is at least as old as copyDate. */
1313 if (vd.copyDate < vd.creationDate) {
1314 vd.creationDate = vd.copyDate;
1316 /* If copyDate is newer, just make copyDate and creationDate the same,
1317 * for consistency with other RWs */
1318 vd.copyDate = vd.creationDate;
1321 p = strrchr(vd.name, '.');
1322 if (p && !strcmp(p, ".readonly")) {
1326 if (FDH_PWRITE(fdhw, &vd, sizeof(struct VolumeDiskData), 0) !=
1327 sizeof(struct VolumeDiskData)) {
1328 Log("1 convertiVolumeInfo: write failed for %lu with code %d\n", vid,
1336 afs_int32 inodeType;
1343 UpdateThisVolume(struct ViceInodeInfo *inodeinfo, VolumeId singleVolumeNumber,
1344 struct specino *specinos)
1347 if ((inodeinfo->u.vnode.vnodeNumber == INODESPECIAL) &&
1348 (inodeinfo->u.vnode.volumeId == singleVolumeNumber)) {
1349 specinos[inodeinfo->u.special.type].inodeNumber =
1350 inodeinfo->inodeNumber;
1352 return 0; /* We aren't using a result file, we're caching */
1356 getDevName(char *pbuffer, char *wpath)
1358 char pbuf[128], *ptr;
1359 strcpy(pbuf, pbuffer);
1360 ptr = (char *)strrchr(pbuf, OS_DIRSEPC);
1363 strcpy(wpath, pbuf);
1366 ptr = (char *)strrchr(pbuffer, OS_DIRSEPC);
1368 strcpy(pbuffer, ptr + 1);
1374 #ifdef FSSYNC_BUILD_CLIENT
1376 inode_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
1378 char dir_name[512], oldpath[512], newpath[512];
1380 char headername[16];
1382 int fd, err, forcep, j;
1383 ssize_t len, nBytes;
1385 struct DiskPartition64 *partP;
1386 struct ViceInodeInfo info;
1387 struct VolumeDiskHeader h;
1388 IHandle_t *ih, *ih2;
1389 FdHandle_t *fdP, *fdP2;
1392 char tmpDevName[100];
1394 struct specino specinos[VI_LINKTABLE+1];
1395 Inode nearInode = 0;
1396 # ifdef AFS_DEMAND_ATTACH_FS
1398 # endif /* AFS_DEMAND_ATTACH_FS */
1401 memset(&specinos, 0, sizeof(specinos));
1403 /* now do the work */
1405 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
1406 partP = partP->next);
1408 Log("1 inode_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
1413 #ifdef AFS_DEMAND_ATTACH_FS
1414 locktype = VVolLockType(V_VOLUPD, 1);
1415 code = VLockVolumeByIdNB(volumeId, partP, locktype);
1423 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
1424 Log("1 inode_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
1425 afs_printable_uint32_lu(volumeId));
1429 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
1431 strcpy(tmpDevName, partP->devName);
1432 name = getDevName(tmpDevName, wpath);
1434 if ((err = ListViceInodes(name, VPartitionPath(partP),
1435 NULL, UpdateThisVolume, volumeId,
1436 &forcep, 0, wpath, &specinos)) < 0)
1438 Log("1 inode_ConvertROtoRWvolume: Couldn't get special inodes\n");
1443 #if defined(NEARINODE_HINT)
1444 nearInodeHash(volumeId, nearInode);
1445 nearInode %= partP->f_files;
1448 for (j = VI_VOLINFO; j < VI_LINKTABLE+1; j++) {
1449 if (specinos[j].inodeNumber > 0) {
1450 specinos[j].ninodeNumber =
1451 IH_CREATE(NULL, partP->device, VPartitionPath(partP),
1452 nearInode, h.parent, INODESPECIAL, j, h.parent);
1453 IH_INIT(ih, partP->device, volumeId,
1454 specinos[j].inodeNumber);
1457 Log("1 inode_ConvertROtoRWvolume: Couldn't find special inode %d for %d\n", j, volumeId);
1462 IH_INIT(ih2, partP->device, h.parent, specinos[j].ninodeNumber);
1463 fdP2 = IH_OPEN(ih2);
1465 Log("1 inode_ConvertROtoRWvolume: Couldn't find special inode %d for %d\n", j, h.parent);
1470 if (j == VI_VOLINFO)
1471 convertVolumeInfo(fdP, fdP2, ih2->ih_vid);
1475 len = FDH_PREAD(fdP, buffer, sizeof(buffer), offset);
1480 nBytes = FDH_PWRITE(fdP2, buffer, len, offset);
1481 if (nBytes != len) {
1492 /* Unlink the old special inode; otherwise we will get duplicate
1493 * special inodes if we recreate the RO again */
1494 if (IH_DEC(ih, specinos[j].inodeNumber, volumeId) == -1) {
1496 Log("IH_DEC failed: %x, %s, %u errno %d\n", ih,
1497 PrintInode(stmp, specinos[j].inodeNumber), volumeId, errno);
1506 #ifdef AFS_64BIT_IOPS_ENV
1507 h.volumeInfo_lo = (afs_int32)specinos[VI_VOLINFO].ninodeNumber & 0xffffffff;
1508 h.volumeInfo_hi = (afs_int32)(specinos[VI_VOLINFO].ninodeNumber >> 32) && 0xffffffff;
1509 h.smallVnodeIndex_lo = (afs_int32)specinos[VI_SMALLINDEX].ninodeNumber & 0xffffffff;
1510 h.smallVnodeIndex_hi = (afs_int32)(specinos[VI_SMALLINDEX].ninodeNumber >> 32) & 0xffffffff;
1511 h.largeVnodeIndex_lo = (afs_int32)specinos[VI_LARGEINDEX].ninodeNumber & 0xffffffff;
1512 h.largeVnodeIndex_hi = (afs_int32)(specinos[VI_LARGEINDEX].ninodeNumber >> 32) & 0xffffffff;
1513 if (specinos[VI_LINKTABLE].ninodeNumber) {
1514 h.linkTable_lo = (afs_int32)specinos[VI_LINKTABLE].ninodeNumber & 0xffffffff;
1515 h.linkTable_hi = (afs_int32)specinos[VI_LINKTABLE].ninodeNumber & 0xffffffff;
1518 h.volumeInfo_lo = specinos[VI_VOLINFO].ninodeNumber;
1519 h.smallVnodeIndex_lo = specinos[VI_SMALLINDEX].ninodeNumber;
1520 h.largeVnodeIndex_lo = specinos[VI_LARGEINDEX].ninodeNumber;
1521 if (specinos[VI_LINKTABLE].ninodeNumber) {
1522 h.linkTable_lo = specinos[VI_LINKTABLE].ninodeNumber;
1526 if (VCreateVolumeDiskHeader(&h, partP)) {
1527 Log("1 inode_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
1528 afs_printable_uint32_lu(h.id));
1533 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
1534 Log("1 inode_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
1535 afs_printable_uint32_lu(volumeId));
1538 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
1539 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
1542 # ifdef AFS_DEMAND_ATTACH_FS
1544 VUnlockVolumeById(volumeId, partP);
1546 # endif /* AFS_DEMAND_ATTACH_FS */
1549 #endif /* FSSYNC_BUILD_CLIENT */
1550 #endif /* AFS_NAMEI_ENV */