pass5.c revision 143235
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1980, 1986, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * Redistribution and use in source and binary forms, with or without
61558Srgrimes * modification, are permitted provided that the following conditions
71558Srgrimes * are met:
81558Srgrimes * 1. Redistributions of source code must retain the above copyright
91558Srgrimes *    notice, this list of conditions and the following disclaimer.
101558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111558Srgrimes *    notice, this list of conditions and the following disclaimer in the
121558Srgrimes *    documentation and/or other materials provided with the distribution.
131558Srgrimes * 4. Neither the name of the University nor the names of its contributors
141558Srgrimes *    may be used to endorse or promote products derived from this software
151558Srgrimes *    without specific prior written permission.
161558Srgrimes *
171558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271558Srgrimes * SUCH DAMAGE.
281558Srgrimes */
291558Srgrimes
30102411Scharnier#if 0
311558Srgrimes#ifndef lint
3223675Speterstatic const char sccsid[] = "@(#)pass5.c	8.9 (Berkeley) 4/28/95";
33102411Scharnier#endif /* not lint */
3441477Sjulian#endif
35102411Scharnier#include <sys/cdefs.h>
36102411Scharnier__FBSDID("$FreeBSD: head/sbin/fsck_ffs/pass5.c 143235 2005-03-07 08:42:49Z delphij $");
37102411Scharnier
381558Srgrimes#include <sys/param.h>
3974556Smckusick#include <sys/sysctl.h>
4023675Speter
411558Srgrimes#include <ufs/ufs/dinode.h>
421558Srgrimes#include <ufs/ffs/fs.h>
4323799Sbde
4423675Speter#include <err.h>
45142123Sdelphij#include <inttypes.h>
46103949Smike#include <limits.h>
471558Srgrimes#include <string.h>
4823675Speter
491558Srgrimes#include "fsck.h"
501558Srgrimes
51100935Sphkstatic void check_maps(u_char *, u_char *, int, int, const char *, int *, int, int);
5274556Smckusick
537585Sbdevoid
5492839Simppass5(void)
551558Srgrimes{
5698542Smckusick	int c, i, j, blk, frags, basesize, mapsize;
5774556Smckusick	int inomapsize, blkmapsize;
5823675Speter	struct fs *fs = &sblock;
5923675Speter	struct cg *cg = &cgrp;
6098542Smckusick	ufs2_daddr_t d, dbase, dmax;
6198542Smckusick	int excessdirs, rewritecg = 0;
621558Srgrimes	struct csum *cs;
6398542Smckusick	struct csum_total cstotal;
641558Srgrimes	struct inodesc idesc[3];
651558Srgrimes	char buf[MAXBSIZE];
6692806Sobrien	struct cg *newcg = (struct cg *)buf;
671558Srgrimes
6841474Sjulian	inoinfo(WINO)->ino_state = USTATE;
6923675Speter	memset(newcg, 0, (size_t)fs->fs_cgsize);
701558Srgrimes	newcg->cg_niblk = fs->fs_ipg;
7123675Speter	if (cvtlevel >= 3) {
721558Srgrimes		if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) {
731558Srgrimes			if (preen)
741558Srgrimes				pwarn("DELETING CLUSTERING MAPS\n");
751558Srgrimes			if (preen || reply("DELETE CLUSTERING MAPS")) {
761558Srgrimes				fs->fs_contigsumsize = 0;
7796483Sphk				rewritecg = 1;
781558Srgrimes				sbdirty();
791558Srgrimes			}
801558Srgrimes		}
811558Srgrimes		if (fs->fs_maxcontig > 1) {
82100935Sphk			const char *doit = 0;
831558Srgrimes
841558Srgrimes			if (fs->fs_contigsumsize < 1) {
851558Srgrimes				doit = "CREAT";
861558Srgrimes			} else if (fs->fs_contigsumsize < fs->fs_maxcontig &&
871558Srgrimes				   fs->fs_contigsumsize < FS_MAXCONTIG) {
881558Srgrimes				doit = "EXPAND";
891558Srgrimes			}
901558Srgrimes			if (doit) {
911558Srgrimes				i = fs->fs_contigsumsize;
921558Srgrimes				fs->fs_contigsumsize =
931558Srgrimes				    MIN(fs->fs_maxcontig, FS_MAXCONTIG);
9474556Smckusick				if (CGSIZE(fs) > (u_int)fs->fs_bsize) {
951558Srgrimes					pwarn("CANNOT %s CLUSTER MAPS\n", doit);
961558Srgrimes					fs->fs_contigsumsize = i;
971558Srgrimes				} else if (preen ||
981558Srgrimes				    reply("CREATE CLUSTER MAPS")) {
991558Srgrimes					if (preen)
1001558Srgrimes						pwarn("%sING CLUSTER MAPS\n",
1011558Srgrimes						    doit);
1021558Srgrimes					fs->fs_cgsize =
1031558Srgrimes					    fragroundup(fs, CGSIZE(fs));
10496483Sphk					rewritecg = 1;
1051558Srgrimes					sbdirty();
1061558Srgrimes				}
1071558Srgrimes			}
1081558Srgrimes		}
1091558Srgrimes	}
11098542Smckusick	basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield);
11198542Smckusick	if (sblock.fs_magic == FS_UFS2_MAGIC) {
11298542Smckusick		newcg->cg_iusedoff = basesize;
11398542Smckusick	} else {
11498542Smckusick		/*
11598542Smckusick		 * We reserve the space for the old rotation summary
11698542Smckusick		 * tables for the benefit of old kernels, but do not
11798542Smckusick		 * maintain them in modern kernels. In time, they can
11898542Smckusick		 * go away.
11998542Smckusick		 */
12098542Smckusick		newcg->cg_old_btotoff = basesize;
12198542Smckusick		newcg->cg_old_boff = newcg->cg_old_btotoff +
12298542Smckusick		    fs->fs_old_cpg * sizeof(int32_t);
12398542Smckusick		newcg->cg_iusedoff = newcg->cg_old_boff +
12498542Smckusick		    fs->fs_old_cpg * fs->fs_old_nrpos * sizeof(u_int16_t);
12598542Smckusick		memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
1261558Srgrimes	}
127103949Smike	inomapsize = howmany(fs->fs_ipg, CHAR_BIT);
12898542Smckusick	newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
129103949Smike	blkmapsize = howmany(fs->fs_fpg, CHAR_BIT);
13098542Smckusick	newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
13198542Smckusick	if (fs->fs_contigsumsize > 0) {
13298542Smckusick		newcg->cg_clustersumoff = newcg->cg_nextfreeoff -
13398542Smckusick		    sizeof(u_int32_t);
13498542Smckusick		newcg->cg_clustersumoff =
13598542Smckusick		    roundup(newcg->cg_clustersumoff, sizeof(u_int32_t));
13698542Smckusick		newcg->cg_clusteroff = newcg->cg_clustersumoff +
13798542Smckusick		    (fs->fs_contigsumsize + 1) * sizeof(u_int32_t);
13898542Smckusick		newcg->cg_nextfreeoff = newcg->cg_clusteroff +
139103949Smike		    howmany(fragstoblks(fs, fs->fs_fpg), CHAR_BIT);
14098542Smckusick	}
14198542Smckusick	newcg->cg_magic = CG_MAGIC;
14298542Smckusick	mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
14323675Speter	memset(&idesc[0], 0, sizeof idesc);
14496483Sphk	for (i = 0; i < 3; i++)
1451558Srgrimes		idesc[i].id_type = ADDR;
14698542Smckusick	memset(&cstotal, 0, sizeof(struct csum_total));
14798542Smckusick	dmax = blknum(fs, fs->fs_size + fs->fs_frag - 1);
14898542Smckusick	for (d = fs->fs_size; d < dmax; d++)
14998542Smckusick		setbmap(d);
1501558Srgrimes	for (c = 0; c < fs->fs_ncg; c++) {
15170050Siedowse		if (got_siginfo) {
15270050Siedowse			printf("%s: phase 5: cyl group %d of %d (%d%%)\n",
15370050Siedowse			    cdevname, c, sblock.fs_ncg,
15470050Siedowse			    c * 100 / sblock.fs_ncg);
15570050Siedowse			got_siginfo = 0;
15670050Siedowse		}
157126345Sscottl		if (got_sigalarm) {
158127638Sdwmalone			setproctitle("%s p5 %d%%", cdevname,
159126345Sscottl			    c * 100 / sblock.fs_ncg);
160126345Sscottl			got_sigalarm = 0;
161126345Sscottl		}
1621558Srgrimes		getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
1631558Srgrimes		if (!cg_chkmagic(cg))
1641558Srgrimes			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
16598542Smckusick		newcg->cg_time = cg->cg_time;
16698542Smckusick		newcg->cg_old_time = cg->cg_old_time;
16798542Smckusick		newcg->cg_cgx = c;
1681558Srgrimes		dbase = cgbase(fs, c);
1691558Srgrimes		dmax = dbase + fs->fs_fpg;
1701558Srgrimes		if (dmax > fs->fs_size)
1711558Srgrimes			dmax = fs->fs_size;
1721558Srgrimes		newcg->cg_ndblk = dmax - dbase;
17398542Smckusick		if (fs->fs_magic == FS_UFS1_MAGIC) {
17498542Smckusick			if (c == fs->fs_ncg - 1)
17598542Smckusick				newcg->cg_old_ncyl = howmany(newcg->cg_ndblk,
17698542Smckusick				    fs->fs_fpg / fs->fs_old_cpg);
17798542Smckusick			else
17898542Smckusick				newcg->cg_old_ncyl = fs->fs_old_cpg;
17998542Smckusick			newcg->cg_old_niblk = fs->fs_ipg;
18098542Smckusick			newcg->cg_niblk = 0;
18198542Smckusick		}
1821558Srgrimes		if (fs->fs_contigsumsize > 0)
1831558Srgrimes			newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag;
1841558Srgrimes		newcg->cg_cs.cs_ndir = 0;
1851558Srgrimes		newcg->cg_cs.cs_nffree = 0;
1861558Srgrimes		newcg->cg_cs.cs_nbfree = 0;
1871558Srgrimes		newcg->cg_cs.cs_nifree = fs->fs_ipg;
188107829Simp		if (cg->cg_rotor >= 0 && cg->cg_rotor < newcg->cg_ndblk)
1891558Srgrimes			newcg->cg_rotor = cg->cg_rotor;
1901558Srgrimes		else
1911558Srgrimes			newcg->cg_rotor = 0;
192107829Simp		if (cg->cg_frotor >= 0 && cg->cg_frotor < newcg->cg_ndblk)
1931558Srgrimes			newcg->cg_frotor = cg->cg_frotor;
1941558Srgrimes		else
1951558Srgrimes			newcg->cg_frotor = 0;
196107829Simp		if (cg->cg_irotor >= 0 && cg->cg_irotor < fs->fs_ipg)
1971558Srgrimes			newcg->cg_irotor = cg->cg_irotor;
1981558Srgrimes		else
1991558Srgrimes			newcg->cg_irotor = 0;
20098542Smckusick		if (fs->fs_magic == FS_UFS1_MAGIC) {
20198542Smckusick			newcg->cg_initediblk = 0;
20298542Smckusick		} else {
20398542Smckusick			if ((unsigned)cg->cg_initediblk > fs->fs_ipg)
20498542Smckusick				newcg->cg_initediblk = fs->fs_ipg;
20598542Smckusick			else
20698542Smckusick				newcg->cg_initediblk = cg->cg_initediblk;
20798542Smckusick		}
20823675Speter		memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum);
20998542Smckusick		memset(cg_inosused(newcg), 0, (size_t)(mapsize));
2101558Srgrimes		j = fs->fs_ipg * c;
21141474Sjulian		for (i = 0; i < inostathead[c].il_numalloced; j++, i++) {
21241474Sjulian			switch (inoinfo(j)->ino_state) {
2131558Srgrimes
2141558Srgrimes			case USTATE:
2151558Srgrimes				break;
2161558Srgrimes
2171558Srgrimes			case DSTATE:
2181558Srgrimes			case DCLEAR:
2191558Srgrimes			case DFOUND:
220136281Struckman			case DZLINK:
2211558Srgrimes				newcg->cg_cs.cs_ndir++;
222102411Scharnier				/* FALLTHROUGH */
2231558Srgrimes
2241558Srgrimes			case FSTATE:
2251558Srgrimes			case FCLEAR:
226136281Struckman			case FZLINK:
2271558Srgrimes				newcg->cg_cs.cs_nifree--;
2281558Srgrimes				setbit(cg_inosused(newcg), i);
2291558Srgrimes				break;
2301558Srgrimes
2311558Srgrimes			default:
23274556Smckusick				if (j < (int)ROOTINO)
2331558Srgrimes					break;
23486514Siedowse				errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
23541474Sjulian				    inoinfo(j)->ino_state, j);
2361558Srgrimes			}
2371558Srgrimes		}
2381558Srgrimes		if (c == 0)
23974556Smckusick			for (i = 0; i < (int)ROOTINO; i++) {
2401558Srgrimes				setbit(cg_inosused(newcg), i);
2411558Srgrimes				newcg->cg_cs.cs_nifree--;
2421558Srgrimes			}
2431558Srgrimes		for (i = 0, d = dbase;
2441558Srgrimes		     d < dmax;
2451558Srgrimes		     d += fs->fs_frag, i += fs->fs_frag) {
2461558Srgrimes			frags = 0;
2471558Srgrimes			for (j = 0; j < fs->fs_frag; j++) {
2481558Srgrimes				if (testbmap(d + j))
2491558Srgrimes					continue;
2501558Srgrimes				setbit(cg_blksfree(newcg), i + j);
2511558Srgrimes				frags++;
2521558Srgrimes			}
2531558Srgrimes			if (frags == fs->fs_frag) {
2541558Srgrimes				newcg->cg_cs.cs_nbfree++;
2551558Srgrimes				if (fs->fs_contigsumsize > 0)
2561558Srgrimes					setbit(cg_clustersfree(newcg),
2571558Srgrimes					    i / fs->fs_frag);
2581558Srgrimes			} else if (frags > 0) {
2591558Srgrimes				newcg->cg_cs.cs_nffree += frags;
2601558Srgrimes				blk = blkmap(fs, cg_blksfree(newcg), i);
2611558Srgrimes				ffs_fragacct(fs, blk, newcg->cg_frsum, 1);
2621558Srgrimes			}
2631558Srgrimes		}
2641558Srgrimes		if (fs->fs_contigsumsize > 0) {
26523675Speter			int32_t *sump = cg_clustersum(newcg);
2661558Srgrimes			u_char *mapp = cg_clustersfree(newcg);
2671558Srgrimes			int map = *mapp++;
2681558Srgrimes			int bit = 1;
2691558Srgrimes			int run = 0;
2701558Srgrimes
2711558Srgrimes			for (i = 0; i < newcg->cg_nclusterblks; i++) {
2721558Srgrimes				if ((map & bit) != 0) {
2731558Srgrimes					run++;
2741558Srgrimes				} else if (run != 0) {
2751558Srgrimes					if (run > fs->fs_contigsumsize)
2761558Srgrimes						run = fs->fs_contigsumsize;
2771558Srgrimes					sump[run]++;
2781558Srgrimes					run = 0;
2791558Srgrimes				}
280103949Smike				if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
2811558Srgrimes					bit <<= 1;
2821558Srgrimes				} else {
2831558Srgrimes					map = *mapp++;
2841558Srgrimes					bit = 1;
2851558Srgrimes				}
2861558Srgrimes			}
2871558Srgrimes			if (run != 0) {
2881558Srgrimes				if (run > fs->fs_contigsumsize)
2891558Srgrimes					run = fs->fs_contigsumsize;
2901558Srgrimes				sump[run]++;
2911558Srgrimes			}
2921558Srgrimes		}
2931558Srgrimes		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
2941558Srgrimes		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
2951558Srgrimes		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
2961558Srgrimes		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
2971558Srgrimes		cs = &fs->fs_cs(fs, c);
29874556Smckusick		if (cursnapshot == 0 &&
29974556Smckusick		    memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0 &&
3001558Srgrimes		    dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
30123675Speter			memmove(cs, &newcg->cg_cs, sizeof *cs);
3021558Srgrimes			sbdirty();
3031558Srgrimes		}
30496483Sphk		if (rewritecg) {
30523675Speter			memmove(cg, newcg, (size_t)fs->fs_cgsize);
3061558Srgrimes			cgdirty();
3071558Srgrimes			continue;
3081558Srgrimes		}
30974556Smckusick		if (cursnapshot == 0 &&
31098542Smckusick		    memcmp(newcg, cg, basesize) != 0 &&
3111558Srgrimes		    dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
31223675Speter			memmove(cg, newcg, (size_t)basesize);
3131558Srgrimes			cgdirty();
3141558Srgrimes		}
31574556Smckusick		if (bkgrdflag != 0 || usedsoftdep || debug) {
31674556Smckusick			excessdirs = cg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
31774556Smckusick			if (excessdirs < 0) {
31874556Smckusick				pfatal("LOST %d DIRECTORIES\n", -excessdirs);
31974556Smckusick				excessdirs = 0;
32062668Smckusick			}
32174556Smckusick			if (excessdirs > 0)
32274556Smckusick				check_maps(cg_inosused(newcg), cg_inosused(cg),
32374556Smckusick				    inomapsize, cg->cg_cgx * fs->fs_ipg, "DIR",
32474556Smckusick				    freedirs, 0, excessdirs);
32574556Smckusick			check_maps(cg_inosused(newcg), cg_inosused(cg),
32674556Smckusick			    inomapsize, cg->cg_cgx * fs->fs_ipg, "FILE",
32774556Smckusick			    freefiles, excessdirs, fs->fs_ipg);
32874556Smckusick			check_maps(cg_blksfree(cg), cg_blksfree(newcg),
32974556Smckusick			    blkmapsize, cg->cg_cgx * fs->fs_fpg, "FRAG",
33074556Smckusick			    freeblks, 0, fs->fs_fpg);
33162668Smckusick		}
33274556Smckusick		if (cursnapshot == 0 &&
33374556Smckusick		    memcmp(cg_inosused(newcg), cg_inosused(cg), mapsize) != 0 &&
33434266Sjulian		    dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
33534266Sjulian			memmove(cg_inosused(cg), cg_inosused(newcg),
33634266Sjulian			      (size_t)mapsize);
33734266Sjulian			cgdirty();
33834266Sjulian		}
3391558Srgrimes	}
34074556Smckusick	if (cursnapshot == 0 &&
34198542Smckusick	    memcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal) != 0
34298542Smckusick	    && dofix(&idesc[0], "SUMMARY BLK COUNT(S) WRONG IN SUPERBLK")) {
34398542Smckusick		memmove(&fs->fs_cstotal, &cstotal, sizeof cstotal);
3441558Srgrimes		fs->fs_ronly = 0;
34541474Sjulian		fs->fs_fmod = 0;
3461558Srgrimes		sbdirty();
3471558Srgrimes	}
348142123Sdelphij
349142123Sdelphij	/*
350142123Sdelphij	 * When doing background fsck on a snapshot, figure out whether
351142123Sdelphij	 * the superblock summary is inaccurate and correct it when
352142123Sdelphij	 * necessary.
353142123Sdelphij	 */
354142123Sdelphij	if (cursnapshot != 0) {
355142123Sdelphij		cmd.size = 1;
356142123Sdelphij
357142123Sdelphij		cmd.value = cstotal.cs_ndir - fs->fs_cstotal.cs_ndir;
358142123Sdelphij		if (cmd.value != 0) {
359142123Sdelphij			if (debug)
360142123Sdelphij				printf("adjndir by %+" PRIi64 "\n", cmd.value);
361143235Sdelphij			if (bkgrdsumadj == 0 || sysctl(adjndir, MIBSIZE, 0, 0,
362142123Sdelphij			    &cmd, sizeof cmd) == -1)
363142123Sdelphij				rwerror("ADJUST NUMBER OF DIRECTORIES", cmd.value);
364142123Sdelphij		}
365142123Sdelphij
366142123Sdelphij		cmd.value = cstotal.cs_nbfree - fs->fs_cstotal.cs_nbfree;
367142123Sdelphij		if (cmd.value != 0) {
368142123Sdelphij			if (debug)
369142123Sdelphij				printf("adjnbfree by %+" PRIi64 "\n", cmd.value);
370143235Sdelphij			if (bkgrdsumadj == 0 || sysctl(adjnbfree, MIBSIZE, 0, 0,
371142123Sdelphij			    &cmd, sizeof cmd) == -1)
372142123Sdelphij				rwerror("ADJUST NUMBER OF FREE BLOCKS", cmd.value);
373142123Sdelphij		}
374142123Sdelphij
375142123Sdelphij		cmd.value = cstotal.cs_nifree - fs->fs_cstotal.cs_nifree;
376142123Sdelphij		if (cmd.value != 0) {
377142123Sdelphij			if (debug)
378142123Sdelphij				printf("adjnifree by %+" PRIi64 "\n", cmd.value);
379143235Sdelphij			if (bkgrdsumadj == 0 || sysctl(adjnifree, MIBSIZE, 0, 0,
380142123Sdelphij			    &cmd, sizeof cmd) == -1)
381142123Sdelphij				rwerror("ADJUST NUMBER OF FREE INODES", cmd.value);
382142123Sdelphij		}
383142123Sdelphij
384142123Sdelphij		cmd.value = cstotal.cs_nffree - fs->fs_cstotal.cs_nffree;
385142123Sdelphij		if (cmd.value != 0) {
386142123Sdelphij			if (debug)
387142123Sdelphij				printf("adjnffree by %+" PRIi64 "\n", cmd.value);
388143235Sdelphij			if (bkgrdsumadj == 0 || sysctl(adjnffree, MIBSIZE, 0, 0,
389142123Sdelphij			    &cmd, sizeof cmd) == -1)
390142123Sdelphij				rwerror("ADJUST NUMBER OF FREE FRAGS", cmd.value);
391142123Sdelphij		}
392142123Sdelphij
393142123Sdelphij		cmd.value = cstotal.cs_numclusters - fs->fs_cstotal.cs_numclusters;
394142123Sdelphij		if (cmd.value != 0) {
395142123Sdelphij			if (debug)
396142123Sdelphij				printf("adjnumclusters by %+" PRIi64 "\n", cmd.value);
397143235Sdelphij			if (bkgrdsumadj == 0 || sysctl(adjnumclusters, MIBSIZE, 0, 0,
398142123Sdelphij			    &cmd, sizeof cmd) == -1)
399142123Sdelphij				rwerror("ADJUST NUMBER OF FREE CLUSTERS", cmd.value);
400142123Sdelphij		}
401142123Sdelphij	}
4021558Srgrimes}
40374556Smckusick
40474556Smckusickstatic void
40592839Simpcheck_maps(
40692839Simp	u_char *map1,	/* map of claimed allocations */
40792839Simp	u_char *map2,	/* map of determined allocations */
40892839Simp	int mapsize,	/* size of above two maps */
40992839Simp	int startvalue,	/* resource value for first element in map */
410100935Sphk	const char *name,	/* name of resource found in maps */
41192839Simp	int *opcode,	/* sysctl opcode to free resource */
41292839Simp	int skip,	/* number of entries to skip before starting to free */
41392839Simp	int limit)	/* limit on number of entries to free */
41474556Smckusick{
41574556Smckusick#	define BUFSIZE 16
41674556Smckusick	char buf[BUFSIZE];
41774556Smckusick	long i, j, k, l, m, n, size;
41874556Smckusick	int astart, aend, ustart, uend;
41992839Simp	void (*msg)(const char *fmt, ...);
42074556Smckusick
42175557Smckusick	if (bkgrdflag)
42275557Smckusick		msg = pfatal;
42375557Smckusick	else
42475557Smckusick		msg = pwarn;
42574556Smckusick	astart = ustart = aend = uend = -1;
42674556Smckusick	for (i = 0; i < mapsize; i++) {
42774556Smckusick		j = *map1++;
42874556Smckusick		k = *map2++;
42974556Smckusick		if (j == k)
43074556Smckusick			continue;
431103949Smike		for (m = 0, l = 1; m < CHAR_BIT; m++, l <<= 1) {
43274556Smckusick			if ((j & l) == (k & l))
43374556Smckusick				continue;
434103949Smike			n = startvalue + i * CHAR_BIT + m;
43574556Smckusick			if ((j & l) != 0) {
43674556Smckusick				if (astart == -1) {
43774556Smckusick					astart = aend = n;
43874556Smckusick					continue;
43974556Smckusick				}
44074556Smckusick				if (aend + 1 == n) {
44174556Smckusick					aend = n;
44274556Smckusick					continue;
44374556Smckusick				}
44474556Smckusick				if (astart == aend)
44575557Smckusick					(*msg)("ALLOCATED %s %d MARKED FREE\n",
44674556Smckusick					    name, astart);
44774556Smckusick				else
44875557Smckusick					(*msg)("%s %sS %d-%d MARKED FREE\n",
44974556Smckusick					    "ALLOCATED", name, astart, aend);
45074556Smckusick				astart = aend = n;
45174556Smckusick			} else {
45274556Smckusick				if (ustart == -1) {
45374556Smckusick					ustart = uend = n;
45474556Smckusick					continue;
45574556Smckusick				}
45674556Smckusick				if (uend + 1 == n) {
45774556Smckusick					uend = n;
45874556Smckusick					continue;
45974556Smckusick				}
46074556Smckusick				size = uend - ustart + 1;
46174556Smckusick				if (size <= skip) {
46274556Smckusick					skip -= size;
46374556Smckusick					ustart = uend = n;
46474556Smckusick					continue;
46574556Smckusick				}
46674556Smckusick				if (skip > 0) {
46774556Smckusick					ustart += skip;
46874556Smckusick					size -= skip;
46974556Smckusick					skip = 0;
47074556Smckusick				}
47174556Smckusick				if (size > limit)
47274556Smckusick					size = limit;
47374556Smckusick				if (debug && size == 1)
47474556Smckusick					pwarn("%s %s %d MARKED USED\n",
47574556Smckusick					    "UNALLOCATED", name, ustart);
47674556Smckusick				else if (debug)
47786514Siedowse					pwarn("%s %sS %d-%ld MARKED USED\n",
47874556Smckusick					    "UNALLOCATED", name, ustart,
47974556Smckusick					    ustart + size - 1);
48074556Smckusick				if (bkgrdflag != 0) {
48174556Smckusick					cmd.value = ustart;
48274556Smckusick					cmd.size = size;
48374556Smckusick					if (sysctl(opcode, MIBSIZE, 0, 0,
48474556Smckusick					    &cmd, sizeof cmd) == -1) {
48574556Smckusick						snprintf(buf, BUFSIZE,
48674556Smckusick						    "FREE %s", name);
48774556Smckusick						rwerror(buf, cmd.value);
48874556Smckusick					}
48974556Smckusick				}
49074556Smckusick				limit -= size;
49174556Smckusick				if (limit <= 0)
49274556Smckusick					return;
49374556Smckusick				ustart = uend = n;
49474556Smckusick			}
49574556Smckusick		}
49674556Smckusick	}
49786514Siedowse	if (astart != -1) {
49874556Smckusick		if (astart == aend)
49975557Smckusick			(*msg)("ALLOCATED %s %d MARKED FREE\n", name, astart);
50074556Smckusick		else
50175557Smckusick			(*msg)("ALLOCATED %sS %d-%d MARKED FREE\n",
50274556Smckusick			    name, astart, aend);
50386514Siedowse	}
50474556Smckusick	if (ustart != -1) {
50574556Smckusick		size = uend - ustart + 1;
50674556Smckusick		if (size <= skip)
50774556Smckusick			return;
50874556Smckusick		if (skip > 0) {
50974556Smckusick			ustart += skip;
51074556Smckusick			size -= skip;
51174556Smckusick		}
51274556Smckusick		if (size > limit)
51374556Smckusick			size = limit;
51474556Smckusick		if (debug) {
51574556Smckusick			if (size == 1)
51674556Smckusick				pwarn("UNALLOCATED %s %d MARKED USED\n",
51774556Smckusick				    name, ustart);
51874556Smckusick			else
51986514Siedowse				pwarn("UNALLOCATED %sS %d-%ld MARKED USED\n",
52074556Smckusick				    name, ustart, ustart + size - 1);
52174556Smckusick		}
52274556Smckusick		if (bkgrdflag != 0) {
52374556Smckusick			cmd.value = ustart;
52474556Smckusick			cmd.size = size;
52574556Smckusick			if (sysctl(opcode, MIBSIZE, 0, 0, &cmd,
52674556Smckusick			    sizeof cmd) == -1) {
52774556Smckusick				snprintf(buf, BUFSIZE, "FREE %s", name);
52874556Smckusick				rwerror(buf, cmd.value);
52974556Smckusick			}
53074556Smckusick		}
53174556Smckusick	}
53274556Smckusick}
533