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