xref: /illumos-gate/usr/src/cmd/fs.d/ufs/fsck/pass5.c (revision 70e93bcc)
17c478bd9Sstevel@tonic-gate /*
2*70e93bccSabalfour  * Copyright 2007 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;
487c478bd9Sstevel@tonic-gate 	size_t	basesize, sumsize, mapsize;
49355d6bb5Sswilcox 	int excessdirs;
50355d6bb5Sswilcox 	int inomapsize, blkmapsize;
5177a343abSabalfour 	int update_csums, bad_csum_sb, bad_csum_cg, update_bitmaps;
527c478bd9Sstevel@tonic-gate 	struct fs *fs = &sblock;
537c478bd9Sstevel@tonic-gate 	struct cg *cg = &cgrp;
547c478bd9Sstevel@tonic-gate 	diskaddr_t dbase, dmax;
557c478bd9Sstevel@tonic-gate 	diskaddr_t d;
567c478bd9Sstevel@tonic-gate 	uint64_t i, j;
577c478bd9Sstevel@tonic-gate 	struct csum *cs;
58355d6bb5Sswilcox 	struct csum backup_cs;
597c478bd9Sstevel@tonic-gate 	time_t now;
607c478bd9Sstevel@tonic-gate 	struct csum cstotal;
617c478bd9Sstevel@tonic-gate 	struct inodesc idesc;
62355d6bb5Sswilcox 	union {				/* keep lint happy about alignment */
63355d6bb5Sswilcox 		struct cg cg;		/* the rest of buf has the bitmaps */
64355d6bb5Sswilcox 		char buf[MAXBSIZE];
65355d6bb5Sswilcox 	} u;
66355d6bb5Sswilcox 	caddr_t buf = u.buf;
67355d6bb5Sswilcox 	struct cg *newcg = &u.cg;
687c478bd9Sstevel@tonic-gate 
69355d6bb5Sswilcox 	(void) memset((void *)buf, 0, sizeof (u.buf));
707c478bd9Sstevel@tonic-gate 	newcg->cg_niblk = fs->fs_ipg;
71355d6bb5Sswilcox 
72355d6bb5Sswilcox 	if (fs->fs_postblformat != FS_DYNAMICPOSTBLFMT) {
73355d6bb5Sswilcox 		pfatal("UNSUPPORTED ROTATIONAL TABLE FORMAT %d\n",
747c478bd9Sstevel@tonic-gate 			fs->fs_postblformat);
75355d6bb5Sswilcox 		errexit("Program terminated.");
76355d6bb5Sswilcox 		/* NOTREACHED */
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
79355d6bb5Sswilcox 	/* LINTED this subtraction can't overflow and is int32-aligned */
80355d6bb5Sswilcox 	basesize = &newcg->cg_space[0] - (uchar_t *)newcg;
81355d6bb5Sswilcox 
82355d6bb5Sswilcox 	/*
83355d6bb5Sswilcox 	 * We reserve the space for the old rotation summary
84355d6bb5Sswilcox 	 * tables for the benefit of old kernels, but do not
85355d6bb5Sswilcox 	 * maintain them in modern kernels. In time, they could
86355d6bb5Sswilcox 	 * theoretically go away, if we wanted to deal with
87355d6bb5Sswilcox 	 * changing the on-disk format.
88355d6bb5Sswilcox 	 */
89355d6bb5Sswilcox 
90355d6bb5Sswilcox 	/*
91355d6bb5Sswilcox 	 * Note that we don't use any of the cg_*() macros until
92355d6bb5Sswilcox 	 * after cg_sanity() has approved of what we've got.
93355d6bb5Sswilcox 	 */
94355d6bb5Sswilcox 	newcg->cg_btotoff = basesize;
95355d6bb5Sswilcox 	newcg->cg_boff = newcg->cg_btotoff + fs->fs_cpg * sizeof (daddr32_t);
96355d6bb5Sswilcox 	newcg->cg_iusedoff = newcg->cg_boff +
97355d6bb5Sswilcox 		fs->fs_cpg * fs->fs_nrpos * sizeof (uint16_t);
98355d6bb5Sswilcox 	(void) memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
99355d6bb5Sswilcox 
100355d6bb5Sswilcox 	inomapsize = howmany(fs->fs_ipg, NBBY);
101355d6bb5Sswilcox 	newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
102355d6bb5Sswilcox 	blkmapsize = howmany(fs->fs_fpg, NBBY);
103355d6bb5Sswilcox 	newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
104355d6bb5Sswilcox 	newcg->cg_magic = CG_MAGIC;
105355d6bb5Sswilcox 
106355d6bb5Sswilcox 	sumsize = newcg->cg_iusedoff - newcg->cg_btotoff;
107355d6bb5Sswilcox 	mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
108355d6bb5Sswilcox 
109355d6bb5Sswilcox 	init_inodesc(&idesc);
1107c478bd9Sstevel@tonic-gate 	idesc.id_type = ADDR;
111355d6bb5Sswilcox 	(void) memset((void *)&cstotal, 0, sizeof (struct csum));
112355d6bb5Sswilcox 	now = time(NULL);
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/*
1157c478bd9Sstevel@tonic-gate 	 * If the last fragments in the file system don't make up a
1167c478bd9Sstevel@tonic-gate 	 * full file system block, mark the bits in the blockmap
1177c478bd9Sstevel@tonic-gate 	 * that correspond to those missing fragments as "allocated",
1187c478bd9Sstevel@tonic-gate 	 * so that the last block doesn't get counted as a free block
1197c478bd9Sstevel@tonic-gate 	 * and those missing fragments don't get counted as free frags.
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	j = blknum(fs, (uint64_t)fs->fs_size + fs->fs_frag - 1);
1227c478bd9Sstevel@tonic-gate 	for (i = fs->fs_size; i < j; i++)
1237c478bd9Sstevel@tonic-gate 		setbmap(i);
124355d6bb5Sswilcox 
125355d6bb5Sswilcox 	/*
126355d6bb5Sswilcox 	 * The cg summaries are not always updated when using
127355d6bb5Sswilcox 	 * logging.  Since we're really concerned with getting a
128355d6bb5Sswilcox 	 * sane filesystem, rather than in trying to debug UFS
129355d6bb5Sswilcox 	 * corner cases, logically we would just always recompute
130355d6bb5Sswilcox 	 * them.  However, it is disconcerting to users to be asked
131355d6bb5Sswilcox 	 * about updating the summaries when, from their point of
132355d6bb5Sswilcox 	 * view, there's been no indication of a problem up to this
133355d6bb5Sswilcox 	 * point.  So, only do it if we find a discrepancy.
134355d6bb5Sswilcox 	 */
135355d6bb5Sswilcox 	update_csums = -1;
136355d6bb5Sswilcox 	update_bitmaps = 0;
1377c478bd9Sstevel@tonic-gate 	for (c = 0; c < fs->fs_ncg; c++) {
138355d6bb5Sswilcox 		backup_cs = cstotal;
139355d6bb5Sswilcox 
140355d6bb5Sswilcox 		/*
141355d6bb5Sswilcox 		 * cg_sanity() will catch i/o errors for us.
142355d6bb5Sswilcox 		 */
143355d6bb5Sswilcox 		(void) getblk(&cgblk, (diskaddr_t)cgtod(fs, c),
144355d6bb5Sswilcox 		    (size_t)fs->fs_cgsize);
14577a343abSabalfour 		err = cg_sanity(cg, c);
146355d6bb5Sswilcox 		if (err != NULL) {
147355d6bb5Sswilcox 			pfatal("CG %d: %s\n", c, err);
148355d6bb5Sswilcox 			free((void *)err);
149355d6bb5Sswilcox 			if (reply("REPAIR") == 0)
150355d6bb5Sswilcox 				errexit("Program terminated.");
151355d6bb5Sswilcox 			fix_cg(cg, c);
152355d6bb5Sswilcox 		}
153355d6bb5Sswilcox 		/*
154355d6bb5Sswilcox 		 * If the on-disk timestamp is in the future, then it
155355d6bb5Sswilcox 		 * by definition is wrong.  Otherwise, if it's in
156355d6bb5Sswilcox 		 * the past, then use that value so that we don't
157355d6bb5Sswilcox 		 * declare a spurious mismatch.
158355d6bb5Sswilcox 		 */
1597c478bd9Sstevel@tonic-gate 		if (now > cg->cg_time)
1607c478bd9Sstevel@tonic-gate 			newcg->cg_time = cg->cg_time;
1617c478bd9Sstevel@tonic-gate 		else
1627c478bd9Sstevel@tonic-gate 			newcg->cg_time = now;
1637c478bd9Sstevel@tonic-gate 		newcg->cg_cgx = c;
164355d6bb5Sswilcox 		dbase = cgbase(fs, c);
165355d6bb5Sswilcox 		dmax = dbase + fs->fs_fpg;
166355d6bb5Sswilcox 		if (dmax > fs->fs_size)
167355d6bb5Sswilcox 			dmax = fs->fs_size;
168355d6bb5Sswilcox 		newcg->cg_ndblk = dmax - dbase;
1697c478bd9Sstevel@tonic-gate 		if (c == fs->fs_ncg - 1)
170ef31a55cSvk 			newcg->cg_ncyl = fs->fs_ncyl - (fs->fs_cpg * c);
1717c478bd9Sstevel@tonic-gate 		else
1727c478bd9Sstevel@tonic-gate 			newcg->cg_ncyl = fs->fs_cpg;
1737c478bd9Sstevel@tonic-gate 		newcg->cg_niblk = sblock.fs_ipg;
1747c478bd9Sstevel@tonic-gate 		newcg->cg_cs.cs_ndir = 0;
1757c478bd9Sstevel@tonic-gate 		newcg->cg_cs.cs_nffree = 0;
1767c478bd9Sstevel@tonic-gate 		newcg->cg_cs.cs_nbfree = 0;
1777c478bd9Sstevel@tonic-gate 		newcg->cg_cs.cs_nifree = fs->fs_ipg;
1787c478bd9Sstevel@tonic-gate 		if ((cg->cg_rotor >= 0) && (cg->cg_rotor < newcg->cg_ndblk))
1797c478bd9Sstevel@tonic-gate 			newcg->cg_rotor = cg->cg_rotor;
1807c478bd9Sstevel@tonic-gate 		else
1817c478bd9Sstevel@tonic-gate 			newcg->cg_rotor = 0;
1827c478bd9Sstevel@tonic-gate 		if ((cg->cg_frotor >= 0) && (cg->cg_frotor < newcg->cg_ndblk))
1837c478bd9Sstevel@tonic-gate 			newcg->cg_frotor = cg->cg_frotor;
1847c478bd9Sstevel@tonic-gate 		else
1857c478bd9Sstevel@tonic-gate 			newcg->cg_frotor = 0;
1867c478bd9Sstevel@tonic-gate 		if ((cg->cg_irotor >= 0) && (cg->cg_irotor < newcg->cg_niblk))
1877c478bd9Sstevel@tonic-gate 			newcg->cg_irotor = cg->cg_irotor;
1887c478bd9Sstevel@tonic-gate 		else
1897c478bd9Sstevel@tonic-gate 			newcg->cg_irotor = 0;
190355d6bb5Sswilcox 		(void) memset((void *)&newcg->cg_frsum[0], 0,
191355d6bb5Sswilcox 		    sizeof (newcg->cg_frsum));
192355d6bb5Sswilcox 		(void) memset((void *)cg_inosused(newcg), 0, (size_t)mapsize);
193355d6bb5Sswilcox 		/* LINTED macro is int32-aligned per newcg->cg_btotoff above */
194355d6bb5Sswilcox 		(void) memset((void *)&cg_blktot(newcg)[0], 0,
195355d6bb5Sswilcox 		    sumsize + mapsize);
1967c478bd9Sstevel@tonic-gate 		j = fs->fs_ipg * c;
1977c478bd9Sstevel@tonic-gate 		for (i = 0; i < fs->fs_ipg; j++, i++) {
198355d6bb5Sswilcox 			switch (statemap[j] & ~(INORPHAN | INDELAYD)) {
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 			case USTATE:
2017c478bd9Sstevel@tonic-gate 				break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 			case DSTATE:
2047c478bd9Sstevel@tonic-gate 			case DCLEAR:
2057c478bd9Sstevel@tonic-gate 			case DFOUND:
206355d6bb5Sswilcox 			case DZLINK:
2077c478bd9Sstevel@tonic-gate 				newcg->cg_cs.cs_ndir++;
208355d6bb5Sswilcox 				/* FALLTHROUGH */
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 			case FSTATE:
2117c478bd9Sstevel@tonic-gate 			case FCLEAR:
212355d6bb5Sswilcox 			case FZLINK:
2137c478bd9Sstevel@tonic-gate 			case SSTATE:
2147c478bd9Sstevel@tonic-gate 			case SCLEAR:
2157c478bd9Sstevel@tonic-gate 				newcg->cg_cs.cs_nifree--;
2167c478bd9Sstevel@tonic-gate 				setbit(cg_inosused(newcg), i);
2177c478bd9Sstevel@tonic-gate 				break;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 			default:
2207c478bd9Sstevel@tonic-gate 				if (j < UFSROOTINO)
2217c478bd9Sstevel@tonic-gate 					break;
222355d6bb5Sswilcox 				errexit("BAD STATE 0x%x FOR INODE I=%d",
223355d6bb5Sswilcox 				    statemap[j], (int)j);
2247c478bd9Sstevel@tonic-gate 			}
2257c478bd9Sstevel@tonic-gate 		}
226355d6bb5Sswilcox 		if (c == 0) {
2277c478bd9Sstevel@tonic-gate 			for (i = 0; i < UFSROOTINO; i++) {
2287c478bd9Sstevel@tonic-gate 				setbit(cg_inosused(newcg), i);
2297c478bd9Sstevel@tonic-gate 				newcg->cg_cs.cs_nifree--;
2307c478bd9Sstevel@tonic-gate 			}
231355d6bb5Sswilcox 		}
232355d6bb5Sswilcox 		/*
233355d6bb5Sswilcox 		 * Count up what fragments and blocks are free, and
234355d6bb5Sswilcox 		 * reflect the relevant section of blockmap[] into
235355d6bb5Sswilcox 		 * newcg's map.
236355d6bb5Sswilcox 		 */
2377c478bd9Sstevel@tonic-gate 		for (i = 0, d = dbase;
2387c478bd9Sstevel@tonic-gate 		    d < dmax;
2397c478bd9Sstevel@tonic-gate 		    d += fs->fs_frag, i += fs->fs_frag) {
2407c478bd9Sstevel@tonic-gate 			frags = 0;
2417c478bd9Sstevel@tonic-gate 			for (j = 0; j < fs->fs_frag; j++) {
2427c478bd9Sstevel@tonic-gate 				if (testbmap(d + j))
2437c478bd9Sstevel@tonic-gate 					continue;
2447c478bd9Sstevel@tonic-gate 				setbit(cg_blksfree(newcg), i + j);
2457c478bd9Sstevel@tonic-gate 				frags++;
2467c478bd9Sstevel@tonic-gate 			}
2477c478bd9Sstevel@tonic-gate 			if (frags == fs->fs_frag) {
2487c478bd9Sstevel@tonic-gate 				newcg->cg_cs.cs_nbfree++;
2497c478bd9Sstevel@tonic-gate 				j = cbtocylno(fs, i);
250355d6bb5Sswilcox 				/* LINTED macro is int32-aligned per above */
2517c478bd9Sstevel@tonic-gate 				cg_blktot(newcg)[j]++;
252355d6bb5Sswilcox 				/* LINTED cg_blks(newcg) is aligned */
2537c478bd9Sstevel@tonic-gate 				cg_blks(fs, newcg, j)[cbtorpos(fs, i)]++;
2547c478bd9Sstevel@tonic-gate 			} else if (frags > 0) {
2557c478bd9Sstevel@tonic-gate 				newcg->cg_cs.cs_nffree += frags;
2567c478bd9Sstevel@tonic-gate 				blk = blkmap(fs, cg_blksfree(newcg), i);
2577c478bd9Sstevel@tonic-gate 				fragacct(fs, blk, newcg->cg_frsum, 1);
2587c478bd9Sstevel@tonic-gate 			}
2597c478bd9Sstevel@tonic-gate 		}
2607c478bd9Sstevel@tonic-gate 		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
2617c478bd9Sstevel@tonic-gate 		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
2627c478bd9Sstevel@tonic-gate 		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
2637c478bd9Sstevel@tonic-gate 		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
2647c478bd9Sstevel@tonic-gate 
265355d6bb5Sswilcox 		/*
266355d6bb5Sswilcox 		 * Note that, just like the kernel, we dynamically
267355d6bb5Sswilcox 		 * allocated an array to hold the csums and stuffed
268355d6bb5Sswilcox 		 * the pointer into the in-core superblock's fs_u.fs_csp
269355d6bb5Sswilcox 		 * field.  This means that the fs_u field contains a
270355d6bb5Sswilcox 		 * random value when the disk version is examined, but
271355d6bb5Sswilcox 		 * fs_cs() gives us a valid pointer nonetheless.
27277a343abSabalfour 		 * We need to compare the recalculated summaries to
27377a343abSabalfour 		 * both the superblock version and the on disk version.
27477a343abSabalfour 		 * If either is bad, copy the calculated version over
27577a343abSabalfour 		 * the corrupt values.
276355d6bb5Sswilcox 		 */
27777a343abSabalfour 
2787c478bd9Sstevel@tonic-gate 		cs = &fs->fs_cs(fs, c);
27977a343abSabalfour 		bad_csum_sb = (memcmp((void *)cs, (void *)&newcg->cg_cs,
280355d6bb5Sswilcox 		    sizeof (*cs)) != 0);
281355d6bb5Sswilcox 
28277a343abSabalfour 		bad_csum_cg = (memcmp((void *)&cg->cg_cs, (void *)&newcg->cg_cs,
28377a343abSabalfour 		    sizeof (struct csum)) != 0);
28477a343abSabalfour 
285355d6bb5Sswilcox 		/*
286355d6bb5Sswilcox 		 * Has the user told us what to do yet?  If not, find out.
287355d6bb5Sswilcox 		 */
28877a343abSabalfour 		if ((bad_csum_sb || bad_csum_cg) && (update_csums == -1)) {
289355d6bb5Sswilcox 			if (preen) {
290355d6bb5Sswilcox 				update_csums = 1;
29177a343abSabalfour 				(void) printf("CORRECTING BAD CG SUMMARIES"
29277a343abSabalfour 					" FOR CG %d\n", c);
293355d6bb5Sswilcox 			} else if (update_csums == -1) {
294355d6bb5Sswilcox 				update_csums = (reply(
29577a343abSabalfour 				    "CORRECT BAD CG SUMMARIES FOR CG %d",
29677a343abSabalfour 				    c) == 1);
297355d6bb5Sswilcox 			}
2987c478bd9Sstevel@tonic-gate 		}
299355d6bb5Sswilcox 
30077a343abSabalfour 		if (bad_csum_sb && (update_csums == 1)) {
301355d6bb5Sswilcox 			(void) memmove((void *)cs, (void *)&newcg->cg_cs,
302355d6bb5Sswilcox 			    sizeof (*cs));
303355d6bb5Sswilcox 			sbdirty();
30477a343abSabalfour 			(void) printf("CORRECTED SUPERBLOCK SUMMARIES FOR"
30577a343abSabalfour 				    " CG %d\n", c);
30677a343abSabalfour 		}
307355d6bb5Sswilcox 
30877a343abSabalfour 		if (bad_csum_cg && (update_csums == 1)) {
309*70e93bccSabalfour 			(void) memmove((void *)cg, (void *)newcg,
310*70e93bccSabalfour 			    (size_t)basesize);
311355d6bb5Sswilcox 			/* LINTED per cg_sanity() */
312355d6bb5Sswilcox 			(void) memmove((void *)&cg_blktot(cg)[0],
313355d6bb5Sswilcox 			    /* LINTED macro aligned as above */
314355d6bb5Sswilcox 			    (void *)&cg_blktot(newcg)[0], sumsize);
3157c478bd9Sstevel@tonic-gate 			cgdirty();
31677a343abSabalfour 			(void) printf("CORRECTED SUMMARIES FOR CG %d\n", c);
3177c478bd9Sstevel@tonic-gate 		}
318355d6bb5Sswilcox 
319355d6bb5Sswilcox 		excessdirs = cg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
320355d6bb5Sswilcox 		if (excessdirs < 0) {
321355d6bb5Sswilcox 			pfatal("LOST %d DIRECTORIES IN CG %d\n",
322355d6bb5Sswilcox 			    -excessdirs, c);
323355d6bb5Sswilcox 			excessdirs = 0;
3247c478bd9Sstevel@tonic-gate 		}
325355d6bb5Sswilcox 		if (excessdirs > 0) {
326355d6bb5Sswilcox 			if (check_maps((uchar_t *)cg_inosused(newcg),
327355d6bb5Sswilcox 			    (uchar_t *)cg_inosused(cg), inomapsize,
328355d6bb5Sswilcox 			    cg->cg_cgx * fs->fs_ipg, "DIR", 0, excessdirs)) {
329355d6bb5Sswilcox 				if (!verbose)
330355d6bb5Sswilcox 					(void) printf("DIR BITMAP WRONG ");
331355d6bb5Sswilcox 				if (preen || update_bitmaps ||
332355d6bb5Sswilcox 				    reply("FIX") == 1) {
333355d6bb5Sswilcox 					(void) memmove((void *)cg_inosused(cg),
334355d6bb5Sswilcox 					    (void *)cg_inosused(newcg),
335355d6bb5Sswilcox 					    inomapsize);
336355d6bb5Sswilcox 					cgdirty();
337355d6bb5Sswilcox 					if (preen ||
338355d6bb5Sswilcox 					    (!verbose && update_bitmaps))
339355d6bb5Sswilcox 						(void) printf("(CORRECTED)\n");
340355d6bb5Sswilcox 					update_bitmaps = 1;
341355d6bb5Sswilcox 				}
342355d6bb5Sswilcox 			}
3437c478bd9Sstevel@tonic-gate 		}
3447c478bd9Sstevel@tonic-gate 
345355d6bb5Sswilcox 		if (check_maps((uchar_t *)cg_inosused(newcg),
346355d6bb5Sswilcox 		    (uchar_t *)cg_inosused(cg), inomapsize,
347355d6bb5Sswilcox 		    cg->cg_cgx * fs->fs_ipg, "FILE", excessdirs, fs->fs_ipg)) {
348355d6bb5Sswilcox 			if (!verbose)
349355d6bb5Sswilcox 				(void) printf("FILE BITMAP WRONG ");
350355d6bb5Sswilcox 			if (preen || update_bitmaps || reply("FIX") == 1) {
351355d6bb5Sswilcox 				(void) memmove((void *)cg_inosused(cg),
352355d6bb5Sswilcox 				    (void *)cg_inosused(newcg), inomapsize);
353355d6bb5Sswilcox 				cgdirty();
354355d6bb5Sswilcox 				if (preen ||
355355d6bb5Sswilcox 				    (!verbose && update_bitmaps))
356355d6bb5Sswilcox 					(void) printf("(CORRECTED)\n");
357355d6bb5Sswilcox 				update_bitmaps = 1;
358355d6bb5Sswilcox 			}
3597c478bd9Sstevel@tonic-gate 		}
3607c478bd9Sstevel@tonic-gate 
361355d6bb5Sswilcox 		if (check_maps((uchar_t *)cg_blksfree(cg),
362355d6bb5Sswilcox 		    (uchar_t *)cg_blksfree(newcg), blkmapsize,
363355d6bb5Sswilcox 		    cg->cg_cgx * fs->fs_fpg, "FRAG", 0, fs->fs_fpg)) {
364355d6bb5Sswilcox 			if (!verbose)
365355d6bb5Sswilcox 				(void) printf("FRAG BITMAP WRONG ");
366355d6bb5Sswilcox 			if (preen || update_bitmaps || reply("FIX") == 1) {
367355d6bb5Sswilcox 				(void) memmove((void *)cg_blksfree(cg),
368355d6bb5Sswilcox 				    (void *)cg_blksfree(newcg), blkmapsize);
369355d6bb5Sswilcox 				cgdirty();
370355d6bb5Sswilcox 				if (preen ||
371355d6bb5Sswilcox 				    (!verbose && update_bitmaps))
372355d6bb5Sswilcox 					(void) printf("(CORRECTED)\n");
373355d6bb5Sswilcox 				update_bitmaps = 1;
374355d6bb5Sswilcox 			}
375355d6bb5Sswilcox 		}
376355d6bb5Sswilcox 
377355d6bb5Sswilcox 		/*
378355d6bb5Sswilcox 		 * Fixing one set of problems often shows up more in the
379355d6bb5Sswilcox 		 * same cg.  Just to make sure, go back and check it
380355d6bb5Sswilcox 		 * again if we found something this time through.
381355d6bb5Sswilcox 		 */
382355d6bb5Sswilcox 		if (cgisdirty()) {
383355d6bb5Sswilcox 			cgflush();
384355d6bb5Sswilcox 			cstotal = backup_cs;
385355d6bb5Sswilcox 			c--;
386355d6bb5Sswilcox 		}
3877c478bd9Sstevel@tonic-gate 	}
388355d6bb5Sswilcox 
3897c478bd9Sstevel@tonic-gate 	if ((fflag || !(islog && islogok)) &&
390355d6bb5Sswilcox 	    (memcmp((void *)&cstotal, (void *)&fs->fs_cstotal,
391355d6bb5Sswilcox 	    sizeof (struct csum)) != 0)) {
392355d6bb5Sswilcox 		if (dofix(&idesc, "CORRECT GLOBAL SUMMARY")) {
393355d6bb5Sswilcox 			(void) memmove((void *)&fs->fs_cstotal,
394355d6bb5Sswilcox 			    (void *)&cstotal, sizeof (struct csum));
395355d6bb5Sswilcox 			fs->fs_ronly = 0;
396355d6bb5Sswilcox 			fs->fs_fmod = 0;
397355d6bb5Sswilcox 			sbdirty();
398355d6bb5Sswilcox 		} else {
399355d6bb5Sswilcox 			iscorrupt = 1;
400355d6bb5Sswilcox 		}
401355d6bb5Sswilcox 	}
402355d6bb5Sswilcox }
403355d6bb5Sswilcox 
404355d6bb5Sswilcox /*
405355d6bb5Sswilcox  * Compare two allocation bitmaps, reporting any discrepancies.
406355d6bb5Sswilcox  *
407355d6bb5Sswilcox  * If a mismatch is found, if the bit is set in map1, it's considered
408355d6bb5Sswilcox  * to be an indication that the corresponding resource is supposed
409355d6bb5Sswilcox  * to be free, but isn't.  Otherwise, it's considered marked as allocated
410355d6bb5Sswilcox  * but not found to be so.  In other words, if the two maps being compared
411355d6bb5Sswilcox  * use a set bit to indicate something is free, pass the on-disk map
412355d6bb5Sswilcox  * first.  Otherwise, pass the calculated map first.
413355d6bb5Sswilcox  */
414355d6bb5Sswilcox static int
415355d6bb5Sswilcox check_maps(
416355d6bb5Sswilcox 	uchar_t *map1,	/* map of claimed allocations */
417355d6bb5Sswilcox 	uchar_t *map2,	/* map of determined allocations */
418355d6bb5Sswilcox 	int mapsize,	/* size of above two maps */
419355d6bb5Sswilcox 	int startvalue,	/* resource value for first element in map */
420355d6bb5Sswilcox 	char *name,	/* name of resource found in maps */
421355d6bb5Sswilcox 	int skip,	/* number of entries to skip before starting to free */
422355d6bb5Sswilcox 	int limit)	/* limit on number of entries to free */
423355d6bb5Sswilcox {
424355d6bb5Sswilcox 	long i, j, k, l, m, n, size;
425355d6bb5Sswilcox 	int astart, aend, ustart, uend;
426355d6bb5Sswilcox 	int mismatch;
427355d6bb5Sswilcox 
428355d6bb5Sswilcox 	mismatch = 0;
429355d6bb5Sswilcox 	astart = ustart = aend = uend = -1;
430355d6bb5Sswilcox 	for (i = 0; i < mapsize; i++) {
431355d6bb5Sswilcox 		j = *map1++;
432355d6bb5Sswilcox 		k = *map2++;
433355d6bb5Sswilcox 		if (j == k)
434355d6bb5Sswilcox 			continue;
435355d6bb5Sswilcox 		for (m = 0, l = 1; m < NBBY; m++, l <<= 1) {
436355d6bb5Sswilcox 			if ((j & l) == (k & l))
437355d6bb5Sswilcox 				continue;
438355d6bb5Sswilcox 			n = startvalue + i * NBBY + m;
439355d6bb5Sswilcox 			if ((j & l) != 0) {
440355d6bb5Sswilcox 				if (astart == -1) {
441355d6bb5Sswilcox 					astart = aend = n;
442355d6bb5Sswilcox 					continue;
443355d6bb5Sswilcox 				}
444355d6bb5Sswilcox 				if (aend + 1 == n) {
445355d6bb5Sswilcox 					aend = n;
446355d6bb5Sswilcox 					continue;
447355d6bb5Sswilcox 				}
448355d6bb5Sswilcox 				if (verbose) {
449355d6bb5Sswilcox 					if (astart == aend)
450355d6bb5Sswilcox 						pwarn(
451355d6bb5Sswilcox 			    "ALLOCATED %s %d WAS MARKED FREE ON DISK\n",
452355d6bb5Sswilcox 						    name, astart);
453355d6bb5Sswilcox 					else
454355d6bb5Sswilcox 						pwarn(
455355d6bb5Sswilcox 			    "ALLOCATED %sS %d-%d WERE MARKED FREE ON DISK\n",
456355d6bb5Sswilcox 						    name, astart, aend);
457355d6bb5Sswilcox 				}
458355d6bb5Sswilcox 				mismatch = 1;
459355d6bb5Sswilcox 				astart = aend = n;
460355d6bb5Sswilcox 			} else {
461355d6bb5Sswilcox 				if (ustart == -1) {
462355d6bb5Sswilcox 					ustart = uend = n;
463355d6bb5Sswilcox 					continue;
464355d6bb5Sswilcox 				}
465355d6bb5Sswilcox 				if (uend + 1 == n) {
466355d6bb5Sswilcox 					uend = n;
467355d6bb5Sswilcox 					continue;
468355d6bb5Sswilcox 				}
469355d6bb5Sswilcox 				size = uend - ustart + 1;
470355d6bb5Sswilcox 				if (size <= skip) {
471355d6bb5Sswilcox 					skip -= size;
472355d6bb5Sswilcox 					ustart = uend = n;
473355d6bb5Sswilcox 					continue;
474355d6bb5Sswilcox 				}
475355d6bb5Sswilcox 				if (skip > 0) {
476355d6bb5Sswilcox 					ustart += skip;
477355d6bb5Sswilcox 					size -= skip;
478355d6bb5Sswilcox 					skip = 0;
479355d6bb5Sswilcox 				}
480355d6bb5Sswilcox 				if (size > limit)
481355d6bb5Sswilcox 					size = limit;
482355d6bb5Sswilcox 				if (verbose) {
483355d6bb5Sswilcox 					if (size == 1)
484355d6bb5Sswilcox 						pwarn(
485355d6bb5Sswilcox 			    "UNALLOCATED %s %d WAS MARKED USED ON DISK\n",
486355d6bb5Sswilcox 						    name, ustart);
487355d6bb5Sswilcox 					else
488355d6bb5Sswilcox 						pwarn(
489355d6bb5Sswilcox 			    "UNALLOCATED %sS %d-%ld WERE MARKED USED ON DISK\n",
490355d6bb5Sswilcox 						    name, ustart,
491355d6bb5Sswilcox 						    ustart + size - 1);
492355d6bb5Sswilcox 				}
493355d6bb5Sswilcox 				mismatch = 1;
494355d6bb5Sswilcox 				limit -= size;
495355d6bb5Sswilcox 				if (limit <= 0)
496355d6bb5Sswilcox 					return (mismatch);
497355d6bb5Sswilcox 				ustart = uend = n;
498355d6bb5Sswilcox 			}
499355d6bb5Sswilcox 		}
500355d6bb5Sswilcox 	}
501355d6bb5Sswilcox 	if (astart != -1) {
502355d6bb5Sswilcox 		if (verbose) {
503355d6bb5Sswilcox 			if (astart == aend)
504355d6bb5Sswilcox 				pwarn(
505355d6bb5Sswilcox 			    "ALLOCATED %s %d WAS MARKED FREE ON DISK\n",
506355d6bb5Sswilcox 				    name, astart);
507355d6bb5Sswilcox 			else
508355d6bb5Sswilcox 				pwarn(
509355d6bb5Sswilcox 			    "ALLOCATED %sS %d-%d WERE MARKED FREE ON DISK\n",
510355d6bb5Sswilcox 				    name, astart, aend);
511355d6bb5Sswilcox 		}
512355d6bb5Sswilcox 		mismatch = 1;
513355d6bb5Sswilcox 	}
514355d6bb5Sswilcox 	if (ustart != -1) {
515355d6bb5Sswilcox 		size = uend - ustart + 1;
516355d6bb5Sswilcox 		if (size <= skip)
517355d6bb5Sswilcox 			return (mismatch);
518355d6bb5Sswilcox 		if (skip > 0) {
519355d6bb5Sswilcox 			ustart += skip;
520355d6bb5Sswilcox 			size -= skip;
521355d6bb5Sswilcox 		}
522355d6bb5Sswilcox 		if (size > limit)
523355d6bb5Sswilcox 			size = limit;
524355d6bb5Sswilcox 		if (verbose) {
525355d6bb5Sswilcox 			if (size == 1)
526355d6bb5Sswilcox 				pwarn(
527355d6bb5Sswilcox 			    "UNALLOCATED %s %d WAS MARKED USED ON DISK\n",
528355d6bb5Sswilcox 				    name, ustart);
529355d6bb5Sswilcox 			else
530355d6bb5Sswilcox 				pwarn(
531355d6bb5Sswilcox 		    "UNALLOCATED %sS %d-%ld WERE MARKED USED ON DISK\n",
532355d6bb5Sswilcox 				    name, ustart, ustart + size - 1);
533355d6bb5Sswilcox 		}
534355d6bb5Sswilcox 		mismatch = 1;
5357c478bd9Sstevel@tonic-gate 	}
536355d6bb5Sswilcox 	return (mismatch);
5377c478bd9Sstevel@tonic-gate }
538