pass5.c revision 100935
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
341558Srgrimes#ifndef lint
3541477Sjulian#if 0
3623675Speterstatic const char sccsid[] = "@(#)pass5.c	8.9 (Berkeley) 4/28/95";
3741477Sjulian#endif
3841477Sjulianstatic const char rcsid[] =
3950476Speter  "$FreeBSD: head/sbin/fsck_ffs/pass5.c 100935 2002-07-30 13:01:25Z phk $";
401558Srgrimes#endif /* not lint */
411558Srgrimes
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>
491558Srgrimes#include <string.h>
5023675Speter
511558Srgrimes#include "fsck.h"
521558Srgrimes
53100935Sphkstatic void check_maps(u_char *, u_char *, int, int, const char *, int *, int, int);
5474556Smckusick
557585Sbdevoid
5692839Simppass5(void)
571558Srgrimes{
5898542Smckusick	int c, i, j, blk, frags, basesize, mapsize;
5974556Smckusick	int inomapsize, blkmapsize;
6023675Speter	struct fs *fs = &sblock;
6123675Speter	struct cg *cg = &cgrp;
6298542Smckusick	ufs2_daddr_t d, dbase, dmax;
6398542Smckusick	int excessdirs, rewritecg = 0;
641558Srgrimes	struct csum *cs;
6598542Smckusick	struct csum_total cstotal;
661558Srgrimes	struct inodesc idesc[3];
671558Srgrimes	char buf[MAXBSIZE];
6892806Sobrien	struct cg *newcg = (struct cg *)buf;
691558Srgrimes
7041474Sjulian	inoinfo(WINO)->ino_state = USTATE;
7123675Speter	memset(newcg, 0, (size_t)fs->fs_cgsize);
721558Srgrimes	newcg->cg_niblk = fs->fs_ipg;
7323675Speter	if (cvtlevel >= 3) {
741558Srgrimes		if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) {
751558Srgrimes			if (preen)
761558Srgrimes				pwarn("DELETING CLUSTERING MAPS\n");
771558Srgrimes			if (preen || reply("DELETE CLUSTERING MAPS")) {
781558Srgrimes				fs->fs_contigsumsize = 0;
7996483Sphk				rewritecg = 1;
801558Srgrimes				sbdirty();
811558Srgrimes			}
821558Srgrimes		}
831558Srgrimes		if (fs->fs_maxcontig > 1) {
84100935Sphk			const char *doit = 0;
851558Srgrimes
861558Srgrimes			if (fs->fs_contigsumsize < 1) {
871558Srgrimes				doit = "CREAT";
881558Srgrimes			} else if (fs->fs_contigsumsize < fs->fs_maxcontig &&
891558Srgrimes				   fs->fs_contigsumsize < FS_MAXCONTIG) {
901558Srgrimes				doit = "EXPAND";
911558Srgrimes			}
921558Srgrimes			if (doit) {
931558Srgrimes				i = fs->fs_contigsumsize;
941558Srgrimes				fs->fs_contigsumsize =
951558Srgrimes				    MIN(fs->fs_maxcontig, FS_MAXCONTIG);
9674556Smckusick				if (CGSIZE(fs) > (u_int)fs->fs_bsize) {
971558Srgrimes					pwarn("CANNOT %s CLUSTER MAPS\n", doit);
981558Srgrimes					fs->fs_contigsumsize = i;
991558Srgrimes				} else if (preen ||
1001558Srgrimes				    reply("CREATE CLUSTER MAPS")) {
1011558Srgrimes					if (preen)
1021558Srgrimes						pwarn("%sING CLUSTER MAPS\n",
1031558Srgrimes						    doit);
1041558Srgrimes					fs->fs_cgsize =
1051558Srgrimes					    fragroundup(fs, CGSIZE(fs));
10696483Sphk					rewritecg = 1;
1071558Srgrimes					sbdirty();
1081558Srgrimes				}
1091558Srgrimes			}
1101558Srgrimes		}
1111558Srgrimes	}
11298542Smckusick	basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield);
11398542Smckusick	if (sblock.fs_magic == FS_UFS2_MAGIC) {
11498542Smckusick		newcg->cg_iusedoff = basesize;
11598542Smckusick	} else {
11698542Smckusick		/*
11798542Smckusick		 * We reserve the space for the old rotation summary
11898542Smckusick		 * tables for the benefit of old kernels, but do not
11998542Smckusick		 * maintain them in modern kernels. In time, they can
12098542Smckusick		 * go away.
12198542Smckusick		 */
12298542Smckusick		newcg->cg_old_btotoff = basesize;
12398542Smckusick		newcg->cg_old_boff = newcg->cg_old_btotoff +
12498542Smckusick		    fs->fs_old_cpg * sizeof(int32_t);
12598542Smckusick		newcg->cg_iusedoff = newcg->cg_old_boff +
12698542Smckusick		    fs->fs_old_cpg * fs->fs_old_nrpos * sizeof(u_int16_t);
12798542Smckusick		memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
1281558Srgrimes	}
12998542Smckusick	inomapsize = howmany(fs->fs_ipg, NBBY);
13098542Smckusick	newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
13198542Smckusick	blkmapsize = howmany(fs->fs_fpg, NBBY);
13298542Smckusick	newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
13398542Smckusick	if (fs->fs_contigsumsize > 0) {
13498542Smckusick		newcg->cg_clustersumoff = newcg->cg_nextfreeoff -
13598542Smckusick		    sizeof(u_int32_t);
13698542Smckusick		newcg->cg_clustersumoff =
13798542Smckusick		    roundup(newcg->cg_clustersumoff, sizeof(u_int32_t));
13898542Smckusick		newcg->cg_clusteroff = newcg->cg_clustersumoff +
13998542Smckusick		    (fs->fs_contigsumsize + 1) * sizeof(u_int32_t);
14098542Smckusick		newcg->cg_nextfreeoff = newcg->cg_clusteroff +
14198542Smckusick		    howmany(fragstoblks(fs, fs->fs_fpg), NBBY);
14298542Smckusick	}
14398542Smckusick	newcg->cg_magic = CG_MAGIC;
14498542Smckusick	mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
14523675Speter	memset(&idesc[0], 0, sizeof idesc);
14696483Sphk	for (i = 0; i < 3; i++)
1471558Srgrimes		idesc[i].id_type = ADDR;
14898542Smckusick	memset(&cstotal, 0, sizeof(struct csum_total));
14998542Smckusick	dmax = blknum(fs, fs->fs_size + fs->fs_frag - 1);
15098542Smckusick	for (d = fs->fs_size; d < dmax; d++)
15198542Smckusick		setbmap(d);
1521558Srgrimes	for (c = 0; c < fs->fs_ncg; c++) {
15370050Siedowse		if (got_siginfo) {
15470050Siedowse			printf("%s: phase 5: cyl group %d of %d (%d%%)\n",
15570050Siedowse			    cdevname, c, sblock.fs_ncg,
15670050Siedowse			    c * 100 / sblock.fs_ncg);
15770050Siedowse			got_siginfo = 0;
15870050Siedowse		}
1591558Srgrimes		getblk(&cgblk, cgtod(fs, c), fs->fs_cgsize);
1601558Srgrimes		if (!cg_chkmagic(cg))
1611558Srgrimes			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
16298542Smckusick		newcg->cg_time = cg->cg_time;
16398542Smckusick		newcg->cg_old_time = cg->cg_old_time;
16498542Smckusick		newcg->cg_cgx = c;
1651558Srgrimes		dbase = cgbase(fs, c);
1661558Srgrimes		dmax = dbase + fs->fs_fpg;
1671558Srgrimes		if (dmax > fs->fs_size)
1681558Srgrimes			dmax = fs->fs_size;
1691558Srgrimes		newcg->cg_ndblk = dmax - dbase;
17098542Smckusick		if (fs->fs_magic == FS_UFS1_MAGIC) {
17198542Smckusick			if (c == fs->fs_ncg - 1)
17298542Smckusick				newcg->cg_old_ncyl = howmany(newcg->cg_ndblk,
17398542Smckusick				    fs->fs_fpg / fs->fs_old_cpg);
17498542Smckusick			else
17598542Smckusick				newcg->cg_old_ncyl = fs->fs_old_cpg;
17698542Smckusick			newcg->cg_old_niblk = fs->fs_ipg;
17798542Smckusick			newcg->cg_niblk = 0;
17898542Smckusick		}
1791558Srgrimes		if (fs->fs_contigsumsize > 0)
1801558Srgrimes			newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag;
1811558Srgrimes		newcg->cg_cs.cs_ndir = 0;
1821558Srgrimes		newcg->cg_cs.cs_nffree = 0;
1831558Srgrimes		newcg->cg_cs.cs_nbfree = 0;
1841558Srgrimes		newcg->cg_cs.cs_nifree = fs->fs_ipg;
1851558Srgrimes		if (cg->cg_rotor < newcg->cg_ndblk)
1861558Srgrimes			newcg->cg_rotor = cg->cg_rotor;
1871558Srgrimes		else
1881558Srgrimes			newcg->cg_rotor = 0;
1891558Srgrimes		if (cg->cg_frotor < newcg->cg_ndblk)
1901558Srgrimes			newcg->cg_frotor = cg->cg_frotor;
1911558Srgrimes		else
1921558Srgrimes			newcg->cg_frotor = 0;
19375047Smckusick		if (cg->cg_irotor < fs->fs_ipg)
1941558Srgrimes			newcg->cg_irotor = cg->cg_irotor;
1951558Srgrimes		else
1961558Srgrimes			newcg->cg_irotor = 0;
19798542Smckusick		if (fs->fs_magic == FS_UFS1_MAGIC) {
19898542Smckusick			newcg->cg_initediblk = 0;
19998542Smckusick		} else {
20098542Smckusick			if ((unsigned)cg->cg_initediblk > fs->fs_ipg)
20198542Smckusick				newcg->cg_initediblk = fs->fs_ipg;
20298542Smckusick			else
20398542Smckusick				newcg->cg_initediblk = cg->cg_initediblk;
20498542Smckusick		}
20523675Speter		memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum);
20698542Smckusick		memset(cg_inosused(newcg), 0, (size_t)(mapsize));
2071558Srgrimes		j = fs->fs_ipg * c;
20841474Sjulian		for (i = 0; i < inostathead[c].il_numalloced; j++, i++) {
20941474Sjulian			switch (inoinfo(j)->ino_state) {
2101558Srgrimes
2111558Srgrimes			case USTATE:
2121558Srgrimes				break;
2131558Srgrimes
2141558Srgrimes			case DSTATE:
2151558Srgrimes			case DCLEAR:
2161558Srgrimes			case DFOUND:
2171558Srgrimes				newcg->cg_cs.cs_ndir++;
2181558Srgrimes				/* fall through */
2191558Srgrimes
2201558Srgrimes			case FSTATE:
2211558Srgrimes			case FCLEAR:
2221558Srgrimes				newcg->cg_cs.cs_nifree--;
2231558Srgrimes				setbit(cg_inosused(newcg), i);
2241558Srgrimes				break;
2251558Srgrimes
2261558Srgrimes			default:
22774556Smckusick				if (j < (int)ROOTINO)
2281558Srgrimes					break;
22986514Siedowse				errx(EEXIT, "BAD STATE %d FOR INODE I=%d",
23041474Sjulian				    inoinfo(j)->ino_state, j);
2311558Srgrimes			}
2321558Srgrimes		}
2331558Srgrimes		if (c == 0)
23474556Smckusick			for (i = 0; i < (int)ROOTINO; i++) {
2351558Srgrimes				setbit(cg_inosused(newcg), i);
2361558Srgrimes				newcg->cg_cs.cs_nifree--;
2371558Srgrimes			}
2381558Srgrimes		for (i = 0, d = dbase;
2391558Srgrimes		     d < dmax;
2401558Srgrimes		     d += fs->fs_frag, i += fs->fs_frag) {
2411558Srgrimes			frags = 0;
2421558Srgrimes			for (j = 0; j < fs->fs_frag; j++) {
2431558Srgrimes				if (testbmap(d + j))
2441558Srgrimes					continue;
2451558Srgrimes				setbit(cg_blksfree(newcg), i + j);
2461558Srgrimes				frags++;
2471558Srgrimes			}
2481558Srgrimes			if (frags == fs->fs_frag) {
2491558Srgrimes				newcg->cg_cs.cs_nbfree++;
2501558Srgrimes				if (fs->fs_contigsumsize > 0)
2511558Srgrimes					setbit(cg_clustersfree(newcg),
2521558Srgrimes					    i / fs->fs_frag);
2531558Srgrimes			} else if (frags > 0) {
2541558Srgrimes				newcg->cg_cs.cs_nffree += frags;
2551558Srgrimes				blk = blkmap(fs, cg_blksfree(newcg), i);
2561558Srgrimes				ffs_fragacct(fs, blk, newcg->cg_frsum, 1);
2571558Srgrimes			}
2581558Srgrimes		}
2591558Srgrimes		if (fs->fs_contigsumsize > 0) {
26023675Speter			int32_t *sump = cg_clustersum(newcg);
2611558Srgrimes			u_char *mapp = cg_clustersfree(newcg);
2621558Srgrimes			int map = *mapp++;
2631558Srgrimes			int bit = 1;
2641558Srgrimes			int run = 0;
2651558Srgrimes
2661558Srgrimes			for (i = 0; i < newcg->cg_nclusterblks; i++) {
2671558Srgrimes				if ((map & bit) != 0) {
2681558Srgrimes					run++;
2691558Srgrimes				} else if (run != 0) {
2701558Srgrimes					if (run > fs->fs_contigsumsize)
2711558Srgrimes						run = fs->fs_contigsumsize;
2721558Srgrimes					sump[run]++;
2731558Srgrimes					run = 0;
2741558Srgrimes				}
2751558Srgrimes				if ((i & (NBBY - 1)) != (NBBY - 1)) {
2761558Srgrimes					bit <<= 1;
2771558Srgrimes				} else {
2781558Srgrimes					map = *mapp++;
2791558Srgrimes					bit = 1;
2801558Srgrimes				}
2811558Srgrimes			}
2821558Srgrimes			if (run != 0) {
2831558Srgrimes				if (run > fs->fs_contigsumsize)
2841558Srgrimes					run = fs->fs_contigsumsize;
2851558Srgrimes				sump[run]++;
2861558Srgrimes			}
2871558Srgrimes		}
2881558Srgrimes		cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
2891558Srgrimes		cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
2901558Srgrimes		cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
2911558Srgrimes		cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
2921558Srgrimes		cs = &fs->fs_cs(fs, c);
29374556Smckusick		if (cursnapshot == 0 &&
29474556Smckusick		    memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0 &&
2951558Srgrimes		    dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
29623675Speter			memmove(cs, &newcg->cg_cs, sizeof *cs);
2971558Srgrimes			sbdirty();
2981558Srgrimes		}
29996483Sphk		if (rewritecg) {
30023675Speter			memmove(cg, newcg, (size_t)fs->fs_cgsize);
3011558Srgrimes			cgdirty();
3021558Srgrimes			continue;
3031558Srgrimes		}
30474556Smckusick		if (cursnapshot == 0 &&
30598542Smckusick		    memcmp(newcg, cg, basesize) != 0 &&
3061558Srgrimes		    dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
30723675Speter			memmove(cg, newcg, (size_t)basesize);
3081558Srgrimes			cgdirty();
3091558Srgrimes		}
31074556Smckusick		if (bkgrdflag != 0 || usedsoftdep || debug) {
31174556Smckusick			excessdirs = cg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
31274556Smckusick			if (excessdirs < 0) {
31374556Smckusick				pfatal("LOST %d DIRECTORIES\n", -excessdirs);
31474556Smckusick				excessdirs = 0;
31562668Smckusick			}
31674556Smckusick			if (excessdirs > 0)
31774556Smckusick				check_maps(cg_inosused(newcg), cg_inosused(cg),
31874556Smckusick				    inomapsize, cg->cg_cgx * fs->fs_ipg, "DIR",
31974556Smckusick				    freedirs, 0, excessdirs);
32074556Smckusick			check_maps(cg_inosused(newcg), cg_inosused(cg),
32174556Smckusick			    inomapsize, cg->cg_cgx * fs->fs_ipg, "FILE",
32274556Smckusick			    freefiles, excessdirs, fs->fs_ipg);
32374556Smckusick			check_maps(cg_blksfree(cg), cg_blksfree(newcg),
32474556Smckusick			    blkmapsize, cg->cg_cgx * fs->fs_fpg, "FRAG",
32574556Smckusick			    freeblks, 0, fs->fs_fpg);
32662668Smckusick		}
32774556Smckusick		if (cursnapshot == 0 &&
32874556Smckusick		    memcmp(cg_inosused(newcg), cg_inosused(cg), mapsize) != 0 &&
32934266Sjulian		    dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
33034266Sjulian			memmove(cg_inosused(cg), cg_inosused(newcg),
33134266Sjulian			      (size_t)mapsize);
33234266Sjulian			cgdirty();
33334266Sjulian		}
3341558Srgrimes	}
33574556Smckusick	if (cursnapshot == 0 &&
33698542Smckusick	    memcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal) != 0
33798542Smckusick	    && dofix(&idesc[0], "SUMMARY BLK COUNT(S) WRONG IN SUPERBLK")) {
33898542Smckusick		memmove(&fs->fs_cstotal, &cstotal, sizeof cstotal);
3391558Srgrimes		fs->fs_ronly = 0;
34041474Sjulian		fs->fs_fmod = 0;
3411558Srgrimes		sbdirty();
3421558Srgrimes	}
3431558Srgrimes}
34474556Smckusick
34574556Smckusickstatic void
34692839Simpcheck_maps(
34792839Simp	u_char *map1,	/* map of claimed allocations */
34892839Simp	u_char *map2,	/* map of determined allocations */
34992839Simp	int mapsize,	/* size of above two maps */
35092839Simp	int startvalue,	/* resource value for first element in map */
351100935Sphk	const char *name,	/* name of resource found in maps */
35292839Simp	int *opcode,	/* sysctl opcode to free resource */
35392839Simp	int skip,	/* number of entries to skip before starting to free */
35492839Simp	int limit)	/* limit on number of entries to free */
35574556Smckusick{
35674556Smckusick#	define BUFSIZE 16
35774556Smckusick	char buf[BUFSIZE];
35874556Smckusick	long i, j, k, l, m, n, size;
35974556Smckusick	int astart, aend, ustart, uend;
36092839Simp	void (*msg)(const char *fmt, ...);
36174556Smckusick
36275557Smckusick	if (bkgrdflag)
36375557Smckusick		msg = pfatal;
36475557Smckusick	else
36575557Smckusick		msg = pwarn;
36674556Smckusick	astart = ustart = aend = uend = -1;
36774556Smckusick	for (i = 0; i < mapsize; i++) {
36874556Smckusick		j = *map1++;
36974556Smckusick		k = *map2++;
37074556Smckusick		if (j == k)
37174556Smckusick			continue;
37274556Smckusick		for (m = 0, l = 1; m < NBBY; m++, l <<= 1) {
37374556Smckusick			if ((j & l) == (k & l))
37474556Smckusick				continue;
37574556Smckusick			n = startvalue + i * NBBY + m;
37674556Smckusick			if ((j & l) != 0) {
37774556Smckusick				if (astart == -1) {
37874556Smckusick					astart = aend = n;
37974556Smckusick					continue;
38074556Smckusick				}
38174556Smckusick				if (aend + 1 == n) {
38274556Smckusick					aend = n;
38374556Smckusick					continue;
38474556Smckusick				}
38598542Smckusick				returntosingle = 1;
38674556Smckusick				if (astart == aend)
38775557Smckusick					(*msg)("ALLOCATED %s %d MARKED FREE\n",
38874556Smckusick					    name, astart);
38974556Smckusick				else
39075557Smckusick					(*msg)("%s %sS %d-%d MARKED FREE\n",
39174556Smckusick					    "ALLOCATED", name, astart, aend);
39274556Smckusick				astart = aend = n;
39374556Smckusick			} else {
39474556Smckusick				if (ustart == -1) {
39574556Smckusick					ustart = uend = n;
39674556Smckusick					continue;
39774556Smckusick				}
39874556Smckusick				if (uend + 1 == n) {
39974556Smckusick					uend = n;
40074556Smckusick					continue;
40174556Smckusick				}
40274556Smckusick				size = uend - ustart + 1;
40374556Smckusick				if (size <= skip) {
40474556Smckusick					skip -= size;
40574556Smckusick					ustart = uend = n;
40674556Smckusick					continue;
40774556Smckusick				}
40874556Smckusick				if (skip > 0) {
40974556Smckusick					ustart += skip;
41074556Smckusick					size -= skip;
41174556Smckusick					skip = 0;
41274556Smckusick				}
41374556Smckusick				if (size > limit)
41474556Smckusick					size = limit;
41574556Smckusick				if (debug && size == 1)
41674556Smckusick					pwarn("%s %s %d MARKED USED\n",
41774556Smckusick					    "UNALLOCATED", name, ustart);
41874556Smckusick				else if (debug)
41986514Siedowse					pwarn("%s %sS %d-%ld MARKED USED\n",
42074556Smckusick					    "UNALLOCATED", name, ustart,
42174556Smckusick					    ustart + size - 1);
42298542Smckusick				returntosingle = 1;
42374556Smckusick				if (bkgrdflag != 0) {
42474556Smckusick					cmd.value = ustart;
42574556Smckusick					cmd.size = size;
42674556Smckusick					if (sysctl(opcode, MIBSIZE, 0, 0,
42774556Smckusick					    &cmd, sizeof cmd) == -1) {
42874556Smckusick						snprintf(buf, BUFSIZE,
42974556Smckusick						    "FREE %s", name);
43074556Smckusick						rwerror(buf, cmd.value);
43174556Smckusick					}
43274556Smckusick				}
43374556Smckusick				limit -= size;
43474556Smckusick				if (limit <= 0)
43574556Smckusick					return;
43674556Smckusick				ustart = uend = n;
43774556Smckusick			}
43874556Smckusick		}
43974556Smckusick	}
44086514Siedowse	if (astart != -1) {
44174556Smckusick		if (astart == aend)
44275557Smckusick			(*msg)("ALLOCATED %s %d MARKED FREE\n", name, astart);
44374556Smckusick		else
44475557Smckusick			(*msg)("ALLOCATED %sS %d-%d MARKED FREE\n",
44574556Smckusick			    name, astart, aend);
44686514Siedowse	}
44774556Smckusick	if (ustart != -1) {
44874556Smckusick		size = uend - ustart + 1;
44974556Smckusick		if (size <= skip)
45074556Smckusick			return;
45174556Smckusick		if (skip > 0) {
45274556Smckusick			ustart += skip;
45374556Smckusick			size -= skip;
45474556Smckusick		}
45574556Smckusick		if (size > limit)
45674556Smckusick			size = limit;
45774556Smckusick		if (debug) {
45874556Smckusick			if (size == 1)
45974556Smckusick				pwarn("UNALLOCATED %s %d MARKED USED\n",
46074556Smckusick				    name, ustart);
46174556Smckusick			else
46286514Siedowse				pwarn("UNALLOCATED %sS %d-%ld MARKED USED\n",
46374556Smckusick				    name, ustart, ustart + size - 1);
46474556Smckusick		}
46598542Smckusick		returntosingle = 1;
46674556Smckusick		if (bkgrdflag != 0) {
46774556Smckusick			cmd.value = ustart;
46874556Smckusick			cmd.size = size;
46974556Smckusick			if (sysctl(opcode, MIBSIZE, 0, 0, &cmd,
47074556Smckusick			    sizeof cmd) == -1) {
47174556Smckusick				snprintf(buf, BUFSIZE, "FREE %s", name);
47274556Smckusick				rwerror(buf, cmd.value);
47374556Smckusick			}
47474556Smckusick		}
47574556Smckusick	}
47674556Smckusick}
477