xref: /freebsd/sbin/fsck_ffs/pass5.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
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 = sizeof(*newcg);
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[1], 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",
415 				    cmd.value);
416 		}
417 
418 		cmd.value = cstotal.cs_nbfree - fs->fs_cstotal.cs_nbfree;
419 		if (cmd.value != 0) {
420 			if (debug)
421 				printf("adjnbfree by %+" PRIi64 "\n",
422 				    cmd.value);
423 			if (bkgrdsumadj == 0 || sysctl(adjnbfree, MIBSIZE, 0, 0,
424 			    &cmd, sizeof cmd) == -1)
425 				rwerror("ADJUST NUMBER OF FREE BLOCKS",
426 				    cmd.value);
427 		}
428 
429 		cmd.value = cstotal.cs_nifree - fs->fs_cstotal.cs_nifree;
430 		if (cmd.value != 0) {
431 			if (debug)
432 				printf("adjnifree by %+" PRIi64 "\n",
433 				    cmd.value);
434 			if (bkgrdsumadj == 0 || sysctl(adjnifree, MIBSIZE, 0, 0,
435 			    &cmd, sizeof cmd) == -1)
436 				rwerror("ADJUST NUMBER OF FREE INODES",
437 				    cmd.value);
438 		}
439 
440 		cmd.value = cstotal.cs_nffree - fs->fs_cstotal.cs_nffree;
441 		if (cmd.value != 0) {
442 			if (debug)
443 				printf("adjnffree by %+" PRIi64 "\n",
444 				    cmd.value);
445 			if (bkgrdsumadj == 0 || sysctl(adjnffree, MIBSIZE, 0, 0,
446 			    &cmd, sizeof cmd) == -1)
447 				rwerror("ADJUST NUMBER OF FREE FRAGS",
448 				    cmd.value);
449 		}
450 
451 		cmd.value = cstotal.cs_numclusters -
452 		    fs->fs_cstotal.cs_numclusters;
453 		if (cmd.value != 0) {
454 			if (debug)
455 				printf("adjnumclusters by %+" PRIi64 "\n",
456 				    cmd.value);
457 			if (bkgrdsumadj == 0 || sysctl(adjnumclusters, MIBSIZE,
458 			    0, 0, &cmd, sizeof cmd) == -1)
459 				rwerror("ADJUST NUMBER OF FREE CLUSTERS",
460 				    cmd.value);
461 		}
462 	}
463 }
464 
465 /*
466  * Compare the original cylinder group inode and block bitmaps with the
467  * updated cylinder group inode and block bitmaps. Free inodes and blocks
468  * that have been added. Complain if any previously freed inodes blocks
469  * are now allocated.
470  */
471 void
472 update_maps(
473 	struct cg *oldcg,	/* cylinder group of claimed allocations */
474 	struct cg *newcg,	/* cylinder group of determined allocations */
475 	int usesysctl)		/* 1 => use sysctl interface to update maps */
476 {
477 	int inomapsize, excessdirs;
478 	struct fs *fs = &sblock;
479 
480 	inomapsize = howmany(fs->fs_ipg, CHAR_BIT);
481 	excessdirs = oldcg->cg_cs.cs_ndir - newcg->cg_cs.cs_ndir;
482 	if (excessdirs < 0) {
483 		pfatal("LOST %d DIRECTORIES\n", -excessdirs);
484 		excessdirs = 0;
485 	}
486 	if (excessdirs > 0)
487 		check_maps(cg_inosused(newcg), cg_inosused(oldcg), inomapsize,
488 		    oldcg->cg_cgx * (ufs2_daddr_t)fs->fs_ipg, "DIR", freedirs,
489 		    0, excessdirs, usesysctl);
490 	check_maps(cg_inosused(newcg), cg_inosused(oldcg), inomapsize,
491 	    oldcg->cg_cgx * (ufs2_daddr_t)fs->fs_ipg, "FILE", freefiles,
492 	    excessdirs, fs->fs_ipg, usesysctl);
493 	check_maps(cg_blksfree(oldcg), cg_blksfree(newcg),
494 	    howmany(fs->fs_fpg, CHAR_BIT),
495 	    oldcg->cg_cgx * (ufs2_daddr_t)fs->fs_fpg, "FRAG",
496 	    freeblks, 0, fs->fs_fpg, usesysctl);
497 }
498 
499 static void
500 check_maps(
501 	u_char *map1,	/* map of claimed allocations */
502 	u_char *map2,	/* map of determined allocations */
503 	int mapsize,	/* size of above two maps */
504 	ufs2_daddr_t startvalue, /* resource value for first element in map */
505 	const char *name,	/* name of resource found in maps */
506 	int *opcode,	/* sysctl opcode to free resource */
507 	int skip,	/* number of entries to skip before starting to free */
508 	int limit,	/* limit on number of entries to free */
509 	int usesysctl)	/* 1 => use sysctl interface to update maps */
510 {
511 #	define BUFSIZE 16
512 	char buf[BUFSIZE];
513 	long i, j, k, l, m, size;
514 	ufs2_daddr_t n, astart, aend, ustart, uend;
515 	void (*msg)(const char *fmt, ...);
516 
517 	if (usesysctl)
518 		msg = pfatal;
519 	else
520 		msg = pwarn;
521 	astart = ustart = aend = uend = -1;
522 	for (i = 0; i < mapsize; i++) {
523 		j = *map1++;
524 		k = *map2++;
525 		if (j == k)
526 			continue;
527 		for (m = 0, l = 1; m < CHAR_BIT; m++, l <<= 1) {
528 			if ((j & l) == (k & l))
529 				continue;
530 			n = startvalue + i * CHAR_BIT + m;
531 			if ((j & l) != 0) {
532 				if (astart == -1) {
533 					astart = aend = n;
534 					continue;
535 				}
536 				if (aend + 1 == n) {
537 					aend = n;
538 					continue;
539 				}
540 				if (astart == aend)
541 					(*msg)("ALLOCATED %s %" PRId64
542 					    " MARKED FREE\n",
543 					    name, astart);
544 				else
545 					(*msg)("%s %sS %" PRId64 "-%" PRId64
546 					    " MARKED FREE\n",
547 					    "ALLOCATED", name, astart, aend);
548 				astart = aend = n;
549 			} else {
550 				if (ustart == -1) {
551 					ustart = uend = n;
552 					continue;
553 				}
554 				if (uend + 1 == n) {
555 					uend = n;
556 					continue;
557 				}
558 				size = uend - ustart + 1;
559 				if (size <= skip) {
560 					skip -= size;
561 					ustart = uend = n;
562 					continue;
563 				}
564 				if (skip > 0) {
565 					ustart += skip;
566 					size -= skip;
567 					skip = 0;
568 				}
569 				if (size > limit)
570 					size = limit;
571 				if (debug && size == 1)
572 					pwarn("%s %s %" PRId64
573 					    " MARKED USED\n",
574 					    "UNALLOCATED", name, ustart);
575 				else if (debug)
576 					pwarn("%s %sS %" PRId64 "-%" PRId64
577 					    " MARKED USED\n",
578 					    "UNALLOCATED", name, ustart,
579 					    ustart + size - 1);
580 				if (usesysctl != 0) {
581 					cmd.value = ustart;
582 					cmd.size = size;
583 					if (sysctl(opcode, MIBSIZE, 0, 0,
584 					    &cmd, sizeof cmd) == -1) {
585 						snprintf(buf, BUFSIZE,
586 						    "FREE %s", name);
587 						rwerror(buf, cmd.value);
588 					}
589 				}
590 				limit -= size;
591 				if (limit <= 0)
592 					return;
593 				ustart = uend = n;
594 			}
595 		}
596 	}
597 	if (astart != -1) {
598 		if (astart == aend)
599 			(*msg)("ALLOCATED %s %" PRId64
600 			    " MARKED FREE\n", name, astart);
601 		else
602 			(*msg)("ALLOCATED %sS %" PRId64 "-%" PRId64
603 			    " MARKED FREE\n",
604 			    name, astart, aend);
605 	}
606 	if (ustart != -1) {
607 		size = uend - ustart + 1;
608 		if (size <= skip)
609 			return;
610 		if (skip > 0) {
611 			ustart += skip;
612 			size -= skip;
613 		}
614 		if (size > limit)
615 			size = limit;
616 		if (debug) {
617 			if (size == 1)
618 				pwarn("UNALLOCATED %s %" PRId64
619 				    " MARKED USED\n",
620 				    name, ustart);
621 			else
622 				pwarn("UNALLOCATED %sS %" PRId64 "-%" PRId64
623 				    " MARKED USED\n",
624 				    name, ustart, ustart + size - 1);
625 		}
626 		if (usesysctl != 0) {
627 			cmd.value = ustart;
628 			cmd.size = size;
629 			if (sysctl(opcode, MIBSIZE, 0, 0, &cmd,
630 			    sizeof cmd) == -1) {
631 				snprintf(buf, BUFSIZE, "FREE %s", name);
632 				rwerror(buf, cmd.value);
633 			}
634 		}
635 	}
636 }
637 
638 static void
639 clear_blocks(ufs2_daddr_t start, ufs2_daddr_t end)
640 {
641 
642 	if (debug)
643 		printf("Zero frags %jd to %jd\n", start, end);
644 	if (Zflag)
645 		blzero(fswritefd, fsbtodb(&sblock, start),
646 		    lfragtosize(&sblock, end - start + 1));
647 	if (Eflag)
648 		blerase(fswritefd, fsbtodb(&sblock, start),
649 		    lfragtosize(&sblock, end - start + 1));
650 }
651