xref: /csrg-svn/sbin/fsck/dir.c (revision 39383)
122045Sdist /*
222045Sdist  * Copyright (c) 1980 Regents of the University of California.
322045Sdist  * All rights reserved.  The Berkeley software License Agreement
422045Sdist  * specifies the terms and conditions for redistribution.
522045Sdist  */
622045Sdist 
716259Smckusick #ifndef lint
8*39383Smckusick static char sccsid[] = "@(#)dir.c	5.8 (Berkeley) 10/24/89";
922045Sdist #endif not lint
1016259Smckusick 
1116259Smckusick #include <sys/param.h>
12*39383Smckusick #include <ufs/dinode.h>
1338336Smckusick #include <ufs/fs.h>
1416259Smckusick #define KERNEL
1538336Smckusick #include <ufs/dir.h>
1616259Smckusick #undef KERNEL
1716259Smckusick #include "fsck.h"
1816259Smckusick 
1916259Smckusick #define MINDIRSIZE	(sizeof (struct dirtemplate))
2016259Smckusick 
2116259Smckusick char	*endpathname = &pathname[BUFSIZ - 2];
2216259Smckusick char	*lfname = "lost+found";
2336827Smckusick int	lfmode = 01777;
2417944Smckusick struct	dirtemplate emptydir = { 0, DIRBLKSIZ };
2517954Smckusick struct	dirtemplate dirhead = { 0, 12, 1, ".", 0, DIRBLKSIZ - 12, 2, ".." };
2616259Smckusick 
2716259Smckusick DIRECT	*fsck_readdir();
2834225Smckusick BUFAREA	*getdirblk();
2916259Smckusick 
3016259Smckusick descend(parentino, inumber)
3116259Smckusick 	struct inodesc *parentino;
3216259Smckusick 	ino_t inumber;
3316259Smckusick {
3416259Smckusick 	register DINODE *dp;
3516259Smckusick 	struct inodesc curino;
3616259Smckusick 
3716259Smckusick 	bzero((char *)&curino, sizeof(struct inodesc));
3817936Smckusick 	if (statemap[inumber] != DSTATE)
3917936Smckusick 		errexit("BAD INODE %d TO DESCEND", statemap[inumber]);
4017936Smckusick 	statemap[inumber] = DFOUND;
4117943Smckusick 	dp = ginode(inumber);
4216259Smckusick 	if (dp->di_size == 0) {
4316259Smckusick 		direrr(inumber, "ZERO LENGTH DIRECTORY");
4416259Smckusick 		if (reply("REMOVE") == 1)
4517936Smckusick 			statemap[inumber] = DCLEAR;
4616259Smckusick 		return;
4716259Smckusick 	}
4816259Smckusick 	if (dp->di_size < MINDIRSIZE) {
4916259Smckusick 		direrr(inumber, "DIRECTORY TOO SHORT");
5016259Smckusick 		dp->di_size = MINDIRSIZE;
5116259Smckusick 		if (reply("FIX") == 1)
5216259Smckusick 			inodirty();
5316259Smckusick 	}
5421742Smckusick 	if ((dp->di_size & (DIRBLKSIZ - 1)) != 0) {
5521742Smckusick 		pwarn("DIRECTORY %s: LENGTH %d NOT MULTIPLE OF %d",
5621742Smckusick 			pathname, dp->di_size, DIRBLKSIZ);
5721742Smckusick 		dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
5821742Smckusick 		if (preen)
5921742Smckusick 			printf(" (ADJUSTED)\n");
6021742Smckusick 		if (preen || reply("ADJUST") == 1)
6121742Smckusick 			inodirty();
6221742Smckusick 	}
6316259Smckusick 	curino.id_type = DATA;
6416259Smckusick 	curino.id_func = parentino->id_func;
6516259Smckusick 	curino.id_parent = parentino->id_number;
6616259Smckusick 	curino.id_number = inumber;
6716259Smckusick 	(void)ckinode(dp, &curino);
6830354Smckusick 	if (curino.id_entryno < 2) {
6930354Smckusick 		direrr(inumber, "NULL DIRECTORY");
7030354Smckusick 		if (reply("REMOVE") == 1)
7130354Smckusick 			statemap[inumber] = DCLEAR;
7230354Smckusick 	}
7316259Smckusick }
7416259Smckusick 
7516259Smckusick dirscan(idesc)
7616259Smckusick 	register struct inodesc *idesc;
7716259Smckusick {
7816259Smckusick 	register DIRECT *dp;
7934225Smckusick 	register BUFAREA *bp;
8016259Smckusick 	int dsize, n;
8116259Smckusick 	long blksiz;
8216259Smckusick 	char dbuf[DIRBLKSIZ];
8316259Smckusick 
8416259Smckusick 	if (idesc->id_type != DATA)
8516259Smckusick 		errexit("wrong type to dirscan %d\n", idesc->id_type);
8617993Smckusick 	if (idesc->id_entryno == 0 &&
8717993Smckusick 	    (idesc->id_filesize & (DIRBLKSIZ - 1)) != 0)
8817993Smckusick 		idesc->id_filesize = roundup(idesc->id_filesize, DIRBLKSIZ);
8916259Smckusick 	blksiz = idesc->id_numfrags * sblock.fs_fsize;
9016259Smckusick 	if (outrange(idesc->id_blkno, idesc->id_numfrags)) {
9116259Smckusick 		idesc->id_filesize -= blksiz;
9216259Smckusick 		return (SKIP);
9316259Smckusick 	}
9416259Smckusick 	idesc->id_loc = 0;
9516259Smckusick 	for (dp = fsck_readdir(idesc); dp != NULL; dp = fsck_readdir(idesc)) {
9616259Smckusick 		dsize = dp->d_reclen;
9716259Smckusick 		bcopy((char *)dp, dbuf, dsize);
9816259Smckusick 		idesc->id_dirp = (DIRECT *)dbuf;
9916259Smckusick 		if ((n = (*idesc->id_func)(idesc)) & ALTERED) {
10034225Smckusick 			bp = getdirblk(idesc->id_blkno, blksiz);
10130354Smckusick 			bcopy(dbuf, (char *)dp, dsize);
10234225Smckusick 			dirty(bp);
10330354Smckusick 			sbdirty();
10416259Smckusick 		}
10516259Smckusick 		if (n & STOP)
10616259Smckusick 			return (n);
10716259Smckusick 	}
10816259Smckusick 	return (idesc->id_filesize > 0 ? KEEPON : STOP);
10916259Smckusick }
11016259Smckusick 
11116259Smckusick /*
11216259Smckusick  * get next entry in a directory.
11316259Smckusick  */
11416259Smckusick DIRECT *
11516259Smckusick fsck_readdir(idesc)
11616259Smckusick 	register struct inodesc *idesc;
11716259Smckusick {
11816259Smckusick 	register DIRECT *dp, *ndp;
11934225Smckusick 	register BUFAREA *bp;
12016259Smckusick 	long size, blksiz;
12116259Smckusick 
12216259Smckusick 	blksiz = idesc->id_numfrags * sblock.fs_fsize;
12334225Smckusick 	bp = getdirblk(idesc->id_blkno, blksiz);
12416259Smckusick 	if (idesc->id_loc % DIRBLKSIZ == 0 && idesc->id_filesize > 0 &&
12516259Smckusick 	    idesc->id_loc < blksiz) {
12634225Smckusick 		dp = (DIRECT *)(bp->b_un.b_buf + idesc->id_loc);
12716259Smckusick 		if (dircheck(idesc, dp))
12816259Smckusick 			goto dpok;
12916259Smckusick 		idesc->id_loc += DIRBLKSIZ;
13016259Smckusick 		idesc->id_filesize -= DIRBLKSIZ;
13116259Smckusick 		dp->d_reclen = DIRBLKSIZ;
13216259Smckusick 		dp->d_ino = 0;
13316259Smckusick 		dp->d_namlen = 0;
13416259Smckusick 		dp->d_name[0] = '\0';
13517930Smckusick 		if (dofix(idesc, "DIRECTORY CORRUPTED"))
13634225Smckusick 			dirty(bp);
13716259Smckusick 		return (dp);
13816259Smckusick 	}
13916259Smckusick dpok:
14016259Smckusick 	if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz)
14116259Smckusick 		return NULL;
14234225Smckusick 	dp = (DIRECT *)(bp->b_un.b_buf + idesc->id_loc);
14316259Smckusick 	idesc->id_loc += dp->d_reclen;
14416259Smckusick 	idesc->id_filesize -= dp->d_reclen;
14516259Smckusick 	if ((idesc->id_loc % DIRBLKSIZ) == 0)
14616259Smckusick 		return (dp);
14734225Smckusick 	ndp = (DIRECT *)(bp->b_un.b_buf + idesc->id_loc);
14816259Smckusick 	if (idesc->id_loc < blksiz && idesc->id_filesize > 0 &&
14916259Smckusick 	    dircheck(idesc, ndp) == 0) {
15016259Smckusick 		size = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
15116259Smckusick 		dp->d_reclen += size;
15216259Smckusick 		idesc->id_loc += size;
15316259Smckusick 		idesc->id_filesize -= size;
15417930Smckusick 		if (dofix(idesc, "DIRECTORY CORRUPTED"))
15534225Smckusick 			dirty(bp);
15616259Smckusick 	}
15716259Smckusick 	return (dp);
15816259Smckusick }
15916259Smckusick 
16016259Smckusick /*
16116259Smckusick  * Verify that a directory entry is valid.
16216259Smckusick  * This is a superset of the checks made in the kernel.
16316259Smckusick  */
16416259Smckusick dircheck(idesc, dp)
16516259Smckusick 	struct inodesc *idesc;
16616259Smckusick 	register DIRECT *dp;
16716259Smckusick {
16816259Smckusick 	register int size;
16916259Smckusick 	register char *cp;
17016259Smckusick 	int spaceleft;
17116259Smckusick 
17216259Smckusick 	size = DIRSIZ(dp);
17316259Smckusick 	spaceleft = DIRBLKSIZ - (idesc->id_loc % DIRBLKSIZ);
17416259Smckusick 	if (dp->d_ino < imax &&
17516259Smckusick 	    dp->d_reclen != 0 &&
17616259Smckusick 	    dp->d_reclen <= spaceleft &&
17716259Smckusick 	    (dp->d_reclen & 0x3) == 0 &&
17816259Smckusick 	    dp->d_reclen >= size &&
17916259Smckusick 	    idesc->id_filesize >= size &&
18016259Smckusick 	    dp->d_namlen <= MAXNAMLEN) {
18116259Smckusick 		if (dp->d_ino == 0)
18216259Smckusick 			return (1);
18316259Smckusick 		for (cp = dp->d_name, size = 0; size < dp->d_namlen; size++)
18416259Smckusick 			if (*cp == 0 || (*cp++ & 0200))
18516259Smckusick 				return (0);
18616259Smckusick 		if (*cp == 0)
18716259Smckusick 			return (1);
18816259Smckusick 	}
18916259Smckusick 	return (0);
19016259Smckusick }
19116259Smckusick 
19216259Smckusick direrr(ino, s)
19316259Smckusick 	ino_t ino;
19416259Smckusick 	char *s;
19516259Smckusick {
19616259Smckusick 	register DINODE *dp;
19716259Smckusick 
19816259Smckusick 	pwarn("%s ", s);
19916259Smckusick 	pinode(ino);
20016259Smckusick 	printf("\n");
20117943Smckusick 	if (ino < ROOTINO || ino > imax) {
20217943Smckusick 		pfatal("NAME=%s\n", pathname);
20317943Smckusick 		return;
20417943Smckusick 	}
20517943Smckusick 	dp = ginode(ino);
20617943Smckusick 	if (ftypeok(dp))
20717930Smckusick 		pfatal("%s=%s\n", DIRCT(dp) ? "DIR" : "FILE", pathname);
20816259Smckusick 	else
20916259Smckusick 		pfatal("NAME=%s\n", pathname);
21016259Smckusick }
21116259Smckusick 
21216259Smckusick adjust(idesc, lcnt)
21316259Smckusick 	register struct inodesc *idesc;
21416259Smckusick 	short lcnt;
21516259Smckusick {
21616259Smckusick 	register DINODE *dp;
21716259Smckusick 
21817943Smckusick 	dp = ginode(idesc->id_number);
21916259Smckusick 	if (dp->di_nlink == lcnt) {
22016259Smckusick 		if (linkup(idesc->id_number, (ino_t)0) == 0)
22116259Smckusick 			clri(idesc, "UNREF", 0);
22217936Smckusick 	} else {
22317930Smckusick 		pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname :
22417930Smckusick 			(DIRCT(dp) ? "DIR" : "FILE"));
22516259Smckusick 		pinode(idesc->id_number);
22616259Smckusick 		printf(" COUNT %d SHOULD BE %d",
22716259Smckusick 			dp->di_nlink, dp->di_nlink-lcnt);
22816259Smckusick 		if (preen) {
22916259Smckusick 			if (lcnt < 0) {
23016259Smckusick 				printf("\n");
23117930Smckusick 				pfatal("LINK COUNT INCREASING");
23216259Smckusick 			}
23316259Smckusick 			printf(" (ADJUSTED)\n");
23416259Smckusick 		}
23516259Smckusick 		if (preen || reply("ADJUST") == 1) {
23616259Smckusick 			dp->di_nlink -= lcnt;
23716259Smckusick 			inodirty();
23816259Smckusick 		}
23916259Smckusick 	}
24016259Smckusick }
24116259Smckusick 
24216259Smckusick mkentry(idesc)
24316259Smckusick 	struct inodesc *idesc;
24416259Smckusick {
24516259Smckusick 	register DIRECT *dirp = idesc->id_dirp;
24616259Smckusick 	DIRECT newent;
24716259Smckusick 	int newlen, oldlen;
24816259Smckusick 
24916259Smckusick 	newent.d_namlen = 11;
25016259Smckusick 	newlen = DIRSIZ(&newent);
25116259Smckusick 	if (dirp->d_ino != 0)
25216259Smckusick 		oldlen = DIRSIZ(dirp);
25316259Smckusick 	else
25416259Smckusick 		oldlen = 0;
25516259Smckusick 	if (dirp->d_reclen - oldlen < newlen)
25616259Smckusick 		return (KEEPON);
25716259Smckusick 	newent.d_reclen = dirp->d_reclen - oldlen;
25816259Smckusick 	dirp->d_reclen = oldlen;
25916259Smckusick 	dirp = (struct direct *)(((char *)dirp) + oldlen);
26016259Smckusick 	dirp->d_ino = idesc->id_parent;	/* ino to be entered is in id_parent */
26116259Smckusick 	dirp->d_reclen = newent.d_reclen;
26217954Smckusick 	dirp->d_namlen = strlen(idesc->id_name);
26317954Smckusick 	bcopy(idesc->id_name, dirp->d_name, dirp->d_namlen + 1);
26416259Smckusick 	return (ALTERED|STOP);
26516259Smckusick }
26616259Smckusick 
26717957Smckusick chgino(idesc)
26816259Smckusick 	struct inodesc *idesc;
26916259Smckusick {
27016259Smckusick 	register DIRECT *dirp = idesc->id_dirp;
27116259Smckusick 
27217957Smckusick 	if (bcmp(dirp->d_name, idesc->id_name, dirp->d_namlen + 1))
27317957Smckusick 		return (KEEPON);
27417957Smckusick 	dirp->d_ino = idesc->id_parent;;
27517957Smckusick 	return (ALTERED|STOP);
27616259Smckusick }
27716259Smckusick 
27816259Smckusick linkup(orphan, pdir)
27916259Smckusick 	ino_t orphan;
28016259Smckusick 	ino_t pdir;
28116259Smckusick {
28216259Smckusick 	register DINODE *dp;
28316259Smckusick 	int lostdir, len;
28417954Smckusick 	ino_t oldlfdir;
28516259Smckusick 	struct inodesc idesc;
28617954Smckusick 	char tempname[BUFSIZ];
28717957Smckusick 	extern int pass4check();
28816259Smckusick 
28916259Smckusick 	bzero((char *)&idesc, sizeof(struct inodesc));
29017943Smckusick 	dp = ginode(orphan);
29117930Smckusick 	lostdir = DIRCT(dp);
29216259Smckusick 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
29316259Smckusick 	pinode(orphan);
29416259Smckusick 	if (preen && dp->di_size == 0)
29516259Smckusick 		return (0);
29616259Smckusick 	if (preen)
29716259Smckusick 		printf(" (RECONNECTED)\n");
29816259Smckusick 	else
29916259Smckusick 		if (reply("RECONNECT") == 0)
30016259Smckusick 			return (0);
30116259Smckusick 	pathp = pathname;
30216259Smckusick 	*pathp++ = '/';
30316259Smckusick 	*pathp = '\0';
30416259Smckusick 	if (lfdir == 0) {
30517943Smckusick 		dp = ginode(ROOTINO);
30617930Smckusick 		idesc.id_name = lfname;
30716259Smckusick 		idesc.id_type = DATA;
30816259Smckusick 		idesc.id_func = findino;
30916259Smckusick 		idesc.id_number = ROOTINO;
31030354Smckusick 		if ((ckinode(dp, &idesc) & FOUND) != 0) {
31117954Smckusick 			lfdir = idesc.id_parent;
31217954Smckusick 		} else {
31317954Smckusick 			pwarn("NO lost+found DIRECTORY");
31417954Smckusick 			if (preen || reply("CREATE")) {
31536827Smckusick 				lfdir = allocdir(ROOTINO, 0, lfmode);
31617992Smckusick 				if (lfdir != 0) {
31717992Smckusick 					if (makeentry(ROOTINO, lfdir, lfname) != 0) {
31817954Smckusick 						if (preen)
31917954Smckusick 							printf(" (CREATED)\n");
32017954Smckusick 					} else {
32117992Smckusick 						freedir(lfdir, ROOTINO);
32217992Smckusick 						lfdir = 0;
32317954Smckusick 						if (preen)
32417954Smckusick 							printf("\n");
32517954Smckusick 					}
32617954Smckusick 				}
32717954Smckusick 			}
32817954Smckusick 		}
32917943Smckusick 		if (lfdir == 0) {
33017954Smckusick 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY");
33116259Smckusick 			printf("\n\n");
33216259Smckusick 			return (0);
33316259Smckusick 		}
33416259Smckusick 	}
33517943Smckusick 	dp = ginode(lfdir);
33617957Smckusick 	if (!DIRCT(dp)) {
33717957Smckusick 		pfatal("lost+found IS NOT A DIRECTORY");
33817957Smckusick 		if (reply("REALLOCATE") == 0)
33917957Smckusick 			return (0);
34017957Smckusick 		oldlfdir = lfdir;
34136827Smckusick 		if ((lfdir = allocdir(ROOTINO, 0, lfmode)) == 0) {
34217957Smckusick 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
34317957Smckusick 			return (0);
34417957Smckusick 		}
34517957Smckusick 		idesc.id_type = DATA;
34617957Smckusick 		idesc.id_func = chgino;
34717957Smckusick 		idesc.id_number = ROOTINO;
34817957Smckusick 		idesc.id_parent = lfdir;	/* new inumber for lost+found */
34917957Smckusick 		idesc.id_name = lfname;
35017957Smckusick 		if ((ckinode(ginode(ROOTINO), &idesc) & ALTERED) == 0) {
35117957Smckusick 			pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n");
35217957Smckusick 			return (0);
35317957Smckusick 		}
35417957Smckusick 		inodirty();
35517957Smckusick 		idesc.id_type = ADDR;
35617957Smckusick 		idesc.id_func = pass4check;
35717957Smckusick 		idesc.id_number = oldlfdir;
35817957Smckusick 		adjust(&idesc, lncntp[oldlfdir] + 1);
35917957Smckusick 		lncntp[oldlfdir] = 0;
36017957Smckusick 		dp = ginode(lfdir);
36117957Smckusick 	}
36217957Smckusick 	if (statemap[lfdir] != DFOUND) {
36317957Smckusick 		pfatal("SORRY. NO lost+found DIRECTORY\n\n");
36416259Smckusick 		return (0);
36516259Smckusick 	}
36616259Smckusick 	len = strlen(lfname);
36716259Smckusick 	bcopy(lfname, pathp, len + 1);
36816259Smckusick 	pathp += len;
36917992Smckusick 	len = lftempname(tempname, orphan);
37017992Smckusick 	if (makeentry(lfdir, orphan, tempname) == 0) {
37116259Smckusick 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
37216259Smckusick 		printf("\n\n");
37316259Smckusick 		return (0);
37416259Smckusick 	}
37516259Smckusick 	lncntp[orphan]--;
37616259Smckusick 	*pathp++ = '/';
37730155Smckusick 	bcopy(tempname, pathp, len + 1);
37817954Smckusick 	pathp += len;
37916259Smckusick 	if (lostdir) {
38016259Smckusick 		dp = ginode(orphan);
38116259Smckusick 		idesc.id_type = DATA;
38217957Smckusick 		idesc.id_func = chgino;
38316259Smckusick 		idesc.id_number = orphan;
38416259Smckusick 		idesc.id_fix = DONTKNOW;
38517957Smckusick 		idesc.id_name = "..";
38617957Smckusick 		idesc.id_parent = lfdir;	/* new value for ".." */
38716259Smckusick 		(void)ckinode(dp, &idesc);
38817943Smckusick 		dp = ginode(lfdir);
38917943Smckusick 		dp->di_nlink++;
39017943Smckusick 		inodirty();
39117943Smckusick 		lncntp[lfdir]++;
39216259Smckusick 		pwarn("DIR I=%u CONNECTED. ", orphan);
39316259Smckusick 		printf("PARENT WAS I=%u\n", pdir);
39416259Smckusick 		if (preen == 0)
39516259Smckusick 			printf("\n");
39616259Smckusick 	}
39716259Smckusick 	return (1);
39816259Smckusick }
39916259Smckusick 
40016259Smckusick /*
40117944Smckusick  * make an entry in a directory
40217944Smckusick  */
40317992Smckusick makeentry(parent, ino, name)
40417992Smckusick 	ino_t parent, ino;
40517992Smckusick 	char *name;
40617992Smckusick {
40717944Smckusick 	DINODE *dp;
40817992Smckusick 	struct inodesc idesc;
40917944Smckusick 
41017992Smckusick 	if (parent < ROOTINO || parent >= imax || ino < ROOTINO || ino >= imax)
41117992Smckusick 		return (0);
41217992Smckusick 	bzero(&idesc, sizeof(struct inodesc));
41317992Smckusick 	idesc.id_type = DATA;
41417992Smckusick 	idesc.id_func = mkentry;
41517992Smckusick 	idesc.id_number = parent;
41617992Smckusick 	idesc.id_parent = ino;	/* this is the inode to enter */
41717992Smckusick 	idesc.id_fix = DONTKNOW;
41817992Smckusick 	idesc.id_name = name;
41917992Smckusick 	dp = ginode(parent);
42017993Smckusick 	if (dp->di_size % DIRBLKSIZ) {
42117993Smckusick 		dp->di_size = roundup(dp->di_size, DIRBLKSIZ);
42217993Smckusick 		inodirty();
42317993Smckusick 	}
42417992Smckusick 	if ((ckinode(dp, &idesc) & ALTERED) != 0)
42517944Smckusick 		return (1);
42617944Smckusick 	if (expanddir(dp) == 0)
42717944Smckusick 		return (0);
42817992Smckusick 	return (ckinode(dp, &idesc) & ALTERED);
42917944Smckusick }
43017944Smckusick 
43117944Smckusick /*
43217944Smckusick  * Attempt to expand the size of a directory
43317944Smckusick  */
43417944Smckusick expanddir(dp)
43517944Smckusick 	register DINODE *dp;
43617944Smckusick {
43717944Smckusick 	daddr_t lastbn, newblk;
43834225Smckusick 	register BUFAREA *bp;
43917944Smckusick 	char *cp, firstblk[DIRBLKSIZ];
44017944Smckusick 
44117944Smckusick 	lastbn = lblkno(&sblock, dp->di_size);
44217944Smckusick 	if (lastbn >= NDADDR - 1)
44317944Smckusick 		return (0);
44417944Smckusick 	if ((newblk = allocblk(sblock.fs_frag)) == 0)
44517944Smckusick 		return (0);
44617944Smckusick 	dp->di_db[lastbn + 1] = dp->di_db[lastbn];
44717944Smckusick 	dp->di_db[lastbn] = newblk;
44817944Smckusick 	dp->di_size += sblock.fs_bsize;
44917944Smckusick 	dp->di_blocks += btodb(sblock.fs_bsize);
45034225Smckusick 	bp = getdirblk(dp->di_db[lastbn + 1],
45134225Smckusick 		dblksize(&sblock, dp, lastbn + 1));
45238375Smckusick 	if (bp->b_errs)
45317944Smckusick 		goto bad;
45434225Smckusick 	bcopy(bp->b_un.b_buf, firstblk, DIRBLKSIZ);
45534225Smckusick 	bp = getdirblk(newblk, sblock.fs_bsize);
45638375Smckusick 	if (bp->b_errs)
45717944Smckusick 		goto bad;
45834225Smckusick 	bcopy(firstblk, bp->b_un.b_buf, DIRBLKSIZ);
45934225Smckusick 	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
46034225Smckusick 	     cp < &bp->b_un.b_buf[sblock.fs_bsize];
46117944Smckusick 	     cp += DIRBLKSIZ)
46217944Smckusick 		bcopy((char *)&emptydir, cp, sizeof emptydir);
46334225Smckusick 	dirty(bp);
46434225Smckusick 	bp = getdirblk(dp->di_db[lastbn + 1],
46534225Smckusick 		dblksize(&sblock, dp, lastbn + 1));
46638375Smckusick 	if (bp->b_errs)
46717944Smckusick 		goto bad;
46834225Smckusick 	bcopy((char *)&emptydir, bp->b_un.b_buf, sizeof emptydir);
46917944Smckusick 	pwarn("NO SPACE LEFT IN %s", pathname);
47017944Smckusick 	if (preen)
47117944Smckusick 		printf(" (EXPANDED)\n");
47217944Smckusick 	else if (reply("EXPAND") == 0)
47317944Smckusick 		goto bad;
47434225Smckusick 	dirty(bp);
47517944Smckusick 	inodirty();
47617944Smckusick 	return (1);
47717944Smckusick bad:
47817944Smckusick 	dp->di_db[lastbn] = dp->di_db[lastbn + 1];
47917944Smckusick 	dp->di_db[lastbn + 1] = 0;
48017944Smckusick 	dp->di_size -= sblock.fs_bsize;
48117944Smckusick 	dp->di_blocks -= btodb(sblock.fs_bsize);
48217944Smckusick 	freeblk(newblk, sblock.fs_frag);
48317944Smckusick 	return (0);
48417944Smckusick }
48517944Smckusick 
48617944Smckusick /*
48717954Smckusick  * allocate a new directory
48817954Smckusick  */
48936827Smckusick allocdir(parent, request, mode)
49017954Smckusick 	ino_t parent, request;
49136827Smckusick 	int mode;
49217954Smckusick {
49317954Smckusick 	ino_t ino;
49417954Smckusick 	char *cp;
49517954Smckusick 	DINODE *dp;
49634225Smckusick 	register BUFAREA *bp;
49717954Smckusick 
49836827Smckusick 	ino = allocino(request, IFDIR|mode);
49917954Smckusick 	dirhead.dot_ino = ino;
50017954Smckusick 	dirhead.dotdot_ino = parent;
50117954Smckusick 	dp = ginode(ino);
50234225Smckusick 	bp = getdirblk(dp->di_db[0], sblock.fs_fsize);
50338375Smckusick 	if (bp->b_errs) {
50417954Smckusick 		freeino(ino);
50517954Smckusick 		return (0);
50617954Smckusick 	}
50734225Smckusick 	bcopy((char *)&dirhead, bp->b_un.b_buf, sizeof dirhead);
50834225Smckusick 	for (cp = &bp->b_un.b_buf[DIRBLKSIZ];
50934225Smckusick 	     cp < &bp->b_un.b_buf[sblock.fs_fsize];
51017954Smckusick 	     cp += DIRBLKSIZ)
51117954Smckusick 		bcopy((char *)&emptydir, cp, sizeof emptydir);
51234225Smckusick 	dirty(bp);
51317954Smckusick 	dp->di_nlink = 2;
51417954Smckusick 	inodirty();
51517954Smckusick 	if (ino == ROOTINO) {
51617954Smckusick 		lncntp[ino] = dp->di_nlink;
51717954Smckusick 		return(ino);
51817954Smckusick 	}
51917954Smckusick 	if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) {
52017954Smckusick 		freeino(ino);
52117954Smckusick 		return (0);
52217954Smckusick 	}
52317954Smckusick 	statemap[ino] = statemap[parent];
52417954Smckusick 	if (statemap[ino] == DSTATE) {
52517954Smckusick 		lncntp[ino] = dp->di_nlink;
52617954Smckusick 		lncntp[parent]++;
52717954Smckusick 	}
52817954Smckusick 	dp = ginode(parent);
52917954Smckusick 	dp->di_nlink++;
53017954Smckusick 	inodirty();
53117954Smckusick 	return (ino);
53217954Smckusick }
53317954Smckusick 
53417954Smckusick /*
53517954Smckusick  * free a directory inode
53617954Smckusick  */
53717954Smckusick freedir(ino, parent)
53817954Smckusick 	ino_t ino, parent;
53917954Smckusick {
54017954Smckusick 	DINODE *dp;
54117954Smckusick 
54217954Smckusick 	if (ino != parent) {
54317954Smckusick 		dp = ginode(parent);
54417954Smckusick 		dp->di_nlink--;
54517954Smckusick 		inodirty();
54617954Smckusick 	}
54717954Smckusick 	freeino(ino);
54817954Smckusick }
54917954Smckusick 
55017954Smckusick /*
55116259Smckusick  * generate a temporary name for the lost+found directory.
55216259Smckusick  */
55316259Smckusick lftempname(bufp, ino)
55416259Smckusick 	char *bufp;
55516259Smckusick 	ino_t ino;
55616259Smckusick {
55716259Smckusick 	register ino_t in;
55816259Smckusick 	register char *cp;
55916259Smckusick 	int namlen;
56016259Smckusick 
56116259Smckusick 	cp = bufp + 2;
56216259Smckusick 	for (in = imax; in > 0; in /= 10)
56316259Smckusick 		cp++;
56416259Smckusick 	*--cp = 0;
56516259Smckusick 	namlen = cp - bufp;
56616259Smckusick 	in = ino;
56716259Smckusick 	while (cp > bufp) {
56816259Smckusick 		*--cp = (in % 10) + '0';
56916259Smckusick 		in /= 10;
57016259Smckusick 	}
57116259Smckusick 	*cp = '#';
57216259Smckusick 	return (namlen);
57316259Smckusick }
57434225Smckusick 
57534225Smckusick /*
57634225Smckusick  * Get a directory block.
57734225Smckusick  * Insure that it is held until another is requested.
57834225Smckusick  */
57934225Smckusick BUFAREA *
58034225Smckusick getdirblk(blkno, size)
58134225Smckusick 	daddr_t blkno;
58234225Smckusick 	long size;
58334225Smckusick {
58434225Smckusick 	static BUFAREA *pbp = 0;
58534225Smckusick 
58634225Smckusick 	if (pbp != 0)
58734225Smckusick 		pbp->b_flags &= ~B_INUSE;
58834225Smckusick 	pbp = getdatablk(blkno, size);
58934225Smckusick 	return (pbp);
59034225Smckusick }
591