116259Smckusick #ifndef lint 2*17957Smckusick static char version[] = "@(#)dir.c 3.7 (Berkeley) 02/13/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 curino.id_filesize = dp->di_size; 5116259Smckusick (void)ckinode(dp, &curino); 5216259Smckusick } 5316259Smckusick 5416259Smckusick dirscan(idesc) 5516259Smckusick register struct inodesc *idesc; 5616259Smckusick { 5716259Smckusick register DIRECT *dp; 5816259Smckusick int dsize, n; 5916259Smckusick long blksiz; 6016259Smckusick char dbuf[DIRBLKSIZ]; 6116259Smckusick 6216259Smckusick if (idesc->id_type != DATA) 6316259Smckusick errexit("wrong type to dirscan %d\n", idesc->id_type); 6416259Smckusick blksiz = idesc->id_numfrags * sblock.fs_fsize; 6516259Smckusick if (outrange(idesc->id_blkno, idesc->id_numfrags)) { 6616259Smckusick idesc->id_filesize -= blksiz; 6716259Smckusick return (SKIP); 6816259Smckusick } 6916259Smckusick idesc->id_loc = 0; 7016259Smckusick for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) { 7116259Smckusick dsize = dp->d_reclen; 7216259Smckusick bcopy((char *)dp, dbuf, dsize); 7316259Smckusick idesc->id_dirp = (DIRECT *)dbuf; 7416259Smckusick if ((n = (*idesc->id_func)(idesc)) & ALTERED) { 7516259Smckusick if (getblk(&fileblk, idesc->id_blkno, blksiz) != NULL) { 7616259Smckusick bcopy(dbuf, (char *)dp, dsize); 7716259Smckusick dirty(&fileblk); 7816259Smckusick sbdirty(); 7916259Smckusick } else 8016259Smckusick n &= ~ALTERED; 8116259Smckusick } 8216259Smckusick if (n & STOP) 8316259Smckusick return (n); 8416259Smckusick } 8516259Smckusick return (idesc->id_filesize > 0 ? KEEPON : STOP); 8616259Smckusick } 8716259Smckusick 8816259Smckusick /* 8916259Smckusick * get next entry in a directory. 9016259Smckusick */ 9116259Smckusick DIRECT * 9216259Smckusick fsck_readdir(idesc) 9316259Smckusick register struct inodesc *idesc; 9416259Smckusick { 9516259Smckusick register DIRECT *dp, *ndp; 9616259Smckusick long size, blksiz; 9716259Smckusick 9816259Smckusick blksiz = idesc->id_numfrags * sblock.fs_fsize; 9916259Smckusick if (getblk(&fileblk, idesc->id_blkno, blksiz) == NULL) { 10016259Smckusick idesc->id_filesize -= blksiz - idesc->id_loc; 10116259Smckusick return NULL; 10216259Smckusick } 10316259Smckusick if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 && 10416259Smckusick idesc->id_loc < blksiz) { 10516259Smckusick dp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); 10616259Smckusick if (dircheck(idesc, dp)) 10716259Smckusick goto dpok; 10816259Smckusick idesc->id_loc += DIRBLKSIZ; 10916259Smckusick idesc->id_filesize -= DIRBLKSIZ; 11016259Smckusick dp->d_reclen = DIRBLKSIZ; 11116259Smckusick dp->d_ino = 0; 11216259Smckusick dp->d_namlen = 0; 11316259Smckusick dp->d_name[0] = '\0'; 11417930Smckusick if (dofix(idesc, "DIRECTORY CORRUPTED")) 11516259Smckusick dirty(&fileblk); 11616259Smckusick return (dp); 11716259Smckusick } 11816259Smckusick dpok: 11916259Smckusick if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) 12016259Smckusick return NULL; 12116259Smckusick dp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); 12216259Smckusick idesc->id_loc += dp->d_reclen; 12316259Smckusick idesc->id_filesize -= dp->d_reclen; 12416259Smckusick if ((idesc->id_loc % DIRBLKSIZ) == 0) 12516259Smckusick return (dp); 12616259Smckusick ndp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); 12716259Smckusick if (idesc->id_loc < blksiz && idesc->id_filesize > 0 && 12816259Smckusick dircheck(idesc, ndp) == 0) { 12916259Smckusick size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 13016259Smckusick dp->d_reclen += size; 13116259Smckusick idesc->id_loc += size; 13216259Smckusick idesc->id_filesize -= size; 13317930Smckusick if (dofix(idesc, "DIRECTORY CORRUPTED")) 13416259Smckusick dirty(&fileblk); 13516259Smckusick } 13616259Smckusick return (dp); 13716259Smckusick } 13816259Smckusick 13916259Smckusick /* 14016259Smckusick * Verify that a directory entry is valid. 14116259Smckusick * This is a superset of the checks made in the kernel. 14216259Smckusick */ 14316259Smckusick dircheck(idesc, dp) 14416259Smckusick struct inodesc *idesc; 14516259Smckusick register DIRECT *dp; 14616259Smckusick { 14716259Smckusick register int size; 14816259Smckusick register char *cp; 14916259Smckusick int spaceleft; 15016259Smckusick 15116259Smckusick size = DIRSIZ(dp); 15216259Smckusick spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 15316259Smckusick if (dp->d_ino < imax && 15416259Smckusick dp->d_reclen != 0 && 15516259Smckusick dp->d_reclen <= spaceleft && 15616259Smckusick (dp->d_reclen & 0x3) == 0 && 15716259Smckusick dp->d_reclen >= size && 15816259Smckusick idesc->id_filesize >= size && 15916259Smckusick dp->d_namlen <= MAXNAMLEN) { 16016259Smckusick if (dp->d_ino == 0) 16116259Smckusick return (1); 16216259Smckusick for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++) 16316259Smckusick if (*cp == 0 || (*cp++ & 0200)) 16416259Smckusick return (0); 16516259Smckusick if (*cp == 0) 16616259Smckusick return (1); 16716259Smckusick } 16816259Smckusick return (0); 16916259Smckusick } 17016259Smckusick 17116259Smckusick direrr(ino, s) 17216259Smckusick ino_t ino; 17316259Smckusick char *s; 17416259Smckusick { 17516259Smckusick register DINODE *dp; 17616259Smckusick 17716259Smckusick pwarn("%s ", s); 17816259Smckusick pinode(ino); 17916259Smckusick printf("\n"); 18017943Smckusick if (ino < ROOTINO || ino > imax) { 18117943Smckusick pfatal("NAME=%s\n", pathname); 18217943Smckusick return; 18317943Smckusick } 18417943Smckusick dp = ginode(ino); 18517943Smckusick if (ftypeok(dp)) 18617930Smckusick pfatal("%s=%s\n", DIRCT(dp) ? "DIR" : "FILE", pathname); 18716259Smckusick else 18816259Smckusick pfatal("NAME=%s\n", pathname); 18916259Smckusick } 19016259Smckusick 19116259Smckusick adjust(idesc, lcnt) 19216259Smckusick register struct inodesc *idesc; 19316259Smckusick short lcnt; 19416259Smckusick { 19516259Smckusick register DINODE *dp; 19616259Smckusick 19717943Smckusick dp = ginode(idesc->id_number); 19816259Smckusick if (dp->di_nlink == lcnt) { 19916259Smckusick if (linkup(idesc->id_number, (ino_t)0) == 0) 20016259Smckusick clri(idesc, "UNREF", 0); 20117936Smckusick } else { 20217930Smckusick pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname : 20317930Smckusick (DIRCT(dp) ? "DIR" : "FILE")); 20416259Smckusick pinode(idesc->id_number); 20516259Smckusick printf(" COUNT %d SHOULD BE %d", 20616259Smckusick dp->di_nlink, dp->di_nlink-lcnt); 20716259Smckusick if (preen) { 20816259Smckusick if (lcnt < 0) { 20916259Smckusick printf("\n"); 21017930Smckusick pfatal("LINK COUNT INCREASING"); 21116259Smckusick } 21216259Smckusick printf(" (ADJUSTED)\n"); 21316259Smckusick } 21416259Smckusick if (preen || reply("ADJUST") == 1) { 21516259Smckusick dp->di_nlink -= lcnt; 21616259Smckusick inodirty(); 21716259Smckusick } 21816259Smckusick } 21916259Smckusick } 22016259Smckusick 22116259Smckusick mkentry(idesc) 22216259Smckusick struct inodesc *idesc; 22316259Smckusick { 22416259Smckusick register DIRECT *dirp = idesc->id_dirp; 22516259Smckusick DIRECT newent; 22616259Smckusick int newlen, oldlen; 22716259Smckusick 22816259Smckusick newent.d_namlen = 11; 22916259Smckusick newlen = DIRSIZ(&newent); 23016259Smckusick if (dirp->d_ino != 0) 23116259Smckusick oldlen = DIRSIZ(dirp); 23216259Smckusick else 23316259Smckusick oldlen = 0; 23416259Smckusick if (dirp->d_reclen - oldlen < newlen) 23516259Smckusick return (KEEPON); 23616259Smckusick newent.d_reclen = dirp->d_reclen - oldlen; 23716259Smckusick dirp->d_reclen = oldlen; 23816259Smckusick dirp = (struct direct *)(((char *)dirp) + oldlen); 23916259Smckusick dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */ 24016259Smckusick dirp->d_reclen = newent.d_reclen; 24117954Smckusick dirp->d_namlen = strlen(idesc->id_name); 24217954Smckusick bcopy(idesc->id_name, dirp->d_name, dirp->d_namlen + 1); 24316259Smckusick return (ALTERED|STOP); 24416259Smckusick } 24516259Smckusick 246*17957Smckusick chgino(idesc) 24716259Smckusick struct inodesc *idesc; 24816259Smckusick { 24916259Smckusick register DIRECT *dirp = idesc->id_dirp; 25016259Smckusick 251*17957Smckusick if (bcmp(dirp->d_name, idesc->id_name, dirp->d_namlen + 1)) 252*17957Smckusick return (KEEPON); 253*17957Smckusick dirp->d_ino = idesc->id_parent;; 254*17957Smckusick return (ALTERED|STOP); 25516259Smckusick } 25616259Smckusick 25716259Smckusick linkup(orphan, pdir) 25816259Smckusick ino_t orphan; 25916259Smckusick ino_t pdir; 26016259Smckusick { 26116259Smckusick register DINODE *dp; 26216259Smckusick int lostdir, len; 26317954Smckusick ino_t oldlfdir; 26416259Smckusick struct inodesc idesc; 26517954Smckusick char tempname[BUFSIZ]; 266*17957Smckusick extern int pass4check(); 26716259Smckusick 26816259Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 26917943Smckusick dp = ginode(orphan); 27017930Smckusick lostdir = DIRCT(dp); 27116259Smckusick pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); 27216259Smckusick pinode(orphan); 27316259Smckusick if (preen && dp->di_size == 0) 27416259Smckusick return (0); 27516259Smckusick if (preen) 27616259Smckusick printf(" (RECONNECTED)\n"); 27716259Smckusick else 27816259Smckusick if (reply("RECONNECT") == 0) 27916259Smckusick return (0); 28016259Smckusick pathp = pathname; 28116259Smckusick *pathp++ = '/'; 28216259Smckusick *pathp = '\0'; 28316259Smckusick if (lfdir == 0) { 28417943Smckusick dp = ginode(ROOTINO); 28517930Smckusick idesc.id_name = lfname; 28616259Smckusick idesc.id_type = DATA; 28716259Smckusick idesc.id_func = findino; 28816259Smckusick idesc.id_number = ROOTINO; 28916259Smckusick idesc.id_filesize = dp->di_size; 29016259Smckusick (void)ckinode(dp, &idesc); 29117954Smckusick if (idesc.id_parent >= ROOTINO && idesc.id_parent < imax) { 29217954Smckusick lfdir = idesc.id_parent; 29317954Smckusick } else { 29417954Smckusick pwarn("NO lost+found DIRECTORY"); 29517954Smckusick if (preen || reply("CREATE")) { 29617954Smckusick idesc.id_func = mkentry; 29717954Smckusick idesc.id_parent = allocdir(ROOTINO, 0); 29817954Smckusick idesc.id_filesize = dp->di_size; 29917954Smckusick if (idesc.id_parent != 0) { 30017954Smckusick if (makeentry(dp, &idesc) != 0) { 30117954Smckusick lfdir = idesc.id_parent; 30217954Smckusick if (preen) 30317954Smckusick printf(" (CREATED)\n"); 30417954Smckusick } else { 30517954Smckusick freedir(idesc.id_parent, ROOTINO); 30617954Smckusick if (preen) 30717954Smckusick printf("\n"); 30817954Smckusick } 30917954Smckusick } 31017954Smckusick } 31117954Smckusick } 31217943Smckusick if (lfdir == 0) { 31317954Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); 31416259Smckusick printf("\n\n"); 31516259Smckusick return (0); 31616259Smckusick } 31716259Smckusick } 31817943Smckusick dp = ginode(lfdir); 319*17957Smckusick if (!DIRCT(dp)) { 320*17957Smckusick pfatal("lost+found IS NOT A DIRECTORY"); 321*17957Smckusick if (reply("REALLOCATE") == 0) 322*17957Smckusick return (0); 323*17957Smckusick oldlfdir = lfdir; 324*17957Smckusick if ((lfdir = allocdir(ROOTINO, 0)) == 0) { 325*17957Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 326*17957Smckusick return (0); 327*17957Smckusick } 328*17957Smckusick idesc.id_type = DATA; 329*17957Smckusick idesc.id_func = chgino; 330*17957Smckusick idesc.id_number = ROOTINO; 331*17957Smckusick idesc.id_parent = lfdir; /* new inumber for lost+found */ 332*17957Smckusick idesc.id_name = lfname; 333*17957Smckusick if ((ckinode(ginode(ROOTINO), &idesc) & ALTERED) == 0) { 334*17957Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 335*17957Smckusick return (0); 336*17957Smckusick } 337*17957Smckusick inodirty(); 338*17957Smckusick idesc.id_type = ADDR; 339*17957Smckusick idesc.id_func = pass4check; 340*17957Smckusick idesc.id_number = oldlfdir; 341*17957Smckusick adjust(&idesc, lncntp[oldlfdir] + 1); 342*17957Smckusick lncntp[oldlfdir] = 0; 343*17957Smckusick dp = ginode(lfdir); 344*17957Smckusick } 345*17957Smckusick if (statemap[lfdir] != DFOUND) { 346*17957Smckusick pfatal("SORRY. NO lost+found DIRECTORY\n\n"); 34716259Smckusick return (0); 34816259Smckusick } 34917944Smckusick if (dp->di_size % DIRBLKSIZ) { 35017944Smckusick dp->di_size = roundup(dp->di_size, DIRBLKSIZ); 35116259Smckusick inodirty(); 35216259Smckusick } 35316259Smckusick len = strlen(lfname); 35416259Smckusick bcopy(lfname, pathp, len + 1); 35516259Smckusick pathp += len; 35616259Smckusick idesc.id_type = DATA; 35716259Smckusick idesc.id_func = mkentry; 35816259Smckusick idesc.id_number = lfdir; 35916259Smckusick idesc.id_filesize = dp->di_size; 36016259Smckusick idesc.id_parent = orphan; /* this is the inode to enter */ 36116259Smckusick idesc.id_fix = DONTKNOW; 36217954Smckusick idesc.id_name = tempname; 36317954Smckusick len = lftempname(idesc.id_name, orphan); 36417944Smckusick if (makeentry(dp, &idesc) == 0) { 36516259Smckusick pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); 36616259Smckusick printf("\n\n"); 36716259Smckusick return (0); 36816259Smckusick } 36916259Smckusick lncntp[orphan]--; 37016259Smckusick *pathp++ = '/'; 37117954Smckusick bcopy(idesc.id_name, pathp, len + 1); 37217954Smckusick pathp += len; 37316259Smckusick if (lostdir) { 37416259Smckusick dp = ginode(orphan); 37516259Smckusick idesc.id_type = DATA; 376*17957Smckusick idesc.id_func = chgino; 37716259Smckusick idesc.id_number = orphan; 37816259Smckusick idesc.id_filesize = dp->di_size; 37916259Smckusick idesc.id_fix = DONTKNOW; 380*17957Smckusick idesc.id_name = ".."; 381*17957Smckusick idesc.id_parent = lfdir; /* new value for ".." */ 38216259Smckusick (void)ckinode(dp, &idesc); 38317943Smckusick dp = ginode(lfdir); 38417943Smckusick dp->di_nlink++; 38517943Smckusick inodirty(); 38617943Smckusick lncntp[lfdir]++; 38716259Smckusick pwarn("DIR I=%u CONNECTED. ", orphan); 38816259Smckusick printf("PARENT WAS I=%u\n", pdir); 38916259Smckusick if (preen == 0) 39016259Smckusick printf("\n"); 39116259Smckusick } 39216259Smckusick return (1); 39316259Smckusick } 39416259Smckusick 39516259Smckusick /* 39617944Smckusick * make an entry in a directory 39717944Smckusick */ 39817944Smckusick makeentry(dp, idesc) 39917944Smckusick DINODE *dp; 40017944Smckusick struct inodesc *idesc; 40117944Smckusick { 40217944Smckusick 40317944Smckusick if ((ckinode(dp, idesc) & ALTERED) != 0) 40417944Smckusick return (1); 40517944Smckusick if (expanddir(dp) == 0) 40617944Smckusick return (0); 40717944Smckusick idesc->id_filesize = dp->di_size; 40817944Smckusick return (ckinode(dp, idesc) & ALTERED); 40917944Smckusick } 41017944Smckusick 41117944Smckusick /* 41217944Smckusick * Attempt to expand the size of a directory 41317944Smckusick */ 41417944Smckusick expanddir(dp) 41517944Smckusick register DINODE *dp; 41617944Smckusick { 41717944Smckusick daddr_t lastbn, newblk; 41817944Smckusick char *cp, firstblk[DIRBLKSIZ]; 41917944Smckusick 42017944Smckusick lastbn = lblkno(&sblock, dp->di_size); 42117944Smckusick if (lastbn >= NDADDR - 1) 42217944Smckusick return (0); 42317944Smckusick if ((newblk = allocblk(sblock.fs_frag)) == 0) 42417944Smckusick return (0); 42517944Smckusick dp->di_db[lastbn + 1] = dp->di_db[lastbn]; 42617944Smckusick dp->di_db[lastbn] = newblk; 42717944Smckusick dp->di_size += sblock.fs_bsize; 42817944Smckusick dp->di_blocks += btodb(sblock.fs_bsize); 42917944Smckusick if (getblk(&fileblk, dp->di_db[lastbn + 1], 43017944Smckusick dblksize(&sblock, dp, lastbn + 1)) == NULL) 43117944Smckusick goto bad; 43217944Smckusick bcopy(dirblk.b_buf, firstblk, DIRBLKSIZ); 43317944Smckusick if (getblk(&fileblk, newblk, sblock.fs_bsize) == NULL) 43417944Smckusick goto bad; 43517944Smckusick bcopy(firstblk, dirblk.b_buf, DIRBLKSIZ); 43617944Smckusick for (cp = &dirblk.b_buf[DIRBLKSIZ]; 43717944Smckusick cp < &dirblk.b_buf[sblock.fs_bsize]; 43817944Smckusick cp += DIRBLKSIZ) 43917944Smckusick bcopy((char *)&emptydir, cp, sizeof emptydir); 44017944Smckusick dirty(&fileblk); 44117944Smckusick if (getblk(&fileblk, dp->di_db[lastbn + 1], 44217944Smckusick dblksize(&sblock, dp, lastbn + 1)) == NULL) 44317944Smckusick goto bad; 44417944Smckusick bcopy((char *)&emptydir, dirblk.b_buf, sizeof emptydir); 44517944Smckusick pwarn("NO SPACE LEFT IN %s", pathname); 44617944Smckusick if (preen) 44717944Smckusick printf(" (EXPANDED)\n"); 44817944Smckusick else if (reply("EXPAND") == 0) 44917944Smckusick goto bad; 45017944Smckusick dirty(&fileblk); 45117944Smckusick inodirty(); 45217944Smckusick return (1); 45317944Smckusick bad: 45417944Smckusick dp->di_db[lastbn] = dp->di_db[lastbn + 1]; 45517944Smckusick dp->di_db[lastbn + 1] = 0; 45617944Smckusick dp->di_size -= sblock.fs_bsize; 45717944Smckusick dp->di_blocks -= btodb(sblock.fs_bsize); 45817944Smckusick freeblk(newblk, sblock.fs_frag); 45917944Smckusick return (0); 46017944Smckusick } 46117944Smckusick 46217944Smckusick /* 46317954Smckusick * allocate a new directory 46417954Smckusick */ 46517954Smckusick allocdir(parent, request) 46617954Smckusick ino_t parent, request; 46717954Smckusick { 46817954Smckusick ino_t ino; 46917954Smckusick char *cp; 47017954Smckusick DINODE *dp; 47117954Smckusick 47217954Smckusick ino = allocino(request, IFDIR|0755); 47317954Smckusick dirhead.dot_ino = ino; 47417954Smckusick dirhead.dotdot_ino = parent; 47517954Smckusick dp = ginode(ino); 47617954Smckusick if (getblk(&fileblk, dp->di_db[0], sblock.fs_fsize) == NULL) { 47717954Smckusick freeino(ino); 47817954Smckusick return (0); 47917954Smckusick } 48017954Smckusick bcopy((char *)&dirhead, dirblk.b_buf, sizeof dirhead); 48117954Smckusick for (cp = &dirblk.b_buf[DIRBLKSIZ]; 48217954Smckusick cp < &dirblk.b_buf[sblock.fs_fsize]; 48317954Smckusick cp += DIRBLKSIZ) 48417954Smckusick bcopy((char *)&emptydir, cp, sizeof emptydir); 48517954Smckusick dirty(&fileblk); 48617954Smckusick dp->di_nlink = 2; 48717954Smckusick inodirty(); 48817954Smckusick if (ino == ROOTINO) { 48917954Smckusick lncntp[ino] = dp->di_nlink; 49017954Smckusick return(ino); 49117954Smckusick } 49217954Smckusick if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) { 49317954Smckusick freeino(ino); 49417954Smckusick return (0); 49517954Smckusick } 49617954Smckusick statemap[ino] = statemap[parent]; 49717954Smckusick if (statemap[ino] == DSTATE) { 49817954Smckusick lncntp[ino] = dp->di_nlink; 49917954Smckusick lncntp[parent]++; 50017954Smckusick } 50117954Smckusick dp = ginode(parent); 50217954Smckusick dp->di_nlink++; 50317954Smckusick inodirty(); 50417954Smckusick return (ino); 50517954Smckusick } 50617954Smckusick 50717954Smckusick /* 50817954Smckusick * free a directory inode 50917954Smckusick */ 51017954Smckusick freedir(ino, parent) 51117954Smckusick ino_t ino, parent; 51217954Smckusick { 51317954Smckusick DINODE *dp; 51417954Smckusick 51517954Smckusick if (ino != parent) { 51617954Smckusick dp = ginode(parent); 51717954Smckusick dp->di_nlink--; 51817954Smckusick inodirty(); 51917954Smckusick } 52017954Smckusick freeino(ino); 52117954Smckusick } 52217954Smckusick 52317954Smckusick /* 52416259Smckusick * generate a temporary name for the lost+found directory. 52516259Smckusick */ 52616259Smckusick lftempname(bufp, ino) 52716259Smckusick char *bufp; 52816259Smckusick ino_t ino; 52916259Smckusick { 53016259Smckusick register ino_t in; 53116259Smckusick register char *cp; 53216259Smckusick int namlen; 53316259Smckusick 53416259Smckusick cp = bufp + 2; 53516259Smckusick for (in = imax; in > 0; in /= 10) 53616259Smckusick cp++; 53716259Smckusick *--cp = 0; 53816259Smckusick namlen = cp - bufp; 53916259Smckusick in = ino; 54016259Smckusick while (cp > bufp) { 54116259Smckusick *--cp = (in % 10) + '0'; 54216259Smckusick in /= 10; 54316259Smckusick } 54416259Smckusick *cp = '#'; 54516259Smckusick return (namlen); 54616259Smckusick } 547