xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision 1165fc9a526630487a1feb63daef65c5aee1a583)
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/limits.h>
39 
40 #ifndef _KERNEL
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <time.h>
45 #include <sys/errno.h>
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ffs/fs.h>
48 
49 uint32_t calculate_crc32c(uint32_t, const void *, size_t);
50 uint32_t ffs_calc_sbhash(struct fs *);
51 struct malloc_type;
52 #define UFS_MALLOC(size, type, flags) malloc(size)
53 #define UFS_FREE(ptr, type) free(ptr)
54 #define maxphys MAXPHYS
55 
56 #else /* _KERNEL */
57 #include <sys/systm.h>
58 #include <sys/gsb_crc32.h>
59 #include <sys/lock.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
62 #include <sys/vnode.h>
63 #include <sys/bio.h>
64 #include <sys/buf.h>
65 #include <sys/ucred.h>
66 
67 #include <ufs/ufs/quota.h>
68 #include <ufs/ufs/inode.h>
69 #include <ufs/ufs/extattr.h>
70 #include <ufs/ufs/ufsmount.h>
71 #include <ufs/ufs/ufs_extern.h>
72 #include <ufs/ffs/ffs_extern.h>
73 #include <ufs/ffs/fs.h>
74 
75 #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
76 #define UFS_FREE(ptr, type) free(ptr, type)
77 
78 #endif /* _KERNEL */
79 
80 /*
81  * Verify an inode check-hash.
82  */
83 int
84 ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
85 {
86 	uint32_t ckhash, save_ckhash;
87 
88 	/*
89 	 * Return success if unallocated or we are not doing inode check-hash.
90 	 */
91 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
92 		return (0);
93 	/*
94 	 * Exclude di_ckhash from the crc32 calculation, e.g., always use
95 	 * a check-hash value of zero when calculating the check-hash.
96 	 */
97 	save_ckhash = dip->di_ckhash;
98 	dip->di_ckhash = 0;
99 	ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
100 	dip->di_ckhash = save_ckhash;
101 	if (save_ckhash == ckhash)
102 		return (0);
103 	return (EINVAL);
104 }
105 
106 /*
107  * Update an inode check-hash.
108  */
109 void
110 ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
111 {
112 
113 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
114 		return;
115 	/*
116 	 * Exclude old di_ckhash from the crc32 calculation, e.g., always use
117 	 * a check-hash value of zero when calculating the new check-hash.
118 	 */
119 	dip->di_ckhash = 0;
120 	dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
121 }
122 
123 /*
124  * These are the low-level functions that actually read and write
125  * the superblock and its associated data.
126  */
127 static off_t sblock_try[] = SBLOCKSEARCH;
128 static int readsuper(void *, struct fs **, off_t, int,
129 	int (*)(void *, off_t, void **, int));
130 static int validate_sblock(struct fs *, int);
131 
132 /*
133  * Read a superblock from the devfd device.
134  *
135  * If an alternate superblock is specified, it is read. Otherwise the
136  * set of locations given in the SBLOCKSEARCH list is searched for a
137  * superblock. Memory is allocated for the superblock by the readfunc and
138  * is returned. If filltype is non-NULL, additional memory is allocated
139  * of type filltype and filled in with the superblock summary information.
140  * All memory is freed when any error is returned.
141  *
142  * If a superblock is found, zero is returned. Otherwise one of the
143  * following error values is returned:
144  *     EIO: non-existent or truncated superblock.
145  *     EIO: error reading summary information.
146  *     ENOENT: no usable known superblock 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(u_int8_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(u_int8_t);
240 	fs->fs_contigdirs = (u_int8_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 #define MPRINT		if (prtmsg) printf
327 #define FCHK(lhs, op, rhs, fmt)						\
328 	if (lhs op rhs) {						\
329 		MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s ("	\
330 		    #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,	\
331 		    #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs);	\
332 		if (error == 0)						\
333 			error = ENOENT;					\
334 	}
335 #define WCHK(lhs, op, rhs, fmt)						\
336 	if (lhs op rhs) {						\
337 		MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s ("	\
338 		    #fmt ")%s\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,\
339 		    #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs, wmsg);\
340 		if (error == 0)						\
341 			error = warnerr;				\
342 	}
343 #define FCHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt)			\
344 	if (lhs1 op1 rhs1 && lhs2 op2 rhs2) {				\
345 		MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s ("	\
346 		    #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n",	\
347 		    fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1,	\
348 		    (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2,	\
349 		    (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2);	\
350 		if (error == 0)						\
351 			error = ENOENT;					\
352 	}
353 #define WCHK2(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 ")%s\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, wmsg);	\
360 		if (error == 0)						\
361 			error = warnerr;				\
362 	}
363 
364 static int
365 validate_sblock(struct fs *fs, int flags)
366 {
367 	u_long i, sectorsize;
368 	u_int64_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 	if (fs->fs_magic == FS_UFS2_MAGIC) {
378 		if ((flags & UFS_ALTSBLK) == 0)
379 			FCHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2,
380 			    fs->fs_sblockactualloc, !=, 0, %jd);
381 		FCHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
382 		FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
383 			sizeof(ufs2_daddr_t)), %jd);
384 		FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t),
385 		    %jd);
386 		FCHK(fs->fs_inopb, !=,
387 		    fs->fs_bsize / sizeof(struct ufs2_dinode), %jd);
388 	} else if (fs->fs_magic == FS_UFS1_MAGIC) {
389 		if ((flags & UFS_ALTSBLK) == 0)
390 			FCHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd);
391 		FCHK(fs->fs_sblockloc, <, 0, %jd);
392 		FCHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
393 		FCHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t),
394 		    %jd);
395 		FCHK(fs->fs_inopb, !=,
396 		    fs->fs_bsize / sizeof(struct ufs1_dinode), %jd);
397 		FCHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
398 			sizeof(ufs1_daddr_t)), %jd);
399 		WCHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd);
400 		WCHK(fs->fs_old_rotdelay, !=, 0, %jd);
401 		WCHK(fs->fs_old_rps, !=, 60, %jd);
402 		WCHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd);
403 		WCHK(fs->fs_old_cpg, !=, 1, %jd);
404 		WCHK(fs->fs_old_interleave, !=, 1, %jd);
405 		WCHK(fs->fs_old_trackskew, !=, 0, %jd);
406 		WCHK(fs->fs_old_cpc, !=, 0, %jd);
407 		WCHK(fs->fs_old_postblformat, !=, 1, %jd);
408 		WCHK(fs->fs_old_nrpos, !=, 1, %jd);
409 		WCHK(fs->fs_old_spc, !=, fs->fs_fpg * fs->fs_old_nspf, %jd);
410 		WCHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd);
411 		WCHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd);
412 		FCHK(fs->fs_old_ncyl, !=, fs->fs_ncg, %jd);
413 	} else {
414 		/* Bad magic number, so assume not a superblock */
415 		return (ENOENT);
416 	}
417 	FCHK(fs->fs_bsize, <, MINBSIZE, %jd);
418 	FCHK(fs->fs_bsize, >, MAXBSIZE, %jd);
419 	FCHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd);
420 	FCHK(powerof2(fs->fs_bsize), ==, 0, %jd);
421 	FCHK(fs->fs_frag, <, 1, %jd);
422 	FCHK(fs->fs_frag, >, MAXFRAG, %jd);
423 	FCHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd);
424 	FCHK(fs->fs_fsize, <, sectorsize, %jd);
425 	FCHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd);
426 	FCHK(powerof2(fs->fs_fsize), ==, 0, %jd);
427 	FCHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd);
428 	FCHK(fs->fs_ncg, <, 1, %jd);
429 	FCHK(fs->fs_ipg, <, 1, %jd);
430 	FCHK(fs->fs_ipg * fs->fs_ncg, >, (((int64_t)(1)) << 32) - INOPB(fs),
431 	    %jd);
432 	FCHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd);
433 	FCHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd);
434 	FCHK(powerof2(fs->fs_maxbsize), ==, 0, %jd);
435 	FCHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd);
436 	FCHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx);
437 	FCHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx);
438 	FCHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx);
439 	FCHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx);
440 	FCHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd);
441 	FCHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd);
442 	FCHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd);
443 	FCHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd);
444 	FCHK(fs->fs_old_cgoffset, <, 0, %jd);
445 	FCHK2(fs->fs_old_cgoffset, >, 0, ~fs->fs_old_cgmask, <, 0, %jd);
446 	FCHK(fs->fs_old_cgoffset * (~fs->fs_old_cgmask), >, fs->fs_fpg, %jd);
447 	FCHK(fs->fs_sblkno, !=, roundup(
448 	    howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize),
449 	    fs->fs_frag), %jd);
450 	FCHK(fs->fs_cblkno, !=, fs->fs_sblkno +
451 	    roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd);
452 	FCHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd);
453 	FCHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd);
454 	FCHK(fs->fs_cgsize, >, fs->fs_bsize, %jd);
455 	/*
456 	 * This test is valid, however older versions of growfs failed
457 	 * to correctly update fs_dsize so will fail this test. Thus we
458 	 * exclude it from the requirements.
459 	 */
460 #ifdef notdef
461 	WCHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno -
462 		fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) -
463 		howmany(fs->fs_cssize, fs->fs_fsize), %jd);
464 #endif
465 	WCHK(fs->fs_metaspace, <, 0, %jd);
466 	WCHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd);
467 	WCHK(fs->fs_minfree, >, 99, %jd%%);
468 	maxfilesize = fs->fs_bsize * UFS_NDADDR - 1;
469 	for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) {
470 		sizepb *= NINDIR(fs);
471 		maxfilesize += sizepb;
472 	}
473 	WCHK(fs->fs_maxfilesize, !=, maxfilesize, %jd);
474 	/*
475 	 * These values have a tight interaction with each other that
476 	 * makes it hard to tightly bound them. So we can only check
477 	 * that they are within a broader possible range.
478 	 *
479 	 * The size cannot always be accurately determined, but ensure
480 	 * that it is consistent with the number of cylinder groups (fs_ncg)
481 	 * and the number of fragments per cylinder group (fs_fpg). Ensure
482 	 * that the summary information size is correct and that it starts
483 	 * and ends in the data area of the same cylinder group.
484 	 */
485 	FCHK(fs->fs_size, <, 8 * fs->fs_frag, %jd);
486 	WCHK(fs->fs_size, <=, (fs->fs_ncg - 1) * fs->fs_fpg, %jd);
487 	WCHK(fs->fs_size, >, fs->fs_ncg * fs->fs_fpg, %jd);
488 	/*
489 	 * If we are not requested to read in the csum data stop here
490 	 * as the correctness of the remaining values is only important
491 	 * to bound the space needed to be allocated to hold the csum data.
492 	 */
493 	if ((flags & UFS_NOCSUM) != 0)
494 		return (error);
495 	FCHK(fs->fs_csaddr, <, 0, %jd);
496 	FCHK(fs->fs_cssize, !=,
497 	    fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd);
498 	FCHK(dtog(fs, fs->fs_csaddr), >, fs->fs_ncg, %jd);
499 	FCHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)), %jd);
500 	FCHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >,
501 	    dtog(fs, fs->fs_csaddr), %jd);
502 	/*
503 	 * With file system clustering it is possible to allocate
504 	 * many contiguous blocks. The kernel variable maxphys defines
505 	 * the maximum transfer size permitted by the controller and/or
506 	 * buffering. The fs_maxcontig parameter controls the maximum
507 	 * number of blocks that the filesystem will read or write
508 	 * in a single transfer. It is calculated when the filesystem
509 	 * is created as maxphys / fs_bsize. The loader uses a maxphys
510 	 * of 128K even when running on a system that supports larger
511 	 * values. If the filesystem was built on a system that supports
512 	 * a larger maxphys (1M is typical) it will have configured
513 	 * fs_maxcontig for that larger system. So we bound the upper
514 	 * allowable limit for fs_maxconfig to be able to at least
515 	 * work with a 1M maxphys on the smallest block size filesystem:
516 	 * 1M / 4096 == 256. There is no harm in allowing the mounting of
517 	 * filesystems that make larger than maxphys I/O requests because
518 	 * those (mostly 32-bit machines) can (very slowly) handle I/O
519 	 * requests that exceed maxphys.
520 	 */
521 	WCHK(fs->fs_maxcontig, <, 0, %jd);
522 	WCHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd);
523 	WCHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd);
524 	WCHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=,
525 	    MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd);
526 	return (error);
527 }
528 
529 /*
530  * Write a superblock to the devfd device from the memory pointed to by fs.
531  * Write out the superblock summary information if it is present.
532  *
533  * If the write is successful, zero is returned. Otherwise one of the
534  * following error values is returned:
535  *     EIO: failed to write superblock.
536  *     EIO: failed to write superblock summary information.
537  */
538 int
539 ffs_sbput(void *devfd, struct fs *fs, off_t loc,
540     int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
541 {
542 	int i, error, blks, size;
543 	uint8_t *space;
544 
545 	/*
546 	 * If there is summary information, write it first, so if there
547 	 * is an error, the superblock will not be marked as clean.
548 	 */
549 	if (fs->fs_si != NULL && fs->fs_csp != NULL) {
550 		blks = howmany(fs->fs_cssize, fs->fs_fsize);
551 		space = (uint8_t *)fs->fs_csp;
552 		for (i = 0; i < blks; i += fs->fs_frag) {
553 			size = fs->fs_bsize;
554 			if (i + fs->fs_frag > blks)
555 				size = (blks - i) * fs->fs_fsize;
556 			if ((error = (*writefunc)(devfd,
557 			     dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
558 			     space, size)) != 0)
559 				return (error);
560 			space += size;
561 		}
562 	}
563 	fs->fs_fmod = 0;
564 #ifndef _KERNEL
565 	{
566 		struct fs_summary_info *fs_si;
567 
568 		fs->fs_time = time(NULL);
569 		/* Clear the pointers for the duration of writing. */
570 		fs_si = fs->fs_si;
571 		fs->fs_si = NULL;
572 		fs->fs_ckhash = ffs_calc_sbhash(fs);
573 		error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
574 		fs->fs_si = fs_si;
575 	}
576 #else /* _KERNEL */
577 	fs->fs_time = time_second;
578 	fs->fs_ckhash = ffs_calc_sbhash(fs);
579 	error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
580 #endif /* _KERNEL */
581 	return (error);
582 }
583 
584 /*
585  * Calculate the check-hash for a superblock.
586  */
587 uint32_t
588 ffs_calc_sbhash(struct fs *fs)
589 {
590 	uint32_t ckhash, save_ckhash;
591 
592 	/*
593 	 * A filesystem that was using a superblock ckhash may be moved
594 	 * to an older kernel that does not support ckhashes. The
595 	 * older kernel will clear the FS_METACKHASH flag indicating
596 	 * that it does not update hashes. When the disk is moved back
597 	 * to a kernel capable of ckhashes it disables them on mount:
598 	 *
599 	 *	if ((fs->fs_flags & FS_METACKHASH) == 0)
600 	 *		fs->fs_metackhash = 0;
601 	 *
602 	 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
603 	 * old stale value in the fs->fs_ckhash field. Thus the need to
604 	 * just accept what is there.
605 	 */
606 	if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
607 		return (fs->fs_ckhash);
608 
609 	save_ckhash = fs->fs_ckhash;
610 	fs->fs_ckhash = 0;
611 	/*
612 	 * If newly read from disk, the caller is responsible for
613 	 * verifying that fs->fs_sbsize <= SBLOCKSIZE.
614 	 */
615 	ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
616 	fs->fs_ckhash = save_ckhash;
617 	return (ckhash);
618 }
619 
620 /*
621  * Update the frsum fields to reflect addition or deletion
622  * of some frags.
623  */
624 void
625 ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
626 {
627 	int inblk;
628 	int field, subfield;
629 	int siz, pos;
630 
631 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
632 	fragmap <<= 1;
633 	for (siz = 1; siz < fs->fs_frag; siz++) {
634 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
635 			continue;
636 		field = around[siz];
637 		subfield = inside[siz];
638 		for (pos = siz; pos <= fs->fs_frag; pos++) {
639 			if ((fragmap & field) == subfield) {
640 				fraglist[siz] += cnt;
641 				pos += siz;
642 				field <<= siz;
643 				subfield <<= siz;
644 			}
645 			field <<= 1;
646 			subfield <<= 1;
647 		}
648 	}
649 }
650 
651 /*
652  * block operations
653  *
654  * check if a block is available
655  */
656 int
657 ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
658 {
659 	unsigned char mask;
660 
661 	switch ((int)fs->fs_frag) {
662 	case 8:
663 		return (cp[h] == 0xff);
664 	case 4:
665 		mask = 0x0f << ((h & 0x1) << 2);
666 		return ((cp[h >> 1] & mask) == mask);
667 	case 2:
668 		mask = 0x03 << ((h & 0x3) << 1);
669 		return ((cp[h >> 2] & mask) == mask);
670 	case 1:
671 		mask = 0x01 << (h & 0x7);
672 		return ((cp[h >> 3] & mask) == mask);
673 	default:
674 #ifdef _KERNEL
675 		panic("ffs_isblock");
676 #endif
677 		break;
678 	}
679 	return (0);
680 }
681 
682 /*
683  * check if a block is free
684  */
685 int
686 ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
687 {
688 
689 	switch ((int)fs->fs_frag) {
690 	case 8:
691 		return (cp[h] == 0);
692 	case 4:
693 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
694 	case 2:
695 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
696 	case 1:
697 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
698 	default:
699 #ifdef _KERNEL
700 		panic("ffs_isfreeblock");
701 #endif
702 		break;
703 	}
704 	return (0);
705 }
706 
707 /*
708  * take a block out of the map
709  */
710 void
711 ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
712 {
713 
714 	switch ((int)fs->fs_frag) {
715 	case 8:
716 		cp[h] = 0;
717 		return;
718 	case 4:
719 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
720 		return;
721 	case 2:
722 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
723 		return;
724 	case 1:
725 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
726 		return;
727 	default:
728 #ifdef _KERNEL
729 		panic("ffs_clrblock");
730 #endif
731 		break;
732 	}
733 }
734 
735 /*
736  * put a block into the map
737  */
738 void
739 ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
740 {
741 
742 	switch ((int)fs->fs_frag) {
743 	case 8:
744 		cp[h] = 0xff;
745 		return;
746 	case 4:
747 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
748 		return;
749 	case 2:
750 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
751 		return;
752 	case 1:
753 		cp[h >> 3] |= (0x01 << (h & 0x7));
754 		return;
755 	default:
756 #ifdef _KERNEL
757 		panic("ffs_setblock");
758 #endif
759 		break;
760 	}
761 }
762 
763 /*
764  * Update the cluster map because of an allocation or free.
765  *
766  * Cnt == 1 means free; cnt == -1 means allocating.
767  */
768 void
769 ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
770 {
771 	int32_t *sump;
772 	int32_t *lp;
773 	u_char *freemapp, *mapp;
774 	int i, start, end, forw, back, map;
775 	u_int bit;
776 
777 	if (fs->fs_contigsumsize <= 0)
778 		return;
779 	freemapp = cg_clustersfree(cgp);
780 	sump = cg_clustersum(cgp);
781 	/*
782 	 * Allocate or clear the actual block.
783 	 */
784 	if (cnt > 0)
785 		setbit(freemapp, blkno);
786 	else
787 		clrbit(freemapp, blkno);
788 	/*
789 	 * Find the size of the cluster going forward.
790 	 */
791 	start = blkno + 1;
792 	end = start + fs->fs_contigsumsize;
793 	if (end >= cgp->cg_nclusterblks)
794 		end = cgp->cg_nclusterblks;
795 	mapp = &freemapp[start / NBBY];
796 	map = *mapp++;
797 	bit = 1U << (start % NBBY);
798 	for (i = start; i < end; i++) {
799 		if ((map & bit) == 0)
800 			break;
801 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
802 			bit <<= 1;
803 		} else {
804 			map = *mapp++;
805 			bit = 1;
806 		}
807 	}
808 	forw = i - start;
809 	/*
810 	 * Find the size of the cluster going backward.
811 	 */
812 	start = blkno - 1;
813 	end = start - fs->fs_contigsumsize;
814 	if (end < 0)
815 		end = -1;
816 	mapp = &freemapp[start / NBBY];
817 	map = *mapp--;
818 	bit = 1U << (start % NBBY);
819 	for (i = start; i > end; i--) {
820 		if ((map & bit) == 0)
821 			break;
822 		if ((i & (NBBY - 1)) != 0) {
823 			bit >>= 1;
824 		} else {
825 			map = *mapp--;
826 			bit = 1U << (NBBY - 1);
827 		}
828 	}
829 	back = start - i;
830 	/*
831 	 * Account for old cluster and the possibly new forward and
832 	 * back clusters.
833 	 */
834 	i = back + forw + 1;
835 	if (i > fs->fs_contigsumsize)
836 		i = fs->fs_contigsumsize;
837 	sump[i] += cnt;
838 	if (back > 0)
839 		sump[back] -= cnt;
840 	if (forw > 0)
841 		sump[forw] -= cnt;
842 	/*
843 	 * Update cluster summary information.
844 	 */
845 	lp = &sump[fs->fs_contigsumsize];
846 	for (i = fs->fs_contigsumsize; i > 0; i--)
847 		if (*lp-- > 0)
848 			break;
849 	fs->fs_maxcluster[cgp->cg_cgx] = i;
850 }
851