xref: /freebsd/sbin/fsck_ffs/pass5.c (revision 9768746ba83efa02837c5b9c66348db6e900208f)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if 0
33 #ifndef lint
34 static const char sccsid[] = "@(#)pass5.c	8.9 (Berkeley) 4/28/95";
35 #endif /* not lint */
36 #endif
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/sysctl.h>
42 
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ffs/fs.h>
45 
46 #include <err.h>
47 #include <inttypes.h>
48 #include <limits.h>
49 #include <string.h>
50 #include <libufs.h>
51 
52 #include "fsck.h"
53 
54 static void check_maps(u_char *, u_char *, int, ufs2_daddr_t, const char *,
55 			int *, int, int, int);
56 static void clear_blocks(ufs2_daddr_t start, ufs2_daddr_t end);
57 
58 void
59 pass5(void)
60 {
61 	int c, i, j, blk, frags, basesize, mapsize;
62 	int inomapsize, blkmapsize;
63 	struct fs *fs = &sblock;
64 	ufs2_daddr_t d, dbase, dmax, start;
65 	int rewritecg = 0;
66 	ino_t inum;
67 	struct csum *cs;
68 	struct csum_total cstotal;
69 	struct inodesc idesc[3];
70 	char buf[MAXBSIZE];
71 	struct cg *cg, *newcg = (struct cg *)buf;
72 	struct bufarea *cgbp;
73 
74 	inoinfo(UFS_WINO)->ino_state = USTATE;
75 	memset(newcg, 0, (size_t)fs->fs_cgsize);
76 	newcg->cg_niblk = fs->fs_ipg;
77 	/* check to see if we are to add a cylinder group check hash */
78 	if ((ckhashadd & CK_CYLGRP) != 0)
79 		rewritecg = 1;
80 	if (cvtlevel >= 3) {
81 		if (fs->fs_maxcontig < 2 && fs->fs_contigsumsize > 0) {
82 			if (preen)
83 				pwarn("DELETING CLUSTERING MAPS\n");
84 			if (preen || reply("DELETE CLUSTERING MAPS")) {
85 				fs->fs_contigsumsize = 0;
86 				rewritecg = 1;
87 				sbdirty();
88 			}
89 		}
90 		if (fs->fs_maxcontig > 1) {
91 			const char *doit = NULL;
92 
93 			if (fs->fs_contigsumsize < 1) {
94 				doit = "CREAT";
95 			} else if (fs->fs_contigsumsize < fs->fs_maxcontig &&
96 				   fs->fs_contigsumsize < FS_MAXCONTIG) {
97 				doit = "EXPAND";
98 			}
99 			if (doit) {
100 				i = fs->fs_contigsumsize;
101 				fs->fs_contigsumsize =
102 				    MIN(fs->fs_maxcontig, FS_MAXCONTIG);
103 				if (CGSIZE(fs) > (u_int)fs->fs_bsize) {
104 					pwarn("CANNOT %s CLUSTER MAPS\n", doit);
105 					fs->fs_contigsumsize = i;
106 				} else if (preen ||
107 				    reply("CREATE CLUSTER MAPS")) {
108 					if (preen)
109 						pwarn("%sING CLUSTER MAPS\n",
110 						    doit);
111 					fs->fs_cgsize =
112 					    fragroundup(fs, CGSIZE(fs));
113 					rewritecg = 1;
114 					sbdirty();
115 				}
116 			}
117 		}
118 	}
119 	basesize = &newcg->cg_space[0] - (u_char *)(&newcg->cg_firstfield);
120 	if (sblock.fs_magic == FS_UFS2_MAGIC) {
121 		newcg->cg_iusedoff = basesize;
122 	} else {
123 		/*
124 		 * We reserve the space for the old rotation summary
125 		 * tables for the benefit of old kernels, but do not
126 		 * maintain them in modern kernels. In time, they can
127 		 * go away.
128 		 */
129 		newcg->cg_old_btotoff = basesize;
130 		newcg->cg_old_boff = newcg->cg_old_btotoff +
131 		    fs->fs_old_cpg * sizeof(int32_t);
132 		newcg->cg_iusedoff = newcg->cg_old_boff +
133 		    fs->fs_old_cpg * fs->fs_old_nrpos * sizeof(u_int16_t);
134 		memset(&newcg->cg_space[0], 0, newcg->cg_iusedoff - basesize);
135 	}
136 	inomapsize = howmany(fs->fs_ipg, CHAR_BIT);
137 	newcg->cg_freeoff = newcg->cg_iusedoff + inomapsize;
138 	blkmapsize = howmany(fs->fs_fpg, CHAR_BIT);
139 	newcg->cg_nextfreeoff = newcg->cg_freeoff + blkmapsize;
140 	if (fs->fs_contigsumsize > 0) {
141 		newcg->cg_clustersumoff = newcg->cg_nextfreeoff -
142 		    sizeof(u_int32_t);
143 		newcg->cg_clustersumoff =
144 		    roundup(newcg->cg_clustersumoff, sizeof(u_int32_t));
145 		newcg->cg_clusteroff = newcg->cg_clustersumoff +
146 		    (fs->fs_contigsumsize + 1) * sizeof(u_int32_t);
147 		newcg->cg_nextfreeoff = newcg->cg_clusteroff +
148 		    howmany(fragstoblks(fs, fs->fs_fpg), CHAR_BIT);
149 	}
150 	newcg->cg_magic = CG_MAGIC;
151 	mapsize = newcg->cg_nextfreeoff - newcg->cg_iusedoff;
152 	memset(&idesc[0], 0, sizeof idesc);
153 	for (i = 0; i < 3; i++)
154 		idesc[i].id_type = ADDR;
155 	memset(&cstotal, 0, sizeof(struct csum_total));
156 	dmax = blknum(fs, fs->fs_size + fs->fs_frag - 1);
157 	for (d = fs->fs_size; d < dmax; d++)
158 		setbmap(d);
159 	for (c = 0; c < fs->fs_ncg; c++) {
160 		if (got_siginfo) {
161 			printf("%s: phase 5: cyl group %d of %d (%d%%)\n",
162 			    cdevname, c, sblock.fs_ncg,
163 			    c * 100 / sblock.fs_ncg);
164 			got_siginfo = 0;
165 		}
166 		if (got_sigalarm) {
167 			setproctitle("%s p5 %d%%", cdevname,
168 			    c * 100 / sblock.fs_ncg);
169 			got_sigalarm = 0;
170 		}
171 		cgbp = cglookup(c);
172 		cg = cgbp->b_un.b_cg;
173 		if (!cg_chkmagic(cg))
174 			pfatal("CG %d: BAD MAGIC NUMBER\n", c);
175 		/*
176 		 * If we have a cylinder group check hash and are not adding
177 		 * it for the first time, verify that it is good.
178 		 */
179 		if ((fs->fs_metackhash & CK_CYLGRP) != 0 &&
180 		    (ckhashadd & CK_CYLGRP) == 0) {
181 			uint32_t ckhash, thishash;
182 
183 			ckhash = cg->cg_ckhash;
184 			cg->cg_ckhash = 0;
185 			thishash = calculate_crc32c(~0L, cg, fs->fs_cgsize);
186 			if (ckhash == thishash) {
187 				cg->cg_ckhash = ckhash;
188 			} else {
189 				pwarn("CG %d: BAD CHECK-HASH %#x vs %#x\n",
190 				    c, ckhash, thishash);
191 				cg->cg_ckhash = thishash;
192 				cgdirty(cgbp);
193 			}
194 		}
195 		newcg->cg_time = cg->cg_time;
196 		newcg->cg_old_time = cg->cg_old_time;
197 		newcg->cg_unrefs = cg->cg_unrefs;
198 		newcg->cg_ckhash = cg->cg_ckhash;
199 		newcg->cg_cgx = c;
200 		dbase = cgbase(fs, c);
201 		dmax = dbase + fs->fs_fpg;
202 		if (dmax > fs->fs_size)
203 			dmax = fs->fs_size;
204 		newcg->cg_ndblk = dmax - dbase;
205 		if (fs->fs_magic == FS_UFS1_MAGIC) {
206 			if (c == fs->fs_ncg - 1)
207 				newcg->cg_old_ncyl = howmany(newcg->cg_ndblk,
208 				    fs->fs_fpg / fs->fs_old_cpg);
209 			else
210 				newcg->cg_old_ncyl = fs->fs_old_cpg;
211 			newcg->cg_old_niblk = fs->fs_ipg;
212 			newcg->cg_niblk = 0;
213 		}
214 		if (fs->fs_contigsumsize > 0)
215 			newcg->cg_nclusterblks = newcg->cg_ndblk / fs->fs_frag;
216 		newcg->cg_cs.cs_ndir = 0;
217 		newcg->cg_cs.cs_nffree = 0;
218 		newcg->cg_cs.cs_nbfree = 0;
219 		newcg->cg_cs.cs_nifree = fs->fs_ipg;
220 		if (cg->cg_rotor >= 0 && cg->cg_rotor < newcg->cg_ndblk)
221 			newcg->cg_rotor = cg->cg_rotor;
222 		else
223 			newcg->cg_rotor = 0;
224 		if (cg->cg_frotor >= 0 && cg->cg_frotor < newcg->cg_ndblk)
225 			newcg->cg_frotor = cg->cg_frotor;
226 		else
227 			newcg->cg_frotor = 0;
228 		if (cg->cg_irotor >= 0 && cg->cg_irotor < fs->fs_ipg)
229 			newcg->cg_irotor = cg->cg_irotor;
230 		else
231 			newcg->cg_irotor = 0;
232 		if (fs->fs_magic == FS_UFS1_MAGIC) {
233 			newcg->cg_initediblk = 0;
234 		} else {
235 			if ((unsigned)cg->cg_initediblk > fs->fs_ipg)
236 				newcg->cg_initediblk = fs->fs_ipg;
237 			else
238 				newcg->cg_initediblk = cg->cg_initediblk;
239 		}
240 		memset(&newcg->cg_frsum[0], 0, sizeof newcg->cg_frsum);
241 		memset(cg_inosused(newcg), 0, (size_t)(mapsize));
242 		inum = fs->fs_ipg * c;
243 		for (i = 0; i < inostathead[c].il_numalloced; inum++, i++) {
244 			switch (inoinfo(inum)->ino_state) {
245 
246 			case USTATE:
247 				break;
248 
249 			case DSTATE:
250 			case DCLEAR:
251 			case DFOUND:
252 			case DZLINK:
253 				newcg->cg_cs.cs_ndir++;
254 				/* FALLTHROUGH */
255 
256 			case FSTATE:
257 			case FCLEAR:
258 			case FZLINK:
259 				newcg->cg_cs.cs_nifree--;
260 				setbit(cg_inosused(newcg), i);
261 				break;
262 
263 			default:
264 				if (inum < UFS_ROOTINO)
265 					break;
266 				errx(EEXIT, "BAD STATE %d FOR INODE I=%ju",
267 				    inoinfo(inum)->ino_state, (uintmax_t)inum);
268 			}
269 		}
270 		if (c == 0)
271 			for (i = 0; i < (int)UFS_ROOTINO; i++) {
272 				setbit(cg_inosused(newcg), i);
273 				newcg->cg_cs.cs_nifree--;
274 			}
275 		start = -1;
276 		for (i = 0, d = dbase;
277 		     d < dmax;
278 		     d += fs->fs_frag, i += fs->fs_frag) {
279 			frags = 0;
280 			for (j = 0; j < fs->fs_frag; j++) {
281 				if (testbmap(d + j)) {
282 					if ((Eflag || Zflag) && start != -1) {
283 						clear_blocks(start, d + j - 1);
284 						start = -1;
285 					}
286 					continue;
287 				}
288 				if (start == -1)
289 					start = d + j;
290 				setbit(cg_blksfree(newcg), i + j);
291 				frags++;
292 			}
293 			if (frags == fs->fs_frag) {
294 				newcg->cg_cs.cs_nbfree++;
295 				if (fs->fs_contigsumsize > 0)
296 					setbit(cg_clustersfree(newcg),
297 					    i / fs->fs_frag);
298 			} else if (frags > 0) {
299 				newcg->cg_cs.cs_nffree += frags;
300 				blk = blkmap(fs, cg_blksfree(newcg), i);
301 				ffs_fragacct(fs, blk, newcg->cg_frsum, 1);
302 			}
303 		}
304 		if ((Eflag || Zflag) && start != -1)
305 			clear_blocks(start, d - 1);
306 		if (fs->fs_contigsumsize > 0) {
307 			int32_t *sump = cg_clustersum(newcg);
308 			u_char *mapp = cg_clustersfree(newcg);
309 			int map = *mapp++;
310 			int bit = 1;
311 			int run = 0;
312 
313 			for (i = 0; i < newcg->cg_nclusterblks; i++) {
314 				if ((map & bit) != 0) {
315 					run++;
316 				} else if (run != 0) {
317 					if (run > fs->fs_contigsumsize)
318 						run = fs->fs_contigsumsize;
319 					sump[run]++;
320 					run = 0;
321 				}
322 				if ((i & (CHAR_BIT - 1)) != (CHAR_BIT - 1)) {
323 					bit <<= 1;
324 				} else {
325 					map = *mapp++;
326 					bit = 1;
327 				}
328 			}
329 			if (run != 0) {
330 				if (run > fs->fs_contigsumsize)
331 					run = fs->fs_contigsumsize;
332 				sump[run]++;
333 			}
334 		}
335 
336 		if (bkgrdflag != 0) {
337 			cstotal.cs_nffree += cg->cg_cs.cs_nffree;
338 			cstotal.cs_nbfree += cg->cg_cs.cs_nbfree;
339 			cstotal.cs_nifree += cg->cg_cs.cs_nifree;
340 			cstotal.cs_ndir += cg->cg_cs.cs_ndir;
341 		} else {
342 			cstotal.cs_nffree += newcg->cg_cs.cs_nffree;
343 			cstotal.cs_nbfree += newcg->cg_cs.cs_nbfree;
344 			cstotal.cs_nifree += newcg->cg_cs.cs_nifree;
345 			cstotal.cs_ndir += newcg->cg_cs.cs_ndir;
346 		}
347 		cs = &fs->fs_cs(fs, c);
348 		if (cursnapshot == 0 &&
349 		    memcmp(&newcg->cg_cs, cs, sizeof *cs) != 0 &&
350 		    dofix(&idesc[0], "FREE BLK COUNT(S) WRONG IN SUPERBLK")) {
351 			memmove(cs, &newcg->cg_cs, sizeof *cs);
352 			sbdirty();
353 		}
354 		if (rewritecg) {
355 			memmove(cg, newcg, (size_t)fs->fs_cgsize);
356 			cgdirty(cgbp);
357 			continue;
358 		}
359 		if (cursnapshot == 0 &&
360 		    memcmp(newcg, cg, basesize) != 0 &&
361 		    dofix(&idesc[2], "SUMMARY INFORMATION BAD")) {
362 			memmove(cg, newcg, (size_t)basesize);
363 			cgdirty(cgbp);
364 		}
365 		if (bkgrdflag != 0 || usedsoftdep || debug)
366 			update_maps(cg, newcg, bkgrdflag);
367 		if (cursnapshot == 0 &&
368 		    memcmp(cg_inosused(newcg), cg_inosused(cg), mapsize) != 0 &&
369 		    dofix(&idesc[1], "BLK(S) MISSING IN BIT MAPS")) {
370 			memmove(cg_inosused(cg), cg_inosused(newcg),
371 			      (size_t)mapsize);
372 			cgdirty(cgbp);
373 		}
374 	}
375 	if (cursnapshot == 0 &&
376 	    memcmp(&cstotal, &fs->fs_cstotal, sizeof cstotal) != 0
377 	    && dofix(&idesc[0], "SUMMARY BLK COUNT(S) WRONG IN SUPERBLK")) {
378 		if (debug) {
379 			printf("cstotal is currently: %jd dirs, %jd blks free, "
380 			    "%jd frags free, %jd inos free, %jd clusters\n",
381 			    (intmax_t)fs->fs_cstotal.cs_ndir,
382 			    (intmax_t)fs->fs_cstotal.cs_nbfree,
383 			    (intmax_t)fs->fs_cstotal.cs_nffree,
384 			    (intmax_t)fs->fs_cstotal.cs_nifree,
385 			    (intmax_t)fs->fs_cstotal.cs_numclusters);
386 			printf("cstotal ought to be:  %jd dirs, %jd blks free, "
387 			    "%jd frags free, %jd inos free, %jd clusters\n",
388 			    (intmax_t)cstotal.cs_ndir,
389 			    (intmax_t)cstotal.cs_nbfree,
390 			    (intmax_t)cstotal.cs_nffree,
391 			    (intmax_t)cstotal.cs_nifree,
392 			    (intmax_t)cstotal.cs_numclusters);
393 		}
394 		memmove(&fs->fs_cstotal, &cstotal, sizeof cstotal);
395 		fs->fs_ronly = 0;
396 		fs->fs_fmod = 0;
397 		sbdirty();
398 	}
399 
400 	/*
401 	 * When doing background fsck on a snapshot, figure out whether
402 	 * the superblock summary is inaccurate and correct it when
403 	 * necessary.
404 	 */
405 	if (cursnapshot != 0) {
406 		cmd.size = 1;
407 
408 		cmd.value = cstotal.cs_ndir - fs->fs_cstotal.cs_ndir;
409 		if (cmd.value != 0) {
410 			if (debug)
411 				printf("adjndir by %+" PRIi64 "\n", cmd.value);
412 			if (bkgrdsumadj == 0 || sysctl(adjndir, MIBSIZE, 0, 0,
413 			    &cmd, sizeof cmd) == -1)
414 				rwerror("ADJUST NUMBER OF DIRECTORIES", cmd.value);
415 		}
416 
417 		cmd.value = cstotal.cs_nbfree - fs->fs_cstotal.cs_nbfree;
418 		if (cmd.value != 0) {
419 			if (debug)
420 				printf("adjnbfree by %+" PRIi64 "\n", cmd.value);
421 			if (bkgrdsumadj == 0 || sysctl(adjnbfree, MIBSIZE, 0, 0,
422 			    &cmd, sizeof cmd) == -1)
423 				rwerror("ADJUST NUMBER OF FREE BLOCKS", cmd.value);
424 		}
425 
426 		cmd.value = cstotal.cs_nifree - fs->fs_cstotal.cs_nifree;
427 		if (cmd.value != 0) {
428 			if (debug)
429 				printf("adjnifree by %+" PRIi64 "\n", cmd.value);
430 			if (bkgrdsumadj == 0 || sysctl(adjnifree, MIBSIZE, 0, 0,
431 			    &cmd, sizeof cmd) == -1)
432 				rwerror("ADJUST NUMBER OF FREE INODES", cmd.value);
433 		}
434 
435 		cmd.value = cstotal.cs_nffree - fs->fs_cstotal.cs_nffree;
436 		if (cmd.value != 0) {
437 			if (debug)
438 				printf("adjnffree by %+" PRIi64 "\n", cmd.value);
439 			if (bkgrdsumadj == 0 || sysctl(adjnffree, MIBSIZE, 0, 0,
440 			    &cmd, sizeof cmd) == -1)
441 				rwerror("ADJUST NUMBER OF FREE FRAGS", cmd.value);
442 		}
443 
444 		cmd.value = cstotal.cs_numclusters - fs->fs_cstotal.cs_numclusters;
445 		if (cmd.value != 0) {
446 			if (debug)
447 				printf("adjnumclusters by %+" PRIi64 "\n", cmd.value);
448 			if (bkgrdsumadj == 0 || sysctl(adjnumclusters, MIBSIZE, 0, 0,
449 			    &cmd, sizeof cmd) == -1)
450 				rwerror("ADJUST NUMBER OF FREE CLUSTERS", cmd.value);
451 		}
452 	}
453 }
454 
455 /*
456  * Compare the original cylinder group inode and block bitmaps with the
457  * updated cylinder group inode and block bitmaps. Free inodes and blocks
458  * that have been added. Complain if any previously freed inodes blocks
459  * are now allocated.
460  */
461 void
462 update_maps(
463 	struct cg *oldcg,	/* cylinder group of claimed allocations */
464 	struct cg *newcg,	/* cylinder group of determined allocations */
465 	int usesysctl)		/* 1 => use sysctl interface to update maps */
466 {
467 	int inomapsize, excessdirs;
468 	struct fs *fs = &sblock;
469 
470 	inomapsize = howmany(fs->fs_ipg, CHAR_BIT);
471 	excessdirs = oldcg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
472 	if (excessdirs < 0) {
473 		pfatal("LOST %d DIRECTORIES\n", -excessdirs);
474 		excessdirs = 0;
475 	}
476 	if (excessdirs > 0)
477 		check_maps(cg_inosused(newcg), cg_inosused(oldcg), inomapsize,
478 		    oldcg->cg_cgx * (ufs2_daddr_t)fs->fs_ipg, "DIR", freedirs,
479 		    0, excessdirs, usesysctl);
480 	check_maps(cg_inosused(newcg), cg_inosused(oldcg), inomapsize,
481 	    oldcg->cg_cgx * (ufs2_daddr_t)fs->fs_ipg, "FILE", freefiles,
482 	    excessdirs, fs->fs_ipg, usesysctl);
483 	check_maps(cg_blksfree(oldcg), cg_blksfree(newcg),
484 	    howmany(fs->fs_fpg, CHAR_BIT),
485 	    oldcg->cg_cgx * (ufs2_daddr_t)fs->fs_fpg, "FRAG",
486 	    freeblks, 0, fs->fs_fpg, usesysctl);
487 }
488 
489 static void
490 check_maps(
491 	u_char *map1,	/* map of claimed allocations */
492 	u_char *map2,	/* map of determined allocations */
493 	int mapsize,	/* size of above two maps */
494 	ufs2_daddr_t startvalue, /* resource value for first element in map */
495 	const char *name,	/* name of resource found in maps */
496 	int *opcode,	/* sysctl opcode to free resource */
497 	int skip,	/* number of entries to skip before starting to free */
498 	int limit,	/* limit on number of entries to free */
499 	int usesysctl)	/* 1 => use sysctl interface to update maps */
500 {
501 #	define BUFSIZE 16
502 	char buf[BUFSIZE];
503 	long i, j, k, l, m, size;
504 	ufs2_daddr_t n, astart, aend, ustart, uend;
505 	void (*msg)(const char *fmt, ...);
506 
507 	if (usesysctl)
508 		msg = pfatal;
509 	else
510 		msg = pwarn;
511 	astart = ustart = aend = uend = -1;
512 	for (i = 0; i < mapsize; i++) {
513 		j = *map1++;
514 		k = *map2++;
515 		if (j == k)
516 			continue;
517 		for (m = 0, l = 1; m < CHAR_BIT; m++, l <<= 1) {
518 			if ((j & l) == (k & l))
519 				continue;
520 			n = startvalue + i * CHAR_BIT + m;
521 			if ((j & l) != 0) {
522 				if (astart == -1) {
523 					astart = aend = n;
524 					continue;
525 				}
526 				if (aend + 1 == n) {
527 					aend = n;
528 					continue;
529 				}
530 				if (astart == aend)
531 					(*msg)("ALLOCATED %s %" PRId64
532 					    " MARKED FREE\n",
533 					    name, astart);
534 				else
535 					(*msg)("%s %sS %" PRId64 "-%" PRId64
536 					    " MARKED FREE\n",
537 					    "ALLOCATED", name, astart, aend);
538 				astart = aend = n;
539 			} else {
540 				if (ustart == -1) {
541 					ustart = uend = n;
542 					continue;
543 				}
544 				if (uend + 1 == n) {
545 					uend = n;
546 					continue;
547 				}
548 				size = uend - ustart + 1;
549 				if (size <= skip) {
550 					skip -= size;
551 					ustart = uend = n;
552 					continue;
553 				}
554 				if (skip > 0) {
555 					ustart += skip;
556 					size -= skip;
557 					skip = 0;
558 				}
559 				if (size > limit)
560 					size = limit;
561 				if (debug && size == 1)
562 					pwarn("%s %s %" PRId64
563 					    " MARKED USED\n",
564 					    "UNALLOCATED", name, ustart);
565 				else if (debug)
566 					pwarn("%s %sS %" PRId64 "-%" PRId64
567 					    " MARKED USED\n",
568 					    "UNALLOCATED", name, ustart,
569 					    ustart + size - 1);
570 				if (usesysctl != 0) {
571 					cmd.value = ustart;
572 					cmd.size = size;
573 					if (sysctl(opcode, MIBSIZE, 0, 0,
574 					    &cmd, sizeof cmd) == -1) {
575 						snprintf(buf, BUFSIZE,
576 						    "FREE %s", name);
577 						rwerror(buf, cmd.value);
578 					}
579 				}
580 				limit -= size;
581 				if (limit <= 0)
582 					return;
583 				ustart = uend = n;
584 			}
585 		}
586 	}
587 	if (astart != -1) {
588 		if (astart == aend)
589 			(*msg)("ALLOCATED %s %" PRId64
590 			    " MARKED FREE\n", name, astart);
591 		else
592 			(*msg)("ALLOCATED %sS %" PRId64 "-%" PRId64
593 			    " MARKED FREE\n",
594 			    name, astart, aend);
595 	}
596 	if (ustart != -1) {
597 		size = uend - ustart + 1;
598 		if (size <= skip)
599 			return;
600 		if (skip > 0) {
601 			ustart += skip;
602 			size -= skip;
603 		}
604 		if (size > limit)
605 			size = limit;
606 		if (debug) {
607 			if (size == 1)
608 				pwarn("UNALLOCATED %s %" PRId64
609 				    " MARKED USED\n",
610 				    name, ustart);
611 			else
612 				pwarn("UNALLOCATED %sS %" PRId64 "-%" PRId64
613 				    " MARKED USED\n",
614 				    name, ustart, ustart + size - 1);
615 		}
616 		if (usesysctl != 0) {
617 			cmd.value = ustart;
618 			cmd.size = size;
619 			if (sysctl(opcode, MIBSIZE, 0, 0, &cmd,
620 			    sizeof cmd) == -1) {
621 				snprintf(buf, BUFSIZE, "FREE %s", name);
622 				rwerror(buf, cmd.value);
623 			}
624 		}
625 	}
626 }
627 
628 static void
629 clear_blocks(ufs2_daddr_t start, ufs2_daddr_t end)
630 {
631 
632 	if (debug)
633 		printf("Zero frags %jd to %jd\n", start, end);
634 	if (Zflag)
635 		blzero(fswritefd, fsbtodb(&sblock, start),
636 		    lfragtosize(&sblock, end - start + 1));
637 	if (Eflag)
638 		blerase(fswritefd, fsbtodb(&sblock, start),
639 		    lfragtosize(&sblock, end - start + 1));
640 }
641