116259Smckusick #ifndef lint 2*17992Smckusick static char version[] = "@(#)dir.c 3.9 (Berkeley) 02/15/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"; 1717944Smckusick struct dirtemplate emptydir = { 0, DIRBLKSIZ }; 1817954Smckusick struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." }; 1916259Smckusick 2016259Smckusick DIRECT *fsck_readdir(); 2116259Smckusick 2216259Smckusick descend(parentino, inumber) 2316259Smckusick struct inodesc *parentino; 2416259Smckusick ino_t inumber; 2516259Smckusick { 2616259Smckusick register DINODE *dp; 2716259Smckusick struct inodesc curino; 2816259Smckusick 2916259Smckusick bzero((char *)&curino, sizeof(struct inodesc)); 3017936Smckusick if (statemap[inumber] != DSTATE) 3117936Smckusick errexit("BAD INODE %d TO DESCEND", statemap[inumber]); 3217936Smckusick statemap[inumber] = DFOUND; 3317943Smckusick dp = ginode(inumber); 3416259Smckusick if (dp->di_size == 0) { 3516259Smckusick direrr(inumber, "ZERO LENGTH DIRECTORY"); 3616259Smckusick if (reply("REMOVE") == 1) 3717936Smckusick statemap[inumber] = DCLEAR; 3816259Smckusick return; 3916259Smckusick } 4016259Smckusick if (dp->di_size < MINDIRSIZE) { 4116259Smckusick direrr(inumber, "DIRECTORY TOO SHORT"); 4216259Smckusick dp->di_size = MINDIRSIZE; 4316259Smckusick if (reply("FIX") == 1) 4416259Smckusick inodirty(); 4516259Smckusick } 4616259Smckusick curino.id_type = DATA; 4716259Smckusick curino.id_func = parentino->id_func; 4816259Smckusick curino.id_parent = parentino->id_number; 4916259Smckusick curino.id_number = inumber; 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"); 17917943Smckusick if (ino < ROOTINO || ino > imax) { 18017943Smckusick pfatal("NAME=%s\n", pathname); 18117943Smckusick return; 18217943Smckusick } 18317943Smckusick dp = ginode(ino); 18417943Smckusick if (ftypeok(dp)) 18517930Smckusick pfatal("%s=%s\n", DIRCT(dp) ? "DIR" : "FILE", pathname); 18616259Smckusick else 18716259Smckusick pfatal("NAME=%s\n", pathname); 18816259Smckusick } 18916259Smckusick 19016259Smckusick adjust(idesc, lcnt) 19116259Smckusick register struct inodesc *idesc; 19216259Smckusick short lcnt; 19316259Smckusick { 19416259Smckusick register DINODE *dp; 19516259Smckusick 19617943Smckusick dp = ginode(idesc->id_number); 19716259Smckusick if (dp->di_nlink == lcnt) { 19816259Smckusick if (linkup(idesc->id_number, (ino_t)0) == 0) 19916259Smckusick clri(idesc, "UNREF", 0); 20017936Smckusick } else { 20117930Smckusick pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname : 20217930Smckusick (DIRCT(dp) ? "DIR" : "FILE")); 20316259Smckusick pinode(idesc->id_number); 20416259Smckusick printf(" COUNT %d SHOULD BE %d", 20516259Smckusick dp->di_nlink, dp->di_nlink-lcnt); 20616259Smckusick if (preen) { 20716259Smckusick if (lcnt < 0) { 20816259Smckusick printf("\n"); 20917930Smckusick pfatal("LINK COUNT INCREASING"); 21016259Smckusick } 21116259Smckusick printf(" (ADJUSTED)\n"); 21216259Smckusick } 21316259Smckusick if (preen || reply("ADJUST") == 1) { 21416259Smckusick dp->di_nlink -= lcnt; 21516259Smckusick inodirty(); 21616259Smckusick } 21716259Smckusick } 21816259Smckusick } 21916259Smckusick 22016259Smckusick mkentry(idesc) 22116259Smckusick struct inodesc *idesc; 22216259Smckusick { 22316259Smckusick register DIRECT *dirp = idesc->id_dirp; 22416259Smckusick DIRECT newent; 22516259Smckusick int newlen, oldlen; 22616259Smckusick 22716259Smckusick newent.d_namlen = 11; 22816259Smckusick newlen = DIRSIZ(&newent); 22916259Smckusick if (dirp->d_ino != 0) 23016259Smckusick oldlen = DIRSIZ(dirp); 23116259Smckusick else 23216259Smckusick oldlen = 0; 23316259Smckusick if (dirp->d_reclen - oldlen < newlen) 23416259Smckusick return (KEEPON); 23516259Smckusick newent.d_reclen = dirp->d_reclen - oldlen; 23616259Smckusick dirp->d_reclen = oldlen; 23716259Smckusick dirp = (struct direct *)(((char *)dirp) + oldlen); 23816259Smckusick dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */ 23916259Smckusick dirp->d_reclen = newent.d_reclen; 24017954Smckusick dirp->d_namlen = strlen(idesc->id_name); 24117954Smckusick bcopy(idesc->id_name, dirp->d_name, dirp->d_namlen + 1); 24216259Smckusick return (ALTERED|STOP); 24316259Smckusick } 24416259Smckusick 24517957Smckusick chgino(idesc) 24616259Smckusick struct inodesc *idesc; 24716259Smckusick { 24816259Smckusick register DIRECT *dirp = idesc->id_dirp; 24916259Smckusick 25017957Smckusick if (bcmp(dirp->d_name, idesc->id_name, dirp->d_namlen + 1)) 25117957Smckusick return (KEEPON); 25217957Smckusick dirp->d_ino = idesc->id_parent;; 25317957Smckusick return (ALTERED|STOP); 25416259Smckusick } 25516259Smckusick 25616259Smckusick linkup(orphan, pdir) 25716259Smckusick ino_t orphan; 25816259Smckusick ino_t pdir; 25916259Smckusick { 26016259Smckusick register DINODE *dp; 26116259Smckusick int lostdir, len; 26217954Smckusick ino_t oldlfdir; 26316259Smckusick struct inodesc idesc; 26417954Smckusick char tempname[BUFSIZ]; 26517957Smckusick extern int pass4check(); 26616259Smckusick 26716259Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 26817943Smckusick dp = ginode(orphan); 26917930Smckusick lostdir = DIRCT(dp); 27016259Smckusick pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); 27116259Smckusick pinode(orphan); 27216259Smckusick if (preen && dp->di_size == 0) 27316259Smckusick return (0); 27416259Smckusick if (preen) 27516259Smckusick printf(" (RECONNECTED)\n"); 27616259Smckusick else 27716259Smckusick if (reply("RECONNECT") == 0) 27816259Smckusick return (0); 27916259Smckusick pathp = pathname; 28016259Smckusick *pathp++ = '/'; 28116259Smckusick *pathp = '\0'; 28216259Smckusick if (lfdir == 0) { 28317943Smckusick dp = ginode(ROOTINO); 28417930Smckusick idesc.id_name = lfname; 28516259Smckusick idesc.id_type = DATA; 28616259Smckusick idesc.id_func = findino; 28716259Smckusick idesc.id_number = ROOTINO; 28816259Smckusick (void)ckinode(dp, &idesc); 28917954Smckusick if (idesc.id_parent >= ROOTINO && idesc.id_parent < imax) { 29017954Smckusick lfdir = idesc.id_parent; 29117954Smckusick } else { 29217954Smckusick pwarn("NO lost+found DIRECTORY"); 29317954Smckusick if (preen || reply("CREATE")) { 294*17992Smckusick lfdir = allocdir(ROOTINO, 0); 295*17992Smckusick if (lfdir != 0) { 296*17992Smckusick if (makeentry(ROOTINO, lfdir, lfname) != 0) { 29717954Smckusick if (preen) 29817954Smckusick printf(" (CREATED)\n"); 29917954Smckusick } else { 300*17992Smckusick freedir(lfdir, ROOTINO); 301*17992Smckusick lfdir = 0; 30217954Smckusick if (preen) 30317954Smckusick printf("\n"); 30417954Smckusick } 30517954Smckusick } 30617954Smckusick } 30717954Smckusick } 30817943Smckusick if (lfdir == 0) { 30917954Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); 31016259Smckusick printf("\n\n"); 31116259Smckusick return (0); 31216259Smckusick } 31316259Smckusick } 31417943Smckusick dp = ginode(lfdir); 31517957Smckusick if (!DIRCT(dp)) { 31617957Smckusick pfatal("lost+found IS NOT A DIRECTORY"); 31717957Smckusick if (reply("REALLOCATE") == 0) 31817957Smckusick return (0); 31917957Smckusick oldlfdir = lfdir; 32017957Smckusick if ((lfdir = allocdir(ROOTINO, 0)) == 0) { 32117957Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 32217957Smckusick return (0); 32317957Smckusick } 32417957Smckusick idesc.id_type = DATA; 32517957Smckusick idesc.id_func = chgino; 32617957Smckusick idesc.id_number = ROOTINO; 32717957Smckusick idesc.id_parent = lfdir; /* new inumber for lost+found */ 32817957Smckusick idesc.id_name = lfname; 32917957Smckusick if ((ckinode(ginode(ROOTINO), &idesc) & ALTERED) == 0) { 33017957Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 33117957Smckusick return (0); 33217957Smckusick } 33317957Smckusick inodirty(); 33417957Smckusick idesc.id_type = ADDR; 33517957Smckusick idesc.id_func = pass4check; 33617957Smckusick idesc.id_number = oldlfdir; 33717957Smckusick adjust(&idesc, lncntp[oldlfdir] + 1); 33817957Smckusick lncntp[oldlfdir] = 0; 33917957Smckusick dp = ginode(lfdir); 34017957Smckusick } 34117957Smckusick if (statemap[lfdir] != DFOUND) { 34217957Smckusick pfatal("SORRY. NO lost+found DIRECTORY\n\n"); 34316259Smckusick return (0); 34416259Smckusick } 34517944Smckusick if (dp->di_size % DIRBLKSIZ) { 34617944Smckusick dp->di_size = roundup(dp->di_size, DIRBLKSIZ); 34716259Smckusick inodirty(); 34816259Smckusick } 34916259Smckusick len = strlen(lfname); 35016259Smckusick bcopy(lfname, pathp, len + 1); 35116259Smckusick pathp += len; 352*17992Smckusick len = lftempname(tempname, orphan); 353*17992Smckusick if (makeentry(lfdir, orphan, tempname) == 0) { 35416259Smckusick pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); 35516259Smckusick printf("\n\n"); 35616259Smckusick return (0); 35716259Smckusick } 35816259Smckusick lncntp[orphan]--; 35916259Smckusick *pathp++ = '/'; 36017954Smckusick bcopy(idesc.id_name, pathp, len + 1); 36117954Smckusick pathp += len; 36216259Smckusick if (lostdir) { 36316259Smckusick dp = ginode(orphan); 36416259Smckusick idesc.id_type = DATA; 36517957Smckusick idesc.id_func = chgino; 36616259Smckusick idesc.id_number = orphan; 36716259Smckusick idesc.id_fix = DONTKNOW; 36817957Smckusick idesc.id_name = ".."; 36917957Smckusick idesc.id_parent = lfdir; /* new value for ".." */ 37016259Smckusick (void)ckinode(dp, &idesc); 37117943Smckusick dp = ginode(lfdir); 37217943Smckusick dp->di_nlink++; 37317943Smckusick inodirty(); 37417943Smckusick lncntp[lfdir]++; 37516259Smckusick pwarn("DIR I=%u CONNECTED. ", orphan); 37616259Smckusick printf("PARENT WAS I=%u\n", pdir); 37716259Smckusick if (preen == 0) 37816259Smckusick printf("\n"); 37916259Smckusick } 38016259Smckusick return (1); 38116259Smckusick } 38216259Smckusick 38316259Smckusick /* 38417944Smckusick * make an entry in a directory 38517944Smckusick */ 386*17992Smckusick makeentry(parent, ino, name) 387*17992Smckusick ino_t parent, ino; 388*17992Smckusick char *name; 389*17992Smckusick { 39017944Smckusick DINODE *dp; 391*17992Smckusick struct inodesc idesc; 39217944Smckusick 393*17992Smckusick if (parent < ROOTINO || parent >= imax || ino < ROOTINO || ino >= imax) 394*17992Smckusick return (0); 395*17992Smckusick bzero(&idesc, sizeof(struct inodesc)); 396*17992Smckusick idesc.id_type = DATA; 397*17992Smckusick idesc.id_func = mkentry; 398*17992Smckusick idesc.id_number = parent; 399*17992Smckusick idesc.id_parent = ino; /* this is the inode to enter */ 400*17992Smckusick idesc.id_fix = DONTKNOW; 401*17992Smckusick idesc.id_name = name; 402*17992Smckusick dp = ginode(parent); 403*17992Smckusick if ((ckinode(dp, &idesc) & ALTERED) != 0) 40417944Smckusick return (1); 40517944Smckusick if (expanddir(dp) == 0) 40617944Smckusick return (0); 407*17992Smckusick return (ckinode(dp, &idesc) & ALTERED); 40817944Smckusick } 40917944Smckusick 41017944Smckusick /* 41117944Smckusick * Attempt to expand the size of a directory 41217944Smckusick */ 41317944Smckusick expanddir(dp) 41417944Smckusick register DINODE *dp; 41517944Smckusick { 41617944Smckusick daddr_t lastbn, newblk; 41717944Smckusick char *cp, firstblk[DIRBLKSIZ]; 41817944Smckusick 41917944Smckusick lastbn = lblkno(&sblock, dp->di_size); 42017944Smckusick if (lastbn >= NDADDR - 1) 42117944Smckusick return (0); 42217944Smckusick if ((newblk = allocblk(sblock.fs_frag)) == 0) 42317944Smckusick return (0); 42417944Smckusick dp->di_db[lastbn + 1] = dp->di_db[lastbn]; 42517944Smckusick dp->di_db[lastbn] = newblk; 42617944Smckusick dp->di_size += sblock.fs_bsize; 42717944Smckusick dp->di_blocks += btodb(sblock.fs_bsize); 42817944Smckusick if (getblk(&fileblk, dp->di_db[lastbn + 1], 42917944Smckusick dblksize(&sblock, dp, lastbn + 1)) == NULL) 43017944Smckusick goto bad; 43117944Smckusick bcopy(dirblk.b_buf, firstblk, DIRBLKSIZ); 43217944Smckusick if (getblk(&fileblk, newblk, sblock.fs_bsize) == NULL) 43317944Smckusick goto bad; 43417944Smckusick bcopy(firstblk, dirblk.b_buf, DIRBLKSIZ); 43517944Smckusick for (cp = &dirblk.b_buf[DIRBLKSIZ]; 43617944Smckusick cp < &dirblk.b_buf[sblock.fs_bsize]; 43717944Smckusick cp += DIRBLKSIZ) 43817944Smckusick bcopy((char *)&emptydir, cp, sizeof emptydir); 43917944Smckusick dirty(&fileblk); 44017944Smckusick if (getblk(&fileblk, dp->di_db[lastbn + 1], 44117944Smckusick dblksize(&sblock, dp, lastbn + 1)) == NULL) 44217944Smckusick goto bad; 44317944Smckusick bcopy((char *)&emptydir, dirblk.b_buf, sizeof emptydir); 44417944Smckusick pwarn("NO SPACE LEFT IN %s", pathname); 44517944Smckusick if (preen) 44617944Smckusick printf(" (EXPANDED)\n"); 44717944Smckusick else if (reply("EXPAND") == 0) 44817944Smckusick goto bad; 44917944Smckusick dirty(&fileblk); 45017944Smckusick inodirty(); 45117944Smckusick return (1); 45217944Smckusick bad: 45317944Smckusick dp->di_db[lastbn] = dp->di_db[lastbn + 1]; 45417944Smckusick dp->di_db[lastbn + 1] = 0; 45517944Smckusick dp->di_size -= sblock.fs_bsize; 45617944Smckusick dp->di_blocks -= btodb(sblock.fs_bsize); 45717944Smckusick freeblk(newblk, sblock.fs_frag); 45817944Smckusick return (0); 45917944Smckusick } 46017944Smckusick 46117944Smckusick /* 46217954Smckusick * allocate a new directory 46317954Smckusick */ 46417954Smckusick allocdir(parent, request) 46517954Smckusick ino_t parent, request; 46617954Smckusick { 46717954Smckusick ino_t ino; 46817954Smckusick char *cp; 46917954Smckusick DINODE *dp; 47017954Smckusick 47117954Smckusick ino = allocino(request, IFDIR|0755); 47217954Smckusick dirhead.dot_ino = ino; 47317954Smckusick dirhead.dotdot_ino = parent; 47417954Smckusick dp = ginode(ino); 47517954Smckusick if (getblk(&fileblk, dp->di_db[0], sblock.fs_fsize) == NULL) { 47617954Smckusick freeino(ino); 47717954Smckusick return (0); 47817954Smckusick } 47917954Smckusick bcopy((char *)&dirhead, dirblk.b_buf, sizeof dirhead); 48017954Smckusick for (cp = &dirblk.b_buf[DIRBLKSIZ]; 48117954Smckusick cp < &dirblk.b_buf[sblock.fs_fsize]; 48217954Smckusick cp += DIRBLKSIZ) 48317954Smckusick bcopy((char *)&emptydir, cp, sizeof emptydir); 48417954Smckusick dirty(&fileblk); 48517954Smckusick dp->di_nlink = 2; 48617954Smckusick inodirty(); 48717954Smckusick if (ino == ROOTINO) { 48817954Smckusick lncntp[ino] = dp->di_nlink; 48917954Smckusick return(ino); 49017954Smckusick } 49117954Smckusick if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) { 49217954Smckusick freeino(ino); 49317954Smckusick return (0); 49417954Smckusick } 49517954Smckusick statemap[ino] = statemap[parent]; 49617954Smckusick if (statemap[ino] == DSTATE) { 49717954Smckusick lncntp[ino] = dp->di_nlink; 49817954Smckusick lncntp[parent]++; 49917954Smckusick } 50017954Smckusick dp = ginode(parent); 50117954Smckusick dp->di_nlink++; 50217954Smckusick inodirty(); 50317954Smckusick return (ino); 50417954Smckusick } 50517954Smckusick 50617954Smckusick /* 50717954Smckusick * free a directory inode 50817954Smckusick */ 50917954Smckusick freedir(ino, parent) 51017954Smckusick ino_t ino, parent; 51117954Smckusick { 51217954Smckusick DINODE *dp; 51317954Smckusick 51417954Smckusick if (ino != parent) { 51517954Smckusick dp = ginode(parent); 51617954Smckusick dp->di_nlink--; 51717954Smckusick inodirty(); 51817954Smckusick } 51917954Smckusick freeino(ino); 52017954Smckusick } 52117954Smckusick 52217954Smckusick /* 52316259Smckusick * generate a temporary name for the lost+found directory. 52416259Smckusick */ 52516259Smckusick lftempname(bufp, ino) 52616259Smckusick char *bufp; 52716259Smckusick ino_t ino; 52816259Smckusick { 52916259Smckusick register ino_t in; 53016259Smckusick register char *cp; 53116259Smckusick int namlen; 53216259Smckusick 53316259Smckusick cp = bufp + 2; 53416259Smckusick for (in = imax; in > 0; in /= 10) 53516259Smckusick cp++; 53616259Smckusick *--cp = 0; 53716259Smckusick namlen = cp - bufp; 53816259Smckusick in = ino; 53916259Smckusick while (cp > bufp) { 54016259Smckusick *--cp = (in % 10) + '0'; 54116259Smckusick in /= 10; 54216259Smckusick } 54316259Smckusick *cp = '#'; 54416259Smckusick return (namlen); 54516259Smckusick } 546