xref: /freebsd/sys/ufs/ffs/ffs_subr.c (revision b21582ee)
160727d8bSWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1515c377c3SEd Maste  * 3. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31996c772fSJohn Dyson  *	@(#)ffs_subr.c	8.5 (Berkeley) 3/21/95
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34f4636c59SDavid E. O'Brien #include <sys/cdefs.h>
35f4636c59SDavid E. O'Brien __FBSDID("$FreeBSD$");
36f4636c59SDavid E. O'Brien 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38076002f2SKirk McKusick #include <sys/limits.h>
39df8bae1dSRodney W. Grimes 
40c4473420SPeter Wemm #ifndef _KERNEL
41dffce215SKirk McKusick #include <stdio.h>
42dffce215SKirk McKusick #include <string.h>
43dffce215SKirk McKusick #include <stdlib.h>
44dffce215SKirk McKusick #include <time.h>
45dffce215SKirk McKusick #include <sys/errno.h>
46996c772fSJohn Dyson #include <ufs/ufs/dinode.h>
471c85e6a3SKirk McKusick #include <ufs/ffs/fs.h>
48dffce215SKirk McKusick 
49ec888383SKirk McKusick uint32_t calculate_crc32c(uint32_t, const void *, size_t);
50ade67b50SKirk McKusick uint32_t ffs_calc_sbhash(struct fs *);
51dffce215SKirk McKusick struct malloc_type;
52dffce215SKirk McKusick #define UFS_MALLOC(size, type, flags) malloc(size)
53dffce215SKirk McKusick #define UFS_FREE(ptr, type) free(ptr)
54076002f2SKirk McKusick #define maxphys MAXPHYS
55dffce215SKirk McKusick 
56dffce215SKirk McKusick #else /* _KERNEL */
57df8bae1dSRodney W. Grimes #include <sys/systm.h>
58f89d2072SXin LI #include <sys/gsb_crc32.h>
591cd52ec3SBruce Evans #include <sys/lock.h>
601c85e6a3SKirk McKusick #include <sys/malloc.h>
611c85e6a3SKirk McKusick #include <sys/mount.h>
62df8bae1dSRodney W. Grimes #include <sys/vnode.h>
639626b608SPoul-Henning Kamp #include <sys/bio.h>
64df8bae1dSRodney W. Grimes #include <sys/buf.h>
6508637435SBruce Evans #include <sys/ucred.h>
6608637435SBruce Evans 
67df8bae1dSRodney W. Grimes #include <ufs/ufs/quota.h>
68df8bae1dSRodney W. Grimes #include <ufs/ufs/inode.h>
691c85e6a3SKirk McKusick #include <ufs/ufs/extattr.h>
701c85e6a3SKirk McKusick #include <ufs/ufs/ufsmount.h>
711c85e6a3SKirk McKusick #include <ufs/ufs/ufs_extern.h>
72996c772fSJohn Dyson #include <ufs/ffs/ffs_extern.h>
731c85e6a3SKirk McKusick #include <ufs/ffs/fs.h>
74df8bae1dSRodney W. Grimes 
75dffce215SKirk McKusick #define UFS_MALLOC(size, type, flags) malloc(size, type, flags)
76dffce215SKirk McKusick #define UFS_FREE(ptr, type) free(ptr, type)
77dffce215SKirk McKusick 
78fb14e73cSKirk McKusick #endif /* _KERNEL */
79df8bae1dSRodney W. Grimes 
80df8bae1dSRodney W. Grimes /*
818f829a5cSKirk McKusick  * Verify an inode check-hash.
828f829a5cSKirk McKusick  */
838f829a5cSKirk McKusick int
848f829a5cSKirk McKusick ffs_verify_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
858f829a5cSKirk McKusick {
86c8f55fc4SKirk McKusick 	uint32_t ckhash, save_ckhash;
878f829a5cSKirk McKusick 
888f829a5cSKirk McKusick 	/*
898f829a5cSKirk McKusick 	 * Return success if unallocated or we are not doing inode check-hash.
908f829a5cSKirk McKusick 	 */
918f829a5cSKirk McKusick 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
928f829a5cSKirk McKusick 		return (0);
938f829a5cSKirk McKusick 	/*
948f829a5cSKirk McKusick 	 * Exclude di_ckhash from the crc32 calculation, e.g., always use
958f829a5cSKirk McKusick 	 * a check-hash value of zero when calculating the check-hash.
968f829a5cSKirk McKusick 	 */
978f829a5cSKirk McKusick 	save_ckhash = dip->di_ckhash;
988f829a5cSKirk McKusick 	dip->di_ckhash = 0;
99c8f55fc4SKirk McKusick 	ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
1008f829a5cSKirk McKusick 	dip->di_ckhash = save_ckhash;
101c8f55fc4SKirk McKusick 	if (save_ckhash == ckhash)
1028f829a5cSKirk McKusick 		return (0);
103c8f55fc4SKirk McKusick 	return (EINVAL);
1048f829a5cSKirk McKusick }
1058f829a5cSKirk McKusick 
1068f829a5cSKirk McKusick /*
1078f829a5cSKirk McKusick  * Update an inode check-hash.
1088f829a5cSKirk McKusick  */
1098f829a5cSKirk McKusick void
1108f829a5cSKirk McKusick ffs_update_dinode_ckhash(struct fs *fs, struct ufs2_dinode *dip)
1118f829a5cSKirk McKusick {
1128f829a5cSKirk McKusick 
1138f829a5cSKirk McKusick 	if (dip->di_mode == 0 || (fs->fs_metackhash & CK_INODE) == 0)
1148f829a5cSKirk McKusick 		return;
1158f829a5cSKirk McKusick 	/*
1168f829a5cSKirk McKusick 	 * Exclude old di_ckhash from the crc32 calculation, e.g., always use
1178f829a5cSKirk McKusick 	 * a check-hash value of zero when calculating the new check-hash.
1188f829a5cSKirk McKusick 	 */
1198f829a5cSKirk McKusick 	dip->di_ckhash = 0;
1208f829a5cSKirk McKusick 	dip->di_ckhash = calculate_crc32c(~0L, (void *)dip, sizeof(*dip));
1218f829a5cSKirk McKusick }
1228f829a5cSKirk McKusick 
1238f829a5cSKirk McKusick /*
124dffce215SKirk McKusick  * These are the low-level functions that actually read and write
125dffce215SKirk McKusick  * the superblock and its associated data.
126dffce215SKirk McKusick  */
127dffce215SKirk McKusick static off_t sblock_try[] = SBLOCKSEARCH;
128b21582eeSKirk McKusick static int readsuper(void *, struct fs **, off_t, int,
129dffce215SKirk McKusick 	int (*)(void *, off_t, void **, int));
130076002f2SKirk McKusick static int validate_sblock(struct fs *, int);
131dffce215SKirk McKusick 
132dffce215SKirk McKusick /*
133dffce215SKirk McKusick  * Read a superblock from the devfd device.
134dffce215SKirk McKusick  *
135dffce215SKirk McKusick  * If an alternate superblock is specified, it is read. Otherwise the
136dffce215SKirk McKusick  * set of locations given in the SBLOCKSEARCH list is searched for a
137dffce215SKirk McKusick  * superblock. Memory is allocated for the superblock by the readfunc and
138dffce215SKirk McKusick  * is returned. If filltype is non-NULL, additional memory is allocated
139dffce215SKirk McKusick  * of type filltype and filled in with the superblock summary information.
140efbf3964SKirk McKusick  * All memory is freed when any error is returned.
141dffce215SKirk McKusick  *
142dffce215SKirk McKusick  * If a superblock is found, zero is returned. Otherwise one of the
143dffce215SKirk McKusick  * following error values is returned:
144dffce215SKirk McKusick  *     EIO: non-existent or truncated superblock.
145dffce215SKirk McKusick  *     EIO: error reading summary information.
146dffce215SKirk McKusick  *     ENOENT: no usable known superblock found.
147076002f2SKirk McKusick  *     ENOMEM: failed to allocate space for the superblock.
148dffce215SKirk McKusick  *     EINVAL: The previous newfs operation on this volume did not complete.
149dffce215SKirk McKusick  *         The administrator must complete newfs before using this volume.
150dffce215SKirk McKusick  */
151dffce215SKirk McKusick int
152b21582eeSKirk McKusick ffs_sbget(void *devfd, struct fs **fsp, off_t sblock, int flags,
153dffce215SKirk McKusick     struct malloc_type *filltype,
154dffce215SKirk McKusick     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
155dffce215SKirk McKusick {
156dffce215SKirk McKusick 	struct fs *fs;
15734816cb9SKirk McKusick 	struct fs_summary_info *fs_si;
158076002f2SKirk McKusick 	int i, error;
159076002f2SKirk McKusick 	uint64_t size, blks;
160dffce215SKirk McKusick 	uint8_t *space;
161dffce215SKirk McKusick 	int32_t *lp;
162dffce215SKirk McKusick 	char *buf;
163dffce215SKirk McKusick 
164efbf3964SKirk McKusick 	fs = NULL;
16516759360SMark Johnston 	*fsp = NULL;
166b21582eeSKirk McKusick 	if (sblock != UFS_STDSB) {
167b21582eeSKirk McKusick 		if ((error = readsuper(devfd, &fs, sblock,
168b21582eeSKirk McKusick 		    flags | UFS_ALTSBLK, readfunc)) != 0) {
169efbf3964SKirk McKusick 			if (fs != NULL)
170efbf3964SKirk McKusick 				UFS_FREE(fs, filltype);
1714cbd996aSKirk McKusick 			return (error);
172efbf3964SKirk McKusick 		}
173dffce215SKirk McKusick 	} else {
174dffce215SKirk McKusick 		for (i = 0; sblock_try[i] != -1; i++) {
175b21582eeSKirk McKusick 			if ((error = readsuper(devfd, &fs, sblock_try[i],
176b21582eeSKirk McKusick 			     flags, readfunc)) == 0) {
177b21582eeSKirk McKusick 				if ((flags & UFS_NOCSUM) != 0) {
178b21582eeSKirk McKusick 					*fsp = fs;
179b21582eeSKirk McKusick 					return (0);
180b21582eeSKirk McKusick 				}
181dffce215SKirk McKusick 				break;
182b21582eeSKirk McKusick 			}
183efbf3964SKirk McKusick 			if (fs != NULL) {
184efbf3964SKirk McKusick 				UFS_FREE(fs, filltype);
185efbf3964SKirk McKusick 				fs = NULL;
186efbf3964SKirk McKusick 			}
1874cbd996aSKirk McKusick 			if (error == ENOENT)
188dffce215SKirk McKusick 				continue;
1894cbd996aSKirk McKusick 			return (error);
190dffce215SKirk McKusick 		}
191dffce215SKirk McKusick 		if (sblock_try[i] == -1)
192dffce215SKirk McKusick 			return (ENOENT);
193dffce215SKirk McKusick 	}
194dffce215SKirk McKusick 	/*
195dffce215SKirk McKusick 	 * Read in the superblock summary information.
196dffce215SKirk McKusick 	 */
197dffce215SKirk McKusick 	size = fs->fs_cssize;
198dffce215SKirk McKusick 	blks = howmany(size, fs->fs_fsize);
199dffce215SKirk McKusick 	if (fs->fs_contigsumsize > 0)
200dffce215SKirk McKusick 		size += fs->fs_ncg * sizeof(int32_t);
201dffce215SKirk McKusick 	size += fs->fs_ncg * sizeof(u_int8_t);
202076002f2SKirk McKusick 	if ((fs_si = UFS_MALLOC(sizeof(*fs_si), filltype, M_NOWAIT)) == NULL) {
20334816cb9SKirk McKusick 		UFS_FREE(fs, filltype);
204076002f2SKirk McKusick 		return (ENOMEM);
20534816cb9SKirk McKusick 	}
20634816cb9SKirk McKusick 	bzero(fs_si, sizeof(*fs_si));
20734816cb9SKirk McKusick 	fs->fs_si = fs_si;
208076002f2SKirk McKusick 	if ((space = UFS_MALLOC(size, filltype, M_NOWAIT)) == NULL) {
20934816cb9SKirk McKusick 		UFS_FREE(fs->fs_si, filltype);
210efbf3964SKirk McKusick 		UFS_FREE(fs, filltype);
211076002f2SKirk McKusick 		return (ENOMEM);
212efbf3964SKirk McKusick 	}
213dffce215SKirk McKusick 	fs->fs_csp = (struct csum *)space;
214dffce215SKirk McKusick 	for (i = 0; i < blks; i += fs->fs_frag) {
215dffce215SKirk McKusick 		size = fs->fs_bsize;
216dffce215SKirk McKusick 		if (i + fs->fs_frag > blks)
217dffce215SKirk McKusick 			size = (blks - i) * fs->fs_fsize;
21816759360SMark Johnston 		buf = NULL;
2194cbd996aSKirk McKusick 		error = (*readfunc)(devfd,
220dffce215SKirk McKusick 		    dbtob(fsbtodb(fs, fs->fs_csaddr + i)), (void **)&buf, size);
2214cbd996aSKirk McKusick 		if (error) {
222efbf3964SKirk McKusick 			if (buf != NULL)
22316759360SMark Johnston 				UFS_FREE(buf, filltype);
224dffce215SKirk McKusick 			UFS_FREE(fs->fs_csp, filltype);
22534816cb9SKirk McKusick 			UFS_FREE(fs->fs_si, filltype);
226efbf3964SKirk McKusick 			UFS_FREE(fs, filltype);
2274cbd996aSKirk McKusick 			return (error);
228dffce215SKirk McKusick 		}
229dffce215SKirk McKusick 		memcpy(space, buf, size);
230dffce215SKirk McKusick 		UFS_FREE(buf, filltype);
231dffce215SKirk McKusick 		space += size;
232dffce215SKirk McKusick 	}
233dffce215SKirk McKusick 	if (fs->fs_contigsumsize > 0) {
234dffce215SKirk McKusick 		fs->fs_maxcluster = lp = (int32_t *)space;
235dffce215SKirk McKusick 		for (i = 0; i < fs->fs_ncg; i++)
236dffce215SKirk McKusick 			*lp++ = fs->fs_contigsumsize;
237dffce215SKirk McKusick 		space = (uint8_t *)lp;
238dffce215SKirk McKusick 	}
239dffce215SKirk McKusick 	size = fs->fs_ncg * sizeof(u_int8_t);
240dffce215SKirk McKusick 	fs->fs_contigdirs = (u_int8_t *)space;
241dffce215SKirk McKusick 	bzero(fs->fs_contigdirs, size);
242efbf3964SKirk McKusick 	*fsp = fs;
243dffce215SKirk McKusick 	return (0);
244dffce215SKirk McKusick }
245dffce215SKirk McKusick 
246dffce215SKirk McKusick /*
247dffce215SKirk McKusick  * Try to read a superblock from the location specified by sblockloc.
248dffce215SKirk McKusick  * Return zero on success or an errno on failure.
249dffce215SKirk McKusick  */
250dffce215SKirk McKusick static int
251b21582eeSKirk McKusick readsuper(void *devfd, struct fs **fsp, off_t sblockloc, int flags,
252b21582eeSKirk McKusick     int (*readfunc)(void *devfd, off_t loc, void **bufp, int size))
253dffce215SKirk McKusick {
254dffce215SKirk McKusick 	struct fs *fs;
255ec888383SKirk McKusick 	int error, res;
256ec888383SKirk McKusick 	uint32_t ckhash;
257dffce215SKirk McKusick 
258dffce215SKirk McKusick 	error = (*readfunc)(devfd, sblockloc, (void **)fsp, SBLOCKSIZE);
259dffce215SKirk McKusick 	if (error != 0)
260dffce215SKirk McKusick 		return (error);
261dffce215SKirk McKusick 	fs = *fsp;
262dffce215SKirk McKusick 	if (fs->fs_magic == FS_BAD_MAGIC)
263dffce215SKirk McKusick 		return (EINVAL);
26436e08b01SKirk McKusick 	/*
26536e08b01SKirk McKusick 	 * For UFS1 with a 65536 block size, the first backup superblock
26636e08b01SKirk McKusick 	 * is at the same location as the UFS2 superblock. Since SBLOCK_UFS2
26736e08b01SKirk McKusick 	 * is the first location checked, the first backup is the superblock
26836e08b01SKirk McKusick 	 * that will be accessed. Here we fail the lookup so that we can
26936e08b01SKirk McKusick 	 * retry with the correct location for the UFS1 superblock.
27036e08b01SKirk McKusick 	 */
271b21582eeSKirk McKusick 	if (fs->fs_magic == FS_UFS1_MAGIC && (flags & UFS_ALTSBLK) == 0 &&
27236e08b01SKirk McKusick 	    fs->fs_bsize == SBLOCK_UFS2 && sblockloc == SBLOCK_UFS2)
27336e08b01SKirk McKusick 		return (ENOENT);
274b21582eeSKirk McKusick 	if ((error = validate_sblock(fs, flags)) > 0)
275076002f2SKirk McKusick 		return (error);
276a02bd3e3SKirk McKusick 	/*
277a02bd3e3SKirk McKusick 	 * If the filesystem has been run on a kernel without
278a02bd3e3SKirk McKusick 	 * metadata check hashes, disable them.
279a02bd3e3SKirk McKusick 	 */
280a02bd3e3SKirk McKusick 	if ((fs->fs_flags & FS_METACKHASH) == 0)
281a02bd3e3SKirk McKusick 		fs->fs_metackhash = 0;
282996d40f9SKirk McKusick 	/*
283996d40f9SKirk McKusick 	 * Clear any check-hashes that are not maintained
284996d40f9SKirk McKusick 	 * by this kernel. Also clear any unsupported flags.
285996d40f9SKirk McKusick 	 */
286996d40f9SKirk McKusick 	fs->fs_metackhash &= CK_SUPPORTED;
287996d40f9SKirk McKusick 	fs->fs_flags &= FS_SUPPORTED;
288ade67b50SKirk McKusick 	if (fs->fs_ckhash != (ckhash = ffs_calc_sbhash(fs))) {
289b21582eeSKirk McKusick 		if ((flags & (UFS_NOMSG | UFS_NOHASHFAIL)) ==
290b21582eeSKirk McKusick 		    (UFS_NOMSG | UFS_NOHASHFAIL))
291f2b39152SKirk McKusick 			return (0);
292b21582eeSKirk McKusick 		if ((flags & UFS_NOMSG) != 0)
293b21582eeSKirk McKusick 			return (EINTEGRITY);
294ec888383SKirk McKusick #ifdef _KERNEL
295ec888383SKirk McKusick 		res = uprintf("Superblock check-hash failed: recorded "
296fb14e73cSKirk McKusick 		    "check-hash 0x%x != computed check-hash 0x%x%s\n",
297fb14e73cSKirk McKusick 		    fs->fs_ckhash, ckhash,
298b21582eeSKirk McKusick 		    (flags & UFS_NOHASHFAIL) != 0 ? " (Ignored)" : "");
299ec888383SKirk McKusick #else
300ec888383SKirk McKusick 		res = 0;
301ec888383SKirk McKusick #endif
302ec888383SKirk McKusick 		/*
303ec888383SKirk McKusick 		 * Print check-hash failure if no controlling terminal
304ec888383SKirk McKusick 		 * in kernel or always if in user-mode (libufs).
305ec888383SKirk McKusick 		 */
306ec888383SKirk McKusick 		if (res == 0)
307ec888383SKirk McKusick 			printf("Superblock check-hash failed: recorded "
308ec888383SKirk McKusick 			    "check-hash 0x%x != computed check-hash "
309fb14e73cSKirk McKusick 			    "0x%x%s\n", fs->fs_ckhash, ckhash,
310b21582eeSKirk McKusick 			    (flags & UFS_NOHASHFAIL) ? " (Ignored)" : "");
311b21582eeSKirk McKusick 		if ((flags & UFS_NOHASHFAIL) != 0)
312fb14e73cSKirk McKusick 			return (0);
313b21582eeSKirk McKusick 		return (EINTEGRITY);
314fb14e73cSKirk McKusick 	}
315dffce215SKirk McKusick 	/* Have to set for old filesystems that predate this field */
316dffce215SKirk McKusick 	fs->fs_sblockactualloc = sblockloc;
317efbf3964SKirk McKusick 	/* Not yet any summary information */
31834816cb9SKirk McKusick 	fs->fs_si = NULL;
319dffce215SKirk McKusick 	return (0);
320dffce215SKirk McKusick }
321dffce215SKirk McKusick 
322dffce215SKirk McKusick /*
323076002f2SKirk McKusick  * Verify the filesystem values.
324076002f2SKirk McKusick  */
325076002f2SKirk McKusick #define ILOG2(num)	(fls(num) - 1)
326b21582eeSKirk McKusick #define MPRINT		if (prtmsg) printf
327ce6296caSKirk McKusick #undef CHK
32850dc4c7dSKirk McKusick #define CHK(lhs, op, rhs, fmt)						\
32950dc4c7dSKirk McKusick 	if (lhs op rhs) {						\
330b21582eeSKirk McKusick 		MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s ("	\
33150dc4c7dSKirk McKusick 		    #fmt ")\n", fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2,	\
33250dc4c7dSKirk McKusick 		    #lhs, (intmax_t)lhs, #op, #rhs, (intmax_t)rhs);	\
333b21582eeSKirk McKusick 		if (error == 0)						\
334b21582eeSKirk McKusick 			error = ENOENT;					\
33550dc4c7dSKirk McKusick 	}
33650dc4c7dSKirk McKusick #define CHK2(lhs1, op1, rhs1, lhs2, op2, rhs2, fmt)			\
33750dc4c7dSKirk McKusick 	if (lhs1 op1 rhs1 && lhs2 op2 rhs2) {				\
338b21582eeSKirk McKusick 		MPRINT("UFS%d superblock failed: %s (" #fmt ") %s %s ("	\
33950dc4c7dSKirk McKusick 		    #fmt ") && %s (" #fmt ") %s %s (" #fmt ")\n",	\
34050dc4c7dSKirk McKusick 		    fs->fs_magic == FS_UFS1_MAGIC ? 1 : 2, #lhs1, 	\
34150dc4c7dSKirk McKusick 		    (intmax_t)lhs1, #op1, #rhs1, (intmax_t)rhs1, #lhs2,	\
34250dc4c7dSKirk McKusick 		    (intmax_t)lhs2, #op2, #rhs2, (intmax_t)rhs2);	\
343b21582eeSKirk McKusick 		if (error == 0)						\
344b21582eeSKirk McKusick 			error = ENOENT;					\
34550dc4c7dSKirk McKusick 	}
346076002f2SKirk McKusick 
347076002f2SKirk McKusick static int
348b21582eeSKirk McKusick validate_sblock(struct fs *fs, int flags)
349076002f2SKirk McKusick {
350548045bfSKirk McKusick 	u_long i, sectorsize;
351f3f5368dSKirk McKusick 	u_int64_t maxfilesize, sizepb;
352b21582eeSKirk McKusick 	int error, prtmsg;
353076002f2SKirk McKusick 
354b21582eeSKirk McKusick 	error = 0;
355076002f2SKirk McKusick 	sectorsize = dbtob(1);
356b21582eeSKirk McKusick 	prtmsg = ((flags & UFS_NOMSG) == 0);
357076002f2SKirk McKusick 	if (fs->fs_magic == FS_UFS2_MAGIC) {
358b21582eeSKirk McKusick 		if ((flags & UFS_ALTSBLK) == 0) {
35950dc4c7dSKirk McKusick 			CHK(fs->fs_sblockloc, !=, SBLOCK_UFS2, %#jx);
36050dc4c7dSKirk McKusick 			CHK2(fs->fs_sblockactualloc, !=, SBLOCK_UFS2,
36150dc4c7dSKirk McKusick 			    fs->fs_sblockactualloc, !=, 0, %jd);
36250dc4c7dSKirk McKusick 		}
36350dc4c7dSKirk McKusick 		CHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
36450dc4c7dSKirk McKusick 			sizeof(ufs2_daddr_t)), %jd);
36550dc4c7dSKirk McKusick 		CHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs2_daddr_t),
36650dc4c7dSKirk McKusick 		    %jd);
367548045bfSKirk McKusick 		CHK(fs->fs_inopb, <, 1, %jd);
36850dc4c7dSKirk McKusick 		CHK(fs->fs_inopb, !=, fs->fs_bsize / sizeof(struct ufs2_dinode),
36950dc4c7dSKirk McKusick 		    %jd);
370076002f2SKirk McKusick 	} else if (fs->fs_magic == FS_UFS1_MAGIC) {
371b21582eeSKirk McKusick 		if ((flags & UFS_ALTSBLK) == 0) {
3729e1f44d0SKirk McKusick 			CHK(fs->fs_sblockloc, >, SBLOCK_UFS1, %jd);
37336e08b01SKirk McKusick 			CHK(fs->fs_sblockactualloc, >, SBLOCK_UFS1, %jd);
3749e1f44d0SKirk McKusick 		}
37550dc4c7dSKirk McKusick 		CHK(fs->fs_nindir, !=, fs->fs_bsize / sizeof(ufs1_daddr_t),
37650dc4c7dSKirk McKusick 		    %jd);
377548045bfSKirk McKusick 		CHK(fs->fs_inopb, <, 1, %jd);
37850dc4c7dSKirk McKusick 		CHK(fs->fs_inopb, !=, fs->fs_bsize / sizeof(struct ufs1_dinode),
37950dc4c7dSKirk McKusick 		    %jd);
38050dc4c7dSKirk McKusick 		CHK(fs->fs_maxsymlinklen, !=, ((UFS_NDADDR + UFS_NIADDR) *
38150dc4c7dSKirk McKusick 			sizeof(ufs1_daddr_t)), %jd);
38250dc4c7dSKirk McKusick 		CHK(fs->fs_old_inodefmt, !=, FS_44INODEFMT, %jd);
38350dc4c7dSKirk McKusick 		CHK(fs->fs_old_cgoffset, !=, 0, %jd);
38450dc4c7dSKirk McKusick 		CHK(fs->fs_old_cgmask, !=, 0xffffffff, %#jx);
38550dc4c7dSKirk McKusick 		CHK(fs->fs_old_rotdelay, !=, 0, %jd);
38650dc4c7dSKirk McKusick 		CHK(fs->fs_old_rps, !=, 60, %jd);
38750dc4c7dSKirk McKusick 		CHK(fs->fs_old_nspf, !=, fs->fs_fsize / sectorsize, %jd);
38850dc4c7dSKirk McKusick 		CHK(fs->fs_old_cpg, !=, 1, %jd);
38950dc4c7dSKirk McKusick 		CHK(fs->fs_old_interleave, !=, 1, %jd);
39050dc4c7dSKirk McKusick 		CHK(fs->fs_old_trackskew, !=, 0, %jd);
39150dc4c7dSKirk McKusick 		CHK(fs->fs_old_cpc, !=, 0, %jd);
39250dc4c7dSKirk McKusick 		CHK(fs->fs_old_postblformat, !=, 1, %jd);
39350dc4c7dSKirk McKusick 		CHK(fs->fs_old_nrpos, !=, 1, %jd);
39450dc4c7dSKirk McKusick 		CHK(fs->fs_old_spc, !=, fs->fs_fpg * fs->fs_old_nspf, %jd);
39550dc4c7dSKirk McKusick 		CHK(fs->fs_old_nsect, !=, fs->fs_old_spc, %jd);
39650dc4c7dSKirk McKusick 		CHK(fs->fs_old_npsect, !=, fs->fs_old_spc, %jd);
39750dc4c7dSKirk McKusick 		CHK(fs->fs_old_ncyl, !=, fs->fs_ncg, %jd);
398076002f2SKirk McKusick 	} else {
39950dc4c7dSKirk McKusick 		/* Bad magic number, so assume not a superblock */
400076002f2SKirk McKusick 		return (ENOENT);
401076002f2SKirk McKusick 	}
40250dc4c7dSKirk McKusick 	CHK(fs->fs_bsize, <, MINBSIZE, %jd);
40350dc4c7dSKirk McKusick 	CHK(fs->fs_bsize, >, MAXBSIZE, %jd);
40450dc4c7dSKirk McKusick 	CHK(fs->fs_bsize, <, roundup(sizeof(struct fs), DEV_BSIZE), %jd);
40550dc4c7dSKirk McKusick 	CHK(powerof2(fs->fs_bsize), ==, 0, %jd);
406548045bfSKirk McKusick 	CHK(fs->fs_frag, <, 1, %jd);
407548045bfSKirk McKusick 	CHK(fs->fs_frag, >, MAXFRAG, %jd);
408548045bfSKirk McKusick 	CHK(fs->fs_frag, !=, numfrags(fs, fs->fs_bsize), %jd);
40950dc4c7dSKirk McKusick 	CHK(fs->fs_fsize, <, sectorsize, %jd);
410548045bfSKirk McKusick 	CHK(fs->fs_fsize * fs->fs_frag, !=, fs->fs_bsize, %jd);
41150dc4c7dSKirk McKusick 	CHK(powerof2(fs->fs_fsize), ==, 0, %jd);
412548045bfSKirk McKusick 	CHK(fs->fs_fpg, <, 3 * fs->fs_frag, %jd);
413548045bfSKirk McKusick 	CHK(fs->fs_ncg, <, 1, %jd);
414548045bfSKirk McKusick 	CHK(fs->fs_ipg, <, 1, %jd);
415548045bfSKirk McKusick 	CHK(fs->fs_ipg * fs->fs_ncg, >, (((int64_t)(1)) << 32) - INOPB(fs),
416548045bfSKirk McKusick 	    %jd);
417548045bfSKirk McKusick 	CHK(fs->fs_sblockloc, <, 0, %jd);
418548045bfSKirk McKusick 	CHK(fs->fs_sblockloc, >, fs->fs_fpg, %jd);
419548045bfSKirk McKusick 	CHK(fs->fs_sbsize, >, SBLOCKSIZE, %jd);
42050dc4c7dSKirk McKusick 	CHK(fs->fs_maxbsize, <, fs->fs_bsize, %jd);
42150dc4c7dSKirk McKusick 	CHK(powerof2(fs->fs_maxbsize), ==, 0, %jd);
42250dc4c7dSKirk McKusick 	CHK(fs->fs_maxbsize, >, FS_MAXCONTIG * fs->fs_bsize, %jd);
42350dc4c7dSKirk McKusick 	CHK(fs->fs_bmask, !=, ~(fs->fs_bsize - 1), %#jx);
42450dc4c7dSKirk McKusick 	CHK(fs->fs_fmask, !=, ~(fs->fs_fsize - 1), %#jx);
42550dc4c7dSKirk McKusick 	CHK(fs->fs_qbmask, !=, ~fs->fs_bmask, %#jx);
42650dc4c7dSKirk McKusick 	CHK(fs->fs_qfmask, !=, ~fs->fs_fmask, %#jx);
42750dc4c7dSKirk McKusick 	CHK(fs->fs_bshift, !=, ILOG2(fs->fs_bsize), %jd);
42850dc4c7dSKirk McKusick 	CHK(fs->fs_fshift, !=, ILOG2(fs->fs_fsize), %jd);
42950dc4c7dSKirk McKusick 	CHK(fs->fs_fragshift, !=, ILOG2(fs->fs_frag), %jd);
43050dc4c7dSKirk McKusick 	CHK(fs->fs_fsbtodb, !=, ILOG2(fs->fs_fsize / sectorsize), %jd);
43150dc4c7dSKirk McKusick 	CHK(fs->fs_sblkno, !=, roundup(
43250dc4c7dSKirk McKusick 	    howmany(fs->fs_sblockloc + SBLOCKSIZE, fs->fs_fsize),
43350dc4c7dSKirk McKusick 	    fs->fs_frag), %jd);
43450dc4c7dSKirk McKusick 	CHK(fs->fs_cblkno, !=, fs->fs_sblkno +
43550dc4c7dSKirk McKusick 	    roundup(howmany(SBLOCKSIZE, fs->fs_fsize), fs->fs_frag), %jd);
43650dc4c7dSKirk McKusick 	CHK(fs->fs_iblkno, !=, fs->fs_cblkno + fs->fs_frag, %jd);
43750dc4c7dSKirk McKusick 	CHK(fs->fs_dblkno, !=, fs->fs_iblkno + fs->fs_ipg / INOPF(fs), %jd);
43850dc4c7dSKirk McKusick 	CHK(fs->fs_cgsize, >, fs->fs_bsize, %jd);
4395bc926afSKirk McKusick 	/*
4405bc926afSKirk McKusick 	 * This test is valid, however older versions of growfs failed
4415bc926afSKirk McKusick 	 * to correctly update fs_dsize so will fail this test. Thus we
4425bc926afSKirk McKusick 	 * exclude it from the requirements.
4435bc926afSKirk McKusick 	 */
4445bc926afSKirk McKusick #ifdef notdef
44550dc4c7dSKirk McKusick 	CHK(fs->fs_dsize, !=, fs->fs_size - fs->fs_sblkno -
446076002f2SKirk McKusick 		fs->fs_ncg * (fs->fs_dblkno - fs->fs_sblkno) -
44750dc4c7dSKirk McKusick 		howmany(fs->fs_cssize, fs->fs_fsize), %jd);
4485bc926afSKirk McKusick #endif
44950dc4c7dSKirk McKusick 	CHK(fs->fs_metaspace, <, 0, %jd);
45050dc4c7dSKirk McKusick 	CHK(fs->fs_metaspace, >, fs->fs_fpg / 2, %jd);
45150dc4c7dSKirk McKusick 	CHK(fs->fs_minfree, >, 99, %jd%%);
452076002f2SKirk McKusick 	maxfilesize = fs->fs_bsize * UFS_NDADDR - 1;
453076002f2SKirk McKusick 	for (sizepb = fs->fs_bsize, i = 0; i < UFS_NIADDR; i++) {
454076002f2SKirk McKusick 		sizepb *= NINDIR(fs);
455076002f2SKirk McKusick 		maxfilesize += sizepb;
456076002f2SKirk McKusick 	}
45750dc4c7dSKirk McKusick 	CHK(fs->fs_maxfilesize, !=, maxfilesize, %jd);
458076002f2SKirk McKusick 	/*
459076002f2SKirk McKusick 	 * These values have a tight interaction with each other that
460076002f2SKirk McKusick 	 * makes it hard to tightly bound them. So we can only check
461076002f2SKirk McKusick 	 * that they are within a broader possible range.
462076002f2SKirk McKusick 	 *
463f3f5368dSKirk McKusick 	 * The size cannot always be accurately determined, but ensure
464f3f5368dSKirk McKusick 	 * that it is consistent with the number of cylinder groups (fs_ncg)
465f3f5368dSKirk McKusick 	 * and the number of fragments per cylinder group (fs_fpg). Ensure
466f3f5368dSKirk McKusick 	 * that the summary information size is correct and that it starts
467f3f5368dSKirk McKusick 	 * and ends in the data area of the same cylinder group.
468076002f2SKirk McKusick 	 */
46950dc4c7dSKirk McKusick 	CHK(fs->fs_size, <, 8 * fs->fs_frag, %jd);
47050dc4c7dSKirk McKusick 	CHK(fs->fs_size, <=, (fs->fs_ncg - 1) * fs->fs_fpg, %jd);
47150dc4c7dSKirk McKusick 	CHK(fs->fs_size, >, fs->fs_ncg * fs->fs_fpg, %jd);
472b21582eeSKirk McKusick 	/*
473b21582eeSKirk McKusick 	 * If we are not requested to read in the csum data stop here
474b21582eeSKirk McKusick 	 * as the correctness of the remaining values is only important
475b21582eeSKirk McKusick 	 * to bound the space needed to be allocated to hold the csum data.
476b21582eeSKirk McKusick 	 */
477b21582eeSKirk McKusick 	if ((flags & UFS_NOCSUM) != 0)
478b21582eeSKirk McKusick 		return (error);
479548045bfSKirk McKusick 	CHK(fs->fs_csaddr, <, 0, %jd);
480f3f5368dSKirk McKusick 	CHK(fs->fs_cssize, !=,
481f3f5368dSKirk McKusick 	    fragroundup(fs, fs->fs_ncg * sizeof(struct csum)), %jd);
482904347a0SKirk McKusick 	CHK(dtog(fs, fs->fs_csaddr), >, fs->fs_ncg, %jd);
483548045bfSKirk McKusick 	CHK(fs->fs_csaddr, <, cgdmin(fs, dtog(fs, fs->fs_csaddr)), %jd);
484f3f5368dSKirk McKusick 	CHK(dtog(fs, fs->fs_csaddr + howmany(fs->fs_cssize, fs->fs_fsize)), >,
485548045bfSKirk McKusick 	    dtog(fs, fs->fs_csaddr), %jd);
486076002f2SKirk McKusick 	/*
487bc218d89SKirk McKusick 	 * With file system clustering it is possible to allocate
488bc218d89SKirk McKusick 	 * many contiguous blocks. The kernel variable maxphys defines
489bc218d89SKirk McKusick 	 * the maximum transfer size permitted by the controller and/or
490bc218d89SKirk McKusick 	 * buffering. The fs_maxcontig parameter controls the maximum
491bc218d89SKirk McKusick 	 * number of blocks that the filesystem will read or write
492bc218d89SKirk McKusick 	 * in a single transfer. It is calculated when the filesystem
493bc218d89SKirk McKusick 	 * is created as maxphys / fs_bsize. The loader uses a maxphys
494bc218d89SKirk McKusick 	 * of 128K even when running on a system that supports larger
495bc218d89SKirk McKusick 	 * values. If the filesystem was built on a system that supports
496bc218d89SKirk McKusick 	 * a larger maxphys (1M is typical) it will have configured
497bc218d89SKirk McKusick 	 * fs_maxcontig for that larger system. So we bound the upper
498bc218d89SKirk McKusick 	 * allowable limit for fs_maxconfig to be able to at least
499bc218d89SKirk McKusick 	 * work with a 1M maxphys on the smallest block size filesystem:
500bc218d89SKirk McKusick 	 * 1M / 4096 == 256. There is no harm in allowing the mounting of
501bc218d89SKirk McKusick 	 * filesystems that make larger than maxphys I/O requests because
502bc218d89SKirk McKusick 	 * those (mostly 32-bit machines) can (very slowly) handle I/O
503bc218d89SKirk McKusick 	 * requests that exceed maxphys.
504076002f2SKirk McKusick 	 */
50550dc4c7dSKirk McKusick 	CHK(fs->fs_maxcontig, <, 0, %jd);
506f3f5368dSKirk McKusick 	CHK(fs->fs_maxcontig, >, MAX(256, maxphys / fs->fs_bsize), %jd);
50750dc4c7dSKirk McKusick 	CHK2(fs->fs_maxcontig, ==, 0, fs->fs_contigsumsize, !=, 0, %jd);
50850dc4c7dSKirk McKusick 	CHK2(fs->fs_maxcontig, >, 1, fs->fs_contigsumsize, !=,
50950dc4c7dSKirk McKusick 	    MIN(fs->fs_maxcontig, FS_MAXCONTIG), %jd);
510b21582eeSKirk McKusick 	return (error);
511076002f2SKirk McKusick }
512076002f2SKirk McKusick 
513076002f2SKirk McKusick /*
514dffce215SKirk McKusick  * Write a superblock to the devfd device from the memory pointed to by fs.
515dffce215SKirk McKusick  * Write out the superblock summary information if it is present.
516dffce215SKirk McKusick  *
517dffce215SKirk McKusick  * If the write is successful, zero is returned. Otherwise one of the
518dffce215SKirk McKusick  * following error values is returned:
519dffce215SKirk McKusick  *     EIO: failed to write superblock.
520dffce215SKirk McKusick  *     EIO: failed to write superblock summary information.
521dffce215SKirk McKusick  */
522dffce215SKirk McKusick int
523dffce215SKirk McKusick ffs_sbput(void *devfd, struct fs *fs, off_t loc,
524dffce215SKirk McKusick     int (*writefunc)(void *devfd, off_t loc, void *buf, int size))
525dffce215SKirk McKusick {
526dffce215SKirk McKusick 	int i, error, blks, size;
527dffce215SKirk McKusick 	uint8_t *space;
528dffce215SKirk McKusick 
529dffce215SKirk McKusick 	/*
530dffce215SKirk McKusick 	 * If there is summary information, write it first, so if there
531dffce215SKirk McKusick 	 * is an error, the superblock will not be marked as clean.
532dffce215SKirk McKusick 	 */
53334816cb9SKirk McKusick 	if (fs->fs_si != NULL && fs->fs_csp != NULL) {
534dffce215SKirk McKusick 		blks = howmany(fs->fs_cssize, fs->fs_fsize);
535dffce215SKirk McKusick 		space = (uint8_t *)fs->fs_csp;
536dffce215SKirk McKusick 		for (i = 0; i < blks; i += fs->fs_frag) {
537dffce215SKirk McKusick 			size = fs->fs_bsize;
538dffce215SKirk McKusick 			if (i + fs->fs_frag > blks)
539dffce215SKirk McKusick 				size = (blks - i) * fs->fs_fsize;
540dffce215SKirk McKusick 			if ((error = (*writefunc)(devfd,
541dffce215SKirk McKusick 			     dbtob(fsbtodb(fs, fs->fs_csaddr + i)),
542dffce215SKirk McKusick 			     space, size)) != 0)
543dffce215SKirk McKusick 				return (error);
544dffce215SKirk McKusick 			space += size;
545dffce215SKirk McKusick 		}
546dffce215SKirk McKusick 	}
547dffce215SKirk McKusick 	fs->fs_fmod = 0;
54893440bbeSKirk McKusick #ifndef _KERNEL
54993440bbeSKirk McKusick 	{
55093440bbeSKirk McKusick 		struct fs_summary_info *fs_si;
55193440bbeSKirk McKusick 
55293440bbeSKirk McKusick 		fs->fs_time = time(NULL);
55393440bbeSKirk McKusick 		/* Clear the pointers for the duration of writing. */
55493440bbeSKirk McKusick 		fs_si = fs->fs_si;
55593440bbeSKirk McKusick 		fs->fs_si = NULL;
556ade67b50SKirk McKusick 		fs->fs_ckhash = ffs_calc_sbhash(fs);
55793440bbeSKirk McKusick 		error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
55893440bbeSKirk McKusick 		fs->fs_si = fs_si;
55993440bbeSKirk McKusick 	}
56093440bbeSKirk McKusick #else /* _KERNEL */
56193440bbeSKirk McKusick 	fs->fs_time = time_second;
56293440bbeSKirk McKusick 	fs->fs_ckhash = ffs_calc_sbhash(fs);
56393440bbeSKirk McKusick 	error = (*writefunc)(devfd, loc, fs, fs->fs_sbsize);
56493440bbeSKirk McKusick #endif /* _KERNEL */
565dffce215SKirk McKusick 	return (error);
566dffce215SKirk McKusick }
567dffce215SKirk McKusick 
568dffce215SKirk McKusick /*
569ec888383SKirk McKusick  * Calculate the check-hash for a superblock.
570ec888383SKirk McKusick  */
571ade67b50SKirk McKusick uint32_t
572ade67b50SKirk McKusick ffs_calc_sbhash(struct fs *fs)
573ec888383SKirk McKusick {
574ec888383SKirk McKusick 	uint32_t ckhash, save_ckhash;
575ec888383SKirk McKusick 
576ec888383SKirk McKusick 	/*
577ec888383SKirk McKusick 	 * A filesystem that was using a superblock ckhash may be moved
578ec888383SKirk McKusick 	 * to an older kernel that does not support ckhashes. The
579ec888383SKirk McKusick 	 * older kernel will clear the FS_METACKHASH flag indicating
580ec888383SKirk McKusick 	 * that it does not update hashes. When the disk is moved back
581ec888383SKirk McKusick 	 * to a kernel capable of ckhashes it disables them on mount:
582ec888383SKirk McKusick 	 *
583ec888383SKirk McKusick 	 *	if ((fs->fs_flags & FS_METACKHASH) == 0)
584ec888383SKirk McKusick 	 *		fs->fs_metackhash = 0;
585ec888383SKirk McKusick 	 *
586ec888383SKirk McKusick 	 * This leaves (fs->fs_metackhash & CK_SUPERBLOCK) == 0) with an
587ec888383SKirk McKusick 	 * old stale value in the fs->fs_ckhash field. Thus the need to
588ec888383SKirk McKusick 	 * just accept what is there.
589ec888383SKirk McKusick 	 */
590ec888383SKirk McKusick 	if ((fs->fs_metackhash & CK_SUPERBLOCK) == 0)
591ec888383SKirk McKusick 		return (fs->fs_ckhash);
592ec888383SKirk McKusick 
593ec888383SKirk McKusick 	save_ckhash = fs->fs_ckhash;
594ec888383SKirk McKusick 	fs->fs_ckhash = 0;
595ec888383SKirk McKusick 	/*
596ec888383SKirk McKusick 	 * If newly read from disk, the caller is responsible for
597ec888383SKirk McKusick 	 * verifying that fs->fs_sbsize <= SBLOCKSIZE.
598ec888383SKirk McKusick 	 */
599ec888383SKirk McKusick 	ckhash = calculate_crc32c(~0L, (void *)fs, fs->fs_sbsize);
600ec888383SKirk McKusick 	fs->fs_ckhash = save_ckhash;
601ec888383SKirk McKusick 	return (ckhash);
602ec888383SKirk McKusick }
603ec888383SKirk McKusick 
604ec888383SKirk McKusick /*
605df8bae1dSRodney W. Grimes  * Update the frsum fields to reflect addition or deletion
606df8bae1dSRodney W. Grimes  * of some frags.
607df8bae1dSRodney W. Grimes  */
608df8bae1dSRodney W. Grimes void
60915c377c3SEd Maste ffs_fragacct(struct fs *fs, int fragmap, int32_t fraglist[], int cnt)
610df8bae1dSRodney W. Grimes {
611df8bae1dSRodney W. Grimes 	int inblk;
61205f4ff5dSPoul-Henning Kamp 	int field, subfield;
61305f4ff5dSPoul-Henning Kamp 	int siz, pos;
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes 	inblk = (int)(fragtbl[fs->fs_frag][fragmap]) << 1;
616df8bae1dSRodney W. Grimes 	fragmap <<= 1;
617df8bae1dSRodney W. Grimes 	for (siz = 1; siz < fs->fs_frag; siz++) {
618df8bae1dSRodney W. Grimes 		if ((inblk & (1 << (siz + (fs->fs_frag % NBBY)))) == 0)
619df8bae1dSRodney W. Grimes 			continue;
620df8bae1dSRodney W. Grimes 		field = around[siz];
621df8bae1dSRodney W. Grimes 		subfield = inside[siz];
622df8bae1dSRodney W. Grimes 		for (pos = siz; pos <= fs->fs_frag; pos++) {
623df8bae1dSRodney W. Grimes 			if ((fragmap & field) == subfield) {
624df8bae1dSRodney W. Grimes 				fraglist[siz] += cnt;
625df8bae1dSRodney W. Grimes 				pos += siz;
626df8bae1dSRodney W. Grimes 				field <<= siz;
627df8bae1dSRodney W. Grimes 				subfield <<= siz;
628df8bae1dSRodney W. Grimes 			}
629df8bae1dSRodney W. Grimes 			field <<= 1;
630df8bae1dSRodney W. Grimes 			subfield <<= 1;
631df8bae1dSRodney W. Grimes 		}
632df8bae1dSRodney W. Grimes 	}
633df8bae1dSRodney W. Grimes }
634df8bae1dSRodney W. Grimes 
635df8bae1dSRodney W. Grimes /*
636df8bae1dSRodney W. Grimes  * block operations
637df8bae1dSRodney W. Grimes  *
638df8bae1dSRodney W. Grimes  * check if a block is available
639df8bae1dSRodney W. Grimes  */
640df8bae1dSRodney W. Grimes int
64115c377c3SEd Maste ffs_isblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
642df8bae1dSRodney W. Grimes {
643df8bae1dSRodney W. Grimes 	unsigned char mask;
644df8bae1dSRodney W. Grimes 
645df8bae1dSRodney W. Grimes 	switch ((int)fs->fs_frag) {
646df8bae1dSRodney W. Grimes 	case 8:
647df8bae1dSRodney W. Grimes 		return (cp[h] == 0xff);
648df8bae1dSRodney W. Grimes 	case 4:
649df8bae1dSRodney W. Grimes 		mask = 0x0f << ((h & 0x1) << 2);
650df8bae1dSRodney W. Grimes 		return ((cp[h >> 1] & mask) == mask);
651df8bae1dSRodney W. Grimes 	case 2:
652df8bae1dSRodney W. Grimes 		mask = 0x03 << ((h & 0x3) << 1);
653df8bae1dSRodney W. Grimes 		return ((cp[h >> 2] & mask) == mask);
654df8bae1dSRodney W. Grimes 	case 1:
655df8bae1dSRodney W. Grimes 		mask = 0x01 << (h & 0x7);
656df8bae1dSRodney W. Grimes 		return ((cp[h >> 3] & mask) == mask);
657df8bae1dSRodney W. Grimes 	default:
658113db2ddSJeff Roberson #ifdef _KERNEL
659df8bae1dSRodney W. Grimes 		panic("ffs_isblock");
660113db2ddSJeff Roberson #endif
661113db2ddSJeff Roberson 		break;
662113db2ddSJeff Roberson 	}
663113db2ddSJeff Roberson 	return (0);
664113db2ddSJeff Roberson }
665113db2ddSJeff Roberson 
666113db2ddSJeff Roberson /*
667113db2ddSJeff Roberson  * check if a block is free
668113db2ddSJeff Roberson  */
669113db2ddSJeff Roberson int
67015c377c3SEd Maste ffs_isfreeblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
671113db2ddSJeff Roberson {
672113db2ddSJeff Roberson 
673113db2ddSJeff Roberson 	switch ((int)fs->fs_frag) {
674113db2ddSJeff Roberson 	case 8:
675113db2ddSJeff Roberson 		return (cp[h] == 0);
676113db2ddSJeff Roberson 	case 4:
677113db2ddSJeff Roberson 		return ((cp[h >> 1] & (0x0f << ((h & 0x1) << 2))) == 0);
678113db2ddSJeff Roberson 	case 2:
679113db2ddSJeff Roberson 		return ((cp[h >> 2] & (0x03 << ((h & 0x3) << 1))) == 0);
680113db2ddSJeff Roberson 	case 1:
681113db2ddSJeff Roberson 		return ((cp[h >> 3] & (0x01 << (h & 0x7))) == 0);
682113db2ddSJeff Roberson 	default:
683113db2ddSJeff Roberson #ifdef _KERNEL
684113db2ddSJeff Roberson 		panic("ffs_isfreeblock");
685113db2ddSJeff Roberson #endif
686113db2ddSJeff Roberson 		break;
687df8bae1dSRodney W. Grimes 	}
688589c7af9SKirk McKusick 	return (0);
689df8bae1dSRodney W. Grimes }
690df8bae1dSRodney W. Grimes 
691df8bae1dSRodney W. Grimes /*
692df8bae1dSRodney W. Grimes  * take a block out of the map
693df8bae1dSRodney W. Grimes  */
694df8bae1dSRodney W. Grimes void
69515c377c3SEd Maste ffs_clrblock(struct fs *fs, u_char *cp, ufs1_daddr_t h)
696df8bae1dSRodney W. Grimes {
697df8bae1dSRodney W. Grimes 
698df8bae1dSRodney W. Grimes 	switch ((int)fs->fs_frag) {
699df8bae1dSRodney W. Grimes 	case 8:
700df8bae1dSRodney W. Grimes 		cp[h] = 0;
701df8bae1dSRodney W. Grimes 		return;
702df8bae1dSRodney W. Grimes 	case 4:
703df8bae1dSRodney W. Grimes 		cp[h >> 1] &= ~(0x0f << ((h & 0x1) << 2));
704df8bae1dSRodney W. Grimes 		return;
705df8bae1dSRodney W. Grimes 	case 2:
706df8bae1dSRodney W. Grimes 		cp[h >> 2] &= ~(0x03 << ((h & 0x3) << 1));
707df8bae1dSRodney W. Grimes 		return;
708df8bae1dSRodney W. Grimes 	case 1:
709df8bae1dSRodney W. Grimes 		cp[h >> 3] &= ~(0x01 << (h & 0x7));
710df8bae1dSRodney W. Grimes 		return;
711df8bae1dSRodney W. Grimes 	default:
712113db2ddSJeff Roberson #ifdef _KERNEL
713df8bae1dSRodney W. Grimes 		panic("ffs_clrblock");
714113db2ddSJeff Roberson #endif
715113db2ddSJeff Roberson 		break;
716df8bae1dSRodney W. Grimes 	}
717df8bae1dSRodney W. Grimes }
718df8bae1dSRodney W. Grimes 
719df8bae1dSRodney W. Grimes /*
720df8bae1dSRodney W. Grimes  * put a block into the map
721df8bae1dSRodney W. Grimes  */
722df8bae1dSRodney W. Grimes void
72315c377c3SEd Maste ffs_setblock(struct fs *fs, unsigned char *cp, ufs1_daddr_t h)
724df8bae1dSRodney W. Grimes {
725df8bae1dSRodney W. Grimes 
726df8bae1dSRodney W. Grimes 	switch ((int)fs->fs_frag) {
727df8bae1dSRodney W. Grimes 	case 8:
728df8bae1dSRodney W. Grimes 		cp[h] = 0xff;
729df8bae1dSRodney W. Grimes 		return;
730df8bae1dSRodney W. Grimes 	case 4:
731df8bae1dSRodney W. Grimes 		cp[h >> 1] |= (0x0f << ((h & 0x1) << 2));
732df8bae1dSRodney W. Grimes 		return;
733df8bae1dSRodney W. Grimes 	case 2:
734df8bae1dSRodney W. Grimes 		cp[h >> 2] |= (0x03 << ((h & 0x3) << 1));
735df8bae1dSRodney W. Grimes 		return;
736df8bae1dSRodney W. Grimes 	case 1:
737df8bae1dSRodney W. Grimes 		cp[h >> 3] |= (0x01 << (h & 0x7));
738df8bae1dSRodney W. Grimes 		return;
739df8bae1dSRodney W. Grimes 	default:
740113db2ddSJeff Roberson #ifdef _KERNEL
741df8bae1dSRodney W. Grimes 		panic("ffs_setblock");
742113db2ddSJeff Roberson #endif
743113db2ddSJeff Roberson 		break;
744df8bae1dSRodney W. Grimes 	}
745df8bae1dSRodney W. Grimes }
746113db2ddSJeff Roberson 
747113db2ddSJeff Roberson /*
748113db2ddSJeff Roberson  * Update the cluster map because of an allocation or free.
749113db2ddSJeff Roberson  *
750113db2ddSJeff Roberson  * Cnt == 1 means free; cnt == -1 means allocating.
751113db2ddSJeff Roberson  */
752113db2ddSJeff Roberson void
75315c377c3SEd Maste ffs_clusteracct(struct fs *fs, struct cg *cgp, ufs1_daddr_t blkno, int cnt)
754113db2ddSJeff Roberson {
755113db2ddSJeff Roberson 	int32_t *sump;
756113db2ddSJeff Roberson 	int32_t *lp;
757113db2ddSJeff Roberson 	u_char *freemapp, *mapp;
758e1c27cf7SKirk McKusick 	int i, start, end, forw, back, map;
759e1c27cf7SKirk McKusick 	u_int bit;
760113db2ddSJeff Roberson 
761113db2ddSJeff Roberson 	if (fs->fs_contigsumsize <= 0)
762113db2ddSJeff Roberson 		return;
763113db2ddSJeff Roberson 	freemapp = cg_clustersfree(cgp);
764113db2ddSJeff Roberson 	sump = cg_clustersum(cgp);
765113db2ddSJeff Roberson 	/*
766113db2ddSJeff Roberson 	 * Allocate or clear the actual block.
767113db2ddSJeff Roberson 	 */
768113db2ddSJeff Roberson 	if (cnt > 0)
769113db2ddSJeff Roberson 		setbit(freemapp, blkno);
770113db2ddSJeff Roberson 	else
771113db2ddSJeff Roberson 		clrbit(freemapp, blkno);
772113db2ddSJeff Roberson 	/*
773113db2ddSJeff Roberson 	 * Find the size of the cluster going forward.
774113db2ddSJeff Roberson 	 */
775113db2ddSJeff Roberson 	start = blkno + 1;
776113db2ddSJeff Roberson 	end = start + fs->fs_contigsumsize;
777113db2ddSJeff Roberson 	if (end >= cgp->cg_nclusterblks)
778113db2ddSJeff Roberson 		end = cgp->cg_nclusterblks;
779113db2ddSJeff Roberson 	mapp = &freemapp[start / NBBY];
780113db2ddSJeff Roberson 	map = *mapp++;
781e1c27cf7SKirk McKusick 	bit = 1U << (start % NBBY);
782113db2ddSJeff Roberson 	for (i = start; i < end; i++) {
783113db2ddSJeff Roberson 		if ((map & bit) == 0)
784113db2ddSJeff Roberson 			break;
785113db2ddSJeff Roberson 		if ((i & (NBBY - 1)) != (NBBY - 1)) {
786113db2ddSJeff Roberson 			bit <<= 1;
787113db2ddSJeff Roberson 		} else {
788113db2ddSJeff Roberson 			map = *mapp++;
789113db2ddSJeff Roberson 			bit = 1;
790113db2ddSJeff Roberson 		}
791113db2ddSJeff Roberson 	}
792113db2ddSJeff Roberson 	forw = i - start;
793113db2ddSJeff Roberson 	/*
794113db2ddSJeff Roberson 	 * Find the size of the cluster going backward.
795113db2ddSJeff Roberson 	 */
796113db2ddSJeff Roberson 	start = blkno - 1;
797113db2ddSJeff Roberson 	end = start - fs->fs_contigsumsize;
798113db2ddSJeff Roberson 	if (end < 0)
799113db2ddSJeff Roberson 		end = -1;
800113db2ddSJeff Roberson 	mapp = &freemapp[start / NBBY];
801113db2ddSJeff Roberson 	map = *mapp--;
802e1c27cf7SKirk McKusick 	bit = 1U << (start % NBBY);
803113db2ddSJeff Roberson 	for (i = start; i > end; i--) {
804113db2ddSJeff Roberson 		if ((map & bit) == 0)
805113db2ddSJeff Roberson 			break;
806113db2ddSJeff Roberson 		if ((i & (NBBY - 1)) != 0) {
807113db2ddSJeff Roberson 			bit >>= 1;
808113db2ddSJeff Roberson 		} else {
809113db2ddSJeff Roberson 			map = *mapp--;
810e1c27cf7SKirk McKusick 			bit = 1U << (NBBY - 1);
811113db2ddSJeff Roberson 		}
812113db2ddSJeff Roberson 	}
813113db2ddSJeff Roberson 	back = start - i;
814113db2ddSJeff Roberson 	/*
815113db2ddSJeff Roberson 	 * Account for old cluster and the possibly new forward and
816113db2ddSJeff Roberson 	 * back clusters.
817113db2ddSJeff Roberson 	 */
818113db2ddSJeff Roberson 	i = back + forw + 1;
819113db2ddSJeff Roberson 	if (i > fs->fs_contigsumsize)
820113db2ddSJeff Roberson 		i = fs->fs_contigsumsize;
821113db2ddSJeff Roberson 	sump[i] += cnt;
822113db2ddSJeff Roberson 	if (back > 0)
823113db2ddSJeff Roberson 		sump[back] -= cnt;
824113db2ddSJeff Roberson 	if (forw > 0)
825113db2ddSJeff Roberson 		sump[forw] -= cnt;
826113db2ddSJeff Roberson 	/*
827113db2ddSJeff Roberson 	 * Update cluster summary information.
828113db2ddSJeff Roberson 	 */
829113db2ddSJeff Roberson 	lp = &sump[fs->fs_contigsumsize];
830113db2ddSJeff Roberson 	for (i = fs->fs_contigsumsize; i > 0; i--)
831113db2ddSJeff Roberson 		if (*lp-- > 0)
832113db2ddSJeff Roberson 			break;
833113db2ddSJeff Roberson 	fs->fs_maxcluster[cgp->cg_cgx] = i;
834113db2ddSJeff Roberson }
835