void pass1b(void) { int c, i; union dinode *dp; struct inodesc idesc; ino_t inumber; memset(&idesc, 0, sizeof(struct inodesc)); idesc.id_type = ADDR; idesc.id_func = pass1bcheck; duphead = duplist; inumber = 0; info_fn = pass1b_info; for (c = 0; c < sblock.fs_ncg; c++) { for (i = 0; i < sblock.fs_ipg; i++, inumber++) { info_inumber = inumber; if (inumber < ROOTINO) continue; dp = ginode(inumber); if (dp == NULL) continue; idesc.id_number = inumber; if (GET_ISTATE(inumber) != USTATE && (ckinode(dp, &idesc) & STOP)) return; } } info_fn = NULL; }
/* * Propagate connected state through the tree. */ void propagate(ino_t inumber) { struct inoinfo *inp; char state; inp = getinoinfo(inumber); state = GET_ISTATE(inp->i_number); for (;;) { SET_ISTATE(inp->i_number, state); if (inp->i_child && GET_ISTATE(inp->i_child->i_number) != state) inp = inp->i_child; else if (inp->i_number == inumber) break; else if (inp->i_sibling) inp = inp->i_sibling; else inp = inp->i_parentp; } }
void pass2(void) { union dinode *dp; struct inoinfo **inpp, *inp, *pinp; struct inoinfo **inpend; struct inodesc curino; union dinode dino; char pathbuf[MAXPATHLEN + 1]; int i; switch (GET_ISTATE(ROOTINO)) { case USTATE: pfatal("ROOT INODE UNALLOCATED"); if (reply("ALLOCATE") == 0) { ckfini(0); errexit("%s", ""); } if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO) errexit("CANNOT ALLOCATE ROOT INODE\n"); break; case DCLEAR: pfatal("DUPS/BAD IN ROOT INODE"); if (reply("REALLOCATE")) { freeino(ROOTINO); if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO) errexit("CANNOT ALLOCATE ROOT INODE\n"); break; } if (reply("CONTINUE") == 0) { ckfini(0); errexit("%s", ""); } break; case FSTATE: case FCLEAR: pfatal("ROOT INODE NOT DIRECTORY"); if (reply("REALLOCATE")) { freeino(ROOTINO); if (allocdir(ROOTINO, ROOTINO, 0755) != ROOTINO) errexit("CANNOT ALLOCATE ROOT INODE\n"); break; } if (reply("FIX") == 0) { ckfini(0); errexit("%s", ""); } dp = ginode(ROOTINO); DIP_SET(dp, di_mode, DIP(dp, di_mode) & ~IFMT); DIP_SET(dp, di_mode, DIP(dp, di_mode) | IFDIR); inodirty(); break; case DSTATE: break; default: errexit("BAD STATE %d FOR ROOT INODE\n", GET_ISTATE(ROOTINO)); } SET_ISTATE(ROOTINO, DFOUND); /* * Sort the directory list into disk block order. */ qsort(inpsort, (size_t)inplast, sizeof *inpsort, blksort); /* * Check the integrity of each directory. */ memset(&curino, 0, sizeof(struct inodesc)); curino.id_type = DATA; curino.id_func = pass2check; inpend = &inpsort[inplast]; info_pos = 0; info_max = inpend - inpsort; info_fn = pass2_info1; for (inpp = inpsort; inpp < inpend; inpp++) { inp = *inpp; info_pos ++; if (inp->i_isize == 0) continue; if (inp->i_isize < MINDIRSIZE) { direrror(inp->i_number, "DIRECTORY TOO SHORT"); inp->i_isize = roundup(MINDIRSIZE, DIRBLKSIZ); if (reply("FIX") == 1) { dp = ginode(inp->i_number); DIP_SET(dp, di_size, inp->i_isize); inodirty(); } } else if ((inp->i_isize & (DIRBLKSIZ - 1)) != 0) { getpathname(pathbuf, sizeof pathbuf, inp->i_number, inp->i_number); if (usedsoftdep) pfatal("%s %s: LENGTH %ld NOT MULTIPLE of %d", "DIRECTORY", pathbuf, (long)inp->i_isize, DIRBLKSIZ); else pwarn("%s %s: LENGTH %ld NOT MULTIPLE OF %d", "DIRECTORY", pathbuf, (long)inp->i_isize, DIRBLKSIZ); if (preen) printf(" (ADJUSTED)\n"); inp->i_isize = roundup(inp->i_isize, DIRBLKSIZ); if (preen || reply("ADJUST") == 1) { dp = ginode(inp->i_number); DIP_SET(dp, di_size, inp->i_isize); inodirty(); } } memset(&dino, 0, sizeof(union dinode)); dp = &dino; DIP_SET(dp, di_mode, IFDIR); DIP_SET(dp, di_size, inp->i_isize); for (i = 0; i < (inp->i_numblks<NDADDR ? inp->i_numblks : NDADDR); i++) DIP_SET(dp, di_db[i], inp->i_blks[i]); if (inp->i_numblks > NDADDR) for (i = 0; i < NIADDR; i++) DIP_SET(dp, di_ib[i], inp->i_blks[NDADDR + i]); curino.id_number = inp->i_number; curino.id_parent = inp->i_parent; (void)ckinode(dp, &curino); } /* * Now that the parents of all directories have been found, * make another pass to verify the value of `..' */ info_pos = 0; info_fn = pass2_info2; for (inpp = inpsort; inpp < inpend; inpp++) { inp = *inpp; info_pos++; if (inp->i_parent == 0 || inp->i_isize == 0) continue; if (inp->i_dotdot == inp->i_parent || inp->i_dotdot == (ino_t)-1) continue; if (inp->i_dotdot == 0) { inp->i_dotdot = inp->i_parent; fileerror(inp->i_parent, inp->i_number, "MISSING '..'"); if (reply("FIX") == 0) continue; (void)makeentry(inp->i_number, inp->i_parent, ".."); lncntp[inp->i_parent]--; continue; } fileerror(inp->i_parent, inp->i_number, "BAD INODE NUMBER FOR '..'"); if (reply("FIX") == 0) continue; lncntp[inp->i_dotdot]++; lncntp[inp->i_parent]--; inp->i_dotdot = inp->i_parent; (void)changeino(inp->i_number, "..", inp->i_parent); } info_fn = NULL; /* * Create a list of children for each directory. */ inpend = &inpsort[inplast]; for (inpp = inpsort; inpp < inpend; inpp++) { inp = *inpp; if (inp->i_parent == 0 || inp->i_number == ROOTINO) continue; pinp = getinoinfo(inp->i_parent); inp->i_parentp = pinp; inp->i_sibling = pinp->i_child; pinp->i_child = inp; } /* * Mark all the directories that can be found from the root. */ propagate(ROOTINO); }
static int pass2check(struct inodesc *idesc) { struct direct *dirp = idesc->id_dirp; struct inoinfo *inp; int n, entrysize, ret = 0; union dinode *dp; char *errmsg; struct direct proto; char namebuf[MAXPATHLEN + 1]; char pathbuf[MAXPATHLEN + 1]; /* * If converting, set directory entry type. */ if (doinglevel2 && dirp->d_ino > 0 && dirp->d_ino < maxino) { dirp->d_type = GET_ITYPE(dirp->d_ino); ret |= ALTERED; } /* * check for "." */ if (idesc->id_entryno != 0) goto chk1; if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") == 0) { if (dirp->d_ino != idesc->id_number) { direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'"); dirp->d_ino = idesc->id_number; if (reply("FIX") == 1) ret |= ALTERED; } if (newinofmt && dirp->d_type != DT_DIR) { direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'"); dirp->d_type = DT_DIR; if (reply("FIX") == 1) ret |= ALTERED; } goto chk1; } direrror(idesc->id_number, "MISSING '.'"); proto.d_ino = idesc->id_number; if (newinofmt) proto.d_type = DT_DIR; else proto.d_type = 0; proto.d_namlen = 1; (void)strlcpy(proto.d_name, ".", sizeof proto.d_name); # if BYTE_ORDER == LITTLE_ENDIAN if (!newinofmt) { u_char tmp; tmp = proto.d_type; proto.d_type = proto.d_namlen; proto.d_namlen = tmp; } # endif entrysize = DIRSIZ(0, &proto); if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") != 0) { pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n", dirp->d_name); } else if (dirp->d_reclen < entrysize) { pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n"); } else if (dirp->d_reclen < 2 * entrysize) { proto.d_reclen = dirp->d_reclen; memcpy(dirp, &proto, (size_t)entrysize); if (reply("FIX") == 1) ret |= ALTERED; } else { n = dirp->d_reclen - entrysize; proto.d_reclen = entrysize; memcpy(dirp, &proto, (size_t)entrysize); idesc->id_entryno++; lncntp[dirp->d_ino]--; dirp = (struct direct *)((char *)(dirp) + entrysize); memset(dirp, 0, (size_t)n); dirp->d_reclen = n; if (reply("FIX") == 1) ret |= ALTERED; } chk1: if (idesc->id_entryno > 1) goto chk2; inp = getinoinfo(idesc->id_number); proto.d_ino = inp->i_parent; if (newinofmt) proto.d_type = DT_DIR; else proto.d_type = 0; proto.d_namlen = 2; (void)strlcpy(proto.d_name, "..", sizeof proto.d_name); # if BYTE_ORDER == LITTLE_ENDIAN if (!newinofmt) { u_char tmp; tmp = proto.d_type; proto.d_type = proto.d_namlen; proto.d_namlen = tmp; } # endif entrysize = DIRSIZ(0, &proto); if (idesc->id_entryno == 0) { n = DIRSIZ(0, dirp); if (dirp->d_reclen < n + entrysize) goto chk2; proto.d_reclen = dirp->d_reclen - n; dirp->d_reclen = n; idesc->id_entryno++; lncntp[dirp->d_ino]--; dirp = (struct direct *)((char *)(dirp) + n); memset(dirp, 0, (size_t)proto.d_reclen); dirp->d_reclen = proto.d_reclen; } if (dirp->d_ino != 0 && strcmp(dirp->d_name, "..") == 0) { inp->i_dotdot = dirp->d_ino; if (newinofmt && dirp->d_type != DT_DIR) { direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'"); dirp->d_type = DT_DIR; if (reply("FIX") == 1) ret |= ALTERED; } goto chk2; } if (dirp->d_ino != 0 && strcmp(dirp->d_name, ".") != 0) { fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n", dirp->d_name); inp->i_dotdot = -1; } else if (dirp->d_reclen < entrysize) { fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n"); inp->i_dotdot = -1; } else if (inp->i_parent != 0) { /* * We know the parent, so fix now. */ inp->i_dotdot = inp->i_parent; fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); proto.d_reclen = dirp->d_reclen; memcpy(dirp, &proto, (size_t)entrysize); if (reply("FIX") == 1) ret |= ALTERED; } idesc->id_entryno++; if (dirp->d_ino != 0) lncntp[dirp->d_ino]--; return (ret|KEEPON); chk2: if (dirp->d_ino == 0) return (ret|KEEPON); if (dirp->d_namlen <= 2 && dirp->d_name[0] == '.' && idesc->id_entryno >= 2) { if (dirp->d_namlen == 1) { direrror(idesc->id_number, "EXTRA '.' ENTRY"); dirp->d_ino = 0; if (reply("FIX") == 1) ret |= ALTERED; return (KEEPON | ret); } if (dirp->d_name[1] == '.') { direrror(idesc->id_number, "EXTRA '..' ENTRY"); dirp->d_ino = 0; if (reply("FIX") == 1) ret |= ALTERED; return (KEEPON | ret); } } idesc->id_entryno++; n = 0; if (dirp->d_ino > maxino) { fileerror(idesc->id_number, dirp->d_ino, "I OUT OF RANGE"); n = reply("REMOVE"); } else { again: switch (GET_ISTATE(dirp->d_ino)) { case USTATE: if (idesc->id_entryno <= 2) break; fileerror(idesc->id_number, dirp->d_ino, "UNALLOCATED"); n = reply("REMOVE"); break; case DCLEAR: case FCLEAR: if (idesc->id_entryno <= 2) break; if (GET_ISTATE(dirp->d_ino) == FCLEAR) errmsg = "DUP/BAD"; else if (!preen && !usedsoftdep) errmsg = "ZERO LENGTH DIRECTORY"; else { n = 1; break; } fileerror(idesc->id_number, dirp->d_ino, errmsg); if ((n = reply("REMOVE")) == 1) break; dp = ginode(dirp->d_ino); SET_ISTATE(dirp->d_ino, (DIP(dp, di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE); lncntp[dirp->d_ino] = DIP(dp, di_nlink); goto again; case DSTATE: case DFOUND: inp = getinoinfo(dirp->d_ino); if (inp->i_parent != 0 && idesc->id_entryno > 2) { getpathname(pathbuf, sizeof pathbuf, idesc->id_number, idesc->id_number); getpathname(namebuf, sizeof namebuf, dirp->d_ino, dirp->d_ino); pwarn("%s %s %s\n", pathbuf, "IS AN EXTRANEOUS HARD LINK TO DIRECTORY", namebuf); if (preen) { printf (" (REMOVED)\n"); n = 1; break; } if ((n = reply("REMOVE")) == 1) break; } if (idesc->id_entryno > 2) inp->i_parent = idesc->id_number; /* FALLTHROUGH */ case FSTATE: if (newinofmt && dirp->d_type != GET_ITYPE(dirp->d_ino)) { fileerror(idesc->id_number, dirp->d_ino, "BAD TYPE VALUE"); dirp->d_type = GET_ITYPE(dirp->d_ino); if (reply("FIX") == 1) ret |= ALTERED; } lncntp[dirp->d_ino]--; break; default: errexit("BAD STATE %d FOR INODE I=%d\n", GET_ISTATE(dirp->d_ino), dirp->d_ino); } } if (n == 0) return (ret|KEEPON); dirp->d_ino = 0; return (ret|KEEPON|ALTERED); }
int linkup(ino_t orphan, ino_t parentdir) { union dinode *dp; int lostdir; ino_t oldlfdir; struct inodesc idesc; char tempname[BUFSIZ]; memset(&idesc, 0, sizeof(struct inodesc)); dp = ginode(orphan); lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR; pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); pinode(orphan); if ((preen || usedsoftdep) && DIP(dp, di_size) == 0) return (0); if (preen) printf(" (RECONNECTED)\n"); else if (reply("RECONNECT") == 0) return (0); if (lfdir == 0) { dp = ginode(ROOTINO); idesc.id_name = lfname; idesc.id_type = DATA; idesc.id_func = findino; idesc.id_number = ROOTINO; if ((ckinode(dp, &idesc) & FOUND) != 0) { lfdir = idesc.id_parent; } else { pwarn("NO lost+found DIRECTORY"); if (preen || reply("CREATE")) { lfdir = allocdir(ROOTINO, 0, lfmode); if (lfdir != 0) { if (makeentry(ROOTINO, lfdir, lfname) != 0) { if (preen) printf(" (CREATED)\n"); } else { freedir(lfdir, ROOTINO); lfdir = 0; if (preen) printf("\n"); } } } } if (lfdir == 0) { pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); printf("\n\n"); return (0); } } dp = ginode(lfdir); if ((DIP(dp, di_mode) & IFMT) != IFDIR) { pfatal("lost+found IS NOT A DIRECTORY"); if (reply("REALLOCATE") == 0) return (0); oldlfdir = lfdir; if ((lfdir = allocdir(ROOTINO, 0, lfmode)) == 0) { pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); return (0); } if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) { pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); return (0); } inodirty(); idesc.id_type = ADDR; idesc.id_func = pass4check; idesc.id_number = oldlfdir; adjust(&idesc, lncntp[oldlfdir] + 1); lncntp[oldlfdir] = 0; dp = ginode(lfdir); } if (GET_ISTATE(lfdir) != DFOUND) { pfatal("SORRY. NO lost+found DIRECTORY\n\n"); return (0); } (void)lftempname(tempname, orphan); if (makeentry(lfdir, orphan, tempname) == 0) { pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); printf("\n\n"); return (0); } lncntp[orphan]--; if (lostdir) { if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 && parentdir != (ino_t)-1) (void)makeentry(orphan, lfdir, ".."); dp = ginode(lfdir); DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1); inodirty(); lncntp[lfdir]++; pwarn("DIR I=%u CONNECTED. ", orphan); if (parentdir != (ino_t)-1) { printf("PARENT WAS I=%u\n", parentdir); /* * The parent directory, because of the ordering * guarantees, has had the link count incremented * for the child, but no entry was made. This * fixes the parent link count so that fsck does * not need to be rerun. */ lncntp[parentdir]++; } if (preen == 0) printf("\n"); } return (1); }
void pass4(void) { ino_t inumber; struct zlncnt *zlnp; union dinode *dp; struct inodesc idesc; int n, c, i; memset(&idesc, 0, sizeof(struct inodesc)); idesc.id_type = ADDR; idesc.id_func = pass4check; info_fn = pass4_info; for (c = 0; c < sblock.fs_ncg; c++) { inumber = c * sblock.fs_ipg; for (i = 0; i < inostathead[c].il_numalloced; i++, inumber++) { if (inumber < ROOTINO) continue; idesc.id_number = inumber; switch (GET_ISTATE(inumber)) { case FSTATE: case DFOUND: n = ILNCOUNT(inumber); if (n) { adjust(&idesc, (short)n); break; } for (zlnp = zlnhead; zlnp; zlnp = zlnp->next) if (zlnp->zlncnt == inumber) { zlnp->zlncnt = zlnhead->zlncnt; zlnp = zlnhead; zlnhead = zlnhead->next; free((char *)zlnp); clri(&idesc, "UNREF", 1); break; } break; case DSTATE: clri(&idesc, "UNREF", 1); break; case DCLEAR: dp = ginode(inumber); if (DIP(dp, di_size) == 0) { clri(&idesc, "ZERO LENGTH", 1); break; } /* FALLTHROUGH */ case FCLEAR: clri(&idesc, "BAD/DUP", 1); break; case USTATE: break; default: errexit("BAD STATE %d FOR INODE I=%llu\n", GET_ISTATE(inumber), (unsigned long long)inumber); } } } info_fn = NULL; }