xref: /csrg-svn/sbin/fsck/dir.c (revision 68061)
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*68061Smckusick static char sccsid[] = "@(#)dir.c	8.4 (Berkeley) 12/08/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;
14067863Smckusick 		if (idesc->id_fix == IGNORE)
14167863Smckusick 			return (0);
14240570Smckusick 		fix = dofix(idesc, "DIRECTORY CORRUPTED");
14340570Smckusick 		bp = getdirblk(idesc->id_blkno, blksiz);
14440570Smckusick 		dp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
14516259Smckusick 		dp->d_reclen = DIRBLKSIZ;
14616259Smckusick 		dp->d_ino = 0;
14754604Smckusick 		dp->d_type = 0;
14816259Smckusick 		dp->d_namlen = 0;
14916259Smckusick 		dp->d_name[0] = '\0';
15040570Smckusick 		if (fix)
15134225Smckusick 			dirty(bp);
15259302Smckusick 		idesc->id_loc += DIRBLKSIZ;
15359302Smckusick 		idesc->id_filesize -= DIRBLKSIZ;
15416259Smckusick 		return (dp);
15516259Smckusick 	}
15616259Smckusick dpok:
15716259Smckusick 	if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
15816259Smckusick 		return NULL;
15954604Smckusick 	dploc = idesc->id_loc;
16054604Smckusick 	dp = (struct direct *)(bp->b_un.b_buf + dploc);
16116259Smckusick 	idesc->id_loc += dp->d_reclen;
16216259Smckusick 	idesc->id_filesize -= dp->d_reclen;
16316259Smckusick 	if ((idesc->id_loc % DIRBLKSIZ) == 0)
16416259Smckusick 		return (dp);
16539973Smckusick 	ndp = (struct direct *)(bp->b_un.b_buf + idesc->id_loc);
16616259Smckusick 	if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
16716259Smckusick 	    dircheck(idesc, ndp) == 0) {
16816259Smckusick 		size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
16916259Smckusick 		idesc->id_loc += size;
17016259Smckusick 		idesc->id_filesize -= size;
17167863Smckusick 		if (idesc->id_fix == IGNORE)
17267863Smckusick 			return (0);
17340570Smckusick 		fix = dofix(idesc, "DIRECTORY CORRUPTED");
17440570Smckusick 		bp = getdirblk(idesc->id_blkno, blksiz);
17554604Smckusick 		dp = (struct direct *)(bp->b_un.b_buf + dploc);
17640570Smckusick 		dp->d_reclen += size;
17740570Smckusick 		if (fix)
17834225Smckusick 			dirty(bp);
17916259Smckusick 	}
18016259Smckusick 	return (dp);
18116259Smckusick }
18216259Smckusick 
18316259Smckusick /*
18416259Smckusick  * Verify that a directory entry is valid.
18516259Smckusick  * This is a superset of the checks made in the kernel.
18616259Smckusick  */
18716259Smckusick dircheck(idesc, dp)
18816259Smckusick 	struct inodesc *idesc;
18939973Smckusick 	register struct direct *dp;
19016259Smckusick {
19116259Smckusick 	register int size;
19216259Smckusick 	register char *cp;
19354604Smckusick 	u_char namlen, type;
19416259Smckusick 	int spaceleft;
19516259Smckusick 
19654604Smckusick 	size = DIRSIZ(!newinofmt, dp);
19716259Smckusick 	spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
19854604Smckusick #	if (BYTE_ORDER == LITTLE_ENDIAN)
19954604Smckusick 		if (!newinofmt) {
20054604Smckusick 			type = dp->d_namlen;
20154604Smckusick 			namlen = dp->d_type;
20254604Smckusick 		} else {
20354604Smckusick 			namlen = dp->d_namlen;
20454604Smckusick 			type = dp->d_type;
20554604Smckusick 		}
20654604Smckusick #	else
20754604Smckusick 		namlen = dp->d_namlen;
20854604Smckusick 		type = dp->d_type;
20954604Smckusick #	endif
21039973Smckusick 	if (dp->d_ino < maxino &&
21116259Smckusick 	    dp->d_reclen != 0 &&
21216259Smckusick 	    dp->d_reclen <= spaceleft &&
21316259Smckusick 	    (dp->d_reclen & 0x3) == 0 &&
21416259Smckusick 	    dp->d_reclen >= size &&
21516259Smckusick 	    idesc->id_filesize >= size &&
21654604Smckusick 	    namlen <= MAXNAMLEN &&
21754604Smckusick 	    type <= 15) {
21816259Smckusick 		if (dp->d_ino == 0)
21916259Smckusick 			return (1);
22054604Smckusick 		for (cp = dp->d_name, size = 0; size < namlen; size++)
22140651Smckusick 			if (*cp == 0 || (*cp++ == '/'))
22216259Smckusick 				return (0);
22316259Smckusick 		if (*cp == 0)
22416259Smckusick 			return (1);
22516259Smckusick 	}
22616259Smckusick 	return (0);
22716259Smckusick }
22816259Smckusick 
22939973Smckusick direrror(ino, errmesg)
23016259Smckusick 	ino_t ino;
23139973Smckusick 	char *errmesg;
23216259Smckusick {
23340022Smckusick 
23440022Smckusick 	fileerror(ino, ino, errmesg);
23540022Smckusick }
23640022Smckusick 
23740022Smckusick fileerror(cwd, ino, errmesg)
23840022Smckusick 	ino_t cwd, ino;
23940022Smckusick 	char *errmesg;
24040022Smckusick {
24139973Smckusick 	register struct dinode *dp;
24240022Smckusick 	char pathbuf[MAXPATHLEN + 1];
24316259Smckusick 
24439973Smckusick 	pwarn("%s ", errmesg);
24516259Smckusick 	pinode(ino);
24616259Smckusick 	printf("\n");
24740022Smckusick 	getpathname(pathbuf, cwd, ino);
24839973Smckusick 	if (ino < ROOTINO || ino > maxino) {
24940022Smckusick 		pfatal("NAME=%s\n", pathbuf);
25017943Smckusick 		return;
25117943Smckusick 	}
25217943Smckusick 	dp = ginode(ino);
25317943Smckusick 	if (ftypeok(dp))
25439973Smckusick 		pfatal("%s=%s\n",
25540022Smckusick 		    (dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE", pathbuf);
25616259Smckusick 	else
25740022Smckusick 		pfatal("NAME=%s\n", pathbuf);
25816259Smckusick }
25916259Smckusick 
26016259Smckusick adjust(idesc, lcnt)
26116259Smckusick 	register struct inodesc *idesc;
26216259Smckusick 	short lcnt;
26316259Smckusick {
26439973Smckusick 	register struct dinode *dp;
26516259Smckusick 
26617943Smckusick 	dp = ginode(idesc->id_number);
26716259Smckusick 	if (dp->di_nlink == lcnt) {
26816259Smckusick 		if (linkup(idesc->id_number, (ino_t)0) == 0)
26916259Smckusick 			clri(idesc, "UNREF", 0);
27017936Smckusick 	} else {
27117930Smckusick 		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
27239973Smckusick 			((dp->di_mode & IFMT) == IFDIR ? "DIR" : "FILE"));
27316259Smckusick 		pinode(idesc->id_number);
27416259Smckusick 		printf(" COUNT %d SHOULD BE %d",
27539973Smckusick 			dp->di_nlink, dp->di_nlink - lcnt);
27616259Smckusick 		if (preen) {
27716259Smckusick 			if (lcnt < 0) {
27816259Smckusick 				printf("\n");
27917930Smckusick 				pfatal("LINK COUNT INCREASING");
28016259Smckusick 			}
28116259Smckusick 			printf(" (ADJUSTED)\n");
28216259Smckusick 		}
28316259Smckusick 		if (preen || reply("ADJUST") == 1) {
28416259Smckusick 			dp->di_nlink -= lcnt;
28516259Smckusick 			inodirty();
28616259Smckusick 		}
28716259Smckusick 	}
28816259Smckusick }
28916259Smckusick 
29016259Smckusick mkentry(idesc)
29116259Smckusick 	struct inodesc *idesc;
29216259Smckusick {
29339973Smckusick 	register struct direct *dirp = idesc->id_dirp;
29439973Smckusick 	struct direct newent;
29516259Smckusick 	int newlen, oldlen;
29616259Smckusick 
29740022Smckusick 	newent.d_namlen = strlen(idesc->id_name);
29854604Smckusick 	newlen = DIRSIZ(0, &newent);
29916259Smckusick 	if (dirp->d_ino != 0)
30054604Smckusick 		oldlen = DIRSIZ(0, dirp);
30116259Smckusick 	else
30216259Smckusick 		oldlen = 0;
30316259Smckusick 	if (dirp->d_reclen - oldlen < newlen)
30416259Smckusick 		return (KEEPON);
30516259Smckusick 	newent.d_reclen = dirp->d_reclen - oldlen;
30616259Smckusick 	dirp->d_reclen = oldlen;
30716259Smckusick 	dirp = (struct direct *)(((char *)dirp) + oldlen);
30816259Smckusick 	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
309*68061Smckusick 	dirp->d_reclen = newent.d_reclen;
310*68061Smckusick 	if (newinofmt)
31154604Smckusick 		dirp->d_type = typemap[idesc->id_parent];
312*68061Smckusick 	else
313*68061Smckusick 		dirp->d_type = 0;
314*68061Smckusick 	dirp->d_namlen = newent.d_namlen;
31567586Smckusick 	bcopy(idesc->id_name, dirp->d_name, (size_t)newent.d_namlen + 1);
316*68061Smckusick #	if (BYTE_ORDER == LITTLE_ENDIAN)
317*68061Smckusick 		/*
318*68061Smckusick 		 * If the entry was split, dirscan() will only reverse the byte
319*68061Smckusick 		 * order of the original entry, and not the new one, before
320*68061Smckusick 		 * writing it back out.  So, we reverse the byte order here if
321*68061Smckusick 		 * necessary.
322*68061Smckusick 		 */
323*68061Smckusick 		if (oldlen != 0 && !newinofmt && !doinglevel2) {
324*68061Smckusick 			u_char tmp;
325*68061Smckusick 
326*68061Smckusick 			tmp = dirp->d_namlen;
327*68061Smckusick 			dirp->d_namlen = dirp->d_type;
328*68061Smckusick 			dirp->d_type = tmp;
329*68061Smckusick 		}
330*68061Smckusick #	endif
33116259Smckusick 	return (ALTERED|STOP);
33216259Smckusick }
33316259Smckusick 
33417957Smckusick chgino(idesc)
33516259Smckusick 	struct inodesc *idesc;
33616259Smckusick {
33739973Smckusick 	register struct direct *dirp = idesc->id_dirp;
33816259Smckusick 
33939973Smckusick 	if (bcmp(dirp->d_name, idesc->id_name, (int)dirp->d_namlen + 1))
34017957Smckusick 		return (KEEPON);
34139973Smckusick 	dirp->d_ino = idesc->id_parent;
34254604Smckusick 	if (newinofmt)
34354604Smckusick 		dirp->d_type = typemap[idesc->id_parent];
34455074Smckusick 	else
34555074Smckusick 		dirp->d_type = 0;
34617957Smckusick 	return (ALTERED|STOP);
34716259Smckusick }
34816259Smckusick 
34939973Smckusick linkup(orphan, parentdir)
35016259Smckusick 	ino_t orphan;
35139973Smckusick 	ino_t parentdir;
35216259Smckusick {
35339973Smckusick 	register struct dinode *dp;
35440022Smckusick 	int lostdir;
35517954Smckusick 	ino_t oldlfdir;
35616259Smckusick 	struct inodesc idesc;
35717954Smckusick 	char tempname[BUFSIZ];
35817957Smckusick 	extern int pass4check();
35916259Smckusick 
36016259Smckusick 	bzero((char *)&idesc, sizeof(struct inodesc));
36117943Smckusick 	dp = ginode(orphan);
36239973Smckusick 	lostdir = (dp->di_mode & IFMT) == IFDIR;
36316259Smckusick 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
36416259Smckusick 	pinode(orphan);
36516259Smckusick 	if (preen && dp->di_size == 0)
36616259Smckusick 		return (0);
36716259Smckusick 	if (preen)
36816259Smckusick 		printf(" (RECONNECTED)\n");
36916259Smckusick 	else
37016259Smckusick 		if (reply("RECONNECT") == 0)
37116259Smckusick 			return (0);
37216259Smckusick 	if (lfdir == 0) {
37317943Smckusick 		dp = ginode(ROOTINO);
37417930Smckusick 		idesc.id_name = lfname;
37516259Smckusick 		idesc.id_type = DATA;
37616259Smckusick 		idesc.id_func = findino;
37716259Smckusick 		idesc.id_number = ROOTINO;
37830354Smckusick 		if ((ckinode(dp, &idesc) & FOUND) != 0) {
37917954Smckusick 			lfdir = idesc.id_parent;
38017954Smckusick 		} else {
38117954Smckusick 			pwarn("NO lost+found DIRECTORY");
38217954Smckusick 			if (preen || reply("CREATE")) {
38339973Smckusick 				lfdir = allocdir(ROOTINO, (ino_t)0, lfmode);
38417992Smckusick 				if (lfdir != 0) {
38517992Smckusick 					if (makeentry(ROOTINO, lfdir, lfname) != 0) {
38617954Smckusick 						if (preen)
38717954Smckusick 							printf(" (CREATED)\n");
38817954Smckusick 					} else {
38917992Smckusick 						freedir(lfdir, ROOTINO);
39017992Smckusick 						lfdir = 0;
39117954Smckusick 						if (preen)
39217954Smckusick 							printf("\n");
39317954Smckusick 					}
39417954Smckusick 				}
39517954Smckusick 			}
39617954Smckusick 		}
39717943Smckusick 		if (lfdir == 0) {
39817954Smckusick 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
39916259Smckusick 			printf("\n\n");
40016259Smckusick 			return (0);
40116259Smckusick 		}
40216259Smckusick 	}
40317943Smckusick 	dp = ginode(lfdir);
40439973Smckusick 	if ((dp->di_mode & IFMT) != IFDIR) {
40517957Smckusick 		pfatal("lost+found IS NOT A DIRECTORY");
40617957Smckusick 		if (reply("REALLOCATE") == 0)
40717957Smckusick 			return (0);
40817957Smckusick 		oldlfdir = lfdir;
40939973Smckusick 		if ((lfdir = allocdir(ROOTINO, (ino_t)0, lfmode)) == 0) {
41017957Smckusick 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
41117957Smckusick 			return (0);
41217957Smckusick 		}
41340022Smckusick 		if ((changeino(ROOTINO, lfname, lfdir) & ALTERED) == 0) {
41417957Smckusick 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
41517957Smckusick 			return (0);
41617957Smckusick 		}
41717957Smckusick 		inodirty();
41817957Smckusick 		idesc.id_type = ADDR;
41917957Smckusick 		idesc.id_func = pass4check;
42017957Smckusick 		idesc.id_number = oldlfdir;
42117957Smckusick 		adjust(&idesc, lncntp[oldlfdir] + 1);
42217957Smckusick 		lncntp[oldlfdir] = 0;
42317957Smckusick 		dp = ginode(lfdir);
42417957Smckusick 	}
42517957Smckusick 	if (statemap[lfdir] != DFOUND) {
42617957Smckusick 		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
42716259Smckusick 		return (0);
42816259Smckusick 	}
42940022Smckusick 	(void)lftempname(tempname, orphan);
43017992Smckusick 	if (makeentry(lfdir, orphan, tempname) == 0) {
43116259Smckusick 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
43216259Smckusick 		printf("\n\n");
43316259Smckusick 		return (0);
43416259Smckusick 	}
43516259Smckusick 	lncntp[orphan]--;
43616259Smckusick 	if (lostdir) {
43740022Smckusick 		if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 &&
43840022Smckusick 		    parentdir != (ino_t)-1)
43944934Smckusick 			(void)makeentry(orphan, lfdir, "..");
44017943Smckusick 		dp = ginode(lfdir);
44117943Smckusick 		dp->di_nlink++;
44217943Smckusick 		inodirty();
44317943Smckusick 		lncntp[lfdir]++;
44444934Smckusick 		pwarn("DIR I=%lu CONNECTED. ", orphan);
44540022Smckusick 		if (parentdir != (ino_t)-1)
44644934Smckusick 			printf("PARENT WAS I=%lu\n", parentdir);
44716259Smckusick 		if (preen == 0)
44816259Smckusick 			printf("\n");
44916259Smckusick 	}
45016259Smckusick 	return (1);
45116259Smckusick }
45216259Smckusick 
45316259Smckusick /*
45440022Smckusick  * fix an entry in a directory.
45540022Smckusick  */
45640022Smckusick changeino(dir, name, newnum)
45740022Smckusick 	ino_t dir;
45840022Smckusick 	char *name;
45940022Smckusick 	ino_t newnum;
46040022Smckusick {
46140022Smckusick 	struct inodesc idesc;
46240022Smckusick 
46340022Smckusick 	bzero((char *)&idesc, sizeof(struct inodesc));
46440022Smckusick 	idesc.id_type = DATA;
46540022Smckusick 	idesc.id_func = chgino;
46640022Smckusick 	idesc.id_number = dir;
46740022Smckusick 	idesc.id_fix = DONTKNOW;
46840022Smckusick 	idesc.id_name = name;
46940022Smckusick 	idesc.id_parent = newnum;	/* new value for name */
47040022Smckusick 	return (ckinode(ginode(dir), &idesc));
47140022Smckusick }
47240022Smckusick 
47340022Smckusick /*
47417944Smckusick  * make an entry in a directory
47517944Smckusick  */
47617992Smckusick makeentry(parent, ino, name)
47717992Smckusick 	ino_t parent, ino;
47817992Smckusick 	char *name;
47917992Smckusick {
48039973Smckusick 	struct dinode *dp;
48117992Smckusick 	struct inodesc idesc;
48240022Smckusick 	char pathbuf[MAXPATHLEN + 1];
48317944Smckusick 
48439973Smckusick 	if (parent < ROOTINO || parent >= maxino ||
48539973Smckusick 	    ino < ROOTINO || ino >= maxino)
48617992Smckusick 		return (0);
48739973Smckusick 	bzero((char *)&idesc, sizeof(struct inodesc));
48817992Smckusick 	idesc.id_type = DATA;
48917992Smckusick 	idesc.id_func = mkentry;
49017992Smckusick 	idesc.id_number = parent;
49117992Smckusick 	idesc.id_parent = ino;	/* this is the inode to enter */
49217992Smckusick 	idesc.id_fix = DONTKNOW;
49317992Smckusick 	idesc.id_name = name;
49417992Smckusick 	dp = ginode(parent);
49517993Smckusick 	if (dp->di_size % DIRBLKSIZ) {
49617993Smckusick 		dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
49717993Smckusick 		inodirty();
49817993Smckusick 	}
49917992Smckusick 	if ((ckinode(dp, &idesc) & ALTERED) != 0)
50017944Smckusick 		return (1);
50140022Smckusick 	getpathname(pathbuf, parent, parent);
50240022Smckusick 	dp = ginode(parent);
50340022Smckusick 	if (expanddir(dp, pathbuf) == 0)
50417944Smckusick 		return (0);
50517992Smckusick 	return (ckinode(dp, &idesc) & ALTERED);
50617944Smckusick }
50717944Smckusick 
50817944Smckusick /*
50917944Smckusick  * Attempt to expand the size of a directory
51017944Smckusick  */
51140022Smckusick expanddir(dp, name)
51239973Smckusick 	register struct dinode *dp;
51340022Smckusick 	char *name;
51417944Smckusick {
51517944Smckusick 	daddr_t lastbn, newblk;
51639973Smckusick 	register struct bufarea *bp;
51717944Smckusick 	char *cp, firstblk[DIRBLKSIZ];
51817944Smckusick 
51917944Smckusick 	lastbn = lblkno(&sblock, dp->di_size);
52041133Smckusick 	if (lastbn >= NDADDR - 1 || dp->di_db[lastbn] == 0 || dp->di_size == 0)
52117944Smckusick 		return (0);
52217944Smckusick 	if ((newblk = allocblk(sblock.fs_frag)) == 0)
52317944Smckusick 		return (0);
52417944Smckusick 	dp->di_db[lastbn + 1] = dp->di_db[lastbn];
52517944Smckusick 	dp->di_db[lastbn] = newblk;
52617944Smckusick 	dp->di_size += sblock.fs_bsize;
52717944Smckusick 	dp->di_blocks += btodb(sblock.fs_bsize);
52834225Smckusick 	bp = getdirblk(dp->di_db[lastbn + 1],
52944934Smckusick 		(long)dblksize(&sblock, dp, lastbn + 1));
53038375Smckusick 	if (bp->b_errs)
53117944Smckusick 		goto bad;
53234225Smckusick 	bcopy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
53334225Smckusick 	bp = getdirblk(newblk, sblock.fs_bsize);
53438375Smckusick 	if (bp->b_errs)
53517944Smckusick 		goto bad;
53634225Smckusick 	bcopy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
53734225Smckusick 	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
53834225Smckusick 	     cp < &bp->b_un.b_buf[sblock.fs_bsize];
53917944Smckusick 	     cp += DIRBLKSIZ)
54017944Smckusick 		bcopy((char *)&emptydir, cp, sizeof emptydir);
54134225Smckusick 	dirty(bp);
54234225Smckusick 	bp = getdirblk(dp->di_db[lastbn + 1],
54344934Smckusick 		(long)dblksize(&sblock, dp, lastbn + 1));
54438375Smckusick 	if (bp->b_errs)
54517944Smckusick 		goto bad;
54634225Smckusick 	bcopy((char *)&emptydir, bp->b_un.b_buf, sizeof emptydir);
54740022Smckusick 	pwarn("NO SPACE LEFT IN %s", name);
54817944Smckusick 	if (preen)
54917944Smckusick 		printf(" (EXPANDED)\n");
55017944Smckusick 	else if (reply("EXPAND") == 0)
55117944Smckusick 		goto bad;
55234225Smckusick 	dirty(bp);
55317944Smckusick 	inodirty();
55417944Smckusick 	return (1);
55517944Smckusick bad:
55617944Smckusick 	dp->di_db[lastbn] = dp->di_db[lastbn + 1];
55717944Smckusick 	dp->di_db[lastbn + 1] = 0;
55817944Smckusick 	dp->di_size -= sblock.fs_bsize;
55917944Smckusick 	dp->di_blocks -= btodb(sblock.fs_bsize);
56017944Smckusick 	freeblk(newblk, sblock.fs_frag);
56117944Smckusick 	return (0);
56217944Smckusick }
56317944Smckusick 
56417944Smckusick /*
56517954Smckusick  * allocate a new directory
56617954Smckusick  */
56736827Smckusick allocdir(parent, request, mode)
56817954Smckusick 	ino_t parent, request;
56936827Smckusick 	int mode;
57017954Smckusick {
57117954Smckusick 	ino_t ino;
57217954Smckusick 	char *cp;
57339973Smckusick 	struct dinode *dp;
57439973Smckusick 	register struct bufarea *bp;
57554604Smckusick 	struct dirtemplate *dirp;
57617954Smckusick 
57736827Smckusick 	ino = allocino(request, IFDIR|mode);
57854604Smckusick 	if (newinofmt)
57954604Smckusick 		dirp = &dirhead;
58054604Smckusick 	else
58154604Smckusick 		dirp = (struct dirtemplate *)&odirhead;
58254604Smckusick 	dirp->dot_ino = ino;
58354604Smckusick 	dirp->dotdot_ino = parent;
58417954Smckusick 	dp = ginode(ino);
58534225Smckusick 	bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
58638375Smckusick 	if (bp->b_errs) {
58717954Smckusick 		freeino(ino);
58817954Smckusick 		return (0);
58917954Smckusick 	}
59054604Smckusick 	bcopy((char *)dirp, bp->b_un.b_buf, sizeof(struct dirtemplate));
59134225Smckusick 	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
59234225Smckusick 	     cp < &bp->b_un.b_buf[sblock.fs_fsize];
59317954Smckusick 	     cp += DIRBLKSIZ)
59417954Smckusick 		bcopy((char *)&emptydir, cp, sizeof emptydir);
59534225Smckusick 	dirty(bp);
59617954Smckusick 	dp->di_nlink = 2;
59717954Smckusick 	inodirty();
59817954Smckusick 	if (ino == ROOTINO) {
59917954Smckusick 		lncntp[ino] = dp->di_nlink;
60050573Smckusick 		cacheino(dp, ino);
60117954Smckusick 		return(ino);
60217954Smckusick 	}
60317954Smckusick 	if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
60417954Smckusick 		freeino(ino);
60517954Smckusick 		return (0);
60617954Smckusick 	}
60750573Smckusick 	cacheino(dp, ino);
60817954Smckusick 	statemap[ino] = statemap[parent];
60917954Smckusick 	if (statemap[ino] == DSTATE) {
61017954Smckusick 		lncntp[ino] = dp->di_nlink;
61117954Smckusick 		lncntp[parent]++;
61217954Smckusick 	}
61317954Smckusick 	dp = ginode(parent);
61417954Smckusick 	dp->di_nlink++;
61517954Smckusick 	inodirty();
61617954Smckusick 	return (ino);
61717954Smckusick }
61817954Smckusick 
61917954Smckusick /*
62017954Smckusick  * free a directory inode
62117954Smckusick  */
62217954Smckusick freedir(ino, parent)
62317954Smckusick 	ino_t ino, parent;
62417954Smckusick {
62539973Smckusick 	struct dinode *dp;
62617954Smckusick 
62717954Smckusick 	if (ino != parent) {
62817954Smckusick 		dp = ginode(parent);
62917954Smckusick 		dp->di_nlink--;
63017954Smckusick 		inodirty();
63117954Smckusick 	}
63217954Smckusick 	freeino(ino);
63317954Smckusick }
63417954Smckusick 
63517954Smckusick /*
63616259Smckusick  * generate a temporary name for the lost+found directory.
63716259Smckusick  */
63816259Smckusick lftempname(bufp, ino)
63916259Smckusick 	char *bufp;
64016259Smckusick 	ino_t ino;
64116259Smckusick {
64216259Smckusick 	register ino_t in;
64316259Smckusick 	register char *cp;
64416259Smckusick 	int namlen;
64516259Smckusick 
64616259Smckusick 	cp = bufp + 2;
64739973Smckusick 	for (in = maxino; in > 0; in /= 10)
64816259Smckusick 		cp++;
64916259Smckusick 	*--cp = 0;
65016259Smckusick 	namlen = cp - bufp;
65116259Smckusick 	in = ino;
65216259Smckusick 	while (cp > bufp) {
65316259Smckusick 		*--cp = (in % 10) + '0';
65416259Smckusick 		in /= 10;
65516259Smckusick 	}
65616259Smckusick 	*cp = '#';
65716259Smckusick 	return (namlen);
65816259Smckusick }
65934225Smckusick 
66034225Smckusick /*
66134225Smckusick  * Get a directory block.
66234225Smckusick  * Insure that it is held until another is requested.
66334225Smckusick  */
66439973Smckusick struct bufarea *
66534225Smckusick getdirblk(blkno, size)
66634225Smckusick 	daddr_t blkno;
66734225Smckusick 	long size;
66834225Smckusick {
66934225Smckusick 
67040648Smckusick 	if (pdirbp != 0)
67140648Smckusick 		pdirbp->b_flags &= ~B_INUSE;
67240648Smckusick 	pdirbp = getdatablk(blkno, size);
67340648Smckusick 	return (pdirbp);
67434225Smckusick }
675