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