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 * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
30114589Sobrien#if 0
311558Srgrimes#ifndef lint
3223675Speterstatic const char sccsid[] = "@(#)dir.c	8.8 (Berkeley) 4/28/95";
33114589Sobrien#endif /* not lint */
3441477Sjulian#endif
35114589Sobrien#include <sys/cdefs.h>
36114589Sobrien__FBSDID("$FreeBSD$");
371558Srgrimes
381558Srgrimes#include <sys/param.h>
3941474Sjulian#include <sys/time.h>
4074556Smckusick#include <sys/sysctl.h>
4123675Speter
421558Srgrimes#include <ufs/ufs/dinode.h>
431558Srgrimes#include <ufs/ufs/dir.h>
441558Srgrimes#include <ufs/ffs/fs.h>
4523796Sbde
4623675Speter#include <err.h>
471558Srgrimes#include <string.h>
4823675Speter
491558Srgrimes#include "fsck.h"
501558Srgrimes
51260178Sscottlstatic struct	dirtemplate emptydir = {
5274556Smckusick	0, DIRBLKSIZ, DT_UNKNOWN, 0, "",
5374556Smckusick	0, 0, DT_UNKNOWN, 0, ""
5474556Smckusick};
55260178Sscottlstatic struct	dirtemplate dirhead = {
561558Srgrimes	0, 12, DT_DIR, 1, ".",
571558Srgrimes	0, DIRBLKSIZ - 12, DT_DIR, 2, ".."
581558Srgrimes};
591558Srgrimes
6092839Simpstatic int chgino(struct inodesc *);
6192839Simpstatic int dircheck(struct inodesc *, struct direct *);
6298542Smckusickstatic int expanddir(union dinode *dp, char *name);
6392839Simpstatic void freedir(ino_t ino, ino_t parent);
6492839Simpstatic struct direct *fsck_readdir(struct inodesc *);
6598542Smckusickstatic struct bufarea *getdirblk(ufs2_daddr_t blkno, long size);
6692839Simpstatic int lftempname(char *bufp, ino_t ino);
6792839Simpstatic int mkentry(struct inodesc *);
681558Srgrimes
691558Srgrimes/*
701558Srgrimes * Propagate connected state through the tree.
711558Srgrimes */
727585Sbdevoid
7392839Simppropagate(void)
741558Srgrimes{
7592806Sobrien	struct inoinfo **inpp, *inp;
761558Srgrimes	struct inoinfo **inpend;
771558Srgrimes	long change;
781558Srgrimes
791558Srgrimes	inpend = &inpsort[inplast];
801558Srgrimes	do {
811558Srgrimes		change = 0;
821558Srgrimes		for (inpp = inpsort; inpp < inpend; inpp++) {
831558Srgrimes			inp = *inpp;
841558Srgrimes			if (inp->i_parent == 0)
851558Srgrimes				continue;
8641474Sjulian			if (inoinfo(inp->i_parent)->ino_state == DFOUND &&
87136281Struckman			    INO_IS_DUNFOUND(inp->i_number)) {
8841474Sjulian				inoinfo(inp->i_number)->ino_state = DFOUND;
891558Srgrimes				change++;
901558Srgrimes			}
911558Srgrimes		}
921558Srgrimes	} while (change > 0);
931558Srgrimes}
941558Srgrimes
951558Srgrimes/*
961558Srgrimes * Scan each entry in a directory block.
971558Srgrimes */
987585Sbdeint
9992839Simpdirscan(struct inodesc *idesc)
1001558Srgrimes{
10192806Sobrien	struct direct *dp;
10292806Sobrien	struct bufarea *bp;
10374556Smckusick	u_int dsize, n;
1041558Srgrimes	long blksiz;
1051558Srgrimes	char dbuf[DIRBLKSIZ];
1061558Srgrimes
1071558Srgrimes	if (idesc->id_type != DATA)
10823675Speter		errx(EEXIT, "wrong type to dirscan %d", idesc->id_type);
1091558Srgrimes	if (idesc->id_entryno == 0 &&
1101558Srgrimes	    (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
1111558Srgrimes		idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
1121558Srgrimes	blksiz = idesc->id_numfrags * sblock.fs_fsize;
1131558Srgrimes	if (chkrange(idesc->id_blkno, idesc->id_numfrags)) {
1141558Srgrimes		idesc->id_filesize -= blksiz;
1151558Srgrimes		return (SKIP);
1161558Srgrimes	}
1171558Srgrimes	idesc->id_loc = 0;
1181558Srgrimes	for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
1191558Srgrimes		dsize = dp->d_reclen;
12041474Sjulian		if (dsize > sizeof(dbuf))
12141474Sjulian			dsize = sizeof(dbuf);
12223675Speter		memmove(dbuf, dp, (size_t)dsize);
1231558Srgrimes		idesc->id_dirp = (struct direct *)dbuf;
1241558Srgrimes		if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
1251558Srgrimes			bp = getdirblk(idesc->id_blkno, blksiz);
12623675Speter			memmove(bp->b_un.b_buf + idesc->id_loc - dsize, dbuf,
1271558Srgrimes			    (size_t)dsize);
1281558Srgrimes			dirty(bp);
1291558Srgrimes			sbdirty();
130260178Sscottl			rerun = 1;
1311558Srgrimes		}
1328871Srgrimes		if (n & STOP)
1331558Srgrimes			return (n);
1341558Srgrimes	}
1351558Srgrimes	return (idesc->id_filesize > 0 ? KEEPON : STOP);
1361558Srgrimes}
1371558Srgrimes
1381558Srgrimes/*
1391558Srgrimes * get next entry in a directory.
1401558Srgrimes */
14123675Speterstatic struct direct *
14292839Simpfsck_readdir(struct inodesc *idesc)
1431558Srgrimes{
14492806Sobrien	struct direct *dp, *ndp;
14592806Sobrien	struct bufarea *bp;
1461558Srgrimes	long size, blksiz, fix, dploc;
1471558Srgrimes
1481558Srgrimes	blksiz = idesc->id_numfrags * sblock.fs_fsize;
1491558Srgrimes	bp = getdirblk(idesc->id_blkno, blksiz);
1501558Srgrimes	if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
1511558Srgrimes	    idesc->id_loc < blksiz) {
1521558Srgrimes		dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
1531558Srgrimes		if (dircheck(idesc, dp))
1541558Srgrimes			goto dpok;
15523675Speter		if (idesc->id_fix == IGNORE)
15623675Speter			return (0);
1571558Srgrimes		fix = dofix(idesc, "DIRECTORY CORRUPTED");
1581558Srgrimes		bp = getdirblk(idesc->id_blkno, blksiz);
1591558Srgrimes		dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
1601558Srgrimes		dp->d_reclen = DIRBLKSIZ;
1611558Srgrimes		dp->d_ino = 0;
1621558Srgrimes		dp->d_type = 0;
1631558Srgrimes		dp->d_namlen = 0;
1641558Srgrimes		dp->d_name[0] = '\0';
1651558Srgrimes		if (fix)
1661558Srgrimes			dirty(bp);
1671558Srgrimes		idesc->id_loc += DIRBLKSIZ;
1681558Srgrimes		idesc->id_filesize -= DIRBLKSIZ;
1691558Srgrimes		return (dp);
1701558Srgrimes	}
1711558Srgrimesdpok:
1721558Srgrimes	if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
1731558Srgrimes		return NULL;
1741558Srgrimes	dploc = idesc->id_loc;
1751558Srgrimes	dp = (struct direct *)(bp->b_un.b_buf + dploc);
1761558Srgrimes	idesc->id_loc += dp->d_reclen;
1771558Srgrimes	idesc->id_filesize -= dp->d_reclen;
1781558Srgrimes	if ((idesc->id_loc % DIRBLKSIZ) == 0)
1791558Srgrimes		return (dp);
1801558Srgrimes	ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
1811558Srgrimes	if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
1821558Srgrimes	    dircheck(idesc, ndp) == 0) {
1831558Srgrimes		size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
1841558Srgrimes		idesc->id_loc += size;
1851558Srgrimes		idesc->id_filesize -= size;
18623675Speter		if (idesc->id_fix == IGNORE)
18723675Speter			return (0);
1881558Srgrimes		fix = dofix(idesc, "DIRECTORY CORRUPTED");
1891558Srgrimes		bp = getdirblk(idesc->id_blkno, blksiz);
1901558Srgrimes		dp = (struct direct *)(bp->b_un.b_buf + dploc);
1911558Srgrimes		dp->d_reclen += size;
1921558Srgrimes		if (fix)
1931558Srgrimes			dirty(bp);
1941558Srgrimes	}
1951558Srgrimes	return (dp);
1961558Srgrimes}
1971558Srgrimes
1981558Srgrimes/*
1991558Srgrimes * Verify that a directory entry is valid.
2001558Srgrimes * This is a superset of the checks made in the kernel.
2011558Srgrimes */
20223675Speterstatic int
20392839Simpdircheck(struct inodesc *idesc, struct direct *dp)
2041558Srgrimes{
205114589Sobrien	size_t size;
20692806Sobrien	char *cp;
207114589Sobrien	u_char type;
208225338Sdelphij	u_int8_t namlen;
2091558Srgrimes	int spaceleft;
2101558Srgrimes
21123675Speter	spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
21241474Sjulian	if (dp->d_reclen == 0 ||
21323675Speter	    dp->d_reclen > spaceleft ||
21423675Speter	    (dp->d_reclen & 0x3) != 0)
21562668Smckusick		goto bad;
21623675Speter	if (dp->d_ino == 0)
21723675Speter		return (1);
21896483Sphk	size = DIRSIZ(0, dp);
21996483Sphk	namlen = dp->d_namlen;
22096483Sphk	type = dp->d_type;
22123675Speter	if (dp->d_reclen < size ||
22223675Speter	    idesc->id_filesize < size ||
223225338Sdelphij	    namlen == 0 ||
22423675Speter	    type > 15)
22562668Smckusick		goto bad;
22623675Speter	for (cp = dp->d_name, size = 0; size < namlen; size++)
22723675Speter		if (*cp == '\0' || (*cp++ == '/'))
22862668Smckusick			goto bad;
22923675Speter	if (*cp != '\0')
23062668Smckusick		goto bad;
23123675Speter	return (1);
23262668Smckusickbad:
23362668Smckusick	if (debug)
23462668Smckusick		printf("Bad dir: ino %d reclen %d namlen %d type %d name %s\n",
23562668Smckusick		    dp->d_ino, dp->d_reclen, dp->d_namlen, dp->d_type,
23662668Smckusick		    dp->d_name);
23762668Smckusick	return (0);
2381558Srgrimes}
2391558Srgrimes
2407585Sbdevoid
241100935Sphkdirerror(ino_t ino, const char *errmesg)
2421558Srgrimes{
2431558Srgrimes
2441558Srgrimes	fileerror(ino, ino, errmesg);
2451558Srgrimes}
2461558Srgrimes
2477585Sbdevoid
248100935Sphkfileerror(ino_t cwd, ino_t ino, const char *errmesg)
2491558Srgrimes{
25098542Smckusick	union dinode *dp;
2511558Srgrimes	char pathbuf[MAXPATHLEN + 1];
2521558Srgrimes
2531558Srgrimes	pwarn("%s ", errmesg);
2541558Srgrimes	pinode(ino);
2551558Srgrimes	printf("\n");
2561558Srgrimes	getpathname(pathbuf, cwd, ino);
2571558Srgrimes	if (ino < ROOTINO || ino > maxino) {
2581558Srgrimes		pfatal("NAME=%s\n", pathbuf);
2591558Srgrimes		return;
2601558Srgrimes	}
2611558Srgrimes	dp = ginode(ino);
2621558Srgrimes	if (ftypeok(dp))
2631558Srgrimes		pfatal("%s=%s\n",
26498542Smckusick		    (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE",
26598542Smckusick		    pathbuf);
2661558Srgrimes	else
2671558Srgrimes		pfatal("NAME=%s\n", pathbuf);
2681558Srgrimes}
2691558Srgrimes
2707585Sbdevoid
27192839Simpadjust(struct inodesc *idesc, int lcnt)
2721558Srgrimes{
27398542Smckusick	union dinode *dp;
27441474Sjulian	int saveresolved;
2751558Srgrimes
2761558Srgrimes	dp = ginode(idesc->id_number);
27798542Smckusick	if (DIP(dp, di_nlink) == lcnt) {
27841474Sjulian		/*
27941474Sjulian		 * If we have not hit any unresolved problems, are running
280102231Strhodes		 * in preen mode, and are on a file system using soft updates,
28141474Sjulian		 * then just toss any partially allocated files.
28241474Sjulian		 */
28374556Smckusick		if (resolved && (preen || bkgrdflag) && usedsoftdep) {
28441474Sjulian			clri(idesc, "UNREF", 1);
28541474Sjulian			return;
28641474Sjulian		} else {
28741474Sjulian			/*
288102231Strhodes			 * The file system can be marked clean even if
28941474Sjulian			 * a file is not linked up, but is cleared.
29041474Sjulian			 * Hence, resolved should not be cleared when
29141474Sjulian			 * linkup is answered no, but clri is answered yes.
29241474Sjulian			 */
29341474Sjulian			saveresolved = resolved;
29441474Sjulian			if (linkup(idesc->id_number, (ino_t)0, NULL) == 0) {
29541474Sjulian				resolved = saveresolved;
29641474Sjulian				clri(idesc, "UNREF", 0);
29741474Sjulian				return;
29841474Sjulian			}
29941474Sjulian			/*
30041474Sjulian			 * Account for the new reference created by linkup().
30141474Sjulian			 */
30241474Sjulian			dp = ginode(idesc->id_number);
30341474Sjulian			lcnt--;
30441474Sjulian		}
30541474Sjulian	}
30641474Sjulian	if (lcnt != 0) {
3071558Srgrimes		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
30898542Smckusick			((DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE"));
3091558Srgrimes		pinode(idesc->id_number);
3101558Srgrimes		printf(" COUNT %d SHOULD BE %d",
31198542Smckusick			DIP(dp, di_nlink), DIP(dp, di_nlink) - lcnt);
31234266Sjulian		if (preen || usedsoftdep) {
3131558Srgrimes			if (lcnt < 0) {
3141558Srgrimes				printf("\n");
3151558Srgrimes				pfatal("LINK COUNT INCREASING");
3161558Srgrimes			}
31734266Sjulian			if (preen)
31834266Sjulian				printf(" (ADJUSTED)\n");
3191558Srgrimes		}
3201558Srgrimes		if (preen || reply("ADJUST") == 1) {
32174556Smckusick			if (bkgrdflag == 0) {
322134589Sscottl				DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - lcnt);
32374556Smckusick				inodirty();
32474556Smckusick			} else {
32574556Smckusick				cmd.value = idesc->id_number;
32674556Smckusick				cmd.size = -lcnt;
32774556Smckusick				if (debug)
328100935Sphk					printf("adjrefcnt ino %ld amt %lld\n",
329100935Sphk					    (long)cmd.value,
330100935Sphk					    (long long)cmd.size);
33174556Smckusick				if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
33274556Smckusick				    &cmd, sizeof cmd) == -1)
33374556Smckusick					rwerror("ADJUST INODE", cmd.value);
33474556Smckusick			}
3351558Srgrimes		}
3361558Srgrimes	}
3371558Srgrimes}
3381558Srgrimes
33923675Speterstatic int
34092839Simpmkentry(struct inodesc *idesc)
3411558Srgrimes{
34292806Sobrien	struct direct *dirp = idesc->id_dirp;
3431558Srgrimes	struct direct newent;
3441558Srgrimes	int newlen, oldlen;
3451558Srgrimes
3461558Srgrimes	newent.d_namlen = strlen(idesc->id_name);
3471558Srgrimes	newlen = DIRSIZ(0, &newent);
3481558Srgrimes	if (dirp->d_ino != 0)
3491558Srgrimes		oldlen = DIRSIZ(0, dirp);
3501558Srgrimes	else
3511558Srgrimes		oldlen = 0;
3521558Srgrimes	if (dirp->d_reclen - oldlen < newlen)
3531558Srgrimes		return (KEEPON);
3541558Srgrimes	newent.d_reclen = dirp->d_reclen - oldlen;
3551558Srgrimes	dirp->d_reclen = oldlen;
3561558Srgrimes	dirp = (struct direct *)(((char *)dirp) + oldlen);
3571558Srgrimes	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
35823675Speter	dirp->d_reclen = newent.d_reclen;
35996483Sphk	dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
36023675Speter	dirp->d_namlen = newent.d_namlen;
36123675Speter	memmove(dirp->d_name, idesc->id_name, (size_t)newent.d_namlen + 1);
3621558Srgrimes	return (ALTERED|STOP);
3631558Srgrimes}
3641558Srgrimes
36523675Speterstatic int
36692839Simpchgino(struct inodesc *idesc)
3671558Srgrimes{
36892806Sobrien	struct direct *dirp = idesc->id_dirp;
3691558Srgrimes
37023675Speter	if (memcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
3711558Srgrimes		return (KEEPON);
3721558Srgrimes	dirp->d_ino = idesc->id_parent;
37396483Sphk	dirp->d_type = inoinfo(idesc->id_parent)->ino_type;
3741558Srgrimes	return (ALTERED|STOP);
3751558Srgrimes}
3761558Srgrimes
3777585Sbdeint
37892839Simplinkup(ino_t orphan, ino_t parentdir, char *name)
3791558Srgrimes{
38098542Smckusick	union dinode *dp;
3811558Srgrimes	int lostdir;
3821558Srgrimes	ino_t oldlfdir;
3831558Srgrimes	struct inodesc idesc;
3841558Srgrimes	char tempname[BUFSIZ];
3851558Srgrimes
38623675Speter	memset(&idesc, 0, sizeof(struct inodesc));
3871558Srgrimes	dp = ginode(orphan);
38898542Smckusick	lostdir = (DIP(dp, di_mode) & IFMT) == IFDIR;
3891558Srgrimes	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
3901558Srgrimes	pinode(orphan);
39198542Smckusick	if (preen && DIP(dp, di_size) == 0)
3921558Srgrimes		return (0);
39374556Smckusick	if (cursnapshot != 0) {
39474556Smckusick		pfatal("FILE LINKUP IN SNAPSHOT");
39574556Smckusick		return (0);
39674556Smckusick	}
3971558Srgrimes	if (preen)
3981558Srgrimes		printf(" (RECONNECTED)\n");
3991558Srgrimes	else
4001558Srgrimes		if (reply("RECONNECT") == 0)
4011558Srgrimes			return (0);
4021558Srgrimes	if (lfdir == 0) {
4031558Srgrimes		dp = ginode(ROOTINO);
404100935Sphk		idesc.id_name = strdup(lfname);
4051558Srgrimes		idesc.id_type = DATA;
4061558Srgrimes		idesc.id_func = findino;
4071558Srgrimes		idesc.id_number = ROOTINO;
4081558Srgrimes		if ((ckinode(dp, &idesc) & FOUND) != 0) {
4091558Srgrimes			lfdir = idesc.id_parent;
4101558Srgrimes		} else {
4111558Srgrimes			pwarn("NO lost+found DIRECTORY");
4121558Srgrimes			if (preen || reply("CREATE")) {
4131558Srgrimes				lfdir = allocdir(ROOTINO, (ino_t)0, lfmode);
4141558Srgrimes				if (lfdir != 0) {
4151558Srgrimes					if (makeentry(ROOTINO, lfdir, lfname) != 0) {
41641474Sjulian						numdirs++;
4171558Srgrimes						if (preen)
4181558Srgrimes							printf(" (CREATED)\n");
4191558Srgrimes					} else {
4201558Srgrimes						freedir(lfdir, ROOTINO);
4211558Srgrimes						lfdir = 0;
4221558Srgrimes						if (preen)
4231558Srgrimes							printf("\n");
4241558Srgrimes					}
4251558Srgrimes				}
4261558Srgrimes			}
4271558Srgrimes		}
4281558Srgrimes		if (lfdir == 0) {
4291558Srgrimes			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
4301558Srgrimes			printf("\n\n");
4311558Srgrimes			return (0);
4321558Srgrimes		}
4331558Srgrimes	}
4341558Srgrimes	dp = ginode(lfdir);
43598542Smckusick	if ((DIP(dp, di_mode) & IFMT) != IFDIR) {
4361558Srgrimes		pfatal("lost+found IS NOT A DIRECTORY");
4371558Srgrimes		if (reply("REALLOCATE") == 0)
4381558Srgrimes			return (0);
4391558Srgrimes		oldlfdir = lfdir;
4401558Srgrimes		if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) {
4411558Srgrimes			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
4421558Srgrimes			return (0);
4431558Srgrimes		}
4441558Srgrimes		if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
4451558Srgrimes			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
4461558Srgrimes			return (0);
4471558Srgrimes		}
4481558Srgrimes		inodirty();
4491558Srgrimes		idesc.id_type = ADDR;
4501558Srgrimes		idesc.id_func = pass4check;
4511558Srgrimes		idesc.id_number = oldlfdir;
45241474Sjulian		adjust(&idesc, inoinfo(oldlfdir)->ino_linkcnt + 1);
45341474Sjulian		inoinfo(oldlfdir)->ino_linkcnt = 0;
4541558Srgrimes		dp = ginode(lfdir);
4551558Srgrimes	}
45641474Sjulian	if (inoinfo(lfdir)->ino_state != DFOUND) {
4571558Srgrimes		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
4581558Srgrimes		return (0);
4591558Srgrimes	}
4601558Srgrimes	(void)lftempname(tempname, orphan);
46141474Sjulian	if (makeentry(lfdir, orphan, (name ? name : tempname)) == 0) {
4621558Srgrimes		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
4631558Srgrimes		printf("\n\n");
4641558Srgrimes		return (0);
4651558Srgrimes	}
46641474Sjulian	inoinfo(orphan)->ino_linkcnt--;
4671558Srgrimes	if (lostdir) {
4681558Srgrimes		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
4691558Srgrimes		    parentdir != (ino_t)-1)
4701558Srgrimes			(void)makeentry(orphan, lfdir, "..");
4711558Srgrimes		dp = ginode(lfdir);
472134589Sscottl		DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
4731558Srgrimes		inodirty();
47441474Sjulian		inoinfo(lfdir)->ino_linkcnt++;
47586514Siedowse		pwarn("DIR I=%lu CONNECTED. ", (u_long)orphan);
47615699Snate		if (parentdir != (ino_t)-1) {
47737236Sbde			printf("PARENT WAS I=%lu\n", (u_long)parentdir);
47841477Sjulian			/*
47941477Sjulian			 * The parent directory, because of the ordering
48041477Sjulian			 * guarantees, has had the link count incremented
48141477Sjulian			 * for the child, but no entry was made.  This
48241477Sjulian			 * fixes the parent link count so that fsck does
48341477Sjulian			 * not need to be rerun.
48441477Sjulian			 */
48541474Sjulian			inoinfo(parentdir)->ino_linkcnt++;
48615699Snate		}
4871558Srgrimes		if (preen == 0)
4881558Srgrimes			printf("\n");
4891558Srgrimes	}
4901558Srgrimes	return (1);
4911558Srgrimes}
4921558Srgrimes
4931558Srgrimes/*
4941558Srgrimes * fix an entry in a directory.
4951558Srgrimes */
4967585Sbdeint
497100935Sphkchangeino(ino_t dir, const char *name, ino_t newnum)
4981558Srgrimes{
4991558Srgrimes	struct inodesc idesc;
5001558Srgrimes
50123675Speter	memset(&idesc, 0, sizeof(struct inodesc));
5021558Srgrimes	idesc.id_type = DATA;
5031558Srgrimes	idesc.id_func = chgino;
5041558Srgrimes	idesc.id_number = dir;
5051558Srgrimes	idesc.id_fix = DONTKNOW;
506100935Sphk	idesc.id_name = strdup(name);
5071558Srgrimes	idesc.id_parent = newnum;	/* new value for name */
5081558Srgrimes	return (ckinode(ginode(dir), &idesc));
5091558Srgrimes}
5101558Srgrimes
5111558Srgrimes/*
5121558Srgrimes * make an entry in a directory
5131558Srgrimes */
5147585Sbdeint
515100935Sphkmakeentry(ino_t parent, ino_t ino, const char *name)
5161558Srgrimes{
51798542Smckusick	union dinode *dp;
5181558Srgrimes	struct inodesc idesc;
5191558Srgrimes	char pathbuf[MAXPATHLEN + 1];
5208871Srgrimes
5211558Srgrimes	if (parent < ROOTINO || parent >= maxino ||
5221558Srgrimes	    ino < ROOTINO || ino >= maxino)
5231558Srgrimes		return (0);
52423675Speter	memset(&idesc, 0, sizeof(struct inodesc));
5251558Srgrimes	idesc.id_type = DATA;
5261558Srgrimes	idesc.id_func = mkentry;
5271558Srgrimes	idesc.id_number = parent;
5281558Srgrimes	idesc.id_parent = ino;	/* this is the inode to enter */
5291558Srgrimes	idesc.id_fix = DONTKNOW;
530100935Sphk	idesc.id_name = strdup(name);
5311558Srgrimes	dp = ginode(parent);
53298542Smckusick	if (DIP(dp, di_size) % DIRBLKSIZ) {
533134589Sscottl		DIP_SET(dp, di_size, roundup(DIP(dp, di_size), DIRBLKSIZ));
5341558Srgrimes		inodirty();
5351558Srgrimes	}
5361558Srgrimes	if ((ckinode(dp, &idesc) & ALTERED) != 0)
5371558Srgrimes		return (1);
5381558Srgrimes	getpathname(pathbuf, parent, parent);
5391558Srgrimes	dp = ginode(parent);
5401558Srgrimes	if (expanddir(dp, pathbuf) == 0)
5411558Srgrimes		return (0);
5421558Srgrimes	return (ckinode(dp, &idesc) & ALTERED);
5431558Srgrimes}
5441558Srgrimes
5451558Srgrimes/*
5461558Srgrimes * Attempt to expand the size of a directory
5471558Srgrimes */
54823675Speterstatic int
54998542Smckusickexpanddir(union dinode *dp, char *name)
5501558Srgrimes{
55198542Smckusick	ufs2_daddr_t lastbn, newblk;
55292806Sobrien	struct bufarea *bp;
5531558Srgrimes	char *cp, firstblk[DIRBLKSIZ];
5541558Srgrimes
55598542Smckusick	lastbn = lblkno(&sblock, DIP(dp, di_size));
55698542Smckusick	if (lastbn >= NDADDR - 1 || DIP(dp, di_db[lastbn]) == 0 ||
55798542Smckusick	    DIP(dp, di_size) == 0)
5581558Srgrimes		return (0);
5591558Srgrimes	if ((newblk = allocblk(sblock.fs_frag)) == 0)
5601558Srgrimes		return (0);
561134589Sscottl	DIP_SET(dp, di_db[lastbn + 1], DIP(dp, di_db[lastbn]));
562134589Sscottl	DIP_SET(dp, di_db[lastbn], newblk);
563134589Sscottl	DIP_SET(dp, di_size, DIP(dp, di_size) + sblock.fs_bsize);
564134589Sscottl	DIP_SET(dp, di_blocks, DIP(dp, di_blocks) + btodb(sblock.fs_bsize));
56598542Smckusick	bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
56698542Smckusick		sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
5671558Srgrimes	if (bp->b_errs)
5681558Srgrimes		goto bad;
56923675Speter	memmove(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
5701558Srgrimes	bp = getdirblk(newblk, sblock.fs_bsize);
5711558Srgrimes	if (bp->b_errs)
5721558Srgrimes		goto bad;
57323675Speter	memmove(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
5741558Srgrimes	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
5751558Srgrimes	     cp < &bp->b_un.b_buf[sblock.fs_bsize];
5761558Srgrimes	     cp += DIRBLKSIZ)
57723675Speter		memmove(cp, &emptydir, sizeof emptydir);
5781558Srgrimes	dirty(bp);
57998542Smckusick	bp = getdirblk(DIP(dp, di_db[lastbn + 1]),
58098542Smckusick		sblksize(&sblock, DIP(dp, di_size), lastbn + 1));
5811558Srgrimes	if (bp->b_errs)
5821558Srgrimes		goto bad;
58323675Speter	memmove(bp->b_un.b_buf, &emptydir, sizeof emptydir);
5841558Srgrimes	pwarn("NO SPACE LEFT IN %s", name);
5851558Srgrimes	if (preen)
5861558Srgrimes		printf(" (EXPANDED)\n");
5871558Srgrimes	else if (reply("EXPAND") == 0)
5881558Srgrimes		goto bad;
5891558Srgrimes	dirty(bp);
5901558Srgrimes	inodirty();
5911558Srgrimes	return (1);
5921558Srgrimesbad:
593134589Sscottl	DIP_SET(dp, di_db[lastbn], DIP(dp, di_db[lastbn + 1]));
594134589Sscottl	DIP_SET(dp, di_db[lastbn + 1], 0);
595134589Sscottl	DIP_SET(dp, di_size, DIP(dp, di_size) - sblock.fs_bsize);
596134589Sscottl	DIP_SET(dp, di_blocks, DIP(dp, di_blocks) - btodb(sblock.fs_bsize));
5971558Srgrimes	freeblk(newblk, sblock.fs_frag);
5981558Srgrimes	return (0);
5991558Srgrimes}
6001558Srgrimes
6011558Srgrimes/*
6021558Srgrimes * allocate a new directory
6031558Srgrimes */
6047586Sbdeino_t
60592839Simpallocdir(ino_t parent, ino_t request, int mode)
6061558Srgrimes{
6071558Srgrimes	ino_t ino;
6081558Srgrimes	char *cp;
60998542Smckusick	union dinode *dp;
61063810Smckusick	struct bufarea *bp;
61163810Smckusick	struct inoinfo *inp;
6121558Srgrimes	struct dirtemplate *dirp;
6131558Srgrimes
6141558Srgrimes	ino = allocino(request, IFDIR|mode);
61596483Sphk	dirp = &dirhead;
6161558Srgrimes	dirp->dot_ino = ino;
6171558Srgrimes	dirp->dotdot_ino = parent;
6181558Srgrimes	dp = ginode(ino);
61998542Smckusick	bp = getdirblk(DIP(dp, di_db[0]), sblock.fs_fsize);
6201558Srgrimes	if (bp->b_errs) {
6211558Srgrimes		freeino(ino);
6221558Srgrimes		return (0);
6231558Srgrimes	}
62423675Speter	memmove(bp->b_un.b_buf, dirp, sizeof(struct dirtemplate));
6251558Srgrimes	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
6261558Srgrimes	     cp < &bp->b_un.b_buf[sblock.fs_fsize];
6271558Srgrimes	     cp += DIRBLKSIZ)
62823675Speter		memmove(cp, &emptydir, sizeof emptydir);
6291558Srgrimes	dirty(bp);
630134589Sscottl	DIP_SET(dp, di_nlink, 2);
6311558Srgrimes	inodirty();
6321558Srgrimes	if (ino == ROOTINO) {
63398542Smckusick		inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
6341558Srgrimes		cacheino(dp, ino);
6351558Srgrimes		return(ino);
6361558Srgrimes	}
637136281Struckman	if (!INO_IS_DVALID(parent)) {
6381558Srgrimes		freeino(ino);
6391558Srgrimes		return (0);
6401558Srgrimes	}
6411558Srgrimes	cacheino(dp, ino);
64263810Smckusick	inp = getinoinfo(ino);
64363810Smckusick	inp->i_parent = parent;
64463810Smckusick	inp->i_dotdot = parent;
64541474Sjulian	inoinfo(ino)->ino_state = inoinfo(parent)->ino_state;
64641474Sjulian	if (inoinfo(ino)->ino_state == DSTATE) {
64798542Smckusick		inoinfo(ino)->ino_linkcnt = DIP(dp, di_nlink);
64841474Sjulian		inoinfo(parent)->ino_linkcnt++;
6491558Srgrimes	}
6501558Srgrimes	dp = ginode(parent);
651134589Sscottl	DIP_SET(dp, di_nlink, DIP(dp, di_nlink) + 1);
6521558Srgrimes	inodirty();
6531558Srgrimes	return (ino);
6541558Srgrimes}
6551558Srgrimes
6561558Srgrimes/*
6571558Srgrimes * free a directory inode
6581558Srgrimes */
6597585Sbdestatic void
66092839Simpfreedir(ino_t ino, ino_t parent)
6611558Srgrimes{
66298542Smckusick	union dinode *dp;
6631558Srgrimes
6641558Srgrimes	if (ino != parent) {
6651558Srgrimes		dp = ginode(parent);
666134589Sscottl		DIP_SET(dp, di_nlink, DIP(dp, di_nlink) - 1);
6671558Srgrimes		inodirty();
6681558Srgrimes	}
6691558Srgrimes	freeino(ino);
6701558Srgrimes}
6711558Srgrimes
6721558Srgrimes/*
6731558Srgrimes * generate a temporary name for the lost+found directory.
6741558Srgrimes */
67523675Speterstatic int
67692839Simplftempname(char *bufp, ino_t ino)
6771558Srgrimes{
67892806Sobrien	ino_t in;
67992806Sobrien	char *cp;
6801558Srgrimes	int namlen;
6811558Srgrimes
6821558Srgrimes	cp = bufp + 2;
6831558Srgrimes	for (in = maxino; in > 0; in /= 10)
6841558Srgrimes		cp++;
6851558Srgrimes	*--cp = 0;
6861558Srgrimes	namlen = cp - bufp;
6871558Srgrimes	in = ino;
6881558Srgrimes	while (cp > bufp) {
6891558Srgrimes		*--cp = (in % 10) + '0';
6901558Srgrimes		in /= 10;
6911558Srgrimes	}
6921558Srgrimes	*cp = '#';
6931558Srgrimes	return (namlen);
6941558Srgrimes}
6951558Srgrimes
6961558Srgrimes/*
6971558Srgrimes * Get a directory block.
6981558Srgrimes * Insure that it is held until another is requested.
6991558Srgrimes */
70023675Speterstatic struct bufarea *
70198542Smckusickgetdirblk(ufs2_daddr_t blkno, long size)
7021558Srgrimes{
7031558Srgrimes
7041558Srgrimes	if (pdirbp != 0)
7051558Srgrimes		pdirbp->b_flags &= ~B_INUSE;
706247212Smckusick	pdirbp = getdatablk(blkno, size, BT_DIRDATA);
7071558Srgrimes	return (pdirbp);
7081558Srgrimes}
709