xref: /illumos-gate/usr/src/cmd/fs.d/ufs/fsck/pass5.c (revision ef31a55c)
17c478bd9Sstevel@tonic-gate /*
2*ef31a55cSvk  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980, 1986, 1990 The Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
147c478bd9Sstevel@tonic-gate  * provided that: (1) source distributions retain this entire copyright
157c478bd9Sstevel@tonic-gate  * notice and comment, and (2) distributions including binaries display
167c478bd9Sstevel@tonic-gate  * the following acknowledgement:  ``This product includes software
177c478bd9Sstevel@tonic-gate  * developed by the University of California, Berkeley and its contributors''
187c478bd9Sstevel@tonic-gate  * in the documentation or other materials provided with the distribution
197c478bd9Sstevel@tonic-gate  * and in all advertising materials mentioning features or use of this
207c478bd9Sstevel@tonic-gate  * software. Neither the name of the University nor the names of its
217c478bd9Sstevel@tonic-gate  * contributors may be used to endorse or promote products derived
227c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
237c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
247c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
257c478bd9Sstevel@tonic-gate  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
297c478bd9Sstevel@tonic-gate 
30355d6bb5Sswilcox #include <stdio.h>
31355d6bb5Sswilcox #include <stdlib.h>
32355d6bb5Sswilcox #include <unistd.h>
33355d6bb5Sswilcox #include <string.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
357c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
367c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_fs.h>
377c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
387c478bd9Sstevel@tonic-gate #include <sys/fs/ufs_inode.h>
397c478bd9Sstevel@tonic-gate #include "fsck.h"
407c478bd9Sstevel@tonic-gate 
41355d6bb5Sswilcox static int check_maps(uchar_t *, uchar_t *, int, int, char *, int, int);
42355d6bb5Sswilcox 
43355d6bb5Sswilcox void
44355d6bb5Sswilcox pass5(void)
457c478bd9Sstevel@tonic-gate {
46355d6bb5Sswilcox 	caddr_t err;
47355d6bb5Sswilcox 	int32_t c, blk, frags;
48355d6bb5Sswilcox 	int cg_fatal;
497c478bd9Sstevel@tonic-gate 	size_t	basesize, sumsize, mapsize;
50355d6bb5Sswilcox 	int excessdirs;
51355d6bb5Sswilcox 	int inomapsize, blkmapsize;
52355d6bb5Sswilcox 	int update_csums, bad_csum, update_bitmaps;
537c478bd9Sstevel@tonic-gate 	struct fs *fs = &sblock;
547c478bd9Sstevel@tonic-gate 	struct cg *cg = &cgrp;
557c478bd9Sstevel@tonic-gate 	diskaddr_t dbase, dmax;
567c478bd9Sstevel@tonic-gate 	diskaddr_t d;
577c478bd9Sstevel@tonic-gate 	uint64_t i, j;
587c478bd9Sstevel@tonic-gate 	struct csum *cs;
59355d6bb5Sswilcox 	struct csum backup_cs;
607c478bd9Sstevel@tonic-gate 	time_t now;
617c478bd9Sstevel@tonic-gate 	struct csum cstotal;
627c478bd9Sstevel@tonic-gate 	struct inodesc idesc;
63355d6bb5Sswilcox 	union {				/* keep lint happy about alignment */
64355d6bb5Sswilcox 		struct cg cg;		/* the rest of buf has the bitmaps */
65355d6bb5Sswilcox 		char buf[MAXBSIZE];
66355d6bb5Sswilcox 	} u;
67355d6bb5Sswilcox 	caddr_t buf = u.buf;
68355d6bb5Sswilcox 	struct cg *newcg = &u.cg;
697c478bd9Sstevel@tonic-gate 
70355d6bb5Sswilcox 	(void) memset((void *)buf, 0, sizeof (u.buf));
717c478bd9Sstevel@tonic-gate 	newcg->cg_niblk = fs->fs_ipg;
72355d6bb5Sswilcox 
73355d6bb5Sswilcox 	if (fs->fs_postblformat != FS_DYNAMICPOSTBLFMT) {
74355d6bb5Sswilcox 		pfatal("UNSUPPORTED ROTATIONAL TABLE FORMAT %d\n",
757c478bd9Sstevel@tonic-gate 			fs->fs_postblformat);
76355d6bb5Sswilcox 		errexit("Program terminated.");
77355d6bb5Sswilcox 		/* NOTREACHED */
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
80355d6bb5Sswilcox 	/* LINTED this subtraction can't overflow and is int32-aligned */
81355d6bb5Sswilcox 	basesize = &newcg->cg_space[0] - (uchar_t *)newcg;
82355d6bb5Sswilcox 
83355d6bb5Sswilcox 	/*
84355d6bb5Sswilcox 	 * We reserve the space for the old rotation summary
85355d6bb5Sswilcox 	 * tables for the benefit of old kernels, but do not
86355d6bb5Sswilcox 	 * maintain them in modern kernels. In time, they could
87355d6bb5Sswilcox 	 * theoretically go away, if we wanted to deal with
88355d6bb5Sswilcox 	 * changing the on-disk format.
89355d6bb5Sswilcox 	 */
90355d6bb5Sswilcox 
91355d6bb5Sswilcox 	/*
92355d6bb5Sswilcox 	 * Note that we don't use any of the cg_*() macros until
93355d6bb5Sswilcox 	 * after cg_sanity() has approved of what we've got.
94355d6bb5Sswilcox 	 */
95355d6bb5Sswilcox 	newcg->cg_btotoff = basesize;
96355d6bb5Sswilcox 	newcg->cg_boff = newcg->cg_btotoff + fs->fs_cpg * sizeof (daddr32_t);
97355d6bb5Sswilcox 	newcg->cg_iusedoff = newcg->cg_boff +
98355d6bb5Sswilcox 		fs->fs_cpg * fs->fs_nrpos * sizeof (uint16_t);
99355d6bb5Sswilcox 	(void) memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
100355d6bb5Sswilcox 
101355d6bb5Sswilcox 	inomapsize = howmany(fs->fs_ipg, NBBY);
102355d6bb5Sswilcox 	newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
103355d6bb5Sswilcox 	blkmapsize = howmany(fs->fs_fpg, NBBY);
104355d6bb5Sswilcox 	newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
105355d6bb5Sswilcox 	newcg->cg_magic = CG_MAGIC;
106355d6bb5Sswilcox 
107355d6bb5Sswilcox 	sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
108355d6bb5Sswilcox 	mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
109355d6bb5Sswilcox 
110355d6bb5Sswilcox 	init_inodesc(&idesc);
1117c478bd9Sstevel@tonic-gate 	idesc.id_type = ADDR;
112355d6bb5Sswilcox 	(void) memset((void *)&cstotal, 0, sizeof (struct csum));
113355d6bb5Sswilcox 	now = time(NULL);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * If the last fragments in the file system don't make up a
1177c478bd9Sstevel@tonic-gate 	 * full file system block, mark the bits in the blockmap
1187c478bd9Sstevel@tonic-gate 	 * that correspond to those missing fragments as "allocated",
1197c478bd9Sstevel@tonic-gate 	 * so that the last block doesn't get counted as a free block
1207c478bd9Sstevel@tonic-gate 	 * and those missing fragments don't get counted as free frags.
1217c478bd9Sstevel@tonic-gate 	 */
1227c478bd9Sstevel@tonic-gate 	j = blknum(fs, (uint64_t)fs->fs_size + fs->fs_frag - 1);
1237c478bd9Sstevel@tonic-gate 	for (i = fs->fs_size; i < j; i++)
1247c478bd9Sstevel@tonic-gate 		setbmap(i);
125355d6bb5Sswilcox 
126355d6bb5Sswilcox 	/*
127355d6bb5Sswilcox 	 * The cg summaries are not always updated when using
128355d6bb5Sswilcox 	 * logging.  Since we're really concerned with getting a
129355d6bb5Sswilcox 	 * sane filesystem, rather than in trying to debug UFS
130355d6bb5Sswilcox 	 * corner cases, logically we would just always recompute
131355d6bb5Sswilcox 	 * them.  However, it is disconcerting to users to be asked
132355d6bb5Sswilcox 	 * about updating the summaries when, from their point of
133355d6bb5Sswilcox 	 * view, there's been no indication of a problem up to this
134355d6bb5Sswilcox 	 * point.  So, only do it if we find a discrepancy.
135355d6bb5Sswilcox 	 */
136355d6bb5Sswilcox 	update_csums = -1;
137355d6bb5Sswilcox 	update_bitmaps = 0;
1387c478bd9Sstevel@tonic-gate 	for (c = 0; c < fs->fs_ncg; c++) {
139355d6bb5Sswilcox 		backup_cs = cstotal;
140355d6bb5Sswilcox 
141355d6bb5Sswilcox 		/*
142355d6bb5Sswilcox 		 * cg_sanity() will catch i/o errors for us.
143355d6bb5Sswilcox 		 */
144355d6bb5Sswilcox 		(void) getblk(&cgblk, (diskaddr_t)cgtod(fs, c),
145355d6bb5Sswilcox 		    (size_t)fs->fs_cgsize);
146355d6bb5Sswilcox 		err = cg_sanity(cg, c, &cg_fatal);
147355d6bb5Sswilcox 		if (err != NULL) {
148355d6bb5Sswilcox 			pfatal("CG %d: %s\n", c, err);
149355d6bb5Sswilcox 			free((void *)err);
150355d6bb5Sswilcox 			if (cg_fatal)
151355d6bb5Sswilcox 				errexit(
152355d6bb5Sswilcox 	    "Irreparable cylinder group header problem.  Program terminated.");
153355d6bb5Sswilcox 			if (reply("REPAIR") == 0)
154355d6bb5Sswilcox 				errexit("Program terminated.");
155355d6bb5Sswilcox 			fix_cg(cg, c);
156355d6bb5Sswilcox 		}
157355d6bb5Sswilcox 		/*
158355d6bb5Sswilcox 		 * If the on-disk timestamp is in the future, then it
159355d6bb5Sswilcox 		 * by definition is wrong.  Otherwise, if it's in
160355d6bb5Sswilcox 		 * the past, then use that value so that we don't
161355d6bb5Sswilcox 		 * declare a spurious mismatch.
162355d6bb5Sswilcox 		 */
1637c478bd9Sstevel@tonic-gate 		if (now > cg->cg_time)
1647c478bd9Sstevel@tonic-gate 			newcg->cg_time = cg->cg_time;
1657c478bd9Sstevel@tonic-gate 		else
1667c478bd9Sstevel@tonic-gate 			newcg->cg_time = now;
1677c478bd9Sstevel@tonic-gate 		newcg->cg_cgx = c;
168355d6bb5Sswilcox 		dbase = cgbase(fs, c);
169355d6bb5Sswilcox 		dmax = dbase + fs->fs_fpg;
170355d6bb5Sswilcox 		if (dmax > fs->fs_size)
171355d6bb5Sswilcox 			dmax = fs->fs_size;
172355d6bb5Sswilcox 		newcg->cg_ndblk = dmax - dbase;
1737c478bd9Sstevel@tonic-gate 		if (c == fs->fs_ncg - 1)
174*ef31a55cSvk 			newcg->cg_ncyl = fs->fs_ncyl - (fs->fs_cpg * c);
1757c478bd9Sstevel@tonic-gate 		else
1767c478bd9Sstevel@tonic-gate 			newcg->cg_ncyl = fs->fs_cpg;
1777c478bd9Sstevel@tonic-gate 		newcg->cg_niblk = sblock.fs_ipg;
1787c478bd9Sstevel@tonic-gate 		newcg->cg_cs.cs_ndir = 0;
1797c478bd9Sstevel@tonic-gate 		newcg->cg_cs.cs_nffree = 0;
1807c478bd9Sstevel@tonic-gate 		newcg->cg_cs.cs_nbfree = 0;
1817c478bd9Sstevel@tonic-gate 		newcg->cg_cs.cs_nifree = fs->fs_ipg;
1827c478bd9Sstevel@tonic-gate 		if ((cg->cg_rotor >= 0) && (cg->cg_rotor < newcg->cg_ndblk))
1837c478bd9Sstevel@tonic-gate 			newcg->cg_rotor = cg->cg_rotor;
1847c478bd9Sstevel@tonic-gate 		else
1857c478bd9Sstevel@tonic-gate 			newcg->cg_rotor = 0;
1867c478bd9Sstevel@tonic-gate 		if ((cg->cg_frotor >= 0) && (cg->cg_frotor < newcg->cg_ndblk))
1877c478bd9Sstevel@tonic-gate 			newcg->cg_frotor = cg->cg_frotor;
1887c478bd9Sstevel@tonic-gate 		else
1897c478bd9Sstevel@tonic-gate 			newcg->cg_frotor = 0;
1907c478bd9Sstevel@tonic-gate 		if ((cg->cg_irotor >= 0) && (cg->cg_irotor < newcg->cg_niblk))
1917c478bd9Sstevel@tonic-gate 			newcg->cg_irotor = cg->cg_irotor;
1927c478bd9Sstevel@tonic-gate 		else
1937c478bd9Sstevel@tonic-gate 			newcg->cg_irotor = 0;
194355d6bb5Sswilcox 		(void) memset((void *)&newcg->cg_frsum[0], 0,
195355d6bb5Sswilcox 		    sizeof (newcg->cg_frsum));
196355d6bb5Sswilcox 		(void) memset((void *)cg_inosused(newcg), 0, (size_t)mapsize);
197355d6bb5Sswilcox 		/* LINTED macro is int32-aligned per newcg->cg_btotoff above */
198355d6bb5Sswilcox 		(void) memset((void *)&cg_blktot(newcg)[0], 0,
199355d6bb5Sswilcox 		    sumsize + mapsize);
2007c478bd9Sstevel@tonic-gate 		j = fs->fs_ipg * c;
2017c478bd9Sstevel@tonic-gate 		for (i = 0; i < fs->fs_ipg; j++, i++) {
202355d6bb5Sswilcox 			switch (statemap[j] & ~(INORPHAN | INDELAYD)) {
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 			case USTATE:
2057c478bd9Sstevel@tonic-gate 				break;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 			case DSTATE:
2087c478bd9Sstevel@tonic-gate 			case DCLEAR:
2097c478bd9Sstevel@tonic-gate 			case DFOUND:
210355d6bb5Sswilcox 			case DZLINK:
2117c478bd9Sstevel@tonic-gate 				newcg->cg_cs.cs_ndir++;
212355d6bb5Sswilcox 				/* FALLTHROUGH */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 			case FSTATE:
2157c478bd9Sstevel@tonic-gate 			case FCLEAR:
216355d6bb5Sswilcox 			case FZLINK:
2177c478bd9Sstevel@tonic-gate 			case SSTATE:
2187c478bd9Sstevel@tonic-gate 			case SCLEAR:
2197c478bd9Sstevel@tonic-gate 				newcg->cg_cs.cs_nifree--;
2207c478bd9Sstevel@tonic-gate 				setbit(cg_inosused(newcg), i);
2217c478bd9Sstevel@tonic-gate 				break;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 			default:
2247c478bd9Sstevel@tonic-gate 				if (j < UFSROOTINO)
2257c478bd9Sstevel@tonic-gate 					break;
226355d6bb5Sswilcox 				errexit("BAD STATE 0x%x FOR INODE I=%d",
227355d6bb5Sswilcox 				    statemap[j], (int)j);
2287c478bd9Sstevel@tonic-gate 			}
2297c478bd9Sstevel@tonic-gate 		}
230355d6bb5Sswilcox 		if (c == 0) {
2317c478bd9Sstevel@tonic-gate 			for (i = 0; i < UFSROOTINO; i++) {
2327c478bd9Sstevel@tonic-gate 				setbit(cg_inosused(newcg), i);
2337c478bd9Sstevel@tonic-gate 				newcg->cg_cs.cs_nifree--;
2347c478bd9Sstevel@tonic-gate 			}
235355d6bb5Sswilcox 		}
236355d6bb5Sswilcox 		/*
237355d6bb5Sswilcox 		 * Count up what fragments and blocks are free, and
238355d6bb5Sswilcox 		 * reflect the relevant section of blockmap[] into
239355d6bb5Sswilcox 		 * newcg's map.
240355d6bb5Sswilcox 		 */
2417c478bd9Sstevel@tonic-gate 		for (i = 0, d = dbase;
2427c478bd9Sstevel@tonic-gate 		    d < dmax;
2437c478bd9Sstevel@tonic-gate 		    d += fs->fs_frag, i += fs->fs_frag) {
2447c478bd9Sstevel@tonic-gate 			frags = 0;
2457c478bd9Sstevel@tonic-gate 			for (j = 0; j < fs->fs_frag; j++) {
2467c478bd9Sstevel@tonic-gate 				if (testbmap(d + j))
2477c478bd9Sstevel@tonic-gate 					continue;
2487c478bd9Sstevel@tonic-gate 				setbit(cg_blksfree(newcg), i + j);
2497c478bd9Sstevel@tonic-gate 				frags++;
2507c478bd9Sstevel@tonic-gate 			}
2517c478bd9Sstevel@tonic-gate 			if (frags == fs->fs_frag) {
2527c478bd9Sstevel@tonic-gate 				newcg->cg_cs.cs_nbfree++;
2537c478bd9Sstevel@tonic-gate 				j = cbtocylno(fs, i);
254355d6bb5Sswilcox 				/* LINTED macro is int32-aligned per above */
2557c478bd9Sstevel@tonic-gate 				cg_blktot(newcg)[j]++;
256355d6bb5Sswilcox 				/* LINTED cg_blks(newcg) is aligned */
2577c478bd9Sstevel@tonic-gate 				cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++;
2587c478bd9Sstevel@tonic-gate 			} else if (frags > 0) {
2597c478bd9Sstevel@tonic-gate 				newcg->cg_cs.cs_nffree += frags;
2607c478bd9Sstevel@tonic-gate 				blk = blkmap(fs, cg_blksfree(newcg), i);
2617c478bd9Sstevel@tonic-gate 				fragacct(fs, blk, newcg->cg_frsum, 1);
2627c478bd9Sstevel@tonic-gate 			}
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
2657c478bd9Sstevel@tonic-gate 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
2667c478bd9Sstevel@tonic-gate 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
2677c478bd9Sstevel@tonic-gate 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
2687c478bd9Sstevel@tonic-gate 
269355d6bb5Sswilcox 		/*
270355d6bb5Sswilcox 		 * Note that, just like the kernel, we dynamically
271355d6bb5Sswilcox 		 * allocated an array to hold the csums and stuffed
272355d6bb5Sswilcox 		 * the pointer into the in-core superblock's fs_u.fs_csp
273355d6bb5Sswilcox 		 * field.  This means that the fs_u field contains a
274355d6bb5Sswilcox 		 * random value when the disk version is examined, but
275355d6bb5Sswilcox 		 * fs_cs() gives us a valid pointer nonetheless.
276355d6bb5Sswilcox 		 */
2777c478bd9Sstevel@tonic-gate 		cs = &fs->fs_cs(fs, c);
278355d6bb5Sswilcox 		bad_csum = (memcmp((void *)cs, (void *)&newcg->cg_cs,
279355d6bb5Sswilcox 		    sizeof (*cs)) != 0);
280355d6bb5Sswilcox 
281355d6bb5Sswilcox 		/*
282355d6bb5Sswilcox 		 * Has the user told us what to do yet?  If not, find out.
283355d6bb5Sswilcox 		 */
284355d6bb5Sswilcox 		if (bad_csum && (update_csums == -1)) {
285355d6bb5Sswilcox 			if (preen) {
286355d6bb5Sswilcox 				update_csums = 1;
287355d6bb5Sswilcox 				(void) printf("CORRECTING BAD CG SUMMARIES\n");
288355d6bb5Sswilcox 			} else if (update_csums == -1) {
289355d6bb5Sswilcox 				update_csums = (reply(
290355d6bb5Sswilcox 				    "CORRECT BAD CG SUMMARIES") == 1);
291355d6bb5Sswilcox 			}
2927c478bd9Sstevel@tonic-gate 		}
293355d6bb5Sswilcox 
294355d6bb5Sswilcox 		if (bad_csum && (update_csums == 1)) {
295355d6bb5Sswilcox 			(void) memmove((void *)cs, (void *)&newcg->cg_cs,
296355d6bb5Sswilcox 			    sizeof (*cs));
297355d6bb5Sswilcox 			sbdirty();
298355d6bb5Sswilcox 
299355d6bb5Sswilcox 			(void) memmove((void *)cg, (void *)newcg,
300355d6bb5Sswilcox 			    (size_t)basesize);
301355d6bb5Sswilcox 			/* LINTED per cg_sanity() */
302355d6bb5Sswilcox 			(void) memmove((void *)&cg_blktot(cg)[0],
303355d6bb5Sswilcox 			    /* LINTED macro aligned as above */
304355d6bb5Sswilcox 			    (void *)&cg_blktot(newcg)[0], sumsize);
3057c478bd9Sstevel@tonic-gate 			cgdirty();
306355d6bb5Sswilcox 			(void) printf("CORRECTED SUMMARY FOR CG %d\n", c);
3077c478bd9Sstevel@tonic-gate 		}
308355d6bb5Sswilcox 
309355d6bb5Sswilcox 		excessdirs = cg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
310355d6bb5Sswilcox 		if (excessdirs < 0) {
311355d6bb5Sswilcox 			pfatal("LOST %d DIRECTORIES IN CG %d\n",
312355d6bb5Sswilcox 			    -excessdirs, c);
313355d6bb5Sswilcox 			excessdirs = 0;
3147c478bd9Sstevel@tonic-gate 		}
315355d6bb5Sswilcox 		if (excessdirs > 0) {
316355d6bb5Sswilcox 			if (check_maps((uchar_t *)cg_inosused(newcg),
317355d6bb5Sswilcox 			    (uchar_t *)cg_inosused(cg), inomapsize,
318355d6bb5Sswilcox 			    cg->cg_cgx * fs->fs_ipg, "DIR", 0, excessdirs)) {
319355d6bb5Sswilcox 				if (!verbose)
320355d6bb5Sswilcox 					(void) printf("DIR BITMAP WRONG ");
321355d6bb5Sswilcox 				if (preen || update_bitmaps ||
322355d6bb5Sswilcox 				    reply("FIX") == 1) {
323355d6bb5Sswilcox 					(void) memmove((void *)cg_inosused(cg),
324355d6bb5Sswilcox 					    (void *)cg_inosused(newcg),
325355d6bb5Sswilcox 					    inomapsize);
326355d6bb5Sswilcox 					cgdirty();
327355d6bb5Sswilcox 					if (preen ||
328355d6bb5Sswilcox 					    (!verbose && update_bitmaps))
329355d6bb5Sswilcox 						(void) printf("(CORRECTED)\n");
330355d6bb5Sswilcox 					update_bitmaps = 1;
331355d6bb5Sswilcox 				}
332355d6bb5Sswilcox 			}
3337c478bd9Sstevel@tonic-gate 		}
3347c478bd9Sstevel@tonic-gate 
335355d6bb5Sswilcox 		if (check_maps((uchar_t *)cg_inosused(newcg),
336355d6bb5Sswilcox 		    (uchar_t *)cg_inosused(cg), inomapsize,
337355d6bb5Sswilcox 		    cg->cg_cgx * fs->fs_ipg, "FILE", excessdirs, fs->fs_ipg)) {
338355d6bb5Sswilcox 			if (!verbose)
339355d6bb5Sswilcox 				(void) printf("FILE BITMAP WRONG ");
340355d6bb5Sswilcox 			if (preen || update_bitmaps || reply("FIX") == 1) {
341355d6bb5Sswilcox 				(void) memmove((void *)cg_inosused(cg),
342355d6bb5Sswilcox 				    (void *)cg_inosused(newcg), inomapsize);
343355d6bb5Sswilcox 				cgdirty();
344355d6bb5Sswilcox 				if (preen ||
345355d6bb5Sswilcox 				    (!verbose && update_bitmaps))
346355d6bb5Sswilcox 					(void) printf("(CORRECTED)\n");
347355d6bb5Sswilcox 				update_bitmaps = 1;
348355d6bb5Sswilcox 			}
3497c478bd9Sstevel@tonic-gate 		}
3507c478bd9Sstevel@tonic-gate 
351355d6bb5Sswilcox 		if (check_maps((uchar_t *)cg_blksfree(cg),
352355d6bb5Sswilcox 		    (uchar_t *)cg_blksfree(newcg), blkmapsize,
353355d6bb5Sswilcox 		    cg->cg_cgx * fs->fs_fpg, "FRAG", 0, fs->fs_fpg)) {
354355d6bb5Sswilcox 			if (!verbose)
355355d6bb5Sswilcox 				(void) printf("FRAG BITMAP WRONG ");
356355d6bb5Sswilcox 			if (preen || update_bitmaps || reply("FIX") == 1) {
357355d6bb5Sswilcox 				(void) memmove((void *)cg_blksfree(cg),
358355d6bb5Sswilcox 				    (void *)cg_blksfree(newcg), blkmapsize);
359355d6bb5Sswilcox 				cgdirty();
360355d6bb5Sswilcox 				if (preen ||
361355d6bb5Sswilcox 				    (!verbose && update_bitmaps))
362355d6bb5Sswilcox 					(void) printf("(CORRECTED)\n");
363355d6bb5Sswilcox 				update_bitmaps = 1;
364355d6bb5Sswilcox 			}
365355d6bb5Sswilcox 		}
366355d6bb5Sswilcox 
367355d6bb5Sswilcox 		/*
368355d6bb5Sswilcox 		 * Fixing one set of problems often shows up more in the
369355d6bb5Sswilcox 		 * same cg.  Just to make sure, go back and check it
370355d6bb5Sswilcox 		 * again if we found something this time through.
371355d6bb5Sswilcox 		 */
372355d6bb5Sswilcox 		if (cgisdirty()) {
373355d6bb5Sswilcox 			cgflush();
374355d6bb5Sswilcox 			cstotal = backup_cs;
375355d6bb5Sswilcox 			c--;
376355d6bb5Sswilcox 		}
3777c478bd9Sstevel@tonic-gate 	}
378355d6bb5Sswilcox 
3797c478bd9Sstevel@tonic-gate 	if ((fflag || !(islog && islogok)) &&
380355d6bb5Sswilcox 	    (memcmp((void *)&cstotal, (void *)&fs->fs_cstotal,
381355d6bb5Sswilcox 	    sizeof (struct csum)) != 0)) {
382355d6bb5Sswilcox 		if (dofix(&idesc, "CORRECT GLOBAL SUMMARY")) {
383355d6bb5Sswilcox 			(void) memmove((void *)&fs->fs_cstotal,
384355d6bb5Sswilcox 			    (void *)&cstotal, sizeof (struct csum));
385355d6bb5Sswilcox 			fs->fs_ronly = 0;
386355d6bb5Sswilcox 			fs->fs_fmod = 0;
387355d6bb5Sswilcox 			sbdirty();
388355d6bb5Sswilcox 		} else {
389355d6bb5Sswilcox 			iscorrupt = 1;
390355d6bb5Sswilcox 		}
391355d6bb5Sswilcox 	}
392355d6bb5Sswilcox }
393355d6bb5Sswilcox 
394355d6bb5Sswilcox /*
395355d6bb5Sswilcox  * Compare two allocation bitmaps, reporting any discrepancies.
396355d6bb5Sswilcox  *
397355d6bb5Sswilcox  * If a mismatch is found, if the bit is set in map1, it's considered
398355d6bb5Sswilcox  * to be an indication that the corresponding resource is supposed
399355d6bb5Sswilcox  * to be free, but isn't.  Otherwise, it's considered marked as allocated
400355d6bb5Sswilcox  * but not found to be so.  In other words, if the two maps being compared
401355d6bb5Sswilcox  * use a set bit to indicate something is free, pass the on-disk map
402355d6bb5Sswilcox  * first.  Otherwise, pass the calculated map first.
403355d6bb5Sswilcox  */
404355d6bb5Sswilcox static int
405355d6bb5Sswilcox check_maps(
406355d6bb5Sswilcox 	uchar_t *map1,	/* map of claimed allocations */
407355d6bb5Sswilcox 	uchar_t *map2,	/* map of determined allocations */
408355d6bb5Sswilcox 	int mapsize,	/* size of above two maps */
409355d6bb5Sswilcox 	int startvalue,	/* resource value for first element in map */
410355d6bb5Sswilcox 	char *name,	/* name of resource found in maps */
411355d6bb5Sswilcox 	int skip,	/* number of entries to skip before starting to free */
412355d6bb5Sswilcox 	int limit)	/* limit on number of entries to free */
413355d6bb5Sswilcox {
414355d6bb5Sswilcox 	long i, j, k, l, m, n, size;
415355d6bb5Sswilcox 	int astart, aend, ustart, uend;
416355d6bb5Sswilcox 	int mismatch;
417355d6bb5Sswilcox 
418355d6bb5Sswilcox 	mismatch = 0;
419355d6bb5Sswilcox 	astart = ustart = aend = uend = -1;
420355d6bb5Sswilcox 	for (i = 0; i < mapsize; i++) {
421355d6bb5Sswilcox 		j = *map1++;
422355d6bb5Sswilcox 		k = *map2++;
423355d6bb5Sswilcox 		if (j == k)
424355d6bb5Sswilcox 			continue;
425355d6bb5Sswilcox 		for (m = 0, l = 1; m < NBBY; m++, l <<= 1) {
426355d6bb5Sswilcox 			if ((j & l) == (k & l))
427355d6bb5Sswilcox 				continue;
428355d6bb5Sswilcox 			n = startvalue + i * NBBY + m;
429355d6bb5Sswilcox 			if ((j & l) != 0) {
430355d6bb5Sswilcox 				if (astart == -1) {
431355d6bb5Sswilcox 					astart = aend = n;
432355d6bb5Sswilcox 					continue;
433355d6bb5Sswilcox 				}
434355d6bb5Sswilcox 				if (aend + 1 == n) {
435355d6bb5Sswilcox 					aend = n;
436355d6bb5Sswilcox 					continue;
437355d6bb5Sswilcox 				}
438355d6bb5Sswilcox 				if (verbose) {
439355d6bb5Sswilcox 					if (astart == aend)
440355d6bb5Sswilcox 						pwarn(
441355d6bb5Sswilcox 			    "ALLOCATED %s %d WAS MARKED FREE ON DISK\n",
442355d6bb5Sswilcox 						    name, astart);
443355d6bb5Sswilcox 					else
444355d6bb5Sswilcox 						pwarn(
445355d6bb5Sswilcox 			    "ALLOCATED %sS %d-%d WERE MARKED FREE ON DISK\n",
446355d6bb5Sswilcox 						    name, astart, aend);
447355d6bb5Sswilcox 				}
448355d6bb5Sswilcox 				mismatch = 1;
449355d6bb5Sswilcox 				astart = aend = n;
450355d6bb5Sswilcox 			} else {
451355d6bb5Sswilcox 				if (ustart == -1) {
452355d6bb5Sswilcox 					ustart = uend = n;
453355d6bb5Sswilcox 					continue;
454355d6bb5Sswilcox 				}
455355d6bb5Sswilcox 				if (uend + 1 == n) {
456355d6bb5Sswilcox 					uend = n;
457355d6bb5Sswilcox 					continue;
458355d6bb5Sswilcox 				}
459355d6bb5Sswilcox 				size = uend - ustart + 1;
460355d6bb5Sswilcox 				if (size <= skip) {
461355d6bb5Sswilcox 					skip -= size;
462355d6bb5Sswilcox 					ustart = uend = n;
463355d6bb5Sswilcox 					continue;
464355d6bb5Sswilcox 				}
465355d6bb5Sswilcox 				if (skip > 0) {
466355d6bb5Sswilcox 					ustart += skip;
467355d6bb5Sswilcox 					size -= skip;
468355d6bb5Sswilcox 					skip = 0;
469355d6bb5Sswilcox 				}
470355d6bb5Sswilcox 				if (size > limit)
471355d6bb5Sswilcox 					size = limit;
472355d6bb5Sswilcox 				if (verbose) {
473355d6bb5Sswilcox 					if (size == 1)
474355d6bb5Sswilcox 						pwarn(
475355d6bb5Sswilcox 			    "UNALLOCATED %s %d WAS MARKED USED ON DISK\n",
476355d6bb5Sswilcox 						    name, ustart);
477355d6bb5Sswilcox 					else
478355d6bb5Sswilcox 						pwarn(
479355d6bb5Sswilcox 			    "UNALLOCATED %sS %d-%ld WERE MARKED USED ON DISK\n",
480355d6bb5Sswilcox 						    name, ustart,
481355d6bb5Sswilcox 						    ustart + size - 1);
482355d6bb5Sswilcox 				}
483355d6bb5Sswilcox 				mismatch = 1;
484355d6bb5Sswilcox 				limit -= size;
485355d6bb5Sswilcox 				if (limit <= 0)
486355d6bb5Sswilcox 					return (mismatch);
487355d6bb5Sswilcox 				ustart = uend = n;
488355d6bb5Sswilcox 			}
489355d6bb5Sswilcox 		}
490355d6bb5Sswilcox 	}
491355d6bb5Sswilcox 	if (astart != -1) {
492355d6bb5Sswilcox 		if (verbose) {
493355d6bb5Sswilcox 			if (astart == aend)
494355d6bb5Sswilcox 				pwarn(
495355d6bb5Sswilcox 			    "ALLOCATED %s %d WAS MARKED FREE ON DISK\n",
496355d6bb5Sswilcox 				    name, astart);
497355d6bb5Sswilcox 			else
498355d6bb5Sswilcox 				pwarn(
499355d6bb5Sswilcox 			    "ALLOCATED %sS %d-%d WERE MARKED FREE ON DISK\n",
500355d6bb5Sswilcox 				    name, astart, aend);
501355d6bb5Sswilcox 		}
502355d6bb5Sswilcox 		mismatch = 1;
503355d6bb5Sswilcox 	}
504355d6bb5Sswilcox 	if (ustart != -1) {
505355d6bb5Sswilcox 		size = uend - ustart + 1;
506355d6bb5Sswilcox 		if (size <= skip)
507355d6bb5Sswilcox 			return (mismatch);
508355d6bb5Sswilcox 		if (skip > 0) {
509355d6bb5Sswilcox 			ustart += skip;
510355d6bb5Sswilcox 			size -= skip;
511355d6bb5Sswilcox 		}
512355d6bb5Sswilcox 		if (size > limit)
513355d6bb5Sswilcox 			size = limit;
514355d6bb5Sswilcox 		if (verbose) {
515355d6bb5Sswilcox 			if (size == 1)
516355d6bb5Sswilcox 				pwarn(
517355d6bb5Sswilcox 			    "UNALLOCATED %s %d WAS MARKED USED ON DISK\n",
518355d6bb5Sswilcox 				    name, ustart);
519355d6bb5Sswilcox 			else
520355d6bb5Sswilcox 				pwarn(
521355d6bb5Sswilcox 		    "UNALLOCATED %sS %d-%ld WERE MARKED USED ON DISK\n",
522355d6bb5Sswilcox 				    name, ustart, ustart + size - 1);
523355d6bb5Sswilcox 		}
524355d6bb5Sswilcox 		mismatch = 1;
5257c478bd9Sstevel@tonic-gate 	}
526355d6bb5Sswilcox 	return (mismatch);
5277c478bd9Sstevel@tonic-gate }
528