xref: /csrg-svn/sbin/fsck/main.c (revision 5877)
1*5877Smckusic static	char *sccsid = "@(#)main.c	1.22 (Berkeley) 02/17/82";
24428Smckusic 
34236Smckusick #include <stdio.h>
44236Smckusick #include <ctype.h>
54236Smckusick #include "../h/param.h"
64236Smckusick #include "../h/fs.h"
7*5877Smckusic #include "../h/ndir.h"
84236Smckusick #include "../h/inode.h"
94236Smckusick #include "../h/stat.h"
104878Smckusic #include "../h/ostat.h"
114236Smckusick #include <fstab.h>
124236Smckusick 
134236Smckusick typedef	int	(*SIG_TYP)();
144236Smckusick 
155347Smckusic #define	MAXNINDIR	(MAXBSIZE / sizeof (daddr_t))
165347Smckusic #define	MAXINOPB	(MAXBSIZE / sizeof (struct dinode))
175325Smckusic #define	SPERB		(MAXBSIZE / sizeof(short))
184236Smckusick 
194236Smckusick #define	MAXDUP	10		/* limit on dup blks (per inode) */
204236Smckusick #define	MAXBAD	10		/* limit on bad blks (per inode) */
214236Smckusick 
224236Smckusick #define	USTATE	0		/* inode not allocated */
234236Smckusick #define	FSTATE	01		/* inode is file */
244236Smckusick #define	DSTATE	02		/* inode is directory */
254236Smckusick #define	CLEAR	03		/* inode is to be cleared */
264236Smckusick 
274236Smckusick typedef struct dinode	DINODE;
284236Smckusick typedef struct direct	DIRECT;
294236Smckusick 
304236Smckusick #define	ALLOC	((dp->di_mode & IFMT) != 0)
31*5877Smckusic #define	DIRCT	((dp->di_mode & IFMT) == IFDIR)
324236Smckusick #define	REG	((dp->di_mode & IFMT) == IFREG)
334236Smckusick #define	BLK	((dp->di_mode & IFMT) == IFBLK)
344236Smckusick #define	CHR	((dp->di_mode & IFMT) == IFCHR)
354236Smckusick #define	MPC	((dp->di_mode & IFMT) == IFMPC)
364236Smckusick #define	MPB	((dp->di_mode & IFMT) == IFMPB)
374236Smckusick #define	SPECIAL	(BLK || CHR || MPC || MPB)
384236Smckusick 
394465Smckusic ino_t	startinum;		/* blk num of first in raw area */
404236Smckusick 
414236Smckusick struct bufarea {
424236Smckusick 	struct bufarea	*b_next;		/* must be first */
434236Smckusick 	daddr_t	b_bno;
444236Smckusick 	int	b_size;
454236Smckusick 	union {
465325Smckusic 		char	b_buf[MAXBSIZE];	/* buffer space */
474236Smckusick 		short	b_lnks[SPERB];		/* link counts */
485325Smckusic 		daddr_t	b_indir[MAXNINDIR];	/* indirect block */
494236Smckusick 		struct	fs b_fs;		/* super block */
504236Smckusick 		struct	cg b_cg;		/* cylinder group */
515325Smckusic 		struct dinode b_dinode[MAXINOPB]; /* inode block */
524236Smckusick 	} b_un;
534236Smckusick 	char	b_dirty;
544236Smckusick };
554236Smckusick 
564236Smckusick typedef struct bufarea BUFAREA;
574236Smckusick 
584236Smckusick BUFAREA	inoblk;			/* inode blocks */
594236Smckusick BUFAREA	fileblk;		/* other blks in filesys */
604236Smckusick BUFAREA	sblk;			/* file system superblock */
614236Smckusick BUFAREA	cgblk;
624236Smckusick 
634236Smckusick #define	initbarea(x)	(x)->b_dirty = 0;(x)->b_bno = (daddr_t)-1
644236Smckusick #define	dirty(x)	(x)->b_dirty = 1
654236Smckusick #define	inodirty()	inoblk.b_dirty = 1
664236Smckusick #define	sbdirty()	sblk.b_dirty = 1
674236Smckusick #define	cgdirty()	cgblk.b_dirty = 1
684236Smckusick 
694236Smckusick #define	dirblk		fileblk.b_un
704236Smckusick #define	sblock		sblk.b_un.b_fs
714236Smckusick #define	cgrp		cgblk.b_un.b_cg
724236Smckusick 
734236Smckusick struct filecntl {
744236Smckusick 	int	rfdes;
754236Smckusick 	int	wfdes;
764236Smckusick 	int	mod;
774236Smckusick } dfile;			/* file descriptors for filesys */
784236Smckusick 
794236Smckusick #define	DUPTBLSIZE	100	/* num of dup blocks to remember */
804236Smckusick daddr_t	duplist[DUPTBLSIZE];	/* dup block table */
814236Smckusick daddr_t	*enddup;		/* next entry in dup table */
824236Smckusick daddr_t	*muldup;		/* multiple dups part of table */
834236Smckusick 
844236Smckusick #define	MAXLNCNT	20	/* num zero link cnts to remember */
854236Smckusick ino_t	badlncnt[MAXLNCNT];	/* table of inos with zero link cnts */
864236Smckusick ino_t	*badlnp;		/* next entry in table */
874236Smckusick 
884236Smckusick char	rawflg;
894236Smckusick char	nflag;			/* assume a no response */
904236Smckusick char	yflag;			/* assume a yes response */
914236Smckusick int	bflag;			/* location of alternate super block */
925381Smckusic int	debug;			/* output debugging info */
934236Smckusick char	preen;			/* just fix normal inconsistencies */
944236Smckusick char	rplyflag;		/* any questions asked? */
954236Smckusick char	hotroot;		/* checking root device */
964236Smckusick char	fixcg;			/* corrupted free list bit maps */
974236Smckusick 
984236Smckusick char	*blkmap;		/* ptr to primary blk allocation map */
994236Smckusick char	*freemap;		/* ptr to secondary blk allocation map */
1004236Smckusick char	*statemap;		/* ptr to inode state table */
1014236Smckusick short	*lncntp;		/* ptr to link count table */
1024236Smckusick 
1034236Smckusick char	*pathp;			/* pointer to pathname position */
1044236Smckusick char	*thisname;		/* ptr to current pathname component */
1054236Smckusick char	*srchname;		/* name being searched for in dir */
106*5877Smckusic char	pathname[BUFSIZ];
1074236Smckusick 
1084236Smckusick char	*lfname = "lost+found";
1094236Smckusick 
1104236Smckusick ino_t	inum;			/* inode we are currently working on */
1114236Smckusick ino_t	imax;			/* number of inodes */
1124236Smckusick ino_t	parentdir;		/* i number of parent directory */
1134236Smckusick ino_t	lastino;		/* hiwater mark of inodes */
1144236Smckusick ino_t	lfdir;			/* lost & found directory */
1154236Smckusick ino_t	orphan;			/* orphaned inode */
1164236Smckusick 
1174236Smckusick off_t	filsize;		/* num blks seen in file */
1184236Smckusick off_t	maxblk;			/* largest logical blk in file */
1194236Smckusick off_t	bmapsz;			/* num chars in blkmap */
1204236Smckusick 
1214236Smckusick daddr_t	n_ffree;		/* number of small free blocks */
1224236Smckusick daddr_t	n_bfree;		/* number of large free blocks */
1234236Smckusick daddr_t	n_blks;			/* number of blocks used */
1244236Smckusick daddr_t	n_files;		/* number of files seen */
1254236Smckusick daddr_t	n_index;
1264236Smckusick daddr_t	n_bad;
1274236Smckusick daddr_t	fmax;			/* number of blocks in the volume */
1284236Smckusick 
1294236Smckusick daddr_t	badblk;
1304236Smckusick daddr_t	dupblk;
1314236Smckusick 
1324236Smckusick int	inosumbad;
1334236Smckusick int	offsumbad;
1344465Smckusic int	frsumbad;
1354236Smckusick 
1364236Smckusick #define	zapino(x)	(*(x) = zino)
1374236Smckusick struct	dinode zino;
1384236Smckusick 
1394236Smckusick #define	setlncnt(x)	(lncntp[inum] = x)
1404236Smckusick #define	getlncnt()	(lncntp[inum])
1414236Smckusick #define	declncnt()	(--lncntp[inum])
1424236Smckusick 
1434236Smckusick #define	setbmap(x)	setbit(blkmap, x)
1444236Smckusick #define	getbmap(x)	isset(blkmap, x)
1454236Smckusick #define	clrbmap(x)	clrbit(blkmap, x)
1464236Smckusick 
1474236Smckusick #define	setfmap(x)	setbit(freemap, x)
1484236Smckusick #define	getfmap(x)	isset(freemap, x)
1494236Smckusick #define	clrfmap(x)	clrbit(freemap, x)
1504236Smckusick 
1514236Smckusick #define	setstate(x)	(statemap[inum] = x)
1524236Smckusick #define	getstate()	statemap[inum]
1534236Smckusick 
1544236Smckusick #define	DATA	1
1554236Smckusick #define	ADDR	0
1564236Smckusick 
1574236Smckusick #define	ALTERD	010
1584236Smckusick #define	KEEPON	04
1594236Smckusick #define	SKIP	02
1604236Smckusick #define	STOP	01
1614236Smckusick 
1624236Smckusick int	(*signal())();
1634236Smckusick long	lseek();
1644236Smckusick time_t	time();
1654236Smckusick DINODE	*ginode();
1664236Smckusick BUFAREA	*getblk();
1674236Smckusick int	dirscan();
1684236Smckusick int	findino();
1694236Smckusick int	catch();
1704236Smckusick int	mkentry();
1714236Smckusick int	chgdd();
1724236Smckusick int	pass1(), pass1b(), pass2(), pass4(), pass5();
1734236Smckusick int	(*pfunc)();
1744236Smckusick char	*rawname(), *rindex(), *unrawname();
1754606Smckusic extern int inside[], around[];
1765325Smckusic extern unsigned char *fragtbl[];
1774236Smckusick 
1784236Smckusick char	*devname;
1794236Smckusick 
1804236Smckusick main(argc, argv)
1814715Smckusic 	int	argc;
1824715Smckusic 	char	*argv[];
1834236Smckusick {
1844236Smckusick 	struct fstab *fsp;
1854236Smckusick 	int pid, passno, anygtr, sumstatus;
1864236Smckusick 
1874236Smckusick 	sync();
1884236Smckusick 	while (--argc > 0 && **++argv == '-') {
1894236Smckusick 		switch (*++*argv) {
1904236Smckusick 
1914236Smckusick 		case 'p':
1924236Smckusick 			preen++;
1934236Smckusick 			break;
1944236Smckusick 
1954236Smckusick 		case 'b':
1964236Smckusick 			bflag = atoi(argv[0]+1);
1974236Smckusick 			printf("Alternate super block location: %d\n", bflag);
1984236Smckusick 			break;
1994236Smckusick 
2005381Smckusic 		case 'd':
2015381Smckusic 			debug++;
2025381Smckusic 			break;
2035381Smckusic 
2044236Smckusick 		case 'n':	/* default no answer flag */
2054236Smckusick 		case 'N':
2064236Smckusick 			nflag++;
2074236Smckusick 			yflag = 0;
2084236Smckusick 			break;
2094236Smckusick 
2104236Smckusick 		case 'y':	/* default yes answer flag */
2114236Smckusick 		case 'Y':
2124236Smckusick 			yflag++;
2134236Smckusick 			nflag = 0;
2144236Smckusick 			break;
2154236Smckusick 
2164236Smckusick 		default:
2174236Smckusick 			errexit("%c option?\n", **argv);
2184236Smckusick 		}
2194236Smckusick 	}
2204236Smckusick 	if (signal(SIGINT, SIG_IGN) != SIG_IGN)
2214236Smckusick 		signal(SIGINT, catch);
2224236Smckusick 	if (argc) {
2234236Smckusick 		while (argc-- > 0) {
2244236Smckusick 			hotroot = 0;
2254236Smckusick 			check(*argv++);
2264236Smckusick 		}
2274236Smckusick 		exit(0);
2284236Smckusick 	}
2294236Smckusick 	sumstatus = 0;
2304236Smckusick 	passno = 1;
2314236Smckusick 	do {
2324236Smckusick 		anygtr = 0;
2334236Smckusick 		if (setfsent() == 0)
2344236Smckusick 			errexit("Can't open checklist file: %s\n", FSTAB);
2354236Smckusick 		while ((fsp = getfsent()) != 0) {
2364236Smckusick 			if (strcmp(fsp->fs_type, FSTAB_RW) &&
2374236Smckusick 			    strcmp(fsp->fs_type, FSTAB_RO))
2384236Smckusick 				continue;
2394236Smckusick 			if (preen == 0 ||
2404236Smckusick 			    passno == 1 && fsp->fs_passno == passno) {
2414236Smckusick 				if (blockcheck(fsp->fs_spec) == 0 && preen)
2424236Smckusick 					exit(8);
2434236Smckusick 			} else if (fsp->fs_passno > passno)
2444236Smckusick 				anygtr = 1;
2454236Smckusick 			else if (fsp->fs_passno == passno) {
2464236Smckusick 				pid = fork();
2474236Smckusick 				if (pid < 0) {
2484236Smckusick 					perror("fork");
2494236Smckusick 					exit(8);
2504236Smckusick 				}
2514236Smckusick 				if (pid == 0)
2524236Smckusick 					if (blockcheck(fsp->fs_spec)==0)
2534236Smckusick 						exit(8);
2544236Smckusick 					else
2554236Smckusick 						exit(0);
2564236Smckusick 			}
2574236Smckusick 		}
2584236Smckusick 		if (preen) {
2594236Smckusick 			int status;
2604236Smckusick 			while (wait(&status) != -1)
2614236Smckusick 				sumstatus |= status;
2624236Smckusick 		}
2634236Smckusick 		passno++;
2644236Smckusick 	} while (anygtr);
2654236Smckusick 	if (sumstatus)
2664236Smckusick 		exit(8);
2674236Smckusick 	endfsent();
2684236Smckusick 	exit(0);
2694236Smckusick }
2704236Smckusick 
2714236Smckusick blockcheck(name)
2724236Smckusick 	char *name;
2734236Smckusick {
2744878Smckusic 	struct ostat stslash, stblock, stchar;
2754236Smckusick 	char *raw;
2764236Smckusick 	int looped = 0;
2774236Smckusick 
2784236Smckusick 	hotroot = 0;
2794236Smckusick 	if (stat("/", &stslash) < 0){
2804236Smckusick 		error("Can't stat root\n");
2814236Smckusick 		return (0);
2824236Smckusick 	}
2834236Smckusick retry:
2844236Smckusick 	if (stat(name, &stblock) < 0){
2854236Smckusick 		error("Can't stat %s\n", name);
2864236Smckusick 		return (0);
2874236Smckusick 	}
2884236Smckusick 	if (stblock.st_mode & S_IFBLK) {
2894236Smckusick 		raw = rawname(name);
2904236Smckusick 		if (stat(raw, &stchar) < 0){
2914236Smckusick 			error("Can't stat %s\n", raw);
2924236Smckusick 			return (0);
2934236Smckusick 		}
2944236Smckusick 		if (stchar.st_mode & S_IFCHR) {
2954236Smckusick 			if (stslash.st_dev == stblock.st_rdev) {
2964236Smckusick 				hotroot++;
2974236Smckusick 				raw = unrawname(name);
2984236Smckusick 			}
2994236Smckusick 			check(raw);
3004236Smckusick 			return (1);
3014236Smckusick 		} else {
3024236Smckusick 			error("%s is not a character device\n", raw);
3034236Smckusick 			return (0);
3044236Smckusick 		}
3054236Smckusick 	} else if (stblock.st_mode & S_IFCHR) {
3064236Smckusick 		if (looped) {
3074236Smckusick 			error("Can't make sense out of name %s\n", name);
3084236Smckusick 			return (0);
3094236Smckusick 		}
3104236Smckusick 		name = unrawname(name);
3114236Smckusick 		looped++;
3124236Smckusick 		goto retry;
3134236Smckusick 	}
3144236Smckusick 	error("Can't make sense out of name %s\n", name);
3154236Smckusick 	return (0);
3164236Smckusick }
3174236Smckusick 
3184236Smckusick char *
3194236Smckusick unrawname(cp)
3204236Smckusick 	char *cp;
3214236Smckusick {
3224236Smckusick 	char *dp = rindex(cp, '/');
3234878Smckusic 	struct ostat stb;
3244236Smckusick 
3254236Smckusick 	if (dp == 0)
3264236Smckusick 		return (cp);
3274236Smckusick 	if (stat(cp, &stb) < 0)
3284236Smckusick 		return (cp);
3294236Smckusick 	if ((stb.st_mode&S_IFMT) != S_IFCHR)
3304236Smckusick 		return (cp);
3314236Smckusick 	if (*(dp+1) != 'r')
3324236Smckusick 		return (cp);
3334236Smckusick 	strcpy(dp+1, dp+2);
3344236Smckusick 	return (cp);
3354236Smckusick }
3364236Smckusick 
3374236Smckusick char *
3384236Smckusick rawname(cp)
3394236Smckusick 	char *cp;
3404236Smckusick {
3414236Smckusick 	static char rawbuf[32];
3424236Smckusick 	char *dp = rindex(cp, '/');
3434236Smckusick 
3444236Smckusick 	if (dp == 0)
3454236Smckusick 		return (0);
3464236Smckusick 	*dp = 0;
3474236Smckusick 	strcpy(rawbuf, cp);
3484236Smckusick 	*dp = '/';
3494236Smckusick 	strcat(rawbuf, "/r");
3504236Smckusick 	strcat(rawbuf, dp+1);
3514236Smckusick 	return (rawbuf);
3524236Smckusick }
3534236Smckusick 
3544236Smckusick check(dev)
3554236Smckusick 	char *dev;
3564236Smckusick {
3574236Smckusick 	register DINODE *dp;
3584236Smckusick 	register ino_t *blp;
3594236Smckusick 	register int i, n;
3604236Smckusick 	ino_t savino;
3614236Smckusick 	int b, c;
3624236Smckusick 	daddr_t d, s;
3634236Smckusick 
3644236Smckusick 	devname = dev;
3654236Smckusick 	if (setup(dev) == 0) {
3664236Smckusick 		if (preen)
3674236Smckusick 			pfatal("CAN'T CHECK DEVICE.");
3684236Smckusick 		return;
3694236Smckusick 	}
3704236Smckusick /* 1 */
3714236Smckusick 	if (preen==0) {
3724236Smckusick 		if (hotroot)
3734236Smckusick 			printf("** Root file system\n");
3744236Smckusick 		printf("** Phase 1 - Check Blocks and Sizes\n");
3754236Smckusick 	}
3764236Smckusick 	pfunc = pass1;
3774236Smckusick 	inum = 0;
3785325Smckusic 	n_blks += howmany(sblock.fs_cssize, sblock.fs_bsize) * sblock.fs_frag;
3794236Smckusick 	for (c = 0; c < sblock.fs_ncg; c++) {
3805381Smckusic 		if (getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize) == 0)
3814236Smckusick 			continue;
3824236Smckusick 		n = 0;
3834465Smckusic 		for (i = 0; i < sblock.fs_ipg; i++, inum++) {
3844465Smckusic 			dp = ginode();
3854465Smckusic 			if (dp == NULL)
3864465Smckusic 				continue;
3874236Smckusick 			if (ALLOC) {
3884236Smckusick 				if (!isset(cgrp.cg_iused, i)) {
3895381Smckusic 					if (debug)
3905381Smckusic 						printf("%d bad, not used\n",
3915381Smckusic 						    inum);
3924236Smckusick 					inosumbad++;
3934465Smckusic 					n++;
3944236Smckusick 				}
3954236Smckusick 				lastino = inum;
3964236Smckusick 				if (ftypeok(dp) == 0) {
3974236Smckusick 					pfatal("UNKNOWN FILE TYPE I=%u", inum);
3984236Smckusick 					if (reply("CLEAR") == 1) {
3994236Smckusick 						zapino(dp);
4004236Smckusick 						inodirty();
4014236Smckusick 						inosumbad++;
4024236Smckusick 					}
4034236Smckusick 					continue;
4044236Smckusick 				}
4054236Smckusick 				n_files++;
4064236Smckusick 				if (setlncnt(dp->di_nlink) <= 0) {
4074236Smckusick 					if (badlnp < &badlncnt[MAXLNCNT])
4084236Smckusick 						*badlnp++ = inum;
4094236Smckusick 					else {
4104236Smckusick 						pfatal("LINK COUNT TABLE OVERFLOW");
4114236Smckusick 						if (reply("CONTINUE") == 0)
4124236Smckusick 							errexit("");
4134236Smckusick 					}
4144236Smckusick 				}
415*5877Smckusic 				setstate(DIRCT ? DSTATE : FSTATE);
4164236Smckusick 				badblk = dupblk = 0; filsize = 0; maxblk = 0;
4174236Smckusick 				ckinode(dp, ADDR);
4184236Smckusick 			} else {
4194236Smckusick 				n++;
4204236Smckusick 				if (isset(cgrp.cg_iused, i)) {
4215381Smckusic 					if (debug)
4225381Smckusic 						printf("%d bad, marked used\n",
4235381Smckusic 						    inum);
4244236Smckusick 					inosumbad++;
4254465Smckusic 					n--;
4264236Smckusick 				}
4274236Smckusick 				if (dp->di_mode != 0) {
4284236Smckusick 					pfatal("PARTIALLY ALLOCATED INODE I=%u", inum);
4294236Smckusick 					if (reply("CLEAR") == 1) {
4304236Smckusick 						zapino(dp);
4314236Smckusick 						inodirty();
4324236Smckusick 						inosumbad++;
4334236Smckusick 					}
4344236Smckusick 				}
4354236Smckusick 			}
4364236Smckusick 		}
4374789Smckusic 		if (n != cgrp.cg_cs.cs_nifree) {
4385381Smckusic 			if (debug)
4395381Smckusic 				printf("cg[%d].cg_cs.cs_nifree is %d not %d\n",
4405381Smckusic 				    c, cgrp.cg_cs.cs_nifree, n);
4414236Smckusick 			inosumbad++;
4424236Smckusick 		}
4434236Smckusick 	}
4444236Smckusick /* 1b */
4454236Smckusick 	if (enddup != &duplist[0]) {
4464236Smckusick 		if (preen)
4474236Smckusick 			pfatal("INTERNAL ERROR: dups with -p");
4484236Smckusick 		printf("** Phase 1b - Rescan For More DUPS\n");
4494236Smckusick 		pfunc = pass1b;
4504236Smckusick 		inum = 0;
4514236Smckusick 		for (c = 0; c < sblock.fs_ncg; c++) {
4524465Smckusic 			for (i = 0; i < sblock.fs_ipg; i++, inum++) {
4534465Smckusic 				dp = ginode();
4544465Smckusic 				if (dp == NULL)
4554465Smckusic 					continue;
4564236Smckusick 				if (getstate() != USTATE &&
4574236Smckusick 				    (ckinode(dp, ADDR) & STOP))
4584236Smckusick 					goto out1b;
4594465Smckusic 			}
4604236Smckusick 		}
4614236Smckusick 	}
4624236Smckusick out1b:
4634465Smckusic 	flush(&dfile, &inoblk);
4644236Smckusick /* 2 */
4654236Smckusick 	if (preen == 0)
4664236Smckusick 		printf("** Phase 2 - Check Pathnames\n");
4674236Smckusick 	inum = ROOTINO;
4684236Smckusick 	thisname = pathp = pathname;
4694236Smckusick 	pfunc = pass2;
4704236Smckusick 	switch (getstate()) {
4714236Smckusick 
4724236Smckusick 	case USTATE:
4734236Smckusick 		errexit("ROOT INODE UNALLOCATED. TERMINATING.\n");
4744236Smckusick 
4754236Smckusick 	case FSTATE:
4764236Smckusick 		pfatal("ROOT INODE NOT DIRECTORY");
4774236Smckusick 		if (reply("FIX") == 0 || (dp = ginode()) == NULL)
4784236Smckusick 			errexit("");
4794236Smckusick 		dp->di_mode &= ~IFMT;
4804236Smckusick 		dp->di_mode |= IFDIR;
4814236Smckusick 		inodirty();
4824236Smckusick 		inosumbad++;
4834236Smckusick 		setstate(DSTATE);
4844236Smckusick 		/* fall into ... */
4854236Smckusick 
4864236Smckusick 	case DSTATE:
4874236Smckusick 		descend();
4884236Smckusick 		break;
4894236Smckusick 
4904236Smckusick 	case CLEAR:
4914236Smckusick 		pfatal("DUPS/BAD IN ROOT INODE");
4924236Smckusick 		printf("\n");
4934236Smckusick 		if (reply("CONTINUE") == 0)
4944236Smckusick 			errexit("");
4954236Smckusick 		setstate(DSTATE);
4964236Smckusick 		descend();
4974236Smckusick 	}
4984236Smckusick /* 3 */
4994236Smckusick 	if (preen == 0)
5004236Smckusick 		printf("** Phase 3 - Check Connectivity\n");
5014236Smckusick 	for (inum = ROOTINO; inum <= lastino; inum++) {
5024236Smckusick 		if (getstate() == DSTATE) {
5034236Smckusick 			pfunc = findino;
5044236Smckusick 			srchname = "..";
5054236Smckusick 			savino = inum;
5064236Smckusick 			do {
5074236Smckusick 				orphan = inum;
5084236Smckusick 				if ((dp = ginode()) == NULL)
5094236Smckusick 					break;
5104236Smckusick 				filsize = dp->di_size;
5114236Smckusick 				parentdir = 0;
5124236Smckusick 				ckinode(dp, DATA);
5134236Smckusick 				if ((inum = parentdir) == 0)
5144236Smckusick 					break;
5154236Smckusick 			} while (getstate() == DSTATE);
5164236Smckusick 			inum = orphan;
5174236Smckusick 			if (linkup() == 1) {
5184236Smckusick 				thisname = pathp = pathname;
5194236Smckusick 				*pathp++ = '?';
5204236Smckusick 				pfunc = pass2;
5214236Smckusick 				descend();
5224236Smckusick 			}
5234236Smckusick 			inum = savino;
5244236Smckusick 		}
5254236Smckusick 	}
5264236Smckusick /* 4 */
5274236Smckusick 	if (preen == 0)
5284236Smckusick 		printf("** Phase 4 - Check Reference Counts\n");
5294236Smckusick 	pfunc = pass4;
5304236Smckusick 	for (inum = ROOTINO; inum <= lastino; inum++) {
5314236Smckusick 		switch (getstate()) {
5324236Smckusick 
5334236Smckusick 		case FSTATE:
5344236Smckusick 			if (n = getlncnt())
5354236Smckusick 				adjust((short)n);
5364236Smckusick 			else {
5374236Smckusick 				for (blp = badlncnt;blp < badlnp; blp++)
5384236Smckusick 					if (*blp == inum) {
5394236Smckusick 						clri("UNREF", 1);
5404236Smckusick 						break;
5414236Smckusick 					}
5424236Smckusick 			}
5434236Smckusick 			break;
5444236Smckusick 
5454236Smckusick 		case DSTATE:
5464236Smckusick 			clri("UNREF", 1);
5474236Smckusick 			break;
5484236Smckusick 
5494236Smckusick 		case CLEAR:
5504236Smckusick 			clri("BAD/DUP", 1);
5514236Smckusick 			break;
5524236Smckusick 		}
5534236Smckusick 	}
5545337Smckusic 	if (imax - ROOTINO - n_files != sblock.fs_cstotal.cs_nifree) {
5554236Smckusick 		pwarn("FREE INODE COUNT WRONG IN SUPERBLK");
5564236Smckusick 		if (preen)
5574236Smckusick 			printf(" (FIXED)\n");
5584236Smckusick 		if (preen || reply("FIX") == 1) {
5594789Smckusic 			sblock.fs_cstotal.cs_nifree = imax - n_files;
5604236Smckusick 			sbdirty();
5614236Smckusick 		}
5624236Smckusick 	}
5634236Smckusick 	flush(&dfile, &fileblk);
5644236Smckusick 
5654236Smckusick /* 5 */
5664236Smckusick 	if (preen == 0)
5674236Smckusick 		printf("** Phase 5 - Check Cyl groups\n");
5684236Smckusick 	copy(blkmap, freemap, (unsigned)bmapsz);
5694236Smckusick 	dupblk = 0;
5705381Smckusic 	n_index = sblock.fs_ncg * (cgdmin(&sblock, 0) - cgtod(&sblock, 0));
5714236Smckusick 	for (c = 0; c < sblock.fs_ncg; c++) {
5725381Smckusic 		daddr_t cbase = cgbase(&sblock, c);
5734236Smckusick 		short bo[MAXCPG][NRPOS];
5745371Smckusic 		long botot[MAXCPG];
5755325Smckusic 		long frsum[MAXFRAG];
5764465Smckusic 		int blk;
5774465Smckusic 
5785371Smckusic 		for (n = 0; n < sblock.fs_cpg; n++) {
5795371Smckusic 			botot[n] = 0;
5804236Smckusick 			for (i = 0; i < NRPOS; i++)
5814236Smckusick 				bo[n][i] = 0;
5825371Smckusic 		}
5835325Smckusic 		for (i = 0; i < sblock.fs_frag; i++) {
5844465Smckusic 			frsum[i] = 0;
5854465Smckusic 		}
5864465Smckusic 		/*
5874465Smckusic 		 * need to account for the spare boot and super blocks
5884465Smckusic 		 * which appear (inaccurately) bad
5894465Smckusic 		 */
5905381Smckusic 		n_bad += cgtod(&sblock, c) - cbase;
5915381Smckusic 		if (getblk(&cgblk, cgtod(&sblock, c), sblock.fs_cgsize) == 0)
5924236Smckusick 			continue;
5935325Smckusic 		for (b = 0; b < sblock.fs_fpg; b += sblock.fs_frag) {
5945325Smckusic 			if (isblock(&sblock, cgrp.cg_free, b/sblock.fs_frag)) {
5955325Smckusic 				if (pass5(cbase+b, sblock.fs_frag) == STOP)
5964236Smckusick 					goto out5;
5974236Smckusick 				/* this is clumsy ... */
5985325Smckusic 				n_ffree -= sblock.fs_frag;
5994236Smckusick 				n_bfree++;
6005371Smckusic 				botot[cbtocylno(&sblock, b)]++;
6015363Smckusic 				bo[cbtocylno(&sblock, b)]
6025363Smckusic 				    [cbtorpos(&sblock, b)]++;
6034465Smckusic 			} else {
6045325Smckusic 				for (d = 0; d < sblock.fs_frag; d++)
6054236Smckusick 					if (isset(cgrp.cg_free, b+d))
6064236Smckusick 						if (pass5(cbase+b+d,1) == STOP)
6074236Smckusick 							goto out5;
6084465Smckusic 				blk = ((cgrp.cg_free[b / NBBY] >> (b % NBBY)) &
6095325Smckusic 				       (0xff >> (NBBY - sblock.fs_frag)));
6104465Smckusic 				if (blk != 0)
6115325Smckusic 					fragacct(&sblock, blk, frsum, 1);
6124465Smckusic 			}
6134236Smckusick 		}
6145325Smckusic 		for (i = 0; i < sblock.fs_frag; i++) {
6154465Smckusic 			if (cgrp.cg_frsum[i] != frsum[i]) {
6165381Smckusic 				if (debug)
6175381Smckusic 					printf("cg[%d].cg_frsum[%d] have %d calc %d\n",
6185381Smckusic 					    c, i, cgrp.cg_frsum[i], frsum[i]);
6194465Smckusic 				frsumbad++;
6204465Smckusic 			}
6214465Smckusic 		}
6225371Smckusic 		for (n = 0; n < sblock.fs_cpg; n++) {
6235371Smckusic 			if (botot[n] != cgrp.cg_btot[n]) {
6245381Smckusic 				if (debug)
6255381Smckusic 					printf("cg[%d].cg_btot[%d] have %d calc %d\n",
6265381Smckusic 					    c, n, cgrp.cg_btot[n], botot[n]);
6275371Smckusic 				offsumbad++;
6285371Smckusic 			}
6294236Smckusick 			for (i = 0; i < NRPOS; i++)
6304236Smckusick 				if (bo[n][i] != cgrp.cg_b[n][i]) {
6315381Smckusic 					if (debug)
6325381Smckusic 						printf("cg[%d].cg_b[%d][%d] have %d calc %d\n",
6335381Smckusic 						    c, n, i, cgrp.cg_b[n][i],
6345381Smckusic 						    bo[n][i]);
6354236Smckusick 					offsumbad++;
6364236Smckusick 				}
6375371Smckusic 		}
6384236Smckusick 	}
6394236Smckusick out5:
6404236Smckusick 	if (dupblk)
6414236Smckusick 		pwarn("%d DUP BLKS IN BIT MAPS\n", dupblk);
6424236Smckusick 	if (fixcg == 0) {
6435325Smckusic 		if ((b = n_blks+n_ffree+sblock.fs_frag*n_bfree+n_index+n_bad) != fmax) {
6444236Smckusick 			pwarn("%ld BLK(S) MISSING\n", fmax - b);
6454236Smckusick 			fixcg = 1;
6464465Smckusic 		} else if (inosumbad + offsumbad + frsumbad) {
6474465Smckusic 			pwarn("SUMMARY INFORMATION %s%s%sBAD\n",
6484236Smckusick 			    inosumbad ? "(INODE FREE) " : "",
6494465Smckusic 			    offsumbad ? "(BLOCK OFFSETS) " : "",
6504465Smckusic 			    frsumbad ? "(FRAG SUMMARIES) " : "");
6514236Smckusick 			fixcg = 1;
6524789Smckusic 		} else if (n_ffree != sblock.fs_cstotal.cs_nffree ||
6534789Smckusic 		    n_bfree != sblock.fs_cstotal.cs_nbfree) {
6544236Smckusick 			pwarn("FREE BLK COUNT(S) WRONG IN SUPERBLK");
6554236Smckusick 			if (preen)
6564236Smckusick 				printf(" (FIXED)\n");
6574236Smckusick 			if (preen || reply("FIX") == 1) {
6584789Smckusic 				sblock.fs_cstotal.cs_nffree = n_ffree;
6594789Smckusic 				sblock.fs_cstotal.cs_nbfree = n_bfree;
6604236Smckusick 				sbdirty();
6614236Smckusick 			}
6624236Smckusick 		}
6634236Smckusick 	}
6644236Smckusick 	if (fixcg) {
6654236Smckusick 		pwarn("BAD CYLINDER GROUPS");
6664236Smckusick 		if (preen)
6674236Smckusick 			printf(" (SALVAGED)\n");
6684236Smckusick 		else if (reply("SALVAGE") == 0)
6694236Smckusick 			fixcg = 0;
6704236Smckusick 	}
6714236Smckusick 
6724236Smckusick 	if (fixcg) {
6734236Smckusick 		if (preen == 0)
6744236Smckusick 			printf("** Phase 6 - Salvage Cylinder Groups\n");
6754236Smckusick 		makecg();
6764789Smckusic 		n_ffree = sblock.fs_cstotal.cs_nffree;
6774789Smckusic 		n_bfree = sblock.fs_cstotal.cs_nbfree;
6784236Smckusick 	}
6794236Smckusick 
6804236Smckusick 	pwarn("%d files, %d used, %d free (%d frags, %d blocks)\n",
6815325Smckusic 	    n_files, n_blks - howmany(sblock.fs_cssize, sblock.fs_bsize) * sblock.fs_frag,
6825325Smckusic 	    n_ffree + sblock.fs_frag * n_bfree, n_ffree, n_bfree);
6834236Smckusick 	if (dfile.mod) {
6844236Smckusick 		time(&sblock.fs_time);
6854236Smckusick 		sbdirty();
6864236Smckusick 	}
6874236Smckusick 	ckfini();
6884236Smckusick 	sync();
6894236Smckusick 	if (dfile.mod && hotroot) {
6904236Smckusick 		printf("\n***** BOOT UNIX (NO SYNC!) *****\n");
6914236Smckusick 		exit(4);
6924236Smckusick 	}
6934236Smckusick 	if (dfile.mod && preen == 0)
6944236Smckusick 		printf("\n***** FILE SYSTEM WAS MODIFIED *****\n");
6954236Smckusick 	free(blkmap);
6964236Smckusick 	free(freemap);
6974236Smckusick 	free(statemap);
6984236Smckusick 	free(lncntp);
6994236Smckusick }
7004236Smckusick 
7014236Smckusick /* VARARGS1 */
7024236Smckusick error(s1, s2, s3, s4)
7034715Smckusic 	char *s1;
7044236Smckusick {
7054236Smckusick 
7064236Smckusick 	printf(s1, s2, s3, s4);
7074236Smckusick }
7084236Smckusick 
7094236Smckusick /* VARARGS1 */
7104236Smckusick errexit(s1, s2, s3, s4)
7114715Smckusic 	char *s1;
7124236Smckusick {
7134236Smckusick 	error(s1, s2, s3, s4);
7144236Smckusick 	exit(8);
7154236Smckusick }
7164236Smckusick 
7174236Smckusick /*
7184236Smckusick  * An inconsistency occured which shouldn't during normal operations.
7194236Smckusick  * Die if preening, otw just printf.
7204236Smckusick  */
7214236Smckusick /* VARARGS1 */
7224236Smckusick pfatal(s, a1, a2, a3)
7234236Smckusick 	char *s;
7244236Smckusick {
7254236Smckusick 
7264236Smckusick 	if (preen) {
7274236Smckusick 		printf("%s: ", devname);
7284236Smckusick 		printf(s, a1, a2, a3);
7294236Smckusick 		printf("\n");
7304236Smckusick 		preendie();
7314236Smckusick 	}
7324236Smckusick 	printf(s, a1, a2, a3);
7334236Smckusick }
7344236Smckusick 
7354236Smckusick preendie()
7364236Smckusick {
7374236Smckusick 
7384236Smckusick 	printf("%s: UNEXPECTED INCONSISTENCY; RUN fsck MANUALLY.\n", devname);
7394236Smckusick 	exit(8);
7404236Smckusick }
7414236Smckusick 
7424236Smckusick /*
7434236Smckusick  * Pwarn is like printf when not preening,
7444236Smckusick  * or a warning (preceded by filename) when preening.
7454236Smckusick  */
7464236Smckusick /* VARARGS1 */
7474236Smckusick pwarn(s, a1, a2, a3, a4, a5)
7484236Smckusick 	char *s;
7494236Smckusick {
7504236Smckusick 
7514236Smckusick 	if (preen)
7524236Smckusick 		printf("%s: ", devname);
7534236Smckusick 	printf(s, a1, a2, a3, a4, a5);
7544236Smckusick }
7554236Smckusick 
7564236Smckusick ckinode(dp, flg)
7574236Smckusick 	DINODE *dp;
7584236Smckusick 	register flg;
7594236Smckusick {
7604236Smckusick 	register daddr_t *ap;
7614236Smckusick 	register ret;
7624236Smckusick 	int (*func)(), n, ndb, size;
7634236Smckusick 
7644236Smckusick 	if (SPECIAL)
7654236Smckusick 		return (KEEPON);
7664236Smckusick 	func = (flg == ADDR) ? pfunc : dirscan;
7675325Smckusic 	ndb = howmany(dp->di_size, sblock.fs_bsize);
7684428Smckusic 	for (ap = &dp->di_db[0]; ap < &dp->di_db[NDADDR]; ap++) {
7695325Smckusic 		if (--ndb == 0 && (dp->di_size % sblock.fs_bsize))
7705325Smckusic 			size = howmany(dp->di_size % sblock.fs_bsize, sblock.fs_fsize);
7714236Smckusick 		else
7725325Smckusic 			size = sblock.fs_frag;
7734236Smckusick 		if (*ap && (ret = (*func)(*ap, size)) & STOP)
7744236Smckusick 			return (ret);
7754236Smckusick 	}
7764428Smckusic 	for (ap = &dp->di_ib[0], n = 1; n <= 2; ap++, n++) {
7775325Smckusic 		if (*ap && (ret = iblock(*ap, n, flg, dp->di_size - sblock.fs_bsize * NDADDR)) & STOP)
7784236Smckusick 			return (ret);
7794236Smckusick 	}
7804236Smckusick 	return (KEEPON);
7814236Smckusick }
7824236Smckusick 
7834236Smckusick iblock(blk, ilevel, flg, isize)
7844236Smckusick 	daddr_t blk;
7854236Smckusick 	register ilevel;
7864236Smckusick 	int isize;
7874236Smckusick {
7884236Smckusick 	register daddr_t *ap;
7894236Smckusick 	register daddr_t *aplim;
7904428Smckusic 	register int i, n;
7914236Smckusick 	int (*func)(), nif;
7924236Smckusick 	BUFAREA ib;
7934236Smckusick 
7944236Smckusick 	if (flg == ADDR) {
7954236Smckusick 		func = pfunc;
7965325Smckusic 		if (((n = (*func)(blk, sblock.fs_frag)) & KEEPON) == 0)
7974236Smckusick 			return (n);
7984236Smckusick 	} else
7994236Smckusick 		func = dirscan;
8004236Smckusick 	if (outrange(blk))		/* protect thyself */
8014236Smckusick 		return (SKIP);
8024236Smckusick 	initbarea(&ib);
8035325Smckusic 	if (getblk(&ib, blk, sblock.fs_bsize) == NULL)
8044236Smckusick 		return (SKIP);
8054236Smckusick 	ilevel--;
8064428Smckusic 	if (ilevel == 0) {
8075325Smckusic 		nif = isize / sblock.fs_bsize + 1;
8084428Smckusic 	} else /* ilevel == 1 */ {
8095325Smckusic 		nif = isize / (sblock.fs_bsize * NINDIR(&sblock)) + 1;
8104428Smckusic 	}
8115325Smckusic 	if (nif > NINDIR(&sblock))
8125325Smckusic 		nif = NINDIR(&sblock);
8134428Smckusic 	aplim = & ib.b_un.b_indir[nif];
8144428Smckusic 	for (ap = ib.b_un.b_indir, i = 1; ap < aplim; ap++, i++)
8154236Smckusick 		if (*ap) {
8164236Smckusick 			if (ilevel > 0)
8175325Smckusic 				n = iblock(*ap, ilevel, flg, isize - i * NINDIR(&sblock) * sblock.fs_bsize);
8184236Smckusick 			else
8195325Smckusic 				n = (*func)(*ap, sblock.fs_frag);
8204236Smckusick 			if (n & STOP)
8214236Smckusick 				return (n);
8224236Smckusick 		}
8234236Smckusick 	return (KEEPON);
8244236Smckusick }
8254236Smckusick 
8264236Smckusick pass1(blk, size)
8274236Smckusick 	daddr_t blk;
8284236Smckusick 	int size;
8294236Smckusick {
8304236Smckusick 	register daddr_t *dlp;
8314236Smckusick 	int res = KEEPON;
8324236Smckusick 
8334236Smckusick 	for (; size > 0; blk++, size--) {
8344236Smckusick 		if (outrange(blk)) {
8354236Smckusick 			blkerr("BAD", blk);
8364236Smckusick 			if (++badblk >= MAXBAD) {
8374236Smckusick 				printf("EXCESSIVE BAD BLKS I=%u", inum);
8384236Smckusick 				if (reply("CONTINUE") == 0)
8394236Smckusick 					errexit("");
8404236Smckusick 				return (STOP);
8414236Smckusick 			}
8424236Smckusick 			res = SKIP;
8434236Smckusick 		} else if (getbmap(blk)) {
8444236Smckusick 			blkerr("DUP", blk);
8454236Smckusick 			if (++dupblk >= MAXDUP) {
8464236Smckusick 				printf("EXCESSIVE DUP BLKS I=%u", inum);
8474236Smckusick 				if (reply("CONTINUE") == 0)
8484236Smckusick 					errexit("");
8494236Smckusick 				return (STOP);
8504236Smckusick 			}
8514236Smckusick 			if (enddup >= &duplist[DUPTBLSIZE]) {
8524236Smckusick 				printf("DUP TABLE OVERFLOW.");
8534236Smckusick 				if (reply("CONTINUE") == 0)
8544236Smckusick 					errexit("");
8554236Smckusick 				return (STOP);
8564236Smckusick 			}
8574236Smckusick 			for (dlp = duplist; dlp < muldup; dlp++)
8584236Smckusick 				if (*dlp == blk) {
8594236Smckusick 					*enddup++ = blk;
8604236Smckusick 					break;
8614236Smckusick 				}
8624236Smckusick 			if (dlp >= muldup) {
8634236Smckusick 				*enddup++ = *muldup;
8644236Smckusick 				*muldup++ = blk;
8654236Smckusick 			}
8664236Smckusick 		} else {
8674236Smckusick 			n_blks++;
8684236Smckusick 			setbmap(blk);
8694236Smckusick 		}
8704236Smckusick 		filsize++;
8714236Smckusick 	}
8724236Smckusick 	return (res);
8734236Smckusick }
8744236Smckusick 
8754236Smckusick pass1b(blk, size)
8764236Smckusick 	daddr_t blk;
8774236Smckusick 	int size;
8784236Smckusick {
8794236Smckusick 	register daddr_t *dlp;
8804236Smckusick 	int res = KEEPON;
8814236Smckusick 
8824236Smckusick 	for (; size > 0; blk++, size--) {
8834236Smckusick 		if (outrange(blk))
8844236Smckusick 			res = SKIP;
8854236Smckusick 		for (dlp = duplist; dlp < muldup; dlp++)
8864236Smckusick 			if (*dlp == blk) {
8874236Smckusick 				blkerr("DUP", blk);
8884236Smckusick 				*dlp = *--muldup;
8894236Smckusick 				*muldup = blk;
8904236Smckusick 				if (muldup == duplist)
8914236Smckusick 					return (STOP);
8924236Smckusick 			}
8934236Smckusick 	}
8944236Smckusick 	return (res);
8954236Smckusick }
8964236Smckusick 
8974236Smckusick pass2(dirp)
8984236Smckusick 	register DIRECT *dirp;
8994236Smckusick {
9004236Smckusick 	register char *p;
9014236Smckusick 	register n;
9024236Smckusick 	DINODE *dp;
9034236Smckusick 
9044236Smckusick 	if ((inum = dirp->d_ino) == 0)
9054236Smckusick 		return (KEEPON);
9064236Smckusick 	thisname = pathp;
907*5877Smckusic 	for (p = dirp->d_name; p < &dirp->d_name[MAXNAMLEN]; )
9084236Smckusick 		if ((*pathp++ = *p++) == 0) {
9094236Smckusick 			--pathp;
9104236Smckusick 			break;
9114236Smckusick 		}
9124236Smckusick 	*pathp = 0;
9134236Smckusick 	n = 0;
9144715Smckusic 	if (inum > imax || inum <= 0)
9154236Smckusick 		n = direrr("I OUT OF RANGE");
9164236Smckusick 	else {
9174236Smckusick again:
9184236Smckusick 		switch (getstate()) {
9194236Smckusick 		case USTATE:
9204236Smckusick 			n = direrr("UNALLOCATED");
9214236Smckusick 			break;
9224236Smckusick 
9234236Smckusick 		case CLEAR:
9244236Smckusick 			if ((n = direrr("DUP/BAD")) == 1)
9254236Smckusick 				break;
9264236Smckusick 			if ((dp = ginode()) == NULL)
9274236Smckusick 				break;
928*5877Smckusic 			setstate(DIRCT ? DSTATE : FSTATE);
9294236Smckusick 			goto again;
9304236Smckusick 
9314236Smckusick 		case FSTATE:
9324236Smckusick 			declncnt();
9334236Smckusick 			break;
9344236Smckusick 
9354236Smckusick 		case DSTATE:
9364236Smckusick 			declncnt();
9374236Smckusick 			descend();
9384236Smckusick 			break;
9394236Smckusick 		}
9404236Smckusick 	}
9414236Smckusick 	pathp = thisname;
9424236Smckusick 	if (n == 0)
9434236Smckusick 		return (KEEPON);
9444236Smckusick 	dirp->d_ino = 0;
9454236Smckusick 	return (KEEPON|ALTERD);
9464236Smckusick }
9474236Smckusick 
9484236Smckusick pass4(blk, size)
9494715Smckusic 	daddr_t blk;
9504236Smckusick {
9514236Smckusick 	register daddr_t *dlp;
9524236Smckusick 	int res = KEEPON;
9534236Smckusick 
9544236Smckusick 	for (; size > 0; blk++, size--) {
9554236Smckusick 		if (outrange(blk))
9564236Smckusick 			res = SKIP;
9574236Smckusick 		else if (getbmap(blk)) {
9584236Smckusick 			for (dlp = duplist; dlp < enddup; dlp++)
9594236Smckusick 				if (*dlp == blk) {
9604236Smckusick 					*dlp = *--enddup;
9614236Smckusick 					return (KEEPON);
9624236Smckusick 				}
9634236Smckusick 			clrbmap(blk);
9644236Smckusick 			n_blks--;
9654236Smckusick 		}
9664236Smckusick 	}
9674236Smckusick 	return (res);
9684236Smckusick }
9694236Smckusick 
9704236Smckusick pass5(blk, size)
9714236Smckusick 	daddr_t blk;
9724236Smckusick 	int size;
9734236Smckusick {
9744236Smckusick 	int res = KEEPON;
9754236Smckusick 
9764236Smckusick 	for (; size > 0; blk++, size--) {
9774236Smckusick 		if (outrange(blk)) {
9784236Smckusick 			fixcg = 1;
9794236Smckusick 			if (preen)
9804236Smckusick 				pfatal("BAD BLOCKS IN BIT MAPS.");
9814236Smckusick 			if (++badblk >= MAXBAD) {
9824236Smckusick 				printf("EXCESSIVE BAD BLKS IN BIT MAPS.");
9834236Smckusick 				if (reply("CONTINUE") == 0)
9844236Smckusick 					errexit("");
9854236Smckusick 				return (STOP);
9864236Smckusick 			}
9874236Smckusick 		} else if (getfmap(blk)) {
9884236Smckusick 			fixcg = 1;
9894236Smckusick 			if (++dupblk >= DUPTBLSIZE) {
9904236Smckusick 				printf("EXCESSIVE DUP BLKS IN BIT MAPS.");
9914236Smckusick 				if (reply("CONTINUE") == 0)
9924236Smckusick 					errexit("");
9934236Smckusick 				return (STOP);
9944236Smckusick 			}
9954236Smckusick 		} else {
9964236Smckusick 			n_ffree++;
9974236Smckusick 			setfmap(blk);
9984236Smckusick 		}
9994236Smckusick 	}
10004236Smckusick 	return (res);
10014236Smckusick }
10024236Smckusick 
10034236Smckusick outrange(blk)
10044236Smckusick 	daddr_t blk;
10054236Smckusick {
10064236Smckusick 	register int c;
10074236Smckusick 
10085381Smckusic 	c = dtog(&sblock, blk);
10095381Smckusic 	if (blk >= fmax || blk < cgdmin(&sblock, c)) {
10104236Smckusick 		return (1);
10114428Smckusic 	}
10124236Smckusick 	return (0);
10134236Smckusick }
10144236Smckusick 
10154236Smckusick blkerr(s, blk)
10164236Smckusick 	daddr_t blk;
10174236Smckusick 	char *s;
10184236Smckusick {
10194236Smckusick 	pfatal("%ld %s I=%u", blk, s, inum);
10204236Smckusick 	printf("\n");
10214236Smckusick 	setstate(CLEAR);	/* mark for possible clearing */
10224236Smckusick }
10234236Smckusick 
10244236Smckusick descend()
10254236Smckusick {
10264236Smckusick 	register DINODE *dp;
10274236Smckusick 	register char *savname;
10284236Smckusick 	off_t savsize;
10294236Smckusick 
10304236Smckusick 	setstate(FSTATE);
10314236Smckusick 	if ((dp = ginode()) == NULL)
10324236Smckusick 		return;
10334236Smckusick 	savname = thisname;
10344236Smckusick 	*pathp++ = '/';
10354236Smckusick 	savsize = filsize;
10364236Smckusick 	filsize = dp->di_size;
10374236Smckusick 	ckinode(dp, DATA);
10384236Smckusick 	thisname = savname;
10394236Smckusick 	*--pathp = 0;
10404236Smckusick 	filsize = savsize;
10414236Smckusick }
10424236Smckusick 
1043*5877Smckusic struct dirstuff {
1044*5877Smckusic 	int loc;
1045*5877Smckusic 	int blkno;
1046*5877Smckusic 	int blksiz;
1047*5877Smckusic };
1048*5877Smckusic 
10494236Smckusick dirscan(blk, nf)
10504715Smckusic 	daddr_t blk;
10514715Smckusic 	int nf;
10524236Smckusick {
1053*5877Smckusic 	register DIRECT *dp;
10544236Smckusick 	DIRECT direntry;
1055*5877Smckusic 	struct dirstuff dirp;
1056*5877Smckusic 	int blksiz, n;
10574236Smckusick 
10584236Smckusick 	if (outrange(blk)) {
10595325Smckusic 		filsize -= sblock.fs_bsize;
10604236Smckusick 		return (SKIP);
10614236Smckusick 	}
1062*5877Smckusic 	blksiz = nf * sblock.fs_fsize;
1063*5877Smckusic 	dirp.loc = 0;
1064*5877Smckusic 	dirp.blkno = blk;
1065*5877Smckusic 	dirp.blksiz = blksiz;
1066*5877Smckusic 	for (dp = readdir(&dirp); dp != NULL; dp = readdir(&dirp)) {
1067*5877Smckusic 		printf("got %s ino %d\n", dp->d_name, dp->d_ino);
1068*5877Smckusic 		copy(dp, &direntry, DIRSIZ(dp));
10694236Smckusick 		if ((n = (*pfunc)(&direntry)) & ALTERD) {
1070*5877Smckusic 			if (getblk(&fileblk, blk, blksiz) != NULL) {
1071*5877Smckusic 				copy(&direntry, dp, DIRSIZ(&direntry));
10724715Smckusic 				dirty(&fileblk);
10734236Smckusick 				sbdirty();
10744236Smckusick 			} else
10754236Smckusick 				n &= ~ALTERD;
10764236Smckusick 		}
1077*5877Smckusic 		if (n & STOP)
10784236Smckusick 			return (n);
10794236Smckusick 	}
10804236Smckusick 	return (filsize > 0 ? KEEPON : STOP);
10814236Smckusick }
10824236Smckusick 
1083*5877Smckusic /*
1084*5877Smckusic  * read an old stlye directory entry and present it as a new one
1085*5877Smckusic  */
1086*5877Smckusic #define	ODIRSIZ	14
1087*5877Smckusic 
1088*5877Smckusic struct	olddirect {
1089*5877Smckusic 	ino_t	d_ino;
1090*5877Smckusic 	char	d_name[ODIRSIZ];
1091*5877Smckusic 	char	d_spare[14];
1092*5877Smckusic };
1093*5877Smckusic 
1094*5877Smckusic /*
1095*5877Smckusic  * get next entry in a directory.
1096*5877Smckusic  */
1097*5877Smckusic DIRECT *
1098*5877Smckusic readdir(dirp)
1099*5877Smckusic 	register struct dirstuff *dirp;
1100*5877Smckusic {
1101*5877Smckusic 	register struct olddirect *dp;
1102*5877Smckusic 	static DIRECT dir;
1103*5877Smckusic 
1104*5877Smckusic 	if (getblk(&fileblk, dirp->blkno, dirp->blksiz) == NULL) {
1105*5877Smckusic 		filsize -= dirp->blksiz - dirp->loc;
1106*5877Smckusic 		return NULL;
1107*5877Smckusic 	}
1108*5877Smckusic 	for (;;) {
1109*5877Smckusic 		if (filsize <= 0 || dirp->loc >= dirp->blksiz)
1110*5877Smckusic 			return NULL;
1111*5877Smckusic 		dp = (struct olddirect *)(dirblk.b_buf + dirp->loc);
1112*5877Smckusic 		dirp->loc += sizeof(struct olddirect);
1113*5877Smckusic 		filsize -= sizeof(struct olddirect);
1114*5877Smckusic 		if (dp->d_ino == 0)
1115*5877Smckusic 			continue;
1116*5877Smckusic 		dir.d_ino = dp->d_ino;
1117*5877Smckusic 		strncpy(dir.d_name, dp->d_name, ODIRSIZ);
1118*5877Smckusic 		dir.d_namlen = strlen(dir.d_name);
1119*5877Smckusic 		return (&dir);
1120*5877Smckusic 	}
1121*5877Smckusic }
1122*5877Smckusic 
11234236Smckusick direrr(s)
11244715Smckusic 	char *s;
11254236Smckusick {
11264236Smckusick 	register DINODE *dp;
11274236Smckusick 
11284236Smckusick 	pwarn("%s ", s);
11294236Smckusick 	pinode();
11304236Smckusick 	printf("\n");
11314236Smckusick 	if ((dp = ginode()) != NULL && ftypeok(dp))
1132*5877Smckusic 		pfatal("%s=%s", DIRCT?"DIR":"FILE", pathname);
11334236Smckusick 	else
11344236Smckusick 		pfatal("NAME=%s", pathname);
11354236Smckusick 	return (reply("REMOVE"));
11364236Smckusick }
11374236Smckusick 
11384236Smckusick adjust(lcnt)
11394465Smckusic 	register short lcnt;
11404236Smckusick {
11414236Smckusick 	register DINODE *dp;
11424236Smckusick 
11434236Smckusick 	if ((dp = ginode()) == NULL)
11444236Smckusick 		return;
11454236Smckusick 	if (dp->di_nlink == lcnt) {
11464236Smckusick 		if (linkup() == 0)
11474236Smckusick 			clri("UNREF", 0);
11484236Smckusick 	}
11494236Smckusick 	else {
11504236Smckusick 		pwarn("LINK COUNT %s",
1151*5877Smckusic 			(lfdir==inum)?lfname:(DIRCT?"DIR":"FILE"));
11524236Smckusick 		pinode();
11534236Smckusick 		printf(" COUNT %d SHOULD BE %d",
11544236Smckusick 			dp->di_nlink, dp->di_nlink-lcnt);
11554236Smckusick 		if (preen) {
11564236Smckusick 			if (lcnt < 0) {
11574236Smckusick 				printf("\n");
11584236Smckusick 				preendie();
11594236Smckusick 			}
11604236Smckusick 			printf(" (ADJUSTED)\n");
11614236Smckusick 		}
11624236Smckusick 		if (preen || reply("ADJUST") == 1) {
11634236Smckusick 			dp->di_nlink -= lcnt;
11644236Smckusick 			inodirty();
11654236Smckusick 		}
11664236Smckusick 	}
11674236Smckusick }
11684236Smckusick 
11694236Smckusick clri(s, flg)
11704715Smckusic 	char *s;
11714236Smckusick {
11724236Smckusick 	register DINODE *dp;
11734236Smckusick 
11744236Smckusick 	if ((dp = ginode()) == NULL)
11754236Smckusick 		return;
11764236Smckusick 	if (flg == 1) {
1177*5877Smckusic 		pwarn("%s %s", s, DIRCT?"DIR":"FILE");
11784236Smckusick 		pinode();
11794236Smckusick 	}
11804236Smckusick 	if (preen || reply("CLEAR") == 1) {
11814236Smckusick 		if (preen)
11824236Smckusick 			printf(" (CLEARED)\n");
11834236Smckusick 		n_files--;
11844236Smckusick 		pfunc = pass4;
11854236Smckusick 		ckinode(dp, ADDR);
11864236Smckusick 		zapino(dp);
11874465Smckusic 		setstate(USTATE);
11884236Smckusick 		inodirty();
11894236Smckusick 		inosumbad++;
11904236Smckusick 	}
11914236Smckusick }
11924236Smckusick 
11934236Smckusick setup(dev)
11944715Smckusic 	char *dev;
11954236Smckusick {
11964236Smckusick 	dev_t rootdev;
11974878Smckusic 	struct ostat statb;
11984236Smckusick 	int super = bflag ? bflag : SBLOCK;
11994236Smckusick 
12004236Smckusick 	bflag = 0;
12014236Smckusick 	if (stat("/", &statb) < 0)
12024236Smckusick 		errexit("Can't stat root\n");
12034236Smckusick 	rootdev = statb.st_dev;
12044236Smckusick 	if (stat(dev, &statb) < 0) {
12054236Smckusick 		error("Can't stat %s\n", dev);
12064236Smckusick 		return (0);
12074236Smckusick 	}
12084236Smckusick 	rawflg = 0;
12094236Smckusick 	if ((statb.st_mode & S_IFMT) == S_IFBLK)
12104236Smckusick 		;
12114236Smckusick 	else if ((statb.st_mode & S_IFMT) == S_IFCHR)
12124236Smckusick 		rawflg++;
12134236Smckusick 	else {
12144236Smckusick 		if (reply("file is not a block or character device; OK") == 0)
12154236Smckusick 			return (0);
12164236Smckusick 	}
12174236Smckusick 	if (rootdev == statb.st_rdev)
12184236Smckusick 		hotroot++;
12194236Smckusick 	if ((dfile.rfdes = open(dev, 0)) < 0) {
12204236Smckusick 		error("Can't open %s\n", dev);
12214236Smckusick 		return (0);
12224236Smckusick 	}
12234236Smckusick 	if (preen == 0)
12244236Smckusick 		printf("** %s", dev);
12254236Smckusick 	if (nflag || (dfile.wfdes = open(dev, 1)) < 0) {
12264236Smckusick 		dfile.wfdes = -1;
12274236Smckusick 		if (preen)
12284236Smckusick 			pfatal("NO WRITE ACCESS");
12294236Smckusick 		printf(" (NO WRITE)");
12304236Smckusick 	}
12314236Smckusick 	if (preen == 0)
12324236Smckusick 		printf("\n");
12334465Smckusic 	fixcg = 0; inosumbad = 0; offsumbad = 0; frsumbad = 0;
12344236Smckusick 	dfile.mod = 0;
12354236Smckusick 	n_files = n_blks = n_ffree = n_bfree = 0;
12364236Smckusick 	muldup = enddup = &duplist[0];
12374236Smckusick 	badlnp = &badlncnt[0];
12384236Smckusick 	lfdir = 0;
12394236Smckusick 	rplyflag = 0;
12404236Smckusick 	initbarea(&sblk);
12414236Smckusick 	initbarea(&fileblk);
12424236Smckusick 	initbarea(&inoblk);
12434236Smckusick 	initbarea(&cgblk);
12445347Smckusic 	if (bread(&dfile, &sblock, super, SBSIZE) == 0)
12454236Smckusick 		return (0);
12464465Smckusic 	sblk.b_bno = super;
12475347Smckusic 	sblk.b_size = SBSIZE;
12485363Smckusic 	/*
12495363Smckusic 	 * run a few consistency checks of the super block
12505363Smckusic 	 */
12514236Smckusick 	if (sblock.fs_magic != FS_MAGIC)
12524236Smckusick 		{ badsb("MAGIC NUMBER WRONG"); return (0); }
12534236Smckusick 	if (sblock.fs_ncg < 1)
12544236Smckusick 		{ badsb("NCG OUT OF RANGE"); return (0); }
12554236Smckusick 	if (sblock.fs_cpg < 1 || sblock.fs_cpg > MAXCPG)
12564236Smckusick 		{ badsb("CPG OUT OF RANGE"); return (0); }
12574236Smckusick 	if (sblock.fs_nsect < 1)
12584236Smckusick 		{ badsb("NSECT < 1"); return (0); }
12594236Smckusick 	if (sblock.fs_ntrak < 1)
12604236Smckusick 		{ badsb("NTRAK < 1"); return (0); }
12615363Smckusic 	if (sblock.fs_spc != sblock.fs_nsect * sblock.fs_ntrak)
12625363Smckusic 		{ badsb("SPC DOES NOT JIVE w/NTRAK*NSECT"); return (0); }
12635363Smckusic 	if (sblock.fs_ipg % INOPB(&sblock))
12645363Smckusic 		{ badsb("INODES NOT MULTIPLE OF A BLOCK"); return (0); }
12655381Smckusic 	if (cgdmin(&sblock, 0) >= sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock))
12664236Smckusick 		{ badsb("IMPLIES MORE INODE THAN DATA BLOCKS"); return (0); }
12675363Smckusic 	if (sblock.fs_ncg * sblock.fs_cpg < sblock.fs_ncyl ||
12685363Smckusic 	    (sblock.fs_ncg - 1) * sblock.fs_cpg >= sblock.fs_ncyl)
12694236Smckusick 		{ badsb("NCYL DOES NOT JIVE WITH NCG*CPG"); return (0); }
12705325Smckusic 	if (sblock.fs_fpg != sblock.fs_cpg * sblock.fs_spc / NSPF(&sblock))
12714236Smckusick 		{ badsb("FPG DOES NOT JIVE WITH CPG & SPC"); return (0); }
12725363Smckusic 	if (sblock.fs_size * NSPF(&sblock) <=
12735363Smckusic 	    (sblock.fs_ncyl - 1) * sblock.fs_spc)
12744236Smckusick 		{ badsb("SIZE PREPOSTEROUSLY SMALL"); return (0); }
12755363Smckusic 	if (sblock.fs_size * NSPF(&sblock) > sblock.fs_ncyl * sblock.fs_spc)
12764236Smckusick 		{ badsb("SIZE PREPOSTEROUSLY LARGE"); return (0); }
12774236Smckusick 	/* rest we COULD repair... */
12784236Smckusick 	if (sblock.fs_sblkno != SBLOCK)
12795363Smckusic 		{ badsb("SBLKNO CORRUPTED"); return (0); }
12805363Smckusic 	if (sblock.fs_cblkno !=
12815363Smckusic 	    roundup(howmany(BBSIZE + SBSIZE, sblock.fs_fsize), sblock.fs_frag))
12825363Smckusic 		{ badsb("CBLKNO CORRUPTED"); return (0); }
12835363Smckusic 	if (sblock.fs_iblkno != sblock.fs_cblkno + sblock.fs_frag)
12845363Smckusic 		{ badsb("IBLKNO CORRUPTED"); return (0); }
12855363Smckusic 	if (sblock.fs_dblkno !=
12865363Smckusic 	    sblock.fs_iblkno + sblock.fs_ipg / INOPF(&sblock))
12875363Smckusic 		{ badsb("DBLKNO CORRUPTED"); return (0); }
12885337Smckusic 	if (sblock.fs_cgsize !=
12895347Smckusic 	    roundup(sizeof(struct cg) + howmany(sblock.fs_fpg, NBBY),
12905347Smckusic 	    sblock.fs_fsize))
12914236Smckusick 		{ badsb("CGSIZE INCORRECT"); return (0); }
12925325Smckusic 	if (sblock.fs_cssize != sblock.fs_ncg * sizeof(struct csum))
12934236Smckusick 		{ badsb("CSSIZE INCORRECT"); return (0); }
12944236Smckusick 	fmax = sblock.fs_size;
12954236Smckusick 	imax = sblock.fs_ncg * sblock.fs_ipg;
12965363Smckusic 	/*
12975363Smckusic 	 * allocate the necessary maps
12985363Smckusic 	 */
12994236Smckusick 	bmapsz = roundup(howmany(fmax, NBBY), sizeof(short));
13004236Smckusick 	blkmap = (char *)calloc(bmapsz, sizeof (char));
13014236Smckusick 	freemap = (char *)calloc(bmapsz, sizeof (char));
13024236Smckusick 	statemap = (char *)calloc(imax+1, sizeof(char));
13034236Smckusick 	lncntp = (short *)calloc(imax+1, sizeof(short));
13044236Smckusick 
13054465Smckusic 	startinum = imax + 1;
13064236Smckusick 	return (1);
13074236Smckusick 
13084236Smckusick badsb:
13094236Smckusick 	ckfini();
13104236Smckusick 	return (0);
13114236Smckusick }
13124236Smckusick 
13134236Smckusick badsb(s)
13144236Smckusick 	char *s;
13154236Smckusick {
13164236Smckusick 
13174236Smckusick 	if (preen)
13184236Smckusick 		printf("%s: ", devname);
13194236Smckusick 	printf("BAD SUPER BLOCK: %s\n", s);
13204236Smckusick 	pwarn("USE -b OPTION TO FSCK TO SPECIFY LOCATION OF AN ALTERNATE\n");
13214236Smckusick 	pfatal("SUPER-BLOCK TO SUPPLY NEEDED INFORMATION; SEE fsck(8).\n");
13224236Smckusick }
13234236Smckusick 
13244236Smckusick DINODE *
13254236Smckusick ginode()
13264236Smckusick {
13274236Smckusick 	daddr_t iblk;
13284236Smckusick 
13295337Smckusic 	if (inum < ROOTINO || inum > imax)
13304236Smckusick 		return (NULL);
13315325Smckusic 	if (inum < startinum || inum >= startinum + INOPB(&sblock)) {
13325381Smckusic 		iblk = itod(&sblock, inum);
13335325Smckusic 		if (getblk(&inoblk, iblk, sblock.fs_bsize) == NULL) {
13344236Smckusick 			return (NULL);
13354236Smckusick 		}
13365325Smckusic 		startinum = (inum / INOPB(&sblock)) * INOPB(&sblock);
13374236Smckusick 	}
13385325Smckusic 	return (&inoblk.b_un.b_dinode[inum % INOPB(&sblock)]);
13394236Smckusick }
13404236Smckusick 
13414236Smckusick ftypeok(dp)
13424236Smckusick 	DINODE *dp;
13434236Smckusick {
13444236Smckusick 	switch (dp->di_mode & IFMT) {
13454236Smckusick 
13464236Smckusick 	case IFDIR:
13474236Smckusick 	case IFREG:
13484236Smckusick 	case IFBLK:
13494236Smckusick 	case IFCHR:
13504236Smckusick 	case IFMPC:
13514236Smckusick 	case IFMPB:
13524236Smckusick 		return (1);
13534236Smckusick 
13544236Smckusick 	default:
13554236Smckusick 		return (0);
13564236Smckusick 	}
13574236Smckusick }
13584236Smckusick 
13594236Smckusick reply(s)
13604236Smckusick 	char *s;
13614236Smckusick {
13624236Smckusick 	char line[80];
13634236Smckusick 
13644236Smckusick 	if (preen)
13654236Smckusick 		pfatal("INTERNAL ERROR: GOT TO reply()");
13664236Smckusick 	rplyflag = 1;
13674236Smckusick 	printf("\n%s? ", s);
13684236Smckusick 	if (nflag || dfile.wfdes < 0) {
13694236Smckusick 		printf(" no\n\n");
13704236Smckusick 		return (0);
13714236Smckusick 	}
13724236Smckusick 	if (yflag) {
13734236Smckusick 		printf(" yes\n\n");
13744236Smckusick 		return (1);
13754236Smckusick 	}
13764236Smckusick 	if (getline(stdin, line, sizeof(line)) == EOF)
13774236Smckusick 		errexit("\n");
13784236Smckusick 	printf("\n");
13794236Smckusick 	if (line[0] == 'y' || line[0] == 'Y')
13804236Smckusick 		return (1);
13814236Smckusick 	else
13824236Smckusick 		return (0);
13834236Smckusick }
13844236Smckusick 
13854236Smckusick getline(fp, loc, maxlen)
13864236Smckusick 	FILE *fp;
13874236Smckusick 	char *loc;
13884236Smckusick {
13894236Smckusick 	register n;
13904236Smckusick 	register char *p, *lastloc;
13914236Smckusick 
13924236Smckusick 	p = loc;
13934236Smckusick 	lastloc = &p[maxlen-1];
13944236Smckusick 	while ((n = getc(fp)) != '\n') {
13954236Smckusick 		if (n == EOF)
13964236Smckusick 			return (EOF);
13974236Smckusick 		if (!isspace(n) && p < lastloc)
13984236Smckusick 			*p++ = n;
13994236Smckusick 	}
14004236Smckusick 	*p = 0;
14014236Smckusick 	return (p - loc);
14024236Smckusick }
14034236Smckusick 
14044236Smckusick BUFAREA *
14054236Smckusick getblk(bp, blk, size)
14064236Smckusick 	daddr_t blk;
14074236Smckusick 	register BUFAREA *bp;
14084236Smckusick 	int size;
14094236Smckusick {
14104236Smckusick 	register struct filecntl *fcp;
14115325Smckusic 	daddr_t dblk;
14124236Smckusick 
14134236Smckusick 	fcp = &dfile;
14145325Smckusic 	dblk = fsbtodb(&sblock, blk);
14155325Smckusic 	if (bp->b_bno == dblk)
14164236Smckusick 		return (bp);
14174236Smckusick 	flush(fcp, bp);
14185325Smckusic 	if (bread(fcp, bp->b_un.b_buf, dblk, size) != 0) {
14195325Smckusic 		bp->b_bno = dblk;
14204236Smckusick 		bp->b_size = size;
14214236Smckusick 		return (bp);
14224236Smckusick 	}
14234236Smckusick 	bp->b_bno = (daddr_t)-1;
14244236Smckusick 	return (NULL);
14254236Smckusick }
14264236Smckusick 
14274236Smckusick flush(fcp, bp)
14284236Smckusick 	struct filecntl *fcp;
14294236Smckusick 	register BUFAREA *bp;
14304236Smckusick {
14314236Smckusick 
14324236Smckusick 	if (bp->b_dirty)
14334236Smckusick 		bwrite(fcp, bp->b_un.b_buf, bp->b_bno, bp->b_size);
14344236Smckusick 	bp->b_dirty = 0;
14354236Smckusick }
14364236Smckusick 
14374236Smckusick rwerr(s, blk)
14384236Smckusick 	char *s;
14394236Smckusick 	daddr_t blk;
14404236Smckusick {
14414236Smckusick 
14424236Smckusick 	if (preen == 0)
14434236Smckusick 		printf("\n");
14444236Smckusick 	pfatal("CANNOT %s: BLK %ld", s, blk);
14454236Smckusick 	if (reply("CONTINUE") == 0)
14464236Smckusick 		errexit("Program terminated\n");
14474236Smckusick }
14484236Smckusick 
14494236Smckusick ckfini()
14504236Smckusick {
14514236Smckusick 
14524236Smckusick 	flush(&dfile, &fileblk);
14534236Smckusick 	flush(&dfile, &sblk);
14544465Smckusic 	if (sblk.b_bno != SBLOCK) {
14554465Smckusic 		sblk.b_bno = SBLOCK;
14564465Smckusic 		sbdirty();
14574465Smckusic 		flush(&dfile, &sblk);
14584465Smckusic 	}
14594236Smckusick 	flush(&dfile, &inoblk);
14604236Smckusick 	close(dfile.rfdes);
14614236Smckusick 	close(dfile.wfdes);
14624236Smckusick }
14634236Smckusick 
14644236Smckusick pinode()
14654236Smckusick {
14664236Smckusick 	register DINODE *dp;
14674236Smckusick 	register char *p;
1468*5877Smckusic 	char uidbuf[BUFSIZ];
14694236Smckusick 	char *ctime();
14704236Smckusick 
14714236Smckusick 	printf(" I=%u ", inum);
14724236Smckusick 	if ((dp = ginode()) == NULL)
14734236Smckusick 		return;
14744236Smckusick 	printf(" OWNER=");
14754236Smckusick 	if (getpw((int)dp->di_uid, uidbuf) == 0) {
14764236Smckusick 		for (p = uidbuf; *p != ':'; p++);
14774236Smckusick 		*p = 0;
14784236Smckusick 		printf("%s ", uidbuf);
14794236Smckusick 	}
14804236Smckusick 	else {
14814236Smckusick 		printf("%d ", dp->di_uid);
14824236Smckusick 	}
14834236Smckusick 	printf("MODE=%o\n", dp->di_mode);
14844236Smckusick 	if (preen)
14854236Smckusick 		printf("%s: ", devname);
14864236Smckusick 	printf("SIZE=%ld ", dp->di_size);
14874236Smckusick 	p = ctime(&dp->di_mtime);
14884236Smckusick 	printf("MTIME=%12.12s %4.4s ", p+4, p+20);
14894236Smckusick }
14904236Smckusick 
14914236Smckusick copy(fp, tp, size)
14924236Smckusick 	register char *tp, *fp;
14934236Smckusick 	unsigned size;
14944236Smckusick {
14954236Smckusick 
14964236Smckusick 	while (size--)
14974236Smckusick 		*tp++ = *fp++;
14984236Smckusick }
14994236Smckusick 
15004236Smckusick makecg()
15014236Smckusick {
15024465Smckusic 	int c, blk;
15034236Smckusick 	daddr_t dbase, d, dmin, dmax;
15044236Smckusick 	long i, j, s;
15055401Smckusic 	int x;
15064236Smckusick 	register struct csum *cs;
15074465Smckusic 	register DINODE *dp;
15084236Smckusick 
15094789Smckusic 	sblock.fs_cstotal.cs_nbfree = 0;
15104789Smckusic 	sblock.fs_cstotal.cs_nffree = 0;
15114789Smckusic 	sblock.fs_cstotal.cs_nifree = 0;
15124789Smckusic 	sblock.fs_cstotal.cs_ndir = 0;
15135325Smckusic 	for (i = 0; i < howmany(sblock.fs_cssize, sblock.fs_bsize); i++) {
15145325Smckusic 		sblock.fs_csp[i] = (struct csum *)calloc(1, sblock.fs_bsize);
15155097Smckusic 		getblk((char *)sblock.fs_csp[i],
15165325Smckusic 		    sblock.fs_csaddr + (i * sblock.fs_frag), sblock.fs_bsize);
15175097Smckusic 	}
15184236Smckusick 	for (c = 0; c < sblock.fs_ncg; c++) {
15195381Smckusic 		dbase = cgbase(&sblock, c);
15204236Smckusick 		dmax = dbase + sblock.fs_fpg;
15215409Smckusic 		if (dmax > sblock.fs_size) {
15225409Smckusic 			for ( ; dmax >= sblock.fs_size ; dmax--)
15235401Smckusic 				clrbit(cgrp.cg_free, dmax - dbase);
15245409Smckusic 			dmax++;
15255409Smckusic 		}
15265401Smckusic 		dmin = sblock.fs_dblkno;
15275325Smckusic 		cs = &sblock.fs_cs(&sblock, c);
15284236Smckusick 		cgrp.cg_time = time(0);
15294236Smckusick 		cgrp.cg_magic = CG_MAGIC;
15304236Smckusick 		cgrp.cg_cgx = c;
15314236Smckusick 		cgrp.cg_ncyl = sblock.fs_cpg;
15324236Smckusick 		cgrp.cg_niblk = sblock.fs_ipg;
15334236Smckusick 		cgrp.cg_ndblk = dmax - dbase;
15344789Smckusic 		cgrp.cg_cs.cs_ndir = 0;
15354789Smckusic 		cgrp.cg_cs.cs_nffree = 0;
15364789Smckusic 		cgrp.cg_cs.cs_nbfree = 0;
15374789Smckusic 		cgrp.cg_cs.cs_nifree = 0;
15384465Smckusic 		cgrp.cg_rotor = dmin;
15394465Smckusic 		cgrp.cg_frotor = dmin;
15404258Smckusic 		cgrp.cg_irotor = 0;
15415325Smckusic 		for (i = 0; i < sblock.fs_frag; i++)
15424465Smckusic 			cgrp.cg_frsum[i] = 0;
15434236Smckusick 		inum = sblock.fs_ipg * c;
15444465Smckusic 		for (i = 0; i < sblock.fs_ipg; inum++, i++) {
15454465Smckusic 			dp = ginode();
15464465Smckusic 			if (dp == NULL)
15474465Smckusic 				continue;
15484465Smckusic 			if (ALLOC) {
1549*5877Smckusic 				if (DIRCT)
15504789Smckusic 					cgrp.cg_cs.cs_ndir++;
15514465Smckusic 				setbit(cgrp.cg_iused, i);
15524465Smckusic 				continue;
15534465Smckusic 			}
15544789Smckusic 			cgrp.cg_cs.cs_nifree++;
15554236Smckusick 			clrbit(cgrp.cg_iused, i);
15564236Smckusick 		}
15574236Smckusick 		while (i < MAXIPG) {
15584236Smckusick 			clrbit(cgrp.cg_iused, i);
15594236Smckusick 			i++;
15604236Smckusick 		}
15615371Smckusic 		for (s = 0; s < MAXCPG; s++) {
15625371Smckusic 			cgrp.cg_btot[s] = 0;
15634236Smckusick 			for (i = 0; i < NRPOS; i++)
15644236Smckusick 				cgrp.cg_b[s][i] = 0;
15655371Smckusic 		}
15664236Smckusick 		if (c == 0) {
15675325Smckusic 			dmin += howmany(sblock.fs_cssize, sblock.fs_bsize) * sblock.fs_frag;
15684236Smckusick 		}
15694236Smckusick 		for (d = 0; d < dmin; d++)
15704236Smckusick 			clrbit(cgrp.cg_free, d);
15715325Smckusic 		for (; (d + sblock.fs_frag) <= dmax - dbase; d += sblock.fs_frag) {
15724236Smckusick 			j = 0;
15735325Smckusic 			for (i = 0; i < sblock.fs_frag; i++) {
15744236Smckusick 				if (!getbmap(dbase+d+i)) {
15754236Smckusick 					setbit(cgrp.cg_free, d+i);
15764236Smckusick 					j++;
15774236Smckusick 				} else
15784236Smckusick 					clrbit(cgrp.cg_free, d+i);
15794236Smckusick 			}
15805325Smckusic 			if (j == sblock.fs_frag) {
15814789Smckusic 				cgrp.cg_cs.cs_nbfree++;
15825371Smckusic 				cgrp.cg_btot[cbtocylno(&sblock, d)]++;
15835363Smckusic 				cgrp.cg_b[cbtocylno(&sblock, d)]
15845363Smckusic 				    [cbtorpos(&sblock, d)]++;
15854465Smckusic 			} else if (j > 0) {
15864789Smckusic 				cgrp.cg_cs.cs_nffree += j;
15874465Smckusic 				blk = ((cgrp.cg_free[d / NBBY] >> (d % NBBY)) &
15885325Smckusic 				       (0xff >> (NBBY - sblock.fs_frag)));
15895325Smckusic 				fragacct(&sblock, blk, cgrp.cg_frsum, 1);
15904465Smckusic 			}
15914236Smckusick 		}
15925401Smckusic 		x = 0;
15934465Smckusic 		for (j = d; d < dmax - dbase; d++) {
15944236Smckusick 			if (!getbmap(dbase+d)) {
15954236Smckusick 				setbit(cgrp.cg_free, d);
15964789Smckusic 				cgrp.cg_cs.cs_nffree++;
15975401Smckusic 				x++;
15984236Smckusick 			} else
15994236Smckusick 				clrbit(cgrp.cg_free, d);
16004236Smckusick 		}
16014465Smckusic 		if (j != d) {
16024465Smckusic 			blk = ((cgrp.cg_free[j / NBBY] >> (j % NBBY)) &
16035325Smckusic 			       (0xff >> (NBBY - sblock.fs_frag)));
16045325Smckusic 			fragacct(&sblock, blk, cgrp.cg_frsum, 1);
16054465Smckusic 		}
16065325Smckusic 		for (; d < MAXBPG(&sblock); d++)
16074236Smckusick 			clrbit(cgrp.cg_free, d);
16084789Smckusic 		sblock.fs_cstotal.cs_nffree += cgrp.cg_cs.cs_nffree;
16094789Smckusic 		sblock.fs_cstotal.cs_nbfree += cgrp.cg_cs.cs_nbfree;
16104789Smckusic 		sblock.fs_cstotal.cs_nifree += cgrp.cg_cs.cs_nifree;
16114789Smckusic 		sblock.fs_cstotal.cs_ndir += cgrp.cg_cs.cs_ndir;
16124789Smckusic 		*cs = cgrp.cg_cs;
16135381Smckusic 		bwrite(&dfile, &cgrp, fsbtodb(&sblock, cgtod(&sblock, c)),
16145325Smckusic 			roundup(sblock.fs_cgsize, DEV_BSIZE));
16154236Smckusick 	}
16165325Smckusic 	for (i = 0; i < howmany(sblock.fs_cssize, sblock.fs_bsize); i++) {
16175097Smckusic 		bwrite(&dfile, (char *)sblock.fs_csp[i],
16185325Smckusic 		    fsbtodb(&sblock, sblock.fs_csaddr + (i * sblock.fs_frag)),
16195325Smckusic 		    sblock.fs_bsize);
16205097Smckusic 	}
16214236Smckusick 	sblock.fs_ronly = 0;
16224236Smckusick 	sblock.fs_fmod = 0;
16234236Smckusick 	sbdirty();
16244236Smckusick }
16254236Smckusick 
16264465Smckusic /*
16274465Smckusic  * update the frsum fields to reflect addition or deletion
16284465Smckusic  * of some frags
16294465Smckusic  */
16305325Smckusic fragacct(fs, fragmap, fraglist, cnt)
16315325Smckusic 	struct fs *fs;
16324470Smckusic 	int fragmap;
16334789Smckusic 	long fraglist[];
16344465Smckusic 	int cnt;
16354465Smckusic {
16364465Smckusic 	int inblk;
16374465Smckusic 	register int field, subfield;
16384465Smckusic 	register int siz, pos;
16394465Smckusic 
16405325Smckusic 	inblk = (int)(fragtbl[fs->fs_frag][fragmap] << 1);
16414465Smckusic 	fragmap <<= 1;
16425325Smckusic 	for (siz = 1; siz < fs->fs_frag; siz++) {
16434465Smckusic 		if (((1 << siz) & inblk) == 0)
16444465Smckusic 			continue;
16454465Smckusic 		field = around[siz];
16464465Smckusic 		subfield = inside[siz];
16475325Smckusic 		for (pos = siz; pos <= fs->fs_frag; pos++) {
16484465Smckusic 			if ((fragmap & field) == subfield) {
16494465Smckusic 				fraglist[siz] += cnt;
16504465Smckusic 				pos += siz;
16514465Smckusic 				field <<= siz;
16524465Smckusic 				subfield <<= siz;
16534465Smckusic 			}
16544465Smckusic 			field <<= 1;
16554465Smckusic 			subfield <<= 1;
16564465Smckusic 		}
16574465Smckusic 	}
16584465Smckusic }
16594465Smckusic 
16604236Smckusick findino(dirp)
16614236Smckusick 	register DIRECT *dirp;
16624236Smckusick {
16634236Smckusick 	if (dirp->d_ino == 0)
16644236Smckusick 		return (KEEPON);
1665*5877Smckusic 	if (!strcmp(dirp->d_name, srchname)) {
1666*5877Smckusic 		if (dirp->d_ino >= ROOTINO && dirp->d_ino <= imax)
1667*5877Smckusic 			parentdir = dirp->d_ino;
1668*5877Smckusic 		return (STOP);
16694236Smckusick 	}
16704236Smckusick 	return (KEEPON);
16714236Smckusick }
16724236Smckusick 
16734236Smckusick mkentry(dirp)
16744236Smckusick 	register DIRECT *dirp;
16754236Smckusick {
16764236Smckusick 	register ino_t in;
16774236Smckusick 	register char *p;
16784236Smckusick 
16794236Smckusick 	if (dirp->d_ino)
16804236Smckusick 		return (KEEPON);
16814236Smckusick 	dirp->d_ino = orphan;
16824236Smckusick 	in = orphan;
16834236Smckusick 	p = &dirp->d_name[8];
16844236Smckusick 	*--p = 0;
16854236Smckusick 	while (p > dirp->d_name) {
16864236Smckusick 		*--p = (in % 10) + '0';
16874236Smckusick 		in /= 10;
16884236Smckusick 	}
16894236Smckusick 	*p = '#';
16904236Smckusick 	return (ALTERD|STOP);
16914236Smckusick }
16924236Smckusick 
16934236Smckusick chgdd(dirp)
16944236Smckusick 	register DIRECT *dirp;
16954236Smckusick {
16964236Smckusick 	if (dirp->d_name[0] == '.' && dirp->d_name[1] == '.' &&
16974236Smckusick 	dirp->d_name[2] == 0) {
16984236Smckusick 		dirp->d_ino = lfdir;
16994236Smckusick 		return (ALTERD|STOP);
17004236Smckusick 	}
17014236Smckusick 	return (KEEPON);
17024236Smckusick }
17034236Smckusick 
17044236Smckusick linkup()
17054236Smckusick {
17064236Smckusick 	register DINODE *dp;
17074236Smckusick 	register lostdir;
17084236Smckusick 	register ino_t pdir;
17094236Smckusick 
17104236Smckusick 	if ((dp = ginode()) == NULL)
17114236Smckusick 		return (0);
1712*5877Smckusic 	lostdir = DIRCT;
17134236Smckusick 	pdir = parentdir;
17144236Smckusick 	pwarn("UNREF %s ", lostdir ? "DIR" : "FILE");
17154236Smckusick 	pinode();
17164236Smckusick 	if (preen && dp->di_size == 0)
17174236Smckusick 		return (0);
17184236Smckusick 	if (preen)
17194236Smckusick 		printf(" (RECONNECTED)\n");
17204236Smckusick 	else
17214236Smckusick 		if (reply("RECONNECT") == 0)
17224236Smckusick 			return (0);
17234236Smckusick 	orphan = inum;
17244236Smckusick 	if (lfdir == 0) {
17254236Smckusick 		inum = ROOTINO;
17264236Smckusick 		if ((dp = ginode()) == NULL) {
17274236Smckusick 			inum = orphan;
17284236Smckusick 			return (0);
17294236Smckusick 		}
17304236Smckusick 		pfunc = findino;
17314236Smckusick 		srchname = lfname;
17324236Smckusick 		filsize = dp->di_size;
17334236Smckusick 		parentdir = 0;
17344236Smckusick 		ckinode(dp, DATA);
17354236Smckusick 		inum = orphan;
17364236Smckusick 		if ((lfdir = parentdir) == 0) {
17374236Smckusick 			pfatal("SORRY. NO lost+found DIRECTORY");
17384236Smckusick 			printf("\n\n");
17394236Smckusick 			return (0);
17404236Smckusick 		}
17414236Smckusick 	}
17424236Smckusick 	inum = lfdir;
1743*5877Smckusic 	if ((dp = ginode()) == NULL || !DIRCT || getstate() != FSTATE) {
17444236Smckusick 		inum = orphan;
17454236Smckusick 		pfatal("SORRY. NO lost+found DIRECTORY");
17464236Smckusick 		printf("\n\n");
17474236Smckusick 		return (0);
17484236Smckusick 	}
17495325Smckusic 	if (dp->di_size % sblock.fs_bsize) {
17505325Smckusic 		dp->di_size = roundup(dp->di_size, sblock.fs_bsize);
17514236Smckusick 		inodirty();
17524236Smckusick 	}
17534236Smckusick 	filsize = dp->di_size;
17544236Smckusick 	inum = orphan;
17554236Smckusick 	pfunc = mkentry;
17564236Smckusick 	if ((ckinode(dp, DATA) & ALTERD) == 0) {
17574236Smckusick 		pfatal("SORRY. NO SPACE IN lost+found DIRECTORY");
17584236Smckusick 		printf("\n\n");
17594236Smckusick 		return (0);
17604236Smckusick 	}
17614236Smckusick 	declncnt();
17624236Smckusick 	if (lostdir) {
17634236Smckusick 		pfunc = chgdd;
17644236Smckusick 		dp = ginode();
17654236Smckusick 		filsize = dp->di_size;
17664236Smckusick 		ckinode(dp, DATA);
17674236Smckusick 		inum = lfdir;
17684236Smckusick 		if ((dp = ginode()) != NULL) {
17694236Smckusick 			dp->di_nlink++;
17704236Smckusick 			inodirty();
17714236Smckusick 			setlncnt(getlncnt()+1);
17724236Smckusick 		}
17734236Smckusick 		inum = orphan;
17744236Smckusick 		pwarn("DIR I=%u CONNECTED. ", orphan);
17754236Smckusick 		printf("PARENT WAS I=%u\n", pdir);
17764236Smckusick 		if (preen == 0)
17774236Smckusick 			printf("\n");
17784236Smckusick 	}
17794236Smckusick 	return (1);
17804236Smckusick }
17814236Smckusick 
17824236Smckusick bread(fcp, buf, blk, size)
17834236Smckusick 	daddr_t blk;
17844236Smckusick 	register struct filecntl *fcp;
17854236Smckusick 	register size;
17864236Smckusick 	char *buf;
17874236Smckusick {
17885325Smckusic 	if (lseek(fcp->rfdes, blk * DEV_BSIZE, 0) < 0)
17894236Smckusick 		rwerr("SEEK", blk);
17904236Smckusick 	else if (read(fcp->rfdes, buf, size) == size)
17914236Smckusick 		return (1);
17924236Smckusick 	rwerr("READ", blk);
17934236Smckusick 	return (0);
17944236Smckusick }
17954236Smckusick 
17964236Smckusick bwrite(fcp, buf, blk, size)
17974236Smckusick 	daddr_t blk;
17984236Smckusick 	register struct filecntl *fcp;
17994236Smckusick 	register size;
18004236Smckusick 	char *buf;
18014236Smckusick {
18024236Smckusick 
18034236Smckusick 	if (fcp->wfdes < 0)
18044236Smckusick 		return (0);
18055325Smckusic 	if (lseek(fcp->wfdes, blk * DEV_BSIZE, 0) < 0)
18064236Smckusick 		rwerr("SEEK", blk);
18074236Smckusick 	else if (write(fcp->wfdes, buf, size) == size) {
18084236Smckusick 		fcp->mod = 1;
18094236Smckusick 		return (1);
18104236Smckusick 	}
18114236Smckusick 	rwerr("WRITE", blk);
18124236Smckusick 	return (0);
18134236Smckusick }
18144236Smckusick 
18154236Smckusick catch()
18164236Smckusick {
18174236Smckusick 
18184236Smckusick 	ckfini();
18194236Smckusick 	exit(12);
18204236Smckusick }
18215325Smckusic 
18225325Smckusic /*
18235325Smckusic  * block operations
18245325Smckusic  */
18255325Smckusic 
18265325Smckusic isblock(fs, cp, h)
18275325Smckusic 	struct fs *fs;
18285325Smckusic 	unsigned char *cp;
18295325Smckusic 	int h;
18305325Smckusic {
18315325Smckusic 	unsigned char mask;
18325325Smckusic 
18335325Smckusic 	switch (fs->fs_frag) {
18345325Smckusic 	case 8:
18355325Smckusic 		return (cp[h] == 0xff);
18365325Smckusic 	case 4:
18375325Smckusic 		mask = 0x0f << ((h & 0x1) << 2);
18385325Smckusic 		return ((cp[h >> 1] & mask) == mask);
18395325Smckusic 	case 2:
18405325Smckusic 		mask = 0x03 << ((h & 0x3) << 1);
18415325Smckusic 		return ((cp[h >> 2] & mask) == mask);
18425325Smckusic 	case 1:
18435325Smckusic 		mask = 0x01 << (h & 0x7);
18445325Smckusic 		return ((cp[h >> 3] & mask) == mask);
18455325Smckusic 	default:
18465381Smckusic 		error("isblock bad fs_frag %d\n", fs->fs_frag);
18475381Smckusic 		return (0);
18485325Smckusic 	}
18495325Smckusic }
1850