dir.c revision 96483
11558Srgrimes/* 21558Srgrimes * Copyright (c) 1980, 1986, 1993 31558Srgrimes * The Regents of the University of California. All rights reserved. 41558Srgrimes * 51558Srgrimes * Redistribution and use in source and binary forms, with or without 61558Srgrimes * modification, are permitted provided that the following conditions 71558Srgrimes * are met: 81558Srgrimes * 1. Redistributions of source code must retain the above copyright 91558Srgrimes * notice, this list of conditions and the following disclaimer. 101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111558Srgrimes * notice, this list of conditions and the following disclaimer in the 121558Srgrimes * documentation and/or other materials provided with the distribution. 131558Srgrimes * 3. All advertising materials mentioning features or use of this software 141558Srgrimes * must display the following acknowledgement: 151558Srgrimes * This product includes software developed by the University of 161558Srgrimes * California, Berkeley and its contributors. 171558Srgrimes * 4. Neither the name of the University nor the names of its contributors 181558Srgrimes * may be used to endorse or promote products derived from this software 191558Srgrimes * without specific prior written permission. 201558Srgrimes * 211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241558Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311558Srgrimes * SUCH DAMAGE. 321558Srgrimes */ 331558Srgrimes 341558Srgrimes#ifndef lint 3541477Sjulian#if 0 3623675Speterstatic const char sccsid[] = "@(#)dir.c 8.8 (Berkeley) 4/28/95"; 3741477Sjulian#endif 3841477Sjulianstatic const char rcsid[] = 3950476Speter "$FreeBSD: head/sbin/fsck_ffs/dir.c 96483 2002-05-12 23:44:15Z phk $"; 401558Srgrimes#endif /* not lint */ 411558Srgrimes 421558Srgrimes#include <sys/param.h> 4341474Sjulian#include <sys/time.h> 4474556Smckusick#include <sys/sysctl.h> 4523675Speter 461558Srgrimes#include <ufs/ufs/dinode.h> 471558Srgrimes#include <ufs/ufs/dir.h> 481558Srgrimes#include <ufs/ffs/fs.h> 4923796Sbde 5023675Speter#include <err.h> 511558Srgrimes#include <string.h> 5223675Speter 531558Srgrimes#include "fsck.h" 541558Srgrimes 551558Srgrimeschar *lfname = "lost+found"; 561558Srgrimesint lfmode = 01777; 5774556Smckusickstruct dirtemplate emptydir = { 5874556Smckusick 0, DIRBLKSIZ, DT_UNKNOWN, 0, "", 5974556Smckusick 0, 0, DT_UNKNOWN, 0, "" 6074556Smckusick}; 611558Srgrimesstruct dirtemplate dirhead = { 621558Srgrimes 0, 12, DT_DIR, 1, ".", 631558Srgrimes 0, DIRBLKSIZ - 12, DT_DIR, 2, ".." 641558Srgrimes}; 651558Srgrimesstruct odirtemplate odirhead = { 661558Srgrimes 0, 12, 1, ".", 671558Srgrimes 0, DIRBLKSIZ - 12, 2, ".." 681558Srgrimes}; 691558Srgrimes 7092839Simpstatic int chgino(struct inodesc *); 7192839Simpstatic int dircheck(struct inodesc *, struct direct *); 7292839Simpstatic int expanddir(struct dinode *dp, char *name); 7392839Simpstatic void freedir(ino_t ino, ino_t parent); 7492839Simpstatic struct direct *fsck_readdir(struct inodesc *); 7592839Simpstatic struct bufarea *getdirblk(ufs_daddr_t blkno, long size); 7692839Simpstatic int lftempname(char *bufp, ino_t ino); 7792839Simpstatic int mkentry(struct inodesc *); 781558Srgrimes 791558Srgrimes/* 801558Srgrimes * Propagate connected state through the tree. 811558Srgrimes */ 827585Sbdevoid 8392839Simppropagate(void) 841558Srgrimes{ 8592806Sobrien struct inoinfo **inpp, *inp; 861558Srgrimes struct inoinfo **inpend; 871558Srgrimes long change; 881558Srgrimes 891558Srgrimes inpend = &inpsort[inplast]; 901558Srgrimes do { 911558Srgrimes change = 0; 921558Srgrimes for (inpp = inpsort; inpp < inpend; inpp++) { 931558Srgrimes inp = *inpp; 941558Srgrimes if (inp->i_parent == 0) 951558Srgrimes continue; 9641474Sjulian if (inoinfo(inp->i_parent)->ino_state == DFOUND && 9741474Sjulian inoinfo(inp->i_number)->ino_state == DSTATE) { 9841474Sjulian inoinfo(inp->i_number)->ino_state = DFOUND; 991558Srgrimes change++; 1001558Srgrimes } 1011558Srgrimes } 1021558Srgrimes } while (change > 0); 1031558Srgrimes} 1041558Srgrimes 1051558Srgrimes/* 1061558Srgrimes * Scan each entry in a directory block. 1071558Srgrimes */ 1087585Sbdeint 10992839Simpdirscan(struct inodesc *idesc) 1101558Srgrimes{ 11192806Sobrien struct direct *dp; 11292806Sobrien struct bufarea *bp; 11374556Smckusick u_int dsize, n; 1141558Srgrimes long blksiz; 1151558Srgrimes char dbuf[DIRBLKSIZ]; 1161558Srgrimes 1171558Srgrimes if (idesc->id_type != DATA) 11823675Speter errx(EEXIT, "wrong type to dirscan %d", idesc->id_type); 1191558Srgrimes if (idesc->id_entryno == 0 && 1201558Srgrimes (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0) 1211558Srgrimes idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ); 1221558Srgrimes blksiz = idesc->id_numfrags * sblock.fs_fsize; 1231558Srgrimes if (chkrange(idesc->id_blkno, idesc->id_numfrags)) { 1241558Srgrimes idesc->id_filesize -= blksiz; 1251558Srgrimes return (SKIP); 1261558Srgrimes } 1271558Srgrimes idesc->id_loc = 0; 1281558Srgrimes for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) { 1291558Srgrimes dsize = dp->d_reclen; 13041474Sjulian if (dsize > sizeof(dbuf)) 13141474Sjulian dsize = sizeof(dbuf); 13223675Speter memmove(dbuf, dp, (size_t)dsize); 1331558Srgrimes idesc->id_dirp = (struct direct *)dbuf; 1341558Srgrimes if ((n = (*idesc->id_func)(idesc)) & ALTERED) { 1351558Srgrimes bp = getdirblk(idesc->id_blkno, blksiz); 13623675Speter memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf, 1371558Srgrimes (size_t)dsize); 1381558Srgrimes dirty(bp); 1391558Srgrimes sbdirty(); 1401558Srgrimes } 1418871Srgrimes if (n & STOP) 1421558Srgrimes return (n); 1431558Srgrimes } 1441558Srgrimes return (idesc->id_filesize > 0 ? KEEPON : STOP); 1451558Srgrimes} 1461558Srgrimes 1471558Srgrimes/* 1481558Srgrimes * get next entry in a directory. 1491558Srgrimes */ 15023675Speterstatic struct direct * 15192839Simpfsck_readdir(struct inodesc *idesc) 1521558Srgrimes{ 15392806Sobrien struct direct *dp, *ndp; 15492806Sobrien struct bufarea *bp; 1551558Srgrimes long size, blksiz, fix, dploc; 1561558Srgrimes 1571558Srgrimes blksiz = idesc->id_numfrags * sblock.fs_fsize; 1581558Srgrimes bp = getdirblk(idesc->id_blkno, blksiz); 1591558Srgrimes if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 && 1601558Srgrimes idesc->id_loc < blksiz) { 1611558Srgrimes dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 1621558Srgrimes if (dircheck(idesc, dp)) 1631558Srgrimes goto dpok; 16423675Speter if (idesc->id_fix == IGNORE) 16523675Speter return (0); 1661558Srgrimes fix = dofix(idesc, "DIRECTORY CORRUPTED"); 1671558Srgrimes bp = getdirblk(idesc->id_blkno, blksiz); 1681558Srgrimes dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 1691558Srgrimes dp->d_reclen = DIRBLKSIZ; 1701558Srgrimes dp->d_ino = 0; 1711558Srgrimes dp->d_type = 0; 1721558Srgrimes dp->d_namlen = 0; 1731558Srgrimes dp->d_name[0] = '\0'; 1741558Srgrimes if (fix) 1751558Srgrimes dirty(bp); 1761558Srgrimes idesc->id_loc += DIRBLKSIZ; 1771558Srgrimes idesc->id_filesize -= DIRBLKSIZ; 1781558Srgrimes return (dp); 1791558Srgrimes } 1801558Srgrimesdpok: 1811558Srgrimes if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) 1821558Srgrimes return NULL; 1831558Srgrimes dploc = idesc->id_loc; 1841558Srgrimes dp = (struct direct *)(bp->b_un.b_buf + dploc); 1851558Srgrimes idesc->id_loc += dp->d_reclen; 1861558Srgrimes idesc->id_filesize -= dp->d_reclen; 1871558Srgrimes if ((idesc->id_loc % DIRBLKSIZ) == 0) 1881558Srgrimes return (dp); 1891558Srgrimes ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc); 1901558Srgrimes if (idesc->id_loc < blksiz && idesc->id_filesize > 0 && 1911558Srgrimes dircheck(idesc, ndp) == 0) { 1921558Srgrimes size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 1931558Srgrimes idesc->id_loc += size; 1941558Srgrimes idesc->id_filesize -= size; 19523675Speter if (idesc->id_fix == IGNORE) 19623675Speter return (0); 1971558Srgrimes fix = dofix(idesc, "DIRECTORY CORRUPTED"); 1981558Srgrimes bp = getdirblk(idesc->id_blkno, blksiz); 1991558Srgrimes dp = (struct direct *)(bp->b_un.b_buf + dploc); 2001558Srgrimes dp->d_reclen += size; 2011558Srgrimes if (fix) 2021558Srgrimes dirty(bp); 2031558Srgrimes } 2041558Srgrimes return (dp); 2051558Srgrimes} 2061558Srgrimes 2071558Srgrimes/* 2081558Srgrimes * Verify that a directory entry is valid. 2091558Srgrimes * This is a superset of the checks made in the kernel. 2101558Srgrimes */ 21123675Speterstatic int 21292839Simpdircheck(struct inodesc *idesc, struct direct *dp) 2131558Srgrimes{ 21492806Sobrien int size; 21592806Sobrien char *cp; 2161558Srgrimes u_char namlen, type; 2171558Srgrimes int spaceleft; 2181558Srgrimes 21923675Speter spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ); 22041474Sjulian if (dp->d_reclen == 0 || 22123675Speter dp->d_reclen > spaceleft || 22223675Speter (dp->d_reclen & 0x3) != 0) 22362668Smckusick goto bad; 22423675Speter if (dp->d_ino == 0) 22523675Speter return (1); 22696483Sphk size = DIRSIZ(0, dp); 22796483Sphk namlen = dp->d_namlen; 22896483Sphk type = dp->d_type; 22923675Speter if (dp->d_reclen < size || 23023675Speter idesc->id_filesize < size || 23123675Speter namlen > MAXNAMLEN || 23223675Speter type > 15) 23362668Smckusick goto bad; 23423675Speter for (cp = dp->d_name, size = 0; size < namlen; size++) 23523675Speter if (*cp == '\0' || (*cp++ == '/')) 23662668Smckusick goto bad; 23723675Speter if (*cp != '\0') 23862668Smckusick goto bad; 23923675Speter return (1); 24062668Smckusickbad: 24162668Smckusick if (debug) 24262668Smckusick printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n", 24362668Smckusick dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type, 24462668Smckusick dp->d_name); 24562668Smckusick return (0); 2461558Srgrimes} 2471558Srgrimes 2487585Sbdevoid 24992839Simpdirerror(ino_t ino, char *errmesg) 2501558Srgrimes{ 2511558Srgrimes 2521558Srgrimes fileerror(ino, ino, errmesg); 2531558Srgrimes} 2541558Srgrimes 2557585Sbdevoid 25692839Simpfileerror(ino_t cwd, ino_t ino, char *errmesg) 2571558Srgrimes{ 25892806Sobrien struct dinode *dp; 2591558Srgrimes char pathbuf[MAXPATHLEN + 1]; 2601558Srgrimes 2611558Srgrimes pwarn("%s ", errmesg); 2621558Srgrimes pinode(ino); 2631558Srgrimes printf("\n"); 2641558Srgrimes getpathname(pathbuf, cwd, ino); 2651558Srgrimes if (ino < ROOTINO || ino > maxino) { 2661558Srgrimes pfatal("NAME=%s\n", pathbuf); 2671558Srgrimes return; 2681558Srgrimes } 2691558Srgrimes dp = ginode(ino); 2701558Srgrimes if (ftypeok(dp)) 2711558Srgrimes pfatal("%s=%s\n", 2721558Srgrimes (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf); 2731558Srgrimes else 2741558Srgrimes pfatal("NAME=%s\n", pathbuf); 2751558Srgrimes} 2761558Srgrimes 2777585Sbdevoid 27892839Simpadjust(struct inodesc *idesc, int lcnt) 2791558Srgrimes{ 28041474Sjulian struct dinode *dp; 28141474Sjulian int saveresolved; 2821558Srgrimes 2831558Srgrimes dp = ginode(idesc->id_number); 2841558Srgrimes if (dp->di_nlink == lcnt) { 28541474Sjulian /* 28641474Sjulian * If we have not hit any unresolved problems, are running 28741474Sjulian * in preen mode, and are on a filesystem using soft updates, 28841474Sjulian * then just toss any partially allocated files. 28941474Sjulian */ 29074556Smckusick if (resolved && (preen || bkgrdflag) && usedsoftdep) { 29141474Sjulian clri(idesc, "UNREF", 1); 29241474Sjulian return; 29341474Sjulian } else { 29441474Sjulian /* 29541474Sjulian * The filesystem can be marked clean even if 29641474Sjulian * a file is not linked up, but is cleared. 29741474Sjulian * Hence, resolved should not be cleared when 29841474Sjulian * linkup is answered no, but clri is answered yes. 29941474Sjulian */ 30041474Sjulian saveresolved = resolved; 30141474Sjulian if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) { 30241474Sjulian resolved = saveresolved; 30341474Sjulian clri(idesc, "UNREF", 0); 30441474Sjulian return; 30541474Sjulian } 30641474Sjulian /* 30741474Sjulian * Account for the new reference created by linkup(). 30841474Sjulian */ 30941474Sjulian dp = ginode(idesc->id_number); 31041474Sjulian lcnt--; 31141474Sjulian } 31241474Sjulian } 31341474Sjulian if (lcnt != 0) { 3141558Srgrimes pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname : 3151558Srgrimes ((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE")); 3161558Srgrimes pinode(idesc->id_number); 3171558Srgrimes printf(" COUNT %d SHOULD BE %d", 3181558Srgrimes dp->di_nlink, dp->di_nlink - lcnt); 31934266Sjulian if (preen || usedsoftdep) { 3201558Srgrimes if (lcnt < 0) { 3211558Srgrimes printf("\n"); 3221558Srgrimes pfatal("LINK COUNT INCREASING"); 3231558Srgrimes } 32434266Sjulian if (preen) 32534266Sjulian printf(" (ADJUSTED)\n"); 3261558Srgrimes } 3271558Srgrimes if (preen || reply("ADJUST") == 1) { 32874556Smckusick if (bkgrdflag == 0) { 32974556Smckusick dp->di_nlink -= lcnt; 33074556Smckusick inodirty(); 33174556Smckusick } else { 33274556Smckusick cmd.value = idesc->id_number; 33374556Smckusick cmd.size = -lcnt; 33474556Smckusick if (debug) 33586514Siedowse printf("adjrefcnt ino %ld amt %ld\n", 33674556Smckusick (long)cmd.value, cmd.size); 33774556Smckusick if (sysctl(adjrefcnt, MIBSIZE, 0, 0, 33874556Smckusick &cmd, sizeof cmd) == -1) 33974556Smckusick rwerror("ADJUST INODE", cmd.value); 34074556Smckusick } 3411558Srgrimes } 3421558Srgrimes } 3431558Srgrimes} 3441558Srgrimes 34523675Speterstatic int 34692839Simpmkentry(struct inodesc *idesc) 3471558Srgrimes{ 34892806Sobrien struct direct *dirp = idesc->id_dirp; 3491558Srgrimes struct direct newent; 3501558Srgrimes int newlen, oldlen; 3511558Srgrimes 3521558Srgrimes newent.d_namlen = strlen(idesc->id_name); 3531558Srgrimes newlen = DIRSIZ(0, &newent); 3541558Srgrimes if (dirp->d_ino != 0) 3551558Srgrimes oldlen = DIRSIZ(0, dirp); 3561558Srgrimes else 3571558Srgrimes oldlen = 0; 3581558Srgrimes if (dirp->d_reclen - oldlen < newlen) 3591558Srgrimes return (KEEPON); 3601558Srgrimes newent.d_reclen = dirp->d_reclen - oldlen; 3611558Srgrimes dirp->d_reclen = oldlen; 3621558Srgrimes dirp = (struct direct *)(((char *)dirp) + oldlen); 3631558Srgrimes dirp->d_ino = idesc->id_parent; /* ino to be entered is in id_parent */ 36423675Speter dirp->d_reclen = newent.d_reclen; 36596483Sphk dirp->d_type = inoinfo(idesc->id_parent)->ino_type; 36623675Speter dirp->d_namlen = newent.d_namlen; 36723675Speter memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1); 3681558Srgrimes return (ALTERED|STOP); 3691558Srgrimes} 3701558Srgrimes 37123675Speterstatic int 37292839Simpchgino(struct inodesc *idesc) 3731558Srgrimes{ 37492806Sobrien struct direct *dirp = idesc->id_dirp; 3751558Srgrimes 37623675Speter if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1)) 3771558Srgrimes return (KEEPON); 3781558Srgrimes dirp->d_ino = idesc->id_parent; 37996483Sphk dirp->d_type = inoinfo(idesc->id_parent)->ino_type; 3801558Srgrimes return (ALTERED|STOP); 3811558Srgrimes} 3821558Srgrimes 3837585Sbdeint 38492839Simplinkup(ino_t orphan, ino_t parentdir, char *name) 3851558Srgrimes{ 38692806Sobrien struct dinode *dp; 3871558Srgrimes int lostdir; 3881558Srgrimes ino_t oldlfdir; 3891558Srgrimes struct inodesc idesc; 3901558Srgrimes char tempname[BUFSIZ]; 3911558Srgrimes 39223675Speter memset(&idesc, 0, sizeof(struct inodesc)); 3931558Srgrimes dp = ginode(orphan); 3941558Srgrimes lostdir = (dp->di_mode & IFMT) == IFDIR; 3951558Srgrimes pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); 3961558Srgrimes pinode(orphan); 39741474Sjulian if (preen && dp->di_size == 0) 3981558Srgrimes return (0); 39974556Smckusick if (cursnapshot != 0) { 40074556Smckusick pfatal("FILE LINKUP IN SNAPSHOT"); 40174556Smckusick return (0); 40274556Smckusick } 4031558Srgrimes if (preen) 4041558Srgrimes printf(" (RECONNECTED)\n"); 4051558Srgrimes else 4061558Srgrimes if (reply("RECONNECT") == 0) 4071558Srgrimes return (0); 4081558Srgrimes if (lfdir == 0) { 4091558Srgrimes dp = ginode(ROOTINO); 4101558Srgrimes idesc.id_name = lfname; 4111558Srgrimes idesc.id_type = DATA; 4121558Srgrimes idesc.id_func = findino; 4131558Srgrimes idesc.id_number = ROOTINO; 4141558Srgrimes if ((ckinode(dp, &idesc) & FOUND) != 0) { 4151558Srgrimes lfdir = idesc.id_parent; 4161558Srgrimes } else { 4171558Srgrimes pwarn("NO lost+found DIRECTORY"); 4181558Srgrimes if (preen || reply("CREATE")) { 4191558Srgrimes lfdir = allocdir(ROOTINO, (ino_t)0, lfmode); 4201558Srgrimes if (lfdir != 0) { 4211558Srgrimes if (makeentry(ROOTINO, lfdir, lfname) != 0) { 42241474Sjulian numdirs++; 4231558Srgrimes if (preen) 4241558Srgrimes printf(" (CREATED)\n"); 4251558Srgrimes } else { 4261558Srgrimes freedir(lfdir, ROOTINO); 4271558Srgrimes lfdir = 0; 4281558Srgrimes if (preen) 4291558Srgrimes printf("\n"); 4301558Srgrimes } 4311558Srgrimes } 4321558Srgrimes } 4331558Srgrimes } 4341558Srgrimes if (lfdir == 0) { 4351558Srgrimes pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); 4361558Srgrimes printf("\n\n"); 4371558Srgrimes return (0); 4381558Srgrimes } 4391558Srgrimes } 4401558Srgrimes dp = ginode(lfdir); 4411558Srgrimes if ((dp->di_mode & IFMT) != IFDIR) { 4421558Srgrimes pfatal("lost+found IS NOT A DIRECTORY"); 4431558Srgrimes if (reply("REALLOCATE") == 0) 4441558Srgrimes return (0); 4451558Srgrimes oldlfdir = lfdir; 4461558Srgrimes if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) { 4471558Srgrimes pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 4481558Srgrimes return (0); 4491558Srgrimes } 4501558Srgrimes if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) { 4511558Srgrimes pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); 4521558Srgrimes return (0); 4531558Srgrimes } 4541558Srgrimes inodirty(); 4551558Srgrimes idesc.id_type = ADDR; 4561558Srgrimes idesc.id_func = pass4check; 4571558Srgrimes idesc.id_number = oldlfdir; 45841474Sjulian adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1); 45941474Sjulian inoinfo(oldlfdir)->ino_linkcnt = 0; 4601558Srgrimes dp = ginode(lfdir); 4611558Srgrimes } 46241474Sjulian if (inoinfo(lfdir)->ino_state != DFOUND) { 4631558Srgrimes pfatal("SORRY. NO lost+found DIRECTORY\n\n"); 4641558Srgrimes return (0); 4651558Srgrimes } 4661558Srgrimes (void)lftempname(tempname, orphan); 46741474Sjulian if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) { 4681558Srgrimes pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); 4691558Srgrimes printf("\n\n"); 4701558Srgrimes return (0); 4711558Srgrimes } 47241474Sjulian inoinfo(orphan)->ino_linkcnt--; 4731558Srgrimes if (lostdir) { 4741558Srgrimes if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 && 4751558Srgrimes parentdir != (ino_t)-1) 4761558Srgrimes (void)makeentry(orphan, lfdir, ".."); 4771558Srgrimes dp = ginode(lfdir); 4781558Srgrimes dp->di_nlink++; 4791558Srgrimes inodirty(); 48041474Sjulian inoinfo(lfdir)->ino_linkcnt++; 48186514Siedowse pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan); 48215699Snate if (parentdir != (ino_t)-1) { 48337236Sbde printf("PARENT WAS I=%lu\n", (u_long)parentdir); 48441477Sjulian /* 48541477Sjulian * The parent directory, because of the ordering 48641477Sjulian * guarantees, has had the link count incremented 48741477Sjulian * for the child, but no entry was made. This 48841477Sjulian * fixes the parent link count so that fsck does 48941477Sjulian * not need to be rerun. 49041477Sjulian */ 49141474Sjulian inoinfo(parentdir)->ino_linkcnt++; 49215699Snate } 4931558Srgrimes if (preen == 0) 4941558Srgrimes printf("\n"); 4951558Srgrimes } 4961558Srgrimes return (1); 4971558Srgrimes} 4981558Srgrimes 4991558Srgrimes/* 5001558Srgrimes * fix an entry in a directory. 5011558Srgrimes */ 5027585Sbdeint 50392839Simpchangeino(ino_t dir, char *name, ino_t newnum) 5041558Srgrimes{ 5051558Srgrimes struct inodesc idesc; 5061558Srgrimes 50723675Speter memset(&idesc, 0, sizeof(struct inodesc)); 5081558Srgrimes idesc.id_type = DATA; 5091558Srgrimes idesc.id_func = chgino; 5101558Srgrimes idesc.id_number = dir; 5111558Srgrimes idesc.id_fix = DONTKNOW; 5121558Srgrimes idesc.id_name = name; 5131558Srgrimes idesc.id_parent = newnum; /* new value for name */ 5141558Srgrimes return (ckinode(ginode(dir), &idesc)); 5151558Srgrimes} 5161558Srgrimes 5171558Srgrimes/* 5181558Srgrimes * make an entry in a directory 5191558Srgrimes */ 5207585Sbdeint 52192839Simpmakeentry(ino_t parent, ino_t ino, char *name) 5221558Srgrimes{ 5231558Srgrimes struct dinode *dp; 5241558Srgrimes struct inodesc idesc; 5251558Srgrimes char pathbuf[MAXPATHLEN + 1]; 5268871Srgrimes 5271558Srgrimes if (parent < ROOTINO || parent >= maxino || 5281558Srgrimes ino < ROOTINO || ino >= maxino) 5291558Srgrimes return (0); 53023675Speter memset(&idesc, 0, sizeof(struct inodesc)); 5311558Srgrimes idesc.id_type = DATA; 5321558Srgrimes idesc.id_func = mkentry; 5331558Srgrimes idesc.id_number = parent; 5341558Srgrimes idesc.id_parent = ino; /* this is the inode to enter */ 5351558Srgrimes idesc.id_fix = DONTKNOW; 5361558Srgrimes idesc.id_name = name; 5371558Srgrimes dp = ginode(parent); 5381558Srgrimes if (dp->di_size % DIRBLKSIZ) { 5391558Srgrimes dp->di_size = roundup(dp->di_size, DIRBLKSIZ); 5401558Srgrimes inodirty(); 5411558Srgrimes } 5421558Srgrimes if ((ckinode(dp, &idesc) & ALTERED) != 0) 5431558Srgrimes return (1); 5441558Srgrimes getpathname(pathbuf, parent, parent); 5451558Srgrimes dp = ginode(parent); 5461558Srgrimes if (expanddir(dp, pathbuf) == 0) 5471558Srgrimes return (0); 5481558Srgrimes return (ckinode(dp, &idesc) & ALTERED); 5491558Srgrimes} 5501558Srgrimes 5511558Srgrimes/* 5521558Srgrimes * Attempt to expand the size of a directory 5531558Srgrimes */ 55423675Speterstatic int 55592839Simpexpanddir(struct dinode *dp, char *name) 5561558Srgrimes{ 55723675Speter ufs_daddr_t lastbn, newblk; 55892806Sobrien struct bufarea *bp; 5591558Srgrimes char *cp, firstblk[DIRBLKSIZ]; 5601558Srgrimes 5611558Srgrimes lastbn = lblkno(&sblock, dp->di_size); 5621558Srgrimes if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0) 5631558Srgrimes return (0); 5641558Srgrimes if ((newblk = allocblk(sblock.fs_frag)) == 0) 5651558Srgrimes return (0); 5661558Srgrimes dp->di_db[lastbn + 1] = dp->di_db[lastbn]; 5671558Srgrimes dp->di_db[lastbn] = newblk; 5681558Srgrimes dp->di_size += sblock.fs_bsize; 5691558Srgrimes dp->di_blocks += btodb(sblock.fs_bsize); 5701558Srgrimes bp = getdirblk(dp->di_db[lastbn + 1], 5711558Srgrimes (long)dblksize(&sblock, dp, lastbn + 1)); 5721558Srgrimes if (bp->b_errs) 5731558Srgrimes goto bad; 57423675Speter memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ); 5751558Srgrimes bp = getdirblk(newblk, sblock.fs_bsize); 5761558Srgrimes if (bp->b_errs) 5771558Srgrimes goto bad; 57823675Speter memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ); 5791558Srgrimes for (cp = &bp->b_un.b_buf[DIRBLKSIZ]; 5801558Srgrimes cp < &bp->b_un.b_buf[sblock.fs_bsize]; 5811558Srgrimes cp += DIRBLKSIZ) 58223675Speter memmove(cp, &emptydir, sizeof emptydir); 5831558Srgrimes dirty(bp); 5841558Srgrimes bp = getdirblk(dp->di_db[lastbn + 1], 5851558Srgrimes (long)dblksize(&sblock, dp, lastbn + 1)); 5861558Srgrimes if (bp->b_errs) 5871558Srgrimes goto bad; 58823675Speter memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir); 5891558Srgrimes pwarn("NO SPACE LEFT IN %s", name); 5901558Srgrimes if (preen) 5911558Srgrimes printf(" (EXPANDED)\n"); 5921558Srgrimes else if (reply("EXPAND") == 0) 5931558Srgrimes goto bad; 5941558Srgrimes dirty(bp); 5951558Srgrimes inodirty(); 5961558Srgrimes return (1); 5971558Srgrimesbad: 5981558Srgrimes dp->di_db[lastbn] = dp->di_db[lastbn + 1]; 5991558Srgrimes dp->di_db[lastbn + 1] = 0; 6001558Srgrimes dp->di_size -= sblock.fs_bsize; 6011558Srgrimes dp->di_blocks -= btodb(sblock.fs_bsize); 6021558Srgrimes freeblk(newblk, sblock.fs_frag); 6031558Srgrimes return (0); 6041558Srgrimes} 6051558Srgrimes 6061558Srgrimes/* 6071558Srgrimes * allocate a new directory 6081558Srgrimes */ 6097586Sbdeino_t 61092839Simpallocdir(ino_t parent, ino_t request, int mode) 6111558Srgrimes{ 6121558Srgrimes ino_t ino; 6131558Srgrimes char *cp; 6141558Srgrimes struct dinode *dp; 61563810Smckusick struct bufarea *bp; 61663810Smckusick struct inoinfo *inp; 6171558Srgrimes struct dirtemplate *dirp; 6181558Srgrimes 6191558Srgrimes ino = allocino(request, IFDIR|mode); 62096483Sphk dirp = &dirhead; 6211558Srgrimes dirp->dot_ino = ino; 6221558Srgrimes dirp->dotdot_ino = parent; 6231558Srgrimes dp = ginode(ino); 6241558Srgrimes bp = getdirblk(dp->di_db[0], sblock.fs_fsize); 6251558Srgrimes if (bp->b_errs) { 6261558Srgrimes freeino(ino); 6271558Srgrimes return (0); 6281558Srgrimes } 62923675Speter memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate)); 6301558Srgrimes for (cp = &bp->b_un.b_buf[DIRBLKSIZ]; 6311558Srgrimes cp < &bp->b_un.b_buf[sblock.fs_fsize]; 6321558Srgrimes cp += DIRBLKSIZ) 63323675Speter memmove(cp, &emptydir, sizeof emptydir); 6341558Srgrimes dirty(bp); 6351558Srgrimes dp->di_nlink = 2; 6361558Srgrimes inodirty(); 6371558Srgrimes if (ino == ROOTINO) { 63841474Sjulian inoinfo(ino)->ino_linkcnt = dp->di_nlink; 6391558Srgrimes cacheino(dp, ino); 6401558Srgrimes return(ino); 6411558Srgrimes } 64241474Sjulian if (inoinfo(parent)->ino_state != DSTATE && 64341474Sjulian inoinfo(parent)->ino_state != DFOUND) { 6441558Srgrimes freeino(ino); 6451558Srgrimes return (0); 6461558Srgrimes } 6471558Srgrimes cacheino(dp, ino); 64863810Smckusick inp = getinoinfo(ino); 64963810Smckusick inp->i_parent = parent; 65063810Smckusick inp->i_dotdot = parent; 65141474Sjulian inoinfo(ino)->ino_state = inoinfo(parent)->ino_state; 65241474Sjulian if (inoinfo(ino)->ino_state == DSTATE) { 65341474Sjulian inoinfo(ino)->ino_linkcnt = dp->di_nlink; 65441474Sjulian inoinfo(parent)->ino_linkcnt++; 6551558Srgrimes } 6561558Srgrimes dp = ginode(parent); 6571558Srgrimes dp->di_nlink++; 6581558Srgrimes inodirty(); 6591558Srgrimes return (ino); 6601558Srgrimes} 6611558Srgrimes 6621558Srgrimes/* 6631558Srgrimes * free a directory inode 6641558Srgrimes */ 6657585Sbdestatic void 66692839Simpfreedir(ino_t ino, ino_t parent) 6671558Srgrimes{ 6681558Srgrimes struct dinode *dp; 6691558Srgrimes 6701558Srgrimes if (ino != parent) { 6711558Srgrimes dp = ginode(parent); 6721558Srgrimes dp->di_nlink--; 6731558Srgrimes inodirty(); 6741558Srgrimes } 6751558Srgrimes freeino(ino); 6761558Srgrimes} 6771558Srgrimes 6781558Srgrimes/* 6791558Srgrimes * generate a temporary name for the lost+found directory. 6801558Srgrimes */ 68123675Speterstatic int 68292839Simplftempname(char *bufp, ino_t ino) 6831558Srgrimes{ 68492806Sobrien ino_t in; 68592806Sobrien char *cp; 6861558Srgrimes int namlen; 6871558Srgrimes 6881558Srgrimes cp = bufp + 2; 6891558Srgrimes for (in = maxino; in > 0; in /= 10) 6901558Srgrimes cp++; 6911558Srgrimes *--cp = 0; 6921558Srgrimes namlen = cp - bufp; 6931558Srgrimes in = ino; 6941558Srgrimes while (cp > bufp) { 6951558Srgrimes *--cp = (in % 10) + '0'; 6961558Srgrimes in /= 10; 6971558Srgrimes } 6981558Srgrimes *cp = '#'; 6991558Srgrimes return (namlen); 7001558Srgrimes} 7011558Srgrimes 7021558Srgrimes/* 7031558Srgrimes * Get a directory block. 7041558Srgrimes * Insure that it is held until another is requested. 7051558Srgrimes */ 70623675Speterstatic struct bufarea * 70792839Simpgetdirblk(ufs_daddr_t blkno, long size) 7081558Srgrimes{ 7091558Srgrimes 7101558Srgrimes if (pdirbp != 0) 7111558Srgrimes pdirbp->b_flags &= ~B_INUSE; 7121558Srgrimes pdirbp = getdatablk(blkno, size); 7131558Srgrimes return (pdirbp); 7141558Srgrimes} 715