xref: /csrg-svn/sbin/fsck/dir.c (revision 67586)
122045Sdist /*
261492Sbostic  * Copyright (c) 1980, 1986, 1993
361492Sbostic  *	The Regents of the University of California.  All rights reserved.
439976Smckusick  *
542701Sbostic  * %sccs.include.redist.c%
622045Sdist  */
722045Sdist 
816259Smckusick #ifndef lint
9*67586Smckusick static char sccsid[] = "@(#)dir.c	8.2 (Berkeley) 08/04/94";
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;
14040570Smckusick 		fix = dofix(idesc, "DIRECTORY CORRUPTED");
14140570Smckusick 		bp = getdirblk(idesc->id_blkno, blksiz);
14240570Smckusick 		dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
14316259Smckusick 		dp->d_reclen = DIRBLKSIZ;
14416259Smckusick 		dp->d_ino = 0;
14554604Smckusick 		dp->d_type = 0;
14616259Smckusick 		dp->d_namlen = 0;
14716259Smckusick 		dp->d_name[0] = '\0';
14840570Smckusick 		if (fix)
14934225Smckusick 			dirty(bp);
15059302Smckusick 		idesc->id_loc += DIRBLKSIZ;
15159302Smckusick 		idesc->id_filesize -= DIRBLKSIZ;
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 */
305*67586Smckusick 	if (newinofmt) {
30654604Smckusick 		dirp->d_type = typemap[idesc->id_parent];
307*67586Smckusick 		dirp->d_namlen = newent.d_namlen;
308*67586Smckusick 	} else {
309*67586Smckusick #		if (BYTE_ORDER == LITTLE_ENDIAN)
310*67586Smckusick 			dirp->d_type = newent.d_namlen;
311*67586Smckusick 			dirp->d_namlen = 0;
312*67586Smckusick #		else
313*67586Smckusick 			dirp->d_type = 0;
314*67586Smckusick 			dirp->d_namlen = newent.d_namlen;
315*67586Smckusick #		endif
316*67586Smckusick 	}
31716259Smckusick 	dirp->d_reclen = newent.d_reclen;
318*67586Smckusick 	bcopy(idesc->id_name, dirp->d_name, (size_t)newent.d_namlen + 1);
31916259Smckusick 	return (ALTERED|STOP);
32016259Smckusick }
32116259Smckusick 
32217957Smckusick chgino(idesc)
32316259Smckusick 	struct inodesc *idesc;
32416259Smckusick {
32539973Smckusick 	register struct direct *dirp = idesc->id_dirp;
32616259Smckusick 
32739973Smckusick 	if (bcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
32817957Smckusick 		return (KEEPON);
32939973Smckusick 	dirp->d_ino = idesc->id_parent;
33054604Smckusick 	if (newinofmt)
33154604Smckusick 		dirp->d_type = typemap[idesc->id_parent];
33255074Smckusick 	else
33355074Smckusick 		dirp->d_type = 0;
33417957Smckusick 	return (ALTERED|STOP);
33516259Smckusick }
33616259Smckusick 
33739973Smckusick linkup(orphan, parentdir)
33816259Smckusick 	ino_t orphan;
33939973Smckusick 	ino_t parentdir;
34016259Smckusick {
34139973Smckusick 	register struct dinode *dp;
34240022Smckusick 	int lostdir;
34317954Smckusick 	ino_t oldlfdir;
34416259Smckusick 	struct inodesc idesc;
34517954Smckusick 	char tempname[BUFSIZ];
34617957Smckusick 	extern int pass4check();
34716259Smckusick 
34816259Smckusick 	bzero((char *)&idesc, sizeof(struct inodesc));
34917943Smckusick 	dp = ginode(orphan);
35039973Smckusick 	lostdir = (dp->di_mode & IFMT) == IFDIR;
35116259Smckusick 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
35216259Smckusick 	pinode(orphan);
35316259Smckusick 	if (preen && dp->di_size == 0)
35416259Smckusick 		return (0);
35516259Smckusick 	if (preen)
35616259Smckusick 		printf(" (RECONNECTED)\n");
35716259Smckusick 	else
35816259Smckusick 		if (reply("RECONNECT") == 0)
35916259Smckusick 			return (0);
36016259Smckusick 	if (lfdir == 0) {
36117943Smckusick 		dp = ginode(ROOTINO);
36217930Smckusick 		idesc.id_name = lfname;
36316259Smckusick 		idesc.id_type = DATA;
36416259Smckusick 		idesc.id_func = findino;
36516259Smckusick 		idesc.id_number = ROOTINO;
36630354Smckusick 		if ((ckinode(dp, &idesc) & FOUND) != 0) {
36717954Smckusick 			lfdir = idesc.id_parent;
36817954Smckusick 		} else {
36917954Smckusick 			pwarn("NO lost+found DIRECTORY");
37017954Smckusick 			if (preen || reply("CREATE")) {
37139973Smckusick 				lfdir = allocdir(ROOTINO, (ino_t)0, lfmode);
37217992Smckusick 				if (lfdir != 0) {
37317992Smckusick 					if (makeentry(ROOTINO, lfdir, lfname) != 0) {
37417954Smckusick 						if (preen)
37517954Smckusick 							printf(" (CREATED)\n");
37617954Smckusick 					} else {
37717992Smckusick 						freedir(lfdir, ROOTINO);
37817992Smckusick 						lfdir = 0;
37917954Smckusick 						if (preen)
38017954Smckusick 							printf("\n");
38117954Smckusick 					}
38217954Smckusick 				}
38317954Smckusick 			}
38417954Smckusick 		}
38517943Smckusick 		if (lfdir == 0) {
38617954Smckusick 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
38716259Smckusick 			printf("\n\n");
38816259Smckusick 			return (0);
38916259Smckusick 		}
39016259Smckusick 	}
39117943Smckusick 	dp = ginode(lfdir);
39239973Smckusick 	if ((dp->di_mode & IFMT) != IFDIR) {
39317957Smckusick 		pfatal("lost+found IS NOT A DIRECTORY");
39417957Smckusick 		if (reply("REALLOCATE") == 0)
39517957Smckusick 			return (0);
39617957Smckusick 		oldlfdir = lfdir;
39739973Smckusick 		if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) {
39817957Smckusick 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
39917957Smckusick 			return (0);
40017957Smckusick 		}
40140022Smckusick 		if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
40217957Smckusick 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
40317957Smckusick 			return (0);
40417957Smckusick 		}
40517957Smckusick 		inodirty();
40617957Smckusick 		idesc.id_type = ADDR;
40717957Smckusick 		idesc.id_func = pass4check;
40817957Smckusick 		idesc.id_number = oldlfdir;
40917957Smckusick 		adjust(&idesc, lncntp[oldlfdir] + 1);
41017957Smckusick 		lncntp[oldlfdir] = 0;
41117957Smckusick 		dp = ginode(lfdir);
41217957Smckusick 	}
41317957Smckusick 	if (statemap[lfdir] != DFOUND) {
41417957Smckusick 		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
41516259Smckusick 		return (0);
41616259Smckusick 	}
41740022Smckusick 	(void)lftempname(tempname, orphan);
41817992Smckusick 	if (makeentry(lfdir, orphan, tempname) == 0) {
41916259Smckusick 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
42016259Smckusick 		printf("\n\n");
42116259Smckusick 		return (0);
42216259Smckusick 	}
42316259Smckusick 	lncntp[orphan]--;
42416259Smckusick 	if (lostdir) {
42540022Smckusick 		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
42640022Smckusick 		    parentdir != (ino_t)-1)
42744934Smckusick 			(void)makeentry(orphan, lfdir, "..");
42817943Smckusick 		dp = ginode(lfdir);
42917943Smckusick 		dp->di_nlink++;
43017943Smckusick 		inodirty();
43117943Smckusick 		lncntp[lfdir]++;
43244934Smckusick 		pwarn("DIR I=%lu CONNECTED. ", orphan);
43340022Smckusick 		if (parentdir != (ino_t)-1)
43444934Smckusick 			printf("PARENT WAS I=%lu\n", parentdir);
43516259Smckusick 		if (preen == 0)
43616259Smckusick 			printf("\n");
43716259Smckusick 	}
43816259Smckusick 	return (1);
43916259Smckusick }
44016259Smckusick 
44116259Smckusick /*
44240022Smckusick  * fix an entry in a directory.
44340022Smckusick  */
44440022Smckusick changeino(dir, name, newnum)
44540022Smckusick 	ino_t dir;
44640022Smckusick 	char *name;
44740022Smckusick 	ino_t newnum;
44840022Smckusick {
44940022Smckusick 	struct inodesc idesc;
45040022Smckusick 
45140022Smckusick 	bzero((char *)&idesc, sizeof(struct inodesc));
45240022Smckusick 	idesc.id_type = DATA;
45340022Smckusick 	idesc.id_func = chgino;
45440022Smckusick 	idesc.id_number = dir;
45540022Smckusick 	idesc.id_fix = DONTKNOW;
45640022Smckusick 	idesc.id_name = name;
45740022Smckusick 	idesc.id_parent = newnum;	/* new value for name */
45840022Smckusick 	return (ckinode(ginode(dir), &idesc));
45940022Smckusick }
46040022Smckusick 
46140022Smckusick /*
46217944Smckusick  * make an entry in a directory
46317944Smckusick  */
46417992Smckusick makeentry(parent, ino, name)
46517992Smckusick 	ino_t parent, ino;
46617992Smckusick 	char *name;
46717992Smckusick {
46839973Smckusick 	struct dinode *dp;
46917992Smckusick 	struct inodesc idesc;
47040022Smckusick 	char pathbuf[MAXPATHLEN + 1];
47117944Smckusick 
47239973Smckusick 	if (parent < ROOTINO || parent >= maxino ||
47339973Smckusick 	    ino < ROOTINO || ino >= maxino)
47417992Smckusick 		return (0);
47539973Smckusick 	bzero((char *)&idesc, sizeof(struct inodesc));
47617992Smckusick 	idesc.id_type = DATA;
47717992Smckusick 	idesc.id_func = mkentry;
47817992Smckusick 	idesc.id_number = parent;
47917992Smckusick 	idesc.id_parent = ino;	/* this is the inode to enter */
48017992Smckusick 	idesc.id_fix = DONTKNOW;
48117992Smckusick 	idesc.id_name = name;
48217992Smckusick 	dp = ginode(parent);
48317993Smckusick 	if (dp->di_size % DIRBLKSIZ) {
48417993Smckusick 		dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
48517993Smckusick 		inodirty();
48617993Smckusick 	}
48717992Smckusick 	if ((ckinode(dp, &idesc) & ALTERED) != 0)
48817944Smckusick 		return (1);
48940022Smckusick 	getpathname(pathbuf, parent, parent);
49040022Smckusick 	dp = ginode(parent);
49140022Smckusick 	if (expanddir(dp, pathbuf) == 0)
49217944Smckusick 		return (0);
49317992Smckusick 	return (ckinode(dp, &idesc) & ALTERED);
49417944Smckusick }
49517944Smckusick 
49617944Smckusick /*
49717944Smckusick  * Attempt to expand the size of a directory
49817944Smckusick  */
49940022Smckusick expanddir(dp, name)
50039973Smckusick 	register struct dinode *dp;
50140022Smckusick 	char *name;
50217944Smckusick {
50317944Smckusick 	daddr_t lastbn, newblk;
50439973Smckusick 	register struct bufarea *bp;
50517944Smckusick 	char *cp, firstblk[DIRBLKSIZ];
50617944Smckusick 
50717944Smckusick 	lastbn = lblkno(&sblock, dp->di_size);
50841133Smckusick 	if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
50917944Smckusick 		return (0);
51017944Smckusick 	if ((newblk = allocblk(sblock.fs_frag)) == 0)
51117944Smckusick 		return (0);
51217944Smckusick 	dp->di_db[lastbn + 1] = dp->di_db[lastbn];
51317944Smckusick 	dp->di_db[lastbn] = newblk;
51417944Smckusick 	dp->di_size += sblock.fs_bsize;
51517944Smckusick 	dp->di_blocks += btodb(sblock.fs_bsize);
51634225Smckusick 	bp = getdirblk(dp->di_db[lastbn + 1],
51744934Smckusick 		(long)dblksize(&sblock, dp, lastbn + 1));
51838375Smckusick 	if (bp->b_errs)
51917944Smckusick 		goto bad;
52034225Smckusick 	bcopy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
52134225Smckusick 	bp = getdirblk(newblk, sblock.fs_bsize);
52238375Smckusick 	if (bp->b_errs)
52317944Smckusick 		goto bad;
52434225Smckusick 	bcopy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
52534225Smckusick 	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
52634225Smckusick 	     cp < &bp->b_un.b_buf[sblock.fs_bsize];
52717944Smckusick 	     cp += DIRBLKSIZ)
52817944Smckusick 		bcopy((char *)&emptydir, cp, sizeof emptydir);
52934225Smckusick 	dirty(bp);
53034225Smckusick 	bp = getdirblk(dp->di_db[lastbn + 1],
53144934Smckusick 		(long)dblksize(&sblock, dp, lastbn + 1));
53238375Smckusick 	if (bp->b_errs)
53317944Smckusick 		goto bad;
53434225Smckusick 	bcopy((char *)&emptydir, bp->b_un.b_buf, sizeof emptydir);
53540022Smckusick 	pwarn("NO SPACE LEFT IN %s", name);
53617944Smckusick 	if (preen)
53717944Smckusick 		printf(" (EXPANDED)\n");
53817944Smckusick 	else if (reply("EXPAND") == 0)
53917944Smckusick 		goto bad;
54034225Smckusick 	dirty(bp);
54117944Smckusick 	inodirty();
54217944Smckusick 	return (1);
54317944Smckusick bad:
54417944Smckusick 	dp->di_db[lastbn] = dp->di_db[lastbn + 1];
54517944Smckusick 	dp->di_db[lastbn + 1] = 0;
54617944Smckusick 	dp->di_size -= sblock.fs_bsize;
54717944Smckusick 	dp->di_blocks -= btodb(sblock.fs_bsize);
54817944Smckusick 	freeblk(newblk, sblock.fs_frag);
54917944Smckusick 	return (0);
55017944Smckusick }
55117944Smckusick 
55217944Smckusick /*
55317954Smckusick  * allocate a new directory
55417954Smckusick  */
55536827Smckusick allocdir(parent, request, mode)
55617954Smckusick 	ino_t parent, request;
55736827Smckusick 	int mode;
55817954Smckusick {
55917954Smckusick 	ino_t ino;
56017954Smckusick 	char *cp;
56139973Smckusick 	struct dinode *dp;
56239973Smckusick 	register struct bufarea *bp;
56354604Smckusick 	struct dirtemplate *dirp;
56417954Smckusick 
56536827Smckusick 	ino = allocino(request, IFDIR|mode);
56654604Smckusick 	if (newinofmt)
56754604Smckusick 		dirp = &dirhead;
56854604Smckusick 	else
56954604Smckusick 		dirp = (struct dirtemplate *)&odirhead;
57054604Smckusick 	dirp->dot_ino = ino;
57154604Smckusick 	dirp->dotdot_ino = parent;
57217954Smckusick 	dp = ginode(ino);
57334225Smckusick 	bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
57438375Smckusick 	if (bp->b_errs) {
57517954Smckusick 		freeino(ino);
57617954Smckusick 		return (0);
57717954Smckusick 	}
57854604Smckusick 	bcopy((char *)dirp, bp->b_un.b_buf, sizeof(struct dirtemplate));
57934225Smckusick 	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
58034225Smckusick 	     cp < &bp->b_un.b_buf[sblock.fs_fsize];
58117954Smckusick 	     cp += DIRBLKSIZ)
58217954Smckusick 		bcopy((char *)&emptydir, cp, sizeof emptydir);
58334225Smckusick 	dirty(bp);
58417954Smckusick 	dp->di_nlink = 2;
58517954Smckusick 	inodirty();
58617954Smckusick 	if (ino == ROOTINO) {
58717954Smckusick 		lncntp[ino] = dp->di_nlink;
58850573Smckusick 		cacheino(dp, ino);
58917954Smckusick 		return(ino);
59017954Smckusick 	}
59117954Smckusick 	if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
59217954Smckusick 		freeino(ino);
59317954Smckusick 		return (0);
59417954Smckusick 	}
59550573Smckusick 	cacheino(dp, ino);
59617954Smckusick 	statemap[ino] = statemap[parent];
59717954Smckusick 	if (statemap[ino] == DSTATE) {
59817954Smckusick 		lncntp[ino] = dp->di_nlink;
59917954Smckusick 		lncntp[parent]++;
60017954Smckusick 	}
60117954Smckusick 	dp = ginode(parent);
60217954Smckusick 	dp->di_nlink++;
60317954Smckusick 	inodirty();
60417954Smckusick 	return (ino);
60517954Smckusick }
60617954Smckusick 
60717954Smckusick /*
60817954Smckusick  * free a directory inode
60917954Smckusick  */
61017954Smckusick freedir(ino, parent)
61117954Smckusick 	ino_t ino, parent;
61217954Smckusick {
61339973Smckusick 	struct dinode *dp;
61417954Smckusick 
61517954Smckusick 	if (ino != parent) {
61617954Smckusick 		dp = ginode(parent);
61717954Smckusick 		dp->di_nlink--;
61817954Smckusick 		inodirty();
61917954Smckusick 	}
62017954Smckusick 	freeino(ino);
62117954Smckusick }
62217954Smckusick 
62317954Smckusick /*
62416259Smckusick  * generate a temporary name for the lost+found directory.
62516259Smckusick  */
62616259Smckusick lftempname(bufp, ino)
62716259Smckusick 	char *bufp;
62816259Smckusick 	ino_t ino;
62916259Smckusick {
63016259Smckusick 	register ino_t in;
63116259Smckusick 	register char *cp;
63216259Smckusick 	int namlen;
63316259Smckusick 
63416259Smckusick 	cp = bufp + 2;
63539973Smckusick 	for (in = maxino; in > 0; in /= 10)
63616259Smckusick 		cp++;
63716259Smckusick 	*--cp = 0;
63816259Smckusick 	namlen = cp - bufp;
63916259Smckusick 	in = ino;
64016259Smckusick 	while (cp > bufp) {
64116259Smckusick 		*--cp = (in % 10) + '0';
64216259Smckusick 		in /= 10;
64316259Smckusick 	}
64416259Smckusick 	*cp = '#';
64516259Smckusick 	return (namlen);
64616259Smckusick }
64734225Smckusick 
64834225Smckusick /*
64934225Smckusick  * Get a directory block.
65034225Smckusick  * Insure that it is held until another is requested.
65134225Smckusick  */
65239973Smckusick struct bufarea *
65334225Smckusick getdirblk(blkno, size)
65434225Smckusick 	daddr_t blkno;
65534225Smckusick 	long size;
65634225Smckusick {
65734225Smckusick 
65840648Smckusick 	if (pdirbp != 0)
65940648Smckusick 		pdirbp->b_flags &= ~B_INUSE;
66040648Smckusick 	pdirbp = getdatablk(blkno, size);
66140648Smckusick 	return (pdirbp);
66234225Smckusick }
663