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