xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 29363fb4)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 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 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/limits.h>
36 
37 #ifndef _KERNEL
38 #include <stdio.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <time.h>
42 #include <sys/errno.h>
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ffs/fs.h>
45 
46 uint32_t calculate_crc32c(uint32_t, const void *, size_t);
47 uint32_t ffs_calc_sbhash(struct fs *);
48 struct malloc_type;
49 #define UFS_MALLOC(size, type, flags) malloc(size)
50 #define UFS_FREE(ptr, type) free(ptr)
51 #define maxphys MAXPHYS
52 
53 #else /* _KERNEL */
54 #include <sys/systm.h>
55 #include <sys/gsb_crc32.h>
56 #include <sys/lock.h>
57 #include <sys/malloc.h>
58 #include <sys/mount.h>
59 #include <sys/vnode.h>
60 #include <sys/bio.h>
61 #include <sys/buf.h>
62 #include <sys/ucred.h>
63 
64 #include <ufs/ufs/quota.h>
65 #include <ufs/ufs/inode.h>
66 #include <ufs/ufs/extattr.h>
67 #include <ufs/ufs/ufsmount.h>
68 #include <ufs/ufs/ufs_extern.h>
69 #include <ufs/ffs/ffs_extern.h>
70 #include <ufs/ffs/fs.h>
71 
72 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
73 #define UFS_FREE(ptr, type) free(ptr, type)
74 
75 #endif /* _KERNEL */
76 
77 /*
78  * Verify an inode check-hash.
79  */
80 int
81 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
82 {
83 	uint32_t ckhash, save_ckhash;
84 
85 	/*
86 	 * Return success if unallocated or we are not doing inode check-hash.
87 	 */
88 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
89 		return (0);
90 	/*
91 	 * Exclude di_ckhash from the crc32 calculation, e.g., always use
92 	 * a check-hash value of zero when calculating the check-hash.
93 	 */
94 	save_ckhash = dip->di_ckhash;
95 	dip->di_ckhash = 0;
96 	ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
97 	dip->di_ckhash = save_ckhash;
98 	if (save_ckhash == ckhash)
99 		return (0);
100 	return (EINVAL);
101 }
102 
103 /*
104  * Update an inode check-hash.
105  */
106 void
107 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
108 {
109 
110 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
111 		return;
112 	/*
113 	 * Exclude old di_ckhash from the crc32 calculation, e.g., always use
114 	 * a check-hash value of zero when calculating the new check-hash.
115 	 */
116 	dip->di_ckhash = 0;
117 	dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
118 }
119 
120 /*
121  * These are the low-level functions that actually read and write
122  * the superblock and its associated data.
123  */
124 static off_t sblock_try[] = SBLOCKSEARCH;
125 static int readsuper(void *, struct fs **, off_t, int,
126 	int (*)(void *, off_t, void **, int));
127 static int validate_sblock(struct fs *, int);
128 
129 /*
130  * Read a superblock from the devfd device.
131  *
132  * If an alternate superblock is specified, it is read. Otherwise the
133  * set of locations given in the SBLOCKSEARCH list is searched for a
134  * superblock. Memory is allocated for the superblock by the readfunc and
135  * is returned. If filltype is non-NULL, additional memory is allocated
136  * of type filltype and filled in with the superblock summary information.
137  * All memory is freed when any error is returned.
138  *
139  * If a superblock is found, zero is returned. Otherwise one of the
140  * following error values is returned:
141  *     EIO: non-existent or truncated superblock.
142  *     EIO: error reading summary information.
143  *     ENOENT: no usable known superblock found.
144  *     EILSEQ: filesystem with wrong byte order found.
145  *     ENOMEM: failed to allocate space for the superblock.
146  *     EINVAL: The previous newfs operation on this volume did not complete.
147  *         The administrator must complete newfs before using this volume.
148  */
149 int
150 ffs_sbget(void *devfd, struct fs **fsp, off_t sblock, int flags,
151     struct malloc_type *filltype,
152     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
153 {
154 	struct fs *fs;
155 	struct fs_summary_info *fs_si;
156 	int i, error;
157 	uint64_t size, blks;
158 	uint8_t *space;
159 	int32_t *lp;
160 	char *buf;
161 
162 	fs = NULL;
163 	*fsp = NULL;
164 	if (sblock != UFS_STDSB) {
165 		if ((error = readsuper(devfd, &fs, sblock,
166 		    flags | UFS_ALTSBLK, readfunc)) != 0) {
167 			if (fs != NULL)
168 				UFS_FREE(fs, filltype);
169 			return (error);
170 		}
171 	} else {
172 		for (i = 0; sblock_try[i] != -1; i++) {
173 			if ((error = readsuper(devfd, &fs, sblock_try[i],
174 			     flags, readfunc)) == 0) {
175 				if ((flags & UFS_NOCSUM) != 0) {
176 					*fsp = fs;
177 					return (0);
178 				}
179 				break;
180 			}
181 			if (fs != NULL) {
182 				UFS_FREE(fs, filltype);
183 				fs = NULL;
184 			}
185 			if (error == ENOENT)
186 				continue;
187 			return (error);
188 		}
189 		if (sblock_try[i] == -1)
190 			return (ENOENT);
191 	}
192 	/*
193 	 * Read in the superblock summary information.
194 	 */
195 	size = fs->fs_cssize;
196 	blks = howmany(size, fs->fs_fsize);
197 	if (fs->fs_contigsumsize > 0)
198 		size += fs->fs_ncg * sizeof(int32_t);
199 	size += fs->fs_ncg * sizeof(uint8_t);
200 	if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_NOWAIT)) == NULL) {
201 		UFS_FREE(fs, filltype);
202 		return (ENOMEM);
203 	}
204 	bzero(fs_si, sizeof(*fs_si));
205 	fs->fs_si = fs_si;
206 	if ((space = UFS_MALLOC(size, filltype, M_NOWAIT)) == NULL) {
207 		UFS_FREE(fs->fs_si, filltype);
208 		UFS_FREE(fs, filltype);
209 		return (ENOMEM);
210 	}
211 	fs->fs_csp = (struct csum *)space;
212 	for (i = 0; i < blks; i += fs->fs_frag) {
213 		size = fs->fs_bsize;
214 		if (i + fs->fs_frag > blks)
215 			size = (blks - i) * fs->fs_fsize;
216 		buf = NULL;
217 		error = (*readfunc)(devfd,
218 		    dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
219 		if (error) {
220 			if (buf != NULL)
221 				UFS_FREE(buf, filltype);
222 			UFS_FREE(fs->fs_csp, filltype);
223 			UFS_FREE(fs->fs_si, filltype);
224 			UFS_FREE(fs, filltype);
225 			return (error);
226 		}
227 		memcpy(space, buf, size);
228 		UFS_FREE(buf, filltype);
229 		space += size;
230 	}
231 	if (fs->fs_contigsumsize > 0) {
232 		fs->fs_maxcluster = lp = (int32_t *)space;
233 		for (i = 0; i < fs->fs_ncg; i++)
234 			*lp++ = fs->fs_contigsumsize;
235 		space = (uint8_t *)lp;
236 	}
237 	size = fs->fs_ncg * sizeof(uint8_t);
238 	fs->fs_contigdirs = (uint8_t *)space;
239 	bzero(fs->fs_contigdirs, size);
240 	*fsp = fs;
241 	return (0);
242 }
243 
244 /*
245  * Try to read a superblock from the location specified by sblockloc.
246  * Return zero on success or an errno on failure.
247  */
248 static int
249 readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int flags,
250     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
251 {
252 	struct fs *fs;
253 	int error, res;
254 	uint32_t ckhash;
255 
256 	error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
257 	if (error != 0)
258 		return (error);
259 	fs = *fsp;
260 	if (fs->fs_magic == FS_BAD_MAGIC)
261 		return (EINVAL);
262 	/*
263 	 * For UFS1 with a 65536 block size, the first backup superblock
264 	 * is at the same location as the UFS2 superblock. Since SBLOCK_UFS2
265 	 * is the first location checked, the first backup is the superblock
266 	 * that will be accessed. Here we fail the lookup so that we can
267 	 * retry with the correct location for the UFS1 superblock.
268 	 */
269 	if (fs->fs_magic == FS_UFS1_MAGIC && (flags & UFS_ALTSBLK) == 0 &&
270 	    fs->fs_bsize == SBLOCK_UFS2 && sblockloc == SBLOCK_UFS2)
271 		return (ENOENT);
272 	if ((error = validate_sblock(fs, flags)) > 0)
273 		return (error);
274 	/*
275 	 * If the filesystem has been run on a kernel without
276 	 * metadata check hashes, disable them.
277 	 */
278 	if ((fs->fs_flags & FS_METACKHASH) == 0)
279 		fs->fs_metackhash = 0;
280 	/*
281 	 * Clear any check-hashes that are not maintained
282 	 * by this kernel. Also clear any unsupported flags.
283 	 */
284 	fs->fs_metackhash &= CK_SUPPORTED;
285 	fs->fs_flags &= FS_SUPPORTED;
286 	if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) {
287 		if ((flags & (UFS_NOMSG | UFS_NOHASHFAIL)) ==
288 		    (UFS_NOMSG | UFS_NOHASHFAIL))
289 			return (0);
290 		if ((flags & UFS_NOMSG) != 0)
291 			return (EINTEGRITY);
292 #ifdef _KERNEL
293 		res = uprintf("Superblock check-hash failed: recorded "
294 		    "check-hash 0x%x != computed check-hash 0x%x%s\n",
295 		    fs->fs_ckhash, ckhash,
296 		    (flags & UFS_NOHASHFAIL) != 0 ? " (Ignored)" : "");
297 #else
298 		res = 0;
299 #endif
300 		/*
301 		 * Print check-hash failure if no controlling terminal
302 		 * in kernel or always if in user-mode (libufs).
303 		 */
304 		if (res == 0)
305 			printf("Superblock check-hash failed: recorded "
306 			    "check-hash 0x%x != computed check-hash "
307 			    "0x%x%s\n", fs->fs_ckhash, ckhash,
308 			    (flags & UFS_NOHASHFAIL) ? " (Ignored)" : "");
309 		if ((flags & UFS_NOHASHFAIL) != 0)
310 			return (0);
311 		return (EINTEGRITY);
312 	}
313 	/* Have to set for old filesystems that predate this field */
314 	fs->fs_sblockactualloc = sblockloc;
315 	/* Not yet any summary information */
316 	fs->fs_si = NULL;
317 	return (0);
318 }
319 
320 /*
321  * Verify the filesystem values.
322  */
323 #define ILOG2(num)	(fls(num) - 1)
324 #ifdef STANDALONE_SMALL
325 #define MPRINT(...)	do { } while (0)
326 #else
327 #define MPRINT(...)	if (prtmsg) printf(__VA_ARGS__)
328 #endif
329 #define FCHK(lhs, op, rhs, fmt)						\
330 	if (lhs op rhs) {						\
331 		MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s ("	\
332 		    #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,	\
333 		    #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs);	\
334 		if (error < 0)						\
335 			return (ENOENT);				\
336 		if (error == 0)						\
337 			error = ENOENT;					\
338 	}
339 #define WCHK(lhs, op, rhs, fmt)						\
340 	if (lhs op rhs) {						\
341 		MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s ("	\
342 		    #fmt ")%s\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,\
343 		    #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs, wmsg);\
344 		if (error == 0)						\
345 			error = warnerr;				\
346 		if (warnerr == 0)					\
347 			lhs = rhs;					\
348 	}
349 #define FCHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt)			\
350 	if (lhs1 op1 rhs1 && lhs2 op2 rhs2) {				\
351 		MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s ("	\
352 		    #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n",	\
353 		    fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1,	\
354 		    (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2,	\
355 		    (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2);	\
356 		if (error < 0)						\
357 			return (ENOENT);				\
358 		if (error == 0)						\
359 			error = ENOENT;					\
360 	}
361 
362 static int
363 validate_sblock(struct fs *fs, int flags)
364 {
365 	uint64_t i, sectorsize;
366 	uint64_t maxfilesize, sizepb;
367 	int error, prtmsg, warnerr;
368 	char *wmsg;
369 
370 	error = 0;
371 	sectorsize = dbtob(1);
372 	prtmsg = ((flags & UFS_NOMSG) == 0);
373 	warnerr = (flags & UFS_NOWARNFAIL) == UFS_NOWARNFAIL ? 0 : ENOENT;
374 	wmsg = warnerr ? "" : " (Ignored)";
375 	/*
376 	 * Check for endian mismatch between machine and filesystem.
377 	 */
378 	if (((fs->fs_magic != FS_UFS2_MAGIC) &&
379 	    (bswap32(fs->fs_magic) == FS_UFS2_MAGIC)) ||
380 	    ((fs->fs_magic != FS_UFS1_MAGIC) &&
381 	    (bswap32(fs->fs_magic) == FS_UFS1_MAGIC))) {
382 		MPRINT("UFS superblock failed due to endian mismatch "
383 		    "between machine and filesystem\n");
384 		return(EILSEQ);
385 	}
386 	/*
387 	 * If just validating for recovery, then do just the minimal
388 	 * checks needed for the superblock fields needed to find
389 	 * alternate superblocks.
390 	 */
391 	if ((flags & UFS_FSRONLY) == UFS_FSRONLY &&
392 	    (fs->fs_magic == FS_UFS1_MAGIC || fs->fs_magic == FS_UFS2_MAGIC)) {
393 		error = -1; /* fail on first error */
394 		if (fs->fs_magic == FS_UFS2_MAGIC) {
395 			FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
396 		} else if (fs->fs_magic == FS_UFS1_MAGIC) {
397 			FCHK(fs->fs_sblockloc, <, 0, %jd);
398 			FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
399 			FCHK(fs->fs_old_ncyl, !=, fs->fs_ncg, %jd);
400 		}
401 		FCHK(fs->fs_frag, <, 1, %jd);
402 		FCHK(fs->fs_frag, >, MAXFRAG, %jd);
403 		FCHK(fs->fs_bsize, <, MINBSIZE, %jd);
404 		FCHK(fs->fs_bsize, >, MAXBSIZE, %jd);
405 		FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE),
406 		    %jd);
407 		FCHK(fs->fs_fsize, <, sectorsize, %jd);
408 		FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd);
409 		FCHK(powerof2(fs->fs_fsize), ==, 0, %jd);
410 		FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd);
411 		FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd);
412 		FCHK(fs->fs_sbsize % sectorsize, !=, 0, %jd);
413 		FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd);
414 		FCHK(fs->fs_ncg, <, 1, %jd);
415 		FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd);
416 		FCHK(fs->fs_old_cgoffset, <, 0, %jd);
417 		FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd);
418 		FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg,
419 		    %jd);
420 		FCHK(fs->fs_sblkno, !=, roundup(
421 		    howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize),
422 		    fs->fs_frag), %jd);
423 		FCHK(CGSIZE(fs), >, fs->fs_bsize, %jd);
424 		/* Only need to validate these if reading in csum data */
425 		if ((flags & UFS_NOCSUM) != 0)
426 			return (error);
427 		FCHK((uint64_t)fs->fs_ipg * fs->fs_ncg, >,
428 		    (((int64_t)(1)) << 32) - INOPB(fs), %jd);
429 		FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd);
430 		FCHK(fs->fs_cstotal.cs_nifree, >,
431 		    (uint64_t)fs->fs_ipg * fs->fs_ncg, %jd);
432 		FCHK(fs->fs_cstotal.cs_ndir, >,
433 		    ((uint64_t)fs->fs_ipg * fs->fs_ncg) -
434 		    fs->fs_cstotal.cs_nifree, %jd);
435 		FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd);
436 		FCHK(fs->fs_size, <=, ((int64_t)fs->fs_ncg - 1) * fs->fs_fpg,
437 		    %jd);
438 		FCHK(fs->fs_size, >, (int64_t)fs->fs_ncg * fs->fs_fpg, %jd);
439 		FCHK(fs->fs_csaddr, <, 0, %jd);
440 		FCHK(fs->fs_cssize, !=,
441 		    fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd);
442 		FCHK(fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize), >,
443 		    fs->fs_size, %jd);
444 		FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)),
445 		    %jd);
446 		FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize,
447 		    fs->fs_fsize)), >, dtog(fs, fs->fs_csaddr), %jd);
448 		return (error);
449 	}
450 	if (fs->fs_magic == FS_UFS2_MAGIC) {
451 		if ((flags & UFS_ALTSBLK) == 0)
452 			FCHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2,
453 			    fs->fs_sblockactualloc, !=, 0, %jd);
454 		FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
455 		FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
456 			sizeof(ufs2_daddr_t)), %jd);
457 		FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t),
458 		    %jd);
459 		FCHK(fs->fs_inopb, !=,
460 		    fs->fs_bsize / sizeof(struct ufs2_dinode), %jd);
461 	} else if (fs->fs_magic == FS_UFS1_MAGIC) {
462 		if ((flags & UFS_ALTSBLK) == 0)
463 			FCHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd);
464 		FCHK(fs->fs_sblockloc, <, 0, %jd);
465 		FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
466 		FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t),
467 		    %jd);
468 		FCHK(fs->fs_inopb, !=,
469 		    fs->fs_bsize / sizeof(struct ufs1_dinode), %jd);
470 		FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
471 			sizeof(ufs1_daddr_t)), %jd);
472 		WCHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd);
473 		WCHK(fs->fs_old_rotdelay, !=, 0, %jd);
474 		WCHK(fs->fs_old_rps, !=, 60, %jd);
475 		WCHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd);
476 		FCHK(fs->fs_old_cpg, !=, 1, %jd);
477 		WCHK(fs->fs_old_interleave, !=, 1, %jd);
478 		WCHK(fs->fs_old_trackskew, !=, 0, %jd);
479 		WCHK(fs->fs_old_cpc, !=, 0, %jd);
480 		WCHK(fs->fs_old_postblformat, !=, 1, %jd);
481 		FCHK(fs->fs_old_nrpos, !=, 1, %jd);
482 		WCHK(fs->fs_old_spc, !=, fs->fs_fpg * fs->fs_old_nspf, %jd);
483 		WCHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd);
484 		WCHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd);
485 		FCHK(fs->fs_old_ncyl, !=, fs->fs_ncg, %jd);
486 	} else {
487 		/* Bad magic number, so assume not a superblock */
488 		return (ENOENT);
489 	}
490 	FCHK(fs->fs_bsize, <, MINBSIZE, %jd);
491 	FCHK(fs->fs_bsize, >, MAXBSIZE, %jd);
492 	FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd);
493 	FCHK(powerof2(fs->fs_bsize), ==, 0, %jd);
494 	FCHK(fs->fs_frag, <, 1, %jd);
495 	FCHK(fs->fs_frag, >, MAXFRAG, %jd);
496 	FCHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd);
497 	FCHK(fs->fs_fsize, <, sectorsize, %jd);
498 	FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd);
499 	FCHK(powerof2(fs->fs_fsize), ==, 0, %jd);
500 	FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd);
501 	FCHK(fs->fs_ncg, <, 1, %jd);
502 	FCHK(fs->fs_ipg, <, fs->fs_inopb, %jd);
503 	FCHK((uint64_t)fs->fs_ipg * fs->fs_ncg, >,
504 	    (((int64_t)(1)) << 32) - INOPB(fs), %jd);
505 	FCHK(fs->fs_cstotal.cs_nifree, <, 0, %jd);
506 	FCHK(fs->fs_cstotal.cs_nifree, >, (uint64_t)fs->fs_ipg * fs->fs_ncg,
507 	    %jd);
508 	FCHK(fs->fs_cstotal.cs_ndir, <, 0, %jd);
509 	FCHK(fs->fs_cstotal.cs_ndir, >,
510 	    ((uint64_t)fs->fs_ipg * fs->fs_ncg) - fs->fs_cstotal.cs_nifree,
511 	    %jd);
512 	FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd);
513 	FCHK(fs->fs_sbsize, <, (signed)sizeof(struct fs), %jd);
514 	/* fix for misconfigured filesystems */
515 	if (fs->fs_maxbsize == 0)
516 		fs->fs_maxbsize = fs->fs_bsize;
517 	FCHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd);
518 	FCHK(powerof2(fs->fs_maxbsize), ==, 0, %jd);
519 	FCHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd);
520 	FCHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx);
521 	FCHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx);
522 	FCHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx);
523 	FCHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx);
524 	FCHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd);
525 	FCHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd);
526 	FCHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd);
527 	FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd);
528 	FCHK(fs->fs_old_cgoffset, <, 0, %jd);
529 	FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd);
530 	FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, %jd);
531 	FCHK(CGSIZE(fs), >, fs->fs_bsize, %jd);
532 	/*
533 	 * If anything has failed up to this point, it is usafe to proceed
534 	 * as checks below may divide by zero or make other fatal calculations.
535 	 * So if we have any errors at this point, give up.
536 	 */
537 	if (error)
538 		return (error);
539 	FCHK(fs->fs_sbsize % sectorsize, !=, 0, %jd);
540 	FCHK(fs->fs_ipg % fs->fs_inopb, !=, 0, %jd);
541 	FCHK(fs->fs_sblkno, !=, roundup(
542 	    howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize),
543 	    fs->fs_frag), %jd);
544 	FCHK(fs->fs_cblkno, !=, fs->fs_sblkno +
545 	    roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd);
546 	FCHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd);
547 	FCHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd);
548 	FCHK(fs->fs_cgsize, >, fs->fs_bsize, %jd);
549 	FCHK(fs->fs_cgsize, <, fs->fs_fsize, %jd);
550 	FCHK(fs->fs_cgsize % fs->fs_fsize, !=, 0, %jd);
551 	/*
552 	 * This test is valid, however older versions of growfs failed
553 	 * to correctly update fs_dsize so will fail this test. Thus we
554 	 * exclude it from the requirements.
555 	 */
556 #ifdef notdef
557 	WCHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno -
558 		fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) -
559 		howmany(fs->fs_cssize, fs->fs_fsize), %jd);
560 #endif
561 	WCHK(fs->fs_metaspace, <, 0, %jd);
562 	WCHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd);
563 	WCHK(fs->fs_minfree, >, 99, %jd%%);
564 	maxfilesize = fs->fs_bsize * UFS_NDADDR - 1;
565 	for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) {
566 		sizepb *= NINDIR(fs);
567 		maxfilesize += sizepb;
568 	}
569 	WCHK(fs->fs_maxfilesize, !=, maxfilesize, %jd);
570 	/*
571 	 * These values have a tight interaction with each other that
572 	 * makes it hard to tightly bound them. So we can only check
573 	 * that they are within a broader possible range.
574 	 *
575 	 * The size cannot always be accurately determined, but ensure
576 	 * that it is consistent with the number of cylinder groups (fs_ncg)
577 	 * and the number of fragments per cylinder group (fs_fpg). Ensure
578 	 * that the summary information size is correct and that it starts
579 	 * and ends in the data area of the same cylinder group.
580 	 */
581 	FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd);
582 	FCHK(fs->fs_size, <=, ((int64_t)fs->fs_ncg - 1) * fs->fs_fpg, %jd);
583 	FCHK(fs->fs_size, >, (int64_t)fs->fs_ncg * fs->fs_fpg, %jd);
584 	/*
585 	 * If we are not requested to read in the csum data stop here
586 	 * as the correctness of the remaining values is only important
587 	 * to bound the space needed to be allocated to hold the csum data.
588 	 */
589 	if ((flags & UFS_NOCSUM) != 0)
590 		return (error);
591 	FCHK(fs->fs_csaddr, <, 0, %jd);
592 	FCHK(fs->fs_cssize, !=,
593 	    fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd);
594 	FCHK(fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize), >,
595 	    fs->fs_size, %jd);
596 	FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)), %jd);
597 	FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >,
598 	    dtog(fs, fs->fs_csaddr), %jd);
599 	/*
600 	 * With file system clustering it is possible to allocate
601 	 * many contiguous blocks. The kernel variable maxphys defines
602 	 * the maximum transfer size permitted by the controller and/or
603 	 * buffering. The fs_maxcontig parameter controls the maximum
604 	 * number of blocks that the filesystem will read or write
605 	 * in a single transfer. It is calculated when the filesystem
606 	 * is created as maxphys / fs_bsize. The loader uses a maxphys
607 	 * of 128K even when running on a system that supports larger
608 	 * values. If the filesystem was built on a system that supports
609 	 * a larger maxphys (1M is typical) it will have configured
610 	 * fs_maxcontig for that larger system. So we bound the upper
611 	 * allowable limit for fs_maxconfig to be able to at least
612 	 * work with a 1M maxphys on the smallest block size filesystem:
613 	 * 1M / 4096 == 256. There is no harm in allowing the mounting of
614 	 * filesystems that make larger than maxphys I/O requests because
615 	 * those (mostly 32-bit machines) can (very slowly) handle I/O
616 	 * requests that exceed maxphys.
617 	 */
618 	WCHK(fs->fs_maxcontig, <, 0, %jd);
619 	WCHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd);
620 	FCHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd);
621 	FCHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=,
622 	    MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd);
623 	return (error);
624 }
625 
626 /*
627  * Make an extensive search to find a superblock. If the superblock
628  * in the standard place cannot be used, try looking for one of the
629  * backup superblocks.
630  *
631  * Flags are made up of the following or'ed together options:
632  *
633  * UFS_NOMSG indicates that superblock inconsistency error messages
634  *    should not be printed.
635  *
636  * UFS_NOCSUM causes only the superblock itself to be returned, but does
637  *    not read in any auxillary data structures like the cylinder group
638  *    summary information.
639  */
640 int
641 ffs_sbsearch(void *devfd, struct fs **fsp, int reqflags,
642     struct malloc_type *filltype,
643     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
644 {
645 	struct fsrecovery *fsr;
646 	struct fs *protofs;
647 	void *fsrbuf;
648 	char *cp;
649 	long nocsum, flags, msg, cg;
650 	off_t sblk, secsize;
651 	int error;
652 
653 	msg = (reqflags & UFS_NOMSG) == 0;
654 	nocsum = reqflags & UFS_NOCSUM;
655 	/*
656 	 * Try normal superblock read and return it if it works.
657 	 *
658 	 * Suppress messages if it fails until we find out if
659 	 * failure can be avoided.
660 	 */
661 	flags = UFS_NOMSG | nocsum;
662 	error = ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc);
663 	/*
664 	 * If successful or endian error, no need to try further.
665 	 */
666 	if (error == 0 || error == EILSEQ) {
667 		if (msg && error == EILSEQ)
668 			printf("UFS superblock failed due to endian mismatch "
669 			    "between machine and filesystem\n");
670 		return (error);
671 	}
672 	/*
673 	 * First try: ignoring hash failures.
674 	 */
675 	flags |= UFS_NOHASHFAIL;
676 	if (msg)
677 		flags &= ~UFS_NOMSG;
678 	if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) == 0)
679 		return (0);
680 	/*
681 	 * Next up is to check if fields of the superblock that are
682 	 * needed to find backup superblocks are usable.
683 	 */
684 	if (msg)
685 		printf("Attempted recovery for standard superblock: failed\n");
686 	flags = UFS_FSRONLY | UFS_NOHASHFAIL | UFS_NOCSUM | UFS_NOMSG;
687 	if (ffs_sbget(devfd, &protofs, UFS_STDSB, flags, filltype,
688 	    readfunc) == 0) {
689 		if (msg)
690 			printf("Attempt extraction of recovery data from "
691 			    "standard superblock.\n");
692 	} else {
693 		/*
694 		 * Final desperation is to see if alternate superblock
695 		 * parameters have been saved in the boot area.
696 		 */
697 		if (msg)
698 			printf("Attempted extraction of recovery data from "
699 			    "standard superblock: failed\nAttempt to find "
700 			    "boot zone recovery data.\n");
701 		/*
702 		 * Look to see if recovery information has been saved.
703 		 * If so we can generate a prototype superblock based
704 		 * on that information.
705 		 *
706 		 * We need fragments-per-group, number of cylinder groups,
707 		 * location of the superblock within the cylinder group, and
708 		 * the conversion from filesystem fragments to disk blocks.
709 		 *
710 		 * When building a UFS2 filesystem, newfs(8) stores these
711 		 * details at the end of the boot block area at the start
712 		 * of the filesystem partition. If they have been overwritten
713 		 * by a boot block, we fail.  But usually they are there
714 		 * and we can use them.
715 		 *
716 		 * We could ask the underlying device for its sector size,
717 		 * but some devices lie. So we just try a plausible range.
718 		 */
719 		error = ENOENT;
720 		fsrbuf = NULL;
721 		for (secsize = dbtob(1); secsize <= SBLOCKSIZE; secsize *= 2)
722 			if ((error = (*readfunc)(devfd, (SBLOCK_UFS2 - secsize),
723 			    &fsrbuf, secsize)) == 0)
724 				break;
725 		if (error != 0)
726 			goto trynowarn;
727 		cp = fsrbuf; /* type change to keep compiler happy */
728 		fsr = (struct fsrecovery *)&cp[secsize - sizeof *fsr];
729 		if (fsr->fsr_magic != FS_UFS2_MAGIC ||
730 		    (protofs = UFS_MALLOC(SBLOCKSIZE, filltype, M_NOWAIT))
731 		    == NULL) {
732 			UFS_FREE(fsrbuf, filltype);
733 			goto trynowarn;
734 		}
735 		memset(protofs, 0, sizeof(struct fs));
736 		protofs->fs_fpg = fsr->fsr_fpg;
737 		protofs->fs_fsbtodb = fsr->fsr_fsbtodb;
738 		protofs->fs_sblkno = fsr->fsr_sblkno;
739 		protofs->fs_magic = fsr->fsr_magic;
740 		protofs->fs_ncg = fsr->fsr_ncg;
741 		UFS_FREE(fsrbuf, filltype);
742 	}
743 	/*
744 	 * Scan looking for alternative superblocks.
745 	 */
746 	flags = nocsum;
747 	if (!msg)
748 		flags |= UFS_NOMSG;
749 	for (cg = 0; cg < protofs->fs_ncg; cg++) {
750 		sblk = fsbtodb(protofs, cgsblock(protofs, cg));
751 		if (msg)
752 			printf("Try cg %ld at sblock loc %jd\n", cg,
753 			    (intmax_t)sblk);
754 		if (ffs_sbget(devfd, fsp, dbtob(sblk), flags, filltype,
755 		    readfunc) == 0) {
756 			if (msg)
757 				printf("Succeeded with alternate superblock "
758 				    "at %jd\n", (intmax_t)sblk);
759 			UFS_FREE(protofs, filltype);
760 			return (0);
761 		}
762 	}
763 	UFS_FREE(protofs, filltype);
764 	/*
765 	 * Our alternate superblock strategies failed. Our last ditch effort
766 	 * is to see if the standard superblock has only non-critical errors.
767 	 */
768 trynowarn:
769 	flags = UFS_NOWARNFAIL | UFS_NOMSG | nocsum;
770 	if (msg) {
771 		printf("Finding an alternate superblock failed.\nCheck for "
772 		    "only non-critical errors in standard superblock\n");
773 		flags &= ~UFS_NOMSG;
774 	}
775 	if (ffs_sbget(devfd, fsp, UFS_STDSB, flags, filltype, readfunc) != 0) {
776 		if (msg)
777 			printf("Failed, superblock has critical errors\n");
778 		return (ENOENT);
779 	}
780 	if (msg)
781 		printf("Success, using standard superblock with "
782 		    "non-critical errors.\n");
783 	return (0);
784 }
785 
786 /*
787  * Write a superblock to the devfd device from the memory pointed to by fs.
788  * Write out the superblock summary information if it is present.
789  *
790  * If the write is successful, zero is returned. Otherwise one of the
791  * following error values is returned:
792  *     EIO: failed to write superblock.
793  *     EIO: failed to write superblock summary information.
794  */
795 int
796 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
797     int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
798 {
799 	int i, error, blks, size;
800 	uint8_t *space;
801 
802 	/*
803 	 * If there is summary information, write it first, so if there
804 	 * is an error, the superblock will not be marked as clean.
805 	 */
806 	if (fs->fs_si != NULL && fs->fs_csp != NULL) {
807 		blks = howmany(fs->fs_cssize, fs->fs_fsize);
808 		space = (uint8_t *)fs->fs_csp;
809 		for (i = 0; i < blks; i += fs->fs_frag) {
810 			size = fs->fs_bsize;
811 			if (i + fs->fs_frag > blks)
812 				size = (blks - i) * fs->fs_fsize;
813 			if ((error = (*writefunc)(devfd,
814 			     dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
815 			     space, size)) != 0)
816 				return (error);
817 			space += size;
818 		}
819 	}
820 	fs->fs_fmod = 0;
821 #ifndef _KERNEL
822 	{
823 		struct fs_summary_info *fs_si;
824 
825 		fs->fs_time = time(NULL);
826 		/* Clear the pointers for the duration of writing. */
827 		fs_si = fs->fs_si;
828 		fs->fs_si = NULL;
829 		fs->fs_ckhash = ffs_calc_sbhash(fs);
830 		error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
831 		fs->fs_si = fs_si;
832 	}
833 #else /* _KERNEL */
834 	fs->fs_time = time_second;
835 	fs->fs_ckhash = ffs_calc_sbhash(fs);
836 	error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
837 #endif /* _KERNEL */
838 	return (error);
839 }
840 
841 /*
842  * Calculate the check-hash for a superblock.
843  */
844 uint32_t
845 ffs_calc_sbhash(struct fs *fs)
846 {
847 	uint32_t ckhash, save_ckhash;
848 
849 	/*
850 	 * A filesystem that was using a superblock ckhash may be moved
851 	 * to an older kernel that does not support ckhashes. The
852 	 * older kernel will clear the FS_METACKHASH flag indicating
853 	 * that it does not update hashes. When the disk is moved back
854 	 * to a kernel capable of ckhashes it disables them on mount:
855 	 *
856 	 *	if ((fs->fs_flags & FS_METACKHASH) == 0)
857 	 *		fs->fs_metackhash = 0;
858 	 *
859 	 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
860 	 * old stale value in the fs->fs_ckhash field. Thus the need to
861 	 * just accept what is there.
862 	 */
863 	if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
864 		return (fs->fs_ckhash);
865 
866 	save_ckhash = fs->fs_ckhash;
867 	fs->fs_ckhash = 0;
868 	/*
869 	 * If newly read from disk, the caller is responsible for
870 	 * verifying that fs->fs_sbsize <= SBLOCKSIZE.
871 	 */
872 	ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
873 	fs->fs_ckhash = save_ckhash;
874 	return (ckhash);
875 }
876 
877 /*
878  * Update the frsum fields to reflect addition or deletion
879  * of some frags.
880  */
881 void
882 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
883 {
884 	int inblk;
885 	int field, subfield;
886 	int siz, pos;
887 
888 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
889 	fragmap <<= 1;
890 	for (siz = 1; siz < fs->fs_frag; siz++) {
891 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
892 			continue;
893 		field = around[siz];
894 		subfield = inside[siz];
895 		for (pos = siz; pos <= fs->fs_frag; pos++) {
896 			if ((fragmap & field) == subfield) {
897 				fraglist[siz] += cnt;
898 				pos += siz;
899 				field <<= siz;
900 				subfield <<= siz;
901 			}
902 			field <<= 1;
903 			subfield <<= 1;
904 		}
905 	}
906 }
907 
908 /*
909  * block operations
910  *
911  * check if a block is available
912  */
913 int
914 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
915 {
916 	unsigned char mask;
917 
918 	switch ((int)fs->fs_frag) {
919 	case 8:
920 		return (cp[h] == 0xff);
921 	case 4:
922 		mask = 0x0f << ((h & 0x1) << 2);
923 		return ((cp[h >> 1] & mask) == mask);
924 	case 2:
925 		mask = 0x03 << ((h & 0x3) << 1);
926 		return ((cp[h >> 2] & mask) == mask);
927 	case 1:
928 		mask = 0x01 << (h & 0x7);
929 		return ((cp[h >> 3] & mask) == mask);
930 	default:
931 #ifdef _KERNEL
932 		panic("ffs_isblock");
933 #endif
934 		break;
935 	}
936 	return (0);
937 }
938 
939 /*
940  * check if a block is free
941  */
942 int
943 ffs_isfreeblock(struct fs *fs, uint8_t *cp, ufs1_daddr_t h)
944 {
945 
946 	switch ((int)fs->fs_frag) {
947 	case 8:
948 		return (cp[h] == 0);
949 	case 4:
950 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
951 	case 2:
952 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
953 	case 1:
954 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
955 	default:
956 #ifdef _KERNEL
957 		panic("ffs_isfreeblock");
958 #endif
959 		break;
960 	}
961 	return (0);
962 }
963 
964 /*
965  * take a block out of the map
966  */
967 void
968 ffs_clrblock(struct fs *fs, uint8_t *cp, ufs1_daddr_t h)
969 {
970 
971 	switch ((int)fs->fs_frag) {
972 	case 8:
973 		cp[h] = 0;
974 		return;
975 	case 4:
976 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
977 		return;
978 	case 2:
979 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
980 		return;
981 	case 1:
982 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
983 		return;
984 	default:
985 #ifdef _KERNEL
986 		panic("ffs_clrblock");
987 #endif
988 		break;
989 	}
990 }
991 
992 /*
993  * put a block into the map
994  */
995 void
996 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
997 {
998 
999 	switch ((int)fs->fs_frag) {
1000 	case 8:
1001 		cp[h] = 0xff;
1002 		return;
1003 	case 4:
1004 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
1005 		return;
1006 	case 2:
1007 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
1008 		return;
1009 	case 1:
1010 		cp[h >> 3] |= (0x01 << (h & 0x7));
1011 		return;
1012 	default:
1013 #ifdef _KERNEL
1014 		panic("ffs_setblock");
1015 #endif
1016 		break;
1017 	}
1018 }
1019 
1020 /*
1021  * Update the cluster map because of an allocation or free.
1022  *
1023  * Cnt == 1 means free; cnt == -1 means allocating.
1024  */
1025 void
1026 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
1027 {
1028 	int32_t *sump;
1029 	int32_t *lp;
1030 	uint8_t *freemapp, *mapp;
1031 	int i, start, end, forw, back, map;
1032 	uint64_t bit;
1033 
1034 	if (fs->fs_contigsumsize <= 0)
1035 		return;
1036 	freemapp = cg_clustersfree(cgp);
1037 	sump = cg_clustersum(cgp);
1038 	/*
1039 	 * Allocate or clear the actual block.
1040 	 */
1041 	if (cnt > 0)
1042 		setbit(freemapp, blkno);
1043 	else
1044 		clrbit(freemapp, blkno);
1045 	/*
1046 	 * Find the size of the cluster going forward.
1047 	 */
1048 	start = blkno + 1;
1049 	end = start + fs->fs_contigsumsize;
1050 	if (end >= cgp->cg_nclusterblks)
1051 		end = cgp->cg_nclusterblks;
1052 	mapp = &freemapp[start / NBBY];
1053 	map = *mapp++;
1054 	bit = 1U << (start % NBBY);
1055 	for (i = start; i < end; i++) {
1056 		if ((map & bit) == 0)
1057 			break;
1058 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
1059 			bit <<= 1;
1060 		} else {
1061 			map = *mapp++;
1062 			bit = 1;
1063 		}
1064 	}
1065 	forw = i - start;
1066 	/*
1067 	 * Find the size of the cluster going backward.
1068 	 */
1069 	start = blkno - 1;
1070 	end = start - fs->fs_contigsumsize;
1071 	if (end < 0)
1072 		end = -1;
1073 	mapp = &freemapp[start / NBBY];
1074 	map = *mapp--;
1075 	bit = 1U << (start % NBBY);
1076 	for (i = start; i > end; i--) {
1077 		if ((map & bit) == 0)
1078 			break;
1079 		if ((i & (NBBY - 1)) != 0) {
1080 			bit >>= 1;
1081 		} else {
1082 			map = *mapp--;
1083 			bit = 1U << (NBBY - 1);
1084 		}
1085 	}
1086 	back = start - i;
1087 	/*
1088 	 * Account for old cluster and the possibly new forward and
1089 	 * back clusters.
1090 	 */
1091 	i = back + forw + 1;
1092 	if (i > fs->fs_contigsumsize)
1093 		i = fs->fs_contigsumsize;
1094 	sump[i] += cnt;
1095 	if (back > 0)
1096 		sump[back] -= cnt;
1097 	if (forw > 0)
1098 		sump[forw] -= cnt;
1099 	/*
1100 	 * Update cluster summary information.
1101 	 */
1102 	lp = &sump[fs->fs_contigsumsize];
1103 	for (i = fs->fs_contigsumsize; i > 0; i--)
1104 		if (*lp-- > 0)
1105 			break;
1106 	fs->fs_maxcluster[cgp->cg_cgx] = i;
1107 }
1108