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