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