2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
14 Institution: The Information Technology Center, Carnegie-Mellon University
18 #define ITC /* Required by inode.h */
20 #include <afsconfig.h>
21 #include <afs/param.h>
29 #if defined(AFS_LINUX20_ENV)
34 * -1 - Unable to read the inodes.
35 * -2 - Unable to completely write temp file. Produces warning message in log.
38 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
39 afs_uint32 (*judgeInode) (), VolumeId judgeParam, int *forcep, int forceR,
40 char *wpath, void *rock)
42 Log("ListViceInodes not implemented for this platform!\n");
46 #if !defined(AFS_SGI_ENV)
47 #ifdef AFS_VFSINCL_ENV
50 #include <sys/fs/ufs_fs.h>
52 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
53 #include <ufs/ufs/dinode.h>
54 #include <ufs/ffs/fs.h>
55 #define itod ino_to_fsba
60 #else /* AFS_VFSINCL_ENV */
62 #include <sys/filsys.h>
66 #endif /* AFS_VFSINCL_ENV */
67 #ifdef AFS_VFSINCL_ENV
68 #include <sys/vnode.h>
70 #include <sys/fs/ufs_inode.h>
72 #if !defined(AFS_DARWIN_ENV)
73 #include <ufs/inode.h>
76 #else /* AFS_VFSINCL_ENV */
77 #include <sys/inode.h>
78 #endif /* AFS_VFSINCL_ENV */
79 #endif /* AFS_SGI_ENV */
80 #include <afs/osi_inode.h>
83 #include <afs/afsint.h>
85 #include <afs/afssyscalls.h>
86 #include "viceinode.h"
87 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV)
91 #include <rx/rx_queue.h>
97 #include "volinodes.h"
98 #include "partition.h"
100 #include "volume_inline.h"
102 /* Notice: parts of this module have been cribbed from vfsck.c */
105 static char *partition;
110 #include <jfs/filsys.h>
113 #define FSBSIZE (4096) /* filesystem block size */
114 #define FSBSHIFT (12) /* log2(FSBSIZE) */
115 #define FSBMASK (FSBSIZE - 1) /* FSBSIZE mask */
117 #define MIN_FSIZE DISKMAP_B /* minimum fs size (FSblocks) */
118 #define LAST_RSVD_I 15 /* last reserved inode */
123 * This will hopefully eventually make it into the system include files
125 #define INOPB (FSBSIZE / sizeof (struct dinode))
133 #endif /* AFS_AIX41_ENV */
136 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
137 XX This was lifted from some `com/cmd/fs/fshlpr_aix3/Fs.h', which indicated X
138 XX a longing to see it make it into a readily accessible include file. XXXXXX
139 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
141 * itoo - inode number to offset within disk block
144 #define itoo(x) (int) ((unsigned)(x) % INOPB)
146 int Bsize = FSBSIZE; /* block size for this system */
147 daddr_t fmax; /* total number of blocks n file system */
148 ino_t imax, inum; /* total number of I-nodes in file system */
150 static struct superblock fs;
151 struct dinode *ginode();
155 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
156 int (*judgeInode) (), VolumeId judgeParam, int *forcep, int forceR,
157 char *wpath, void *rock)
159 char dev[50], rdev[51];
162 struct ViceInodeInfo info;
163 struct stat root_inode;
164 int ninodes = 0, err = 0;
166 pfd = INVALID_FD; /* initialize so we don't close on error output below. */
169 sleep(1); /* simulate operator */
175 partition = mountedOn;
176 sprintf(dev, "/dev/%s", devname);
177 sprintf(rdev, "/dev/r%s", devname);
179 if (stat(mountedOn, &root_inode) < 0) {
180 Log("cannot stat: %s\n", mountedOn);
184 if (root_inode.st_ino != ROOTDIR_I) {
185 Log("%s is not root of a filesystem\n", mountedOn);
191 * done with the superblock, now try to read the raw device.
193 if (ReadSuper(&fs, dev) < 0)
198 case FM_CLEAN: /* clean and unmounted */
199 Log("Most peculiar - Super blk in FM_CLEAN state!\n");
201 case FM_MOUNT: /* mounted cleanly */
204 case FM_MDIRTY: /* dirty when mounted or commit fail */
205 case FM_LOGREDO: /* log redo attempted but failed */
206 Log("File system %s is in a bad state.\n", rdev);
207 Log("Call your IBM representative.\n");
211 if (IsBigFilesFileSystem(&fs, (char *)0)) {
212 Log("%s is a big files filesystem, can't salvage.\n", mountedOn);
216 if (strncmp(fs.s_magic, fsv3magic, strlen(fsv3magic)) != 0) {
218 if ((strncmp(fs.s_magic, fsv3pmagic, strlen(fsv3pmagic)) != 0)
219 || (fs.s_version != fsv3pvers)) {
220 Log("Super block doesn't have the problem magic (%s vs v3magic %s v3pmagic %s)\n", fs.s_magic, fsv3magic, fsv3pmagic);
224 Log("Super block doesn't have the problem magic (%s vs v3magic %s)\n",
225 fs.s_magic, fsv3magic);
232 fragsize = (fs.s_fragsize) ? fs.s_fragsize : FSBSIZE;
233 iagsize = (fs.s_iagsize) ? fs.s_iagsize : fs.s_agsize;
234 ag512 = fragsize * fs.s_agsize / 512;
235 agblocks = fragsize * fs.s_agsize >> BSHIFT;
236 #endif /* AFS_AIX41_ENV */
238 fmax = fs.s_fsize / (FSBSIZE / 512); /* first invalid blk num */
240 pfd = OS_OPEN(rdev, O_RDONLY, 0666);
241 if (pfd == INVALID_FD) {
242 Log("Unable to open `%s' inode for reading\n", rdev);
247 * calculate the maximum number of inodes possible
250 imax = iagsize * (fs.s_fsize / ag512) - 1;
251 #else /* AFS_AIX41_ENV */
253 ((fmax / fs.s_agsize +
254 ((fmax % fs.s_agsize) >= fs.s_agsize / INOPB ? 1 : 0))
256 #endif /* AFS_AIX41_ENV */
259 * check for "FORCESALVAGE" equivalent:
260 * LAST_RSVD_I is a vice inode, with dead beef, and
261 * di_nlink == 2 to indicate the FORCE.
263 opr_Verify(p = ginode(LAST_RSVD_I));
265 if (p->di_vicemagic == VICEMAGIC && p->di_vicep1 == 0xdeadbeef
266 && p->di_nlink == 2) {
268 idec(root_inode.st_dev, LAST_RSVD_I, 0xdeadbeef);
271 for (inum = LAST_RSVD_I + 1; inum <= imax; ++inum) {
272 if ((p = ginode(inum)) == NULL || p->di_vicemagic != VICEMAGIC
273 || (p->di_mode & IFMT) != IFREG)
276 info.inodeNumber = inum;
277 info.byteCount = p->di_size;
278 info.linkCount = p->di_nlink;
279 info.u.param[0] = p->di_vicep1;
280 info.u.param[1] = p->di_vicep2;
281 info.u.param[2] = p->di_vicep3;
282 info.u.param[3] = p->di_vicep4;
284 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
287 if (inodeFile != INVALID_FD) {
288 if (OS_WRITE(inodeFile, &info, sizeof(info)) != sizeof(info)) {
289 Log("Error writing inode file for partition %s\n", partition);
296 if (inodeFile != INVALID_FD) {
297 if (OS_SYNC(inodeFile) == -1) {
298 Log("Unable to successfully fsync inode file for %s\n", partition);
304 * Paranoia: check that the file is really the right size
306 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
307 Log("Wrong size (%d instead of %d) in inode file for %s\n",
308 status.st_size, ninodes * sizeof(struct ViceInodeInfo),
320 if (pfd != INVALID_FD)
326 /* Read in the superblock for devName */
328 ReadSuper(struct superblock *fs, char *devName)
332 pfd = OS_OPEN(devName, O_RDONLY, 0666);
333 if (pfd == INVALID_FD) {
334 Log("Unable to open inode on %s for reading superblock.\n", devName);
338 if (bread(pfd, fs, SUPER_B, sizeof(struct superblock)) < 0) {
339 Log("Unable to read superblock on %s.\n", devName);
347 /* IsBigFilesFileSystem returns 1 if it's a big files filesystem, 0 otherwise. */
349 IsBigFilesFileSystem(struct superblock *sb)
351 if ((strncmp(sb->s_magic, fsv3pmagic, 4) == 0)
352 && (sb->s_version == fsbigfile)
366 static char buf[FSBSIZE];
367 static daddr_t last_blk = -1;
373 0) ? INODES_B + inum / INOPB : ag * agblocks + (inum -
376 #else /* AFS_AIX41_ENV */
377 ag = inum / fs.s_agsize;
380 0) ? INODES_B + inum / INOPB : ag * fs.s_agsize + (inum -
384 #endif /* AFS_AIX41_ENV */
386 if (last_blk != pblk) {
387 if (bread(pfd, buf, pblk, sizeof(buf)) < 0) {
394 dp = (struct dinode *)buf;
399 #else /* !AFS_AIX31_ENV */
401 #if defined(AFS_SGI_ENV)
403 /* libefs.h includes <assert.h>, which we don't want */
406 #ifdef AFS_SGI_XFS_IOPS_ENV
407 #include <afs/xfsattrs.h>
408 /* xfs_ListViceInodes
410 * xfs_ListViceInodes verifies and correct the XFS namespace as it collects
411 * the inode information. The name is required for the idec operation to work.
412 * Steps 2 and 3 below are for the AFS_XFS_NAME_VERS == 1. If the name space
413 * changes, the algorithm will need to change.
414 * 1) If the parent inode number does not match the directory's inod number,
415 * change it in the attribute.
416 * 2) If the unqifier in the attribute does not match the name, rename the
417 * file. This is done by doing an exclusive open, incrementing the tag
418 * number until a file can be created. If the tag changes, then the
419 * attribute will need updating.
420 * 3) If the tag in the attribute does not match the name, change the
422 * 4) Verify uid = RW volume id and gid = XFS_VICEMAGIC.
428 * Does the verifications listed above.
429 * We can't change the names until the readdir is complete, so we set the
430 * rename flag if the file needs renaming.
433 xfs_VerifyInode(char *dir, uint64_t pino, char *name, i_list_inode_t * info,
440 int update_chown = 0;
447 (void)sprintf(path, "%s/%s", dir, name);
448 /* Verify uid and gid fields */
449 if (info->ili_magic != XFS_VICEMAGIC) {
450 Log("%s magic for %s/%s (inode %s) from %d to %d\n",
451 Testing ? "Would have changed" : "Changing", dir, name,
452 PrintInode(stmp, info->ili_info.inodeNumber), info->ili_magic,
458 vno = info->ili_info.param[0];
459 if (info->ili_vno != AFS_XFS_VNO_CLIP(vno)) {
460 Log("%s volume id for %s/%s (inode %s) from %d to %d\n",
461 Testing ? "Would have changed" : "Changing", dir, name,
462 PrintInode(stmp, info->ili_info.inodeNumber), info->ili_vno,
463 AFS_XFS_VNO_CLIP(vno));
469 if (chown(path, AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC) < 0) {
470 Log("Can't chown %s to uid=%d, gid=0x%x\n", path,
471 AFS_XFS_VNO_CLIP(vno), XFS_VICEMAGIC);
476 /* Check Parent inode number. */
477 if (info->ili_pino != pino) {
478 afs_ino_str_t sino, sipino, spino;
479 (void)PrintInode(sino, info->ili_info.inodeNumber);
480 (void)PrintInode(sipino, info->ili_pino);
481 (void)PrintInode(spino, pino);
482 Log("%s parent ino for %s (inode %s) from %s to %s.\n",
483 Testing ? "Would have changed" : "Changing", path, sino, sipino,
489 /* Verify the file name. */
490 (void)strcpy(tmpName, ".");
491 (void)strcat(tmpName, int_to_base64(stmp, info->ili_info.param[2]));
492 if (strncmp(name, tmpName, strlen(tmpName))) {
493 Log("%s name %s (inode %s) in directory %s, unique=%d, tag=%d\n",
494 Testing ? "Would have returned bad" : "Bad", name,
495 PrintInode(stmp, info->ili_info.inodeNumber), dir,
496 info->ili_info.param[2], info->ili_tag);
502 /* update the tag? */
503 (void)strcat(tmpName, ".");
504 (void)strcat(tmpName, int_to_base64(stmp, info->ili_tag));
505 if (strcmp(name, tmpName)) {
507 (void)strcpy(tmpName, name);
508 p = strchr(tmpName + 1, '.');
510 Log("No tag found on name %s (inode %s)in directory, %s.\n",
511 name, PrintInode(stmp, info->ili_info.inodeNumber), dir,
512 Testing ? "would have renamed" : "will rename");
516 tag = base64_to_int(p + 1);
517 Log("%s the tag for %s (inode %s) from %d to %d.\n",
518 Testing ? "Would have changed" : "Will change", path,
519 PrintInode(stmp, info->ili_info.inodeNumber), dir, tag,
527 if (update_pino || update_tag) {
528 afs_xfs_attr_t attrs;
531 length = SIZEOF_XFS_ATTR_T;
532 if (attr_get(path, AFS_XFS_ATTR, (char *)&attrs, &length, ATTR_ROOT) <
534 Log("Can't get AFS attribute for %s\n", path);
538 attrs.at_pino = pino;
542 (path, AFS_XFS_ATTR, (char *)&attrs, length,
543 ATTR_ROOT | ATTR_REPLACE) < 0) {
544 Log("Can't set AFS attribute into %s\n", path);
558 xfs_RenameFiles(char *dir, xfs_Rename_t * renames, int n_renames)
561 char opath[128], nbase[128], npath[128];
562 afs_xfs_attr_t attrs;
563 int length = SIZEOF_XFS_ATTR_T;
568 for (i = 0; i < n_renames; i++) {
569 (void)sprintf(opath, "%s/%s", dir, renames[i].name);
570 (void)sprintf(nbase, "%s/.%s", dir,
571 int_to_base64(stmp, renames[i].uniq));
572 for (tag = 2, j = 0; j < 64; tag++, j++) {
573 (void)sprintf(npath, "%s.%s", nbase, int_to_base64(stmp, tag));
574 fd = afs_open(npath, O_CREAT | O_EXCL | O_RDWR, 0);
581 Log("Can't find a new name for %s\n", opath);
584 if (rename(opath, npath) < 0) {
585 Log("Can't rename %s to %s\n", opath, npath);
588 Log("Renamed %s to %s\n", opath, npath);
595 xfs_ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
596 int (*judgeInode) (), VolumeId judgeParam, int *forcep,
597 int forceR, char *wpath, void *rock)
600 int info_size = sizeof(i_list_inode_t);
603 dirent64_t *top_direntp;
605 dirent64_t *vol_direntp;
606 struct stat64 sdirbuf;
607 struct stat64 sfilebuf;
608 afs_xfs_attr_t attrs;
609 afs_xfs_dattr_t dattrs;
611 char vol_dirname[1024];
614 xfs_Rename_t *renames = (xfs_Rename_t *) 0;
616 #define N_RENAME_STEP 64
624 if (stat64(mountedOn, &sdirbuf) < 0) {
625 perror("xfs_ListViceInodes: stat64");
629 if ((top_dirp = opendir(mountedOn)) == NULL) {
630 Log("Can't open directory %s to read inodes.\n", mountedOn);
634 while (top_direntp = readdir64(top_dirp)) {
635 /* Only descend directories with the AFSDIR attribute set.
636 * Could also verify the contents of the atribute, but currently
638 * Performance could be improved for single volume salvages by
639 * only going through the directory containing the volume's inodes.
640 * But I'm being complete as a first pass.
642 (void)sprintf(vol_dirname, "%s/%s", mountedOn, top_direntp->d_name);
643 length = SIZEOF_XFS_DATTR_T;
645 (vol_dirname, AFS_XFS_DATTR, (char *)&dattrs, &length, ATTR_ROOT))
648 if ((vol_dirp = opendir(vol_dirname)) == NULL) {
649 if (errno == ENOTDIR)
651 Log("Can't open directory %s to read inodes.\n", vol_dirname);
655 pino = top_direntp->d_ino;
657 while (vol_direntp = readdir64(vol_dirp)) {
658 if (vol_direntp->d_name[1] == '\0'
659 || vol_direntp->d_name[1] == '.')
662 info.ili_version = AFS_XFS_ILI_VERSION;
663 info_size = sizeof(i_list_inode_t);
665 ilistinode64(sdirbuf.st_dev, vol_direntp->d_ino, &info,
668 /* Where possible, give more explicit messages. */
672 Log("%s (device id %d) is not on an XFS filesystem.\n",
673 vol_dirname, sdirbuf.st_dev);
678 if (info_size != sizeof(i_list_inode_t)
679 || info.ili_version != AFS_XFS_ILI_VERSION) {
680 Log("Version skew between kernel and salvager.\n");
685 /* Continue, so we collect all the errors in the first pass. */
686 Log("Error listing inode named %s/%s: %s\n", vol_dirname,
687 vol_direntp->d_name, strerror(errno));
692 if (info.ili_attr_version != AFS_XFS_ATTR_VERS) {
693 Log("Unrecognized XFS attribute version %d in %s/%s. Upgrade salvager\n", info.ili_attr_version, vol_dirname, vol_direntp->d_name);
697 if (judgeInode && (*judgeInode) (&info.ili_info, judgeParam, rock) == 0)
702 (vol_dirname, pino, vol_direntp->d_name, &info,
708 /* Add this name to the list of items to rename. */
709 if (n_renames >= n_avail) {
710 n_avail += N_RENAME_STEP;
711 if (n_avail == N_RENAME_STEP)
712 renames = malloc(n_avail * sizeof(xfs_Rename_t));
714 renames = realloc(renames,
715 n_avail * sizeof(xfs_Rename_t));
717 Log("Can't %salloc %lu bytes for rename list.\n",
718 (n_avail == N_RENAME_STEP) ? "m" : "re",
719 n_avail * sizeof(xfs_Rename_t));
723 (void)strcpy(renames[n_renames].name, vol_direntp->d_name);
724 renames[n_renames].uniq = info.ili_info.param[2];
728 if (inodeFile != INVALID_FD) {
730 (inodeFile, &info.ili_info, sizeof(vice_inode_info_t))
731 != sizeof(vice_inode_info_t)) {
732 Log("Error writing inode file for partition %s\n", mountedOn);
738 } /* end while vol_direntp */
741 vol_dirp = (DIR *) 0;
743 Log("Renaming files.\n");
744 if (xfs_RenameFiles(vol_dirname, renames, n_renames) < 0) {
753 if (inodeFile != INVALID_FD) {
754 if (OS_SYNC(inodeFile) == -1) {
755 Log("Unable to successfully fsync inode file for %s\n", mountedOn);
756 return errors ? -1 : -2;
759 * Paranoia: check that the file is really the right size
761 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
762 Log("Wrong size (%d instead of %d) in inode file for %s\n",
763 OS_SIZE(inodeFile), ninodes * sizeof(struct ViceInodeInfo),
765 return errors ? -1 : -2;
770 Log("Errors encontered listing inodes, not salvaging partition.\n");
789 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
790 int (*judgeInode) (), VolumeId judgeParam, int *forcep, int forceR,
791 char *wpath, void *rock)
793 char dev[50], rdev[51];
795 struct efs_dinode *p;
796 struct ViceInodeInfo info;
797 struct stat root_inode;
798 int ninodes = 0, err = 0;
799 struct efs_dinode *dinodeBuf = NULL;
801 ino_t imax, inum; /* total number of I-nodes in file system */
805 sleep(1); /* simulate operator */
811 if (stat(mountedOn, &root_inode) < 0) {
812 Log("cannot stat: %s\n", mountedOn);
815 #ifdef AFS_SGI_XFS_IOPS_ENV
816 if (!strcmp("xfs", root_inode.st_fstype)) {
817 return xfs_ListViceInodes(devname, mountedOn, inodeFile, judgeInode,
818 judgeParam, forcep, forceR, wpath, rock);
822 Log("%s is not root of a filesystem\n", mountedOn);
827 #else /* AFS_SGI_ENV */
830 #define SPERB (MAXBSIZE / sizeof(short))
831 #define MAXNINDIR (MAXBSIZE / sizeof(daddr_t))
834 struct bufarea *b_next; /* must be first */
838 char b_buf[MAXBSIZE]; /* buffer space */
839 short b_lnks[SPERB]; /* link counts */
840 daddr_t b_indir[MAXNINDIR]; /* indirect block */
841 struct fs b_fs; /* super block */
842 struct cg b_cg; /* cylinder group */
846 typedef struct bufarea BUFAREA;
849 #define sblock sblk.b_un.b_fs
850 #endif /* AFS_HPUX_ENV */
852 extern char *afs_rawname();
854 ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
855 int (*judgeInode) (), VolumeId judgeParam, int *forcep, int forceR,
856 char *wpath, void *rock)
862 #else /* !AFS_AIX_ENV */
867 int i, c, e, bufsize, code, err = 0;
868 char dev[50], rdev[100], err1[512], *ptr1;
869 struct dinode *inodes = NULL, *einodes, *dptr;
872 struct ViceInodeInfo info;
875 partition = mountedOn;
876 sprintf(rdev, "%s/%s", wpath, devname);
877 ptr1 = afs_rawname(rdev);
881 /* Bletch: this is terrible; is there a better way to do this? Does this work? vfsck doesn't even sleep!! */
883 sleep(5); /* Trying a smaller one for aix */
888 pfd = OS_OPEN(rdev, O_RDONLY, 0666);
889 if (pfd == INVALID_FD) {
890 sprintf(err1, "Could not open device %s to get inode list\n", rdev);
895 if (bread(pfd, (char *)&super.fs, SUPERB, sizeof super.fs) == -1) {
898 if (bread(pfd, (char *)&sblock, SBLOCK, SBSIZE) == -1) {
900 if (bread(pfd, super.block, SBLOCK, SBSIZE) == -1) {
901 #endif /* AFS_HPUX_ENV */
903 Log("Unable to read superblock, partition %s\n", partition);
909 * char *FSlabel(), *fslabel=0;
910 * fslabel = FSlabel(&super.fs);
912 if (super.fs.s_bsize == 0)
913 super.fs.s_bsize = 512;
914 if (super.fs.s_bsize != BSIZE) {
915 Log("SuperBlk: Cluster size not %d; run vfsck\n", BSIZE);
918 fmax = super.fs.s_fsize; /* first invalid blk num */
919 imax = ((ino_t) super.fs.s_isize - (SUPERB + 1)) * INOPB;
921 Log("Size check: imax==0!\n");
924 if (GetAuxInodeFile(partition, &status) == 0) {
925 Log("Can't access Aux inode file for partition %s, aborting\n",
929 for (inum = 1; inum <= imax; inum++) {
930 struct dauxinode *auxp;
931 if ((auxp = IsAfsInode(inum)) == NULL) {
932 /* Not an afs inode, keep going */
935 if ((p = ginode(inum)) == NULL)
937 /* deleted/non-existent inode when di_mode == 0 */
940 info.inodeNumber = (int)inum;
941 info.byteCount = p->di_size;
942 info.linkCount = p->di_nlink;
943 info.u.param[0] = auxp->aux_param1;
944 info.u.param[1] = auxp->aux_param2;
945 info.u.param[2] = auxp->aux_param3;
946 info.u.param[3] = auxp->aux_param4;
947 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
949 if (inodeFile != INVALID_FD) {
950 if (OS_WRITE(inodeFile, &info, sizeof(info)) != sizeof(info)) {
951 Log("Error writing inode file for partition %s\n", partition);
959 * run a few consistency checks of the superblock
960 * (Cribbed from vfsck)
963 #if defined(FD_FSMAGIC)
964 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)
965 && (sblock.fs_magic != FD_FSMAGIC)
966 #if defined(AFS_HPUX101_ENV)
967 && (sblock.fs_magic != FD_FSMAGIC_2)
971 if ((sblock.fs_magic != FS_MAGIC) && (sblock.fs_magic != FS_MAGIC_LFN)) {
973 Log("There's something wrong with the superblock for partition %s; bad magic (%d) run vfsck\n", partition, sblock.fs_magic);
976 if (sblock.fs_ncg < 1) {
977 Log("There's something wrong with the superblock for partition %s; NCG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_ncg);
980 if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG) {
981 Log("There's something wrong with the superblock for partition %s; CPG OUT OF RANGE (%d) run vfsck\n", partition, sblock.fs_cpg);
984 if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl
985 || (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl) {
986 Log("There's something wrong with the superblock for partition %s; NCYL LESS THAN NCG*CPG run vfsck\n", partition);
989 if (sblock.fs_sbsize > SBSIZE) {
990 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);
994 if ((super.fs.fs_magic != FS_MAGIC)
995 || (super.fs.fs_ncg < 1)
996 #if defined(AFS_SUN_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
997 || (super.fs.fs_cpg < 1)
999 || (super.fs.fs_cpg < 1 || super.fs.fs_cpg > MAXCPG)
1001 || (super.fs.fs_ncg * super.fs.fs_cpg < super.fs.fs_ncyl
1002 || (super.fs.fs_ncg - 1) * super.fs.fs_cpg >= super.fs.fs_ncyl)
1003 || (super.fs.fs_sbsize > SBSIZE)) {
1004 Log("There's something wrong with the superblock for partition %s; run vfsck\n", partition);
1007 #endif /* AFS_HPUX_ENV */
1010 bufsize = sblock.fs_ipg * sizeof(struct dinode);
1012 bufsize = super.fs.fs_ipg * sizeof(struct dinode);
1013 #endif /* AFS_HPUX_ENV */
1014 inodes = malloc(bufsize);
1015 einodes = (struct dinode *)(((char *)inodes) + bufsize);
1016 if (inodes == NULL) {
1017 Log("Unable to allocate enough memory to scan inodes; help!\n");
1020 Log("Scanning inodes on device %s...\n", rdev);
1022 for (c = 0; c < sblock.fs_ncg; c++) {
1023 i = c * sblock.fs_ipg;
1024 e = i + sblock.fs_ipg;
1025 #if defined(AFS_HPUX102_ENV)
1026 if (OS_SEEK(pfd, dbtoo(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1029 if (OS_SEEK(pfd, dbtob(fsbtodb(&sblock, itod(&sblock, i))), L_SET) ==
1033 for (c = 0; c < super.fs.fs_ncg; c++) {
1035 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1037 #if defined(AFS_DARWIN_ENV)
1038 #define offset_t off_t
1039 #define llseek lseek
1042 #endif /* AFS_SUN5_ENV */
1043 i = c * super.fs.fs_ipg;
1044 e = i + super.fs.fs_ipg;
1045 #if defined(AFS_SUN5_ENV) || defined(AFS_DARWIN_ENV)
1046 f1 = fsbtodb(&super.fs, itod(&super.fs, i));
1047 off = (offset_t) f1 << DEV_BSHIFT;
1048 if (OS_SEEK(pfd, off, L_SET) == -1) {
1050 if (OS_SEEK(pfd, dbtob(fsbtodb(&super.fs, itod(&super.fs, i))), L_SET)
1052 #endif /* AFS_SUN5_ENV */
1053 #endif /* AFS_HPUX_ENV */
1054 Log("Error reading inodes for partition %s; run vfsck\n",
1060 if (OS_READ(pfd, inodes, bufsize) != bufsize) {
1061 Log("Error reading inodes for partition %s; run vfsck\n",
1068 for (bj = bk = 0; bj < bufsize; bj = bj + 512, bk++) {
1069 if ((code = OS_READ(pfd, dptr, 512)) != 512) {
1070 Log("Error reading inode %d? for partition %s (errno = %d); run vfsck\n", bk + i, partition, errno);
1071 if (OS_SEEK(pfd, 512, L_SET) == -1) {
1072 Log("OS_SEEK failed\n");
1087 for (p = inodes; p < einodes && i < e; i++, p++) {
1089 Log("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1090 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1091 p->di_size, p->di_nlink);
1093 ("Ino=%d, v1=%x, v2=%x, v3=%x, mode=%x size=%d, lcnt=%d\n",
1094 i, p->di_vicep1, p->di_vicep2, p->di_vicep3, p->di_mode,
1095 p->di_size, p->di_nlink);
1097 #if defined(AFS_SUN5_ENV)
1098 /* if this is a pre-sol2.6 unconverted inode, bail out */
1100 afs_uint32 p1, p2, p3, p4;
1104 q = (quad *) & (p->di_ic.ic_lsize);
1106 p2 = p->di_ic.ic_flags;
1108 p4 = p->di_ic.ic_uid;
1109 p5 = p->di_ic.ic_gid;
1111 if ((p2 || p3) && !p4 && (p5 == -2)) {
1112 Log("Found unconverted inode %d\n", i);
1113 Log("You should run the AFS file conversion utility\n");
1118 if (IS_DVICEMAGIC(p) && (p->di_mode & IFMT) == IFREG) {
1119 afs_uint32 p2 = p->di_vicep2, p3 = DI_VICEP3(p);
1121 info.u.param[0] = p->di_vicep1;
1122 #ifdef AFS_3DISPARES
1123 if (((p2 >> 3) == INODESPECIAL) && (p2 & 0x3)) {
1124 info.u.param[1] = INODESPECIAL;
1125 info.u.param[2] = p3;
1126 info.u.param[3] = p2 & 0x3;
1128 info.u.param[1] = ((p2 >> 27) << 16) + (p3 & 0xffff);
1129 info.u.param[2] = (p2 & 0x3fffff);
1131 (((p2 >> 22) & 0x1f) << 16) + (p3 >> 16);
1134 info.u.param[1] = p->di_vicep2;
1135 info.u.param[2] = DI_VICEP3(p);
1136 info.u.param[3] = p->di_vicep4;
1138 info.inodeNumber = i;
1139 info.byteCount = p->di_size;
1140 info.linkCount = p->di_nlink;
1141 if (judgeInode && (*judgeInode) (&info, judgeParam, rock) == 0)
1143 if (inodeFile != INVALID_FD) {
1144 if (OS_WRITE(inodeFile, &info, sizeof(info)) != sizeof(info)) {
1145 Log("Error writing inode file for partition %s\n",
1158 if (inodeFile != INVALID_FD) {
1159 if (OS_SYNC(inodeFile) == -1) {
1160 Log("Unable to successfully fsync inode file for %s\n", partition);
1166 * Paranoia: check that the file is really the right size
1168 if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
1169 Log("Wrong size (%d instead of %d) in inode file for %s\n",
1170 OS_SIZE(inodeFile), ninodes * sizeof(struct ViceInodeInfo),
1187 #endif /* !AFS_SGI_ENV */
1188 #endif /* !AFS_AIX31_ENV */
1190 #ifdef AFS_DARWIN_ENV
1192 #define dbtob(db) ((unsigned)(db) << DEV_BSHIFT)
1196 bread(FD_t fd, char *buf, daddr_t blk, afs_int32 size)
1199 #ifdef AFS_AIX41_ENV
1200 offset_t off = (offset_t) blk << FSBSHIFT;
1201 if (OS_SEEK(fd, off, 0) < 0) {
1202 Log("Unable to seek to offset %llu for block %u\n", off, blk);
1205 #else /* AFS_AIX41_ENV */
1206 if (OS_SEEK(fd, blk * Bsize, 0) < 0) {
1207 Log("Unable to seek to offset %u for block %u\n", blk * Bsize, blk);
1209 #endif /* AFS_AIX41_ENV */
1211 if (OS_SEEK(fd, (off_t) dbtob(blk), L_SET) < 0) {
1212 Log("Unable to seek to offset %u for block %u\n", dbtob(blk), blk);
1215 if (OS_READ(fd, buf, size) != size) {
1216 Log("Unable to read block %d, partition %s\n", blk, partition);
1222 #endif /* AFS_LINUX20_ENV */
1224 convertVolumeInfo(FdHandle_t *fdhr, FdHandle_t *fdhw, VolumeId vid)
1226 struct VolumeDiskData vd;
1229 if (FDH_PREAD(fdhr, &vd, sizeof(struct VolumeDiskData), 0) !=
1230 sizeof(struct VolumeDiskData)) {
1231 Log("1 convertiVolumeInfo: read failed for %lu with code %d\n", vid,
1235 vd.restoredFromId = vd.id; /* remember the RO volume here */
1237 vd.id = vd.parentId;
1241 vd.uniquifier += 5000; /* just in case there are still file copies
1242 from the old RW volume around */
1244 /* For ROs, the copyDate contains the time that the RO volume was actually
1245 * created, and the creationDate just contains the last time the RO was
1246 * copied from the RW data. So, make the new RW creationDate more accurate
1247 * by setting it to copyDate, if copyDate is older. Since, we know the
1248 * volume is at least as old as copyDate. */
1249 if (vd.copyDate < vd.creationDate) {
1250 vd.creationDate = vd.copyDate;
1252 /* If copyDate is newer, just make copyDate and creationDate the same,
1253 * for consistency with other RWs */
1254 vd.copyDate = vd.creationDate;
1257 p = strrchr(vd.name, '.');
1258 if (p && !strcmp(p, ".readonly")) {
1262 if (FDH_PWRITE(fdhw, &vd, sizeof(struct VolumeDiskData), 0) !=
1263 sizeof(struct VolumeDiskData)) {
1264 Log("1 convertiVolumeInfo: write failed for %lu with code %d\n", vid,
1272 afs_int32 inodeType;
1279 UpdateThisVolume(struct ViceInodeInfo *inodeinfo, VolumeId singleVolumeNumber,
1280 struct specino *specinos)
1283 if ((inodeinfo->u.vnode.vnodeNumber == INODESPECIAL) &&
1284 (inodeinfo->u.vnode.volumeId == singleVolumeNumber)) {
1285 specinos[inodeinfo->u.special.type].inodeNumber =
1286 inodeinfo->inodeNumber;
1288 return 0; /* We aren't using a result file, we're caching */
1292 getDevName(char *pbuffer, char *wpath)
1294 char pbuf[128], *ptr;
1295 strcpy(pbuf, pbuffer);
1296 ptr = (char *)strrchr(pbuf, OS_DIRSEPC);
1299 strcpy(wpath, pbuf);
1302 ptr = (char *)strrchr(pbuffer, OS_DIRSEPC);
1304 strcpy(pbuffer, ptr + 1);
1310 #ifdef FSSYNC_BUILD_CLIENT
1312 inode_ConvertROtoRWvolume(char *pname, VolumeId volumeId)
1314 char dir_name[512], oldpath[512], newpath[512];
1316 char headername[16];
1318 int fd, err, forcep, j;
1319 ssize_t len, nBytes;
1321 struct DiskPartition64 *partP;
1322 struct ViceInodeInfo info;
1323 struct VolumeDiskHeader h;
1324 IHandle_t *ih, *ih2;
1325 FdHandle_t *fdP, *fdP2;
1328 char tmpDevName[100];
1330 struct specino specinos[VI_LINKTABLE+1];
1331 Inode nearInode = 0;
1332 # ifdef AFS_DEMAND_ATTACH_FS
1334 # endif /* AFS_DEMAND_ATTACH_FS */
1337 memset(&specinos, 0, sizeof(specinos));
1339 /* now do the work */
1341 for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
1342 partP = partP->next);
1344 Log("1 inode_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
1349 #ifdef AFS_DEMAND_ATTACH_FS
1350 locktype = VVolLockType(V_VOLUPD, 1);
1351 code = VLockVolumeByIdNB(volumeId, partP, locktype);
1359 if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
1360 Log("1 inode_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
1361 afs_printable_uint32_lu(volumeId));
1365 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
1367 strcpy(tmpDevName, partP->devName);
1368 name = getDevName(tmpDevName, wpath);
1370 if ((err = ListViceInodes(name, VPartitionPath(partP),
1371 NULL, UpdateThisVolume, volumeId,
1372 &forcep, 0, wpath, &specinos)) < 0)
1374 Log("1 inode_ConvertROtoRWvolume: Couldn't get special inodes\n");
1379 #if defined(NEARINODE_HINT)
1380 nearInodeHash(volumeId, nearInode);
1381 nearInode %= partP->f_files;
1384 for (j = VI_VOLINFO; j < VI_LINKTABLE+1; j++) {
1385 if (specinos[j].inodeNumber > 0) {
1386 specinos[j].ninodeNumber =
1387 IH_CREATE(NULL, partP->device, VPartitionPath(partP),
1388 nearInode, h.parent, INODESPECIAL, j, h.parent);
1389 IH_INIT(ih, partP->device, volumeId,
1390 specinos[j].inodeNumber);
1393 Log("1 inode_ConvertROtoRWvolume: Couldn't find special inode %d for %d\n", j, volumeId);
1398 IH_INIT(ih2, partP->device, h.parent, specinos[j].ninodeNumber);
1399 fdP2 = IH_OPEN(ih2);
1401 Log("1 inode_ConvertROtoRWvolume: Couldn't find special inode %d for %d\n", j, h.parent);
1406 if (j == VI_VOLINFO)
1407 convertVolumeInfo(fdP, fdP2, ih2->ih_vid);
1411 len = FDH_PREAD(fdP, buffer, sizeof(buffer), offset);
1416 nBytes = FDH_PWRITE(fdP2, buffer, len, offset);
1417 if (nBytes != len) {
1428 /* Unlink the old special inode; otherwise we will get duplicate
1429 * special inodes if we recreate the RO again */
1430 if (IH_DEC(ih, specinos[j].inodeNumber, volumeId) == -1) {
1432 Log("IH_DEC failed: %x, %s, %u errno %d\n", ih,
1433 PrintInode(stmp, specinos[j].inodeNumber), volumeId, errno);
1442 #ifdef AFS_64BIT_IOPS_ENV
1443 h.volumeInfo_lo = (afs_int32)specinos[VI_VOLINFO].ninodeNumber & 0xffffffff;
1444 h.volumeInfo_hi = (afs_int32)(specinos[VI_VOLINFO].ninodeNumber >> 32) && 0xffffffff;
1445 h.smallVnodeIndex_lo = (afs_int32)specinos[VI_SMALLINDEX].ninodeNumber & 0xffffffff;
1446 h.smallVnodeIndex_hi = (afs_int32)(specinos[VI_SMALLINDEX].ninodeNumber >> 32) & 0xffffffff;
1447 h.largeVnodeIndex_lo = (afs_int32)specinos[VI_LARGEINDEX].ninodeNumber & 0xffffffff;
1448 h.largeVnodeIndex_hi = (afs_int32)(specinos[VI_LARGEINDEX].ninodeNumber >> 32) & 0xffffffff;
1449 if (specinos[VI_LINKTABLE].ninodeNumber) {
1450 h.linkTable_lo = (afs_int32)specinos[VI_LINKTABLE].ninodeNumber & 0xffffffff;
1451 h.linkTable_hi = (afs_int32)specinos[VI_LINKTABLE].ninodeNumber & 0xffffffff;
1454 h.volumeInfo_lo = specinos[VI_VOLINFO].ninodeNumber;
1455 h.smallVnodeIndex_lo = specinos[VI_SMALLINDEX].ninodeNumber;
1456 h.largeVnodeIndex_lo = specinos[VI_LARGEINDEX].ninodeNumber;
1457 if (specinos[VI_LINKTABLE].ninodeNumber) {
1458 h.linkTable_lo = specinos[VI_LINKTABLE].ninodeNumber;
1462 if (VCreateVolumeDiskHeader(&h, partP)) {
1463 Log("1 inode_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
1464 afs_printable_uint32_lu(h.id));
1469 if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
1470 Log("1 inode_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
1471 afs_printable_uint32_lu(volumeId));
1474 FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
1475 FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
1478 # ifdef AFS_DEMAND_ATTACH_FS
1480 VUnlockVolumeById(volumeId, partP);
1482 # endif /* AFS_DEMAND_ATTACH_FS */
1485 #endif /* FSSYNC_BUILD_CLIENT */
1486 #endif /* AFS_NAMEI_ENV */