pass5.c revision 127638
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 * 3. All advertising materials mentioning features or use of this software
141558Srgrimes *    must display the following acknowledgement:
151558Srgrimes *	This product includes software developed by the University of
161558Srgrimes *	California, Berkeley and its contributors.
171558Srgrimes * 4. Neither the name of the University nor the names of its contributors
181558Srgrimes *    may be used to endorse or promote products derived from this software
191558Srgrimes *    without specific prior written permission.
201558Srgrimes *
211558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311558Srgrimes * SUCH DAMAGE.
321558Srgrimes */
331558Srgrimes
34102411Scharnier#if 0
351558Srgrimes#ifndef lint
3623675Speterstatic const char sccsid[] = "@(#)pass5.c	8.9 (Berkeley) 4/28/95";
37102411Scharnier#endif /* not lint */
3841477Sjulian#endif
39102411Scharnier#include <sys/cdefs.h>
40102411Scharnier__FBSDID("$FreeBSD: head/sbin/fsck_ffs/pass5.c 127638 2004-03-30 20:01:25Z dwmalone $");
41102411Scharnier
421558Srgrimes#include <sys/param.h>
4374556Smckusick#include <sys/sysctl.h>
4423675Speter
451558Srgrimes#include <ufs/ufs/dinode.h>
461558Srgrimes#include <ufs/ffs/fs.h>
4723799Sbde
4823675Speter#include <err.h>
49103949Smike#include <limits.h>
501558Srgrimes#include <string.h>
5123675Speter
521558Srgrimes#include "fsck.h"
531558Srgrimes
54100935Sphkstatic void check_maps(u_char *, u_char *, int, int, const char *, int *, int, int);
5574556Smckusick
567585Sbdevoid
5792839Simppass5(void)
581558Srgrimes{
5998542Smckusick	int c, i, j, blk, frags, basesize, mapsize;
6074556Smckusick	int inomapsize, blkmapsize;
6123675Speter	struct fs *fs = &sblock;
6223675Speter	struct cg *cg = &cgrp;
6398542Smckusick	ufs2_daddr_t d, dbase, dmax;
6498542Smckusick	int excessdirs, rewritecg = 0;
651558Srgrimes	struct csum *cs;
6698542Smckusick	struct csum_total cstotal;
671558Srgrimes	struct inodesc idesc[3];
681558Srgrimes	char buf[MAXBSIZE];
6992806Sobrien	struct cg *newcg = (struct cg *)buf;
701558Srgrimes
7141474Sjulian	inoinfo(WINO)->ino_state = USTATE;
7223675Speter	memset(newcg, 0, (size_t)fs->fs_cgsize);
731558Srgrimes	newcg->cg_niblk = fs->fs_ipg;
7423675Speter	if (cvtlevel >= 3) {
751558Srgrimes		if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) {
761558Srgrimes			if (preen)
771558Srgrimes				pwarn("DELETING CLUSTERING MAPS\n");
781558Srgrimes			if (preen || reply("DELETE CLUSTERING MAPS")) {
791558Srgrimes				fs->fs_contigsumsize = 0;
8096483Sphk				rewritecg = 1;
811558Srgrimes				sbdirty();
821558Srgrimes			}
831558Srgrimes		}
841558Srgrimes		if (fs->fs_maxcontig > 1) {
85100935Sphk			const char *doit = 0;
861558Srgrimes
871558Srgrimes			if (fs->fs_contigsumsize < 1) {
881558Srgrimes				doit = "CREAT";
891558Srgrimes			} else if (fs->fs_contigsumsize < fs->fs_maxcontig &&
901558Srgrimes				   fs->fs_contigsumsize < FS_MAXCONTIG) {
911558Srgrimes				doit = "EXPAND";
921558Srgrimes			}
931558Srgrimes			if (doit) {
941558Srgrimes				i = fs->fs_contigsumsize;
951558Srgrimes				fs->fs_contigsumsize =
961558Srgrimes				    MIN(fs->fs_maxcontig, FS_MAXCONTIG);
9774556Smckusick				if (CGSIZE(fs) > (u_int)fs->fs_bsize) {
981558Srgrimes					pwarn("CANNOT %s CLUSTER MAPS\n", doit);
991558Srgrimes					fs->fs_contigsumsize = i;
1001558Srgrimes				} else if (preen ||
1011558Srgrimes				    reply("CREATE CLUSTER MAPS")) {
1021558Srgrimes					if (preen)
1031558Srgrimes						pwarn("%sING CLUSTER MAPS\n",
1041558Srgrimes						    doit);
1051558Srgrimes					fs->fs_cgsize =
1061558Srgrimes					    fragroundup(fs, CGSIZE(fs));
10796483Sphk					rewritecg = 1;
1081558Srgrimes					sbdirty();
1091558Srgrimes				}
1101558Srgrimes			}
1111558Srgrimes		}
1121558Srgrimes	}
11398542Smckusick	basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield);
11498542Smckusick	if (sblock.fs_magic == FS_UFS2_MAGIC) {
11598542Smckusick		newcg->cg_iusedoff = basesize;
11698542Smckusick	} else {
11798542Smckusick		/*
11898542Smckusick		 * We reserve the space for the old rotation summary
11998542Smckusick		 * tables for the benefit of old kernels, but do not
12098542Smckusick		 * maintain them in modern kernels. In time, they can
12198542Smckusick		 * go away.
12298542Smckusick		 */
12398542Smckusick		newcg->cg_old_btotoff = basesize;
12498542Smckusick		newcg->cg_old_boff = newcg->cg_old_btotoff +
12598542Smckusick		    fs->fs_old_cpg * sizeof(int32_t);
12698542Smckusick		newcg->cg_iusedoff = newcg->cg_old_boff +
12798542Smckusick		    fs->fs_old_cpg * fs->fs_old_nrpos * sizeof(u_int16_t);
12898542Smckusick		memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
1291558Srgrimes	}
130103949Smike	inomapsize = howmany(fs->fs_ipg, CHAR_BIT);
13198542Smckusick	newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
132103949Smike	blkmapsize = howmany(fs->fs_fpg, CHAR_BIT);
13398542Smckusick	newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
13498542Smckusick	if (fs->fs_contigsumsize > 0) {
13598542Smckusick		newcg->cg_clustersumoff = newcg->cg_nextfreeoff -
13698542Smckusick		    sizeof(u_int32_t);
13798542Smckusick		newcg->cg_clustersumoff =
13898542Smckusick		    roundup(newcg->cg_clustersumoff, sizeof(u_int32_t));
13998542Smckusick		newcg->cg_clusteroff = newcg->cg_clustersumoff +
14098542Smckusick		    (fs->fs_contigsumsize + 1) * sizeof(u_int32_t);
14198542Smckusick		newcg->cg_nextfreeoff = newcg->cg_clusteroff +
142103949Smike		    howmany(fragstoblks(fs, fs->fs_fpg), CHAR_BIT);
14398542Smckusick	}
14498542Smckusick	newcg->cg_magic = CG_MAGIC;
14598542Smckusick	mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
14623675Speter	memset(&idesc[0], 0, sizeof idesc);
14796483Sphk	for (i = 0; i < 3; i++)
1481558Srgrimes		idesc[i].id_type = ADDR;
14998542Smckusick	memset(&cstotal, 0, sizeof(struct csum_total));
15098542Smckusick	dmax = blknum(fs, fs->fs_size + fs->fs_frag - 1);
15198542Smckusick	for (d = fs->fs_size; d < dmax; d++)
15298542Smckusick		setbmap(d);
1531558Srgrimes	for (c = 0; c < fs->fs_ncg; c++) {
15470050Siedowse		if (got_siginfo) {
15570050Siedowse			printf("%s: phase 5: cyl group %d of %d (%d%%)\n",
15670050Siedowse			    cdevname, c, sblock.fs_ncg,
15770050Siedowse			    c * 100 / sblock.fs_ncg);
15870050Siedowse			got_siginfo = 0;
15970050Siedowse		}
160126345Sscottl		if (got_sigalarm) {
161127638Sdwmalone			setproctitle("%s p5 %d%%", cdevname,
162126345Sscottl			    c * 100 / sblock.fs_ncg);
163126345Sscottl			got_sigalarm = 0;
164126345Sscottl		}
1651558Srgrimes		getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
1661558Srgrimes		if (!cg_chkmagic(cg))
1671558Srgrimes			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
16898542Smckusick		newcg->cg_time = cg->cg_time;
16998542Smckusick		newcg->cg_old_time = cg->cg_old_time;
17098542Smckusick		newcg->cg_cgx = c;
1711558Srgrimes		dbase = cgbase(fs, c);
1721558Srgrimes		dmax = dbase + fs->fs_fpg;
1731558Srgrimes		if (dmax > fs->fs_size)
1741558Srgrimes			dmax = fs->fs_size;
1751558Srgrimes		newcg->cg_ndblk = dmax - dbase;
17698542Smckusick		if (fs->fs_magic == FS_UFS1_MAGIC) {
17798542Smckusick			if (c == fs->fs_ncg - 1)
17898542Smckusick				newcg->cg_old_ncyl = howmany(newcg->cg_ndblk,
17998542Smckusick				    fs->fs_fpg / fs->fs_old_cpg);
18098542Smckusick			else
18198542Smckusick				newcg->cg_old_ncyl = fs->fs_old_cpg;
18298542Smckusick			newcg->cg_old_niblk = fs->fs_ipg;
18398542Smckusick			newcg->cg_niblk = 0;
18498542Smckusick		}
1851558Srgrimes		if (fs->fs_contigsumsize > 0)
1861558Srgrimes			newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag;
1871558Srgrimes		newcg->cg_cs.cs_ndir = 0;
1881558Srgrimes		newcg->cg_cs.cs_nffree = 0;
1891558Srgrimes		newcg->cg_cs.cs_nbfree = 0;
1901558Srgrimes		newcg->cg_cs.cs_nifree = fs->fs_ipg;
191107829Simp		if (cg->cg_rotor >= 0 && cg->cg_rotor < newcg->cg_ndblk)
1921558Srgrimes			newcg->cg_rotor = cg->cg_rotor;
1931558Srgrimes		else
1941558Srgrimes			newcg->cg_rotor = 0;
195107829Simp		if (cg->cg_frotor >= 0 && cg->cg_frotor < newcg->cg_ndblk)
1961558Srgrimes			newcg->cg_frotor = cg->cg_frotor;
1971558Srgrimes		else
1981558Srgrimes			newcg->cg_frotor = 0;
199107829Simp		if (cg->cg_irotor >= 0 && cg->cg_irotor < fs->fs_ipg)
2001558Srgrimes			newcg->cg_irotor = cg->cg_irotor;
2011558Srgrimes		else
2021558Srgrimes			newcg->cg_irotor = 0;
20398542Smckusick		if (fs->fs_magic == FS_UFS1_MAGIC) {
20498542Smckusick			newcg->cg_initediblk = 0;
20598542Smckusick		} else {
20698542Smckusick			if ((unsigned)cg->cg_initediblk > fs->fs_ipg)
20798542Smckusick				newcg->cg_initediblk = fs->fs_ipg;
20898542Smckusick			else
20998542Smckusick				newcg->cg_initediblk = cg->cg_initediblk;
21098542Smckusick		}
21123675Speter		memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum);
21298542Smckusick		memset(cg_inosused(newcg), 0, (size_t)(mapsize));
2131558Srgrimes		j = fs->fs_ipg * c;
21441474Sjulian		for (i = 0; i < inostathead[c].il_numalloced; j++, i++) {
21541474Sjulian			switch (inoinfo(j)->ino_state) {
2161558Srgrimes
2171558Srgrimes			case USTATE:
2181558Srgrimes				break;
2191558Srgrimes
2201558Srgrimes			case DSTATE:
2211558Srgrimes			case DCLEAR:
2221558Srgrimes			case DFOUND:
2231558Srgrimes				newcg->cg_cs.cs_ndir++;
224102411Scharnier				/* FALLTHROUGH */
2251558Srgrimes
2261558Srgrimes			case FSTATE:
2271558Srgrimes			case FCLEAR:
2281558Srgrimes				newcg->cg_cs.cs_nifree--;
2291558Srgrimes				setbit(cg_inosused(newcg), i);
2301558Srgrimes				break;
2311558Srgrimes
2321558Srgrimes			default:
23374556Smckusick				if (j < (int)ROOTINO)
2341558Srgrimes					break;
23586514Siedowse				errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
23641474Sjulian				    inoinfo(j)->ino_state, j);
2371558Srgrimes			}
2381558Srgrimes		}
2391558Srgrimes		if (c == 0)
24074556Smckusick			for (i = 0; i < (int)ROOTINO; i++) {
2411558Srgrimes				setbit(cg_inosused(newcg), i);
2421558Srgrimes				newcg->cg_cs.cs_nifree--;
2431558Srgrimes			}
2441558Srgrimes		for (i = 0, d = dbase;
2451558Srgrimes		     d < dmax;
2461558Srgrimes		     d += fs->fs_frag, i += fs->fs_frag) {
2471558Srgrimes			frags = 0;
2481558Srgrimes			for (j = 0; j < fs->fs_frag; j++) {
2491558Srgrimes				if (testbmap(d + j))
2501558Srgrimes					continue;
2511558Srgrimes				setbit(cg_blksfree(newcg), i + j);
2521558Srgrimes				frags++;
2531558Srgrimes			}
2541558Srgrimes			if (frags == fs->fs_frag) {
2551558Srgrimes				newcg->cg_cs.cs_nbfree++;
2561558Srgrimes				if (fs->fs_contigsumsize > 0)
2571558Srgrimes					setbit(cg_clustersfree(newcg),
2581558Srgrimes					    i / fs->fs_frag);
2591558Srgrimes			} else if (frags > 0) {
2601558Srgrimes				newcg->cg_cs.cs_nffree += frags;
2611558Srgrimes				blk = blkmap(fs, cg_blksfree(newcg), i);
2621558Srgrimes				ffs_fragacct(fs, blk, newcg->cg_frsum, 1);
2631558Srgrimes			}
2641558Srgrimes		}
2651558Srgrimes		if (fs->fs_contigsumsize > 0) {
26623675Speter			int32_t *sump = cg_clustersum(newcg);
2671558Srgrimes			u_char *mapp = cg_clustersfree(newcg);
2681558Srgrimes			int map = *mapp++;
2691558Srgrimes			int bit = 1;
2701558Srgrimes			int run = 0;
2711558Srgrimes
2721558Srgrimes			for (i = 0; i < newcg->cg_nclusterblks; i++) {
2731558Srgrimes				if ((map & bit) != 0) {
2741558Srgrimes					run++;
2751558Srgrimes				} else if (run != 0) {
2761558Srgrimes					if (run > fs->fs_contigsumsize)
2771558Srgrimes						run = fs->fs_contigsumsize;
2781558Srgrimes					sump[run]++;
2791558Srgrimes					run = 0;
2801558Srgrimes				}
281103949Smike				if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
2821558Srgrimes					bit <<= 1;
2831558Srgrimes				} else {
2841558Srgrimes					map = *mapp++;
2851558Srgrimes					bit = 1;
2861558Srgrimes				}
2871558Srgrimes			}
2881558Srgrimes			if (run != 0) {
2891558Srgrimes				if (run > fs->fs_contigsumsize)
2901558Srgrimes					run = fs->fs_contigsumsize;
2911558Srgrimes				sump[run]++;
2921558Srgrimes			}
2931558Srgrimes		}
2941558Srgrimes		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
2951558Srgrimes		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
2961558Srgrimes		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
2971558Srgrimes		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
2981558Srgrimes		cs = &fs->fs_cs(fs, c);
29974556Smckusick		if (cursnapshot == 0 &&
30074556Smckusick		    memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0 &&
3011558Srgrimes		    dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
30223675Speter			memmove(cs, &newcg->cg_cs, sizeof *cs);
3031558Srgrimes			sbdirty();
3041558Srgrimes		}
30596483Sphk		if (rewritecg) {
30623675Speter			memmove(cg, newcg, (size_t)fs->fs_cgsize);
3071558Srgrimes			cgdirty();
3081558Srgrimes			continue;
3091558Srgrimes		}
31074556Smckusick		if (cursnapshot == 0 &&
31198542Smckusick		    memcmp(newcg, cg, basesize) != 0 &&
3121558Srgrimes		    dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
31323675Speter			memmove(cg, newcg, (size_t)basesize);
3141558Srgrimes			cgdirty();
3151558Srgrimes		}
31674556Smckusick		if (bkgrdflag != 0 || usedsoftdep || debug) {
31774556Smckusick			excessdirs = cg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
31874556Smckusick			if (excessdirs < 0) {
31974556Smckusick				pfatal("LOST %d DIRECTORIES\n", -excessdirs);
32074556Smckusick				excessdirs = 0;
32162668Smckusick			}
32274556Smckusick			if (excessdirs > 0)
32374556Smckusick				check_maps(cg_inosused(newcg), cg_inosused(cg),
32474556Smckusick				    inomapsize, cg->cg_cgx * fs->fs_ipg, "DIR",
32574556Smckusick				    freedirs, 0, excessdirs);
32674556Smckusick			check_maps(cg_inosused(newcg), cg_inosused(cg),
32774556Smckusick			    inomapsize, cg->cg_cgx * fs->fs_ipg, "FILE",
32874556Smckusick			    freefiles, excessdirs, fs->fs_ipg);
32974556Smckusick			check_maps(cg_blksfree(cg), cg_blksfree(newcg),
33074556Smckusick			    blkmapsize, cg->cg_cgx * fs->fs_fpg, "FRAG",
33174556Smckusick			    freeblks, 0, fs->fs_fpg);
33262668Smckusick		}
33374556Smckusick		if (cursnapshot == 0 &&
33474556Smckusick		    memcmp(cg_inosused(newcg), cg_inosused(cg), mapsize) != 0 &&
33534266Sjulian		    dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
33634266Sjulian			memmove(cg_inosused(cg), cg_inosused(newcg),
33734266Sjulian			      (size_t)mapsize);
33834266Sjulian			cgdirty();
33934266Sjulian		}
3401558Srgrimes	}
34174556Smckusick	if (cursnapshot == 0 &&
34298542Smckusick	    memcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal) != 0
34398542Smckusick	    && dofix(&idesc[0], "SUMMARY BLK COUNT(S) WRONG IN SUPERBLK")) {
34498542Smckusick		memmove(&fs->fs_cstotal, &cstotal, sizeof cstotal);
3451558Srgrimes		fs->fs_ronly = 0;
34641474Sjulian		fs->fs_fmod = 0;
3471558Srgrimes		sbdirty();
3481558Srgrimes	}
3491558Srgrimes}
35074556Smckusick
35174556Smckusickstatic void
35292839Simpcheck_maps(
35392839Simp	u_char *map1,	/* map of claimed allocations */
35492839Simp	u_char *map2,	/* map of determined allocations */
35592839Simp	int mapsize,	/* size of above two maps */
35692839Simp	int startvalue,	/* resource value for first element in map */
357100935Sphk	const char *name,	/* name of resource found in maps */
35892839Simp	int *opcode,	/* sysctl opcode to free resource */
35992839Simp	int skip,	/* number of entries to skip before starting to free */
36092839Simp	int limit)	/* limit on number of entries to free */
36174556Smckusick{
36274556Smckusick#	define BUFSIZE 16
36374556Smckusick	char buf[BUFSIZE];
36474556Smckusick	long i, j, k, l, m, n, size;
36574556Smckusick	int astart, aend, ustart, uend;
36692839Simp	void (*msg)(const char *fmt, ...);
36774556Smckusick
36875557Smckusick	if (bkgrdflag)
36975557Smckusick		msg = pfatal;
37075557Smckusick	else
37175557Smckusick		msg = pwarn;
37274556Smckusick	astart = ustart = aend = uend = -1;
37374556Smckusick	for (i = 0; i < mapsize; i++) {
37474556Smckusick		j = *map1++;
37574556Smckusick		k = *map2++;
37674556Smckusick		if (j == k)
37774556Smckusick			continue;
378103949Smike		for (m = 0, l = 1; m < CHAR_BIT; m++, l <<= 1) {
37974556Smckusick			if ((j & l) == (k & l))
38074556Smckusick				continue;
381103949Smike			n = startvalue + i * CHAR_BIT + m;
38274556Smckusick			if ((j & l) != 0) {
38374556Smckusick				if (astart == -1) {
38474556Smckusick					astart = aend = n;
38574556Smckusick					continue;
38674556Smckusick				}
38774556Smckusick				if (aend + 1 == n) {
38874556Smckusick					aend = n;
38974556Smckusick					continue;
39074556Smckusick				}
39174556Smckusick				if (astart == aend)
39275557Smckusick					(*msg)("ALLOCATED %s %d MARKED FREE\n",
39374556Smckusick					    name, astart);
39474556Smckusick				else
39575557Smckusick					(*msg)("%s %sS %d-%d MARKED FREE\n",
39674556Smckusick					    "ALLOCATED", name, astart, aend);
39774556Smckusick				astart = aend = n;
39874556Smckusick			} else {
39974556Smckusick				if (ustart == -1) {
40074556Smckusick					ustart = uend = n;
40174556Smckusick					continue;
40274556Smckusick				}
40374556Smckusick				if (uend + 1 == n) {
40474556Smckusick					uend = n;
40574556Smckusick					continue;
40674556Smckusick				}
40774556Smckusick				size = uend - ustart + 1;
40874556Smckusick				if (size <= skip) {
40974556Smckusick					skip -= size;
41074556Smckusick					ustart = uend = n;
41174556Smckusick					continue;
41274556Smckusick				}
41374556Smckusick				if (skip > 0) {
41474556Smckusick					ustart += skip;
41574556Smckusick					size -= skip;
41674556Smckusick					skip = 0;
41774556Smckusick				}
41874556Smckusick				if (size > limit)
41974556Smckusick					size = limit;
42074556Smckusick				if (debug && size == 1)
42174556Smckusick					pwarn("%s %s %d MARKED USED\n",
42274556Smckusick					    "UNALLOCATED", name, ustart);
42374556Smckusick				else if (debug)
42486514Siedowse					pwarn("%s %sS %d-%ld MARKED USED\n",
42574556Smckusick					    "UNALLOCATED", name, ustart,
42674556Smckusick					    ustart + size - 1);
42774556Smckusick				if (bkgrdflag != 0) {
42874556Smckusick					cmd.value = ustart;
42974556Smckusick					cmd.size = size;
43074556Smckusick					if (sysctl(opcode, MIBSIZE, 0, 0,
43174556Smckusick					    &cmd, sizeof cmd) == -1) {
43274556Smckusick						snprintf(buf, BUFSIZE,
43374556Smckusick						    "FREE %s", name);
43474556Smckusick						rwerror(buf, cmd.value);
43574556Smckusick					}
43674556Smckusick				}
43774556Smckusick				limit -= size;
43874556Smckusick				if (limit <= 0)
43974556Smckusick					return;
44074556Smckusick				ustart = uend = n;
44174556Smckusick			}
44274556Smckusick		}
44374556Smckusick	}
44486514Siedowse	if (astart != -1) {
44574556Smckusick		if (astart == aend)
44675557Smckusick			(*msg)("ALLOCATED %s %d MARKED FREE\n", name, astart);
44774556Smckusick		else
44875557Smckusick			(*msg)("ALLOCATED %sS %d-%d MARKED FREE\n",
44974556Smckusick			    name, astart, aend);
45086514Siedowse	}
45174556Smckusick	if (ustart != -1) {
45274556Smckusick		size = uend - ustart + 1;
45374556Smckusick		if (size <= skip)
45474556Smckusick			return;
45574556Smckusick		if (skip > 0) {
45674556Smckusick			ustart += skip;
45774556Smckusick			size -= skip;
45874556Smckusick		}
45974556Smckusick		if (size > limit)
46074556Smckusick			size = limit;
46174556Smckusick		if (debug) {
46274556Smckusick			if (size == 1)
46374556Smckusick				pwarn("UNALLOCATED %s %d MARKED USED\n",
46474556Smckusick				    name, ustart);
46574556Smckusick			else
46686514Siedowse				pwarn("UNALLOCATED %sS %d-%ld MARKED USED\n",
46774556Smckusick				    name, ustart, ustart + size - 1);
46874556Smckusick		}
46974556Smckusick		if (bkgrdflag != 0) {
47074556Smckusick			cmd.value = ustart;
47174556Smckusick			cmd.size = size;
47274556Smckusick			if (sysctl(opcode, MIBSIZE, 0, 0, &cmd,
47374556Smckusick			    sizeof cmd) == -1) {
47474556Smckusick				snprintf(buf, BUFSIZE, "FREE %s", name);
47574556Smckusick				rwerror(buf, cmd.value);
47674556Smckusick			}
47774556Smckusick		}
47874556Smckusick	}
47974556Smckusick}
480