122045Sdist /* 222045Sdist * Copyright (c) 1980 Regents of the University of California. 322045Sdist * All rights reserved. The Berkeley software License Agreement 422045Sdist * specifies the terms and conditions for redistribution. 522045Sdist */ 622045Sdist 716259Smckusick #ifndef lint 8*30155Smckusick static char sccsid[] = "@(#)dir.c 5.2 (Berkeley) 11/24/86"; 922045Sdist #endif not lint 1016259Smckusick 1116259Smckusick #include <sys/param.h> 1216259Smckusick #include <sys/inode.h> 1316259Smckusick #include <sys/fs.h> 1416259Smckusick #define KERNEL 1516259Smckusick #include <sys/dir.h> 1616259Smckusick #undef KERNEL 1716259Smckusick #include "fsck.h" 1816259Smckusick 1916259Smckusick #define MINDIRSIZE (sizeof (struct dirtemplate)) 2016259Smckusick 2116259Smckusick char *endpathname = &pathname[BUFSIZ - 2]; 2216259Smckusick char *lfname = "lost+found"; 2317944Smckusick struct dirtemplate emptydir = { 0, DIRBLKSIZ }; 2417954Smckusick struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." }; 2516259Smckusick 2616259Smckusick DIRECT *fsck_readdir(); 2716259Smckusick 2816259Smckusick descend(parentino, inumber) 2916259Smckusick struct inodesc *parentino; 3016259Smckusick ino_t inumber; 3116259Smckusick { 3216259Smckusick register DINODE *dp; 3316259Smckusick struct inodesc curino; 3416259Smckusick 3516259Smckusick bzero((char *)&curino, sizeof(struct inodesc)); 3617936Smckusick if (statemap[inumber] != DSTATE) 3717936Smckusick errexit("BAD INODE %d TO DESCEND", statemap[inumber]); 3817936Smckusick statemap[inumber] = DFOUND; 3917943Smckusick dp = ginode(inumber); 4016259Smckusick if (dp->di_size == 0) { 4116259Smckusick direrr(inumber, "ZERO LENGTH DIRECTORY"); 4216259Smckusick if (reply("REMOVE") == 1) 4317936Smckusick statemap[inumber] = DCLEAR; 4416259Smckusick return; 4516259Smckusick } 4616259Smckusick if (dp->di_size < MINDIRSIZE) { 4716259Smckusick direrr(inumber, "DIRECTORY TOO SHORT"); 4816259Smckusick dp->di_size = MINDIRSIZE; 4916259Smckusick if (reply("FIX") == 1) 5016259Smckusick inodirty(); 5116259Smckusick } 5221742Smckusick if ((dp->di_size & (DIRBLKSIZ - 1)) != 0) { 5321742Smckusick pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d", 5421742Smckusick pathname, dp->di_size, DIRBLKSIZ); 5521742Smckusick dp->di_size = roundup(dp->di_size, DIRBLKSIZ); 5621742Smckusick if (preen) 5721742Smckusick printf(" (ADJUSTED)\n"); 5821742Smckusick if (preen || reply("ADJUST") == 1) 5921742Smckusick inodirty(); 6021742Smckusick } 6116259Smckusick curino.id_type = DATA; 6216259Smckusick curino.id_func = parentino->id_func; 6316259Smckusick curino.id_parent = parentino->id_number; 6416259Smckusick curino.id_number = inumber; 6516259Smckusick (void)ckinode(dp, &curino); 6616259Smckusick } 6716259Smckusick 6816259Smckusick dirscan(idesc) 6916259Smckusick register struct inodesc *idesc; 7016259Smckusick { 7116259Smckusick register DIRECT *dp; 7216259Smckusick int dsize, n; 7316259Smckusick long blksiz; 7416259Smckusick char dbuf[DIRBLKSIZ]; 7516259Smckusick 7616259Smckusick if (idesc->id_type != DATA) 7716259Smckusick errexit("wrong type to dirscan %d\n", idesc->id_type); 7817993Smckusick if (idesc->id_entryno == 0 && 7917993Smckusick (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0) 8017993Smckusick idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ); 8116259Smckusick blksiz = idesc->id_numfrags * sblock.fs_fsize; 8216259Smckusick if (outrange(idesc->id_blkno, idesc->id_numfrags)) { 8316259Smckusick idesc->id_filesize -= blksiz; 8416259Smckusick return (SKIP); 8516259Smckusick } 8616259Smckusick idesc->id_loc = 0; 8716259Smckusick for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) { 8816259Smckusick dsize = dp->d_reclen; 8916259Smckusick bcopy((char *)dp, dbuf, dsize); 9016259Smckusick idesc->id_dirp = (DIRECT *)dbuf; 9116259Smckusick if ((n = (*idesc->id_func)(idesc)) & ALTERED) { 9221742Smckusick getblk(&fileblk, idesc->id_blkno, blksiz); 9321742Smckusick if (fileblk.b_errs != NULL) { 9421742Smckusick n &= ~ALTERED; 9521742Smckusick } else { 9616259Smckusick bcopy(dbuf, (char *)dp, dsize); 9716259Smckusick dirty(&fileblk); 9816259Smckusick sbdirty(); 9921742Smckusick } 10016259Smckusick } 10116259Smckusick if (n & STOP) 10216259Smckusick return (n); 10316259Smckusick } 10416259Smckusick return (idesc->id_filesize > 0 ? KEEPON : STOP); 10516259Smckusick } 10616259Smckusick 10716259Smckusick /* 10816259Smckusick * get next entry in a directory. 10916259Smckusick */ 11016259Smckusick DIRECT * 11116259Smckusick fsck_readdir(idesc) 11216259Smckusick register struct inodesc *idesc; 11316259Smckusick { 11416259Smckusick register DIRECT *dp, *ndp; 11516259Smckusick long size, blksiz; 11616259Smckusick 11716259Smckusick blksiz = idesc->id_numfrags * sblock.fs_fsize; 11821742Smckusick getblk(&fileblk, idesc->id_blkno, blksiz); 11921742Smckusick if (fileblk.b_errs != NULL) { 12016259Smckusick idesc->id_filesize -= blksiz - idesc->id_loc; 12116259Smckusick return NULL; 12216259Smckusick } 12316259Smckusick if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 && 12416259Smckusick idesc->id_loc < blksiz) { 12516259Smckusick dp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); 12616259Smckusick if (dircheck(idesc, dp)) 12716259Smckusick goto dpok; 12816259Smckusick idesc->id_loc += DIRBLKSIZ; 12916259Smckusick idesc->id_filesize -= DIRBLKSIZ; 13016259Smckusick dp->d_reclen = DIRBLKSIZ; 13116259Smckusick dp->d_ino = 0; 13216259Smckusick dp->d_namlen = 0; 13316259Smckusick dp->d_name[0] = '\0'; 13417930Smckusick if (dofix(idesc, "DIRECTORY CORRUPTED")) 13516259Smckusick dirty(&fileblk); 13616259Smckusick return (dp); 13716259Smckusick } 13816259Smckusick dpok: 13916259Smckusick if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) 14016259Smckusick return NULL; 14116259Smckusick dp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); 14216259Smckusick idesc->id_loc += dp->d_reclen; 14316259Smckusick idesc->id_filesize -= dp->d_reclen; 14416259Smckusick if ((idesc->id_loc % DIRBLKSIZ) == 0) 14516259Smckusick return (dp); 14616259Smckusick ndp = (DIRECT *)(dirblk.b_buf + idesc->id_loc); 14716259Smckusick if (idesc->id_loc < blksiz && idesc->id_filesize > 0 && 14816259Smckusick dircheck(idesc, ndp) == 0) { 14916259Smckusick size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 15016259Smckusick dp->d_reclen += size; 15116259Smckusick idesc->id_loc += size; 15216259Smckusick idesc->id_filesize -= size; 15317930Smckusick if (dofix(idesc, "DIRECTORY CORRUPTED")) 15416259Smckusick dirty(&fileblk); 15516259Smckusick } 15616259Smckusick return (dp); 15716259Smckusick } 15816259Smckusick 15916259Smckusick /* 16016259Smckusick * Verify that a directory entry is valid. 16116259Smckusick * This is a superset of the checks made in the kernel. 16216259Smckusick */ 16316259Smckusick dircheck(idesc, dp) 16416259Smckusick struct inodesc *idesc; 16516259Smckusick register DIRECT *dp; 16616259Smckusick { 16716259Smckusick register int size; 16816259Smckusick register char *cp; 16916259Smckusick int spaceleft; 17016259Smckusick 17116259Smckusick size = DIRSIZ(dp); 17216259Smckusick spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 17316259Smckusick if (dp->d_ino < imax && 17416259Smckusick dp->d_reclen != 0 && 17516259Smckusick dp->d_reclen <= spaceleft && 17616259Smckusick (dp->d_reclen & 0x3) == 0 && 17716259Smckusick dp->d_reclen >= size && 17816259Smckusick idesc->id_filesize >= size && 17916259Smckusick dp->d_namlen <= MAXNAMLEN) { 18016259Smckusick if (dp->d_ino == 0) 18116259Smckusick return (1); 18216259Smckusick for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++) 18316259Smckusick if (*cp == 0 || (*cp++ & 0200)) 18416259Smckusick return (0); 18516259Smckusick if (*cp == 0) 18616259Smckusick return (1); 18716259Smckusick } 18816259Smckusick return (0); 18916259Smckusick } 19016259Smckusick 19116259Smckusick direrr(ino, s) 19216259Smckusick ino_t ino; 19316259Smckusick char *s; 19416259Smckusick { 19516259Smckusick register DINODE *dp; 19616259Smckusick 19716259Smckusick pwarn("%s ", s); 19816259Smckusick pinode(ino); 19916259Smckusick printf("\n"); 20017943Smckusick if (ino < ROOTINO || ino > imax) { 20117943Smckusick pfatal("NAME=%s\n", pathname); 20217943Smckusick return; 20317943Smckusick } 20417943Smckusick dp = ginode(ino); 20517943Smckusick if (ftypeok(dp)) 20617930Smckusick pfatal("%s=%s\n", DIRCT(dp) ? "DIR" : "FILE", pathname); 20716259Smckusick else 20816259Smckusick pfatal("NAME=%s\n", pathname); 20916259Smckusick } 21016259Smckusick 21116259Smckusick adjust(idesc, lcnt) 21216259Smckusick register struct inodesc *idesc; 21316259Smckusick short lcnt; 21416259Smckusick { 21516259Smckusick register DINODE *dp; 21616259Smckusick 21717943Smckusick dp = ginode(idesc->id_number); 21816259Smckusick if (dp->di_nlink == lcnt) { 21916259Smckusick if (linkup(idesc->id_number, (ino_t)0) == 0) 22016259Smckusick clri(idesc, "UNREF", 0); 22117936Smckusick } else { 22217930Smckusick pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname : 22317930Smckusick (DIRCT(dp) ? "DIR" : "FILE")); 22416259Smckusick pinode(idesc->id_number); 22516259Smckusick printf(" COUNT %d SHOULD BE %d", 22616259Smckusick dp->di_nlink, dp->di_nlink-lcnt); 22716259Smckusick if (preen) { 22816259Smckusick if (lcnt < 0) { 22916259Smckusick printf("\n"); 23017930Smckusick pfatal("LINK COUNT INCREASING"); 23116259Smckusick } 23216259Smckusick printf(" (ADJUSTED)\n"); 23316259Smckusick } 23416259Smckusick if (preen || reply("ADJUST") == 1) { 23516259Smckusick dp->di_nlink -= lcnt; 23616259Smckusick inodirty(); 23716259Smckusick } 23816259Smckusick } 23916259Smckusick } 24016259Smckusick 24116259Smckusick mkentry(idesc) 24216259Smckusick struct inodesc *idesc; 24316259Smckusick { 24416259Smckusick register DIRECT *dirp = idesc->id_dirp; 24516259Smckusick DIRECT newent; 24616259Smckusick int newlen, oldlen; 24716259Smckusick 24816259Smckusick newent.d_namlen = 11; 24916259Smckusick newlen = DIRSIZ(&newent); 25016259Smckusick if (dirp->d_ino != 0) 25116259Smckusick oldlen = DIRSIZ(dirp); 25216259Smckusick else 25316259Smckusick oldlen = 0; 25416259Smckusick if (dirp->d_reclen - oldlen < newlen) 25516259Smckusick return (KEEPON); 25616259Smckusick newent.d_reclen = dirp->d_reclen - oldlen; 25716259Smckusick dirp->d_reclen = oldlen; 25816259Smckusick dirp = (struct direct *)(((char *)dirp) + oldlen); 25916259Smckusick dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */ 26016259Smckusick dirp->d_reclen = newent.d_reclen; 26117954Smckusick dirp->d_namlen = strlen(idesc->id_name); 26217954Smckusick bcopy(idesc->id_name, dirp->d_name, dirp->d_namlen + 1); 26316259Smckusick return (ALTERED|STOP); 26416259Smckusick } 26516259Smckusick 26617957Smckusick chgino(idesc) 26716259Smckusick struct inodesc *idesc; 26816259Smckusick { 26916259Smckusick register DIRECT *dirp = idesc->id_dirp; 27016259Smckusick 27117957Smckusick if (bcmp(dirp->d_name, idesc->id_name, dirp->d_namlen + 1)) 27217957Smckusick return (KEEPON); 27317957Smckusick dirp->d_ino = idesc->id_parent;; 27417957Smckusick return (ALTERED|STOP); 27516259Smckusick } 27616259Smckusick 27716259Smckusick linkup(orphan, pdir) 27816259Smckusick ino_t orphan; 27916259Smckusick ino_t pdir; 28016259Smckusick { 28116259Smckusick register DINODE *dp; 28216259Smckusick int lostdir, len; 28317954Smckusick ino_t oldlfdir; 28416259Smckusick struct inodesc idesc; 28517954Smckusick char tempname[BUFSIZ]; 28617957Smckusick extern int pass4check(); 28716259Smckusick 28816259Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 28917943Smckusick dp = ginode(orphan); 29017930Smckusick lostdir = DIRCT(dp); 29116259Smckusick pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); 29216259Smckusick pinode(orphan); 29316259Smckusick if (preen && dp->di_size == 0) 29416259Smckusick return (0); 29516259Smckusick if (preen) 29616259Smckusick printf(" (RECONNECTED)\n"); 29716259Smckusick else 29816259Smckusick if (reply("RECONNECT") == 0) 29916259Smckusick return (0); 30016259Smckusick pathp = pathname; 30116259Smckusick *pathp++ = '/'; 30216259Smckusick *pathp = '\0'; 30316259Smckusick if (lfdir == 0) { 30417943Smckusick dp = ginode(ROOTINO); 30517930Smckusick idesc.id_name = lfname; 30616259Smckusick idesc.id_type = DATA; 30716259Smckusick idesc.id_func = findino; 30816259Smckusick idesc.id_number = ROOTINO; 30916259Smckusick (void)ckinode(dp, &idesc); 31017954Smckusick if (idesc.id_parent >= ROOTINO && idesc.id_parent < imax) { 31117954Smckusick lfdir = idesc.id_parent; 31217954Smckusick } else { 31317954Smckusick pwarn("NO lost+found DIRECTORY"); 31417954Smckusick if (preen || reply("CREATE")) { 31517992Smckusick lfdir = allocdir(ROOTINO, 0); 31617992Smckusick if (lfdir != 0) { 31717992Smckusick if (makeentry(ROOTINO, lfdir, lfname) != 0) { 31817954Smckusick if (preen) 31917954Smckusick printf(" (CREATED)\n"); 32017954Smckusick } else { 32117992Smckusick freedir(lfdir, ROOTINO); 32217992Smckusick lfdir = 0; 32317954Smckusick if (preen) 32417954Smckusick printf("\n"); 32517954Smckusick } 32617954Smckusick } 32717954Smckusick } 32817954Smckusick } 32917943Smckusick if (lfdir == 0) { 33017954Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); 33116259Smckusick printf("\n\n"); 33216259Smckusick return (0); 33316259Smckusick } 33416259Smckusick } 33517943Smckusick dp = ginode(lfdir); 33617957Smckusick if (!DIRCT(dp)) { 33717957Smckusick pfatal("lost+found IS NOT A DIRECTORY"); 33817957Smckusick if (reply("REALLOCATE") == 0) 33917957Smckusick return (0); 34017957Smckusick oldlfdir = lfdir; 34117957Smckusick if ((lfdir = allocdir(ROOTINO, 0)) == 0) { 34217957Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 34317957Smckusick return (0); 34417957Smckusick } 34517957Smckusick idesc.id_type = DATA; 34617957Smckusick idesc.id_func = chgino; 34717957Smckusick idesc.id_number = ROOTINO; 34817957Smckusick idesc.id_parent = lfdir; /* new inumber for lost+found */ 34917957Smckusick idesc.id_name = lfname; 35017957Smckusick if ((ckinode(ginode(ROOTINO), &idesc) & ALTERED) == 0) { 35117957Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 35217957Smckusick return (0); 35317957Smckusick } 35417957Smckusick inodirty(); 35517957Smckusick idesc.id_type = ADDR; 35617957Smckusick idesc.id_func = pass4check; 35717957Smckusick idesc.id_number = oldlfdir; 35817957Smckusick adjust(&idesc, lncntp[oldlfdir] + 1); 35917957Smckusick lncntp[oldlfdir] = 0; 36017957Smckusick dp = ginode(lfdir); 36117957Smckusick } 36217957Smckusick if (statemap[lfdir] != DFOUND) { 36317957Smckusick pfatal("SORRY. NO lost+found DIRECTORY\n\n"); 36416259Smckusick return (0); 36516259Smckusick } 36616259Smckusick len = strlen(lfname); 36716259Smckusick bcopy(lfname, pathp, len + 1); 36816259Smckusick pathp += len; 36917992Smckusick len = lftempname(tempname, orphan); 37017992Smckusick if (makeentry(lfdir, orphan, tempname) == 0) { 37116259Smckusick pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); 37216259Smckusick printf("\n\n"); 37316259Smckusick return (0); 37416259Smckusick } 37516259Smckusick lncntp[orphan]--; 37616259Smckusick *pathp++ = '/'; 377*30155Smckusick bcopy(tempname, pathp, len + 1); 37817954Smckusick pathp += len; 37916259Smckusick if (lostdir) { 38016259Smckusick dp = ginode(orphan); 38116259Smckusick idesc.id_type = DATA; 38217957Smckusick idesc.id_func = chgino; 38316259Smckusick idesc.id_number = orphan; 38416259Smckusick idesc.id_fix = DONTKNOW; 38517957Smckusick idesc.id_name = ".."; 38617957Smckusick idesc.id_parent = lfdir; /* new value for ".." */ 38716259Smckusick (void)ckinode(dp, &idesc); 38817943Smckusick dp = ginode(lfdir); 38917943Smckusick dp->di_nlink++; 39017943Smckusick inodirty(); 39117943Smckusick lncntp[lfdir]++; 39216259Smckusick pwarn("DIR I=%u CONNECTED. ", orphan); 39316259Smckusick printf("PARENT WAS I=%u\n", pdir); 39416259Smckusick if (preen == 0) 39516259Smckusick printf("\n"); 39616259Smckusick } 39716259Smckusick return (1); 39816259Smckusick } 39916259Smckusick 40016259Smckusick /* 40117944Smckusick * make an entry in a directory 40217944Smckusick */ 40317992Smckusick makeentry(parent, ino, name) 40417992Smckusick ino_t parent, ino; 40517992Smckusick char *name; 40617992Smckusick { 40717944Smckusick DINODE *dp; 40817992Smckusick struct inodesc idesc; 40917944Smckusick 41017992Smckusick if (parent < ROOTINO || parent >= imax || ino < ROOTINO || ino >= imax) 41117992Smckusick return (0); 41217992Smckusick bzero(&idesc, sizeof(struct inodesc)); 41317992Smckusick idesc.id_type = DATA; 41417992Smckusick idesc.id_func = mkentry; 41517992Smckusick idesc.id_number = parent; 41617992Smckusick idesc.id_parent = ino; /* this is the inode to enter */ 41717992Smckusick idesc.id_fix = DONTKNOW; 41817992Smckusick idesc.id_name = name; 41917992Smckusick dp = ginode(parent); 42017993Smckusick if (dp->di_size % DIRBLKSIZ) { 42117993Smckusick dp->di_size = roundup(dp->di_size, DIRBLKSIZ); 42217993Smckusick inodirty(); 42317993Smckusick } 42417992Smckusick if ((ckinode(dp, &idesc) & ALTERED) != 0) 42517944Smckusick return (1); 42617944Smckusick if (expanddir(dp) == 0) 42717944Smckusick return (0); 42817992Smckusick return (ckinode(dp, &idesc) & ALTERED); 42917944Smckusick } 43017944Smckusick 43117944Smckusick /* 43217944Smckusick * Attempt to expand the size of a directory 43317944Smckusick */ 43417944Smckusick expanddir(dp) 43517944Smckusick register DINODE *dp; 43617944Smckusick { 43717944Smckusick daddr_t lastbn, newblk; 43817944Smckusick char *cp, firstblk[DIRBLKSIZ]; 43917944Smckusick 44017944Smckusick lastbn = lblkno(&sblock, dp->di_size); 44117944Smckusick if (lastbn >= NDADDR - 1) 44217944Smckusick return (0); 44317944Smckusick if ((newblk = allocblk(sblock.fs_frag)) == 0) 44417944Smckusick return (0); 44517944Smckusick dp->di_db[lastbn + 1] = dp->di_db[lastbn]; 44617944Smckusick dp->di_db[lastbn] = newblk; 44717944Smckusick dp->di_size += sblock.fs_bsize; 44817944Smckusick dp->di_blocks += btodb(sblock.fs_bsize); 44921742Smckusick getblk(&fileblk, dp->di_db[lastbn + 1], 45021742Smckusick dblksize(&sblock, dp, lastbn + 1)); 45121742Smckusick if (fileblk.b_errs != NULL) 45217944Smckusick goto bad; 45317944Smckusick bcopy(dirblk.b_buf, firstblk, DIRBLKSIZ); 45421742Smckusick getblk(&fileblk, newblk, sblock.fs_bsize); 45521742Smckusick if (fileblk.b_errs != NULL) 45617944Smckusick goto bad; 45717944Smckusick bcopy(firstblk, dirblk.b_buf, DIRBLKSIZ); 45817944Smckusick for (cp = &dirblk.b_buf[DIRBLKSIZ]; 45917944Smckusick cp < &dirblk.b_buf[sblock.fs_bsize]; 46017944Smckusick cp += DIRBLKSIZ) 46117944Smckusick bcopy((char *)&emptydir, cp, sizeof emptydir); 46217944Smckusick dirty(&fileblk); 46321742Smckusick getblk(&fileblk, dp->di_db[lastbn + 1], 46421742Smckusick dblksize(&sblock, dp, lastbn + 1)); 46521742Smckusick if (fileblk.b_errs != NULL) 46617944Smckusick goto bad; 46717944Smckusick bcopy((char *)&emptydir, dirblk.b_buf, sizeof emptydir); 46817944Smckusick pwarn("NO SPACE LEFT IN %s", pathname); 46917944Smckusick if (preen) 47017944Smckusick printf(" (EXPANDED)\n"); 47117944Smckusick else if (reply("EXPAND") == 0) 47217944Smckusick goto bad; 47317944Smckusick dirty(&fileblk); 47417944Smckusick inodirty(); 47517944Smckusick return (1); 47617944Smckusick bad: 47717944Smckusick dp->di_db[lastbn] = dp->di_db[lastbn + 1]; 47817944Smckusick dp->di_db[lastbn + 1] = 0; 47917944Smckusick dp->di_size -= sblock.fs_bsize; 48017944Smckusick dp->di_blocks -= btodb(sblock.fs_bsize); 48117944Smckusick freeblk(newblk, sblock.fs_frag); 48217944Smckusick return (0); 48317944Smckusick } 48417944Smckusick 48517944Smckusick /* 48617954Smckusick * allocate a new directory 48717954Smckusick */ 48817954Smckusick allocdir(parent, request) 48917954Smckusick ino_t parent, request; 49017954Smckusick { 49117954Smckusick ino_t ino; 49217954Smckusick char *cp; 49317954Smckusick DINODE *dp; 49417954Smckusick 49517954Smckusick ino = allocino(request, IFDIR|0755); 49617954Smckusick dirhead.dot_ino = ino; 49717954Smckusick dirhead.dotdot_ino = parent; 49817954Smckusick dp = ginode(ino); 49921742Smckusick getblk(&fileblk, dp->di_db[0], sblock.fs_fsize); 50021742Smckusick if (fileblk.b_errs != NULL) { 50117954Smckusick freeino(ino); 50217954Smckusick return (0); 50317954Smckusick } 50417954Smckusick bcopy((char *)&dirhead, dirblk.b_buf, sizeof dirhead); 50517954Smckusick for (cp = &dirblk.b_buf[DIRBLKSIZ]; 50617954Smckusick cp < &dirblk.b_buf[sblock.fs_fsize]; 50717954Smckusick cp += DIRBLKSIZ) 50817954Smckusick bcopy((char *)&emptydir, cp, sizeof emptydir); 50917954Smckusick dirty(&fileblk); 51017954Smckusick dp->di_nlink = 2; 51117954Smckusick inodirty(); 51217954Smckusick if (ino == ROOTINO) { 51317954Smckusick lncntp[ino] = dp->di_nlink; 51417954Smckusick return(ino); 51517954Smckusick } 51617954Smckusick if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) { 51717954Smckusick freeino(ino); 51817954Smckusick return (0); 51917954Smckusick } 52017954Smckusick statemap[ino] = statemap[parent]; 52117954Smckusick if (statemap[ino] == DSTATE) { 52217954Smckusick lncntp[ino] = dp->di_nlink; 52317954Smckusick lncntp[parent]++; 52417954Smckusick } 52517954Smckusick dp = ginode(parent); 52617954Smckusick dp->di_nlink++; 52717954Smckusick inodirty(); 52817954Smckusick return (ino); 52917954Smckusick } 53017954Smckusick 53117954Smckusick /* 53217954Smckusick * free a directory inode 53317954Smckusick */ 53417954Smckusick freedir(ino, parent) 53517954Smckusick ino_t ino, parent; 53617954Smckusick { 53717954Smckusick DINODE *dp; 53817954Smckusick 53917954Smckusick if (ino != parent) { 54017954Smckusick dp = ginode(parent); 54117954Smckusick dp->di_nlink--; 54217954Smckusick inodirty(); 54317954Smckusick } 54417954Smckusick freeino(ino); 54517954Smckusick } 54617954Smckusick 54717954Smckusick /* 54816259Smckusick * generate a temporary name for the lost+found directory. 54916259Smckusick */ 55016259Smckusick lftempname(bufp, ino) 55116259Smckusick char *bufp; 55216259Smckusick ino_t ino; 55316259Smckusick { 55416259Smckusick register ino_t in; 55516259Smckusick register char *cp; 55616259Smckusick int namlen; 55716259Smckusick 55816259Smckusick cp = bufp + 2; 55916259Smckusick for (in = imax; in > 0; in /= 10) 56016259Smckusick cp++; 56116259Smckusick *--cp = 0; 56216259Smckusick namlen = cp - bufp; 56316259Smckusick in = ino; 56416259Smckusick while (cp > bufp) { 56516259Smckusick *--cp = (in % 10) + '0'; 56616259Smckusick in /= 10; 56716259Smckusick } 56816259Smckusick *cp = '#'; 56916259Smckusick return (namlen); 57016259Smckusick } 571