dir.c revision 98542
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 98542 2002-06-21 06:18:05Z mckusick $";
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 *);
7298542Smckusickstatic int expanddir(union dinode *dp, char *name);
7392839Simpstatic void freedir(ino_t ino, ino_t parent);
7492839Simpstatic struct direct *fsck_readdir(struct inodesc *);
7598542Smckusickstatic struct bufarea *getdirblk(ufs2_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{
25898542Smckusick	union 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",
27298542Smckusick		    (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
27398542Smckusick		    pathbuf);
2741558Srgrimes	else
2751558Srgrimes		pfatal("NAME=%s\n", pathbuf);
2761558Srgrimes}
2771558Srgrimes
2787585Sbdevoid
27992839Simpadjust(struct inodesc *idesc, int lcnt)
2801558Srgrimes{
28198542Smckusick	union dinode *dp;
28241474Sjulian	int saveresolved;
2831558Srgrimes
2841558Srgrimes	dp = ginode(idesc->id_number);
28598542Smckusick	if (DIP(dp, di_nlink) == lcnt) {
28641474Sjulian		/*
28741474Sjulian		 * If we have not hit any unresolved problems, are running
28841474Sjulian		 * in preen mode, and are on a filesystem using soft updates,
28941474Sjulian		 * then just toss any partially allocated files.
29041474Sjulian		 */
29174556Smckusick		if (resolved && (preen || bkgrdflag) && usedsoftdep) {
29241474Sjulian			clri(idesc, "UNREF", 1);
29341474Sjulian			return;
29441474Sjulian		} else {
29541474Sjulian			/*
29641474Sjulian			 * The filesystem can be marked clean even if
29741474Sjulian			 * a file is not linked up, but is cleared.
29841474Sjulian			 * Hence, resolved should not be cleared when
29941474Sjulian			 * linkup is answered no, but clri is answered yes.
30041474Sjulian			 */
30141474Sjulian			saveresolved = resolved;
30241474Sjulian			if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
30341474Sjulian				resolved = saveresolved;
30441474Sjulian				clri(idesc, "UNREF", 0);
30541474Sjulian				return;
30641474Sjulian			}
30741474Sjulian			/*
30841474Sjulian			 * Account for the new reference created by linkup().
30941474Sjulian			 */
31041474Sjulian			dp = ginode(idesc->id_number);
31141474Sjulian			lcnt--;
31241474Sjulian		}
31341474Sjulian	}
31441474Sjulian	if (lcnt != 0) {
3151558Srgrimes		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
31698542Smckusick			((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
3171558Srgrimes		pinode(idesc->id_number);
3181558Srgrimes		printf(" COUNT %d SHOULD BE %d",
31998542Smckusick			DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt);
32034266Sjulian		if (preen || usedsoftdep) {
3211558Srgrimes			if (lcnt < 0) {
3221558Srgrimes				printf("\n");
3231558Srgrimes				pfatal("LINK COUNT INCREASING");
3241558Srgrimes			}
32534266Sjulian			if (preen)
32634266Sjulian				printf(" (ADJUSTED)\n");
3271558Srgrimes		}
3281558Srgrimes		if (preen || reply("ADJUST") == 1) {
32974556Smckusick			if (bkgrdflag == 0) {
33098542Smckusick				DIP(dp, di_nlink) -= lcnt;
33174556Smckusick				inodirty();
33274556Smckusick			} else {
33374556Smckusick				cmd.value = idesc->id_number;
33474556Smckusick				cmd.size = -lcnt;
33574556Smckusick				if (debug)
33686514Siedowse					printf("adjrefcnt ino %ld amt %ld\n",
33774556Smckusick					    (long)cmd.value, cmd.size);
33874556Smckusick				if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
33974556Smckusick				    &cmd, sizeof cmd) == -1)
34074556Smckusick					rwerror("ADJUST INODE", cmd.value);
34174556Smckusick			}
3421558Srgrimes		}
3431558Srgrimes	}
3441558Srgrimes}
3451558Srgrimes
34623675Speterstatic int
34792839Simpmkentry(struct inodesc *idesc)
3481558Srgrimes{
34992806Sobrien	struct direct *dirp = idesc->id_dirp;
3501558Srgrimes	struct direct newent;
3511558Srgrimes	int newlen, oldlen;
3521558Srgrimes
3531558Srgrimes	newent.d_namlen = strlen(idesc->id_name);
3541558Srgrimes	newlen = DIRSIZ(0, &newent);
3551558Srgrimes	if (dirp->d_ino != 0)
3561558Srgrimes		oldlen = DIRSIZ(0, dirp);
3571558Srgrimes	else
3581558Srgrimes		oldlen = 0;
3591558Srgrimes	if (dirp->d_reclen - oldlen < newlen)
3601558Srgrimes		return (KEEPON);
3611558Srgrimes	newent.d_reclen = dirp->d_reclen - oldlen;
3621558Srgrimes	dirp->d_reclen = oldlen;
3631558Srgrimes	dirp = (struct direct *)(((char *)dirp) + oldlen);
3641558Srgrimes	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
36523675Speter	dirp->d_reclen = newent.d_reclen;
36696483Sphk	dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
36723675Speter	dirp->d_namlen = newent.d_namlen;
36823675Speter	memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
3691558Srgrimes	return (ALTERED|STOP);
3701558Srgrimes}
3711558Srgrimes
37223675Speterstatic int
37392839Simpchgino(struct inodesc *idesc)
3741558Srgrimes{
37592806Sobrien	struct direct *dirp = idesc->id_dirp;
3761558Srgrimes
37723675Speter	if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
3781558Srgrimes		return (KEEPON);
3791558Srgrimes	dirp->d_ino = idesc->id_parent;
38096483Sphk	dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
3811558Srgrimes	return (ALTERED|STOP);
3821558Srgrimes}
3831558Srgrimes
3847585Sbdeint
38592839Simplinkup(ino_t orphan, ino_t parentdir, char *name)
3861558Srgrimes{
38798542Smckusick	union dinode *dp;
3881558Srgrimes	int lostdir;
3891558Srgrimes	ino_t oldlfdir;
3901558Srgrimes	struct inodesc idesc;
3911558Srgrimes	char tempname[BUFSIZ];
3921558Srgrimes
39323675Speter	memset(&idesc, 0, sizeof(struct inodesc));
3941558Srgrimes	dp = ginode(orphan);
39598542Smckusick	lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR;
3961558Srgrimes	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
3971558Srgrimes	pinode(orphan);
39898542Smckusick	if (preen && DIP(dp, di_size) == 0)
3991558Srgrimes		return (0);
40074556Smckusick	if (cursnapshot != 0) {
40174556Smckusick		pfatal("FILE LINKUP IN SNAPSHOT");
40274556Smckusick		return (0);
40374556Smckusick	}
4041558Srgrimes	if (preen)
4051558Srgrimes		printf(" (RECONNECTED)\n");
4061558Srgrimes	else
4071558Srgrimes		if (reply("RECONNECT") == 0)
4081558Srgrimes			return (0);
4091558Srgrimes	if (lfdir == 0) {
4101558Srgrimes		dp = ginode(ROOTINO);
4111558Srgrimes		idesc.id_name = lfname;
4121558Srgrimes		idesc.id_type = DATA;
4131558Srgrimes		idesc.id_func = findino;
4141558Srgrimes		idesc.id_number = ROOTINO;
4151558Srgrimes		if ((ckinode(dp, &idesc) & FOUND) != 0) {
4161558Srgrimes			lfdir = idesc.id_parent;
4171558Srgrimes		} else {
4181558Srgrimes			pwarn("NO lost+found DIRECTORY");
4191558Srgrimes			if (preen || reply("CREATE")) {
4201558Srgrimes				lfdir = allocdir(ROOTINO, (ino_t)0, lfmode);
4211558Srgrimes				if (lfdir != 0) {
4221558Srgrimes					if (makeentry(ROOTINO, lfdir, lfname) != 0) {
42341474Sjulian						numdirs++;
4241558Srgrimes						if (preen)
4251558Srgrimes							printf(" (CREATED)\n");
4261558Srgrimes					} else {
4271558Srgrimes						freedir(lfdir, ROOTINO);
4281558Srgrimes						lfdir = 0;
4291558Srgrimes						if (preen)
4301558Srgrimes							printf("\n");
4311558Srgrimes					}
4321558Srgrimes				}
4331558Srgrimes			}
4341558Srgrimes		}
4351558Srgrimes		if (lfdir == 0) {
4361558Srgrimes			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
4371558Srgrimes			printf("\n\n");
4381558Srgrimes			return (0);
4391558Srgrimes		}
4401558Srgrimes	}
4411558Srgrimes	dp = ginode(lfdir);
44298542Smckusick	if ((DIP(dp, di_mode) & IFMT) != IFDIR) {
4431558Srgrimes		pfatal("lost+found IS NOT A DIRECTORY");
4441558Srgrimes		if (reply("REALLOCATE") == 0)
4451558Srgrimes			return (0);
4461558Srgrimes		oldlfdir = lfdir;
4471558Srgrimes		if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) {
4481558Srgrimes			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
4491558Srgrimes			return (0);
4501558Srgrimes		}
4511558Srgrimes		if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
4521558Srgrimes			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
4531558Srgrimes			return (0);
4541558Srgrimes		}
4551558Srgrimes		inodirty();
4561558Srgrimes		idesc.id_type = ADDR;
4571558Srgrimes		idesc.id_func = pass4check;
4581558Srgrimes		idesc.id_number = oldlfdir;
45941474Sjulian		adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
46041474Sjulian		inoinfo(oldlfdir)->ino_linkcnt = 0;
4611558Srgrimes		dp = ginode(lfdir);
4621558Srgrimes	}
46341474Sjulian	if (inoinfo(lfdir)->ino_state != DFOUND) {
4641558Srgrimes		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
4651558Srgrimes		return (0);
4661558Srgrimes	}
4671558Srgrimes	(void)lftempname(tempname, orphan);
46841474Sjulian	if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
4691558Srgrimes		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
4701558Srgrimes		printf("\n\n");
4711558Srgrimes		return (0);
4721558Srgrimes	}
47341474Sjulian	inoinfo(orphan)->ino_linkcnt--;
4741558Srgrimes	if (lostdir) {
4751558Srgrimes		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
4761558Srgrimes		    parentdir != (ino_t)-1)
4771558Srgrimes			(void)makeentry(orphan, lfdir, "..");
4781558Srgrimes		dp = ginode(lfdir);
47998542Smckusick		DIP(dp, di_nlink)++;
4801558Srgrimes		inodirty();
48141474Sjulian		inoinfo(lfdir)->ino_linkcnt++;
48286514Siedowse		pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan);
48315699Snate		if (parentdir != (ino_t)-1) {
48437236Sbde			printf("PARENT WAS I=%lu\n", (u_long)parentdir);
48541477Sjulian			/*
48641477Sjulian			 * The parent directory, because of the ordering
48741477Sjulian			 * guarantees, has had the link count incremented
48841477Sjulian			 * for the child, but no entry was made.  This
48941477Sjulian			 * fixes the parent link count so that fsck does
49041477Sjulian			 * not need to be rerun.
49141477Sjulian			 */
49241474Sjulian			inoinfo(parentdir)->ino_linkcnt++;
49315699Snate		}
4941558Srgrimes		if (preen == 0)
4951558Srgrimes			printf("\n");
4961558Srgrimes	}
4971558Srgrimes	return (1);
4981558Srgrimes}
4991558Srgrimes
5001558Srgrimes/*
5011558Srgrimes * fix an entry in a directory.
5021558Srgrimes */
5037585Sbdeint
50492839Simpchangeino(ino_t dir, char *name, ino_t newnum)
5051558Srgrimes{
5061558Srgrimes	struct inodesc idesc;
5071558Srgrimes
50823675Speter	memset(&idesc, 0, sizeof(struct inodesc));
5091558Srgrimes	idesc.id_type = DATA;
5101558Srgrimes	idesc.id_func = chgino;
5111558Srgrimes	idesc.id_number = dir;
5121558Srgrimes	idesc.id_fix = DONTKNOW;
5131558Srgrimes	idesc.id_name = name;
5141558Srgrimes	idesc.id_parent = newnum;	/* new value for name */
5151558Srgrimes	return (ckinode(ginode(dir), &idesc));
5161558Srgrimes}
5171558Srgrimes
5181558Srgrimes/*
5191558Srgrimes * make an entry in a directory
5201558Srgrimes */
5217585Sbdeint
52292839Simpmakeentry(ino_t parent, ino_t ino, char *name)
5231558Srgrimes{
52498542Smckusick	union dinode *dp;
5251558Srgrimes	struct inodesc idesc;
5261558Srgrimes	char pathbuf[MAXPATHLEN + 1];
5278871Srgrimes
5281558Srgrimes	if (parent < ROOTINO || parent >= maxino ||
5291558Srgrimes	    ino < ROOTINO || ino >= maxino)
5301558Srgrimes		return (0);
53123675Speter	memset(&idesc, 0, sizeof(struct inodesc));
5321558Srgrimes	idesc.id_type = DATA;
5331558Srgrimes	idesc.id_func = mkentry;
5341558Srgrimes	idesc.id_number = parent;
5351558Srgrimes	idesc.id_parent = ino;	/* this is the inode to enter */
5361558Srgrimes	idesc.id_fix = DONTKNOW;
5371558Srgrimes	idesc.id_name = name;
5381558Srgrimes	dp = ginode(parent);
53998542Smckusick	if (DIP(dp, di_size) % DIRBLKSIZ) {
54098542Smckusick		DIP(dp, di_size) = roundup(DIP(dp, di_size), DIRBLKSIZ);
5411558Srgrimes		inodirty();
5421558Srgrimes	}
5431558Srgrimes	if ((ckinode(dp, &idesc) & ALTERED) != 0)
5441558Srgrimes		return (1);
5451558Srgrimes	getpathname(pathbuf, parent, parent);
5461558Srgrimes	dp = ginode(parent);
5471558Srgrimes	if (expanddir(dp, pathbuf) == 0)
5481558Srgrimes		return (0);
5491558Srgrimes	return (ckinode(dp, &idesc) & ALTERED);
5501558Srgrimes}
5511558Srgrimes
5521558Srgrimes/*
5531558Srgrimes * Attempt to expand the size of a directory
5541558Srgrimes */
55523675Speterstatic int
55698542Smckusickexpanddir(union dinode *dp, char *name)
5571558Srgrimes{
55898542Smckusick	ufs2_daddr_t lastbn, newblk;
55992806Sobrien	struct bufarea *bp;
5601558Srgrimes	char *cp, firstblk[DIRBLKSIZ];
5611558Srgrimes
56298542Smckusick	lastbn = lblkno(&sblock, DIP(dp, di_size));
56398542Smckusick	if (lastbn >= NDADDR - 1 || DIP(dp, di_db[lastbn]) == 0 ||
56498542Smckusick	    DIP(dp, di_size) == 0)
5651558Srgrimes		return (0);
5661558Srgrimes	if ((newblk = allocblk(sblock.fs_frag)) == 0)
5671558Srgrimes		return (0);
56898542Smckusick	DIP(dp, di_db[lastbn + 1]) = DIP(dp, di_db[lastbn]);
56998542Smckusick	DIP(dp, di_db[lastbn]) = newblk;
57098542Smckusick	DIP(dp, di_size) += sblock.fs_bsize;
57198542Smckusick	DIP(dp, di_blocks) += btodb(sblock.fs_bsize);
57298542Smckusick	bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
57398542Smckusick		sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
5741558Srgrimes	if (bp->b_errs)
5751558Srgrimes		goto bad;
57623675Speter	memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
5771558Srgrimes	bp = getdirblk(newblk, sblock.fs_bsize);
5781558Srgrimes	if (bp->b_errs)
5791558Srgrimes		goto bad;
58023675Speter	memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
5811558Srgrimes	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
5821558Srgrimes	     cp < &bp->b_un.b_buf[sblock.fs_bsize];
5831558Srgrimes	     cp += DIRBLKSIZ)
58423675Speter		memmove(cp, &emptydir, sizeof emptydir);
5851558Srgrimes	dirty(bp);
58698542Smckusick	bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
58798542Smckusick		sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
5881558Srgrimes	if (bp->b_errs)
5891558Srgrimes		goto bad;
59023675Speter	memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir);
5911558Srgrimes	pwarn("NO SPACE LEFT IN %s", name);
5921558Srgrimes	if (preen)
5931558Srgrimes		printf(" (EXPANDED)\n");
5941558Srgrimes	else if (reply("EXPAND") == 0)
5951558Srgrimes		goto bad;
5961558Srgrimes	dirty(bp);
5971558Srgrimes	inodirty();
5981558Srgrimes	return (1);
5991558Srgrimesbad:
60098542Smckusick	DIP(dp, di_db[lastbn]) = DIP(dp, di_db[lastbn + 1]);
60198542Smckusick	DIP(dp, di_db[lastbn + 1]) = 0;
60298542Smckusick	DIP(dp, di_size) -= sblock.fs_bsize;
60398542Smckusick	DIP(dp, di_blocks) -= btodb(sblock.fs_bsize);
6041558Srgrimes	freeblk(newblk, sblock.fs_frag);
6051558Srgrimes	return (0);
6061558Srgrimes}
6071558Srgrimes
6081558Srgrimes/*
6091558Srgrimes * allocate a new directory
6101558Srgrimes */
6117586Sbdeino_t
61292839Simpallocdir(ino_t parent, ino_t request, int mode)
6131558Srgrimes{
6141558Srgrimes	ino_t ino;
6151558Srgrimes	char *cp;
61698542Smckusick	union dinode *dp;
61763810Smckusick	struct bufarea *bp;
61863810Smckusick	struct inoinfo *inp;
6191558Srgrimes	struct dirtemplate *dirp;
6201558Srgrimes
6211558Srgrimes	ino = allocino(request, IFDIR|mode);
62296483Sphk	dirp = &dirhead;
6231558Srgrimes	dirp->dot_ino = ino;
6241558Srgrimes	dirp->dotdot_ino = parent;
6251558Srgrimes	dp = ginode(ino);
62698542Smckusick	bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
6271558Srgrimes	if (bp->b_errs) {
6281558Srgrimes		freeino(ino);
6291558Srgrimes		return (0);
6301558Srgrimes	}
63123675Speter	memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
6321558Srgrimes	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
6331558Srgrimes	     cp < &bp->b_un.b_buf[sblock.fs_fsize];
6341558Srgrimes	     cp += DIRBLKSIZ)
63523675Speter		memmove(cp, &emptydir, sizeof emptydir);
6361558Srgrimes	dirty(bp);
63798542Smckusick	DIP(dp, di_nlink) = 2;
6381558Srgrimes	inodirty();
6391558Srgrimes	if (ino == ROOTINO) {
64098542Smckusick		inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
6411558Srgrimes		cacheino(dp, ino);
6421558Srgrimes		return(ino);
6431558Srgrimes	}
64441474Sjulian	if (inoinfo(parent)->ino_state != DSTATE &&
64541474Sjulian	    inoinfo(parent)->ino_state != DFOUND) {
6461558Srgrimes		freeino(ino);
6471558Srgrimes		return (0);
6481558Srgrimes	}
6491558Srgrimes	cacheino(dp, ino);
65063810Smckusick	inp = getinoinfo(ino);
65163810Smckusick	inp->i_parent = parent;
65263810Smckusick	inp->i_dotdot = parent;
65341474Sjulian	inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
65441474Sjulian	if (inoinfo(ino)->ino_state == DSTATE) {
65598542Smckusick		inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
65641474Sjulian		inoinfo(parent)->ino_linkcnt++;
6571558Srgrimes	}
6581558Srgrimes	dp = ginode(parent);
65998542Smckusick	DIP(dp, di_nlink)++;
6601558Srgrimes	inodirty();
6611558Srgrimes	return (ino);
6621558Srgrimes}
6631558Srgrimes
6641558Srgrimes/*
6651558Srgrimes * free a directory inode
6661558Srgrimes */
6677585Sbdestatic void
66892839Simpfreedir(ino_t ino, ino_t parent)
6691558Srgrimes{
67098542Smckusick	union dinode *dp;
6711558Srgrimes
6721558Srgrimes	if (ino != parent) {
6731558Srgrimes		dp = ginode(parent);
67498542Smckusick		DIP(dp, di_nlink)--;
6751558Srgrimes		inodirty();
6761558Srgrimes	}
6771558Srgrimes	freeino(ino);
6781558Srgrimes}
6791558Srgrimes
6801558Srgrimes/*
6811558Srgrimes * generate a temporary name for the lost+found directory.
6821558Srgrimes */
68323675Speterstatic int
68492839Simplftempname(char *bufp, ino_t ino)
6851558Srgrimes{
68692806Sobrien	ino_t in;
68792806Sobrien	char *cp;
6881558Srgrimes	int namlen;
6891558Srgrimes
6901558Srgrimes	cp = bufp + 2;
6911558Srgrimes	for (in = maxino; in > 0; in /= 10)
6921558Srgrimes		cp++;
6931558Srgrimes	*--cp = 0;
6941558Srgrimes	namlen = cp - bufp;
6951558Srgrimes	in = ino;
6961558Srgrimes	while (cp > bufp) {
6971558Srgrimes		*--cp = (in % 10) + '0';
6981558Srgrimes		in /= 10;
6991558Srgrimes	}
7001558Srgrimes	*cp = '#';
7011558Srgrimes	return (namlen);
7021558Srgrimes}
7031558Srgrimes
7041558Srgrimes/*
7051558Srgrimes * Get a directory block.
7061558Srgrimes * Insure that it is held until another is requested.
7071558Srgrimes */
70823675Speterstatic struct bufarea *
70998542Smckusickgetdirblk(ufs2_daddr_t blkno, long size)
7101558Srgrimes{
7111558Srgrimes
7121558Srgrimes	if (pdirbp != 0)
7131558Srgrimes		pdirbp->b_flags &= ~B_INUSE;
7141558Srgrimes	pdirbp = getdatablk(blkno, size);
7151558Srgrimes	return (pdirbp);
7161558Srgrimes}
717