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 #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, FILE *inodeFile,
194 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
195 char *wpath, void *rock)
197 char dev[50], rdev[51];
200 struct ViceInodeInfo info;
201 struct stat root_inode;
202 int ninodes = 0, err = 0;
204 pfd = -1; /* initialize so we don't close on error output below. */
207 sleep(1); /* simulate operator */
213 partition = mountedOn;
214 sprintf(dev, "/dev/%s", devname);
215 sprintf(rdev, "/dev/r%s", devname);
217 if (stat(mountedOn, &root_inode) < 0) {
218 Log("cannot stat: %s\n", mountedOn);
222 if (root_inode.st_ino != ROOTDIR_I) {
223 Log("%s is not root of a filesystem\n", mountedOn);
229 * done with the superblock, now try to read the raw device.
231 if (ReadSuper(&fs, dev) < 0)
236 case FM_CLEAN: /* clean and unmounted */
237 Log("Most peculiar - Super blk in FM_CLEAN state!\n");
239 case FM_MOUNT: /* mounted cleanly */
242 case FM_MDIRTY: /* dirty when mounted or commit fail */
243 case FM_LOGREDO: /* log redo attempted but failed */
244 Log("File system %s is in a bad state.\n", rdev);
245 Log("Call your IBM representative.\n");
249 if (IsBigFilesFileSystem(&fs, (char *)0)) {
250 Log("%s is a big files filesystem, can't salvage.\n", mountedOn);
254 if (strncmp(fs.s_magic, fsv3magic, strlen(fsv3magic)) != 0) {
256 if ((strncmp(fs.s_magic, fsv3pmagic, strlen(fsv3pmagic)) != 0)
257 || (fs.s_version != fsv3pvers)) {
258 Log("Super block doesn't have the problem magic (%s vs v3magic %s v3pmagic %s)\n", fs.s_magic, fsv3magic, fsv3pmagic);
262 Log("Super block doesn't have the problem magic (%s vs v3magic %s)\n",
263 fs.s_magic, fsv3magic);
270 fragsize = (fs.s_fragsize) ? fs.s_fragsize : FSBSIZE;
271 iagsize = (fs.s_iagsize) ? fs.s_iagsize : fs.s_agsize;
272 ag512 = fragsize * fs.s_agsize / 512;
273 agblocks = fragsize * fs.s_agsize >> BSHIFT;
274 #endif /* AFS_AIX41_ENV */
276 fmax = fs.s_fsize / (FSBSIZE / 512); /* first invalid blk num */
278 pfd = afs_open(rdev, O_RDONLY);
280 Log("Unable to open `%s' inode for reading\n", rdev);
285 * calculate the maximum number of inodes possible
288 imax = iagsize * (fs.s_fsize / ag512) - 1;
289 #else /* AFS_AIX41_ENV */
291 ((fmax / fs.s_agsize +
292 ((fmax % fs.s_agsize) >= fs.s_agsize / INOPB ? 1 : 0))
294 #endif /* AFS_AIX41_ENV */
297 * check for "FORCESALVAGE" equivalent:
298 * LAST_RSVD_I is a vice inode, with dead beef, and
299 * di_nlink == 2 to indicate the FORCE.
301 assert(p = ginode(LAST_RSVD_I));
303 if (p->di_vicemagic == VICEMAGIC && p->di_vicep1 == 0xdeadbeef
304 && p->di_nlink == 2) {
306 idec(root_inode.st_dev, LAST_RSVD_I, 0xdeadbeef);
309 for (inum = LAST_RSVD_I + 1; inum <= imax; ++inum) {
310 if ((p = ginode(inum)) == NULL || p->di_vicemagic != VICEMAGIC
311 || (p->di_mode & IFMT) != IFREG)
314 info.inodeNumber = inum;
315 info.byteCount = p->di_size;
316 info.linkCount = p->di_nlink;
317 info.u.param[0] = p->di_vicep1;
318 info.u.param[1] = p->di_vicep2;
319 info.u.param[2] = p->di_vicep3;
320 info.u.param[3] = p->di_vicep4;
322 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
326 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
327 Log("Error writing inode file for partition %s\n", partition);
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);
347 * Paranoia: check that the file is really the right size
349 if (fstat(fileno(inodeFile), &status) == -1) {
350 Log("Unable to successfully stat inode file for %s\n", partition);
354 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
355 Log("Wrong size (%d instead of %d) in inode file for %s\n",
356 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
374 /* Read in the superblock for devName */
376 ReadSuper(struct superblock *fs, char *devName)
380 pfd = afs_open(devName, O_RDONLY);
382 Log("Unable to open inode on %s for reading superblock.\n", devName);
386 if (bread(pfd, fs, SUPER_B, sizeof(struct superblock)) < 0) {
387 Log("Unable to read superblock on %s.\n", devName);
395 /* IsBigFilesFileSystem returns 1 if it's a big files filesystem, 0 otherwise. */
397 IsBigFilesFileSystem(struct superblock *sb)
399 if ((strncmp(sb->s_magic, fsv3pmagic, 4) == 0)
400 && (sb->s_version == fsbigfile)
414 static char buf[FSBSIZE];
415 static daddr_t last_blk = -1;
421 0) ? INODES_B + inum / INOPB : ag * agblocks + (inum -
424 #else /* AFS_AIX41_ENV */
425 ag = inum / fs.s_agsize;
428 0) ? INODES_B + inum / INOPB : ag * fs.s_agsize + (inum -
432 #endif /* AFS_AIX41_ENV */
434 if (last_blk != pblk) {
435 if (bread(pfd, buf, pblk, sizeof(buf)) < 0) {
442 dp = (struct dinode *)buf;
447 #else /* !AFS_AIX31_ENV */
449 #if defined(AFS_SGI_ENV)
451 /* libefs.h includes <assert.h>, which we don't want */
454 #ifdef AFS_SGI_XFS_IOPS_ENV
456 #include <afs/xfsattrs.h>
457 /* xfs_ListViceInodes
459 * xfs_ListViceInodes verifies and correct the XFS namespace as it collects
460 * the inode information. The name is required for the idec operation to work.
461 * Steps 2 and 3 below are for the AFS_XFS_NAME_VERS == 1. If the name space
462 * changes, the algorithm will need to change.
463 * 1) If the parent inode number does not match the directory's inod number,
464 * change it in the attribute.
465 * 2) If the unqifier in the attribute does not match the name, rename the
466 * file. This is done by doing an exclusive open, incrementing the tag
467 * number until a file can be created. If the tag changes, then the
468 * attribute will need updating.
469 * 3) If the tag in the attribute does not match the name, change the
471 * 4) Verify uid = RW volume id and gid = XFS_VICEMAGIC.
477 * Does the verifications listed above.
478 * We can't change the names until the readdir is complete, so we set the
479 * rename flag if the file needs renaming.
482 xfs_VerifyInode(char *dir, uint64_t pino, char *name, i_list_inode_t * info,
489 int update_chown = 0;
496 (void)sprintf(path, "%s/%s", dir, name);
497 /* Verify uid and gid fields */
498 if (info->ili_magic != XFS_VICEMAGIC) {
499 Log("%s magic for %s/%s (inode %s) from %d to %d\n",
500 Testing ? "Would have changed" : "Changing", dir, name,
501 PrintInode(NULL, info->ili_info.inodeNumber), info->ili_magic,
507 vno = info->ili_info.param[0];
508 if (info->ili_vno != AFS_XFS_VNO_CLIP(vno)) {
509 Log("%s volume id for %s/%s (inode %s) from %d to %d\n",
510 Testing ? "Would have changed" : "Changing", dir, name,
511 PrintInode(NULL, info->ili_info.inodeNumber), info->ili_vno,
512 AFS_XFS_VNO_CLIP(vno));
518 if (chown(path, AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC) < 0) {
519 Log("Can't chown %s to uid=%d, gid=0x%x\n", path,
520 AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC);
525 /* Check Parent inode number. */
526 if (info->ili_pino != pino) {
527 afs_ino_str_t sino, sipino, spino;
528 (void)PrintInode(sino, info->ili_info.inodeNumber);
529 (void)PrintInode(sipino, info->ili_pino);
530 (void)PrintInode(spino, pino);
531 Log("%s parent ino for %s (inode %s) from %s to %s.\n",
532 Testing ? "Would have changed" : "Changing", path, sino, sipino,
538 /* Verify the file name. */
539 (void)strcpy(tmpName, ".");
540 (void)strcat(tmpName, int_to_base64(stmp, info->ili_info.param[2]));
541 if (strncmp(name, tmpName, strlen(tmpName))) {
542 Log("%s name %s (inode %s) in directory %s, unique=%d, tag=%d\n",
543 Testing ? "Would have returned bad" : "Bad", name,
544 PrintInode(NULL, info->ili_info.inodeNumber), dir,
545 info->ili_info.param[2], info->ili_tag);
551 /* update the tag? */
552 (void)strcat(tmpName, ".");
553 (void)strcat(tmpName, int_to_base64(stmp, info->ili_tag));
554 if (strcmp(name, tmpName)) {
556 (void)strcpy(tmpName, name);
557 p = strchr(tmpName + 1, '.');
559 Log("No tag found on name %s (inode %s)in directory, %s.\n",
560 name, PrintInode(NULL, info->ili_info.inodeNumber), dir,
561 Testing ? "would have renamed" : "will rename");
565 tag = base64_to_int(p + 1);
566 Log("%s the tag for %s (inode %s) from %d to %d.\n",
567 Testing ? "Would have changed" : "Will change", path,
568 PrintInode(NULL, info->ili_info.inodeNumber), dir, tag,
576 if (update_pino || update_tag) {
577 afs_xfs_attr_t attrs;
580 length = SIZEOF_XFS_ATTR_T;
581 if (attr_get(path, AFS_XFS_ATTR, (char *)&attrs, &length, ATTR_ROOT) <
583 Log("Can't get AFS attribute for %s\n", path);
587 attrs.at_pino = pino;
591 (path, AFS_XFS_ATTR, (char *)&attrs, length,
592 ATTR_ROOT | ATTR_REPLACE) < 0) {
593 Log("Can't set AFS attribute into %s\n", path);
607 xfs_RenameFiles(char *dir, xfs_Rename_t * renames, int n_renames)
610 char opath[128], nbase[128], npath[128];
611 afs_xfs_attr_t attrs;
612 int length = SIZEOF_XFS_ATTR_T;
617 for (i = 0; i < n_renames; i++) {
618 (void)sprintf(opath, "%s/%s", dir, renames[i].name);
619 (void)sprintf(nbase, "%s/.%s", dir,
620 int_to_base64(stmp, renames[i].uniq));
621 for (tag = 2, j = 0; j < 64; tag++, j++) {
622 (void)sprintf(npath, "%s.%s", nbase, int_to_base64(stmp, tag));
623 fd = afs_open(npath, O_CREAT | O_EXCL | O_RDWR, 0);
630 Log("Can't find a new name for %s\n", opath);
633 if (rename(opath, npath) < 0) {
634 Log("Can't rename %s to %s\n", opath, npath);
637 Log("Renamed %s to %s\n", opath, npath);
644 xfs_ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
645 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep,
646 int forceR, char *wpath, void *rock)
649 int info_size = sizeof(i_list_inode_t);
652 dirent64_t *top_direntp;
654 dirent64_t *vol_direntp;
655 struct stat64 sdirbuf;
656 struct stat64 sfilebuf;
657 afs_xfs_attr_t attrs;
658 afs_xfs_dattr_t dattrs;
660 char vol_dirname[1024];
663 xfs_Rename_t *renames = (xfs_Rename_t *) 0;
665 #define N_RENAME_STEP 64
674 if (stat64(mountedOn, &sdirbuf) < 0) {
675 perror("xfs_ListViceInodes: stat64");
679 if ((top_dirp = opendir(mountedOn)) == NULL) {
680 Log("Can't open directory %s to read inodes.\n", mountedOn);
684 while (top_direntp = readdir64(top_dirp)) {
685 /* Only descend directories with the AFSDIR attribute set.
686 * Could also verify the contents of the atribute, but currently
688 * Performance could be improved for single volume salvages by
689 * only going through the directory containing the volume's inodes.
690 * But I'm being complete as a first pass.
692 (void)sprintf(vol_dirname, "%s/%s", mountedOn, top_direntp->d_name);
693 length = SIZEOF_XFS_DATTR_T;
695 (vol_dirname, AFS_XFS_DATTR, (char *)&dattrs, &length, ATTR_ROOT))
698 if ((vol_dirp = opendir(vol_dirname)) == NULL) {
699 if (errno == ENOTDIR)
701 Log("Can't open directory %s to read inodes.\n", vol_dirname);
705 pino = top_direntp->d_ino;
707 while (vol_direntp = readdir64(vol_dirp)) {
708 if (vol_direntp->d_name[1] == '\0'
709 || vol_direntp->d_name[1] == '.')
712 info.ili_version = AFS_XFS_ILI_VERSION;
713 info_size = sizeof(i_list_inode_t);
715 ilistinode64(sdirbuf.st_dev, vol_direntp->d_ino, &info,
718 /* Where possible, give more explicit messages. */
722 Log("%s (device id %d) is not on an XFS filesystem.\n",
723 vol_dirname, sdirbuf.st_dev);
728 if (info_size != sizeof(i_list_inode_t)
729 || info.ili_version != AFS_XFS_ILI_VERSION) {
730 Log("Version skew between kernel and salvager.\n");
735 /* Continue, so we collect all the errors in the first pass. */
736 Log("Error listing inode named %s/%s: %s\n", vol_dirname,
737 vol_direntp->d_name, strerror(errno));
742 if (info.ili_attr_version != AFS_XFS_ATTR_VERS) {
743 Log("Unrecognized XFS attribute version %d in %s/%s. Upgrade salvager\n", info.ili_attr_version, vol_dirname, vol_direntp->d_name);
747 if (judgeInode && (*judgeInode) (&info.ili_info, judgeParam, rock) == 0)
752 (vol_dirname, pino, vol_direntp->d_name, &info,
758 /* Add this name to the list of items to rename. */
759 if (n_renames >= n_avail) {
760 n_avail += N_RENAME_STEP;
761 if (n_avail == N_RENAME_STEP)
762 renames = (xfs_Rename_t *)
763 malloc(n_avail * sizeof(xfs_Rename_t));
765 renames = (xfs_Rename_t *)
766 realloc((char *)renames,
767 n_avail * sizeof(xfs_Rename_t));
769 Log("Can't %salloc %lu bytes for rename list.\n",
770 (n_avail == N_RENAME_STEP) ? "m" : "re",
771 n_avail * sizeof(xfs_Rename_t));
775 (void)strcpy(renames[n_renames].name, vol_direntp->d_name);
776 renames[n_renames].uniq = info.ili_info.param[2];
782 (&info.ili_info, sizeof(vice_inode_info_t), 1, inodeFile)
784 Log("Error writing inode file for partition %s\n", mountedOn);
790 } /* end while vol_direntp */
793 vol_dirp = (DIR *) 0;
795 Log("Renaming files.\n");
796 if (xfs_RenameFiles(vol_dirname, renames, n_renames) < 0) {
804 free((char *)renames);
806 if (fflush(inodeFile) == EOF) {
807 ("Unable to successfully flush inode file for %s\n", mountedOn);
808 return errors ? -1 : -2;
810 if (fsync(fileno(inodeFile)) == -1) {
811 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
812 return errors ? -1 : -2;
815 * Paranoia: check that the file is really the right size
817 if (fstat(fileno(inodeFile), &status) == -1) {
818 Log("Unable to successfully stat inode file for %s\n", partition);
819 return errors ? -1 : -2;
821 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
822 Log("Wrong size (%d instead of %d) in inode file for %s\n",
823 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
825 return errors ? -1 : -2;
830 Log("Errors encontered listing inodes, not salvaging partition.\n");
842 free((char *)renames);
849 ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
850 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
851 char *wpath, void *rock)
853 char dev[50], rdev[51];
855 struct efs_dinode *p;
856 struct ViceInodeInfo info;
857 struct stat root_inode;
858 int ninodes = 0, err = 0;
859 struct efs_dinode *dinodeBuf = NULL;
861 ino_t imax, inum; /* total number of I-nodes in file system */
865 sleep(1); /* simulate operator */
871 if (stat(mountedOn, &root_inode) < 0) {
872 Log("cannot stat: %s\n", mountedOn);
875 #ifdef AFS_SGI_XFS_IOPS_ENV
876 if (!strcmp("xfs", root_inode.st_fstype)) {
877 return xfs_ListViceInodes(devname, mountedOn, inodeFile, judgeInode,
878 judgeParam, forcep, forceR, wpath, rock);
882 Log("%s is not root of a filesystem\n", mountedOn);
887 #else /* AFS_SGI_ENV */
890 #define SPERB (MAXBSIZE / sizeof(short))
891 #define MAXNINDIR (MAXBSIZE / sizeof(daddr_t))
894 struct bufarea *b_next; /* must be first */
898 char b_buf[MAXBSIZE]; /* buffer space */
899 short b_lnks[SPERB]; /* link counts */
900 daddr_t b_indir[MAXNINDIR]; /* indirect block */
901 struct fs b_fs; /* super block */
902 struct cg b_cg; /* cylinder group */
906 typedef struct bufarea BUFAREA;
909 #define sblock sblk.b_un.b_fs
910 #endif /* AFS_HPUX_ENV */
912 extern char *afs_rawname();
914 ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
915 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
916 char *wpath, void *rock)
922 #else /* !AFS_AIX_ENV */
927 int i, c, e, bufsize, code, err = 0;
928 char dev[50], rdev[100], err1[512], *ptr1;
929 struct dinode *inodes = NULL, *einodes, *dptr;
933 struct ViceInodeInfo info;
936 partition = mountedOn;
937 sprintf(rdev, "%s/%s", wpath, devname);
938 ptr1 = afs_rawname(rdev);
942 /* Bletch: this is terrible; is there a better way to do this? Does this work? vfsck doesn't even sleep!! */
944 sleep(5); /* Trying a smaller one for aix */
949 pfd = afs_open(rdev, O_RDONLY);
951 sprintf(err1, "Could not open device %s to get inode list\n", rdev);
956 if (bread(pfd, (char *)&super.fs, SUPERB, sizeof super.fs) == -1) {
959 if (bread(pfd, (char *)&sblock, SBLOCK, SBSIZE) == -1) {
961 if (bread(pfd, super.block, SBLOCK, SBSIZE) == -1) {
962 #endif /* AFS_HPUX_ENV */
964 Log("Unable to read superblock, partition %s\n", partition);
970 * char *FSlabel(), *fslabel=0;
971 * fslabel = FSlabel(&super.fs);
973 if (super.fs.s_bsize == 0)
974 super.fs.s_bsize = 512;
975 if (super.fs.s_bsize != BSIZE) {
976 Log("SuperBlk: Cluster size not %d; run vfsck\n", BSIZE);
979 fmax = super.fs.s_fsize; /* first invalid blk num */
980 imax = ((ino_t) super.fs.s_isize - (SUPERB + 1)) * INOPB;
982 Log("Size check: imax==0!\n");
985 if (GetAuxInodeFile(partition, &status) == 0) {
986 Log("Can't access Aux inode file for partition %s, aborting\n",
990 for (inum = 1; inum <= imax; inum++) {
991 struct dauxinode *auxp;
992 if ((auxp = IsAfsInode(inum)) == NULL) {
993 /* Not an afs inode, keep going */
996 if ((p = ginode(inum)) == NULL)
998 /* deleted/non-existent inode when di_mode == 0 */
1001 info.inodeNumber = (int)inum;
1002 info.byteCount = p->di_size;
1003 info.linkCount = p->di_nlink;
1004 info.u.param[0] = auxp->aux_param1;
1005 info.u.param[1] = auxp->aux_param2;
1006 info.u.param[2] = auxp->aux_param3;
1007 info.u.param[3] = auxp->aux_param4;
1008 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1011 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1012 Log("Error writing inode file for partition %s\n", partition);
1020 * run a few consistency checks of the superblock
1021 * (Cribbed from vfsck)
1024 #if defined(FD_FSMAGIC)
1025 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)
1026 && (sblock.fs_magic != FD_FSMAGIC)
1027 #if defined(AFS_HPUX101_ENV)
1028 && (sblock.fs_magic != FD_FSMAGIC_2)
1032 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)) {
1034 Log("There's something wrong with the superblock for partition %s; bad magic (%d) run vfsck\n", partition, sblock.fs_magic);
1037 if (sblock.fs_ncg < 1) {
1038 Log("There's something wrong with the superblock for partition %s; NCG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_ncg);
1041 if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG) {
1042 Log("There's something wrong with the superblock for partition %s; CPG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_cpg);
1045 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl
1046 || (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) {
1047 Log("There's something wrong with the superblock for partition %s; NCYL LESS THAN NCG*CPG run vfsck\n", partition);
1050 if (sblock.fs_sbsize > SBSIZE) {
1051 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);
1055 if ((super.fs.fs_magic != FS_MAGIC)
1056 || (super.fs.fs_ncg < 1)
1057 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1058 || (super.fs.fs_cpg < 1)
1060 || (super.fs.fs_cpg < 1 || super.fs.fs_cpg > MAXCPG)
1062 || (super.fs.fs_ncg * super.fs.fs_cpg < super.fs.fs_ncyl
1063 || (super.fs.fs_ncg - 1) * super.fs.fs_cpg >= super.fs.fs_ncyl)
1064 || (super.fs.fs_sbsize > SBSIZE)) {
1065 Log("There's something wrong with the superblock for partition %s; run vfsck\n", partition);
1068 #endif /* AFS_HPUX_ENV */
1071 bufsize = sblock.fs_ipg * sizeof(struct dinode);
1073 bufsize = super.fs.fs_ipg * sizeof(struct dinode);
1074 #endif /* AFS_HPUX_ENV */
1075 inodes = (struct dinode *)malloc(bufsize);
1076 einodes = (struct dinode *)(((char *)inodes) + bufsize);
1077 if (inodes == NULL) {
1078 Log("Unable to allocate enough memory to scan inodes; help!\n");
1081 Log("Scanning inodes on device %s...\n", rdev);
1083 for (c = 0; c < sblock.fs_ncg; c++) {
1084 i = c * sblock.fs_ipg;
1085 e = i + sblock.fs_ipg;
1086 #if defined(AFS_HPUX102_ENV)
1087 if (afs_lseek(pfd, dbtoo(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1090 if (afs_lseek(pfd, dbtob(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1094 for (c = 0; c < super.fs.fs_ncg; c++) {
1096 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1098 #if defined(AFS_DARWIN_ENV)
1099 #define offset_t off_t
1100 #define llseek lseek
1103 #endif /* AFS_SUN5_ENV */
1104 i = c * super.fs.fs_ipg;
1105 e = i + super.fs.fs_ipg;
1107 dblk1 = fsbtodb(&super.fs, itod(&super.fs, i));
1108 if (afs_lseek(pfd, (off_t) ((off_t) dblk1 * DEV_BSIZE), L_SET) == -1) {
1110 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1111 f1 = fsbtodb(&super.fs, itod(&super.fs, i));
1112 off = (offset_t) f1 << DEV_BSHIFT;
1113 if (llseek(pfd, off, L_SET) == -1) {
1115 if (afs_lseek(pfd, dbtob(fsbtodb(&super.fs, itod(&super.fs, i))), L_SET)
1117 #endif /* AFS_SUN5_ENV */
1118 #endif /* AFS_OSF_ENV */
1119 #endif /* AFS_HPUX_ENV */
1120 Log("Error reading inodes for partition %s; run vfsck\n",
1126 if (read(pfd, inodes, bufsize) != bufsize) {
1127 Log("Error reading inodes for partition %s; run vfsck\n",
1132 register int bj, bk;
1134 for (bj = bk = 0; bj < bufsize; bj = bj + 512, bk++) {
1135 if ((code = read(pfd, dptr, 512)) != 512) {
1136 Log("Error reading inode %d? for partition %s (errno = %d); run vfsck\n", bk + i, partition, errno);
1137 if (afs_lseek(pfd, 512, L_SET) == -1) {
1138 Log("Lseek failed\n");
1153 for (p = inodes; p < einodes && i < e; i++, p++) {
1155 Log("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1156 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1157 p->di_size, p->di_nlink);
1159 ("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1160 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1161 p->di_size, p->di_nlink);
1164 #ifdef AFS_3DISPARES
1165 /* Check to see if this inode is a pre-"OSF1 4.0D" inode */
1166 if ((p->di_uid || p->di_gid)
1167 && !(p->di_flags & (IC_XUID | IC_XGID))) {
1168 Log("Found unconverted inode %d: Use 'fs_conv_dux40D convert' on partition %s\n", i, partition);
1172 assert(0); /* define AFS_3DISPARES in param.h */
1175 #if defined(AFS_SUN56_ENV)
1176 /* if this is a pre-sol2.6 unconverted inode, bail out */
1178 afs_uint32 p1, p2, p3, p4;
1182 q = (quad *) & (p->di_ic.ic_lsize);
1184 p2 = p->di_ic.ic_flags;
1186 p4 = p->di_ic.ic_uid;
1187 p5 = p->di_ic.ic_gid;
1189 if ((p2 || p3) && !p4 && (p5 == -2)) {
1190 Log("Found unconverted inode %d\n", i);
1191 Log("You should run the AFS file conversion utility\n");
1196 if (IS_DVICEMAGIC(p) && (p->di_mode & IFMT) == IFREG) {
1197 afs_uint32 p2 = p->di_vicep2, p3 = DI_VICEP3(p);
1199 info.u.param[0] = p->di_vicep1;
1200 #ifdef AFS_3DISPARES
1201 if (((p2 >> 3) == INODESPECIAL) && (p2 & 0x3)) {
1202 info.u.param[1] = INODESPECIAL;
1203 info.u.param[2] = p3;
1204 info.u.param[3] = p2 & 0x3;
1206 info.u.param[1] = ((p2 >> 27) << 16) + (p3 & 0xffff);
1207 info.u.param[2] = (p2 & 0x3fffff);
1209 (((p2 >> 22) & 0x1f) << 16) + (p3 >> 16);
1212 info.u.param[1] = p->di_vicep2;
1213 info.u.param[2] = DI_VICEP3(p);
1214 info.u.param[3] = p->di_vicep4;
1216 info.inodeNumber = i;
1217 info.byteCount = p->di_size;
1218 info.linkCount = p->di_nlink;
1219 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1222 if (fwrite(&info, sizeof info, 1, inodeFile) != 1) {
1223 Log("Error writing inode file for partition %s\n",
1237 if (fflush(inodeFile) == EOF) {
1238 Log("Unable to successfully flush inode file for %s\n", partition);
1242 if (fsync(fileno(inodeFile)) == -1) {
1243 Log("Unable to successfully fsync inode file for %s\n", partition);
1249 * Paranoia: check that the file is really the right size
1251 if (fstat(fileno(inodeFile), &status) == -1) {
1252 Log("Unable to successfully stat inode file for %s\n", partition);
1256 if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
1257 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1258 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
1275 #endif /* !AFS_SGI_ENV */
1276 #endif /* !AFS_AIX31_ENV */
1278 #ifdef AFS_DARWIN_ENV
1280 #define dbtob(db) ((unsigned)(db) << DEV_BSHIFT)
1284 bread(int fd, char *buf, daddr_t blk, afs_int32 size)
1287 #ifdef AFS_AIX41_ENV
1288 offset_t off = (offset_t) blk << FSBSHIFT;
1289 if (llseek(fd, off, 0) < 0) {
1290 Log("Unable to seek to offset %llu for block %u\n", off, blk);
1293 #else /* AFS_AIX41_ENV */
1294 if (afs_lseek(fd, blk * Bsize, 0) < 0) {
1295 Log("Unable to seek to offset %u for block %u\n", blk * Bsize, blk);
1297 #endif /* AFS_AIX41_ENV */
1299 if (afs_lseek(fd, (off_t) dbtob(blk), L_SET) < 0) {
1300 Log("Unable to seek to offset %u for block %u\n", dbtob(blk), blk);
1303 if (read(fd, buf, size) != size) {
1304 Log("Unable to read block %d, partition %s\n", blk, partition);
1310 #endif /* AFS_LINUX20_ENV */
1312 convertVolumeInfo(int fdr, int fdw, afs_uint32 vid)
1314 struct VolumeDiskData vd;
1317 if (read(fdr, &vd, sizeof(struct VolumeDiskData)) !=
1318 sizeof(struct VolumeDiskData)) {
1319 Log("1 convertiVolumeInfo: read failed for %lu with code %d\n", vid,
1323 vd.restoredFromId = vd.id; /* remember the RO volume here */
1325 vd.id = vd.parentId;
1329 vd.uniquifier += 5000; /* just in case there are still file copies
1330 from the old RW volume around */
1332 p = strrchr(vd.name, '.');
1333 if (p && !strcmp(p, ".readonly")) {
1337 if (write(fdw, &vd, sizeof(struct VolumeDiskData)) !=
1338 sizeof(struct VolumeDiskData)) {
1339 Log("1 convertiVolumeInfo: write failed for %lu with code %d\n", vid,
1347 afs_int32 inodeType;
1354 UpdateThisVolume(struct ViceInodeInfo *inodeinfo, VolumeId singleVolumeNumber,
1355 struct specino *specinos)
1358 if ((inodeinfo->u.vnode.vnodeNumber == INODESPECIAL) &&
1359 (inodeinfo->u.vnode.volumeId == singleVolumeNumber)) {
1360 specinos[inodeinfo->u.special.type].inodeNumber =
1361 inodeinfo->inodeNumber;
1363 return 0; /* We aren't using a result file, we're caching */
1367 getDevName(char *pbuffer, char *wpath)
1369 char pbuf[128], *ptr;
1370 strcpy(pbuf, pbuffer);
1371 ptr = (char *)strrchr(pbuf, '/');
1374 strcpy(wpath, pbuf);
1377 ptr = (char *)strrchr(pbuffer, '/');
1379 strcpy(pbuffer, ptr + 1);
1385 #ifdef FSSYNC_BUILD_CLIENT
1387 inode_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
1389 char dir_name[512], oldpath[512], newpath[512];
1391 char headername[16];
1393 int fd, err, forcep, len, j, code;
1395 struct DiskPartition64 *partP;
1396 struct ViceInodeInfo info;
1397 struct VolumeDiskHeader h;
1398 IHandle_t *ih, *ih2;
1399 FdHandle_t *fdP, *fdP2;
1401 char tmpDevName[100];
1403 struct specino specinos[VI_LINKTABLE+1];
1404 Inode nearInode = 0;
1406 memset(&specinos, 0, sizeof(specinos));
1408 /* now do the work */
1410 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
1411 partP = partP->next);
1413 Log("1 inode_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
1417 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
1418 Log("1 inode_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
1419 afs_printable_uint32_lu(volumeId));
1423 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
1425 strcpy(tmpDevName, partP->devName);
1426 name = getDevName(tmpDevName, wpath);
1428 if ((err = ListViceInodes(name, VPartitionPath(partP),
1429 NULL, UpdateThisVolume, volumeId,
1430 &forcep, 0, wpath, &specinos)) < 0)
1432 Log("1 inode_ConvertROtoRWvolume: Couldn't get special inodes\n");
1436 #if defined(NEARINODE_HINT)
1437 nearInodeHash(volumeId, nearInode);
1438 nearInode %= partP->f_files;
1441 for (j = VI_VOLINFO; j < VI_LINKTABLE+1; j++) {
1442 if (specinos[j].inodeNumber > 0) {
1443 specinos[j].ninodeNumber =
1444 IH_CREATE(NULL, partP->device, VPartitionPath(partP),
1445 nearInode, h.parent, INODESPECIAL, j, h.parent);
1446 IH_INIT(ih, partP->device, volumeId,
1447 specinos[j].inodeNumber);
1450 Log("1 inode_ConvertROtoRWvolume: Couldn't find special inode %d for %d\n", j, volumeId);
1454 IH_INIT(ih2, partP->device, h.parent, specinos[j].ninodeNumber);
1455 fdP2 = IH_OPEN(ih2);
1457 Log("1 inode_ConvertROtoRWvolume: Couldn't find special inode %d for %d\n", j, h.parent);
1461 if (j == VI_VOLINFO)
1462 convertVolumeInfo(fdP->fd_fd, fdP2->fd_fd, ih2->ih_vid);
1465 len = read(fdP->fd_fd, buffer, sizeof(buffer));
1470 code = write(fdP2->fd_fd, buffer, len);
1479 /* Unlink the old special inode; otherwise we will get duplicate
1480 * special inodes if we recreate the RO again */
1481 if (IH_DEC(ih, specinos[j].inodeNumber, volumeId) == -1) {
1482 Log("IH_DEC failed: %x, %s, %u errno %d\n", ih,
1483 PrintInode(NULL, specinos[j].inodeNumber), volumeId, errno);
1492 #ifdef AFS_64BIT_IOPS_ENV
1493 h.volumeInfo_lo = (afs_int32)specinos[VI_VOLINFO].ninodeNumber & 0xffffffff;
1494 h.volumeInfo_hi = (afs_int32)(specinos[VI_VOLINFO].ninodeNumber >> 32) && 0xffffffff;
1495 h.smallVnodeIndex_lo = (afs_int32)specinos[VI_SMALLINDEX].ninodeNumber & 0xffffffff;
1496 h.smallVnodeIndex_hi = (afs_int32)(specinos[VI_SMALLINDEX].ninodeNumber >> 32) & 0xffffffff;
1497 h.largeVnodeIndex_lo = (afs_int32)specinos[VI_LARGEINDEX].ninodeNumber & 0xffffffff;
1498 h.largeVnodeIndex_hi = (afs_int32)(specinos[VI_LARGEINDEX].ninodeNumber >> 32) & 0xffffffff;
1499 if (specinos[VI_LINKTABLE].ninodeNumber) {
1500 h.linkTable_lo = (afs_int32)specinos[VI_LINKTABLE].ninodeNumber & 0xffffffff;
1501 h.linkTable_hi = (afs_int32)specinos[VI_LINKTABLE].ninodeNumber & 0xffffffff;
1504 h.volumeInfo_lo = specinos[VI_VOLINFO].ninodeNumber;
1505 h.smallVnodeIndex_lo = specinos[VI_SMALLINDEX].ninodeNumber;
1506 h.largeVnodeIndex_lo = specinos[VI_LARGEINDEX].ninodeNumber;
1507 if (specinos[VI_LINKTABLE].ninodeNumber) {
1508 h.linkTable_lo = specinos[VI_LINKTABLE].ninodeNumber;
1512 if (VCreateVolumeDiskHeader(&h, partP)) {
1513 Log("1 inode_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
1514 afs_printable_uint32_lu(h.id));
1518 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
1519 Log("1 inode_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
1520 afs_printable_uint32_lu(volumeId));
1523 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
1524 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
1527 #endif /* FSSYNC_BUILD_CLIENT */
1528 #endif /* AFS_NAMEI_ENV */