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>
26 #if defined(AFS_LINUX20_ENV) || defined(AFS_SUN4_ENV)
31 * -1 - Unable to read the inodes.
32 * -2 - Unable to completely write temp file. Produces warning message in log.
35 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
36 afs_uint32 (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
37 char *wpath, void *rock)
39 Log("ListViceInodes not implemented for this platform!\n");
44 #if defined(AFS_SGI_ENV)
48 #else /* AFS_OSF_ENV */
49 #ifdef AFS_VFSINCL_ENV
52 #include <sys/fs/ufs_fs.h>
54 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
55 #include <ufs/ufs/dinode.h>
56 #include <ufs/ffs/fs.h>
57 #define itod ino_to_fsba
62 #else /* AFS_VFSINCL_ENV */
64 #include <sys/filsys.h>
68 #endif /* AFS_VFSINCL_ENV */
69 #endif /* AFS_OSF_ENV */
70 #ifdef AFS_VFSINCL_ENV
71 #include <sys/vnode.h>
73 #include <sys/fs/ufs_inode.h>
75 #if !defined(AFS_DARWIN_ENV)
76 #include <ufs/inode.h>
79 #else /* AFS_VFSINCL_ENV */
81 #include <ufs/inode.h>
82 #else /* AFS_OSF_ENV */
83 #include <sys/inode.h>
85 #endif /* AFS_VFSINCL_ENV */
86 #endif /* AFS_SGI_ENV */
87 #include <afs/osi_inode.h>
90 #include <afs/afsint.h>
92 #include <afs/afssyscalls.h>
93 #include "viceinode.h"
94 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV)
97 #include <afs/afs_assert.h>
103 #include "volinodes.h"
104 #include "partition.h"
106 #include "volume_inline.h"
108 /* Notice: parts of this module have been cribbed from vfsck.c */
111 static char *partition;
116 #include <jfs/filsys.h>
119 #define FSBSIZE (4096) /* filesystem block size */
120 #define FSBSHIFT (12) /* log2(FSBSIZE) */
121 #define FSBMASK (FSBSIZE - 1) /* FSBSIZE mask */
123 #define MIN_FSIZE DISKMAP_B /* minimum fs size (FSblocks) */
124 #define LAST_RSVD_I 15 /* last reserved inode */
129 * This will hopefully eventually make it into the system include files
131 #define INOPB (FSBSIZE / sizeof (struct dinode))
139 #endif /* AFS_AIX41_ENV */
142 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
143 XX This was lifted from some `com/cmd/fs/fshlpr_aix3/Fs.h', which indicated X
144 XX a longing to see it make it into a readily accessible include file. XXXXXX
145 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
147 * itoo - inode number to offset within disk block
150 #define itoo(x) (int) ((unsigned)(x) % INOPB)
152 int Bsize = FSBSIZE; /* block size for this system */
153 daddr_t fmax; /* total number of blocks n file system */
154 ino_t imax, inum; /* total number of I-nodes in file system */
156 static struct superblock fs;
157 struct dinode *ginode();
161 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
162 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
163 char *wpath, void *rock)
165 char dev[50], rdev[51];
168 struct ViceInodeInfo info;
169 struct stat root_inode;
170 int ninodes = 0, err = 0;
172 pfd = INVALID_FD; /* initialize so we don't close on error output below. */
175 sleep(1); /* simulate operator */
181 partition = mountedOn;
182 sprintf(dev, "/dev/%s", devname);
183 sprintf(rdev, "/dev/r%s", devname);
185 if (stat(mountedOn, &root_inode) < 0) {
186 Log("cannot stat: %s\n", mountedOn);
190 if (root_inode.st_ino != ROOTDIR_I) {
191 Log("%s is not root of a filesystem\n", mountedOn);
197 * done with the superblock, now try to read the raw device.
199 if (ReadSuper(&fs, dev) < 0)
204 case FM_CLEAN: /* clean and unmounted */
205 Log("Most peculiar - Super blk in FM_CLEAN state!\n");
207 case FM_MOUNT: /* mounted cleanly */
210 case FM_MDIRTY: /* dirty when mounted or commit fail */
211 case FM_LOGREDO: /* log redo attempted but failed */
212 Log("File system %s is in a bad state.\n", rdev);
213 Log("Call your IBM representative.\n");
217 if (IsBigFilesFileSystem(&fs, (char *)0)) {
218 Log("%s is a big files filesystem, can't salvage.\n", mountedOn);
222 if (strncmp(fs.s_magic, fsv3magic, strlen(fsv3magic)) != 0) {
224 if ((strncmp(fs.s_magic, fsv3pmagic, strlen(fsv3pmagic)) != 0)
225 || (fs.s_version != fsv3pvers)) {
226 Log("Super block doesn't have the problem magic (%s vs v3magic %s v3pmagic %s)\n", fs.s_magic, fsv3magic, fsv3pmagic);
230 Log("Super block doesn't have the problem magic (%s vs v3magic %s)\n",
231 fs.s_magic, fsv3magic);
238 fragsize = (fs.s_fragsize) ? fs.s_fragsize : FSBSIZE;
239 iagsize = (fs.s_iagsize) ? fs.s_iagsize : fs.s_agsize;
240 ag512 = fragsize * fs.s_agsize / 512;
241 agblocks = fragsize * fs.s_agsize >> BSHIFT;
242 #endif /* AFS_AIX41_ENV */
244 fmax = fs.s_fsize / (FSBSIZE / 512); /* first invalid blk num */
246 pfd = OS_OPEN(rdev, O_RDONLY, 0666);
247 if (pfd == INVALID_FD) {
248 Log("Unable to open `%s' inode for reading\n", rdev);
253 * calculate the maximum number of inodes possible
256 imax = iagsize * (fs.s_fsize / ag512) - 1;
257 #else /* AFS_AIX41_ENV */
259 ((fmax / fs.s_agsize +
260 ((fmax % fs.s_agsize) >= fs.s_agsize / INOPB ? 1 : 0))
262 #endif /* AFS_AIX41_ENV */
265 * check for "FORCESALVAGE" equivalent:
266 * LAST_RSVD_I is a vice inode, with dead beef, and
267 * di_nlink == 2 to indicate the FORCE.
269 osi_Assert(p = ginode(LAST_RSVD_I));
271 if (p->di_vicemagic == VICEMAGIC && p->di_vicep1 == 0xdeadbeef
272 && p->di_nlink == 2) {
274 idec(root_inode.st_dev, LAST_RSVD_I, 0xdeadbeef);
277 for (inum = LAST_RSVD_I + 1; inum <= imax; ++inum) {
278 if ((p = ginode(inum)) == NULL || p->di_vicemagic != VICEMAGIC
279 || (p->di_mode & IFMT) != IFREG)
282 info.inodeNumber = inum;
283 info.byteCount = p->di_size;
284 info.linkCount = p->di_nlink;
285 info.u.param[0] = p->di_vicep1;
286 info.u.param[1] = p->di_vicep2;
287 info.u.param[2] = p->di_vicep3;
288 info.u.param[3] = p->di_vicep4;
290 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
293 if (inodeFile != INVALID_FD) {
294 if (OS_WRITE(inodeFile, &info, sizeof(info)) != sizeof(info)) {
295 Log("Error writing inode file for partition %s\n", partition);
302 if (inodeFile != INVALID_FD) {
303 if (OS_SYNC(inodeFile) == -1) {
304 Log("Unable to successfully fsync inode file for %s\n", partition);
310 * Paranoia: check that the file is really the right size
312 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
313 Log("Wrong size (%d instead of %d) in inode file for %s\n",
314 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
326 if (pfd != INVALID_FD)
332 /* Read in the superblock for devName */
334 ReadSuper(struct superblock *fs, char *devName)
338 pfd = OS_OPEN(devName, O_RDONLY, 0666);
339 if (pfd == INVALID_FD) {
340 Log("Unable to open inode on %s for reading superblock.\n", devName);
344 if (bread(pfd, fs, SUPER_B, sizeof(struct superblock)) < 0) {
345 Log("Unable to read superblock on %s.\n", devName);
353 /* IsBigFilesFileSystem returns 1 if it's a big files filesystem, 0 otherwise. */
355 IsBigFilesFileSystem(struct superblock *sb)
357 if ((strncmp(sb->s_magic, fsv3pmagic, 4) == 0)
358 && (sb->s_version == fsbigfile)
372 static char buf[FSBSIZE];
373 static daddr_t last_blk = -1;
379 0) ? INODES_B + inum / INOPB : ag * agblocks + (inum -
382 #else /* AFS_AIX41_ENV */
383 ag = inum / fs.s_agsize;
386 0) ? INODES_B + inum / INOPB : ag * fs.s_agsize + (inum -
390 #endif /* AFS_AIX41_ENV */
392 if (last_blk != pblk) {
393 if (bread(pfd, buf, pblk, sizeof(buf)) < 0) {
400 dp = (struct dinode *)buf;
405 #else /* !AFS_AIX31_ENV */
407 #if defined(AFS_SGI_ENV)
409 /* libefs.h includes <assert.h>, which we don't want */
412 #ifdef AFS_SGI_XFS_IOPS_ENV
414 #include <afs/xfsattrs.h>
415 /* xfs_ListViceInodes
417 * xfs_ListViceInodes verifies and correct the XFS namespace as it collects
418 * the inode information. The name is required for the idec operation to work.
419 * Steps 2 and 3 below are for the AFS_XFS_NAME_VERS == 1. If the name space
420 * changes, the algorithm will need to change.
421 * 1) If the parent inode number does not match the directory's inod number,
422 * change it in the attribute.
423 * 2) If the unqifier in the attribute does not match the name, rename the
424 * file. This is done by doing an exclusive open, incrementing the tag
425 * number until a file can be created. If the tag changes, then the
426 * attribute will need updating.
427 * 3) If the tag in the attribute does not match the name, change the
429 * 4) Verify uid = RW volume id and gid = XFS_VICEMAGIC.
435 * Does the verifications listed above.
436 * We can't change the names until the readdir is complete, so we set the
437 * rename flag if the file needs renaming.
440 xfs_VerifyInode(char *dir, uint64_t pino, char *name, i_list_inode_t * info,
447 int update_chown = 0;
455 (void)sprintf(path, "%s/%s", dir, name);
456 /* Verify uid and gid fields */
457 if (info->ili_magic != XFS_VICEMAGIC) {
458 Log("%s magic for %s/%s (inode %s) from %d to %d\n",
459 Testing ? "Would have changed" : "Changing", dir, name,
460 PrintInode(stmp, info->ili_info.inodeNumber), info->ili_magic,
466 vno = info->ili_info.param[0];
467 if (info->ili_vno != AFS_XFS_VNO_CLIP(vno)) {
468 Log("%s volume id for %s/%s (inode %s) from %d to %d\n",
469 Testing ? "Would have changed" : "Changing", dir, name,
470 PrintInode(stmp, info->ili_info.inodeNumber), info->ili_vno,
471 AFS_XFS_VNO_CLIP(vno));
477 if (chown(path, AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC) < 0) {
478 Log("Can't chown %s to uid=%d, gid=0x%x\n", path,
479 AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC);
484 /* Check Parent inode number. */
485 if (info->ili_pino != pino) {
486 afs_ino_str_t sino, sipino, spino;
487 (void)PrintInode(sino, info->ili_info.inodeNumber);
488 (void)PrintInode(sipino, info->ili_pino);
489 (void)PrintInode(spino, pino);
490 Log("%s parent ino for %s (inode %s) from %s to %s.\n",
491 Testing ? "Would have changed" : "Changing", path, sino, sipino,
497 /* Verify the file name. */
498 (void)strcpy(tmpName, ".");
499 (void)strcat(tmpName, int_to_base64(stmp, info->ili_info.param[2]));
500 if (strncmp(name, tmpName, strlen(tmpName))) {
501 Log("%s name %s (inode %s) in directory %s, unique=%d, tag=%d\n",
502 Testing ? "Would have returned bad" : "Bad", name,
503 PrintInode(stmp, info->ili_info.inodeNumber), dir,
504 info->ili_info.param[2], info->ili_tag);
510 /* update the tag? */
511 (void)strcat(tmpName, ".");
512 (void)strcat(tmpName, int_to_base64(stmp, info->ili_tag));
513 if (strcmp(name, tmpName)) {
515 (void)strcpy(tmpName, name);
516 p = strchr(tmpName + 1, '.');
518 Log("No tag found on name %s (inode %s)in directory, %s.\n",
519 name, PrintInode(stmp, info->ili_info.inodeNumber), dir,
520 Testing ? "would have renamed" : "will rename");
524 tag = base64_to_int(p + 1);
525 Log("%s the tag for %s (inode %s) from %d to %d.\n",
526 Testing ? "Would have changed" : "Will change", path,
527 PrintInode(stmp, info->ili_info.inodeNumber), dir, tag,
535 if (update_pino || update_tag) {
536 afs_xfs_attr_t attrs;
539 length = SIZEOF_XFS_ATTR_T;
540 if (attr_get(path, AFS_XFS_ATTR, (char *)&attrs, &length, ATTR_ROOT) <
542 Log("Can't get AFS attribute for %s\n", path);
546 attrs.at_pino = pino;
550 (path, AFS_XFS_ATTR, (char *)&attrs, length,
551 ATTR_ROOT | ATTR_REPLACE) < 0) {
552 Log("Can't set AFS attribute into %s\n", path);
566 xfs_RenameFiles(char *dir, xfs_Rename_t * renames, int n_renames)
569 char opath[128], nbase[128], npath[128];
570 afs_xfs_attr_t attrs;
571 int length = SIZEOF_XFS_ATTR_T;
576 for (i = 0; i < n_renames; i++) {
577 (void)sprintf(opath, "%s/%s", dir, renames[i].name);
578 (void)sprintf(nbase, "%s/.%s", dir,
579 int_to_base64(stmp, renames[i].uniq));
580 for (tag = 2, j = 0; j < 64; tag++, j++) {
581 (void)sprintf(npath, "%s.%s", nbase, int_to_base64(stmp, tag));
582 fd = afs_open(npath, O_CREAT | O_EXCL | O_RDWR, 0);
589 Log("Can't find a new name for %s\n", opath);
592 if (rename(opath, npath) < 0) {
593 Log("Can't rename %s to %s\n", opath, npath);
596 Log("Renamed %s to %s\n", opath, npath);
603 xfs_ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
604 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep,
605 int forceR, char *wpath, void *rock)
608 int info_size = sizeof(i_list_inode_t);
611 dirent64_t *top_direntp;
613 dirent64_t *vol_direntp;
614 struct stat64 sdirbuf;
615 struct stat64 sfilebuf;
616 afs_xfs_attr_t attrs;
617 afs_xfs_dattr_t dattrs;
619 char vol_dirname[1024];
622 xfs_Rename_t *renames = (xfs_Rename_t *) 0;
624 #define N_RENAME_STEP 64
632 if (stat64(mountedOn, &sdirbuf) < 0) {
633 perror("xfs_ListViceInodes: stat64");
637 if ((top_dirp = opendir(mountedOn)) == NULL) {
638 Log("Can't open directory %s to read inodes.\n", mountedOn);
642 while (top_direntp = readdir64(top_dirp)) {
643 /* Only descend directories with the AFSDIR attribute set.
644 * Could also verify the contents of the atribute, but currently
646 * Performance could be improved for single volume salvages by
647 * only going through the directory containing the volume's inodes.
648 * But I'm being complete as a first pass.
650 (void)sprintf(vol_dirname, "%s/%s", mountedOn, top_direntp->d_name);
651 length = SIZEOF_XFS_DATTR_T;
653 (vol_dirname, AFS_XFS_DATTR, (char *)&dattrs, &length, ATTR_ROOT))
656 if ((vol_dirp = opendir(vol_dirname)) == NULL) {
657 if (errno == ENOTDIR)
659 Log("Can't open directory %s to read inodes.\n", vol_dirname);
663 pino = top_direntp->d_ino;
665 while (vol_direntp = readdir64(vol_dirp)) {
666 if (vol_direntp->d_name[1] == '\0'
667 || vol_direntp->d_name[1] == '.')
670 info.ili_version = AFS_XFS_ILI_VERSION;
671 info_size = sizeof(i_list_inode_t);
673 ilistinode64(sdirbuf.st_dev, vol_direntp->d_ino, &info,
676 /* Where possible, give more explicit messages. */
680 Log("%s (device id %d) is not on an XFS filesystem.\n",
681 vol_dirname, sdirbuf.st_dev);
686 if (info_size != sizeof(i_list_inode_t)
687 || info.ili_version != AFS_XFS_ILI_VERSION) {
688 Log("Version skew between kernel and salvager.\n");
693 /* Continue, so we collect all the errors in the first pass. */
694 Log("Error listing inode named %s/%s: %s\n", vol_dirname,
695 vol_direntp->d_name, strerror(errno));
700 if (info.ili_attr_version != AFS_XFS_ATTR_VERS) {
701 Log("Unrecognized XFS attribute version %d in %s/%s. Upgrade salvager\n", info.ili_attr_version, vol_dirname, vol_direntp->d_name);
705 if (judgeInode && (*judgeInode) (&info.ili_info, judgeParam, rock) == 0)
710 (vol_dirname, pino, vol_direntp->d_name, &info,
716 /* Add this name to the list of items to rename. */
717 if (n_renames >= n_avail) {
718 n_avail += N_RENAME_STEP;
719 if (n_avail == N_RENAME_STEP)
720 renames = (xfs_Rename_t *)
721 malloc(n_avail * sizeof(xfs_Rename_t));
723 renames = (xfs_Rename_t *)
724 realloc((char *)renames,
725 n_avail * sizeof(xfs_Rename_t));
727 Log("Can't %salloc %lu bytes for rename list.\n",
728 (n_avail == N_RENAME_STEP) ? "m" : "re",
729 n_avail * sizeof(xfs_Rename_t));
733 (void)strcpy(renames[n_renames].name, vol_direntp->d_name);
734 renames[n_renames].uniq = info.ili_info.param[2];
738 if (inodeFile != INVALID_FD) {
740 (inodeFile, &info.ili_info, sizeof(vice_inode_info_t))
741 != sizeof(vice_inode_info_t)) {
742 Log("Error writing inode file for partition %s\n", mountedOn);
748 } /* end while vol_direntp */
751 vol_dirp = (DIR *) 0;
753 Log("Renaming files.\n");
754 if (xfs_RenameFiles(vol_dirname, renames, n_renames) < 0) {
762 free((char *)renames);
763 if (inodeFile != INVALID_FD) {
764 if (OS_SYNC(inodeFile) == -1) {
765 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
766 return errors ? -1 : -2;
769 * Paranoia: check that the file is really the right size
771 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
772 Log("Wrong size (%d instead of %d) in inode file for %s\n",
773 OS_SIZE(inodeFile), ninodes * sizeof(struct ViceInodeInfo),
775 return errors ? -1 : -2;
780 Log("Errors encontered listing inodes, not salvaging partition.\n");
792 free((char *)renames);
799 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
800 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
801 char *wpath, void *rock)
803 char dev[50], rdev[51];
805 struct efs_dinode *p;
806 struct ViceInodeInfo info;
807 struct stat root_inode;
808 int ninodes = 0, err = 0;
809 struct efs_dinode *dinodeBuf = NULL;
811 ino_t imax, inum; /* total number of I-nodes in file system */
815 sleep(1); /* simulate operator */
821 if (stat(mountedOn, &root_inode) < 0) {
822 Log("cannot stat: %s\n", mountedOn);
825 #ifdef AFS_SGI_XFS_IOPS_ENV
826 if (!strcmp("xfs", root_inode.st_fstype)) {
827 return xfs_ListViceInodes(devname, mountedOn, inodeFile, judgeInode,
828 judgeParam, forcep, forceR, wpath, rock);
832 Log("%s is not root of a filesystem\n", mountedOn);
837 #else /* AFS_SGI_ENV */
840 #define SPERB (MAXBSIZE / sizeof(short))
841 #define MAXNINDIR (MAXBSIZE / sizeof(daddr_t))
844 struct bufarea *b_next; /* must be first */
848 char b_buf[MAXBSIZE]; /* buffer space */
849 short b_lnks[SPERB]; /* link counts */
850 daddr_t b_indir[MAXNINDIR]; /* indirect block */
851 struct fs b_fs; /* super block */
852 struct cg b_cg; /* cylinder group */
856 typedef struct bufarea BUFAREA;
859 #define sblock sblk.b_un.b_fs
860 #endif /* AFS_HPUX_ENV */
862 extern char *afs_rawname();
864 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
865 int (*judgeInode) (), afs_uint32 judgeParam, int *forcep, int forceR,
866 char *wpath, void *rock)
872 #else /* !AFS_AIX_ENV */
877 int i, c, e, bufsize, code, err = 0;
878 char dev[50], rdev[100], err1[512], *ptr1;
879 struct dinode *inodes = NULL, *einodes, *dptr;
882 struct ViceInodeInfo info;
885 partition = mountedOn;
886 sprintf(rdev, "%s/%s", wpath, devname);
887 ptr1 = afs_rawname(rdev);
891 /* Bletch: this is terrible; is there a better way to do this? Does this work? vfsck doesn't even sleep!! */
893 sleep(5); /* Trying a smaller one for aix */
898 pfd = OS_OPEN(rdev, O_RDONLY, 0666);
899 if (pfd == INVALID_FD) {
900 sprintf(err1, "Could not open device %s to get inode list\n", rdev);
905 if (bread(pfd, (char *)&super.fs, SUPERB, sizeof super.fs) == -1) {
908 if (bread(pfd, (char *)&sblock, SBLOCK, SBSIZE) == -1) {
910 if (bread(pfd, super.block, SBLOCK, SBSIZE) == -1) {
911 #endif /* AFS_HPUX_ENV */
913 Log("Unable to read superblock, partition %s\n", partition);
919 * char *FSlabel(), *fslabel=0;
920 * fslabel = FSlabel(&super.fs);
922 if (super.fs.s_bsize == 0)
923 super.fs.s_bsize = 512;
924 if (super.fs.s_bsize != BSIZE) {
925 Log("SuperBlk: Cluster size not %d; run vfsck\n", BSIZE);
928 fmax = super.fs.s_fsize; /* first invalid blk num */
929 imax = ((ino_t) super.fs.s_isize - (SUPERB + 1)) * INOPB;
931 Log("Size check: imax==0!\n");
934 if (GetAuxInodeFile(partition, &status) == 0) {
935 Log("Can't access Aux inode file for partition %s, aborting\n",
939 for (inum = 1; inum <= imax; inum++) {
940 struct dauxinode *auxp;
941 if ((auxp = IsAfsInode(inum)) == NULL) {
942 /* Not an afs inode, keep going */
945 if ((p = ginode(inum)) == NULL)
947 /* deleted/non-existent inode when di_mode == 0 */
950 info.inodeNumber = (int)inum;
951 info.byteCount = p->di_size;
952 info.linkCount = p->di_nlink;
953 info.u.param[0] = auxp->aux_param1;
954 info.u.param[1] = auxp->aux_param2;
955 info.u.param[2] = auxp->aux_param3;
956 info.u.param[3] = auxp->aux_param4;
957 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
959 if (inodeFile != INVALID_FD) {
960 if (OS_WRITE(inodeFile, &info, sizeof(info)) != sizeof(info)) {
961 Log("Error writing inode file for partition %s\n", partition);
969 * run a few consistency checks of the superblock
970 * (Cribbed from vfsck)
973 #if defined(FD_FSMAGIC)
974 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)
975 && (sblock.fs_magic != FD_FSMAGIC)
976 #if defined(AFS_HPUX101_ENV)
977 && (sblock.fs_magic != FD_FSMAGIC_2)
981 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)) {
983 Log("There's something wrong with the superblock for partition %s; bad magic (%d) run vfsck\n", partition, sblock.fs_magic);
986 if (sblock.fs_ncg < 1) {
987 Log("There's something wrong with the superblock for partition %s; NCG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_ncg);
990 if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG) {
991 Log("There's something wrong with the superblock for partition %s; CPG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_cpg);
994 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl
995 || (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) {
996 Log("There's something wrong with the superblock for partition %s; NCYL LESS THAN NCG*CPG run vfsck\n", partition);
999 if (sblock.fs_sbsize > SBSIZE) {
1000 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);
1004 if ((super.fs.fs_magic != FS_MAGIC)
1005 || (super.fs.fs_ncg < 1)
1006 #if defined(AFS_SUN_ENV) || defined(AFS_OSF_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
1007 || (super.fs.fs_cpg < 1)
1009 || (super.fs.fs_cpg < 1 || super.fs.fs_cpg > MAXCPG)
1011 || (super.fs.fs_ncg * super.fs.fs_cpg < super.fs.fs_ncyl
1012 || (super.fs.fs_ncg - 1) * super.fs.fs_cpg >= super.fs.fs_ncyl)
1013 || (super.fs.fs_sbsize > SBSIZE)) {
1014 Log("There's something wrong with the superblock for partition %s; run vfsck\n", partition);
1017 #endif /* AFS_HPUX_ENV */
1020 bufsize = sblock.fs_ipg * sizeof(struct dinode);
1022 bufsize = super.fs.fs_ipg * sizeof(struct dinode);
1023 #endif /* AFS_HPUX_ENV */
1024 inodes = (struct dinode *)malloc(bufsize);
1025 einodes = (struct dinode *)(((char *)inodes) + bufsize);
1026 if (inodes == NULL) {
1027 Log("Unable to allocate enough memory to scan inodes; help!\n");
1030 Log("Scanning inodes on device %s...\n", rdev);
1032 for (c = 0; c < sblock.fs_ncg; c++) {
1033 i = c * sblock.fs_ipg;
1034 e = i + sblock.fs_ipg;
1035 #if defined(AFS_HPUX102_ENV)
1036 if (OS_SEEK(pfd, dbtoo(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1039 if (OS_SEEK(pfd, dbtob(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1043 for (c = 0; c < super.fs.fs_ncg; c++) {
1045 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1047 #if defined(AFS_DARWIN_ENV)
1048 #define offset_t off_t
1049 #define llseek lseek
1052 #endif /* AFS_SUN5_ENV */
1053 i = c * super.fs.fs_ipg;
1054 e = i + super.fs.fs_ipg;
1056 dblk1 = fsbtodb(&super.fs, itod(&super.fs, i));
1057 if (OS_SEEK(pfd, (off_t) ((off_t) dblk1 * DEV_BSIZE), L_SET) == -1) {
1059 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1060 f1 = fsbtodb(&super.fs, itod(&super.fs, i));
1061 off = (offset_t) f1 << DEV_BSHIFT;
1062 if (OS_SEEK(pfd, off, L_SET) == -1) {
1064 if (OS_SEEK(pfd, dbtob(fsbtodb(&super.fs, itod(&super.fs, i))), L_SET)
1066 #endif /* AFS_SUN5_ENV */
1067 #endif /* AFS_OSF_ENV */
1068 #endif /* AFS_HPUX_ENV */
1069 Log("Error reading inodes for partition %s; run vfsck\n",
1075 if (OS_READ(pfd, inodes, bufsize) != bufsize) {
1076 Log("Error reading inodes for partition %s; run vfsck\n",
1083 for (bj = bk = 0; bj < bufsize; bj = bj + 512, bk++) {
1084 if ((code = OS_READ(pfd, dptr, 512)) != 512) {
1085 Log("Error reading inode %d? for partition %s (errno = %d); run vfsck\n", bk + i, partition, errno);
1086 if (OS_SEEK(pfd, 512, L_SET) == -1) {
1087 Log("OS_SEEK failed\n");
1102 for (p = inodes; p < einodes && i < e; i++, p++) {
1104 Log("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1105 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1106 p->di_size, p->di_nlink);
1108 ("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1109 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1110 p->di_size, p->di_nlink);
1113 #ifdef AFS_3DISPARES
1114 /* Check to see if this inode is a pre-"OSF1 4.0D" inode */
1115 if ((p->di_uid || p->di_gid)
1116 && !(p->di_flags & (IC_XUID | IC_XGID))) {
1117 Log("Found unconverted inode %d: Use 'fs_conv_dux40D convert' on partition %s\n", i, partition);
1121 osi_Panic("Tru64 needs AFS_3DISPARES\n");
1124 #if defined(AFS_SUN56_ENV)
1125 /* if this is a pre-sol2.6 unconverted inode, bail out */
1127 afs_uint32 p1, p2, p3, p4;
1131 q = (quad *) & (p->di_ic.ic_lsize);
1133 p2 = p->di_ic.ic_flags;
1135 p4 = p->di_ic.ic_uid;
1136 p5 = p->di_ic.ic_gid;
1138 if ((p2 || p3) && !p4 && (p5 == -2)) {
1139 Log("Found unconverted inode %d\n", i);
1140 Log("You should run the AFS file conversion utility\n");
1145 if (IS_DVICEMAGIC(p) && (p->di_mode & IFMT) == IFREG) {
1146 afs_uint32 p2 = p->di_vicep2, p3 = DI_VICEP3(p);
1148 info.u.param[0] = p->di_vicep1;
1149 #ifdef AFS_3DISPARES
1150 if (((p2 >> 3) == INODESPECIAL) && (p2 & 0x3)) {
1151 info.u.param[1] = INODESPECIAL;
1152 info.u.param[2] = p3;
1153 info.u.param[3] = p2 & 0x3;
1155 info.u.param[1] = ((p2 >> 27) << 16) + (p3 & 0xffff);
1156 info.u.param[2] = (p2 & 0x3fffff);
1158 (((p2 >> 22) & 0x1f) << 16) + (p3 >> 16);
1161 info.u.param[1] = p->di_vicep2;
1162 info.u.param[2] = DI_VICEP3(p);
1163 info.u.param[3] = p->di_vicep4;
1165 info.inodeNumber = i;
1166 info.byteCount = p->di_size;
1167 info.linkCount = p->di_nlink;
1168 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1170 if (inodeFile != INVALID_FD) {
1171 if (OS_WRITE(inodeFile, &info, sizeof(info)) != sizeof(info)) {
1172 Log("Error writing inode file for partition %s\n",
1185 if (inodeFile != INVALID_FD) {
1186 if (OS_SYNC(inodeFile) == -1) {
1187 Log("Unable to successfully fsync inode file for %s\n", partition);
1193 * Paranoia: check that the file is really the right size
1195 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
1196 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1197 OS_SIZE(inodeFile), ninodes * sizeof(struct ViceInodeInfo),
1214 #endif /* !AFS_SGI_ENV */
1215 #endif /* !AFS_AIX31_ENV */
1217 #ifdef AFS_DARWIN_ENV
1219 #define dbtob(db) ((unsigned)(db) << DEV_BSHIFT)
1223 bread(FD_t fd, char *buf, daddr_t blk, afs_int32 size)
1226 #ifdef AFS_AIX41_ENV
1227 offset_t off = (offset_t) blk << FSBSHIFT;
1228 if (OS_SEEK(fd, off, 0) < 0) {
1229 Log("Unable to seek to offset %llu for block %u\n", off, blk);
1232 #else /* AFS_AIX41_ENV */
1233 if (OS_SEEK(fd, blk * Bsize, 0) < 0) {
1234 Log("Unable to seek to offset %u for block %u\n", blk * Bsize, blk);
1236 #endif /* AFS_AIX41_ENV */
1238 if (OS_SEEK(fd, (off_t) dbtob(blk), L_SET) < 0) {
1239 Log("Unable to seek to offset %u for block %u\n", dbtob(blk), blk);
1242 if (OS_READ(fd, buf, size) != size) {
1243 Log("Unable to read block %d, partition %s\n", blk, partition);
1249 #endif /* AFS_LINUX20_ENV */
1251 convertVolumeInfo(FdHandle_t *fdhr, FdHandle_t *fdhw, afs_uint32 vid)
1253 struct VolumeDiskData vd;
1256 if (FDH_PREAD(fdhr, &vd, sizeof(struct VolumeDiskData), 0) !=
1257 sizeof(struct VolumeDiskData)) {
1258 Log("1 convertiVolumeInfo: read failed for %lu with code %d\n", vid,
1262 vd.restoredFromId = vd.id; /* remember the RO volume here */
1264 vd.id = vd.parentId;
1268 vd.uniquifier += 5000; /* just in case there are still file copies
1269 from the old RW volume around */
1271 /* For ROs, the copyDate contains the time that the RO volume was actually
1272 * created, and the creationDate just contains the last time the RO was
1273 * copied from the RW data. So, make the new RW creationDate more accurate
1274 * by setting it to copyDate, if copyDate is older. Since, we know the
1275 * volume is at least as old as copyDate. */
1276 if (vd.copyDate < vd.creationDate) {
1277 vd.creationDate = vd.copyDate;
1279 /* If copyDate is newer, just make copyDate and creationDate the same,
1280 * for consistency with other RWs */
1281 vd.copyDate = vd.creationDate;
1284 p = strrchr(vd.name, '.');
1285 if (p && !strcmp(p, ".readonly")) {
1289 if (FDH_PWRITE(fdhw, &vd, sizeof(struct VolumeDiskData), 0) !=
1290 sizeof(struct VolumeDiskData)) {
1291 Log("1 convertiVolumeInfo: write failed for %lu with code %d\n", vid,
1299 afs_int32 inodeType;
1306 UpdateThisVolume(struct ViceInodeInfo *inodeinfo, VolumeId singleVolumeNumber,
1307 struct specino *specinos)
1310 if ((inodeinfo->u.vnode.vnodeNumber == INODESPECIAL) &&
1311 (inodeinfo->u.vnode.volumeId == singleVolumeNumber)) {
1312 specinos[inodeinfo->u.special.type].inodeNumber =
1313 inodeinfo->inodeNumber;
1315 return 0; /* We aren't using a result file, we're caching */
1319 getDevName(char *pbuffer, char *wpath)
1321 char pbuf[128], *ptr;
1322 strcpy(pbuf, pbuffer);
1323 ptr = (char *)strrchr(pbuf, OS_DIRSEPC);
1326 strcpy(wpath, pbuf);
1329 ptr = (char *)strrchr(pbuffer, OS_DIRSEPC);
1331 strcpy(pbuffer, ptr + 1);
1337 #ifdef FSSYNC_BUILD_CLIENT
1339 inode_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
1341 char dir_name[512], oldpath[512], newpath[512];
1343 char headername[16];
1345 int fd, err, forcep, j;
1346 ssize_t len, nBytes;
1348 struct DiskPartition64 *partP;
1349 struct ViceInodeInfo info;
1350 struct VolumeDiskHeader h;
1351 IHandle_t *ih, *ih2;
1352 FdHandle_t *fdP, *fdP2;
1355 char tmpDevName[100];
1357 struct specino specinos[VI_LINKTABLE+1];
1358 Inode nearInode = 0;
1359 # ifdef AFS_DEMAND_ATTACH_FS
1361 # endif /* AFS_DEMAND_ATTACH_FS */
1364 memset(&specinos, 0, sizeof(specinos));
1366 /* now do the work */
1368 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
1369 partP = partP->next);
1371 Log("1 inode_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
1376 #ifdef AFS_DEMAND_ATTACH_FS
1377 locktype = VVolLockType(V_VOLUPD, 1);
1378 code = VLockVolumeByIdNB(volumeId, partP, locktype);
1386 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
1387 Log("1 inode_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
1388 afs_printable_uint32_lu(volumeId));
1392 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
1394 strcpy(tmpDevName, partP->devName);
1395 name = getDevName(tmpDevName, wpath);
1397 if ((err = ListViceInodes(name, VPartitionPath(partP),
1398 NULL, UpdateThisVolume, volumeId,
1399 &forcep, 0, wpath, &specinos)) < 0)
1401 Log("1 inode_ConvertROtoRWvolume: Couldn't get special inodes\n");
1406 #if defined(NEARINODE_HINT)
1407 nearInodeHash(volumeId, nearInode);
1408 nearInode %= partP->f_files;
1411 for (j = VI_VOLINFO; j < VI_LINKTABLE+1; j++) {
1412 if (specinos[j].inodeNumber > 0) {
1413 specinos[j].ninodeNumber =
1414 IH_CREATE(NULL, partP->device, VPartitionPath(partP),
1415 nearInode, h.parent, INODESPECIAL, j, h.parent);
1416 IH_INIT(ih, partP->device, volumeId,
1417 specinos[j].inodeNumber);
1420 Log("1 inode_ConvertROtoRWvolume: Couldn't find special inode %d for %d\n", j, volumeId);
1425 IH_INIT(ih2, partP->device, h.parent, specinos[j].ninodeNumber);
1426 fdP2 = IH_OPEN(ih2);
1428 Log("1 inode_ConvertROtoRWvolume: Couldn't find special inode %d for %d\n", j, h.parent);
1433 if (j == VI_VOLINFO)
1434 convertVolumeInfo(fdP, fdP2, ih2->ih_vid);
1438 len = FDH_PREAD(fdP, buffer, sizeof(buffer), offset);
1443 nBytes = FDH_PWRITE(fdP2, buffer, len, offset);
1444 if (nBytes != len) {
1455 /* Unlink the old special inode; otherwise we will get duplicate
1456 * special inodes if we recreate the RO again */
1457 if (IH_DEC(ih, specinos[j].inodeNumber, volumeId) == -1) {
1459 Log("IH_DEC failed: %x, %s, %u errno %d\n", ih,
1460 PrintInode(stmp, specinos[j].inodeNumber), volumeId, errno);
1469 #ifdef AFS_64BIT_IOPS_ENV
1470 h.volumeInfo_lo = (afs_int32)specinos[VI_VOLINFO].ninodeNumber & 0xffffffff;
1471 h.volumeInfo_hi = (afs_int32)(specinos[VI_VOLINFO].ninodeNumber >> 32) && 0xffffffff;
1472 h.smallVnodeIndex_lo = (afs_int32)specinos[VI_SMALLINDEX].ninodeNumber & 0xffffffff;
1473 h.smallVnodeIndex_hi = (afs_int32)(specinos[VI_SMALLINDEX].ninodeNumber >> 32) & 0xffffffff;
1474 h.largeVnodeIndex_lo = (afs_int32)specinos[VI_LARGEINDEX].ninodeNumber & 0xffffffff;
1475 h.largeVnodeIndex_hi = (afs_int32)(specinos[VI_LARGEINDEX].ninodeNumber >> 32) & 0xffffffff;
1476 if (specinos[VI_LINKTABLE].ninodeNumber) {
1477 h.linkTable_lo = (afs_int32)specinos[VI_LINKTABLE].ninodeNumber & 0xffffffff;
1478 h.linkTable_hi = (afs_int32)specinos[VI_LINKTABLE].ninodeNumber & 0xffffffff;
1481 h.volumeInfo_lo = specinos[VI_VOLINFO].ninodeNumber;
1482 h.smallVnodeIndex_lo = specinos[VI_SMALLINDEX].ninodeNumber;
1483 h.largeVnodeIndex_lo = specinos[VI_LARGEINDEX].ninodeNumber;
1484 if (specinos[VI_LINKTABLE].ninodeNumber) {
1485 h.linkTable_lo = specinos[VI_LINKTABLE].ninodeNumber;
1489 if (VCreateVolumeDiskHeader(&h, partP)) {
1490 Log("1 inode_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
1491 afs_printable_uint32_lu(h.id));
1496 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
1497 Log("1 inode_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
1498 afs_printable_uint32_lu(volumeId));
1501 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
1502 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
1505 # ifdef AFS_DEMAND_ATTACH_FS
1507 VUnlockVolumeById(volumeId, partP);
1509 # endif /* AFS_DEMAND_ATTACH_FS */
1512 #endif /* FSSYNC_BUILD_CLIENT */
1513 #endif /* AFS_NAMEI_ENV */