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