116259Smckusick #ifndef lint 2*17936Smckusick static char version[] = "@(#)dir.c 3.3 (Berkeley) 02/08/85"; 316259Smckusick #endif 416259Smckusick 516259Smckusick #include <sys/param.h> 616259Smckusick #include <sys/inode.h> 716259Smckusick #include <sys/fs.h> 816259Smckusick #define KERNEL 916259Smckusick #include <sys/dir.h> 1016259Smckusick #undef KERNEL 1116259Smckusick #include "fsck.h" 1216259Smckusick 1316259Smckusick #define MINDIRSIZE (sizeof (struct dirtemplate)) 1416259Smckusick 1516259Smckusick char *endpathname = &pathname[BUFSIZ - 2]; 1616259Smckusick char *lfname = "lost+found"; 1716259Smckusick 1816259Smckusick DIRECT *fsck_readdir(); 1916259Smckusick 2016259Smckusick descend(parentino, inumber) 2116259Smckusick struct inodesc *parentino; 2216259Smckusick ino_t inumber; 2316259Smckusick { 2416259Smckusick register DINODE *dp; 2516259Smckusick struct inodesc curino; 2616259Smckusick 2716259Smckusick bzero((char *)&curino, sizeof(struct inodesc)); 28*17936Smckusick if (statemap[inumber] != DSTATE) 29*17936Smckusick errexit("BAD INODE %d TO DESCEND", statemap[inumber]); 30*17936Smckusick statemap[inumber] = DFOUND; 3116259Smckusick if ((dp = ginode(inumber)) == NULL) 3216259Smckusick return; 3316259Smckusick if (dp->di_size == 0) { 3416259Smckusick direrr(inumber, "ZERO LENGTH DIRECTORY"); 3516259Smckusick if (reply("REMOVE") == 1) 36*17936Smckusick statemap[inumber] = DCLEAR; 3716259Smckusick return; 3816259Smckusick } 3916259Smckusick if (dp->di_size < MINDIRSIZE) { 4016259Smckusick direrr(inumber, "DIRECTORY TOO SHORT"); 4116259Smckusick dp->di_size = MINDIRSIZE; 4216259Smckusick if (reply("FIX") == 1) 4316259Smckusick inodirty(); 4416259Smckusick } 4516259Smckusick curino.id_type = DATA; 4616259Smckusick curino.id_func = parentino->id_func; 4716259Smckusick curino.id_parent = parentino->id_number; 4816259Smckusick curino.id_number = inumber; 4916259Smckusick curino.id_filesize = dp->di_size; 5016259Smckusick (void)ckinode(dp, &curino); 5116259Smckusick } 5216259Smckusick 5316259Smckusick dirscan(idesc) 5416259Smckusick register struct inodesc *idesc; 5516259Smckusick { 5616259Smckusick register DIRECT *dp; 5716259Smckusick int dsize, n; 5816259Smckusick long blksiz; 5916259Smckusick char dbuf[DIRBLKSIZ]; 6016259Smckusick 6116259Smckusick if (idesc->id_type != DATA) 6216259Smckusick errexit("wrong type to dirscan %d\n", idesc->id_type); 6316259Smckusick blksiz = idesc->id_numfrags * sblock.fs_fsize; 6416259Smckusick if (outrange(idesc->id_blkno, idesc->id_numfrags)) { 6516259Smckusick idesc->id_filesize -= blksiz; 6616259Smckusick return (SKIP); 6716259Smckusick } 6816259Smckusick idesc->id_loc = 0; 6916259Smckusick for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) { 7016259Smckusick dsize = dp->d_reclen; 7116259Smckusick bcopy((char *)dp, dbuf, dsize); 7216259Smckusick idesc->id_dirp = (DIRECT *)dbuf; 7316259Smckusick if ((n = (*idesc->id_func)(idesc)) & ALTERED) { 7416259Smckusick if (getblk(&fileblk, idesc->id_blkno, blksiz) != NULL) { 7516259Smckusick bcopy(dbuf, (char *)dp, dsize); 7616259Smckusick dirty(&fileblk); 7716259Smckusick sbdirty(); 7816259Smckusick } else 7916259Smckusick n &= ~ALTERED; 8016259Smckusick } 8116259Smckusick if (n & STOP) 8216259Smckusick return (n); 8316259Smckusick } 8416259Smckusick return (idesc->id_filesize > 0 ? KEEPON : STOP); 8516259Smckusick } 8616259Smckusick 8716259Smckusick /* 8816259Smckusick * get next entry in a directory. 8916259Smckusick */ 9016259Smckusick DIRECT * 9116259Smckusick fsck_readdir(idesc) 9216259Smckusick register struct inodesc *idesc; 9316259Smckusick { 9416259Smckusick register DIRECT *dp, *ndp; 9516259Smckusick long size, blksiz; 9616259Smckusick 9716259Smckusick blksiz = idesc->id_numfrags * sblock.fs_fsize; 9816259Smckusick if (getblk(&fileblk, idesc->id_blkno, blksiz) == NULL) { 9916259Smckusick idesc->id_filesize -= blksiz - idesc->id_loc; 10016259Smckusick return NULL; 10116259Smckusick } 10216259Smckusick if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 && 10316259Smckusick idesc->id_loc < blksiz) { 10416259Smckusick dp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); 10516259Smckusick if (dircheck(idesc, dp)) 10616259Smckusick goto dpok; 10716259Smckusick idesc->id_loc += DIRBLKSIZ; 10816259Smckusick idesc->id_filesize -= DIRBLKSIZ; 10916259Smckusick dp->d_reclen = DIRBLKSIZ; 11016259Smckusick dp->d_ino = 0; 11116259Smckusick dp->d_namlen = 0; 11216259Smckusick dp->d_name[0] = '\0'; 11317930Smckusick if (dofix(idesc, "DIRECTORY CORRUPTED")) 11416259Smckusick dirty(&fileblk); 11516259Smckusick return (dp); 11616259Smckusick } 11716259Smckusick dpok: 11816259Smckusick if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) 11916259Smckusick return NULL; 12016259Smckusick dp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); 12116259Smckusick idesc->id_loc += dp->d_reclen; 12216259Smckusick idesc->id_filesize -= dp->d_reclen; 12316259Smckusick if ((idesc->id_loc % DIRBLKSIZ) == 0) 12416259Smckusick return (dp); 12516259Smckusick ndp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); 12616259Smckusick if (idesc->id_loc < blksiz && idesc->id_filesize > 0 && 12716259Smckusick dircheck(idesc, ndp) == 0) { 12816259Smckusick size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 12916259Smckusick dp->d_reclen += size; 13016259Smckusick idesc->id_loc += size; 13116259Smckusick idesc->id_filesize -= size; 13217930Smckusick if (dofix(idesc, "DIRECTORY CORRUPTED")) 13316259Smckusick dirty(&fileblk); 13416259Smckusick } 13516259Smckusick return (dp); 13616259Smckusick } 13716259Smckusick 13816259Smckusick /* 13916259Smckusick * Verify that a directory entry is valid. 14016259Smckusick * This is a superset of the checks made in the kernel. 14116259Smckusick */ 14216259Smckusick dircheck(idesc, dp) 14316259Smckusick struct inodesc *idesc; 14416259Smckusick register DIRECT *dp; 14516259Smckusick { 14616259Smckusick register int size; 14716259Smckusick register char *cp; 14816259Smckusick int spaceleft; 14916259Smckusick 15016259Smckusick size = DIRSIZ(dp); 15116259Smckusick spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 15216259Smckusick if (dp->d_ino < imax && 15316259Smckusick dp->d_reclen != 0 && 15416259Smckusick dp->d_reclen <= spaceleft && 15516259Smckusick (dp->d_reclen & 0x3) == 0 && 15616259Smckusick dp->d_reclen >= size && 15716259Smckusick idesc->id_filesize >= size && 15816259Smckusick dp->d_namlen <= MAXNAMLEN) { 15916259Smckusick if (dp->d_ino == 0) 16016259Smckusick return (1); 16116259Smckusick for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++) 16216259Smckusick if (*cp == 0 || (*cp++ & 0200)) 16316259Smckusick return (0); 16416259Smckusick if (*cp == 0) 16516259Smckusick return (1); 16616259Smckusick } 16716259Smckusick return (0); 16816259Smckusick } 16916259Smckusick 17016259Smckusick direrr(ino, s) 17116259Smckusick ino_t ino; 17216259Smckusick char *s; 17316259Smckusick { 17416259Smckusick register DINODE *dp; 17516259Smckusick 17616259Smckusick pwarn("%s ", s); 17716259Smckusick pinode(ino); 17816259Smckusick printf("\n"); 17916259Smckusick if ((dp = ginode(ino)) != NULL && ftypeok(dp)) 18017930Smckusick pfatal("%s=%s\n", DIRCT(dp) ? "DIR" : "FILE", pathname); 18116259Smckusick else 18216259Smckusick pfatal("NAME=%s\n", pathname); 18316259Smckusick } 18416259Smckusick 18516259Smckusick adjust(idesc, lcnt) 18616259Smckusick register struct inodesc *idesc; 18716259Smckusick short lcnt; 18816259Smckusick { 18916259Smckusick register DINODE *dp; 19016259Smckusick 19116259Smckusick if ((dp = ginode(idesc->id_number)) == NULL) 19216259Smckusick return; 19316259Smckusick if (dp->di_nlink == lcnt) { 19416259Smckusick if (linkup(idesc->id_number, (ino_t)0) == 0) 19516259Smckusick clri(idesc, "UNREF", 0); 196*17936Smckusick } else { 19717930Smckusick pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname : 19817930Smckusick (DIRCT(dp) ? "DIR" : "FILE")); 19916259Smckusick pinode(idesc->id_number); 20016259Smckusick printf(" COUNT %d SHOULD BE %d", 20116259Smckusick dp->di_nlink, dp->di_nlink-lcnt); 20216259Smckusick if (preen) { 20316259Smckusick if (lcnt < 0) { 20416259Smckusick printf("\n"); 20517930Smckusick pfatal("LINK COUNT INCREASING"); 20616259Smckusick } 20716259Smckusick printf(" (ADJUSTED)\n"); 20816259Smckusick } 20916259Smckusick if (preen || reply("ADJUST") == 1) { 21016259Smckusick dp->di_nlink -= lcnt; 21116259Smckusick inodirty(); 21216259Smckusick } 21316259Smckusick } 21416259Smckusick } 21516259Smckusick 21616259Smckusick mkentry(idesc) 21716259Smckusick struct inodesc *idesc; 21816259Smckusick { 21916259Smckusick register DIRECT *dirp = idesc->id_dirp; 22016259Smckusick DIRECT newent; 22116259Smckusick int newlen, oldlen; 22216259Smckusick 22316259Smckusick newent.d_namlen = 11; 22416259Smckusick newlen = DIRSIZ(&newent); 22516259Smckusick if (dirp->d_ino != 0) 22616259Smckusick oldlen = DIRSIZ(dirp); 22716259Smckusick else 22816259Smckusick oldlen = 0; 22916259Smckusick if (dirp->d_reclen - oldlen < newlen) 23016259Smckusick return (KEEPON); 23116259Smckusick newent.d_reclen = dirp->d_reclen - oldlen; 23216259Smckusick dirp->d_reclen = oldlen; 23316259Smckusick dirp = (struct direct *)(((char *)dirp) + oldlen); 23416259Smckusick dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */ 23516259Smckusick dirp->d_reclen = newent.d_reclen; 23616259Smckusick dirp->d_namlen = lftempname(dirp->d_name, idesc->id_parent); 23716259Smckusick return (ALTERED|STOP); 23816259Smckusick } 23916259Smckusick 24016259Smckusick chgdd(idesc) 24116259Smckusick struct inodesc *idesc; 24216259Smckusick { 24316259Smckusick register DIRECT *dirp = idesc->id_dirp; 24416259Smckusick 24516259Smckusick if (dirp->d_name[0] == '.' && dirp->d_name[1] == '.' && 24616259Smckusick dirp->d_name[2] == 0) { 24716259Smckusick dirp->d_ino = lfdir; 24816259Smckusick return (ALTERED|STOP); 24916259Smckusick } 25016259Smckusick return (KEEPON); 25116259Smckusick } 25216259Smckusick 25316259Smckusick linkup(orphan, pdir) 25416259Smckusick ino_t orphan; 25516259Smckusick ino_t pdir; 25616259Smckusick { 25716259Smckusick register DINODE *dp; 25816259Smckusick int lostdir, len; 25916259Smckusick struct inodesc idesc; 26016259Smckusick 26116259Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 26216259Smckusick if ((dp = ginode(orphan)) == NULL) 26316259Smckusick return (0); 26417930Smckusick lostdir = DIRCT(dp); 26516259Smckusick pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); 26616259Smckusick pinode(orphan); 26716259Smckusick if (preen && dp->di_size == 0) 26816259Smckusick return (0); 26916259Smckusick if (preen) 27016259Smckusick printf(" (RECONNECTED)\n"); 27116259Smckusick else 27216259Smckusick if (reply("RECONNECT") == 0) 27316259Smckusick return (0); 27416259Smckusick pathp = pathname; 27516259Smckusick *pathp++ = '/'; 27616259Smckusick *pathp = '\0'; 27716259Smckusick if (lfdir == 0) { 27816259Smckusick if ((dp = ginode(ROOTINO)) == NULL) 27916259Smckusick return (0); 28017930Smckusick idesc.id_name = lfname; 28116259Smckusick idesc.id_type = DATA; 28216259Smckusick idesc.id_func = findino; 28316259Smckusick idesc.id_number = ROOTINO; 28416259Smckusick idesc.id_filesize = dp->di_size; 28516259Smckusick (void)ckinode(dp, &idesc); 28616259Smckusick if ((lfdir = idesc.id_parent) == 0) { 28716259Smckusick pfatal("SORRY. NO lost+found DIRECTORY"); 28816259Smckusick printf("\n\n"); 28916259Smckusick return (0); 29016259Smckusick } 29116259Smckusick } 29216259Smckusick if ((dp = ginode(lfdir)) == NULL || 293*17936Smckusick !DIRCT(dp) || statemap[lfdir] != DFOUND) { 29416259Smckusick pfatal("SORRY. NO lost+found DIRECTORY"); 29516259Smckusick printf("\n\n"); 29616259Smckusick return (0); 29716259Smckusick } 29816259Smckusick if (fragoff(&sblock, dp->di_size)) { 29916259Smckusick dp->di_size = fragroundup(&sblock, dp->di_size); 30016259Smckusick inodirty(); 30116259Smckusick } 30216259Smckusick len = strlen(lfname); 30316259Smckusick bcopy(lfname, pathp, len + 1); 30416259Smckusick pathp += len; 30516259Smckusick idesc.id_type = DATA; 30616259Smckusick idesc.id_func = mkentry; 30716259Smckusick idesc.id_number = lfdir; 30816259Smckusick idesc.id_filesize = dp->di_size; 30916259Smckusick idesc.id_parent = orphan; /* this is the inode to enter */ 31016259Smckusick idesc.id_fix = DONTKNOW; 31116259Smckusick if ((ckinode(dp, &idesc) & ALTERED) == 0) { 31216259Smckusick pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); 31316259Smckusick printf("\n\n"); 31416259Smckusick return (0); 31516259Smckusick } 31616259Smckusick lncntp[orphan]--; 31716259Smckusick *pathp++ = '/'; 31816259Smckusick pathp += lftempname(pathp, orphan); 31916259Smckusick if (lostdir) { 32016259Smckusick dp = ginode(orphan); 32116259Smckusick idesc.id_type = DATA; 32216259Smckusick idesc.id_func = chgdd; 32316259Smckusick idesc.id_number = orphan; 32416259Smckusick idesc.id_filesize = dp->di_size; 32516259Smckusick idesc.id_fix = DONTKNOW; 32616259Smckusick (void)ckinode(dp, &idesc); 32716259Smckusick if ((dp = ginode(lfdir)) != NULL) { 32816259Smckusick dp->di_nlink++; 32916259Smckusick inodirty(); 33016259Smckusick lncntp[lfdir]++; 33116259Smckusick } 33216259Smckusick pwarn("DIR I=%u CONNECTED. ", orphan); 33316259Smckusick printf("PARENT WAS I=%u\n", pdir); 33416259Smckusick if (preen == 0) 33516259Smckusick printf("\n"); 33616259Smckusick } 33716259Smckusick return (1); 33816259Smckusick } 33916259Smckusick 34016259Smckusick /* 34116259Smckusick * generate a temporary name for the lost+found directory. 34216259Smckusick */ 34316259Smckusick lftempname(bufp, ino) 34416259Smckusick char *bufp; 34516259Smckusick ino_t ino; 34616259Smckusick { 34716259Smckusick register ino_t in; 34816259Smckusick register char *cp; 34916259Smckusick int namlen; 35016259Smckusick 35116259Smckusick cp = bufp + 2; 35216259Smckusick for (in = imax; in > 0; in /= 10) 35316259Smckusick cp++; 35416259Smckusick *--cp = 0; 35516259Smckusick namlen = cp - bufp; 35616259Smckusick in = ino; 35716259Smckusick while (cp > bufp) { 35816259Smckusick *--cp = (in % 10) + '0'; 35916259Smckusick in /= 10; 36016259Smckusick } 36116259Smckusick *cp = '#'; 36216259Smckusick return (namlen); 36316259Smckusick } 364