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