122045Sdist /* 239976Smckusick * Copyright (c) 1980, 1986 The Regents of the University of California. 339976Smckusick * All rights reserved. 439976Smckusick * 542701Sbostic * %sccs.include.redist.c% 622045Sdist */ 722045Sdist 816259Smckusick #ifndef lint 9*44934Smckusick static char sccsid[] = "@(#)dir.c 5.18 (Berkeley) 07/20/90"; 1039976Smckusick #endif /* not lint */ 1116259Smckusick 1216259Smckusick #include <sys/param.h> 1339383Smckusick #include <ufs/dinode.h> 1438336Smckusick #include <ufs/fs.h> 1516259Smckusick #define KERNEL 1638336Smckusick #include <ufs/dir.h> 1716259Smckusick #undef KERNEL 18*44934Smckusick #include <stdlib.h> 19*44934Smckusick #include <string.h> 2016259Smckusick #include "fsck.h" 2116259Smckusick 2216259Smckusick char *lfname = "lost+found"; 2336827Smckusick int lfmode = 01777; 2417944Smckusick struct dirtemplate emptydir = { 0, DIRBLKSIZ }; 2517954Smckusick struct dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." }; 2616259Smckusick 2739973Smckusick struct direct *fsck_readdir(); 2839973Smckusick struct bufarea *getdirblk(); 2916259Smckusick 3040022Smckusick /* 3140022Smckusick * Propagate connected state through the tree. 3240022Smckusick */ 3340022Smckusick propagate() 3416259Smckusick { 3540022Smckusick register struct inoinfo **inpp, *inp; 3640022Smckusick struct inoinfo **inpend; 3740022Smckusick long change; 3816259Smckusick 3940022Smckusick inpend = &inpsort[inplast]; 4040022Smckusick do { 4140022Smckusick change = 0; 4240022Smckusick for (inpp = inpsort; inpp < inpend; inpp++) { 4340022Smckusick inp = *inpp; 4440022Smckusick if (inp->i_parent == 0) 4540022Smckusick continue; 4640022Smckusick if (statemap[inp->i_parent] == DFOUND && 4740022Smckusick statemap[inp->i_number] == DSTATE) { 4840022Smckusick statemap[inp->i_number] = DFOUND; 4940022Smckusick change++; 5040022Smckusick } 5139980Smckusick } 5240022Smckusick } while (change > 0); 5316259Smckusick } 5416259Smckusick 5540022Smckusick /* 5640022Smckusick * Scan each entry in a directory block. 5740022Smckusick */ 5816259Smckusick dirscan(idesc) 5916259Smckusick register struct inodesc *idesc; 6016259Smckusick { 6139973Smckusick register struct direct *dp; 6239973Smckusick register struct bufarea *bp; 6316259Smckusick int dsize, n; 6416259Smckusick long blksiz; 6516259Smckusick char dbuf[DIRBLKSIZ]; 6616259Smckusick 6716259Smckusick if (idesc->id_type != DATA) 6816259Smckusick errexit("wrong type to dirscan %d\n", idesc->id_type); 6917993Smckusick if (idesc->id_entryno == 0 && 7017993Smckusick (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0) 7117993Smckusick idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ); 7216259Smckusick blksiz = idesc->id_numfrags * sblock.fs_fsize; 7339973Smckusick if (chkrange(idesc->id_blkno, idesc->id_numfrags)) { 7416259Smckusick idesc->id_filesize -= blksiz; 7516259Smckusick return (SKIP); 7616259Smckusick } 7716259Smckusick idesc->id_loc = 0; 7816259Smckusick for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) { 7916259Smckusick dsize = dp->d_reclen; 80*44934Smckusick bcopy((char *)dp, dbuf, (size_t)dsize); 8139973Smckusick idesc->id_dirp = (struct direct *)dbuf; 8216259Smckusick if ((n = (*idesc->id_func)(idesc)) & ALTERED) { 8334225Smckusick bp = getdirblk(idesc->id_blkno, blksiz); 8440570Smckusick bcopy(dbuf, bp->b_un.b_buf + idesc->id_loc - dsize, 85*44934Smckusick (size_t)dsize); 8634225Smckusick dirty(bp); 8730354Smckusick sbdirty(); 8816259Smckusick } 8916259Smckusick if (n & STOP) 9016259Smckusick return (n); 9116259Smckusick } 9216259Smckusick return (idesc->id_filesize > 0 ? KEEPON : STOP); 9316259Smckusick } 9416259Smckusick 9516259Smckusick /* 9616259Smckusick * get next entry in a directory. 9716259Smckusick */ 9839973Smckusick struct direct * 9916259Smckusick fsck_readdir(idesc) 10016259Smckusick register struct inodesc *idesc; 10116259Smckusick { 10239973Smckusick register struct direct *dp, *ndp; 10339973Smckusick register struct bufarea *bp; 10440570Smckusick long size, blksiz, fix; 10516259Smckusick 10616259Smckusick blksiz = idesc->id_numfrags * sblock.fs_fsize; 10734225Smckusick bp = getdirblk(idesc->id_blkno, blksiz); 10816259Smckusick if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 && 10916259Smckusick idesc->id_loc < blksiz) { 11039973Smckusick dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 11116259Smckusick if (dircheck(idesc, dp)) 11216259Smckusick goto dpok; 11316259Smckusick idesc->id_loc += DIRBLKSIZ; 11416259Smckusick idesc->id_filesize -= DIRBLKSIZ; 11540570Smckusick fix = dofix(idesc, "DIRECTORY CORRUPTED"); 11640570Smckusick bp = getdirblk(idesc->id_blkno, blksiz); 11740570Smckusick dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 11816259Smckusick dp->d_reclen = DIRBLKSIZ; 11916259Smckusick dp->d_ino = 0; 12016259Smckusick dp->d_namlen = 0; 12116259Smckusick dp->d_name[0] = '\0'; 12240570Smckusick if (fix) 12334225Smckusick dirty(bp); 12416259Smckusick return (dp); 12516259Smckusick } 12616259Smckusick dpok: 12716259Smckusick if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) 12816259Smckusick return NULL; 12939973Smckusick dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 13016259Smckusick idesc->id_loc += dp->d_reclen; 13116259Smckusick idesc->id_filesize -= dp->d_reclen; 13216259Smckusick if ((idesc->id_loc % DIRBLKSIZ) == 0) 13316259Smckusick return (dp); 13439973Smckusick ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 13516259Smckusick if (idesc->id_loc < blksiz && idesc->id_filesize > 0 && 13616259Smckusick dircheck(idesc, ndp) == 0) { 13716259Smckusick size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 13816259Smckusick idesc->id_loc += size; 13916259Smckusick idesc->id_filesize -= size; 14040570Smckusick fix = dofix(idesc, "DIRECTORY CORRUPTED"); 14140570Smckusick bp = getdirblk(idesc->id_blkno, blksiz); 14240570Smckusick dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 14340570Smckusick dp->d_reclen += size; 14440570Smckusick if (fix) 14534225Smckusick dirty(bp); 14616259Smckusick } 14716259Smckusick return (dp); 14816259Smckusick } 14916259Smckusick 15016259Smckusick /* 15116259Smckusick * Verify that a directory entry is valid. 15216259Smckusick * This is a superset of the checks made in the kernel. 15316259Smckusick */ 15416259Smckusick dircheck(idesc, dp) 15516259Smckusick struct inodesc *idesc; 15639973Smckusick register struct direct *dp; 15716259Smckusick { 15816259Smckusick register int size; 15916259Smckusick register char *cp; 16016259Smckusick int spaceleft; 16116259Smckusick 16216259Smckusick size = DIRSIZ(dp); 16316259Smckusick spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 16439973Smckusick if (dp->d_ino < maxino && 16516259Smckusick dp->d_reclen != 0 && 16616259Smckusick dp->d_reclen <= spaceleft && 16716259Smckusick (dp->d_reclen & 0x3) == 0 && 16816259Smckusick dp->d_reclen >= size && 16916259Smckusick idesc->id_filesize >= size && 17016259Smckusick dp->d_namlen <= MAXNAMLEN) { 17116259Smckusick if (dp->d_ino == 0) 17216259Smckusick return (1); 17316259Smckusick for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++) 17440651Smckusick if (*cp == 0 || (*cp++ == '/')) 17516259Smckusick return (0); 17616259Smckusick if (*cp == 0) 17716259Smckusick return (1); 17816259Smckusick } 17916259Smckusick return (0); 18016259Smckusick } 18116259Smckusick 18239973Smckusick direrror(ino, errmesg) 18316259Smckusick ino_t ino; 18439973Smckusick char *errmesg; 18516259Smckusick { 18640022Smckusick 18740022Smckusick fileerror(ino, ino, errmesg); 18840022Smckusick } 18940022Smckusick 19040022Smckusick fileerror(cwd, ino, errmesg) 19140022Smckusick ino_t cwd, ino; 19240022Smckusick char *errmesg; 19340022Smckusick { 19439973Smckusick register struct dinode *dp; 19540022Smckusick char pathbuf[MAXPATHLEN + 1]; 19616259Smckusick 19739973Smckusick pwarn("%s ", errmesg); 19816259Smckusick pinode(ino); 19916259Smckusick printf("\n"); 20040022Smckusick getpathname(pathbuf, cwd, ino); 20139973Smckusick if (ino < ROOTINO || ino > maxino) { 20240022Smckusick pfatal("NAME=%s\n", pathbuf); 20317943Smckusick return; 20417943Smckusick } 20517943Smckusick dp = ginode(ino); 20617943Smckusick if (ftypeok(dp)) 20739973Smckusick pfatal("%s=%s\n", 20840022Smckusick (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf); 20916259Smckusick else 21040022Smckusick pfatal("NAME=%s\n", pathbuf); 21116259Smckusick } 21216259Smckusick 21316259Smckusick adjust(idesc, lcnt) 21416259Smckusick register struct inodesc *idesc; 21516259Smckusick short lcnt; 21616259Smckusick { 21739973Smckusick register struct dinode *dp; 21816259Smckusick 21917943Smckusick dp = ginode(idesc->id_number); 22016259Smckusick if (dp->di_nlink == lcnt) { 22116259Smckusick if (linkup(idesc->id_number, (ino_t)0) == 0) 22216259Smckusick clri(idesc, "UNREF", 0); 22317936Smckusick } else { 22417930Smckusick pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname : 22539973Smckusick ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE")); 22616259Smckusick pinode(idesc->id_number); 22716259Smckusick printf(" COUNT %d SHOULD BE %d", 22839973Smckusick dp->di_nlink, dp->di_nlink - lcnt); 22916259Smckusick if (preen) { 23016259Smckusick if (lcnt < 0) { 23116259Smckusick printf("\n"); 23217930Smckusick pfatal("LINK COUNT INCREASING"); 23316259Smckusick } 23416259Smckusick printf(" (ADJUSTED)\n"); 23516259Smckusick } 23616259Smckusick if (preen || reply("ADJUST") == 1) { 23716259Smckusick dp->di_nlink -= lcnt; 23816259Smckusick inodirty(); 23916259Smckusick } 24016259Smckusick } 24116259Smckusick } 24216259Smckusick 24316259Smckusick mkentry(idesc) 24416259Smckusick struct inodesc *idesc; 24516259Smckusick { 24639973Smckusick register struct direct *dirp = idesc->id_dirp; 24739973Smckusick struct direct newent; 24816259Smckusick int newlen, oldlen; 24916259Smckusick 25040022Smckusick newent.d_namlen = strlen(idesc->id_name); 25116259Smckusick newlen = DIRSIZ(&newent); 25216259Smckusick if (dirp->d_ino != 0) 25316259Smckusick oldlen = DIRSIZ(dirp); 25416259Smckusick else 25516259Smckusick oldlen = 0; 25616259Smckusick if (dirp->d_reclen - oldlen < newlen) 25716259Smckusick return (KEEPON); 25816259Smckusick newent.d_reclen = dirp->d_reclen - oldlen; 25916259Smckusick dirp->d_reclen = oldlen; 26016259Smckusick dirp = (struct direct *)(((char *)dirp) + oldlen); 26116259Smckusick dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */ 26216259Smckusick dirp->d_reclen = newent.d_reclen; 26340022Smckusick dirp->d_namlen = newent.d_namlen; 264*44934Smckusick bcopy(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1); 26516259Smckusick return (ALTERED|STOP); 26616259Smckusick } 26716259Smckusick 26817957Smckusick chgino(idesc) 26916259Smckusick struct inodesc *idesc; 27016259Smckusick { 27139973Smckusick register struct direct *dirp = idesc->id_dirp; 27216259Smckusick 27339973Smckusick if (bcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1)) 27417957Smckusick return (KEEPON); 27539973Smckusick dirp->d_ino = idesc->id_parent; 27617957Smckusick return (ALTERED|STOP); 27716259Smckusick } 27816259Smckusick 27939973Smckusick linkup(orphan, parentdir) 28016259Smckusick ino_t orphan; 28139973Smckusick ino_t parentdir; 28216259Smckusick { 28339973Smckusick register struct dinode *dp; 28440022Smckusick int lostdir; 28517954Smckusick ino_t oldlfdir; 28616259Smckusick struct inodesc idesc; 28717954Smckusick char tempname[BUFSIZ]; 28817957Smckusick extern int pass4check(); 28916259Smckusick 29016259Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 29117943Smckusick dp = ginode(orphan); 29239973Smckusick lostdir = (dp->di_mode & IFMT) == IFDIR; 29316259Smckusick pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); 29416259Smckusick pinode(orphan); 29516259Smckusick if (preen && dp->di_size == 0) 29616259Smckusick return (0); 29716259Smckusick if (preen) 29816259Smckusick printf(" (RECONNECTED)\n"); 29916259Smckusick else 30016259Smckusick if (reply("RECONNECT") == 0) 30116259Smckusick return (0); 30216259Smckusick if (lfdir == 0) { 30317943Smckusick dp = ginode(ROOTINO); 30417930Smckusick idesc.id_name = lfname; 30516259Smckusick idesc.id_type = DATA; 30616259Smckusick idesc.id_func = findino; 30716259Smckusick idesc.id_number = ROOTINO; 30830354Smckusick if ((ckinode(dp, &idesc) & FOUND) != 0) { 30917954Smckusick lfdir = idesc.id_parent; 31017954Smckusick } else { 31117954Smckusick pwarn("NO lost+found DIRECTORY"); 31217954Smckusick if (preen || reply("CREATE")) { 31339973Smckusick lfdir = allocdir(ROOTINO, (ino_t)0, lfmode); 31417992Smckusick if (lfdir != 0) { 31517992Smckusick if (makeentry(ROOTINO, lfdir, lfname) != 0) { 31617954Smckusick if (preen) 31717954Smckusick printf(" (CREATED)\n"); 31817954Smckusick } else { 31917992Smckusick freedir(lfdir, ROOTINO); 32017992Smckusick lfdir = 0; 32117954Smckusick if (preen) 32217954Smckusick printf("\n"); 32317954Smckusick } 32417954Smckusick } 32517954Smckusick } 32617954Smckusick } 32717943Smckusick if (lfdir == 0) { 32817954Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); 32916259Smckusick printf("\n\n"); 33016259Smckusick return (0); 33116259Smckusick } 33216259Smckusick } 33317943Smckusick dp = ginode(lfdir); 33439973Smckusick if ((dp->di_mode & IFMT) != IFDIR) { 33517957Smckusick pfatal("lost+found IS NOT A DIRECTORY"); 33617957Smckusick if (reply("REALLOCATE") == 0) 33717957Smckusick return (0); 33817957Smckusick oldlfdir = lfdir; 33939973Smckusick if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) { 34017957Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 34117957Smckusick return (0); 34217957Smckusick } 34340022Smckusick if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) { 34417957Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 34517957Smckusick return (0); 34617957Smckusick } 34717957Smckusick inodirty(); 34817957Smckusick idesc.id_type = ADDR; 34917957Smckusick idesc.id_func = pass4check; 35017957Smckusick idesc.id_number = oldlfdir; 35117957Smckusick adjust(&idesc, lncntp[oldlfdir] + 1); 35217957Smckusick lncntp[oldlfdir] = 0; 35317957Smckusick dp = ginode(lfdir); 35417957Smckusick } 35517957Smckusick if (statemap[lfdir] != DFOUND) { 35617957Smckusick pfatal("SORRY. NO lost+found DIRECTORY\n\n"); 35716259Smckusick return (0); 35816259Smckusick } 35940022Smckusick (void)lftempname(tempname, orphan); 36017992Smckusick if (makeentry(lfdir, orphan, tempname) == 0) { 36116259Smckusick pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); 36216259Smckusick printf("\n\n"); 36316259Smckusick return (0); 36416259Smckusick } 36516259Smckusick lncntp[orphan]--; 36616259Smckusick if (lostdir) { 36740022Smckusick if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 && 36840022Smckusick parentdir != (ino_t)-1) 369*44934Smckusick (void)makeentry(orphan, lfdir, ".."); 37017943Smckusick dp = ginode(lfdir); 37117943Smckusick dp->di_nlink++; 37217943Smckusick inodirty(); 37317943Smckusick lncntp[lfdir]++; 374*44934Smckusick pwarn("DIR I=%lu CONNECTED. ", orphan); 37540022Smckusick if (parentdir != (ino_t)-1) 376*44934Smckusick printf("PARENT WAS I=%lu\n", parentdir); 37716259Smckusick if (preen == 0) 37816259Smckusick printf("\n"); 37916259Smckusick } 38016259Smckusick return (1); 38116259Smckusick } 38216259Smckusick 38316259Smckusick /* 38440022Smckusick * fix an entry in a directory. 38540022Smckusick */ 38640022Smckusick changeino(dir, name, newnum) 38740022Smckusick ino_t dir; 38840022Smckusick char *name; 38940022Smckusick ino_t newnum; 39040022Smckusick { 39140022Smckusick struct inodesc idesc; 39240022Smckusick 39340022Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 39440022Smckusick idesc.id_type = DATA; 39540022Smckusick idesc.id_func = chgino; 39640022Smckusick idesc.id_number = dir; 39740022Smckusick idesc.id_fix = DONTKNOW; 39840022Smckusick idesc.id_name = name; 39940022Smckusick idesc.id_parent = newnum; /* new value for name */ 40040022Smckusick return (ckinode(ginode(dir), &idesc)); 40140022Smckusick } 40240022Smckusick 40340022Smckusick /* 40417944Smckusick * make an entry in a directory 40517944Smckusick */ 40617992Smckusick makeentry(parent, ino, name) 40717992Smckusick ino_t parent, ino; 40817992Smckusick char *name; 40917992Smckusick { 41039973Smckusick struct dinode *dp; 41117992Smckusick struct inodesc idesc; 41240022Smckusick char pathbuf[MAXPATHLEN + 1]; 41317944Smckusick 41439973Smckusick if (parent < ROOTINO || parent >= maxino || 41539973Smckusick ino < ROOTINO || ino >= maxino) 41617992Smckusick return (0); 41739973Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 41817992Smckusick idesc.id_type = DATA; 41917992Smckusick idesc.id_func = mkentry; 42017992Smckusick idesc.id_number = parent; 42117992Smckusick idesc.id_parent = ino; /* this is the inode to enter */ 42217992Smckusick idesc.id_fix = DONTKNOW; 42317992Smckusick idesc.id_name = name; 42417992Smckusick dp = ginode(parent); 42517993Smckusick if (dp->di_size % DIRBLKSIZ) { 42617993Smckusick dp->di_size = roundup(dp->di_size, DIRBLKSIZ); 42717993Smckusick inodirty(); 42817993Smckusick } 42917992Smckusick if ((ckinode(dp, &idesc) & ALTERED) != 0) 43017944Smckusick return (1); 43140022Smckusick getpathname(pathbuf, parent, parent); 43240022Smckusick dp = ginode(parent); 43340022Smckusick if (expanddir(dp, pathbuf) == 0) 43417944Smckusick return (0); 43517992Smckusick return (ckinode(dp, &idesc) & ALTERED); 43617944Smckusick } 43717944Smckusick 43817944Smckusick /* 43917944Smckusick * Attempt to expand the size of a directory 44017944Smckusick */ 44140022Smckusick expanddir(dp, name) 44239973Smckusick register struct dinode *dp; 44340022Smckusick char *name; 44417944Smckusick { 44517944Smckusick daddr_t lastbn, newblk; 44639973Smckusick register struct bufarea *bp; 44717944Smckusick char *cp, firstblk[DIRBLKSIZ]; 44817944Smckusick 44917944Smckusick lastbn = lblkno(&sblock, dp->di_size); 45041133Smckusick if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0) 45117944Smckusick return (0); 45217944Smckusick if ((newblk = allocblk(sblock.fs_frag)) == 0) 45317944Smckusick return (0); 45417944Smckusick dp->di_db[lastbn + 1] = dp->di_db[lastbn]; 45517944Smckusick dp->di_db[lastbn] = newblk; 45617944Smckusick dp->di_size += sblock.fs_bsize; 45717944Smckusick dp->di_blocks += btodb(sblock.fs_bsize); 45834225Smckusick bp = getdirblk(dp->di_db[lastbn + 1], 459*44934Smckusick (long)dblksize(&sblock, dp, lastbn + 1)); 46038375Smckusick if (bp->b_errs) 46117944Smckusick goto bad; 46234225Smckusick bcopy(bp->b_un.b_buf, firstblk, DIRBLKSIZ); 46334225Smckusick bp = getdirblk(newblk, sblock.fs_bsize); 46438375Smckusick if (bp->b_errs) 46517944Smckusick goto bad; 46634225Smckusick bcopy(firstblk, bp->b_un.b_buf, DIRBLKSIZ); 46734225Smckusick for (cp = &bp->b_un.b_buf[DIRBLKSIZ]; 46834225Smckusick cp < &bp->b_un.b_buf[sblock.fs_bsize]; 46917944Smckusick cp += DIRBLKSIZ) 47017944Smckusick bcopy((char *)&emptydir, cp, sizeof emptydir); 47134225Smckusick dirty(bp); 47234225Smckusick bp = getdirblk(dp->di_db[lastbn + 1], 473*44934Smckusick (long)dblksize(&sblock, dp, lastbn + 1)); 47438375Smckusick if (bp->b_errs) 47517944Smckusick goto bad; 47634225Smckusick bcopy((char *)&emptydir, bp->b_un.b_buf, sizeof emptydir); 47740022Smckusick pwarn("NO SPACE LEFT IN %s", name); 47817944Smckusick if (preen) 47917944Smckusick printf(" (EXPANDED)\n"); 48017944Smckusick else if (reply("EXPAND") == 0) 48117944Smckusick goto bad; 48234225Smckusick dirty(bp); 48317944Smckusick inodirty(); 48417944Smckusick return (1); 48517944Smckusick bad: 48617944Smckusick dp->di_db[lastbn] = dp->di_db[lastbn + 1]; 48717944Smckusick dp->di_db[lastbn + 1] = 0; 48817944Smckusick dp->di_size -= sblock.fs_bsize; 48917944Smckusick dp->di_blocks -= btodb(sblock.fs_bsize); 49017944Smckusick freeblk(newblk, sblock.fs_frag); 49117944Smckusick return (0); 49217944Smckusick } 49317944Smckusick 49417944Smckusick /* 49517954Smckusick * allocate a new directory 49617954Smckusick */ 49736827Smckusick allocdir(parent, request, mode) 49817954Smckusick ino_t parent, request; 49936827Smckusick int mode; 50017954Smckusick { 50117954Smckusick ino_t ino; 50217954Smckusick char *cp; 50339973Smckusick struct dinode *dp; 50439973Smckusick register struct bufarea *bp; 50517954Smckusick 50636827Smckusick ino = allocino(request, IFDIR|mode); 50717954Smckusick dirhead.dot_ino = ino; 50817954Smckusick dirhead.dotdot_ino = parent; 50917954Smckusick dp = ginode(ino); 51034225Smckusick bp = getdirblk(dp->di_db[0], sblock.fs_fsize); 51138375Smckusick if (bp->b_errs) { 51217954Smckusick freeino(ino); 51317954Smckusick return (0); 51417954Smckusick } 51534225Smckusick bcopy((char *)&dirhead, bp->b_un.b_buf, sizeof dirhead); 51634225Smckusick for (cp = &bp->b_un.b_buf[DIRBLKSIZ]; 51734225Smckusick cp < &bp->b_un.b_buf[sblock.fs_fsize]; 51817954Smckusick cp += DIRBLKSIZ) 51917954Smckusick bcopy((char *)&emptydir, cp, sizeof emptydir); 52034225Smckusick dirty(bp); 52117954Smckusick dp->di_nlink = 2; 52217954Smckusick inodirty(); 52317954Smckusick if (ino == ROOTINO) { 52417954Smckusick lncntp[ino] = dp->di_nlink; 52517954Smckusick return(ino); 52617954Smckusick } 52717954Smckusick if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) { 52817954Smckusick freeino(ino); 52917954Smckusick return (0); 53017954Smckusick } 53117954Smckusick statemap[ino] = statemap[parent]; 53217954Smckusick if (statemap[ino] == DSTATE) { 53317954Smckusick lncntp[ino] = dp->di_nlink; 53417954Smckusick lncntp[parent]++; 53517954Smckusick } 53617954Smckusick dp = ginode(parent); 53717954Smckusick dp->di_nlink++; 53817954Smckusick inodirty(); 53917954Smckusick return (ino); 54017954Smckusick } 54117954Smckusick 54217954Smckusick /* 54317954Smckusick * free a directory inode 54417954Smckusick */ 54517954Smckusick freedir(ino, parent) 54617954Smckusick ino_t ino, parent; 54717954Smckusick { 54839973Smckusick struct dinode *dp; 54917954Smckusick 55017954Smckusick if (ino != parent) { 55117954Smckusick dp = ginode(parent); 55217954Smckusick dp->di_nlink--; 55317954Smckusick inodirty(); 55417954Smckusick } 55517954Smckusick freeino(ino); 55617954Smckusick } 55717954Smckusick 55817954Smckusick /* 55916259Smckusick * generate a temporary name for the lost+found directory. 56016259Smckusick */ 56116259Smckusick lftempname(bufp, ino) 56216259Smckusick char *bufp; 56316259Smckusick ino_t ino; 56416259Smckusick { 56516259Smckusick register ino_t in; 56616259Smckusick register char *cp; 56716259Smckusick int namlen; 56816259Smckusick 56916259Smckusick cp = bufp + 2; 57039973Smckusick for (in = maxino; in > 0; in /= 10) 57116259Smckusick cp++; 57216259Smckusick *--cp = 0; 57316259Smckusick namlen = cp - bufp; 57416259Smckusick in = ino; 57516259Smckusick while (cp > bufp) { 57616259Smckusick *--cp = (in % 10) + '0'; 57716259Smckusick in /= 10; 57816259Smckusick } 57916259Smckusick *cp = '#'; 58016259Smckusick return (namlen); 58116259Smckusick } 58234225Smckusick 58334225Smckusick /* 58434225Smckusick * Get a directory block. 58534225Smckusick * Insure that it is held until another is requested. 58634225Smckusick */ 58739973Smckusick struct bufarea * 58834225Smckusick getdirblk(blkno, size) 58934225Smckusick daddr_t blkno; 59034225Smckusick long size; 59134225Smckusick { 59234225Smckusick 59340648Smckusick if (pdirbp != 0) 59440648Smckusick pdirbp->b_flags &= ~B_INUSE; 59540648Smckusick pdirbp = getdatablk(blkno, size); 59640648Smckusick return (pdirbp); 59734225Smckusick } 598