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