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*55074Smckusick static char sccsid[] = "@(#)dir.c 5.23 (Berkeley) 07/12/92"; 1039976Smckusick #endif /* not lint */ 1116259Smckusick 1216259Smckusick #include <sys/param.h> 1353702Smckusick #include <sys/time.h> 1451532Sbostic #include <ufs/ufs/dinode.h> 1551532Sbostic #include <ufs/ufs/dir.h> 1651532Sbostic #include <ufs/ffs/fs.h> 1744934Smckusick #include <stdlib.h> 1844934Smckusick #include <string.h> 1916259Smckusick #include "fsck.h" 2016259Smckusick 2116259Smckusick char *lfname = "lost+found"; 2236827Smckusick int lfmode = 01777; 2317944Smckusick struct dirtemplate emptydir = { 0, DIRBLKSIZ }; 2454604Smckusick struct dirtemplate dirhead = { 2554604Smckusick 0, 12, DT_DIR, 1, ".", 2654604Smckusick 0, DIRBLKSIZ - 12, DT_DIR, 2, ".." 2754604Smckusick }; 2854604Smckusick struct odirtemplate odirhead = { 2954604Smckusick 0, 12, 1, ".", 3054604Smckusick 0, DIRBLKSIZ - 12, 2, ".." 3154604Smckusick }; 3216259Smckusick 3339973Smckusick struct direct *fsck_readdir(); 3439973Smckusick struct bufarea *getdirblk(); 3516259Smckusick 3640022Smckusick /* 3740022Smckusick * Propagate connected state through the tree. 3840022Smckusick */ 3940022Smckusick propagate() 4016259Smckusick { 4140022Smckusick register struct inoinfo **inpp, *inp; 4240022Smckusick struct inoinfo **inpend; 4340022Smckusick long change; 4416259Smckusick 4540022Smckusick inpend = &inpsort[inplast]; 4640022Smckusick do { 4740022Smckusick change = 0; 4840022Smckusick for (inpp = inpsort; inpp < inpend; inpp++) { 4940022Smckusick inp = *inpp; 5040022Smckusick if (inp->i_parent == 0) 5140022Smckusick continue; 5240022Smckusick if (statemap[inp->i_parent] == DFOUND && 5340022Smckusick statemap[inp->i_number] == DSTATE) { 5440022Smckusick statemap[inp->i_number] = DFOUND; 5540022Smckusick change++; 5640022Smckusick } 5739980Smckusick } 5840022Smckusick } while (change > 0); 5916259Smckusick } 6016259Smckusick 6140022Smckusick /* 6240022Smckusick * Scan each entry in a directory block. 6340022Smckusick */ 6416259Smckusick dirscan(idesc) 6516259Smckusick register struct inodesc *idesc; 6616259Smckusick { 6739973Smckusick register struct direct *dp; 6839973Smckusick register struct bufarea *bp; 6916259Smckusick int dsize, n; 7016259Smckusick long blksiz; 7116259Smckusick char dbuf[DIRBLKSIZ]; 7216259Smckusick 7316259Smckusick if (idesc->id_type != DATA) 7416259Smckusick errexit("wrong type to dirscan %d\n", idesc->id_type); 7517993Smckusick if (idesc->id_entryno == 0 && 7617993Smckusick (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0) 7717993Smckusick idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ); 7816259Smckusick blksiz = idesc->id_numfrags * sblock.fs_fsize; 7939973Smckusick if (chkrange(idesc->id_blkno, idesc->id_numfrags)) { 8016259Smckusick idesc->id_filesize -= blksiz; 8116259Smckusick return (SKIP); 8216259Smckusick } 8316259Smckusick idesc->id_loc = 0; 8416259Smckusick for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) { 8516259Smckusick dsize = dp->d_reclen; 8644934Smckusick bcopy((char *)dp, dbuf, (size_t)dsize); 8754604Smckusick # if (BYTE_ORDER == LITTLE_ENDIAN) 8854604Smckusick if (!newinofmt) { 8954604Smckusick struct direct *tdp = (struct direct *)dbuf; 9054604Smckusick u_char tmp; 9154604Smckusick 9254604Smckusick tmp = tdp->d_namlen; 9354604Smckusick tdp->d_namlen = tdp->d_type; 9454604Smckusick tdp->d_type = tmp; 9554604Smckusick } 9654604Smckusick # endif 9739973Smckusick idesc->id_dirp = (struct direct *)dbuf; 9816259Smckusick if ((n = (*idesc->id_func)(idesc)) & ALTERED) { 9954604Smckusick # if (BYTE_ORDER == LITTLE_ENDIAN) 10054604Smckusick if (!newinofmt && !doinglevel2) { 10154604Smckusick struct direct *tdp; 10254604Smckusick u_char tmp; 10354604Smckusick 10454604Smckusick tdp = (struct direct *)dbuf; 10554604Smckusick tmp = tdp->d_namlen; 10654604Smckusick tdp->d_namlen = tdp->d_type; 10754604Smckusick tdp->d_type = tmp; 10854604Smckusick } 10954604Smckusick # endif 11034225Smckusick bp = getdirblk(idesc->id_blkno, blksiz); 11140570Smckusick bcopy(dbuf, bp->b_un.b_buf + idesc->id_loc - dsize, 11244934Smckusick (size_t)dsize); 11334225Smckusick dirty(bp); 11430354Smckusick sbdirty(); 11516259Smckusick } 11616259Smckusick if (n & STOP) 11716259Smckusick return (n); 11816259Smckusick } 11916259Smckusick return (idesc->id_filesize > 0 ? KEEPON : STOP); 12016259Smckusick } 12116259Smckusick 12216259Smckusick /* 12316259Smckusick * get next entry in a directory. 12416259Smckusick */ 12539973Smckusick struct direct * 12616259Smckusick fsck_readdir(idesc) 12716259Smckusick register struct inodesc *idesc; 12816259Smckusick { 12939973Smckusick register struct direct *dp, *ndp; 13039973Smckusick register struct bufarea *bp; 13154604Smckusick long size, blksiz, fix, dploc; 13216259Smckusick 13316259Smckusick blksiz = idesc->id_numfrags * sblock.fs_fsize; 13434225Smckusick bp = getdirblk(idesc->id_blkno, blksiz); 13516259Smckusick if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 && 13616259Smckusick idesc->id_loc < blksiz) { 13739973Smckusick dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 13816259Smckusick if (dircheck(idesc, dp)) 13916259Smckusick goto dpok; 14016259Smckusick idesc->id_loc += DIRBLKSIZ; 14116259Smckusick idesc->id_filesize -= DIRBLKSIZ; 14240570Smckusick fix = dofix(idesc, "DIRECTORY CORRUPTED"); 14340570Smckusick bp = getdirblk(idesc->id_blkno, blksiz); 14440570Smckusick dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 14516259Smckusick dp->d_reclen = DIRBLKSIZ; 14616259Smckusick dp->d_ino = 0; 14754604Smckusick dp->d_type = 0; 14816259Smckusick dp->d_namlen = 0; 14916259Smckusick dp->d_name[0] = '\0'; 15040570Smckusick if (fix) 15134225Smckusick dirty(bp); 15216259Smckusick return (dp); 15316259Smckusick } 15416259Smckusick dpok: 15516259Smckusick if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) 15616259Smckusick return NULL; 15754604Smckusick dploc = idesc->id_loc; 15854604Smckusick dp = (struct direct *)(bp->b_un.b_buf + dploc); 15916259Smckusick idesc->id_loc += dp->d_reclen; 16016259Smckusick idesc->id_filesize -= dp->d_reclen; 16116259Smckusick if ((idesc->id_loc % DIRBLKSIZ) == 0) 16216259Smckusick return (dp); 16339973Smckusick ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 16416259Smckusick if (idesc->id_loc < blksiz && idesc->id_filesize > 0 && 16516259Smckusick dircheck(idesc, ndp) == 0) { 16616259Smckusick size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 16716259Smckusick idesc->id_loc += size; 16816259Smckusick idesc->id_filesize -= size; 16940570Smckusick fix = dofix(idesc, "DIRECTORY CORRUPTED"); 17040570Smckusick bp = getdirblk(idesc->id_blkno, blksiz); 17154604Smckusick dp = (struct direct *)(bp->b_un.b_buf + dploc); 17240570Smckusick dp->d_reclen += size; 17340570Smckusick if (fix) 17434225Smckusick dirty(bp); 17516259Smckusick } 17616259Smckusick return (dp); 17716259Smckusick } 17816259Smckusick 17916259Smckusick /* 18016259Smckusick * Verify that a directory entry is valid. 18116259Smckusick * This is a superset of the checks made in the kernel. 18216259Smckusick */ 18316259Smckusick dircheck(idesc, dp) 18416259Smckusick struct inodesc *idesc; 18539973Smckusick register struct direct *dp; 18616259Smckusick { 18716259Smckusick register int size; 18816259Smckusick register char *cp; 18954604Smckusick u_char namlen, type; 19016259Smckusick int spaceleft; 19116259Smckusick 19254604Smckusick size = DIRSIZ(!newinofmt, dp); 19316259Smckusick spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 19454604Smckusick # if (BYTE_ORDER == LITTLE_ENDIAN) 19554604Smckusick if (!newinofmt) { 19654604Smckusick type = dp->d_namlen; 19754604Smckusick namlen = dp->d_type; 19854604Smckusick } else { 19954604Smckusick namlen = dp->d_namlen; 20054604Smckusick type = dp->d_type; 20154604Smckusick } 20254604Smckusick # else 20354604Smckusick namlen = dp->d_namlen; 20454604Smckusick type = dp->d_type; 20554604Smckusick # endif 20639973Smckusick if (dp->d_ino < maxino && 20716259Smckusick dp->d_reclen != 0 && 20816259Smckusick dp->d_reclen <= spaceleft && 20916259Smckusick (dp->d_reclen & 0x3) == 0 && 21016259Smckusick dp->d_reclen >= size && 21116259Smckusick idesc->id_filesize >= size && 21254604Smckusick namlen <= MAXNAMLEN && 21354604Smckusick type <= 15) { 21416259Smckusick if (dp->d_ino == 0) 21516259Smckusick return (1); 21654604Smckusick for (cp = dp->d_name, size = 0; size < namlen; size++) 21740651Smckusick if (*cp == 0 || (*cp++ == '/')) 21816259Smckusick return (0); 21916259Smckusick if (*cp == 0) 22016259Smckusick return (1); 22116259Smckusick } 22216259Smckusick return (0); 22316259Smckusick } 22416259Smckusick 22539973Smckusick direrror(ino, errmesg) 22616259Smckusick ino_t ino; 22739973Smckusick char *errmesg; 22816259Smckusick { 22940022Smckusick 23040022Smckusick fileerror(ino, ino, errmesg); 23140022Smckusick } 23240022Smckusick 23340022Smckusick fileerror(cwd, ino, errmesg) 23440022Smckusick ino_t cwd, ino; 23540022Smckusick char *errmesg; 23640022Smckusick { 23739973Smckusick register struct dinode *dp; 23840022Smckusick char pathbuf[MAXPATHLEN + 1]; 23916259Smckusick 24039973Smckusick pwarn("%s ", errmesg); 24116259Smckusick pinode(ino); 24216259Smckusick printf("\n"); 24340022Smckusick getpathname(pathbuf, cwd, ino); 24439973Smckusick if (ino < ROOTINO || ino > maxino) { 24540022Smckusick pfatal("NAME=%s\n", pathbuf); 24617943Smckusick return; 24717943Smckusick } 24817943Smckusick dp = ginode(ino); 24917943Smckusick if (ftypeok(dp)) 25039973Smckusick pfatal("%s=%s\n", 25140022Smckusick (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf); 25216259Smckusick else 25340022Smckusick pfatal("NAME=%s\n", pathbuf); 25416259Smckusick } 25516259Smckusick 25616259Smckusick adjust(idesc, lcnt) 25716259Smckusick register struct inodesc *idesc; 25816259Smckusick short lcnt; 25916259Smckusick { 26039973Smckusick register struct dinode *dp; 26116259Smckusick 26217943Smckusick dp = ginode(idesc->id_number); 26316259Smckusick if (dp->di_nlink == lcnt) { 26416259Smckusick if (linkup(idesc->id_number, (ino_t)0) == 0) 26516259Smckusick clri(idesc, "UNREF", 0); 26617936Smckusick } else { 26717930Smckusick pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname : 26839973Smckusick ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE")); 26916259Smckusick pinode(idesc->id_number); 27016259Smckusick printf(" COUNT %d SHOULD BE %d", 27139973Smckusick dp->di_nlink, dp->di_nlink - lcnt); 27216259Smckusick if (preen) { 27316259Smckusick if (lcnt < 0) { 27416259Smckusick printf("\n"); 27517930Smckusick pfatal("LINK COUNT INCREASING"); 27616259Smckusick } 27716259Smckusick printf(" (ADJUSTED)\n"); 27816259Smckusick } 27916259Smckusick if (preen || reply("ADJUST") == 1) { 28016259Smckusick dp->di_nlink -= lcnt; 28116259Smckusick inodirty(); 28216259Smckusick } 28316259Smckusick } 28416259Smckusick } 28516259Smckusick 28616259Smckusick mkentry(idesc) 28716259Smckusick struct inodesc *idesc; 28816259Smckusick { 28939973Smckusick register struct direct *dirp = idesc->id_dirp; 29039973Smckusick struct direct newent; 29116259Smckusick int newlen, oldlen; 29216259Smckusick 29340022Smckusick newent.d_namlen = strlen(idesc->id_name); 29454604Smckusick newlen = DIRSIZ(0, &newent); 29516259Smckusick if (dirp->d_ino != 0) 29654604Smckusick oldlen = DIRSIZ(0, dirp); 29716259Smckusick else 29816259Smckusick oldlen = 0; 29916259Smckusick if (dirp->d_reclen - oldlen < newlen) 30016259Smckusick return (KEEPON); 30116259Smckusick newent.d_reclen = dirp->d_reclen - oldlen; 30216259Smckusick dirp->d_reclen = oldlen; 30316259Smckusick dirp = (struct direct *)(((char *)dirp) + oldlen); 30416259Smckusick dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */ 30554604Smckusick if (newinofmt) 30654604Smckusick dirp->d_type = typemap[idesc->id_parent]; 307*55074Smckusick else 308*55074Smckusick dirp->d_type = 0; 30916259Smckusick dirp->d_reclen = newent.d_reclen; 31040022Smckusick dirp->d_namlen = newent.d_namlen; 31144934Smckusick bcopy(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1); 31216259Smckusick return (ALTERED|STOP); 31316259Smckusick } 31416259Smckusick 31517957Smckusick chgino(idesc) 31616259Smckusick struct inodesc *idesc; 31716259Smckusick { 31839973Smckusick register struct direct *dirp = idesc->id_dirp; 31916259Smckusick 32039973Smckusick if (bcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1)) 32117957Smckusick return (KEEPON); 32239973Smckusick dirp->d_ino = idesc->id_parent; 32354604Smckusick if (newinofmt) 32454604Smckusick dirp->d_type = typemap[idesc->id_parent]; 325*55074Smckusick else 326*55074Smckusick dirp->d_type = 0; 32717957Smckusick return (ALTERED|STOP); 32816259Smckusick } 32916259Smckusick 33039973Smckusick linkup(orphan, parentdir) 33116259Smckusick ino_t orphan; 33239973Smckusick ino_t parentdir; 33316259Smckusick { 33439973Smckusick register struct dinode *dp; 33540022Smckusick int lostdir; 33617954Smckusick ino_t oldlfdir; 33716259Smckusick struct inodesc idesc; 33817954Smckusick char tempname[BUFSIZ]; 33917957Smckusick extern int pass4check(); 34016259Smckusick 34116259Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 34217943Smckusick dp = ginode(orphan); 34339973Smckusick lostdir = (dp->di_mode & IFMT) == IFDIR; 34416259Smckusick pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); 34516259Smckusick pinode(orphan); 34616259Smckusick if (preen && dp->di_size == 0) 34716259Smckusick return (0); 34816259Smckusick if (preen) 34916259Smckusick printf(" (RECONNECTED)\n"); 35016259Smckusick else 35116259Smckusick if (reply("RECONNECT") == 0) 35216259Smckusick return (0); 35316259Smckusick if (lfdir == 0) { 35417943Smckusick dp = ginode(ROOTINO); 35517930Smckusick idesc.id_name = lfname; 35616259Smckusick idesc.id_type = DATA; 35716259Smckusick idesc.id_func = findino; 35816259Smckusick idesc.id_number = ROOTINO; 35930354Smckusick if ((ckinode(dp, &idesc) & FOUND) != 0) { 36017954Smckusick lfdir = idesc.id_parent; 36117954Smckusick } else { 36217954Smckusick pwarn("NO lost+found DIRECTORY"); 36317954Smckusick if (preen || reply("CREATE")) { 36439973Smckusick lfdir = allocdir(ROOTINO, (ino_t)0, lfmode); 36517992Smckusick if (lfdir != 0) { 36617992Smckusick if (makeentry(ROOTINO, lfdir, lfname) != 0) { 36717954Smckusick if (preen) 36817954Smckusick printf(" (CREATED)\n"); 36917954Smckusick } else { 37017992Smckusick freedir(lfdir, ROOTINO); 37117992Smckusick lfdir = 0; 37217954Smckusick if (preen) 37317954Smckusick printf("\n"); 37417954Smckusick } 37517954Smckusick } 37617954Smckusick } 37717954Smckusick } 37817943Smckusick if (lfdir == 0) { 37917954Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); 38016259Smckusick printf("\n\n"); 38116259Smckusick return (0); 38216259Smckusick } 38316259Smckusick } 38417943Smckusick dp = ginode(lfdir); 38539973Smckusick if ((dp->di_mode & IFMT) != IFDIR) { 38617957Smckusick pfatal("lost+found IS NOT A DIRECTORY"); 38717957Smckusick if (reply("REALLOCATE") == 0) 38817957Smckusick return (0); 38917957Smckusick oldlfdir = lfdir; 39039973Smckusick if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) { 39117957Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 39217957Smckusick return (0); 39317957Smckusick } 39440022Smckusick if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) { 39517957Smckusick pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 39617957Smckusick return (0); 39717957Smckusick } 39817957Smckusick inodirty(); 39917957Smckusick idesc.id_type = ADDR; 40017957Smckusick idesc.id_func = pass4check; 40117957Smckusick idesc.id_number = oldlfdir; 40217957Smckusick adjust(&idesc, lncntp[oldlfdir] + 1); 40317957Smckusick lncntp[oldlfdir] = 0; 40417957Smckusick dp = ginode(lfdir); 40517957Smckusick } 40617957Smckusick if (statemap[lfdir] != DFOUND) { 40717957Smckusick pfatal("SORRY. NO lost+found DIRECTORY\n\n"); 40816259Smckusick return (0); 40916259Smckusick } 41040022Smckusick (void)lftempname(tempname, orphan); 41117992Smckusick if (makeentry(lfdir, orphan, tempname) == 0) { 41216259Smckusick pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); 41316259Smckusick printf("\n\n"); 41416259Smckusick return (0); 41516259Smckusick } 41616259Smckusick lncntp[orphan]--; 41716259Smckusick if (lostdir) { 41840022Smckusick if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 && 41940022Smckusick parentdir != (ino_t)-1) 42044934Smckusick (void)makeentry(orphan, lfdir, ".."); 42117943Smckusick dp = ginode(lfdir); 42217943Smckusick dp->di_nlink++; 42317943Smckusick inodirty(); 42417943Smckusick lncntp[lfdir]++; 42544934Smckusick pwarn("DIR I=%lu CONNECTED. ", orphan); 42640022Smckusick if (parentdir != (ino_t)-1) 42744934Smckusick printf("PARENT WAS I=%lu\n", parentdir); 42816259Smckusick if (preen == 0) 42916259Smckusick printf("\n"); 43016259Smckusick } 43116259Smckusick return (1); 43216259Smckusick } 43316259Smckusick 43416259Smckusick /* 43540022Smckusick * fix an entry in a directory. 43640022Smckusick */ 43740022Smckusick changeino(dir, name, newnum) 43840022Smckusick ino_t dir; 43940022Smckusick char *name; 44040022Smckusick ino_t newnum; 44140022Smckusick { 44240022Smckusick struct inodesc idesc; 44340022Smckusick 44440022Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 44540022Smckusick idesc.id_type = DATA; 44640022Smckusick idesc.id_func = chgino; 44740022Smckusick idesc.id_number = dir; 44840022Smckusick idesc.id_fix = DONTKNOW; 44940022Smckusick idesc.id_name = name; 45040022Smckusick idesc.id_parent = newnum; /* new value for name */ 45140022Smckusick return (ckinode(ginode(dir), &idesc)); 45240022Smckusick } 45340022Smckusick 45440022Smckusick /* 45517944Smckusick * make an entry in a directory 45617944Smckusick */ 45717992Smckusick makeentry(parent, ino, name) 45817992Smckusick ino_t parent, ino; 45917992Smckusick char *name; 46017992Smckusick { 46139973Smckusick struct dinode *dp; 46217992Smckusick struct inodesc idesc; 46340022Smckusick char pathbuf[MAXPATHLEN + 1]; 46417944Smckusick 46539973Smckusick if (parent < ROOTINO || parent >= maxino || 46639973Smckusick ino < ROOTINO || ino >= maxino) 46717992Smckusick return (0); 46839973Smckusick bzero((char *)&idesc, sizeof(struct inodesc)); 46917992Smckusick idesc.id_type = DATA; 47017992Smckusick idesc.id_func = mkentry; 47117992Smckusick idesc.id_number = parent; 47217992Smckusick idesc.id_parent = ino; /* this is the inode to enter */ 47317992Smckusick idesc.id_fix = DONTKNOW; 47417992Smckusick idesc.id_name = name; 47517992Smckusick dp = ginode(parent); 47617993Smckusick if (dp->di_size % DIRBLKSIZ) { 47717993Smckusick dp->di_size = roundup(dp->di_size, DIRBLKSIZ); 47817993Smckusick inodirty(); 47917993Smckusick } 48017992Smckusick if ((ckinode(dp, &idesc) & ALTERED) != 0) 48117944Smckusick return (1); 48240022Smckusick getpathname(pathbuf, parent, parent); 48340022Smckusick dp = ginode(parent); 48440022Smckusick if (expanddir(dp, pathbuf) == 0) 48517944Smckusick return (0); 48617992Smckusick return (ckinode(dp, &idesc) & ALTERED); 48717944Smckusick } 48817944Smckusick 48917944Smckusick /* 49017944Smckusick * Attempt to expand the size of a directory 49117944Smckusick */ 49240022Smckusick expanddir(dp, name) 49339973Smckusick register struct dinode *dp; 49440022Smckusick char *name; 49517944Smckusick { 49617944Smckusick daddr_t lastbn, newblk; 49739973Smckusick register struct bufarea *bp; 49817944Smckusick char *cp, firstblk[DIRBLKSIZ]; 49917944Smckusick 50017944Smckusick lastbn = lblkno(&sblock, dp->di_size); 50141133Smckusick if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0) 50217944Smckusick return (0); 50317944Smckusick if ((newblk = allocblk(sblock.fs_frag)) == 0) 50417944Smckusick return (0); 50517944Smckusick dp->di_db[lastbn + 1] = dp->di_db[lastbn]; 50617944Smckusick dp->di_db[lastbn] = newblk; 50717944Smckusick dp->di_size += sblock.fs_bsize; 50817944Smckusick dp->di_blocks += btodb(sblock.fs_bsize); 50934225Smckusick bp = getdirblk(dp->di_db[lastbn + 1], 51044934Smckusick (long)dblksize(&sblock, dp, lastbn + 1)); 51138375Smckusick if (bp->b_errs) 51217944Smckusick goto bad; 51334225Smckusick bcopy(bp->b_un.b_buf, firstblk, DIRBLKSIZ); 51434225Smckusick bp = getdirblk(newblk, sblock.fs_bsize); 51538375Smckusick if (bp->b_errs) 51617944Smckusick goto bad; 51734225Smckusick bcopy(firstblk, bp->b_un.b_buf, DIRBLKSIZ); 51834225Smckusick for (cp = &bp->b_un.b_buf[DIRBLKSIZ]; 51934225Smckusick cp < &bp->b_un.b_buf[sblock.fs_bsize]; 52017944Smckusick cp += DIRBLKSIZ) 52117944Smckusick bcopy((char *)&emptydir, cp, sizeof emptydir); 52234225Smckusick dirty(bp); 52334225Smckusick bp = getdirblk(dp->di_db[lastbn + 1], 52444934Smckusick (long)dblksize(&sblock, dp, lastbn + 1)); 52538375Smckusick if (bp->b_errs) 52617944Smckusick goto bad; 52734225Smckusick bcopy((char *)&emptydir, bp->b_un.b_buf, sizeof emptydir); 52840022Smckusick pwarn("NO SPACE LEFT IN %s", name); 52917944Smckusick if (preen) 53017944Smckusick printf(" (EXPANDED)\n"); 53117944Smckusick else if (reply("EXPAND") == 0) 53217944Smckusick goto bad; 53334225Smckusick dirty(bp); 53417944Smckusick inodirty(); 53517944Smckusick return (1); 53617944Smckusick bad: 53717944Smckusick dp->di_db[lastbn] = dp->di_db[lastbn + 1]; 53817944Smckusick dp->di_db[lastbn + 1] = 0; 53917944Smckusick dp->di_size -= sblock.fs_bsize; 54017944Smckusick dp->di_blocks -= btodb(sblock.fs_bsize); 54117944Smckusick freeblk(newblk, sblock.fs_frag); 54217944Smckusick return (0); 54317944Smckusick } 54417944Smckusick 54517944Smckusick /* 54617954Smckusick * allocate a new directory 54717954Smckusick */ 54836827Smckusick allocdir(parent, request, mode) 54917954Smckusick ino_t parent, request; 55036827Smckusick int mode; 55117954Smckusick { 55217954Smckusick ino_t ino; 55317954Smckusick char *cp; 55439973Smckusick struct dinode *dp; 55539973Smckusick register struct bufarea *bp; 55654604Smckusick struct dirtemplate *dirp; 55717954Smckusick 55836827Smckusick ino = allocino(request, IFDIR|mode); 55954604Smckusick if (newinofmt) 56054604Smckusick dirp = &dirhead; 56154604Smckusick else 56254604Smckusick dirp = (struct dirtemplate *)&odirhead; 56354604Smckusick dirp->dot_ino = ino; 56454604Smckusick dirp->dotdot_ino = parent; 56517954Smckusick dp = ginode(ino); 56634225Smckusick bp = getdirblk(dp->di_db[0], sblock.fs_fsize); 56738375Smckusick if (bp->b_errs) { 56817954Smckusick freeino(ino); 56917954Smckusick return (0); 57017954Smckusick } 57154604Smckusick bcopy((char *)dirp, bp->b_un.b_buf, sizeof(struct dirtemplate)); 57234225Smckusick for (cp = &bp->b_un.b_buf[DIRBLKSIZ]; 57334225Smckusick cp < &bp->b_un.b_buf[sblock.fs_fsize]; 57417954Smckusick cp += DIRBLKSIZ) 57517954Smckusick bcopy((char *)&emptydir, cp, sizeof emptydir); 57634225Smckusick dirty(bp); 57717954Smckusick dp->di_nlink = 2; 57817954Smckusick inodirty(); 57917954Smckusick if (ino == ROOTINO) { 58017954Smckusick lncntp[ino] = dp->di_nlink; 58150573Smckusick cacheino(dp, ino); 58217954Smckusick return(ino); 58317954Smckusick } 58417954Smckusick if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) { 58517954Smckusick freeino(ino); 58617954Smckusick return (0); 58717954Smckusick } 58850573Smckusick cacheino(dp, ino); 58917954Smckusick statemap[ino] = statemap[parent]; 59017954Smckusick if (statemap[ino] == DSTATE) { 59117954Smckusick lncntp[ino] = dp->di_nlink; 59217954Smckusick lncntp[parent]++; 59317954Smckusick } 59417954Smckusick dp = ginode(parent); 59517954Smckusick dp->di_nlink++; 59617954Smckusick inodirty(); 59717954Smckusick return (ino); 59817954Smckusick } 59917954Smckusick 60017954Smckusick /* 60117954Smckusick * free a directory inode 60217954Smckusick */ 60317954Smckusick freedir(ino, parent) 60417954Smckusick ino_t ino, parent; 60517954Smckusick { 60639973Smckusick struct dinode *dp; 60717954Smckusick 60817954Smckusick if (ino != parent) { 60917954Smckusick dp = ginode(parent); 61017954Smckusick dp->di_nlink--; 61117954Smckusick inodirty(); 61217954Smckusick } 61317954Smckusick freeino(ino); 61417954Smckusick } 61517954Smckusick 61617954Smckusick /* 61716259Smckusick * generate a temporary name for the lost+found directory. 61816259Smckusick */ 61916259Smckusick lftempname(bufp, ino) 62016259Smckusick char *bufp; 62116259Smckusick ino_t ino; 62216259Smckusick { 62316259Smckusick register ino_t in; 62416259Smckusick register char *cp; 62516259Smckusick int namlen; 62616259Smckusick 62716259Smckusick cp = bufp + 2; 62839973Smckusick for (in = maxino; in > 0; in /= 10) 62916259Smckusick cp++; 63016259Smckusick *--cp = 0; 63116259Smckusick namlen = cp - bufp; 63216259Smckusick in = ino; 63316259Smckusick while (cp > bufp) { 63416259Smckusick *--cp = (in % 10) + '0'; 63516259Smckusick in /= 10; 63616259Smckusick } 63716259Smckusick *cp = '#'; 63816259Smckusick return (namlen); 63916259Smckusick } 64034225Smckusick 64134225Smckusick /* 64234225Smckusick * Get a directory block. 64334225Smckusick * Insure that it is held until another is requested. 64434225Smckusick */ 64539973Smckusick struct bufarea * 64634225Smckusick getdirblk(blkno, size) 64734225Smckusick daddr_t blkno; 64834225Smckusick long size; 64934225Smckusick { 65034225Smckusick 65140648Smckusick if (pdirbp != 0) 65240648Smckusick pdirbp->b_flags &= ~B_INUSE; 65340648Smckusick pdirbp = getdatablk(blkno, size); 65440648Smckusick return (pdirbp); 65534225Smckusick } 656